Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * The simplest mpeg encoder (well, it was the simplest!) | |
406792e7 | 3 | * Copyright (c) 2000,2001 Fabrice Bellard |
8f2ab833 | 4 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> |
de6d9b64 | 5 | * |
7b94177e DB |
6 | * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at> |
7 | * | |
2912e87a | 8 | * This file is part of Libav. |
b78e7197 | 9 | * |
2912e87a | 10 | * Libav is free software; you can redistribute it and/or |
ff4ec49e FB |
11 | * modify it under the terms of the GNU Lesser General Public |
12 | * License as published by the Free Software Foundation; either | |
b78e7197 | 13 | * version 2.1 of the License, or (at your option) any later version. |
de6d9b64 | 14 | * |
2912e87a | 15 | * Libav is distributed in the hope that it will be useful, |
de6d9b64 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ff4ec49e FB |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | * Lesser General Public License for more details. | |
de6d9b64 | 19 | * |
ff4ec49e | 20 | * You should have received a copy of the GNU Lesser General Public |
2912e87a | 21 | * License along with Libav; if not, write to the Free Software |
5509bffa | 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 | 23 | */ |
115329f1 | 24 | |
983e3246 | 25 | /** |
ba87f080 | 26 | * @file |
983e3246 | 27 | * The simplest mpeg encoder (well, it was the simplest!). |
115329f1 DB |
28 | */ |
29 | ||
6fee1b90 | 30 | #include "libavutil/attributes.h" |
759001c5 | 31 | #include "libavutil/avassert.h" |
737eb597 | 32 | #include "libavutil/imgutils.h" |
19e30a58 | 33 | #include "libavutil/internal.h" |
fb0c9d41 | 34 | #include "libavutil/timer.h" |
de6d9b64 FB |
35 | #include "avcodec.h" |
36 | #include "dsputil.h" | |
603a5f04 | 37 | #include "internal.h" |
9734b8ba | 38 | #include "mathops.h" |
e0c16e4e | 39 | #include "mpegutils.h" |
de6d9b64 | 40 | #include "mpegvideo.h" |
d9c9259f | 41 | #include "mjpegenc.h" |
15025553 | 42 | #include "msmpeg4.h" |
4440bd0d | 43 | #include "xvmc_internal.h" |
6a9c8594 | 44 | #include "thread.h" |
e96682e6 | 45 | #include <limits.h> |
de6d9b64 | 46 | |
363114e8 KT |
47 | static const uint8_t ff_default_chroma_qscale_table[32] = { |
48 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
49 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
50 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 | |
332f9ac4 MN |
51 | }; |
52 | ||
363114e8 KT |
53 | const uint8_t ff_mpeg1_dc_scale_table[128] = { |
54 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
55 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
56 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
57 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
58 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
59 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
60 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
61 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
62 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
6cbe71bd AJ |
63 | }; |
64 | ||
363114e8 KT |
65 | static const uint8_t mpeg2_dc_scale_table1[128] = { |
66 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
67 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
68 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
69 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
70 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
71 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
72 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
73 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
74 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
6506c3d2 MN |
75 | }; |
76 | ||
363114e8 KT |
77 | static const uint8_t mpeg2_dc_scale_table2[128] = { |
78 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
79 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
80 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
81 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
82 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
83 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
84 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
85 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
86 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
6506c3d2 MN |
87 | }; |
88 | ||
363114e8 KT |
89 | static const uint8_t mpeg2_dc_scale_table3[128] = { |
90 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
91 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
92 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
93 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
94 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
95 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
96 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
97 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
98 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
6506c3d2 MN |
99 | }; |
100 | ||
363114e8 | 101 | const uint8_t *const ff_mpeg2_dc_scale_table[4] = { |
6506c3d2 MN |
102 | ff_mpeg1_dc_scale_table, |
103 | mpeg2_dc_scale_table1, | |
104 | mpeg2_dc_scale_table2, | |
105 | mpeg2_dc_scale_table3, | |
106 | }; | |
107 | ||
a4d0c6e0 AK |
108 | static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, |
109 | int16_t *block, int n, int qscale) | |
110 | { | |
111 | int i, level, nCoeffs; | |
112 | const uint16_t *quant_matrix; | |
113 | ||
114 | nCoeffs= s->block_last_index[n]; | |
115 | ||
116 | if (n < 4) | |
117 | block[0] = block[0] * s->y_dc_scale; | |
118 | else | |
119 | block[0] = block[0] * s->c_dc_scale; | |
120 | /* XXX: only mpeg1 */ | |
121 | quant_matrix = s->intra_matrix; | |
122 | for(i=1;i<=nCoeffs;i++) { | |
123 | int j= s->intra_scantable.permutated[i]; | |
124 | level = block[j]; | |
125 | if (level) { | |
126 | if (level < 0) { | |
127 | level = -level; | |
128 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
129 | level = (level - 1) | 1; | |
130 | level = -level; | |
131 | } else { | |
132 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
133 | level = (level - 1) | 1; | |
134 | } | |
135 | block[j] = level; | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, | |
141 | int16_t *block, int n, int qscale) | |
142 | { | |
143 | int i, level, nCoeffs; | |
144 | const uint16_t *quant_matrix; | |
145 | ||
146 | nCoeffs= s->block_last_index[n]; | |
147 | ||
148 | quant_matrix = s->inter_matrix; | |
149 | for(i=0; i<=nCoeffs; i++) { | |
150 | int j= s->intra_scantable.permutated[i]; | |
151 | level = block[j]; | |
152 | if (level) { | |
153 | if (level < 0) { | |
154 | level = -level; | |
155 | level = (((level << 1) + 1) * qscale * | |
156 | ((int) (quant_matrix[j]))) >> 4; | |
157 | level = (level - 1) | 1; | |
158 | level = -level; | |
159 | } else { | |
160 | level = (((level << 1) + 1) * qscale * | |
161 | ((int) (quant_matrix[j]))) >> 4; | |
162 | level = (level - 1) | 1; | |
163 | } | |
164 | block[j] = level; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, | |
170 | int16_t *block, int n, int qscale) | |
171 | { | |
172 | int i, level, nCoeffs; | |
173 | const uint16_t *quant_matrix; | |
174 | ||
175 | if(s->alternate_scan) nCoeffs= 63; | |
176 | else nCoeffs= s->block_last_index[n]; | |
177 | ||
178 | if (n < 4) | |
179 | block[0] = block[0] * s->y_dc_scale; | |
180 | else | |
181 | block[0] = block[0] * s->c_dc_scale; | |
182 | quant_matrix = s->intra_matrix; | |
183 | for(i=1;i<=nCoeffs;i++) { | |
184 | int j= s->intra_scantable.permutated[i]; | |
185 | level = block[j]; | |
186 | if (level) { | |
187 | if (level < 0) { | |
188 | level = -level; | |
189 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
190 | level = -level; | |
191 | } else { | |
192 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
193 | } | |
194 | block[j] = level; | |
195 | } | |
196 | } | |
197 | } | |
198 | ||
199 | static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s, | |
200 | int16_t *block, int n, int qscale) | |
201 | { | |
202 | int i, level, nCoeffs; | |
203 | const uint16_t *quant_matrix; | |
204 | int sum=-1; | |
205 | ||
206 | if(s->alternate_scan) nCoeffs= 63; | |
207 | else nCoeffs= s->block_last_index[n]; | |
208 | ||
209 | if (n < 4) | |
210 | block[0] = block[0] * s->y_dc_scale; | |
211 | else | |
212 | block[0] = block[0] * s->c_dc_scale; | |
213 | quant_matrix = s->intra_matrix; | |
214 | for(i=1;i<=nCoeffs;i++) { | |
215 | int j= s->intra_scantable.permutated[i]; | |
216 | level = block[j]; | |
217 | if (level) { | |
218 | if (level < 0) { | |
219 | level = -level; | |
220 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
221 | level = -level; | |
222 | } else { | |
223 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
224 | } | |
225 | block[j] = level; | |
226 | sum+=level; | |
227 | } | |
228 | } | |
229 | block[63]^=sum&1; | |
230 | } | |
231 | ||
232 | static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, | |
233 | int16_t *block, int n, int qscale) | |
234 | { | |
235 | int i, level, nCoeffs; | |
236 | const uint16_t *quant_matrix; | |
237 | int sum=-1; | |
238 | ||
239 | if(s->alternate_scan) nCoeffs= 63; | |
240 | else nCoeffs= s->block_last_index[n]; | |
241 | ||
242 | quant_matrix = s->inter_matrix; | |
243 | for(i=0; i<=nCoeffs; i++) { | |
244 | int j= s->intra_scantable.permutated[i]; | |
245 | level = block[j]; | |
246 | if (level) { | |
247 | if (level < 0) { | |
248 | level = -level; | |
249 | level = (((level << 1) + 1) * qscale * | |
250 | ((int) (quant_matrix[j]))) >> 4; | |
251 | level = -level; | |
252 | } else { | |
253 | level = (((level << 1) + 1) * qscale * | |
254 | ((int) (quant_matrix[j]))) >> 4; | |
255 | } | |
256 | block[j] = level; | |
257 | sum+=level; | |
258 | } | |
259 | } | |
260 | block[63]^=sum&1; | |
261 | } | |
262 | ||
263 | static void dct_unquantize_h263_intra_c(MpegEncContext *s, | |
264 | int16_t *block, int n, int qscale) | |
265 | { | |
266 | int i, level, qmul, qadd; | |
267 | int nCoeffs; | |
268 | ||
269 | assert(s->block_last_index[n]>=0); | |
270 | ||
271 | qmul = qscale << 1; | |
272 | ||
273 | if (!s->h263_aic) { | |
274 | if (n < 4) | |
275 | block[0] = block[0] * s->y_dc_scale; | |
276 | else | |
277 | block[0] = block[0] * s->c_dc_scale; | |
278 | qadd = (qscale - 1) | 1; | |
279 | }else{ | |
280 | qadd = 0; | |
281 | } | |
282 | if(s->ac_pred) | |
283 | nCoeffs=63; | |
284 | else | |
285 | nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ]; | |
286 | ||
287 | for(i=1; i<=nCoeffs; i++) { | |
288 | level = block[i]; | |
289 | if (level) { | |
290 | if (level < 0) { | |
291 | level = level * qmul - qadd; | |
292 | } else { | |
293 | level = level * qmul + qadd; | |
294 | } | |
295 | block[i] = level; | |
296 | } | |
297 | } | |
298 | } | |
299 | ||
300 | static void dct_unquantize_h263_inter_c(MpegEncContext *s, | |
301 | int16_t *block, int n, int qscale) | |
302 | { | |
303 | int i, level, qmul, qadd; | |
304 | int nCoeffs; | |
305 | ||
306 | assert(s->block_last_index[n]>=0); | |
307 | ||
308 | qadd = (qscale - 1) | 1; | |
309 | qmul = qscale << 1; | |
310 | ||
311 | nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ]; | |
312 | ||
313 | for(i=0; i<=nCoeffs; i++) { | |
314 | level = block[i]; | |
315 | if (level) { | |
316 | if (level < 0) { | |
317 | level = level * qmul - qadd; | |
318 | } else { | |
319 | level = level * qmul + qadd; | |
320 | } | |
321 | block[i] = level; | |
322 | } | |
323 | } | |
324 | } | |
325 | ||
54974c62 AK |
326 | static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type, |
327 | int (*mv)[2][4][2], | |
328 | int mb_x, int mb_y, int mb_intra, int mb_skipped) | |
329 | { | |
330 | MpegEncContext *s = opaque; | |
331 | ||
332 | s->mv_dir = mv_dir; | |
333 | s->mv_type = mv_type; | |
334 | s->mb_intra = mb_intra; | |
335 | s->mb_skipped = mb_skipped; | |
336 | s->mb_x = mb_x; | |
337 | s->mb_y = mb_y; | |
338 | memcpy(s->mv, mv, sizeof(*mv)); | |
339 | ||
340 | ff_init_block_index(s); | |
341 | ff_update_block_index(s); | |
342 | ||
343 | s->dsp.clear_blocks(s->block[0]); | |
344 | ||
345 | s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16; | |
346 | s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift); | |
347 | s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift); | |
348 | ||
349 | assert(ref == 0); | |
350 | ff_MPV_decode_mb(s, s->block); | |
351 | } | |
352 | ||
defdfc9a | 353 | /* init common dct for both encoder and decoder */ |
5ef251e5 | 354 | av_cold int ff_dct_common_init(MpegEncContext *s) |
de6d9b64 | 355 | { |
9cf0841e | 356 | ff_dsputil_init(&s->dsp, s->avctx); |
f4fed5a2 | 357 | ff_hpeldsp_init(&s->hdsp, s->avctx->flags); |
1f4ea4e0 | 358 | ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample); |
c3027b4d | 359 | |
d50635cd MN |
360 | s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c; |
361 | s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c; | |
362 | s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c; | |
363 | s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c; | |
364 | s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c; | |
363114e8 | 365 | if (s->flags & CODEC_FLAG_BITEXACT) |
e27b6e62 | 366 | s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact; |
d50635cd | 367 | s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c; |
b0368839 | 368 | |
70dd8892 DB |
369 | if (ARCH_ARM) |
370 | ff_MPV_common_init_arm(s); | |
70dd8892 DB |
371 | if (ARCH_PPC) |
372 | ff_MPV_common_init_ppc(s); | |
373 | if (ARCH_X86) | |
374 | ff_MPV_common_init_x86(s); | |
676e200c | 375 | |
2ad1516a | 376 | /* load & permutate scantables |
363114e8 KT |
377 | * note: only wmv uses different ones |
378 | */ | |
379 | if (s->alternate_scan) { | |
bb198e19 MN |
380 | ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan); |
381 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan); | |
363114e8 | 382 | } else { |
bb198e19 MN |
383 | ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct); |
384 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct); | |
385 | } | |
3d2e8cce MN |
386 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan); |
387 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); | |
d930ef19 | 388 | |
defdfc9a AB |
389 | return 0; |
390 | } | |
391 | ||
c99307ca | 392 | static int frame_size_alloc(MpegEncContext *s, int linesize) |
f1d8763a JG |
393 | { |
394 | int alloc_size = FFALIGN(FFABS(linesize) + 32, 32); | |
395 | ||
396 | // edge emu needs blocksize + filter length - 1 | |
397 | // (= 17x17 for halfpel / 21x21 for h264) | |
45635885 JG |
398 | // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9 |
399 | // at uvlinesize. It supports only YUV420 so 24x24 is enough | |
f1d8763a | 400 | // linesize * interlaced * MBsize |
45635885 | 401 | FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24, |
f1d8763a JG |
402 | fail); |
403 | ||
259af1b9 | 404 | FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3, |
f1d8763a JG |
405 | fail) |
406 | s->me.temp = s->me.scratchpad; | |
407 | s->rd_scratchpad = s->me.scratchpad; | |
408 | s->b_scratchpad = s->me.scratchpad; | |
409 | s->obmc_scratchpad = s->me.scratchpad + 16; | |
410 | ||
411 | return 0; | |
412 | fail: | |
413 | av_freep(&s->edge_emu_buffer); | |
414 | return AVERROR(ENOMEM); | |
415 | } | |
416 | ||
34e46c44 | 417 | /** |
49bd8e4b | 418 | * Allocate a frame buffer |
34e46c44 GB |
419 | */ |
420 | static int alloc_frame_buffer(MpegEncContext *s, Picture *pic) | |
421 | { | |
024db249 | 422 | int edges_needed = av_codec_is_encoder(s->avctx->codec); |
f1d8763a | 423 | int r, ret; |
34e46c44 | 424 | |
759001c5 | 425 | pic->tf.f = &pic->f; |
ee769c6a AD |
426 | if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && |
427 | s->codec_id != AV_CODEC_ID_VC1IMAGE && | |
024db249 AK |
428 | s->codec_id != AV_CODEC_ID_MSS2) { |
429 | if (edges_needed) { | |
430 | pic->f.width = s->avctx->width + 2 * EDGE_WIDTH; | |
431 | pic->f.height = s->avctx->height + 2 * EDGE_WIDTH; | |
432 | } | |
433 | ||
759001c5 AK |
434 | r = ff_thread_get_buffer(s->avctx, &pic->tf, |
435 | pic->reference ? AV_GET_BUFFER_FLAG_REF : 0); | |
024db249 | 436 | } else { |
759001c5 AK |
437 | pic->f.width = s->avctx->width; |
438 | pic->f.height = s->avctx->height; | |
439 | pic->f.format = s->avctx->pix_fmt; | |
440 | r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0); | |
441 | } | |
442 | ||
a553c6a3 | 443 | if (r < 0 || !pic->f.buf[0]) { |
759001c5 AK |
444 | av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n", |
445 | r, pic->f.data[0]); | |
34e46c44 GB |
446 | return -1; |
447 | } | |
448 | ||
024db249 AK |
449 | if (edges_needed) { |
450 | int i; | |
451 | for (i = 0; pic->f.data[i]; i++) { | |
452 | int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) * | |
453 | pic->f.linesize[i] + | |
454 | (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0)); | |
455 | pic->f.data[i] += offset; | |
456 | } | |
457 | pic->f.width = s->avctx->width; | |
458 | pic->f.height = s->avctx->height; | |
459 | } | |
460 | ||
c3ebfcd6 HL |
461 | if (s->avctx->hwaccel) { |
462 | assert(!pic->hwaccel_picture_private); | |
463 | if (s->avctx->hwaccel->priv_data_size) { | |
464 | pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size); | |
465 | if (!pic->hwaccel_priv_buf) { | |
466 | av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n"); | |
467 | return -1; | |
468 | } | |
469 | pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data; | |
470 | } | |
471 | } | |
472 | ||
363114e8 KT |
473 | if (s->linesize && (s->linesize != pic->f.linesize[0] || |
474 | s->uvlinesize != pic->f.linesize[1])) { | |
475 | av_log(s->avctx, AV_LOG_ERROR, | |
476 | "get_buffer() failed (stride changed)\n"); | |
759001c5 | 477 | ff_mpeg_unref_picture(s, pic); |
34e46c44 GB |
478 | return -1; |
479 | } | |
480 | ||
657ccb5a | 481 | if (pic->f.linesize[1] != pic->f.linesize[2]) { |
363114e8 KT |
482 | av_log(s->avctx, AV_LOG_ERROR, |
483 | "get_buffer() failed (uv stride mismatch)\n"); | |
759001c5 | 484 | ff_mpeg_unref_picture(s, pic); |
34e46c44 GB |
485 | return -1; |
486 | } | |
487 | ||
f1d8763a | 488 | if (!s->edge_emu_buffer && |
c99307ca | 489 | (ret = frame_size_alloc(s, pic->f.linesize[0])) < 0) { |
f1d8763a JG |
490 | av_log(s->avctx, AV_LOG_ERROR, |
491 | "get_buffer() failed to allocate context scratch buffers.\n"); | |
759001c5 | 492 | ff_mpeg_unref_picture(s, pic); |
f1d8763a JG |
493 | return ret; |
494 | } | |
495 | ||
34e46c44 GB |
496 | return 0; |
497 | } | |
498 | ||
0b0a7a75 | 499 | void ff_free_picture_tables(Picture *pic) |
363114e8 | 500 | { |
759001c5 | 501 | int i; |
363114e8 | 502 | |
759001c5 AK |
503 | av_buffer_unref(&pic->mb_var_buf); |
504 | av_buffer_unref(&pic->mc_mb_var_buf); | |
505 | av_buffer_unref(&pic->mb_mean_buf); | |
506 | av_buffer_unref(&pic->mbskip_table_buf); | |
507 | av_buffer_unref(&pic->qscale_table_buf); | |
508 | av_buffer_unref(&pic->mb_type_buf); | |
363114e8 | 509 | |
759001c5 AK |
510 | for (i = 0; i < 2; i++) { |
511 | av_buffer_unref(&pic->motion_val_buf[i]); | |
512 | av_buffer_unref(&pic->ref_index_buf[i]); | |
513 | } | |
514 | } | |
515 | ||
516 | static int alloc_picture_tables(MpegEncContext *s, Picture *pic) | |
517 | { | |
518 | const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1; | |
363114e8 KT |
519 | const int mb_array_size = s->mb_stride * s->mb_height; |
520 | const int b8_array_size = s->b8_stride * s->mb_height * 2; | |
0da71265 | 521 | int i; |
759001c5 AK |
522 | |
523 | ||
524 | pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2); | |
525 | pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride); | |
526 | pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) * | |
527 | sizeof(uint32_t)); | |
528 | if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf) | |
529 | return AVERROR(ENOMEM); | |
530 | ||
531 | if (s->encoding) { | |
532 | pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t)); | |
533 | pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t)); | |
534 | pic->mb_mean_buf = av_buffer_allocz(mb_array_size); | |
535 | if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf) | |
536 | return AVERROR(ENOMEM); | |
537 | } | |
538 | ||
ccc71298 | 539 | if (s->out_format == FMT_H263 || s->encoding) { |
759001c5 AK |
540 | int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t); |
541 | int ref_index_size = 4 * mb_array_size; | |
542 | ||
543 | for (i = 0; mv_size && i < 2; i++) { | |
544 | pic->motion_val_buf[i] = av_buffer_allocz(mv_size); | |
545 | pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size); | |
546 | if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) | |
547 | return AVERROR(ENOMEM); | |
548 | } | |
549 | } | |
550 | ||
551 | return 0; | |
552 | } | |
553 | ||
554 | static int make_tables_writable(Picture *pic) | |
555 | { | |
556 | int ret, i; | |
557 | #define MAKE_WRITABLE(table) \ | |
558 | do {\ | |
559 | if (pic->table &&\ | |
560 | (ret = av_buffer_make_writable(&pic->table)) < 0)\ | |
561 | return ret;\ | |
562 | } while (0) | |
563 | ||
564 | MAKE_WRITABLE(mb_var_buf); | |
565 | MAKE_WRITABLE(mc_mb_var_buf); | |
566 | MAKE_WRITABLE(mb_mean_buf); | |
567 | MAKE_WRITABLE(mbskip_table_buf); | |
568 | MAKE_WRITABLE(qscale_table_buf); | |
569 | MAKE_WRITABLE(mb_type_buf); | |
570 | ||
571 | for (i = 0; i < 2; i++) { | |
572 | MAKE_WRITABLE(motion_val_buf[i]); | |
573 | MAKE_WRITABLE(ref_index_buf[i]); | |
574 | } | |
575 | ||
576 | return 0; | |
577 | } | |
578 | ||
579 | /** | |
580 | * Allocate a Picture. | |
581 | * The pixels are allocated/set by calling get_buffer() if shared = 0 | |
582 | */ | |
583 | int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared) | |
584 | { | |
585 | int i, ret; | |
115329f1 | 586 | |
363114e8 | 587 | if (shared) { |
657ccb5a | 588 | assert(pic->f.data[0]); |
759001c5 | 589 | pic->shared = 1; |
363114e8 | 590 | } else { |
a553c6a3 | 591 | assert(!pic->f.buf[0]); |
115329f1 | 592 | |
34e46c44 | 593 | if (alloc_frame_buffer(s, pic) < 0) |
4e00e76b | 594 | return -1; |
4e00e76b | 595 | |
657ccb5a DB |
596 | s->linesize = pic->f.linesize[0]; |
597 | s->uvlinesize = pic->f.linesize[1]; | |
1e491e29 | 598 | } |
115329f1 | 599 | |
759001c5 AK |
600 | if (!pic->qscale_table_buf) |
601 | ret = alloc_picture_tables(s, pic); | |
602 | else | |
603 | ret = make_tables_writable(pic); | |
604 | if (ret < 0) | |
605 | goto fail; | |
1e491e29 | 606 | |
759001c5 AK |
607 | if (s->encoding) { |
608 | pic->mb_var = (uint16_t*)pic->mb_var_buf->data; | |
609 | pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data; | |
610 | pic->mb_mean = pic->mb_mean_buf->data; | |
4e00e76b | 611 | } |
0da71265 | 612 | |
759001c5 AK |
613 | pic->mbskip_table = pic->mbskip_table_buf->data; |
614 | pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1; | |
615 | pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1; | |
616 | ||
617 | if (pic->motion_val_buf[0]) { | |
618 | for (i = 0; i < 2; i++) { | |
619 | pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4; | |
620 | pic->ref_index[i] = pic->ref_index_buf[i]->data; | |
621 | } | |
622 | } | |
115329f1 | 623 | |
1e491e29 | 624 | return 0; |
759001c5 AK |
625 | fail: |
626 | av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n"); | |
627 | ff_mpeg_unref_picture(s, pic); | |
0b0a7a75 | 628 | ff_free_picture_tables(pic); |
759001c5 | 629 | return AVERROR(ENOMEM); |
1e491e29 MN |
630 | } |
631 | ||
4e00e76b | 632 | /** |
58c42af7 | 633 | * Deallocate a picture. |
4e00e76b | 634 | */ |
759001c5 | 635 | void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic) |
363114e8 | 636 | { |
759001c5 AK |
637 | int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean); |
638 | ||
639 | pic->tf.f = &pic->f; | |
640 | /* WM Image / Screen codecs allocate internal buffers with different | |
641 | * dimensions / colorspaces; ignore user-defined callbacks for these. */ | |
642 | if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && | |
643 | s->codec_id != AV_CODEC_ID_VC1IMAGE && | |
644 | s->codec_id != AV_CODEC_ID_MSS2) | |
645 | ff_thread_release_buffer(s->avctx, &pic->tf); | |
646 | else | |
647 | av_frame_unref(&pic->f); | |
648 | ||
649 | av_buffer_unref(&pic->hwaccel_priv_buf); | |
4e00e76b | 650 | |
3eae9b03 | 651 | if (pic->needs_realloc) |
0b0a7a75 | 652 | ff_free_picture_tables(pic); |
3eae9b03 | 653 | |
759001c5 AK |
654 | memset((uint8_t*)pic + off, 0, sizeof(*pic) - off); |
655 | } | |
656 | ||
657 | static int update_picture_tables(Picture *dst, Picture *src) | |
658 | { | |
659 | int i; | |
660 | ||
661 | #define UPDATE_TABLE(table)\ | |
662 | do {\ | |
663 | if (src->table &&\ | |
664 | (!dst->table || dst->table->buffer != src->table->buffer)) {\ | |
665 | av_buffer_unref(&dst->table);\ | |
666 | dst->table = av_buffer_ref(src->table);\ | |
667 | if (!dst->table) {\ | |
0b0a7a75 | 668 | ff_free_picture_tables(dst);\ |
759001c5 AK |
669 | return AVERROR(ENOMEM);\ |
670 | }\ | |
671 | }\ | |
672 | } while (0) | |
673 | ||
674 | UPDATE_TABLE(mb_var_buf); | |
675 | UPDATE_TABLE(mc_mb_var_buf); | |
676 | UPDATE_TABLE(mb_mean_buf); | |
677 | UPDATE_TABLE(mbskip_table_buf); | |
678 | UPDATE_TABLE(qscale_table_buf); | |
679 | UPDATE_TABLE(mb_type_buf); | |
363114e8 | 680 | for (i = 0; i < 2; i++) { |
759001c5 AK |
681 | UPDATE_TABLE(motion_val_buf[i]); |
682 | UPDATE_TABLE(ref_index_buf[i]); | |
0da71265 | 683 | } |
115329f1 | 684 | |
759001c5 AK |
685 | dst->mb_var = src->mb_var; |
686 | dst->mc_mb_var = src->mc_mb_var; | |
687 | dst->mb_mean = src->mb_mean; | |
688 | dst->mbskip_table = src->mbskip_table; | |
689 | dst->qscale_table = src->qscale_table; | |
690 | dst->mb_type = src->mb_type; | |
691 | for (i = 0; i < 2; i++) { | |
692 | dst->motion_val[i] = src->motion_val[i]; | |
693 | dst->ref_index[i] = src->ref_index[i]; | |
1e491e29 | 694 | } |
759001c5 AK |
695 | |
696 | return 0; | |
697 | } | |
698 | ||
699 | int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src) | |
700 | { | |
701 | int ret; | |
702 | ||
703 | av_assert0(!dst->f.buf[0]); | |
704 | av_assert0(src->f.buf[0]); | |
705 | ||
706 | src->tf.f = &src->f; | |
707 | dst->tf.f = &dst->f; | |
708 | ret = ff_thread_ref_frame(&dst->tf, &src->tf); | |
709 | if (ret < 0) | |
710 | goto fail; | |
711 | ||
712 | ret = update_picture_tables(dst, src); | |
713 | if (ret < 0) | |
714 | goto fail; | |
715 | ||
716 | if (src->hwaccel_picture_private) { | |
717 | dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf); | |
718 | if (!dst->hwaccel_priv_buf) | |
719 | goto fail; | |
720 | dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data; | |
721 | } | |
722 | ||
723 | dst->field_picture = src->field_picture; | |
724 | dst->mb_var_sum = src->mb_var_sum; | |
725 | dst->mc_mb_var_sum = src->mc_mb_var_sum; | |
726 | dst->b_frame_score = src->b_frame_score; | |
727 | dst->needs_realloc = src->needs_realloc; | |
728 | dst->reference = src->reference; | |
729 | dst->shared = src->shared; | |
730 | ||
731 | return 0; | |
732 | fail: | |
733 | ff_mpeg_unref_picture(s, dst); | |
734 | return ret; | |
1e491e29 MN |
735 | } |
736 | ||
16e7b189 KS |
737 | static void exchange_uv(MpegEncContext *s) |
738 | { | |
739 | int16_t (*tmp)[64]; | |
740 | ||
741 | tmp = s->pblocks[4]; | |
742 | s->pblocks[4] = s->pblocks[5]; | |
743 | s->pblocks[5] = tmp; | |
744 | } | |
745 | ||
12b54a1f | 746 | static int init_duplicate_context(MpegEncContext *s) |
363114e8 | 747 | { |
2cbd734a MR |
748 | int y_size = s->b8_stride * (2 * s->mb_height + 1); |
749 | int c_size = s->mb_stride * (s->mb_height + 1); | |
750 | int yc_size = y_size + 2 * c_size; | |
9c3d33d6 MN |
751 | int i; |
752 | ||
f1d8763a JG |
753 | s->edge_emu_buffer = |
754 | s->me.scratchpad = | |
755 | s->me.temp = | |
756 | s->rd_scratchpad = | |
757 | s->b_scratchpad = | |
758 | s->obmc_scratchpad = NULL; | |
363114e8 | 759 | |
9c3d33d6 | 760 | if (s->encoding) { |
363114e8 KT |
761 | FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map, |
762 | ME_MAP_SIZE * sizeof(uint32_t), fail) | |
763 | FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map, | |
764 | ME_MAP_SIZE * sizeof(uint32_t), fail) | |
765 | if (s->avctx->noise_reduction) { | |
766 | FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum, | |
767 | 2 * 64 * sizeof(int), fail) | |
9c3d33d6 | 768 | } |
115329f1 | 769 | } |
88bd7fdc | 770 | FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail) |
363114e8 | 771 | s->block = s->blocks[0]; |
9c3d33d6 | 772 | |
363114e8 | 773 | for (i = 0; i < 12; i++) { |
21effaa4 | 774 | s->pblocks[i] = &s->block[i]; |
9c3d33d6 | 775 | } |
16e7b189 KS |
776 | if (s->avctx->codec_tag == AV_RL32("VCR2")) |
777 | exchange_uv(s); | |
2cbd734a | 778 | |
79042a6e MR |
779 | if (s->out_format == FMT_H263) { |
780 | /* ac values */ | |
363114e8 KT |
781 | FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, |
782 | yc_size * sizeof(int16_t) * 16, fail); | |
2cbd734a MR |
783 | s->ac_val[0] = s->ac_val_base + s->b8_stride + 1; |
784 | s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1; | |
785 | s->ac_val[2] = s->ac_val[1] + c_size; | |
786 | } | |
787 | ||
9c3d33d6 MN |
788 | return 0; |
789 | fail: | |
efd29844 | 790 | return -1; // free() through ff_MPV_common_end() |
9c3d33d6 MN |
791 | } |
792 | ||
363114e8 KT |
793 | static void free_duplicate_context(MpegEncContext *s) |
794 | { | |
795 | if (s == NULL) | |
796 | return; | |
9c3d33d6 | 797 | |
330deb75 | 798 | av_freep(&s->edge_emu_buffer); |
9c3d33d6 | 799 | av_freep(&s->me.scratchpad); |
363114e8 KT |
800 | s->me.temp = |
801 | s->rd_scratchpad = | |
802 | s->b_scratchpad = | |
803 | s->obmc_scratchpad = NULL; | |
115329f1 | 804 | |
9c3d33d6 MN |
805 | av_freep(&s->dct_error_sum); |
806 | av_freep(&s->me.map); | |
807 | av_freep(&s->me.score_map); | |
808 | av_freep(&s->blocks); | |
2cbd734a | 809 | av_freep(&s->ac_val_base); |
363114e8 | 810 | s->block = NULL; |
9c3d33d6 MN |
811 | } |
812 | ||
363114e8 KT |
813 | static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src) |
814 | { | |
815 | #define COPY(a) bak->a = src->a | |
9c3d33d6 MN |
816 | COPY(edge_emu_buffer); |
817 | COPY(me.scratchpad); | |
a6f2c0d6 | 818 | COPY(me.temp); |
9c3d33d6 MN |
819 | COPY(rd_scratchpad); |
820 | COPY(b_scratchpad); | |
821 | COPY(obmc_scratchpad); | |
822 | COPY(me.map); | |
823 | COPY(me.score_map); | |
824 | COPY(blocks); | |
825 | COPY(block); | |
826 | COPY(start_mb_y); | |
827 | COPY(end_mb_y); | |
828 | COPY(me.map_generation); | |
829 | COPY(pb); | |
830 | COPY(dct_error_sum); | |
da16b204 MN |
831 | COPY(dct_count[0]); |
832 | COPY(dct_count[1]); | |
2cbd734a MR |
833 | COPY(ac_val_base); |
834 | COPY(ac_val[0]); | |
835 | COPY(ac_val[1]); | |
836 | COPY(ac_val[2]); | |
9c3d33d6 MN |
837 | #undef COPY |
838 | } | |
839 | ||
f1d8763a | 840 | int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src) |
363114e8 | 841 | { |
9c3d33d6 | 842 | MpegEncContext bak; |
f1d8763a | 843 | int i, ret; |
363114e8 KT |
844 | // FIXME copy only needed parts |
845 | // START_TIMER | |
9c3d33d6 MN |
846 | backup_duplicate_context(&bak, dst); |
847 | memcpy(dst, src, sizeof(MpegEncContext)); | |
848 | backup_duplicate_context(dst, &bak); | |
363114e8 | 849 | for (i = 0; i < 12; i++) { |
21effaa4 | 850 | dst->pblocks[i] = &dst->block[i]; |
c62c07d3 | 851 | } |
16e7b189 KS |
852 | if (dst->avctx->codec_tag == AV_RL32("VCR2")) |
853 | exchange_uv(dst); | |
f1d8763a | 854 | if (!dst->edge_emu_buffer && |
c99307ca | 855 | (ret = frame_size_alloc(dst, dst->linesize)) < 0) { |
f1d8763a JG |
856 | av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context " |
857 | "scratch buffers.\n"); | |
858 | return ret; | |
859 | } | |
363114e8 KT |
860 | // STOP_TIMER("update_duplicate_context") |
861 | // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads | |
f1d8763a | 862 | return 0; |
9c3d33d6 MN |
863 | } |
864 | ||
363114e8 KT |
865 | int ff_mpeg_update_thread_context(AVCodecContext *dst, |
866 | const AVCodecContext *src) | |
6a9c8594 | 867 | { |
759001c5 | 868 | int i, ret; |
6a9c8594 AS |
869 | MpegEncContext *s = dst->priv_data, *s1 = src->priv_data; |
870 | ||
363114e8 KT |
871 | if (dst == src || !s1->context_initialized) |
872 | return 0; | |
6a9c8594 | 873 | |
363114e8 KT |
874 | // FIXME can parameters change on I-frames? |
875 | // in that case dst may need a reinit | |
876 | if (!s->context_initialized) { | |
6a9c8594 AS |
877 | memcpy(s, s1, sizeof(MpegEncContext)); |
878 | ||
879 | s->avctx = dst; | |
6a9c8594 AS |
880 | s->bitstream_buffer = NULL; |
881 | s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0; | |
882 | ||
efd29844 | 883 | ff_MPV_common_init(s); |
6a9c8594 AS |
884 | } |
885 | ||
8701f4f8 JG |
886 | if (s->height != s1->height || s->width != s1->width || s->context_reinit) { |
887 | int err; | |
888 | s->context_reinit = 0; | |
889 | s->height = s1->height; | |
890 | s->width = s1->width; | |
891 | if ((err = ff_MPV_common_frame_size_change(s)) < 0) | |
892 | return err; | |
893 | } | |
894 | ||
6a9c8594 AS |
895 | s->avctx->coded_height = s1->avctx->coded_height; |
896 | s->avctx->coded_width = s1->avctx->coded_width; | |
897 | s->avctx->width = s1->avctx->width; | |
898 | s->avctx->height = s1->avctx->height; | |
899 | ||
900 | s->coded_picture_number = s1->coded_picture_number; | |
901 | s->picture_number = s1->picture_number; | |
6a9c8594 | 902 | |
759001c5 AK |
903 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
904 | ff_mpeg_unref_picture(s, &s->picture[i]); | |
a553c6a3 | 905 | if (s1->picture[i].f.buf[0] && |
759001c5 AK |
906 | (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0) |
907 | return ret; | |
908 | } | |
909 | ||
910 | #define UPDATE_PICTURE(pic)\ | |
911 | do {\ | |
912 | ff_mpeg_unref_picture(s, &s->pic);\ | |
a553c6a3 | 913 | if (s1->pic.f.buf[0])\ |
759001c5 AK |
914 | ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\ |
915 | else\ | |
916 | ret = update_picture_tables(&s->pic, &s1->pic);\ | |
917 | if (ret < 0)\ | |
918 | return ret;\ | |
919 | } while (0) | |
920 | ||
921 | UPDATE_PICTURE(current_picture); | |
922 | UPDATE_PICTURE(last_picture); | |
923 | UPDATE_PICTURE(next_picture); | |
1481e198 | 924 | |
4d9ec050 KT |
925 | s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1); |
926 | s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1); | |
927 | s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1); | |
6a9c8594 | 928 | |
4d9ec050 | 929 | // Error/bug resilience |
6a9c8594 AS |
930 | s->next_p_frame_damaged = s1->next_p_frame_damaged; |
931 | s->workaround_bugs = s1->workaround_bugs; | |
932 | ||
4d9ec050 | 933 | // MPEG4 timing info |
e62a43f6 | 934 | memcpy(&s->last_time_base, &s1->last_time_base, |
ee8af2dd | 935 | (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) - |
e62a43f6 | 936 | (char *) &s1->last_time_base); |
4d9ec050 KT |
937 | |
938 | // B-frame info | |
939 | s->max_b_frames = s1->max_b_frames; | |
940 | s->low_delay = s1->low_delay; | |
ba0c8981 | 941 | s->droppable = s1->droppable; |
4d9ec050 KT |
942 | |
943 | // DivX handling (doesn't work) | |
944 | s->divx_packed = s1->divx_packed; | |
945 | ||
946 | if (s1->bitstream_buffer) { | |
947 | if (s1->bitstream_buffer_size + | |
948 | FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) | |
949 | av_fast_malloc(&s->bitstream_buffer, | |
950 | &s->allocated_bitstream_buffer_size, | |
951 | s1->allocated_bitstream_buffer_size); | |
952 | s->bitstream_buffer_size = s1->bitstream_buffer_size; | |
953 | memcpy(s->bitstream_buffer, s1->bitstream_buffer, | |
954 | s1->bitstream_buffer_size); | |
955 | memset(s->bitstream_buffer + s->bitstream_buffer_size, 0, | |
956 | FF_INPUT_BUFFER_PADDING_SIZE); | |
6a9c8594 AS |
957 | } |
958 | ||
f1d8763a JG |
959 | // linesize dependend scratch buffer allocation |
960 | if (!s->edge_emu_buffer) | |
961 | if (s1->linesize) { | |
c99307ca | 962 | if (frame_size_alloc(s, s1->linesize) < 0) { |
f1d8763a JG |
963 | av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context " |
964 | "scratch buffers.\n"); | |
965 | return AVERROR(ENOMEM); | |
966 | } | |
967 | } else { | |
968 | av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not " | |
969 | "be allocated due to unknown size.\n"); | |
970 | return AVERROR_BUG; | |
971 | } | |
972 | ||
4d9ec050 KT |
973 | // MPEG2/interlacing info |
974 | memcpy(&s->progressive_sequence, &s1->progressive_sequence, | |
975 | (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence); | |
6a9c8594 | 976 | |
4d9ec050 KT |
977 | if (!s1->first_field) { |
978 | s->last_pict_type = s1->pict_type; | |
979 | if (s1->current_picture_ptr) | |
980 | s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality; | |
6a9c8594 AS |
981 | } |
982 | ||
983 | return 0; | |
984 | } | |
985 | ||
3edcacde | 986 | /** |
58c42af7 | 987 | * Set the given MpegEncContext to common defaults |
4d9ec050 | 988 | * (same for encoding and decoding). |
58c42af7 | 989 | * The changed fields will not depend upon the |
4d9ec050 | 990 | * prior state of the MpegEncContext. |
3edcacde | 991 | */ |
efd29844 | 992 | void ff_MPV_common_defaults(MpegEncContext *s) |
4d9ec050 KT |
993 | { |
994 | s->y_dc_scale_table = | |
995 | s->c_dc_scale_table = ff_mpeg1_dc_scale_table; | |
996 | s->chroma_qscale_table = ff_default_chroma_qscale_table; | |
997 | s->progressive_frame = 1; | |
998 | s->progressive_sequence = 1; | |
999 | s->picture_structure = PICT_FRAME; | |
1000 | ||
1001 | s->coded_picture_number = 0; | |
1002 | s->picture_number = 0; | |
7976241a | 1003 | |
4d9ec050 KT |
1004 | s->f_code = 1; |
1005 | s->b_code = 1; | |
6a9c8594 | 1006 | |
881a5e04 | 1007 | s->slice_context_count = 1; |
3edcacde MN |
1008 | } |
1009 | ||
1010 | /** | |
58c42af7 | 1011 | * Set the given MpegEncContext to defaults for decoding. |
4d9ec050 KT |
1012 | * the changed fields will not depend upon |
1013 | * the prior state of the MpegEncContext. | |
3edcacde | 1014 | */ |
efd29844 | 1015 | void ff_MPV_decode_defaults(MpegEncContext *s) |
4d9ec050 | 1016 | { |
efd29844 | 1017 | ff_MPV_common_defaults(s); |
3edcacde MN |
1018 | } |
1019 | ||
54974c62 AK |
1020 | static int init_er(MpegEncContext *s) |
1021 | { | |
1022 | ERContext *er = &s->er; | |
1023 | int mb_array_size = s->mb_height * s->mb_stride; | |
1024 | int i; | |
1025 | ||
1026 | er->avctx = s->avctx; | |
1027 | er->dsp = &s->dsp; | |
1028 | ||
1029 | er->mb_index2xy = s->mb_index2xy; | |
1030 | er->mb_num = s->mb_num; | |
1031 | er->mb_width = s->mb_width; | |
1032 | er->mb_height = s->mb_height; | |
1033 | er->mb_stride = s->mb_stride; | |
1034 | er->b8_stride = s->b8_stride; | |
1035 | ||
1036 | er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride); | |
1037 | er->error_status_table = av_mallocz(mb_array_size); | |
1038 | if (!er->er_temp_buffer || !er->error_status_table) | |
1039 | goto fail; | |
1040 | ||
1041 | er->mbskip_table = s->mbskip_table; | |
1042 | er->mbintra_table = s->mbintra_table; | |
1043 | ||
1044 | for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++) | |
1045 | er->dc_val[i] = s->dc_val[i]; | |
1046 | ||
1047 | er->decode_mb = mpeg_er_decode_mb; | |
1048 | er->opaque = s; | |
1049 | ||
1050 | return 0; | |
1051 | fail: | |
1052 | av_freep(&er->er_temp_buffer); | |
1053 | av_freep(&er->error_status_table); | |
1054 | return AVERROR(ENOMEM); | |
1055 | } | |
1056 | ||
3edcacde | 1057 | /** |
1b3439b3 JG |
1058 | * Initialize and allocates MpegEncContext fields dependent on the resolution. |
1059 | */ | |
1060 | static int init_context_frame(MpegEncContext *s) | |
1061 | { | |
1062 | int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y; | |
1063 | ||
1064 | s->mb_width = (s->width + 15) / 16; | |
1065 | s->mb_stride = s->mb_width + 1; | |
1066 | s->b8_stride = s->mb_width * 2 + 1; | |
1067 | s->b4_stride = s->mb_width * 4 + 1; | |
1068 | mb_array_size = s->mb_height * s->mb_stride; | |
1069 | mv_table_size = (s->mb_height + 2) * s->mb_stride + 1; | |
1070 | ||
1071 | /* set default edge pos, will be overriden | |
1072 | * in decode_header if needed */ | |
1073 | s->h_edge_pos = s->mb_width * 16; | |
1074 | s->v_edge_pos = s->mb_height * 16; | |
1075 | ||
1076 | s->mb_num = s->mb_width * s->mb_height; | |
1077 | ||
1078 | s->block_wrap[0] = | |
1079 | s->block_wrap[1] = | |
1080 | s->block_wrap[2] = | |
1081 | s->block_wrap[3] = s->b8_stride; | |
1082 | s->block_wrap[4] = | |
1083 | s->block_wrap[5] = s->mb_stride; | |
1084 | ||
1085 | y_size = s->b8_stride * (2 * s->mb_height + 1); | |
1086 | c_size = s->mb_stride * (s->mb_height + 1); | |
1087 | yc_size = y_size + 2 * c_size; | |
1088 | ||
1089 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), | |
1090 | fail); // error ressilience code looks cleaner with this | |
1091 | for (y = 0; y < s->mb_height; y++) | |
1092 | for (x = 0; x < s->mb_width; x++) | |
1093 | s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride; | |
1094 | ||
1095 | s->mb_index2xy[s->mb_height * s->mb_width] = | |
1096 | (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed? | |
1097 | ||
1098 | if (s->encoding) { | |
1099 | /* Allocate MV tables */ | |
1100 | FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, | |
1101 | mv_table_size * 2 * sizeof(int16_t), fail); | |
1102 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, | |
1103 | mv_table_size * 2 * sizeof(int16_t), fail); | |
1104 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, | |
1105 | mv_table_size * 2 * sizeof(int16_t), fail); | |
1106 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, | |
1107 | mv_table_size * 2 * sizeof(int16_t), fail); | |
1108 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, | |
1109 | mv_table_size * 2 * sizeof(int16_t), fail); | |
1110 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, | |
1111 | mv_table_size * 2 * sizeof(int16_t), fail); | |
1112 | s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1; | |
1113 | s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1; | |
1114 | s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1; | |
1115 | s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + | |
1116 | s->mb_stride + 1; | |
1117 | s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + | |
1118 | s->mb_stride + 1; | |
1119 | s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1; | |
1120 | ||
1121 | /* Allocate MB type table */ | |
1122 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * | |
1123 | sizeof(uint16_t), fail); // needed for encoding | |
1124 | ||
1125 | FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * | |
1126 | sizeof(int), fail); | |
1127 | ||
1128 | FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab, | |
1129 | mb_array_size * sizeof(float), fail); | |
1130 | FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab, | |
1131 | mb_array_size * sizeof(float), fail); | |
1132 | ||
1133 | } | |
1134 | ||
1b3439b3 JG |
1135 | if (s->codec_id == AV_CODEC_ID_MPEG4 || |
1136 | (s->flags & CODEC_FLAG_INTERLACED_ME)) { | |
1137 | /* interlaced direct mode decoding tables */ | |
1138 | for (i = 0; i < 2; i++) { | |
1139 | int j, k; | |
1140 | for (j = 0; j < 2; j++) { | |
1141 | for (k = 0; k < 2; k++) { | |
1142 | FF_ALLOCZ_OR_GOTO(s->avctx, | |
1143 | s->b_field_mv_table_base[i][j][k], | |
1144 | mv_table_size * 2 * sizeof(int16_t), | |
1145 | fail); | |
1146 | s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + | |
1147 | s->mb_stride + 1; | |
1148 | } | |
1149 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], | |
1150 | mb_array_size * 2 * sizeof(uint8_t), fail); | |
1151 | FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], | |
1152 | mv_table_size * 2 * sizeof(int16_t), fail); | |
1153 | s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] | |
1154 | + s->mb_stride + 1; | |
1155 | } | |
1156 | FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], | |
1157 | mb_array_size * 2 * sizeof(uint8_t), fail); | |
1158 | } | |
1159 | } | |
1160 | if (s->out_format == FMT_H263) { | |
1161 | /* cbp values */ | |
1162 | FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail); | |
1163 | s->coded_block = s->coded_block_base + s->b8_stride + 1; | |
1164 | ||
1165 | /* cbp, ac_pred, pred_dir */ | |
1166 | FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table, | |
1167 | mb_array_size * sizeof(uint8_t), fail); | |
1168 | FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, | |
1169 | mb_array_size * sizeof(uint8_t), fail); | |
1170 | } | |
1171 | ||
1172 | if (s->h263_pred || s->h263_plus || !s->encoding) { | |
1173 | /* dc values */ | |
1174 | // MN: we need these for error resilience of intra-frames | |
1175 | FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, | |
1176 | yc_size * sizeof(int16_t), fail); | |
1177 | s->dc_val[0] = s->dc_val_base + s->b8_stride + 1; | |
1178 | s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1; | |
1179 | s->dc_val[2] = s->dc_val[1] + c_size; | |
1180 | for (i = 0; i < yc_size; i++) | |
1181 | s->dc_val_base[i] = 1024; | |
1182 | } | |
1183 | ||
1184 | /* which mb is a intra block */ | |
1185 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail); | |
1186 | memset(s->mbintra_table, 1, mb_array_size); | |
1187 | ||
1188 | /* init macroblock skip table */ | |
1189 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail); | |
1190 | // Note the + 1 is for a quicker mpeg4 slice_end detection | |
1191 | ||
54974c62 | 1192 | return init_er(s); |
1b3439b3 JG |
1193 | fail: |
1194 | return AVERROR(ENOMEM); | |
1195 | } | |
1196 | ||
1197 | /** | |
3edcacde MN |
1198 | * init common structure for both encoder and decoder. |
1199 | * this assumes that some variables like width/height are already set | |
1200 | */ | |
efd29844 | 1201 | av_cold int ff_MPV_common_init(MpegEncContext *s) |
defdfc9a | 1202 | { |
7e76fc52 | 1203 | int i; |
881a5e04 JG |
1204 | int nb_slices = (HAVE_THREADS && |
1205 | s->avctx->active_thread_type & FF_THREAD_SLICE) ? | |
1206 | s->avctx->thread_count : 1; | |
1207 | ||
1208 | if (s->encoding && s->avctx->slices) | |
1209 | nb_slices = s->avctx->slices; | |
defdfc9a | 1210 | |
36ef5369 | 1211 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence) |
0127b861 | 1212 | s->mb_height = (s->height + 31) / 32 * 2; |
19cac8e3 | 1213 | else |
1b661802 | 1214 | s->mb_height = (s->height + 15) / 16; |
fdb52bcc | 1215 | |
716d413c | 1216 | if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) { |
4d9ec050 | 1217 | av_log(s->avctx, AV_LOG_ERROR, |
716d413c | 1218 | "decoding to AV_PIX_FMT_NONE is not supported.\n"); |
9cfc1b3a IK |
1219 | return -1; |
1220 | } | |
1221 | ||
881a5e04 JG |
1222 | if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) { |
1223 | int max_slices; | |
1224 | if (s->mb_height) | |
1225 | max_slices = FFMIN(MAX_THREADS, s->mb_height); | |
1226 | else | |
1227 | max_slices = MAX_THREADS; | |
1228 | av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d)," | |
1229 | " reducing to %d\n", nb_slices, max_slices); | |
1230 | nb_slices = max_slices; | |
000a9c02 MN |
1231 | } |
1232 | ||
4d9ec050 KT |
1233 | if ((s->width || s->height) && |
1234 | av_image_check_size(s->width, s->height, 0, s->avctx)) | |
0ecca7a4 MN |
1235 | return -1; |
1236 | ||
6180ade7 | 1237 | ff_dct_common_init(s); |
eb4b3dd3 | 1238 | |
4d9ec050 KT |
1239 | s->flags = s->avctx->flags; |
1240 | s->flags2 = s->avctx->flags2; | |
defdfc9a | 1241 | |
5f24fe82 MS |
1242 | /* set chroma shifts */ |
1243 | av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, | |
1244 | &s->chroma_x_shift, | |
1245 | &s->chroma_y_shift); | |
eb4b3dd3 | 1246 | |
5f24fe82 MS |
1247 | /* convert fourcc to upper case */ |
1248 | s->codec_tag = avpriv_toupper4(s->avctx->codec_tag); | |
115329f1 | 1249 | |
5f24fe82 | 1250 | s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag); |
603a5f04 | 1251 | |
4d9ec050 | 1252 | FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, |
759001c5 AK |
1253 | MAX_PICTURE_COUNT * sizeof(Picture), fail); |
1254 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { | |
2d1f4288 | 1255 | av_frame_unref(&s->picture[i].f); |
747069e2 | 1256 | } |
759001c5 AK |
1257 | memset(&s->next_picture, 0, sizeof(s->next_picture)); |
1258 | memset(&s->last_picture, 0, sizeof(s->last_picture)); | |
1259 | memset(&s->current_picture, 0, sizeof(s->current_picture)); | |
2d1f4288 AK |
1260 | av_frame_unref(&s->next_picture.f); |
1261 | av_frame_unref(&s->last_picture.f); | |
1262 | av_frame_unref(&s->current_picture.f); | |
b465449e | 1263 | |
fb22c237 | 1264 | if (s->width && s->height) { |
7e76fc52 | 1265 | if (init_context_frame(s)) |
1b3439b3 | 1266 | goto fail; |
4d9ec050 KT |
1267 | |
1268 | s->parse_context.state = -1; | |
fb22c237 | 1269 | } |
d7425f59 | 1270 | |
de6d9b64 | 1271 | s->context_initialized = 1; |
4d9ec050 | 1272 | s->thread_context[0] = s; |
9c3d33d6 | 1273 | |
fb22c237 | 1274 | if (s->width && s->height) { |
881a5e04 JG |
1275 | if (nb_slices > 1) { |
1276 | for (i = 1; i < nb_slices; i++) { | |
4d9ec050 KT |
1277 | s->thread_context[i] = av_malloc(sizeof(MpegEncContext)); |
1278 | memcpy(s->thread_context[i], s, sizeof(MpegEncContext)); | |
1279 | } | |
9c3d33d6 | 1280 | |
881a5e04 | 1281 | for (i = 0; i < nb_slices; i++) { |
12b54a1f | 1282 | if (init_duplicate_context(s->thread_context[i]) < 0) |
4d9ec050 KT |
1283 | goto fail; |
1284 | s->thread_context[i]->start_mb_y = | |
881a5e04 | 1285 | (s->mb_height * (i) + nb_slices / 2) / nb_slices; |
4d9ec050 | 1286 | s->thread_context[i]->end_mb_y = |
881a5e04 | 1287 | (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices; |
4d9ec050 KT |
1288 | } |
1289 | } else { | |
12b54a1f | 1290 | if (init_duplicate_context(s) < 0) |
d969e93a | 1291 | goto fail; |
4d9ec050 KT |
1292 | s->start_mb_y = 0; |
1293 | s->end_mb_y = s->mb_height; | |
d969e93a | 1294 | } |
881a5e04 | 1295 | s->slice_context_count = nb_slices; |
fb22c237 RB |
1296 | } |
1297 | ||
de6d9b64 FB |
1298 | return 0; |
1299 | fail: | |
efd29844 | 1300 | ff_MPV_common_end(s); |
de6d9b64 FB |
1301 | return -1; |
1302 | } | |
1303 | ||
1b3439b3 JG |
1304 | /** |
1305 | * Frees and resets MpegEncContext fields depending on the resolution. | |
1306 | * Is used during resolution changes to avoid a full reinitialization of the | |
1307 | * codec. | |
1308 | */ | |
1309 | static int free_context_frame(MpegEncContext *s) | |
de6d9b64 | 1310 | { |
bb198e19 | 1311 | int i, j, k; |
de6d9b64 | 1312 | |
6000abfa | 1313 | av_freep(&s->mb_type); |
7bc9090a MN |
1314 | av_freep(&s->p_mv_table_base); |
1315 | av_freep(&s->b_forw_mv_table_base); | |
1316 | av_freep(&s->b_back_mv_table_base); | |
1317 | av_freep(&s->b_bidir_forw_mv_table_base); | |
1318 | av_freep(&s->b_bidir_back_mv_table_base); | |
1319 | av_freep(&s->b_direct_mv_table_base); | |
4d9ec050 KT |
1320 | s->p_mv_table = NULL; |
1321 | s->b_forw_mv_table = NULL; | |
1322 | s->b_back_mv_table = NULL; | |
1323 | s->b_bidir_forw_mv_table = NULL; | |
1324 | s->b_bidir_back_mv_table = NULL; | |
1325 | s->b_direct_mv_table = NULL; | |
1326 | for (i = 0; i < 2; i++) { | |
1327 | for (j = 0; j < 2; j++) { | |
1328 | for (k = 0; k < 2; k++) { | |
bb198e19 | 1329 | av_freep(&s->b_field_mv_table_base[i][j][k]); |
4d9ec050 | 1330 | s->b_field_mv_table[i][j][k] = NULL; |
bb198e19 MN |
1331 | } |
1332 | av_freep(&s->b_field_select_table[i][j]); | |
1333 | av_freep(&s->p_field_mv_table_base[i][j]); | |
4d9ec050 | 1334 | s->p_field_mv_table[i][j] = NULL; |
bb198e19 MN |
1335 | } |
1336 | av_freep(&s->p_field_select_table[i]); | |
1337 | } | |
115329f1 | 1338 | |
137c8468 | 1339 | av_freep(&s->dc_val_base); |
137c8468 | 1340 | av_freep(&s->coded_block_base); |
6000abfa | 1341 | av_freep(&s->mbintra_table); |
7f2fe444 MN |
1342 | av_freep(&s->cbp_table); |
1343 | av_freep(&s->pred_dir_table); | |
115329f1 | 1344 | |
6000abfa | 1345 | av_freep(&s->mbskip_table); |
0ecca7a4 | 1346 | |
54974c62 AK |
1347 | av_freep(&s->er.error_status_table); |
1348 | av_freep(&s->er.er_temp_buffer); | |
7bc9090a | 1349 | av_freep(&s->mb_index2xy); |
158c7f05 | 1350 | av_freep(&s->lambda_table); |
1b3439b3 JG |
1351 | av_freep(&s->cplx_tab); |
1352 | av_freep(&s->bits_tab); | |
1353 | ||
1354 | s->linesize = s->uvlinesize = 0; | |
1355 | ||
1b3439b3 JG |
1356 | return 0; |
1357 | } | |
1358 | ||
435c0b87 JG |
1359 | int ff_MPV_common_frame_size_change(MpegEncContext *s) |
1360 | { | |
1361 | int i, err = 0; | |
1362 | ||
1363 | if (s->slice_context_count > 1) { | |
1364 | for (i = 0; i < s->slice_context_count; i++) { | |
1365 | free_duplicate_context(s->thread_context[i]); | |
1366 | } | |
1367 | for (i = 1; i < s->slice_context_count; i++) { | |
1368 | av_freep(&s->thread_context[i]); | |
1369 | } | |
1370 | } else | |
1371 | free_duplicate_context(s); | |
1372 | ||
759001c5 AK |
1373 | if ((err = free_context_frame(s)) < 0) |
1374 | return err; | |
435c0b87 JG |
1375 | |
1376 | if (s->picture) | |
759001c5 | 1377 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
435c0b87 JG |
1378 | s->picture[i].needs_realloc = 1; |
1379 | } | |
1380 | ||
1381 | s->last_picture_ptr = | |
1382 | s->next_picture_ptr = | |
1383 | s->current_picture_ptr = NULL; | |
1384 | ||
1385 | // init | |
1386 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence) | |
1387 | s->mb_height = (s->height + 31) / 32 * 2; | |
19cac8e3 | 1388 | else |
435c0b87 JG |
1389 | s->mb_height = (s->height + 15) / 16; |
1390 | ||
1391 | if ((s->width || s->height) && | |
1392 | av_image_check_size(s->width, s->height, 0, s->avctx)) | |
1393 | return AVERROR_INVALIDDATA; | |
1394 | ||
1395 | if ((err = init_context_frame(s))) | |
1396 | goto fail; | |
1397 | ||
1398 | s->thread_context[0] = s; | |
1399 | ||
1400 | if (s->width && s->height) { | |
1401 | int nb_slices = s->slice_context_count; | |
1402 | if (nb_slices > 1) { | |
1403 | for (i = 1; i < nb_slices; i++) { | |
1404 | s->thread_context[i] = av_malloc(sizeof(MpegEncContext)); | |
1405 | memcpy(s->thread_context[i], s, sizeof(MpegEncContext)); | |
1406 | } | |
1407 | ||
1408 | for (i = 0; i < nb_slices; i++) { | |
12b54a1f | 1409 | if (init_duplicate_context(s->thread_context[i]) < 0) |
435c0b87 JG |
1410 | goto fail; |
1411 | s->thread_context[i]->start_mb_y = | |
1412 | (s->mb_height * (i) + nb_slices / 2) / nb_slices; | |
1413 | s->thread_context[i]->end_mb_y = | |
1414 | (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices; | |
1415 | } | |
1416 | } else { | |
12b54a1f | 1417 | if (init_duplicate_context(s) < 0) |
435c0b87 JG |
1418 | goto fail; |
1419 | s->start_mb_y = 0; | |
1420 | s->end_mb_y = s->mb_height; | |
1421 | } | |
1422 | s->slice_context_count = nb_slices; | |
1423 | } | |
1424 | ||
1425 | return 0; | |
1426 | fail: | |
1427 | ff_MPV_common_end(s); | |
1428 | return err; | |
1429 | } | |
1430 | ||
1b3439b3 JG |
1431 | /* init common structure for both encoder and decoder */ |
1432 | void ff_MPV_common_end(MpegEncContext *s) | |
1433 | { | |
1434 | int i; | |
1435 | ||
1436 | if (s->slice_context_count > 1) { | |
1437 | for (i = 0; i < s->slice_context_count; i++) { | |
1438 | free_duplicate_context(s->thread_context[i]); | |
1439 | } | |
1440 | for (i = 1; i < s->slice_context_count; i++) { | |
1441 | av_freep(&s->thread_context[i]); | |
1442 | } | |
1443 | s->slice_context_count = 1; | |
1444 | } else free_duplicate_context(s); | |
1445 | ||
1446 | av_freep(&s->parse_context.buffer); | |
1447 | s->parse_context.buffer_size = 0; | |
1448 | ||
1449 | av_freep(&s->bitstream_buffer); | |
1450 | s->allocated_bitstream_buffer_size = 0; | |
1451 | ||
759001c5 AK |
1452 | if (s->picture) { |
1453 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { | |
0b0a7a75 | 1454 | ff_free_picture_tables(&s->picture[i]); |
759001c5 | 1455 | ff_mpeg_unref_picture(s, &s->picture[i]); |
9b4b6e09 | 1456 | } |
de6d9b64 | 1457 | } |
b465449e | 1458 | av_freep(&s->picture); |
0b0a7a75 | 1459 | ff_free_picture_tables(&s->last_picture); |
759001c5 | 1460 | ff_mpeg_unref_picture(s, &s->last_picture); |
0b0a7a75 | 1461 | ff_free_picture_tables(&s->current_picture); |
759001c5 | 1462 | ff_mpeg_unref_picture(s, &s->current_picture); |
0b0a7a75 | 1463 | ff_free_picture_tables(&s->next_picture); |
759001c5 | 1464 | ff_mpeg_unref_picture(s, &s->next_picture); |
32c7589b JG |
1465 | |
1466 | free_context_frame(s); | |
1467 | ||
4d9ec050 KT |
1468 | s->context_initialized = 0; |
1469 | s->last_picture_ptr = | |
1470 | s->next_picture_ptr = | |
1471 | s->current_picture_ptr = NULL; | |
1472 | s->linesize = s->uvlinesize = 0; | |
de6d9b64 FB |
1473 | } |
1474 | ||
6fee1b90 DB |
1475 | av_cold void ff_init_rl(RLTable *rl, |
1476 | uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3]) | |
1d0d55da | 1477 | { |
4d9ec050 KT |
1478 | int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1]; |
1479 | uint8_t index_run[MAX_RUN + 1]; | |
1d0d55da MN |
1480 | int last, run, level, start, end, i; |
1481 | ||
c65dfac4 | 1482 | /* If table is static, we can quit if rl->max_level[0] is not NULL */ |
4d9ec050 | 1483 | if (static_store && rl->max_level[0]) |
073c2593 BP |
1484 | return; |
1485 | ||
1d0d55da | 1486 | /* compute max_level[], max_run[] and index_run[] */ |
4d9ec050 | 1487 | for (last = 0; last < 2; last++) { |
1d0d55da MN |
1488 | if (last == 0) { |
1489 | start = 0; | |
1490 | end = rl->last; | |
1491 | } else { | |
1492 | start = rl->last; | |
1493 | end = rl->n; | |
1494 | } | |
1495 | ||
1496 | memset(max_level, 0, MAX_RUN + 1); | |
1497 | memset(max_run, 0, MAX_LEVEL + 1); | |
1498 | memset(index_run, rl->n, MAX_RUN + 1); | |
4d9ec050 KT |
1499 | for (i = start; i < end; i++) { |
1500 | run = rl->table_run[i]; | |
1d0d55da MN |
1501 | level = rl->table_level[i]; |
1502 | if (index_run[run] == rl->n) | |
1503 | index_run[run] = i; | |
1504 | if (level > max_level[run]) | |
1505 | max_level[run] = level; | |
1506 | if (run > max_run[level]) | |
1507 | max_run[level] = run; | |
1508 | } | |
4d9ec050 | 1509 | if (static_store) |
3502a54f | 1510 | rl->max_level[last] = static_store[last]; |
073c2593 BP |
1511 | else |
1512 | rl->max_level[last] = av_malloc(MAX_RUN + 1); | |
1d0d55da | 1513 | memcpy(rl->max_level[last], max_level, MAX_RUN + 1); |
4d9ec050 KT |
1514 | if (static_store) |
1515 | rl->max_run[last] = static_store[last] + MAX_RUN + 1; | |
073c2593 | 1516 | else |
4d9ec050 | 1517 | rl->max_run[last] = av_malloc(MAX_LEVEL + 1); |
1d0d55da | 1518 | memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1); |
4d9ec050 | 1519 | if (static_store) |
3502a54f | 1520 | rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2; |
073c2593 BP |
1521 | else |
1522 | rl->index_run[last] = av_malloc(MAX_RUN + 1); | |
1d0d55da MN |
1523 | memcpy(rl->index_run[last], index_run, MAX_RUN + 1); |
1524 | } | |
1525 | } | |
1526 | ||
6fee1b90 | 1527 | av_cold void ff_init_vlc_rl(RLTable *rl) |
898d5d5d AJ |
1528 | { |
1529 | int i, q; | |
1530 | ||
4d9ec050 KT |
1531 | for (q = 0; q < 32; q++) { |
1532 | int qmul = q * 2; | |
1533 | int qadd = (q - 1) | 1; | |
898d5d5d | 1534 | |
4d9ec050 KT |
1535 | if (q == 0) { |
1536 | qmul = 1; | |
1537 | qadd = 0; | |
898d5d5d | 1538 | } |
4d9ec050 KT |
1539 | for (i = 0; i < rl->vlc.table_size; i++) { |
1540 | int code = rl->vlc.table[i][0]; | |
1541 | int len = rl->vlc.table[i][1]; | |
898d5d5d AJ |
1542 | int level, run; |
1543 | ||
4d9ec050 KT |
1544 | if (len == 0) { // illegal code |
1545 | run = 66; | |
1546 | level = MAX_LEVEL; | |
1547 | } else if (len < 0) { // more bits needed | |
1548 | run = 0; | |
1549 | level = code; | |
1550 | } else { | |
1551 | if (code == rl->n) { // esc | |
1552 | run = 66; | |
1553 | level = 0; | |
1554 | } else { | |
1555 | run = rl->table_run[code] + 1; | |
1556 | level = rl->table_level[code] * qmul + qadd; | |
1557 | if (code >= rl->last) run += 192; | |
898d5d5d AJ |
1558 | } |
1559 | } | |
4d9ec050 KT |
1560 | rl->rl_vlc[q][i].len = len; |
1561 | rl->rl_vlc[q][i].level = level; | |
1562 | rl->rl_vlc[q][i].run = run; | |
898d5d5d AJ |
1563 | } |
1564 | } | |
1565 | } | |
1566 | ||
282c6a1a | 1567 | static void release_unused_pictures(MpegEncContext *s) |
6a9c8594 AS |
1568 | { |
1569 | int i; | |
1570 | ||
1571 | /* release non reference frames */ | |
759001c5 | 1572 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
282c6a1a | 1573 | if (!s->picture[i].reference) |
759001c5 | 1574 | ff_mpeg_unref_picture(s, &s->picture[i]); |
6a9c8594 AS |
1575 | } |
1576 | } | |
1577 | ||
435c0b87 JG |
1578 | static inline int pic_is_unused(MpegEncContext *s, Picture *pic) |
1579 | { | |
a553c6a3 | 1580 | if (pic->f.buf[0] == NULL) |
435c0b87 | 1581 | return 1; |
759001c5 AK |
1582 | if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF)) |
1583 | return 1; | |
435c0b87 JG |
1584 | return 0; |
1585 | } | |
1586 | ||
1587 | static int find_unused_picture(MpegEncContext *s, int shared) | |
4d9ec050 | 1588 | { |
4e00e76b | 1589 | int i; |
115329f1 | 1590 | |
4d9ec050 | 1591 | if (shared) { |
759001c5 | 1592 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
a553c6a3 | 1593 | if (s->picture[i].f.buf[0] == NULL) |
657ccb5a | 1594 | return i; |
4e00e76b | 1595 | } |
4d9ec050 | 1596 | } else { |
759001c5 | 1597 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
435c0b87 | 1598 | if (pic_is_unused(s, &s->picture[i])) |
657ccb5a | 1599 | return i; |
4e00e76b MN |
1600 | } |
1601 | } | |
1602 | ||
4f820131 | 1603 | return AVERROR_INVALIDDATA; |
4e00e76b MN |
1604 | } |
1605 | ||
435c0b87 JG |
1606 | int ff_find_unused_picture(MpegEncContext *s, int shared) |
1607 | { | |
1608 | int ret = find_unused_picture(s, shared); | |
1609 | ||
759001c5 | 1610 | if (ret >= 0 && ret < MAX_PICTURE_COUNT) { |
435c0b87 JG |
1611 | if (s->picture[ret].needs_realloc) { |
1612 | s->picture[ret].needs_realloc = 0; | |
0b0a7a75 | 1613 | ff_free_picture_tables(&s->picture[ret]); |
759001c5 | 1614 | ff_mpeg_unref_picture(s, &s->picture[ret]); |
435c0b87 JG |
1615 | } |
1616 | } | |
1617 | return ret; | |
1618 | } | |
1619 | ||
5f194811 | 1620 | /** |
aec25b1c AK |
1621 | * generic function called after decoding |
1622 | * the header and before a frame is decoded. | |
5f194811 | 1623 | */ |
efd29844 | 1624 | int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) |
de6d9b64 | 1625 | { |
759001c5 | 1626 | int i, ret; |
357ec71f | 1627 | Picture *pic; |
160d679c | 1628 | s->mb_skipped = 0; |
0da71265 | 1629 | |
c65dfac4 | 1630 | /* mark & release old frames */ |
ee870491 AK |
1631 | if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && |
1632 | s->last_picture_ptr != s->next_picture_ptr && | |
a553c6a3 | 1633 | s->last_picture_ptr->f.buf[0]) { |
ee870491 AK |
1634 | ff_mpeg_unref_picture(s, s->last_picture_ptr); |
1635 | } | |
c65dfac4 | 1636 | |
ee870491 AK |
1637 | /* release forgotten pictures */ |
1638 | /* if (mpeg124/h263) */ | |
aec25b1c AK |
1639 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
1640 | if (&s->picture[i] != s->last_picture_ptr && | |
1641 | &s->picture[i] != s->next_picture_ptr && | |
1642 | s->picture[i].reference && !s->picture[i].needs_realloc) { | |
1643 | if (!(avctx->active_thread_type & FF_THREAD_FRAME)) | |
1644 | av_log(avctx, AV_LOG_ERROR, | |
1645 | "releasing zombie picture\n"); | |
1646 | ff_mpeg_unref_picture(s, &s->picture[i]); | |
d6db1c9c | 1647 | } |
ee870491 | 1648 | } |
d52b4abe | 1649 | |
4b796681 JG |
1650 | ff_mpeg_unref_picture(s, &s->current_picture); |
1651 | ||
aec25b1c | 1652 | release_unused_pictures(s); |
e20c4069 | 1653 | |
aec25b1c AK |
1654 | if (s->current_picture_ptr && |
1655 | s->current_picture_ptr->f.buf[0] == NULL) { | |
1656 | // we already have a unused image | |
1657 | // (maybe it was set before reading the header) | |
1658 | pic = s->current_picture_ptr; | |
1659 | } else { | |
1660 | i = ff_find_unused_picture(s, 0); | |
1661 | if (i < 0) { | |
1662 | av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
1663 | return i; | |
5f194811 | 1664 | } |
aec25b1c AK |
1665 | pic = &s->picture[i]; |
1666 | } | |
5f194811 | 1667 | |
aec25b1c AK |
1668 | pic->reference = 0; |
1669 | if (!s->droppable) { | |
1670 | if (s->pict_type != AV_PICTURE_TYPE_B) | |
1671 | pic->reference = 3; | |
1672 | } | |
b536d0aa | 1673 | |
aec25b1c | 1674 | pic->f.coded_picture_number = s->coded_picture_number++; |
115329f1 | 1675 | |
aec25b1c AK |
1676 | if (ff_alloc_picture(s, pic, 0) < 0) |
1677 | return -1; | |
93a21abd | 1678 | |
aec25b1c AK |
1679 | s->current_picture_ptr = pic; |
1680 | // FIXME use only the vars from current_pic | |
1681 | s->current_picture_ptr->f.top_field_first = s->top_field_first; | |
1682 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || | |
1683 | s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { | |
1684 | if (s->picture_structure != PICT_FRAME) | |
1685 | s->current_picture_ptr->f.top_field_first = | |
1686 | (s->picture_structure == PICT_TOP_FIELD) == s->first_field; | |
1e491e29 | 1687 | } |
aec25b1c AK |
1688 | s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame && |
1689 | !s->progressive_sequence; | |
1690 | s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME; | |
b7adc711 | 1691 | |
657ccb5a | 1692 | s->current_picture_ptr->f.pict_type = s->pict_type; |
c65dfac4 KT |
1693 | // if (s->flags && CODEC_FLAG_QSCALE) |
1694 | // s->current_picture_ptr->quality = s->new_picture_ptr->quality; | |
657ccb5a | 1695 | s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; |
9f2e61b6 | 1696 | |
759001c5 AK |
1697 | if ((ret = ff_mpeg_ref_picture(s, &s->current_picture, |
1698 | s->current_picture_ptr)) < 0) | |
1699 | return ret; | |
115329f1 | 1700 | |
19cac8e3 | 1701 | if (s->pict_type != AV_PICTURE_TYPE_B) { |
c65dfac4 | 1702 | s->last_picture_ptr = s->next_picture_ptr; |
ba0c8981 | 1703 | if (!s->droppable) |
c65dfac4 | 1704 | s->next_picture_ptr = s->current_picture_ptr; |
de6d9b64 | 1705 | } |
1218777f DB |
1706 | av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", |
1707 | s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr, | |
1708 | s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL, | |
1709 | s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL, | |
1710 | s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL, | |
ba0c8981 | 1711 | s->pict_type, s->droppable); |
c65dfac4 | 1712 | |
ee870491 | 1713 | if ((s->last_picture_ptr == NULL || |
a553c6a3 | 1714 | s->last_picture_ptr->f.buf[0] == NULL) && |
ee870491 AK |
1715 | (s->pict_type != AV_PICTURE_TYPE_I || |
1716 | s->picture_structure != PICT_FRAME)) { | |
1717 | int h_chroma_shift, v_chroma_shift; | |
1718 | av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, | |
1719 | &h_chroma_shift, &v_chroma_shift); | |
1720 | if (s->pict_type != AV_PICTURE_TYPE_I) | |
1721 | av_log(avctx, AV_LOG_ERROR, | |
1722 | "warning: first frame is no keyframe\n"); | |
1723 | else if (s->picture_structure != PICT_FRAME) | |
1724 | av_log(avctx, AV_LOG_INFO, | |
1725 | "allocate dummy last picture for field based first keyframe\n"); | |
1726 | ||
1727 | /* Allocate a dummy frame */ | |
1728 | i = ff_find_unused_picture(s, 0); | |
1729 | if (i < 0) { | |
1730 | av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
1731 | return i; | |
d52b4abe | 1732 | } |
ee870491 | 1733 | s->last_picture_ptr = &s->picture[i]; |
feded990 AK |
1734 | |
1735 | s->last_picture_ptr->reference = 3; | |
1736 | s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_I; | |
1737 | ||
ee870491 AK |
1738 | if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) { |
1739 | s->last_picture_ptr = NULL; | |
1740 | return -1; | |
d52b4abe | 1741 | } |
d52b4abe | 1742 | |
ee870491 AK |
1743 | memset(s->last_picture_ptr->f.data[0], 0, |
1744 | avctx->height * s->last_picture_ptr->f.linesize[0]); | |
1745 | memset(s->last_picture_ptr->f.data[1], 0x80, | |
1746 | (avctx->height >> v_chroma_shift) * | |
1747 | s->last_picture_ptr->f.linesize[1]); | |
1748 | memset(s->last_picture_ptr->f.data[2], 0x80, | |
1749 | (avctx->height >> v_chroma_shift) * | |
1750 | s->last_picture_ptr->f.linesize[2]); | |
1751 | ||
1752 | ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0); | |
1753 | ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1); | |
1754 | } | |
1755 | if ((s->next_picture_ptr == NULL || | |
a553c6a3 | 1756 | s->next_picture_ptr->f.buf[0] == NULL) && |
ee870491 AK |
1757 | s->pict_type == AV_PICTURE_TYPE_B) { |
1758 | /* Allocate a dummy frame */ | |
1759 | i = ff_find_unused_picture(s, 0); | |
1760 | if (i < 0) { | |
1761 | av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
1762 | return i; | |
759001c5 | 1763 | } |
ee870491 | 1764 | s->next_picture_ptr = &s->picture[i]; |
feded990 AK |
1765 | |
1766 | s->next_picture_ptr->reference = 3; | |
1767 | s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_I; | |
1768 | ||
ee870491 AK |
1769 | if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) { |
1770 | s->next_picture_ptr = NULL; | |
1771 | return -1; | |
759001c5 | 1772 | } |
ee870491 AK |
1773 | ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0); |
1774 | ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1); | |
1775 | } | |
1776 | ||
1777 | if (s->last_picture_ptr) { | |
1778 | ff_mpeg_unref_picture(s, &s->last_picture); | |
a553c6a3 | 1779 | if (s->last_picture_ptr->f.buf[0] && |
ee870491 AK |
1780 | (ret = ff_mpeg_ref_picture(s, &s->last_picture, |
1781 | s->last_picture_ptr)) < 0) | |
1782 | return ret; | |
1783 | } | |
1784 | if (s->next_picture_ptr) { | |
1785 | ff_mpeg_unref_picture(s, &s->next_picture); | |
a553c6a3 | 1786 | if (s->next_picture_ptr->f.buf[0] && |
ee870491 AK |
1787 | (ret = ff_mpeg_ref_picture(s, &s->next_picture, |
1788 | s->next_picture_ptr)) < 0) | |
1789 | return ret; | |
1790 | } | |
115329f1 | 1791 | |
72072bf9 | 1792 | if (s->pict_type != AV_PICTURE_TYPE_I && |
a553c6a3 | 1793 | !(s->last_picture_ptr && s->last_picture_ptr->f.buf[0])) { |
72072bf9 LB |
1794 | av_log(s, AV_LOG_ERROR, |
1795 | "Non-reference picture received and no reference available\n"); | |
1796 | return AVERROR_INVALIDDATA; | |
1797 | } | |
3ab77000 | 1798 | |
19cac8e3 | 1799 | if (s->picture_structure!= PICT_FRAME) { |
b536d0aa | 1800 | int i; |
c65dfac4 KT |
1801 | for (i = 0; i < 4; i++) { |
1802 | if (s->picture_structure == PICT_BOTTOM_FIELD) { | |
1803 | s->current_picture.f.data[i] += | |
1804 | s->current_picture.f.linesize[i]; | |
115329f1 | 1805 | } |
657ccb5a DB |
1806 | s->current_picture.f.linesize[i] *= 2; |
1807 | s->last_picture.f.linesize[i] *= 2; | |
1808 | s->next_picture.f.linesize[i] *= 2; | |
b536d0aa MN |
1809 | } |
1810 | } | |
115329f1 | 1811 | |
5b22d6e1 | 1812 | s->err_recognition = avctx->err_recognition; |
aa388dba | 1813 | |
c65dfac4 KT |
1814 | /* set dequantizer, we can't do it during init as |
1815 | * it might change for mpeg4 and we can't do it in the header | |
1816 | * decode as init is not called for mpeg4 there yet */ | |
36ef5369 | 1817 | if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
d50635cd MN |
1818 | s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra; |
1819 | s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter; | |
c65dfac4 | 1820 | } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) { |
d50635cd MN |
1821 | s->dct_unquantize_intra = s->dct_unquantize_h263_intra; |
1822 | s->dct_unquantize_inter = s->dct_unquantize_h263_inter; | |
c65dfac4 | 1823 | } else { |
d50635cd MN |
1824 | s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra; |
1825 | s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter; | |
1826 | } | |
d6db1c9c | 1827 | |
19e30a58 DB |
1828 | #if FF_API_XVMC |
1829 | FF_DISABLE_DEPRECATION_WARNINGS | |
c65dfac4 | 1830 | if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) |
a002da79 | 1831 | return ff_xvmc_field_start(s, avctx); |
19e30a58 DB |
1832 | FF_ENABLE_DEPRECATION_WARNINGS |
1833 | #endif /* FF_API_XVMC */ | |
83344066 | 1834 | |
d6db1c9c | 1835 | return 0; |
de6d9b64 | 1836 | } |
21af69f7 | 1837 | |
381a7225 | 1838 | /* called after a frame has been decoded. */ |
efd29844 | 1839 | void ff_MPV_frame_end(MpegEncContext *s) |
de6d9b64 | 1840 | { |
19e30a58 DB |
1841 | #if FF_API_XVMC |
1842 | FF_DISABLE_DEPRECATION_WARNINGS | |
6a9c8594 | 1843 | /* redraw edges for the frame if decoding didn't complete */ |
c65dfac4 KT |
1844 | // just to make sure that all data is rendered. |
1845 | if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) { | |
a002da79 | 1846 | ff_xvmc_field_end(s); |
19e30a58 DB |
1847 | } else |
1848 | FF_ENABLE_DEPRECATION_WARNINGS | |
1849 | #endif /* FF_API_XVMC */ | |
6a9c8594 | 1850 | |
5975626d | 1851 | emms_c(); |
115329f1 | 1852 | |
19cac8e3 | 1853 | if (s->current_picture.reference) |
759001c5 | 1854 | ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0); |
de6d9b64 FB |
1855 | } |
1856 | ||
7bc9090a | 1857 | /** |
c65dfac4 | 1858 | * Print debugging info for the given picture. |
7bc9090a | 1859 | */ |
759001c5 | 1860 | void ff_print_debug_info(MpegEncContext *s, Picture *p) |
c65dfac4 | 1861 | { |
759001c5 AK |
1862 | AVFrame *pict; |
1863 | if (s->avctx->hwaccel || !p || !p->mb_type) | |
c65dfac4 | 1864 | return; |
759001c5 | 1865 | pict = &p->f; |
7bc9090a | 1866 | |
c65dfac4 | 1867 | if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) { |
7bc9090a | 1868 | int x,y; |
115329f1 | 1869 | |
0c9bbaec WH |
1870 | av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: "); |
1871 | switch (pict->pict_type) { | |
c65dfac4 KT |
1872 | case AV_PICTURE_TYPE_I: |
1873 | av_log(s->avctx,AV_LOG_DEBUG,"I\n"); | |
1874 | break; | |
1875 | case AV_PICTURE_TYPE_P: | |
1876 | av_log(s->avctx,AV_LOG_DEBUG,"P\n"); | |
1877 | break; | |
1878 | case AV_PICTURE_TYPE_B: | |
1879 | av_log(s->avctx,AV_LOG_DEBUG,"B\n"); | |
1880 | break; | |
1881 | case AV_PICTURE_TYPE_S: | |
1882 | av_log(s->avctx,AV_LOG_DEBUG,"S\n"); | |
1883 | break; | |
1884 | case AV_PICTURE_TYPE_SI: | |
1885 | av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); | |
1886 | break; | |
1887 | case AV_PICTURE_TYPE_SP: | |
1888 | av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); | |
1889 | break; | |
0c9bbaec | 1890 | } |
c65dfac4 KT |
1891 | for (y = 0; y < s->mb_height; y++) { |
1892 | for (x = 0; x < s->mb_width; x++) { | |
1893 | if (s->avctx->debug & FF_DEBUG_SKIP) { | |
1894 | int count = s->mbskip_table[x + y * s->mb_stride]; | |
1895 | if (count > 9) | |
1896 | count = 9; | |
9b879566 | 1897 | av_log(s->avctx, AV_LOG_DEBUG, "%1d", count); |
7bc9090a | 1898 | } |
c65dfac4 KT |
1899 | if (s->avctx->debug & FF_DEBUG_QP) { |
1900 | av_log(s->avctx, AV_LOG_DEBUG, "%2d", | |
759001c5 | 1901 | p->qscale_table[x + y * s->mb_stride]); |
7bc9090a | 1902 | } |
c65dfac4 | 1903 | if (s->avctx->debug & FF_DEBUG_MB_TYPE) { |
759001c5 | 1904 | int mb_type = p->mb_type[x + y * s->mb_stride]; |
c65dfac4 KT |
1905 | // Type & MV direction |
1906 | if (IS_PCM(mb_type)) | |
9b879566 | 1907 | av_log(s->avctx, AV_LOG_DEBUG, "P"); |
c65dfac4 | 1908 | else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type)) |
9b879566 | 1909 | av_log(s->avctx, AV_LOG_DEBUG, "A"); |
c65dfac4 | 1910 | else if (IS_INTRA4x4(mb_type)) |
9b879566 | 1911 | av_log(s->avctx, AV_LOG_DEBUG, "i"); |
c65dfac4 | 1912 | else if (IS_INTRA16x16(mb_type)) |
9b879566 | 1913 | av_log(s->avctx, AV_LOG_DEBUG, "I"); |
c65dfac4 | 1914 | else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) |
9b879566 | 1915 | av_log(s->avctx, AV_LOG_DEBUG, "d"); |
c65dfac4 | 1916 | else if (IS_DIRECT(mb_type)) |
9b879566 | 1917 | av_log(s->avctx, AV_LOG_DEBUG, "D"); |
c65dfac4 | 1918 | else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) |
9b879566 | 1919 | av_log(s->avctx, AV_LOG_DEBUG, "g"); |
c65dfac4 | 1920 | else if (IS_GMC(mb_type)) |
9b879566 | 1921 | av_log(s->avctx, AV_LOG_DEBUG, "G"); |
c65dfac4 | 1922 | else if (IS_SKIP(mb_type)) |
9b879566 | 1923 | av_log(s->avctx, AV_LOG_DEBUG, "S"); |
c65dfac4 | 1924 | else if (!USES_LIST(mb_type, 1)) |
9b879566 | 1925 | av_log(s->avctx, AV_LOG_DEBUG, ">"); |
c65dfac4 | 1926 | else if (!USES_LIST(mb_type, 0)) |
9b879566 | 1927 | av_log(s->avctx, AV_LOG_DEBUG, "<"); |
c65dfac4 | 1928 | else { |
7bc9090a | 1929 | assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1)); |
9b879566 | 1930 | av_log(s->avctx, AV_LOG_DEBUG, "X"); |
7bc9090a | 1931 | } |
115329f1 | 1932 | |
c65dfac4 KT |
1933 | // segmentation |
1934 | if (IS_8X8(mb_type)) | |
9b879566 | 1935 | av_log(s->avctx, AV_LOG_DEBUG, "+"); |
c65dfac4 | 1936 | else if (IS_16X8(mb_type)) |
9b879566 | 1937 | av_log(s->avctx, AV_LOG_DEBUG, "-"); |
c65dfac4 | 1938 | else if (IS_8X16(mb_type)) |
30344a83 | 1939 | av_log(s->avctx, AV_LOG_DEBUG, "|"); |
c65dfac4 | 1940 | else if (IS_INTRA(mb_type) || IS_16X16(mb_type)) |
9b879566 | 1941 | av_log(s->avctx, AV_LOG_DEBUG, " "); |
7bc9090a | 1942 | else |
9b879566 | 1943 | av_log(s->avctx, AV_LOG_DEBUG, "?"); |
115329f1 DB |
1944 | |
1945 | ||
c65dfac4 | 1946 | if (IS_INTERLACED(mb_type)) |
9b879566 | 1947 | av_log(s->avctx, AV_LOG_DEBUG, "="); |
7bc9090a | 1948 | else |
9b879566 | 1949 | av_log(s->avctx, AV_LOG_DEBUG, " "); |
7bc9090a | 1950 | } |
7bc9090a | 1951 | } |
9b879566 | 1952 | av_log(s->avctx, AV_LOG_DEBUG, "\n"); |
7bc9090a MN |
1953 | } |
1954 | } | |
1955 | } | |
1956 | ||
6a9c8594 AS |
1957 | /** |
1958 | * find the lowest MB row referenced in the MVs | |
1959 | */ | |
efd29844 | 1960 | int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir) |
6a9c8594 AS |
1961 | { |
1962 | int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample; | |
1963 | int my, off, i, mvs; | |
1964 | ||
6bcdfe48 MN |
1965 | if (s->picture_structure != PICT_FRAME || s->mcsel) |
1966 | goto unhandled; | |
6a9c8594 AS |
1967 | |
1968 | switch (s->mv_type) { | |
1969 | case MV_TYPE_16X16: | |
1970 | mvs = 1; | |
1971 | break; | |
1972 | case MV_TYPE_16X8: | |
1973 | mvs = 2; | |
1974 | break; | |
1975 | case MV_TYPE_8X8: | |
1976 | mvs = 4; | |
1977 | break; | |
1978 | default: | |
1979 | goto unhandled; | |
1980 | } | |
1981 | ||
1982 | for (i = 0; i < mvs; i++) { | |
1983 | my = s->mv[dir][i][1]<<qpel_shift; | |
1984 | my_max = FFMAX(my_max, my); | |
1985 | my_min = FFMIN(my_min, my); | |
1986 | } | |
1987 | ||
1988 | off = (FFMAX(-my_min, my_max) + 63) >> 6; | |
1989 | ||
1990 | return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1); | |
1991 | unhandled: | |
1992 | return s->mb_height-1; | |
1993 | } | |
1994 | ||
3ada94ba BF |
1995 | /* put block[] to dest[] */ |
1996 | static inline void put_dct(MpegEncContext *s, | |
88bd7fdc | 1997 | int16_t *block, int i, uint8_t *dest, int line_size, int qscale) |
178fcca8 | 1998 | { |
3ada94ba BF |
1999 | s->dct_unquantize_intra(s, block, i, qscale); |
2000 | s->dsp.idct_put (dest, line_size, block); | |
2001 | } | |
da9c9637 | 2002 | |
3ada94ba BF |
2003 | /* add block[] to dest[] */ |
2004 | static inline void add_dct(MpegEncContext *s, | |
88bd7fdc | 2005 | int16_t *block, int i, uint8_t *dest, int line_size) |
3ada94ba BF |
2006 | { |
2007 | if (s->block_last_index[i] >= 0) { | |
2008 | s->dsp.idct_add (dest, line_size, block); | |
2009 | } | |
2010 | } | |
2417652e | 2011 | |
115329f1 | 2012 | static inline void add_dequant_dct(MpegEncContext *s, |
88bd7fdc | 2013 | int16_t *block, int i, uint8_t *dest, int line_size, int qscale) |
0f440e02 | 2014 | { |
de6d9b64 | 2015 | if (s->block_last_index[i] >= 0) { |
d50635cd | 2016 | s->dct_unquantize_inter(s, block, i, qscale); |
9dbcbd92 | 2017 | |
b0368839 | 2018 | s->dsp.idct_add (dest, line_size, block); |
de6d9b64 FB |
2019 | } |
2020 | } | |
2021 | ||
7f2fe444 | 2022 | /** |
58c42af7 | 2023 | * Clean dc, ac, coded_block for the current non-intra MB. |
7f2fe444 MN |
2024 | */ |
2025 | void ff_clean_intra_table_entries(MpegEncContext *s) | |
2026 | { | |
137c8468 | 2027 | int wrap = s->b8_stride; |
7f2fe444 | 2028 | int xy = s->block_index[0]; |
115329f1 DB |
2029 | |
2030 | s->dc_val[0][xy ] = | |
2031 | s->dc_val[0][xy + 1 ] = | |
7f2fe444 MN |
2032 | s->dc_val[0][xy + wrap] = |
2033 | s->dc_val[0][xy + 1 + wrap] = 1024; | |
2034 | /* ac pred */ | |
0c1a9eda ZK |
2035 | memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t)); |
2036 | memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t)); | |
7f2fe444 MN |
2037 | if (s->msmpeg4_version>=3) { |
2038 | s->coded_block[xy ] = | |
2039 | s->coded_block[xy + 1 ] = | |
2040 | s->coded_block[xy + wrap] = | |
2041 | s->coded_block[xy + 1 + wrap] = 0; | |
2042 | } | |
2043 | /* chroma */ | |
137c8468 MN |
2044 | wrap = s->mb_stride; |
2045 | xy = s->mb_x + s->mb_y * wrap; | |
7f2fe444 MN |
2046 | s->dc_val[1][xy] = |
2047 | s->dc_val[2][xy] = 1024; | |
2048 | /* ac pred */ | |
0c1a9eda ZK |
2049 | memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t)); |
2050 | memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t)); | |
115329f1 | 2051 | |
137c8468 | 2052 | s->mbintra_table[xy]= 0; |
7f2fe444 MN |
2053 | } |
2054 | ||
de6d9b64 FB |
2055 | /* generic function called after a macroblock has been parsed by the |
2056 | decoder or after it has been encoded by the encoder. | |
2057 | ||
2058 | Important variables used: | |
2059 | s->mb_intra : true if intra macroblock | |
2060 | s->mv_dir : motion vector direction | |
2061 | s->mv_type : motion vector type | |
2062 | s->mv : motion vector | |
2063 | s->interlaced_dct : true if interlaced dct used (mpeg2) | |
2064 | */ | |
54816a3e | 2065 | static av_always_inline |
88bd7fdc | 2066 | void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64], |
2bcbd984 | 2067 | int is_mpeg12) |
de6d9b64 | 2068 | { |
7bc9090a | 2069 | const int mb_xy = s->mb_y * s->mb_stride + s->mb_x; |
19e30a58 DB |
2070 | |
2071 | #if FF_API_XVMC | |
2072 | FF_DISABLE_DEPRECATION_WARNINGS | |
83344066 | 2073 | if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){ |
78f9a878 | 2074 | ff_xvmc_decode_mb(s);//xvmc uses pblocks |
2e7b4c84 IK |
2075 | return; |
2076 | } | |
19e30a58 DB |
2077 | FF_ENABLE_DEPRECATION_WARNINGS |
2078 | #endif /* FF_API_XVMC */ | |
de6d9b64 | 2079 | |
8289c6fa | 2080 | if(s->avctx->debug&FF_DEBUG_DCT_COEFF) { |
759001c5 | 2081 | /* print DCT coefficients */ |
8289c6fa | 2082 | int i,j; |
c4fb3b03 MN |
2083 | av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y); |
2084 | for(i=0; i<6; i++){ | |
2085 | for(j=0; j<64; j++){ | |
759001c5 | 2086 | av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]); |
c4fb3b03 MN |
2087 | } |
2088 | av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
2089 | } | |
8289c6fa WH |
2090 | } |
2091 | ||
759001c5 | 2092 | s->current_picture.qscale_table[mb_xy] = s->qscale; |
79e7b305 | 2093 | |
de6d9b64 FB |
2094 | /* update DC predictors for P macroblocks */ |
2095 | if (!s->mb_intra) { | |
bd7c626a | 2096 | if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) { |
0f440e02 | 2097 | if(s->mbintra_table[mb_xy]) |
7f2fe444 | 2098 | ff_clean_intra_table_entries(s); |
de6d9b64 | 2099 | } else { |
7f2fe444 MN |
2100 | s->last_dc[0] = |
2101 | s->last_dc[1] = | |
de6d9b64 FB |
2102 | s->last_dc[2] = 128 << s->intra_dc_precision; |
2103 | } | |
2104 | } | |
bd7c626a | 2105 | else if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) |
0f440e02 | 2106 | s->mbintra_table[mb_xy]=1; |
bff6ecaa | 2107 | |
975a1447 | 2108 | if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc |
0c1a9eda | 2109 | uint8_t *dest_y, *dest_cb, *dest_cr; |
0f440e02 | 2110 | int dct_linesize, dct_offset; |
b3184779 MN |
2111 | op_pixels_func (*op_pix)[4]; |
2112 | qpel_mc_func (*op_qpix)[16]; | |
657ccb5a DB |
2113 | const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics |
2114 | const int uvlinesize = s->current_picture.f.linesize[1]; | |
2bcbd984 MR |
2115 | const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band; |
2116 | const int block_size = 8; | |
3bb4e23a | 2117 | |
1e491e29 | 2118 | /* avoid copy if macroblock skipped in last frame too */ |
1e491e29 MN |
2119 | /* skip only during decoding as we might trash the buffers during encoding a bit */ |
2120 | if(!s->encoding){ | |
0c1a9eda | 2121 | uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy]; |
1e491e29 | 2122 | |
160d679c MM |
2123 | if (s->mb_skipped) { |
2124 | s->mb_skipped= 0; | |
975a1447 | 2125 | assert(s->pict_type!=AV_PICTURE_TYPE_I); |
8400b126 | 2126 | *mbskip_ptr = 1; |
759001c5 | 2127 | } else if(!s->current_picture.reference) { |
8400b126 | 2128 | *mbskip_ptr = 1; |
f943e138 | 2129 | } else{ |
3bb4e23a FB |
2130 | *mbskip_ptr = 0; /* not skipped */ |
2131 | } | |
3994623d | 2132 | } |
115329f1 | 2133 | |
ffdff4d7 | 2134 | dct_linesize = linesize << s->interlaced_dct; |
3dc99a18 | 2135 | dct_offset = s->interlaced_dct ? linesize : linesize * block_size; |
115329f1 | 2136 | |
b68ab260 MN |
2137 | if(readable){ |
2138 | dest_y= s->dest[0]; | |
2139 | dest_cb= s->dest[1]; | |
2140 | dest_cr= s->dest[2]; | |
2141 | }else{ | |
9c3d33d6 | 2142 | dest_y = s->b_scratchpad; |
ae35f5e1 | 2143 | dest_cb= s->b_scratchpad+16*linesize; |
ffdff4d7 | 2144 | dest_cr= s->b_scratchpad+32*linesize; |
b68ab260 | 2145 | } |
178fcca8 | 2146 | |
de6d9b64 FB |
2147 | if (!s->mb_intra) { |
2148 | /* motion handling */ | |
dfb706da | 2149 | /* decoding or more than one mb_type (MC was already done otherwise) */ |
7d1c3fc1 | 2150 | if(!s->encoding){ |
6a9c8594 | 2151 | |
27237d52 | 2152 | if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) { |
6a9c8594 | 2153 | if (s->mv_dir & MV_DIR_FORWARD) { |
759001c5 | 2154 | ff_thread_await_progress(&s->last_picture_ptr->tf, |
47c0ac96 DB |
2155 | ff_MPV_lowest_referenced_row(s, 0), |
2156 | 0); | |
6a9c8594 AS |
2157 | } |
2158 | if (s->mv_dir & MV_DIR_BACKWARD) { | |
759001c5 | 2159 | ff_thread_await_progress(&s->next_picture_ptr->tf, |
47c0ac96 DB |
2160 | ff_MPV_lowest_referenced_row(s, 1), |
2161 | 0); | |
6a9c8594 AS |
2162 | } |
2163 | } | |
2164 | ||
2bcbd984 MR |
2165 | op_qpix= s->me.qpel_put; |
2166 | if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){ | |
f4fed5a2 | 2167 | op_pix = s->hdsp.put_pixels_tab; |
178fcca8 | 2168 | }else{ |
f4fed5a2 | 2169 | op_pix = s->hdsp.put_no_rnd_pixels_tab; |
2bcbd984 MR |
2170 | } |
2171 | if (s->mv_dir & MV_DIR_FORWARD) { | |
7a851153 | 2172 | ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix); |
f4fed5a2 | 2173 | op_pix = s->hdsp.avg_pixels_tab; |
2bcbd984 MR |
2174 | op_qpix= s->me.qpel_avg; |
2175 | } | |
2176 | if (s->mv_dir & MV_DIR_BACKWARD) { | |
7a851153 | 2177 | ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix); |
9dbcbd92 | 2178 | } |
de6d9b64 FB |
2179 | } |
2180 | ||
0f440e02 | 2181 | /* skip dequant / idct if we are really late ;) */ |
8c3eba7c | 2182 | if(s->avctx->skip_idct){ |
975a1447 SS |
2183 | if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) |
2184 | ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) | |
8c3eba7c MN |
2185 | || s->avctx->skip_idct >= AVDISCARD_ALL) |
2186 | goto skip_idct; | |
2187 | } | |
0f440e02 | 2188 | |
de6d9b64 | 2189 | /* add dct residue */ |
36ef5369 AK |
2190 | if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO |
2191 | || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){ | |
178fcca8 MN |
2192 | add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale); |
2193 | add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale); | |
2194 | add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale); | |
2195 | add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale); | |
0f440e02 | 2196 | |
49fb20cb | 2197 | if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){ |
2d974017 BC |
2198 | if (s->chroma_y_shift){ |
2199 | add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale); | |
2200 | add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale); | |
2201 | }else{ | |
2202 | dct_linesize >>= 1; | |
2203 | dct_offset >>=1; | |
2204 | add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale); | |
2205 | add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale); | |
2206 | add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale); | |
2207 | add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale); | |
2208 | } | |
b50eef3a | 2209 | } |
36ef5369 | 2210 | } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){ |
178fcca8 MN |
2211 | add_dct(s, block[0], 0, dest_y , dct_linesize); |
2212 | add_dct(s, block[1], 1, dest_y + block_size, dct_linesize); | |
2213 | add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize); | |
2214 | add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize); | |
de6d9b64 | 2215 | |
49fb20cb | 2216 | if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){ |
ffdff4d7 IK |
2217 | if(s->chroma_y_shift){//Chroma420 |
2218 | add_dct(s, block[4], 4, dest_cb, uvlinesize); | |
2219 | add_dct(s, block[5], 5, dest_cr, uvlinesize); | |
2220 | }else{ | |
2221 | //chroma422 | |
2222 | dct_linesize = uvlinesize << s->interlaced_dct; | |
3dc99a18 | 2223 | dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8; |
ffdff4d7 IK |
2224 | |
2225 | add_dct(s, block[4], 4, dest_cb, dct_linesize); | |
2226 | add_dct(s, block[5], 5, dest_cr, dct_linesize); | |
2227 | add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize); | |
2228 | add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize); | |
2229 | if(!s->chroma_x_shift){//Chroma444 | |
2230 | add_dct(s, block[8], 8, dest_cb+8, dct_linesize); | |
2231 | add_dct(s, block[9], 9, dest_cr+8, dct_linesize); | |
2232 | add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize); | |
2233 | add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize); | |
2234 | } | |
2235 | } | |
2236 | }//fi gray | |
2237 | } | |
d702a2e6 | 2238 | else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) { |
1457ab52 | 2239 | ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr); |
0f440e02 | 2240 | } |
de6d9b64 FB |
2241 | } else { |
2242 | /* dct only in intra block */ | |
36ef5369 | 2243 | if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){ |
178fcca8 MN |
2244 | put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale); |
2245 | put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale); | |
2246 | put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale); | |
2247 | put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale); | |
a0201736 | 2248 | |
49fb20cb | 2249 | if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){ |
2d974017 BC |
2250 | if(s->chroma_y_shift){ |
2251 | put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale); | |
2252 | put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale); | |
2253 | }else{ | |
2254 | dct_offset >>=1; | |
2255 | dct_linesize >>=1; | |
2256 | put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale); | |
2257 | put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale); | |
2258 | put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale); | |
3ada94ba | 2259 | put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale); |
477ab036 MN |
2260 | } |
2261 | } | |
2262 | }else{ | |
3ada94ba BF |
2263 | s->dsp.idct_put(dest_y , dct_linesize, block[0]); |
2264 | s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]); | |
2265 | s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]); | |
2266 | s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]); | |
fbb89806 | 2267 | |
49fb20cb | 2268 | if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){ |
3ada94ba BF |
2269 | if(s->chroma_y_shift){ |
2270 | s->dsp.idct_put(dest_cb, uvlinesize, block[4]); | |
2271 | s->dsp.idct_put(dest_cr, uvlinesize, block[5]); | |
2272 | }else{ | |
fbb89806 | 2273 | |
3ada94ba | 2274 | dct_linesize = uvlinesize << s->interlaced_dct; |
3dc99a18 | 2275 | dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8; |
9981dfc6 | 2276 | |
3ada94ba BF |
2277 | s->dsp.idct_put(dest_cb, dct_linesize, block[4]); |
2278 | s->dsp.idct_put(dest_cr, dct_linesize, block[5]); | |
2279 | s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]); | |
2280 | s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]); | |
2281 | if(!s->chroma_x_shift){//Chroma444 | |
2282 | s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]); | |
2283 | s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]); | |
2284 | s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]); | |
2285 | s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]); | |
2286 | } | |
2287 | } | |
2288 | }//gray | |
9981dfc6 | 2289 | } |
477ab036 | 2290 | } |
3ada94ba BF |
2291 | skip_idct: |
2292 | if(!readable){ | |
f4fed5a2 RB |
2293 | s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16); |
2294 | s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift); | |
2295 | s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift); | |
34f60ee6 | 2296 | } |
477ab036 | 2297 | } |
477ab036 MN |
2298 | } |
2299 | ||
88bd7fdc | 2300 | void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){ |
b250f9c6 | 2301 | #if !CONFIG_SMALL |
bd7c626a | 2302 | if(s->out_format == FMT_MPEG1) { |
2bcbd984 | 2303 | MPV_decode_mb_internal(s, block, 1); |
bd7c626a KC |
2304 | } else |
2305 | #endif | |
2bcbd984 | 2306 | MPV_decode_mb_internal(s, block, 0); |
77ea0d4b MN |
2307 | } |
2308 | ||
1d0feb5d AK |
2309 | void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h) |
2310 | { | |
75af13a0 VG |
2311 | ff_draw_horiz_band(s->avctx, &s->current_picture.f, |
2312 | &s->last_picture.f, y, h, s->picture_structure, | |
54b2ce74 | 2313 | s->first_field, s->low_delay); |
1d0feb5d AK |
2314 | } |
2315 | ||
3ada94ba | 2316 | void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename |
657ccb5a DB |
2317 | const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics |
2318 | const int uvlinesize = s->current_picture.f.linesize[1]; | |
2bcbd984 | 2319 | const int mb_size= 4; |
77ea0d4b | 2320 | |
3ada94ba BF |
2321 | s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2; |
2322 | s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2; | |
2323 | s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2; | |
2324 | s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2; | |
2325 | s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1; | |
2326 | s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1; | |
2327 | //block_index is not used by mpeg2, so it is not affected by chroma_format | |
115329f1 | 2328 | |
657ccb5a DB |
2329 | s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size); |
2330 | s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift)); | |
2331 | s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift)); | |
115329f1 | 2332 | |
975a1447 | 2333 | if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME)) |
3ada94ba | 2334 | { |
078cdecf | 2335 | if(s->picture_structure==PICT_FRAME){ |
3ada94ba BF |
2336 | s->dest[0] += s->mb_y * linesize << mb_size; |
2337 | s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift); | |
2338 | s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift); | |
078cdecf MN |
2339 | }else{ |
2340 | s->dest[0] += (s->mb_y>>1) * linesize << mb_size; | |
2341 | s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift); | |
2342 | s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift); | |
2343 | assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD)); | |
2344 | } | |
77ea0d4b | 2345 | } |
77ea0d4b | 2346 | } |
115329f1 | 2347 | |
0b016eb9 DB |
2348 | /** |
2349 | * Permute an 8x8 block. | |
2350 | * @param block the block which will be permuted according to the given permutation vector | |
2351 | * @param permutation the permutation vector | |
2352 | * @param last the last non zero coefficient in scantable order, used to speed the permutation up | |
2353 | * @param scantable the used scantable, this is only used to speed the permutation up, the block is not | |
2354 | * (inverse) permutated to scantable order! | |
2355 | */ | |
2356 | void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last) | |
2357 | { | |
2358 | int i; | |
2359 | int16_t temp[64]; | |
2360 | ||
2361 | if(last<=0) return; | |
2362 | //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations | |
2363 | ||
2364 | for(i=0; i<=last; i++){ | |
2365 | const int j= scantable[i]; | |
2366 | temp[j]= block[j]; | |
2367 | block[j]=0; | |
2368 | } | |
2369 | ||
2370 | for(i=0; i<=last; i++){ | |
2371 | const int j= scantable[i]; | |
2372 | const int perm_j= permutation[j]; | |
2373 | block[perm_j]= temp[j]; | |
2374 | } | |
2375 | } | |
2376 | ||
3ada94ba BF |
2377 | void ff_mpeg_flush(AVCodecContext *avctx){ |
2378 | int i; | |
2379 | MpegEncContext *s = avctx->priv_data; | |
77ea0d4b | 2380 | |
3ada94ba BF |
2381 | if(s==NULL || s->picture==NULL) |
2382 | return; | |
77ea0d4b | 2383 | |
759001c5 AK |
2384 | for (i = 0; i < MAX_PICTURE_COUNT; i++) |
2385 | ff_mpeg_unref_picture(s, &s->picture[i]); | |
3ada94ba | 2386 | s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL; |
115329f1 | 2387 | |
feec9349 HL |
2388 | ff_mpeg_unref_picture(s, &s->current_picture); |
2389 | ff_mpeg_unref_picture(s, &s->last_picture); | |
2390 | ff_mpeg_unref_picture(s, &s->next_picture); | |
2391 | ||
3ada94ba | 2392 | s->mb_x= s->mb_y= 0; |
7801d21d | 2393 | |
3ada94ba BF |
2394 | s->parse_context.state= -1; |
2395 | s->parse_context.frame_start_found= 0; | |
2396 | s->parse_context.overread= 0; | |
2397 | s->parse_context.overread_index= 0; | |
2398 | s->parse_context.index= 0; | |
2399 | s->parse_context.last_index= 0; | |
2400 | s->bitstream_buffer_size=0; | |
2401 | s->pp_time=0; | |
de6d9b64 FB |
2402 | } |
2403 | ||
b776e3d1 AJ |
2404 | /** |
2405 | * set qscale and update qscale dependent variables. | |
2406 | */ | |
2407 | void ff_set_qscale(MpegEncContext * s, int qscale) | |
2408 | { | |
2409 | if (qscale < 1) | |
2410 | qscale = 1; | |
2411 | else if (qscale > 31) | |
2412 | qscale = 31; | |
2413 | ||
2414 | s->qscale = qscale; | |
2415 | s->chroma_qscale= s->chroma_qscale_table[qscale]; | |
2416 | ||
2417 | s->y_dc_scale= s->y_dc_scale_table[ qscale ]; | |
2418 | s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ]; | |
2419 | } | |
6a9c8594 | 2420 | |
efd29844 | 2421 | void ff_MPV_report_decode_progress(MpegEncContext *s) |
6a9c8594 | 2422 | { |
54974c62 | 2423 | if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred) |
759001c5 | 2424 | ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0); |
6a9c8594 | 2425 | } |
54974c62 | 2426 | |
d88738e4 | 2427 | #if CONFIG_ERROR_RESILIENCE |
d66e305b VG |
2428 | void ff_mpeg_set_erpic(ERPicture *dst, Picture *src) |
2429 | { | |
2430 | int i; | |
2431 | ||
2432 | if (!src) | |
2433 | return; | |
2434 | ||
2435 | dst->f = &src->f; | |
2436 | dst->tf = &src->tf; | |
2437 | ||
2438 | for (i = 0; i < 2; i++) { | |
2439 | dst->motion_val[i] = src->motion_val[i]; | |
2440 | dst->ref_index[i] = src->ref_index[i]; | |
2441 | } | |
2442 | ||
2443 | dst->mb_type = src->mb_type; | |
2444 | dst->field_picture = src->field_picture; | |
2445 | } | |
2446 | ||
54974c62 AK |
2447 | void ff_mpeg_er_frame_start(MpegEncContext *s) |
2448 | { | |
2449 | ERContext *er = &s->er; | |
2450 | ||
d66e305b VG |
2451 | ff_mpeg_set_erpic(&er->cur_pic, s->current_picture_ptr); |
2452 | ff_mpeg_set_erpic(&er->next_pic, s->next_picture_ptr); | |
2453 | ff_mpeg_set_erpic(&er->last_pic, s->last_picture_ptr); | |
54974c62 AK |
2454 | |
2455 | er->pp_time = s->pp_time; | |
2456 | er->pb_time = s->pb_time; | |
2457 | er->quarter_sample = s->quarter_sample; | |
2458 | er->partitioned_frame = s->partitioned_frame; | |
2459 | ||
2460 | ff_er_frame_start(er); | |
2461 | } | |
d88738e4 | 2462 | #endif /* CONFIG_ERROR_RESILIENCE */ |