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