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