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