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