10000l (__CPU__ wasnt set)
[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
6000abfa 479 av_freep(&s->mb_type);
7bc9090a
MN
480 av_freep(&s->p_mv_table_base);
481 av_freep(&s->b_forw_mv_table_base);
482 av_freep(&s->b_back_mv_table_base);
483 av_freep(&s->b_bidir_forw_mv_table_base);
484 av_freep(&s->b_bidir_back_mv_table_base);
485 av_freep(&s->b_direct_mv_table_base);
486 s->p_mv_table= NULL;
487 s->b_forw_mv_table= NULL;
488 s->b_back_mv_table= NULL;
489 s->b_bidir_forw_mv_table= NULL;
490 s->b_bidir_back_mv_table= NULL;
491 s->b_direct_mv_table= NULL;
492
6000abfa
FB
493 av_freep(&s->motion_val);
494 av_freep(&s->dc_val[0]);
495 av_freep(&s->ac_val[0]);
496 av_freep(&s->coded_block);
497 av_freep(&s->mbintra_table);
7f2fe444
MN
498 av_freep(&s->cbp_table);
499 av_freep(&s->pred_dir_table);
1457ab52
MN
500 av_freep(&s->me.scratchpad);
501 av_freep(&s->me.map);
502 av_freep(&s->me.score_map);
7f2fe444 503
6000abfa 504 av_freep(&s->mbskip_table);
f943e138 505 av_freep(&s->prev_pict_types);
6000abfa 506 av_freep(&s->bitstream_buffer);
7f2fe444
MN
507 av_freep(&s->tex_pb_buffer);
508 av_freep(&s->pb2_buffer);
f7b47594 509 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
bea669e5
MN
510 av_freep(&s->field_mv_table);
511 av_freep(&s->field_select_table);
3aa102be 512 av_freep(&s->avctx->stats_out);
6b460aa3 513 av_freep(&s->ac_stats);
4d2858de 514 av_freep(&s->error_status_table);
7bc9090a 515 av_freep(&s->mb_index2xy);
1e491e29
MN
516
517 for(i=0; i<MAX_PICTURE_COUNT; i++){
518 free_picture(s, &s->picture[i]);
de6d9b64 519 }
d90cf87b 520 avcodec_default_free_buffers(s->avctx);
de6d9b64
FB
521 s->context_initialized = 0;
522}
523
7604246d
WH
524#ifdef CONFIG_ENCODERS
525
de6d9b64
FB
526/* init video encoder */
527int MPV_encode_init(AVCodecContext *avctx)
528{
529 MpegEncContext *s = avctx->priv_data;
519c2b6d 530 int i;
de6d9b64 531
bc657ac3
ZK
532 avctx->pix_fmt = PIX_FMT_YUV420P;
533
de6d9b64 534 s->bit_rate = avctx->bit_rate;
9cdd6a24 535 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
de6d9b64
FB
536 s->width = avctx->width;
537 s->height = avctx->height;
7f2fe444 538 if(avctx->gop_size > 600){
333547be 539 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
7f2fe444
MN
540 avctx->gop_size=600;
541 }
de6d9b64 542 s->gop_size = avctx->gop_size;
644d98a4
J
543 s->rtp_mode = avctx->rtp_mode;
544 s->rtp_payload_size = avctx->rtp_payload_size;
81401c1f
J
545 if (avctx->rtp_callback)
546 s->rtp_callback = avctx->rtp_callback;
9cdd6a24
MN
547 s->max_qdiff= avctx->max_qdiff;
548 s->qcompress= avctx->qcompress;
549 s->qblur= avctx->qblur;
477c35a9 550 s->avctx = avctx;
ba6802de 551 s->flags= avctx->flags;
9dbcbd92 552 s->max_b_frames= avctx->max_b_frames;
8b4c7dbc 553 s->b_frame_strategy= avctx->b_frame_strategy;
d7e9533a 554 s->codec_id= avctx->codec->id;
7f2fe444
MN
555 s->luma_elim_threshold = avctx->luma_elim_threshold;
556 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
557 s->strict_std_compliance= avctx->strict_std_compliance;
558 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
1457ab52 559 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
87f8cab4 560 s->mpeg_quant= avctx->mpeg_quant;
d7e9533a 561
de6d9b64
FB
562 if (s->gop_size <= 1) {
563 s->intra_only = 1;
564 s->gop_size = 12;
565 } else {
566 s->intra_only = 0;
567 }
5e746b99 568
1457ab52 569 s->me_method = avctx->me_method;
5e746b99 570
e4986da9 571 /* Fixed QSCALE */
de6d9b64 572 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
37fbfd0a 573
c5d309f2 574 s->adaptive_quant= ( s->avctx->lumi_masking
5e746b99 575 || s->avctx->dark_masking
c5d309f2
MN
576 || s->avctx->temporal_cplx_masking
577 || s->avctx->spatial_cplx_masking
578 || s->avctx->p_masking)
579 && !s->fixed_qscale;
fcb48651
MN
580
581 s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
5e746b99 582
9acbbd16
MN
583 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4){
584 fprintf(stderr, "4MV not supporetd by codec\n");
585 return -1;
586 }
587
588 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
589 fprintf(stderr, "qpel not supporetd by codec\n");
590 return -1;
591 }
592
593 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
594 fprintf(stderr, "data partitioning not supporetd by codec\n");
595 return -1;
596 }
597
129dfab2 598 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO){
9acbbd16
MN
599 fprintf(stderr, "b frames not supporetd by codec\n");
600 return -1;
601 }
602
603 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
604 fprintf(stderr, "mpeg2 style quantization not supporetd by codec\n");
605 return -1;
606 }
607
1984f635
MN
608 if(s->codec_id==CODEC_ID_MJPEG){
609 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
610 s->inter_quant_bias= 0;
611 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO){
612 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
613 s->inter_quant_bias= 0;
614 }else{
615 s->intra_quant_bias=0;
616 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
617 }
618
619 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
620 s->intra_quant_bias= avctx->intra_quant_bias;
621 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
622 s->inter_quant_bias= avctx->inter_quant_bias;
623
de6d9b64
FB
624 switch(avctx->codec->id) {
625 case CODEC_ID_MPEG1VIDEO:
626 s->out_format = FMT_MPEG1;
14bea432
MN
627 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
628 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
de6d9b64
FB
629 break;
630 case CODEC_ID_MJPEG:
631 s->out_format = FMT_MJPEG;
632 s->intra_only = 1; /* force intra only for jpeg */
37fbfd0a 633 s->mjpeg_write_tables = 1; /* write all tables */
a69b930c 634 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
37fbfd0a
J
635 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
636 s->mjpeg_vsample[1] = 1; /* the only currently supported values */
637 s->mjpeg_vsample[2] = 1;
a69b930c 638 s->mjpeg_hsample[0] = 2;
37fbfd0a
J
639 s->mjpeg_hsample[1] = 1;
640 s->mjpeg_hsample[2] = 1;
de6d9b64
FB
641 if (mjpeg_init(s) < 0)
642 return -1;
1ff662cc 643 avctx->delay=0;
4e00e76b 644 s->low_delay=1;
de6d9b64 645 break;
1d0d55da 646#ifdef CONFIG_RISKY
de6d9b64 647 case CODEC_ID_H263:
37fbfd0a
J
648 if (h263_get_picture_format(s->width, s->height) == 7) {
649 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
de6d9b64 650 return -1;
37fbfd0a 651 }
de6d9b64 652 s->out_format = FMT_H263;
1ff662cc 653 avctx->delay=0;
4e00e76b 654 s->low_delay=1;
de6d9b64
FB
655 break;
656 case CODEC_ID_H263P:
657 s->out_format = FMT_H263;
658 s->h263_plus = 1;
21e59552
MN
659 /* Fx */
660 s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
661 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
662 /* /Fx */
544286b3 663 /* These are just to be sure */
e51d6d27 664 s->umvplus = 1;
1ff662cc 665 avctx->delay=0;
4e00e76b 666 s->low_delay=1;
de6d9b64
FB
667 break;
668 case CODEC_ID_RV10:
669 s->out_format = FMT_H263;
670 s->h263_rv10 = 1;
1ff662cc 671 avctx->delay=0;
4e00e76b 672 s->low_delay=1;
de6d9b64 673 break;
58f26ba9 674 case CODEC_ID_MPEG4:
de6d9b64
FB
675 s->out_format = FMT_H263;
676 s->h263_pred = 1;
677 s->unrestricted_mv = 1;
4e00e76b 678 s->low_delay= s->max_b_frames ? 0 : 1;
4d2858de 679 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
de6d9b64 680 break;
84afee34 681 case CODEC_ID_MSMPEG4V1:
de6d9b64
FB
682 s->out_format = FMT_H263;
683 s->h263_msmpeg4 = 1;
684 s->h263_pred = 1;
685 s->unrestricted_mv = 1;
84afee34 686 s->msmpeg4_version= 1;
1ff662cc 687 avctx->delay=0;
4e00e76b 688 s->low_delay=1;
84afee34
MN
689 break;
690 case CODEC_ID_MSMPEG4V2:
691 s->out_format = FMT_H263;
692 s->h263_msmpeg4 = 1;
693 s->h263_pred = 1;
694 s->unrestricted_mv = 1;
695 s->msmpeg4_version= 2;
1ff662cc 696 avctx->delay=0;
4e00e76b 697 s->low_delay=1;
84afee34
MN
698 break;
699 case CODEC_ID_MSMPEG4V3:
700 s->out_format = FMT_H263;
701 s->h263_msmpeg4 = 1;
702 s->h263_pred = 1;
703 s->unrestricted_mv = 1;
704 s->msmpeg4_version= 3;
1f9aea9b 705 s->flipflop_rounding=1;
1ff662cc 706 avctx->delay=0;
4e00e76b 707 s->low_delay=1;
de6d9b64 708 break;
f5957f3f
MN
709 case CODEC_ID_WMV1:
710 s->out_format = FMT_H263;
711 s->h263_msmpeg4 = 1;
712 s->h263_pred = 1;
713 s->unrestricted_mv = 1;
714 s->msmpeg4_version= 4;
1f9aea9b 715 s->flipflop_rounding=1;
f5957f3f 716 avctx->delay=0;
4e00e76b 717 s->low_delay=1;
f5957f3f
MN
718 break;
719 case CODEC_ID_WMV2:
720 s->out_format = FMT_H263;
721 s->h263_msmpeg4 = 1;
722 s->h263_pred = 1;
723 s->unrestricted_mv = 1;
724 s->msmpeg4_version= 5;
1f9aea9b 725 s->flipflop_rounding=1;
f5957f3f 726 avctx->delay=0;
4e00e76b 727 s->low_delay=1;
f5957f3f 728 break;
1d0d55da 729#endif
de6d9b64
FB
730 default:
731 return -1;
732 }
cf8039b2 733
45870f57
MN
734 { /* set up some save defaults, some codecs might override them later */
735 static int done=0;
736 if(!done){
737 int i;
738 done=1;
bbed9259 739
30952237
MN
740 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
741 memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
0c1a9eda 742 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
45870f57
MN
743
744 for(i=-16; i<16; i++){
745 default_fcode_tab[i + MAX_MV]= 1;
746 }
747 }
748 }
1457ab52 749 s->me.mv_penalty= default_mv_penalty;
45870f57 750 s->fcode_tab= default_fcode_tab;
a406617f
MN
751 s->y_dc_scale_table=
752 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
753
9d2a0355 754 /* dont use mv_penalty table for crap MV as it would be confused */
1457ab52
MN
755 //FIXME remove after fixing / removing old ME
756 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
9d2a0355 757
3bb4e23a
FB
758 s->encoding = 1;
759
de6d9b64
FB
760 /* init */
761 if (MPV_common_init(s) < 0)
762 return -1;
763
1457ab52
MN
764 ff_init_me(s);
765
bbed9259 766#ifdef CONFIG_ENCODERS
1d0d55da 767#ifdef CONFIG_RISKY
2ad1516a
MN
768 if (s->out_format == FMT_H263)
769 h263_encode_init(s);
2ad1516a
MN
770 if(s->msmpeg4_version)
771 ff_msmpeg4_encode_init(s);
bbed9259 772#endif
1d0d55da
MN
773 if (s->out_format == FMT_MPEG1)
774 ff_mpeg1_encode_init(s);
775#endif
2ad1516a 776
519c2b6d
FB
777 /* init default q matrix */
778 for(i=0;i<64;i++) {
b0368839 779 int j= s->dsp.idct_permutation[i];
1d0d55da 780#ifdef CONFIG_RISKY
87f8cab4 781 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
2ad1516a
MN
782 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
783 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
87f8cab4 784 }else if(s->out_format == FMT_H263){
2ad1516a
MN
785 s->intra_matrix[j] =
786 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
1d0d55da
MN
787 }else
788#endif
789 { /* mpeg1 */
2ad1516a
MN
790 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
791 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
87f8cab4 792 }
d7e9533a
MN
793 }
794
795 /* precompute matrix */
ef5b1b5a 796 /* for mjpeg, we do include qscale in the matrix */
d7e9533a 797 if (s->out_format != FMT_MJPEG) {
28db7fce 798 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
cc7ac888 799 s->intra_matrix, s->intra_quant_bias, 1, 31);
28db7fce 800 convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
cc7ac888 801 s->inter_matrix, s->inter_quant_bias, 1, 31);
519c2b6d
FB
802 }
803
8b4c7dbc
MN
804 if(ff_rate_control_init(s) < 0)
805 return -1;
de6d9b64
FB
806
807 s->picture_number = 0;
45870f57 808 s->picture_in_gop_number = 0;
de6d9b64
FB
809 s->fake_picture_number = 0;
810 /* motion detector init */
811 s->f_code = 1;
9dbcbd92 812 s->b_code = 1;
de6d9b64
FB
813
814 return 0;
815}
816
817int MPV_encode_end(AVCodecContext *avctx)
818{
819 MpegEncContext *s = avctx->priv_data;
820
821#ifdef STATS
822 print_stats();
823#endif
8b4c7dbc
MN
824
825 ff_rate_control_uninit(s);
826
de6d9b64
FB
827 MPV_common_end(s);
828 if (s->out_format == FMT_MJPEG)
829 mjpeg_close(s);
37fbfd0a 830
de6d9b64
FB
831 return 0;
832}
833
7604246d
WH
834#endif //CONFIG_ENCODERS
835
1d0d55da
MN
836void init_rl(RLTable *rl)
837{
0c1a9eda
ZK
838 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
839 uint8_t index_run[MAX_RUN+1];
1d0d55da
MN
840 int last, run, level, start, end, i;
841
842 /* compute max_level[], max_run[] and index_run[] */
843 for(last=0;last<2;last++) {
844 if (last == 0) {
845 start = 0;
846 end = rl->last;
847 } else {
848 start = rl->last;
849 end = rl->n;
850 }
851
852 memset(max_level, 0, MAX_RUN + 1);
853 memset(max_run, 0, MAX_LEVEL + 1);
854 memset(index_run, rl->n, MAX_RUN + 1);
855 for(i=start;i<end;i++) {
856 run = rl->table_run[i];
857 level = rl->table_level[i];
858 if (index_run[run] == rl->n)
859 index_run[run] = i;
860 if (level > max_level[run])
861 max_level[run] = level;
862 if (run > max_run[level])
863 max_run[level] = run;
864 }
865 rl->max_level[last] = av_malloc(MAX_RUN + 1);
866 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
867 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
868 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
869 rl->index_run[last] = av_malloc(MAX_RUN + 1);
870 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
871 }
872}
873
de6d9b64 874/* draw the edges of width 'w' of an image of size width, height */
fd7db0fd 875//FIXME check that this is ok for mpeg4 interlaced
0c1a9eda 876static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
de6d9b64 877{
0c1a9eda 878 uint8_t *ptr, *last_line;
de6d9b64
FB
879 int i;
880
881 last_line = buf + (height - 1) * wrap;
882 for(i=0;i<w;i++) {
883 /* top and bottom */
884 memcpy(buf - (i + 1) * wrap, buf, width);
885 memcpy(last_line + (i + 1) * wrap, last_line, width);
886 }
887 /* left and right */
888 ptr = buf;
889 for(i=0;i<height;i++) {
890 memset(ptr - w, ptr[0], w);
891 memset(ptr + width, ptr[width-1], w);
892 ptr += wrap;
893 }
894 /* corners */
895 for(i=0;i<w;i++) {
896 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
897 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
898 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
899 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
900 }
901}
902
4e00e76b
MN
903static int find_unused_picture(MpegEncContext *s, int shared){
904 int i;
905
906 if(shared){
907 for(i=0; i<MAX_PICTURE_COUNT; i++){
908 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
909 }
910 }else{
911 for(i=0; i<MAX_PICTURE_COUNT; i++){
0da71265 912 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break; //FIXME
4e00e76b
MN
913 }
914 for(i=0; i<MAX_PICTURE_COUNT; i++){
915 if(s->picture[i].data[0]==NULL) break;
916 }
917 }
918
919 assert(i<MAX_PICTURE_COUNT);
920 return i;
921}
922
de6d9b64 923/* generic function for encode/decode called before a frame is coded/decoded */
d6db1c9c 924int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
de6d9b64 925{
4e00e76b 926 int i;
492cd3a9 927 AVFrame *pic;
de6d9b64 928
425dddb7 929 s->mb_skiped = 0;
0da71265 930
8b82a956 931 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
e20c4069 932
1e491e29 933 /* mark&release old frames */
b536d0aa
MN
934 if (s->pict_type != B_TYPE && s->last_picture_ptr) {
935 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
1e491e29
MN
936
937 /* release forgotten pictures */
938 /* if(mpeg124/h263) */
939 if(!s->encoding){
940 for(i=0; i<MAX_PICTURE_COUNT; i++){
b536d0aa 941 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
1e491e29 942 fprintf(stderr, "releasing zombie picture\n");
492cd3a9 943 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
1e491e29
MN
944 }
945 }
d6db1c9c 946 }
93a21abd 947 }
aa388dba
MN
948alloc:
949 if(!s->encoding){
e20c4069
MN
950 /* release non refernce frames */
951 for(i=0; i<MAX_PICTURE_COUNT; i++){
952 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
953 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
954 }
955 }
956
4e00e76b 957 i= find_unused_picture(s, 0);
1e491e29 958
492cd3a9 959 pic= (AVFrame*)&s->picture[i];
0da71265 960 pic->reference= s->pict_type != B_TYPE ? 3 : 0;
b536d0aa
MN
961
962 if(s->current_picture_ptr)
963 pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
1e491e29 964
4e00e76b 965 alloc_picture(s, (Picture*)pic, 0);
93a21abd 966
b536d0aa 967 s->current_picture_ptr= &s->picture[i];
1e491e29 968 }
b7adc711 969
9f2e61b6
MN
970 s->current_picture_ptr->pict_type= s->pict_type;
971 s->current_picture_ptr->quality= s->qscale;
7bc9090a 972 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
9f2e61b6 973
0da71265 974 s->current_picture= *s->current_picture_ptr;
9f2e61b6 975
8b82a956 976 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1e491e29 977 if (s->pict_type != B_TYPE) {
b536d0aa
MN
978 s->last_picture_ptr= s->next_picture_ptr;
979 s->next_picture_ptr= s->current_picture_ptr;
de6d9b64 980 }
d90cf87b 981
b536d0aa
MN
982 if(s->last_picture_ptr) s->last_picture= *s->last_picture_ptr;
983 if(s->next_picture_ptr) s->next_picture= *s->next_picture_ptr;
984 if(s->new_picture_ptr ) s->new_picture = *s->new_picture_ptr;
aa388dba 985
b536d0aa
MN
986 if(s->picture_structure!=PICT_FRAME){
987 int i;
988 for(i=0; i<4; i++){
989 if(s->picture_structure == PICT_BOTTOM_FIELD){
990 s->current_picture.data[i] += s->current_picture.linesize[i];
991 }
992 s->current_picture.linesize[i] *= 2;
993 s->last_picture.linesize[i] *=2;
994 s->next_picture.linesize[i] *=2;
995 }
996 }
997
998 if(s->pict_type != I_TYPE && s->last_picture_ptr==NULL){
aa388dba
MN
999 fprintf(stderr, "warning: first frame is no keyframe\n");
1000 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1001 goto alloc;
1002 }
0da71265 1003 }
1e491e29 1004
aa388dba
MN
1005 s->hurry_up= s->avctx->hurry_up;
1006 s->error_resilience= avctx->error_resilience;
1007
d930ef19
MN
1008 /* set dequantizer, we cant do it during init as it might change for mpeg4
1009 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
1010 if(s->out_format == FMT_H263){
1011 if(s->mpeg_quant)
1012 s->dct_unquantize = s->dct_unquantize_mpeg2;
1013 else
1014 s->dct_unquantize = s->dct_unquantize_h263;
1015 }else
1016 s->dct_unquantize = s->dct_unquantize_mpeg1;
d6db1c9c
MN
1017
1018 return 0;
de6d9b64 1019}
21af69f7 1020
de6d9b64
FB
1021/* generic function for encode/decode called after a frame has been coded/decoded */
1022void MPV_frame_end(MpegEncContext *s)
1023{
1e491e29 1024 int i;
de6d9b64 1025 /* draw edge for correct motion prediction if outside */
1e491e29
MN
1026 if(s->codec_id!=CODEC_ID_SVQ1){
1027 if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1028 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
1029 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1030 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1031 }
de6d9b64 1032 }
5975626d 1033 emms_c();
8b4c7dbc 1034
3aa102be 1035 s->last_pict_type = s->pict_type;
8b4c7dbc
MN
1036 if(s->pict_type!=B_TYPE){
1037 s->last_non_b_pict_type= s->pict_type;
8b4c7dbc 1038 }
b536d0aa
MN
1039#if 0
1040 /* copy back current_picture variables */
1e491e29
MN
1041 for(i=0; i<MAX_PICTURE_COUNT; i++){
1042 if(s->picture[i].data[0] == s->current_picture.data[0]){
1043 s->picture[i]= s->current_picture;
1044 break;
1045 }
1046 }
1047 assert(i<MAX_PICTURE_COUNT);
b536d0aa 1048#endif
1e491e29 1049
e20c4069
MN
1050 if(s->encoding){
1051 /* release non refernce frames */
1052 for(i=0; i<MAX_PICTURE_COUNT; i++){
1053 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1054 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1055 }
1056 }
1e491e29 1057 }
b536d0aa
MN
1058 // clear copies, to avoid confusion
1059#if 0
1060 memset(&s->last_picture, 0, sizeof(Picture));
1061 memset(&s->next_picture, 0, sizeof(Picture));
1062 memset(&s->current_picture, 0, sizeof(Picture));
1063#endif
de6d9b64
FB
1064}
1065
7bc9090a
MN
1066/**
1067 * prints debuging info for the given picture.
1068 */
1069void ff_print_debug_info(MpegEncContext *s, Picture *pict){
1070
1071 if(!pict || !pict->mb_type) return;
1072
1073 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1074 int x,y;
1075
1076 for(y=0; y<s->mb_height; y++){
1077 for(x=0; x<s->mb_width; x++){
1078 if(s->avctx->debug&FF_DEBUG_SKIP){
1079 int count= s->mbskip_table[x + y*s->mb_stride];
1080 if(count>9) count=9;
1081 printf("%1d", count);
1082 }
1083 if(s->avctx->debug&FF_DEBUG_QP){
1084 printf("%2d", pict->qscale_table[x + y*s->mb_stride]);
1085 }
1086 if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1087 int mb_type= pict->mb_type[x + y*s->mb_stride];
1088
1089 //Type & MV direction
1090 if(IS_PCM(mb_type))
1091 printf("P");
1092 else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1093 printf("A");
1094 else if(IS_INTRA4x4(mb_type))
1095 printf("i");
1096 else if(IS_INTRA16x16(mb_type))
1097 printf("I");
1098 else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1099 printf("d");
1100 else if(IS_DIRECT(mb_type))
1101 printf("D");
1102 else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
1103 printf("g");
1104 else if(IS_GMC(mb_type))
1105 printf("G");
1106 else if(IS_SKIP(mb_type))
1107 printf("S");
1108 else if(!USES_LIST(mb_type, 1))
1109 printf(">");
1110 else if(!USES_LIST(mb_type, 0))
1111 printf("<");
1112 else{
1113 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1114 printf("X");
1115 }
1116
1117 //segmentation
1118 if(IS_8X8(mb_type))
1119 printf("+");
1120 else if(IS_16X8(mb_type))
1121 printf("-");
1122 else if(IS_8X16(mb_type))
1123