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