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