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