/src/ffmpeg/libavcodec/mpeg4videodec.c
Line | Count | Source |
1 | | /* |
2 | | * MPEG-4 decoder |
3 | | * Copyright (c) 2000,2001 Fabrice Bellard |
4 | | * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at> |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * FFmpeg is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | #define UNCHECKED_BITSTREAM_READER 1 |
24 | | |
25 | | #include "config_components.h" |
26 | | |
27 | | #include "libavutil/avassert.h" |
28 | | #include "libavutil/internal.h" |
29 | | #include "libavutil/opt.h" |
30 | | #include "libavutil/thread.h" |
31 | | #include "codec_internal.h" |
32 | | #include "error_resilience.h" |
33 | | #include "hwconfig.h" |
34 | | #include "idctdsp.h" |
35 | | #include "mpegutils.h" |
36 | | #include "mpegvideo.h" |
37 | | #include "mpegvideodata.h" |
38 | | #include "mpegvideodec.h" |
39 | | #include "mpegvideo_unquantize.h" |
40 | | #include "mpeg4video.h" |
41 | | #include "mpeg4videodata.h" |
42 | | #include "mpeg4videodec.h" |
43 | | #include "mpeg4videodefs.h" |
44 | | #include "h263.h" |
45 | | #include "h263data.h" |
46 | | #include "h263dec.h" |
47 | | #include "internal.h" |
48 | | #include "profiles.h" |
49 | | #include "qpeldsp.h" |
50 | | #include "threadprogress.h" |
51 | | #include "unary.h" |
52 | | |
53 | | #if 0 //3IV1 is quite rare and it slows things down a tiny bit |
54 | | #define IS_3IV1 (s->codec_tag == AV_RL32("3IV1")) |
55 | | #else |
56 | 1.03M | #define IS_3IV1 0 |
57 | | #endif |
58 | | |
59 | | /* The defines below define the number of bits that are read at once for |
60 | | * reading vlc values. Changing these may improve speed and data cache needs |
61 | | * be aware though that decreasing them may need the number of stages that is |
62 | | * passed to get_vlc* to be increased. */ |
63 | 110k | #define SPRITE_TRAJ_VLC_BITS 6 |
64 | 160k | #define DC_VLC_BITS 9 |
65 | 19.0k | #define MB_TYPE_B_VLC_BITS 4 |
66 | 2.24M | #define STUDIO_INTRA_BITS 9 |
67 | | |
68 | | static VLCElem dc_lum[512], dc_chrom[512]; |
69 | | static VLCElem sprite_trajectory[128]; |
70 | | static VLCElem mb_type_b_vlc[16]; |
71 | | static const VLCElem *studio_intra_tab[12]; |
72 | | static VLCElem studio_luma_dc[528]; |
73 | | static VLCElem studio_chroma_dc[528]; |
74 | | |
75 | | static const uint8_t mpeg4_block_count[4] = { 0, 6, 8, 12 }; |
76 | | |
77 | | static const int16_t mb_type_b_map[4] = { |
78 | | MB_TYPE_DIRECT2 | MB_TYPE_BIDIR_MV, |
79 | | MB_TYPE_BIDIR_MV | MB_TYPE_16x16, |
80 | | MB_TYPE_BACKWARD_MV | MB_TYPE_16x16, |
81 | | MB_TYPE_FORWARD_MV | MB_TYPE_16x16, |
82 | | }; |
83 | | |
84 | | static inline Mpeg4DecContext *h263_to_mpeg4(H263DecContext *h) |
85 | 2.66M | { |
86 | 2.66M | av_assert2(h->c.codec_id == AV_CODEC_ID_MPEG4 && h->c.avctx->priv_data == h); |
87 | 2.66M | return (Mpeg4DecContext*)h; |
88 | 2.66M | } |
89 | | |
90 | | static void gmc1_motion(MpegEncContext *s, const Mpeg4DecContext *ctx, |
91 | | uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, |
92 | | uint8_t *const *ref_picture) |
93 | 34.2k | { |
94 | 34.2k | const uint8_t *ptr; |
95 | 34.2k | int src_x, src_y, motion_x, motion_y; |
96 | 34.2k | ptrdiff_t offset, linesize, uvlinesize; |
97 | 34.2k | int emu = 0; |
98 | | |
99 | 34.2k | motion_x = ctx->sprite_offset[0][0]; |
100 | 34.2k | motion_y = ctx->sprite_offset[0][1]; |
101 | 34.2k | src_x = s->mb_x * 16 + (motion_x >> (ctx->sprite_warping_accuracy + 1)); |
102 | 34.2k | src_y = s->mb_y * 16 + (motion_y >> (ctx->sprite_warping_accuracy + 1)); |
103 | 34.2k | motion_x *= 1 << (3 - ctx->sprite_warping_accuracy); |
104 | 34.2k | motion_y *= 1 << (3 - ctx->sprite_warping_accuracy); |
105 | 34.2k | src_x = av_clip(src_x, -16, s->width); |
106 | 34.2k | if (src_x == s->width) |
107 | 972 | motion_x = 0; |
108 | 34.2k | src_y = av_clip(src_y, -16, s->height); |
109 | 34.2k | if (src_y == s->height) |
110 | 1.77k | motion_y = 0; |
111 | | |
112 | 34.2k | linesize = s->linesize; |
113 | 34.2k | uvlinesize = s->uvlinesize; |
114 | | |
115 | 34.2k | ptr = ref_picture[0] + src_y * linesize + src_x; |
116 | | |
117 | 34.2k | if ((unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0) || |
118 | 28.5k | (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)) { |
119 | 16.2k | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr, |
120 | 16.2k | linesize, linesize, |
121 | 16.2k | 17, 17, |
122 | 16.2k | src_x, src_y, |
123 | 16.2k | s->h_edge_pos, s->v_edge_pos); |
124 | 16.2k | ptr = s->sc.edge_emu_buffer; |
125 | 16.2k | } |
126 | | |
127 | 34.2k | if ((motion_x | motion_y) & 7) { |
128 | 0 | ctx->mdsp.gmc1(dest_y, ptr, linesize, 16, |
129 | 0 | motion_x & 15, motion_y & 15, 128 - s->no_rounding); |
130 | 0 | ctx->mdsp.gmc1(dest_y + 8, ptr + 8, linesize, 16, |
131 | 0 | motion_x & 15, motion_y & 15, 128 - s->no_rounding); |
132 | 34.2k | } else { |
133 | 34.2k | int dxy; |
134 | | |
135 | 34.2k | dxy = ((motion_x >> 3) & 1) | ((motion_y >> 2) & 2); |
136 | 34.2k | if (s->no_rounding) { |
137 | 19.0k | s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16); |
138 | 19.0k | } else { |
139 | 15.2k | s->hdsp.put_pixels_tab[0][dxy](dest_y, ptr, linesize, 16); |
140 | 15.2k | } |
141 | 34.2k | } |
142 | | |
143 | 34.2k | if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) |
144 | 0 | return; |
145 | | |
146 | 34.2k | motion_x = ctx->sprite_offset[1][0]; |
147 | 34.2k | motion_y = ctx->sprite_offset[1][1]; |
148 | 34.2k | src_x = s->mb_x * 8 + (motion_x >> (ctx->sprite_warping_accuracy + 1)); |
149 | 34.2k | src_y = s->mb_y * 8 + (motion_y >> (ctx->sprite_warping_accuracy + 1)); |
150 | 34.2k | motion_x *= 1 << (3 - ctx->sprite_warping_accuracy); |
151 | 34.2k | motion_y *= 1 << (3 - ctx->sprite_warping_accuracy); |
152 | 34.2k | src_x = av_clip(src_x, -8, s->width >> 1); |
153 | 34.2k | if (src_x == s->width >> 1) |
154 | 1.31k | motion_x = 0; |
155 | 34.2k | src_y = av_clip(src_y, -8, s->height >> 1); |
156 | 34.2k | if (src_y == s->height >> 1) |
157 | 2.98k | motion_y = 0; |
158 | | |
159 | 34.2k | offset = (src_y * uvlinesize) + src_x; |
160 | 34.2k | ptr = ref_picture[1] + offset; |
161 | 34.2k | if ((unsigned)src_x >= FFMAX((s->h_edge_pos >> 1) - 9, 0) || |
162 | 28.3k | (unsigned)src_y >= FFMAX((s->v_edge_pos >> 1) - 9, 0)) { |
163 | 16.3k | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr, |
164 | 16.3k | uvlinesize, uvlinesize, |
165 | 16.3k | 9, 9, |
166 | 16.3k | src_x, src_y, |
167 | 16.3k | s->h_edge_pos >> 1, s->v_edge_pos >> 1); |
168 | 16.3k | ptr = s->sc.edge_emu_buffer; |
169 | 16.3k | emu = 1; |
170 | 16.3k | } |
171 | 34.2k | ctx->mdsp.gmc1(dest_cb, ptr, uvlinesize, 8, |
172 | 34.2k | motion_x & 15, motion_y & 15, 128 - s->no_rounding); |
173 | | |
174 | 34.2k | ptr = ref_picture[2] + offset; |
175 | 34.2k | if (emu) { |
176 | 16.3k | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr, |
177 | 16.3k | uvlinesize, uvlinesize, |
178 | 16.3k | 9, 9, |
179 | 16.3k | src_x, src_y, |
180 | 16.3k | s->h_edge_pos >> 1, s->v_edge_pos >> 1); |
181 | 16.3k | ptr = s->sc.edge_emu_buffer; |
182 | 16.3k | } |
183 | 34.2k | ctx->mdsp.gmc1(dest_cr, ptr, uvlinesize, 8, |
184 | 34.2k | motion_x & 15, motion_y & 15, 128 - s->no_rounding); |
185 | 34.2k | } |
186 | | |
187 | | static void gmc_motion(MpegEncContext *s, const Mpeg4DecContext *ctx, |
188 | | uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, |
189 | | uint8_t *const *ref_picture) |
190 | 58.1k | { |
191 | 58.1k | const uint8_t *ptr; |
192 | 58.1k | int linesize, uvlinesize; |
193 | 58.1k | const int a = ctx->sprite_warping_accuracy; |
194 | 58.1k | int ox, oy; |
195 | | |
196 | 58.1k | linesize = s->linesize; |
197 | 58.1k | uvlinesize = s->uvlinesize; |
198 | | |
199 | 58.1k | ptr = ref_picture[0]; |
200 | | |
201 | 58.1k | ox = ctx->sprite_offset[0][0] + ctx->sprite_delta[0][0] * s->mb_x * 16 + |
202 | 58.1k | ctx->sprite_delta[0][1] * s->mb_y * 16; |
203 | 58.1k | oy = ctx->sprite_offset[0][1] + ctx->sprite_delta[1][0] * s->mb_x * 16 + |
204 | 58.1k | ctx->sprite_delta[1][1] * s->mb_y * 16; |
205 | | |
206 | 58.1k | ctx->mdsp.gmc(dest_y, ptr, linesize, 16, |
207 | 58.1k | ox, oy, |
208 | 58.1k | ctx->sprite_delta[0][0], ctx->sprite_delta[0][1], |
209 | 58.1k | ctx->sprite_delta[1][0], ctx->sprite_delta[1][1], |
210 | 58.1k | a + 1, (1 << (2 * a + 1)) - s->no_rounding, |
211 | 58.1k | s->h_edge_pos, s->v_edge_pos); |
212 | 58.1k | ctx->mdsp.gmc(dest_y + 8, ptr, linesize, 16, |
213 | 58.1k | ox + ctx->sprite_delta[0][0] * 8, |
214 | 58.1k | oy + ctx->sprite_delta[1][0] * 8, |
215 | 58.1k | ctx->sprite_delta[0][0], ctx->sprite_delta[0][1], |
216 | 58.1k | ctx->sprite_delta[1][0], ctx->sprite_delta[1][1], |
217 | 58.1k | a + 1, (1 << (2 * a + 1)) - s->no_rounding, |
218 | 58.1k | s->h_edge_pos, s->v_edge_pos); |
219 | | |
220 | 58.1k | if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) |
221 | 0 | return; |
222 | | |
223 | 58.1k | ox = ctx->sprite_offset[1][0] + ctx->sprite_delta[0][0] * s->mb_x * 8 + |
224 | 58.1k | ctx->sprite_delta[0][1] * s->mb_y * 8; |
225 | 58.1k | oy = ctx->sprite_offset[1][1] + ctx->sprite_delta[1][0] * s->mb_x * 8 + |
226 | 58.1k | ctx->sprite_delta[1][1] * s->mb_y * 8; |
227 | | |
228 | 58.1k | ptr = ref_picture[1]; |
229 | 58.1k | ctx->mdsp.gmc(dest_cb, ptr, uvlinesize, 8, |
230 | 58.1k | ox, oy, |
231 | 58.1k | ctx->sprite_delta[0][0], ctx->sprite_delta[0][1], |
232 | 58.1k | ctx->sprite_delta[1][0], ctx->sprite_delta[1][1], |
233 | 58.1k | a + 1, (1 << (2 * a + 1)) - s->no_rounding, |
234 | 58.1k | (s->h_edge_pos + 1) >> 1, (s->v_edge_pos + 1) >> 1); |
235 | | |
236 | 58.1k | ptr = ref_picture[2]; |
237 | 58.1k | ctx->mdsp.gmc(dest_cr, ptr, uvlinesize, 8, |
238 | 58.1k | ox, oy, |
239 | 58.1k | ctx->sprite_delta[0][0], ctx->sprite_delta[0][1], |
240 | 58.1k | ctx->sprite_delta[1][0], ctx->sprite_delta[1][1], |
241 | 58.1k | a + 1, (1 << (2 * a + 1)) - s->no_rounding, |
242 | 58.1k | (s->h_edge_pos + 1) >> 1, (s->v_edge_pos + 1) >> 1); |
243 | 58.1k | } |
244 | | |
245 | | void ff_mpeg4_mcsel_motion(MpegEncContext *s, |
246 | | uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, |
247 | | uint8_t *const *ref_picture) |
248 | 92.4k | { |
249 | 92.4k | const Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s; |
250 | | |
251 | 92.4k | if (ctx->real_sprite_warping_points == 1) { |
252 | 34.2k | gmc1_motion(s, ctx, dest_y, dest_cb, dest_cr, |
253 | 34.2k | ref_picture); |
254 | 58.1k | } else { |
255 | 58.1k | gmc_motion(s, ctx, dest_y, dest_cb, dest_cr, |
256 | 58.1k | ref_picture); |
257 | 58.1k | } |
258 | 92.4k | } |
259 | | |
260 | | void ff_mpeg4_decode_studio(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, |
261 | | uint8_t *dest_cr, int block_size, int uvlinesize, |
262 | | int dct_linesize, int dct_offset) |
263 | 15.2k | { |
264 | 15.2k | Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s; |
265 | 15.2k | const int act_block_size = block_size * 2; |
266 | | |
267 | 15.2k | if (ctx->dpcm_direction == 0) { |
268 | 13.0k | s->idsp.idct_put(dest_y, dct_linesize, (int16_t*)ctx->block32[0]); |
269 | 13.0k | s->idsp.idct_put(dest_y + act_block_size, dct_linesize, (int16_t*)ctx->block32[1]); |
270 | 13.0k | s->idsp.idct_put(dest_y + dct_offset, dct_linesize, (int16_t*)ctx->block32[2]); |
271 | 13.0k | s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)ctx->block32[3]); |
272 | | |
273 | 13.0k | dct_linesize = uvlinesize << s->interlaced_dct; |
274 | 13.0k | dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size; |
275 | | |
276 | 13.0k | s->idsp.idct_put(dest_cb, dct_linesize, (int16_t*)ctx->block32[4]); |
277 | 13.0k | s->idsp.idct_put(dest_cr, dct_linesize, (int16_t*)ctx->block32[5]); |
278 | 13.0k | s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)ctx->block32[6]); |
279 | 13.0k | s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)ctx->block32[7]); |
280 | 13.0k | if (!s->chroma_x_shift){ //Chroma444 |
281 | 11.0k | s->idsp.idct_put(dest_cb + act_block_size, dct_linesize, (int16_t*)ctx->block32[8]); |
282 | 11.0k | s->idsp.idct_put(dest_cr + act_block_size, dct_linesize, (int16_t*)ctx->block32[9]); |
283 | 11.0k | s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[10]); |
284 | 11.0k | s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[11]); |
285 | 11.0k | } |
286 | 13.0k | } else if (ctx->dpcm_direction == 1) { |
287 | 893 | uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr}; |
288 | 893 | int linesize[3] = {dct_linesize, uvlinesize, uvlinesize}; |
289 | 3.57k | for (int i = 0; i < 3; i++) { |
290 | 2.67k | const uint16_t *src = ctx->dpcm_macroblock[i]; |
291 | 2.67k | int vsub = i ? s->chroma_y_shift : 0; |
292 | 2.67k | int hsub = i ? s->chroma_x_shift : 0; |
293 | 2.67k | int lowres = s->avctx->lowres; |
294 | 2.67k | int step = 1 << lowres; |
295 | 18.0k | for (int h = 0; h < (16 >> (vsub + lowres)); h++){ |
296 | 144k | for (int w = 0, idx = 0; w < (16 >> (hsub + lowres)); w++, idx += step) |
297 | 129k | dest_pcm[i][w] = src[idx]; |
298 | 15.4k | dest_pcm[i] += linesize[i] / 2; |
299 | 15.4k | src += (16 >> hsub) * step; |
300 | 15.4k | } |
301 | 2.67k | } |
302 | 1.35k | } else { |
303 | 1.35k | uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr}; |
304 | 1.35k | int linesize[3] = {dct_linesize, uvlinesize, uvlinesize}; |
305 | 1.35k | av_assert2(ctx->dpcm_direction == -1); |
306 | 5.42k | for (int i = 0; i < 3; i++) { |
307 | 4.06k | const uint16_t *src = ctx->dpcm_macroblock[i]; |
308 | 4.06k | int vsub = i ? s->chroma_y_shift : 0; |
309 | 4.06k | int hsub = i ? s->chroma_x_shift : 0; |
310 | 4.06k | int lowres = s->avctx->lowres; |
311 | 4.06k | int step = 1 << lowres; |
312 | 4.06k | dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub + lowres) - 1); |
313 | 43.6k | for (int h = (16 >> (vsub + lowres)) - 1; h >= 0; h--){ |
314 | 544k | for (int w = (16 >> (hsub + lowres)) - 1, idx = 0; w >= 0; w--, idx += step) |
315 | 504k | dest_pcm[i][w] = src[idx]; |
316 | 39.5k | src += step * (16 >> hsub); |
317 | 39.5k | dest_pcm[i] -= linesize[i] / 2; |
318 | 39.5k | } |
319 | 4.06k | } |
320 | 1.35k | } |
321 | 15.2k | } |
322 | | |
323 | | /** |
324 | | * Predict the ac. |
325 | | * @param n block index (0-3 are luma, 4-5 are chroma) |
326 | | * @param dir the ac prediction direction |
327 | | */ |
328 | | void ff_mpeg4_pred_ac(H263DecContext *const h, int16_t *block, int n, int dir) |
329 | 26.4M | { |
330 | 26.4M | int i; |
331 | 26.4M | int16_t *ac_val, *ac_val1; |
332 | 26.4M | int8_t *const qscale_table = h->c.cur_pic.qscale_table; |
333 | | |
334 | | /* find prediction */ |
335 | 26.4M | ac_val = &h->c.ac_val[0][0] + h->c.block_index[n] * 16; |
336 | 26.4M | ac_val1 = ac_val; |
337 | 26.4M | if (h->c.ac_pred) { |
338 | 9.95M | if (dir == 0) { |
339 | 6.11M | const int xy = h->c.mb_x - 1 + h->c.mb_y * h->c.mb_stride; |
340 | | /* left prediction */ |
341 | 6.11M | ac_val -= 16; |
342 | | |
343 | 6.11M | if (h->c.mb_x == 0 || h->c.qscale == qscale_table[xy] || |
344 | 6.09M | n == 1 || n == 3) { |
345 | | /* same qscale */ |
346 | 48.7M | for (i = 1; i < 8; i++) |
347 | 42.6M | block[h->c.idsp.idct_permutation[i << 3]] += ac_val[i]; |
348 | 6.09M | } else { |
349 | | /* different qscale, we must rescale */ |
350 | 161k | for (i = 1; i < 8; i++) |
351 | 141k | block[h->c.idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], h->c.qscale); |
352 | 20.1k | } |
353 | 6.11M | } else { |
354 | 3.83M | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride - h->c.mb_stride; |
355 | | /* top prediction */ |
356 | 3.83M | ac_val -= 16 * h->c.block_wrap[n]; |
357 | | |
358 | 3.83M | if (h->c.mb_y == 0 || h->c.qscale == qscale_table[xy] || |
359 | 3.82M | n == 2 || n == 3) { |
360 | | /* same qscale */ |
361 | 30.5M | for (i = 1; i < 8; i++) |
362 | 26.7M | block[h->c.idsp.idct_permutation[i]] += ac_val[i + 8]; |
363 | 3.82M | } else { |
364 | | /* different qscale, we must rescale */ |
365 | 106k | for (i = 1; i < 8; i++) |
366 | 93.1k | block[h->c.idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], h->c.qscale); |
367 | 13.3k | } |
368 | 3.83M | } |
369 | 9.95M | } |
370 | | /* left copy */ |
371 | 211M | for (i = 1; i < 8; i++) |
372 | 184M | ac_val1[i] = block[h->c.idsp.idct_permutation[i << 3]]; |
373 | | |
374 | | /* top copy */ |
375 | 211M | for (i = 1; i < 8; i++) |
376 | 184M | ac_val1[8 + i] = block[h->c.idsp.idct_permutation[i]]; |
377 | 26.4M | } |
378 | | |
379 | | /** |
380 | | * check if the next stuff is a resync marker or the end. |
381 | | * @return 0 if not |
382 | | */ |
383 | | static inline int mpeg4_is_resync(Mpeg4DecContext *ctx) |
384 | 1.09M | { |
385 | 1.09M | H263DecContext *const h = &ctx->h; |
386 | 1.09M | int bits_count = get_bits_count(&h->gb); |
387 | 1.09M | int v = show_bits(&h->gb, 16); |
388 | | |
389 | 1.09M | if (h->c.workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker) |
390 | 31.6k | return 0; |
391 | | |
392 | 1.06M | while (v <= 0xFF) { |
393 | 56.4k | if (h->c.pict_type == AV_PICTURE_TYPE_B || |
394 | 49.7k | (v >> (8 - h->c.pict_type) != 1) || h->partitioned_frame) |
395 | 55.3k | break; |
396 | 1.15k | skip_bits(&h->gb, 8 + h->c.pict_type); |
397 | 1.15k | bits_count += 8 + h->c.pict_type; |
398 | 1.15k | v = show_bits(&h->gb, 16); |
399 | 1.15k | } |
400 | | |
401 | 1.06M | if (bits_count + 8 >= h->gb.size_in_bits) { |
402 | 15.4k | v >>= 8; |
403 | 15.4k | v |= 0x7F >> (7 - (bits_count & 7)); |
404 | | |
405 | 15.4k | if (v == 0x7F) |
406 | 2.13k | return h->c.mb_num; |
407 | 1.04M | } else { |
408 | 1.04M | static const uint16_t mpeg4_resync_prefix[8] = { |
409 | 1.04M | 0x7F00, 0x7E00, 0x7C00, 0x7800, 0x7000, 0x6000, 0x4000, 0x0000 |
410 | 1.04M | }; |
411 | | |
412 | 1.04M | if (v == mpeg4_resync_prefix[bits_count & 7]) { |
413 | 21.4k | int len, mb_num; |
414 | 21.4k | int mb_num_bits = av_log2(h->c.mb_num - 1) + 1; |
415 | 21.4k | GetBitContext gb = h->gb; |
416 | | |
417 | 21.4k | skip_bits(&h->gb, 1); |
418 | 21.4k | align_get_bits(&h->gb); |
419 | | |
420 | 460k | for (len = 0; len < 32; len++) |
421 | 456k | if (get_bits1(&h->gb)) |
422 | 18.3k | break; |
423 | | |
424 | 21.4k | mb_num = get_bits(&h->gb, mb_num_bits); |
425 | 21.4k | if (!mb_num || mb_num > h->c.mb_num || get_bits_count(&h->gb) + 6 > h->gb.size_in_bits) |
426 | 3.28k | mb_num= -1; |
427 | | |
428 | 21.4k | h->gb = gb; |
429 | | |
430 | 21.4k | if (len >= ff_mpeg4_get_video_packet_prefix_length(h->c.pict_type, ctx->f_code, ctx->b_code)) |
431 | 18.6k | return mb_num; |
432 | 21.4k | } |
433 | 1.04M | } |
434 | 1.04M | return 0; |
435 | 1.06M | } |
436 | | |
437 | | static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb) |
438 | 29.4k | { |
439 | 29.4k | MpegEncContext *s = &ctx->h.c; |
440 | 29.4k | int a = 2 << ctx->sprite_warping_accuracy; |
441 | 29.4k | int rho = 3 - ctx->sprite_warping_accuracy; |
442 | 29.4k | int r = 16 / a; |
443 | 29.4k | int alpha = 1; |
444 | 29.4k | int beta = 0; |
445 | 29.4k | int w = s->width; |
446 | 29.4k | int h = s->height; |
447 | 29.4k | int min_ab, i, w2, h2, w3, h3; |
448 | 29.4k | int sprite_ref[4][2]; |
449 | 29.4k | int virtual_ref[2][2]; |
450 | 29.4k | int64_t sprite_offset[2][2]; |
451 | 29.4k | int64_t sprite_delta[2][2]; |
452 | | |
453 | | // only true for rectangle shapes |
454 | 29.4k | const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 }, |
455 | 29.4k | { 0, s->height }, { s->width, s->height } }; |
456 | 29.4k | int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }; |
457 | | |
458 | 29.4k | if (w <= 0 || h <= 0) |
459 | 530 | return AVERROR_INVALIDDATA; |
460 | | |
461 | 84.3k | for (i = 0; i < ctx->num_sprite_warping_points; i++) { |
462 | 55.4k | int length; |
463 | 55.4k | int x = 0, y = 0; |
464 | | |
465 | 55.4k | length = get_vlc2(gb, sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 2); |
466 | 55.4k | if (length > 0) |
467 | 26.8k | x = get_xbits(gb, length); |
468 | | |
469 | 55.4k | if (!(ctx->divx_version == 500 && ctx->divx_build == 413)) |
470 | 55.4k | check_marker(s->avctx, gb, "before sprite_trajectory"); |
471 | | |
472 | 55.4k | length = get_vlc2(gb, sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 2); |
473 | 55.4k | if (length > 0) |
474 | 29.7k | y = get_xbits(gb, length); |
475 | | |
476 | 55.4k | check_marker(s->avctx, gb, "after sprite_trajectory"); |
477 | 55.4k | ctx->sprite_traj[i][0] = d[i][0] = x; |
478 | 55.4k | ctx->sprite_traj[i][1] = d[i][1] = y; |
479 | 55.4k | } |
480 | 89.1k | for (; i < 4; i++) |
481 | 60.2k | ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0; |
482 | | |
483 | 201k | while ((1 << alpha) < w) |
484 | 172k | alpha++; |
485 | 254k | while ((1 << beta) < h) |
486 | 225k | beta++; /* typo in the MPEG-4 std for the definition of w' and h' */ |
487 | 28.9k | w2 = 1 << alpha; |
488 | 28.9k | h2 = 1 << beta; |
489 | | |
490 | | // Note, the 4th point isn't used for GMC |
491 | 28.9k | if (ctx->divx_version == 500 && ctx->divx_build == 413) { |
492 | 0 | sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0]; |
493 | 0 | sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1]; |
494 | 0 | sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0]; |
495 | 0 | sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1]; |
496 | 0 | sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0]; |
497 | 0 | sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1]; |
498 | 28.9k | } else { |
499 | 28.9k | sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]); |
500 | 28.9k | sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]); |
501 | 28.9k | sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]); |
502 | 28.9k | sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]); |
503 | 28.9k | sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]); |
504 | 28.9k | sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]); |
505 | 28.9k | } |
506 | | /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]); |
507 | | * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */ |
508 | | |
509 | | /* This is mostly identical to the MPEG-4 std (and is totally unreadable |
510 | | * because of that...). Perhaps it should be reordered to be more readable. |
511 | | * The idea behind this virtual_ref mess is to be able to use shifts later |
512 | | * per pixel instead of divides so the distance between points is converted |
513 | | * from w&h based to w2&h2 based which are of the 2^x form. */ |
514 | 28.9k | virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) + |
515 | 28.9k | ROUNDED_DIV(((w - w2) * |
516 | 28.9k | (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) + |
517 | 28.9k | w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w); |
518 | 28.9k | virtual_ref[0][1] = 16 * vop_ref[0][1] + |
519 | 28.9k | ROUNDED_DIV(((w - w2) * |
520 | 28.9k | (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) + |
521 | 28.9k | w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w); |
522 | 28.9k | virtual_ref[1][0] = 16 * vop_ref[0][0] + |
523 | 28.9k | ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) + |
524 | 28.9k | h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h); |
525 | 28.9k | virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) + |
526 | 28.9k | ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) + |
527 | 28.9k | h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h); |
528 | | |
529 | 28.9k | switch (ctx->num_sprite_warping_points) { |
530 | 6.94k | case 0: |
531 | 6.94k | sprite_offset[0][0] = |
532 | 6.94k | sprite_offset[0][1] = |
533 | 6.94k | sprite_offset[1][0] = |
534 | 6.94k | sprite_offset[1][1] = 0; |
535 | 6.94k | sprite_delta[0][0] = a; |
536 | 6.94k | sprite_delta[0][1] = |
537 | 6.94k | sprite_delta[1][0] = 0; |
538 | 6.94k | sprite_delta[1][1] = a; |
539 | 6.94k | ctx->sprite_shift[0] = |
540 | 6.94k | ctx->sprite_shift[1] = 0; |
541 | 6.94k | break; |
542 | 2.70k | case 1: // GMC only |
543 | 2.70k | sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0]; |
544 | 2.70k | sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1]; |
545 | 2.70k | sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) - |
546 | 2.70k | a * (vop_ref[0][0] / 2); |
547 | 2.70k | sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) - |
548 | 2.70k | a * (vop_ref[0][1] / 2); |
549 | 2.70k | sprite_delta[0][0] = a; |
550 | 2.70k | sprite_delta[0][1] = |
551 | 2.70k | sprite_delta[1][0] = 0; |
552 | 2.70k | sprite_delta[1][1] = a; |
553 | 2.70k | ctx->sprite_shift[0] = |
554 | 2.70k | ctx->sprite_shift[1] = 0; |
555 | 2.70k | break; |
556 | 5.06k | case 2: |
557 | 5.06k | sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) + |
558 | 5.06k | ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) * |
559 | 5.06k | ((int64_t) -vop_ref[0][0]) + |
560 | 5.06k | ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) * |
561 | 5.06k | ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1)); |
562 | 5.06k | sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) + |
563 | 5.06k | ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) * |
564 | 5.06k | ((int64_t) -vop_ref[0][0]) + |
565 | 5.06k | ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) * |
566 | 5.06k | ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1)); |
567 | 5.06k | sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * |
568 | 5.06k | ((int64_t)-2 * vop_ref[0][0] + 1) + |
569 | 5.06k | ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) * |
570 | 5.06k | ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r * |
571 | 5.06k | (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1))); |
572 | 5.06k | sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * |
573 | 5.06k | ((int64_t)-2 * vop_ref[0][0] + 1) + |
574 | 5.06k | ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * |
575 | 5.06k | ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r * |
576 | 5.06k | (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1))); |
577 | 5.06k | sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]); |
578 | 5.06k | sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]); |
579 | 5.06k | sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]); |
580 | 5.06k | sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]); |
581 | | |
582 | 5.06k | ctx->sprite_shift[0] = alpha + rho; |
583 | 5.06k | ctx->sprite_shift[1] = alpha + rho + 2; |
584 | 5.06k | break; |
585 | 14.2k | case 3: |
586 | 14.2k | min_ab = FFMIN(alpha, beta); |
587 | 14.2k | w3 = w2 >> min_ab; |
588 | 14.2k | h3 = h2 >> min_ab; |
589 | 14.2k | sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) + |
590 | 14.2k | ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) + |
591 | 14.2k | ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) + |
592 | 14.2k | ((int64_t)1 << (alpha + beta + rho - min_ab - 1)); |
593 | 14.2k | sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) + |
594 | 14.2k | ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) + |
595 | 14.2k | ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) + |
596 | 14.2k | ((int64_t)1 << (alpha + beta + rho - min_ab - 1)); |
597 | 14.2k | sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) + |
598 | 14.2k | ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) + |
599 | 14.2k | (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 + |
600 | 14.2k | ((int64_t)1 << (alpha + beta + rho - min_ab + 1)); |
601 | 14.2k | sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) + |
602 | 14.2k | ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) + |
603 | 14.2k | (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 + |
604 | 14.2k | ((int64_t)1 << (alpha + beta + rho - min_ab + 1)); |
605 | 14.2k | sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3; |
606 | 14.2k | sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3; |
607 | 14.2k | sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3; |
608 | 14.2k | sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3; |
609 | | |
610 | 14.2k | ctx->sprite_shift[0] = alpha + beta + rho - min_ab; |
611 | 14.2k | ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2; |
612 | 14.2k | break; |
613 | 0 | default: |
614 | 0 | av_unreachable("num_sprite_warping_points outside of 0..3 results in an error" |
615 | 28.9k | "in which num_sprite_warping_points is reset to zero"); |
616 | 28.9k | } |
617 | | /* try to simplify the situation */ |
618 | 28.9k | if (sprite_delta[0][0] == a << ctx->sprite_shift[0] && |
619 | 20.4k | sprite_delta[0][1] == 0 && |
620 | 15.6k | sprite_delta[1][0] == 0 && |
621 | 12.3k | sprite_delta[1][1] == a << ctx->sprite_shift[0]) { |
622 | 11.7k | sprite_offset[0][0] >>= ctx->sprite_shift[0]; |
623 | 11.7k | sprite_offset[0][1] >>= ctx->sprite_shift[0]; |
624 | 11.7k | sprite_offset[1][0] >>= ctx->sprite_shift[1]; |
625 | 11.7k | sprite_offset[1][1] >>= ctx->sprite_shift[1]; |
626 | 11.7k | sprite_delta[0][0] = a; |
627 | 11.7k | sprite_delta[0][1] = 0; |
628 | 11.7k | sprite_delta[1][0] = 0; |
629 | 11.7k | sprite_delta[1][1] = a; |
630 | 11.7k | ctx->sprite_shift[0] = 0; |
631 | 11.7k | ctx->sprite_shift[1] = 0; |
632 | 11.7k | ctx->real_sprite_warping_points = 1; |
633 | 17.1k | } else { |
634 | 17.1k | int shift_y = 16 - ctx->sprite_shift[0]; |
635 | 17.1k | int shift_c = 16 - ctx->sprite_shift[1]; |
636 | | |
637 | 46.6k | for (i = 0; i < 2; i++) { |
638 | 32.4k | if (shift_c < 0 || shift_y < 0 || |
639 | 31.0k | FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y || |
640 | 30.6k | FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c || |
641 | 30.4k | FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y || |
642 | 29.7k | FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y |
643 | 32.4k | ) { |
644 | 2.94k | avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset"); |
645 | 2.94k | goto overflow; |
646 | 2.94k | } |
647 | 32.4k | } |
648 | | |
649 | 42.6k | for (i = 0; i < 2; i++) { |
650 | 28.4k | sprite_offset[0][i] *= 1 << shift_y; |
651 | 28.4k | sprite_offset[1][i] *= 1 << shift_c; |
652 | 28.4k | sprite_delta[0][i] *= 1 << shift_y; |
653 | 28.4k | sprite_delta[1][i] *= 1 << shift_y; |
654 | 28.4k | ctx->sprite_shift[i] = 16; |
655 | | |
656 | 28.4k | } |
657 | 33.4k | for (i = 0; i < 2; i++) { |
658 | 24.7k | int64_t sd[2] = { |
659 | 24.7k | sprite_delta[i][0] - a * (1LL<<16), |
660 | 24.7k | sprite_delta[i][1] - a * (1LL<<16) |
661 | 24.7k | }; |
662 | | |
663 | 24.7k | if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX || |
664 | 22.9k | llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX || |
665 | 22.0k | llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX || |
666 | 21.8k | llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX || |
667 | 21.6k | llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX || |
668 | 21.4k | llabs(sd[0]) >= INT_MAX || |
669 | 21.4k | llabs(sd[1]) >= INT_MAX || |
670 | 21.4k | llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX || |
671 | 20.9k | llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX || |
672 | 19.5k | llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX |
673 | 24.7k | ) { |
674 | 5.49k | avpriv_request_sample(s->avctx, "Overflow on sprite points"); |
675 | 5.49k | goto overflow; |
676 | 5.49k | } |
677 | 24.7k | } |
678 | 8.70k | ctx->real_sprite_warping_points = ctx->num_sprite_warping_points; |
679 | 8.70k | } |
680 | | |
681 | 102k | for (i = 0; i < 4; i++) { |
682 | 81.8k | ctx->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1]; |
683 | 81.8k | ctx->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1]; |
684 | 81.8k | } |
685 | | |
686 | 20.4k | return 0; |
687 | 8.44k | overflow: |
688 | 8.44k | memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset)); |
689 | 8.44k | memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta)); |
690 | 8.44k | return AVERROR_PATCHWELCOME; |
691 | 28.9k | } |
692 | | |
693 | 49.7k | static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) { |
694 | 49.7k | int len = FFMIN(ctx->time_increment_bits + 3, 15); |
695 | | |
696 | 49.7k | get_bits(gb, len); |
697 | 49.7k | if (get_bits1(gb)) |
698 | 19.2k | get_bits(gb, len); |
699 | 49.7k | check_marker(ctx->h.c.avctx, gb, "after new_pred"); |
700 | | |
701 | 49.7k | return 0; |
702 | 49.7k | } |
703 | | |
704 | | /** |
705 | | * Decode the next video packet. |
706 | | * @return <0 if something went wrong |
707 | | */ |
708 | | int ff_mpeg4_decode_video_packet_header(H263DecContext *const h) |
709 | 1.00M | { |
710 | 1.00M | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); |
711 | | |
712 | 1.00M | int mb_num_bits = av_log2(h->c.mb_num - 1) + 1; |
713 | 1.00M | int header_extension = 0, mb_num, len; |
714 | | |
715 | | /* is there enough space left for a video packet + header */ |
716 | 1.00M | if (get_bits_count(&h->gb) > h->gb.size_in_bits - 20) |
717 | 37.7k | return AVERROR_INVALIDDATA; |
718 | | |
719 | 29.3M | for (len = 0; len < 32; len++) |
720 | 28.5M | if (get_bits1(&h->gb)) |
721 | 234k | break; |
722 | | |
723 | 972k | if (len != ff_mpeg4_get_video_packet_prefix_length(h->c.pict_type, ctx->f_code, ctx->b_code)) { |
724 | 882k | av_log(h->c.avctx, AV_LOG_ERROR, "marker does not match f_code\n"); |
725 | 882k | return AVERROR_INVALIDDATA; |
726 | 882k | } |
727 | | |
728 | 89.2k | if (ctx->shape != RECT_SHAPE) { |
729 | 3.33k | header_extension = get_bits1(&h->gb); |
730 | | // FIXME more stuff here |
731 | 3.33k | } |
732 | | |
733 | 89.2k | mb_num = get_bits(&h->gb, mb_num_bits); |
734 | 89.2k | if (mb_num >= h->c.mb_num || !mb_num) { |
735 | 9.47k | av_log(h->c.avctx, AV_LOG_ERROR, |
736 | 9.47k | "illegal mb_num in video packet (%d %d) \n", mb_num, h->c.mb_num); |
737 | 9.47k | return AVERROR_INVALIDDATA; |
738 | 9.47k | } |
739 | | |
740 | 79.8k | h->c.mb_x = mb_num % h->c.mb_width; |
741 | 79.8k | h->c.mb_y = mb_num / h->c.mb_width; |
742 | | |
743 | 79.8k | if (ctx->shape != BIN_ONLY_SHAPE) { |
744 | 77.9k | int qscale = get_bits(&h->gb, ctx->quant_precision); |
745 | 77.9k | if (qscale) |
746 | 64.2k | h->c.chroma_qscale = h->c.qscale = qscale; |
747 | 77.9k | } |
748 | | |
749 | 79.8k | if (ctx->shape == RECT_SHAPE) |
750 | 77.3k | header_extension = get_bits1(&h->gb); |
751 | | |
752 | 79.8k | if (header_extension) { |
753 | 170k | while (get_bits1(&h->gb) != 0) |
754 | 131k | ; |
755 | | |
756 | 38.4k | check_marker(h->c.avctx, &h->gb, "before time_increment in video packed header"); |
757 | 38.4k | skip_bits(&h->gb, ctx->time_increment_bits); /* time_increment */ |
758 | 38.4k | check_marker(h->c.avctx, &h->gb, "before vop_coding_type in video packed header"); |
759 | | |
760 | 38.4k | skip_bits(&h->gb, 2); /* vop coding type */ |
761 | | // FIXME not rect stuff here |
762 | | |
763 | 38.4k | if (ctx->shape != BIN_ONLY_SHAPE) { |
764 | 37.6k | skip_bits(&h->gb, 3); /* intra dc vlc threshold */ |
765 | | // FIXME don't just ignore everything |
766 | 37.6k | if (h->c.pict_type == AV_PICTURE_TYPE_S && |
767 | 23.4k | ctx->vol_sprite_usage == GMC_SPRITE) { |
768 | 10.0k | if (mpeg4_decode_sprite_trajectory(ctx, &h->gb) < 0) |
769 | 5.31k | return AVERROR_INVALIDDATA; |
770 | 4.77k | av_log(h->c.avctx, AV_LOG_ERROR, "untested\n"); |
771 | 4.77k | } |
772 | | |
773 | | // FIXME reduced res stuff here |
774 | | |
775 | 32.3k | if (h->c.pict_type != AV_PICTURE_TYPE_I) { |
776 | 24.0k | int f_code = get_bits(&h->gb, 3); /* fcode_for */ |
777 | 24.0k | if (f_code == 0) |
778 | 6.97k | av_log(h->c.avctx, AV_LOG_ERROR, |
779 | 6.97k | "Error, video packet header damaged (f_code=0)\n"); |
780 | 24.0k | } |
781 | 32.3k | if (h->c.pict_type == AV_PICTURE_TYPE_B) { |
782 | 579 | int b_code = get_bits(&h->gb, 3); |
783 | 579 | if (b_code == 0) |
784 | 260 | av_log(h->c.avctx, AV_LOG_ERROR, |
785 | 260 | "Error, video packet header damaged (b_code=0)\n"); |
786 | 579 | } |
787 | 32.3k | } |
788 | 38.4k | } |
789 | 74.4k | if (ctx->new_pred) |
790 | 7.28k | decode_new_pred(ctx, &h->gb); |
791 | | |
792 | 74.4k | return 0; |
793 | 79.8k | } |
794 | | |
795 | | static void reset_studio_dc_predictors(Mpeg4DecContext *const ctx) |
796 | 59.2k | { |
797 | 59.2k | H263DecContext *const h = &ctx->h; |
798 | | /* Reset DC Predictors */ |
799 | 59.2k | h->last_dc[0] = |
800 | 59.2k | h->last_dc[1] = |
801 | 59.2k | h->last_dc[2] = 1 << (h->c.avctx->bits_per_raw_sample + ctx->dct_precision + h->c.intra_dc_precision - 1); |
802 | 59.2k | } |
803 | | |
804 | | /** |
805 | | * Decode the next video packet. |
806 | | * @return <0 if something went wrong |
807 | | */ |
808 | | int ff_mpeg4_decode_studio_slice_header(H263DecContext *const h) |
809 | 34.2k | { |
810 | 34.2k | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); |
811 | 34.2k | GetBitContext *gb = &h->gb; |
812 | 34.2k | unsigned vlc_len; |
813 | 34.2k | uint16_t mb_num; |
814 | | |
815 | 34.2k | if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_STARTCODE) { |
816 | 30.2k | vlc_len = av_log2(h->c.mb_width * h->c.mb_height) + 1; |
817 | 30.2k | mb_num = get_bits(gb, vlc_len); |
818 | | |
819 | 30.2k | if (mb_num >= h->c.mb_num) |
820 | 505 | return AVERROR_INVALIDDATA; |
821 | | |
822 | 29.7k | h->c.mb_x = mb_num % h->c.mb_width; |
823 | 29.7k | h->c.mb_y = mb_num / h->c.mb_width; |
824 | | |
825 | 29.7k | if (ctx->shape != BIN_ONLY_SHAPE) |
826 | 29.4k | h->c.qscale = mpeg_get_qscale(&h->gb, h->c.q_scale_type); |
827 | | |
828 | 29.7k | if (get_bits1(gb)) { /* slice_extension_flag */ |
829 | 14.0k | skip_bits1(gb); /* intra_slice */ |
830 | 14.0k | skip_bits1(gb); /* slice_VOP_id_enable */ |
831 | 14.0k | skip_bits(gb, 6); /* slice_VOP_id */ |
832 | 24.5k | while (get_bits1(gb)) /* extra_bit_slice */ |
833 | 10.4k | skip_bits(gb, 8); /* extra_information_slice */ |
834 | 14.0k | } |
835 | | |
836 | 29.7k | reset_studio_dc_predictors(ctx); |
837 | 29.7k | } |
838 | 4.02k | else { |
839 | 4.02k | return AVERROR_INVALIDDATA; |
840 | 4.02k | } |
841 | | |
842 | 29.7k | return 0; |
843 | 34.2k | } |
844 | | |
845 | | /** |
846 | | * Get the average motion vector for a GMC MB. |
847 | | * @param n either 0 for the x component or 1 for y |
848 | | * @return the average MV for a GMC MB |
849 | | */ |
850 | | static inline int get_amv(Mpeg4DecContext *ctx, int n) |
851 | 739k | { |
852 | 739k | MPVContext *const s = &ctx->h.c; |
853 | 739k | int x, y, mb_v, sum, dx, dy, shift; |
854 | 739k | int len = 1 << (ctx->f_code + 4); |
855 | 739k | const int a = ctx->sprite_warping_accuracy; |
856 | | |
857 | 739k | if (s->workaround_bugs & FF_BUG_AMV) |
858 | 185k | len >>= s->quarter_sample; |
859 | | |
860 | 739k | if (ctx->real_sprite_warping_points == 1) { |
861 | 220k | if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample) |
862 | 0 | sum = ctx->sprite_offset[0][n] / (1 << (a - s->quarter_sample)); |
863 | 220k | else |
864 | 220k | sum = RSHIFT(ctx->sprite_offset[0][n] * (1 << s->quarter_sample), a); |
865 | 519k | } else { |
866 | 519k | dx = ctx->sprite_delta[n][0]; |
867 | 519k | dy = ctx->sprite_delta[n][1]; |
868 | 519k | shift = ctx->sprite_shift[0]; |
869 | 519k | if (n) |
870 | 259k | dy -= 1 << (shift + a + 1); |
871 | 259k | else |
872 | 259k | dx -= 1 << (shift + a + 1); |
873 | 519k | mb_v = ctx->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U; |
874 | | |
875 | 519k | sum = 0; |
876 | 8.82M | for (y = 0; y < 16; y++) { |
877 | 8.30M | int v; |
878 | | |
879 | 8.30M | v = mb_v + (unsigned)dy * y; |
880 | | // FIXME optimize |
881 | 141M | for (x = 0; x < 16; x++) { |
882 | 132M | sum += v >> shift; |
883 | 132M | v += dx; |
884 | 132M | } |
885 | 8.30M | } |
886 | 519k | sum = RSHIFT(sum, a + 8 - s->quarter_sample); |
887 | 519k | } |
888 | | |
889 | 739k | if (sum < -len) |
890 | 85.5k | sum = -len; |
891 | 654k | else if (sum >= len) |
892 | 103k | sum = len - 1; |
893 | | |
894 | 739k | return sum; |
895 | 739k | } |
896 | | |
897 | | /** |
898 | | * Predict the dc. |
899 | | * @param n block index (0-3 are luma, 4-5 are chroma) |
900 | | * @param dir_ptr pointer to an integer where the prediction direction will be stored |
901 | | */ |
902 | | static inline int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr) |
903 | 415k | { |
904 | 415k | const int16_t *const dc_val = s->dc_val + s->block_index[n]; |
905 | 415k | const int wrap = s->block_wrap[n]; |
906 | 415k | int pred; |
907 | | |
908 | | /* find prediction */ |
909 | | |
910 | | /* B C |
911 | | * A X |
912 | | */ |
913 | 415k | int a = dc_val[-1]; |
914 | 415k | int b = dc_val[-1 - wrap]; |
915 | 415k | int c = dc_val[-wrap]; |
916 | | |
917 | | /* outside slice handling (we can't do that by memset as we need the |
918 | | * dc for error resilience) */ |
919 | 415k | if (s->first_slice_line && n != 3) { |
920 | 216k | if (n != 2) |
921 | 174k | b = c = 1024; |
922 | 216k | if (n != 1 && s->mb_x == s->resync_mb_x) |
923 | 56.8k | b = a = 1024; |
924 | 216k | } |
925 | 415k | if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1) { |
926 | 21.6k | if (n == 0 || n == 4 || n == 5) |
927 | 10.7k | b = 1024; |
928 | 21.6k | } |
929 | | |
930 | 415k | if (abs(a - b) < abs(b - c)) { |
931 | 104k | pred = c; |
932 | 104k | *dir_ptr = 1; /* top */ |
933 | 310k | } else { |
934 | 310k | pred = a; |
935 | 310k | *dir_ptr = 0; /* left */ |
936 | 310k | } |
937 | 415k | return pred; |
938 | 415k | } |
939 | | |
940 | | static inline int mpeg4_get_level_dc(MpegEncContext *s, int n, int pred, int level) |
941 | 370k | { |
942 | 370k | int scale = n < 4 ? s->y_dc_scale : s->c_dc_scale; |
943 | 370k | int ret; |
944 | | |
945 | 370k | if (IS_3IV1) |
946 | 0 | scale = 8; |
947 | | |
948 | | /* we assume pred is positive */ |
949 | 370k | pred = FASTDIV((pred + (scale >> 1)), scale); |
950 | | |
951 | 370k | level += pred; |
952 | 370k | ret = level; |
953 | 370k | level *= scale; |
954 | 370k | if (level & (~2047)) { |
955 | 53.9k | if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) { |
956 | 4.21k | if (level < 0) { |
957 | 1.16k | av_log(s->avctx, AV_LOG_ERROR, |
958 | 1.16k | "dc<0 at %dx%d\n", s->mb_x, s->mb_y); |
959 | 1.16k | return AVERROR_INVALIDDATA; |
960 | 1.16k | } |
961 | 3.05k | if (level > 2048 + scale) { |
962 | 1.96k | av_log(s->avctx, AV_LOG_ERROR, |
963 | 1.96k | "dc overflow at %dx%d\n", s->mb_x, s->mb_y); |
964 | 1.96k | return AVERROR_INVALIDDATA; |
965 | 1.96k | } |
966 | 3.05k | } |
967 | 50.8k | if (level < 0) |
968 | 2.96k | level = 0; |
969 | 47.8k | else if (!(s->workaround_bugs & FF_BUG_DC_CLIP)) |
970 | 11.6k | level = 2047; |
971 | 50.8k | } |
972 | 367k | s->dc_val[s->block_index[n]] = level; |
973 | | |
974 | 367k | return ret; |
975 | 370k | } |
976 | | |
977 | | /** |
978 | | * Decode the dc value. |
979 | | * @param n block index (0-3 are luma, 4-5 are chroma) |
980 | | * @param dir_ptr the prediction direction will be stored here |
981 | | * @return the quantized dc |
982 | | */ |
983 | | static inline int mpeg4_decode_dc(H263DecContext *const h, int n, int *dir_ptr) |
984 | 160k | { |
985 | 160k | int level, code, pred; |
986 | | |
987 | 160k | if (n < 4) |
988 | 115k | code = get_vlc2(&h->gb, dc_lum, DC_VLC_BITS, 1); |
989 | 44.9k | else |
990 | 44.9k | code = get_vlc2(&h->gb, dc_chrom, DC_VLC_BITS, 1); |
991 | | |
992 | 160k | if (code < 0) { |
993 | 5.66k | av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc vlc\n"); |
994 | 5.66k | return AVERROR_INVALIDDATA; |
995 | 5.66k | } |
996 | | |
997 | 154k | if (code == 0) { |
998 | 31.5k | level = 0; |
999 | 122k | } else { |
1000 | 122k | if (IS_3IV1) { |
1001 | 0 | if (code == 1) |
1002 | 0 | level = 2 * get_bits1(&h->gb) - 1; |
1003 | 0 | else { |
1004 | 0 | if (get_bits1(&h->gb)) |
1005 | 0 | level = get_bits(&h->gb, code - 1) + (1 << (code - 1)); |
1006 | 0 | else |
1007 | 0 | level = -get_bits(&h->gb, code - 1) - (1 << (code - 1)); |
1008 | 0 | } |
1009 | 122k | } else { |
1010 | 122k | level = get_xbits(&h->gb, code); |
1011 | 122k | } |
1012 | | |
1013 | 122k | if (code > 8) { |
1014 | 1.52k | if (get_bits1(&h->gb) == 0) { /* marker */ |
1015 | 1.11k | if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) { |
1016 | 264 | av_log(h->c.avctx, AV_LOG_ERROR, "dc marker bit missing\n"); |
1017 | 264 | return AVERROR_INVALIDDATA; |
1018 | 264 | } |
1019 | 1.11k | } |
1020 | 1.52k | } |
1021 | 122k | } |
1022 | | |
1023 | 154k | pred = mpeg4_pred_dc(&h->c, n, dir_ptr); |
1024 | 154k | return mpeg4_get_level_dc(&h->c, n, pred, level); |
1025 | 154k | } |
1026 | | |
1027 | | /** |
1028 | | * Decode first partition. |
1029 | | * @return number of MBs decoded or <0 if an error occurred |
1030 | | */ |
1031 | | static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx) |
1032 | 38.7k | { |
1033 | 38.7k | H263DecContext *const h = &ctx->h; |
1034 | 38.7k | int mb_num = 0; |
1035 | 38.7k | static const int8_t quant_tab[4] = { -1, -2, 1, 2 }; |
1036 | | |
1037 | | /* decode first partition */ |
1038 | 38.7k | h->c.first_slice_line = 1; |
1039 | 162k | for (; h->c.mb_y < h->c.mb_height; h->c.mb_y++) { |
1040 | 157k | ff_init_block_index(&h->c); |
1041 | 801k | for (; h->c.mb_x < h->c.mb_width; h->c.mb_x++) { |
1042 | 678k | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride; |
1043 | 678k | int cbpc; |
1044 | 678k | int dir = 0; |
1045 | | |
1046 | 678k | mb_num++; |
1047 | 678k | ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1); |
1048 | 678k | if (h->c.mb_x == h->c.resync_mb_x && h->c.mb_y == h->c.resync_mb_y + 1) |
1049 | 13.4k | h->c.first_slice_line = 0; |
1050 | | |
1051 | 678k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
1052 | 23.7k | int i; |
1053 | | |
1054 | 24.3k | do { |
1055 | 24.3k | if (show_bits(&h->gb, 19) == DC_MARKER) |
1056 | 808 | return mb_num - 1; |
1057 | | |
1058 | 23.5k | cbpc = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2); |
1059 | 23.5k | if (cbpc < 0) { |
1060 | 2.42k | av_log(h->c.avctx, AV_LOG_ERROR, |
1061 | 2.42k | "mcbpc corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); |
1062 | 2.42k | return AVERROR_INVALIDDATA; |
1063 | 2.42k | } |
1064 | 23.5k | } while (cbpc == 8); |
1065 | | |
1066 | 20.5k | h->c.cbp_table[xy] = cbpc & 3; |
1067 | 20.5k | h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA; |
1068 | 20.5k | h->c.mb_intra = 1; |
1069 | | |
1070 | 20.5k | if (cbpc & 4) |
1071 | 3.07k | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); |
1072 | | |
1073 | 20.5k | h->c.cur_pic.qscale_table[xy] = h->c.qscale; |
1074 | | |
1075 | 20.5k | h->c.mbintra_table[xy] = 1; |
1076 | 116k | for (i = 0; i < 6; i++) { |
1077 | 102k | int dc_pred_dir; |
1078 | 102k | int dc = mpeg4_decode_dc(h, i, &dc_pred_dir); |
1079 | 102k | if (dc < 0) { |
1080 | 6.24k | av_log(h->c.avctx, AV_LOG_ERROR, |
1081 | 6.24k | "DC corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); |
1082 | 6.24k | return dc; |
1083 | 6.24k | } |
1084 | 96.0k | dir <<= 1; |
1085 | 96.0k | if (dc_pred_dir) |
1086 | 22.9k | dir |= 1; |
1087 | 96.0k | } |
1088 | 14.2k | h->c.pred_dir_table[xy] = dir; |
1089 | 654k | } else { /* P/S_TYPE */ |
1090 | 654k | int mx, my, pred_x, pred_y, bits; |
1091 | 654k | int16_t *const mot_val = h->c.cur_pic.motion_val[0][h->c.block_index[0]]; |
1092 | 654k | const int stride = h->c.b8_stride * 2; |
1093 | | |
1094 | 655k | try_again: |
1095 | 655k | bits = show_bits(&h->gb, 17); |
1096 | 655k | if (bits == MOTION_MARKER) |
1097 | 7.03k | return mb_num - 1; |
1098 | | |
1099 | 648k | skip_bits1(&h->gb); |
1100 | 648k | if (bits & 0x10000) { |
1101 | | /* skip mb */ |
1102 | 491k | if (h->c.pict_type == AV_PICTURE_TYPE_S && |
1103 | 351k | ctx->vol_sprite_usage == GMC_SPRITE) { |
1104 | 222k | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | |
1105 | 222k | MB_TYPE_16x16 | |
1106 | 222k | MB_TYPE_GMC | |
1107 | 222k | MB_TYPE_FORWARD_MV; |
1108 | 222k | mx = get_amv(ctx, 0); |
1109 | 222k | my = get_amv(ctx, 1); |
1110 | 268k | } else { |
1111 | 268k | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | |
1112 | 268k | MB_TYPE_16x16 | |
1113 | 268k | MB_TYPE_FORWARD_MV; |
1114 | 268k | mx = my = 0; |
1115 | 268k | } |
1116 | 491k | mot_val[0] = |
1117 | 491k | mot_val[2] = |
1118 | 491k | mot_val[0 + stride] = |
1119 | 491k | mot_val[2 + stride] = mx; |
1120 | 491k | mot_val[1] = |
1121 | 491k | mot_val[3] = |
1122 | 491k | mot_val[1 + stride] = |
1123 | 491k | mot_val[3 + stride] = my; |
1124 | | |
1125 | 491k | ff_h263_clean_intra_table_entries(&h->c, xy); |
1126 | 491k | continue; |
1127 | 491k | } |
1128 | | |
1129 | 157k | cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2); |
1130 | 157k | if (cbpc < 0) { |
1131 | 9.37k | av_log(h->c.avctx, AV_LOG_ERROR, |
1132 | 9.37k | "mcbpc corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); |
1133 | 9.37k | return AVERROR_INVALIDDATA; |
1134 | 9.37k | } |
1135 | 147k | if (cbpc == 20) |
1136 | 1.12k | goto try_again; |
1137 | | |
1138 | 146k | h->c.cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant |
1139 | | |
1140 | 146k | h->c.mb_intra = ((cbpc & 4) != 0); |
1141 | | |
1142 | 146k | if (h->c.mb_intra) { |
1143 | 17.4k | h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA; |
1144 | 17.4k | h->c.mbintra_table[xy] = 1; |
1145 | 17.4k | mot_val[0] = |
1146 | 17.4k | mot_val[2] = |
1147 | 17.4k | mot_val[0 + stride] = |
1148 | 17.4k | mot_val[2 + stride] = 0; |
1149 | 17.4k | mot_val[1] = |
1150 | 17.4k | mot_val[3] = |
1151 | 17.4k | mot_val[1 + stride] = |
1152 | 17.4k | mot_val[3 + stride] = 0; |
1153 | 129k | } else { |
1154 | 129k | ff_h263_clean_intra_table_entries(&h->c, xy); |
1155 | | |
1156 | 129k | if (h->c.pict_type == AV_PICTURE_TYPE_S && |
1157 | 84.3k | ctx->vol_sprite_usage == GMC_SPRITE && |
1158 | 54.1k | (cbpc & 16) == 0) |
1159 | 40.8k | h->c.mcsel = get_bits1(&h->gb); |
1160 | 88.2k | else |
1161 | 88.2k | h->c.mcsel = 0; |
1162 | | |
1163 | 129k | if ((cbpc & 16) == 0) { |
1164 | | /* 16x16 motion prediction */ |
1165 | | |
1166 | 102k | ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y); |
1167 | 102k | if (!h->c.mcsel) { |
1168 | 79.4k | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); |
1169 | 79.4k | if (mx >= 0xffff) |
1170 | 1.40k | return AVERROR_INVALIDDATA; |
1171 | | |
1172 | 78.0k | my = ff_h263_decode_motion(h, pred_y, ctx->f_code); |
1173 | 78.0k | if (my >= 0xffff) |
1174 | 1.14k | return AVERROR_INVALIDDATA; |
1175 | 76.9k | h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | |
1176 | 76.9k | MB_TYPE_FORWARD_MV; |
1177 | 76.9k | } else { |
1178 | 22.7k | mx = get_amv(ctx, 0); |
1179 | 22.7k | my = get_amv(ctx, 1); |
1180 | 22.7k | h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | |
1181 | 22.7k | MB_TYPE_GMC | |
1182 | 22.7k | MB_TYPE_FORWARD_MV; |
1183 | 22.7k | } |
1184 | | |
1185 | 99.6k | mot_val[0] = |
1186 | 99.6k | mot_val[2] = |
1187 | 99.6k | mot_val[0 + stride] = |
1188 | 99.6k | mot_val[2 + stride] = mx; |
1189 | 99.6k | mot_val[1] = |
1190 | 99.6k | mot_val[3] = |
1191 | 99.6k | mot_val[1 + stride] = |
1192 | 99.6k | mot_val[3 + stride] = my; |
1193 | 99.6k | } else { |
1194 | 26.9k | int i; |
1195 | 26.9k | h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | |
1196 | 26.9k | MB_TYPE_FORWARD_MV; |
1197 | 119k | for (i = 0; i < 4; i++) { |
1198 | 97.5k | int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y); |
1199 | 97.5k | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); |
1200 | 97.5k | if (mx >= 0xffff) |
1201 | 2.80k | return AVERROR_INVALIDDATA; |
1202 | | |
1203 | 94.7k | my = ff_h263_decode_motion(h, pred_y, ctx->f_code); |
1204 | 94.7k | if (my >= 0xffff) |
1205 | 2.50k | return AVERROR_INVALIDDATA; |
1206 | 92.2k | mot_val[0] = mx; |
1207 | 92.2k | mot_val[1] = my; |
1208 | 92.2k | } |
1209 | 26.9k | } |
1210 | 129k | } |
1211 | 146k | } |
1212 | 678k | } |
1213 | 123k | h->c.mb_x = 0; |
1214 | 123k | } |
1215 | | |
1216 | 5.03k | return mb_num; |
1217 | 38.7k | } |
1218 | | |
1219 | | /** |
1220 | | * decode second partition. |
1221 | | * @return <0 if an error occurred |
1222 | | */ |
1223 | | static int mpeg4_decode_partition_b(H263DecContext *const h, int mb_count) |
1224 | 7.51k | { |
1225 | 7.51k | int mb_num = 0; |
1226 | 7.51k | static const int8_t quant_tab[4] = { -1, -2, 1, 2 }; |
1227 | | |
1228 | 7.51k | h->c.mb_x = h->c.resync_mb_x; |
1229 | 7.51k | h->c.first_slice_line = 1; |
1230 | 28.2k | for (h->c.mb_y = h->c.resync_mb_y; mb_num < mb_count; h->c.mb_y++) { |
1231 | 28.2k | ff_init_block_index(&h->c); |
1232 | 140k | for (; mb_num < mb_count && h->c.mb_x < h->c.mb_width; h->c.mb_x++) { |
1233 | 113k | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride; |
1234 | | |
1235 | 113k | mb_num++; |
1236 | 113k | ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1); |
1237 | 113k | if (h->c.mb_x == h->c.resync_mb_x && h->c.mb_y == h->c.resync_mb_y + 1) |
1238 | 2.60k | h->c.first_slice_line = 0; |
1239 | | |
1240 | 113k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
1241 | 2.31k | int ac_pred = get_bits1(&h->gb); |
1242 | 2.31k | int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
1243 | 2.31k | if (cbpy < 0) { |
1244 | 239 | av_log(h->c.avctx, AV_LOG_ERROR, |
1245 | 239 | "cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); |
1246 | 239 | return AVERROR_INVALIDDATA; |
1247 | 239 | } |
1248 | | |
1249 | 2.08k | h->c.cbp_table[xy] |= cbpy << 2; |
1250 | 2.08k | h->c.cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED; |
1251 | 111k | } else { /* P || S_TYPE */ |
1252 | 111k | if (IS_INTRA(h->c.cur_pic.mb_type[xy])) { |
1253 | 2.96k | int i; |
1254 | 2.96k | int dir = 0; |
1255 | 2.96k | int ac_pred = get_bits1(&h->gb); |
1256 | 2.96k | int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
1257 | | |
1258 | 2.96k | if (cbpy < 0) { |
1259 | 405 | av_log(h->c.avctx, AV_LOG_ERROR, |
1260 | 405 | "I cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); |
1261 | 405 | return AVERROR_INVALIDDATA; |
1262 | 405 | } |
1263 | | |
1264 | 2.55k | if (h->c.cbp_table[xy] & 8) |
1265 | 634 | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); |
1266 | 2.55k | h->c.cur_pic.qscale_table[xy] = h->c.qscale; |
1267 | | |
1268 | 15.7k | for (i = 0; i < 6; i++) { |
1269 | 13.7k | int dc_pred_dir; |
1270 | 13.7k | int dc = mpeg4_decode_dc(h, i, &dc_pred_dir); |
1271 | 13.7k | if (dc < 0) { |
1272 | 500 | av_log(h->c.avctx, AV_LOG_ERROR, |
1273 | 500 | "DC corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); |
1274 | 500 | return dc; |
1275 | 500 | } |
1276 | 13.2k | dir <<= 1; |
1277 | 13.2k | if (dc_pred_dir) |
1278 | 3.57k | dir |= 1; |
1279 | 13.2k | } |
1280 | 2.05k | h->c.cbp_table[xy] &= 3; // remove dquant |
1281 | 2.05k | h->c.cbp_table[xy] |= cbpy << 2; |
1282 | 2.05k | h->c.cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED; |
1283 | 2.05k | h->c.pred_dir_table[xy] = dir; |
1284 | 108k | } else if (IS_SKIP(h->c.cur_pic.mb_type[xy])) { |
1285 | 90.9k | h->c.cur_pic.qscale_table[xy] = h->c.qscale; |
1286 | 90.9k | h->c.cbp_table[xy] = 0; |
1287 | 90.9k | } else { |
1288 | 17.5k | int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
1289 | | |
1290 | 17.5k | if (cbpy < 0) { |
1291 | 726 | av_log(h->c.avctx, AV_LOG_ERROR, |
1292 | 726 | "P cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); |
1293 | 726 | return AVERROR_INVALIDDATA; |
1294 | 726 | } |
1295 | | |
1296 | 16.8k | if (h->c.cbp_table[xy] & 8) |
1297 | 4.07k | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); |
1298 | 16.8k | h->c.cur_pic.qscale_table[xy] = h->c.qscale; |
1299 | | |
1300 | 16.8k | h->c.cbp_table[xy] &= 3; // remove dquant |
1301 | 16.8k | h->c.cbp_table[xy] |= (cbpy ^ 0xf) << 2; |
1302 | 16.8k | } |
1303 | 111k | } |
1304 | 113k | } |
1305 | 26.3k | if (mb_num >= mb_count) |
1306 | 5.64k | return 0; |
1307 | 20.7k | h->c.mb_x = 0; |
1308 | 20.7k | } |
1309 | 0 | return 0; |
1310 | 7.51k | } |
1311 | | |
1312 | | /** |
1313 | | * Decode the first and second partition. |
1314 | | * @return <0 if error (and sets error type in the error_status_table) |
1315 | | */ |
1316 | | int ff_mpeg4_decode_partitions(H263DecContext *const h) |
1317 | 38.7k | { |
1318 | 38.7k | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); |
1319 | 38.7k | int mb_num; |
1320 | 38.7k | int ret; |
1321 | 38.7k | const int part_a_error = h->c.pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR; |
1322 | 38.7k | const int part_a_end = h->c.pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END; |
1323 | | |
1324 | 38.7k | mb_num = mpeg4_decode_partition_a(ctx); |
1325 | 38.7k | if (mb_num <= 0) { |
1326 | 26.3k | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, |
1327 | 26.3k | h->c.mb_x, h->c.mb_y, part_a_error); |
1328 | 26.3k | return mb_num ? mb_num : AVERROR_INVALIDDATA; |
1329 | 26.3k | } |
1330 | | |
1331 | 12.4k | if (h->c.resync_mb_x + h->c.resync_mb_y * h->c.mb_width + mb_num > h->c.mb_num) { |
1332 | 0 | av_log(h->c.avctx, AV_LOG_ERROR, "slice below monitor ...\n"); |
1333 | 0 | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, |
1334 | 0 | h->c.mb_x, h->c.mb_y, part_a_error); |
1335 | 0 | return AVERROR_INVALIDDATA; |
1336 | 0 | } |
1337 | | |
1338 | 12.4k | h->mb_num_left = mb_num; |
1339 | | |
1340 | 12.4k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
1341 | 2.09k | while (show_bits(&h->gb, 9) == 1) |
1342 | 611 | skip_bits(&h->gb, 9); |
1343 | 1.48k | if (get_bits(&h->gb, 19) != DC_MARKER) { |
1344 | 914 | av_log(h->c.avctx, AV_LOG_ERROR, |
1345 | 914 | "marker missing after first I partition at %d %d\n", |
1346 | 914 | h->c.mb_x, h->c.mb_y); |
1347 | 914 | return AVERROR_INVALIDDATA; |
1348 | 914 | } |
1349 | 10.9k | } else { |
1350 | 11.9k | while (show_bits(&h->gb, 10) == 1) |
1351 | 920 | skip_bits(&h->gb, 10); |
1352 | 10.9k | if (get_bits(&h->gb, 17) != MOTION_MARKER) { |
1353 | 4.04k | av_log(h->c.avctx, AV_LOG_ERROR, |
1354 | 4.04k | "marker missing after first P partition at %d %d\n", |
1355 | 4.04k | h->c.mb_x, h->c.mb_y); |
1356 | 4.04k | return AVERROR_INVALIDDATA; |
1357 | 4.04k | } |
1358 | 10.9k | } |
1359 | 7.51k | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, |
1360 | 7.51k | h->c.mb_x - 1, h->c.mb_y, part_a_end); |
1361 | | |
1362 | 7.51k | ret = mpeg4_decode_partition_b(h, mb_num); |
1363 | 7.51k | if (ret < 0) { |
1364 | 1.87k | if (h->c.pict_type == AV_PICTURE_TYPE_P) |
1365 | 951 | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, |
1366 | 951 | h->c.mb_x, h->c.mb_y, ER_DC_ERROR); |
1367 | 1.87k | return ret; |
1368 | 5.64k | } else { |
1369 | 5.64k | if (h->c.pict_type == AV_PICTURE_TYPE_P) |
1370 | 1.87k | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, |
1371 | 1.87k | h->c.mb_x - 1, h->c.mb_y, ER_DC_END); |
1372 | 5.64k | } |
1373 | | |
1374 | 5.64k | return 0; |
1375 | 7.51k | } |
1376 | | |
1377 | | /** |
1378 | | * Decode a block. |
1379 | | * @return <0 if an error occurred |
1380 | | */ |
1381 | | static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block, |
1382 | | int n, int coded, int intra, |
1383 | | int use_intra_dc_vlc, int rvlc) |
1384 | 1.45M | { |
1385 | 1.45M | H263DecContext *const h = &ctx->h; |
1386 | 1.45M | int level, i, last, run, qmul, qadd, pred; |
1387 | 1.45M | int av_uninit(dc_pred_dir); |
1388 | 1.45M | const RLTable *rl; |
1389 | 1.45M | const RL_VLC_ELEM *rl_vlc; |
1390 | 1.45M | const uint8_t *scan_table; |
1391 | | |
1392 | | // Note intra & rvlc should be optimized away if this is inlined |
1393 | | |
1394 | 1.45M | if (intra) { |
1395 | | // FIXME add short header support |
1396 | 308k | if (use_intra_dc_vlc) { |
1397 | | /* DC coef */ |
1398 | 47.0k | if (h->partitioned_frame) { |
1399 | 2.98k | level = h->c.dc_val[h->c.block_index[n]]; |
1400 | 2.98k | if (n < 4) |
1401 | 2.17k | level = FASTDIV((level + (h->c.y_dc_scale >> 1)), h->c.y_dc_scale); |
1402 | 815 | else |
1403 | 815 | level = FASTDIV((level + (h->c.c_dc_scale >> 1)), h->c.c_dc_scale); |
1404 | 2.98k | dc_pred_dir = (h->c.pred_dir_table[h->c.mb_x + h->c.mb_y * h->c.mb_stride] << n) & 32; |
1405 | 44.0k | } else { |
1406 | 44.0k | level = mpeg4_decode_dc(h, n, &dc_pred_dir); |
1407 | 44.0k | if (level < 0) |
1408 | 3.13k | return level; |
1409 | 44.0k | } |
1410 | 43.9k | block[0] = level; |
1411 | 43.9k | i = 0; |
1412 | 261k | } else { |
1413 | 261k | i = -1; |
1414 | 261k | pred = mpeg4_pred_dc(&h->c, n, &dc_pred_dir); |
1415 | 261k | } |
1416 | 305k | if (!coded) |
1417 | 161k | goto not_coded; |
1418 | | |
1419 | 143k | if (rvlc) { |
1420 | 773 | rl = &ff_rvlc_rl_intra; |
1421 | 773 | rl_vlc = ff_rvlc_rl_intra.rl_vlc[0]; |
1422 | 142k | } else { |
1423 | 142k | rl = &ff_mpeg4_rl_intra; |
1424 | 142k | rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0]; |
1425 | 142k | } |
1426 | 143k | if (h->c.ac_pred) { |
1427 | 111k | if (dc_pred_dir == 0) |
1428 | 84.9k | scan_table = h->permutated_intra_v_scantable; /* left */ |
1429 | 26.1k | else |
1430 | 26.1k | scan_table = h->permutated_intra_h_scantable; /* top */ |
1431 | 111k | } else { |
1432 | 32.0k | scan_table = h->c.intra_scantable.permutated; |
1433 | 32.0k | } |
1434 | 143k | qmul = 1; |
1435 | 143k | qadd = 0; |
1436 | 1.15M | } else { |
1437 | 1.15M | i = -1; |
1438 | 1.15M | if (!coded) { |
1439 | 1.00M | h->c.block_last_index[n] = i; |
1440 | 1.00M | return 0; |
1441 | 1.00M | } |
1442 | 144k | if (rvlc) |
1443 | 4.43k | rl = &ff_rvlc_rl_inter; |
1444 | 139k | else |
1445 | 139k | rl = &ff_h263_rl_inter; |
1446 | | |
1447 | 144k | scan_table = h->c.intra_scantable.permutated; |
1448 | | |
1449 | 144k | if (ctx->mpeg_quant) { |
1450 | 64.8k | qmul = 1; |
1451 | 64.8k | qadd = 0; |
1452 | 64.8k | if (rvlc) |
1453 | 778 | rl_vlc = ff_rvlc_rl_inter.rl_vlc[0]; |
1454 | 64.0k | else |
1455 | 64.0k | rl_vlc = ff_h263_rl_inter.rl_vlc[0]; |
1456 | 79.2k | } else { |
1457 | 79.2k | qmul = h->c.qscale << 1; |
1458 | 79.2k | qadd = (h->c.qscale - 1) | 1; |
1459 | 79.2k | if (rvlc) |
1460 | 3.65k | rl_vlc = ff_rvlc_rl_inter.rl_vlc[h->c.qscale]; |
1461 | 75.5k | else |
1462 | 75.5k | rl_vlc = ff_h263_rl_inter.rl_vlc[h->c.qscale]; |
1463 | 79.2k | } |
1464 | 144k | } |
1465 | 287k | { |
1466 | 287k | OPEN_READER(re, &h->gb); |
1467 | 1.22M | for (;;) { |
1468 | 1.22M | UPDATE_CACHE(re, &h->gb); |
1469 | 1.22M | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 0); |
1470 | 1.22M | if (level == 0) { |
1471 | | /* escape */ |
1472 | 11.7k | if (rvlc) { |
1473 | 1.98k | if (SHOW_UBITS(re, &h->gb, 1) == 0) { |
1474 | 780 | av_log(h->c.avctx, AV_LOG_ERROR, |
1475 | 780 | "1. marker bit missing in rvlc esc\n"); |
1476 | 780 | return AVERROR_INVALIDDATA; |
1477 | 780 | } |
1478 | 1.20k | SKIP_CACHE(re, &h->gb, 1); |
1479 | | |
1480 | 1.20k | last = SHOW_UBITS(re, &h->gb, 1); |
1481 | 1.20k | SKIP_CACHE(re, &h->gb, 1); |
1482 | 1.20k | run = SHOW_UBITS(re, &h->gb, 6); |
1483 | 1.20k | SKIP_COUNTER(re, &h->gb, 1 + 1 + 6); |
1484 | 1.20k | UPDATE_CACHE(re, &h->gb); |
1485 | | |
1486 | 1.20k | if (SHOW_UBITS(re, &h->gb, 1) == 0) { |
1487 | 353 | av_log(h->c.avctx, AV_LOG_ERROR, |
1488 | 353 | "2. marker bit missing in rvlc esc\n"); |
1489 | 353 | return AVERROR_INVALIDDATA; |
1490 | 353 | } |
1491 | 855 | SKIP_CACHE(re, &h->gb, 1); |
1492 | | |
1493 | 855 | level = SHOW_UBITS(re, &h->gb, 11); |
1494 | 855 | SKIP_CACHE(re, &h->gb, 11); |
1495 | | |
1496 | 855 | if (SHOW_UBITS(re, &h->gb, 5) != 0x10) { |
1497 | 413 | av_log(h->c.avctx, AV_LOG_ERROR, "reverse esc missing\n"); |
1498 | 413 | return AVERROR_INVALIDDATA; |
1499 | 413 | } |
1500 | 442 | SKIP_CACHE(re, &h->gb, 5); |
1501 | | |
1502 | 442 | level = level * qmul + qadd; |
1503 | 442 | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
1504 | 442 | SKIP_COUNTER(re, &h->gb, 1 + 11 + 5 + 1); |
1505 | | |
1506 | 442 | i += run + 1; |
1507 | 442 | if (last) |
1508 | 240 | i += 192; |
1509 | 9.77k | } else { |
1510 | 9.77k | int cache; |
1511 | 9.77k | cache = GET_CACHE(re, &h->gb); |
1512 | | |
1513 | 9.77k | if (IS_3IV1) |
1514 | 0 | cache ^= 0xC0000000; |
1515 | | |
1516 | 9.77k | if (cache & 0x80000000) { |
1517 | 8.36k | if (cache & 0x40000000) { |
1518 | | /* third escape */ |
1519 | 7.58k | SKIP_CACHE(re, &h->gb, 2); |
1520 | 7.58k | last = SHOW_UBITS(re, &h->gb, 1); |
1521 | 7.58k | SKIP_CACHE(re, &h->gb, 1); |
1522 | 7.58k | run = SHOW_UBITS(re, &h->gb, 6); |
1523 | 7.58k | SKIP_COUNTER(re, &h->gb, 2 + 1 + 6); |
1524 | 7.58k | UPDATE_CACHE(re, &h->gb); |
1525 | | |
1526 | 7.58k | if (IS_3IV1) { |
1527 | 0 | level = SHOW_SBITS(re, &h->gb, 12); |
1528 | 0 | LAST_SKIP_BITS(re, &h->gb, 12); |
1529 | 7.58k | } else { |
1530 | 7.58k | if (SHOW_UBITS(re, &h->gb, 1) == 0) { |
1531 | 589 | av_log(h->c.avctx, AV_LOG_ERROR, |
1532 | 589 | "1. marker bit missing in 3. esc\n"); |
1533 | 589 | if (!(h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) || get_bits_left(&h->gb) <= 0) |
1534 | 589 | return AVERROR_INVALIDDATA; |
1535 | 589 | } |
1536 | 6.99k | SKIP_CACHE(re, &h->gb, 1); |
1537 | | |
1538 | 6.99k | level = SHOW_SBITS(re, &h->gb, 12); |
1539 | 6.99k | SKIP_CACHE(re, &h->gb, 12); |
1540 | | |
1541 | 6.99k | if (SHOW_UBITS(re, &h->gb, 1) == 0) { |
1542 | 369 | av_log(h->c.avctx, AV_LOG_ERROR, |
1543 | 369 | "2. marker bit missing in 3. esc\n"); |
1544 | 369 | if (!(h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) || get_bits_left(&h->gb) <= 0) |
1545 | 369 | return AVERROR_INVALIDDATA; |
1546 | 369 | } |
1547 | | |
1548 | 6.62k | SKIP_COUNTER(re, &h->gb, 1 + 12 + 1); |
1549 | 6.62k | } |
1550 | | |
1551 | | #if 0 |
1552 | | if (h->c.error_recognition >= FF_ER_COMPLIANT) { |
1553 | | const int abs_level= FFABS(level); |
1554 | | if (abs_level<=MAX_LEVEL && run<=MAX_RUN) { |
1555 | | const int run1= run - rl->max_run[last][abs_level] - 1; |
1556 | | if (abs_level <= rl->max_level[last][run]) { |
1557 | | av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n"); |
1558 | | return AVERROR_INVALIDDATA; |
1559 | | } |
1560 | | if (h->c.error_recognition > FF_ER_COMPLIANT) { |
1561 | | if (abs_level <= rl->max_level[last][run]*2) { |
1562 | | av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n"); |
1563 | | return AVERROR_INVALIDDATA; |
1564 | | } |
1565 | | if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) { |
1566 | | av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n"); |
1567 | | return AVERROR_INVALIDDATA; |
1568 | | } |
1569 | | } |
1570 | | } |
1571 | | } |
1572 | | #endif |
1573 | 6.62k | if (level > 0) |
1574 | 5.00k | level = level * qmul + qadd; |
1575 | 1.62k | else |
1576 | 1.62k | level = level * qmul - qadd; |
1577 | | |
1578 | 6.62k | if ((unsigned)(level + 2048) > 4095) { |
1579 | 4.19k | if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) { |
1580 | 760 | if (level > 2560 || level < -2560) { |
1581 | 443 | av_log(h->c.avctx, AV_LOG_ERROR, |
1582 | 443 | "|level| overflow in 3. esc, qp=%d\n", |
1583 | 443 | h->c.qscale); |
1584 | 443 | return AVERROR_INVALIDDATA; |
1585 | 443 | } |
1586 | 760 | } |
1587 | 3.75k | level = level < 0 ? -2048 : 2047; |
1588 | 3.75k | } |
1589 | | |
1590 | 6.17k | i += run + 1; |
1591 | 6.17k | if (last) |
1592 | 2.83k | i += 192; |
1593 | 6.17k | } else { |
1594 | | /* second escape */ |
1595 | 780 | SKIP_BITS(re, &h->gb, 2); |
1596 | 780 | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
1597 | 780 | i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing |
1598 | 780 | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
1599 | 780 | LAST_SKIP_BITS(re, &h->gb, 1); |
1600 | 780 | } |
1601 | 8.36k | } else { |
1602 | | /* first escape */ |
1603 | 1.41k | SKIP_BITS(re, &h->gb, 1); |
1604 | 1.41k | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); |
1605 | 1.41k | i += run; |
1606 | 1.41k | level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing |
1607 | 1.41k | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
1608 | 1.41k | LAST_SKIP_BITS(re, &h->gb, 1); |
1609 | 1.41k | } |
1610 | 9.77k | } |
1611 | 1.21M | } else { |
1612 | 1.21M | i += run; |
1613 | 1.21M | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); |
1614 | 1.21M | LAST_SKIP_BITS(re, &h->gb, 1); |
1615 | 1.21M | } |
1616 | 1.21M | ff_tlog(h->c.avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62); |
1617 | 1.21M | if (i > 62) { |
1618 | 284k | i -= 192; |
1619 | 284k | if (i & (~63)) { |
1620 | 87.7k | av_log(h->c.avctx, AV_LOG_ERROR, |
1621 | 87.7k | "ac-tex damaged at %d %d\n", h->c.mb_x, h->c.mb_y); |
1622 | 87.7k | return AVERROR_INVALIDDATA; |
1623 | 87.7k | } |
1624 | | |
1625 | 196k | block[scan_table[i]] = level; |
1626 | 196k | break; |
1627 | 284k | } |
1628 | | |
1629 | 935k | block[scan_table[i]] = level; |
1630 | 935k | } |
1631 | 196k | CLOSE_READER(re, &h->gb); |
1632 | 196k | } |
1633 | | |
1634 | 358k | not_coded: |
1635 | 358k | if (intra) { |
1636 | 256k | if (!use_intra_dc_vlc) { |
1637 | 216k | block[0] = mpeg4_get_level_dc(&h->c, n, pred, block[0]); |
1638 | | |
1639 | 216k | i -= i >> 31; // if (i == -1) i = 0; |
1640 | 216k | } |
1641 | | |
1642 | 256k | ff_mpeg4_pred_ac(h, block, n, dc_pred_dir); |
1643 | 256k | if (h->c.ac_pred) |
1644 | 160k | i = 63; // FIXME not optimal |
1645 | 256k | } |
1646 | 358k | h->c.block_last_index[n] = i; |
1647 | 358k | return 0; |
1648 | 196k | } |
1649 | | |
1650 | | /** |
1651 | | * decode partition C of one MB. |
1652 | | * @return <0 if an error occurred |
1653 | | */ |
1654 | | static int mpeg4_decode_partitioned_mb(H263DecContext *const h) |
1655 | 42.3k | { |
1656 | 42.3k | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); |
1657 | 42.3k | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride; |
1658 | | |
1659 | 42.3k | const int mb_type = h->c.cur_pic.mb_type[xy]; |
1660 | 42.3k | int cbp = h->c.cbp_table[xy]; |
1661 | | |
1662 | 42.3k | const int use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold; |
1663 | | |
1664 | 42.3k | if (h->c.cur_pic.qscale_table[xy] != h->c.qscale) |
1665 | 1.38k | ff_set_qscale(&h->c, h->c.cur_pic.qscale_table[xy]); |
1666 | | |
1667 | 42.3k | if (h->c.pict_type == AV_PICTURE_TYPE_P || |
1668 | 41.7k | h->c.pict_type == AV_PICTURE_TYPE_S) { |
1669 | 41.7k | int i; |
1670 | 208k | for (i = 0; i < 4; i++) { |
1671 | 167k | h->c.mv[0][i][0] = h->c.cur_pic.motion_val[0][h->c.block_index[i]][0]; |
1672 | 167k | h->c.mv[0][i][1] = h->c.cur_pic.motion_val[0][h->c.block_index[i]][1]; |
1673 | 167k | } |
1674 | 41.7k | h->c.mb_intra = IS_INTRA(mb_type); |
1675 | | |
1676 | 41.7k | if (IS_SKIP(mb_type)) { |
1677 | | /* skip mb */ |
1678 | 235k | for (i = 0; i < 6; i++) |
1679 | 201k | h->c.block_last_index[i] = -1; |
1680 | 33.6k | h->c.mv_dir = MV_DIR_FORWARD; |
1681 | 33.6k | h->c.mv_type = MV_TYPE_16X16; |
1682 | 33.6k | if (h->c.pict_type == AV_PICTURE_TYPE_S |
1683 | 20.1k | && ctx->vol_sprite_usage == GMC_SPRITE) { |
1684 | 5.59k | h->c.mcsel = 1; |
1685 | 5.59k | h->c.mb_skipped = 0; |
1686 | 5.59k | h->c.cur_pic.mbskip_table[xy] = 0; |
1687 | 28.0k | } else { |
1688 | 28.0k | h->c.mcsel = 0; |
1689 | 28.0k | h->c.mb_skipped = 1; |
1690 | 28.0k | h->c.cur_pic.mbskip_table[xy] = 1; |
1691 | 28.0k | } |
1692 | 33.6k | } else if (h->c.mb_intra) { |
1693 | 524 | h->c.ac_pred = IS_ACPRED(h->c.cur_pic.mb_type[xy]); |
1694 | 7.60k | } else if (!h->c.mb_intra) { |
1695 | | // h->c.mcsel = 0; // FIXME do we need to init that? |
1696 | | |
1697 | 7.60k | h->c.mv_dir = MV_DIR_FORWARD; |
1698 | 7.60k | if (IS_8X8(mb_type)) { |
1699 | 1.28k | h->c.mv_type = MV_TYPE_8X8; |
1700 | 6.32k | } else { |
1701 | 6.32k | h->c.mv_type = MV_TYPE_16X16; |
1702 | 6.32k | } |
1703 | 7.60k | } |
1704 | 41.7k | } else { /* I-Frame */ |
1705 | 513 | h->c.mb_intra = 1; |
1706 | 513 | h->c.ac_pred = IS_ACPRED(h->c.cur_pic.mb_type[xy]); |
1707 | 513 | } |
1708 | | |
1709 | 42.3k | if (!IS_SKIP(mb_type)) { |
1710 | 8.63k | int i; |
1711 | 8.63k | h->c.bdsp.clear_blocks(h->block[0]); |
1712 | | /* decode each block */ |
1713 | 43.0k | for (i = 0; i < 6; i++) { |
1714 | 38.6k | if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, h->c.mb_intra, |
1715 | 38.6k | use_intra_dc_vlc, ctx->rvlc) < 0) { |
1716 | 4.27k | av_log(h->c.avctx, AV_LOG_ERROR, |
1717 | 4.27k | "texture corrupted at %d %d %d\n", |
1718 | 4.27k | h->c.mb_x, h->c.mb_y, h->c.mb_intra); |
1719 | 4.27k | return AVERROR_INVALIDDATA; |
1720 | 4.27k | } |
1721 | 34.4k | cbp += cbp; |
1722 | 34.4k | } |
1723 | 8.63k | } |
1724 | | |
1725 | | /* per-MB end of slice check */ |
1726 | 38.0k | if (--h->mb_num_left <= 0) { |
1727 | 885 | if (mpeg4_is_resync(ctx)) |
1728 | 213 | return SLICE_END; |
1729 | 672 | else |
1730 | 672 | return SLICE_NOEND; |
1731 | 37.1k | } else { |
1732 | 37.1k | if (mpeg4_is_resync(ctx)) { |
1733 | 4.95k | const int delta = h->c.mb_x + 1 == h->c.mb_width ? 2 : 1; |
1734 | 4.95k | if (h->c.cbp_table[xy + delta]) |
1735 | 486 | return SLICE_END; |
1736 | 4.95k | } |
1737 | 36.6k | return SLICE_OK; |
1738 | 37.1k | } |
1739 | 38.0k | } |
1740 | | |
1741 | | static int mpeg4_decode_mb(H263DecContext *const h) |
1742 | 1.18M | { |
1743 | 1.18M | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); |
1744 | 1.18M | int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant; |
1745 | 1.18M | static const int8_t quant_tab[4] = { -1, -2, 1, 2 }; |
1746 | 1.18M | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride; |
1747 | 1.18M | int next; |
1748 | | |
1749 | 1.18M | av_assert2(h->c.h263_pred); |
1750 | | |
1751 | 1.18M | if (h->c.pict_type == AV_PICTURE_TYPE_P || |
1752 | 1.04M | h->c.pict_type == AV_PICTURE_TYPE_S) { |
1753 | 1.04M | do { |
1754 | 1.04M | if (get_bits1(&h->gb)) { |
1755 | | /* skip mb */ |
1756 | 839k | h->c.mb_intra = 0; |
1757 | 5.87M | for (i = 0; i < 6; i++) |
1758 | 5.03M | h->c.block_last_index[i] = -1; |
1759 | 839k | h->c.mv_dir = MV_DIR_FORWARD; |
1760 | 839k | h->c.mv_type = MV_TYPE_16X16; |
1761 | 839k | if (h->c.pict_type == AV_PICTURE_TYPE_S && |
1762 | 502k | ctx->vol_sprite_usage == GMC_SPRITE) { |
1763 | 112k | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | |
1764 | 112k | MB_TYPE_GMC | |
1765 | 112k | MB_TYPE_16x16 | |
1766 | 112k | MB_TYPE_FORWARD_MV; |
1767 | 112k | h->c.mcsel = 1; |
1768 | 112k | h->c.mv[0][0][0] = get_amv(ctx, 0); |
1769 | 112k | h->c.mv[0][0][1] = get_amv(ctx, 1); |
1770 | 112k | h->c.cur_pic.mbskip_table[xy] = 0; |
1771 | 112k | h->c.mb_skipped = 0; |
1772 | 726k | } else { |
1773 | 726k | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | |
1774 | 726k | MB_TYPE_FORWARD_MV; |
1775 | 726k | h->c.mcsel = 0; |
1776 | 726k | h->c.mv[0][0][0] = 0; |
1777 | 726k | h->c.mv[0][0][1] = 0; |
1778 | 726k | h->c.cur_pic.mbskip_table[xy] = 1; |
1779 | 726k | h->c.mb_skipped = 1; |
1780 | 726k | } |
1781 | 839k | goto end; |
1782 | 839k | } |
1783 | 203k | cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2); |
1784 | 203k | if (cbpc < 0) { |
1785 | 12.0k | av_log(h->c.avctx, AV_LOG_ERROR, |
1786 | 12.0k | "mcbpc damaged at %d %d\n", h->c.mb_x, h->c.mb_y); |
1787 | 12.0k | return AVERROR_INVALIDDATA; |
1788 | 12.0k | } |
1789 | 203k | } while (cbpc == 20); |
1790 | | |
1791 | 189k | dquant = cbpc & 8; |
1792 | 189k | h->c.mb_intra = ((cbpc & 4) != 0); |
1793 | 189k | if (h->c.mb_intra) |
1794 | 44.3k | goto intra; |
1795 | 144k | h->c.bdsp.clear_blocks(h->block[0]); |
1796 | | |
1797 | 144k | if (h->c.pict_type == AV_PICTURE_TYPE_S && |
1798 | 115k | ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0) |
1799 | 21.2k | h->c.mcsel = get_bits1(&h->gb); |
1800 | 123k | else |
1801 | 123k | h->c.mcsel = 0; |
1802 | 144k | cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1) ^ 0x0F; |
1803 | 144k | if (cbpy < 0) { |
1804 | 3.64k | av_log(h->c.avctx, AV_LOG_ERROR, |
1805 | 3.64k | "P cbpy damaged at %d %d\n", h->c.mb_x, h->c.mb_y); |
1806 | 3.64k | return AVERROR_INVALIDDATA; |
1807 | 3.64k | } |
1808 | | |
1809 | 141k | cbp = (cbpc & 3) | (cbpy << 2); |
1810 | 141k | if (dquant) |
1811 | 11.6k | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); |
1812 | 141k | if ((!h->c.progressive_sequence) && |
1813 | 95.7k | (cbp || (h->c.workaround_bugs & FF_BUG_XVID_ILACE))) |
1814 | 69.0k | h->c.interlaced_dct = get_bits1(&h->gb); |
1815 | | |
1816 | 141k | h->c.mv_dir = MV_DIR_FORWARD; |
1817 | 141k | if ((cbpc & 16) == 0) { |
1818 | 113k | if (h->c.mcsel) { |
1819 | 11.6k | h->c.cur_pic.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 | |
1820 | 11.6k | MB_TYPE_FORWARD_MV; |
1821 | | /* 16x16 global motion prediction */ |
1822 | 11.6k | h->c.mv_type = MV_TYPE_16X16; |
1823 | 11.6k | mx = get_amv(ctx, 0); |
1824 | 11.6k | my = get_amv(ctx, 1); |
1825 | 11.6k | h->c.mv[0][0][0] = mx; |
1826 | 11.6k | h->c.mv[0][0][1] = my; |
1827 | 102k | } else if ((!h->c.progressive_sequence) && get_bits1(&h->gb)) { |
1828 | 37.2k | h->c.cur_pic.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_FORWARD_MV | |
1829 | 37.2k | MB_TYPE_INTERLACED; |
1830 | | /* 16x8 field motion prediction */ |
1831 | 37.2k | h->c.mv_type = MV_TYPE_FIELD; |
1832 | | |
1833 | 37.2k | h->c.field_select[0][0] = get_bits1(&h->gb); |
1834 | 37.2k | h->c.field_select[0][1] = get_bits1(&h->gb); |
1835 | | |
1836 | 37.2k | ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y); |
1837 | | |
1838 | 107k | for (i = 0; i < 2; i++) { |
1839 | 72.6k | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); |
1840 | 72.6k | if (mx >= 0xffff) |
1841 | 1.81k | return AVERROR_INVALIDDATA; |
1842 | | |
1843 | 70.7k | my = ff_h263_decode_motion(h, pred_y / 2, ctx->f_code); |
1844 | 70.7k | if (my >= 0xffff) |
1845 | 876 | return AVERROR_INVALIDDATA; |
1846 | | |
1847 | 69.9k | h->c.mv[0][i][0] = mx; |
1848 | 69.9k | h->c.mv[0][i][1] = my; |
1849 | 69.9k | } |
1850 | 65.0k | } else { |
1851 | 65.0k | h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; |
1852 | | /* 16x16 motion prediction */ |
1853 | 65.0k | h->c.mv_type = MV_TYPE_16X16; |
1854 | 65.0k | ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y); |
1855 | 65.0k | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); |
1856 | | |
1857 | 65.0k | if (mx >= 0xffff) |
1858 | 782 | return AVERROR_INVALIDDATA; |
1859 | | |
1860 | 64.2k | my = ff_h263_decode_motion(h, pred_y, ctx->f_code); |
1861 | | |
1862 | 64.2k | if (my >= 0xffff) |
1863 | 1.69k | return AVERROR_INVALIDDATA; |
1864 | 62.5k | h->c.mv[0][0][0] = mx; |
1865 | 62.5k | h->c.mv[0][0][1] = my; |
1866 | 62.5k | } |
1867 | 113k | } else { |
1868 | 27.2k | h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV; |
1869 | 27.2k | h->c.mv_type = MV_TYPE_8X8; |
1870 | 127k | for (i = 0; i < 4; i++) { |
1871 | 103k | int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y); |
1872 | 103k | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); |
1873 | 103k | if (mx >= 0xffff) |
1874 | 2.00k | return AVERROR_INVALIDDATA; |
1875 | | |
1876 | 101k | my = ff_h263_decode_motion(h, pred_y, ctx->f_code); |
1877 | 101k | if (my >= 0xffff) |
1878 | 1.47k | return AVERROR_INVALIDDATA; |
1879 | 99.8k | h->c.mv[0][i][0] = mx; |
1880 | 99.8k | h->c.mv[0][i][1] = my; |
1881 | 99.8k | mot_val[0] = mx; |
1882 | 99.8k | mot_val[1] = my; |
1883 | 99.8k | } |
1884 | 27.2k | } |
1885 | 141k | } else if (h->c.pict_type == AV_PICTURE_TYPE_B) { |
1886 | 88.2k | int modb1; // first bit of modb |
1887 | 88.2k | int modb2; // second bit of modb |
1888 | 88.2k | int mb_type; |
1889 | | |
1890 | 88.2k | h->c.mb_intra = 0; // B-frames never contain intra blocks |
1891 | 88.2k | h->c.mcsel = 0; // ... true gmc blocks |
1892 | | |
1893 | 88.2k | if (h->c.mb_x == 0) { |
1894 | 51.7k | for (i = 0; i < 2; i++) { |
1895 | 34.4k | h->c.last_mv[i][0][0] = |
1896 | 34.4k | h->c.last_mv[i][0][1] = |
1897 | 34.4k | h->c.last_mv[i][1][0] = |
1898 | 34.4k | h->c.last_mv[i][1][1] = 0; |
1899 | 34.4k | } |
1900 | | |
1901 | 17.2k | ff_thread_progress_await(&h->c.next_pic.ptr->progress, h->c.mb_y); |
1902 | 17.2k | } |
1903 | | |
1904 | | /* if we skipped it in the future P-frame than skip it now too */ |
1905 | 88.2k | h->c.mb_skipped = h->c.next_pic.mbskip_table[h->c.mb_y * h->c.mb_stride + h->c.mb_x]; // Note, skiptab=0 if last was GMC |
1906 | | |
1907 | 88.2k | if (h->c.mb_skipped) { |
1908 | | /* skip mb */ |
1909 | 76.4k | for (i = 0; i < 6; i++) |
1910 | 65.5k | h->c.block_last_index[i] = -1; |
1911 | | |
1912 | 10.9k | h->c.mv_dir = MV_DIR_FORWARD; |
1913 | 10.9k | h->c.mv_type = MV_TYPE_16X16; |
1914 | 10.9k | h->c.mv[0][0][0] = |
1915 | 10.9k | h->c.mv[0][0][1] = |
1916 | 10.9k | h->c.mv[1][0][0] = |
1917 | 10.9k | h->c.mv[1][0][1] = 0; |
1918 | 10.9k | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | |
1919 | 10.9k | MB_TYPE_16x16 | |
1920 | 10.9k | MB_TYPE_FORWARD_MV; |
1921 | 10.9k | goto end; |
1922 | 10.9k | } |
1923 | | |
1924 | 77.3k | modb1 = get_bits1(&h->gb); |
1925 | 77.3k | if (modb1) { |
1926 | | // like MB_TYPE_B_DIRECT but no vectors coded |
1927 | 58.2k | mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_BIDIR_MV; |
1928 | 58.2k | cbp = 0; |
1929 | 58.2k | } else { |
1930 | 19.0k | modb2 = get_bits1(&h->gb); |
1931 | 19.0k | mb_type = get_vlc2(&h->gb, mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 1); |
1932 | 19.0k | if (mb_type < 0) { |
1933 | 1.55k | av_log(h->c.avctx, AV_LOG_ERROR, "illegal MB_type\n"); |
1934 | 1.55k | return AVERROR_INVALIDDATA; |
1935 | 1.55k | } |
1936 | 17.4k | if (modb2) { |
1937 | 11.6k | cbp = 0; |
1938 | 11.6k | } else { |
1939 | 5.83k | h->c.bdsp.clear_blocks(h->block[0]); |
1940 | 5.83k | cbp = get_bits(&h->gb, 6); |
1941 | 5.83k | } |
1942 | | |
1943 | 17.4k | if ((!IS_DIRECT(mb_type)) && cbp) { |
1944 | 1.47k | if (get_bits1(&h->gb)) |
1945 | 396 | ff_set_qscale(&h->c, h->c.qscale + get_bits1(&h->gb) * 4 - 2); |
1946 | 1.47k | } |
1947 | | |
1948 | 17.4k | if (!h->c.progressive_sequence) { |
1949 | 5.08k | if (cbp) |
1950 | 1.21k | h->c.interlaced_dct = get_bits1(&h->gb); |
1951 | | |
1952 | 5.08k | if (!IS_DIRECT(mb_type) && get_bits1(&h->gb)) { |
1953 | 1.96k | mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; |
1954 | 1.96k | mb_type &= ~MB_TYPE_16x16; |
1955 | | |
1956 | 1.96k | if (HAS_FORWARD_MV(mb_type)) { |
1957 | 1.55k | h->c.field_select[0][0] = get_bits1(&h->gb); |
1958 | 1.55k | h->c.field_select[0][1] = get_bits1(&h->gb); |
1959 | 1.55k | } |
1960 | 1.96k | if (HAS_BACKWARD_MV(mb_type)) { |
1961 | 1.57k | h->c.field_select[1][0] = get_bits1(&h->gb); |
1962 | 1.57k | h->c.field_select[1][1] = get_bits1(&h->gb); |
1963 | 1.57k | } |
1964 | 1.96k | } |
1965 | 5.08k | } |
1966 | | |
1967 | 17.4k | h->c.mv_dir = 0; |
1968 | 17.4k | if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) { |
1969 | 4.39k | h->c.mv_type = MV_TYPE_16X16; |
1970 | | |
1971 | 4.39k | if (HAS_FORWARD_MV(mb_type)) { |
1972 | 3.32k | h->c.mv_dir = MV_DIR_FORWARD; |
1973 | | |
1974 | 3.32k | mx = ff_h263_decode_motion(h, h->c.last_mv[0][0][0], ctx->f_code); |
1975 | 3.32k | my = ff_h263_decode_motion(h, h->c.last_mv[0][0][1], ctx->f_code); |
1976 | 3.32k | h->c.last_mv[0][1][0] = |
1977 | 3.32k | h->c.last_mv[0][0][0] = |
1978 | 3.32k | h->c.mv[0][0][0] = mx; |
1979 | 3.32k | h->c.last_mv[0][1][1] = |
1980 | 3.32k | h->c.last_mv[0][0][1] = |
1981 | 3.32k | h->c.mv[0][0][1] = my; |
1982 | 3.32k | } |
1983 | | |
1984 | 4.39k | if (HAS_BACKWARD_MV(mb_type)) { |
1985 | 3.87k | h->c.mv_dir |= MV_DIR_BACKWARD; |
1986 | | |
1987 | 3.87k | mx = ff_h263_decode_motion(h, h->c.last_mv[1][0][0], ctx->b_code); |
1988 | 3.87k | my = ff_h263_decode_motion(h, h->c.last_mv[1][0][1], ctx->b_code); |
1989 | 3.87k | h->c.last_mv[1][1][0] = |
1990 | 3.87k | h->c.last_mv[1][0][0] = |
1991 | 3.87k | h->c.mv[1][0][0] = mx; |
1992 | 3.87k | h->c.last_mv[1][1][1] = |
1993 | 3.87k | h->c.last_mv[1][0][1] = |
1994 | 3.87k | h->c.mv[1][0][1] = my; |
1995 | 3.87k | } |
1996 | 13.0k | } else if (!IS_DIRECT(mb_type)) { |
1997 | 1.96k | h->c.mv_type = MV_TYPE_FIELD; |
1998 | | |
1999 | 1.96k | if (HAS_FORWARD_MV(mb_type)) { |
2000 | 1.55k | h->c.mv_dir = MV_DIR_FORWARD; |
2001 | | |
2002 | 4.67k | for (i = 0; i < 2; i++) { |
2003 | 3.11k | mx = ff_h263_decode_motion(h, h->c.last_mv[0][i][0], ctx->f_code); |
2004 | 3.11k | my = ff_h263_decode_motion(h, h->c.last_mv[0][i][1] / 2, ctx->f_code); |
2005 | 3.11k | h->c.last_mv[0][i][0] = |
2006 | 3.11k | h->c.mv[0][i][0] = mx; |
2007 | 3.11k | h->c.last_mv[0][i][1] = (h->c.mv[0][i][1] = my) * 2; |
2008 | 3.11k | } |
2009 | 1.55k | } |
2010 | | |
2011 | 1.96k | if (HAS_BACKWARD_MV(mb_type)) { |
2012 | 1.57k | h->c.mv_dir |= MV_DIR_BACKWARD; |
2013 | | |
2014 | 4.72k | for (i = 0; i < 2; i++) { |
2015 | 3.15k | mx = ff_h263_decode_motion(h, h->c.last_mv[1][i][0], ctx->b_code); |
2016 | 3.15k | my = ff_h263_decode_motion(h, h->c.last_mv[1][i][1] / 2, ctx->b_code); |
2017 | 3.15k | h->c.last_mv[1][i][0] = |
2018 | 3.15k | h->c.mv[1][i][0] = mx; |
2019 | 3.15k | h->c.last_mv[1][i][1] = (h->c.mv[1][i][1] = my) * 2; |
2020 | 3.15k | } |
2021 | 1.57k | } |
2022 | 1.96k | } |
2023 | 17.4k | } |
2024 | | |
2025 | 75.7k | if (IS_DIRECT(mb_type)) { |
2026 | 69.3k | if (IS_SKIP(mb_type)) { |
2027 | 58.2k | mx = |
2028 | 58.2k | my = 0; |
2029 | 58.2k | } else { |
2030 | 11.1k | mx = ff_h263_decode_motion(h, 0, 1); |
2031 | 11.1k | my = ff_h263_decode_motion(h, 0, 1); |
2032 | 11.1k | } |
2033 | | |
2034 | 69.3k | h->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; |
2035 | 69.3k | mb_type |= ff_mpeg4_set_direct_mv(&h->c, mx, my); |
2036 | 69.3k | } |
2037 | 75.7k | h->c.cur_pic.mb_type[xy] = mb_type; |
2038 | 75.7k | } else { /* I-Frame */ |
2039 | 51.3k | int use_intra_dc_vlc; |
2040 | | |
2041 | 51.7k | do { |
2042 | 51.7k | cbpc = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2); |
2043 | 51.7k | if (cbpc < 0) { |
2044 | 4.44k | av_log(h->c.avctx, AV_LOG_ERROR, |
2045 | 4.44k | "I cbpc damaged at %d %d\n", h->c.mb_x, h->c.mb_y); |
2046 | 4.44k | return AVERROR_INVALIDDATA; |
2047 | 4.44k | } |
2048 | 51.7k | } while (cbpc == 8); |
2049 | | |
2050 | 46.9k | dquant = cbpc & 4; |
2051 | 46.9k | h->c.mb_intra = 1; |
2052 | | |
2053 | 91.2k | intra: |
2054 | 91.2k | h->c.ac_pred = get_bits1(&h->gb); |
2055 | 91.2k | if (h->c.ac_pred) |
2056 | 67.5k | h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED; |
2057 | 23.7k | else |
2058 | 23.7k | h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA; |
2059 | | |
2060 | 91.2k | cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); |
2061 | 91.2k | if (cbpy < 0) { |
2062 | 3.17k | av_log(h->c.avctx, AV_LOG_ERROR, |
2063 | 3.17k | "I cbpy damaged at %d %d\n", h->c.mb_x, h->c.mb_y); |
2064 | 3.17k | return AVERROR_INVALIDDATA; |
2065 | 3.17k | } |
2066 | 88.1k | cbp = (cbpc & 3) | (cbpy << 2); |
2067 | | |
2068 | 88.1k | use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold; |
2069 | | |
2070 | 88.1k | if (dquant) |
2071 | 49.9k | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); |
2072 | | |
2073 | 88.1k | if (!h->c.progressive_sequence) |
2074 | 24.2k | h->c.interlaced_dct = get_bits1(&h->gb); |
2075 | | |
2076 | 88.1k | h->c.bdsp.clear_blocks(h->block[0]); |
2077 | | /* decode each block */ |
2078 | 340k | for (i = 0; i < 6; i++) { |
2079 | 303k | if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, |
2080 | 303k | 1, use_intra_dc_vlc, 0) < 0) |
2081 | 51.7k | return AVERROR_INVALIDDATA; |
2082 | 252k | cbp += cbp; |
2083 | 252k | } |
2084 | 36.3k | goto end; |
2085 | 88.1k | } |
2086 | | |
2087 | | /* decode each block */ |
2088 | 1.28M | for (i = 0; i < 6; i++) { |
2089 | 1.11M | if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, 0, 0, 0) < 0) |
2090 | 37.8k | return AVERROR_INVALIDDATA; |
2091 | 1.07M | cbp += cbp; |
2092 | 1.07M | } |
2093 | | |
2094 | 1.05M | end: |
2095 | | /* per-MB end of slice check */ |
2096 | 1.05M | next = mpeg4_is_resync(ctx); |
2097 | 1.05M | if (next) { |
2098 | 15.6k | if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 > next && (h->c.avctx->err_recognition & AV_EF_AGGRESSIVE)) { |
2099 | 330 | return AVERROR_INVALIDDATA; |
2100 | 15.2k | } else if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 >= next) |
2101 | 8.88k | return SLICE_END; |
2102 | | |
2103 | 6.40k | if (h->c.pict_type == AV_PICTURE_TYPE_B) { |
2104 | 3.01k | const int delta = h->c.mb_x + 1 == h->c.mb_width ? 2 : 1; |
2105 | 3.01k | ff_thread_progress_await(&h->c.next_pic.ptr->progress, |
2106 | 3.01k | (h->c.mb_x + delta >= h->c.mb_width) |
2107 | 3.01k | ? FFMIN(h->c.mb_y + 1, h->c.mb_height - 1) |
2108 | 3.01k | : h->c.mb_y); |
2109 | 3.01k | if (h->c.next_pic.mbskip_table[xy + delta]) |
2110 | 2.52k | return SLICE_OK; |
2111 | 3.01k | } |
2112 | | |
2113 | 3.88k | return SLICE_END; |
2114 | 6.40k | } |
2115 | | |
2116 | 1.04M | return SLICE_OK; |
2117 | 1.05M | } |
2118 | | |
2119 | | /* As per spec, studio start code search isn't the same as the old type of start code */ |
2120 | | static void next_start_code_studio(GetBitContext *gb) |
2121 | 203k | { |
2122 | 203k | align_get_bits(gb); |
2123 | | |
2124 | 5.70M | while (get_bits_left(gb) >= 24 && show_bits(gb, 24) != 0x1) { |
2125 | 5.49M | get_bits(gb, 8); |
2126 | 5.49M | } |
2127 | 203k | } |
2128 | | |
2129 | | /* additional_code, vlc index */ |
2130 | | static const uint8_t ac_state_tab[22][2] = |
2131 | | { |
2132 | | {0, 0}, |
2133 | | {0, 1}, |
2134 | | {1, 1}, |
2135 | | {2, 1}, |
2136 | | {3, 1}, |
2137 | | {4, 1}, |
2138 | | {5, 1}, |
2139 | | {1, 2}, |
2140 | | {2, 2}, |
2141 | | {3, 2}, |
2142 | | {4, 2}, |
2143 | | {5, 2}, |
2144 | | {6, 2}, |
2145 | | {1, 3}, |
2146 | | {2, 4}, |
2147 | | {3, 5}, |
2148 | | {4, 6}, |
2149 | | {5, 7}, |
2150 | | {6, 8}, |
2151 | | {7, 9}, |
2152 | | {8, 10}, |
2153 | | {0, 11} |
2154 | | }; |
2155 | | |
2156 | | static int mpeg4_decode_studio_block(Mpeg4DecContext *const ctx, int32_t block[64], int n) |
2157 | 246k | { |
2158 | 246k | H263DecContext *const h = &ctx->h; |
2159 | | |
2160 | 246k | int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0, |
2161 | 246k | additional_code_len, sign, mismatch; |
2162 | 246k | const VLCElem *cur_vlc = studio_intra_tab[0]; |
2163 | 246k | const uint8_t *const scantable = h->c.intra_scantable.permutated; |
2164 | 246k | const uint16_t *quant_matrix; |
2165 | 246k | uint32_t flc; |
2166 | 246k | const int min = -1 * (1 << (h->c.avctx->bits_per_raw_sample + 6)); |
2167 | 246k | const int max = ((1 << (h->c.avctx->bits_per_raw_sample + 6)) - 1); |
2168 | 246k | int shift = 3 - ctx->dct_precision; |
2169 | | |
2170 | 246k | mismatch = 1; |
2171 | | |
2172 | 246k | memset(block, 0, 64 * sizeof(int32_t)); |
2173 | | |
2174 | 246k | if (n < 4) { |
2175 | 103k | cc = 0; |
2176 | 103k | dct_dc_size = get_vlc2(&h->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2); |
2177 | 103k | quant_matrix = h->c.intra_matrix; |
2178 | 143k | } else { |
2179 | 143k | cc = (n & 1) + 1; |
2180 | 143k | if (ctx->rgb) |
2181 | 19.7k | dct_dc_size = get_vlc2(&h->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2); |
2182 | 123k | else |
2183 | 123k | dct_dc_size = get_vlc2(&h->gb, studio_chroma_dc, STUDIO_INTRA_BITS, 2); |
2184 | 143k | quant_matrix = h->c.chroma_intra_matrix; |
2185 | 143k | } |
2186 | | |
2187 | 246k | if (dct_dc_size == 0) { |
2188 | 15.4k | dct_diff = 0; |
2189 | 230k | } else { |
2190 | 230k | dct_diff = get_xbits(&h->gb, dct_dc_size); |
2191 | | |
2192 | 230k | if (dct_dc_size > 8) { |
2193 | 21.1k | if(!check_marker(h->c.avctx, &h->gb, "dct_dc_size > 8")) |
2194 | 2.12k | return AVERROR_INVALIDDATA; |
2195 | 21.1k | } |
2196 | | |
2197 | 230k | } |
2198 | | |
2199 | 244k | h->last_dc[cc] += dct_diff; |
2200 | | |
2201 | 244k | if (ctx->mpeg_quant) |
2202 | 80.0k | block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision); |
2203 | 164k | else |
2204 | 164k | block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision) * (8 >> ctx->dct_precision); |
2205 | | /* TODO: support mpeg_quant for AC coefficients */ |
2206 | | |
2207 | 244k | block[0] = av_clip(block[0], min, max); |
2208 | 244k | mismatch ^= block[0]; |
2209 | | |
2210 | | /* AC Coefficients */ |
2211 | 1.99M | while (1) { |
2212 | 1.99M | group = get_vlc2(&h->gb, cur_vlc, STUDIO_INTRA_BITS, 2); |
2213 | | |
2214 | 1.99M | if (group < 0) { |
2215 | 11.7k | av_log(h->c.avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n"); |
2216 | 11.7k | return AVERROR_INVALIDDATA; |
2217 | 11.7k | } |
2218 | | |
2219 | 1.98M | additional_code_len = ac_state_tab[group][0]; |
2220 | 1.98M | cur_vlc = studio_intra_tab[ac_state_tab[group][1]]; |
2221 | | |
2222 | 1.98M | if (group == 0) { |
2223 | | /* End of Block */ |
2224 | 227k | break; |
2225 | 1.75M | } else if (group >= 1 && group <= 6) { |
2226 | | /* Zero run length (Table B.47) */ |
2227 | 66.1k | run = 1 << additional_code_len; |
2228 | 66.1k | if (additional_code_len) |
2229 | 11.0k | run += get_bits(&h->gb, additional_code_len); |
2230 | 66.1k | idx += run; |
2231 | 66.1k | continue; |
2232 | 1.68M | } else if (group >= 7 && group <= 12) { |
2233 | | /* Zero run length and +/-1 level (Table B.48) */ |
2234 | 471k | code = get_bits(&h->gb, additional_code_len); |
2235 | 471k | sign = code & 1; |
2236 | 471k | code >>= 1; |
2237 | 471k | run = (1 << (additional_code_len - 1)) + code; |
2238 | 471k | idx += run; |
2239 | 471k | if (idx > 63) |
2240 | 2.98k | return AVERROR_INVALIDDATA; |
2241 | 468k | j = scantable[idx++]; |
2242 | 468k | block[j] = sign ? 1 : -1; |
2243 | 1.21M | } else if (group >= 13 && group <= 20) { |
2244 | | /* Level value (Table B.49) */ |
2245 | 1.21M | if (idx > 63) |
2246 | 2.22k | return AVERROR_INVALIDDATA; |
2247 | 1.21M | j = scantable[idx++]; |
2248 | 1.21M | block[j] = get_xbits(&h->gb, additional_code_len); |
2249 | 1.21M | } else if (group == 21) { |
2250 | | /* Escape */ |
2251 | 1.97k | if (idx > 63) |
2252 | 258 | return AVERROR_INVALIDDATA; |
2253 | 1.71k | j = scantable[idx++]; |
2254 | 1.71k | additional_code_len = h->c.avctx->bits_per_raw_sample + ctx->dct_precision + 4; |
2255 | 1.71k | flc = get_bits(&h->gb, additional_code_len); |
2256 | 1.71k | if (flc >> (additional_code_len-1)) |
2257 | 467 | block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1); |
2258 | 1.25k | else |
2259 | 1.25k | block[j] = flc; |
2260 | 1.71k | } |
2261 | 1.68M | block[j] = ((block[j] * quant_matrix[j] * h->c.qscale) * (1 << shift)) / 16; |
2262 | 1.68M | block[j] = av_clip(block[j], min, max); |
2263 | 1.68M | mismatch ^= block[j]; |
2264 | 1.68M | } |
2265 | | |
2266 | 227k | block[63] ^= mismatch & 1; |
2267 | | |
2268 | 227k | return 0; |
2269 | 244k | } |
2270 | | |
2271 | | static int mpeg4_decode_dpcm_macroblock(Mpeg4DecContext *const ctx, |
2272 | | int16_t macroblock[256], int n) |
2273 | 17.4k | { |
2274 | 17.4k | H263DecContext *const h = &ctx->h; |
2275 | 17.4k | int j, w, height, idx = 0; |
2276 | 17.4k | int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code, |
2277 | 17.4k | dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output; |
2278 | 17.4k | height = 16 >> (n ? h->c.chroma_y_shift : 0); |
2279 | 17.4k | w = 16 >> (n ? h->c.chroma_x_shift : 0); |
2280 | | |
2281 | 17.4k | block_mean = get_bits(&h->gb, h->c.avctx->bits_per_raw_sample); |
2282 | 17.4k | if (block_mean == 0){ |
2283 | 1.99k | av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden block_mean\n"); |
2284 | 1.99k | return AVERROR_INVALIDDATA; |
2285 | 1.99k | } |
2286 | 15.4k | h->last_dc[n] = block_mean * (1 << (ctx->dct_precision + h->c.intra_dc_precision)); |
2287 | | |
2288 | 15.4k | rice_parameter = get_bits(&h->gb, 4); |
2289 | 15.4k | if (rice_parameter == 0) { |
2290 | 622 | av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n"); |
2291 | 622 | return AVERROR_INVALIDDATA; |
2292 | 622 | } |
2293 | | |
2294 | 14.8k | if (rice_parameter == 15) |
2295 | 9.87k | rice_parameter = 0; |
2296 | | |
2297 | 14.8k | if (rice_parameter > 11) { |
2298 | 1.19k | av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n"); |
2299 | 1.19k | return AVERROR_INVALIDDATA; |
2300 | 1.19k | } |
2301 | | |
2302 | 169k | for (int i = 0; i < height; i++) { |
2303 | 161k | output = 1 << (h->c.avctx->bits_per_raw_sample - 1); |
2304 | 161k | top = 1 << (h->c.avctx->bits_per_raw_sample - 1); |
2305 | | |
2306 | 2.68M | for (j = 0; j < w; j++) { |
2307 | 2.52M | left = output; |
2308 | 2.52M | topleft = top; |
2309 | | |
2310 | 2.52M | rice_prefix_code = get_unary(&h->gb, 1, 12); |
2311 | | |
2312 | | /* Escape */ |
2313 | 2.52M | if (rice_prefix_code == 11) |
2314 | 932 | dpcm_residual = get_bits(&h->gb, h->c.avctx->bits_per_raw_sample); |
2315 | 2.52M | else { |
2316 | 2.52M | if (rice_prefix_code == 12) { |
2317 | 5.31k | av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n"); |
2318 | 5.31k | return AVERROR_INVALIDDATA; |
2319 | 5.31k | } |
2320 | 2.51M | rice_suffix_code = get_bitsz(&h->gb, rice_parameter); |
2321 | 2.51M | dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code; |
2322 | 2.51M | } |
2323 | | |
2324 | | /* Map to a signed residual */ |
2325 | 2.51M | if (dpcm_residual & 1) |
2326 | 375k | dpcm_residual = (-1 * dpcm_residual) >> 1; |
2327 | 2.14M | else |
2328 | 2.14M | dpcm_residual = (dpcm_residual >> 1); |
2329 | | |
2330 | 2.51M | if (i != 0) |
2331 | 2.31M | top = macroblock[idx-w]; |
2332 | | |
2333 | 2.51M | p = left + top - topleft; |
2334 | 2.51M | min_left_top = FFMIN(left, top); |
2335 | 2.51M | if (p < min_left_top) |
2336 | 116k | p = min_left_top; |
2337 | | |
2338 | 2.51M | max_left_top = FFMAX(left, top); |
2339 | 2.51M | if (p > max_left_top) |
2340 | 118k | p = max_left_top; |
2341 | | |
2342 | 2.51M | p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1; |
2343 | 2.51M | if (p2 == p) |
2344 | 1.75M | p2 = block_mean; |
2345 | | |
2346 | 2.51M | if (p2 > p) |
2347 | 1.66M | dpcm_residual *= -1; |
2348 | | |
2349 | 2.51M | macroblock[idx++] = output = (dpcm_residual + p) & ((1 << h->c.avctx->bits_per_raw_sample) - 1); |
2350 | 2.51M | } |
2351 | 161k | } |
2352 | | |
2353 | 8.31k | return 0; |
2354 | 13.6k | } |
2355 | | |
2356 | | static int mpeg4_decode_studio_mb(H263DecContext *const h) |
2357 | 43.7k | { |
2358 | 43.7k | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); |
2359 | 43.7k | int i; |
2360 | | |
2361 | 43.7k | ctx->dpcm_direction = 0; |
2362 | | |
2363 | | /* StudioMacroblock */ |
2364 | | /* Assumes I-VOP */ |
2365 | 43.7k | h->c.mb_intra = 1; |
2366 | 43.7k | if (get_bits1(&h->gb)) { /* compression_mode */ |
2367 | | /* DCT */ |
2368 | | /* macroblock_type, 1 or 2-bit VLC */ |
2369 | 32.3k | if (!get_bits1(&h->gb)) { |
2370 | 21.4k | skip_bits1(&h->gb); |
2371 | 21.4k | h->c.qscale = mpeg_get_qscale(&h->gb, h->c.q_scale_type); |
2372 | 21.4k | } |
2373 | | |
2374 | 259k | for (i = 0; i < mpeg4_block_count[h->c.chroma_format]; i++) { |
2375 | 246k | if (mpeg4_decode_studio_block(ctx, ctx->block32[i], i) < 0) |
2376 | 19.2k | return AVERROR_INVALIDDATA; |
2377 | 246k | } |
2378 | 32.3k | } else { |
2379 | | /* DPCM */ |
2380 | 11.3k | check_marker(h->c.avctx, &h->gb, "DPCM block start"); |
2381 | 11.3k | ctx->dpcm_direction = get_bits1(&h->gb) ? -1 : 1; |
2382 | 19.6k | for (i = 0; i < 3; i++) { |
2383 | 17.4k | if (mpeg4_decode_dpcm_macroblock(ctx, ctx->dpcm_macroblock[i], i) < 0) |
2384 | 9.11k | return AVERROR_INVALIDDATA; |
2385 | 17.4k | } |
2386 | 11.3k | } |
2387 | | |
2388 | 15.2k | if (get_bits_left(&h->gb) >= 24 && show_bits(&h->gb, 23) == 0) { |
2389 | 858 | next_start_code_studio(&h->gb); |
2390 | 858 | return SLICE_END; |
2391 | 858 | } |
2392 | | |
2393 | | //vcon-stp9L1.bits (first frame) |
2394 | 14.4k | if (get_bits_left(&h->gb) == 0) |
2395 | 197 | return SLICE_END; |
2396 | | |
2397 | | //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame) |
2398 | 14.2k | if (get_bits_left(&h->gb) < 8U && show_bits(&h->gb, get_bits_left(&h->gb)) == 0) |
2399 | 223 | return SLICE_END; |
2400 | | |
2401 | 14.0k | return SLICE_OK; |
2402 | 14.2k | } |
2403 | | |
2404 | | static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb) |
2405 | 83.4k | { |
2406 | 83.4k | int hours, minutes, seconds; |
2407 | | |
2408 | 83.4k | if (!show_bits(gb, 23)) { |
2409 | 70.3k | av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n"); |
2410 | 70.3k | return AVERROR_INVALIDDATA; |
2411 | 70.3k | } |
2412 | | |
2413 | 13.1k | hours = get_bits(gb, 5); |
2414 | 13.1k | minutes = get_bits(gb, 6); |
2415 | 13.1k | check_marker(s->avctx, gb, "in gop_header"); |
2416 | 13.1k | seconds = get_bits(gb, 6); |
2417 | | |
2418 | 13.1k | s->time_base = seconds + 60*(minutes + 60*hours); |
2419 | | |
2420 | 13.1k | skip_bits1(gb); |
2421 | 13.1k | skip_bits1(gb); |
2422 | | |
2423 | 13.1k | return 0; |
2424 | 83.4k | } |
2425 | | |
2426 | | static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level) |
2427 | 8.33k | { |
2428 | | |
2429 | 8.33k | *profile = get_bits(gb, 4); |
2430 | 8.33k | *level = get_bits(gb, 4); |
2431 | | |
2432 | | // for Simple profile, level 0 |
2433 | 8.33k | if (*profile == 0 && *level == 8) { |
2434 | 436 | *level = 0; |
2435 | 436 | } |
2436 | | |
2437 | 8.33k | return 0; |
2438 | 8.33k | } |
2439 | | |
2440 | | static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb) |
2441 | 12.0k | { |
2442 | 12.0k | int visual_object_type; |
2443 | 12.0k | int is_visual_object_identifier = get_bits1(gb); |
2444 | | |
2445 | 12.0k | if (is_visual_object_identifier) { |
2446 | 6.23k | skip_bits(gb, 4+3); |
2447 | 6.23k | } |
2448 | 12.0k | visual_object_type = get_bits(gb, 4); |
2449 | | |
2450 | 12.0k | if (visual_object_type == VOT_VIDEO_ID || |
2451 | 9.67k | visual_object_type == VOT_STILL_TEXTURE_ID) { |
2452 | 5.21k | int video_signal_type = get_bits1(gb); |
2453 | 5.21k | if (video_signal_type) { |
2454 | 4.00k | int video_range, color_description; |
2455 | 4.00k | skip_bits(gb, 3); // video_format |
2456 | 4.00k | video_range = get_bits1(gb); |
2457 | 4.00k | color_description = get_bits1(gb); |
2458 | | |
2459 | 4.00k | s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; |
2460 | | |
2461 | 4.00k | if (color_description) { |
2462 | 2.91k | s->avctx->color_primaries = get_bits(gb, 8); |
2463 | 2.91k | s->avctx->color_trc = get_bits(gb, 8); |
2464 | 2.91k | s->avctx->colorspace = get_bits(gb, 8); |
2465 | 2.91k | } |
2466 | 4.00k | } |
2467 | 5.21k | } |
2468 | | |
2469 | 12.0k | return 0; |
2470 | 12.0k | } |
2471 | | |
2472 | | static void mpeg4_load_default_matrices(MpegEncContext *s) |
2473 | 194k | { |
2474 | 194k | int i, v; |
2475 | | |
2476 | | /* load default matrices */ |
2477 | 12.6M | for (i = 0; i < 64; i++) { |
2478 | 12.4M | int j = s->idsp.idct_permutation[i]; |
2479 | 12.4M | v = ff_mpeg4_default_intra_matrix[i]; |
2480 | 12.4M | s->intra_matrix[j] = v; |
2481 | 12.4M | s->chroma_intra_matrix[j] = v; |
2482 | | |
2483 | 12.4M | v = ff_mpeg4_default_non_intra_matrix[i]; |
2484 | 12.4M | s->inter_matrix[j] = v; |
2485 | 12.4M | s->chroma_inter_matrix[j] = v; |
2486 | 12.4M | } |
2487 | 194k | } |
2488 | | |
2489 | | static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb) |
2490 | 14.0k | { |
2491 | 14.0k | int i, j, v; |
2492 | | |
2493 | 14.0k | if (get_bits1(gb)) { |
2494 | 10.5k | if (get_bits_left(gb) < 64*8) |
2495 | 3.96k | return AVERROR_INVALIDDATA; |
2496 | | /* intra_quantiser_matrix */ |
2497 | 427k | for (i = 0; i < 64; i++) { |
2498 | 420k | v = get_bits(gb, 8); |
2499 | 420k | j = s->idsp.idct_permutation[ff_zigzag_direct[i]]; |
2500 | 420k | s->intra_matrix[j] = v; |
2501 | 420k | s->chroma_intra_matrix[j] = v; |
2502 | 420k | } |
2503 | 6.57k | } |
2504 | | |
2505 | 10.0k | if (get_bits1(gb)) { |
2506 | 6.93k | if (get_bits_left(gb) < 64*8) |
2507 | 1.69k | return AVERROR_INVALIDDATA; |
2508 | | /* non_intra_quantiser_matrix */ |
2509 | 340k | for (i = 0; i < 64; i++) { |
2510 | 335k | get_bits(gb, 8); |
2511 | 335k | } |
2512 | 5.24k | } |
2513 | | |
2514 | 8.36k | if (get_bits1(gb)) { |
2515 | 5.26k | if (get_bits_left(gb) < 64*8) |
2516 | 862 | return AVERROR_INVALIDDATA; |
2517 | | /* chroma_intra_quantiser_matrix */ |
2518 | 286k | for (i = 0; i < 64; i++) { |
2519 | 281k | v = get_bits(gb, 8); |
2520 | 281k | j = s->idsp.idct_permutation[ff_zigzag_direct[i]]; |
2521 | 281k | s->chroma_intra_matrix[j] = v; |
2522 | 281k | } |
2523 | 4.40k | } |
2524 | | |
2525 | 7.49k | if (get_bits1(gb)) { |
2526 | 4.19k | if (get_bits_left(gb) < 64*8) |
2527 | 848 | return AVERROR_INVALIDDATA; |
2528 | | /* chroma_non_intra_quantiser_matrix */ |
2529 | 217k | for (i = 0; i < 64; i++) { |
2530 | 213k | get_bits(gb, 8); |
2531 | 213k | } |
2532 | 3.34k | } |
2533 | | |
2534 | 6.65k | next_start_code_studio(gb); |
2535 | 6.65k | return 0; |
2536 | 7.49k | } |
2537 | | |
2538 | | static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id) |
2539 | 196k | { |
2540 | 196k | uint32_t startcode; |
2541 | 196k | uint8_t extension_type; |
2542 | | |
2543 | 196k | startcode = show_bits_long(gb, 32); |
2544 | 196k | if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) { |
2545 | | |
2546 | 16.3k | if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) { |
2547 | 14.9k | skip_bits_long(gb, 32); |
2548 | 14.9k | extension_type = get_bits(gb, 4); |
2549 | 14.9k | if (extension_type == QUANT_MATRIX_EXT_ID) |
2550 | 14.0k | read_quant_matrix_ext(s, gb); |
2551 | 14.9k | } |
2552 | 16.3k | } |
2553 | 196k | } |
2554 | | |
2555 | | static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb) |
2556 | 28.2k | { |
2557 | 28.2k | MPVContext *const s = &ctx->h.c; |
2558 | 28.2k | int width, height, aspect_ratio_info; |
2559 | 28.2k | int bits_per_raw_sample; |
2560 | 28.2k | int rgb, chroma_format; |
2561 | | |
2562 | | // random_accessible_vol and video_object_type_indication have already |
2563 | | // been read by the caller decode_vol_header() |
2564 | 28.2k | skip_bits(gb, 4); /* video_object_layer_verid */ |
2565 | 28.2k | ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */ |
2566 | 28.2k | skip_bits(gb, 4); /* video_object_layer_shape_extension */ |
2567 | 28.2k | skip_bits1(gb); /* progressive_sequence */ |
2568 | 28.2k | if (ctx->shape != RECT_SHAPE) { |
2569 | 1.32k | avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape"); |
2570 | 1.32k | return AVERROR_PATCHWELCOME; |
2571 | 1.32k | } |
2572 | 26.9k | if (ctx->shape != BIN_ONLY_SHAPE) { |
2573 | 26.9k | rgb = get_bits1(gb); /* rgb_components */ |
2574 | 26.9k | chroma_format = get_bits(gb, 2); /* chroma_format */ |
2575 | 26.9k | if (!chroma_format || chroma_format == CHROMA_420 || (rgb && chroma_format == CHROMA_422)) { |
2576 | 1.58k | av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n"); |
2577 | 1.58k | return AVERROR_INVALIDDATA; |
2578 | 1.58k | } |
2579 | | |
2580 | 25.3k | bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */ |
2581 | 25.3k | if (bits_per_raw_sample == 10) { |
2582 | 24.0k | if (rgb) { |
2583 | 19.1k | s->avctx->pix_fmt = AV_PIX_FMT_GBRP10; |
2584 | 19.1k | } else { |
2585 | 4.87k | s->avctx->pix_fmt = chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10; |
2586 | 4.87k | } |
2587 | 24.0k | } else { |
2588 | 1.34k | avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample); |
2589 | 1.34k | return AVERROR_PATCHWELCOME; |
2590 | 1.34k | } |
2591 | 24.0k | if (rgb != ctx->rgb || s->chroma_format != chroma_format) |
2592 | 4.01k | s->context_reinit = 1; |
2593 | 24.0k | s->avctx->bits_per_raw_sample = bits_per_raw_sample; |
2594 | 24.0k | ctx->rgb = rgb; |
2595 | 24.0k | s->chroma_format = chroma_format; |
2596 | 24.0k | } |
2597 | 24.0k | if (ctx->shape == RECT_SHAPE) { |
2598 | 24.0k | check_marker(s->avctx, gb, "before video_object_layer_width"); |
2599 | 24.0k | width = get_bits(gb, 14); /* video_object_layer_width */ |
2600 | 24.0k | check_marker(s->avctx, gb, "before video_object_layer_height"); |
2601 | 24.0k | height = get_bits(gb, 14); /* video_object_layer_height */ |
2602 | 24.0k | check_marker(s->avctx, gb, "after video_object_layer_height"); |
2603 | | |
2604 | | /* Do the same check as non-studio profile */ |
2605 | 24.0k | if (width && height) { |
2606 | 20.1k | if (s->width && s->height && |
2607 | 17.4k | (s->width != width || s->height != height)) |
2608 | 6.23k | s->context_reinit = 1; |
2609 | 20.1k | s->width = width; |
2610 | 20.1k | s->height = height; |
2611 | 20.1k | } |
2612 | 24.0k | } |
2613 | 24.0k | aspect_ratio_info = get_bits(gb, 4); |
2614 | 24.0k | if (aspect_ratio_info == FF_ASPECT_EXTENDED) { |
2615 | 1.67k | s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width |
2616 | 1.67k | s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height |
2617 | 22.3k | } else { |
2618 | 22.3k | s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info]; |
2619 | 22.3k | } |
2620 | 24.0k | skip_bits(gb, 4); /* frame_rate_code */ |
2621 | 24.0k | skip_bits(gb, 15); /* first_half_bit_rate */ |
2622 | 24.0k | check_marker(s->avctx, gb, "after first_half_bit_rate"); |
2623 | 24.0k | skip_bits(gb, 15); /* latter_half_bit_rate */ |
2624 | 24.0k | check_marker(s->avctx, gb, "after latter_half_bit_rate"); |
2625 | 24.0k | skip_bits(gb, 15); /* first_half_vbv_buffer_size */ |
2626 | 24.0k | check_marker(s->avctx, gb, "after first_half_vbv_buffer_size"); |
2627 | 24.0k | skip_bits(gb, 3); /* latter_half_vbv_buffer_size */ |
2628 | 24.0k | skip_bits(gb, 11); /* first_half_vbv_buffer_size */ |
2629 | 24.0k | check_marker(s->avctx, gb, "after first_half_vbv_buffer_size"); |
2630 | 24.0k | skip_bits(gb, 15); /* latter_half_vbv_occupancy */ |
2631 | 24.0k | check_marker(s->avctx, gb, "after latter_half_vbv_occupancy"); |
2632 | 24.0k | s->low_delay = get_bits1(gb); |
2633 | 24.0k | ctx->mpeg_quant = get_bits1(gb); /* mpeg2_stream */ |
2634 | | |
2635 | 24.0k | next_start_code_studio(gb); |
2636 | 24.0k | extension_and_user_data(s, gb, 2); |
2637 | | |
2638 | 24.0k | return 0; |
2639 | 26.9k | } |
2640 | | |
2641 | | static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb) |
2642 | 132k | { |
2643 | 132k | H263DecContext *const h = &ctx->h; |
2644 | 132k | int width, height, vo_ver_id, aspect_ratio_info; |
2645 | | |
2646 | | /* vol header */ |
2647 | 132k | skip_bits(gb, 1); /* random access */ |
2648 | 132k | ctx->vo_type = get_bits(gb, 8); |
2649 | | |
2650 | | /* If we are in studio profile (per vo_type), check if its all consistent |
2651 | | * and if so continue pass control to decode_studio_vol_header(). |
2652 | | * elIf something is inconsistent, error out |
2653 | | * else continue with (non studio) vol header decpoding. |
2654 | | */ |
2655 | 132k | if (ctx->vo_type == CORE_STUDIO_VO_TYPE || |
2656 | 127k | ctx->vo_type == SIMPLE_STUDIO_VO_TYPE) { |
2657 | 31.1k | if (h->c.avctx->profile != AV_PROFILE_UNKNOWN && h->c.avctx->profile != AV_PROFILE_MPEG4_SIMPLE_STUDIO) |
2658 | 2.94k | return AVERROR_INVALIDDATA; |
2659 | 28.2k | h->c.studio_profile = 1; |
2660 | 28.2k | h->c.avctx->profile = AV_PROFILE_MPEG4_SIMPLE_STUDIO; |
2661 | 28.2k | return decode_studio_vol_header(ctx, gb); |
2662 | 101k | } else if (h->c.studio_profile) { |
2663 | 8.86k | return AVERROR_PATCHWELCOME; |
2664 | 8.86k | } |
2665 | | |
2666 | 92.1k | if (get_bits1(gb) != 0) { /* is_ol_id */ |
2667 | 49.8k | vo_ver_id = get_bits(gb, 4); /* vo_ver_id */ |
2668 | 49.8k | skip_bits(gb, 3); /* vo_priority */ |
2669 | 49.8k | } else { |
2670 | 42.3k | vo_ver_id = 1; |
2671 | 42.3k | } |
2672 | 92.1k | aspect_ratio_info = get_bits(gb, 4); |
2673 | 92.1k | if (aspect_ratio_info == FF_ASPECT_EXTENDED) { |
2674 | 4.99k | h->c.avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width |
2675 | 4.99k | h->c.avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height |
2676 | 87.1k | } else { |
2677 | 87.1k | h->c.avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info]; |
2678 | 87.1k | } |
2679 | | |
2680 | 92.1k | if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */ |
2681 | 34.5k | int chroma_format = get_bits(gb, 2); |
2682 | 34.5k | if (chroma_format != CHROMA_420) |
2683 | 25.9k | av_log(h->c.avctx, AV_LOG_ERROR, "illegal chroma format\n"); |
2684 | | |
2685 | 34.5k | h->c.low_delay = get_bits1(gb); |
2686 | 34.5k | if (get_bits1(gb)) { /* vbv parameters */ |
2687 | 17.3k | get_bits(gb, 15); /* first_half_bitrate */ |
2688 | 17.3k | check_marker(h->c.avctx, gb, "after first_half_bitrate"); |
2689 | 17.3k | get_bits(gb, 15); /* latter_half_bitrate */ |
2690 | 17.3k | check_marker(h->c.avctx, gb, "after latter_half_bitrate"); |
2691 | 17.3k | get_bits(gb, 15); /* first_half_vbv_buffer_size */ |
2692 | 17.3k | check_marker(h->c.avctx, gb, "after first_half_vbv_buffer_size"); |
2693 | 17.3k | get_bits(gb, 3); /* latter_half_vbv_buffer_size */ |
2694 | 17.3k | get_bits(gb, 11); /* first_half_vbv_occupancy */ |
2695 | 17.3k | check_marker(h->c.avctx, gb, "after first_half_vbv_occupancy"); |
2696 | 17.3k | get_bits(gb, 15); /* latter_half_vbv_occupancy */ |
2697 | 17.3k | check_marker(h->c.avctx, gb, "after latter_half_vbv_occupancy"); |
2698 | 17.3k | } |
2699 | 57.5k | } else { |
2700 | | /* is setting low delay flag only once the smartest thing to do? |
2701 | | * low delay detection will not be overridden. */ |
2702 | 57.5k | if (h->picture_number == 0) { |
2703 | 12.5k | switch (ctx->vo_type) { |
2704 | 984 | case SIMPLE_VO_TYPE: |
2705 | 1.21k | case ADV_SIMPLE_VO_TYPE: |
2706 | 1.21k | h->c.low_delay = 1; |
2707 | 1.21k | break; |
2708 | 11.3k | default: |
2709 | 11.3k | h->c.low_delay = 0; |
2710 | 12.5k | } |
2711 | 12.5k | } |
2712 | 57.5k | } |
2713 | | |
2714 | 92.1k | ctx->shape = get_bits(gb, 2); /* vol shape */ |
2715 | 92.1k | if (ctx->shape != RECT_SHAPE) |
2716 | 17.3k | av_log(h->c.avctx, AV_LOG_ERROR, "only rectangular vol supported\n"); |
2717 | 92.1k | if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) { |
2718 | 2.67k | av_log(h->c.avctx, AV_LOG_ERROR, "Gray shape not supported\n"); |
2719 | 2.67k | skip_bits(gb, 4); /* video_object_layer_shape_extension */ |
2720 | 2.67k | } |
2721 | | |
2722 | 92.1k | check_marker(h->c.avctx, gb, "before time_increment_resolution"); |
2723 | | |
2724 | 92.1k | h->c.avctx->framerate.num = get_bits(gb, 16); |
2725 | 92.1k | if (!h->c.avctx->framerate.num) { |
2726 | 3.57k | av_log(h->c.avctx, AV_LOG_ERROR, "framerate==0\n"); |
2727 | 3.57k | return AVERROR_INVALIDDATA; |
2728 | 3.57k | } |
2729 | | |
2730 | 88.5k | ctx->time_increment_bits = av_log2(h->c.avctx->framerate.num - 1) + 1; |
2731 | 88.5k | if (ctx->time_increment_bits < 1) |
2732 | 0 | ctx->time_increment_bits = 1; |
2733 | | |
2734 | 88.5k | check_marker(h->c.avctx, gb, "before fixed_vop_rate"); |
2735 | | |
2736 | 88.5k | if (get_bits1(gb) != 0) /* fixed_vop_rate */ |
2737 | 36.6k | h->c.avctx->framerate.den = get_bits(gb, ctx->time_increment_bits); |
2738 | 51.9k | else |
2739 | 51.9k | h->c.avctx->framerate.den = 1; |
2740 | | |
2741 | 88.5k | ctx->t_frame = 0; |
2742 | | |
2743 | 88.5k | if (ctx->shape != BIN_ONLY_SHAPE) { |
2744 | 84.3k | if (ctx->shape == RECT_SHAPE) { |
2745 | 71.7k | check_marker(h->c.avctx, gb, "before width"); |
2746 | 71.7k | width = get_bits(gb, 13); |
2747 | 71.7k | check_marker(h->c.avctx, gb, "before height"); |
2748 | 71.7k | height = get_bits(gb, 13); |
2749 | 71.7k | check_marker(h->c.avctx, gb, "after height"); |
2750 | 71.7k | if (width && height && /* they should be non zero but who knows */ |
2751 | 57.6k | !(h->c.width && h->c.codec_tag == AV_RL32("MP4S"))) { |
2752 | 57.4k | if (h->c.width && h->c.height && |
2753 | 44.8k | (h->c.width != width || h->c.height != height)) |
2754 | 30.1k | h->c.context_reinit = 1; |
2755 | 57.4k | h->c.width = width; |
2756 | 57.4k | h->c.height = height; |
2757 | 57.4k | } |
2758 | 71.7k | } |
2759 | | |
2760 | 84.3k | h->c.progressive_sequence = |
2761 | 84.3k | h->c.progressive_frame = get_bits1(gb) ^ 1; |
2762 | 84.3k | h->c.interlaced_dct = 0; |
2763 | 84.3k | if (!get_bits1(gb) && (h->c.avctx->debug & FF_DEBUG_PICT_INFO)) |
2764 | 0 | av_log(h->c.avctx, AV_LOG_INFO, /* OBMC Disable */ |
2765 | 0 | "MPEG-4 OBMC not supported (very likely buggy encoder)\n"); |
2766 | 84.3k | if (vo_ver_id == 1) |
2767 | 39.8k | ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */ |
2768 | 44.4k | else |
2769 | 44.4k | ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */ |
2770 | | |
2771 | 84.3k | if (ctx->vol_sprite_usage == STATIC_SPRITE) |
2772 | 11.9k | av_log(h->c.avctx, AV_LOG_ERROR, "Static Sprites not supported\n"); |
2773 | 84.3k | if (ctx->vol_sprite_usage == STATIC_SPRITE || |
2774 | 72.3k | ctx->vol_sprite_usage == GMC_SPRITE) { |
2775 | 21.3k | if (ctx->vol_sprite_usage == STATIC_SPRITE) { |
2776 | 11.9k | skip_bits(gb, 13); // sprite_width |
2777 | 11.9k | check_marker(h->c.avctx, gb, "after sprite_width"); |
2778 | 11.9k | skip_bits(gb, 13); // sprite_height |
2779 | 11.9k | check_marker(h->c.avctx, gb, "after sprite_height"); |
2780 | 11.9k | skip_bits(gb, 13); // sprite_left |
2781 | 11.9k | check_marker(h->c.avctx, gb, "after sprite_left"); |
2782 | 11.9k | skip_bits(gb, 13); // sprite_top |
2783 | 11.9k | check_marker(h->c.avctx, gb, "after sprite_top"); |
2784 | 11.9k | } |
2785 | 21.3k | ctx->num_sprite_warping_points = get_bits(gb, 6); |
2786 | 21.3k | if (ctx->num_sprite_warping_points > 3) { |
2787 | 8.91k | av_log(h->c.avctx, AV_LOG_ERROR, |
2788 | 8.91k | "%d sprite_warping_points\n", |
2789 | 8.91k | ctx->num_sprite_warping_points); |
2790 | 8.91k | ctx->num_sprite_warping_points = 0; |
2791 | 8.91k | return AVERROR_INVALIDDATA; |
2792 | 8.91k | } |
2793 | 12.3k | ctx->sprite_warping_accuracy = get_bits(gb, 2); |
2794 | 12.3k | ctx->sprite_brightness_change = get_bits1(gb); |
2795 | 12.3k | if (ctx->vol_sprite_usage == STATIC_SPRITE) |
2796 | 3.88k | skip_bits1(gb); // low_latency_sprite |
2797 | 12.3k | } |
2798 | | // FIXME sadct disable bit if verid!=1 && shape not rect |
2799 | | |
2800 | 75.3k | if (get_bits1(gb) == 1) { /* not_8_bit */ |
2801 | 19.3k | ctx->quant_precision = get_bits(gb, 4); /* quant_precision */ |
2802 | 19.3k | if (get_bits(gb, 4) != 8) /* bits_per_pixel */ |
2803 | 18.1k | av_log(h->c.avctx, AV_LOG_ERROR, "N-bit not supported\n"); |
2804 | 19.3k | if (ctx->quant_precision != 5) |
2805 | 17.9k | av_log(h->c.avctx, AV_LOG_ERROR, |
2806 | 17.9k | "quant precision %d\n", ctx->quant_precision); |
2807 | 19.3k | if (ctx->quant_precision < 3 || ctx->quant_precision > 9) |
2808 | 12.9k | ctx->quant_precision = 5; |
2809 | 56.0k | } else { |
2810 | 56.0k | ctx->quant_precision = 5; |
2811 | 56.0k | } |
2812 | | |
2813 | | // FIXME a bunch of grayscale shape things |
2814 | | |
2815 | 75.3k | if ((ctx->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */ |
2816 | 25.3k | int i, v; |
2817 | | |
2818 | 25.3k | mpeg4_load_default_matrices(&h->c); |
2819 | | |
2820 | | /* load custom intra matrix */ |
2821 | 25.3k | if (get_bits1(gb)) { |
2822 | 12.3k | int last = 0; |
2823 | 108k | for (i = 0; i < 64; i++) { |
2824 | 107k | int j; |
2825 | 107k | if (get_bits_left(gb) < 8) { |
2826 | 3.94k | av_log(h->c.avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n"); |
2827 | 3.94k | return AVERROR_INVALIDDATA; |
2828 | 3.94k | } |
2829 | 103k | v = get_bits(gb, 8); |
2830 | 103k | if (v == 0) |
2831 | 7.81k | break; |
2832 | | |
2833 | 96.0k | last = v; |
2834 | 96.0k | j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]]; |
2835 | 96.0k | h->c.intra_matrix[j] = last; |
2836 | 96.0k | } |
2837 | | |
2838 | | /* replicate last value */ |
2839 | 448k | for (; i < 64; i++) { |
2840 | 440k | int j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]]; |
2841 | 440k | h->c.intra_matrix[j] = last; |
2842 | 440k | } |
2843 | 8.36k | } |
2844 | | |
2845 | | /* load custom non intra matrix */ |
2846 | 21.3k | if (get_bits1(gb)) { |
2847 | 8.30k | int last = 0; |
2848 | 79.1k | for (i = 0; i < 64; i++) { |
2849 | 78.7k | int j; |
2850 | 78.7k | if (get_bits_left(gb) < 8) { |
2851 | 580 | av_log(h->c.avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n"); |
2852 | 580 | return AVERROR_INVALIDDATA; |
2853 | 580 | } |
2854 | 78.2k | v = get_bits(gb, 8); |
2855 | 78.2k | if (v == 0) |
2856 | 7.37k | break; |
2857 | | |
2858 | 70.8k | last = v; |
2859 | 70.8k | j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]]; |
2860 | 70.8k | h->c.inter_matrix[j] = v; |
2861 | 70.8k | } |
2862 | | |
2863 | | /* replicate last value */ |
2864 | 433k | for (; i < 64; i++) { |
2865 | 425k | int j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]]; |
2866 | 425k | h->c.inter_matrix[j] = last; |
2867 | 425k | } |
2868 | 7.72k | } |
2869 | | |
2870 | | // FIXME a bunch of grayscale shape things |
2871 | 21.3k | } |
2872 | | |
2873 | 70.8k | if (vo_ver_id != 1) |
2874 | 38.8k | h->c.quarter_sample = get_bits1(gb); |
2875 | 32.0k | else |
2876 | 32.0k | h->c.quarter_sample = 0; |
2877 | | |
2878 | 70.8k | if (get_bits_left(gb) < 4) { |
2879 | 4.81k | av_log(h->c.avctx, AV_LOG_ERROR, "VOL Header truncated\n"); |
2880 | 4.81k | return AVERROR_INVALIDDATA; |
2881 | 4.81k | } |
2882 | | |
2883 | 66.0k | if (!get_bits1(gb)) { |
2884 | 52.6k | int pos = get_bits_count(gb); |
2885 | 52.6k | int estimation_method = get_bits(gb, 2); |
2886 | 52.6k | if (estimation_method < 2) { |
2887 | 40.9k | if (!get_bits1(gb)) { |
2888 | 35.0k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */ |
2889 | 35.0k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */ |
2890 | 35.0k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */ |
2891 | 35.0k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */ |
2892 | 35.0k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */ |
2893 | 35.0k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */ |
2894 | 35.0k | } |
2895 | 40.9k | if (!get_bits1(gb)) { |
2896 | 32.1k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */ |
2897 | 32.1k | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */ |
2898 | 32.1k | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */ |
2899 | 32.1k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */ |
2900 | 32.1k | } |
2901 | 40.9k | if (!check_marker(h->c.avctx, gb, "in complexity estimation part 1")) { |
2902 | 29.5k | skip_bits_long(gb, pos - get_bits_count(gb)); |
2903 | 29.5k | goto no_cplx_est; |
2904 | 29.5k | } |
2905 | 11.3k | if (!get_bits1(gb)) { |
2906 | 6.80k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */ |
2907 | 6.80k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */ |
2908 | 6.80k | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */ |
2909 | 6.80k | ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */ |
2910 | 6.80k | } |
2911 | 11.3k | if (!get_bits1(gb)) { |
2912 | 4.89k | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */ |
2913 | 4.89k | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */ |
2914 | 4.89k | ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */ |
2915 | 4.89k | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */ |
2916 | 4.89k | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */ |
2917 | 4.89k | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */ |
2918 | 4.89k | } |
2919 | 11.3k | if (!check_marker(h->c.avctx, gb, "in complexity estimation part 2")) { |
2920 | 6.67k | skip_bits_long(gb, pos - get_bits_count(gb)); |
2921 | 6.67k | goto no_cplx_est; |
2922 | 6.67k | } |
2923 | 4.69k | if (estimation_method == 1) { |
2924 | 860 | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */ |
2925 | 860 | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */ |
2926 | 860 | } |
2927 | 4.69k | } else |
2928 | 11.7k | av_log(h->c.avctx, AV_LOG_ERROR, |
2929 | 11.7k | "Invalid Complexity estimation method %d\n", |
2930 | 11.7k | estimation_method); |
2931 | 52.6k | } else { |
2932 | | |
2933 | 49.6k | no_cplx_est: |
2934 | 49.6k | ctx->cplx_estimation_trash_i = |
2935 | 49.6k | ctx->cplx_estimation_trash_p = |
2936 | 49.6k | ctx->cplx_estimation_trash_b = 0; |
2937 | 49.6k | } |
2938 | | |
2939 | 66.0k | ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */ |
2940 | | |
2941 | 66.0k | h->data_partitioning = get_bits1(gb); |
2942 | 66.0k | if (h->data_partitioning) |
2943 | 18.7k | ctx->rvlc = get_bits1(gb); |
2944 | | |
2945 | 66.0k | if (vo_ver_id != 1) { |
2946 | 37.4k | ctx->new_pred = get_bits1(gb); |
2947 | 37.4k | if (ctx->new_pred) { |
2948 | 10.9k | av_log(h->c.avctx, AV_LOG_ERROR, "new pred not supported\n"); |
2949 | 10.9k | skip_bits(gb, 2); /* requested upstream message type */ |
2950 | 10.9k | skip_bits1(gb); /* newpred segment type */ |
2951 | 10.9k | } |
2952 | 37.4k | if (get_bits1(gb)) // reduced_res_vop |
2953 | 11.7k | av_log(h->c.avctx, AV_LOG_ERROR, |
2954 | 11.7k | "reduced resolution VOP not supported\n"); |
2955 | 37.4k | } else { |
2956 | 28.5k | ctx->new_pred = 0; |
2957 | 28.5k | } |
2958 | | |
2959 | 66.0k | ctx->scalability = get_bits1(gb); |
2960 | | |
2961 | 66.0k | if (ctx->scalability) { |
2962 | 15.4k | GetBitContext bak = *gb; |
2963 | 15.4k | int h_sampling_factor_n; |
2964 | 15.4k | int h_sampling_factor_m; |
2965 | 15.4k | int v_sampling_factor_n; |
2966 | 15.4k | int v_sampling_factor_m; |
2967 | | |
2968 | 15.4k | skip_bits1(gb); // hierarchy_type |
2969 | 15.4k | skip_bits(gb, 4); /* ref_layer_id */ |
2970 | 15.4k | skip_bits1(gb); /* ref_layer_sampling_dir */ |
2971 | 15.4k | h_sampling_factor_n = get_bits(gb, 5); |
2972 | 15.4k | h_sampling_factor_m = get_bits(gb, 5); |
2973 | 15.4k | v_sampling_factor_n = get_bits(gb, 5); |
2974 | 15.4k | v_sampling_factor_m = get_bits(gb, 5); |
2975 | 15.4k | ctx->enhancement_type = get_bits1(gb); |
2976 | | |
2977 | 15.4k | if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 || |
2978 | 11.3k | v_sampling_factor_n == 0 || v_sampling_factor_m == 0) { |
2979 | | /* illegal scalability header (VERY broken encoder), |
2980 | | * trying to workaround */ |
2981 | 11.3k | ctx->scalability = 0; |
2982 | 11.3k | *gb = bak; |
2983 | 11.3k | } else |
2984 | 4.10k | av_log(h->c.avctx, AV_LOG_ERROR, "scalability not supported\n"); |
2985 | | |
2986 | | // bin shape stuff FIXME |
2987 | 15.4k | } |
2988 | 66.0k | } |
2989 | | |
2990 | 70.3k | if (h->c.avctx->debug&FF_DEBUG_PICT_INFO) { |
2991 | 0 | av_log(h->c.avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d %s%s%s%s\n", |
2992 | 0 | h->c.avctx->framerate.den, h->c.avctx->framerate.num, |
2993 | 0 | ctx->time_increment_bits, |
2994 | 0 | ctx->quant_precision, |
2995 | 0 | h->c.progressive_sequence, |
2996 | 0 | h->c.low_delay, |
2997 | 0 | ctx->scalability ? "scalability " :"" , |
2998 | 0 | h->c.quarter_sample ? "qpel " : "", |
2999 | 0 | h->data_partitioning ? "partition " : "", |
3000 | 0 | ctx->rvlc ? "rvlc " : "" |
3001 | 0 | ); |
3002 | 0 | } |
3003 | | |
3004 | 70.3k | return 0; |
3005 | 88.5k | } |
3006 | | |
3007 | | /** |
3008 | | * Decode the user data stuff in the header. |
3009 | | * Also initializes divx/xvid/lavc_version/build. |
3010 | | */ |
3011 | | static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb) |
3012 | 18.0k | { |
3013 | 18.0k | H263DecContext *const h = &ctx->h; |
3014 | 18.0k | char buf[256]; |
3015 | 18.0k | int i; |
3016 | 18.0k | int e; |
3017 | 18.0k | int ver = 0, build = 0, ver2 = 0, ver3 = 0; |
3018 | 18.0k | char last; |
3019 | | |
3020 | 512k | for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) { |
3021 | 512k | if (show_bits(gb, 23) == 0) |
3022 | 17.2k | break; |
3023 | 494k | buf[i] = get_bits(gb, 8); |
3024 | 494k | } |
3025 | 18.0k | buf[i] = 0; |
3026 | | |
3027 | | /* divx detection */ |
3028 | 18.0k | e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last); |
3029 | 18.0k | if (e < 2) |
3030 | 18.0k | e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last); |
3031 | 18.0k | if (e >= 2) { |
3032 | 2.77k | ctx->divx_version = ver; |
3033 | 2.77k | ctx->divx_build = build; |
3034 | 2.77k | h->divx_packed = e == 3 && last == 'p'; |
3035 | 2.77k | } |
3036 | | |
3037 | | /* libavcodec detection */ |
3038 | 18.0k | e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3; |
3039 | 18.0k | if (e != 4) |
3040 | 18.0k | e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build); |
3041 | 18.0k | if (e != 4) { |
3042 | 18.0k | e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1; |
3043 | 18.0k | if (e > 1) { |
3044 | 1.19k | if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) { |
3045 | 887 | av_log(h->c.avctx, AV_LOG_WARNING, |
3046 | 887 | "Unknown Lavc version string encountered, %d.%d.%d; " |
3047 | 887 | "clamping sub-version values to 8-bits.\n", |
3048 | 887 | ver, ver2, ver3); |
3049 | 887 | } |
3050 | 1.19k | build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF); |
3051 | 1.19k | } |
3052 | 18.0k | } |
3053 | 18.0k | if (e != 4) { |
3054 | 17.2k | if (strcmp(buf, "ffmpeg") == 0) |
3055 | 1.58k | ctx->lavc_build = 4600; |
3056 | 17.2k | } |
3057 | 18.0k | if (e == 4) |
3058 | 768 | ctx->lavc_build = build; |
3059 | | |
3060 | | /* Xvid detection */ |
3061 | 18.0k | e = sscanf(buf, "XviD%d", &build); |
3062 | 18.0k | if (e == 1) |
3063 | 3.14k | ctx->xvid_build = build; |
3064 | | |
3065 | 18.0k | return 0; |
3066 | 18.0k | } |
3067 | | |
3068 | | static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb, |
3069 | | int parse_only) |
3070 | 524k | { |
3071 | 524k | H263DecContext *const h = &ctx->h; |
3072 | 524k | int time_incr, time_increment; |
3073 | 524k | int64_t pts; |
3074 | | |
3075 | 524k | h->c.mcsel = 0; |
3076 | 524k | h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */ |
3077 | 524k | if (h->c.pict_type == AV_PICTURE_TYPE_B && h->c.low_delay && |
3078 | 9.81k | ctx->vol_control_parameters == 0 && !(h->c.avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) { |
3079 | 1.30k | av_log(h->c.avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n"); |
3080 | 1.30k | h->c.low_delay = 0; |
3081 | 1.30k | } |
3082 | | |
3083 | 524k | h->partitioned_frame = h->data_partitioning && h->c.pict_type != AV_PICTURE_TYPE_B; |
3084 | 524k | if (h->partitioned_frame) |
3085 | 90.0k | h->decode_mb = mpeg4_decode_partitioned_mb; |
3086 | 434k | else |
3087 | 434k | h->decode_mb = mpeg4_decode_mb; |
3088 | | |
3089 | 524k | time_incr = 0; |
3090 | 1.28M | while (get_bits1(gb) != 0) |
3091 | 760k | time_incr++; |
3092 | | |
3093 | 524k | check_marker(h->c.avctx, gb, "before time_increment"); |
3094 | | |
3095 | 524k | if (ctx->time_increment_bits == 0 || |
3096 | 522k | !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) { |
3097 | 154k | av_log(h->c.avctx, AV_LOG_WARNING, |
3098 | 154k | "time_increment_bits %d is invalid in relation to the current bitstream, this is likely caused by a missing VOL header\n", ctx->time_increment_bits); |
3099 | | |
3100 | 154k | for (ctx->time_increment_bits = 1; |
3101 | 2.07M | ctx->time_increment_bits < 16; |
3102 | 1.96M | ctx->time_increment_bits++) { |
3103 | 1.96M | if (h->c.pict_type == AV_PICTURE_TYPE_P || |
3104 | 1.81M | (h->c.pict_type == AV_PICTURE_TYPE_S && |
3105 | 437k | ctx->vol_sprite_usage == GMC_SPRITE)) { |
3106 | 254k | if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30) |
3107 | 9.76k | break; |
3108 | 1.71M | } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18) |
3109 | 35.8k | break; |
3110 | 1.96M | } |
3111 | | |
3112 | 154k | av_log(h->c.avctx, AV_LOG_WARNING, |
3113 | 154k | "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits); |
3114 | 154k | } |
3115 | | |
3116 | 524k | if (IS_3IV1) |
3117 | 0 | time_increment = get_bits1(gb); // FIXME investigate further |
3118 | 524k | else |
3119 | 524k | time_increment = get_bits(gb, ctx->time_increment_bits); |
3120 | | |
3121 | 524k | if (h->c.pict_type != AV_PICTURE_TYPE_B) { |
3122 | 459k | h->c.last_time_base = h->c.time_base; |
3123 | 459k | h->c.time_base += time_incr; |
3124 | 459k | h->c.time = h->c.time_base * (int64_t)h->c.avctx->framerate.num + time_increment; |
3125 | 459k | if (h->c.workaround_bugs & FF_BUG_UMP4) { |
3126 | 63.9k | if (h->c.time < h->c.last_non_b_time) { |
3127 | | /* header is not mpeg-4-compatible, broken encoder, |
3128 | | * trying to workaround */ |
3129 | 3.92k | h->c.time_base++; |
3130 | 3.92k | h->c.time += h->c.avctx->framerate.num; |
3131 | 3.92k | } |
3132 | 63.9k | } |
3133 | 459k | h->c.pp_time = h->c.time - h->c.last_non_b_time; |
3134 | 459k | h->c.last_non_b_time = h->c.time; |
3135 | 459k | } else { |
3136 | 64.3k | h->c.time = (h->c.last_time_base + time_incr) * (int64_t)h->c.avctx->framerate.num + time_increment; |
3137 | 64.3k | h->c.pb_time = h->c.pp_time - (h->c.last_non_b_time - h->c.time); |
3138 | 64.3k | if (h->c.pp_time <= h->c.pb_time || |
3139 | 33.9k | h->c.pp_time <= h->c.pp_time - h->c.pb_time || |
3140 | 33.3k | h->c.pp_time <= 0) { |
3141 | | /* messed up order, maybe after seeking? skipping current B-frame */ |
3142 | 33.3k | return FRAME_SKIPPED; |
3143 | 33.3k | } |
3144 | 30.9k | ff_mpeg4_init_direct_mv(&h->c); |
3145 | | |
3146 | 30.9k | if (ctx->t_frame == 0) |
3147 | 10.3k | ctx->t_frame = h->c.pb_time; |
3148 | 30.9k | if (ctx->t_frame == 0) |
3149 | 0 | ctx->t_frame = 1; // 1/0 protection |
3150 | 30.9k | h->c.pp_field_time = (ROUNDED_DIV(h->c.last_non_b_time, ctx->t_frame) - |
3151 | 30.9k | ROUNDED_DIV(h->c.last_non_b_time - h->c.pp_time, ctx->t_frame)) * 2; |
3152 | 30.9k | h->c.pb_field_time = (ROUNDED_DIV(h->c.time, ctx->t_frame) - |
3153 | 30.9k | ROUNDED_DIV(h->c.last_non_b_time - h->c.pp_time, ctx->t_frame)) * 2; |
3154 | 30.9k | if (h->c.pp_field_time <= h->c.pb_field_time || h->c.pb_field_time <= 1) { |
3155 | 23.6k | h->c.pb_field_time = 2; |
3156 | 23.6k | h->c.pp_field_time = 4; |
3157 | 23.6k | if (!h->c.progressive_sequence) |
3158 | 9.08k | return FRAME_SKIPPED; |
3159 | 23.6k | } |
3160 | 30.9k | } |
3161 | | |
3162 | 481k | if (h->c.avctx->framerate.den) |
3163 | 473k | pts = ROUNDED_DIV(h->c.time, h->c.avctx->framerate.den); |
3164 | 8.30k | else |
3165 | 8.30k | pts = AV_NOPTS_VALUE; |
3166 | 481k | ff_dlog(h->c.avctx, "MPEG4 PTS: %"PRId64"\n", pts); |
3167 | | |
3168 | 481k | check_marker(h->c.avctx, gb, "before vop_coded"); |
3169 | | |
3170 | | /* vop coded */ |
3171 | 481k | if (get_bits1(gb) != 1) { |
3172 | 90.6k | if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) |
3173 | 0 | av_log(h->c.avctx, AV_LOG_ERROR, "vop not coded\n"); |
3174 | 90.6k | h->skipped_last_frame = 1; |
3175 | 90.6k | return FRAME_SKIPPED; |
3176 | 90.6k | } |
3177 | 391k | if (ctx->new_pred) |
3178 | 42.4k | decode_new_pred(ctx, gb); |
3179 | | |
3180 | 391k | if (ctx->shape != BIN_ONLY_SHAPE && |
3181 | 377k | (h->c.pict_type == AV_PICTURE_TYPE_P || |
3182 | 248k | (h->c.pict_type == AV_PICTURE_TYPE_S && |
3183 | 179k | ctx->vol_sprite_usage == GMC_SPRITE))) { |
3184 | | /* rounding type for motion estimation */ |
3185 | 157k | h->c.no_rounding = get_bits1(gb); |
3186 | 233k | } else { |
3187 | 233k | h->c.no_rounding = 0; |
3188 | 233k | } |
3189 | | // FIXME reduced res stuff |
3190 | | |
3191 | 391k | if (ctx->shape != RECT_SHAPE) { |
3192 | 43.2k | if (ctx->vol_sprite_usage != 1 || h->c.pict_type != AV_PICTURE_TYPE_I) { |
3193 | 35.4k | skip_bits(gb, 13); /* width */ |
3194 | 35.4k | check_marker(h->c.avctx, gb, "after width"); |
3195 | 35.4k | skip_bits(gb, 13); /* height */ |
3196 | 35.4k | check_marker(h->c.avctx, gb, "after height"); |
3197 | 35.4k | skip_bits(gb, 13); /* hor_spat_ref */ |
3198 | 35.4k | check_marker(h->c.avctx, gb, "after hor_spat_ref"); |
3199 | 35.4k | skip_bits(gb, 13); /* ver_spat_ref */ |
3200 | 35.4k | } |
3201 | 43.2k | skip_bits1(gb); /* change_CR_disable */ |
3202 | | |
3203 | 43.2k | if (get_bits1(gb) != 0) |
3204 | 24.1k | skip_bits(gb, 8); /* constant_alpha_value */ |
3205 | 43.2k | } |
3206 | | |
3207 | | // FIXME complexity estimation stuff |
3208 | | |
3209 | 391k | if (ctx->shape != BIN_ONLY_SHAPE) { |
3210 | 377k | skip_bits_long(gb, ctx->cplx_estimation_trash_i); |
3211 | 377k | if (h->c.pict_type != AV_PICTURE_TYPE_I) |
3212 | 324k | skip_bits_long(gb, ctx->cplx_estimation_trash_p); |
3213 | 377k | if (h->c.pict_type == AV_PICTURE_TYPE_B) |
3214 | 15.8k | skip_bits_long(gb, ctx->cplx_estimation_trash_b); |
3215 | | |
3216 | 377k | if (get_bits_left(gb) < 3) { |
3217 | 16.0k | av_log(h->c.avctx, AV_LOG_ERROR, "Header truncated\n"); |
3218 | 16.0k | return AVERROR_INVALIDDATA; |
3219 | 16.0k | } |
3220 | 361k | ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)]; |
3221 | 361k | if (!h->c.progressive_sequence) { |
3222 | 143k | h->c.top_field_first = get_bits1(gb); |
3223 | 143k | h->c.alternate_scan = get_bits1(gb); |
3224 | 143k | } else |
3225 | 217k | h->c.alternate_scan = 0; |
3226 | 361k | } |
3227 | | /* Skip at this point when only parsing since the remaining |
3228 | | * data is not useful for a parser and requires the |
3229 | | * sprite_trajectory VLC to be initialized. */ |
3230 | 374k | if (parse_only) |
3231 | 216k | goto end; |
3232 | | |
3233 | 158k | if (h->c.alternate_scan) { |
3234 | 16.7k | ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, ff_alternate_vertical_scan); |
3235 | 16.7k | ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_vertical_scan, |
3236 | 16.7k | h->c.idsp.idct_permutation); |
3237 | 141k | } else { |
3238 | 141k | ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, ff_zigzag_direct); |
3239 | 141k | ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_horizontal_scan, |
3240 | 141k | h->c.idsp.idct_permutation); |
3241 | 141k | } |
3242 | 158k | ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan, |
3243 | 158k | h->c.idsp.idct_permutation); |
3244 | | |
3245 | 158k | if (h->c.pict_type == AV_PICTURE_TYPE_S) { |
3246 | 79.6k | if((ctx->vol_sprite_usage == STATIC_SPRITE || |
3247 | 74.7k | ctx->vol_sprite_usage == GMC_SPRITE)) { |
3248 | 19.3k | if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0) |
3249 | 3.66k | return AVERROR_INVALIDDATA; |
3250 | 15.6k | if (ctx->sprite_brightness_change) |
3251 | 5.36k | av_log(h->c.avctx, AV_LOG_ERROR, |
3252 | 5.36k | "sprite_brightness_change not supported\n"); |
3253 | 15.6k | if (ctx->vol_sprite_usage == STATIC_SPRITE) |
3254 | 4.25k | av_log(h->c.avctx, AV_LOG_ERROR, "static sprite not supported\n"); |
3255 | 60.3k | } else { |
3256 | 60.3k | memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset)); |
3257 | 60.3k | memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta)); |
3258 | 60.3k | } |
3259 | 79.6k | } |
3260 | | |
3261 | 154k | ctx->f_code = 1; |
3262 | 154k | ctx->b_code = 1; |
3263 | 154k | if (ctx->shape != BIN_ONLY_SHAPE) { |
3264 | 151k | h->c.chroma_qscale = h->c.qscale = get_bits(gb, ctx->quant_precision); |
3265 | 151k | if (h->c.qscale == 0) { |
3266 | 7.05k | av_log(h->c.avctx, AV_LOG_ERROR, |
3267 | 7.05k | "Error, header damaged or not MPEG-4 header (qscale=0)\n"); |
3268 | 7.05k | return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then |
3269 | 7.05k | } |
3270 | | |
3271 | 144k | if (h->c.pict_type != AV_PICTURE_TYPE_I) { |
3272 | 131k | ctx->f_code = get_bits(gb, 3); /* fcode_for */ |
3273 | 131k | if (ctx->f_code == 0) { |
3274 | 4.14k | av_log(h->c.avctx, AV_LOG_ERROR, |
3275 | 4.14k | "Error, header damaged or not MPEG-4 header (f_code=0)\n"); |
3276 | 4.14k | ctx->f_code = 1; |
3277 | 4.14k | return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then |
3278 | 4.14k | } |
3279 | 131k | } |
3280 | | |
3281 | 139k | if (h->c.pict_type == AV_PICTURE_TYPE_B) { |
3282 | 4.62k | ctx->b_code = get_bits(gb, 3); |
3283 | 4.62k | if (ctx->b_code == 0) { |
3284 | 226 | av_log(h->c.avctx, AV_LOG_ERROR, |
3285 | 226 | "Error, header damaged or not MPEG4 header (b_code=0)\n"); |
3286 | 226 | ctx->b_code=1; |
3287 | 226 | return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly |
3288 | 226 | } |
3289 | 4.62k | } |
3290 | | |
3291 | 139k | if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) { |
3292 | 0 | av_log(h->c.avctx, AV_LOG_DEBUG, |
3293 | 0 | "qp:%d fc:%d,%d %c size:%d pro:%d alt:%d top:%d %cpel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n", |
3294 | 0 | h->c.qscale, ctx->f_code, ctx->b_code, |
3295 | 0 | h->c.pict_type == AV_PICTURE_TYPE_I ? 'I' : (h->c.pict_type == AV_PICTURE_TYPE_P ? 'P' : (h->c.pict_type == AV_PICTURE_TYPE_B ? 'B' : 'S')), |
3296 | 0 | gb->size_in_bits,h->c.progressive_sequence, h->c.alternate_scan, |
3297 | 0 | h->c.top_field_first, h->c.quarter_sample ? 'q' : 'h', |
3298 | 0 | h->data_partitioning, ctx->resync_marker, |
3299 | 0 | ctx->num_sprite_warping_points, ctx->sprite_warping_accuracy, |
3300 | 0 | 1 - h->c.no_rounding, ctx->vo_type, |
3301 | 0 | ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold, |
3302 | 0 | ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p, |
3303 | 0 | ctx->cplx_estimation_trash_b, |
3304 | 0 | h->c.time, |
3305 | 0 | time_increment |
3306 | 0 | ); |
3307 | 0 | } |
3308 | | |
3309 | 139k | if (!ctx->scalability) { |
3310 | 135k | if (ctx->shape != RECT_SHAPE && h->c.pict_type != AV_PICTURE_TYPE_I) |
3311 | 1.39k | skip_bits1(gb); // vop shape coding type |
3312 | 135k | } else { |
3313 | 4.56k | if (ctx->enhancement_type) { |
3314 | 2.44k | int load_backward_shape = get_bits1(gb); |
3315 | 2.44k | if (load_backward_shape) |
3316 | 1.21k | av_log(h->c.avctx, AV_LOG_ERROR, |
3317 | 1.21k | "load backward shape isn't supported\n"); |
3318 | 2.44k | } |
3319 | 4.56k | skip_bits(gb, 2); // ref_select_code |
3320 | 4.56k | } |
3321 | 139k | } |
3322 | | |
3323 | 143k | h->c.dct_unquantize_intra = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_intra |
3324 | 143k | : ctx->dct_unquantize_h263_intra; |
3325 | | // The following tells ff_mpv_reconstruct_mb() to unquantize iff mpeg_quant |
3326 | 143k | h->c.dct_unquantize_inter = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_inter : NULL; |
3327 | | |
3328 | 359k | end: |
3329 | | /* detect buggy encoders which don't set the low_delay flag |
3330 | | * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames |
3331 | | * easily (although it's buggy too) */ |
3332 | 359k | if (ctx->vo_type == 0 && ctx->vol_control_parameters == 0 && |
3333 | 24.6k | ctx->divx_version == -1 && h->picture_number == 0) { |
3334 | 994 | av_log(h->c.avctx, AV_LOG_WARNING, |
3335 | 994 | "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n"); |
3336 | 994 | h->c.low_delay = 1; |
3337 | 994 | } |
3338 | | |
3339 | 359k | h->picture_number++; // better than pic number==0 always ;) |
3340 | | |
3341 | 359k | if (h->c.workaround_bugs & FF_BUG_EDGE) { |
3342 | 48.5k | h->c.h_edge_pos = h->c.width; |
3343 | 48.5k | h->c.v_edge_pos = h->c.height; |
3344 | 48.5k | } |
3345 | 359k | return 0; |
3346 | 143k | } |
3347 | | |
3348 | | static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb) |
3349 | 168k | { |
3350 | 168k | AVCodecContext *const avctx = ctx->h.c.avctx; |
3351 | | |
3352 | 168k | skip_bits(gb, 16); /* Time_code[63..48] */ |
3353 | 168k | check_marker(avctx, gb, "after Time_code[63..48]"); |
3354 | 168k | skip_bits(gb, 16); /* Time_code[47..32] */ |
3355 | 168k | check_marker(avctx, gb, "after Time_code[47..32]"); |
3356 | 168k | skip_bits(gb, 16); /* Time_code[31..16] */ |
3357 | 168k | check_marker(avctx, gb, "after Time_code[31..16]"); |
3358 | 168k | skip_bits(gb, 16); /* Time_code[15..0] */ |
3359 | 168k | check_marker(avctx, gb, "after Time_code[15..0]"); |
3360 | 168k | skip_bits(gb, 4); /* reserved_bits */ |
3361 | 168k | } |
3362 | | |
3363 | | /** |
3364 | | * Decode the next studio vop header. |
3365 | | * @return <0 if something went wrong |
3366 | | */ |
3367 | | static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb) |
3368 | 211k | { |
3369 | 211k | H263DecContext *const h = &ctx->h; |
3370 | | |
3371 | 211k | if (get_bits_left(gb) <= 32) |
3372 | 42.2k | return 0; |
3373 | | |
3374 | 168k | h->partitioned_frame = 0; |
3375 | 168k | h->c.interlaced_dct = 0; |
3376 | 168k | h->decode_mb = mpeg4_decode_studio_mb; |
3377 | | |
3378 | 168k | decode_smpte_tc(ctx, gb); |
3379 | | |
3380 | 168k | skip_bits(gb, 10); /* temporal_reference */ |
3381 | 168k | skip_bits(gb, 2); /* vop_structure */ |
3382 | 168k | h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */ |
3383 | 168k | if (get_bits1(gb)) { /* vop_coded */ |
3384 | 59.9k | skip_bits1(gb); /* top_field_first */ |
3385 | 59.9k | skip_bits1(gb); /* repeat_first_field */ |
3386 | 59.9k | h->c.progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */ |
3387 | 59.9k | } |
3388 | | |
3389 | 168k | if (h->c.pict_type == AV_PICTURE_TYPE_I) { |
3390 | 103k | if (get_bits1(gb)) |
3391 | 29.5k | reset_studio_dc_predictors(ctx); |
3392 | 103k | } |
3393 | | |
3394 | 168k | if (ctx->shape != BIN_ONLY_SHAPE) { |
3395 | 167k | h->c.alternate_scan = get_bits1(gb); |
3396 | 167k | h->c.frame_pred_frame_dct = get_bits1(gb); |
3397 | 167k | ctx->dct_precision = get_bits(gb, 2); |
3398 | 167k | h->c.intra_dc_precision = get_bits(gb, 2); |
3399 | 167k | h->c.q_scale_type = get_bits1(gb); |
3400 | 167k | } |
3401 | | |
3402 | 168k | ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, |
3403 | 168k | h->c.alternate_scan ? ff_alternate_vertical_scan : ff_zigzag_direct); |
3404 | | |
3405 | 168k | mpeg4_load_default_matrices(&h->c); |
3406 | | |
3407 | 168k | next_start_code_studio(gb); |
3408 | 168k | extension_and_user_data(&h->c, gb, 4); |
3409 | | |
3410 | 168k | return 0; |
3411 | 211k | } |
3412 | | |
3413 | | static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb) |
3414 | 4.38k | { |
3415 | 4.38k | int visual_object_type; |
3416 | | |
3417 | 4.38k | skip_bits(gb, 4); /* visual_object_verid */ |
3418 | 4.38k | visual_object_type = get_bits(gb, 4); |
3419 | 4.38k | if (visual_object_type != VOT_VIDEO_ID) { |
3420 | 1.92k | avpriv_request_sample(ctx->h.c.avctx, "VO type %u", visual_object_type); |
3421 | 1.92k | return AVERROR_PATCHWELCOME; |
3422 | 1.92k | } |
3423 | | |
3424 | 2.45k | next_start_code_studio(gb); |
3425 | 2.45k | extension_and_user_data(&ctx->h.c, gb, 1); |
3426 | | |
3427 | 2.45k | return 0; |
3428 | 4.38k | } |
3429 | | |
3430 | | /** |
3431 | | * Decode MPEG-4 headers. |
3432 | | * |
3433 | | * @param header If set the absence of a VOP is not treated as error; otherwise, it is treated as such. |
3434 | | * @param parse_only If set, things only relevant to a decoder may be skipped; |
3435 | | * furthermore, the VLC tables may be uninitialized. |
3436 | | * @return <0 if an error occurred |
3437 | | * FRAME_SKIPPED if a not coded VOP is found |
3438 | | * 0 else |
3439 | | */ |
3440 | | int ff_mpeg4_parse_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, |
3441 | | int header, int parse_only) |
3442 | 911k | { |
3443 | 911k | MPVContext *const s = &ctx->h.c; |
3444 | 911k | unsigned startcode, v; |
3445 | 911k | int ret; |
3446 | 911k | int vol = 0; |
3447 | | |
3448 | | /* search next start code */ |
3449 | 911k | align_get_bits(gb); |
3450 | | |
3451 | | // If we have not switched to studio profile than we also did not switch bps |
3452 | | // that means something else (like a previous instance) outside set bps which |
3453 | | // would be inconsistent with the correct state, thus reset it |
3454 | 911k | if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8) |
3455 | 656k | s->avctx->bits_per_raw_sample = 0; |
3456 | | |
3457 | 911k | if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) { |
3458 | 3 | skip_bits(gb, 24); |
3459 | 3 | if (get_bits(gb, 8) == 0xF0) |
3460 | 2 | goto end; |
3461 | 3 | } |
3462 | | |
3463 | 911k | startcode = 0xff; |
3464 | 73.0M | for (;;) { |
3465 | 73.0M | if (get_bits_count(gb) >= gb->size_in_bits) { |
3466 | 123k | if (gb->size_in_bits == 8 && |
3467 | 90.5k | (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) { |
3468 | 33.6k | av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits); |
3469 | 33.6k | return FRAME_SKIPPED; // divx bug |
3470 | 90.3k | } else if (header && get_bits_count(gb) == gb->size_in_bits) { |
3471 | 3.75k | return 0; // ordinary return value for parsing of extradata |
3472 | 3.75k | } else |
3473 | 86.5k | return AVERROR_INVALIDDATA; // end of stream |
3474 | 123k | } |
3475 | | |
3476 | | /* use the bits after the test */ |
3477 | 72.9M | v = get_bits(gb, 8); |
3478 | 72.9M | startcode = ((startcode << 8) | v) & 0xffffffff; |
3479 | | |
3480 | 72.9M | if ((startcode & 0xFFFFFF00) != 0x100) |
3481 | 71.5M | continue; // no startcode |
3482 | | |
3483 | 1.40M | if (s->avctx->debug & FF_DEBUG_STARTCODE) { |
3484 | 0 | const char *name; |
3485 | 0 | if (startcode <= 0x11F) |
3486 | 0 | name = "Video Object Start"; |
3487 | 0 | else if (startcode <= 0x12F) |
3488 | 0 | name = "Video Object Layer Start"; |
3489 | 0 | else if (startcode <= 0x13F) |
3490 | 0 | name = "Reserved"; |
3491 | 0 | else if (startcode <= 0x15F) |
3492 | 0 | name = "FGS bp start"; |
3493 | 0 | else if (startcode <= 0x1AF) |
3494 | 0 | name = "Reserved"; |
3495 | 0 | else if (startcode == 0x1B0) |
3496 | 0 | name = "Visual Object Seq Start"; |
3497 | 0 | else if (startcode == 0x1B1) |
3498 | 0 | name = "Visual Object Seq End"; |
3499 | 0 | else if (startcode == 0x1B2) |
3500 | 0 | name = "User Data"; |
3501 | 0 | else if (startcode == 0x1B3) |
3502 | 0 | name = "Group of VOP start"; |
3503 | 0 | else if (startcode == 0x1B4) |
3504 | 0 | name = "Video Session Error"; |
3505 | 0 | else if (startcode == 0x1B5) |
3506 | 0 | name = "Visual Object Start"; |
3507 | 0 | else if (startcode == 0x1B6) |
3508 | 0 | name = "Video Object Plane start"; |
3509 | 0 | else if (startcode == 0x1B7) |
3510 | 0 | name = "slice start"; |
3511 | 0 | else if (startcode == 0x1B8) |
3512 | 0 | name = "extension start"; |
3513 | 0 | else if (startcode == 0x1B9) |
3514 | 0 | name = "fgs start"; |
3515 | 0 | else if (startcode == 0x1BA) |
3516 | 0 | name = "FBA Object start"; |
3517 | 0 | else if (startcode == 0x1BB) |
3518 | 0 | name = "FBA Object Plane start"; |
3519 | 0 | else if (startcode == 0x1BC) |
3520 | 0 | name = "Mesh Object start"; |
3521 | 0 | else if (startcode == 0x1BD) |
3522 | 0 | name = "Mesh Object Plane start"; |
3523 | 0 | else if (startcode == 0x1BE) |
3524 | 0 | name = "Still Texture Object start"; |
3525 | 0 | else if (startcode == 0x1BF) |
3526 | 0 | name = "Texture Spatial Layer start"; |
3527 | 0 | else if (startcode == 0x1C0) |
3528 | 0 | name = "Texture SNR Layer start"; |
3529 | 0 | else if (startcode == 0x1C1) |
3530 | 0 | name = "Texture Tile start"; |
3531 | 0 | else if (startcode == 0x1C2) |
3532 | 0 | name = "Texture Shape Layer start"; |
3533 | 0 | else if (startcode == 0x1C3) |
3534 | 0 | name = "stuffing start"; |
3535 | 0 | else if (startcode <= 0x1C5) |
3536 | 0 | name = "Reserved"; |
3537 | 0 | else if (startcode <= 0x1FF) |
3538 | 0 | name = "System start"; |
3539 | 0 | else |
3540 | 0 | av_unreachable("Unexpected startcode"); |
3541 | 0 | av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X %s at %d\n", |
3542 | 0 | startcode, name, get_bits_count(gb)); |
3543 | 0 | } |
3544 | | |
3545 | 1.40M | if (startcode >= 0x120 && startcode <= 0x12F) { |
3546 | 147k | if (vol) { |
3547 | 15.2k | av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n"); |
3548 | 15.2k | continue; |
3549 | 15.2k | } |
3550 | 132k | vol++; |
3551 | 132k | if ((ret = decode_vol_header(ctx, gb)) < 0) |
3552 | 37.8k | return ret; |
3553 | 1.25M | } else if (startcode == USER_DATA_STARTCODE) { |
3554 | 18.0k | decode_user_data(ctx, gb); |
3555 | 1.23M | } else if (startcode == GOP_STARTCODE) { |
3556 | 83.4k | mpeg4_decode_gop_header(s, gb); |
3557 | 1.15M | } else if (startcode == VOS_STARTCODE) { |
3558 | 8.33k | int profile, level; |
3559 | 8.33k | mpeg4_decode_profile_level(s, gb, &profile, &level); |
3560 | 8.33k | if (profile == AV_PROFILE_MPEG4_SIMPLE_STUDIO && |
3561 | 2.11k | (level > 0 && level < 9)) { |
3562 | 843 | s->studio_profile = 1; |
3563 | 843 | next_start_code_studio(gb); |
3564 | 843 | extension_and_user_data(s, gb, 0); |
3565 | 7.48k | } else if (s->studio_profile) { |
3566 | 1.46k | avpriv_request_sample(s->avctx, "Mix of studio and non studio profile"); |
3567 | 1.46k | return AVERROR_PATCHWELCOME; |
3568 | 1.46k | } |
3569 | 6.86k | s->avctx->profile = profile; |
3570 | 6.86k | s->avctx->level = level; |
3571 | 1.14M | } else if (startcode == VISUAL_OBJ_STARTCODE) { |
3572 | 16.3k | if (s->studio_profile) { |
3573 | 4.38k | if ((ret = decode_studiovisualobject(ctx, gb)) < 0) |
3574 | 1.92k | return ret; |
3575 | 4.38k | } else |
3576 | 12.0k | mpeg4_decode_visual_object(s, gb); |
3577 | 1.12M | } else if (startcode == VOP_STARTCODE) { |
3578 | 746k | break; |
3579 | 746k | } |
3580 | | |
3581 | 598k | align_get_bits(gb); |
3582 | 598k | startcode = 0xff; |
3583 | 598k | } |
3584 | | |
3585 | 746k | end: |
3586 | 746k | if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY) |
3587 | 0 | s->low_delay = 1; |
3588 | | |
3589 | 746k | if (s->studio_profile) { |
3590 | 222k | if (!s->avctx->bits_per_raw_sample) { |
3591 | 10.8k | av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n"); |
3592 | 10.8k | return AVERROR_INVALIDDATA; |
3593 | 10.8k | } |
3594 | 211k | return decode_studio_vop_header(ctx, gb); |
3595 | 222k | } else |
3596 | 524k | return decode_vop_header(ctx, gb, parse_only); |
3597 | 746k | } |
3598 | | |
3599 | | #if CONFIG_MPEG4_DECODER |
3600 | | static av_cold void permute_quant_matrix(uint16_t matrix[64], |
3601 | | const uint8_t new_perm[64], |
3602 | | const uint8_t old_perm[64]) |
3603 | 560 | { |
3604 | 560 | uint16_t tmp[64]; |
3605 | | |
3606 | 560 | memcpy(tmp, matrix, sizeof(tmp)); |
3607 | 36.4k | for (int i = 0; i < 64; ++i) |
3608 | 35.8k | matrix[new_perm[i]] = tmp[old_perm[i]]; |
3609 | 560 | } |
3610 | | |
3611 | | static av_cold void switch_to_xvid_idct(AVCodecContext *const avctx, |
3612 | | H263DecContext *const h) |
3613 | 280 | { |
3614 | 280 | uint8_t old_permutation[64]; |
3615 | | |
3616 | 280 | memcpy(old_permutation, h->c.idsp.idct_permutation, sizeof(old_permutation)); |
3617 | | |
3618 | 280 | avctx->idct_algo = FF_IDCT_XVID; |
3619 | 280 | ff_mpv_idct_init(&h->c); |
3620 | 280 | ff_permute_scantable(h->permutated_intra_h_scantable, |
3621 | 280 | h->c.alternate_scan ? ff_alternate_vertical_scan : ff_alternate_horizontal_scan, |
3622 | 280 | h->c.idsp.idct_permutation); |
3623 | 280 | ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan, |
3624 | 280 | h->c.idsp.idct_permutation); |
3625 | | |
3626 | | // Normal (i.e. non-studio) MPEG-4 does not use the chroma matrices. |
3627 | 280 | permute_quant_matrix(h->c.inter_matrix, h->c.idsp.idct_permutation, old_permutation); |
3628 | 280 | permute_quant_matrix(h->c.intra_matrix, h->c.idsp.idct_permutation, old_permutation); |
3629 | 280 | } |
3630 | | |
3631 | | void ff_mpeg4_workaround_bugs(AVCodecContext *avctx) |
3632 | 134k | { |
3633 | 134k | Mpeg4DecContext *ctx = avctx->priv_data; |
3634 | 134k | H263DecContext *const h = &ctx->h; |
3635 | | |
3636 | 134k | if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) { |
3637 | 116k | if (h->c.codec_tag == AV_RL32("XVID") || |
3638 | 116k | h->c.codec_tag == AV_RL32("XVIX") || |
3639 | 116k | h->c.codec_tag == AV_RL32("RMP4") || |
3640 | 116k | h->c.codec_tag == AV_RL32("ZMP4") || |
3641 | 115k | h->c.codec_tag == AV_RL32("SIPP")) |
3642 | 857 | ctx->xvid_build = 0; |
3643 | 116k | } |
3644 | | |
3645 | 134k | if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) |
3646 | 115k | if (h->c.codec_tag == AV_RL32("DIVX") && ctx->vo_type == 0 && |
3647 | 394 | ctx->vol_control_parameters == 0) |
3648 | 195 | ctx->divx_version = 400; // divx 4 |
3649 | | |
3650 | 134k | if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) { |
3651 | 211 | ctx->divx_version = |
3652 | 211 | ctx->divx_build = -1; |
3653 | 211 | } |
3654 | | |
3655 | 134k | if (h->c.workaround_bugs & FF_BUG_AUTODETECT) { |
3656 | 56.7k | if (h->c.codec_tag == AV_RL32("XVIX")) |
3657 | 471 | h->c.workaround_bugs |= FF_BUG_XVID_ILACE; |
3658 | | |
3659 | 56.7k | if (h->c.codec_tag == AV_RL32("UMP4")) |
3660 | 197 | h->c.workaround_bugs |= FF_BUG_UMP4; |
3661 | | |
3662 | 56.7k | if (ctx->divx_version >= 500 && ctx->divx_build < 1814) |
3663 | 1.97k | h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA; |
3664 | | |
3665 | 56.7k | if (ctx->divx_version > 502 && ctx->divx_build < 1814) |
3666 | 1.65k | h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA2; |
3667 | | |
3668 | 56.7k | if (ctx->xvid_build <= 3U) |
3669 | 1.48k | h->padding_bug_score = 256 * 256 * 256 * 64; |
3670 | | |
3671 | 56.7k | if (ctx->xvid_build <= 1U) |
3672 | 1.24k | h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA; |
3673 | | |
3674 | 56.7k | if (ctx->xvid_build <= 12U) |
3675 | 1.52k | h->c.workaround_bugs |= FF_BUG_EDGE; |
3676 | | |
3677 | 56.7k | if (ctx->xvid_build <= 32U) |
3678 | 1.63k | h->c.workaround_bugs |= FF_BUG_DC_CLIP; |
3679 | | |
3680 | 56.7k | #define SET_QPEL_FUNC(postfix1, postfix2) \ |
3681 | 221k | h->c.qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \ |
3682 | 221k | h->c.qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \ |
3683 | 221k | h->c.qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2; |
3684 | | |
3685 | 56.7k | if (ctx->lavc_build < 4653U) |
3686 | 1.26k | h->c.workaround_bugs |= FF_BUG_STD_QPEL; |
3687 | | |
3688 | 56.7k | if (ctx->lavc_build < 4655U) |
3689 | 1.26k | h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE; |
3690 | | |
3691 | 56.7k | if (ctx->lavc_build < 4670U) |
3692 | 1.27k | h->c.workaround_bugs |= FF_BUG_EDGE; |
3693 | | |
3694 | 56.7k | if (ctx->lavc_build <= 4712U) |
3695 | 1.27k | h->c.workaround_bugs |= FF_BUG_DC_CLIP; |
3696 | | |
3697 | 56.7k | if ((ctx->lavc_build&0xFF) >= 100) { |
3698 | 55.8k | if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 && |
3699 | 887 | (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+ |
3700 | 55.8k | ) |
3701 | 496 | h->c.workaround_bugs |= FF_BUG_IEDGE; |
3702 | 55.8k | } |
3703 | | |
3704 | 56.7k | if (ctx->divx_version >= 0) |
3705 | 5.30k | h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE; |
3706 | 56.7k | if (ctx->divx_version == 501 && ctx->divx_build == 20020416) |
3707 | 0 | h->padding_bug_score = 256 * 256 * 256 * 64; |
3708 | | |
3709 | 56.7k | if (ctx->divx_version < 500U) |
3710 | 3.03k | h->c.workaround_bugs |= FF_BUG_EDGE; |
3711 | | |
3712 | 56.7k | if (ctx->divx_version >= 0) |
3713 | 5.30k | h->c.workaround_bugs |= FF_BUG_HPEL_CHROMA; |
3714 | 56.7k | } |
3715 | | |
3716 | 134k | if (h->c.workaround_bugs & FF_BUG_STD_QPEL) { |
3717 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c) |
3718 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c) |
3719 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c) |
3720 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c) |
3721 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c) |
3722 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c) |
3723 | | |
3724 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c) |
3725 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c) |
3726 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c) |
3727 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c) |
3728 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c) |
3729 | 18.4k | SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c) |
3730 | 18.4k | } |
3731 | | |
3732 | 134k | if (avctx->debug & FF_DEBUG_BUGS) |
3733 | 0 | av_log(h->c.avctx, AV_LOG_DEBUG, |
3734 | 0 | "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n", |
3735 | 0 | h->c.workaround_bugs, ctx->lavc_build, ctx->xvid_build, |
3736 | 0 | ctx->divx_version, ctx->divx_build, h->divx_packed ? "p" : ""); |
3737 | | |
3738 | 134k | if (ctx->xvid_build >= 0 && |
3739 | 3.47k | avctx->idct_algo == FF_IDCT_AUTO && !h->c.studio_profile) { |
3740 | 280 | switch_to_xvid_idct(avctx, h); |
3741 | 280 | } |
3742 | 134k | } |
3743 | | |
3744 | | static int mpeg4_decode_picture_header(H263DecContext *const h) |
3745 | 317k | { |
3746 | 317k | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); |
3747 | | |
3748 | 317k | h->skipped_last_frame = 0; |
3749 | | |
3750 | 317k | if (ctx->bitstream_buffer) { |
3751 | 2.67k | int buf_size = get_bits_left(&h->gb) / 8U; |
3752 | 2.67k | int bitstream_buffer_size = ctx->bitstream_buffer->size; |
3753 | 2.67k | const uint8_t *buf = h->gb.buffer; |
3754 | | |
3755 | 2.67k | if (h->divx_packed) { |
3756 | 53.2k | for (int i = 0; i < buf_size - 3; i++) { |
3757 | 52.3k | if (buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 1) { |
3758 | 1.66k | if (buf[i+3] == 0xB0) { |
3759 | 211 | av_log(h->c.avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n"); |
3760 | 211 | bitstream_buffer_size = 0; |
3761 | 211 | } |
3762 | 1.66k | break; |
3763 | 1.66k | } |
3764 | 52.3k | } |
3765 | 2.60k | } |
3766 | 2.67k | ctx->bitstream_buffer->size = 0; |
3767 | 2.67k | if (bitstream_buffer_size && (h->divx_packed || buf_size <= MAX_NVOP_SIZE)) {// divx 5.01+/xvid frame reorder |
3768 | 1.51k | int ret = init_get_bits8(&h->gb, ctx->bitstream_buffer->data, |
3769 | 1.51k | bitstream_buffer_size); |
3770 | 1.51k | if (ret < 0) |
3771 | 0 | return ret; |
3772 | 1.51k | } else |
3773 | 1.16k | av_buffer_unref(&ctx->bitstream_buffer); |
3774 | 2.67k | } |
3775 | | |
3776 | 317k | return ff_mpeg4_parse_picture_header(ctx, &h->gb, 0, 0); |
3777 | 317k | } |
3778 | | |
3779 | | int ff_mpeg4_frame_end(AVCodecContext *avctx, const AVPacket *pkt) |
3780 | 118k | { |
3781 | 118k | Mpeg4DecContext *ctx = avctx->priv_data; |
3782 | 118k | H263DecContext *const h = &ctx->h; |
3783 | 118k | int ret; |
3784 | | |
3785 | 118k | av_assert1(!ctx->bitstream_buffer || !ctx->bitstream_buffer->size); |
3786 | | |
3787 | | /* divx 5.01+ bitstream reorder stuff */ |
3788 | 118k | if (h->divx_packed) { |
3789 | 5.47k | int current_pos = ctx->bitstream_buffer && h->gb.buffer == ctx->bitstream_buffer->data ? 0 : (get_bits_count(&h->gb) >> 3); |
3790 | 5.47k | int startcode_found = 0; |
3791 | 5.47k | uint8_t *buf = pkt->data; |
3792 | 5.47k | int buf_size = pkt->size; |
3793 | | |
3794 | 5.47k | if (buf_size - current_pos > 7) { |
3795 | | |
3796 | 3.17k | int i; |
3797 | 202k | for (i = current_pos; i < buf_size - 4; i++) |
3798 | | |
3799 | 201k | if (buf[i] == 0 && |
3800 | 24.7k | buf[i + 1] == 0 && |
3801 | 14.5k | buf[i + 2] == 1 && |
3802 | 3.50k | buf[i + 3] == 0xB6) { |
3803 | 2.22k | startcode_found = !(buf[i + 4] & 0x40); |
3804 | 2.22k | break; |
3805 | 2.22k | } |
3806 | 3.17k | } |
3807 | | |
3808 | 5.47k | if (startcode_found) { |
3809 | 1.83k | if (!ctx->showed_packed_warning) { |
3810 | 111 | av_log(h->c.avctx, AV_LOG_INFO, "Video uses a non-standard and " |
3811 | 111 | "wasteful way to store B-frames ('packed B-frames'). " |
3812 | 111 | "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n"); |
3813 | 111 | ctx->showed_packed_warning = 1; |
3814 | 111 | } |
3815 | 1.83k | ret = av_buffer_replace(&ctx->bitstream_buffer, pkt->buf); |
3816 | 1.83k | if (ret < 0) |
3817 | 0 | return ret; |
3818 | | |
3819 | 1.83k | ctx->bitstream_buffer->data = buf + current_pos; |
3820 | 1.83k | ctx->bitstream_buffer->size = buf_size - current_pos; |
3821 | 1.83k | } |
3822 | 5.47k | } |
3823 | | |
3824 | 118k | return 0; |
3825 | 118k | } |
3826 | | |
3827 | | #if HAVE_THREADS |
3828 | | static av_cold void clear_context(MpegEncContext *s) |
3829 | 0 | { |
3830 | 0 | memset(&s->buffer_pools, 0, sizeof(s->buffer_pools)); |
3831 | 0 | memset(&s->next_pic, 0, sizeof(s->next_pic)); |
3832 | 0 | memset(&s->last_pic, 0, sizeof(s->last_pic)); |
3833 | 0 | memset(&s->cur_pic, 0, sizeof(s->cur_pic)); |
3834 | |
|
3835 | 0 | memset(s->thread_context, 0, sizeof(s->thread_context)); |
3836 | |
|
3837 | 0 | s->ac_val_base = NULL; |
3838 | 0 | s->ac_val = NULL; |
3839 | 0 | memset(&s->sc, 0, sizeof(s->sc)); |
3840 | |
|
3841 | 0 | s->p_field_mv_table_base = NULL; |
3842 | 0 | for (int i = 0; i < 2; i++) |
3843 | 0 | for (int j = 0; j < 2; j++) |
3844 | 0 | s->p_field_mv_table[i][j] = NULL; |
3845 | |
|
3846 | 0 | s->dc_val_base = NULL; |
3847 | 0 | s->coded_block_base = NULL; |
3848 | 0 | s->mbintra_table = NULL; |
3849 | 0 | s->cbp_table = NULL; |
3850 | 0 | s->pred_dir_table = NULL; |
3851 | |
|
3852 | 0 | s->mbskip_table = NULL; |
3853 | |
|
3854 | 0 | s->er.error_status_table = NULL; |
3855 | 0 | s->er.er_temp_buffer = NULL; |
3856 | 0 | s->mb_index2xy = NULL; |
3857 | |
|
3858 | 0 | s->context_initialized = 0; |
3859 | 0 | s->context_reinit = 0; |
3860 | 0 | } |
3861 | | |
3862 | | static av_cold int update_mpvctx(MpegEncContext *s, const MpegEncContext *s1) |
3863 | 0 | { |
3864 | 0 | AVCodecContext *avctx = s->avctx; |
3865 | | // FIXME the following leads to a data race; instead copy only |
3866 | | // the necessary fields. |
3867 | 0 | memcpy(s, s1, sizeof(*s)); |
3868 | 0 | clear_context(s); |
3869 | |
|
3870 | 0 | s->avctx = avctx; |
3871 | |
|
3872 | 0 | if (s1->context_initialized) { |
3873 | 0 | int err = ff_mpv_common_init(s); |
3874 | 0 | if (err < 0) |
3875 | 0 | return err; |
3876 | 0 | } |
3877 | 0 | return 0; |
3878 | 0 | } |
3879 | | |
3880 | | static int mpeg4_update_thread_context(AVCodecContext *dst, |
3881 | | const AVCodecContext *src) |
3882 | 0 | { |
3883 | 0 | Mpeg4DecContext *s = dst->priv_data; |
3884 | 0 | const Mpeg4DecContext *s1 = src->priv_data; |
3885 | 0 | int init = s->h.c.context_initialized; |
3886 | 0 | int ret; |
3887 | |
|
3888 | 0 | if (!init) { |
3889 | 0 | ret = update_mpvctx(&s->h.c, &s1->h.c); |
3890 | 0 | if (ret < 0) |
3891 | 0 | return ret; |
3892 | 0 | } |
3893 | | |
3894 | 0 | ret = ff_mpeg_update_thread_context(dst, src); |
3895 | 0 | if (ret < 0) |
3896 | 0 | return ret; |
3897 | | |
3898 | | // copy all the necessary fields explicitly |
3899 | 0 | s->time_increment_bits = s1->time_increment_bits; |
3900 | 0 | s->shape = s1->shape; |
3901 | 0 | s->vol_sprite_usage = s1->vol_sprite_usage; |
3902 | 0 | s->sprite_brightness_change = s1->sprite_brightness_change; |
3903 | 0 | s->sprite_warping_accuracy = s1->sprite_warping_accuracy; |
3904 | 0 | s->num_sprite_warping_points = s1->num_sprite_warping_points; |
3905 | 0 | s->h.data_partitioning = s1->h.data_partitioning; |
3906 | 0 | s->mpeg_quant = s1->mpeg_quant; |
3907 | 0 | s->rvlc = s1->rvlc; |
3908 | 0 | s->resync_marker = s1->resync_marker; |
3909 | 0 | s->t_frame = s1->t_frame; |
3910 | 0 | s->new_pred = s1->new_pred; |
3911 | 0 | s->enhancement_type = s1->enhancement_type; |
3912 | 0 | s->scalability = s1->scalability; |
3913 | 0 | s->intra_dc_threshold = s1->intra_dc_threshold; |
3914 | 0 | s->h.divx_packed = s1->h.divx_packed; |
3915 | 0 | s->divx_version = s1->divx_version; |
3916 | 0 | s->divx_build = s1->divx_build; |
3917 | 0 | s->xvid_build = s1->xvid_build; |
3918 | 0 | s->lavc_build = s1->lavc_build; |
3919 | 0 | s->vo_type = s1->vo_type; |
3920 | 0 | s->showed_packed_warning = s1->showed_packed_warning; |
3921 | 0 | s->vol_control_parameters = s1->vol_control_parameters; |
3922 | 0 | s->cplx_estimation_trash_i = s1->cplx_estimation_trash_i; |
3923 | 0 | s->cplx_estimation_trash_p = s1->cplx_estimation_trash_p; |
3924 | 0 | s->cplx_estimation_trash_b = s1->cplx_estimation_trash_b; |
3925 | 0 | s->rgb = s1->rgb; |
3926 | 0 | s->h.c.studio_profile = s1->h.c.studio_profile; |
3927 | |
|
3928 | 0 | s->h.skipped_last_frame = s1->h.skipped_last_frame; |
3929 | 0 | s->h.padding_bug_score = s1->h.padding_bug_score; // FIXME: racy |
3930 | |
|
3931 | 0 | s->h.picture_number = s1->h.picture_number; |
3932 | |
|
3933 | 0 | memcpy(s->sprite_shift, s1->sprite_shift, sizeof(s1->sprite_shift)); |
3934 | 0 | memcpy(s->sprite_traj, s1->sprite_traj, sizeof(s1->sprite_traj)); |
3935 | |
|
3936 | 0 | return av_buffer_replace(&s->bitstream_buffer, s1->bitstream_buffer); |
3937 | 0 | } |
3938 | | |
3939 | | static int mpeg4_update_thread_context_for_user(AVCodecContext *dst, |
3940 | | const AVCodecContext *src) |
3941 | 0 | { |
3942 | 0 | H263DecContext *const h = dst->priv_data; |
3943 | 0 | const H263DecContext *const h1 = src->priv_data; |
3944 | |
|
3945 | 0 | h->c.quarter_sample = h1->c.quarter_sample; |
3946 | 0 | h->divx_packed = h1->divx_packed; |
3947 | |
|
3948 | 0 | return 0; |
3949 | 0 | } |
3950 | | #endif |
3951 | | |
3952 | | static av_cold void mpeg4_init_static(void) |
3953 | 1 | { |
3954 | 1 | static VLCElem vlc_buf[6498]; |
3955 | 1 | VLCInitState state = VLC_INIT_STATE(vlc_buf); |
3956 | | |
3957 | 1 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(studio_luma_dc, STUDIO_INTRA_BITS, 19, |
3958 | 1 | &ff_mpeg4_studio_dc_luma[0][1], 2, |
3959 | 1 | &ff_mpeg4_studio_dc_luma[0][0], 2, 1, |
3960 | 1 | 0, 0); |
3961 | | |
3962 | 1 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(studio_chroma_dc, STUDIO_INTRA_BITS, 19, |
3963 | 1 | &ff_mpeg4_studio_dc_chroma[0][1], 2, |
3964 | 1 | &ff_mpeg4_studio_dc_chroma[0][0], 2, 1, |
3965 | 1 | 0, 0); |
3966 | | |
3967 | 13 | for (unsigned i = 0; i < 12; i++) { |
3968 | 12 | studio_intra_tab[i] = |
3969 | 12 | ff_vlc_init_tables_from_lengths(&state, STUDIO_INTRA_BITS, 24, |
3970 | 12 | &ff_mpeg4_studio_intra[i][0][1], 2, |
3971 | 12 | &ff_mpeg4_studio_intra[i][0][0], 2, 1, |
3972 | 12 | 0, 0); |
3973 | 12 | } |
3974 | | |
3975 | 1 | static uint8_t mpeg4_rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3]; |
3976 | 1 | ff_rl_init(&ff_mpeg4_rl_intra, mpeg4_rl_intra_table); |
3977 | 1 | ff_h263_init_rl_inter(); |
3978 | | |
3979 | 1 | INIT_FIRST_VLC_RL(ff_mpeg4_rl_intra, 554); |
3980 | 1 | VLC_INIT_RL(ff_rvlc_rl_inter, 1072); |
3981 | 1 | INIT_FIRST_VLC_RL(ff_rvlc_rl_intra, 1072); |
3982 | 1 | VLC_INIT_STATIC_TABLE(dc_lum, DC_VLC_BITS, 10 /* 13 */, |
3983 | 1 | &ff_mpeg4_DCtab_lum[0][1], 2, 1, |
3984 | 1 | &ff_mpeg4_DCtab_lum[0][0], 2, 1, 0); |
3985 | 1 | VLC_INIT_STATIC_TABLE(dc_chrom, DC_VLC_BITS, 10 /* 13 */, |
3986 | 1 | &ff_mpeg4_DCtab_chrom[0][1], 2, 1, |
3987 | 1 | &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 0); |
3988 | 1 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15, |
3989 | 1 | ff_sprite_trajectory_lens, 1, |
3990 | 1 | NULL, 0, 0, 0, 0); |
3991 | 1 | VLC_INIT_STATIC_SPARSE_TABLE(mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4, |
3992 | 1 | &ff_mb_type_b_tab[0][1], 2, 1, |
3993 | 1 | &ff_mb_type_b_tab[0][0], 2, 1, |
3994 | 1 | mb_type_b_map, 2, 2, 0); |
3995 | 1 | } |
3996 | | |
3997 | | static av_cold int decode_init(AVCodecContext *avctx) |
3998 | 11.9k | { |
3999 | 11.9k | static AVOnce init_static_once = AV_ONCE_INIT; |
4000 | 11.9k | Mpeg4DecContext *ctx = avctx->priv_data; |
4001 | 11.9k | H263DecContext *const h = &ctx->h; |
4002 | 11.9k | MPVUnquantDSPContext unquant_dsp_ctx; |
4003 | 11.9k | int ret; |
4004 | | |
4005 | 11.9k | ctx->divx_version = |
4006 | 11.9k | ctx->divx_build = |
4007 | 11.9k | ctx->xvid_build = |
4008 | 11.9k | ctx->lavc_build = -1; |
4009 | | |
4010 | 11.9k | if ((ret = ff_h263_decode_init(avctx)) < 0) |
4011 | 0 | return ret; |
4012 | | |
4013 | 11.9k | ff_mpv_unquantize_init(&unquant_dsp_ctx, |
4014 | 11.9k | avctx->flags & AV_CODEC_FLAG_BITEXACT, 0); |
4015 | | |
4016 | 11.9k | ctx->dct_unquantize_h263_intra = unquant_dsp_ctx.dct_unquantize_h263_intra; |
4017 | 11.9k | ctx->dct_unquantize_mpeg2_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra; |
4018 | | // dct_unquantize_inter is only used with MPEG-2 quantizers, |
4019 | | // so that is all we keep. |
4020 | 11.9k | ctx->dct_unquantize_mpeg2_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter; |
4021 | | |
4022 | 11.9k | h->c.y_dc_scale_table = ff_mpeg4_y_dc_scale_table; |
4023 | 11.9k | h->c.c_dc_scale_table = ff_mpeg4_c_dc_scale_table; |
4024 | | |
4025 | 11.9k | h->c.h263_pred = 1; |
4026 | 11.9k | h->c.low_delay = 0; /* default, might be overridden in the vol header during header parsing */ |
4027 | 11.9k | h->decode_header = mpeg4_decode_picture_header; |
4028 | 11.9k | h->decode_mb = mpeg4_decode_mb; |
4029 | 11.9k | ctx->time_increment_bits = 4; /* default value for broken headers */ |
4030 | 11.9k | ctx->quant_precision = 5; |
4031 | | |
4032 | 11.9k | avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; |
4033 | | |
4034 | 11.9k | ff_qpeldsp_init(&h->c.qdsp); |
4035 | 11.9k | ff_mpeg4videodsp_init(&ctx->mdsp); |
4036 | | |
4037 | 11.9k | ff_thread_once(&init_static_once, mpeg4_init_static); |
4038 | | |
4039 | | /* Must be after initializing the MPEG-4 static tables */ |
4040 | 11.9k | if (avctx->extradata_size && !avctx->internal->is_copy) { |
4041 | 179 | GetBitContext gb; |
4042 | | |
4043 | 179 | if (init_get_bits8(&gb, avctx->extradata, avctx->extradata_size) >= 0) |
4044 | 179 | ff_mpeg4_parse_picture_header(ctx, &gb, 1, 0); |
4045 | 179 | } |
4046 | | |
4047 | 11.9k | return 0; |
4048 | 11.9k | } |
4049 | | |
4050 | | static av_cold void mpeg4_flush(AVCodecContext *avctx) |
4051 | 118k | { |
4052 | 118k | Mpeg4DecContext *const ctx = avctx->priv_data; |
4053 | | |
4054 | 118k | av_buffer_unref(&ctx->bitstream_buffer); |
4055 | 118k | ff_mpeg_flush(avctx); |
4056 | 118k | } |
4057 | | |
4058 | | static av_cold int mpeg4_close(AVCodecContext *avctx) |
4059 | 11.9k | { |
4060 | 11.9k | Mpeg4DecContext *const ctx = avctx->priv_data; |
4061 | | |
4062 | 11.9k | av_buffer_unref(&ctx->bitstream_buffer); |
4063 | | |
4064 | 11.9k | return ff_mpv_decode_close(avctx); |
4065 | 11.9k | } |
4066 | | |
4067 | | #define OFFSET(x) offsetof(H263DecContext, x) |
4068 | | #define FLAGS AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY |
4069 | | static const AVOption mpeg4_options[] = { |
4070 | | {"quarter_sample", "1/4 subpel MC", OFFSET(c.quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS}, |
4071 | | {"divx_packed", "divx style packed b frames", OFFSET(divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS}, |
4072 | | {NULL} |
4073 | | }; |
4074 | | |
4075 | | static const AVClass mpeg4_class = { |
4076 | | .class_name = "MPEG4 Video Decoder", |
4077 | | .item_name = av_default_item_name, |
4078 | | .option = mpeg4_options, |
4079 | | .version = LIBAVUTIL_VERSION_INT, |
4080 | | }; |
4081 | | |
4082 | | const FFCodec ff_mpeg4_decoder = { |
4083 | | .p.name = "mpeg4", |
4084 | | CODEC_LONG_NAME("MPEG-4 part 2"), |
4085 | | .p.type = AVMEDIA_TYPE_VIDEO, |
4086 | | .p.id = AV_CODEC_ID_MPEG4, |
4087 | | .priv_data_size = sizeof(Mpeg4DecContext), |
4088 | | .init = decode_init, |
4089 | | FF_CODEC_DECODE_CB(ff_h263_decode_frame), |
4090 | | .close = mpeg4_close, |
4091 | | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 | |
4092 | | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_FRAME_THREADS, |
4093 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | |
4094 | | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, |
4095 | | .flush = mpeg4_flush, |
4096 | | .p.max_lowres = 3, |
4097 | | .p.profiles = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles), |
4098 | | UPDATE_THREAD_CONTEXT(mpeg4_update_thread_context), |
4099 | | UPDATE_THREAD_CONTEXT_FOR_USER(mpeg4_update_thread_context_for_user), |
4100 | | .p.priv_class = &mpeg4_class, |
4101 | | .hw_configs = (const AVCodecHWConfigInternal *const []) { |
4102 | | #if CONFIG_MPEG4_NVDEC_HWACCEL |
4103 | | HWACCEL_NVDEC(mpeg4), |
4104 | | #endif |
4105 | | #if CONFIG_MPEG4_NVDEC_CUARRAY_HWACCEL |
4106 | | HWACCEL_NVDEC_CUARRAY(mpeg4), |
4107 | | #endif |
4108 | | #if CONFIG_MPEG4_VAAPI_HWACCEL |
4109 | | HWACCEL_VAAPI(mpeg4), |
4110 | | #endif |
4111 | | #if CONFIG_MPEG4_VDPAU_HWACCEL |
4112 | | HWACCEL_VDPAU(mpeg4), |
4113 | | #endif |
4114 | | #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL |
4115 | | HWACCEL_VIDEOTOOLBOX(mpeg4), |
4116 | | #endif |
4117 | | NULL |
4118 | | }, |
4119 | | }; |
4120 | | #endif /* CONFIG_MPEG4_DECODER */ |