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 "libavutil/internal.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "error_resilience.h"
29 #include "hwconfig.h"
30 #include "idctdsp.h"
31 #include "internal.h"
32 #include "mpegutils.h"
33 #include "mpegvideo.h"
34 #include "mpegvideodata.h"
35 #include "mpeg4video.h"
36 #include "h263.h"
37 #include "profiles.h"
38 #include "thread.h"
39 #include "xvididct.h"
40 #include "unary.h"
41
42 /* The defines below define the number of bits that are read at once for
43 * reading vlc values. Changing these may improve speed and data cache needs
44 * be aware though that decreasing them may need the number of stages that is
45 * passed to get_vlc* to be increased. */
46 #define SPRITE_TRAJ_VLC_BITS 6
47 #define DC_VLC_BITS 9
48 #define MB_TYPE_B_VLC_BITS 4
49 #define STUDIO_INTRA_BITS 9
50
51 static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb);
52
53 static VLC dc_lum, dc_chrom;
54 static VLC sprite_trajectory;
55 static VLC mb_type_b_vlc;
56
57 static const int mb_type_b_map[4] = {
58 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
59 MB_TYPE_L0L1 | MB_TYPE_16x16,
60 MB_TYPE_L1 | MB_TYPE_16x16,
61 MB_TYPE_L0 | MB_TYPE_16x16,
62 };
63
64 /**
65 * Predict the ac.
66 * @param n block index (0-3 are luma, 4-5 are chroma)
67 * @param dir the ac prediction direction
68 */
ff_mpeg4_pred_ac(MpegEncContext * s,int16_t * block,int n,int dir)69 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
70 {
71 int i;
72 int16_t *ac_val, *ac_val1;
73 int8_t *const qscale_table = s->current_picture.qscale_table;
74
75 /* find prediction */
76 ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
77 ac_val1 = ac_val;
78 if (s->ac_pred) {
79 if (dir == 0) {
80 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
81 /* left prediction */
82 ac_val -= 16;
83
84 if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
85 n == 1 || n == 3) {
86 /* same qscale */
87 for (i = 1; i < 8; i++)
88 block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
89 } else {
90 /* different qscale, we must rescale */
91 for (i = 1; i < 8; i++)
92 block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
93 }
94 } else {
95 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
96 /* top prediction */
97 ac_val -= 16 * s->block_wrap[n];
98
99 if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
100 n == 2 || n == 3) {
101 /* same qscale */
102 for (i = 1; i < 8; i++)
103 block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
104 } else {
105 /* different qscale, we must rescale */
106 for (i = 1; i < 8; i++)
107 block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
108 }
109 }
110 }
111 /* left copy */
112 for (i = 1; i < 8; i++)
113 ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
114
115 /* top copy */
116 for (i = 1; i < 8; i++)
117 ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
118 }
119
120 /**
121 * check if the next stuff is a resync marker or the end.
122 * @return 0 if not
123 */
mpeg4_is_resync(Mpeg4DecContext * ctx)124 static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
125 {
126 MpegEncContext *s = &ctx->m;
127 int bits_count = get_bits_count(&s->gb);
128 int v = show_bits(&s->gb, 16);
129
130 if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
131 return 0;
132
133 while (v <= 0xFF) {
134 if (s->pict_type == AV_PICTURE_TYPE_B ||
135 (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
136 break;
137 skip_bits(&s->gb, 8 + s->pict_type);
138 bits_count += 8 + s->pict_type;
139 v = show_bits(&s->gb, 16);
140 }
141
142 if (bits_count + 8 >= s->gb.size_in_bits) {
143 v >>= 8;
144 v |= 0x7F >> (7 - (bits_count & 7));
145
146 if (v == 0x7F)
147 return s->mb_num;
148 } else {
149 if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
150 int len, mb_num;
151 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
152 GetBitContext gb = s->gb;
153
154 skip_bits(&s->gb, 1);
155 align_get_bits(&s->gb);
156
157 for (len = 0; len < 32; len++)
158 if (get_bits1(&s->gb))
159 break;
160
161 mb_num = get_bits(&s->gb, mb_num_bits);
162 if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
163 mb_num= -1;
164
165 s->gb = gb;
166
167 if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
168 return mb_num;
169 }
170 }
171 return 0;
172 }
173
mpeg4_decode_sprite_trajectory(Mpeg4DecContext * ctx,GetBitContext * gb)174 static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
175 {
176 MpegEncContext *s = &ctx->m;
177 int a = 2 << s->sprite_warping_accuracy;
178 int rho = 3 - s->sprite_warping_accuracy;
179 int r = 16 / a;
180 int alpha = 1;
181 int beta = 0;
182 int w = s->width;
183 int h = s->height;
184 int min_ab, i, w2, h2, w3, h3;
185 int sprite_ref[4][2];
186 int virtual_ref[2][2];
187 int64_t sprite_offset[2][2];
188 int64_t sprite_delta[2][2];
189
190 // only true for rectangle shapes
191 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
192 { 0, s->height }, { s->width, s->height } };
193 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
194
195 if (w <= 0 || h <= 0)
196 return AVERROR_INVALIDDATA;
197
198 /* the decoder was not properly initialized and we cannot continue */
199 if (sprite_trajectory.table == NULL)
200 return AVERROR_INVALIDDATA;
201
202 for (i = 0; i < ctx->num_sprite_warping_points; i++) {
203 int length;
204 int x = 0, y = 0;
205
206 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
207 if (length > 0)
208 x = get_xbits(gb, length);
209
210 if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
211 check_marker(s->avctx, gb, "before sprite_trajectory");
212
213 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
214 if (length > 0)
215 y = get_xbits(gb, length);
216
217 check_marker(s->avctx, gb, "after sprite_trajectory");
218 ctx->sprite_traj[i][0] = d[i][0] = x;
219 ctx->sprite_traj[i][1] = d[i][1] = y;
220 }
221 for (; i < 4; i++)
222 ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
223
224 while ((1 << alpha) < w)
225 alpha++;
226 while ((1 << beta) < h)
227 beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
228 w2 = 1 << alpha;
229 h2 = 1 << beta;
230
231 // Note, the 4th point isn't used for GMC
232 if (ctx->divx_version == 500 && ctx->divx_build == 413) {
233 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
234 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
235 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
236 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
237 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
238 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
239 } else {
240 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
241 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
242 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
243 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
244 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
245 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
246 }
247 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
248 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
249
250 /* This is mostly identical to the MPEG-4 std (and is totally unreadable
251 * because of that...). Perhaps it should be reordered to be more readable.
252 * The idea behind this virtual_ref mess is to be able to use shifts later
253 * per pixel instead of divides so the distance between points is converted
254 * from w&h based to w2&h2 based which are of the 2^x form. */
255 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
256 ROUNDED_DIV(((w - w2) *
257 (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
258 w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
259 virtual_ref[0][1] = 16 * vop_ref[0][1] +
260 ROUNDED_DIV(((w - w2) *
261 (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
262 w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
263 virtual_ref[1][0] = 16 * vop_ref[0][0] +
264 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
265 h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
266 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
267 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
268 h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
269
270 switch (ctx->num_sprite_warping_points) {
271 case 0:
272 sprite_offset[0][0] =
273 sprite_offset[0][1] =
274 sprite_offset[1][0] =
275 sprite_offset[1][1] = 0;
276 sprite_delta[0][0] = a;
277 sprite_delta[0][1] =
278 sprite_delta[1][0] = 0;
279 sprite_delta[1][1] = a;
280 ctx->sprite_shift[0] =
281 ctx->sprite_shift[1] = 0;
282 break;
283 case 1: // GMC only
284 sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
285 sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
286 sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
287 a * (vop_ref[0][0] / 2);
288 sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
289 a * (vop_ref[0][1] / 2);
290 sprite_delta[0][0] = a;
291 sprite_delta[0][1] =
292 sprite_delta[1][0] = 0;
293 sprite_delta[1][1] = a;
294 ctx->sprite_shift[0] =
295 ctx->sprite_shift[1] = 0;
296 break;
297 case 2:
298 sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
299 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
300 ((int64_t) -vop_ref[0][0]) +
301 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
302 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
303 sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
304 ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
305 ((int64_t) -vop_ref[0][0]) +
306 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
307 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
308 sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
309 ((int64_t)-2 * vop_ref[0][0] + 1) +
310 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
311 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
312 (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
313 sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
314 ((int64_t)-2 * vop_ref[0][0] + 1) +
315 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
316 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
317 (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
318 sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
319 sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
320 sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
321 sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
322
323 ctx->sprite_shift[0] = alpha + rho;
324 ctx->sprite_shift[1] = alpha + rho + 2;
325 break;
326 case 3:
327 min_ab = FFMIN(alpha, beta);
328 w3 = w2 >> min_ab;
329 h3 = h2 >> min_ab;
330 sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
331 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
332 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
333 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
334 sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
335 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
336 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
337 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
338 sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
339 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
340 (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
341 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
342 sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
343 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
344 (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
345 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
346 sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
347 sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
348 sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
349 sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
350
351 ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
352 ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
353 break;
354 }
355 /* try to simplify the situation */
356 if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
357 sprite_delta[0][1] == 0 &&
358 sprite_delta[1][0] == 0 &&
359 sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
360 sprite_offset[0][0] >>= ctx->sprite_shift[0];
361 sprite_offset[0][1] >>= ctx->sprite_shift[0];
362 sprite_offset[1][0] >>= ctx->sprite_shift[1];
363 sprite_offset[1][1] >>= ctx->sprite_shift[1];
364 sprite_delta[0][0] = a;
365 sprite_delta[0][1] = 0;
366 sprite_delta[1][0] = 0;
367 sprite_delta[1][1] = a;
368 ctx->sprite_shift[0] = 0;
369 ctx->sprite_shift[1] = 0;
370 s->real_sprite_warping_points = 1;
371 } else {
372 int shift_y = 16 - ctx->sprite_shift[0];
373 int shift_c = 16 - ctx->sprite_shift[1];
374
375 for (i = 0; i < 2; i++) {
376 if (shift_c < 0 || shift_y < 0 ||
377 FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
378 FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
379 FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
380 FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
381 ) {
382 avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
383 goto overflow;
384 }
385 }
386
387 for (i = 0; i < 2; i++) {
388 sprite_offset[0][i] *= 1 << shift_y;
389 sprite_offset[1][i] *= 1 << shift_c;
390 sprite_delta[0][i] *= 1 << shift_y;
391 sprite_delta[1][i] *= 1 << shift_y;
392 ctx->sprite_shift[i] = 16;
393
394 }
395 for (i = 0; i < 2; i++) {
396 int64_t sd[2] = {
397 sprite_delta[i][0] - a * (1LL<<16),
398 sprite_delta[i][1] - a * (1LL<<16)
399 };
400
401 if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
402 llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
403 llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
404 llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
405 llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
406 llabs(sd[0]) >= INT_MAX ||
407 llabs(sd[1]) >= INT_MAX ||
408 llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
409 llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
410 llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
411 ) {
412 avpriv_request_sample(s->avctx, "Overflow on sprite points");
413 goto overflow;
414 }
415 }
416 s->real_sprite_warping_points = ctx->num_sprite_warping_points;
417 }
418
419 for (i = 0; i < 4; i++) {
420 s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
421 s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
422 }
423
424 return 0;
425 overflow:
426 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
427 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
428 return AVERROR_PATCHWELCOME;
429 }
430
decode_new_pred(Mpeg4DecContext * ctx,GetBitContext * gb)431 static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
432 MpegEncContext *s = &ctx->m;
433 int len = FFMIN(ctx->time_increment_bits + 3, 15);
434
435 get_bits(gb, len);
436 if (get_bits1(gb))
437 get_bits(gb, len);
438 check_marker(s->avctx, gb, "after new_pred");
439
440 return 0;
441 }
442
443 /**
444 * Decode the next video packet.
445 * @return <0 if something went wrong
446 */
ff_mpeg4_decode_video_packet_header(Mpeg4DecContext * ctx)447 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
448 {
449 MpegEncContext *s = &ctx->m;
450
451 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
452 int header_extension = 0, mb_num, len;
453
454 /* is there enough space left for a video packet + header */
455 if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
456 return AVERROR_INVALIDDATA;
457
458 for (len = 0; len < 32; len++)
459 if (get_bits1(&s->gb))
460 break;
461
462 if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
463 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
464 return AVERROR_INVALIDDATA;
465 }
466
467 if (ctx->shape != RECT_SHAPE) {
468 header_extension = get_bits1(&s->gb);
469 // FIXME more stuff here
470 }
471
472 mb_num = get_bits(&s->gb, mb_num_bits);
473 if (mb_num >= s->mb_num || !mb_num) {
474 av_log(s->avctx, AV_LOG_ERROR,
475 "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
476 return AVERROR_INVALIDDATA;
477 }
478
479 s->mb_x = mb_num % s->mb_width;
480 s->mb_y = mb_num / s->mb_width;
481
482 if (ctx->shape != BIN_ONLY_SHAPE) {
483 int qscale = get_bits(&s->gb, s->quant_precision);
484 if (qscale)
485 s->chroma_qscale = s->qscale = qscale;
486 }
487
488 if (ctx->shape == RECT_SHAPE)
489 header_extension = get_bits1(&s->gb);
490
491 if (header_extension) {
492 int time_incr = 0;
493
494 while (get_bits1(&s->gb) != 0)
495 time_incr++;
496
497 check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
498 skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
499 check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
500
501 skip_bits(&s->gb, 2); /* vop coding type */
502 // FIXME not rect stuff here
503
504 if (ctx->shape != BIN_ONLY_SHAPE) {
505 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
506 // FIXME don't just ignore everything
507 if (s->pict_type == AV_PICTURE_TYPE_S &&
508 ctx->vol_sprite_usage == GMC_SPRITE) {
509 if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
510 return AVERROR_INVALIDDATA;
511 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
512 }
513
514 // FIXME reduced res stuff here
515
516 if (s->pict_type != AV_PICTURE_TYPE_I) {
517 int f_code = get_bits(&s->gb, 3); /* fcode_for */
518 if (f_code == 0)
519 av_log(s->avctx, AV_LOG_ERROR,
520 "Error, video packet header damaged (f_code=0)\n");
521 }
522 if (s->pict_type == AV_PICTURE_TYPE_B) {
523 int b_code = get_bits(&s->gb, 3);
524 if (b_code == 0)
525 av_log(s->avctx, AV_LOG_ERROR,
526 "Error, video packet header damaged (b_code=0)\n");
527 }
528 }
529 }
530 if (ctx->new_pred)
531 decode_new_pred(ctx, &s->gb);
532
533 return 0;
534 }
535
reset_studio_dc_predictors(MpegEncContext * s)536 static void reset_studio_dc_predictors(MpegEncContext *s)
537 {
538 /* Reset DC Predictors */
539 s->last_dc[0] =
540 s->last_dc[1] =
541 s->last_dc[2] = 1 << (s->avctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1);
542 }
543
544 /**
545 * Decode the next video packet.
546 * @return <0 if something went wrong
547 */
ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext * ctx)548 int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
549 {
550 MpegEncContext *s = &ctx->m;
551 GetBitContext *gb = &s->gb;
552 unsigned vlc_len;
553 uint16_t mb_num;
554
555 if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_START_CODE) {
556 vlc_len = av_log2(s->mb_width * s->mb_height) + 1;
557 mb_num = get_bits(gb, vlc_len);
558
559 if (mb_num >= s->mb_num)
560 return AVERROR_INVALIDDATA;
561
562 s->mb_x = mb_num % s->mb_width;
563 s->mb_y = mb_num / s->mb_width;
564
565 if (ctx->shape != BIN_ONLY_SHAPE)
566 s->qscale = mpeg_get_qscale(s);
567
568 if (get_bits1(gb)) { /* slice_extension_flag */
569 skip_bits1(gb); /* intra_slice */
570 skip_bits1(gb); /* slice_VOP_id_enable */
571 skip_bits(gb, 6); /* slice_VOP_id */
572 while (get_bits1(gb)) /* extra_bit_slice */
573 skip_bits(gb, 8); /* extra_information_slice */
574 }
575
576 reset_studio_dc_predictors(s);
577 }
578 else {
579 return AVERROR_INVALIDDATA;
580 }
581
582 return 0;
583 }
584
585 /**
586 * Get the average motion vector for a GMC MB.
587 * @param n either 0 for the x component or 1 for y
588 * @return the average MV for a GMC MB
589 */
get_amv(Mpeg4DecContext * ctx,int n)590 static inline int get_amv(Mpeg4DecContext *ctx, int n)
591 {
592 MpegEncContext *s = &ctx->m;
593 int x, y, mb_v, sum, dx, dy, shift;
594 int len = 1 << (s->f_code + 4);
595 const int a = s->sprite_warping_accuracy;
596
597 if (s->workaround_bugs & FF_BUG_AMV)
598 len >>= s->quarter_sample;
599
600 if (s->real_sprite_warping_points == 1) {
601 if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample)
602 sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
603 else
604 sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
605 } else {
606 dx = s->sprite_delta[n][0];
607 dy = s->sprite_delta[n][1];
608 shift = ctx->sprite_shift[0];
609 if (n)
610 dy -= 1 << (shift + a + 1);
611 else
612 dx -= 1 << (shift + a + 1);
613 mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U;
614
615 sum = 0;
616 for (y = 0; y < 16; y++) {
617 int v;
618
619 v = mb_v + dy * y;
620 // FIXME optimize
621 for (x = 0; x < 16; x++) {
622 sum += v >> shift;
623 v += dx;
624 }
625 }
626 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
627 }
628
629 if (sum < -len)
630 sum = -len;
631 else if (sum >= len)
632 sum = len - 1;
633
634 return sum;
635 }
636
637 /**
638 * Decode the dc value.
639 * @param n block index (0-3 are luma, 4-5 are chroma)
640 * @param dir_ptr the prediction direction will be stored here
641 * @return the quantized dc
642 */
mpeg4_decode_dc(MpegEncContext * s,int n,int * dir_ptr)643 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
644 {
645 int level, code;
646
647 if (n < 4)
648 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
649 else
650 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
651
652 if (code < 0 || code > 9 /* && s->nbit < 9 */) {
653 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
654 return AVERROR_INVALIDDATA;
655 }
656
657 if (code == 0) {
658 level = 0;
659 } else {
660 if (IS_3IV1) {
661 if (code == 1)
662 level = 2 * get_bits1(&s->gb) - 1;
663 else {
664 if (get_bits1(&s->gb))
665 level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
666 else
667 level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
668 }
669 } else {
670 level = get_xbits(&s->gb, code);
671 }
672
673 if (code > 8) {
674 if (get_bits1(&s->gb) == 0) { /* marker */
675 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
676 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
677 return AVERROR_INVALIDDATA;
678 }
679 }
680 }
681 }
682
683 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
684 }
685
686 /**
687 * Decode first partition.
688 * @return number of MBs decoded or <0 if an error occurred
689 */
mpeg4_decode_partition_a(Mpeg4DecContext * ctx)690 static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
691 {
692 MpegEncContext *s = &ctx->m;
693 int mb_num = 0;
694 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
695
696 /* decode first partition */
697 s->first_slice_line = 1;
698 for (; s->mb_y < s->mb_height; s->mb_y++) {
699 ff_init_block_index(s);
700 for (; s->mb_x < s->mb_width; s->mb_x++) {
701 const int xy = s->mb_x + s->mb_y * s->mb_stride;
702 int cbpc;
703 int dir = 0;
704
705 mb_num++;
706 ff_update_block_index(s);
707 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
708 s->first_slice_line = 0;
709
710 if (s->pict_type == AV_PICTURE_TYPE_I) {
711 int i;
712
713 do {
714 if (show_bits(&s->gb, 19) == DC_MARKER)
715 return mb_num - 1;
716
717 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
718 if (cbpc < 0) {
719 av_log(s->avctx, AV_LOG_ERROR,
720 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
721 return AVERROR_INVALIDDATA;
722 }
723 } while (cbpc == 8);
724
725 s->cbp_table[xy] = cbpc & 3;
726 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
727 s->mb_intra = 1;
728
729 if (cbpc & 4)
730 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
731
732 s->current_picture.qscale_table[xy] = s->qscale;
733
734 s->mbintra_table[xy] = 1;
735 for (i = 0; i < 6; i++) {
736 int dc_pred_dir;
737 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
738 if (dc < 0) {
739 av_log(s->avctx, AV_LOG_ERROR,
740 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
741 return dc;
742 }
743 dir <<= 1;
744 if (dc_pred_dir)
745 dir |= 1;
746 }
747 s->pred_dir_table[xy] = dir;
748 } else { /* P/S_TYPE */
749 int mx, my, pred_x, pred_y, bits;
750 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
751 const int stride = s->b8_stride * 2;
752
753 try_again:
754 bits = show_bits(&s->gb, 17);
755 if (bits == MOTION_MARKER)
756 return mb_num - 1;
757
758 skip_bits1(&s->gb);
759 if (bits & 0x10000) {
760 /* skip mb */
761 if (s->pict_type == AV_PICTURE_TYPE_S &&
762 ctx->vol_sprite_usage == GMC_SPRITE) {
763 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
764 MB_TYPE_16x16 |
765 MB_TYPE_GMC |
766 MB_TYPE_L0;
767 mx = get_amv(ctx, 0);
768 my = get_amv(ctx, 1);
769 } else {
770 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
771 MB_TYPE_16x16 |
772 MB_TYPE_L0;
773 mx = my = 0;
774 }
775 mot_val[0] =
776 mot_val[2] =
777 mot_val[0 + stride] =
778 mot_val[2 + stride] = mx;
779 mot_val[1] =
780 mot_val[3] =
781 mot_val[1 + stride] =
782 mot_val[3 + stride] = my;
783
784 if (s->mbintra_table[xy])
785 ff_clean_intra_table_entries(s);
786 continue;
787 }
788
789 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
790 if (cbpc < 0) {
791 av_log(s->avctx, AV_LOG_ERROR,
792 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
793 return AVERROR_INVALIDDATA;
794 }
795 if (cbpc == 20)
796 goto try_again;
797
798 s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
799
800 s->mb_intra = ((cbpc & 4) != 0);
801
802 if (s->mb_intra) {
803 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
804 s->mbintra_table[xy] = 1;
805 mot_val[0] =
806 mot_val[2] =
807 mot_val[0 + stride] =
808 mot_val[2 + stride] = 0;
809 mot_val[1] =
810 mot_val[3] =
811 mot_val[1 + stride] =
812 mot_val[3 + stride] = 0;
813 } else {
814 if (s->mbintra_table[xy])
815 ff_clean_intra_table_entries(s);
816
817 if (s->pict_type == AV_PICTURE_TYPE_S &&
818 ctx->vol_sprite_usage == GMC_SPRITE &&
819 (cbpc & 16) == 0)
820 s->mcsel = get_bits1(&s->gb);
821 else
822 s->mcsel = 0;
823
824 if ((cbpc & 16) == 0) {
825 /* 16x16 motion prediction */
826
827 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
828 if (!s->mcsel) {
829 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
830 if (mx >= 0xffff)
831 return AVERROR_INVALIDDATA;
832
833 my = ff_h263_decode_motion(s, pred_y, s->f_code);
834 if (my >= 0xffff)
835 return AVERROR_INVALIDDATA;
836 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
837 MB_TYPE_L0;
838 } else {
839 mx = get_amv(ctx, 0);
840 my = get_amv(ctx, 1);
841 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
842 MB_TYPE_GMC |
843 MB_TYPE_L0;
844 }
845
846 mot_val[0] =
847 mot_val[2] =
848 mot_val[0 + stride] =
849 mot_val[2 + stride] = mx;
850 mot_val[1] =
851 mot_val[3] =
852 mot_val[1 + stride] =
853 mot_val[3 + stride] = my;
854 } else {
855 int i;
856 s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
857 MB_TYPE_L0;
858 for (i = 0; i < 4; i++) {
859 int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
860 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
861 if (mx >= 0xffff)
862 return AVERROR_INVALIDDATA;
863
864 my = ff_h263_decode_motion(s, pred_y, s->f_code);
865 if (my >= 0xffff)
866 return AVERROR_INVALIDDATA;
867 mot_val[0] = mx;
868 mot_val[1] = my;
869 }
870 }
871 }
872 }
873 }
874 s->mb_x = 0;
875 }
876
877 return mb_num;
878 }
879
880 /**
881 * decode second partition.
882 * @return <0 if an error occurred
883 */
mpeg4_decode_partition_b(MpegEncContext * s,int mb_count)884 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
885 {
886 int mb_num = 0;
887 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
888
889 s->mb_x = s->resync_mb_x;
890 s->first_slice_line = 1;
891 for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
892 ff_init_block_index(s);
893 for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
894 const int xy = s->mb_x + s->mb_y * s->mb_stride;
895
896 mb_num++;
897 ff_update_block_index(s);
898 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
899 s->first_slice_line = 0;
900
901 if (s->pict_type == AV_PICTURE_TYPE_I) {
902 int ac_pred = get_bits1(&s->gb);
903 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
904 if (cbpy < 0) {
905 av_log(s->avctx, AV_LOG_ERROR,
906 "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
907 return AVERROR_INVALIDDATA;
908 }
909
910 s->cbp_table[xy] |= cbpy << 2;
911 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
912 } else { /* P || S_TYPE */
913 if (IS_INTRA(s->current_picture.mb_type[xy])) {
914 int i;
915 int dir = 0;
916 int ac_pred = get_bits1(&s->gb);
917 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
918
919 if (cbpy < 0) {
920 av_log(s->avctx, AV_LOG_ERROR,
921 "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
922 return AVERROR_INVALIDDATA;
923 }
924
925 if (s->cbp_table[xy] & 8)
926 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
927 s->current_picture.qscale_table[xy] = s->qscale;
928
929 for (i = 0; i < 6; i++) {
930 int dc_pred_dir;
931 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
932 if (dc < 0) {
933 av_log(s->avctx, AV_LOG_ERROR,
934 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
935 return dc;
936 }
937 dir <<= 1;
938 if (dc_pred_dir)
939 dir |= 1;
940 }
941 s->cbp_table[xy] &= 3; // remove dquant
942 s->cbp_table[xy] |= cbpy << 2;
943 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
944 s->pred_dir_table[xy] = dir;
945 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
946 s->current_picture.qscale_table[xy] = s->qscale;
947 s->cbp_table[xy] = 0;
948 } else {
949 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
950
951 if (cbpy < 0) {
952 av_log(s->avctx, AV_LOG_ERROR,
953 "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
954 return AVERROR_INVALIDDATA;
955 }
956
957 if (s->cbp_table[xy] & 8)
958 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
959 s->current_picture.qscale_table[xy] = s->qscale;
960
961 s->cbp_table[xy] &= 3; // remove dquant
962 s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
963 }
964 }
965 }
966 if (mb_num >= mb_count)
967 return 0;
968 s->mb_x = 0;
969 }
970 return 0;
971 }
972
973 /**
974 * Decode the first and second partition.
975 * @return <0 if error (and sets error type in the error_status_table)
976 */
ff_mpeg4_decode_partitions(Mpeg4DecContext * ctx)977 int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
978 {
979 MpegEncContext *s = &ctx->m;
980 int mb_num;
981 int ret;
982 const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
983 const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
984
985 mb_num = mpeg4_decode_partition_a(ctx);
986 if (mb_num <= 0) {
987 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
988 s->mb_x, s->mb_y, part_a_error);
989 return mb_num ? mb_num : AVERROR_INVALIDDATA;
990 }
991
992 if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
993 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
994 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
995 s->mb_x, s->mb_y, part_a_error);
996 return AVERROR_INVALIDDATA;
997 }
998
999 s->mb_num_left = mb_num;
1000
1001 if (s->pict_type == AV_PICTURE_TYPE_I) {
1002 while (show_bits(&s->gb, 9) == 1)
1003 skip_bits(&s->gb, 9);
1004 if (get_bits(&s->gb, 19) != DC_MARKER) {
1005 av_log(s->avctx, AV_LOG_ERROR,
1006 "marker missing after first I partition at %d %d\n",
1007 s->mb_x, s->mb_y);
1008 return AVERROR_INVALIDDATA;
1009 }
1010 } else {
1011 while (show_bits(&s->gb, 10) == 1)
1012 skip_bits(&s->gb, 10);
1013 if (get_bits(&s->gb, 17) != MOTION_MARKER) {
1014 av_log(s->avctx, AV_LOG_ERROR,
1015 "marker missing after first P partition at %d %d\n",
1016 s->mb_x, s->mb_y);
1017 return AVERROR_INVALIDDATA;
1018 }
1019 }
1020 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1021 s->mb_x - 1, s->mb_y, part_a_end);
1022
1023 ret = mpeg4_decode_partition_b(s, mb_num);
1024 if (ret < 0) {
1025 if (s->pict_type == AV_PICTURE_TYPE_P)
1026 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1027 s->mb_x, s->mb_y, ER_DC_ERROR);
1028 return ret;
1029 } else {
1030 if (s->pict_type == AV_PICTURE_TYPE_P)
1031 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1032 s->mb_x - 1, s->mb_y, ER_DC_END);
1033 }
1034
1035 return 0;
1036 }
1037
1038 /**
1039 * Decode a block.
1040 * @return <0 if an error occurred
1041 */
mpeg4_decode_block(Mpeg4DecContext * ctx,int16_t * block,int n,int coded,int intra,int rvlc)1042 static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
1043 int n, int coded, int intra, int rvlc)
1044 {
1045 MpegEncContext *s = &ctx->m;
1046 int level, i, last, run, qmul, qadd;
1047 int av_uninit(dc_pred_dir);
1048 RLTable *rl;
1049 RL_VLC_ELEM *rl_vlc;
1050 const uint8_t *scan_table;
1051
1052 // Note intra & rvlc should be optimized away if this is inlined
1053
1054 if (intra) {
1055 if (ctx->use_intra_dc_vlc) {
1056 /* DC coef */
1057 if (s->partitioned_frame) {
1058 level = s->dc_val[0][s->block_index[n]];
1059 if (n < 4)
1060 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
1061 else
1062 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
1063 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
1064 } else {
1065 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1066 if (level < 0)
1067 return level;
1068 }
1069 block[0] = level;
1070 i = 0;
1071 } else {
1072 i = -1;
1073 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
1074 }
1075 if (!coded)
1076 goto not_coded;
1077
1078 if (rvlc) {
1079 rl = &ff_rvlc_rl_intra;
1080 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1081 } else {
1082 rl = &ff_mpeg4_rl_intra;
1083 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1084 }
1085 if (s->ac_pred) {
1086 if (dc_pred_dir == 0)
1087 scan_table = s->intra_v_scantable.permutated; /* left */
1088 else
1089 scan_table = s->intra_h_scantable.permutated; /* top */
1090 } else {
1091 scan_table = s->intra_scantable.permutated;
1092 }
1093 qmul = 1;
1094 qadd = 0;
1095 } else {
1096 i = -1;
1097 if (!coded) {
1098 s->block_last_index[n] = i;
1099 return 0;
1100 }
1101 if (rvlc)
1102 rl = &ff_rvlc_rl_inter;
1103 else
1104 rl = &ff_h263_rl_inter;
1105
1106 scan_table = s->intra_scantable.permutated;
1107
1108 if (s->mpeg_quant) {
1109 qmul = 1;
1110 qadd = 0;
1111 if (rvlc)
1112 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1113 else
1114 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1115 } else {
1116 qmul = s->qscale << 1;
1117 qadd = (s->qscale - 1) | 1;
1118 if (rvlc)
1119 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1120 else
1121 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1122 }
1123 }
1124 {
1125 OPEN_READER(re, &s->gb);
1126 for (;;) {
1127 UPDATE_CACHE(re, &s->gb);
1128 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1129 if (level == 0) {
1130 /* escape */
1131 if (rvlc) {
1132 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1133 av_log(s->avctx, AV_LOG_ERROR,
1134 "1. marker bit missing in rvlc esc\n");
1135 return AVERROR_INVALIDDATA;
1136 }
1137 SKIP_CACHE(re, &s->gb, 1);
1138
1139 last = SHOW_UBITS(re, &s->gb, 1);
1140 SKIP_CACHE(re, &s->gb, 1);
1141 run = SHOW_UBITS(re, &s->gb, 6);
1142 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1143 UPDATE_CACHE(re, &s->gb);
1144
1145 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1146 av_log(s->avctx, AV_LOG_ERROR,
1147 "2. marker bit missing in rvlc esc\n");
1148 return AVERROR_INVALIDDATA;
1149 }
1150 SKIP_CACHE(re, &s->gb, 1);
1151
1152 level = SHOW_UBITS(re, &s->gb, 11);
1153 SKIP_CACHE(re, &s->gb, 11);
1154
1155 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1156 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1157 return AVERROR_INVALIDDATA;
1158 }
1159 SKIP_CACHE(re, &s->gb, 5);
1160
1161 level = level * qmul + qadd;
1162 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1163 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1164
1165 i += run + 1;
1166 if (last)
1167 i += 192;
1168 } else {
1169 int cache;
1170 cache = GET_CACHE(re, &s->gb);
1171
1172 if (IS_3IV1)
1173 cache ^= 0xC0000000;
1174
1175 if (cache & 0x80000000) {
1176 if (cache & 0x40000000) {
1177 /* third escape */
1178 SKIP_CACHE(re, &s->gb, 2);
1179 last = SHOW_UBITS(re, &s->gb, 1);
1180 SKIP_CACHE(re, &s->gb, 1);
1181 run = SHOW_UBITS(re, &s->gb, 6);
1182 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1183 UPDATE_CACHE(re, &s->gb);
1184
1185 if (IS_3IV1) {
1186 level = SHOW_SBITS(re, &s->gb, 12);
1187 LAST_SKIP_BITS(re, &s->gb, 12);
1188 } else {
1189 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1190 av_log(s->avctx, AV_LOG_ERROR,
1191 "1. marker bit missing in 3. esc\n");
1192 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1193 return AVERROR_INVALIDDATA;
1194 }
1195 SKIP_CACHE(re, &s->gb, 1);
1196
1197 level = SHOW_SBITS(re, &s->gb, 12);
1198 SKIP_CACHE(re, &s->gb, 12);
1199
1200 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1201 av_log(s->avctx, AV_LOG_ERROR,
1202 "2. marker bit missing in 3. esc\n");
1203 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1204 return AVERROR_INVALIDDATA;
1205 }
1206
1207 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1208 }
1209
1210 #if 0
1211 if (s->error_recognition >= FF_ER_COMPLIANT) {
1212 const int abs_level= FFABS(level);
1213 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1214 const int run1= run - rl->max_run[last][abs_level] - 1;
1215 if (abs_level <= rl->max_level[last][run]) {
1216 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1217 return AVERROR_INVALIDDATA;
1218 }
1219 if (s->error_recognition > FF_ER_COMPLIANT) {
1220 if (abs_level <= rl->max_level[last][run]*2) {
1221 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1222 return AVERROR_INVALIDDATA;
1223 }
1224 if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1225 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1226 return AVERROR_INVALIDDATA;
1227 }
1228 }
1229 }
1230 }
1231 #endif
1232 if (level > 0)
1233 level = level * qmul + qadd;
1234 else
1235 level = level * qmul - qadd;
1236
1237 if ((unsigned)(level + 2048) > 4095) {
1238 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1239 if (level > 2560 || level < -2560) {
1240 av_log(s->avctx, AV_LOG_ERROR,
1241 "|level| overflow in 3. esc, qp=%d\n",
1242 s->qscale);
1243 return AVERROR_INVALIDDATA;
1244 }
1245 }
1246 level = level < 0 ? -2048 : 2047;
1247 }
1248
1249 i += run + 1;
1250 if (last)
1251 i += 192;
1252 } else {
1253 /* second escape */
1254 SKIP_BITS(re, &s->gb, 2);
1255 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1256 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1257 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1258 LAST_SKIP_BITS(re, &s->gb, 1);
1259 }
1260 } else {
1261 /* first escape */
1262 SKIP_BITS(re, &s->gb, 1);
1263 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1264 i += run;
1265 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1266 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1267 LAST_SKIP_BITS(re, &s->gb, 1);
1268 }
1269 }
1270 } else {
1271 i += run;
1272 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1273 LAST_SKIP_BITS(re, &s->gb, 1);
1274 }
1275 ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1276 if (i > 62) {
1277 i -= 192;
1278 if (i & (~63)) {
1279 av_log(s->avctx, AV_LOG_ERROR,
1280 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1281 return AVERROR_INVALIDDATA;
1282 }
1283
1284 block[scan_table[i]] = level;
1285 break;
1286 }
1287
1288 block[scan_table[i]] = level;
1289 }
1290 CLOSE_READER(re, &s->gb);
1291 }
1292
1293 not_coded:
1294 if (intra) {
1295 if (!ctx->use_intra_dc_vlc) {
1296 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1297
1298 i -= i >> 31; // if (i == -1) i = 0;
1299 }
1300
1301 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1302 if (s->ac_pred)
1303 i = 63; // FIXME not optimal
1304 }
1305 s->block_last_index[n] = i;
1306 return 0;
1307 }
1308
1309 /**
1310 * decode partition C of one MB.
1311 * @return <0 if an error occurred
1312 */
mpeg4_decode_partitioned_mb(MpegEncContext * s,int16_t block[6][64])1313 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1314 {
1315 Mpeg4DecContext *ctx = s->avctx->priv_data;
1316 int cbp, mb_type;
1317 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1318
1319 av_assert2(s == (void*)ctx);
1320
1321 mb_type = s->current_picture.mb_type[xy];
1322 cbp = s->cbp_table[xy];
1323
1324 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1325
1326 if (s->current_picture.qscale_table[xy] != s->qscale)
1327 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1328
1329 if (s->pict_type == AV_PICTURE_TYPE_P ||
1330 s->pict_type == AV_PICTURE_TYPE_S) {
1331 int i;
1332 for (i = 0; i < 4; i++) {
1333 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1334 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1335 }
1336 s->mb_intra = IS_INTRA(mb_type);
1337
1338 if (IS_SKIP(mb_type)) {
1339 /* skip mb */
1340 for (i = 0; i < 6; i++)
1341 s->block_last_index[i] = -1;
1342 s->mv_dir = MV_DIR_FORWARD;
1343 s->mv_type = MV_TYPE_16X16;
1344 if (s->pict_type == AV_PICTURE_TYPE_S
1345 && ctx->vol_sprite_usage == GMC_SPRITE) {
1346 s->mcsel = 1;
1347 s->mb_skipped = 0;
1348 } else {
1349 s->mcsel = 0;
1350 s->mb_skipped = 1;
1351 }
1352 } else if (s->mb_intra) {
1353 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1354 } else if (!s->mb_intra) {
1355 // s->mcsel = 0; // FIXME do we need to init that?
1356
1357 s->mv_dir = MV_DIR_FORWARD;
1358 if (IS_8X8(mb_type)) {
1359 s->mv_type = MV_TYPE_8X8;
1360 } else {
1361 s->mv_type = MV_TYPE_16X16;
1362 }
1363 }
1364 } else { /* I-Frame */
1365 s->mb_intra = 1;
1366 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1367 }
1368
1369 if (!IS_SKIP(mb_type)) {
1370 int i;
1371 s->bdsp.clear_blocks(s->block[0]);
1372 /* decode each block */
1373 for (i = 0; i < 6; i++) {
1374 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1375 av_log(s->avctx, AV_LOG_ERROR,
1376 "texture corrupted at %d %d %d\n",
1377 s->mb_x, s->mb_y, s->mb_intra);
1378 return AVERROR_INVALIDDATA;
1379 }
1380 cbp += cbp;
1381 }
1382 }
1383
1384 /* per-MB end of slice check */
1385 if (--s->mb_num_left <= 0) {
1386 if (mpeg4_is_resync(ctx))
1387 return SLICE_END;
1388 else
1389 return SLICE_NOEND;
1390 } else {
1391 if (mpeg4_is_resync(ctx)) {
1392 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1393 if (s->cbp_table[xy + delta])
1394 return SLICE_END;
1395 }
1396 return SLICE_OK;
1397 }
1398 }
1399
mpeg4_decode_mb(MpegEncContext * s,int16_t block[6][64])1400 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1401 {
1402 Mpeg4DecContext *ctx = s->avctx->priv_data;
1403 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1404 int16_t *mot_val;
1405 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1406 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1407
1408 av_assert2(s == (void*)ctx);
1409 av_assert2(s->h263_pred);
1410
1411 if (s->pict_type == AV_PICTURE_TYPE_P ||
1412 s->pict_type == AV_PICTURE_TYPE_S) {
1413 do {
1414 if (get_bits1(&s->gb)) {
1415 /* skip mb */
1416 s->mb_intra = 0;
1417 for (i = 0; i < 6; i++)
1418 s->block_last_index[i] = -1;
1419 s->mv_dir = MV_DIR_FORWARD;
1420 s->mv_type = MV_TYPE_16X16;
1421 if (s->pict_type == AV_PICTURE_TYPE_S &&
1422 ctx->vol_sprite_usage == GMC_SPRITE) {
1423 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1424 MB_TYPE_GMC |
1425 MB_TYPE_16x16 |
1426 MB_TYPE_L0;
1427 s->mcsel = 1;
1428 s->mv[0][0][0] = get_amv(ctx, 0);
1429 s->mv[0][0][1] = get_amv(ctx, 1);
1430 s->mb_skipped = 0;
1431 } else {
1432 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1433 MB_TYPE_16x16 |
1434 MB_TYPE_L0;
1435 s->mcsel = 0;
1436 s->mv[0][0][0] = 0;
1437 s->mv[0][0][1] = 0;
1438 s->mb_skipped = 1;
1439 }
1440 goto end;
1441 }
1442 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1443 if (cbpc < 0) {
1444 av_log(s->avctx, AV_LOG_ERROR,
1445 "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1446 return AVERROR_INVALIDDATA;
1447 }
1448 } while (cbpc == 20);
1449
1450 s->bdsp.clear_blocks(s->block[0]);
1451 dquant = cbpc & 8;
1452 s->mb_intra = ((cbpc & 4) != 0);
1453 if (s->mb_intra)
1454 goto intra;
1455
1456 if (s->pict_type == AV_PICTURE_TYPE_S &&
1457 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1458 s->mcsel = get_bits1(&s->gb);
1459 else
1460 s->mcsel = 0;
1461 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1462 if (cbpy < 0) {
1463 av_log(s->avctx, AV_LOG_ERROR,
1464 "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1465 return AVERROR_INVALIDDATA;
1466 }
1467
1468 cbp = (cbpc & 3) | (cbpy << 2);
1469 if (dquant)
1470 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1471 if ((!s->progressive_sequence) &&
1472 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1473 s->interlaced_dct = get_bits1(&s->gb);
1474
1475 s->mv_dir = MV_DIR_FORWARD;
1476 if ((cbpc & 16) == 0) {
1477 if (s->mcsel) {
1478 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1479 MB_TYPE_16x16 |
1480 MB_TYPE_L0;
1481 /* 16x16 global motion prediction */
1482 s->mv_type = MV_TYPE_16X16;
1483 mx = get_amv(ctx, 0);
1484 my = get_amv(ctx, 1);
1485 s->mv[0][0][0] = mx;
1486 s->mv[0][0][1] = my;
1487 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1488 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1489 MB_TYPE_L0 |
1490 MB_TYPE_INTERLACED;
1491 /* 16x8 field motion prediction */
1492 s->mv_type = MV_TYPE_FIELD;
1493
1494 s->field_select[0][0] = get_bits1(&s->gb);
1495 s->field_select[0][1] = get_bits1(&s->gb);
1496
1497 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1498
1499 for (i = 0; i < 2; i++) {
1500 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1501 if (mx >= 0xffff)
1502 return AVERROR_INVALIDDATA;
1503
1504 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1505 if (my >= 0xffff)
1506 return AVERROR_INVALIDDATA;
1507
1508 s->mv[0][i][0] = mx;
1509 s->mv[0][i][1] = my;
1510 }
1511 } else {
1512 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1513 /* 16x16 motion prediction */
1514 s->mv_type = MV_TYPE_16X16;
1515 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1516 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1517
1518 if (mx >= 0xffff)
1519 return AVERROR_INVALIDDATA;
1520
1521 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1522
1523 if (my >= 0xffff)
1524 return AVERROR_INVALIDDATA;
1525 s->mv[0][0][0] = mx;
1526 s->mv[0][0][1] = my;
1527 }
1528 } else {
1529 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1530 s->mv_type = MV_TYPE_8X8;
1531 for (i = 0; i < 4; i++) {
1532 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1533 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1534 if (mx >= 0xffff)
1535 return AVERROR_INVALIDDATA;
1536
1537 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1538 if (my >= 0xffff)
1539 return AVERROR_INVALIDDATA;
1540 s->mv[0][i][0] = mx;
1541 s->mv[0][i][1] = my;
1542 mot_val[0] = mx;
1543 mot_val[1] = my;
1544 }
1545 }
1546 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1547 int modb1; // first bit of modb
1548 int modb2; // second bit of modb
1549 int mb_type;
1550
1551 s->mb_intra = 0; // B-frames never contain intra blocks
1552 s->mcsel = 0; // ... true gmc blocks
1553
1554 if (s->mb_x == 0) {
1555 for (i = 0; i < 2; i++) {
1556 s->last_mv[i][0][0] =
1557 s->last_mv[i][0][1] =
1558 s->last_mv[i][1][0] =
1559 s->last_mv[i][1][1] = 0;
1560 }
1561
1562 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1563 }
1564
1565 /* if we skipped it in the future P-frame than skip it now too */
1566 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1567
1568 if (s->mb_skipped) {
1569 /* skip mb */
1570 for (i = 0; i < 6; i++)
1571 s->block_last_index[i] = -1;
1572
1573 s->mv_dir = MV_DIR_FORWARD;
1574 s->mv_type = MV_TYPE_16X16;
1575 s->mv[0][0][0] =
1576 s->mv[0][0][1] =
1577 s->mv[1][0][0] =
1578 s->mv[1][0][1] = 0;
1579 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1580 MB_TYPE_16x16 |
1581 MB_TYPE_L0;
1582 goto end;
1583 }
1584
1585 modb1 = get_bits1(&s->gb);
1586 if (modb1) {
1587 // like MB_TYPE_B_DIRECT but no vectors coded
1588 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1589 cbp = 0;
1590 } else {
1591 modb2 = get_bits1(&s->gb);
1592 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1593 if (mb_type < 0) {
1594 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1595 return AVERROR_INVALIDDATA;
1596 }
1597 mb_type = mb_type_b_map[mb_type];
1598 if (modb2) {
1599 cbp = 0;
1600 } else {
1601 s->bdsp.clear_blocks(s->block[0]);
1602 cbp = get_bits(&s->gb, 6);
1603 }
1604
1605 if ((!IS_DIRECT(mb_type)) && cbp) {
1606 if (get_bits1(&s->gb))
1607 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1608 }
1609
1610 if (!s->progressive_sequence) {
1611 if (cbp)
1612 s->interlaced_dct = get_bits1(&s->gb);
1613
1614 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1615 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1616 mb_type &= ~MB_TYPE_16x16;
1617
1618 if (USES_LIST(mb_type, 0)) {
1619 s->field_select[0][0] = get_bits1(&s->gb);
1620 s->field_select[0][1] = get_bits1(&s->gb);
1621 }
1622 if (USES_LIST(mb_type, 1)) {
1623 s->field_select[1][0] = get_bits1(&s->gb);
1624 s->field_select[1][1] = get_bits1(&s->gb);
1625 }
1626 }
1627 }
1628
1629 s->mv_dir = 0;
1630 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1631 s->mv_type = MV_TYPE_16X16;
1632
1633 if (USES_LIST(mb_type, 0)) {
1634 s->mv_dir = MV_DIR_FORWARD;
1635
1636 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1637 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1638 s->last_mv[0][1][0] =
1639 s->last_mv[0][0][0] =
1640 s->mv[0][0][0] = mx;
1641 s->last_mv[0][1][1] =
1642 s->last_mv[0][0][1] =
1643 s->mv[0][0][1] = my;
1644 }
1645
1646 if (USES_LIST(mb_type, 1)) {
1647 s->mv_dir |= MV_DIR_BACKWARD;
1648
1649 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1650 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1651 s->last_mv[1][1][0] =
1652 s->last_mv[1][0][0] =
1653 s->mv[1][0][0] = mx;
1654 s->last_mv[1][1][1] =
1655 s->last_mv[1][0][1] =
1656 s->mv[1][0][1] = my;
1657 }
1658 } else if (!IS_DIRECT(mb_type)) {
1659 s->mv_type = MV_TYPE_FIELD;
1660
1661 if (USES_LIST(mb_type, 0)) {
1662 s->mv_dir = MV_DIR_FORWARD;
1663
1664 for (i = 0; i < 2; i++) {
1665 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1666 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1667 s->last_mv[0][i][0] =
1668 s->mv[0][i][0] = mx;
1669 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1670 }
1671 }
1672
1673 if (USES_LIST(mb_type, 1)) {
1674 s->mv_dir |= MV_DIR_BACKWARD;
1675
1676 for (i = 0; i < 2; i++) {
1677 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1678 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1679 s->last_mv[1][i][0] =
1680 s->mv[1][i][0] = mx;
1681 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1682 }
1683 }
1684 }
1685 }
1686
1687 if (IS_DIRECT(mb_type)) {
1688 if (IS_SKIP(mb_type)) {
1689 mx =
1690 my = 0;
1691 } else {
1692 mx = ff_h263_decode_motion(s, 0, 1);
1693 my = ff_h263_decode_motion(s, 0, 1);
1694 }
1695
1696 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1697 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1698 }
1699 s->current_picture.mb_type[xy] = mb_type;
1700 } else { /* I-Frame */
1701 do {
1702 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1703 if (cbpc < 0) {
1704 av_log(s->avctx, AV_LOG_ERROR,
1705 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1706 return AVERROR_INVALIDDATA;
1707 }
1708 } while (cbpc == 8);
1709
1710 dquant = cbpc & 4;
1711 s->mb_intra = 1;
1712
1713 intra:
1714 s->ac_pred = get_bits1(&s->gb);
1715 if (s->ac_pred)
1716 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1717 else
1718 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1719
1720 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1721 if (cbpy < 0) {
1722 av_log(s->avctx, AV_LOG_ERROR,
1723 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1724 return AVERROR_INVALIDDATA;
1725 }
1726 cbp = (cbpc & 3) | (cbpy << 2);
1727
1728 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1729
1730 if (dquant)
1731 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1732
1733 if (!s->progressive_sequence)
1734 s->interlaced_dct = get_bits1(&s->gb);
1735
1736 s->bdsp.clear_blocks(s->block[0]);
1737 /* decode each block */
1738 for (i = 0; i < 6; i++) {
1739 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
1740 return AVERROR_INVALIDDATA;
1741 cbp += cbp;
1742 }
1743 goto end;
1744 }
1745
1746 /* decode each block */
1747 for (i = 0; i < 6; i++) {
1748 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
1749 return AVERROR_INVALIDDATA;
1750 cbp += cbp;
1751 }
1752
1753 end:
1754 /* per-MB end of slice check */
1755 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1756 int next = mpeg4_is_resync(ctx);
1757 if (next) {
1758 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1759 return AVERROR_INVALIDDATA;
1760 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1761 return SLICE_END;
1762
1763 if (s->pict_type == AV_PICTURE_TYPE_B) {
1764 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1765 ff_thread_await_progress(&s->next_picture_ptr->tf,
1766 (s->mb_x + delta >= s->mb_width)
1767 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1768 : s->mb_y, 0);
1769 if (s->next_picture.mbskip_table[xy + delta])
1770 return SLICE_OK;
1771 }
1772
1773 return SLICE_END;
1774 }
1775 }
1776
1777 return SLICE_OK;
1778 }
1779
1780 /* As per spec, studio start code search isn't the same as the old type of start code */
next_start_code_studio(GetBitContext * gb)1781 static void next_start_code_studio(GetBitContext *gb)
1782 {
1783 align_get_bits(gb);
1784
1785 while (get_bits_left(gb) >= 24 && show_bits(gb, 24) != 0x1) {
1786 get_bits(gb, 8);
1787 }
1788 }
1789
1790 /* additional_code, vlc index */
1791 static const uint8_t ac_state_tab[22][2] =
1792 {
1793 {0, 0},
1794 {0, 1},
1795 {1, 1},
1796 {2, 1},
1797 {3, 1},
1798 {4, 1},
1799 {5, 1},
1800 {1, 2},
1801 {2, 2},
1802 {3, 2},
1803 {4, 2},
1804 {5, 2},
1805 {6, 2},
1806 {1, 3},
1807 {2, 4},
1808 {3, 5},
1809 {4, 6},
1810 {5, 7},
1811 {6, 8},
1812 {7, 9},
1813 {8, 10},
1814 {0, 11}
1815 };
1816
mpeg4_decode_studio_block(MpegEncContext * s,int32_t block[64],int n)1817 static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n)
1818 {
1819 Mpeg4DecContext *ctx = s->avctx->priv_data;
1820
1821 int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
1822 additional_code_len, sign, mismatch;
1823 VLC *cur_vlc = &ctx->studio_intra_tab[0];
1824 uint8_t *const scantable = s->intra_scantable.permutated;
1825 const uint16_t *quant_matrix;
1826 uint32_t flc;
1827 const int min = -1 * (1 << (s->avctx->bits_per_raw_sample + 6));
1828 const int max = ((1 << (s->avctx->bits_per_raw_sample + 6)) - 1);
1829 int shift = 3 - s->dct_precision;
1830
1831 mismatch = 1;
1832
1833 memset(block, 0, 64 * sizeof(int32_t));
1834
1835 if (n < 4) {
1836 cc = 0;
1837 dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1838 quant_matrix = s->intra_matrix;
1839 } else {
1840 cc = (n & 1) + 1;
1841 if (ctx->rgb)
1842 dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1843 else
1844 dct_dc_size = get_vlc2(&s->gb, ctx->studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
1845 quant_matrix = s->chroma_intra_matrix;
1846 }
1847
1848 if (dct_dc_size < 0) {
1849 av_log(s->avctx, AV_LOG_ERROR, "illegal dct_dc_size vlc\n");
1850 return AVERROR_INVALIDDATA;
1851 } else if (dct_dc_size == 0) {
1852 dct_diff = 0;
1853 } else {
1854 dct_diff = get_xbits(&s->gb, dct_dc_size);
1855
1856 if (dct_dc_size > 8) {
1857 if(!check_marker(s->avctx, &s->gb, "dct_dc_size > 8"))
1858 return AVERROR_INVALIDDATA;
1859 }
1860
1861 }
1862
1863 s->last_dc[cc] += dct_diff;
1864
1865 if (s->mpeg_quant)
1866 block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision);
1867 else
1868 block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision) * (8 >> s->dct_precision);
1869 /* TODO: support mpeg_quant for AC coefficients */
1870
1871 block[0] = av_clip(block[0], min, max);
1872 mismatch ^= block[0];
1873
1874 /* AC Coefficients */
1875 while (1) {
1876 group = get_vlc2(&s->gb, cur_vlc->table, STUDIO_INTRA_BITS, 2);
1877
1878 if (group < 0) {
1879 av_log(s->avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
1880 return AVERROR_INVALIDDATA;
1881 }
1882
1883 additional_code_len = ac_state_tab[group][0];
1884 cur_vlc = &ctx->studio_intra_tab[ac_state_tab[group][1]];
1885
1886 if (group == 0) {
1887 /* End of Block */
1888 break;
1889 } else if (group >= 1 && group <= 6) {
1890 /* Zero run length (Table B.47) */
1891 run = 1 << additional_code_len;
1892 if (additional_code_len)
1893 run += get_bits(&s->gb, additional_code_len);
1894 idx += run;
1895 continue;
1896 } else if (group >= 7 && group <= 12) {
1897 /* Zero run length and +/-1 level (Table B.48) */
1898 code = get_bits(&s->gb, additional_code_len);
1899 sign = code & 1;
1900 code >>= 1;
1901 run = (1 << (additional_code_len - 1)) + code;
1902 idx += run;
1903 if (idx > 63)
1904 return AVERROR_INVALIDDATA;
1905 j = scantable[idx++];
1906 block[j] = sign ? 1 : -1;
1907 } else if (group >= 13 && group <= 20) {
1908 /* Level value (Table B.49) */
1909 if (idx > 63)
1910 return AVERROR_INVALIDDATA;
1911 j = scantable[idx++];
1912 block[j] = get_xbits(&s->gb, additional_code_len);
1913 } else if (group == 21) {
1914 /* Escape */
1915 if (idx > 63)
1916 return AVERROR_INVALIDDATA;
1917 j = scantable[idx++];
1918 additional_code_len = s->avctx->bits_per_raw_sample + s->dct_precision + 4;
1919 flc = get_bits(&s->gb, additional_code_len);
1920 if (flc >> (additional_code_len-1))
1921 block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
1922 else
1923 block[j] = flc;
1924 }
1925 block[j] = ((block[j] * quant_matrix[j] * s->qscale) * (1 << shift)) / 16;
1926 block[j] = av_clip(block[j], min, max);
1927 mismatch ^= block[j];
1928 }
1929
1930 block[63] ^= mismatch & 1;
1931
1932 return 0;
1933 }
1934
mpeg4_decode_dpcm_macroblock(MpegEncContext * s,int16_t macroblock[256],int n)1935 static int mpeg4_decode_dpcm_macroblock(MpegEncContext *s, int16_t macroblock[256], int n)
1936 {
1937 int i, j, w, h, idx = 0;
1938 int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code,
1939 dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output;
1940 h = 16 >> (n ? s->chroma_y_shift : 0);
1941 w = 16 >> (n ? s->chroma_x_shift : 0);
1942
1943 block_mean = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
1944 if (block_mean == 0){
1945 av_log(s->avctx, AV_LOG_ERROR, "Forbidden block_mean\n");
1946 return AVERROR_INVALIDDATA;
1947 }
1948 s->last_dc[n] = block_mean * (1 << (s->dct_precision + s->intra_dc_precision));
1949
1950 rice_parameter = get_bits(&s->gb, 4);
1951 if (rice_parameter == 0) {
1952 av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
1953 return AVERROR_INVALIDDATA;
1954 }
1955
1956 if (rice_parameter == 15)
1957 rice_parameter = 0;
1958
1959 if (rice_parameter > 11) {
1960 av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
1961 return AVERROR_INVALIDDATA;
1962 }
1963
1964 for (i = 0; i < h; i++) {
1965 output = 1 << (s->avctx->bits_per_raw_sample - 1);
1966 top = 1 << (s->avctx->bits_per_raw_sample - 1);
1967
1968 for (j = 0; j < w; j++) {
1969 left = output;
1970 topleft = top;
1971
1972 rice_prefix_code = get_unary(&s->gb, 1, 12);
1973
1974 /* Escape */
1975 if (rice_prefix_code == 11)
1976 dpcm_residual = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
1977 else {
1978 if (rice_prefix_code == 12) {
1979 av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n");
1980 return AVERROR_INVALIDDATA;
1981 }
1982 rice_suffix_code = get_bitsz(&s->gb, rice_parameter);
1983 dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code;
1984 }
1985
1986 /* Map to a signed residual */
1987 if (dpcm_residual & 1)
1988 dpcm_residual = (-1 * dpcm_residual) >> 1;
1989 else
1990 dpcm_residual = (dpcm_residual >> 1);
1991
1992 if (i != 0)
1993 top = macroblock[idx-w];
1994
1995 p = left + top - topleft;
1996 min_left_top = FFMIN(left, top);
1997 if (p < min_left_top)
1998 p = min_left_top;
1999
2000 max_left_top = FFMAX(left, top);
2001 if (p > max_left_top)
2002 p = max_left_top;
2003
2004 p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1;
2005 if (p2 == p)
2006 p2 = block_mean;
2007
2008 if (p2 > p)
2009 dpcm_residual *= -1;
2010
2011 macroblock[idx++] = output = (dpcm_residual + p) & ((1 << s->avctx->bits_per_raw_sample) - 1);
2012 }
2013 }
2014
2015 return 0;
2016 }
2017
mpeg4_decode_studio_mb(MpegEncContext * s,int16_t block_[12][64])2018 static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
2019 {
2020 int i;
2021
2022 s->dpcm_direction = 0;
2023
2024 /* StudioMacroblock */
2025 /* Assumes I-VOP */
2026 s->mb_intra = 1;
2027 if (get_bits1(&s->gb)) { /* compression_mode */
2028 /* DCT */
2029 /* macroblock_type, 1 or 2-bit VLC */
2030 if (!get_bits1(&s->gb)) {
2031 skip_bits1(&s->gb);
2032 s->qscale = mpeg_get_qscale(s);
2033 }
2034
2035 for (i = 0; i < mpeg4_block_count[s->chroma_format]; i++) {
2036 if (mpeg4_decode_studio_block(s, (*s->block32)[i], i) < 0)
2037 return AVERROR_INVALIDDATA;
2038 }
2039 } else {
2040 /* DPCM */
2041 check_marker(s->avctx, &s->gb, "DPCM block start");
2042 s->dpcm_direction = get_bits1(&s->gb) ? -1 : 1;
2043 for (i = 0; i < 3; i++) {
2044 if (mpeg4_decode_dpcm_macroblock(s, (*s->dpcm_macroblock)[i], i) < 0)
2045 return AVERROR_INVALIDDATA;
2046 }
2047 }
2048
2049 if (get_bits_left(&s->gb) >= 24 && show_bits(&s->gb, 23) == 0) {
2050 next_start_code_studio(&s->gb);
2051 return SLICE_END;
2052 }
2053
2054 //vcon-stp9L1.bits (first frame)
2055 if (get_bits_left(&s->gb) == 0)
2056 return SLICE_END;
2057
2058 //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame)
2059 if (get_bits_left(&s->gb) < 8U && show_bits(&s->gb, get_bits_left(&s->gb)) == 0)
2060 return SLICE_END;
2061
2062 return SLICE_OK;
2063 }
2064
mpeg4_decode_gop_header(MpegEncContext * s,GetBitContext * gb)2065 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
2066 {
2067 int hours, minutes, seconds;
2068
2069 if (!show_bits(gb, 23)) {
2070 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
2071 return AVERROR_INVALIDDATA;
2072 }
2073
2074 hours = get_bits(gb, 5);
2075 minutes = get_bits(gb, 6);
2076 check_marker(s->avctx, gb, "in gop_header");
2077 seconds = get_bits(gb, 6);
2078
2079 s->time_base = seconds + 60*(minutes + 60*hours);
2080
2081 skip_bits1(gb);
2082 skip_bits1(gb);
2083
2084 return 0;
2085 }
2086
mpeg4_decode_profile_level(MpegEncContext * s,GetBitContext * gb,int * profile,int * level)2087 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
2088 {
2089
2090 *profile = get_bits(gb, 4);
2091 *level = get_bits(gb, 4);
2092
2093 // for Simple profile, level 0
2094 if (*profile == 0 && *level == 8) {
2095 *level = 0;
2096 }
2097
2098 return 0;
2099 }
2100
mpeg4_decode_visual_object(MpegEncContext * s,GetBitContext * gb)2101 static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
2102 {
2103 int visual_object_type;
2104 int is_visual_object_identifier = get_bits1(gb);
2105
2106 if (is_visual_object_identifier) {
2107 skip_bits(gb, 4+3);
2108 }
2109 visual_object_type = get_bits(gb, 4);
2110
2111 if (visual_object_type == VOT_VIDEO_ID ||
2112 visual_object_type == VOT_STILL_TEXTURE_ID) {
2113 int video_signal_type = get_bits1(gb);
2114 if (video_signal_type) {
2115 int video_range, color_description;
2116 skip_bits(gb, 3); // video_format
2117 video_range = get_bits1(gb);
2118 color_description = get_bits1(gb);
2119
2120 s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2121
2122 if (color_description) {
2123 s->avctx->color_primaries = get_bits(gb, 8);
2124 s->avctx->color_trc = get_bits(gb, 8);
2125 s->avctx->colorspace = get_bits(gb, 8);
2126 }
2127 }
2128 }
2129
2130 return 0;
2131 }
2132
mpeg4_load_default_matrices(MpegEncContext * s)2133 static void mpeg4_load_default_matrices(MpegEncContext *s)
2134 {
2135 int i, v;
2136
2137 /* load default matrices */
2138 for (i = 0; i < 64; i++) {
2139 int j = s->idsp.idct_permutation[i];
2140 v = ff_mpeg4_default_intra_matrix[i];
2141 s->intra_matrix[j] = v;
2142 s->chroma_intra_matrix[j] = v;
2143
2144 v = ff_mpeg4_default_non_intra_matrix[i];
2145 s->inter_matrix[j] = v;
2146 s->chroma_inter_matrix[j] = v;
2147 }
2148 }
2149
decode_vol_header(Mpeg4DecContext * ctx,GetBitContext * gb)2150 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2151 {
2152 MpegEncContext *s = &ctx->m;
2153 int width, height, vo_ver_id;
2154
2155 /* vol header */
2156 skip_bits(gb, 1); /* random access */
2157 s->vo_type = get_bits(gb, 8);
2158
2159 /* If we are in studio profile (per vo_type), check if its all consistent
2160 * and if so continue pass control to decode_studio_vol_header().
2161 * elIf something is inconsistent, error out
2162 * else continue with (non studio) vol header decpoding.
2163 */
2164 if (s->vo_type == CORE_STUDIO_VO_TYPE ||
2165 s->vo_type == SIMPLE_STUDIO_VO_TYPE) {
2166 if (s->avctx->profile != FF_PROFILE_UNKNOWN && s->avctx->profile != FF_PROFILE_MPEG4_SIMPLE_STUDIO)
2167 return AVERROR_INVALIDDATA;
2168 s->studio_profile = 1;
2169 s->avctx->profile = FF_PROFILE_MPEG4_SIMPLE_STUDIO;
2170 return decode_studio_vol_header(ctx, gb);
2171 } else if (s->studio_profile) {
2172 return AVERROR_PATCHWELCOME;
2173 }
2174
2175 if (get_bits1(gb) != 0) { /* is_ol_id */
2176 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
2177 skip_bits(gb, 3); /* vo_priority */
2178 } else {
2179 vo_ver_id = 1;
2180 }
2181 s->aspect_ratio_info = get_bits(gb, 4);
2182 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
2183 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2184 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2185 } else {
2186 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
2187 }
2188
2189 if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2190 int chroma_format = get_bits(gb, 2);
2191 if (chroma_format != CHROMA_420)
2192 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2193
2194 s->low_delay = get_bits1(gb);
2195 if (get_bits1(gb)) { /* vbv parameters */
2196 get_bits(gb, 15); /* first_half_bitrate */
2197 check_marker(s->avctx, gb, "after first_half_bitrate");
2198 get_bits(gb, 15); /* latter_half_bitrate */
2199 check_marker(s->avctx, gb, "after latter_half_bitrate");
2200 get_bits(gb, 15); /* first_half_vbv_buffer_size */
2201 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2202 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
2203 get_bits(gb, 11); /* first_half_vbv_occupancy */
2204 check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
2205 get_bits(gb, 15); /* latter_half_vbv_occupancy */
2206 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2207 }
2208 } else {
2209 /* is setting low delay flag only once the smartest thing to do?
2210 * low delay detection will not be overridden. */
2211 if (s->picture_number == 0) {
2212 switch(s->vo_type) {
2213 case SIMPLE_VO_TYPE:
2214 case ADV_SIMPLE_VO_TYPE:
2215 s->low_delay = 1;
2216 break;
2217 default:
2218 s->low_delay = 0;
2219 }
2220 }
2221 }
2222
2223 ctx->shape = get_bits(gb, 2); /* vol shape */
2224 if (ctx->shape != RECT_SHAPE)
2225 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2226 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2227 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2228 skip_bits(gb, 4); /* video_object_layer_shape_extension */
2229 }
2230
2231 check_marker(s->avctx, gb, "before time_increment_resolution");
2232
2233 s->avctx->framerate.num = get_bits(gb, 16);
2234 if (!s->avctx->framerate.num) {
2235 av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
2236 return AVERROR_INVALIDDATA;
2237 }
2238
2239 ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
2240 if (ctx->time_increment_bits < 1)
2241 ctx->time_increment_bits = 1;
2242
2243 check_marker(s->avctx, gb, "before fixed_vop_rate");
2244
2245 if (get_bits1(gb) != 0) /* fixed_vop_rate */
2246 s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
2247 else
2248 s->avctx->framerate.den = 1;
2249
2250 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2251
2252 ctx->t_frame = 0;
2253
2254 if (ctx->shape != BIN_ONLY_SHAPE) {
2255 if (ctx->shape == RECT_SHAPE) {
2256 check_marker(s->avctx, gb, "before width");
2257 width = get_bits(gb, 13);
2258 check_marker(s->avctx, gb, "before height");
2259 height = get_bits(gb, 13);
2260 check_marker(s->avctx, gb, "after height");
2261 if (width && height && /* they should be non zero but who knows */
2262 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
2263 if (s->width && s->height &&
2264 (s->width != width || s->height != height))
2265 s->context_reinit = 1;
2266 s->width = width;
2267 s->height = height;
2268 }
2269 }
2270
2271 s->progressive_sequence =
2272 s->progressive_frame = get_bits1(gb) ^ 1;
2273 s->interlaced_dct = 0;
2274 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
2275 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
2276 "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2277 if (vo_ver_id == 1)
2278 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
2279 else
2280 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
2281
2282 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2283 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2284 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2285 ctx->vol_sprite_usage == GMC_SPRITE) {
2286 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2287 skip_bits(gb, 13); // sprite_width
2288 check_marker(s->avctx, gb, "after sprite_width");
2289 skip_bits(gb, 13); // sprite_height
2290 check_marker(s->avctx, gb, "after sprite_height");
2291 skip_bits(gb, 13); // sprite_left
2292 check_marker(s->avctx, gb, "after sprite_left");
2293 skip_bits(gb, 13); // sprite_top
2294 check_marker(s->avctx, gb, "after sprite_top");
2295 }
2296 ctx->num_sprite_warping_points = get_bits(gb, 6);
2297 if (ctx->num_sprite_warping_points > 3) {
2298 av_log(s->avctx, AV_LOG_ERROR,
2299 "%d sprite_warping_points\n",
2300 ctx->num_sprite_warping_points);
2301 ctx->num_sprite_warping_points = 0;
2302 return AVERROR_INVALIDDATA;
2303 }
2304 s->sprite_warping_accuracy = get_bits(gb, 2);
2305 ctx->sprite_brightness_change = get_bits1(gb);
2306 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2307 skip_bits1(gb); // low_latency_sprite
2308 }
2309 // FIXME sadct disable bit if verid!=1 && shape not rect
2310
2311 if (get_bits1(gb) == 1) { /* not_8_bit */
2312 s->quant_precision = get_bits(gb, 4); /* quant_precision */
2313 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
2314 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
2315 if (s->quant_precision != 5)
2316 av_log(s->avctx, AV_LOG_ERROR,
2317 "quant precision %d\n", s->quant_precision);
2318 if (s->quant_precision<3 || s->quant_precision>9) {
2319 s->quant_precision = 5;
2320 }
2321 } else {
2322 s->quant_precision = 5;
2323 }
2324
2325 // FIXME a bunch of grayscale shape things
2326
2327 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2328 int i, v;
2329
2330 mpeg4_load_default_matrices(s);
2331
2332 /* load custom intra matrix */
2333 if (get_bits1(gb)) {
2334 int last = 0;
2335 for (i = 0; i < 64; i++) {
2336 int j;
2337 if (get_bits_left(gb) < 8) {
2338 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2339 return AVERROR_INVALIDDATA;
2340 }
2341 v = get_bits(gb, 8);
2342 if (v == 0)
2343 break;
2344
2345 last = v;
2346 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2347 s->intra_matrix[j] = last;
2348 s->chroma_intra_matrix[j] = last;
2349 }
2350
2351 /* replicate last value */
2352 for (; i < 64; i++) {
2353 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2354 s->intra_matrix[j] = last;
2355 s->chroma_intra_matrix[j] = last;
2356 }
2357 }
2358
2359 /* load custom non intra matrix */
2360 if (get_bits1(gb)) {
2361 int last = 0;
2362 for (i = 0; i < 64; i++) {
2363 int j;
2364 if (get_bits_left(gb) < 8) {
2365 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2366 return AVERROR_INVALIDDATA;
2367 }
2368 v = get_bits(gb, 8);
2369 if (v == 0)
2370 break;
2371
2372 last = v;
2373 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2374 s->inter_matrix[j] = v;
2375 s->chroma_inter_matrix[j] = v;
2376 }
2377
2378 /* replicate last value */
2379 for (; i < 64; i++) {
2380 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2381 s->inter_matrix[j] = last;
2382 s->chroma_inter_matrix[j] = last;
2383 }
2384 }
2385
2386 // FIXME a bunch of grayscale shape things
2387 }
2388
2389 if (vo_ver_id != 1)
2390 s->quarter_sample = get_bits1(gb);
2391 else
2392 s->quarter_sample = 0;
2393
2394 if (get_bits_left(gb) < 4) {
2395 av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2396 return AVERROR_INVALIDDATA;
2397 }
2398
2399 if (!get_bits1(gb)) {
2400 int pos = get_bits_count(gb);
2401 int estimation_method = get_bits(gb, 2);
2402 if (estimation_method < 2) {
2403 if (!get_bits1(gb)) {
2404 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2405 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2406 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2407 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2408 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2409 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2410 }
2411 if (!get_bits1(gb)) {
2412 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2413 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2414 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2415 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2416 }
2417 if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2418 skip_bits_long(gb, pos - get_bits_count(gb));
2419 goto no_cplx_est;
2420 }
2421 if (!get_bits1(gb)) {
2422 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2423 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2424 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2425 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2426 }
2427 if (!get_bits1(gb)) {
2428 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2429 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2430 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2431 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2432 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2433 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2434 }
2435 if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2436 skip_bits_long(gb, pos - get_bits_count(gb));
2437 goto no_cplx_est;
2438 }
2439 if (estimation_method == 1) {
2440 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2441 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2442 }
2443 } else
2444 av_log(s->avctx, AV_LOG_ERROR,
2445 "Invalid Complexity estimation method %d\n",
2446 estimation_method);
2447 } else {
2448
2449 no_cplx_est:
2450 ctx->cplx_estimation_trash_i =
2451 ctx->cplx_estimation_trash_p =
2452 ctx->cplx_estimation_trash_b = 0;
2453 }
2454
2455 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2456
2457 s->data_partitioning = get_bits1(gb);
2458 if (s->data_partitioning)
2459 ctx->rvlc = get_bits1(gb);
2460
2461 if (vo_ver_id != 1) {
2462 ctx->new_pred = get_bits1(gb);
2463 if (ctx->new_pred) {
2464 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2465 skip_bits(gb, 2); /* requested upstream message type */
2466 skip_bits1(gb); /* newpred segment type */
2467 }
2468 if (get_bits1(gb)) // reduced_res_vop
2469 av_log(s->avctx, AV_LOG_ERROR,
2470 "reduced resolution VOP not supported\n");
2471 } else {
2472 ctx->new_pred = 0;
2473 }
2474
2475 ctx->scalability = get_bits1(gb);
2476
2477 if (ctx->scalability) {
2478 GetBitContext bak = *gb;
2479 int h_sampling_factor_n;
2480 int h_sampling_factor_m;
2481 int v_sampling_factor_n;
2482 int v_sampling_factor_m;
2483
2484 skip_bits1(gb); // hierarchy_type
2485 skip_bits(gb, 4); /* ref_layer_id */
2486 skip_bits1(gb); /* ref_layer_sampling_dir */
2487 h_sampling_factor_n = get_bits(gb, 5);
2488 h_sampling_factor_m = get_bits(gb, 5);
2489 v_sampling_factor_n = get_bits(gb, 5);
2490 v_sampling_factor_m = get_bits(gb, 5);
2491 ctx->enhancement_type = get_bits1(gb);
2492
2493 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2494 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2495 /* illegal scalability header (VERY broken encoder),
2496 * trying to workaround */
2497 ctx->scalability = 0;
2498 *gb = bak;
2499 } else
2500 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2501
2502 // bin shape stuff FIXME
2503 }
2504 }
2505
2506 if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2507 av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d %s%s%s%s\n",
2508 s->avctx->framerate.den, s->avctx->framerate.num,
2509 ctx->time_increment_bits,
2510 s->quant_precision,
2511 s->progressive_sequence,
2512 s->low_delay,
2513 ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2514 s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2515 );
2516 }
2517
2518 return 0;
2519 }
2520
2521 /**
2522 * Decode the user data stuff in the header.
2523 * Also initializes divx/xvid/lavc_version/build.
2524 */
decode_user_data(Mpeg4DecContext * ctx,GetBitContext * gb)2525 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2526 {
2527 MpegEncContext *s = &ctx->m;
2528 char buf[256];
2529 int i;
2530 int e;
2531 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2532 char last;
2533
2534 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2535 if (show_bits(gb, 23) == 0)
2536 break;
2537 buf[i] = get_bits(gb, 8);
2538 }
2539 buf[i] = 0;
2540
2541 /* divx detection */
2542 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2543 if (e < 2)
2544 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2545 if (e >= 2) {
2546 ctx->divx_version = ver;
2547 ctx->divx_build = build;
2548 s->divx_packed = e == 3 && last == 'p';
2549 }
2550
2551 /* libavcodec detection */
2552 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2553 if (e != 4)
2554 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2555 if (e != 4) {
2556 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2557 if (e > 1) {
2558 if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
2559 av_log(s->avctx, AV_LOG_WARNING,
2560 "Unknown Lavc version string encountered, %d.%d.%d; "
2561 "clamping sub-version values to 8-bits.\n",
2562 ver, ver2, ver3);
2563 }
2564 build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
2565 }
2566 }
2567 if (e != 4) {
2568 if (strcmp(buf, "ffmpeg") == 0)
2569 ctx->lavc_build = 4600;
2570 }
2571 if (e == 4)
2572 ctx->lavc_build = build;
2573
2574 /* Xvid detection */
2575 e = sscanf(buf, "XviD%d", &build);
2576 if (e == 1)
2577 ctx->xvid_build = build;
2578
2579 return 0;
2580 }
2581
ff_mpeg4_workaround_bugs(AVCodecContext * avctx)2582 int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
2583 {
2584 Mpeg4DecContext *ctx = avctx->priv_data;
2585 MpegEncContext *s = &ctx->m;
2586
2587 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2588 if (s->codec_tag == AV_RL32("XVID") ||
2589 s->codec_tag == AV_RL32("XVIX") ||
2590 s->codec_tag == AV_RL32("RMP4") ||
2591 s->codec_tag == AV_RL32("ZMP4") ||
2592 s->codec_tag == AV_RL32("SIPP"))
2593 ctx->xvid_build = 0;
2594 }
2595
2596 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2597 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2598 ctx->vol_control_parameters == 0)
2599 ctx->divx_version = 400; // divx 4
2600
2601 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2602 ctx->divx_version =
2603 ctx->divx_build = -1;
2604 }
2605
2606 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2607 if (s->codec_tag == AV_RL32("XVIX"))
2608 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2609
2610 if (s->codec_tag == AV_RL32("UMP4"))
2611 s->workaround_bugs |= FF_BUG_UMP4;
2612
2613 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2614 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2615
2616 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2617 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2618
2619 if (ctx->xvid_build <= 3U)
2620 s->padding_bug_score = 256 * 256 * 256 * 64;
2621
2622 if (ctx->xvid_build <= 1U)
2623 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2624
2625 if (ctx->xvid_build <= 12U)
2626 s->workaround_bugs |= FF_BUG_EDGE;
2627
2628 if (ctx->xvid_build <= 32U)
2629 s->workaround_bugs |= FF_BUG_DC_CLIP;
2630
2631 #define SET_QPEL_FUNC(postfix1, postfix2) \
2632 s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2633 s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2634 s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2635
2636 if (ctx->lavc_build < 4653U)
2637 s->workaround_bugs |= FF_BUG_STD_QPEL;
2638
2639 if (ctx->lavc_build < 4655U)
2640 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2641
2642 if (ctx->lavc_build < 4670U)
2643 s->workaround_bugs |= FF_BUG_EDGE;
2644
2645 if (ctx->lavc_build <= 4712U)
2646 s->workaround_bugs |= FF_BUG_DC_CLIP;
2647
2648 if ((ctx->lavc_build&0xFF) >= 100) {
2649 if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2650 (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2651 )
2652 s->workaround_bugs |= FF_BUG_IEDGE;
2653 }
2654
2655 if (ctx->divx_version >= 0)
2656 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2657 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2658 s->padding_bug_score = 256 * 256 * 256 * 64;
2659
2660 if (ctx->divx_version < 500U)
2661 s->workaround_bugs |= FF_BUG_EDGE;
2662
2663 if (ctx->divx_version >= 0)
2664 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2665 }
2666
2667 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2668 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2669 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2670 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2671 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2672 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2673 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2674
2675 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2676 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2677 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2678 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2679 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2680 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2681 }
2682
2683 if (avctx->debug & FF_DEBUG_BUGS)
2684 av_log(s->avctx, AV_LOG_DEBUG,
2685 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2686 s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2687 ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2688
2689 if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2690 s->codec_id == AV_CODEC_ID_MPEG4 &&
2691 avctx->idct_algo == FF_IDCT_AUTO) {
2692 avctx->idct_algo = FF_IDCT_XVID;
2693 ff_mpv_idct_init(s);
2694 return 1;
2695 }
2696
2697 return 0;
2698 }
2699
decode_vop_header(Mpeg4DecContext * ctx,GetBitContext * gb)2700 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2701 {
2702 MpegEncContext *s = &ctx->m;
2703 int time_incr, time_increment;
2704 int64_t pts;
2705
2706 s->mcsel = 0;
2707 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2708 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2709 ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
2710 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2711 s->low_delay = 0;
2712 }
2713
2714 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2715 if (s->partitioned_frame)
2716 s->decode_mb = mpeg4_decode_partitioned_mb;
2717 else
2718 s->decode_mb = mpeg4_decode_mb;
2719
2720 time_incr = 0;
2721 while (get_bits1(gb) != 0)
2722 time_incr++;
2723
2724 check_marker(s->avctx, gb, "before time_increment");
2725
2726 if (ctx->time_increment_bits == 0 ||
2727 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2728 av_log(s->avctx, AV_LOG_WARNING,
2729 "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);
2730
2731 for (ctx->time_increment_bits = 1;
2732 ctx->time_increment_bits < 16;
2733 ctx->time_increment_bits++) {
2734 if (s->pict_type == AV_PICTURE_TYPE_P ||
2735 (s->pict_type == AV_PICTURE_TYPE_S &&
2736 ctx->vol_sprite_usage == GMC_SPRITE)) {
2737 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2738 break;
2739 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2740 break;
2741 }
2742
2743 av_log(s->avctx, AV_LOG_WARNING,
2744 "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2745 if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2746 s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2747 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2748 }
2749 }
2750
2751 if (IS_3IV1)
2752 time_increment = get_bits1(gb); // FIXME investigate further
2753 else
2754 time_increment = get_bits(gb, ctx->time_increment_bits);
2755
2756 if (s->pict_type != AV_PICTURE_TYPE_B) {
2757 s->last_time_base = s->time_base;
2758 s->time_base += time_incr;
2759 s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
2760 if (s->workaround_bugs & FF_BUG_UMP4) {
2761 if (s->time < s->last_non_b_time) {
2762 /* header is not mpeg-4-compatible, broken encoder,
2763 * trying to workaround */
2764 s->time_base++;
2765 s->time += s->avctx->framerate.num;
2766 }
2767 }
2768 s->pp_time = s->time - s->last_non_b_time;
2769 s->last_non_b_time = s->time;
2770 } else {
2771 s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
2772 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2773 if (s->pp_time <= s->pb_time ||
2774 s->pp_time <= s->pp_time - s->pb_time ||
2775 s->pp_time <= 0) {
2776 /* messed up order, maybe after seeking? skipping current B-frame */
2777 return FRAME_SKIPPED;
2778 }
2779 ff_mpeg4_init_direct_mv(s);
2780
2781 if (ctx->t_frame == 0)
2782 ctx->t_frame = s->pb_time;
2783 if (ctx->t_frame == 0)
2784 ctx->t_frame = 1; // 1/0 protection
2785 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2786 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2787 s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2788 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2789 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
2790 s->pb_field_time = 2;
2791 s->pp_field_time = 4;
2792 if (!s->progressive_sequence)
2793 return FRAME_SKIPPED;
2794 }
2795 }
2796
2797 if (s->avctx->framerate.den)
2798 pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
2799 else
2800 pts = AV_NOPTS_VALUE;
2801 ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
2802
2803 check_marker(s->avctx, gb, "before vop_coded");
2804
2805 /* vop coded */
2806 if (get_bits1(gb) != 1) {
2807 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2808 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2809 return FRAME_SKIPPED;
2810 }
2811 if (ctx->new_pred)
2812 decode_new_pred(ctx, gb);
2813
2814 if (ctx->shape != BIN_ONLY_SHAPE &&
2815 (s->pict_type == AV_PICTURE_TYPE_P ||
2816 (s->pict_type == AV_PICTURE_TYPE_S &&
2817 ctx->vol_sprite_usage == GMC_SPRITE))) {
2818 /* rounding type for motion estimation */
2819 s->no_rounding = get_bits1(gb);
2820 } else {
2821 s->no_rounding = 0;
2822 }
2823 // FIXME reduced res stuff
2824
2825 if (ctx->shape != RECT_SHAPE) {
2826 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2827 skip_bits(gb, 13); /* width */
2828 check_marker(s->avctx, gb, "after width");
2829 skip_bits(gb, 13); /* height */
2830 check_marker(s->avctx, gb, "after height");
2831 skip_bits(gb, 13); /* hor_spat_ref */
2832 check_marker(s->avctx, gb, "after hor_spat_ref");
2833 skip_bits(gb, 13); /* ver_spat_ref */
2834 }
2835 skip_bits1(gb); /* change_CR_disable */
2836
2837 if (get_bits1(gb) != 0)
2838 skip_bits(gb, 8); /* constant_alpha_value */
2839 }
2840
2841 // FIXME complexity estimation stuff
2842
2843 if (ctx->shape != BIN_ONLY_SHAPE) {
2844 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2845 if (s->pict_type != AV_PICTURE_TYPE_I)
2846 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2847 if (s->pict_type == AV_PICTURE_TYPE_B)
2848 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2849
2850 if (get_bits_left(gb) < 3) {
2851 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2852 return AVERROR_INVALIDDATA;
2853 }
2854 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2855 if (!s->progressive_sequence) {
2856 s->top_field_first = get_bits1(gb);
2857 s->alternate_scan = get_bits1(gb);
2858 } else
2859 s->alternate_scan = 0;
2860 }
2861
2862 if (s->alternate_scan) {
2863 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2864 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2865 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2866 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2867 } else {
2868 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2869 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2870 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2871 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2872 }
2873
2874 if (s->pict_type == AV_PICTURE_TYPE_S) {
2875 if((ctx->vol_sprite_usage == STATIC_SPRITE ||
2876 ctx->vol_sprite_usage == GMC_SPRITE)) {
2877 if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2878 return AVERROR_INVALIDDATA;
2879 if (ctx->sprite_brightness_change)
2880 av_log(s->avctx, AV_LOG_ERROR,
2881 "sprite_brightness_change not supported\n");
2882 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2883 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2884 } else {
2885 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
2886 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
2887 }
2888 }
2889
2890 if (ctx->shape != BIN_ONLY_SHAPE) {
2891 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2892 if (s->qscale == 0) {
2893 av_log(s->avctx, AV_LOG_ERROR,
2894 "Error, header damaged or not MPEG-4 header (qscale=0)\n");
2895 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2896 }
2897
2898 if (s->pict_type != AV_PICTURE_TYPE_I) {
2899 s->f_code = get_bits(gb, 3); /* fcode_for */
2900 if (s->f_code == 0) {
2901 av_log(s->avctx, AV_LOG_ERROR,
2902 "Error, header damaged or not MPEG-4 header (f_code=0)\n");
2903 s->f_code = 1;
2904 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2905 }
2906 } else
2907 s->f_code = 1;
2908
2909 if (s->pict_type == AV_PICTURE_TYPE_B) {
2910 s->b_code = get_bits(gb, 3);
2911 if (s->b_code == 0) {
2912 av_log(s->avctx, AV_LOG_ERROR,
2913 "Error, header damaged or not MPEG4 header (b_code=0)\n");
2914 s->b_code=1;
2915 return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
2916 }
2917 } else
2918 s->b_code = 1;
2919
2920 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2921 av_log(s->avctx, AV_LOG_DEBUG,
2922 "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
2923 s->qscale, s->f_code, s->b_code,
2924 s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
2925 gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
2926 s->top_field_first, s->quarter_sample ? "q" : "h",
2927 s->data_partitioning, ctx->resync_marker,
2928 ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2929 1 - s->no_rounding, s->vo_type,
2930 ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
2931 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2932 ctx->cplx_estimation_trash_b,
2933 s->time,
2934 time_increment
2935 );
2936 }
2937
2938 if (!ctx->scalability) {
2939 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2940 skip_bits1(gb); // vop shape coding type
2941 } else {
2942 if (ctx->enhancement_type) {
2943 int load_backward_shape = get_bits1(gb);
2944 if (load_backward_shape)
2945 av_log(s->avctx, AV_LOG_ERROR,
2946 "load backward shape isn't supported\n");
2947 }
2948 skip_bits(gb, 2); // ref_select_code
2949 }
2950 }
2951 /* detect buggy encoders which don't set the low_delay flag
2952 * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
2953 * easily (although it's buggy too) */
2954 if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
2955 ctx->divx_version == -1 && s->picture_number == 0) {
2956 av_log(s->avctx, AV_LOG_WARNING,
2957 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2958 s->low_delay = 1;
2959 }
2960
2961 s->picture_number++; // better than pic number==0 always ;)
2962
2963 // FIXME add short header support
2964 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2965 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2966
2967 if (s->workaround_bugs & FF_BUG_EDGE) {
2968 s->h_edge_pos = s->width;
2969 s->v_edge_pos = s->height;
2970 }
2971 return 0;
2972 }
2973
read_quant_matrix_ext(MpegEncContext * s,GetBitContext * gb)2974 static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
2975 {
2976 int i, j, v;
2977
2978 if (get_bits1(gb)) {
2979 if (get_bits_left(gb) < 64*8)
2980 return AVERROR_INVALIDDATA;
2981 /* intra_quantiser_matrix */
2982 for (i = 0; i < 64; i++) {
2983 v = get_bits(gb, 8);
2984 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2985 s->intra_matrix[j] = v;
2986 s->chroma_intra_matrix[j] = v;
2987 }
2988 }
2989
2990 if (get_bits1(gb)) {
2991 if (get_bits_left(gb) < 64*8)
2992 return AVERROR_INVALIDDATA;
2993 /* non_intra_quantiser_matrix */
2994 for (i = 0; i < 64; i++) {
2995 get_bits(gb, 8);
2996 }
2997 }
2998
2999 if (get_bits1(gb)) {
3000 if (get_bits_left(gb) < 64*8)
3001 return AVERROR_INVALIDDATA;
3002 /* chroma_intra_quantiser_matrix */
3003 for (i = 0; i < 64; i++) {
3004 v = get_bits(gb, 8);
3005 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
3006 s->chroma_intra_matrix[j] = v;
3007 }
3008 }
3009
3010 if (get_bits1(gb)) {
3011 if (get_bits_left(gb) < 64*8)
3012 return AVERROR_INVALIDDATA;
3013 /* chroma_non_intra_quantiser_matrix */
3014 for (i = 0; i < 64; i++) {
3015 get_bits(gb, 8);
3016 }
3017 }
3018
3019 next_start_code_studio(gb);
3020 return 0;
3021 }
3022
extension_and_user_data(MpegEncContext * s,GetBitContext * gb,int id)3023 static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
3024 {
3025 uint32_t startcode;
3026 uint8_t extension_type;
3027
3028 startcode = show_bits_long(gb, 32);
3029 if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
3030
3031 if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
3032 skip_bits_long(gb, 32);
3033 extension_type = get_bits(gb, 4);
3034 if (extension_type == QUANT_MATRIX_EXT_ID)
3035 read_quant_matrix_ext(s, gb);
3036 }
3037 }
3038 }
3039
decode_smpte_tc(Mpeg4DecContext * ctx,GetBitContext * gb)3040 static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
3041 {
3042 MpegEncContext *s = &ctx->m;
3043
3044 skip_bits(gb, 16); /* Time_code[63..48] */
3045 check_marker(s->avctx, gb, "after Time_code[63..48]");
3046 skip_bits(gb, 16); /* Time_code[47..32] */
3047 check_marker(s->avctx, gb, "after Time_code[47..32]");
3048 skip_bits(gb, 16); /* Time_code[31..16] */
3049 check_marker(s->avctx, gb, "after Time_code[31..16]");
3050 skip_bits(gb, 16); /* Time_code[15..0] */
3051 check_marker(s->avctx, gb, "after Time_code[15..0]");
3052 skip_bits(gb, 4); /* reserved_bits */
3053 }
3054
3055 /**
3056 * Decode the next studio vop header.
3057 * @return <0 if something went wrong
3058 */
decode_studio_vop_header(Mpeg4DecContext * ctx,GetBitContext * gb)3059 static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
3060 {
3061 MpegEncContext *s = &ctx->m;
3062
3063 if (get_bits_left(gb) <= 32)
3064 return 0;
3065
3066 s->partitioned_frame = 0;
3067 s->interlaced_dct = 0;
3068 s->decode_mb = mpeg4_decode_studio_mb;
3069
3070 decode_smpte_tc(ctx, gb);
3071
3072 skip_bits(gb, 10); /* temporal_reference */
3073 skip_bits(gb, 2); /* vop_structure */
3074 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
3075 if (get_bits1(gb)) { /* vop_coded */
3076 skip_bits1(gb); /* top_field_first */
3077 skip_bits1(gb); /* repeat_first_field */
3078 s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
3079 }
3080
3081 if (s->pict_type == AV_PICTURE_TYPE_I) {
3082 if (get_bits1(gb))
3083 reset_studio_dc_predictors(s);
3084 }
3085
3086 if (ctx->shape != BIN_ONLY_SHAPE) {
3087 s->alternate_scan = get_bits1(gb);
3088 s->frame_pred_frame_dct = get_bits1(gb);
3089 s->dct_precision = get_bits(gb, 2);
3090 s->intra_dc_precision = get_bits(gb, 2);
3091 s->q_scale_type = get_bits1(gb);
3092 }
3093
3094 if (s->alternate_scan) {
3095 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
3096 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
3097 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
3098 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3099 } else {
3100 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
3101 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
3102 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
3103 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3104 }
3105
3106 mpeg4_load_default_matrices(s);
3107
3108 next_start_code_studio(gb);
3109 extension_and_user_data(s, gb, 4);
3110
3111 return 0;
3112 }
3113
decode_studiovisualobject(Mpeg4DecContext * ctx,GetBitContext * gb)3114 static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
3115 {
3116 MpegEncContext *s = &ctx->m;
3117 int visual_object_type;
3118
3119 skip_bits(gb, 4); /* visual_object_verid */
3120 visual_object_type = get_bits(gb, 4);
3121 if (visual_object_type != VOT_VIDEO_ID) {
3122 avpriv_request_sample(s->avctx, "VO type %u", visual_object_type);
3123 return AVERROR_PATCHWELCOME;
3124 }
3125
3126 next_start_code_studio(gb);
3127 extension_and_user_data(s, gb, 1);
3128
3129 return 0;
3130 }
3131
decode_studio_vol_header(Mpeg4DecContext * ctx,GetBitContext * gb)3132 static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
3133 {
3134 MpegEncContext *s = &ctx->m;
3135 int width, height;
3136 int bits_per_raw_sample;
3137 int rgb, chroma_format;
3138
3139 // random_accessible_vol and video_object_type_indication have already
3140 // been read by the caller decode_vol_header()
3141 skip_bits(gb, 4); /* video_object_layer_verid */
3142 ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
3143 skip_bits(gb, 4); /* video_object_layer_shape_extension */
3144 skip_bits1(gb); /* progressive_sequence */
3145 if (ctx->shape != RECT_SHAPE) {
3146 avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape");
3147 return AVERROR_PATCHWELCOME;
3148 }
3149 if (ctx->shape != BIN_ONLY_SHAPE) {
3150 rgb = get_bits1(gb); /* rgb_components */
3151 chroma_format = get_bits(gb, 2); /* chroma_format */
3152 if (!chroma_format || chroma_format == CHROMA_420 || (rgb && chroma_format == CHROMA_422)) {
3153 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
3154 return AVERROR_INVALIDDATA;
3155 }
3156
3157 bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
3158 if (bits_per_raw_sample == 10) {
3159 if (rgb) {
3160 s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
3161 }
3162 else {
3163 s->avctx->pix_fmt = chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
3164 }
3165 }
3166 else {
3167 avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
3168 return AVERROR_PATCHWELCOME;
3169 }
3170 if (rgb != ctx->rgb || s->chroma_format != chroma_format)
3171 s->context_reinit = 1;
3172 s->avctx->bits_per_raw_sample = bits_per_raw_sample;
3173 ctx->rgb = rgb;
3174 s->chroma_format = chroma_format;
3175 }
3176 if (ctx->shape == RECT_SHAPE) {
3177 check_marker(s->avctx, gb, "before video_object_layer_width");
3178 width = get_bits(gb, 14); /* video_object_layer_width */
3179 check_marker(s->avctx, gb, "before video_object_layer_height");
3180 height = get_bits(gb, 14); /* video_object_layer_height */
3181 check_marker(s->avctx, gb, "after video_object_layer_height");
3182
3183 /* Do the same check as non-studio profile */
3184 if (width && height) {
3185 if (s->width && s->height &&
3186 (s->width != width || s->height != height))
3187 s->context_reinit = 1;
3188 s->width = width;
3189 s->height = height;
3190 }
3191 }
3192 s->aspect_ratio_info = get_bits(gb, 4);
3193 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
3194 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
3195 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
3196 } else {
3197 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
3198 }
3199 skip_bits(gb, 4); /* frame_rate_code */
3200 skip_bits(gb, 15); /* first_half_bit_rate */
3201 check_marker(s->avctx, gb, "after first_half_bit_rate");
3202 skip_bits(gb, 15); /* latter_half_bit_rate */
3203 check_marker(s->avctx, gb, "after latter_half_bit_rate");
3204 skip_bits(gb, 15); /* first_half_vbv_buffer_size */
3205 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
3206 skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
3207 skip_bits(gb, 11); /* first_half_vbv_buffer_size */
3208 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
3209 skip_bits(gb, 15); /* latter_half_vbv_occupancy */
3210 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
3211 s->low_delay = get_bits1(gb);
3212 s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
3213
3214 next_start_code_studio(gb);
3215 extension_and_user_data(s, gb, 2);
3216
3217 return 0;
3218 }
3219
3220 /**
3221 * Decode MPEG-4 headers.
3222 *
3223 * @param header If set the absence of a VOP is not treated as error; otherwise, it is treated as such.
3224 * @return <0 if an error occurred
3225 * FRAME_SKIPPED if a not coded VOP is found
3226 * 0 else
3227 */
ff_mpeg4_decode_picture_header(Mpeg4DecContext * ctx,GetBitContext * gb,int header)3228 int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, int header)
3229 {
3230 MpegEncContext *s = &ctx->m;
3231 unsigned startcode, v;
3232 int ret;
3233 int vol = 0;
3234
3235 /* search next start code */
3236 align_get_bits(gb);
3237
3238 // If we have not switched to studio profile than we also did not switch bps
3239 // that means something else (like a previous instance) outside set bps which
3240 // would be inconsistant with the currect state, thus reset it
3241 if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3242 s->avctx->bits_per_raw_sample = 0;
3243
3244 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3245 skip_bits(gb, 24);
3246 if (get_bits(gb, 8) == 0xF0)
3247 goto end;
3248 }
3249
3250 startcode = 0xff;
3251 for (;;) {
3252 if (get_bits_count(gb) >= gb->size_in_bits) {
3253 if (gb->size_in_bits == 8 &&
3254 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3255 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3256 return FRAME_SKIPPED; // divx bug
3257 } else if (header && get_bits_count(gb) == gb->size_in_bits) {
3258 return 0; // ordinary return value for parsing of extradata
3259 } else
3260 return AVERROR_INVALIDDATA; // end of stream
3261 }
3262
3263 /* use the bits after the test */
3264 v = get_bits(gb, 8);
3265 startcode = ((startcode << 8) | v) & 0xffffffff;
3266
3267 if ((startcode & 0xFFFFFF00) != 0x100)
3268 continue; // no startcode
3269
3270 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3271 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
3272 if (startcode <= 0x11F)
3273 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
3274 else if (startcode <= 0x12F)
3275 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
3276 else if (startcode <= 0x13F)
3277 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
3278 else if (startcode <= 0x15F)
3279 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
3280 else if (startcode <= 0x1AF)
3281 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
3282 else if (startcode == 0x1B0)
3283 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
3284 else if (startcode == 0x1B1)
3285 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
3286 else if (startcode == 0x1B2)
3287 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
3288 else if (startcode == 0x1B3)
3289 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
3290 else if (startcode == 0x1B4)
3291 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
3292 else if (startcode == 0x1B5)
3293 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
3294 else if (startcode == 0x1B6)
3295 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
3296 else if (startcode == 0x1B7)
3297 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
3298 else if (startcode == 0x1B8)
3299 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
3300 else if (startcode == 0x1B9)
3301 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
3302 else if (startcode == 0x1BA)
3303 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
3304 else if (startcode == 0x1BB)
3305 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
3306 else if (startcode == 0x1BC)
3307 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
3308 else if (startcode == 0x1BD)
3309 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
3310 else if (startcode == 0x1BE)
3311 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
3312 else if (startcode == 0x1BF)
3313 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
3314 else if (startcode == 0x1C0)
3315 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
3316 else if (startcode == 0x1C1)
3317 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
3318 else if (startcode == 0x1C2)
3319 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
3320 else if (startcode == 0x1C3)
3321 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
3322 else if (startcode <= 0x1C5)
3323 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
3324 else if (startcode <= 0x1FF)
3325 av_log(s->avctx, AV_LOG_DEBUG, "System start");
3326 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
3327 }
3328
3329 if (startcode >= 0x120 && startcode <= 0x12F) {
3330 if (vol) {
3331 av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3332 continue;
3333 }
3334 vol++;
3335 if ((ret = decode_vol_header(ctx, gb)) < 0)
3336 return ret;
3337 } else if (startcode == USER_DATA_STARTCODE) {
3338 decode_user_data(ctx, gb);
3339 } else if (startcode == GOP_STARTCODE) {
3340 mpeg4_decode_gop_header(s, gb);
3341 } else if (startcode == VOS_STARTCODE) {
3342 int profile, level;
3343 mpeg4_decode_profile_level(s, gb, &profile, &level);
3344 if (profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO &&
3345 (level > 0 && level < 9)) {
3346 s->studio_profile = 1;
3347 next_start_code_studio(gb);
3348 extension_and_user_data(s, gb, 0);
3349 } else if (s->studio_profile) {
3350 avpriv_request_sample(s->avctx, "Mixes studio and non studio profile\n");
3351 return AVERROR_PATCHWELCOME;
3352 }
3353 s->avctx->profile = profile;
3354 s->avctx->level = level;
3355 } else if (startcode == VISUAL_OBJ_STARTCODE) {
3356 if (s->studio_profile) {
3357 if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3358 return ret;
3359 } else
3360 mpeg4_decode_visual_object(s, gb);
3361 } else if (startcode == VOP_STARTCODE) {
3362 break;
3363 }
3364
3365 align_get_bits(gb);
3366 startcode = 0xff;
3367 }
3368
3369 end:
3370 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
3371 s->low_delay = 1;
3372 s->avctx->has_b_frames = !s->low_delay;
3373
3374 if (s->studio_profile) {
3375 if (!s->avctx->bits_per_raw_sample) {
3376 av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n");
3377 return AVERROR_INVALIDDATA;
3378 }
3379 return decode_studio_vop_header(ctx, gb);
3380 } else
3381 return decode_vop_header(ctx, gb);
3382 }
3383
ff_mpeg4videodec_static_init(void)3384 av_cold void ff_mpeg4videodec_static_init(void) {
3385 static int done = 0;
3386
3387 if (!done) {
3388 ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
3389 ff_rl_init(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
3390 ff_rl_init(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
3391 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
3392 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
3393 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
3394 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
3395 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3396 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
3397 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
3398 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3399 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
3400 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
3401 &ff_sprite_trajectory_tab[0][1], 4, 2,
3402 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
3403 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
3404 &ff_mb_type_b_tab[0][1], 2, 1,
3405 &ff_mb_type_b_tab[0][0], 2, 1, 16);
3406 done = 1;
3407 }
3408 }
3409
ff_mpeg4_frame_end(AVCodecContext * avctx,const uint8_t * buf,int buf_size)3410 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
3411 {
3412 Mpeg4DecContext *ctx = avctx->priv_data;
3413 MpegEncContext *s = &ctx->m;
3414
3415 /* divx 5.01+ bitstream reorder stuff */
3416 /* Since this clobbers the input buffer and hwaccel codecs still need the
3417 * data during hwaccel->end_frame we should not do this any earlier */
3418 if (s->divx_packed) {
3419 int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
3420 int startcode_found = 0;
3421
3422 if (buf_size - current_pos > 7) {
3423
3424 int i;
3425 for (i = current_pos; i < buf_size - 4; i++)
3426
3427 if (buf[i] == 0 &&
3428 buf[i + 1] == 0 &&
3429 buf[i + 2] == 1 &&
3430 buf[i + 3] == 0xB6) {
3431 startcode_found = !(buf[i + 4] & 0x40);
3432 break;
3433 }
3434 }
3435
3436 if (startcode_found) {
3437 if (!ctx->showed_packed_warning) {
3438 av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
3439 "wasteful way to store B-frames ('packed B-frames'). "
3440 "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
3441 ctx->showed_packed_warning = 1;
3442 }
3443 av_fast_padded_malloc(&s->bitstream_buffer,
3444 &s->allocated_bitstream_buffer_size,
3445 buf_size - current_pos);
3446 if (!s->bitstream_buffer) {
3447 s->bitstream_buffer_size = 0;
3448 return AVERROR(ENOMEM);
3449 }
3450 memcpy(s->bitstream_buffer, buf + current_pos,
3451 buf_size - current_pos);
3452 s->bitstream_buffer_size = buf_size - current_pos;
3453 }
3454 }
3455
3456 return 0;
3457 }
3458
3459 #if HAVE_THREADS
mpeg4_update_thread_context(AVCodecContext * dst,const AVCodecContext * src)3460 static int mpeg4_update_thread_context(AVCodecContext *dst,
3461 const AVCodecContext *src)
3462 {
3463 Mpeg4DecContext *s = dst->priv_data;
3464 const Mpeg4DecContext *s1 = src->priv_data;
3465 int init = s->m.context_initialized;
3466
3467 int ret = ff_mpeg_update_thread_context(dst, src);
3468
3469 if (ret < 0)
3470 return ret;
3471
3472 // copy all the necessary fields explicitly
3473 s->time_increment_bits = s1->time_increment_bits;
3474 s->shape = s1->shape;
3475 s->vol_sprite_usage = s1->vol_sprite_usage;
3476 s->sprite_brightness_change = s1->sprite_brightness_change;
3477 s->num_sprite_warping_points = s1->num_sprite_warping_points;
3478 s->rvlc = s1->rvlc;
3479 s->resync_marker = s1->resync_marker;
3480 s->t_frame = s1->t_frame;
3481 s->new_pred = s1->new_pred;
3482 s->enhancement_type = s1->enhancement_type;
3483 s->scalability = s1->scalability;
3484 s->use_intra_dc_vlc = s1->use_intra_dc_vlc;
3485 s->intra_dc_threshold = s1->intra_dc_threshold;
3486 s->divx_version = s1->divx_version;
3487 s->divx_build = s1->divx_build;
3488 s->xvid_build = s1->xvid_build;
3489 s->lavc_build = s1->lavc_build;
3490 s->showed_packed_warning = s1->showed_packed_warning;
3491 s->vol_control_parameters = s1->vol_control_parameters;
3492 s->cplx_estimation_trash_i = s1->cplx_estimation_trash_i;
3493 s->cplx_estimation_trash_p = s1->cplx_estimation_trash_p;
3494 s->cplx_estimation_trash_b = s1->cplx_estimation_trash_b;
3495 s->rgb = s1->rgb;
3496
3497 memcpy(s->sprite_shift, s1->sprite_shift, sizeof(s1->sprite_shift));
3498 memcpy(s->sprite_traj, s1->sprite_traj, sizeof(s1->sprite_traj));
3499
3500 if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
3501 ff_xvid_idct_init(&s->m.idsp, dst);
3502
3503 return 0;
3504 }
3505 #endif
3506
init_studio_vlcs(Mpeg4DecContext * ctx)3507 static av_cold int init_studio_vlcs(Mpeg4DecContext *ctx)
3508 {
3509 int i, ret;
3510
3511 for (i = 0; i < 12; i++) {
3512 ret = init_vlc(&ctx->studio_intra_tab[i], STUDIO_INTRA_BITS, 22,
3513 &ff_mpeg4_studio_intra[i][0][1], 4, 2,
3514 &ff_mpeg4_studio_intra[i][0][0], 4, 2,
3515 0);
3516
3517 if (ret < 0)
3518 return ret;
3519 }
3520
3521 ret = init_vlc(&ctx->studio_luma_dc, STUDIO_INTRA_BITS, 19,
3522 &ff_mpeg4_studio_dc_luma[0][1], 4, 2,
3523 &ff_mpeg4_studio_dc_luma[0][0], 4, 2,
3524 0);
3525 if (ret < 0)
3526 return ret;
3527
3528 ret = init_vlc(&ctx->studio_chroma_dc, STUDIO_INTRA_BITS, 19,
3529 &ff_mpeg4_studio_dc_chroma[0][1], 4, 2,
3530 &ff_mpeg4_studio_dc_chroma[0][0], 4, 2,
3531 0);
3532 if (ret < 0)
3533 return ret;
3534
3535 return 0;
3536 }
3537
decode_init(AVCodecContext * avctx)3538 static av_cold int decode_init(AVCodecContext *avctx)
3539 {
3540 Mpeg4DecContext *ctx = avctx->priv_data;
3541 MpegEncContext *s = &ctx->m;
3542 int ret;
3543
3544 ctx->divx_version =
3545 ctx->divx_build =
3546 ctx->xvid_build =
3547 ctx->lavc_build = -1;
3548
3549 if ((ret = ff_h263_decode_init(avctx)) < 0)
3550 return ret;
3551
3552 ff_mpeg4videodec_static_init();
3553 if ((ret = init_studio_vlcs(ctx)) < 0)
3554 return ret;
3555
3556 s->h263_pred = 1;
3557 s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
3558 s->decode_mb = mpeg4_decode_mb;
3559 ctx->time_increment_bits = 4; /* default value for broken headers */
3560
3561 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
3562
3563 return 0;
3564 }
3565
decode_end(AVCodecContext * avctx)3566 static av_cold int decode_end(AVCodecContext *avctx)
3567 {
3568 Mpeg4DecContext *ctx = avctx->priv_data;
3569 int i;
3570
3571 for (i = 0; i < 12; i++)
3572 ff_free_vlc(&ctx->studio_intra_tab[i]);
3573
3574 ff_free_vlc(&ctx->studio_luma_dc);
3575 ff_free_vlc(&ctx->studio_chroma_dc);
3576
3577 return ff_h263_decode_end(avctx);
3578 }
3579
3580 static const AVOption mpeg4_options[] = {
3581 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
3582 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
3583 {NULL}
3584 };
3585
3586 static const AVClass mpeg4_class = {
3587 .class_name = "MPEG4 Video Decoder",
3588 .item_name = av_default_item_name,
3589 .option = mpeg4_options,
3590 .version = LIBAVUTIL_VERSION_INT,
3591 };
3592
3593 AVCodec ff_mpeg4_decoder = {
3594 .name = "mpeg4",
3595 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
3596 .type = AVMEDIA_TYPE_VIDEO,
3597 .id = AV_CODEC_ID_MPEG4,
3598 .priv_data_size = sizeof(Mpeg4DecContext),
3599 .init = decode_init,
3600 .close = decode_end,
3601 .decode = ff_h263_decode_frame,
3602 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
3603 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
3604 AV_CODEC_CAP_FRAME_THREADS,
3605 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
3606 FF_CODEC_CAP_ALLOCATE_PROGRESS,
3607 .flush = ff_mpeg_flush,
3608 .max_lowres = 3,
3609 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
3610 .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles),
3611 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
3612 .priv_class = &mpeg4_class,
3613 .hw_configs = (const AVCodecHWConfigInternal*[]) {
3614 #if CONFIG_MPEG4_NVDEC_HWACCEL
3615 HWACCEL_NVDEC(mpeg4),
3616 #endif
3617 #if CONFIG_MPEG4_VAAPI_HWACCEL
3618 HWACCEL_VAAPI(mpeg4),
3619 #endif
3620 #if CONFIG_MPEG4_VDPAU_HWACCEL
3621 HWACCEL_VDPAU(mpeg4),
3622 #endif
3623 #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
3624 HWACCEL_VIDEOTOOLBOX(mpeg4),
3625 #endif
3626 NULL
3627 },
3628 };
3629