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