Commit | Line | Data |
---|---|---|
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 | 43 | static void encode_picture(MpegEncContext *s, int picture_number); |
7604246d | 44 | #endif //CONFIG_ENCODERS |
d50635cd | 45 | static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, |
21af69f7 | 46 | DCTELEM *block, int n, int qscale); |
d50635cd | 47 | static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, |
9dbf1ddd | 48 | DCTELEM *block, int n, int qscale); |
d50635cd MN |
49 | static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, |
50 | DCTELEM *block, int n, int qscale); | |
51 | static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, | |
52 | DCTELEM *block, int n, int qscale); | |
53 | static void dct_unquantize_h263_intra_c(MpegEncContext *s, | |
54 | DCTELEM *block, int n, int qscale); | |
55 | static void dct_unquantize_h263_inter_c(MpegEncContext *s, | |
21af69f7 | 56 | DCTELEM *block, int n, int qscale); |
0c1a9eda | 57 | static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w); |
7604246d | 58 | #ifdef CONFIG_ENCODERS |
d7e9533a | 59 | static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); |
477ab036 | 60 | static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); |
77ea0d4b | 61 | static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale); |
7d1c3fc1 | 62 | static int sse_mb(MpegEncContext *s); |
783df5f3 | 63 | static void denoise_dct_c(MpegEncContext *s, DCTELEM *block); |
7604246d | 64 | #endif //CONFIG_ENCODERS |
3d9fccbf | 65 | |
2e7b4c84 IK |
66 | #ifdef HAVE_XVMC |
67 | extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx); | |
68 | extern void XVMC_field_end(MpegEncContext *s); | |
a579db0c | 69 | extern void XVMC_decode_mb(MpegEncContext *s); |
2e7b4c84 IK |
70 | #endif |
71 | ||
0c1a9eda | 72 | void (*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 | 84 | static 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 | 96 | static 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 |
101 | static 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 | 107 | static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL; |
0c1a9eda | 108 | static uint8_t default_fcode_tab[MAX_MV*2+1]; |
45870f57 | 109 | |
a33c7159 MN |
110 | enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1}; |
111 | ||
6b56c616 | 112 | static 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 | |
168 | static 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 | 176 | void 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 |
201 | void 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 */ |
215 | int 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 |
275 | static void copy_picture(Picture *dst, Picture *src){ |
276 | *dst = *src; | |
277 | dst->type= FF_BUFFER_TYPE_COPY; | |
278 | } | |
279 | ||
71c47d6e MN |
280 | static 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 | 295 | static 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 | } |
7bc9090a | 357 | pic->qstride= s->mb_stride; |
fa384dcc | 358 | CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan)) |
4e00e76b | 359 | } |
0da71265 | 360 | |
f943e138 MN |
361 | //it might be nicer if the application would keep track of these but it would require a API change |
362 | memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1); | |
363 | s->prev_pict_types[0]= s->pict_type; | |
364 | if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE) | |
365 | pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway | |
366 | ||
1e491e29 MN |
367 | return 0; |
368 | fail: //for the CHECKED_ALLOCZ macro | |
369 | return -1; | |
370 | } | |
371 | ||
4e00e76b MN |
372 | /** |
373 | * deallocates a picture | |
374 | */ | |
1e491e29 MN |
375 | static void free_picture(MpegEncContext *s, Picture *pic){ |
376 | int i; | |
4e00e76b MN |
377 | |
378 | if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){ | |
492cd3a9 | 379 | s->avctx->release_buffer(s->avctx, (AVFrame*)pic); |
4e00e76b MN |
380 | } |
381 | ||
1e491e29 MN |
382 | av_freep(&pic->mb_var); |
383 | av_freep(&pic->mc_mb_var); | |
384 | av_freep(&pic->mb_mean); | |
385 | av_freep(&pic->mbskip_table); | |
386 | av_freep(&pic->qscale_table); | |
0da71265 | 387 | av_freep(&pic->mb_type_base); |
fa384dcc | 388 | av_freep(&pic->pan_scan); |
0da71265 MN |
389 | pic->mb_type= NULL; |
390 | for(i=0; i<2; i++){ | |
b40cd4e0 | 391 | av_freep(&pic->motion_val_base[i]); |
0da71265 MN |
392 | av_freep(&pic->ref_index[i]); |
393 | } | |
d90cf87b MN |
394 | |
395 | if(pic->type == FF_BUFFER_TYPE_SHARED){ | |
4e00e76b MN |
396 | for(i=0; i<4; i++){ |
397 | pic->base[i]= | |
398 | pic->data[i]= NULL; | |
399 | } | |
400 | pic->type= 0; | |
1e491e29 MN |
401 | } |
402 | } | |
403 | ||
defdfc9a AB |
404 | /* init common structure for both encoder and decoder */ |
405 | int MPV_common_init(MpegEncContext *s) | |
406 | { | |
bb198e19 | 407 | int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y; |
defdfc9a | 408 | |
b0368839 | 409 | dsputil_init(&s->dsp, s->avctx); |
defdfc9a | 410 | DCT_common_init(s); |
eb4b3dd3 | 411 | |
9fee1e23 | 412 | s->flags= s->avctx->flags; |
303e50e6 | 413 | s->flags2= s->avctx->flags2; |
defdfc9a | 414 | |
1e491e29 | 415 | s->mb_width = (s->width + 15) / 16; |
de6d9b64 | 416 | s->mb_height = (s->height + 15) / 16; |
7bc9090a | 417 | s->mb_stride = s->mb_width + 1; |
b40cd4e0 MN |
418 | s->b8_stride = s->mb_width*2 + 1; |
419 | s->b4_stride = s->mb_width*4 + 1; | |
7bc9090a | 420 | mb_array_size= s->mb_height * s->mb_stride; |
bb198e19 | 421 | mv_table_size= (s->mb_height+2) * s->mb_stride + 1; |
eb4b3dd3 | 422 | |
b5a093b3 MN |
423 | /* set default edge pos, will be overriden in decode_header if needed */ |
424 | s->h_edge_pos= s->mb_width*16; | |
425 | s->v_edge_pos= s->mb_height*16; | |
eb4b3dd3 ZK |
426 | |
427 | s->mb_num = s->mb_width * s->mb_height; | |
7bc9090a MN |
428 | |
429 | s->block_wrap[0]= | |
430 | s->block_wrap[1]= | |
431 | s->block_wrap[2]= | |
432 | s->block_wrap[3]= s->mb_width*2 + 2; | |
433 | s->block_wrap[4]= | |
434 | s->block_wrap[5]= s->mb_width + 2; | |
eb4b3dd3 | 435 | |
332f9ac4 MN |
436 | s->y_dc_scale_table= |
437 | s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | |
2be9f03a | 438 | s->chroma_qscale_table= ff_default_chroma_qscale_table; |
db9b1b5e FB |
439 | if (!s->encoding) |
440 | s->progressive_sequence= 1; | |
2be9f03a | 441 | s->progressive_frame= 1; |
1031fabd | 442 | s->coded_picture_number = 0; |
db9b1b5e | 443 | |
eb4b3dd3 ZK |
444 | y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); |
445 | c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
446 | yc_size = y_size + 2 * c_size; | |
447 | ||
202ef8b8 | 448 | /* convert fourcc to upper case */ |
7004ffb3 MN |
449 | s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF) |
450 | + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 ) | |
451 | + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16) | |
452 | + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24); | |
b5a093b3 | 453 | |
541ae140 MN |
454 | s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF) |
455 | + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 ) | |
456 | + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16) | |
457 | + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24); | |
458 | ||
f7b47594 MN |
459 | CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance |
460 | s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17; | |
1e491e29 | 461 | |
492cd3a9 | 462 | s->avctx->coded_frame= (AVFrame*)&s->current_picture; |
1e491e29 | 463 | |
7bc9090a MN |
464 | CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this |
465 | for(y=0; y<s->mb_height; y++){ | |
466 | for(x=0; x<s->mb_width; x++){ | |
467 | s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride; | |
468 | } | |
469 | } | |
470 | s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed? | |
471 | ||
37fbfd0a | 472 | if (s->encoding) { |
9dbcbd92 | 473 | /* Allocate MV tables */ |
7bc9090a MN |
474 | CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) |
475 | CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) | |
476 | CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) | |
477 | CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) | |
478 | CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) | |
479 | CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) | |
480 | s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1; | |
481 | s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1; | |
482 | s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1; | |
483 | s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1; | |
484 | s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1; | |
485 | s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1; | |
7f2fe444 | 486 | |
1e491e29 | 487 | //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer() |
1457ab52 | 488 | CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t)) |
7f2fe444 | 489 | |
1457ab52 MN |
490 | CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t)) |
491 | CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t)) | |
91029be7 | 492 | |
7f2fe444 MN |
493 | if(s->codec_id==CODEC_ID_MPEG4){ |
494 | CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE); | |
495 | CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE); | |
496 | } | |
3aa102be | 497 | |
6b460aa3 MN |
498 | if(s->msmpeg4_version){ |
499 | CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int)); | |
500 | } | |
3aa102be | 501 | CHECKED_ALLOCZ(s->avctx->stats_out, 256); |
7bc9090a MN |
502 | |
503 | /* Allocate MB type table */ | |
bb198e19 | 504 | CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding |
158c7f05 MN |
505 | |
506 | CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int)) | |
7e4995c3 MN |
507 | |
508 | CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int)) | |
509 | CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int)) | |
642ccefb MN |
510 | CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t)) |
511 | CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t)) | |
9d9e3172 MN |
512 | CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*)) |
513 | CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*)) | |
821cb11f MN |
514 | |
515 | if(s->avctx->noise_reduction){ | |
516 | CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int)) | |
517 | CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t)) | |
518 | } | |
37fbfd0a | 519 | } |
45686da2 | 520 | CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM)) |
4d2858de | 521 | |
b465449e MN |
522 | CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture)) |
523 | ||
7bc9090a | 524 | CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t)) |
37fbfd0a | 525 | |
bb198e19 | 526 | if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){ |
4d2858de | 527 | /* interlaced direct mode decoding tables */ |
bb198e19 MN |
528 | for(i=0; i<2; i++){ |
529 | int j, k; | |
530 | for(j=0; j<2; j++){ | |
531 | for(k=0; k<2; k++){ | |
532 | CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t)) | |
533 | s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1; | |
534 | } | |
535 | CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t)) | |
536 | CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t)) | |
537 | s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1; | |
538 | } | |
539 | CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t)) | |
540 | } | |
de6d9b64 | 541 | } |
6e2d5f1a | 542 | if (s->out_format == FMT_H263) { |
de6d9b64 | 543 | /* ac values */ |
0c1a9eda | 544 | CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16); |
de6d9b64 FB |
545 | s->ac_val[1] = s->ac_val[0] + y_size; |
546 | s->ac_val[2] = s->ac_val[1] + c_size; | |
547 | ||
548 | /* cbp values */ | |
7f2fe444 | 549 | CHECKED_ALLOCZ(s->coded_block, y_size); |
eec1c6b9 MN |
550 | |
551 | /* divx501 bitstream reorder buffer */ | |
7f2fe444 | 552 | CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE); |
f01a1894 | 553 | |
7f2fe444 | 554 | /* cbp, ac_pred, pred_dir */ |
7bc9090a MN |
555 | CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t)) |
556 | CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t)) | |
5b3438c6 | 557 | } |
8b32880c MN |
558 | |
559 | if (s->h263_pred || s->h263_plus || !s->encoding) { | |
560 | /* dc values */ | |
561 | //MN: we need these for error resilience of intra-frames | |
0c1a9eda | 562 | CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t)); |
8b32880c MN |
563 | s->dc_val[1] = s->dc_val[0] + y_size; |
564 | s->dc_val[2] = s->dc_val[1] + c_size; | |
565 | for(i=0;i<yc_size;i++) | |
566 | s->dc_val[0][i] = 1024; | |
567 | } | |
568 | ||
7806197d | 569 | /* which mb is a intra block */ |
7bc9090a MN |
570 | CHECKED_ALLOCZ(s->mbintra_table, mb_array_size); |
571 | memset(s->mbintra_table, 1, mb_array_size); | |
7806197d | 572 | |
de6d9b64 FB |
573 | /* default structure is frame */ |
574 | s->picture_structure = PICT_FRAME; | |
93a21abd | 575 | |
3bb4e23a | 576 | /* init macroblock skip table */ |
7bc9090a | 577 | CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2); |
4d2858de | 578 | //Note the +1 is for a quicker mpeg4 slice_end detection |
f943e138 | 579 | CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE); |
ba6802de | 580 | |
91029be7 | 581 | s->block= s->blocks[0]; |
3bb4e23a | 582 | |
a579db0c IK |
583 | for(i=0;i<12;i++){ |
584 | s->pblocks[i] = (short *)(&s->block[i]); | |
585 | } | |
586 | ||
d7425f59 | 587 | s->parse_context.state= -1; |
0c9bbaec WH |
588 | if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){ |
589 | s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH); | |
590 | s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH); | |
591 | s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH); | |
592 | } | |
d7425f59 | 593 | |
de6d9b64 FB |
594 | s->context_initialized = 1; |
595 | return 0; | |
596 | fail: | |
8257bf05 | 597 | MPV_common_end(s); |
de6d9b64 FB |
598 | return -1; |
599 | } | |
600 | ||
7f2fe444 MN |
601 | |
602 | //extern int sads; | |
603 | ||
de6d9b64 FB |
604 | /* init common structure for both encoder and decoder */ |
605 | void MPV_common_end(MpegEncContext *s) | |
606 | { | |
bb198e19 | 607 | int i, j, k; |
de6d9b64 | 608 | |
147e5200 MN |
609 | av_freep(&s->parse_context.buffer); |
610 | s->parse_context.buffer_size=0; | |
611 | ||
6000abfa | 612 | av_freep(&s->mb_type); |
7bc9090a MN |
613 | av_freep(&s->p_mv_table_base); |
614 | av_freep(&s->b_forw_mv_table_base); | |
615 | av_freep(&s->b_back_mv_table_base); | |
616 | av_freep(&s->b_bidir_forw_mv_table_base); | |
617 | av_freep(&s->b_bidir_back_mv_table_base); | |
618 | av_freep(&s->b_direct_mv_table_base); | |
619 | s->p_mv_table= NULL; | |
620 | s->b_forw_mv_table= NULL; | |
621 | s->b_back_mv_table= NULL; | |
622 | s->b_bidir_forw_mv_table= NULL; | |
623 | s->b_bidir_back_mv_table= NULL; | |
624 | s->b_direct_mv_table= NULL; | |
bb198e19 MN |
625 | for(i=0; i<2; i++){ |
626 | for(j=0; j<2; j++){ | |
627 | for(k=0; k<2; k++){ | |
628 | av_freep(&s->b_field_mv_table_base[i][j][k]); | |
629 | s->b_field_mv_table[i][j][k]=NULL; | |
630 | } | |
631 | av_freep(&s->b_field_select_table[i][j]); | |
632 | av_freep(&s->p_field_mv_table_base[i][j]); | |
633 | s->p_field_mv_table[i][j]=NULL; | |
634 | } | |
635 | av_freep(&s->p_field_select_table[i]); | |
636 | } | |
7bc9090a | 637 | |
6000abfa FB |
638 | av_freep(&s->dc_val[0]); |
639 | av_freep(&s->ac_val[0]); | |
640 | av_freep(&s->coded_block); | |
641 | av_freep(&s->mbintra_table); | |
7f2fe444 MN |
642 | av_freep(&s->cbp_table); |
643 | av_freep(&s->pred_dir_table); | |
1457ab52 MN |
644 | av_freep(&s->me.scratchpad); |
645 | av_freep(&s->me.map); | |
646 | av_freep(&s->me.score_map); | |
7f2fe444 | 647 | |
6000abfa | 648 | av_freep(&s->mbskip_table); |
f943e138 | 649 | av_freep(&s->prev_pict_types); |
6000abfa | 650 | av_freep(&s->bitstream_buffer); |
7f2fe444 MN |
651 | av_freep(&s->tex_pb_buffer); |
652 | av_freep(&s->pb2_buffer); | |
f7b47594 | 653 | av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL; |
3aa102be | 654 | av_freep(&s->avctx->stats_out); |
6b460aa3 | 655 | av_freep(&s->ac_stats); |
4d2858de | 656 | av_freep(&s->error_status_table); |
7bc9090a | 657 | av_freep(&s->mb_index2xy); |
158c7f05 | 658 | av_freep(&s->lambda_table); |
7e4995c3 MN |
659 | av_freep(&s->q_intra_matrix); |
660 | av_freep(&s->q_inter_matrix); | |
642ccefb MN |
661 | av_freep(&s->q_intra_matrix16); |
662 | av_freep(&s->q_inter_matrix16); | |
45686da2 | 663 | av_freep(&s->blocks); |
9d9e3172 MN |
664 | av_freep(&s->input_picture); |
665 | av_freep(&s->reordered_input_picture); | |
821cb11f MN |
666 | av_freep(&s->dct_error_sum); |
667 | av_freep(&s->dct_offset); | |
1e491e29 | 668 | |
9b4b6e09 MN |
669 | if(s->picture){ |
670 | for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
671 | free_picture(s, &s->picture[i]); | |
672 | } | |
de6d9b64 | 673 | } |
b465449e | 674 | av_freep(&s->picture); |
d90cf87b | 675 | avcodec_default_free_buffers(s->avctx); |
de6d9b64 | 676 | s->context_initialized = 0; |
431f2172 MN |
677 | s->last_picture_ptr= |
678 | s->next_picture_ptr= | |
679 | s->current_picture_ptr= NULL; | |
0c9bbaec WH |
680 | for(i=0; i<3; i++) |
681 | if (s->visualization_buffer[i]) | |
682 | av_free(s->visualization_buffer[i]); | |
de6d9b64 FB |
683 | } |
684 | ||
7604246d WH |
685 | #ifdef CONFIG_ENCODERS |
686 | ||
de6d9b64 FB |
687 | /* init video encoder */ |
688 | int MPV_encode_init(AVCodecContext *avctx) | |
689 | { | |
690 | MpegEncContext *s = avctx->priv_data; | |
22ddd60b | 691 | int i, dummy; |
b1e6b355 | 692 | int chroma_h_shift, chroma_v_shift; |
bc657ac3 | 693 | |
36b58e85 RS |
694 | avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME |
695 | ||
de6d9b64 | 696 | s->bit_rate = avctx->bit_rate; |
de6d9b64 FB |
697 | s->width = avctx->width; |
698 | s->height = avctx->height; | |
7f2fe444 | 699 | if(avctx->gop_size > 600){ |
9b879566 | 700 | av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n"); |
7f2fe444 MN |
701 | avctx->gop_size=600; |
702 | } | |
de6d9b64 | 703 | s->gop_size = avctx->gop_size; |
477c35a9 | 704 | s->avctx = avctx; |
ba6802de | 705 | s->flags= avctx->flags; |
303e50e6 | 706 | s->flags2= avctx->flags2; |
9dbcbd92 | 707 | s->max_b_frames= avctx->max_b_frames; |
d7e9533a | 708 | s->codec_id= avctx->codec->id; |
7f2fe444 MN |
709 | s->luma_elim_threshold = avctx->luma_elim_threshold; |
710 | s->chroma_elim_threshold= avctx->chroma_elim_threshold; | |
711 | s->strict_std_compliance= avctx->strict_std_compliance; | |
712 | s->data_partitioning= avctx->flags & CODEC_FLAG_PART; | |
1457ab52 | 713 | s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0; |
87f8cab4 | 714 | s->mpeg_quant= avctx->mpeg_quant; |
ba58dabc | 715 | s->rtp_mode= !!avctx->rtp_payload_size; |
e4eadb4b | 716 | |
de6d9b64 FB |
717 | if (s->gop_size <= 1) { |
718 | s->intra_only = 1; | |
719 | s->gop_size = 12; | |
720 | } else { | |
721 | s->intra_only = 0; | |
722 | } | |
5e746b99 | 723 | |
1457ab52 | 724 | s->me_method = avctx->me_method; |
5e746b99 | 725 | |
e4986da9 | 726 | /* Fixed QSCALE */ |
bb198e19 | 727 | s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE); |
37fbfd0a | 728 | |
c5d309f2 | 729 | s->adaptive_quant= ( s->avctx->lumi_masking |
5e746b99 | 730 | || s->avctx->dark_masking |
c5d309f2 MN |
731 | || s->avctx->temporal_cplx_masking |
732 | || s->avctx->spatial_cplx_masking | |
1f26c6f3 MN |
733 | || s->avctx->p_masking |
734 | || (s->flags&CODEC_FLAG_QP_RD)) | |
c5d309f2 | 735 | && !s->fixed_qscale; |
fcb48651 | 736 | |
bb198e19 MN |
737 | s->obmc= !!(s->flags & CODEC_FLAG_OBMC); |
738 | s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER); | |
739 | s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN); | |
5e746b99 | 740 | |
273977d8 MN |
741 | if(avctx->rc_max_rate && !avctx->rc_buffer_size){ |
742 | av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n"); | |
743 | return -1; | |
744 | } | |
745 | ||
746 | if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){ | |
747 | av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isnt recommanded!\n"); | |
748 | } | |
749 | ||
f7190f73 MN |
750 | if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4 |
751 | && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){ | |
9b879566 | 752 | av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n"); |
9acbbd16 MN |
753 | return -1; |
754 | } | |
273977d8 | 755 | |
f7190f73 MN |
756 | if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){ |
757 | av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n"); | |
758 | return -1; | |
759 | } | |
760 | ||
332f9ac4 MN |
761 | if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){ |
762 | av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n"); | |
763 | return -1; | |
764 | } | |
765 | ||
9acbbd16 | 766 | if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){ |
9b879566 | 767 | av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n"); |
9acbbd16 MN |
768 | return -1; |
769 | } | |
770 | ||
771 | if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){ | |
9b879566 | 772 | av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n"); |
9acbbd16 MN |
773 | return -1; |
774 | } | |
775 | ||
029911d1 | 776 | if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){ |
9b879566 | 777 | av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n"); |
9acbbd16 MN |
778 | return -1; |
779 | } | |
e4eadb4b | 780 | |
9acbbd16 | 781 | if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too |
9b879566 | 782 | av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n"); |
9acbbd16 MN |
783 | return -1; |
784 | } | |
e4eadb4b | 785 | |
f2f6134b | 786 | if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){ |
9b879566 | 787 | av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n"); |
f2f6134b MN |
788 | return -1; |
789 | } | |
790 | ||
dd3e415e MN |
791 | if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){ |
792 | av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n"); | |
793 | return -1; | |
794 | } | |
795 | ||
303e50e6 MN |
796 | if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){ |
797 | av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet\n"); | |
798 | return -1; | |
799 | } | |
11a8a71d MN |
800 | |
801 | i= ff_gcd(avctx->frame_rate, avctx->frame_rate_base); | |
802 | if(i > 1){ | |
803 | av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n"); | |
804 | avctx->frame_rate /= i; | |
805 | avctx->frame_rate_base /= i; | |
806 | // return -1; | |
807 | } | |
303e50e6 | 808 | |
1984f635 MN |
809 | if(s->codec_id==CODEC_ID_MJPEG){ |
810 | s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x | |
811 | s->inter_quant_bias= 0; | |
029911d1 | 812 | }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){ |
1984f635 MN |
813 | s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x |
814 | s->inter_quant_bias= 0; | |
815 | }else{ | |
816 | s->intra_quant_bias=0; | |
817 | s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x | |
818 | } | |
819 | ||
820 | if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS) | |
821 | s->intra_quant_bias= avctx->intra_quant_bias; | |
822 | if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS) | |
823 | s->inter_quant_bias= avctx->inter_quant_bias; | |
b1e6b355 MN |
824 | |
825 | avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift); | |
826 | ||
22ddd60b MN |
827 | av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1); |
828 | s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1; | |
829 | ||
de6d9b64 FB |
830 | switch(avctx->codec->id) { |
831 | case CODEC_ID_MPEG1VIDEO: | |
832 | s->out_format = FMT_MPEG1; | |
14bea432 MN |
833 | s->low_delay= 0; //s->max_b_frames ? 0 : 1; |
834 | avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); | |
de6d9b64 | 835 | break; |
029911d1 MN |
836 | case CODEC_ID_MPEG2VIDEO: |
837 | s->out_format = FMT_MPEG1; | |
838 | s->low_delay= 0; //s->max_b_frames ? 0 : 1; | |
839 | avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); | |
ba58dabc | 840 | s->rtp_mode= 1; |
029911d1 | 841 | break; |
b1e6b355 | 842 | case CODEC_ID_LJPEG: |
de6d9b64 FB |
843 | case CODEC_ID_MJPEG: |
844 | s->out_format = FMT_MJPEG; | |
845 | s->intra_only = 1; /* force intra only for jpeg */ | |
37fbfd0a | 846 | s->mjpeg_write_tables = 1; /* write all tables */ |
a69b930c | 847 | s->mjpeg_data_only_frames = 0; /* write all the needed headers */ |
b1e6b355 MN |
848 | s->mjpeg_vsample[0] = 1<<chroma_v_shift; |
849 | s->mjpeg_vsample[1] = 1; | |
37fbfd0a | 850 | s->mjpeg_vsample[2] = 1; |
b1e6b355 | 851 | s->mjpeg_hsample[0] = 1<<chroma_h_shift; |
37fbfd0a J |
852 | s->mjpeg_hsample[1] = 1; |
853 | s->mjpeg_hsample[2] = 1; | |
de6d9b64 FB |
854 | if (mjpeg_init(s) < 0) |
855 | return -1; | |
1ff662cc | 856 | avctx->delay=0; |
4e00e76b | 857 | s->low_delay=1; |
de6d9b64 | 858 | break; |
1d0d55da | 859 | #ifdef CONFIG_RISKY |
de6d9b64 | 860 | case CODEC_ID_H263: |
37fbfd0a | 861 | if (h263_get_picture_format(s->width, s->height) == 7) { |
9b879566 | 862 | av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n"); |
de6d9b64 | 863 | return -1; |
37fbfd0a | 864 | } |
de6d9b64 | 865 | s->out_format = FMT_H263; |
332f9ac4 | 866 | s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0; |
1ff662cc | 867 | avctx->delay=0; |
4e00e76b | 868 | s->low_delay=1; |
de6d9b64 FB |
869 | break; |
870 | case CODEC_ID_H263P: | |
871 | s->out_format = FMT_H263; | |
872 | s->h263_plus = 1; | |
21e59552 | 873 | /* Fx */ |
332f9ac4 | 874 | s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0; |
21e59552 | 875 | s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0; |
332f9ac4 | 876 | s->modified_quant= s->h263_aic; |
dba019da | 877 | s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0; |
332f9ac4 MN |
878 | s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0; |
879 | s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0; | |
880 | s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus; | |
ba58dabc | 881 | s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0; |
332f9ac4 | 882 | |
21e59552 | 883 | /* /Fx */ |
544286b3 | 884 | /* These are just to be sure */ |
1ff662cc | 885 | avctx->delay=0; |
4e00e76b | 886 | s->low_delay=1; |
de6d9b64 | 887 | break; |
d4f5d74a GM |
888 | case CODEC_ID_FLV1: |
889 | s->out_format = FMT_H263; | |
890 | s->h263_flv = 2; /* format = 1; 11-bit codes */ | |
891 | s->unrestricted_mv = 1; | |
892 | s->rtp_mode=0; /* don't allow GOB */ | |
893 | avctx->delay=0; | |
894 | s->low_delay=1; | |
895 | break; | |
de6d9b64 FB |
896 | case CODEC_ID_RV10: |
897 | s->out_format = FMT_H263; | |
1ff662cc | 898 | avctx->delay=0; |
4e00e76b | 899 | s->low_delay=1; |
de6d9b64 | 900 | break; |
58f26ba9 | 901 | case CODEC_ID_MPEG4: |
de6d9b64 FB |
902 | s->out_format = FMT_H263; |
903 | s->h263_pred = 1; | |
904 | s->unrestricted_mv = 1; | |
4e00e76b | 905 | s->low_delay= s->max_b_frames ? 0 : 1; |
4d2858de | 906 | avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); |
de6d9b64 | 907 | break; |
84afee34 | 908 | case CODEC_ID_MSMPEG4V1: |
de6d9b64 FB |
909 | s->out_format = FMT_H263; |
910 | s->h263_msmpeg4 = 1; | |
911 | s->h263_pred = 1; | |
912 | s->unrestricted_mv = 1; | |
84afee34 | 913 | s->msmpeg4_version= 1; |
1ff662cc | 914 | avctx->delay=0; |
4e00e76b | 915 | s->low_delay=1; |
84afee34 MN |
916 | break; |
917 | case CODEC_ID_MSMPEG4V2: | |
918 | s->out_format = FMT_H263; | |
919 | s->h263_msmpeg4 = 1; | |
920 | s->h263_pred = 1; | |
921 | s->unrestricted_mv = 1; | |
922 | s->msmpeg4_version= 2; | |
1ff662cc | 923 | avctx->delay=0; |
4e00e76b | 924 | s->low_delay=1; |
84afee34 MN |
925 | break; |
926 | case CODEC_ID_MSMPEG4V3: | |
927 | s->out_format = FMT_H263; | |
928 | s->h263_msmpeg4 = 1; | |
929 | s->h263_pred = 1; | |
930 | s->unrestricted_mv = 1; | |
931 | s->msmpeg4_version= 3; | |
1f9aea9b | 932 | s->flipflop_rounding=1; |
1ff662cc | 933 | avctx->delay=0; |
4e00e76b | 934 | s->low_delay=1; |
de6d9b64 | 935 | break; |
f5957f3f MN |
936 | case CODEC_ID_WMV1: |
937 | s->out_format = FMT_H263; | |
938 | s->h263_msmpeg4 = 1; | |
939 | s->h263_pred = 1; | |
940 | s->unrestricted_mv = 1; | |
941 | s->msmpeg4_version= 4; | |
1f9aea9b | 942 | s->flipflop_rounding=1; |
f5957f3f | 943 | avctx->delay=0; |
4e00e76b | 944 | s->low_delay=1; |
f5957f3f MN |
945 | break; |
946 | case CODEC_ID_WMV2: | |
947 | s->out_format = FMT_H263; | |
948 | s->h263_msmpeg4 = 1; | |
949 | s->h263_pred = 1; | |
950 | s->unrestricted_mv = 1; | |
951 | s->msmpeg4_version= 5; | |
1f9aea9b | 952 | s->flipflop_rounding=1; |
f5957f3f | 953 | avctx->delay=0; |
4e00e76b | 954 | s->low_delay=1; |
f5957f3f | 955 | break; |
1d0d55da | 956 | #endif |
de6d9b64 FB |
957 | default: |
958 | return -1; | |
959 | } | |
2c492e94 | 960 | |
45870f57 MN |
961 | { /* set up some save defaults, some codecs might override them later */ |
962 | static int done=0; | |
963 | if(!done){ | |
964 | int i; | |
965 | done=1; | |
bbed9259 | 966 | |
30952237 MN |
967 | default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) ); |
968 | memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1)); | |
0c1a9eda | 969 | memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1)); |
45870f57 MN |
970 | |
971 | for(i=-16; i<16; i++){ | |
972 | default_fcode_tab[i + MAX_MV]= 1; | |
973 | } | |
974 | } | |
975 | } | |
1457ab52 | 976 | s->me.mv_penalty= default_mv_penalty; |
45870f57 | 977 | s->fcode_tab= default_fcode_tab; |
a406617f | 978 | |
9d2a0355 | 979 | /* dont use mv_penalty table for crap MV as it would be confused */ |
1457ab52 MN |
980 | //FIXME remove after fixing / removing old ME |
981 | if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty; | |
9d2a0355 | 982 | |
3bb4e23a FB |
983 | s->encoding = 1; |
984 | ||
de6d9b64 FB |
985 | /* init */ |
986 | if (MPV_common_init(s) < 0) | |
987 | return -1; | |
dd5e90cd MN |
988 | |
989 | if(s->modified_quant) | |
990 | s->chroma_qscale_table= ff_h263_chroma_qscale_table; | |
991 | s->progressive_frame= | |
bb198e19 | 992 | s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)); |
de6d9b64 | 993 | |
622348f9 MN |
994 | ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp); |
995 | ||
1457ab52 MN |
996 | ff_init_me(s); |
997 | ||
bbed9259 | 998 | #ifdef CONFIG_ENCODERS |
1d0d55da | 999 | #ifdef CONFIG_RISKY |
2ad1516a MN |
1000 | if (s->out_format == FMT_H263) |
1001 | h263_encode_init(s); | |
2ad1516a MN |
1002 | if(s->msmpeg4_version) |
1003 | ff_msmpeg4_encode_init(s); | |
bbed9259 | 1004 | #endif |
1d0d55da MN |
1005 | if (s->out_format == FMT_MPEG1) |
1006 | ff_mpeg1_encode_init(s); | |
1007 | #endif | |
2ad1516a | 1008 | |
519c2b6d FB |
1009 | /* init default q matrix */ |
1010 | for(i=0;i<64;i++) { | |
b0368839 | 1011 | int j= s->dsp.idct_permutation[i]; |
1d0d55da | 1012 | #ifdef CONFIG_RISKY |
87f8cab4 | 1013 | if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){ |
2ad1516a MN |
1014 | s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i]; |
1015 | s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i]; | |
87f8cab4 | 1016 | }else if(s->out_format == FMT_H263){ |
2ad1516a MN |
1017 | s->intra_matrix[j] = |
1018 | s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i]; | |
1d0d55da MN |
1019 | }else |
1020 | #endif | |
029911d1 | 1021 | { /* mpeg1/2 */ |
2ad1516a MN |
1022 | s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i]; |
1023 | s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i]; | |
87f8cab4 | 1024 | } |
d6eb3c50 MN |
1025 | if(s->avctx->intra_matrix) |
1026 | s->intra_matrix[j] = s->avctx->intra_matrix[i]; | |
1027 | if(s->avctx->inter_matrix) | |
1028 | s->inter_matrix[j] = s->avctx->inter_matrix[i]; | |
d7e9533a MN |
1029 | } |
1030 | ||
1031 | /* precompute matrix */ | |
ef5b1b5a | 1032 | /* for mjpeg, we do include qscale in the matrix */ |
d7e9533a | 1033 | if (s->out_format != FMT_MJPEG) { |
6b56c616 | 1034 | convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, |
cc7ac888 | 1035 | s->intra_matrix, s->intra_quant_bias, 1, 31); |
6b56c616 | 1036 | convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16, |
cc7ac888 | 1037 | s->inter_matrix, s->inter_quant_bias, 1, 31); |
519c2b6d FB |
1038 | } |
1039 | ||
8b4c7dbc MN |
1040 | if(ff_rate_control_init(s) < 0) |
1041 | return -1; | |
de6d9b64 FB |
1042 | |
1043 | s->picture_number = 0; | |
1031fabd | 1044 | s->input_picture_number = 0; |
45870f57 | 1045 | s->picture_in_gop_number = 0; |
de6d9b64 FB |
1046 | /* motion detector init */ |
1047 | s->f_code = 1; | |
9dbcbd92 | 1048 | s->b_code = 1; |
de6d9b64 FB |
1049 | |
1050 | return 0; | |
1051 | } | |
1052 | ||
1053 | int MPV_encode_end(AVCodecContext *avctx) | |
1054 | { | |
1055 | MpegEncContext *s = avctx->priv_data; | |
1056 | ||
1057 | #ifdef STATS | |
1058 | print_stats(); | |
1059 | #endif | |
8b4c7dbc MN |
1060 | |
1061 | ff_rate_control_uninit(s); | |
1062 | ||
de6d9b64 FB |
1063 | MPV_common_end(s); |
1064 | if (s->out_format == FMT_MJPEG) | |
1065 | mjpeg_close(s); | |
541ae140 | 1066 | |
22ddd60b | 1067 | av_freep(&avctx->extradata); |
37fbfd0a | 1068 | |
de6d9b64 FB |
1069 | return 0; |
1070 | } | |
1071 | ||
7604246d WH |
1072 | #endif //CONFIG_ENCODERS |
1073 | ||
1d0d55da MN |
1074 | void init_rl(RLTable *rl) |
1075 | { | |
0c1a9eda ZK |
1076 | int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1]; |
1077 | uint8_t index_run[MAX_RUN+1]; | |
1d0d55da MN |
1078 | int last, run, level, start, end, i; |
1079 | ||
1080 | /* compute max_level[], max_run[] and index_run[] */ | |
1081 | for(last=0;last<2;last++) { | |
1082 | if (last == 0) { | |
1083 | start = 0; | |
1084 | end = rl->last; | |
1085 | } else { | |
1086 | start = rl->last; | |
1087 | end = rl->n; | |
1088 | } | |
1089 | ||
1090 | memset(max_level, 0, MAX_RUN + 1); | |
1091 | memset(max_run, 0, MAX_LEVEL + 1); | |
1092 | memset(index_run, rl->n, MAX_RUN + 1); | |
1093 | for(i=start;i<end;i++) { | |
1094 | run = rl->table_run[i]; | |
1095 | level = rl->table_level[i]; | |
1096 | if (index_run[run] == rl->n) | |
1097 | index_run[run] = i; | |
1098 | if (level > max_level[run]) | |
1099 | max_level[run] = level; | |
1100 | if (run > max_run[level]) | |
1101 | max_run[level] = run; | |
1102 | } | |
1103 | rl->max_level[last] = av_malloc(MAX_RUN + 1); | |
1104 | memcpy(rl->max_level[last], max_level, MAX_RUN + 1); | |
1105 | rl->max_run[last] = av_malloc(MAX_LEVEL + 1); | |
1106 | memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1); | |
1107 | rl->index_run[last] = av_malloc(MAX_RUN + 1); | |
1108 | memcpy(rl->index_run[last], index_run, MAX_RUN + 1); | |
1109 | } | |
1110 | } | |
1111 | ||
de6d9b64 | 1112 | /* draw the edges of width 'w' of an image of size width, height */ |
fd7db0fd | 1113 | //FIXME check that this is ok for mpeg4 interlaced |
0c1a9eda | 1114 | static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w) |
de6d9b64 | 1115 | { |
0c1a9eda | 1116 | uint8_t *ptr, *last_line; |
de6d9b64 FB |
1117 | int i; |
1118 | ||
1119 | last_line = buf + (height - 1) * wrap; | |
1120 | for(i=0;i<w;i++) { | |
1121 | /* top and bottom */ | |
1122 | memcpy(buf - (i + 1) * wrap, buf, width); | |
1123 | memcpy(last_line + (i + 1) * wrap, last_line, width); | |
1124 | } | |
1125 | /* left and right */ | |
1126 | ptr = buf; | |
1127 | for(i=0;i<height;i++) { | |
1128 | memset(ptr - w, ptr[0], w); | |
1129 | memset(ptr + width, ptr[width-1], w); | |
1130 | ptr += wrap; | |
1131 | } | |
1132 | /* corners */ | |
1133 | for(i=0;i<w;i++) { | |
1134 | memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
1135 | memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
1136 | memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
1137 | memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
1138 | } | |
1139 | } | |
1140 | ||
5f194811 | 1141 | int ff_find_unused_picture(MpegEncContext *s, int shared){ |
4e00e76b MN |
1142 | int i; |
1143 | ||
1144 | if(shared){ | |
1145 | for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
5f194811 | 1146 | if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i; |
4e00e76b MN |
1147 | } |
1148 | }else{ | |
1149 | for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
5f194811 | 1150 | if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME |
4e00e76b MN |
1151 | } |
1152 | for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
5f194811 | 1153 | if(s->picture[i].data[0]==NULL) return i; |
4e00e76b MN |
1154 | } |
1155 | } | |
1156 | ||
5f194811 MN |
1157 | assert(0); |
1158 | return -1; | |
4e00e76b MN |
1159 | } |
1160 | ||
821cb11f MN |
1161 | static void update_noise_reduction(MpegEncContext *s){ |
1162 | int intra, i; | |
1163 | ||
1164 | for(intra=0; intra<2; intra++){ | |
1165 | if(s->dct_count[intra] > (1<<16)){ | |
1166 | for(i=0; i<64; i++){ | |
1167 | s->dct_error_sum[intra][i] >>=1; | |
1168 | } | |
1169 | s->dct_count[intra] >>= 1; | |
1170 | } | |
1171 | ||
1172 | for(i=0; i<64; i++){ | |
1173 | 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); | |
1174 | } | |
1175 | } | |
1176 | } | |
1177 | ||
5f194811 MN |
1178 | /** |
1179 | * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded | |
1180 | */ | |
d6db1c9c | 1181 | int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) |
de6d9b64 | 1182 | { |
4e00e76b | 1183 | int i; |
492cd3a9 | 1184 | AVFrame *pic; |
425dddb7 | 1185 | s->mb_skiped = 0; |
0da71265 | 1186 | |
8b82a956 | 1187 | assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3); |
e20c4069 | 1188 | |
1e491e29 | 1189 | /* mark&release old frames */ |
59c673d5 | 1190 | if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) { |
b536d0aa | 1191 | avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr); |
1e491e29 MN |
1192 | |
1193 | /* release forgotten pictures */ | |
1194 | /* if(mpeg124/h263) */ | |
1195 | if(!s->encoding){ | |
1196 | for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
b536d0aa | 1197 | if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){ |
9b879566 | 1198 | av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n"); |
492cd3a9 | 1199 | avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]); |
1e491e29 MN |
1200 | } |
1201 | } | |
d6db1c9c | 1202 | } |
93a21abd | 1203 | } |
aa388dba MN |
1204 | alloc: |
1205 | if(!s->encoding){ | |
e20c4069 MN |
1206 | /* release non refernce frames */ |
1207 | for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
1208 | if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){ | |
1209 | s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]); | |
1210 | } | |
1211 | } | |
1212 | ||
5f194811 MN |
1213 | if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL) |
1214 | pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header) | |
1215 | else{ | |
1216 | i= ff_find_unused_picture(s, 0); | |
1217 | pic= (AVFrame*)&s->picture[i]; | |
1218 | } | |
1219 | ||
0da71265 | 1220 | pic->reference= s->pict_type != B_TYPE ? 3 : 0; |
b536d0aa | 1221 | |
1031fabd | 1222 | pic->coded_picture_number= s->coded_picture_number++; |
1e491e29 | 1223 | |
f23a68df IK |
1224 | if( alloc_picture(s, (Picture*)pic, 0) < 0) |
1225 | return -1; | |
93a21abd | 1226 | |
5f194811 | 1227 | s->current_picture_ptr= (Picture*)pic; |
c70f1716 | 1228 | s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic |
2be9f03a | 1229 | s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence; |
1e491e29 | 1230 | } |
b7adc711 | 1231 | |
9f2e61b6 | 1232 | s->current_picture_ptr->pict_type= s->pict_type; |
158c7f05 MN |
1233 | // if(s->flags && CODEC_FLAG_QSCALE) |
1234 | // s->current_picture_ptr->quality= s->new_picture_ptr->quality; | |
7bc9090a | 1235 | s->current_picture_ptr->key_frame= s->pict_type == I_TYPE; |
9f2e61b6 | 1236 | |
6571e41d | 1237 | copy_picture(&s->current_picture, s->current_picture_ptr); |
9f2e61b6 | 1238 | |
8b82a956 | 1239 | if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){ |
1e491e29 | 1240 | if (s->pict_type != B_TYPE) { |
b536d0aa MN |
1241 | s->last_picture_ptr= s->next_picture_ptr; |
1242 | s->next_picture_ptr= s->current_picture_ptr; | |
de6d9b64 | 1243 | } |
d90cf87b | 1244 | |
6571e41d MN |
1245 | if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr); |
1246 | if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr); | |
aa388dba | 1247 | |
6571e41d | 1248 | if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){ |
9b879566 | 1249 | av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n"); |
ffba1dc0 MN |
1250 | assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference |
1251 | goto alloc; | |
1252 | } | |
1253 | ||
1254 | assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0])); | |
1255 | ||
b536d0aa MN |
1256 | if(s->picture_structure!=PICT_FRAME){ |
1257 | int i; | |
1258 | for(i=0; i<4; i++){ | |
1259 | if(s->picture_structure == PICT_BOTTOM_FIELD){ | |
1260 | s->current_picture.data[i] += s->current_picture.linesize[i]; | |
1261 | } | |
1262 | s->current_picture.linesize[i] *= 2; | |
1263 | s->last_picture.linesize[i] *=2; | |
1264 | s->next_picture.linesize[i] *=2; | |
1265 | } | |
1266 | } | |
0da71265 | 1267 | } |
1e491e29 | 1268 | |
aa388dba MN |
1269 | s->hurry_up= s->avctx->hurry_up; |
1270 | s->error_resilience= avctx->error_resilience; | |
1271 | ||
d930ef19 MN |
1272 | /* set dequantizer, we cant do it during init as it might change for mpeg4 |
1273 | and we cant do it in the header decode as init isnt called for mpeg4 there yet */ | |
d50635cd MN |
1274 | if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){ |
1275 | s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra; | |
1276 | s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter; | |
1277 | }else if(s->out_format == FMT_H263){ | |
1278 | s->dct_unquantize_intra = s->dct_unquantize_h263_intra; | |
1279 | s->dct_unquantize_inter = s->dct_unquantize_h263_inter; | |
1280 | }else{ | |
1281 | s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra; | |
1282 | s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter; | |
1283 | } | |
d6db1c9c | 1284 | |
821cb11f MN |
1285 | if(s->dct_error_sum){ |
1286 | assert(s->avctx->noise_reduction && s->encoding); | |
1287 | ||
1288 | update_noise_reduction(s); | |
1289 | } | |
1290 | ||
2e7b4c84 IK |
1291 | #ifdef HAVE_XVMC |
1292 | if(s->avctx->xvmc_acceleration) | |
1293 | return XVMC_field_start(s, avctx); | |
1294 | #endif | |
d6db1c9c | 1295 | return 0; |
de6d9b64 | 1296 | } |
21af69f7 | 1297 | |
de6d9b64 FB |
1298 | /* generic function for encode/decode called after a frame has been coded/decoded */ |
1299 | void MPV_frame_end(MpegEncContext *s) | |
1300 | { | |
1e491e29 | 1301 | int i; |
de6d9b64 | 1302 | /* draw edge for correct motion prediction if outside */ |
2e7b4c84 IK |
1303 | #ifdef HAVE_XVMC |
1304 | //just to make sure that all data is rendered. | |
1305 | if(s->avctx->xvmc_acceleration){ | |
1306 | XVMC_field_end(s); | |
1307 | }else | |
1308 | #endif | |
a573cc27 | 1309 | if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) { |
1e491e29 MN |
1310 | draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH ); |
1311 | draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); | |
1312 | draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); | |
de6d9b64 | 1313 | } |
5975626d | 1314 | emms_c(); |
8b4c7dbc | 1315 | |
3aa102be | 1316 | s->last_pict_type = s->pict_type; |
8b4c7dbc MN |
1317 | if(s->pict_type!=B_TYPE){ |
1318 | s->last_non_b_pict_type= s->pict_type; | |
8b4c7dbc | 1319 | } |
b536d0aa MN |
1320 | #if 0 |
1321 | /* copy back current_picture variables */ | |
1e491e29 MN |
1322 | for(i=0; i<MAX_PICTURE_COUNT; i++){ |
1323 | if(s->picture[i].data[0] == s->current_picture.data[0]){ | |
1324 | s->picture[i]= s->current_picture; | |
1325 | break; | |
1326 | } | |
1327 | } | |
1328 | assert(i<MAX_PICTURE_COUNT); | |
b536d0aa | 1329 | #endif |
1e491e29 | 1330 | |
e20c4069 MN |
1331 | if(s->encoding){ |
1332 | /* release non refernce frames */ | |
1333 | for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
1334 | if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){ | |
1335 | s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]); | |
1336 | } | |
1337 | } | |
1e491e29 | 1338 | } |
b536d0aa MN |
1339 | // clear copies, to avoid confusion |
1340 | #if 0 | |
1341 | memset(&s->last_picture, 0, sizeof(Picture)); | |
1342 | memset(&s->next_picture, 0, sizeof(Picture)); | |
1343 | memset(&s->current_picture, 0, sizeof(Picture)); | |
1344 | #endif | |
de6d9b64 FB |
1345 | } |
1346 | ||
7bc9090a | 1347 | /** |
db6e7795 MN |
1348 | * draws an line from (ex, ey) -> (sx, sy). |
1349 | * @param w width of the image | |
1350 | * @param h height of the image | |
1351 | * @param stride stride/linesize of the image | |
1352 | * @param color color of the arrow | |
1353 | */ | |
1354 | static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ | |
1355 | int t, x, y, f; | |
1356 | ||
1357 | sx= clip(sx, 0, w-1); | |
1358 | sy= clip(sy, 0, h-1); | |
1359 | ex= clip(ex, 0, w-1); | |
1360 | ey= clip(ey, 0, h-1); | |
1361 | ||
1362 | buf[sy*stride + sx]+= color; | |
1363 | ||
1364 | if(ABS(ex - sx) > ABS(ey - sy)){ | |
1365 | if(sx > ex){ | |
1366 | t=sx; sx=ex; ex=t; | |
1367 | t=sy; sy=ey; ey=t; | |
1368 | } | |
1369 | buf+= sx + sy*stride; | |
1370 | ex-= sx; | |
1371 | f= ((ey-sy)<<16)/ex; | |
1372 | for(x= 0; x <= ex; x++){ | |
1373 | y= ((x*f) + (1<<15))>>16; | |
1374 | buf[y*stride + x]+= color; | |
1375 | } | |
1376 | }else{ | |
1377 | if(sy > ey){ | |
1378 | t=sx; sx=ex; ex=t; | |
1379 | t=sy; sy=ey; ey=t; | |
1380 | } | |
1381 | buf+= sx + sy*stride; | |
1382 | ey-= sy; | |
1383 | if(ey) f= ((ex-sx)<<16)/ey; | |
1384 | else f= 0; | |
1385 | for(y= 0; y <= ey; y++){ | |
1386 | x= ((y*f) + (1<<15))>>16; | |
1387 | buf[y*stride + x]+= color; | |
1388 | } | |
1389 | } | |
1390 | } | |
1391 | ||
1392 | /** | |
1393 | * draws an arrow from (ex, ey) -> (sx, sy). | |
1394 | * @param w width of the image | |
1395 | * @param h height of the image | |
1396 | * @param stride stride/linesize of the image | |
1397 | * @param color color of the arrow | |
1398 | */ | |
1399 | static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ | |
1400 | int dx,dy; | |
1401 | ||
1402 | sx= clip(sx, -100, w+100); | |
1403 | sy= clip(sy, -100, h+100); | |
1404 | ex= clip(ex, -100, w+100); | |
1405 | ey= clip(ey, -100, h+100); | |
1406 | ||
1407 | dx= ex - sx; | |
1408 | dy= ey - sy; | |
1409 | ||
1410 | if(dx*dx + dy*dy > 3*3){ | |
1411 | int rx= dx + dy; | |
1412 | int ry= -dx + dy; | |
1413 | int length= ff_sqrt((rx*rx + ry*ry)<<8); | |
1414 | ||
1415 | //FIXME subpixel accuracy | |
1416 | rx= ROUNDED_DIV(rx*3<<4, length); | |
1417 | ry= ROUNDED_DIV(ry*3<<4, length); | |
1418 | ||
1419 | draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color); | |
1420 | draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color); | |
1421 | } | |
1422 | draw_line(buf, sx, sy, ex, ey, w, h, stride, color); | |
1423 | } | |
1424 | ||
1425 | /** | |
7bc9090a MN |
1426 | * prints debuging info for the given picture. |
1427 | */ | |
0c9bbaec | 1428 | void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){ |
7bc9090a MN |
1429 | |
1430 | if(!pict || !pict->mb_type) return; | |
1431 | ||
1432 | if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){ | |
1433 | int x,y; | |
0c9bbaec WH |
1434 | |
1435 | av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: "); | |
1436 | switch (pict->pict_type) { | |
1437 | case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break; | |
1438 | case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break; | |
1439 | case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break; | |
1440 | case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break; | |
1441 | case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break; | |
1442 | case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break; | |
1443 | } | |
7bc9090a MN |
1444 | for(y=0; y<s->mb_height; y++){ |
1445 | for(x=0; x<s->mb_width; x++){ | |
1446 | if(s->avctx->debug&FF_DEBUG_SKIP){ | |
1447 | int count= s->mbskip_table[x + y*s->mb_stride]; | |
1448 | if(count>9) count=9; | |
9b879566 | 1449 | av_log(s->avctx, AV_LOG_DEBUG, "%1d", count); |
7bc9090a MN |
1450 | } |
1451 | if(s->avctx->debug&FF_DEBUG_QP){ | |
9b879566 | 1452 | av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]); |
7bc9090a MN |
1453 | } |
1454 | if(s->avctx->debug&FF_DEBUG_MB_TYPE){ | |
1455 | int mb_type= pict->mb_type[x + y*s->mb_stride]; | |
7bc9090a MN |
1456 | //Type & MV direction |
1457 | if(IS_PCM(mb_type)) | |
9b879566 | 1458 | av_log(s->avctx, AV_LOG_DEBUG, "P"); |
7bc9090a | 1459 | else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type)) |
9b879566 | 1460 | av_log(s->avctx, AV_LOG_DEBUG, "A"); |
7bc9090a | 1461 | else if(IS_INTRA4x4(mb_type)) |
9b879566 | 1462 | av_log(s->avctx, AV_LOG_DEBUG, "i"); |
7bc9090a | 1463 | else if(IS_INTRA16x16(mb_type)) |
9b879566 | 1464 | av_log(s->avctx, AV_LOG_DEBUG, "I"); |
7bc9090a | 1465 | else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)) |
9b879566 | 1466 | av_log(s->avctx, AV_LOG_DEBUG, "d"); |
7bc9090a | 1467 | else if(IS_DIRECT(mb_type)) |
9b879566 | 1468 | av_log(s->avctx, AV_LOG_DEBUG, "D"); |
7bc9090a | 1469 | else if(IS_GMC(mb_type) && IS_SKIP(mb_type)) |
9b879566 | 1470 | av_log(s->avctx, AV_LOG_DEBUG, "g"); |
7bc9090a | 1471 | else if(IS_GMC(mb_type)) |
9b879566 | 1472 | av_log(s->avctx, AV_LOG_DEBUG, "G"); |
7bc9090a | 1473 | else if(IS_SKIP(mb_type)) |
9b879566 | 1474 | av_log(s->avctx, AV_LOG_DEBUG, "S"); |
7bc9090a | 1475 | else if(!USES_LIST(mb_type, 1)) |
9b879566 | 1476 | av_log(s->avctx, AV_LOG_DEBUG, ">"); |
7bc9090a | 1477 | else if(!USES_LIST(mb_type, 0)) |
9b879566 | 1478 | av_log(s->avctx, AV_LOG_DEBUG, "<"); |
7bc9090a MN |
1479 | else{ |
1480 | assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1)); | |
9b879566 | 1481 | av_log(s->avctx, AV_LOG_DEBUG, "X"); |
7bc9090a MN |
1482 | } |
1483 | ||
1484 | //segmentation | |
1485 | if(IS_8X8(mb_type)) | |
9b879566 | 1486 | av_log(s->avctx, AV_LOG_DEBUG, "+"); |
7bc9090a | 1487 | else if(IS_16X8(mb_type)) |
9b879566 | 1488 | av_log(s->avctx, AV_LOG_DEBUG, "-"); |
7bc9090a | 1489 | else if(IS_8X16(mb_type)) |
9b879566 | 1490 |