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