10l ...
[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*))
37fbfd0a 461 }
45686da2 462 CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM))
4d2858de 463
b465449e
MN
464 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
465
7bc9090a 466 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
37fbfd0a 467
e4eadb4b 468 if (s->out_format == FMT_H263 || s->encoding) {
de6d9b64 469 int size;
7f2fe444 470
de6d9b64
FB
471 /* MV prediction */
472 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
0c1a9eda 473 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
bea669e5
MN
474 }
475
476 if(s->codec_id==CODEC_ID_MPEG4){
4d2858de 477 /* interlaced direct mode decoding tables */
7bc9090a
MN
478 CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t))
479 CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t))
de6d9b64 480 }
6e2d5f1a 481 if (s->out_format == FMT_H263) {
de6d9b64 482 /* ac values */
0c1a9eda 483 CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
de6d9b64
FB
484 s->ac_val[1] = s->ac_val[0] + y_size;
485 s->ac_val[2] = s->ac_val[1] + c_size;
486
487 /* cbp values */
7f2fe444 488 CHECKED_ALLOCZ(s->coded_block, y_size);
eec1c6b9
MN
489
490 /* divx501 bitstream reorder buffer */
7f2fe444 491 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
f01a1894 492
7f2fe444 493 /* cbp, ac_pred, pred_dir */
7bc9090a
MN
494 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
495 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
5b3438c6 496 }
8b32880c
MN
497
498 if (s->h263_pred || s->h263_plus || !s->encoding) {
499 /* dc values */
500 //MN: we need these for error resilience of intra-frames
0c1a9eda 501 CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
8b32880c
MN
502 s->dc_val[1] = s->dc_val[0] + y_size;
503 s->dc_val[2] = s->dc_val[1] + c_size;
504 for(i=0;i<yc_size;i++)
505 s->dc_val[0][i] = 1024;
506 }
507
7806197d 508 /* which mb is a intra block */
7bc9090a
MN
509 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
510 memset(s->mbintra_table, 1, mb_array_size);
7806197d 511
de6d9b64
FB
512 /* default structure is frame */
513 s->picture_structure = PICT_FRAME;
93a21abd 514
3bb4e23a 515 /* init macroblock skip table */
7bc9090a 516 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
4d2858de 517 //Note the +1 is for a quicker mpeg4 slice_end detection
f943e138 518 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
ba6802de 519
91029be7 520 s->block= s->blocks[0];
3bb4e23a 521
a579db0c
IK
522 for(i=0;i<12;i++){
523 s->pblocks[i] = (short *)(&s->block[i]);
524 }
525
d7425f59
MN
526 s->parse_context.state= -1;
527
de6d9b64
FB
528 s->context_initialized = 1;
529 return 0;
530 fail:
8257bf05 531 MPV_common_end(s);
de6d9b64
FB
532 return -1;
533}
534
7f2fe444
MN
535
536//extern int sads;
537
de6d9b64
FB
538/* init common structure for both encoder and decoder */
539void MPV_common_end(MpegEncContext *s)
540{
541 int i;
542
147e5200
MN
543 av_freep(&s->parse_context.buffer);
544 s->parse_context.buffer_size=0;
545
6000abfa 546 av_freep(&s->mb_type);
7bc9090a
MN
547 av_freep(&s->p_mv_table_base);
548 av_freep(&s->b_forw_mv_table_base);
549 av_freep(&s->b_back_mv_table_base);
550 av_freep(&s->b_bidir_forw_mv_table_base);
551 av_freep(&s->b_bidir_back_mv_table_base);
552 av_freep(&s->b_direct_mv_table_base);
553 s->p_mv_table= NULL;
554 s->b_forw_mv_table= NULL;
555 s->b_back_mv_table= NULL;
556 s->b_bidir_forw_mv_table= NULL;
557 s->b_bidir_back_mv_table= NULL;
558 s->b_direct_mv_table= NULL;
559
6000abfa
FB
560 av_freep(&s->motion_val);
561 av_freep(&s->dc_val[0]);
562 av_freep(&s->ac_val[0]);
563 av_freep(&s->coded_block);
564 av_freep(&s->mbintra_table);
7f2fe444
MN
565 av_freep(&s->cbp_table);
566 av_freep(&s->pred_dir_table);
1457ab52
MN
567 av_freep(&s->me.scratchpad);
568 av_freep(&s->me.map);
569 av_freep(&s->me.score_map);
7f2fe444 570
6000abfa 571 av_freep(&s->mbskip_table);
f943e138 572 av_freep(&s->prev_pict_types);
6000abfa 573 av_freep(&s->bitstream_buffer);
7f2fe444
MN
574 av_freep(&s->tex_pb_buffer);
575 av_freep(&s->pb2_buffer);
f7b47594 576 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
bea669e5
MN
577 av_freep(&s->field_mv_table);
578 av_freep(&s->field_select_table);
3aa102be 579 av_freep(&s->avctx->stats_out);
6b460aa3 580 av_freep(&s->ac_stats);
4d2858de 581 av_freep(&s->error_status_table);
7bc9090a 582 av_freep(&s->mb_index2xy);
158c7f05 583 av_freep(&s->lambda_table);
7e4995c3
MN
584 av_freep(&s->q_intra_matrix);
585 av_freep(&s->q_inter_matrix);
642ccefb
MN
586 av_freep(&s->q_intra_matrix16);
587 av_freep(&s->q_inter_matrix16);
45686da2 588 av_freep(&s->blocks);
9d9e3172
MN
589 av_freep(&s->input_picture);
590 av_freep(&s->reordered_input_picture);
1e491e29 591
9b4b6e09
MN
592 if(s->picture){
593 for(i=0; i<MAX_PICTURE_COUNT; i++){
594 free_picture(s, &s->picture[i]);
595 }
de6d9b64 596 }
b465449e 597 av_freep(&s->picture);
d90cf87b 598 avcodec_default_free_buffers(s->avctx);
de6d9b64 599 s->context_initialized = 0;
431f2172
MN
600 s->last_picture_ptr=
601 s->next_picture_ptr=
602 s->current_picture_ptr= NULL;
de6d9b64
FB
603}
604
7604246d
WH
605#ifdef CONFIG_ENCODERS
606
de6d9b64
FB
607/* init video encoder */
608int MPV_encode_init(AVCodecContext *avctx)
609{
610 MpegEncContext *s = avctx->priv_data;
22ddd60b 611 int i, dummy;
b1e6b355 612 int chroma_h_shift, chroma_v_shift;
bc657ac3 613
36b58e85
RS
614 avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
615
de6d9b64 616 s->bit_rate = avctx->bit_rate;
9cdd6a24 617 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
de6d9b64
FB
618 s->width = avctx->width;
619 s->height = avctx->height;
7f2fe444 620 if(avctx->gop_size > 600){
333547be 621 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
7f2fe444
MN
622 avctx->gop_size=600;
623 }
de6d9b64 624 s->gop_size = avctx->gop_size;
644d98a4
J
625 s->rtp_mode = avctx->rtp_mode;
626 s->rtp_payload_size = avctx->rtp_payload_size;
81401c1f
J
627 if (avctx->rtp_callback)
628 s->rtp_callback = avctx->rtp_callback;
9cdd6a24
MN
629 s->max_qdiff= avctx->max_qdiff;
630 s->qcompress= avctx->qcompress;
631 s->qblur= avctx->qblur;
477c35a9 632 s->avctx = avctx;
ba6802de 633 s->flags= avctx->flags;
9dbcbd92 634 s->max_b_frames= avctx->max_b_frames;
8b4c7dbc 635 s->b_frame_strategy= avctx->b_frame_strategy;
d7e9533a 636 s->codec_id= avctx->codec->id;
7f2fe444
MN
637 s->luma_elim_threshold = avctx->luma_elim_threshold;
638 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
639 s->strict_std_compliance= avctx->strict_std_compliance;
640 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
1457ab52 641 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
87f8cab4 642 s->mpeg_quant= avctx->mpeg_quant;
e4eadb4b 643
de6d9b64
FB
644 if (s->gop_size <= 1) {
645 s->intra_only = 1;
646 s->gop_size = 12;
647 } else {
648 s->intra_only = 0;
649 }
5e746b99 650
1457ab52 651 s->me_method = avctx->me_method;
5e746b99 652
e4986da9 653 /* Fixed QSCALE */
de6d9b64 654 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
37fbfd0a 655
c5d309f2 656 s->adaptive_quant= ( s->avctx->lumi_masking
5e746b99 657 || s->avctx->dark_masking
c5d309f2
MN
658 || s->avctx->temporal_cplx_masking
659 || s->avctx->spatial_cplx_masking
660 || s->avctx->p_masking)
661 && !s->fixed_qscale;
fcb48651
MN
662
663 s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
5e746b99 664
9acbbd16
MN
665 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4){
666 fprintf(stderr, "4MV not supporetd by codec\n");
667 return -1;
668 }
669
670 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
671 fprintf(stderr, "qpel not supporetd by codec\n");
672 return -1;
673 }
674
675 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
676 fprintf(stderr, "data partitioning not supporetd by codec\n");
677 return -1;
678 }
679
029911d1 680 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
681 fprintf(stderr, "b frames not supporetd by codec\n");
682 return -1;
683 }
e4eadb4b 684
9acbbd16
MN
685 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
686 fprintf(stderr, "mpeg2 style quantization not supporetd by codec\n");
687 return -1;
688 }
e4eadb4b 689
f2f6134b
MN
690 if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
691 fprintf(stderr, "CBP RD needs trellis quant\n");
692 return -1;
693 }
694
1984f635
MN
695 if(s->codec_id==CODEC_ID_MJPEG){
696 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
697 s->inter_quant_bias= 0;
029911d1 698 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
1984f635
MN
699 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
700 s->inter_quant_bias= 0;
701 }else{
702 s->intra_quant_bias=0;
703 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
704 }
705
706 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
707 s->intra_quant_bias= avctx->intra_quant_bias;
708 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
709 s->inter_quant_bias= avctx->inter_quant_bias;
b1e6b355
MN
710
711 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
712
22ddd60b
MN
713 av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
714 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
715
de6d9b64
FB
716 switch(avctx->codec->id) {
717 case CODEC_ID_MPEG1VIDEO:
718 s->out_format = FMT_MPEG1;
14bea432
MN
719 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
720 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
de6d9b64 721 break;
029911d1
MN
722 case CODEC_ID_MPEG2VIDEO:
723 s->out_format = FMT_MPEG1;
724 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
725 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
726 s->rtp_mode= 1; // mpeg2 must have slices
727 if(s->rtp_payload_size == 0) s->rtp_payload_size= 256*256*256;
728 break;
b1e6b355 729 case CODEC_ID_LJPEG:
de6d9b64
FB
730 case CODEC_ID_MJPEG:
731 s->out_format = FMT_MJPEG;
732 s->intra_only = 1; /* force intra only for jpeg */
37fbfd0a 733 s->mjpeg_write_tables = 1; /* write all tables */
a69b930c 734 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
b1e6b355
MN
735 s->mjpeg_vsample[0] = 1<<chroma_v_shift;
736 s->mjpeg_vsample[1] = 1;
37fbfd0a 737 s->mjpeg_vsample[2] = 1;
b1e6b355 738 s->mjpeg_hsample[0] = 1<<chroma_h_shift;
37fbfd0a
J
739 s->mjpeg_hsample[1] = 1;
740 s->mjpeg_hsample[2] = 1;
de6d9b64
FB
741 if (mjpeg_init(s) < 0)
742 return -1;
1ff662cc 743 avctx->delay=0;
4e00e76b 744 s->low_delay=1;
de6d9b64 745 break;
1d0d55da 746#ifdef CONFIG_RISKY
de6d9b64 747 case CODEC_ID_H263:
37fbfd0a
J
748 if (h263_get_picture_format(s->width, s->height) == 7) {
749 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
de6d9b64 750 return -1;
37fbfd0a 751 }
de6d9b64 752 s->out_format = FMT_H263;
1ff662cc 753 avctx->delay=0;
4e00e76b 754 s->low_delay=1;
de6d9b64
FB
755 break;
756 case CODEC_ID_H263P:
757 s->out_format = FMT_H263;
758 s->h263_plus = 1;
21e59552
MN
759 /* Fx */
760 s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
761 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
762 /* /Fx */
544286b3 763 /* These are just to be sure */
e51d6d27 764 s->umvplus = 1;
1ff662cc 765 avctx->delay=0;
4e00e76b 766 s->low_delay=1;
de6d9b64 767 break;
d4f5d74a
GM
768 case CODEC_ID_FLV1:
769 s->out_format = FMT_H263;
770 s->h263_flv = 2; /* format = 1; 11-bit codes */
771 s->unrestricted_mv = 1;
772 s->rtp_mode=0; /* don't allow GOB */
773 avctx->delay=0;
774 s->low_delay=1;
775 break;
de6d9b64
FB
776 case CODEC_ID_RV10:
777 s->out_format = FMT_H263;
778 s->h263_rv10 = 1;
1ff662cc 779 avctx->delay=0;
4e00e76b 780 s->low_delay=1;
de6d9b64 781 break;
58f26ba9 782 case CODEC_ID_MPEG4:
de6d9b64
FB
783 s->out_format = FMT_H263;
784 s->h263_pred = 1;
785 s->unrestricted_mv = 1;
4e00e76b 786 s->low_delay= s->max_b_frames ? 0 : 1;
4d2858de 787 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
de6d9b64 788 break;
84afee34 789 case CODEC_ID_MSMPEG4V1:
de6d9b64
FB
790 s->out_format = FMT_H263;
791 s->h263_msmpeg4 = 1;
792 s->h263_pred = 1;
793 s->unrestricted_mv = 1;
84afee34 794 s->msmpeg4_version= 1;
1ff662cc 795 avctx->delay=0;
4e00e76b 796 s->low_delay=1;
84afee34
MN
797 break;
798 case CODEC_ID_MSMPEG4V2:
799 s->out_format = FMT_H263;
800 s->h263_msmpeg4 = 1;
801 s->h263_pred = 1;
802 s->unrestricted_mv = 1;
803 s->msmpeg4_version= 2;
1ff662cc 804 avctx->delay=0;
4e00e76b 805 s->low_delay=1;
84afee34
MN
806 break;
807 case CODEC_ID_MSMPEG4V3:
808 s->out_format = FMT_H263;
809 s->h263_msmpeg4 = 1;
810 s->h263_pred = 1;
811 s->unrestricted_mv = 1;
812 s->msmpeg4_version= 3;
1f9aea9b 813 s->flipflop_rounding=1;
1ff662cc 814 avctx->delay=0;
4e00e76b 815 s->low_delay=1;
de6d9b64 816 break;
f5957f3f
MN
817 case CODEC_ID_WMV1:
818 s->out_format = FMT_H263;
819 s->h263_msmpeg4 = 1;
820 s->h263_pred = 1;
821 s->unrestricted_mv = 1;
822 s->msmpeg4_version= 4;
1f9aea9b 823 s->flipflop_rounding=1;
f5957f3f 824 avctx->delay=0;
4e00e76b 825 s->low_delay=1;
f5957f3f
MN
826 break;
827 case CODEC_ID_WMV2:
828 s->out_format = FMT_H263;
829 s->h263_msmpeg4 = 1;
830 s->h263_pred = 1;
831 s->unrestricted_mv = 1;
832 s->msmpeg4_version= 5;
1f9aea9b 833 s->flipflop_rounding=1;
f5957f3f 834 avctx->delay=0;
4e00e76b 835 s->low_delay=1;
f5957f3f 836 break;
1d0d55da 837#endif
de6d9b64
FB
838 default:
839 return -1;
840 }
cf8039b2 841
45870f57
MN
842 { /* set up some save defaults, some codecs might override them later */
843 static int done=0;
844 if(!done){
845 int i;
846 done=1;
bbed9259 847
30952237
MN
848 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
849 memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
0c1a9eda 850 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
45870f57
MN
851
852 for(i=-16; i<16; i++){
853 default_fcode_tab[i + MAX_MV]= 1;
854 }
855 }
856 }
1457ab52 857 s->me.mv_penalty= default_mv_penalty;
45870f57 858 s->fcode_tab= default_fcode_tab;
a406617f
MN
859 s->y_dc_scale_table=
860 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
861
9d2a0355 862 /* dont use mv_penalty table for crap MV as it would be confused */
1457ab52
MN
863 //FIXME remove after fixing / removing old ME
864 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
9d2a0355 865
3bb4e23a
FB
866 s->encoding = 1;
867
de6d9b64
FB
868 /* init */
869 if (MPV_common_init(s) < 0)
870 return -1;
871
1457ab52
MN
872 ff_init_me(s);
873
bbed9259 874#ifdef CONFIG_ENCODERS
1d0d55da 875#ifdef CONFIG_RISKY
2ad1516a
MN
876 if (s->out_format == FMT_H263)
877 h263_encode_init(s);
2ad1516a
MN
878 if(s->msmpeg4_version)
879 ff_msmpeg4_encode_init(s);
bbed9259 880#endif
1d0d55da
MN
881 if (s->out_format == FMT_MPEG1)
882 ff_mpeg1_encode_init(s);
883#endif
2ad1516a 884
519c2b6d
FB
885 /* init default q matrix */
886 for(i=0;i<64;i++) {
b0368839 887 int j= s->dsp.idct_permutation[i];
1d0d55da 888#ifdef CONFIG_RISKY
87f8cab4 889 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
2ad1516a
MN
890 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
891 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
87f8cab4 892 }else if(s->out_format == FMT_H263){
2ad1516a
MN
893 s->intra_matrix[j] =
894 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
1d0d55da
MN
895 }else
896#endif
029911d1 897 { /* mpeg1/2 */
2ad1516a
MN
898 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
899 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
87f8cab4 900 }
d6eb3c50
MN
901 if(s->avctx->intra_matrix)
902 s->intra_matrix[j] = s->avctx->intra_matrix[i];
903 if(s->avctx->inter_matrix)
904 s->inter_matrix[j] = s->avctx->inter_matrix[i];
d7e9533a
MN
905 }
906
907 /* precompute matrix */
ef5b1b5a 908 /* for mjpeg, we do include qscale in the matrix */
d7e9533a 909 if (s->out_format != FMT_MJPEG) {
6b56c616 910 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
cc7ac888 911 s->intra_matrix, s->intra_quant_bias, 1, 31);
6b56c616 912 convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
cc7ac888 913 s->inter_matrix, s->inter_quant_bias, 1, 31);
519c2b6d
FB
914 }
915
8b4c7dbc
MN
916 if(ff_rate_control_init(s) < 0)
917 return -1;
de6d9b64
FB
918
919 s->picture_number = 0;
45870f57 920 s->picture_in_gop_number = 0;
de6d9b64
FB
921 s->fake_picture_number = 0;
922 /* motion detector init */
923 s->f_code = 1;
9dbcbd92 924 s->b_code = 1;
de6d9b64
FB
925
926 return 0;
927}
928
929int MPV_encode_end(AVCodecContext *avctx)
930{
931 MpegEncContext *s = avctx->priv_data;
932
933#ifdef STATS
934 print_stats();
935#endif
8b4c7dbc
MN
936
937 ff_rate_control_uninit(s);
938
de6d9b64
FB
939 MPV_common_end(s);
940 if (s->out_format == FMT_MJPEG)
941 mjpeg_close(s);
541ae140 942
22ddd60b 943 av_freep(&avctx->extradata);
37fbfd0a 944
de6d9b64
FB
945 return 0;
946}
947
7604246d
WH
948#endif //CONFIG_ENCODERS
949
1d0d55da
MN
950void init_rl(RLTable *rl)
951{
0c1a9eda
ZK
952 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
953 uint8_t index_run[MAX_RUN+1];
1d0d55da
MN
954 int last, run, level, start, end, i;
955
956 /* compute max_level[], max_run[] and index_run[] */
957 for(last=0;last<2;last++) {
958 if (last == 0) {
959 start = 0;
960 end = rl->last;
961 } else {
962 start = rl->last;
963 end = rl->n;
964 }
965
966 memset(max_level, 0, MAX_RUN + 1);
967 memset(max_run, 0, MAX_LEVEL + 1);
968 memset(index_run, rl->n, MAX_RUN + 1);
969 for(i=start;i<end;i++) {
970 run = rl->table_run[i];
971 level = rl->table_level[i];
972 if (index_run[run] == rl->n)
973 index_run[run] = i;
974 if (level > max_level[run])
975 max_level[run] = level;
976 if (run > max_run[level])
977 max_run[level] = run;
978 }
979 rl->max_level[last] = av_malloc(MAX_RUN + 1);
980 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
981 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
982 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
983 rl->index_run[last] = av_malloc(MAX_RUN + 1);
984 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
985 }
986}
987
de6d9b64 988/* draw the edges of width 'w' of an image of size width, height */
fd7db0fd 989//FIXME check that this is ok for mpeg4 interlaced
0c1a9eda 990static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
de6d9b64 991{
0c1a9eda 992 uint8_t *ptr, *last_line;
de6d9b64
FB
993 int i;
994
995 last_line = buf + (height - 1) * wrap;
996 for(i=0;i<w;i++) {
997 /* top and bottom */
998 memcpy(buf - (i + 1) * wrap, buf, width);
999 memcpy(last_line + (i + 1) * wrap, last_line, width);
1000 }
1001 /* left and right */
1002 ptr = buf;
1003 for(i=0;i<height;i++) {
1004 memset(ptr - w, ptr[0], w);
1005 memset(ptr + width, ptr[width-1], w);
1006 ptr += wrap;
1007 }
1008 /* corners */
1009 for(i=0;i<w;i++) {
1010 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
1011 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
1012 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
1013 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
1014 }
1015}
1016
5f194811 1017int ff_find_unused_picture(MpegEncContext *s, int shared){
4e00e76b
MN
1018 int i;
1019
1020 if(shared){
1021 for(i=0; i<MAX_PICTURE_COUNT; i++){
5f194811 1022 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
4e00e76b
MN
1023 }
1024 }else{
1025 for(i=0; i<MAX_PICTURE_COUNT; i++){
5f194811 1026 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
4e00e76b
MN
1027 }
1028 for(i=0; i<MAX_PICTURE_COUNT; i++){
5f194811 1029 if(s->picture[i].data[0]==NULL) return i;
4e00e76b
MN
1030 }
1031 }
1032
5f194811
MN
1033 assert(0);
1034 return -1;
4e00e76b
MN
1035}
1036
5f194811
MN
1037/**
1038 * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
1039 */
d6db1c9c 1040int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
de6d9b64 1041{
4e00e76b 1042 int i;
492cd3a9 1043 AVFrame *pic;
425dddb7 1044 s->mb_skiped = 0;
0da71265 1045
8b82a956 1046 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
e20c4069 1047
1e491e29 1048 /* mark&release old frames */
59c673d5 1049 if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
b536d0aa 1050 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
1e491e29
MN
1051
1052 /* release forgotten pictures */
1053 /* if(mpeg124/h263) */
1054 if(!s->encoding){
1055 for(i=0; i<MAX_PICTURE_COUNT; i++){
b536d0aa 1056 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
1e491e29 1057 fprintf(stderr, "releasing zombie picture\n");
492cd3a9 1058 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
1e491e29
MN
1059 }
1060 }
d6db1c9c 1061 }
93a21abd 1062 }
aa388dba
MN
1063alloc:
1064 if(!s->encoding){
e20c4069
MN
1065 /* release non refernce frames */
1066 for(i=0; i<MAX_PICTURE_COUNT; i++){
1067 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1068 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1069 }
1070 }
1071
5f194811
MN
1072 if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
1073 pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
1074 else{
1075 i= ff_find_unused_picture(s, 0);
1076 pic= (AVFrame*)&s->picture[i];
1077 }
1078
0da71265 1079 pic->reference= s->pict_type != B_TYPE ? 3 : 0;
b536d0aa 1080
5f194811 1081 if(s->current_picture_ptr) //FIXME broken, we need a coded_picture_number in MpegEncContext
b536d0aa 1082 pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
1e491e29 1083
f23a68df
IK
1084 if( alloc_picture(s, (Picture*)pic, 0) < 0)
1085 return -1;
93a21abd 1086
5f194811 1087 s->current_picture_ptr= (Picture*)pic;
1e491e29 1088 }
b7adc711 1089
9f2e61b6 1090 s->current_picture_ptr->pict_type= s->pict_type;
158c7f05
MN
1091// if(s->flags && CODEC_FLAG_QSCALE)
1092 // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
7bc9090a 1093 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
9f2e61b6 1094
6571e41d 1095 copy_picture(&s->current_picture, s->current_picture_ptr);
9f2e61b6 1096
8b82a956 1097 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1e491e29 1098 if (s->pict_type != B_TYPE) {
b536d0aa
MN
1099 s->last_picture_ptr= s->next_picture_ptr;
1100 s->next_picture_ptr= s->current_picture_ptr;
de6d9b64 1101 }
d90cf87b 1102
6571e41d
MN
1103 if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
1104 if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
aa388dba 1105
6571e41d 1106 if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
ffba1dc0
MN
1107 fprintf(stderr, "warning: first frame is no keyframe\n");
1108 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1109 goto alloc;
1110 }
1111
1112 assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
1113
b536d0aa
MN
1114 if(s->picture_structure!=PICT_FRAME){
1115 int i;
1116 for(i=0; i<4; i++){
1117 if(s->picture_structure == PICT_BOTTOM_FIELD){
1118 s->current_picture.data[i] += s->current_picture.linesize[i];
1119 }
1120 s->current_picture.linesize[i] *= 2;
1121 s->last_picture.linesize[i] *=2;
1122 s->next_picture.linesize[i] *=2;
1123 }
1124 }
0da71265 1125 }
1e491e29 1126
aa388dba
MN
1127 s->hurry_up= s->avctx->hurry_up;
1128 s->error_resilience= avctx->error_resilience;
1129
d930ef19
MN
1130 /* set dequantizer, we cant do it during init as it might change for mpeg4
1131 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
029911d1
MN
1132 if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO)
1133 s->dct_unquantize = s->dct_unquantize_mpeg2;
1134 else if(s->out_format == FMT_H263)
1135 s->dct_unquantize = s->dct_unquantize_h263;
1136 else
d930ef19 1137 s->dct_unquantize = s->dct_unquantize_mpeg1;
d6db1c9c 1138
2e7b4c84
IK
1139#ifdef HAVE_XVMC
1140 if(s->avctx->xvmc_acceleration)
1141 return XVMC_field_start(s, avctx);
1142#endif
d6db1c9c 1143 return 0;
de6d9b64 1144}
21af69f7 1145
de6d9b64
FB
1146/* generic function for encode/decode called after a frame has been coded/decoded */
1147void MPV_frame_end(MpegEncContext *s)
1148{
1e491e29 1149 int i;
de6d9b64 1150 /* draw edge for correct motion prediction if outside */
2e7b4c84
IK
1151#ifdef HAVE_XVMC
1152//just to make sure that all data is rendered.
1153 if(s->avctx->xvmc_acceleration){
1154 XVMC_field_end(s);
1155 }else
1156#endif
a573cc27 1157 if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1e491e29
MN
1158 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
1159 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1160 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
de6d9b64 1161 }
5975626d 1162 emms_c();
8b4c7dbc 1163
3aa102be 1164 s->last_pict_type = s->pict_type;
8b4c7dbc
MN
1165 if(s->pict_type!=B_TYPE){
1166 s->last_non_b_pict_type= s->pict_type;
8b4c7dbc 1167 }
b536d0aa
MN
1168#if 0
1169 /* copy back current_picture variables */
1e491e29
MN
1170 for(i=0; i<MAX_PICTURE_COUNT; i++){
1171 if(s->picture[i].data[0] == s->current_picture.data[0]){
1172 s->picture[i]= s->current_picture;
1173 break;
1174 }
1175 }
1176 assert(i<MAX_PICTURE_COUNT);
b536d0aa 1177#endif
1e491e29 1178
e20c4069
MN
1179 if(s->encoding){
1180 /* release non refernce frames */
1181 for(i=0; i<MAX_PICTURE_COUNT; i++){
1182 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1183 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1184 }
1185 }
1e491e29 1186 }
b536d0aa
MN
1187 // clear copies, to avoid confusion
1188#if 0
1189 memset(&s->last_picture, 0, sizeof(Picture));
1190 memset(&s->next_picture, 0, sizeof(Picture));
1191 memset(&s->current_picture, 0, sizeof(Picture));
1192#endif
de6d9b64
FB
1193}
1194
7bc9090a 1195/**
db6e7795
MN
1196 * draws an line from (ex, ey) -> (sx, sy).
1197 * @param w width of the image
1198 * @param h height of the image
1199 * @param stride stride/linesize of the image
1200 * @param color color of the arrow
1201 */
1202static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1203 int t, x, y, f;
1204
1205 sx= clip(sx, 0, w-1);
1206 sy= clip(sy, 0, h-1);
1207 ex= clip(ex, 0, w-1);
1208 ey= clip(ey, 0, h-1);
1209
1210 buf[sy*stride + sx]+= color;
1211
1212 if(ABS(ex - sx) > ABS(ey - sy)){
1213 if(sx > ex){
1214 t=sx; sx=ex; ex=t;
1215 t=sy; sy=ey; ey=t;
1216 }
1217 buf+= sx + sy*stride;
1218 ex-= sx;
1219 f= ((ey-sy)<<16)/ex;
1220 for(x= 0; x <= ex; x++){
1221 y= ((x*f) + (1<<15))>>16;
1222 buf[y*stride + x]+= color;
1223 }
1224 }else{
1225 if(sy > ey){
1226 t=sx; sx=ex; ex=t;
1227 t=sy; sy=ey; ey=t;
1228 }
1229 buf+= sx + sy*stride;
1230 ey-= sy;
1231 if(ey) f= ((ex-sx)<<16)/ey;
1232 else f= 0;
1233 for(y= 0; y <= ey; y++){
1234 x= ((y*f) + (1<<15))>>16;
1235 buf[y*stride + x]+= color;
1236 }
1237 }
1238}
1239
1240/**
1241 * draws an arrow from (ex, ey) -> (sx, sy).
1242 * @param w width of the image
1243 * @param h height of the image
1244 * @param stride stride/linesize of the image
1245 * @param color color of the arrow
1246 */
1247static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1248 int dx,dy;
1249
1250 sx= clip(sx, -100, w+100);
1251 sy= clip(sy, -100, h+100);
1252 ex= clip(ex, -100, w+100);
1253 ey= clip(ey, -100, h+100);
1254
1255 dx= ex - sx;
1256 dy= ey - sy;
1257
1258 if(dx*dx + dy*dy > 3*3){
1259 int rx= dx + dy;
1260 int ry= -dx + dy;
1261 int length= ff_sqrt((rx*rx + ry*ry)<<8);
1262
1263 //FIXME subpixel accuracy
1264 rx= ROUNDED_DIV(rx*3<<4, length);
1265 ry= ROUNDED_DIV(ry*3<<4, length);
1266
1267 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1268 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1269 }
1270 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1271}
1272
1273/**
7bc9090a
MN
1274 * prints debuging info for the given picture.
1275 */
1276void ff_print_debug_info(MpegEncContext *s, Picture *pict){
1277
1278 if(!pict || !pict->mb_type) return;
1279
1280 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1281 int x,y;
1282
1283 for(y=0; y<s->mb_height; y++){
1284 for(x=0; x<s->mb_width; x++){
1285 if(s->avctx->debug&FF_DEBUG_SKIP){
1286 int count= s->mbskip_table[x + y*s->mb_stride];
1287 if(count>9) count=9;
1288 printf("%1d", count);
1289 }
1290 if(s->avctx->debug&FF_DEBUG_QP){
1291 printf("%2d", pict->qscale_table[x + y*s->mb_stride]);
1292 }
1293 if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1294 int mb_type= pict->mb_type[x + y*s->mb_stride];
1295
1296 //Type & MV direction
1297 if(IS_PCM(mb_type))
1298 printf("P");
1299 else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1300 printf("A");
1301 else if(IS_INTRA4x4(mb_type))
1302 printf("i");
1303 else if(IS_INTRA16x16(mb_type))
1304 printf("I");
1305 else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1306 printf("d");
1307 else if(IS_DIRECT(mb_type))
1308 printf("D");
1309 else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
1310 printf("g");
1311 else if(IS_GMC(mb_type))
1312 printf("G");
1313 else if(IS_SKIP(mb_type))
1314 printf("S");
1315 else if(!USES_LIST(mb_type, 1))
1316 printf(">");
1317 else if(!USES_LIST(mb_type, 0))
1318 printf("<");
1319 else{
1320 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1321 printf("X");
1322 }
1323
1324 //segmentation
1325 if(IS_8X8(mb_type))
1326 printf("+");
1327 else if(IS_16X8(mb_type))
1328 printf("-");
1329 else if(IS_8X16(mb_type))
1330