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