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