mpeg4videodec: move mpeg4-specific bug detection from h263 generic code to mpeg4
[libav.git] / libavcodec / mpeg4videodec.c
CommitLineData
ca334dd1
MN
1/*
2 * MPEG4 decoder.
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5 *
2912e87a 6 * This file is part of Libav.
ca334dd1 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ca334dd1
MN
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
ca334dd1
MN
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
ca334dd1
MN
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
5f401b7b 23#include "error_resilience.h"
759001c5 24#include "internal.h"
ca334dd1
MN
25#include "mpegvideo.h"
26#include "mpeg4video.h"
27#include "h263.h"
6a9c8594 28#include "thread.h"
ca334dd1 29
d234c7a0
VG
30/* The defines below define the number of bits that are read at once for
31 * reading vlc values. Changing these may improve speed and data cache needs
32 * be aware though that decreasing them may need the number of stages that is
33 * passed to get_vlc* to be increased. */
ca334dd1
MN
34#define SPRITE_TRAJ_VLC_BITS 6
35#define DC_VLC_BITS 9
36#define MB_TYPE_B_VLC_BITS 4
37
ca334dd1
MN
38static VLC dc_lum, dc_chrom;
39static VLC sprite_trajectory;
40static VLC mb_type_b_vlc;
41
d234c7a0 42static const int mb_type_b_map[4] = {
ca334dd1 43 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
d234c7a0
VG
44 MB_TYPE_L0L1 | MB_TYPE_16x16,
45 MB_TYPE_L1 | MB_TYPE_16x16,
46 MB_TYPE_L0 | MB_TYPE_16x16,
ca334dd1
MN
47};
48
49/**
58c42af7 50 * Predict the ac.
ca334dd1
MN
51 * @param n block index (0-3 are luma, 4-5 are chroma)
52 * @param dir the ac prediction direction
53 */
d234c7a0 54void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
ca334dd1
MN
55{
56 int i;
57 int16_t *ac_val, *ac_val1;
d234c7a0 58 int8_t *const qscale_table = s->current_picture.qscale_table;
ca334dd1
MN
59
60 /* find prediction */
d234c7a0 61 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
ca334dd1
MN
62 ac_val1 = ac_val;
63 if (s->ac_pred) {
64 if (dir == 0) {
d234c7a0 65 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
ca334dd1
MN
66 /* left prediction */
67 ac_val -= 16;
68
d234c7a0
VG
69 if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
70 n == 1 || n == 3) {
ca334dd1 71 /* same qscale */
d234c7a0
VG
72 for (i = 1; i < 8; i++)
73 block[s->dsp.idct_permutation[i << 3]] += ac_val[i];
74 } else {
ca334dd1 75 /* different qscale, we must rescale */
d234c7a0
VG
76 for (i = 1; i < 8; i++)
77 block[s->dsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
ca334dd1
MN
78 }
79 } else {
d234c7a0 80 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
ca334dd1
MN
81 /* top prediction */
82 ac_val -= 16 * s->block_wrap[n];
83
d234c7a0
VG
84 if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
85 n == 2 || n == 3) {
ca334dd1 86 /* same qscale */
d234c7a0 87 for (i = 1; i < 8; i++)
ca334dd1 88 block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
d234c7a0 89 } else {
ca334dd1 90 /* different qscale, we must rescale */
d234c7a0
VG
91 for (i = 1; i < 8; i++)
92 block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
ca334dd1
MN
93 }
94 }
95 }
96 /* left copy */
d234c7a0
VG
97 for (i = 1; i < 8; i++)
98 ac_val1[i] = block[s->dsp.idct_permutation[i << 3]];
ca334dd1
MN
99
100 /* top copy */
d234c7a0
VG
101 for (i = 1; i < 8; i++)
102 ac_val1[8 + i] = block[s->dsp.idct_permutation[i]];
ca334dd1
MN
103}
104
105/**
106 * check if the next stuff is a resync marker or the end.
107 * @return 0 if not
108 */
d234c7a0
VG
109static inline int mpeg4_is_resync(MpegEncContext *s)
110{
111 int bits_count = get_bits_count(&s->gb);
112 int v = show_bits(&s->gb, 16);
ca334dd1 113
d234c7a0 114 if (s->workaround_bugs & FF_BUG_NO_PADDING)
ca334dd1 115 return 0;
ca334dd1 116
d234c7a0
VG
117 while (v <= 0xFF) {
118 if (s->pict_type == AV_PICTURE_TYPE_B ||
119 (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
ca334dd1 120 break;
d234c7a0
VG
121 skip_bits(&s->gb, 8 + s->pict_type);
122 bits_count += 8 + s->pict_type;
123 v = show_bits(&s->gb, 16);
ca334dd1
MN
124 }
125
d234c7a0
VG
126 if (bits_count + 8 >= s->gb.size_in_bits) {
127 v >>= 8;
128 v |= 0x7F >> (7 - (bits_count & 7));
ca334dd1 129
d234c7a0 130 if (v == 0x7F)
ca334dd1 131 return 1;
d234c7a0
VG
132 } else {
133 if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
ca334dd1 134 int len;
d234c7a0 135 GetBitContext gb = s->gb;
ca334dd1
MN
136
137 skip_bits(&s->gb, 1);
138 align_get_bits(&s->gb);
139
d234c7a0
VG
140 for (len = 0; len < 32; len++)
141 if (get_bits1(&s->gb))
142 break;
ca334dd1 143
d234c7a0 144 s->gb = gb;
ca334dd1 145
d234c7a0 146 if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
ca334dd1
MN
147 return 1;
148 }
149 }
150 return 0;
151}
152
f875a732 153static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
ca334dd1 154{
d234c7a0
VG
155 int a = 2 << s->sprite_warping_accuracy;
156 int rho = 3 - s->sprite_warping_accuracy;
157 int r = 16 / a;
158 int alpha = 0;
159 int beta = 0;
160 int w = s->width;
161 int h = s->height;
162 int min_ab, i, w2, h2, w3, h3;
ca334dd1
MN
163 int sprite_ref[4][2];
164 int virtual_ref[2][2];
d234c7a0
VG
165
166 // only true for rectangle shapes
167 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
168 { 0, s->height }, { s->width, s->height } };
169 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
ca334dd1 170
f875a732
MS
171 if (w <= 0 || h <= 0)
172 return AVERROR_INVALIDDATA;
173
d234c7a0 174 for (i = 0; i < s->num_sprite_warping_points; i++) {
ca334dd1 175 int length;
d234c7a0 176 int x = 0, y = 0;
ca334dd1 177
d234c7a0
VG
178 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
179 if (length)
180 x = get_xbits(gb, length);
ca334dd1 181
d234c7a0
VG
182 if (!(s->divx_version == 500 && s->divx_build == 413))
183 skip_bits1(gb); /* marker bit */
184
185 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
186 if (length)
187 y = get_xbits(gb, length);
188
189 skip_bits1(gb); /* marker bit */
190 s->sprite_traj[i][0] = d[i][0] = x;
191 s->sprite_traj[i][1] = d[i][1] = y;
ca334dd1 192 }
d234c7a0
VG
193 for (; i < 4; i++)
194 s->sprite_traj[i][0] = s->sprite_traj[i][1] = 0;
195
196 while ((1 << alpha) < w)
197 alpha++;
198 while ((1 << beta) < h)
199 beta++; /* typo in the mpeg4 std for the definition of w' and h' */
200 w2 = 1 << alpha;
201 h2 = 1 << beta;
202
203 // Note, the 4th point isn't used for GMC
204 if (s->divx_version == 500 && s->divx_build == 413) {
205 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
206 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
207 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
208 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
209 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
210 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
ca334dd1 211 } else {
d234c7a0
VG
212 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
213 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
214 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
215 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
216 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
217 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
ca334dd1 218 }
d234c7a0
VG
219 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
220 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
221
222 /* this is mostly identical to the mpeg4 std (and is totally unreadable
223 * because of that...). Perhaps it should be reordered to be more readable.
224 * The idea behind this virtual_ref mess is to be able to use shifts later
225 * per pixel instead of divides so the distance between points is converted
226 * from w&h based to w2&h2 based which are of the 2^x form. */
227 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
228 ROUNDED_DIV(((w - w2) *
229 (r * sprite_ref[0][0] - 16 * vop_ref[0][0]) +
230 w2 * (r * sprite_ref[1][0] - 16 * vop_ref[1][0])), w);
231 virtual_ref[0][1] = 16 * vop_ref[0][1] +
232 ROUNDED_DIV(((w - w2) *
233 (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
234 w2 * (r * sprite_ref[1][1] - 16 * vop_ref[1][1])), w);
235 virtual_ref[1][0] = 16 * vop_ref[0][0] +
236 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16 * vop_ref[0][0]) +
237 h2 * (r * sprite_ref[2][0] - 16 * vop_ref[2][0])), h);
238 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
239 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
240 h2 * (r * sprite_ref[2][1] - 16 * vop_ref[2][1])), h);
241
242 switch (s->num_sprite_warping_points) {
243 case 0:
244 s->sprite_offset[0][0] =
245 s->sprite_offset[0][1] =
246 s->sprite_offset[1][0] =
247 s->sprite_offset[1][1] = 0;
248 s->sprite_delta[0][0] = a;
249 s->sprite_delta[0][1] =
250 s->sprite_delta[1][0] = 0;
251 s->sprite_delta[1][1] = a;
252 s->sprite_shift[0] =
253 s->sprite_shift[1] = 0;
254 break;
255 case 1: // GMC only
256 s->sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
257 s->sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
258 s->sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
259 a * (vop_ref[0][0] / 2);
260 s->sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
261 a * (vop_ref[0][1] / 2);
262 s->sprite_delta[0][0] = a;
263 s->sprite_delta[0][1] =
264 s->sprite_delta[1][0] = 0;
265 s->sprite_delta[1][1] = a;
266 s->sprite_shift[0] =
267 s->sprite_shift[1] = 0;
268 break;
269 case 2:
270 s->sprite_offset[0][0] = (sprite_ref[0][0] << (alpha + rho)) +
271 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
272 (-vop_ref[0][0]) +
273 (r * sprite_ref[0][1] - virtual_ref[0][1]) *
274 (-vop_ref[0][1]) + (1 << (alpha + rho - 1));
275 s->sprite_offset[0][1] = (sprite_ref[0][1] << (alpha + rho)) +
276 (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
277 (-vop_ref[0][0]) +
278 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
279 (-vop_ref[0][1]) + (1 << (alpha + rho - 1));
280 s->sprite_offset[1][0] = ((-r * sprite_ref[0][0] + virtual_ref[0][0]) *
281 (-2 * vop_ref[0][0] + 1) +
282 (r * sprite_ref[0][1] - virtual_ref[0][1]) *
283 (-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
284 sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
285 s->sprite_offset[1][1] = ((-r * sprite_ref[0][1] + virtual_ref[0][1]) *
286 (-2 * vop_ref[0][0] + 1) +
287 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
288 (-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
289 sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
290 s->sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
291 s->sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
292 s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
293 s->sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
294
295 s->sprite_shift[0] = alpha + rho;
296 s->sprite_shift[1] = alpha + rho + 2;
297 break;
298 case 3:
299 min_ab = FFMIN(alpha, beta);
300 w3 = w2 >> min_ab;
301 h3 = h2 >> min_ab;
302 s->sprite_offset[0][0] = (sprite_ref[0][0] << (alpha + beta + rho - min_ab)) +
303 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
304 h3 * (-vop_ref[0][0]) +
305 (-r * sprite_ref[0][0] + virtual_ref[1][0]) *
306 w3 * (-vop_ref[0][1]) +
307 (1 << (alpha + beta + rho - min_ab - 1));
308 s->sprite_offset[0][1] = (sprite_ref[0][1] << (alpha + beta + rho - min_ab)) +
309 (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
310 h3 * (-vop_ref[0][0]) +
311 (-r * sprite_ref[0][1] + virtual_ref[1][1]) *
312 w3 * (-vop_ref[0][1]) +
313 (1 << (alpha + beta + rho - min_ab - 1));
314 s->sprite_offset[1][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
315 h3 * (-2 * vop_ref[0][0] + 1) +
316 (-r * sprite_ref[0][0] + virtual_ref[1][0]) *
317 w3 * (-2 * vop_ref[0][1] + 1) + 2 * w2 * h3 *
318 r * sprite_ref[0][0] - 16 * w2 * h3 +
319 (1 << (alpha + beta + rho - min_ab + 1));
320 s->sprite_offset[1][1] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
321 h3 * (-2 * vop_ref[0][0] + 1) +
322 (-r * sprite_ref[0][1] + virtual_ref[1][1]) *
323 w3 * (-2 * vop_ref[0][1] + 1) + 2 * w2 * h3 *
324 r * sprite_ref[0][1] - 16 * w2 * h3 +
325 (1 << (alpha + beta + rho - min_ab + 1));
326 s->sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3;
327 s->sprite_delta[0][1] = (-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3;
328 s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3;
329 s->sprite_delta[1][1] = (-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3;
330
331 s->sprite_shift[0] = alpha + beta + rho - min_ab;
332 s->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
333 break;
ca334dd1
MN
334 }
335 /* try to simplify the situation */
d234c7a0
VG
336 if (s->sprite_delta[0][0] == a << s->sprite_shift[0] &&
337 s->sprite_delta[0][1] == 0 &&
338 s->sprite_delta[1][0] == 0 &&
339 s->sprite_delta[1][1] == a << s->sprite_shift[0]) {
340 s->sprite_offset[0][0] >>= s->sprite_shift[0];
341 s->sprite_offset[0][1] >>= s->sprite_shift[0];
342 s->sprite_offset[1][0] >>= s->sprite_shift[1];
343 s->sprite_offset[1][1] >>= s->sprite_shift[1];
344 s->sprite_delta[0][0] = a;
345 s->sprite_delta[0][1] = 0;
346 s->sprite_delta[1][0] = 0;
347 s->sprite_delta[1][1] = a;
348 s->sprite_shift[0] = 0;
349 s->sprite_shift[1] = 0;
350 s->real_sprite_warping_points = 1;
351 } else {
352 int shift_y = 16 - s->sprite_shift[0];
353 int shift_c = 16 - s->sprite_shift[1];
354 for (i = 0; i < 2; i++) {
355 s->sprite_offset[0][i] <<= shift_y;
356 s->sprite_offset[1][i] <<= shift_c;
357 s->sprite_delta[0][i] <<= shift_y;
358 s->sprite_delta[1][i] <<= shift_y;
359 s->sprite_shift[i] = 16;
ca334dd1 360 }
d234c7a0 361 s->real_sprite_warping_points = s->num_sprite_warping_points;
ca334dd1 362 }
d234c7a0 363
f875a732 364 return 0;
ca334dd1
MN
365}
366
367/**
58c42af7 368 * Decode the next video packet.
ca334dd1
MN
369 * @return <0 if something went wrong
370 */
ee8af2dd 371int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
ca334dd1 372{
ee8af2dd
AK
373 MpegEncContext *s = &ctx->m;
374
d234c7a0
VG
375 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
376 int header_extension = 0, mb_num, len;
ca334dd1
MN
377
378 /* is there enough space left for a video packet + header */
d234c7a0
VG
379 if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
380 return -1;
ca334dd1 381
d234c7a0
VG
382 for (len = 0; len < 32; len++)
383 if (get_bits1(&s->gb))
384 break;
ca334dd1 385
d234c7a0 386 if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
ca334dd1
MN
387 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
388 return -1;
389 }
390
ee8af2dd 391 if (ctx->shape != RECT_SHAPE) {
d234c7a0
VG
392 header_extension = get_bits1(&s->gb);
393 // FIXME more stuff here
ca334dd1
MN
394 }
395
d234c7a0
VG
396 mb_num = get_bits(&s->gb, mb_num_bits);
397 if (mb_num >= s->mb_num) {
398 av_log(s->avctx, AV_LOG_ERROR,
399 "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
ca334dd1
MN
400 return -1;
401 }
d234c7a0 402 if (s->pict_type == AV_PICTURE_TYPE_B) {
6a9c8594
AS
403 int mb_x = 0, mb_y = 0;
404
759001c5 405 while (s->next_picture.mbskip_table[s->mb_index2xy[mb_num]]) {
47c0ac96 406 if (!mb_x)
759001c5 407 ff_thread_await_progress(&s->next_picture_ptr->tf, mb_y++, 0);
6a9c8594 408 mb_num++;
d234c7a0
VG
409 if (++mb_x == s->mb_width)
410 mb_x = 0;
6a9c8594 411 }
d234c7a0
VG
412 if (mb_num >= s->mb_num)
413 return -1; // slice contains just skipped MBs (already decoded)
ca334dd1
MN
414 }
415
d234c7a0
VG
416 s->mb_x = mb_num % s->mb_width;
417 s->mb_y = mb_num / s->mb_width;
ca334dd1 418
ee8af2dd 419 if (ctx->shape != BIN_ONLY_SHAPE) {
d234c7a0
VG
420 int qscale = get_bits(&s->gb, s->quant_precision);
421 if (qscale)
422 s->chroma_qscale = s->qscale = qscale;
ca334dd1
MN
423 }
424
ee8af2dd 425 if (ctx->shape == RECT_SHAPE)
d234c7a0
VG
426 header_extension = get_bits1(&s->gb);
427
428 if (header_extension) {
429 int time_incr = 0;
ca334dd1
MN
430
431 while (get_bits1(&s->gb) != 0)
432 time_incr++;
433
434 check_marker(&s->gb, "before time_increment in video packed header");
e62a43f6 435 skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
ca334dd1
MN
436 check_marker(&s->gb, "before vop_coding_type in video packed header");
437
438 skip_bits(&s->gb, 2); /* vop coding type */
d234c7a0 439 // FIXME not rect stuff here
ca334dd1 440
ee8af2dd 441 if (ctx->shape != BIN_ONLY_SHAPE) {
ca334dd1 442 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
d234c7a0
VG
443 // FIXME don't just ignore everything
444 if (s->pict_type == AV_PICTURE_TYPE_S &&
1a890257 445 ctx->vol_sprite_usage == GMC_SPRITE) {
f875a732
MS
446 if (mpeg4_decode_sprite_trajectory(s, &s->gb) < 0)
447 return AVERROR_INVALIDDATA;
ca334dd1
MN
448 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
449 }
450
d234c7a0 451 // FIXME reduced res stuff here
ca334dd1 452
975a1447 453 if (s->pict_type != AV_PICTURE_TYPE_I) {
ca334dd1 454 int f_code = get_bits(&s->gb, 3); /* fcode_for */
d234c7a0
VG
455 if (f_code == 0)
456 av_log(s->avctx, AV_LOG_ERROR,
457 "Error, video packet header damaged (f_code=0)\n");
ca334dd1 458 }
975a1447 459 if (s->pict_type == AV_PICTURE_TYPE_B) {
ca334dd1 460 int b_code = get_bits(&s->gb, 3);
d234c7a0
VG
461 if (b_code == 0)
462 av_log(s->avctx, AV_LOG_ERROR,
463 "Error, video packet header damaged (b_code=0)\n");
ca334dd1
MN
464 }
465 }
466 }
d234c7a0 467 // FIXME new-pred stuff
ca334dd1
MN
468
469 return 0;
470}
471
472/**
58c42af7 473 * Get the average motion vector for a GMC MB.
ca334dd1 474 * @param n either 0 for the x component or 1 for y
32e543f8 475 * @return the average MV for a GMC MB
ca334dd1 476 */
d234c7a0
VG
477static inline int get_amv(MpegEncContext *s, int n)
478{
ca334dd1 479 int x, y, mb_v, sum, dx, dy, shift;
d234c7a0
VG
480 int len = 1 << (s->f_code + 4);
481 const int a = s->sprite_warping_accuracy;
ca334dd1 482
d234c7a0 483 if (s->workaround_bugs & FF_BUG_AMV)
ca334dd1
MN
484 len >>= s->quarter_sample;
485
d234c7a0
VG
486 if (s->real_sprite_warping_points == 1) {
487 if (s->divx_version == 500 && s->divx_build == 413)
488 sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
489 else
490 sum = RSHIFT(s->sprite_offset[0][n] << s->quarter_sample, a);
491 } else {
492 dx = s->sprite_delta[n][0];
493 dy = s->sprite_delta[n][1];
494 shift = s->sprite_shift[0];
495 if (n)
496 dy -= 1 << (shift + a + 1);
ca334dd1 497 else
d234c7a0
VG
498 dx -= 1 << (shift + a + 1);
499 mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
500
501 sum = 0;
502 for (y = 0; y < 16; y++) {
ca334dd1
MN
503 int v;
504
d234c7a0
VG
505 v = mb_v + dy * y;
506 // FIXME optimize
507 for (x = 0; x < 16; x++) {
508 sum += v >> shift;
509 v += dx;
ca334dd1
MN
510 }
511 }
d234c7a0 512 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
ca334dd1
MN
513 }
514
d234c7a0
VG
515 if (sum < -len)
516 sum = -len;
517 else if (sum >= len)
518 sum = len - 1;
ca334dd1
MN
519
520 return sum;
521}
522
523/**
58c42af7 524 * Decode the dc value.
ca334dd1
MN
525 * @param n block index (0-3 are luma, 4-5 are chroma)
526 * @param dir_ptr the prediction direction will be stored here
527 * @return the quantized dc
528 */
d234c7a0 529static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
ca334dd1
MN
530{
531 int level, code;
532
533 if (n < 4)
534 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
535 else
536 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
d234c7a0
VG
537
538 if (code < 0 || code > 9 /* && s->nbit < 9 */) {
ca334dd1
MN
539 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
540 return -1;
541 }
d234c7a0 542
ca334dd1
MN
543 if (code == 0) {
544 level = 0;
545 } else {
d234c7a0
VG
546 if (IS_3IV1) {
547 if (code == 1)
548 level = 2 * get_bits1(&s->gb) - 1;
549 else {
550 if (get_bits1(&s->gb))
551 level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
ca334dd1 552 else
d234c7a0 553 level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
ca334dd1 554 }
d234c7a0 555 } else {
ca334dd1
MN
556 level = get_xbits(&s->gb, code);
557 }
558
d234c7a0
VG
559 if (code > 8) {
560 if (get_bits1(&s->gb) == 0) { /* marker */
561 if (s->err_recognition & AV_EF_BITSTREAM) {
ca334dd1
MN
562 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
563 return -1;
564 }
565 }
566 }
567 }
568
569 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
570}
571
572/**
58c42af7 573 * Decode first partition.
ca334dd1
MN
574 * @return number of MBs decoded or <0 if an error occurred
575 */
1a890257 576static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
d234c7a0 577{
1a890257 578 MpegEncContext *s = &ctx->m;
d234c7a0 579 int mb_num = 0;
ca334dd1
MN
580 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
581
582 /* decode first partition */
d234c7a0
VG
583 s->first_slice_line = 1;
584 for (; s->mb_y < s->mb_height; s->mb_y++) {
ca334dd1 585 ff_init_block_index(s);
d234c7a0
VG
586 for (; s->mb_x < s->mb_width; s->mb_x++) {
587 const int xy = s->mb_x + s->mb_y * s->mb_stride;
ca334dd1 588 int cbpc;
d234c7a0 589 int dir = 0;
ca334dd1
MN
590
591 mb_num++;
592 ff_update_block_index(s);
d234c7a0
VG
593 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
594 s->first_slice_line = 0;
ca334dd1 595
d234c7a0 596 if (s->pict_type == AV_PICTURE_TYPE_I) {
ca334dd1
MN
597 int i;
598
d234c7a0
VG
599 do {
600 if (show_bits_long(&s->gb, 19) == DC_MARKER)
601 return mb_num - 1;
ca334dd1 602
101ada9a 603 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
d234c7a0
VG
604 if (cbpc < 0) {
605 av_log(s->avctx, AV_LOG_ERROR,
606 "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
607 return -1;
608 }
d234c7a0 609 } while (cbpc == 8);
ca334dd1 610
d234c7a0 611 s->cbp_table[xy] = cbpc & 3;
759001c5 612 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
d234c7a0 613 s->mb_intra = 1;
ca334dd1 614
d234c7a0 615 if (cbpc & 4)
ca334dd1 616 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
ca334dd1 617
d234c7a0
VG
618 s->current_picture.qscale_table[xy] = s->qscale;
619
620 s->mbintra_table[xy] = 1;
621 for (i = 0; i < 6; i++) {
ca334dd1 622 int dc_pred_dir;
d234c7a0
VG
623 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
624 if (dc < 0) {
625 av_log(s->avctx, AV_LOG_ERROR,
626 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
627 return -1;
628 }
d234c7a0
VG
629 dir <<= 1;
630 if (dc_pred_dir)
631 dir |= 1;
ca334dd1 632 }
d234c7a0
VG
633 s->pred_dir_table[xy] = dir;
634 } else { /* P/S_TYPE */
ca334dd1 635 int mx, my, pred_x, pred_y, bits;
d234c7a0
VG
636 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
637 const int stride = s->b8_stride * 2;
ca334dd1
MN
638
639try_again:
d234c7a0
VG
640 bits = show_bits(&s->gb, 17);
641 if (bits == MOTION_MARKER)
642 return mb_num - 1;
643
ca334dd1 644 skip_bits1(&s->gb);
d234c7a0 645 if (bits & 0x10000) {
ca334dd1 646 /* skip mb */
d234c7a0 647 if (s->pict_type == AV_PICTURE_TYPE_S &&
1a890257 648 ctx->vol_sprite_usage == GMC_SPRITE) {
d234c7a0
VG
649 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
650 MB_TYPE_16x16 |
651 MB_TYPE_GMC |
652 MB_TYPE_L0;
653 mx = get_amv(s, 0);
654 my = get_amv(s, 1);
655 } else {
656 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
657 MB_TYPE_16x16 |
658 MB_TYPE_L0;
659 mx = my = 0;
ca334dd1 660 }
d234c7a0
VG
661 mot_val[0] =
662 mot_val[2] =
663 mot_val[0 + stride] =
664 mot_val[2 + stride] = mx;
665 mot_val[1] =
666 mot_val[3] =
667 mot_val[1 + stride] =
668 mot_val[3 + stride] = my;
669
670 if (s->mbintra_table[xy])
ca334dd1
MN
671 ff_clean_intra_table_entries(s);
672 continue;
673 }
674
101ada9a 675 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
d234c7a0
VG
676 if (cbpc < 0) {
677 av_log(s->avctx, AV_LOG_ERROR,
678 "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
679 return -1;
680 }
d234c7a0 681 if (cbpc == 20)
ca334dd1
MN
682 goto try_again;
683
d234c7a0 684 s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
ca334dd1
MN
685
686 s->mb_intra = ((cbpc & 4) != 0);
687
d234c7a0 688 if (s->mb_intra) {
759001c5 689 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
d234c7a0
VG
690 s->mbintra_table[xy] = 1;
691 mot_val[0] =
692 mot_val[2] =
693 mot_val[0 + stride] =
694 mot_val[2 + stride] = 0;
695 mot_val[1] =
696 mot_val[3] =
697 mot_val[1 + stride] =
698 mot_val[3 + stride] = 0;
699 } else {
700 if (s->mbintra_table[xy])
ca334dd1
MN
701 ff_clean_intra_table_entries(s);
702
d234c7a0 703 if (s->pict_type == AV_PICTURE_TYPE_S &&
1a890257 704 ctx->vol_sprite_usage == GMC_SPRITE &&
d234c7a0
VG
705 (cbpc & 16) == 0)
706 s->mcsel = get_bits1(&s->gb);
707 else
708 s->mcsel = 0;
ca334dd1
MN
709
710 if ((cbpc & 16) == 0) {
711 /* 16x16 motion prediction */
712
ddce8953 713 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
d234c7a0 714 if (!s->mcsel) {
ddce8953 715 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
ca334dd1
MN
716 if (mx >= 0xffff)
717 return -1;
718
ddce8953 719 my = ff_h263_decode_motion(s, pred_y, s->f_code);
ca334dd1
MN
720 if (my >= 0xffff)
721 return -1;
d234c7a0
VG
722 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
723 MB_TYPE_L0;
ca334dd1
MN
724 } else {
725 mx = get_amv(s, 0);
726 my = get_amv(s, 1);
d234c7a0
VG
727 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
728 MB_TYPE_GMC |
729 MB_TYPE_L0;
ca334dd1
MN
730 }
731
d234c7a0
VG
732 mot_val[0] =
733 mot_val[2] =
734 mot_val[0 + stride] =
735 mot_val[2 + stride] = mx;
736 mot_val[1] =
737 mot_val[3] =
738 mot_val[1 + stride] =
739 mot_val[3 + stride] = my;
ca334dd1
MN
740 } else {
741 int i;
d234c7a0
VG
742 s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
743 MB_TYPE_L0;
744 for (i = 0; i < 4; i++) {
745 int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
ddce8953 746 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
ca334dd1
MN
747 if (mx >= 0xffff)
748 return -1;
749
ddce8953 750 my = ff_h263_decode_motion(s, pred_y, s->f_code);
ca334dd1
MN
751 if (my >= 0xffff)
752 return -1;
753 mot_val[0] = mx;
754 mot_val[1] = my;
755 }
756 }
757 }
758 }
759 }
d234c7a0 760 s->mb_x = 0;
ca334dd1
MN
761 }
762
763 return mb_num;
764}
765
766/**
767 * decode second partition.
768 * @return <0 if an error occurred
769 */
d234c7a0
VG
770static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
771{
772 int mb_num = 0;
ca334dd1
MN
773 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
774
d234c7a0
VG
775 s->mb_x = s->resync_mb_x;
776 s->first_slice_line = 1;
777 for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
ca334dd1 778 ff_init_block_index(s);
d234c7a0
VG
779 for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
780 const int xy = s->mb_x + s->mb_y * s->mb_stride;
ca334dd1
MN
781
782 mb_num++;
783 ff_update_block_index(s);
d234c7a0
VG
784 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
785 s->first_slice_line = 0;
786
787 if (s->pict_type == AV_PICTURE_TYPE_I) {
788 int ac_pred = get_bits1(&s->gb);
789 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
790 if (cbpy < 0) {
791 av_log(s->avctx, AV_LOG_ERROR,
792 "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
793 return -1;
794 }
795
d234c7a0
VG
796 s->cbp_table[xy] |= cbpy << 2;
797 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
798 } else { /* P || S_TYPE */
759001c5 799 if (IS_INTRA(s->current_picture.mb_type[xy])) {
d234c7a0
VG
800 int i;
801 int dir = 0;
ca334dd1 802 int ac_pred = get_bits1(&s->gb);
d234c7a0 803 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
ca334dd1 804
d234c7a0
VG
805 if (cbpy < 0) {
806 av_log(s->avctx, AV_LOG_ERROR,
807 "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
808 return -1;
809 }
810
d234c7a0 811 if (s->cbp_table[xy] & 8)
ca334dd1 812 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
759001c5 813 s->current_picture.qscale_table[xy] = s->qscale;
ca334dd1 814
d234c7a0 815 for (i = 0; i < 6; i++) {
ca334dd1 816 int dc_pred_dir;
d234c7a0
VG
817 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
818 if (dc < 0) {
819 av_log(s->avctx, AV_LOG_ERROR,
820 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
821 return -1;
822 }
d234c7a0
VG
823 dir <<= 1;
824 if (dc_pred_dir)
825 dir |= 1;
ca334dd1 826 }
d234c7a0
VG
827 s->cbp_table[xy] &= 3; // remove dquant
828 s->cbp_table[xy] |= cbpy << 2;
829 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
830 s->pred_dir_table[xy] = dir;
759001c5
AK
831 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
832 s->current_picture.qscale_table[xy] = s->qscale;
d234c7a0
VG
833 s->cbp_table[xy] = 0;
834 } else {
101ada9a 835 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
ca334dd1 836
d234c7a0
VG
837 if (cbpy < 0) {
838 av_log(s->avctx, AV_LOG_ERROR,
839 "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
840 return -1;
841 }
842
d234c7a0 843 if (s->cbp_table[xy] & 8)
ca334dd1 844 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
759001c5 845 s->current_picture.qscale_table[xy] = s->qscale;
ca334dd1 846
d234c7a0
VG
847 s->cbp_table[xy] &= 3; // remove dquant
848 s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
ca334dd1
MN
849 }
850 }
851 }
d234c7a0
VG
852 if (mb_num >= mb_count)
853 return 0;
854 s->mb_x = 0;
ca334dd1
MN
855 }
856 return 0;
857}
858
859/**
58c42af7 860 * Decode the first and second partition.
ca334dd1
MN
861 * @return <0 if error (and sets error type in the error_status_table)
862 */
1a890257 863int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
ca334dd1 864{
1a890257 865 MpegEncContext *s = &ctx->m;
ca334dd1 866 int mb_num;
d234c7a0
VG
867 const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
868 const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
ca334dd1 869
1a890257 870 mb_num = mpeg4_decode_partition_a(ctx);
d234c7a0
VG
871 if (mb_num < 0) {
872 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
873 s->mb_x, s->mb_y, part_a_error);
ca334dd1
MN
874 return -1;
875 }
876
d234c7a0 877 if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
ca334dd1 878 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
d234c7a0
VG
879 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
880 s->mb_x, s->mb_y, part_a_error);
ca334dd1
MN
881 return -1;
882 }
883
d234c7a0 884 s->mb_num_left = mb_num;
ca334dd1 885
d234c7a0
VG
886 if (s->pict_type == AV_PICTURE_TYPE_I) {
887 while (show_bits(&s->gb, 9) == 1)
ca334dd1 888 skip_bits(&s->gb, 9);
d234c7a0
VG
889 if (get_bits_long(&s->gb, 19) != DC_MARKER) {
890 av_log(s->avctx, AV_LOG_ERROR,
891 "marker missing after first I partition at %d %d\n",
892 s->mb_x, s->mb_y);
ca334dd1
MN
893 return -1;
894 }
d234c7a0
VG
895 } else {
896 while (show_bits(&s->gb, 10) == 1)
ca334dd1 897 skip_bits(&s->gb, 10);
d234c7a0
VG
898 if (get_bits(&s->gb, 17) != MOTION_MARKER) {
899 av_log(s->avctx, AV_LOG_ERROR,
900 "marker missing after first P partition at %d %d\n",
901 s->mb_x, s->mb_y);
ca334dd1
MN
902 return -1;
903 }
904 }
d234c7a0
VG
905 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
906 s->mb_x - 1, s->mb_y, part_a_end);
ca334dd1 907
d234c7a0
VG
908 if (mpeg4_decode_partition_b(s, mb_num) < 0) {
909 if (s->pict_type == AV_PICTURE_TYPE_P)
910 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
911 s->mb_x, s->mb_y, ER_DC_ERROR);
ca334dd1 912 return -1;
d234c7a0
VG
913 } else {
914 if (s->pict_type == AV_PICTURE_TYPE_P)
915 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
916 s->mb_x - 1, s->mb_y, ER_DC_END);
ca334dd1
MN
917 }
918
919 return 0;
920}
921
922/**
58c42af7 923 * Decode a block.
ca334dd1
MN
924 * @return <0 if an error occurred
925 */
d234c7a0
VG
926static inline int mpeg4_decode_block(MpegEncContext *s, int16_t *block,
927 int n, int coded, int intra, int rvlc)
ca334dd1 928{
d234c7a0
VG
929 int level, i, last, run, qmul, qadd, dc_pred_dir;
930 RLTable *rl;
931 RL_VLC_ELEM *rl_vlc;
932 const uint8_t *scan_table;
933
934 // Note intra & rvlc should be optimized away if this is inlined
935
936 if (intra) {
937 if (s->use_intra_dc_vlc) {
938 /* DC coef */
939 if (s->partitioned_frame) {
940 level = s->dc_val[0][s->block_index[n]];
941 if (n < 4)
942 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
943 else
944 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
945 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
946 } else {
947 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
948 if (level < 0)
949 return -1;
950 }
951 block[0] = level;
952 i = 0;
953 } else {
ca334dd1
MN
954 i = -1;
955 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
d234c7a0
VG
956 }
957 if (!coded)
958 goto not_coded;
959
960 if (rvlc) {
961 rl = &ff_rvlc_rl_intra;
962 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
963 } else {
964 rl = &ff_mpeg4_rl_intra;
965 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
966 }
967 if (s->ac_pred) {
968 if (dc_pred_dir == 0)
969 scan_table = s->intra_v_scantable.permutated; /* left */
970 else
971 scan_table = s->intra_h_scantable.permutated; /* top */
972 } else {
ca334dd1 973 scan_table = s->intra_scantable.permutated;
d234c7a0
VG
974 }
975 qmul = 1;
976 qadd = 0;
ca334dd1
MN
977 } else {
978 i = -1;
979 if (!coded) {
980 s->block_last_index[n] = i;
981 return 0;
982 }
d234c7a0
VG
983 if (rvlc)
984 rl = &ff_rvlc_rl_inter;
985 else
986 rl = &ff_h263_rl_inter;
ca334dd1
MN
987
988 scan_table = s->intra_scantable.permutated;
989
d234c7a0
VG
990 if (s->mpeg_quant) {
991 qmul = 1;
992 qadd = 0;
993 if (rvlc)
6f13a371 994 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
d234c7a0 995 else
101ada9a 996 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
d234c7a0 997 } else {
ca334dd1
MN
998 qmul = s->qscale << 1;
999 qadd = (s->qscale - 1) | 1;
d234c7a0 1000 if (rvlc)
6f13a371 1001 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
d234c7a0 1002 else
101ada9a 1003 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
ca334dd1
MN
1004 }
1005 }
d234c7a0
VG
1006 {
1007 OPEN_READER(re, &s->gb);
1008 for (;;) {
1009 UPDATE_CACHE(re, &s->gb);
1010 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1011 if (level == 0) {
1012 /* escape */
1013 if (rvlc) {
1014 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1015 av_log(s->avctx, AV_LOG_ERROR,
1016 "1. marker bit missing in rvlc esc\n");
1017 return -1;
1018 }
1019 SKIP_CACHE(re, &s->gb, 1);
ca334dd1 1020
d234c7a0
VG
1021 last = SHOW_UBITS(re, &s->gb, 1);
1022 SKIP_CACHE(re, &s->gb, 1);
1023 run = SHOW_UBITS(re, &s->gb, 6);
1024 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
ca334dd1
MN
1025 UPDATE_CACHE(re, &s->gb);
1026
d234c7a0
VG
1027 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1028 av_log(s->avctx, AV_LOG_ERROR,
1029 "2. marker bit missing in rvlc esc\n");
1030 return -1;
1031 }
1032 SKIP_CACHE(re, &s->gb, 1);
ca334dd1 1033
d234c7a0
VG
1034 level = SHOW_UBITS(re, &s->gb, 11);
1035 SKIP_CACHE(re, &s->gb, 11);
ca334dd1 1036
d234c7a0
VG
1037 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1038 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1039 return -1;
ca334dd1 1040 }
d234c7a0 1041 SKIP_CACHE(re, &s->gb, 5);
ca334dd1 1042
d234c7a0
VG
1043 level = level * qmul + qadd;
1044 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1045 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
ca334dd1 1046
d234c7a0
VG
1047 i += run + 1;
1048 if (last)
1049 i += 192;
1050 } else {
1051 int cache;
1052 cache = GET_CACHE(re, &s->gb);
1053
1054 if (IS_3IV1)
1055 cache ^= 0xC0000000;
1056
1057 if (cache & 0x80000000) {
1058 if (cache & 0x40000000) {
1059 /* third escape */
1060 SKIP_CACHE(re, &s->gb, 2);
1061 last = SHOW_UBITS(re, &s->gb, 1);
1062 SKIP_CACHE(re, &s->gb, 1);
1063 run = SHOW_UBITS(re, &s->gb, 6);
1064 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1065 UPDATE_CACHE(re, &s->gb);
1066
1067 if (IS_3IV1) {
1068 level = SHOW_SBITS(re, &s->gb, 12);
1069 LAST_SKIP_BITS(re, &s->gb, 12);
1070 } else {
1071 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1072 av_log(s->avctx, AV_LOG_ERROR,
1073 "1. marker bit missing in 3. esc\n");
1074 return -1;
1075 }
1076 SKIP_CACHE(re, &s->gb, 1);
1077
1078 level = SHOW_SBITS(re, &s->gb, 12);
1079 SKIP_CACHE(re, &s->gb, 12);
1080
1081 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1082 av_log(s->avctx, AV_LOG_ERROR,
1083 "2. marker bit missing in 3. esc\n");
1084 return -1;
1085 }
1086
1087 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
ca334dd1 1088 }
d234c7a0
VG
1089
1090 if (level > 0)
1091 level = level * qmul + qadd;
1092 else
1093 level = level * qmul - qadd;
1094
1095 if ((unsigned)(level + 2048) > 4095) {
1096 if (s->err_recognition & AV_EF_BITSTREAM) {
1097 if (level > 2560 || level < -2560) {
1098 av_log(s->avctx, AV_LOG_ERROR,
1099 "|level| overflow in 3. esc, qp=%d\n",
1100 s->qscale);
1101 return -1;
1102 }
1103 }
1104 level = level < 0 ? -2048 : 2047;
1105 }
1106
1107 i += run + 1;
1108 if (last)
1109 i += 192;
1110 } else {
1111 /* second escape */
1112 SKIP_BITS(re, &s->gb, 2);
1113 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1114 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1115 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1116 LAST_SKIP_BITS(re, &s->gb, 1);
ca334dd1 1117 }
d234c7a0
VG
1118 } else {
1119 /* first escape */
1120 SKIP_BITS(re, &s->gb, 1);
1121 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1122 i += run;
1123 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1124 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1125 LAST_SKIP_BITS(re, &s->gb, 1);
ca334dd1 1126 }
ca334dd1
MN
1127 }
1128 } else {
d234c7a0 1129 i += run;
ca334dd1
MN
1130 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1131 LAST_SKIP_BITS(re, &s->gb, 1);
1132 }
d234c7a0
VG
1133 if (i > 62) {
1134 i -= 192;
1135 if (i & (~63)) {
1136 av_log(s->avctx, AV_LOG_ERROR,
1137 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1138 return -1;
1139 }
1140
1141 block[scan_table[i]] = level;
1142 break;
ca334dd1
MN
1143 }
1144
1145 block[scan_table[i]] = level;
ca334dd1 1146 }
d234c7a0 1147 CLOSE_READER(re, &s->gb);
ca334dd1 1148 }
d234c7a0
VG
1149
1150not_coded:
ca334dd1 1151 if (intra) {
d234c7a0 1152 if (!s->use_intra_dc_vlc) {
ca334dd1
MN
1153 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1154
d234c7a0 1155 i -= i >> 31; // if (i == -1) i = 0;
ca334dd1
MN
1156 }
1157
6f13a371 1158 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
d234c7a0
VG
1159 if (s->ac_pred)
1160 i = 63; // FIXME not optimal
ca334dd1
MN
1161 }
1162 s->block_last_index[n] = i;
1163 return 0;
1164}
1165
1166/**
1167 * decode partition C of one MB.
1168 * @return <0 if an error occurred
1169 */
88bd7fdc 1170static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
ca334dd1 1171{
1a890257 1172 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
ca334dd1 1173 int cbp, mb_type;
d234c7a0 1174 const int xy = s->mb_x + s->mb_y * s->mb_stride;
ca334dd1 1175
759001c5 1176 mb_type = s->current_picture.mb_type[xy];
d234c7a0 1177 cbp = s->cbp_table[xy];
ca334dd1 1178
d234c7a0 1179 s->use_intra_dc_vlc = s->qscale < s->intra_dc_threshold;
ca334dd1 1180
d234c7a0 1181 if (s->current_picture.qscale_table[xy] != s->qscale)
759001c5 1182 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
ca334dd1 1183
d234c7a0
VG
1184 if (s->pict_type == AV_PICTURE_TYPE_P ||
1185 s->pict_type == AV_PICTURE_TYPE_S) {
ca334dd1 1186 int i;
d234c7a0 1187 for (i = 0; i < 4; i++) {
759001c5
AK
1188 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1189 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
ca334dd1
MN
1190 }
1191 s->mb_intra = IS_INTRA(mb_type);
1192
1193 if (IS_SKIP(mb_type)) {
1194 /* skip mb */
d234c7a0 1195 for (i = 0; i < 6; i++)
ca334dd1 1196 s->block_last_index[i] = -1;
d234c7a0 1197 s->mv_dir = MV_DIR_FORWARD;
ca334dd1 1198 s->mv_type = MV_TYPE_16X16;
d234c7a0 1199 if (s->pict_type == AV_PICTURE_TYPE_S
1a890257 1200 && ctx->vol_sprite_usage == GMC_SPRITE) {
d234c7a0 1201 s->mcsel = 1;
ca334dd1 1202 s->mb_skipped = 0;
d234c7a0
VG
1203 } else {
1204 s->mcsel = 0;
ca334dd1
MN
1205 s->mb_skipped = 1;
1206 }
d234c7a0 1207 } else if (s->mb_intra) {
759001c5 1208 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
d234c7a0
VG
1209 } else if (!s->mb_intra) {
1210 // s->mcsel = 0; // FIXME do we need to init that?
ca334dd1
MN
1211
1212 s->mv_dir = MV_DIR_FORWARD;
1213 if (IS_8X8(mb_type)) {
1214 s->mv_type = MV_TYPE_8X8;
1215 } else {
1216 s->mv_type = MV_TYPE_16X16;
1217 }
1218 }
1219 } else { /* I-Frame */
1220 s->mb_intra = 1;
d234c7a0 1221 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
ca334dd1
MN
1222 }
1223
1224 if (!IS_SKIP(mb_type)) {
1225 int i;
1226 s->dsp.clear_blocks(s->block[0]);
1227 /* decode each block */
1228 for (i = 0; i < 6; i++) {
bc5abfb1 1229 if (mpeg4_decode_block(s, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
d234c7a0
VG
1230 av_log(s->avctx, AV_LOG_ERROR,
1231 "texture corrupted at %d %d %d\n",
1232 s->mb_x, s->mb_y, s->mb_intra);
ca334dd1
MN
1233 return -1;
1234 }
d234c7a0 1235 cbp += cbp;
ca334dd1
MN
1236 }
1237 }
1238
1239 /* per-MB end of slice check */
d234c7a0
VG
1240 if (--s->mb_num_left <= 0) {
1241 if (mpeg4_is_resync(s))
ca334dd1
MN
1242 return SLICE_END;
1243 else
1244 return SLICE_NOEND;
d234c7a0
VG
1245 } else {
1246 if (mpeg4_is_resync(s)) {
1247 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1248 if (s->cbp_table[xy + delta])
ca334dd1
MN
1249 return SLICE_END;
1250 }
1251 return SLICE_OK;
1252 }
1253}
1254
d234c7a0 1255static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
ca334dd1 1256{
1a890257 1257 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
ca334dd1
MN
1258 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1259 int16_t *mot_val;
1260 static int8_t quant_tab[4] = { -1, -2, 1, 2 };
d234c7a0 1261 const int xy = s->mb_x + s->mb_y * s->mb_stride;
ca334dd1
MN
1262
1263 assert(s->h263_pred);
1264
d234c7a0
VG
1265 if (s->pict_type == AV_PICTURE_TYPE_P ||
1266 s->pict_type == AV_PICTURE_TYPE_S) {
1267 do {
ca334dd1
MN
1268 if (get_bits1(&s->gb)) {
1269 /* skip mb */
1270 s->mb_intra = 0;
d234c7a0 1271 for (i = 0; i < 6; i++)
ca334dd1 1272 s->block_last_index[i] = -1;
d234c7a0 1273 s->mv_dir = MV_DIR_FORWARD;
ca334dd1 1274 s->mv_type = MV_TYPE_16X16;
d234c7a0 1275 if (s->pict_type == AV_PICTURE_TYPE_S &&
1a890257 1276 ctx->vol_sprite_usage == GMC_SPRITE) {
d234c7a0
VG
1277 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1278 MB_TYPE_GMC |
1279 MB_TYPE_16x16 |
1280 MB_TYPE_L0;
1281 s->mcsel = 1;
1282 s->mv[0][0][0] = get_amv(s, 0);
1283 s->mv[0][0][1] = get_amv(s, 1);
1284 s->mb_skipped = 0;
1285 } else {
1286 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1287 MB_TYPE_16x16 |
1288 MB_TYPE_L0;
1289 s->mcsel = 0;
ca334dd1
MN
1290 s->mv[0][0][0] = 0;
1291 s->mv[0][0][1] = 0;
d234c7a0 1292 s->mb_skipped = 1;
ca334dd1
MN
1293 }
1294 goto end;
1295 }
101ada9a 1296 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
d234c7a0
VG
1297 if (cbpc < 0) {
1298 av_log(s->avctx, AV_LOG_ERROR,
1299 "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
1300 return -1;
1301 }
d234c7a0 1302 } while (cbpc == 20);
ca334dd1
MN
1303
1304 s->dsp.clear_blocks(s->block[0]);
d234c7a0 1305 dquant = cbpc & 8;
ca334dd1 1306 s->mb_intra = ((cbpc & 4) != 0);
d234c7a0
VG
1307 if (s->mb_intra)
1308 goto intra;
ca334dd1 1309
d234c7a0 1310 if (s->pict_type == AV_PICTURE_TYPE_S &&
1a890257 1311 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
d234c7a0
VG
1312 s->mcsel = get_bits1(&s->gb);
1313 else
1314 s->mcsel = 0;
101ada9a 1315 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
ca334dd1
MN
1316
1317 cbp = (cbpc & 3) | (cbpy << 2);
d234c7a0 1318 if (dquant)
ca334dd1 1319 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
d234c7a0
VG
1320 if ((!s->progressive_sequence) &&
1321 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1322 s->interlaced_dct = get_bits1(&s->gb);
ca334dd1
MN
1323
1324 s->mv_dir = MV_DIR_FORWARD;
1325 if ((cbpc & 16) == 0) {
d234c7a0
VG
1326 if (s->mcsel) {
1327 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1328 MB_TYPE_16x16 |
1329 MB_TYPE_L0;
ca334dd1 1330 /* 16x16 global motion prediction */
d234c7a0
VG
1331 s->mv_type = MV_TYPE_16X16;
1332 mx = get_amv(s, 0);
1333 my = get_amv(s, 1);
ca334dd1
MN
1334 s->mv[0][0][0] = mx;
1335 s->mv[0][0][1] = my;
d234c7a0
VG
1336 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1337 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1338 MB_TYPE_L0 |
1339 MB_TYPE_INTERLACED;
ca334dd1 1340 /* 16x8 field motion prediction */
d234c7a0 1341 s->mv_type = MV_TYPE_FIELD;
ca334dd1 1342
d234c7a0
VG
1343 s->field_select[0][0] = get_bits1(&s->gb);
1344 s->field_select[0][1] = get_bits1(&s->gb);
ca334dd1 1345
ddce8953 1346 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
ca334dd1 1347
d234c7a0 1348 for (i = 0; i < 2; i++) {
ddce8953 1349 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
ca334dd1
MN
1350 if (mx >= 0xffff)
1351 return -1;
1352
d234c7a0 1353 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
ca334dd1
MN
1354 if (my >= 0xffff)
1355 return -1;
1356
1357 s->mv[0][i][0] = mx;
1358 s->mv[0][i][1] = my;
1359 }
d234c7a0 1360 } else {
759001c5 1361 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
ca334dd1
MN
1362 /* 16x16 motion prediction */
1363 s->mv_type = MV_TYPE_16X16;
ddce8953
MS
1364 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1365 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
ca334dd1
MN
1366
1367 if (mx >= 0xffff)
1368 return -1;
1369
ddce8953 1370 my = ff_h263_decode_motion(s, pred_y, s->f_code);
ca334dd1
MN
1371
1372 if (my >= 0xffff)
1373 return -1;
1374 s->mv[0][0][0] = mx;
1375 s->mv[0][0][1] = my;
1376 }
1377 } else {
759001c5 1378 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
d234c7a0
VG
1379 s->mv_type = MV_TYPE_8X8;
1380 for (i = 0; i < 4; i++) {
ddce8953 1381 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
d234c7a0 1382 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
ca334dd1
MN
1383 if (mx >= 0xffff)
1384 return -1;
1385
ddce8953 1386 my = ff_h263_decode_motion(s, pred_y, s->f_code);
ca334dd1
MN
1387 if (my >= 0xffff)
1388 return -1;
1389 s->mv[0][i][0] = mx;
1390 s->mv[0][i][1] = my;
d234c7a0
VG
1391 mot_val[0] = mx;
1392 mot_val[1] = my;
ca334dd1
MN
1393 }
1394 }
d234c7a0
VG
1395 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1396 int modb1; // first bit of modb
1397 int modb2; // second bit of modb
ca334dd1
MN
1398 int mb_type;
1399
d234c7a0
VG
1400 s->mb_intra = 0; // B-frames never contain intra blocks
1401 s->mcsel = 0; // ... true gmc blocks
ca334dd1 1402
d234c7a0
VG
1403 if (s->mb_x == 0) {
1404 for (i = 0; i < 2; i++) {
1405 s->last_mv[i][0][0] =
1406 s->last_mv[i][0][1] =
1407 s->last_mv[i][1][0] =
1408 s->last_mv[i][1][1] = 0;
ca334dd1 1409 }
6a9c8594 1410
759001c5 1411 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
ca334dd1
MN
1412 }
1413
1414 /* if we skipped it in the future P Frame than skip it now too */
d234c7a0 1415 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
ca334dd1 1416
d234c7a0
VG
1417 if (s->mb_skipped) {
1418 /* skip mb */
1419 for (i = 0; i < 6; i++)
ca334dd1
MN
1420 s->block_last_index[i] = -1;
1421
d234c7a0
VG
1422 s->mv_dir = MV_DIR_FORWARD;
1423 s->mv_type = MV_TYPE_16X16;
1424 s->mv[0][0][0] =
1425 s->mv[0][0][1] =
1426 s->mv[1][0][0] =
ca334dd1 1427 s->mv[1][0][1] = 0;
d234c7a0
VG
1428 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1429 MB_TYPE_16x16 |
1430 MB_TYPE_L0;
ca334dd1
MN
1431 goto end;
1432 }
1433
d234c7a0
VG
1434 modb1 = get_bits1(&s->gb);
1435 if (modb1) {
1436 // like MB_TYPE_B_DIRECT but no vectors coded
1437 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1438 cbp = 0;
1439 } else {
1440 modb2 = get_bits1(&s->gb);
1441 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1442 if (mb_type < 0) {
ca334dd1
MN
1443 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1444 return -1;
1445 }
d234c7a0
VG
1446 mb_type = mb_type_b_map[mb_type];
1447 if (modb2) {
1448 cbp = 0;
1449 } else {
ca334dd1 1450 s->dsp.clear_blocks(s->block[0]);
d234c7a0 1451 cbp = get_bits(&s->gb, 6);
ca334dd1
MN
1452 }
1453
1454 if ((!IS_DIRECT(mb_type)) && cbp) {
d234c7a0
VG
1455 if (get_bits1(&s->gb))
1456 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
ca334dd1
MN
1457 }
1458
d234c7a0
VG
1459 if (!s->progressive_sequence) {
1460 if (cbp)
1461 s->interlaced_dct = get_bits1(&s->gb);
ca334dd1 1462
d234c7a0 1463 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
ca334dd1
MN
1464 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1465 mb_type &= ~MB_TYPE_16x16;
1466
d234c7a0
VG
1467 if (USES_LIST(mb_type, 0)) {
1468 s->field_select[0][0] = get_bits1(&s->gb);
1469 s->field_select[0][1] = get_bits1(&s->gb);
ca334dd1 1470 }
d234c7a0
VG
1471 if (USES_LIST(mb_type, 1)) {
1472 s->field_select[1][0] = get_bits1(&s->gb);
1473 s->field_select[1][1] = get_bits1(&s->gb);
ca334dd1
MN
1474 }
1475 }
1476 }
1477
1478 s->mv_dir = 0;
d234c7a0
VG
1479 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1480 s->mv_type = MV_TYPE_16X16;
ca334dd1 1481
d234c7a0 1482 if (USES_LIST(mb_type, 0)) {
ca334dd1
MN
1483 s->mv_dir = MV_DIR_FORWARD;
1484
ddce8953
MS
1485 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1486 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
d234c7a0
VG
1487 s->last_mv[0][1][0] =
1488 s->last_mv[0][0][0] =
1489 s->mv[0][0][0] = mx;
1490 s->last_mv[0][1][1] =
1491 s->last_mv[0][0][1] =
1492 s->mv[0][0][1] = my;
ca334dd1
MN
1493 }
1494
d234c7a0 1495 if (USES_LIST(mb_type, 1)) {
ca334dd1
MN
1496 s->mv_dir |= MV_DIR_BACKWARD;
1497
ddce8953
MS
1498 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1499 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
d234c7a0
VG
1500 s->last_mv[1][1][0] =
1501 s->last_mv[1][0][0] =
1502 s->mv[1][0][0] = mx;
1503 s->last_mv[1][1][1] =
1504 s->last_mv[1][0][1] =
1505 s->mv[1][0][1] = my;
ca334dd1 1506 }
d234c7a0
VG
1507 } else if (!IS_DIRECT(mb_type)) {
1508 s->mv_type = MV_TYPE_FIELD;
ca334dd1 1509
d234c7a0 1510 if (USES_LIST(mb_type, 0)) {
ca334dd1
MN
1511 s->mv_dir = MV_DIR_FORWARD;
1512
d234c7a0
VG
1513 for (i = 0; i < 2; i++) {
1514 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1515 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1516 s->last_mv[0][i][0] =
1517 s->mv[0][i][0] = mx;
1518 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
ca334dd1
MN
1519 }
1520 }
1521
d234c7a0 1522 if (USES_LIST(mb_type, 1)) {
ca334dd1
MN
1523 s->mv_dir |= MV_DIR_BACKWARD;
1524
d234c7a0
VG
1525 for (i = 0; i < 2; i++) {
1526 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1527 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1528 s->last_mv[1][i][0] =
1529 s->mv[1][i][0] = mx;
1530 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
ca334dd1
MN
1531 }
1532 }
1533 }
1534 }
1535
d234c7a0
VG
1536 if (IS_DIRECT(mb_type)) {
1537 if (IS_SKIP(mb_type)) {
1538 mx =
1539 my = 0;
1540 } else {
ddce8953
MS
1541 mx = ff_h263_decode_motion(s, 0, 1);
1542 my = ff_h263_decode_motion(s, 0, 1);
ca334dd1
MN
1543 }
1544
1545 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
d234c7a0 1546 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
ca334dd1 1547 }
759001c5 1548 s->current_picture.mb_type[xy] = mb_type;
ca334dd1 1549 } else { /* I-Frame */
d234c7a0 1550 do {
101ada9a 1551 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
d234c7a0
VG
1552 if (cbpc < 0) {
1553 av_log(s->avctx, AV_LOG_ERROR,
1554 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
1555 return -1;
1556 }
d234c7a0 1557 } while (cbpc == 8);
ca334dd1
MN
1558
1559 dquant = cbpc & 4;
1560 s->mb_intra = 1;
d234c7a0 1561
ca334dd1
MN
1562intra:
1563 s->ac_pred = get_bits1(&s->gb);
d234c7a0 1564 if (s->ac_pred)
759001c5 1565 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
ca334dd1 1566 else
759001c5 1567 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
ca334dd1 1568
101ada9a 1569 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
d234c7a0
VG
1570 if (cbpy < 0) {
1571 av_log(s->avctx, AV_LOG_ERROR,
1572 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
ca334dd1
MN
1573 return -1;
1574 }
1575 cbp = (cbpc & 3) | (cbpy << 2);
1576
d234c7a0 1577 s->use_intra_dc_vlc = s->qscale < s->intra_dc_threshold;
ca334dd1 1578
d234c7a0 1579 if (dquant)
ca334dd1 1580 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
ca334dd1 1581
d234c7a0
VG
1582 if (!s->progressive_sequence)
1583 s->interlaced_dct = get_bits1(&s->gb);
ca334dd1
MN
1584
1585 s->dsp.clear_blocks(s->block[0]);
1586 /* decode each block */
1587 for (i = 0; i < 6; i++) {
d234c7a0 1588 if (mpeg4_decode_block(s, block[i], i, cbp & 32, 1, 0) < 0)
ca334dd1 1589 return -1;
d234c7a0 1590 cbp += cbp;
ca334dd1
MN
1591 }
1592 goto end;
1593 }
1594
1595 /* decode each block */
1596 for (i = 0; i < 6; i++) {
d234c7a0 1597 if (mpeg4_decode_block(s, block[i], i, cbp & 32, 0, 0) < 0)
ca334dd1 1598 return -1;
d234c7a0 1599 cbp += cbp;
ca334dd1 1600 }
ca334dd1 1601
d234c7a0
VG
1602end:
1603 /* per-MB end of slice check */
1604 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1605 if (mpeg4_is_resync(s)) {
1606 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
6a9c8594 1607
d234c7a0
VG
1608 if (s->pict_type == AV_PICTURE_TYPE_B &&
1609 s->next_picture.mbskip_table[xy + delta]) {
759001c5 1610 ff_thread_await_progress(&s->next_picture_ptr->tf,
d234c7a0
VG
1611 (s->mb_x + delta >= s->mb_width)
1612 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1613 : s->mb_y, 0);
6a9c8594
AS
1614 }
1615
d234c7a0
VG
1616 if (s->pict_type == AV_PICTURE_TYPE_B &&
1617 s->next_picture.mbskip_table[xy + delta])
ca334dd1
MN
1618 return SLICE_OK;
1619 return SLICE_END;
1620 }
1621 }
1622
1623 return SLICE_OK;
1624}
1625
d234c7a0
VG
1626static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
1627{
ca334dd1 1628 int hours, minutes, seconds;
7a4abe59
AN
1629 unsigned time_code = show_bits(gb, 18);
1630
1631 if (time_code & 0x40) { /* marker_bit */
1632 hours = time_code >> 13;
d234c7a0
VG
1633 minutes = time_code >> 7 & 0x3f;
1634 seconds = time_code & 0x3f;
1635 s->time_base = seconds + 60 * (minutes + 60 * hours);
7a4abe59
AN
1636 skip_bits(gb, 20); /* time_code, closed_gov, broken_link */
1637 } else {
1638 av_log(s->avctx, AV_LOG_WARNING, "GOP header missing marker_bit\n");
1639 }
ca334dd1
MN
1640
1641 return 0;
1642}
1643
d234c7a0
VG
1644static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
1645{
1646 int profile_and_level_indication;
0637e505 1647
d234c7a0 1648 profile_and_level_indication = get_bits(gb, 8);
0637e505 1649
d234c7a0
VG
1650 s->avctx->profile = (profile_and_level_indication & 0xf0) >> 4;
1651 s->avctx->level = (profile_and_level_indication & 0x0f);
0637e505 1652
d234c7a0
VG
1653 // for Simple profile, level 0
1654 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1655 s->avctx->level = 0;
1656 }
0637e505 1657
d234c7a0 1658 return 0;
0637e505
TF
1659}
1660
ee8af2dd 1661static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
d234c7a0 1662{
ee8af2dd 1663 MpegEncContext *s = &ctx->m;
ca334dd1
MN
1664 int width, height, vo_ver_id;
1665
1666 /* vol header */
d234c7a0
VG
1667 skip_bits(gb, 1); /* random access */
1668 s->vo_type = get_bits(gb, 8);
1669 if (get_bits1(gb) != 0) { /* is_ol_id */
1670 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1671 skip_bits(gb, 3); /* vo_priority */
ca334dd1
MN
1672 } else {
1673 vo_ver_id = 1;
1674 }
d234c7a0
VG
1675 s->aspect_ratio_info = get_bits(gb, 4);
1676 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1677 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
1678 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
1679 } else {
1680 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
ca334dd1
MN
1681 }
1682
d234c7a0
VG
1683 if ((s->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
1684 int chroma_format = get_bits(gb, 2);
1685 if (chroma_format != CHROMA_420)
ca334dd1 1686 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
d234c7a0
VG
1687
1688 s->low_delay = get_bits1(gb);
1689 if (get_bits1(gb)) { /* vbv parameters */
ca334dd1
MN
1690 get_bits(gb, 15); /* first_half_bitrate */
1691 skip_bits1(gb); /* marker */
1692 get_bits(gb, 15); /* latter_half_bitrate */
1693 skip_bits1(gb); /* marker */
1694 get_bits(gb, 15); /* first_half_vbv_buffer_size */
1695 skip_bits1(gb); /* marker */
1696 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1697 get_bits(gb, 11); /* first_half_vbv_occupancy */
1698 skip_bits1(gb); /* marker */
1699 get_bits(gb, 15); /* latter_half_vbv_occupancy */
1700 skip_bits1(gb); /* marker */
1701 }
d234c7a0
VG
1702 } else {
1703 /* is setting low delay flag only once the smartest thing to do?
1704 * low delay detection won't be overriden. */
1705 if (s->picture_number == 0)
1706 s->low_delay = 0;
ca334dd1
MN
1707 }
1708
ee8af2dd
AK
1709 ctx->shape = get_bits(gb, 2); /* vol shape */
1710 if (ctx->shape != RECT_SHAPE)
d234c7a0 1711 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
ee8af2dd 1712 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
ca334dd1 1713 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
d234c7a0 1714 skip_bits(gb, 4); /* video_object_layer_shape_extension */
ca334dd1
MN
1715 }
1716
1717 check_marker(gb, "before time_increment_resolution");
1718
1719 s->avctx->time_base.den = get_bits(gb, 16);
d234c7a0 1720 if (!s->avctx->time_base.den) {
ca334dd1 1721 av_log(s->avctx, AV_LOG_ERROR, "time_base.den==0\n");
4f18f1b0 1722 s->avctx->time_base.num = 0;
ca334dd1
MN
1723 return -1;
1724 }
1725
e62a43f6
AK
1726 ctx->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
1727 if (ctx->time_increment_bits < 1)
1728 ctx->time_increment_bits = 1;
ca334dd1
MN
1729
1730 check_marker(gb, "before fixed_vop_rate");
1731
d234c7a0 1732 if (get_bits1(gb) != 0) /* fixed_vop_rate */
e62a43f6 1733 s->avctx->time_base.num = get_bits(gb, ctx->time_increment_bits);
d234c7a0 1734 else
ca334dd1
MN
1735 s->avctx->time_base.num = 1;
1736
d234c7a0 1737 s->t_frame = 0;
ca334dd1 1738
ee8af2dd
AK
1739 if (ctx->shape != BIN_ONLY_SHAPE) {
1740 if (ctx->shape == RECT_SHAPE) {
ca334dd1
MN
1741 skip_bits1(gb); /* marker */
1742 width = get_bits(gb, 13);
1743 skip_bits1(gb); /* marker */
1744 height = get_bits(gb, 13);
1745 skip_bits1(gb); /* marker */
d234c7a0
VG
1746 if (width && height && /* they should be non zero but who knows */
1747 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
8701f4f8
JG
1748 if (s->width && s->height &&
1749 (s->width != width || s->height != height))
1750 s->context_reinit = 1;
d234c7a0 1751 s->width = width;
ca334dd1
MN
1752 s->height = height;
1753 }
1754 }
1755
d234c7a0
VG
1756 s->progressive_sequence =
1757 s->progressive_frame = get_bits1(gb) ^ 1;
1758 s->interlaced_dct = 0;
1759 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1760 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
1761 "MPEG4 OBMC not supported (very likely buggy encoder)\n");
1762 if (vo_ver_id == 1)
1a890257 1763 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
d234c7a0 1764 else
1a890257 1765 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
d234c7a0 1766
1a890257 1767 if (ctx->vol_sprite_usage == STATIC_SPRITE)
d234c7a0 1768 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1a890257
AK
1769 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
1770 ctx->vol_sprite_usage == GMC_SPRITE) {
1771 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
9f0617d2 1772 skip_bits(gb, 13); // sprite_width
ca334dd1 1773 skip_bits1(gb); /* marker */
9f0617d2 1774 skip_bits(gb, 13); // sprite_height
ca334dd1 1775 skip_bits1(gb); /* marker */
9f0617d2 1776 skip_bits(gb, 13); // sprite_left
ca334dd1 1777 skip_bits1(gb); /* marker */
9f0617d2 1778 skip_bits(gb, 13); // sprite_top
ca334dd1
MN
1779 skip_bits1(gb); /* marker */
1780 }
d234c7a0
VG
1781 s->num_sprite_warping_points = get_bits(gb, 6);
1782 if (s->num_sprite_warping_points > 3) {
1783 av_log(s->avctx, AV_LOG_ERROR,
1784 "%d sprite_warping_points\n",
1785 s->num_sprite_warping_points);
1786 s->num_sprite_warping_points = 0;
ca334dd1
MN
1787 return -1;
1788 }
d234c7a0
VG
1789 s->sprite_warping_accuracy = get_bits(gb, 2);
1790 s->sprite_brightness_change = get_bits1(gb);
1a890257 1791 if (ctx->vol_sprite_usage == STATIC_SPRITE)
d234c7a0 1792 s->low_latency_sprite = get_bits1(gb);
ca334dd1
MN
1793 }
1794 // FIXME sadct disable bit if verid!=1 && shape not rect
1795
d234c7a0
VG
1796 if (get_bits1(gb) == 1) { /* not_8_bit */
1797 s->quant_precision = get_bits(gb, 4); /* quant_precision */
1798 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
1799 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
1800 if (s->quant_precision != 5)
1801 av_log(s->avctx, AV_LOG_ERROR,
1802 "quant precision %d\n", s->quant_precision);
ca334dd1
MN
1803 } else {
1804 s->quant_precision = 5;
1805 }
1806
1807 // FIXME a bunch of grayscale shape things
1808
d234c7a0 1809 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
ca334dd1
MN
1810 int i, v;
1811
1812 /* load default matrixes */
d234c7a0
VG
1813 for (i = 0; i < 64; i++) {
1814 int j = s->dsp.idct_permutation[i];
1815 v = ff_mpeg4_default_intra_matrix[i];
1816 s->intra_matrix[j] = v;
1817 s->chroma_intra_matrix[j] = v;
1818
1819 v = ff_mpeg4_default_non_intra_matrix[i];
1820 s->inter_matrix[j] = v;
1821 s->chroma_inter_matrix[j] = v;
ca334dd1
MN
1822 }
1823
1824 /* load custom intra matrix */
d234c7a0
VG
1825 if (get_bits1(gb)) {
1826 int last = 0;
1827 for (i = 0; i < 64; i++) {
ca334dd1 1828 int j;
d234c7a0
VG
1829 v = get_bits(gb, 8);
1830 if (v == 0)
1831 break;
1832
1833 last = v;
1834 j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1835 s->intra_matrix[j] = last;
1836 s->chroma_intra_matrix[j] = last;
ca334dd1
MN
1837 }
1838
1839 /* replicate last value */
d234c7a0
VG
1840 for (; i < 64; i++) {
1841 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1842 s->intra_matrix[j] = last;
1843 s->chroma_intra_matrix[j] = last;
ca334dd1
MN
1844 }
1845 }
1846
1847 /* load custom non intra matrix */
d234c7a0
VG
1848 if (get_bits1(gb)) {
1849 int last = 0;
1850 for (i = 0; i < 64; i++) {
ca334dd1 1851 int j;
d234c7a0
VG
1852 v = get_bits(gb, 8);
1853 if (v == 0)
1854 break;
1855
1856 last = v;
1857 j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1858 s->inter_matrix[j] = v;
1859 s->chroma_inter_matrix[j] = v;
ca334dd1
MN
1860 }
1861
1862 /* replicate last value */
d234c7a0
VG
1863 for (; i < 64; i++) {
1864 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1865 s->inter_matrix[j] = last;
1866 s->chroma_inter_matrix[j] = last;
ca334dd1
MN
1867 }
1868 }
1869
1870 // FIXME a bunch of grayscale shape things
1871 }
1872
d234c7a0
VG
1873 if (vo_ver_id != 1)
1874 s->quarter_sample = get_bits1(gb);
1875 else
1876 s->quarter_sample = 0;
1877
1878 if (!get_bits1(gb)) {
1879 int pos = get_bits_count(gb);
1880 int estimation_method = get_bits(gb, 2);
1881 if (estimation_method < 2) {
1882 if (!get_bits1(gb)) {
1883 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
1884 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
1885 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
1886 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
1887 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
1888 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upampling */
ca334dd1 1889 }
d234c7a0
VG
1890 if (!get_bits1(gb)) {
1891 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
1892 s->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
1893 s->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
1894 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
ca334dd1 1895 }
d234c7a0 1896 if (!check_marker(gb, "in complexity estimation part 1")) {
ca334dd1
MN
1897 skip_bits_long(gb, pos - get_bits_count(gb));
1898 goto no_cplx_est;
1899 }
d234c7a0
VG
1900 if (!get_bits1(gb)) {
1901 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
1902 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
1903 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
1904 s->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
ca334dd1 1905 }
d234c7a0
VG
1906 if (!get_bits1(gb)) {
1907 s->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
1908 s->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
1909 s->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
1910 s->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
1911 s->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
1912 s->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
ca334dd1 1913 }
d234c7a0 1914 if (!check_marker(gb, "in complexity estimation part 2")) {
ca334dd1
MN
1915 skip_bits_long(gb, pos - get_bits_count(gb));
1916 goto no_cplx_est;
1917 }
d234c7a0
VG
1918 if (estimation_method == 1) {
1919 s->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
1920 s->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
ca334dd1 1921 }
d234c7a0
VG
1922 } else
1923 av_log(s->avctx, AV_LOG_ERROR,
1924 "Invalid Complexity estimation method %d\n",
1925 estimation_method);
1926 } else {
1927
ca334dd1 1928no_cplx_est:
d234c7a0
VG
1929 s->cplx_estimation_trash_i =
1930 s->cplx_estimation_trash_p =
1931 s->cplx_estimation_trash_b = 0;
ca334dd1
MN
1932 }
1933
48e13940 1934 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
ca334dd1 1935
d234c7a0
VG
1936 s->data_partitioning = get_bits1(gb);
1937 if (s->data_partitioning)
bc5abfb1 1938 ctx->rvlc = get_bits1(gb);
ca334dd1 1939
d234c7a0
VG
1940 if (vo_ver_id != 1) {
1941 s->new_pred = get_bits1(gb);
1942 if (s->new_pred) {
ca334dd1
MN
1943 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
1944 skip_bits(gb, 2); /* requested upstream message type */
d234c7a0 1945 skip_bits1(gb); /* newpred segment type */
ca334dd1 1946 }
d234c7a0
VG
1947 s->reduced_res_vop = get_bits1(gb);
1948 if (s->reduced_res_vop)
1949 av_log(s->avctx, AV_LOG_ERROR,
1950 "reduced resolution VOP not supported\n");
1951 } else {
1952 s->new_pred = 0;
1953 s->reduced_res_vop = 0;
ca334dd1
MN
1954 }
1955
d234c7a0 1956 s->scalability = get_bits1(gb);
ca334dd1
MN
1957
1958 if (s->scalability) {
d234c7a0 1959 GetBitContext bak = *gb;
ca334dd1
MN
1960 int h_sampling_factor_n;
1961 int h_sampling_factor_m;
1962 int v_sampling_factor_n;
1963 int v_sampling_factor_m;
1964
d234c7a0 1965 s->hierachy_type = get_bits1(gb);
e65ab9d9
MR
1966 skip_bits(gb, 4); /* ref_layer_id */
1967 skip_bits1(gb); /* ref_layer_sampling_dir */
d234c7a0
VG
1968 h_sampling_factor_n = get_bits(gb, 5);
1969 h_sampling_factor_m = get_bits(gb, 5);
1970 v_sampling_factor_n = get_bits(gb, 5);
1971 v_sampling_factor_m = get_bits(gb, 5);
1972 s->enhancement_type = get_bits1(gb);
1973
1974 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
1975 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
ca334dd1
MN
1976 /* illegal scalability header (VERY broken encoder),
1977 * trying to workaround */
d234c7a0
VG
1978 s->scalability = 0;
1979 *gb = bak;
1980 } else
ca334dd1
MN
1981 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
1982
1983 // bin shape stuff FIXME
1984 }
1985 }
d234c7a0 1986
ca334dd1
MN
1987 return 0;
1988}
1989
1990/**
58c42af7 1991 * Decode the user data stuff in the header.
ca334dd1
MN
1992 * Also initializes divx/xvid/lavc_version/build.
1993 */
d234c7a0
VG
1994static int decode_user_data(MpegEncContext *s, GetBitContext *gb)
1995{
ca334dd1
MN
1996 char buf[256];
1997 int i;
1998 int e;
1999 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2000 char last;
2001
d234c7a0
VG
2002 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2003 if (show_bits(gb, 23) == 0)
2004 break;
2005 buf[i] = get_bits(gb, 8);
ca334dd1 2006 }
d234c7a0 2007 buf[i] = 0;
ca334dd1
MN
2008
2009 /* divx detection */
d234c7a0
VG
2010 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2011 if (e < 2)
2012 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2013 if (e >= 2) {
2014 s->divx_version = ver;
2015 s->divx_build = build;
2016 s->divx_packed = e == 3 && last == 'p';
2017 if (s->divx_packed && !s->showed_packed_warning) {
2018 av_log(s->avctx, AV_LOG_WARNING,
2019 "Invalid and inefficient vfw-avi packed B frames detected\n");
2020 s->showed_packed_warning = 1;
ca334dd1
MN
2021 }
2022 }
2023
2f5df0b1 2024 /* libavcodec detection */
d234c7a0
VG
2025 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2026 if (e != 4)
2027 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2028 if (e != 4) {
2029 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2030 if (e > 1)
2031 build = (ver << 16) + (ver2 << 8) + ver3;
ca334dd1 2032 }
d234c7a0
VG
2033 if (e != 4) {
2034 if (strcmp(buf, "ffmpeg") == 0)
2035 s->lavc_build = 4600;
ca334dd1 2036 }
d234c7a0
VG
2037 if (e == 4)
2038 s->lavc_build = build;
ca334dd1
MN
2039
2040 /* Xvid detection */
d234c7a0
VG
2041 e = sscanf(buf, "XviD%d", &build);
2042 if (e == 1)
2043 s->xvid_build = build;
ca334dd1
MN
2044
2045 return 0;
2046}
2047
ee8af2dd 2048static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
d234c7a0 2049{
ee8af2dd 2050 MpegEncContext *s = &ctx->m;
ca334dd1
MN
2051 int time_incr, time_increment;
2052
975a1447 2053 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
d234c7a0
VG
2054 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2055 s->vol_control_parameters == 0 && !(s->flags & CODEC_FLAG_LOW_DELAY)) {
ca334dd1 2056 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
d234c7a0 2057 s->low_delay = 0;
ca334dd1
MN
2058 }
2059
d234c7a0
VG
2060 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2061 if (s->partitioned_frame)
2062 s->decode_mb = mpeg4_decode_partitioned_mb;
ca334dd1 2063 else
d234c7a0 2064 s->decode_mb = mpeg4_decode_mb;
ca334dd1 2065
d234c7a0 2066 time_incr = 0;
ca334dd1
MN
2067 while (get_bits1(gb) != 0)
2068 time_incr++;
2069
2070 check_marker(gb, "before time_increment");
2071
e62a43f6
AK
2072 if (ctx->time_increment_bits == 0 ||
2073 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
d234c7a0
VG
2074 av_log(s->avctx, AV_LOG_ERROR,
2075 "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
2076
e62a43f6
AK
2077 for (ctx->time_increment_bits = 1;
2078 ctx->time_increment_bits < 16;
2079 ctx->time_increment_bits++) {
d234c7a0
VG
2080 if (s->pict_type == AV_PICTURE_TYPE_P ||
2081 (s->pict_type == AV_PICTURE_TYPE_S &&
1a890257 2082 ctx->vol_sprite_usage == GMC_SPRITE)) {
e62a43f6 2083 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
d234c7a0 2084 break;
e62a43f6 2085 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
d234c7a0 2086 break;
ca334dd1
MN
2087 }
2088
d234c7a0 2089 av_log(s->avctx, AV_LOG_ERROR,
e62a43f6 2090 "my guess is %d bits ;)\n", ctx->time_increment_bits);
ca334dd1
MN
2091 }
2092
d234c7a0
VG
2093 if (IS_3IV1)
2094 time_increment = get_bits1(gb); // FIXME investigate further
2095 else
e62a43f6 2096 time_increment = get_bits(gb, ctx->time_increment_bits);
d234c7a0
VG
2097
2098 if (s->pict_type != AV_PICTURE_TYPE_B) {
2099 s->last_time_base = s->time_base;
2100 s->time_base += time_incr;
2101 s->time = s->time_base * s->avctx->time_base.den + time_increment;
2102 if (s->workaround_bugs & FF_BUG_UMP4) {
2103 if (s->time < s->last_non_b_time) {
ca334dd1
MN
2104 /* header is not mpeg-4-compatible, broken encoder,
2105 * trying to workaround */
2106 s->time_base++;
d234c7a0 2107 s->time += s->avctx->time_base.den;
ca334dd1
MN
2108 }
2109 }
d234c7a0
VG
2110 s->pp_time = s->time - s->last_non_b_time;
2111 s->last_non_b_time = s->time;
2112 } else {
2113 s->time = (s->last_time_base + time_incr) * s->avctx->time_base.den + time_increment;
2114 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2115 if (s->pp_time <= s->pb_time ||
2116 s->pp_time <= s->pp_time - s->pb_time ||
2117 s->pp_time <= 0) {
ca334dd1
MN
2118 /* messed up order, maybe after seeking? skipping current b-frame */
2119 return FRAME_SKIPPED;
2120 }
2121 ff_mpeg4_init_direct_mv(s);
2122
d234c7a0
VG
2123 if (s->t_frame == 0)
2124 s->t_frame = s->pb_time;
2125 if (s->t_frame == 0)
2126 s->t_frame = 1; // 1/0 protection
2127 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, s->t_frame) -
2128 ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame)) * 2;
2129 s->pb_field_time = (ROUNDED_DIV(s->time, s->t_frame) -
2130 ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame)) * 2;
2131 if (!s->progressive_sequence) {
2132 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
ca334dd1
MN
2133 return FRAME_SKIPPED;
2134 }
2135 }
2136
ca334dd1
MN
2137 check_marker(gb, "before vop_coded");
2138
2139 /* vop coded */
d234c7a0
VG
2140 if (get_bits1(gb) != 1) {
2141 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
ca334dd1
MN
2142 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2143 return FRAME_SKIPPED;
2144 }
ee8af2dd 2145 if (ctx->shape != BIN_ONLY_SHAPE &&
d234c7a0
VG
2146 (s->pict_type == AV_PICTURE_TYPE_P ||
2147 (s->pict_type == AV_PICTURE_TYPE_S &&
1a890257 2148 ctx->vol_sprite_usage == GMC_SPRITE))) {
ca334dd1
MN
2149 /* rounding type for motion estimation */
2150 s->no_rounding = get_bits1(gb);
2151 } else {
2152 s->no_rounding = 0;
2153 }
d234c7a0
VG
2154 // FIXME reduced res stuff
2155
ee8af2dd 2156 if (ctx->shape != RECT_SHAPE) {
1a890257 2157 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
d234c7a0
VG
2158 skip_bits(gb, 13); /* width */
2159 skip_bits1(gb); /* marker */
2160 skip_bits(gb, 13); /* height */
2161 skip_bits1(gb); /* marker */
2162 skip_bits(gb, 13); /* hor_spat_ref */
2163 skip_bits1(gb); /* marker */
2164 skip_bits(gb, 13); /* ver_spat_ref */
2165 }
2166 skip_bits1(gb); /* change_CR_disable */
2167
2168 if (get_bits1(gb) != 0)
2169 skip_bits(gb, 8); /* constant_alpha_value */
2170 }
2171
2172 // FIXME complexity estimation stuff
2173
ee8af2dd 2174 if (ctx->shape != BIN_ONLY_SHAPE) {
d234c7a0
VG
2175 skip_bits_long(gb, s->cplx_estimation_trash_i);
2176 if (s->pict_type != AV_PICTURE_TYPE_I)
ca334dd1 2177 skip_bits_long(gb, s->cplx_estimation_trash_p);
d234c7a0 2178 if (s->pict_type == AV_PICTURE_TYPE_B)
ca334dd1
MN
2179 skip_bits_long(gb, s->cplx_estimation_trash_b);
2180
d234c7a0
VG
2181 s->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2182 if (!s->progressive_sequence) {
2183 s->top_field_first = get_bits1(gb);
2184 s->alternate_scan = get_bits1(gb);
2185 } else
2186 s->alternate_scan = 0;
2187 }
2188
2189 if (s->alternate_scan) {
2190 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2191 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2192 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2193 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2194 } else {
2195 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2196 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2197 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2198 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2199 }
2200
2201 if (s->pict_type == AV_PICTURE_TYPE_S &&
1a890257
AK
2202 (ctx->vol_sprite_usage == STATIC_SPRITE ||
2203 ctx->vol_sprite_usage == GMC_SPRITE)) {
d234c7a0
VG
2204 if (mpeg4_decode_sprite_trajectory(s, gb) < 0)
2205 return AVERROR_INVALIDDATA;
2206 if (s->sprite_brightness_change)
2207 av_log(s->avctx, AV_LOG_ERROR,
2208 "sprite_brightness_change not supported\n");
1a890257 2209 if (ctx->vol_sprite_usage == STATIC_SPRITE)
d234c7a0
VG
2210 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2211 }
2212
ee8af2dd 2213 if (ctx->shape != BIN_ONLY_SHAPE) {
d234c7a0
VG
2214 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2215 if (s->qscale == 0) {
2216 av_log(s->avctx, AV_LOG_ERROR,
2217 "Error, header damaged or not MPEG4 header (qscale=0)\n");
2218 return -1; // makes no sense to continue, as there is nothing left from the image then
2219 }
2220
2221 if (s->pict_type != AV_PICTURE_TYPE_I) {
2222 s->f_code = get_bits(gb, 3); /* fcode_for */
2223 if (s->f_code == 0) {
2224 av_log(s->avctx, AV_LOG_ERROR,
2225 "Error, header damaged or not MPEG4 header (f_code=0)\n");
2226 return -1; // makes no sense to continue, as there is nothing left from the image then
2227 }
2228 } else
2229 s->f_code = 1;
2230
2231 if (s->pict_type == AV_PICTURE_TYPE_B) {
2232 s->b_code = get_bits(gb, 3);
2233 } else
2234 s->b_code = 1;
2235
2236 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2237 av_log(s->avctx, AV_LOG_DEBUG,
2238 "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\n",
2239 s->qscale, s->f_code, s->b_code,
2240 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")),
2241 gb->size_in_bits, s->progressive_sequence, s->alternate_scan,
2242 s->top_field_first, s->quarter_sample ? "q" : "h",
48e13940 2243 s->data_partitioning, ctx->resync_marker,
d234c7a0
VG
2244 s->num_sprite_warping_points, s->sprite_warping_accuracy,
2245 1 - s->no_rounding, s->vo_type,
2246 s->vol_control_parameters ? " VOLC" : " ", s->intra_dc_threshold,
2247 s->cplx_estimation_trash_i, s->cplx_estimation_trash_p,
2248 s->cplx_estimation_trash_b);
2249 }
2250
2251 if (!s->scalability) {
ee8af2dd 2252 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
d234c7a0
VG
2253 skip_bits1(gb); // vop shape coding type
2254 } else {
2255 if (s->enhancement_type) {
2256 int load_backward_shape = get_bits1(gb);
2257 if (load_backward_shape)
2258 av_log(s->avctx, AV_LOG_ERROR,
2259 "load backward shape isn't supported\n");
2260 }
2261 skip_bits(gb, 2); // ref_select_code
2262 }
2263 }
2264 /* detect buggy encoders which don't set the low_delay flag
2265 * (divx4/xvid/opendivx). Note we cannot detect divx5 without b-frames
2266 * easily (although it's buggy too) */
2267 if (s->vo_type == 0 && s->vol_control_parameters == 0 &&
2268 s->divx_version == -1 && s->picture_number == 0) {
2269 av_log(s->avctx, AV_LOG_WARNING,
2270 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2271 s->low_delay = 1;
2272 }
2273
2274 s->picture_number++; // better than pic number==0 always ;)
2275
2276 // FIXME add short header support
2277 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2278 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2279
2280 if (s->workaround_bugs & FF_BUG_EDGE) {
2281 s->h_edge_pos = s->width;
2282 s->v_edge_pos = s->height;
2283 }
2284 return 0;
ca334dd1
MN
2285}
2286
2287/**
58c42af7 2288 * Decode mpeg4 headers.
ca334dd1
MN
2289 * @return <0 if no VOP found (or a damaged one)
2290 * FRAME_SKIPPED if a not coded VOP is found
2291 * 0 if a VOP is found
2292 */
ee8af2dd 2293int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
ca334dd1 2294{
ee8af2dd 2295 MpegEncContext *s = &ctx->m;
581810f5 2296 unsigned startcode, v;
ca334dd1
MN
2297
2298 /* search next start code */
2299 align_get_bits(gb);
2300
d234c7a0 2301 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
ca334dd1 2302 skip_bits(gb, 24);
d234c7a0 2303 if (get_bits(gb, 8) == 0xF0)
ca334dd1
MN
2304 goto end;
2305 }
2306
2307 startcode = 0xff;
d234c7a0
VG
2308 for (;;) {
2309 if (get_bits_count(gb) >= gb->size_in_bits) {
2310 if (gb->size_in_bits == 8 &&
2311 (s->divx_version >= 0 || s->xvid_build >= 0)) {
ae210479 2312 av_log(s->avctx, AV_LOG_WARNING, "frame skip %d\n", gb->size_in_bits);
d234c7a0
VG
2313 return FRAME_SKIPPED; // divx bug
2314 } else
2315 return -1; // end of stream
ca334dd1
MN
2316 }
2317
2318 /* use the bits after the test */
2319 v = get_bits(gb, 8);
2320 startcode = ((startcode << 8) | v) & 0xffffffff;
2321
d234c7a0
VG
2322 if ((startcode & 0xFFFFFF00) != 0x100)
2323 continue; // no startcode
ca334dd1 2324
d234c7a0 2325 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
ca334dd1 2326 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
d234c7a0
VG
2327 if (startcode <= 0x11F)
2328 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2329 else if (startcode <= 0x12F)
2330 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2331 else if (startcode <= 0x13F)
2332 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2333 else if (startcode <= 0x15F)
2334 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2335 else if (startcode <= 0x1AF)
2336 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2337 else if (startcode == 0x1B0)
2338 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2339 else if (startcode == 0x1B1)
2340 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2341 else if (startcode == 0x1B2)
2342 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2343 else if (startcode == 0x1B3)
2344 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2345 else if (startcode == 0x1B4)
2346 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2347 else if (startcode == 0x1B5)
2348 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2349 else if (startcode == 0x1B6)
2350 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2351 else if (startcode == 0x1B7)
2352 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2353 else if (startcode == 0x1B8)
2354 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2355 else if (startcode == 0x1B9)
2356 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2357 else if (startcode == 0x1BA)
2358 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2359 else if (startcode == 0x1BB)
2360 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2361 else if (startcode == 0x1BC)
2362 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2363 else if (startcode == 0x1BD)
2364 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2365 else if (startcode == 0x1BE)
2366 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2367 else if (startcode == 0x1BF)
2368 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2369 else if (startcode == 0x1C0)
2370 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2371 else if (startcode == 0x1C1)
2372 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2373 else if (startcode == 0x1C2)
2374 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2375 else if (startcode == 0x1C3)
2376 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2377 else if (startcode <= 0x1C5)
2378 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2379 else if (startcode <= 0x1FF)
2380 av_log(s->avctx, AV_LOG_DEBUG, "System start");
ca334dd1
MN
2381 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2382 }
2383
d234c7a0 2384 if (startcode >= 0x120 && startcode <= 0x12F) {
ee8af2dd 2385 if (decode_vol_header(ctx, gb) < 0)
ca334dd1 2386 return -1;
d234c7a0 2387 } else if (startcode == USER_DATA_STARTCODE) {
ca334dd1 2388 decode_user_data(s, gb);
d234c7a0 2389 } else if (startcode == GOP_STARTCODE) {
ca334dd1 2390 mpeg4_decode_gop_header(s, gb);
d234c7a0 2391 } else if (startcode == VOS_STARTCODE) {
0637e505 2392 mpeg4_decode_profile_level(s, gb);
d234c7a0 2393 } else if (startcode == VOP_STARTCODE) {
ca334dd1
MN
2394 break;
2395 }
2396
2397 align_get_bits(gb);
2398 startcode = 0xff;
2399 }
d234c7a0 2400
ca334dd1 2401end:
d234c7a0
VG
2402 if (s->flags & CODEC_FLAG_LOW_DELAY)
2403 s->low_delay = 1;
2404 s->avctx->has_b_frames = !s->low_delay;
2405
b452d5ae
AK
2406 if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1) {
2407 if (s->stream_codec_tag == AV_RL32("XVID") ||
2408 s->codec_tag == AV_RL32("XVID") ||
2409 s->codec_tag == AV_RL32("XVIX") ||
2410 s->codec_tag == AV_RL32("RMP4") ||
2411 s->codec_tag == AV_RL32("ZMP4") ||
2412 s->codec_tag == AV_RL32("SIPP"))
2413 s->xvid_build = 0;
2414 }
2415
2416 if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1)
2417 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2418 s->vol_control_parameters == 0)
2419 s->divx_version = 400; // divx 4
2420
2421 if (s->xvid_build >= 0 && s->divx_version >= 0) {
2422 s->divx_version =
2423 s->divx_build = -1;
2424 }
2425
2426 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2427 if (s->codec_tag == AV_RL32("XVIX"))
2428 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2429
2430 if (s->codec_tag == AV_RL32("UMP4"))
2431 s->workaround_bugs |= FF_BUG_UMP4;
2432
2433 if (s->divx_version >= 500 && s->divx_build < 1814)
2434 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2435
2436 if (s->divx_version > 502 && s->divx_build < 1814)
2437 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2438
2439 if (s->xvid_build <= 3U)
2440 s->padding_bug_score = 256 * 256 * 256 * 64;
2441
2442 if (s->xvid_build <= 1U)
2443 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2444
2445 if (s->xvid_build <= 12U)
2446 s->workaround_bugs |= FF_BUG_EDGE;
2447
2448 if (s->xvid_build <= 32U)
2449 s->workaround_bugs |= FF_BUG_DC_CLIP;
2450
2451 if (s->lavc_build < 4653U)
2452 s->workaround_bugs |= FF_BUG_STD_QPEL;
2453
2454 if (s->lavc_build < 4655U)
2455 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2456
2457 if (s->lavc_build < 4670U)
2458 s->workaround_bugs |= FF_BUG_EDGE;
2459
2460 if (s->lavc_build <= 4712U)
2461 s->workaround_bugs |= FF_BUG_DC_CLIP;
2462
2463 if (s->divx_version >= 0)
2464 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2465
2466 if (s->divx_version == 501 && s->divx_build == 20020416)
2467 s->padding_bug_score = 256 * 256 * 256 * 64;
2468
2469 if (s->divx_version < 500U)
2470 s->workaround_bugs |= FF_BUG_EDGE;
2471
2472 if (s->divx_version >= 0)
2473 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2474 }
2475
2476#if HAVE_MMX
2477 if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build >= 0 &&
2478 s->avctx->idct_algo == FF_IDCT_AUTO &&
2479 (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
2480 s->avctx->idct_algo = FF_IDCT_XVIDMMX;
2481 ff_dct_common_init(s);
2482 s->picture_number = 0;
2483 }
2484#endif
2485
2486 if (s->avctx->debug & FF_DEBUG_BUGS)
2487 av_log(s->avctx, AV_LOG_DEBUG,
2488 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2489 s->workaround_bugs, s->lavc_build, s->xvid_build,
2490 s->divx_version, s->divx_build, s->divx_packed ? "p" : "");
2491
ee8af2dd 2492 return decode_vop_header(ctx, gb);
ca334dd1
MN
2493}
2494
35e0833d
AK
2495static int mpeg4_update_thread_context(AVCodecContext *dst,
2496 const AVCodecContext *src)
2497{
2498 Mpeg4DecContext *s = dst->priv_data;
2499 const Mpeg4DecContext *s1 = src->priv_data;
2500
2501 int ret = ff_mpeg_update_thread_context(dst, src);
2502
2503 if (ret < 0)
2504 return ret;
2505
e62a43f6
AK
2506 s->shape = s1->shape;
2507 s->time_increment_bits = s1->time_increment_bits;
ee8af2dd 2508
35e0833d
AK
2509 return 0;
2510}
2511
ca334dd1
MN
2512static av_cold int decode_init(AVCodecContext *avctx)
2513{
35e0833d
AK
2514 Mpeg4DecContext *ctx = avctx->priv_data;
2515 MpegEncContext *s = &ctx->m;
ca334dd1
MN
2516 int ret;
2517 static int done = 0;
2518
d234c7a0
VG
2519 s->divx_version =
2520 s->divx_build =
2521 s->xvid_build =
2522 s->lavc_build = -1;
b317567c 2523
d234c7a0 2524 if ((ret = ff_h263_decode_init(avctx)) < 0)
ca334dd1
MN
2525 return ret;
2526
2527 if (!done) {
2528 done = 1;
2529
e96b4a53 2530 ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
6f13a371
MS
2531 ff_init_rl(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
2532 ff_init_rl(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
fef59a5b 2533 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
6f13a371
MS
2534 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
2535 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
ca334dd1 2536 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
d234c7a0
VG
2537 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2538 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
ca334dd1 2539 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
d234c7a0
VG
2540 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2541 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
ca334dd1 2542 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
d234c7a0
VG
2543 &ff_sprite_trajectory_tab[0][1], 4, 2,
2544 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
ca334dd1 2545 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
d234c7a0
VG
2546 &ff_mb_type_b_tab[0][1], 2, 1,
2547 &ff_mb_type_b_tab[0][0], 2, 1, 16);
ca334dd1
MN
2548 }
2549
2550 s->h263_pred = 1;
d234c7a0
VG
2551 s->low_delay = 0; /* default, might be overriden in the vol header during header parsing */
2552 s->decode_mb = mpeg4_decode_mb;
e62a43f6 2553 ctx->time_increment_bits = 4; /* default value for broken headers */
ca334dd1 2554
d234c7a0 2555 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
759001c5
AK
2556 avctx->internal->allocate_progress = 1;
2557
ca334dd1
MN
2558 return 0;
2559}
2560
c4ab43ff
TF
2561static const AVProfile mpeg4_video_profiles[] = {
2562 { FF_PROFILE_MPEG4_SIMPLE, "Simple Profile" },
2563 { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, "Simple Scalable Profile" },
2564 { FF_PROFILE_MPEG4_CORE, "Core Profile" },
2565 { FF_PROFILE_MPEG4_MAIN, "Main Profile" },
2566 { FF_PROFILE_MPEG4_N_BIT, "N-bit Profile" },
2567 { FF_PROFILE_MPEG4_SCALABLE_TEXTURE, "Scalable Texture Profile" },
2568 { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION, "Simple Face Animation Profile" },
2569 { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE, "Basic Animated Texture Profile" },
2570 { FF_PROFILE_MPEG4_HYBRID, "Hybrid Profile" },
2571 { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME, "Advanced Real Time Simple Profile" },
2572 { FF_PROFILE_MPEG4_CORE_SCALABLE, "Code Scalable Profile" },
2573 { FF_PROFILE_MPEG4_ADVANCED_CODING, "Advanced Coding Profile" },
2574 { FF_PROFILE_MPEG4_ADVANCED_CORE, "Advanced Core Profile" },
2575 { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
2576 { FF_PROFILE_MPEG4_SIMPLE_STUDIO, "Simple Studio Profile" },
2577 { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, "Advanced Simple Profile" },
2578};
2579
d36beb3f 2580AVCodec ff_mpeg4_decoder = {
00c3b67b 2581 .name = "mpeg4",
b2bed932 2582 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
00c3b67b 2583 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 2584 .id = AV_CODEC_ID_MPEG4,
35e0833d 2585 .priv_data_size = sizeof(Mpeg4DecContext),
00c3b67b
MS
2586 .init = decode_init,
2587 .close = ff_h263_decode_end,
2588 .decode = ff_h263_decode_frame,
2589 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2590 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2591 CODEC_CAP_FRAME_THREADS,
2592 .flush = ff_mpeg_flush,
8d061989 2593 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
00c3b67b 2594 .profiles = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
35e0833d 2595 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
ca334dd1 2596};