stuffing to stay above min_bitrate
[libav.git] / libavcodec / mpegvideo.c
CommitLineData
de6d9b64
FB
1/*
2 * The simplest mpeg encoder (well, it was the simplest!)
ff4ec49e 3 * Copyright (c) 2000,2001 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
cf8039b2 18 *
9dbf1ddd 19 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
de6d9b64 20 */
202ef8b8 21
983e3246
MN
22/**
23 * @file mpegvideo.c
24 * The simplest mpeg encoder (well, it was the simplest!).
25 */
26
f943e138 27#include <limits.h>
de6d9b64
FB
28#include "avcodec.h"
29#include "dsputil.h"
30#include "mpegvideo.h"
65e4c8c9 31#include "faandct.h"
de6d9b64 32
54329dd5
NK
33#ifdef USE_FASTMEMCPY
34#include "fastmemcpy.h"
35#endif
36
e4eadb4b
MN
37//#undef NDEBUG
38//#include <assert.h>
2ad1516a 39
7604246d 40#ifdef CONFIG_ENCODERS
21af69f7 41static void encode_picture(MpegEncContext *s, int picture_number);
7604246d 42#endif //CONFIG_ENCODERS
21af69f7
FB
43static void dct_unquantize_mpeg1_c(MpegEncContext *s,
44 DCTELEM *block, int n, int qscale);
9dbf1ddd
MN
45static void dct_unquantize_mpeg2_c(MpegEncContext *s,
46 DCTELEM *block, int n, int qscale);
21af69f7
FB
47static void dct_unquantize_h263_c(MpegEncContext *s,
48 DCTELEM *block, int n, int qscale);
0c1a9eda 49static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
7604246d 50#ifdef CONFIG_ENCODERS
d7e9533a 51static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
477ab036 52static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
7d1c3fc1 53static int sse_mb(MpegEncContext *s);
7604246d 54#endif //CONFIG_ENCODERS
3d9fccbf 55
2e7b4c84
IK
56#ifdef HAVE_XVMC
57extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
58extern void XVMC_field_end(MpegEncContext *s);
a579db0c 59extern void XVMC_decode_mb(MpegEncContext *s);
2e7b4c84
IK
60#endif
61
0c1a9eda 62void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
3d9fccbf 63
de6d9b64
FB
64
65/* enable all paranoid tests for rounding, overflows, etc... */
66//#define PARANOID
67
68//#define DEBUG
69
101bea5f 70
de6d9b64
FB
71/* for jpeg fast DCT */
72#define CONST_BITS 14
73
eb4b3dd3 74static const uint16_t aanscales[64] = {
de6d9b64
FB
75 /* precomputed values scaled up by 14 bits */
76 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
77 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
78 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
79 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
80 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
81 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
1457ab52
MN
82 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
83 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
de6d9b64
FB
84};
85
eb4b3dd3 86static const uint8_t h263_chroma_roundtab[16] = {
67725183 87// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
de6d9b64
FB
88 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
89};
90
332f9ac4
MN
91static const uint8_t ff_default_chroma_qscale_table[32]={
92// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
93 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
94};
95
7604246d 96#ifdef CONFIG_ENCODERS
30952237 97static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
0c1a9eda 98static uint8_t default_fcode_tab[MAX_MV*2+1];
45870f57 99
a33c7159
MN
100enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
101
6b56c616 102static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
0c1a9eda 103 const uint16_t *quant_matrix, int bias, int qmin, int qmax)
de6d9b64 104{
d7e9533a
MN
105 int qscale;
106
cc7ac888 107 for(qscale=qmin; qscale<=qmax; qscale++){
d7e9533a 108 int i;
6b56c616 109 if (dsp->fdct == ff_jpeg_fdct_islow
b4c3816c 110#ifdef FAAN_POSTSCALE
6b56c616 111 || dsp->fdct == ff_faandct
b4c3816c
MN
112#endif
113 ) {
28db7fce 114 for(i=0;i<64;i++) {
6b56c616 115 const int j= dsp->idct_permutation[i];
28db7fce
MN
116 /* 16 <= qscale * quant_matrix[i] <= 7905 */
117 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
118 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
119 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
120
0c1a9eda 121 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
28db7fce
MN
122 (qscale * quant_matrix[j]));
123 }
6b56c616 124 } else if (dsp->fdct == fdct_ifast
b4c3816c 125#ifndef FAAN_POSTSCALE
6b56c616 126 || dsp->fdct == ff_faandct
b4c3816c
MN
127#endif
128 ) {
d7e9533a 129 for(i=0;i<64;i++) {
6b56c616 130 const int j= dsp->idct_permutation[i];
d7e9533a
MN
131 /* 16 <= qscale * quant_matrix[i] <= 7905 */
132 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
133 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
134 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
135
0c1a9eda 136 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
d7e9533a
MN
137 (aanscales[i] * qscale * quant_matrix[j]));
138 }
139 } else {
140 for(i=0;i<64;i++) {
6b56c616 141 const int j= dsp->idct_permutation[i];
d7e9533a
MN
142 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
143 So 16 <= qscale * quant_matrix[i] <= 7905
144 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
145 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
146 */
0c1a9eda 147 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
477ab036 148// qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
642ccefb 149 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
d7e9533a 150
642ccefb
MN
151 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
152 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
d7e9533a 153 }
de6d9b64
FB
154 }
155 }
156}
158c7f05
MN
157
158static inline void update_qscale(MpegEncContext *s){
159 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
160 s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
161
162 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
163}
7604246d
WH
164#endif //CONFIG_ENCODERS
165
3d2e8cce 166void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
2ad1516a
MN
167 int i;
168 int end;
7801d21d
MN
169
170 st->scantable= src_scantable;
2ad1516a
MN
171
172 for(i=0; i<64; i++){
173 int j;
174 j = src_scantable[i];
3d2e8cce 175 st->permutated[i] = permutation[j];
05c4072b
MN
176#ifdef ARCH_POWERPC
177 st->inverse[j] = i;
178#endif
2ad1516a
MN
179 }
180
181 end=-1;
182 for(i=0; i<64; i++){
183 int j;
184 j = st->permutated[i];
185 if(j>end) end=j;
186 st->raster_end[i]= end;
187 }
188}
189
764ef400 190#ifdef CONFIG_ENCODERS
d6eb3c50
MN
191void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
192 int i;
193
194 if(matrix){
195 put_bits(pb, 1, 1);
196 for(i=0;i<64;i++) {
197 put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
198 }
199 }else
200 put_bits(pb, 1, 0);
201}
764ef400 202#endif //CONFIG_ENCODERS
d6eb3c50 203
defdfc9a
AB
204/* init common dct for both encoder and decoder */
205int DCT_common_init(MpegEncContext *s)
de6d9b64 206{
3bf43d42 207 s->dct_unquantize_h263 = dct_unquantize_h263_c;
9dbf1ddd
MN
208 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
209 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
b0368839 210
7604246d 211#ifdef CONFIG_ENCODERS
28db7fce 212 s->dct_quantize= dct_quantize_c;
b0368839 213#endif
21af69f7
FB
214
215#ifdef HAVE_MMX
e4eadb4b 216 MPV_common_init_mmx(s);
a9b3f630 217#endif
e0580f8c
FH
218#ifdef ARCH_ALPHA
219 MPV_common_init_axp(s);
220#endif
c7e07931
MO
221#ifdef HAVE_MLIB
222 MPV_common_init_mlib(s);
223#endif
5917d17c
LS
224#ifdef HAVE_MMI
225 MPV_common_init_mmi(s);
226#endif
676e200c 227#ifdef ARCH_ARMV4L
83f238cb 228 MPV_common_init_armv4l(s);
676e200c 229#endif
05c4072b
MN
230#ifdef ARCH_POWERPC
231 MPV_common_init_ppc(s);
232#endif
676e200c 233
7604246d 234#ifdef CONFIG_ENCODERS
3a87ac94
MN
235 s->fast_dct_quantize= s->dct_quantize;
236
477ab036
MN
237 if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
238 s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
239 }
240
7604246d
WH
241#endif //CONFIG_ENCODERS
242
2ad1516a
MN
243 /* load & permutate scantables
244 note: only wmv uses differnt ones
245 */
3d2e8cce
MN
246 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
247 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
248 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
249 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
d930ef19 250
dfb476cb
MN
251 s->picture_structure= PICT_FRAME;
252
defdfc9a
AB
253 return 0;
254}
255
6571e41d
MN
256static void copy_picture(Picture *dst, Picture *src){
257 *dst = *src;
258 dst->type= FF_BUFFER_TYPE_COPY;
259}
260
1e491e29 261/**
4e00e76b
MN
262 * allocates a Picture
263 * The pixels are allocated/set by calling get_buffer() if shared=0
1e491e29 264 */
4e00e76b 265static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
7bc9090a
MN
266 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
267 const int mb_array_size= s->mb_stride*s->mb_height;
b40cd4e0
MN
268 const int b8_array_size= s->b8_stride*s->mb_height*2;
269 const int b4_array_size= s->b4_stride*s->mb_height*4;
0da71265 270 int i;
7bc9090a 271
4e00e76b
MN
272 if(shared){
273 assert(pic->data[0]);
274 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
275 pic->type= FF_BUFFER_TYPE_SHARED;
276 }else{
277 int r;
278
279 assert(!pic->data[0]);
280
492cd3a9 281 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
4e00e76b
MN
282
283 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
9b879566 284 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
4e00e76b
MN
285 return -1;
286 }
287
288 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
9b879566 289 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
4e00e76b
MN
290 return -1;
291 }
292
293 if(pic->linesize[1] != pic->linesize[2]){
9b879566 294 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
4e00e76b
MN
295 return -1;
296 }
297
298 s->linesize = pic->linesize[0];
299 s->uvlinesize= pic->linesize[1];
1e491e29 300 }
4e00e76b
MN
301
302 if(pic->qscale_table==NULL){
303 if (s->encoding) {
7bc9090a
MN
304 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
305 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
306 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
4e00e76b 307 }
1e491e29 308
7bc9090a 309 CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
e4eadb4b 310 CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
7bc9090a
MN
311 CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(int))
312 pic->mb_type= pic->mb_type_base + s->mb_stride+1;
0da71265 313 if(s->out_format == FMT_H264){
0da71265 314 for(i=0; i<2; i++){
b40cd4e0
MN
315 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+1) * sizeof(uint16_t))
316 pic->motion_val[i]= pic->motion_val_base[i]+1;
317 CHECKED_ALLOCZ(pic->ref_index[i] , b8_array_size * sizeof(uint8_t))
318 }
5ea4b18d 319 pic->motion_subsample_log2= 2;
8d7ec294 320 }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&(FF_DEBUG_VIS_MV|FF_DEBUG_MV))){
b40cd4e0 321 for(i=0; i<2; i++){
8d7ec294 322 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+1) * sizeof(uint16_t)*2) //FIXME
b40cd4e0 323 pic->motion_val[i]= pic->motion_val_base[i]+1;
0da71265 324 }
5ea4b18d 325 pic->motion_subsample_log2= 3;
0da71265 326 }
7bc9090a 327 pic->qstride= s->mb_stride;
fa384dcc 328 CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
4e00e76b 329 }
0da71265 330
f943e138
MN
331 //it might be nicer if the application would keep track of these but it would require a API change
332 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
333 s->prev_pict_types[0]= s->pict_type;
334 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
335 pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
336
1e491e29
MN
337 return 0;
338fail: //for the CHECKED_ALLOCZ macro
339 return -1;
340}
341
4e00e76b
MN
342/**
343 * deallocates a picture
344 */
1e491e29
MN
345static void free_picture(MpegEncContext *s, Picture *pic){
346 int i;
4e00e76b
MN
347
348 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
492cd3a9 349 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
4e00e76b
MN
350 }
351
1e491e29
MN
352 av_freep(&pic->mb_var);
353 av_freep(&pic->mc_mb_var);
354 av_freep(&pic->mb_mean);
355 av_freep(&pic->mbskip_table);
356 av_freep(&pic->qscale_table);
0da71265 357 av_freep(&pic->mb_type_base);
fa384dcc 358 av_freep(&pic->pan_scan);
0da71265
MN
359 pic->mb_type= NULL;
360 for(i=0; i<2; i++){
b40cd4e0 361 av_freep(&pic->motion_val_base[i]);
0da71265
MN
362 av_freep(&pic->ref_index[i]);
363 }
d90cf87b
MN
364
365 if(pic->type == FF_BUFFER_TYPE_SHARED){
4e00e76b
MN
366 for(i=0; i<4; i++){
367 pic->base[i]=
368 pic->data[i]= NULL;
369 }
370 pic->type= 0;
1e491e29
MN
371 }
372}
373
defdfc9a
AB
374/* init common structure for both encoder and decoder */
375int MPV_common_init(MpegEncContext *s)
376{
7bc9090a 377 int y_size, c_size, yc_size, i, mb_array_size, x, y;
defdfc9a 378
b0368839 379 dsputil_init(&s->dsp, s->avctx);
defdfc9a 380 DCT_common_init(s);
eb4b3dd3 381
9fee1e23 382 s->flags= s->avctx->flags;
defdfc9a 383
1e491e29 384 s->mb_width = (s->width + 15) / 16;
de6d9b64 385 s->mb_height = (s->height + 15) / 16;
7bc9090a 386 s->mb_stride = s->mb_width + 1;
b40cd4e0
MN
387 s->b8_stride = s->mb_width*2 + 1;
388 s->b4_stride = s->mb_width*4 + 1;
7bc9090a 389 mb_array_size= s->mb_height * s->mb_stride;
eb4b3dd3 390
b5a093b3
MN
391 /* set default edge pos, will be overriden in decode_header if needed */
392 s->h_edge_pos= s->mb_width*16;
393 s->v_edge_pos= s->mb_height*16;
eb4b3dd3
ZK
394
395 s->mb_num = s->mb_width * s->mb_height;
7bc9090a
MN
396
397 s->block_wrap[0]=
398 s->block_wrap[1]=
399 s->block_wrap[2]=
400 s->block_wrap[3]= s->mb_width*2 + 2;
401 s->block_wrap[4]=
402 s->block_wrap[5]= s->mb_width + 2;
eb4b3dd3 403
332f9ac4
MN
404 s->y_dc_scale_table=
405 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
2be9f03a 406 s->chroma_qscale_table= ff_default_chroma_qscale_table;
db9b1b5e
FB
407 if (!s->encoding)
408 s->progressive_sequence= 1;
2be9f03a 409 s->progressive_frame= 1;
db9b1b5e 410
eb4b3dd3
ZK
411 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
412 c_size = (s->mb_width + 2) * (s->mb_height + 2);
413 yc_size = y_size + 2 * c_size;
414
202ef8b8 415 /* convert fourcc to upper case */
7004ffb3
MN
416 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
417 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
418 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
419 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
b5a093b3 420
541ae140
MN
421 s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
422 + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
423 + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
424 + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
425
f7b47594
MN
426 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
427 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
1e491e29 428
492cd3a9 429 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
1e491e29 430
7bc9090a
MN
431 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
432 for(y=0; y<s->mb_height; y++){
433 for(x=0; x<s->mb_width; x++){
434 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
435 }
436 }
437 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
438
37fbfd0a 439 if (s->encoding) {
7bc9090a 440 int mv_table_size= s->mb_stride * (s->mb_height+2) + 1;
9dbcbd92
MN
441
442 /* Allocate MV tables */
7bc9090a
MN
443 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
444 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
445 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
446 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
447 CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
448 CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
449 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
450 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
451 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
452 s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
453 s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
454 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
7f2fe444 455
1e491e29 456 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
1457ab52 457 CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
7f2fe444 458
1457ab52
MN
459 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
460 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
91029be7 461
7f2fe444
MN
462 if(s->codec_id==CODEC_ID_MPEG4){
463 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
464 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
465 }
3aa102be 466
6b460aa3
MN
467 if(s->msmpeg4_version){
468 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
469 }
3aa102be 470 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
7bc9090a
MN
471
472 /* Allocate MB type table */
473 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint8_t)) //needed for encoding
158c7f05
MN
474
475 CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
7e4995c3
MN
476
477 CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
478 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
642ccefb
MN
479 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
480 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
9d9e3172
MN
481 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
482 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
821cb11f
MN
483
484 if(s->avctx->noise_reduction){
485 CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
486 CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
487 }
37fbfd0a 488 }
45686da2 489 CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM))
4d2858de 490
b465449e
MN
491 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
492
7bc9090a 493 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
37fbfd0a 494
bea669e5 495 if(s->codec_id==CODEC_ID_MPEG4){
4d2858de 496 /* interlaced direct mode decoding tables */
7bc9090a
MN
497 CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t))
498 CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t))
de6d9b64 499 }
6e2d5f1a 500 if (s->out_format == FMT_H263) {
de6d9b64 501 /* ac values */
0c1a9eda 502 CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
de6d9b64
FB
503 s->ac_val[1] = s->ac_val[0] + y_size;
504 s->ac_val[2] = s->ac_val[1] + c_size;
505
506 /* cbp values */
7f2fe444 507 CHECKED_ALLOCZ(s->coded_block, y_size);
eec1c6b9
MN
508
509 /* divx501 bitstream reorder buffer */
7f2fe444 510 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
f01a1894 511
7f2fe444 512 /* cbp, ac_pred, pred_dir */
7bc9090a
MN
513 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
514 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
5b3438c6 515 }
8b32880c
MN
516
517 if (s->h263_pred || s->h263_plus || !s->encoding) {
518 /* dc values */
519 //MN: we need these for error resilience of intra-frames
0c1a9eda 520 CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
8b32880c
MN
521 s->dc_val[1] = s->dc_val[0] + y_size;
522 s->dc_val[2] = s->dc_val[1] + c_size;
523 for(i=0;i<yc_size;i++)
524 s->dc_val[0][i] = 1024;
525 }
526
7806197d 527 /* which mb is a intra block */
7bc9090a
MN
528 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
529 memset(s->mbintra_table, 1, mb_array_size);
7806197d 530
de6d9b64
FB
531 /* default structure is frame */
532 s->picture_structure = PICT_FRAME;
93a21abd 533
3bb4e23a 534 /* init macroblock skip table */
7bc9090a 535 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
4d2858de 536 //Note the +1 is for a quicker mpeg4 slice_end detection
f943e138 537 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
ba6802de 538
91029be7 539 s->block= s->blocks[0];
3bb4e23a 540
a579db0c
IK
541 for(i=0;i<12;i++){
542 s->pblocks[i] = (short *)(&s->block[i]);
543 }
544
d7425f59
MN
545 s->parse_context.state= -1;
546
de6d9b64
FB
547 s->context_initialized = 1;
548 return 0;
549 fail:
8257bf05 550 MPV_common_end(s);
de6d9b64
FB
551 return -1;
552}
553
7f2fe444
MN
554
555//extern int sads;
556
de6d9b64
FB
557/* init common structure for both encoder and decoder */
558void MPV_common_end(MpegEncContext *s)
559{
560 int i;
561
147e5200
MN
562 av_freep(&s->parse_context.buffer);
563 s->parse_context.buffer_size=0;
564
6000abfa 565 av_freep(&s->mb_type);
7bc9090a
MN
566 av_freep(&s->p_mv_table_base);
567 av_freep(&s->b_forw_mv_table_base);
568 av_freep(&s->b_back_mv_table_base);
569 av_freep(&s->b_bidir_forw_mv_table_base);
570 av_freep(&s->b_bidir_back_mv_table_base);
571 av_freep(&s->b_direct_mv_table_base);
572 s->p_mv_table= NULL;
573 s->b_forw_mv_table= NULL;
574 s->b_back_mv_table= NULL;
575 s->b_bidir_forw_mv_table= NULL;
576 s->b_bidir_back_mv_table= NULL;
577 s->b_direct_mv_table= NULL;
578
6000abfa
FB
579 av_freep(&s->dc_val[0]);
580 av_freep(&s->ac_val[0]);
581 av_freep(&s->coded_block);
582 av_freep(&s->mbintra_table);
7f2fe444
MN
583 av_freep(&s->cbp_table);
584 av_freep(&s->pred_dir_table);
1457ab52
MN
585 av_freep(&s->me.scratchpad);
586 av_freep(&s->me.map);
587 av_freep(&s->me.score_map);
7f2fe444 588
6000abfa 589 av_freep(&s->mbskip_table);
f943e138 590 av_freep(&s->prev_pict_types);
6000abfa 591 av_freep(&s->bitstream_buffer);
7f2fe444
MN
592 av_freep(&s->tex_pb_buffer);
593 av_freep(&s->pb2_buffer);
f7b47594 594 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
bea669e5
MN
595 av_freep(&s->field_mv_table);
596 av_freep(&s->field_select_table);
3aa102be 597 av_freep(&s->avctx->stats_out);
6b460aa3 598 av_freep(&s->ac_stats);
4d2858de 599 av_freep(&s->error_status_table);
7bc9090a 600 av_freep(&s->mb_index2xy);
158c7f05 601 av_freep(&s->lambda_table);
7e4995c3
MN
602 av_freep(&s->q_intra_matrix);
603 av_freep(&s->q_inter_matrix);
642ccefb
MN
604 av_freep(&s->q_intra_matrix16);
605 av_freep(&s->q_inter_matrix16);
45686da2 606 av_freep(&s->blocks);
9d9e3172
MN
607 av_freep(&s->input_picture);
608 av_freep(&s->reordered_input_picture);
821cb11f
MN
609 av_freep(&s->dct_error_sum);
610 av_freep(&s->dct_offset);
1e491e29 611
9b4b6e09
MN
612 if(s->picture){
613 for(i=0; i<MAX_PICTURE_COUNT; i++){
614 free_picture(s, &s->picture[i]);
615 }
de6d9b64 616 }
b465449e 617 av_freep(&s->picture);
d90cf87b 618 avcodec_default_free_buffers(s->avctx);
de6d9b64 619 s->context_initialized = 0;
431f2172
MN
620 s->last_picture_ptr=
621 s->next_picture_ptr=
622 s->current_picture_ptr= NULL;
de6d9b64
FB
623}
624
7604246d
WH
625#ifdef CONFIG_ENCODERS
626
de6d9b64
FB
627/* init video encoder */
628int MPV_encode_init(AVCodecContext *avctx)
629{
630 MpegEncContext *s = avctx->priv_data;
22ddd60b 631 int i, dummy;
b1e6b355 632 int chroma_h_shift, chroma_v_shift;
bc657ac3 633
36b58e85
RS
634 avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
635
de6d9b64 636 s->bit_rate = avctx->bit_rate;
9cdd6a24 637 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
de6d9b64
FB
638 s->width = avctx->width;
639 s->height = avctx->height;
7f2fe444 640 if(avctx->gop_size > 600){
9b879566 641 av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
7f2fe444
MN
642 avctx->gop_size=600;
643 }
de6d9b64 644 s->gop_size = avctx->gop_size;
9cdd6a24
MN
645 s->max_qdiff= avctx->max_qdiff;
646 s->qcompress= avctx->qcompress;
647 s->qblur= avctx->qblur;
477c35a9 648 s->avctx = avctx;
ba6802de 649 s->flags= avctx->flags;
9dbcbd92 650 s->max_b_frames= avctx->max_b_frames;
8b4c7dbc 651 s->b_frame_strategy= avctx->b_frame_strategy;
d7e9533a 652 s->codec_id= avctx->codec->id;
7f2fe444
MN
653 s->luma_elim_threshold = avctx->luma_elim_threshold;
654 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
655 s->strict_std_compliance= avctx->strict_std_compliance;
656 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
1457ab52 657 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
87f8cab4 658 s->mpeg_quant= avctx->mpeg_quant;
ba58dabc 659 s->rtp_mode= !!avctx->rtp_payload_size;
e4eadb4b 660
de6d9b64
FB
661 if (s->gop_size <= 1) {
662 s->intra_only = 1;
663 s->gop_size = 12;
664 } else {
665 s->intra_only = 0;
666 }
5e746b99 667
1457ab52 668 s->me_method = avctx->me_method;
5e746b99 669
e4986da9 670 /* Fixed QSCALE */
de6d9b64 671 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
37fbfd0a 672
c5d309f2 673 s->adaptive_quant= ( s->avctx->lumi_masking
5e746b99 674 || s->avctx->dark_masking
c5d309f2
MN
675 || s->avctx->temporal_cplx_masking
676 || s->avctx->spatial_cplx_masking
1f26c6f3
MN
677 || s->avctx->p_masking
678 || (s->flags&CODEC_FLAG_QP_RD))
c5d309f2 679 && !s->fixed_qscale;
fcb48651 680
332f9ac4
MN
681 s->obmc= (s->flags & CODEC_FLAG_OBMC);
682 s->loop_filter= (s->flags & CODEC_FLAG_LOOP_FILTER);
5e746b99 683
f7190f73
MN
684 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
685 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
9b879566 686 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
9acbbd16
MN
687 return -1;
688 }
689
f7190f73
MN
690 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
691 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n");
692 return -1;
693 }
694
332f9ac4
MN
695 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
696 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
697 return -1;
698 }
699
9acbbd16 700 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
9b879566 701 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
9acbbd16
MN
702 return -1;
703 }
704
705 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
9b879566 706 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
9acbbd16
MN
707 return -1;
708 }
709
029911d1 710 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
9b879566 711 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
9acbbd16
MN
712 return -1;
713 }
e4eadb4b 714
9acbbd16 715 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
9b879566 716 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
9acbbd16
MN
717 return -1;
718 }
e4eadb4b 719
f2f6134b 720 if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
9b879566 721 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
f2f6134b
MN
722 return -1;
723 }
724
dd3e415e
MN
725 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
726 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
727 return -1;
728 }
729
1984f635
MN
730 if(s->codec_id==CODEC_ID_MJPEG){
731 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
732 s->inter_quant_bias= 0;
029911d1 733 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
1984f635
MN
734 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
735 s->inter_quant_bias= 0;
736 }else{
737 s->intra_quant_bias=0;
738 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
739 }
740
741 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
742 s->intra_quant_bias= avctx->intra_quant_bias;
743 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
744 s->inter_quant_bias= avctx->inter_quant_bias;
b1e6b355
MN
745
746 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
747
22ddd60b
MN
748 av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
749 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
750
de6d9b64
FB
751 switch(avctx->codec->id) {
752 case CODEC_ID_MPEG1VIDEO:
753 s->out_format = FMT_MPEG1;
14bea432
MN
754 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
755 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
de6d9b64 756 break;
029911d1
MN
757 case CODEC_ID_MPEG2VIDEO:
758 s->out_format = FMT_MPEG1;
759 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
760 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
ba58dabc 761 s->rtp_mode= 1;
029911d1 762 break;
b1e6b355 763 case CODEC_ID_LJPEG:
de6d9b64
FB
764 case CODEC_ID_MJPEG:
765 s->out_format = FMT_MJPEG;
766 s->intra_only = 1; /* force intra only for jpeg */
37fbfd0a 767 s->mjpeg_write_tables = 1; /* write all tables */
a69b930c 768 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
b1e6b355
MN
769 s->mjpeg_vsample[0] = 1<<chroma_v_shift;
770 s->mjpeg_vsample[1] = 1;
37fbfd0a 771 s->mjpeg_vsample[2] = 1;
b1e6b355 772 s->mjpeg_hsample[0] = 1<<chroma_h_shift;
37fbfd0a
J
773 s->mjpeg_hsample[1] = 1;
774 s->mjpeg_hsample[2] = 1;
de6d9b64
FB
775 if (mjpeg_init(s) < 0)
776 return -1;
1ff662cc 777 avctx->delay=0;
4e00e76b 778 s->low_delay=1;
de6d9b64 779 break;
1d0d55da 780#ifdef CONFIG_RISKY
de6d9b64 781 case CODEC_ID_H263:
37fbfd0a 782 if (h263_get_picture_format(s->width, s->height) == 7) {
9b879566 783 av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
de6d9b64 784 return -1;
37fbfd0a 785 }
de6d9b64 786 s->out_format = FMT_H263;
332f9ac4 787 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
1ff662cc 788 avctx->delay=0;
4e00e76b 789 s->low_delay=1;
de6d9b64
FB
790 break;
791 case CODEC_ID_H263P:
792 s->out_format = FMT_H263;
793 s->h263_plus = 1;
21e59552 794 /* Fx */
332f9ac4 795 s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
21e59552 796 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
332f9ac4 797 s->modified_quant= s->h263_aic;
dba019da 798 s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
332f9ac4
MN
799 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
800 s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
801 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
ba58dabc 802 s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
332f9ac4 803
21e59552 804 /* /Fx */
544286b3 805 /* These are just to be sure */
1ff662cc 806 avctx->delay=0;
4e00e76b 807 s->low_delay=1;
de6d9b64 808 break;
d4f5d74a
GM
809 case CODEC_ID_FLV1:
810 s->out_format = FMT_H263;
811 s->h263_flv = 2; /* format = 1; 11-bit codes */
812 s->unrestricted_mv = 1;
813 s->rtp_mode=0; /* don't allow GOB */
814 avctx->delay=0;
815 s->low_delay=1;
816 break;
de6d9b64
FB
817 case CODEC_ID_RV10:
818 s->out_format = FMT_H263;
1ff662cc 819 avctx->delay=0;
4e00e76b 820 s->low_delay=1;
de6d9b64 821 break;
58f26ba9 822 case CODEC_ID_MPEG4:
de6d9b64
FB
823 s->out_format = FMT_H263;
824 s->h263_pred = 1;
825 s->unrestricted_mv = 1;
4e00e76b 826 s->low_delay= s->max_b_frames ? 0 : 1;
4d2858de 827 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
de6d9b64 828 break;
84afee34 829 case CODEC_ID_MSMPEG4V1:
de6d9b64
FB
830 s->out_format = FMT_H263;
831 s->h263_msmpeg4 = 1;
832 s->h263_pred = 1;
833 s->unrestricted_mv = 1;
84afee34 834 s->msmpeg4_version= 1;
1ff662cc 835 avctx->delay=0;
4e00e76b 836 s->low_delay=1;
84afee34
MN
837 break;
838 case CODEC_ID_MSMPEG4V2:
839 s->out_format = FMT_H263;
840 s->h263_msmpeg4 = 1;
841 s->h263_pred = 1;
842 s->unrestricted_mv = 1;
843 s->msmpeg4_version= 2;
1ff662cc 844 avctx->delay=0;
4e00e76b 845 s->low_delay=1;
84afee34
MN
846 break;
847 case CODEC_ID_MSMPEG4V3:
848 s->out_format = FMT_H263;
849 s->h263_msmpeg4 = 1;
850 s->h263_pred = 1;
851 s->unrestricted_mv = 1;
852 s->msmpeg4_version= 3;
1f9aea9b 853 s->flipflop_rounding=1;
1ff662cc 854 avctx->delay=0;
4e00e76b 855 s->low_delay=1;
de6d9b64 856 break;
f5957f3f
MN
857 case CODEC_ID_WMV1:
858 s->out_format = FMT_H263;
859 s->h263_msmpeg4 = 1;
860 s->h263_pred = 1;
861 s->unrestricted_mv = 1;
862 s->msmpeg4_version= 4;
1f9aea9b 863 s->flipflop_rounding=1;
f5957f3f 864 avctx->delay=0;
4e00e76b 865 s->low_delay=1;
f5957f3f
MN
866 break;
867 case CODEC_ID_WMV2:
868 s->out_format = FMT_H263;
869 s->h263_msmpeg4 = 1;
870 s->h263_pred = 1;
871 s->unrestricted_mv = 1;
872 s->msmpeg4_version= 5;
1f9aea9b 873 s->flipflop_rounding=1;
f5957f3f 874 avctx->delay=0;
4e00e76b 875 s->low_delay=1;
f5957f3f 876 break;
1d0d55da 877#endif
de6d9b64
FB
878 default:
879 return -1;
880 }
cf8039b2 881
45870f57
MN
882 { /* set up some save defaults, some codecs might override them later */
883 static int done=0;
884 if(!done){
885 int i;
886 done=1;
bbed9259 887
30952237
MN
888 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
889 memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
0c1a9eda 890 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
45870f57
MN
891
892 for(i=-16; i<16; i++){
893 default_fcode_tab[i + MAX_MV]= 1;
894 }
895 }
896 }
1457ab52 897 s->me.mv_penalty= default_mv_penalty;
45870f57 898 s->fcode_tab= default_fcode_tab;
a406617f 899
9d2a0355 900 /* dont use mv_penalty table for crap MV as it would be confused */
1457ab52
MN
901 //FIXME remove after fixing / removing old ME
902 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
9d2a0355 903
3bb4e23a
FB
904 s->encoding = 1;
905
de6d9b64
FB
906 /* init */
907 if (MPV_common_init(s) < 0)
908 return -1;
dd5e90cd
MN
909
910 if(s->modified_quant)
911 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
912 s->progressive_frame=
913 s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
de6d9b64 914
1457ab52
MN
915 ff_init_me(s);
916
bbed9259 917#ifdef CONFIG_ENCODERS
1d0d55da 918#ifdef CONFIG_RISKY
2ad1516a
MN
919 if (s->out_format == FMT_H263)
920 h263_encode_init(s);
2ad1516a
MN
921 if(s->msmpeg4_version)
922 ff_msmpeg4_encode_init(s);
bbed9259 923#endif
1d0d55da
MN
924 if (s->out_format == FMT_MPEG1)
925 ff_mpeg1_encode_init(s);
926#endif
2ad1516a 927
519c2b6d
FB
928 /* init default q matrix */
929 for(i=0;i<64;i++) {
b0368839 930 int j= s->dsp.idct_permutation[i];
1d0d55da 931#ifdef CONFIG_RISKY
87f8cab4 932 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
2ad1516a
MN
933 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
934 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
87f8cab4 935 }else if(s->out_format == FMT_H263){
2ad1516a
MN
936 s->intra_matrix[j] =
937 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
1d0d55da
MN
938 }else
939#endif
029911d1 940 { /* mpeg1/2 */
2ad1516a
MN
941 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
942 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
87f8cab4 943 }
d6eb3c50
MN
944 if(s->avctx->intra_matrix)
945 s->intra_matrix[j] = s->avctx->intra_matrix[i];
946 if(s->avctx->inter_matrix)
947 s->inter_matrix[j] = s->avctx->inter_matrix[i];
d7e9533a
MN
948 }
949
950 /* precompute matrix */
ef5b1b5a 951 /* for mjpeg, we do include qscale in the matrix */
d7e9533a 952 if (s->out_format != FMT_MJPEG) {
6b56c616 953 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
cc7ac888 954 s->intra_matrix, s->intra_quant_bias, 1, 31);
6b56c616 955 convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
cc7ac888 956 s->inter_matrix, s->inter_quant_bias, 1, 31);
519c2b6d
FB
957 }
958
8b4c7dbc
MN
959 if(ff_rate_control_init(s) < 0)
960 return -1;
de6d9b64
FB
961
962 s->picture_number = 0;
45870f57 963 s->picture_in_gop_number = 0;
de6d9b64
FB
964 s->fake_picture_number = 0;
965 /* motion detector init */
966 s->f_code = 1;
9dbcbd92 967 s->b_code = 1;
de6d9b64
FB
968
969 return 0;
970}
971
972int MPV_encode_end(AVCodecContext *avctx)
973{
974 MpegEncContext *s = avctx->priv_data;
975
976#ifdef STATS
977 print_stats();
978#endif
8b4c7dbc
MN
979
980 ff_rate_control_uninit(s);
981
de6d9b64
FB
982 MPV_common_end(s);
983 if (s->out_format == FMT_MJPEG)
984 mjpeg_close(s);
541ae140 985
22ddd60b 986 av_freep(&avctx->extradata);
37fbfd0a 987
de6d9b64
FB
988 return 0;
989}
990
7604246d
WH
991#endif //CONFIG_ENCODERS
992
1d0d55da
MN
993void init_rl(RLTable *rl)
994{
0c1a9eda
ZK
995 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
996 uint8_t index_run[MAX_RUN+1];
1d0d55da
MN
997 int last, run, level, start, end, i;
998
999 /* compute max_level[], max_run[] and index_run[] */
1000 for(last=0;last<2;last++) {
1001 if (last == 0) {
1002 start = 0;
1003 end = rl->last;
1004 } else {
1005 start = rl->last;
1006 end = rl->n;
1007 }
1008
1009 memset(max_level, 0, MAX_RUN + 1);
1010 memset(max_run, 0, MAX_LEVEL + 1);
1011 memset(index_run, rl->n, MAX_RUN + 1);
1012 for(i=start;i<end;i++) {
1013 run = rl->table_run[i];
1014 level = rl->table_level[i];
1015 if (index_run[run] == rl->n)
1016 index_run[run] = i;
1017 if (level > max_level[run])
1018 max_level[run] = level;
1019 if (run > max_run[level])
1020 max_run[level] = run;
1021 }
1022 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1023 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1024 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1025 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1026 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1027 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1028 }
1029}
1030
de6d9b64 1031/* draw the edges of width 'w' of an image of size width, height */
fd7db0fd 1032//FIXME check that this is ok for mpeg4 interlaced
0c1a9eda 1033static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
de6d9b64 1034{
0c1a9eda 1035 uint8_t *ptr, *last_line;
de6d9b64
FB
1036 int i;
1037
1038 last_line = buf + (height - 1) * wrap;
1039 for(i=0;i<w;i++) {
1040 /* top and bottom */
1041 memcpy(buf - (i + 1) * wrap, buf, width);
1042 memcpy(last_line + (i + 1) * wrap, last_line, width);
1043 }
1044 /* left and right */
1045 ptr = buf;
1046 for(i=0;i<height;i++) {
1047 memset(ptr - w, ptr[0], w);
1048 memset(ptr + width, ptr[width-1], w);
1049 ptr += wrap;
1050 }
1051 /* corners */
1052 for(i=0;i<w;i++) {
1053 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
1054 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
1055 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
1056 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
1057 }
1058}
1059
5f194811 1060int ff_find_unused_picture(MpegEncContext *s, int shared){
4e00e76b
MN
1061 int i;
1062
1063 if(shared){
1064 for(i=0; i<MAX_PICTURE_COUNT; i++){
5f194811 1065 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
4e00e76b
MN
1066 }
1067 }else{
1068 for(i=0; i<MAX_PICTURE_COUNT; i++){
5f194811 1069 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
4e00e76b
MN
1070 }
1071 for(i=0; i<MAX_PICTURE_COUNT; i++){
5f194811 1072 if(s->picture[i].data[0]==NULL) return i;
4e00e76b
MN
1073 }
1074 }
1075
5f194811
MN
1076 assert(0);
1077 return -1;
4e00e76b
MN
1078}
1079
821cb11f
MN
1080static void update_noise_reduction(MpegEncContext *s){
1081 int intra, i;
1082
1083 for(intra=0; intra<2; intra++){
1084 if(s->dct_count[intra] > (1<<16)){
1085 for(i=0; i<64; i++){
1086 s->dct_error_sum[intra][i] >>=1;
1087 }
1088 s->dct_count[intra] >>= 1;
1089 }
1090
1091 for(i=0; i<64; i++){
1092 s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
1093 }
1094 }
1095}
1096
5f194811
MN
1097/**
1098 * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
1099 */
d6db1c9c 1100int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
de6d9b64 1101{
4e00e76b 1102 int i;
492cd3a9 1103 AVFrame *pic;
425dddb7 1104 s->mb_skiped = 0;
0da71265 1105
8b82a956 1106 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
e20c4069 1107
1e491e29 1108 /* mark&release old frames */
59c673d5 1109 if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
b536d0aa 1110 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
1e491e29
MN
1111
1112 /* release forgotten pictures */
1113 /* if(mpeg124/h263) */
1114 if(!s->encoding){
1115 for(i=0; i<MAX_PICTURE_COUNT; i++){
b536d0aa 1116 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
9b879566 1117 av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
492cd3a9 1118 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
1e491e29
MN
1119 }
1120 }
d6db1c9c 1121 }
93a21abd 1122 }
aa388dba
MN
1123alloc:
1124 if(!s->encoding){
e20c4069
MN
1125 /* release non refernce frames */
1126 for(i=0; i<MAX_PICTURE_COUNT; i++){
1127 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1128 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1129 }
1130 }
1131
5f194811
MN
1132 if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
1133 pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
1134 else{
1135 i= ff_find_unused_picture(s, 0);
1136 pic= (AVFrame*)&s->picture[i];
1137 }
1138
0da71265 1139 pic->reference= s->pict_type != B_TYPE ? 3 : 0;
b536d0aa 1140
5f194811 1141 if(s->current_picture_ptr) //FIXME broken, we need a coded_picture_number in MpegEncContext
b536d0aa 1142 pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
1e491e29 1143
f23a68df
IK
1144 if( alloc_picture(s, (Picture*)pic, 0) < 0)
1145 return -1;
93a21abd 1146
5f194811 1147 s->current_picture_ptr= (Picture*)pic;
c70f1716 1148 s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
2be9f03a 1149 s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
1e491e29 1150 }
b7adc711 1151
9f2e61b6 1152 s->current_picture_ptr->pict_type= s->pict_type;
158c7f05
MN
1153// if(s->flags && CODEC_FLAG_QSCALE)
1154 // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
7bc9090a 1155 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
9f2e61b6 1156
6571e41d 1157 copy_picture(&s->current_picture, s->current_picture_ptr);
9f2e61b6 1158
8b82a956 1159 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1e491e29 1160 if (s->pict_type != B_TYPE) {
b536d0aa
MN
1161 s->last_picture_ptr= s->next_picture_ptr;
1162 s->next_picture_ptr= s->current_picture_ptr;
de6d9b64 1163 }
d90cf87b 1164
6571e41d
MN
1165 if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
1166 if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
aa388dba 1167
6571e41d 1168 if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
9b879566 1169 av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
ffba1dc0
MN
1170 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1171 goto alloc;
1172 }
1173
1174 assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
1175
b536d0aa
MN
1176 if(s->picture_structure!=PICT_FRAME){
1177 int i;
1178 for(i=0; i<4; i++){
1179 if(s->picture_structure == PICT_BOTTOM_FIELD){
1180 s->current_picture.data[i] += s->current_picture.linesize[i];
1181 }
1182 s->current_picture.linesize[i] *= 2;
1183 s->last_picture.linesize[i] *=2;
1184 s->next_picture.linesize[i] *=2;
1185 }
1186 }
0da71265 1187 }
1e491e29 1188
aa388dba
MN
1189 s->hurry_up= s->avctx->hurry_up;
1190 s->error_resilience= avctx->error_resilience;
1191
d930ef19
MN
1192 /* set dequantizer, we cant do it during init as it might change for mpeg4
1193 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
029911d1
MN
1194 if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO)
1195 s->dct_unquantize = s->dct_unquantize_mpeg2;
1196 else if(s->out_format == FMT_H263)
1197 s->dct_unquantize = s->dct_unquantize_h263;
1198 else
d930ef19 1199 s->dct_unquantize = s->dct_unquantize_mpeg1;
d6db1c9c 1200
821cb11f
MN
1201 if(s->dct_error_sum){
1202 assert(s->avctx->noise_reduction && s->encoding);
1203
1204 update_noise_reduction(s);
1205 }
1206
2e7b4c84
IK
1207#ifdef HAVE_XVMC
1208 if(s->avctx->xvmc_acceleration)
1209 return XVMC_field_start(s, avctx);
1210#endif
d6db1c9c 1211 return 0;
de6d9b64 1212}
21af69f7 1213
de6d9b64
FB
1214/* generic function for encode/decode called after a frame has been coded/decoded */
1215void MPV_frame_end(MpegEncContext *s)
1216{
1e491e29 1217 int i;
de6d9b64 1218 /* draw edge for correct motion prediction if outside */
2e7b4c84
IK
1219#ifdef HAVE_XVMC
1220//just to make sure that all data is rendered.
1221 if(s->avctx->xvmc_acceleration){
1222 XVMC_field_end(s);
1223 }else
1224#endif
a573cc27 1225 if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1e491e29
MN
1226 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
1227 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1228 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
de6d9b64 1229 }
5975626d 1230 emms_c();
8b4c7dbc 1231
3aa102be 1232 s->last_pict_type = s->pict_type;
8b4c7dbc
MN
1233 if(s->pict_type!=B_TYPE){
1234 s->last_non_b_pict_type= s->pict_type;
8b4c7dbc 1235 }
b536d0aa
MN
1236#if 0
1237 /* copy back current_picture variables */
1e491e29
MN
1238 for(i=0; i<MAX_PICTURE_COUNT; i++){
1239 if(s->picture[i].data[0] == s->current_picture.data[0]){
1240 s->picture[i]= s->current_picture;
1241 break;
1242 }
1243 }
1244 assert(i<MAX_PICTURE_COUNT);
b536d0aa 1245#endif
1e491e29 1246
e20c4069
MN
1247 if(s->encoding){
1248 /* release non refernce frames */
1249 for(i=0; i<MAX_PICTURE_COUNT; i++){
1250 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1251 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1252 }
1253 }
1e491e29 1254 }
b536d0aa
MN
1255 // clear copies, to avoid confusion
1256#if 0
1257 memset(&s->last_picture, 0, sizeof(Picture));
1258 memset(&s->next_picture, 0, sizeof(Picture));
1259 memset(&s->current_picture, 0, sizeof(Picture));
1260#endif
de6d9b64
FB
1261}
1262
7bc9090a 1263/**
db6e7795
MN
1264 * draws an line from (ex, ey) -> (sx, sy).
1265 * @param w width of the image
1266 * @param h height of the image
1267 * @param stride stride/linesize of the image
1268 * @param color color of the arrow
1269 */
1270static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1271 int t, x, y, f;
1272
1273 sx= clip(sx, 0, w-1);
1274 sy= clip(sy, 0, h-1);
1275 ex= clip(ex, 0, w-1);
1276 ey= clip(ey, 0, h-1);
1277
1278 buf[sy*stride + sx]+= color;
1279
1280 if(ABS(ex - sx) > ABS(ey - sy)){
1281 if(sx > ex){
1282 t=sx; sx=ex; ex=t;
1283 t=sy; sy=ey; ey=t;
1284 }
1285 buf+= sx + sy*stride;
1286 ex-= sx;
1287 f= ((ey-sy)<<16)/ex;
1288 for(x= 0; x <= ex; x++){
1289 y= ((x*f) + (1<<15))>>16;
1290 buf[y*stride + x]+= color;
1291 }
1292 }else{
1293 if(sy > ey){
1294 t=sx; sx=ex; ex=t;
1295 t=sy; sy=ey; ey=t;
1296 }
1297 buf+= sx + sy*stride;
1298 ey-= sy;
1299 if(ey) f= ((ex-sx)<<16)/ey;
1300 else f= 0;
1301 for(y= 0; y <= ey; y++){
1302 x= ((y*f) + (1<<15))>>16;
1303 buf[y*stride + x]+= color;
1304 }
1305 }
1306}
1307
1308/**
1309 * draws an arrow from (ex, ey) -> (sx, sy).
1310 * @param w width of the image
1311 * @param h height of the image
1312 * @param stride stride/linesize of the image
1313 * @param color color of the arrow
1314 */
1315static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1316 int dx,dy;
1317
1318 sx= clip(sx, -100, w+100);
1319 sy= clip(sy, -100, h+100);
1320 ex= clip(ex, -100, w+100);
1321 ey= clip(ey, -100, h+100);
1322
1323 dx= ex - sx;
1324 dy= ey - sy;
1325
1326 if(dx*dx + dy*dy > 3*3){
1327 int rx= dx + dy;
1328 int ry= -dx + dy;
1329 int length= ff_sqrt((rx*rx + ry*ry)<<8);
1330
1331 //FIXME subpixel accuracy
1332 rx= ROUNDED_DIV(rx*3<<4, length);
1333 ry= ROUNDED_DIV(ry*3<<4, length);
1334
1335 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1336 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1337 }
1338 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1339}
1340
1341/**
7bc9090a
MN
1342 * prints debuging info for the given picture.
1343 */
1344void ff_print_debug_info(MpegEncContext *s, Picture *pict){
1345
1346 if(!pict || !pict->mb_type) return;
1347
1348 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1349 int x,y;
1350
1351 for(y=0; y<s->mb_height; y++){
1352 for(x=0; x<s->mb_width; x++){
1353 if(s->avctx->debug&FF_DEBUG_SKIP){
1354 int count= s->mbskip_table[x + y*s->mb_stride];
1355 if(count>9) count=9;
9b879566 1356 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
7bc9090a
MN
1357 }
1358 if(s->avctx->debug&FF_DEBUG_QP){
9b879566 1359 av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
7bc9090a
MN
1360 }
1361 if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1362 int mb_type= pict->mb_type[x + y*s->mb_stride];
1363
1364 //Type & MV direction
1365 if(IS_PCM(mb_type))
9b879566 1366 av_log(s->avctx, AV_LOG_DEBUG, "P");
7bc9090a 1367 else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
9b879566 1368 av_log(s->avctx, AV_LOG_DEBUG, "A");
7bc9090a 1369 else if(IS_INTRA4x4(mb_type))
9b879566 1370 av_log(s->avctx, AV_LOG_DEBUG, "i");
7bc9090a 1371 else if(IS_INTRA16x16(mb_type))
9b879566 1372 av_log(s->avctx, AV_LOG_DEBUG, "I");
7bc9090a 1373 else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
9b879566 1374 av_log(s->avctx, AV_LOG_DEBUG, "d");
7bc9090a 1375 else if(IS_DIRECT(mb_type))
9b879566 1376 av_log(s->avctx, AV_LOG_DEBUG, "D");
7bc9090a 1377 else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
9b879566 1378 av_log(s->avctx, AV_LOG_DEBUG, "g");
7bc9090a 1379 else if(IS_GMC(mb_type))
9b879566 1380 av_log(s->avctx, AV_LOG_DEBUG, "G");
7bc9090a 1381 else if(IS_SKIP(mb_type))
9b879566 1382 av_log(s->avctx, AV_LOG_DEBUG, "S");
7bc9090a 1383 else if(!USES_LIST(mb_type, 1))
9b879566 1384 av_log(s->avctx, AV_LOG_DEBUG, ">");
7bc9090a 1385 else if(!USES_LIST(mb_type, 0))
9b879566 1386 av_log(s->avctx, AV_LOG_DEBUG, "<");
7bc9090a
MN
1387 else{
1388 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
9b879566 1389 av_log(s->avctx, AV_LOG_DEBUG, "X");
7bc9090a
MN
1390 }
1391
1392 //segmentation
1393 if(IS_8X8(mb_type))
9b879566 1394 av_log(s->avctx, AV_LOG_DEBUG, "+");
7bc9090a 1395 else if(IS_16X8(mb_type))
9b879566 1396 av_log(s->avctx, AV_LOG_DEBUG, "-");
7bc9090a 1397 else if(IS_8X16(mb_type))
9b879566 1398