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