FFmpeg
mpeg4videodec.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 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 "config_components.h"
26 
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/thread.h"
30 #include "codec_internal.h"
31 #include "error_resilience.h"
32 #include "hwconfig.h"
33 #include "idctdsp.h"
34 #include "mpegutils.h"
35 #include "mpegvideo.h"
36 #include "mpegvideodata.h"
37 #include "mpegvideodec.h"
38 #include "mpeg4video.h"
39 #include "mpeg4videodata.h"
40 #include "mpeg4videodec.h"
41 #include "mpeg4videodefs.h"
42 #include "h263.h"
43 #include "h263data.h"
44 #include "h263dec.h"
45 #include "profiles.h"
46 #include "threadframe.h"
47 #include "xvididct.h"
48 #include "unary.h"
49 
50 /* The defines below define the number of bits that are read at once for
51  * reading vlc values. Changing these may improve speed and data cache needs
52  * be aware though that decreasing them may need the number of stages that is
53  * passed to get_vlc* to be increased. */
54 #define SPRITE_TRAJ_VLC_BITS 6
55 #define DC_VLC_BITS 9
56 #define MB_TYPE_B_VLC_BITS 4
57 #define STUDIO_INTRA_BITS 9
58 
62 static VLC studio_intra_tab[12];
65 
66 static const uint8_t mpeg4_block_count[4] = { 0, 6, 8, 12 };
67 
68 static const int mb_type_b_map[4] = {
73 };
74 
75 void ff_mpeg4_decode_studio(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb,
76  uint8_t *dest_cr, int block_size, int uvlinesize,
77  int dct_linesize, int dct_offset)
78 {
80  const int act_block_size = block_size * 2;
81 
82  if (ctx->dpcm_direction == 0) {
83  s->idsp.idct_put(dest_y, dct_linesize, (int16_t*)ctx->block32[0]);
84  s->idsp.idct_put(dest_y + act_block_size, dct_linesize, (int16_t*)ctx->block32[1]);
85  s->idsp.idct_put(dest_y + dct_offset, dct_linesize, (int16_t*)ctx->block32[2]);
86  s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)ctx->block32[3]);
87 
88  dct_linesize = uvlinesize << s->interlaced_dct;
89  dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
90 
91  s->idsp.idct_put(dest_cb, dct_linesize, (int16_t*)ctx->block32[4]);
92  s->idsp.idct_put(dest_cr, dct_linesize, (int16_t*)ctx->block32[5]);
93  s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)ctx->block32[6]);
94  s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)ctx->block32[7]);
95  if (!s->chroma_x_shift){ //Chroma444
96  s->idsp.idct_put(dest_cb + act_block_size, dct_linesize, (int16_t*)ctx->block32[8]);
97  s->idsp.idct_put(dest_cr + act_block_size, dct_linesize, (int16_t*)ctx->block32[9]);
98  s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[10]);
99  s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[11]);
100  }
101  } else if (ctx->dpcm_direction == 1) {
102  uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
103  int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
104  for (int i = 0; i < 3; i++) {
105  const uint16_t *src = ctx->dpcm_macroblock[i];
106  int vsub = i ? s->chroma_y_shift : 0;
107  int hsub = i ? s->chroma_x_shift : 0;
108  int lowres = s->avctx->lowres;
109  int step = 1 << lowres;
110  for (int h = 0; h < (16 >> (vsub + lowres)); h++){
111  for (int w = 0, idx = 0; w < (16 >> (hsub + lowres)); w++, idx += step)
112  dest_pcm[i][w] = src[idx];
113  dest_pcm[i] += linesize[i] / 2;
114  src += (16 >> hsub) * step;
115  }
116  }
117  } else {
118  uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
119  int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
120  av_assert2(ctx->dpcm_direction == -1);
121  for (int i = 0; i < 3; i++) {
122  const uint16_t *src = ctx->dpcm_macroblock[i];
123  int vsub = i ? s->chroma_y_shift : 0;
124  int hsub = i ? s->chroma_x_shift : 0;
125  int lowres = s->avctx->lowres;
126  int step = 1 << lowres;
127  dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub) - 1);
128  for (int h = (16 >> (vsub + lowres)) - 1; h >= 0; h--){
129  for (int w = (16 >> (hsub + lowres)) - 1, idx = 0; w >= 0; w--, idx += step)
130  dest_pcm[i][w] = src[idx];
131  src += step * (16 >> hsub);
132  dest_pcm[i] -= linesize[i] / 2;
133  }
134  }
135  }
136 }
137 
138 /**
139  * Predict the ac.
140  * @param n block index (0-3 are luma, 4-5 are chroma)
141  * @param dir the ac prediction direction
142  */
143 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
144 {
145  int i;
146  int16_t *ac_val, *ac_val1;
147  int8_t *const qscale_table = s->current_picture.qscale_table;
148 
149  /* find prediction */
150  ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
151  ac_val1 = ac_val;
152  if (s->ac_pred) {
153  if (dir == 0) {
154  const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
155  /* left prediction */
156  ac_val -= 16;
157 
158  if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
159  n == 1 || n == 3) {
160  /* same qscale */
161  for (i = 1; i < 8; i++)
162  block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
163  } else {
164  /* different qscale, we must rescale */
165  for (i = 1; i < 8; i++)
166  block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
167  }
168  } else {
169  const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
170  /* top prediction */
171  ac_val -= 16 * s->block_wrap[n];
172 
173  if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
174  n == 2 || n == 3) {
175  /* same qscale */
176  for (i = 1; i < 8; i++)
177  block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
178  } else {
179  /* different qscale, we must rescale */
180  for (i = 1; i < 8; i++)
181  block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
182  }
183  }
184  }
185  /* left copy */
186  for (i = 1; i < 8; i++)
187  ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
188 
189  /* top copy */
190  for (i = 1; i < 8; i++)
191  ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
192 }
193 
194 /**
195  * check if the next stuff is a resync marker or the end.
196  * @return 0 if not
197  */
199 {
200  MpegEncContext *s = &ctx->m;
201  int bits_count = get_bits_count(&s->gb);
202  int v = show_bits(&s->gb, 16);
203 
204  if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
205  return 0;
206 
207  while (v <= 0xFF) {
208  if (s->pict_type == AV_PICTURE_TYPE_B ||
209  (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
210  break;
211  skip_bits(&s->gb, 8 + s->pict_type);
212  bits_count += 8 + s->pict_type;
213  v = show_bits(&s->gb, 16);
214  }
215 
216  if (bits_count + 8 >= s->gb.size_in_bits) {
217  v >>= 8;
218  v |= 0x7F >> (7 - (bits_count & 7));
219 
220  if (v == 0x7F)
221  return s->mb_num;
222  } else {
223  if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
224  int len, mb_num;
225  int mb_num_bits = av_log2(s->mb_num - 1) + 1;
226  GetBitContext gb = s->gb;
227 
228  skip_bits(&s->gb, 1);
229  align_get_bits(&s->gb);
230 
231  for (len = 0; len < 32; len++)
232  if (get_bits1(&s->gb))
233  break;
234 
235  mb_num = get_bits(&s->gb, mb_num_bits);
236  if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
237  mb_num= -1;
238 
239  s->gb = gb;
240 
242  return mb_num;
243  }
244  }
245  return 0;
246 }
247 
249 {
250  MpegEncContext *s = &ctx->m;
251  int a = 2 << s->sprite_warping_accuracy;
252  int rho = 3 - s->sprite_warping_accuracy;
253  int r = 16 / a;
254  int alpha = 1;
255  int beta = 0;
256  int w = s->width;
257  int h = s->height;
258  int min_ab, i, w2, h2, w3, h3;
259  int sprite_ref[4][2];
260  int virtual_ref[2][2];
261  int64_t sprite_offset[2][2];
262  int64_t sprite_delta[2][2];
263 
264  // only true for rectangle shapes
265  const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
266  { 0, s->height }, { s->width, s->height } };
267  int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
268 
269  if (w <= 0 || h <= 0)
270  return AVERROR_INVALIDDATA;
271 
272  for (i = 0; i < ctx->num_sprite_warping_points; i++) {
273  int length;
274  int x = 0, y = 0;
275 
277  if (length > 0)
278  x = get_xbits(gb, length);
279 
280  if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
281  check_marker(s->avctx, gb, "before sprite_trajectory");
282 
284  if (length > 0)
285  y = get_xbits(gb, length);
286 
287  check_marker(s->avctx, gb, "after sprite_trajectory");
288  ctx->sprite_traj[i][0] = d[i][0] = x;
289  ctx->sprite_traj[i][1] = d[i][1] = y;
290  }
291  for (; i < 4; i++)
292  ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
293 
294  while ((1 << alpha) < w)
295  alpha++;
296  while ((1 << beta) < h)
297  beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
298  w2 = 1 << alpha;
299  h2 = 1 << beta;
300 
301  // Note, the 4th point isn't used for GMC
302  if (ctx->divx_version == 500 && ctx->divx_build == 413) {
303  sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
304  sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
305  sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
306  sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
307  sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
308  sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
309  } else {
310  sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
311  sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
312  sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
313  sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
314  sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
315  sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
316  }
317  /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
318  * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
319 
320  /* This is mostly identical to the MPEG-4 std (and is totally unreadable
321  * because of that...). Perhaps it should be reordered to be more readable.
322  * The idea behind this virtual_ref mess is to be able to use shifts later
323  * per pixel instead of divides so the distance between points is converted
324  * from w&h based to w2&h2 based which are of the 2^x form. */
325  virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
326  ROUNDED_DIV(((w - w2) *
327  (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
328  w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
329  virtual_ref[0][1] = 16 * vop_ref[0][1] +
330  ROUNDED_DIV(((w - w2) *
331  (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
332  w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
333  virtual_ref[1][0] = 16 * vop_ref[0][0] +
334  ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
335  h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
336  virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
337  ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
338  h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
339 
340  switch (ctx->num_sprite_warping_points) {
341  case 0:
342  sprite_offset[0][0] =
343  sprite_offset[0][1] =
344  sprite_offset[1][0] =
345  sprite_offset[1][1] = 0;
346  sprite_delta[0][0] = a;
347  sprite_delta[0][1] =
348  sprite_delta[1][0] = 0;
349  sprite_delta[1][1] = a;
350  ctx->sprite_shift[0] =
351  ctx->sprite_shift[1] = 0;
352  break;
353  case 1: // GMC only
354  sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
355  sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
356  sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
357  a * (vop_ref[0][0] / 2);
358  sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
359  a * (vop_ref[0][1] / 2);
360  sprite_delta[0][0] = a;
361  sprite_delta[0][1] =
362  sprite_delta[1][0] = 0;
363  sprite_delta[1][1] = a;
364  ctx->sprite_shift[0] =
365  ctx->sprite_shift[1] = 0;
366  break;
367  case 2:
368  sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
369  ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
370  ((int64_t) -vop_ref[0][0]) +
371  ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
372  ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
373  sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
374  ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
375  ((int64_t) -vop_ref[0][0]) +
376  ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
377  ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
378  sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
379  ((int64_t)-2 * vop_ref[0][0] + 1) +
380  ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
381  ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
382  (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
383  sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
384  ((int64_t)-2 * vop_ref[0][0] + 1) +
385  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
386  ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
387  (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
388  sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
389  sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
390  sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
391  sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
392 
393  ctx->sprite_shift[0] = alpha + rho;
394  ctx->sprite_shift[1] = alpha + rho + 2;
395  break;
396  case 3:
397  min_ab = FFMIN(alpha, beta);
398  w3 = w2 >> min_ab;
399  h3 = h2 >> min_ab;
400  sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
401  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
402  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
403  ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
404  sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
405  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
406  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
407  ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
408  sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
409  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
410  (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
411  ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
412  sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
413  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
414  (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
415  ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
416  sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
417  sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
418  sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
419  sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
420 
421  ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
422  ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
423  break;
424  }
425  /* try to simplify the situation */
426  if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
427  sprite_delta[0][1] == 0 &&
428  sprite_delta[1][0] == 0 &&
429  sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
430  sprite_offset[0][0] >>= ctx->sprite_shift[0];
431  sprite_offset[0][1] >>= ctx->sprite_shift[0];
432  sprite_offset[1][0] >>= ctx->sprite_shift[1];
433  sprite_offset[1][1] >>= ctx->sprite_shift[1];
434  sprite_delta[0][0] = a;
435  sprite_delta[0][1] = 0;
436  sprite_delta[1][0] = 0;
437  sprite_delta[1][1] = a;
438  ctx->sprite_shift[0] = 0;
439  ctx->sprite_shift[1] = 0;
440  s->real_sprite_warping_points = 1;
441  } else {
442  int shift_y = 16 - ctx->sprite_shift[0];
443  int shift_c = 16 - ctx->sprite_shift[1];
444 
445  for (i = 0; i < 2; i++) {
446  if (shift_c < 0 || shift_y < 0 ||
447  FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
448  FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
449  FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
450  FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
451  ) {
452  avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
453  goto overflow;
454  }
455  }
456 
457  for (i = 0; i < 2; i++) {
458  sprite_offset[0][i] *= 1 << shift_y;
459  sprite_offset[1][i] *= 1 << shift_c;
460  sprite_delta[0][i] *= 1 << shift_y;
461  sprite_delta[1][i] *= 1 << shift_y;
462  ctx->sprite_shift[i] = 16;
463 
464  }
465  for (i = 0; i < 2; i++) {
466  int64_t sd[2] = {
467  sprite_delta[i][0] - a * (1LL<<16),
468  sprite_delta[i][1] - a * (1LL<<16)
469  };
470 
471  if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
472  llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
473  llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
474  llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
475  llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
476  llabs(sd[0]) >= INT_MAX ||
477  llabs(sd[1]) >= INT_MAX ||
478  llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
479  llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
480  llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
481  ) {
482  avpriv_request_sample(s->avctx, "Overflow on sprite points");
483  goto overflow;
484  }
485  }
486  s->real_sprite_warping_points = ctx->num_sprite_warping_points;
487  }
488 
489  for (i = 0; i < 4; i++) {
490  s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
491  s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
492  }
493 
494  return 0;
495 overflow:
496  memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
497  memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
498  return AVERROR_PATCHWELCOME;
499 }
500 
502  MpegEncContext *s = &ctx->m;
503  int len = FFMIN(ctx->time_increment_bits + 3, 15);
504 
505  get_bits(gb, len);
506  if (get_bits1(gb))
507  get_bits(gb, len);
508  check_marker(s->avctx, gb, "after new_pred");
509 
510  return 0;
511 }
512 
513 /**
514  * Decode the next video packet.
515  * @return <0 if something went wrong
516  */
518 {
519  MpegEncContext *s = &ctx->m;
520 
521  int mb_num_bits = av_log2(s->mb_num - 1) + 1;
522  int header_extension = 0, mb_num, len;
523 
524  /* is there enough space left for a video packet + header */
525  if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
526  return AVERROR_INVALIDDATA;
527 
528  for (len = 0; len < 32; len++)
529  if (get_bits1(&s->gb))
530  break;
531 
533  av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
534  return AVERROR_INVALIDDATA;
535  }
536 
537  if (ctx->shape != RECT_SHAPE) {
538  header_extension = get_bits1(&s->gb);
539  // FIXME more stuff here
540  }
541 
542  mb_num = get_bits(&s->gb, mb_num_bits);
543  if (mb_num >= s->mb_num || !mb_num) {
544  av_log(s->avctx, AV_LOG_ERROR,
545  "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
546  return AVERROR_INVALIDDATA;
547  }
548 
549  s->mb_x = mb_num % s->mb_width;
550  s->mb_y = mb_num / s->mb_width;
551 
552  if (ctx->shape != BIN_ONLY_SHAPE) {
553  int qscale = get_bits(&s->gb, s->quant_precision);
554  if (qscale)
555  s->chroma_qscale = s->qscale = qscale;
556  }
557 
558  if (ctx->shape == RECT_SHAPE)
559  header_extension = get_bits1(&s->gb);
560 
561  if (header_extension) {
562  int time_incr = 0;
563 
564  while (get_bits1(&s->gb) != 0)
565  time_incr++;
566 
567  check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
568  skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
569  check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
570 
571  skip_bits(&s->gb, 2); /* vop coding type */
572  // FIXME not rect stuff here
573 
574  if (ctx->shape != BIN_ONLY_SHAPE) {
575  skip_bits(&s->gb, 3); /* intra dc vlc threshold */
576  // FIXME don't just ignore everything
577  if (s->pict_type == AV_PICTURE_TYPE_S &&
578  ctx->vol_sprite_usage == GMC_SPRITE) {
579  if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
580  return AVERROR_INVALIDDATA;
581  av_log(s->avctx, AV_LOG_ERROR, "untested\n");
582  }
583 
584  // FIXME reduced res stuff here
585 
586  if (s->pict_type != AV_PICTURE_TYPE_I) {
587  int f_code = get_bits(&s->gb, 3); /* fcode_for */
588  if (f_code == 0)
589  av_log(s->avctx, AV_LOG_ERROR,
590  "Error, video packet header damaged (f_code=0)\n");
591  }
592  if (s->pict_type == AV_PICTURE_TYPE_B) {
593  int b_code = get_bits(&s->gb, 3);
594  if (b_code == 0)
595  av_log(s->avctx, AV_LOG_ERROR,
596  "Error, video packet header damaged (b_code=0)\n");
597  }
598  }
599  }
600  if (ctx->new_pred)
601  decode_new_pred(ctx, &s->gb);
602 
603  return 0;
604 }
605 
607 {
608  /* Reset DC Predictors */
609  s->last_dc[0] =
610  s->last_dc[1] =
611  s->last_dc[2] = 1 << (s->avctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1);
612 }
613 
614 /**
615  * Decode the next video packet.
616  * @return <0 if something went wrong
617  */
619 {
620  MpegEncContext *s = &ctx->m;
621  GetBitContext *gb = &s->gb;
622  unsigned vlc_len;
623  uint16_t mb_num;
624 
625  if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_STARTCODE) {
626  vlc_len = av_log2(s->mb_width * s->mb_height) + 1;
627  mb_num = get_bits(gb, vlc_len);
628 
629  if (mb_num >= s->mb_num)
630  return AVERROR_INVALIDDATA;
631 
632  s->mb_x = mb_num % s->mb_width;
633  s->mb_y = mb_num / s->mb_width;
634 
635  if (ctx->shape != BIN_ONLY_SHAPE)
636  s->qscale = mpeg_get_qscale(s);
637 
638  if (get_bits1(gb)) { /* slice_extension_flag */
639  skip_bits1(gb); /* intra_slice */
640  skip_bits1(gb); /* slice_VOP_id_enable */
641  skip_bits(gb, 6); /* slice_VOP_id */
642  while (get_bits1(gb)) /* extra_bit_slice */
643  skip_bits(gb, 8); /* extra_information_slice */
644  }
645 
647  }
648  else {
649  return AVERROR_INVALIDDATA;
650  }
651 
652  return 0;
653 }
654 
655 /**
656  * Get the average motion vector for a GMC MB.
657  * @param n either 0 for the x component or 1 for y
658  * @return the average MV for a GMC MB
659  */
660 static inline int get_amv(Mpeg4DecContext *ctx, int n)
661 {
662  MpegEncContext *s = &ctx->m;
663  int x, y, mb_v, sum, dx, dy, shift;
664  int len = 1 << (s->f_code + 4);
665  const int a = s->sprite_warping_accuracy;
666 
667  if (s->workaround_bugs & FF_BUG_AMV)
668  len >>= s->quarter_sample;
669 
670  if (s->real_sprite_warping_points == 1) {
671  if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample)
672  sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
673  else
674  sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
675  } else {
676  dx = s->sprite_delta[n][0];
677  dy = s->sprite_delta[n][1];
678  shift = ctx->sprite_shift[0];
679  if (n)
680  dy -= 1 << (shift + a + 1);
681  else
682  dx -= 1 << (shift + a + 1);
683  mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U;
684 
685  sum = 0;
686  for (y = 0; y < 16; y++) {
687  int v;
688 
689  v = mb_v + dy * y;
690  // FIXME optimize
691  for (x = 0; x < 16; x++) {
692  sum += v >> shift;
693  v += dx;
694  }
695  }
696  sum = RSHIFT(sum, a + 8 - s->quarter_sample);
697  }
698 
699  if (sum < -len)
700  sum = -len;
701  else if (sum >= len)
702  sum = len - 1;
703 
704  return sum;
705 }
706 
707 /**
708  * Decode the dc value.
709  * @param n block index (0-3 are luma, 4-5 are chroma)
710  * @param dir_ptr the prediction direction will be stored here
711  * @return the quantized dc
712  */
713 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
714 {
715  int level, code;
716 
717  if (n < 4)
718  code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
719  else
720  code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
721 
722  if (code < 0 || code > 9 /* && s->nbit < 9 */) {
723  av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
724  return AVERROR_INVALIDDATA;
725  }
726 
727  if (code == 0) {
728  level = 0;
729  } else {
730  if (IS_3IV1) {
731  if (code == 1)
732  level = 2 * get_bits1(&s->gb) - 1;
733  else {
734  if (get_bits1(&s->gb))
735  level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
736  else
737  level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
738  }
739  } else {
740  level = get_xbits(&s->gb, code);
741  }
742 
743  if (code > 8) {
744  if (get_bits1(&s->gb) == 0) { /* marker */
745  if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
746  av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
747  return AVERROR_INVALIDDATA;
748  }
749  }
750  }
751  }
752 
753  return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
754 }
755 
756 /**
757  * Decode first partition.
758  * @return number of MBs decoded or <0 if an error occurred
759  */
761 {
762  MpegEncContext *s = &ctx->m;
763  int mb_num = 0;
764  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
765 
766  /* decode first partition */
767  s->first_slice_line = 1;
768  for (; s->mb_y < s->mb_height; s->mb_y++) {
770  for (; s->mb_x < s->mb_width; s->mb_x++) {
771  const int xy = s->mb_x + s->mb_y * s->mb_stride;
772  int cbpc;
773  int dir = 0;
774 
775  mb_num++;
776  ff_update_block_index(s, s->avctx->bits_per_raw_sample,
777  s->avctx->lowres, s->chroma_x_shift);
778  if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
779  s->first_slice_line = 0;
780 
781  if (s->pict_type == AV_PICTURE_TYPE_I) {
782  int i;
783 
784  do {
785  if (show_bits(&s->gb, 19) == DC_MARKER)
786  return mb_num - 1;
787 
789  if (cbpc < 0) {
790  av_log(s->avctx, AV_LOG_ERROR,
791  "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
792  return AVERROR_INVALIDDATA;
793  }
794  } while (cbpc == 8);
795 
796  s->cbp_table[xy] = cbpc & 3;
797  s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
798  s->mb_intra = 1;
799 
800  if (cbpc & 4)
801  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
802 
803  s->current_picture.qscale_table[xy] = s->qscale;
804 
805  s->mbintra_table[xy] = 1;
806  for (i = 0; i < 6; i++) {
807  int dc_pred_dir;
808  int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
809  if (dc < 0) {
810  av_log(s->avctx, AV_LOG_ERROR,
811  "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
812  return dc;
813  }
814  dir <<= 1;
815  if (dc_pred_dir)
816  dir |= 1;
817  }
818  s->pred_dir_table[xy] = dir;
819  } else { /* P/S_TYPE */
820  int mx, my, pred_x, pred_y, bits;
821  int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
822  const int stride = s->b8_stride * 2;
823 
824 try_again:
825  bits = show_bits(&s->gb, 17);
826  if (bits == MOTION_MARKER)
827  return mb_num - 1;
828 
829  skip_bits1(&s->gb);
830  if (bits & 0x10000) {
831  /* skip mb */
832  if (s->pict_type == AV_PICTURE_TYPE_S &&
833  ctx->vol_sprite_usage == GMC_SPRITE) {
834  s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
835  MB_TYPE_16x16 |
836  MB_TYPE_GMC |
837  MB_TYPE_L0;
838  mx = get_amv(ctx, 0);
839  my = get_amv(ctx, 1);
840  } else {
841  s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
842  MB_TYPE_16x16 |
843  MB_TYPE_L0;
844  mx = my = 0;
845  }
846  mot_val[0] =
847  mot_val[2] =
848  mot_val[0 + stride] =
849  mot_val[2 + stride] = mx;
850  mot_val[1] =
851  mot_val[3] =
852  mot_val[1 + stride] =
853  mot_val[3 + stride] = my;
854 
855  if (s->mbintra_table[xy])
857  continue;
858  }
859 
861  if (cbpc < 0) {
862  av_log(s->avctx, AV_LOG_ERROR,
863  "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
864  return AVERROR_INVALIDDATA;
865  }
866  if (cbpc == 20)
867  goto try_again;
868 
869  s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
870 
871  s->mb_intra = ((cbpc & 4) != 0);
872 
873  if (s->mb_intra) {
874  s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
875  s->mbintra_table[xy] = 1;
876  mot_val[0] =
877  mot_val[2] =
878  mot_val[0 + stride] =
879  mot_val[2 + stride] = 0;
880  mot_val[1] =
881  mot_val[3] =
882  mot_val[1 + stride] =
883  mot_val[3 + stride] = 0;
884  } else {
885  if (s->mbintra_table[xy])
887 
888  if (s->pict_type == AV_PICTURE_TYPE_S &&
889  ctx->vol_sprite_usage == GMC_SPRITE &&
890  (cbpc & 16) == 0)
891  s->mcsel = get_bits1(&s->gb);
892  else
893  s->mcsel = 0;
894 
895  if ((cbpc & 16) == 0) {
896  /* 16x16 motion prediction */
897 
898  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
899  if (!s->mcsel) {
900  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
901  if (mx >= 0xffff)
902  return AVERROR_INVALIDDATA;
903 
904  my = ff_h263_decode_motion(s, pred_y, s->f_code);
905  if (my >= 0xffff)
906  return AVERROR_INVALIDDATA;
907  s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
908  MB_TYPE_L0;
909  } else {
910  mx = get_amv(ctx, 0);
911  my = get_amv(ctx, 1);
912  s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
913  MB_TYPE_GMC |
914  MB_TYPE_L0;
915  }
916 
917  mot_val[0] =
918  mot_val[2] =
919  mot_val[0 + stride] =
920  mot_val[2 + stride] = mx;
921  mot_val[1] =
922  mot_val[3] =
923  mot_val[1 + stride] =
924  mot_val[3 + stride] = my;
925  } else {
926  int i;
927  s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
928  MB_TYPE_L0;
929  for (i = 0; i < 4; i++) {
930  int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
931  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
932  if (mx >= 0xffff)
933  return AVERROR_INVALIDDATA;
934 
935  my = ff_h263_decode_motion(s, pred_y, s->f_code);
936  if (my >= 0xffff)
937  return AVERROR_INVALIDDATA;
938  mot_val[0] = mx;
939  mot_val[1] = my;
940  }
941  }
942  }
943  }
944  }
945  s->mb_x = 0;
946  }
947 
948  return mb_num;
949 }
950 
951 /**
952  * decode second partition.
953  * @return <0 if an error occurred
954  */
955 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
956 {
957  int mb_num = 0;
958  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
959 
960  s->mb_x = s->resync_mb_x;
961  s->first_slice_line = 1;
962  for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
964  for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
965  const int xy = s->mb_x + s->mb_y * s->mb_stride;
966 
967  mb_num++;
968  ff_update_block_index(s, s->avctx->bits_per_raw_sample,
969  s->avctx->lowres, s->chroma_x_shift);
970  if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
971  s->first_slice_line = 0;
972 
973  if (s->pict_type == AV_PICTURE_TYPE_I) {
974  int ac_pred = get_bits1(&s->gb);
975  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
976  if (cbpy < 0) {
977  av_log(s->avctx, AV_LOG_ERROR,
978  "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
979  return AVERROR_INVALIDDATA;
980  }
981 
982  s->cbp_table[xy] |= cbpy << 2;
983  s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
984  } else { /* P || S_TYPE */
985  if (IS_INTRA(s->current_picture.mb_type[xy])) {
986  int i;
987  int dir = 0;
988  int ac_pred = get_bits1(&s->gb);
989  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
990 
991  if (cbpy < 0) {
992  av_log(s->avctx, AV_LOG_ERROR,
993  "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
994  return AVERROR_INVALIDDATA;
995  }
996 
997  if (s->cbp_table[xy] & 8)
998  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
999  s->current_picture.qscale_table[xy] = s->qscale;
1000 
1001  for (i = 0; i < 6; i++) {
1002  int dc_pred_dir;
1003  int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
1004  if (dc < 0) {
1005  av_log(s->avctx, AV_LOG_ERROR,
1006  "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
1007  return dc;
1008  }
1009  dir <<= 1;
1010  if (dc_pred_dir)
1011  dir |= 1;
1012  }
1013  s->cbp_table[xy] &= 3; // remove dquant
1014  s->cbp_table[xy] |= cbpy << 2;
1015  s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
1016  s->pred_dir_table[xy] = dir;
1017  } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
1018  s->current_picture.qscale_table[xy] = s->qscale;
1019  s->cbp_table[xy] = 0;
1020  } else {
1021  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1022 
1023  if (cbpy < 0) {
1024  av_log(s->avctx, AV_LOG_ERROR,
1025  "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
1026  return AVERROR_INVALIDDATA;
1027  }
1028 
1029  if (s->cbp_table[xy] & 8)
1030  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1031  s->current_picture.qscale_table[xy] = s->qscale;
1032 
1033  s->cbp_table[xy] &= 3; // remove dquant
1034  s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
1035  }
1036  }
1037  }
1038  if (mb_num >= mb_count)
1039  return 0;
1040  s->mb_x = 0;
1041  }
1042  return 0;
1043 }
1044 
1045 /**
1046  * Decode the first and second partition.
1047  * @return <0 if error (and sets error type in the error_status_table)
1048  */
1050 {
1051  MpegEncContext *s = &ctx->m;
1052  int mb_num;
1053  int ret;
1054  const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
1055  const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
1056 
1057  mb_num = mpeg4_decode_partition_a(ctx);
1058  if (mb_num <= 0) {
1059  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1060  s->mb_x, s->mb_y, part_a_error);
1061  return mb_num ? mb_num : AVERROR_INVALIDDATA;
1062  }
1063 
1064  if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
1065  av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
1066  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1067  s->mb_x, s->mb_y, part_a_error);
1068  return AVERROR_INVALIDDATA;
1069  }
1070 
1071  s->mb_num_left = mb_num;
1072 
1073  if (s->pict_type == AV_PICTURE_TYPE_I) {
1074  while (show_bits(&s->gb, 9) == 1)
1075  skip_bits(&s->gb, 9);
1076  if (get_bits(&s->gb, 19) != DC_MARKER) {
1077  av_log(s->avctx, AV_LOG_ERROR,
1078  "marker missing after first I partition at %d %d\n",
1079  s->mb_x, s->mb_y);
1080  return AVERROR_INVALIDDATA;
1081  }
1082  } else {
1083  while (show_bits(&s->gb, 10) == 1)
1084  skip_bits(&s->gb, 10);
1085  if (get_bits(&s->gb, 17) != MOTION_MARKER) {
1086  av_log(s->avctx, AV_LOG_ERROR,
1087  "marker missing after first P partition at %d %d\n",
1088  s->mb_x, s->mb_y);
1089  return AVERROR_INVALIDDATA;
1090  }
1091  }
1092  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1093  s->mb_x - 1, s->mb_y, part_a_end);
1094 
1095  ret = mpeg4_decode_partition_b(s, mb_num);
1096  if (ret < 0) {
1097  if (s->pict_type == AV_PICTURE_TYPE_P)
1098  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1099  s->mb_x, s->mb_y, ER_DC_ERROR);
1100  return ret;
1101  } else {
1102  if (s->pict_type == AV_PICTURE_TYPE_P)
1103  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1104  s->mb_x - 1, s->mb_y, ER_DC_END);
1105  }
1106 
1107  return 0;
1108 }
1109 
1110 /**
1111  * Decode a block.
1112  * @return <0 if an error occurred
1113  */
1114 static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
1115  int n, int coded, int intra,
1116  int use_intra_dc_vlc, int rvlc)
1117 {
1118  MpegEncContext *s = &ctx->m;
1119  int level, i, last, run, qmul, qadd;
1120  int av_uninit(dc_pred_dir);
1121  RLTable *rl;
1123  const uint8_t *scan_table;
1124 
1125  // Note intra & rvlc should be optimized away if this is inlined
1126 
1127  if (intra) {
1128  if (use_intra_dc_vlc) {
1129  /* DC coef */
1130  if (s->partitioned_frame) {
1131  level = s->dc_val[0][s->block_index[n]];
1132  if (n < 4)
1133  level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
1134  else
1135  level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
1136  dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
1137  } else {
1138  level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1139  if (level < 0)
1140  return level;
1141  }
1142  block[0] = level;
1143  i = 0;
1144  } else {
1145  i = -1;
1146  ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
1147  }
1148  if (!coded)
1149  goto not_coded;
1150 
1151  if (rvlc) {
1152  rl = &ff_rvlc_rl_intra;
1154  } else {
1155  rl = &ff_mpeg4_rl_intra;
1157  }
1158  if (s->ac_pred) {
1159  if (dc_pred_dir == 0)
1160  scan_table = s->intra_v_scantable.permutated; /* left */
1161  else
1162  scan_table = s->intra_h_scantable.permutated; /* top */
1163  } else {
1164  scan_table = s->intra_scantable.permutated;
1165  }
1166  qmul = 1;
1167  qadd = 0;
1168  } else {
1169  i = -1;
1170  if (!coded) {
1171  s->block_last_index[n] = i;
1172  return 0;
1173  }
1174  if (rvlc)
1175  rl = &ff_rvlc_rl_inter;
1176  else
1177  rl = &ff_h263_rl_inter;
1178 
1179  scan_table = s->intra_scantable.permutated;
1180 
1181  if (s->mpeg_quant) {
1182  qmul = 1;
1183  qadd = 0;
1184  if (rvlc)
1186  else
1188  } else {
1189  qmul = s->qscale << 1;
1190  qadd = (s->qscale - 1) | 1;
1191  if (rvlc)
1192  rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1193  else
1194  rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1195  }
1196  }
1197  {
1198  OPEN_READER(re, &s->gb);
1199  for (;;) {
1200  UPDATE_CACHE(re, &s->gb);
1201  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1202  if (level == 0) {
1203  /* escape */
1204  if (rvlc) {
1205  if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1206  av_log(s->avctx, AV_LOG_ERROR,
1207  "1. marker bit missing in rvlc esc\n");
1208  return AVERROR_INVALIDDATA;
1209  }
1210  SKIP_CACHE(re, &s->gb, 1);
1211 
1212  last = SHOW_UBITS(re, &s->gb, 1);
1213  SKIP_CACHE(re, &s->gb, 1);
1214  run = SHOW_UBITS(re, &s->gb, 6);
1215  SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1216  UPDATE_CACHE(re, &s->gb);
1217 
1218  if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1219  av_log(s->avctx, AV_LOG_ERROR,
1220  "2. marker bit missing in rvlc esc\n");
1221  return AVERROR_INVALIDDATA;
1222  }
1223  SKIP_CACHE(re, &s->gb, 1);
1224 
1225  level = SHOW_UBITS(re, &s->gb, 11);
1226  SKIP_CACHE(re, &s->gb, 11);
1227 
1228  if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1229  av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1230  return AVERROR_INVALIDDATA;
1231  }
1232  SKIP_CACHE(re, &s->gb, 5);
1233 
1234  level = level * qmul + qadd;
1235  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1236  SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1237 
1238  i += run + 1;
1239  if (last)
1240  i += 192;
1241  } else {
1242  int cache;
1243  cache = GET_CACHE(re, &s->gb);
1244 
1245  if (IS_3IV1)
1246  cache ^= 0xC0000000;
1247 
1248  if (cache & 0x80000000) {
1249  if (cache & 0x40000000) {
1250  /* third escape */
1251  SKIP_CACHE(re, &s->gb, 2);
1252  last = SHOW_UBITS(re, &s->gb, 1);
1253  SKIP_CACHE(re, &s->gb, 1);
1254  run = SHOW_UBITS(re, &s->gb, 6);
1255  SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1256  UPDATE_CACHE(re, &s->gb);
1257 
1258  if (IS_3IV1) {
1259  level = SHOW_SBITS(re, &s->gb, 12);
1260  LAST_SKIP_BITS(re, &s->gb, 12);
1261  } else {
1262  if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1263  av_log(s->avctx, AV_LOG_ERROR,
1264  "1. marker bit missing in 3. esc\n");
1265  if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1266  return AVERROR_INVALIDDATA;
1267  }
1268  SKIP_CACHE(re, &s->gb, 1);
1269 
1270  level = SHOW_SBITS(re, &s->gb, 12);
1271  SKIP_CACHE(re, &s->gb, 12);
1272 
1273  if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1274  av_log(s->avctx, AV_LOG_ERROR,
1275  "2. marker bit missing in 3. esc\n");
1276  if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1277  return AVERROR_INVALIDDATA;
1278  }
1279 
1280  SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1281  }
1282 
1283 #if 0
1284  if (s->error_recognition >= FF_ER_COMPLIANT) {
1285  const int abs_level= FFABS(level);
1286  if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1287  const int run1= run - rl->max_run[last][abs_level] - 1;
1288  if (abs_level <= rl->max_level[last][run]) {
1289  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1290  return AVERROR_INVALIDDATA;
1291  }
1292  if (s->error_recognition > FF_ER_COMPLIANT) {
1293  if (abs_level <= rl->max_level[last][run]*2) {
1294  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1295  return AVERROR_INVALIDDATA;
1296  }
1297  if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1298  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1299  return AVERROR_INVALIDDATA;
1300  }
1301  }
1302  }
1303  }
1304 #endif
1305  if (level > 0)
1306  level = level * qmul + qadd;
1307  else
1308  level = level * qmul - qadd;
1309 
1310  if ((unsigned)(level + 2048) > 4095) {
1311  if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1312  if (level > 2560 || level < -2560) {
1313  av_log(s->avctx, AV_LOG_ERROR,
1314  "|level| overflow in 3. esc, qp=%d\n",
1315  s->qscale);
1316  return AVERROR_INVALIDDATA;
1317  }
1318  }
1319  level = level < 0 ? -2048 : 2047;
1320  }
1321 
1322  i += run + 1;
1323  if (last)
1324  i += 192;
1325  } else {
1326  /* second escape */
1327  SKIP_BITS(re, &s->gb, 2);
1328  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1329  i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1330  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1331  LAST_SKIP_BITS(re, &s->gb, 1);
1332  }
1333  } else {
1334  /* first escape */
1335  SKIP_BITS(re, &s->gb, 1);
1336  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1337  i += run;
1338  level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1339  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1340  LAST_SKIP_BITS(re, &s->gb, 1);
1341  }
1342  }
1343  } else {
1344  i += run;
1345  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1346  LAST_SKIP_BITS(re, &s->gb, 1);
1347  }
1348  ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1349  if (i > 62) {
1350  i -= 192;
1351  if (i & (~63)) {
1352  av_log(s->avctx, AV_LOG_ERROR,
1353  "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1354  return AVERROR_INVALIDDATA;
1355  }
1356 
1357  block[scan_table[i]] = level;
1358  break;
1359  }
1360 
1361  block[scan_table[i]] = level;
1362  }
1363  CLOSE_READER(re, &s->gb);
1364  }
1365 
1366 not_coded:
1367  if (intra) {
1368  if (!use_intra_dc_vlc) {
1369  block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1370 
1371  i -= i >> 31; // if (i == -1) i = 0;
1372  }
1373 
1374  ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1375  if (s->ac_pred)
1376  i = 63; // FIXME not optimal
1377  }
1378  s->block_last_index[n] = i;
1379  return 0;
1380 }
1381 
1382 /**
1383  * decode partition C of one MB.
1384  * @return <0 if an error occurred
1385  */
1387 {
1388  Mpeg4DecContext *ctx = s->avctx->priv_data;
1389  int cbp, mb_type, use_intra_dc_vlc;
1390  const int xy = s->mb_x + s->mb_y * s->mb_stride;
1391 
1392  av_assert2(s == (void*)ctx);
1393 
1394  mb_type = s->current_picture.mb_type[xy];
1395  cbp = s->cbp_table[xy];
1396 
1397  use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1398 
1399  if (s->current_picture.qscale_table[xy] != s->qscale)
1400  ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1401 
1402  if (s->pict_type == AV_PICTURE_TYPE_P ||
1403  s->pict_type == AV_PICTURE_TYPE_S) {
1404  int i;
1405  for (i = 0; i < 4; i++) {
1406  s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1407  s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1408  }
1409  s->mb_intra = IS_INTRA(mb_type);
1410 
1411  if (IS_SKIP(mb_type)) {
1412  /* skip mb */
1413  for (i = 0; i < 6; i++)
1414  s->block_last_index[i] = -1;
1415  s->mv_dir = MV_DIR_FORWARD;
1416  s->mv_type = MV_TYPE_16X16;
1417  if (s->pict_type == AV_PICTURE_TYPE_S
1418  && ctx->vol_sprite_usage == GMC_SPRITE) {
1419  s->mcsel = 1;
1420  s->mb_skipped = 0;
1421  } else {
1422  s->mcsel = 0;
1423  s->mb_skipped = 1;
1424  }
1425  } else if (s->mb_intra) {
1426  s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1427  } else if (!s->mb_intra) {
1428  // s->mcsel = 0; // FIXME do we need to init that?
1429 
1430  s->mv_dir = MV_DIR_FORWARD;
1431  if (IS_8X8(mb_type)) {
1432  s->mv_type = MV_TYPE_8X8;
1433  } else {
1434  s->mv_type = MV_TYPE_16X16;
1435  }
1436  }
1437  } else { /* I-Frame */
1438  s->mb_intra = 1;
1439  s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1440  }
1441 
1442  if (!IS_SKIP(mb_type)) {
1443  int i;
1444  s->bdsp.clear_blocks(s->block[0]);
1445  /* decode each block */
1446  for (i = 0; i < 6; i++) {
1447  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra,
1448  use_intra_dc_vlc, ctx->rvlc) < 0) {
1449  av_log(s->avctx, AV_LOG_ERROR,
1450  "texture corrupted at %d %d %d\n",
1451  s->mb_x, s->mb_y, s->mb_intra);
1452  return AVERROR_INVALIDDATA;
1453  }
1454  cbp += cbp;
1455  }
1456  }
1457 
1458  /* per-MB end of slice check */
1459  if (--s->mb_num_left <= 0) {
1460  if (mpeg4_is_resync(ctx))
1461  return SLICE_END;
1462  else
1463  return SLICE_NOEND;
1464  } else {
1465  if (mpeg4_is_resync(ctx)) {
1466  const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1467  if (s->cbp_table[xy + delta])
1468  return SLICE_END;
1469  }
1470  return SLICE_OK;
1471  }
1472 }
1473 
1474 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1475 {
1476  Mpeg4DecContext *ctx = s->avctx->priv_data;
1477  int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1478  int16_t *mot_val;
1479  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1480  const int xy = s->mb_x + s->mb_y * s->mb_stride;
1481 
1482  av_assert2(s == (void*)ctx);
1483  av_assert2(s->h263_pred);
1484 
1485  if (s->pict_type == AV_PICTURE_TYPE_P ||
1486  s->pict_type == AV_PICTURE_TYPE_S) {
1487  do {
1488  if (get_bits1(&s->gb)) {
1489  /* skip mb */
1490  s->mb_intra = 0;
1491  for (i = 0; i < 6; i++)
1492  s->block_last_index[i] = -1;
1493  s->mv_dir = MV_DIR_FORWARD;
1494  s->mv_type = MV_TYPE_16X16;
1495  if (s->pict_type == AV_PICTURE_TYPE_S &&
1496  ctx->vol_sprite_usage == GMC_SPRITE) {
1497  s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1498  MB_TYPE_GMC |
1499  MB_TYPE_16x16 |
1500  MB_TYPE_L0;
1501  s->mcsel = 1;
1502  s->mv[0][0][0] = get_amv(ctx, 0);
1503  s->mv[0][0][1] = get_amv(ctx, 1);
1504  s->mb_skipped = 0;
1505  } else {
1506  s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1507  MB_TYPE_16x16 |
1508  MB_TYPE_L0;
1509  s->mcsel = 0;
1510  s->mv[0][0][0] = 0;
1511  s->mv[0][0][1] = 0;
1512  s->mb_skipped = 1;
1513  }
1514  goto end;
1515  }
1517  if (cbpc < 0) {
1518  av_log(s->avctx, AV_LOG_ERROR,
1519  "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1520  return AVERROR_INVALIDDATA;
1521  }
1522  } while (cbpc == 20);
1523 
1524  s->bdsp.clear_blocks(s->block[0]);
1525  dquant = cbpc & 8;
1526  s->mb_intra = ((cbpc & 4) != 0);
1527  if (s->mb_intra)
1528  goto intra;
1529 
1530  if (s->pict_type == AV_PICTURE_TYPE_S &&
1531  ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1532  s->mcsel = get_bits1(&s->gb);
1533  else
1534  s->mcsel = 0;
1535  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1536  if (cbpy < 0) {
1537  av_log(s->avctx, AV_LOG_ERROR,
1538  "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1539  return AVERROR_INVALIDDATA;
1540  }
1541 
1542  cbp = (cbpc & 3) | (cbpy << 2);
1543  if (dquant)
1544  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1545  if ((!s->progressive_sequence) &&
1546  (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1547  s->interlaced_dct = get_bits1(&s->gb);
1548 
1549  s->mv_dir = MV_DIR_FORWARD;
1550  if ((cbpc & 16) == 0) {
1551  if (s->mcsel) {
1552  s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1553  MB_TYPE_16x16 |
1554  MB_TYPE_L0;
1555  /* 16x16 global motion prediction */
1556  s->mv_type = MV_TYPE_16X16;
1557  mx = get_amv(ctx, 0);
1558  my = get_amv(ctx, 1);
1559  s->mv[0][0][0] = mx;
1560  s->mv[0][0][1] = my;
1561  } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1562  s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1563  MB_TYPE_L0 |
1565  /* 16x8 field motion prediction */
1566  s->mv_type = MV_TYPE_FIELD;
1567 
1568  s->field_select[0][0] = get_bits1(&s->gb);
1569  s->field_select[0][1] = get_bits1(&s->gb);
1570 
1571  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1572 
1573  for (i = 0; i < 2; i++) {
1574  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1575  if (mx >= 0xffff)
1576  return AVERROR_INVALIDDATA;
1577 
1578  my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1579  if (my >= 0xffff)
1580  return AVERROR_INVALIDDATA;
1581 
1582  s->mv[0][i][0] = mx;
1583  s->mv[0][i][1] = my;
1584  }
1585  } else {
1586  s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1587  /* 16x16 motion prediction */
1588  s->mv_type = MV_TYPE_16X16;
1589  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1590  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1591 
1592  if (mx >= 0xffff)
1593  return AVERROR_INVALIDDATA;
1594 
1595  my = ff_h263_decode_motion(s, pred_y, s->f_code);
1596 
1597  if (my >= 0xffff)
1598  return AVERROR_INVALIDDATA;
1599  s->mv[0][0][0] = mx;
1600  s->mv[0][0][1] = my;
1601  }
1602  } else {
1603  s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1604  s->mv_type = MV_TYPE_8X8;
1605  for (i = 0; i < 4; i++) {
1606  mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1607  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1608  if (mx >= 0xffff)
1609  return AVERROR_INVALIDDATA;
1610 
1611  my = ff_h263_decode_motion(s, pred_y, s->f_code);
1612  if (my >= 0xffff)
1613  return AVERROR_INVALIDDATA;
1614  s->mv[0][i][0] = mx;
1615  s->mv[0][i][1] = my;
1616  mot_val[0] = mx;
1617  mot_val[1] = my;
1618  }
1619  }
1620  } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1621  int modb1; // first bit of modb
1622  int modb2; // second bit of modb
1623  int mb_type;
1624 
1625  s->mb_intra = 0; // B-frames never contain intra blocks
1626  s->mcsel = 0; // ... true gmc blocks
1627 
1628  if (s->mb_x == 0) {
1629  for (i = 0; i < 2; i++) {
1630  s->last_mv[i][0][0] =
1631  s->last_mv[i][0][1] =
1632  s->last_mv[i][1][0] =
1633  s->last_mv[i][1][1] = 0;
1634  }
1635 
1636  ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1637  }
1638 
1639  /* if we skipped it in the future P-frame than skip it now too */
1640  s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1641 
1642  if (s->mb_skipped) {
1643  /* skip mb */
1644  for (i = 0; i < 6; i++)
1645  s->block_last_index[i] = -1;
1646 
1647  s->mv_dir = MV_DIR_FORWARD;
1648  s->mv_type = MV_TYPE_16X16;
1649  s->mv[0][0][0] =
1650  s->mv[0][0][1] =
1651  s->mv[1][0][0] =
1652  s->mv[1][0][1] = 0;
1653  s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1654  MB_TYPE_16x16 |
1655  MB_TYPE_L0;
1656  goto end;
1657  }
1658 
1659  modb1 = get_bits1(&s->gb);
1660  if (modb1) {
1661  // like MB_TYPE_B_DIRECT but no vectors coded
1663  cbp = 0;
1664  } else {
1665  modb2 = get_bits1(&s->gb);
1666  mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1667  if (mb_type < 0) {
1668  av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1669  return AVERROR_INVALIDDATA;
1670  }
1671  mb_type = mb_type_b_map[mb_type];
1672  if (modb2) {
1673  cbp = 0;
1674  } else {
1675  s->bdsp.clear_blocks(s->block[0]);
1676  cbp = get_bits(&s->gb, 6);
1677  }
1678 
1679  if ((!IS_DIRECT(mb_type)) && cbp) {
1680  if (get_bits1(&s->gb))
1681  ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1682  }
1683 
1684  if (!s->progressive_sequence) {
1685  if (cbp)
1686  s->interlaced_dct = get_bits1(&s->gb);
1687 
1688  if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1689  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1690  mb_type &= ~MB_TYPE_16x16;
1691 
1692  if (USES_LIST(mb_type, 0)) {
1693  s->field_select[0][0] = get_bits1(&s->gb);
1694  s->field_select[0][1] = get_bits1(&s->gb);
1695  }
1696  if (USES_LIST(mb_type, 1)) {
1697  s->field_select[1][0] = get_bits1(&s->gb);
1698  s->field_select[1][1] = get_bits1(&s->gb);
1699  }
1700  }
1701  }
1702 
1703  s->mv_dir = 0;
1704  if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1705  s->mv_type = MV_TYPE_16X16;
1706 
1707  if (USES_LIST(mb_type, 0)) {
1708  s->mv_dir = MV_DIR_FORWARD;
1709 
1710  mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1711  my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1712  s->last_mv[0][1][0] =
1713  s->last_mv[0][0][0] =
1714  s->mv[0][0][0] = mx;
1715  s->last_mv[0][1][1] =
1716  s->last_mv[0][0][1] =
1717  s->mv[0][0][1] = my;
1718  }
1719 
1720  if (USES_LIST(mb_type, 1)) {
1721  s->mv_dir |= MV_DIR_BACKWARD;
1722 
1723  mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1724  my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1725  s->last_mv[1][1][0] =
1726  s->last_mv[1][0][0] =
1727  s->mv[1][0][0] = mx;
1728  s->last_mv[1][1][1] =
1729  s->last_mv[1][0][1] =
1730  s->mv[1][0][1] = my;
1731  }
1732  } else if (!IS_DIRECT(mb_type)) {
1733  s->mv_type = MV_TYPE_FIELD;
1734 
1735  if (USES_LIST(mb_type, 0)) {
1736  s->mv_dir = MV_DIR_FORWARD;
1737 
1738  for (i = 0; i < 2; i++) {
1739  mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1740  my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1741  s->last_mv[0][i][0] =
1742  s->mv[0][i][0] = mx;
1743  s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1744  }
1745  }
1746 
1747  if (USES_LIST(mb_type, 1)) {
1748  s->mv_dir |= MV_DIR_BACKWARD;
1749 
1750  for (i = 0; i < 2; i++) {
1751  mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1752  my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1753  s->last_mv[1][i][0] =
1754  s->mv[1][i][0] = mx;
1755  s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1756  }
1757  }
1758  }
1759  }
1760 
1761  if (IS_DIRECT(mb_type)) {
1762  if (IS_SKIP(mb_type)) {
1763  mx =
1764  my = 0;
1765  } else {
1766  mx = ff_h263_decode_motion(s, 0, 1);
1767  my = ff_h263_decode_motion(s, 0, 1);
1768  }
1769 
1770  s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1771  mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1772  }
1773  s->current_picture.mb_type[xy] = mb_type;
1774  } else { /* I-Frame */
1775  int use_intra_dc_vlc;
1776 
1777  do {
1779  if (cbpc < 0) {
1780  av_log(s->avctx, AV_LOG_ERROR,
1781  "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1782  return AVERROR_INVALIDDATA;
1783  }
1784  } while (cbpc == 8);
1785 
1786  dquant = cbpc & 4;
1787  s->mb_intra = 1;
1788 
1789 intra:
1790  s->ac_pred = get_bits1(&s->gb);
1791  if (s->ac_pred)
1792  s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1793  else
1794  s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1795 
1796  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1797  if (cbpy < 0) {
1798  av_log(s->avctx, AV_LOG_ERROR,
1799  "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1800  return AVERROR_INVALIDDATA;
1801  }
1802  cbp = (cbpc & 3) | (cbpy << 2);
1803 
1804  use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1805 
1806  if (dquant)
1807  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1808 
1809  if (!s->progressive_sequence)
1810  s->interlaced_dct = get_bits1(&s->gb);
1811 
1812  s->bdsp.clear_blocks(s->block[0]);
1813  /* decode each block */
1814  for (i = 0; i < 6; i++) {
1815  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32,
1816  1, use_intra_dc_vlc, 0) < 0)
1817  return AVERROR_INVALIDDATA;
1818  cbp += cbp;
1819  }
1820  goto end;
1821  }
1822 
1823  /* decode each block */
1824  for (i = 0; i < 6; i++) {
1825  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0, 0) < 0)
1826  return AVERROR_INVALIDDATA;
1827  cbp += cbp;
1828  }
1829 
1830 end:
1831  /* per-MB end of slice check */
1832  if (s->codec_id == AV_CODEC_ID_MPEG4) {
1833  int next = mpeg4_is_resync(ctx);
1834  if (next) {
1835  if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1836  return AVERROR_INVALIDDATA;
1837  } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1838  return SLICE_END;
1839 
1840  if (s->pict_type == AV_PICTURE_TYPE_B) {
1841  const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1842  ff_thread_await_progress(&s->next_picture_ptr->tf,
1843  (s->mb_x + delta >= s->mb_width)
1844  ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1845  : s->mb_y, 0);
1846  if (s->next_picture.mbskip_table[xy + delta])
1847  return SLICE_OK;
1848  }
1849 
1850  return SLICE_END;
1851  }
1852  }
1853 
1854  return SLICE_OK;
1855 }
1856 
1857 /* As per spec, studio start code search isn't the same as the old type of start code */
1859 {
1860  align_get_bits(gb);
1861 
1862  while (get_bits_left(gb) >= 24 && show_bits(gb, 24) != 0x1) {
1863  get_bits(gb, 8);
1864  }
1865 }
1866 
1867 /* additional_code, vlc index */
1868 static const uint8_t ac_state_tab[22][2] =
1869 {
1870  {0, 0},
1871  {0, 1},
1872  {1, 1},
1873  {2, 1},
1874  {3, 1},
1875  {4, 1},
1876  {5, 1},
1877  {1, 2},
1878  {2, 2},
1879  {3, 2},
1880  {4, 2},
1881  {5, 2},
1882  {6, 2},
1883  {1, 3},
1884  {2, 4},
1885  {3, 5},
1886  {4, 6},
1887  {5, 7},
1888  {6, 8},
1889  {7, 9},
1890  {8, 10},
1891  {0, 11}
1892 };
1893 
1895 {
1896  Mpeg4DecContext *ctx = s->avctx->priv_data;
1897 
1898  int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
1899  additional_code_len, sign, mismatch;
1900  const VLC *cur_vlc = &studio_intra_tab[0];
1901  uint8_t *const scantable = s->intra_scantable.permutated;
1902  const uint16_t *quant_matrix;
1903  uint32_t flc;
1904  const int min = -1 * (1 << (s->avctx->bits_per_raw_sample + 6));
1905  const int max = ((1 << (s->avctx->bits_per_raw_sample + 6)) - 1);
1906  int shift = 3 - s->dct_precision;
1907 
1908  mismatch = 1;
1909 
1910  memset(block, 0, 64 * sizeof(int32_t));
1911 
1912  if (n < 4) {
1913  cc = 0;
1914  dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1915  quant_matrix = s->intra_matrix;
1916  } else {
1917  cc = (n & 1) + 1;
1918  if (ctx->rgb)
1919  dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1920  else
1921  dct_dc_size = get_vlc2(&s->gb, studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
1922  quant_matrix = s->chroma_intra_matrix;
1923  }
1924 
1925  if (dct_dc_size == 0) {
1926  dct_diff = 0;
1927  } else {
1928  dct_diff = get_xbits(&s->gb, dct_dc_size);
1929 
1930  if (dct_dc_size > 8) {
1931  if(!check_marker(s->avctx, &s->gb, "dct_dc_size > 8"))
1932  return AVERROR_INVALIDDATA;
1933  }
1934 
1935  }
1936 
1937  s->last_dc[cc] += dct_diff;
1938 
1939  if (s->mpeg_quant)
1940  block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision);
1941  else
1942  block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision) * (8 >> s->dct_precision);
1943  /* TODO: support mpeg_quant for AC coefficients */
1944 
1945  block[0] = av_clip(block[0], min, max);
1946  mismatch ^= block[0];
1947 
1948  /* AC Coefficients */
1949  while (1) {
1950  group = get_vlc2(&s->gb, cur_vlc->table, STUDIO_INTRA_BITS, 2);
1951 
1952  if (group < 0) {
1953  av_log(s->avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
1954  return AVERROR_INVALIDDATA;
1955  }
1956 
1957  additional_code_len = ac_state_tab[group][0];
1958  cur_vlc = &studio_intra_tab[ac_state_tab[group][1]];
1959 
1960  if (group == 0) {
1961  /* End of Block */
1962  break;
1963  } else if (group >= 1 && group <= 6) {
1964  /* Zero run length (Table B.47) */
1965  run = 1 << additional_code_len;
1966  if (additional_code_len)
1967  run += get_bits(&s->gb, additional_code_len);
1968  idx += run;
1969  continue;
1970  } else if (group >= 7 && group <= 12) {
1971  /* Zero run length and +/-1 level (Table B.48) */
1972  code = get_bits(&s->gb, additional_code_len);
1973  sign = code & 1;
1974  code >>= 1;
1975  run = (1 << (additional_code_len - 1)) + code;
1976  idx += run;
1977  if (idx > 63)
1978  return AVERROR_INVALIDDATA;
1979  j = scantable[idx++];
1980  block[j] = sign ? 1 : -1;
1981  } else if (group >= 13 && group <= 20) {
1982  /* Level value (Table B.49) */
1983  if (idx > 63)
1984  return AVERROR_INVALIDDATA;
1985  j = scantable[idx++];
1986  block[j] = get_xbits(&s->gb, additional_code_len);
1987  } else if (group == 21) {
1988  /* Escape */
1989  if (idx > 63)
1990  return AVERROR_INVALIDDATA;
1991  j = scantable[idx++];
1992  additional_code_len = s->avctx->bits_per_raw_sample + s->dct_precision + 4;
1993  flc = get_bits(&s->gb, additional_code_len);
1994  if (flc >> (additional_code_len-1))
1995  block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
1996  else
1997  block[j] = flc;
1998  }
1999  block[j] = ((block[j] * quant_matrix[j] * s->qscale) * (1 << shift)) / 16;
2000  block[j] = av_clip(block[j], min, max);
2001  mismatch ^= block[j];
2002  }
2003 
2004  block[63] ^= mismatch & 1;
2005 
2006  return 0;
2007 }
2008 
2009 static int mpeg4_decode_dpcm_macroblock(MpegEncContext *s, int16_t macroblock[256], int n)
2010 {
2011  int i, j, w, h, idx = 0;
2012  int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code,
2013  dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output;
2014  h = 16 >> (n ? s->chroma_y_shift : 0);
2015  w = 16 >> (n ? s->chroma_x_shift : 0);
2016 
2017  block_mean = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
2018  if (block_mean == 0){
2019  av_log(s->avctx, AV_LOG_ERROR, "Forbidden block_mean\n");
2020  return AVERROR_INVALIDDATA;
2021  }
2022  s->last_dc[n] = block_mean * (1 << (s->dct_precision + s->intra_dc_precision));
2023 
2024  rice_parameter = get_bits(&s->gb, 4);
2025  if (rice_parameter == 0) {
2026  av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2027  return AVERROR_INVALIDDATA;
2028  }
2029 
2030  if (rice_parameter == 15)
2031  rice_parameter = 0;
2032 
2033  if (rice_parameter > 11) {
2034  av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2035  return AVERROR_INVALIDDATA;
2036  }
2037 
2038  for (i = 0; i < h; i++) {
2039  output = 1 << (s->avctx->bits_per_raw_sample - 1);
2040  top = 1 << (s->avctx->bits_per_raw_sample - 1);
2041 
2042  for (j = 0; j < w; j++) {
2043  left = output;
2044  topleft = top;
2045 
2046  rice_prefix_code = get_unary(&s->gb, 1, 12);
2047 
2048  /* Escape */
2049  if (rice_prefix_code == 11)
2050  dpcm_residual = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
2051  else {
2052  if (rice_prefix_code == 12) {
2053  av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n");
2054  return AVERROR_INVALIDDATA;
2055  }
2056  rice_suffix_code = get_bitsz(&s->gb, rice_parameter);
2057  dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code;
2058  }
2059 
2060  /* Map to a signed residual */
2061  if (dpcm_residual & 1)
2062  dpcm_residual = (-1 * dpcm_residual) >> 1;
2063  else
2064  dpcm_residual = (dpcm_residual >> 1);
2065 
2066  if (i != 0)
2067  top = macroblock[idx-w];
2068 
2069  p = left + top - topleft;
2070  min_left_top = FFMIN(left, top);
2071  if (p < min_left_top)
2072  p = min_left_top;
2073 
2074  max_left_top = FFMAX(left, top);
2075  if (p > max_left_top)
2076  p = max_left_top;
2077 
2078  p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1;
2079  if (p2 == p)
2080  p2 = block_mean;
2081 
2082  if (p2 > p)
2083  dpcm_residual *= -1;
2084 
2085  macroblock[idx++] = output = (dpcm_residual + p) & ((1 << s->avctx->bits_per_raw_sample) - 1);
2086  }
2087  }
2088 
2089  return 0;
2090 }
2091 
2092 static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
2093 {
2094  Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s;
2095  int i;
2096 
2097  ctx->dpcm_direction = 0;
2098 
2099  /* StudioMacroblock */
2100  /* Assumes I-VOP */
2101  s->mb_intra = 1;
2102  if (get_bits1(&s->gb)) { /* compression_mode */
2103  /* DCT */
2104  /* macroblock_type, 1 or 2-bit VLC */
2105  if (!get_bits1(&s->gb)) {
2106  skip_bits1(&s->gb);
2107  s->qscale = mpeg_get_qscale(s);
2108  }
2109 
2110  for (i = 0; i < mpeg4_block_count[s->chroma_format]; i++) {
2111  if (mpeg4_decode_studio_block(s, ctx->block32[i], i) < 0)
2112  return AVERROR_INVALIDDATA;
2113  }
2114  } else {
2115  /* DPCM */
2116  check_marker(s->avctx, &s->gb, "DPCM block start");
2117  ctx->dpcm_direction = get_bits1(&s->gb) ? -1 : 1;
2118  for (i = 0; i < 3; i++) {
2119  if (mpeg4_decode_dpcm_macroblock(s, ctx->dpcm_macroblock[i], i) < 0)
2120  return AVERROR_INVALIDDATA;
2121  }
2122  }
2123 
2124  if (get_bits_left(&s->gb) >= 24 && show_bits(&s->gb, 23) == 0) {
2125  next_start_code_studio(&s->gb);
2126  return SLICE_END;
2127  }
2128 
2129  //vcon-stp9L1.bits (first frame)
2130  if (get_bits_left(&s->gb) == 0)
2131  return SLICE_END;
2132 
2133  //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame)
2134  if (get_bits_left(&s->gb) < 8U && show_bits(&s->gb, get_bits_left(&s->gb)) == 0)
2135  return SLICE_END;
2136 
2137  return SLICE_OK;
2138 }
2139 
2141 {
2142  int hours, minutes, seconds;
2143 
2144  if (!show_bits(gb, 23)) {
2145  av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
2146  return AVERROR_INVALIDDATA;
2147  }
2148 
2149  hours = get_bits(gb, 5);
2150  minutes = get_bits(gb, 6);
2151  check_marker(s->avctx, gb, "in gop_header");
2152  seconds = get_bits(gb, 6);
2153 
2154  s->time_base = seconds + 60*(minutes + 60*hours);
2155 
2156  skip_bits1(gb);
2157  skip_bits1(gb);
2158 
2159  return 0;
2160 }
2161 
2163 {
2164 
2165  *profile = get_bits(gb, 4);
2166  *level = get_bits(gb, 4);
2167 
2168  // for Simple profile, level 0
2169  if (*profile == 0 && *level == 8) {
2170  *level = 0;
2171  }
2172 
2173  return 0;
2174 }
2175 
2177 {
2178  int visual_object_type;
2179  int is_visual_object_identifier = get_bits1(gb);
2180 
2181  if (is_visual_object_identifier) {
2182  skip_bits(gb, 4+3);
2183  }
2184  visual_object_type = get_bits(gb, 4);
2185 
2186  if (visual_object_type == VOT_VIDEO_ID ||
2187  visual_object_type == VOT_STILL_TEXTURE_ID) {
2188  int video_signal_type = get_bits1(gb);
2189  if (video_signal_type) {
2190  int video_range, color_description;
2191  skip_bits(gb, 3); // video_format
2192  video_range = get_bits1(gb);
2193  color_description = get_bits1(gb);
2194 
2195  s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2196 
2197  if (color_description) {
2198  s->avctx->color_primaries = get_bits(gb, 8);
2199  s->avctx->color_trc = get_bits(gb, 8);
2200  s->avctx->colorspace = get_bits(gb, 8);
2201  }
2202  }
2203  }
2204 
2205  return 0;
2206 }
2207 
2209 {
2210  int i, v;
2211 
2212  /* load default matrices */
2213  for (i = 0; i < 64; i++) {
2214  int j = s->idsp.idct_permutation[i];
2216  s->intra_matrix[j] = v;
2217  s->chroma_intra_matrix[j] = v;
2218 
2220  s->inter_matrix[j] = v;
2221  s->chroma_inter_matrix[j] = v;
2222  }
2223 }
2224 
2226 {
2227  int i, j, v;
2228 
2229  if (get_bits1(gb)) {
2230  if (get_bits_left(gb) < 64*8)
2231  return AVERROR_INVALIDDATA;
2232  /* intra_quantiser_matrix */
2233  for (i = 0; i < 64; i++) {
2234  v = get_bits(gb, 8);
2235  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2236  s->intra_matrix[j] = v;
2237  s->chroma_intra_matrix[j] = v;
2238  }
2239  }
2240 
2241  if (get_bits1(gb)) {
2242  if (get_bits_left(gb) < 64*8)
2243  return AVERROR_INVALIDDATA;
2244  /* non_intra_quantiser_matrix */
2245  for (i = 0; i < 64; i++) {
2246  get_bits(gb, 8);
2247  }
2248  }
2249 
2250  if (get_bits1(gb)) {
2251  if (get_bits_left(gb) < 64*8)
2252  return AVERROR_INVALIDDATA;
2253  /* chroma_intra_quantiser_matrix */
2254  for (i = 0; i < 64; i++) {
2255  v = get_bits(gb, 8);
2256  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2257  s->chroma_intra_matrix[j] = v;
2258  }
2259  }
2260 
2261  if (get_bits1(gb)) {
2262  if (get_bits_left(gb) < 64*8)
2263  return AVERROR_INVALIDDATA;
2264  /* chroma_non_intra_quantiser_matrix */
2265  for (i = 0; i < 64; i++) {
2266  get_bits(gb, 8);
2267  }
2268  }
2269 
2271  return 0;
2272 }
2273 
2275 {
2276  uint32_t startcode;
2277  uint8_t extension_type;
2278 
2279  startcode = show_bits_long(gb, 32);
2280  if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
2281 
2282  if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2283  skip_bits_long(gb, 32);
2284  extension_type = get_bits(gb, 4);
2285  if (extension_type == QUANT_MATRIX_EXT_ID)
2286  read_quant_matrix_ext(s, gb);
2287  }
2288  }
2289 }
2290 
2292 {
2293  MpegEncContext *s = &ctx->m;
2294  int width, height, aspect_ratio_info;
2295  int bits_per_raw_sample;
2296  int rgb, chroma_format;
2297 
2298  // random_accessible_vol and video_object_type_indication have already
2299  // been read by the caller decode_vol_header()
2300  skip_bits(gb, 4); /* video_object_layer_verid */
2301  ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
2302  skip_bits(gb, 4); /* video_object_layer_shape_extension */
2303  skip_bits1(gb); /* progressive_sequence */
2304  if (ctx->shape != RECT_SHAPE) {
2305  avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape");
2306  return AVERROR_PATCHWELCOME;
2307  }
2308  if (ctx->shape != BIN_ONLY_SHAPE) {
2309  rgb = get_bits1(gb); /* rgb_components */
2310  chroma_format = get_bits(gb, 2); /* chroma_format */
2311  if (!chroma_format || chroma_format == CHROMA_420 || (rgb && chroma_format == CHROMA_422)) {
2312  av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2313  return AVERROR_INVALIDDATA;
2314  }
2315 
2316  bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
2317  if (bits_per_raw_sample == 10) {
2318  if (rgb) {
2319  s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
2320  } else {
2321  s->avctx->pix_fmt = chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
2322  }
2323  } else {
2324  avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
2325  return AVERROR_PATCHWELCOME;
2326  }
2327  if (rgb != ctx->rgb || s->chroma_format != chroma_format)
2328  s->context_reinit = 1;
2329  s->avctx->bits_per_raw_sample = bits_per_raw_sample;
2330  ctx->rgb = rgb;
2331  s->chroma_format = chroma_format;
2332  }
2333  if (ctx->shape == RECT_SHAPE) {
2334  check_marker(s->avctx, gb, "before video_object_layer_width");
2335  width = get_bits(gb, 14); /* video_object_layer_width */
2336  check_marker(s->avctx, gb, "before video_object_layer_height");
2337  height = get_bits(gb, 14); /* video_object_layer_height */
2338  check_marker(s->avctx, gb, "after video_object_layer_height");
2339 
2340  /* Do the same check as non-studio profile */
2341  if (width && height) {
2342  if (s->width && s->height &&
2343  (s->width != width || s->height != height))
2344  s->context_reinit = 1;
2345  s->width = width;
2346  s->height = height;
2347  }
2348  }
2349  aspect_ratio_info = get_bits(gb, 4);
2350  if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2351  s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2352  s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2353  } else {
2354  s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2355  }
2356  skip_bits(gb, 4); /* frame_rate_code */
2357  skip_bits(gb, 15); /* first_half_bit_rate */
2358  check_marker(s->avctx, gb, "after first_half_bit_rate");
2359  skip_bits(gb, 15); /* latter_half_bit_rate */
2360  check_marker(s->avctx, gb, "after latter_half_bit_rate");
2361  skip_bits(gb, 15); /* first_half_vbv_buffer_size */
2362  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2363  skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
2364  skip_bits(gb, 11); /* first_half_vbv_buffer_size */
2365  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2366  skip_bits(gb, 15); /* latter_half_vbv_occupancy */
2367  check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2368  s->low_delay = get_bits1(gb);
2369  s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
2370 
2372  extension_and_user_data(s, gb, 2);
2373 
2374  return 0;
2375 }
2376 
2378 {
2379  MpegEncContext *s = &ctx->m;
2380  int width, height, vo_ver_id, aspect_ratio_info;
2381 
2382  /* vol header */
2383  skip_bits(gb, 1); /* random access */
2384  ctx->vo_type = get_bits(gb, 8);
2385 
2386  /* If we are in studio profile (per vo_type), check if its all consistent
2387  * and if so continue pass control to decode_studio_vol_header().
2388  * elIf something is inconsistent, error out
2389  * else continue with (non studio) vol header decpoding.
2390  */
2391  if (ctx->vo_type == CORE_STUDIO_VO_TYPE ||
2392  ctx->vo_type == SIMPLE_STUDIO_VO_TYPE) {
2393  if (s->avctx->profile != FF_PROFILE_UNKNOWN && s->avctx->profile != FF_PROFILE_MPEG4_SIMPLE_STUDIO)
2394  return AVERROR_INVALIDDATA;
2395  s->studio_profile = 1;
2396  s->avctx->profile = FF_PROFILE_MPEG4_SIMPLE_STUDIO;
2397  return decode_studio_vol_header(ctx, gb);
2398  } else if (s->studio_profile) {
2399  return AVERROR_PATCHWELCOME;
2400  }
2401 
2402  if (get_bits1(gb) != 0) { /* is_ol_id */
2403  vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
2404  skip_bits(gb, 3); /* vo_priority */
2405  } else {
2406  vo_ver_id = 1;
2407  }
2408  aspect_ratio_info = get_bits(gb, 4);
2409  if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2410  s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2411  s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2412  } else {
2413  s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2414  }
2415 
2416  if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2417  int chroma_format = get_bits(gb, 2);
2418  if (chroma_format != CHROMA_420)
2419  av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2420 
2421  s->low_delay = get_bits1(gb);
2422  if (get_bits1(gb)) { /* vbv parameters */
2423  get_bits(gb, 15); /* first_half_bitrate */
2424  check_marker(s->avctx, gb, "after first_half_bitrate");
2425  get_bits(gb, 15); /* latter_half_bitrate */
2426  check_marker(s->avctx, gb, "after latter_half_bitrate");
2427  get_bits(gb, 15); /* first_half_vbv_buffer_size */
2428  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2429  get_bits(gb, 3); /* latter_half_vbv_buffer_size */
2430  get_bits(gb, 11); /* first_half_vbv_occupancy */
2431  check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
2432  get_bits(gb, 15); /* latter_half_vbv_occupancy */
2433  check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2434  }
2435  } else {
2436  /* is setting low delay flag only once the smartest thing to do?
2437  * low delay detection will not be overridden. */
2438  if (s->picture_number == 0) {
2439  switch (ctx->vo_type) {
2440  case SIMPLE_VO_TYPE:
2441  case ADV_SIMPLE_VO_TYPE:
2442  s->low_delay = 1;
2443  break;
2444  default:
2445  s->low_delay = 0;
2446  }
2447  }
2448  }
2449 
2450  ctx->shape = get_bits(gb, 2); /* vol shape */
2451  if (ctx->shape != RECT_SHAPE)
2452  av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2453  if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2454  av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2455  skip_bits(gb, 4); /* video_object_layer_shape_extension */
2456  }
2457 
2458  check_marker(s->avctx, gb, "before time_increment_resolution");
2459 
2460  s->avctx->framerate.num = get_bits(gb, 16);
2461  if (!s->avctx->framerate.num) {
2462  av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
2463  return AVERROR_INVALIDDATA;
2464  }
2465 
2466  ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
2467  if (ctx->time_increment_bits < 1)
2468  ctx->time_increment_bits = 1;
2469 
2470  check_marker(s->avctx, gb, "before fixed_vop_rate");
2471 
2472  if (get_bits1(gb) != 0) /* fixed_vop_rate */
2473  s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
2474  else
2475  s->avctx->framerate.den = 1;
2476 
2477  s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2478 
2479  ctx->t_frame = 0;
2480 
2481  if (ctx->shape != BIN_ONLY_SHAPE) {
2482  if (ctx->shape == RECT_SHAPE) {
2483  check_marker(s->avctx, gb, "before width");
2484  width = get_bits(gb, 13);
2485  check_marker(s->avctx, gb, "before height");
2486  height = get_bits(gb, 13);
2487  check_marker(s->avctx, gb, "after height");
2488  if (width && height && /* they should be non zero but who knows */
2489  !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
2490  if (s->width && s->height &&
2491  (s->width != width || s->height != height))
2492  s->context_reinit = 1;
2493  s->width = width;
2494  s->height = height;
2495  }
2496  }
2497 
2498  s->progressive_sequence =
2499  s->progressive_frame = get_bits1(gb) ^ 1;
2500  s->interlaced_dct = 0;
2501  if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
2502  av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
2503  "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2504  if (vo_ver_id == 1)
2505  ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
2506  else
2507  ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
2508 
2509  if (ctx->vol_sprite_usage == STATIC_SPRITE)
2510  av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2511  if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2512  ctx->vol_sprite_usage == GMC_SPRITE) {
2513  if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2514  skip_bits(gb, 13); // sprite_width
2515  check_marker(s->avctx, gb, "after sprite_width");
2516  skip_bits(gb, 13); // sprite_height
2517  check_marker(s->avctx, gb, "after sprite_height");
2518  skip_bits(gb, 13); // sprite_left
2519  check_marker(s->avctx, gb, "after sprite_left");
2520  skip_bits(gb, 13); // sprite_top
2521  check_marker(s->avctx, gb, "after sprite_top");
2522  }
2523  ctx->num_sprite_warping_points = get_bits(gb, 6);
2524  if (ctx->num_sprite_warping_points > 3) {
2525  av_log(s->avctx, AV_LOG_ERROR,
2526  "%d sprite_warping_points\n",
2527  ctx->num_sprite_warping_points);
2528  ctx->num_sprite_warping_points = 0;
2529  return AVERROR_INVALIDDATA;
2530  }
2531  s->sprite_warping_accuracy = get_bits(gb, 2);
2532  ctx->sprite_brightness_change = get_bits1(gb);
2533  if (ctx->vol_sprite_usage == STATIC_SPRITE)
2534  skip_bits1(gb); // low_latency_sprite
2535  }
2536  // FIXME sadct disable bit if verid!=1 && shape not rect
2537 
2538  if (get_bits1(gb) == 1) { /* not_8_bit */
2539  s->quant_precision = get_bits(gb, 4); /* quant_precision */
2540  if (get_bits(gb, 4) != 8) /* bits_per_pixel */
2541  av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
2542  if (s->quant_precision != 5)
2543  av_log(s->avctx, AV_LOG_ERROR,
2544  "quant precision %d\n", s->quant_precision);
2545  if (s->quant_precision<3 || s->quant_precision>9) {
2546  s->quant_precision = 5;
2547  }
2548  } else {
2549  s->quant_precision = 5;
2550  }
2551 
2552  // FIXME a bunch of grayscale shape things
2553 
2554  if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2555  int i, v;
2556 
2558 
2559  /* load custom intra matrix */
2560  if (get_bits1(gb)) {
2561  int last = 0;
2562  for (i = 0; i < 64; i++) {
2563  int j;
2564  if (get_bits_left(gb) < 8) {
2565  av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2566  return AVERROR_INVALIDDATA;
2567  }
2568  v = get_bits(gb, 8);
2569  if (v == 0)
2570  break;
2571 
2572  last = v;
2573  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2574  s->intra_matrix[j] = last;
2575  s->chroma_intra_matrix[j] = last;
2576  }
2577 
2578  /* replicate last value */
2579  for (; i < 64; i++) {
2580  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2581  s->intra_matrix[j] = last;
2582  s->chroma_intra_matrix[j] = last;
2583  }
2584  }
2585 
2586  /* load custom non intra matrix */
2587  if (get_bits1(gb)) {
2588  int last = 0;
2589  for (i = 0; i < 64; i++) {
2590  int j;
2591  if (get_bits_left(gb) < 8) {
2592  av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2593  return AVERROR_INVALIDDATA;
2594  }
2595  v = get_bits(gb, 8);
2596  if (v == 0)
2597  break;
2598 
2599  last = v;
2600  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2601  s->inter_matrix[j] = v;
2602  s->chroma_inter_matrix[j] = v;
2603  }
2604 
2605  /* replicate last value */
2606  for (; i < 64; i++) {
2607  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2608  s->inter_matrix[j] = last;
2609  s->chroma_inter_matrix[j] = last;
2610  }
2611  }
2612 
2613  // FIXME a bunch of grayscale shape things
2614  }
2615 
2616  if (vo_ver_id != 1)
2617  s->quarter_sample = get_bits1(gb);
2618  else
2619  s->quarter_sample = 0;
2620 
2621  if (get_bits_left(gb) < 4) {
2622  av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2623  return AVERROR_INVALIDDATA;
2624  }
2625 
2626  if (!get_bits1(gb)) {
2627  int pos = get_bits_count(gb);
2628  int estimation_method = get_bits(gb, 2);
2629  if (estimation_method < 2) {
2630  if (!get_bits1(gb)) {
2631  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2632  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2633  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2634  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2635  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2636  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2637  }
2638  if (!get_bits1(gb)) {
2639  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2640  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2641  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2642  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2643  }
2644  if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2645  skip_bits_long(gb, pos - get_bits_count(gb));
2646  goto no_cplx_est;
2647  }
2648  if (!get_bits1(gb)) {
2649  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2650  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2651  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2652  ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2653  }
2654  if (!get_bits1(gb)) {
2655  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2656  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2657  ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2658  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2659  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2660  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2661  }
2662  if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2663  skip_bits_long(gb, pos - get_bits_count(gb));
2664  goto no_cplx_est;
2665  }
2666  if (estimation_method == 1) {
2667  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2668  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2669  }
2670  } else
2671  av_log(s->avctx, AV_LOG_ERROR,
2672  "Invalid Complexity estimation method %d\n",
2673  estimation_method);
2674  } else {
2675 
2676 no_cplx_est:
2677  ctx->cplx_estimation_trash_i =
2678  ctx->cplx_estimation_trash_p =
2679  ctx->cplx_estimation_trash_b = 0;
2680  }
2681 
2682  ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2683 
2684  s->data_partitioning = get_bits1(gb);
2685  if (s->data_partitioning)
2686  ctx->rvlc = get_bits1(gb);
2687 
2688  if (vo_ver_id != 1) {
2689  ctx->new_pred = get_bits1(gb);
2690  if (ctx->new_pred) {
2691  av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2692  skip_bits(gb, 2); /* requested upstream message type */
2693  skip_bits1(gb); /* newpred segment type */
2694  }
2695  if (get_bits1(gb)) // reduced_res_vop
2696  av_log(s->avctx, AV_LOG_ERROR,
2697  "reduced resolution VOP not supported\n");
2698  } else {
2699  ctx->new_pred = 0;
2700  }
2701 
2702  ctx->scalability = get_bits1(gb);
2703 
2704  if (ctx->scalability) {
2705  GetBitContext bak = *gb;
2706  int h_sampling_factor_n;
2707  int h_sampling_factor_m;
2708  int v_sampling_factor_n;
2709  int v_sampling_factor_m;
2710 
2711  skip_bits1(gb); // hierarchy_type
2712  skip_bits(gb, 4); /* ref_layer_id */
2713  skip_bits1(gb); /* ref_layer_sampling_dir */
2714  h_sampling_factor_n = get_bits(gb, 5);
2715  h_sampling_factor_m = get_bits(gb, 5);
2716  v_sampling_factor_n = get_bits(gb, 5);
2717  v_sampling_factor_m = get_bits(gb, 5);
2718  ctx->enhancement_type = get_bits1(gb);
2719 
2720  if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2721  v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2722  /* illegal scalability header (VERY broken encoder),
2723  * trying to workaround */
2724  ctx->scalability = 0;
2725  *gb = bak;
2726  } else
2727  av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2728 
2729  // bin shape stuff FIXME
2730  }
2731  }
2732 
2733  if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2734  av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d %s%s%s%s\n",
2735  s->avctx->framerate.den, s->avctx->framerate.num,
2736  ctx->time_increment_bits,
2737  s->quant_precision,
2738  s->progressive_sequence,
2739  s->low_delay,
2740  ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2741  s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2742  );
2743  }
2744 
2745  return 0;
2746 }
2747 
2748 /**
2749  * Decode the user data stuff in the header.
2750  * Also initializes divx/xvid/lavc_version/build.
2751  */
2753 {
2754  MpegEncContext *s = &ctx->m;
2755  char buf[256];
2756  int i;
2757  int e;
2758  int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2759  char last;
2760 
2761  for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2762  if (show_bits(gb, 23) == 0)
2763  break;
2764  buf[i] = get_bits(gb, 8);
2765  }
2766  buf[i] = 0;
2767 
2768  /* divx detection */
2769  e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2770  if (e < 2)
2771  e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2772  if (e >= 2) {
2773  ctx->divx_version = ver;
2774  ctx->divx_build = build;
2775  s->divx_packed = e == 3 && last == 'p';
2776  }
2777 
2778  /* libavcodec detection */
2779  e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2780  if (e != 4)
2781  e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2782  if (e != 4) {
2783  e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2784  if (e > 1) {
2785  if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
2786  av_log(s->avctx, AV_LOG_WARNING,
2787  "Unknown Lavc version string encountered, %d.%d.%d; "
2788  "clamping sub-version values to 8-bits.\n",
2789  ver, ver2, ver3);
2790  }
2791  build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
2792  }
2793  }
2794  if (e != 4) {
2795  if (strcmp(buf, "ffmpeg") == 0)
2796  ctx->lavc_build = 4600;
2797  }
2798  if (e == 4)
2799  ctx->lavc_build = build;
2800 
2801  /* Xvid detection */
2802  e = sscanf(buf, "XviD%d", &build);
2803  if (e == 1)
2804  ctx->xvid_build = build;
2805 
2806  return 0;
2807 }
2808 
2810 {
2811  Mpeg4DecContext *ctx = avctx->priv_data;
2812  MpegEncContext *s = &ctx->m;
2813 
2814  if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2815  if (s->codec_tag == AV_RL32("XVID") ||
2816  s->codec_tag == AV_RL32("XVIX") ||
2817  s->codec_tag == AV_RL32("RMP4") ||
2818  s->codec_tag == AV_RL32("ZMP4") ||
2819  s->codec_tag == AV_RL32("SIPP"))
2820  ctx->xvid_build = 0;
2821  }
2822 
2823  if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2824  if (s->codec_tag == AV_RL32("DIVX") && ctx->vo_type == 0 &&
2825  ctx->vol_control_parameters == 0)
2826  ctx->divx_version = 400; // divx 4
2827 
2828  if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2829  ctx->divx_version =
2830  ctx->divx_build = -1;
2831  }
2832 
2833  if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2834  if (s->codec_tag == AV_RL32("XVIX"))
2835  s->workaround_bugs |= FF_BUG_XVID_ILACE;
2836 
2837  if (s->codec_tag == AV_RL32("UMP4"))
2838  s->workaround_bugs |= FF_BUG_UMP4;
2839 
2840  if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2841  s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2842 
2843  if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2844  s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2845 
2846  if (ctx->xvid_build <= 3U)
2847  s->padding_bug_score = 256 * 256 * 256 * 64;
2848 
2849  if (ctx->xvid_build <= 1U)
2850  s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2851 
2852  if (ctx->xvid_build <= 12U)
2853  s->workaround_bugs |= FF_BUG_EDGE;
2854 
2855  if (ctx->xvid_build <= 32U)
2856  s->workaround_bugs |= FF_BUG_DC_CLIP;
2857 
2858 #define SET_QPEL_FUNC(postfix1, postfix2) \
2859  s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2860  s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2861  s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2862 
2863  if (ctx->lavc_build < 4653U)
2864  s->workaround_bugs |= FF_BUG_STD_QPEL;
2865 
2866  if (ctx->lavc_build < 4655U)
2867  s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2868 
2869  if (ctx->lavc_build < 4670U)
2870  s->workaround_bugs |= FF_BUG_EDGE;
2871 
2872  if (ctx->lavc_build <= 4712U)
2873  s->workaround_bugs |= FF_BUG_DC_CLIP;
2874 
2875  if ((ctx->lavc_build&0xFF) >= 100) {
2876  if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2877  (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2878  )
2879  s->workaround_bugs |= FF_BUG_IEDGE;
2880  }
2881 
2882  if (ctx->divx_version >= 0)
2883  s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2884  if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2885  s->padding_bug_score = 256 * 256 * 256 * 64;
2886 
2887  if (ctx->divx_version < 500U)
2888  s->workaround_bugs |= FF_BUG_EDGE;
2889 
2890  if (ctx->divx_version >= 0)
2891  s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2892  }
2893 
2894  if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2895  SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2896  SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2897  SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2898  SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2899  SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2900  SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2901 
2902  SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2903  SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2904  SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2905  SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2906  SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2907  SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2908  }
2909 
2910  if (avctx->debug & FF_DEBUG_BUGS)
2911  av_log(s->avctx, AV_LOG_DEBUG,
2912  "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2913  s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2914  ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2915 
2916  if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2917  s->codec_id == AV_CODEC_ID_MPEG4 &&
2918  avctx->idct_algo == FF_IDCT_AUTO) {
2919  avctx->idct_algo = FF_IDCT_XVID;
2921  return 1;
2922  }
2923 
2924  return 0;
2925 }
2926 
2928  int parse_only)
2929 {
2930  MpegEncContext *s = &ctx->m;
2931  int time_incr, time_increment;
2932  int64_t pts;
2933 
2934  s->mcsel = 0;
2935  s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2936  if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2937  ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
2938  av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2939  s->low_delay = 0;
2940  }
2941 
2942  s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2943  if (s->partitioned_frame)
2944  s->decode_mb = mpeg4_decode_partitioned_mb;
2945  else
2946  s->decode_mb = mpeg4_decode_mb;
2947 
2948  time_incr = 0;
2949  while (get_bits1(gb) != 0)
2950  time_incr++;
2951 
2952  check_marker(s->avctx, gb, "before time_increment");
2953 
2954  if (ctx->time_increment_bits == 0 ||
2955  !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2956  av_log(s->avctx, AV_LOG_WARNING,
2957  "time_increment_bits %d is invalid in relation to the current bitstream, this is likely caused by a missing VOL header\n", ctx->time_increment_bits);
2958 
2959  for (ctx->time_increment_bits = 1;
2960  ctx->time_increment_bits < 16;
2961  ctx->time_increment_bits++) {
2962  if (s->pict_type == AV_PICTURE_TYPE_P ||
2963  (s->pict_type == AV_PICTURE_TYPE_S &&
2964  ctx->vol_sprite_usage == GMC_SPRITE)) {
2965  if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2966  break;
2967  } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2968  break;
2969  }
2970 
2971  av_log(s->avctx, AV_LOG_WARNING,
2972  "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2973  if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2974  s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2975  s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2976  }
2977  }
2978 
2979  if (IS_3IV1)
2980  time_increment = get_bits1(gb); // FIXME investigate further
2981  else
2982  time_increment = get_bits(gb, ctx->time_increment_bits);
2983 
2984  if (s->pict_type != AV_PICTURE_TYPE_B) {
2985  s->last_time_base = s->time_base;
2986  s->time_base += time_incr;
2987  s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
2988  if (s->workaround_bugs & FF_BUG_UMP4) {
2989  if (s->time < s->last_non_b_time) {
2990  /* header is not mpeg-4-compatible, broken encoder,
2991  * trying to workaround */
2992  s->time_base++;
2993  s->time += s->avctx->framerate.num;
2994  }
2995  }
2996  s->pp_time = s->time - s->last_non_b_time;
2997  s->last_non_b_time = s->time;
2998  } else {
2999  s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
3000  s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
3001  if (s->pp_time <= s->pb_time ||
3002  s->pp_time <= s->pp_time - s->pb_time ||
3003  s->pp_time <= 0) {
3004  /* messed up order, maybe after seeking? skipping current B-frame */
3005  return FRAME_SKIPPED;
3006  }
3008 
3009  if (ctx->t_frame == 0)
3010  ctx->t_frame = s->pb_time;
3011  if (ctx->t_frame == 0)
3012  ctx->t_frame = 1; // 1/0 protection
3013  s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
3014  ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
3015  s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
3016  ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
3017  if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
3018  s->pb_field_time = 2;
3019  s->pp_field_time = 4;
3020  if (!s->progressive_sequence)
3021  return FRAME_SKIPPED;
3022  }
3023  }
3024 
3025  if (s->avctx->framerate.den)
3026  pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
3027  else
3028  pts = AV_NOPTS_VALUE;
3029  ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
3030 
3031  check_marker(s->avctx, gb, "before vop_coded");
3032 
3033  /* vop coded */
3034  if (get_bits1(gb) != 1) {
3035  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
3036  av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
3037  return FRAME_SKIPPED;
3038  }
3039  if (ctx->new_pred)
3040  decode_new_pred(ctx, gb);
3041 
3042  if (ctx->shape != BIN_ONLY_SHAPE &&
3043  (s->pict_type == AV_PICTURE_TYPE_P ||
3044  (s->pict_type == AV_PICTURE_TYPE_S &&
3045  ctx->vol_sprite_usage == GMC_SPRITE))) {
3046  /* rounding type for motion estimation */
3047  s->no_rounding = get_bits1(gb);
3048  } else {
3049  s->no_rounding = 0;
3050  }
3051  // FIXME reduced res stuff
3052 
3053  if (ctx->shape != RECT_SHAPE) {
3054  if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
3055  skip_bits(gb, 13); /* width */
3056  check_marker(s->avctx, gb, "after width");
3057  skip_bits(gb, 13); /* height */
3058  check_marker(s->avctx, gb, "after height");
3059  skip_bits(gb, 13); /* hor_spat_ref */
3060  check_marker(s->avctx, gb, "after hor_spat_ref");
3061  skip_bits(gb, 13); /* ver_spat_ref */
3062  }
3063  skip_bits1(gb); /* change_CR_disable */
3064 
3065  if (get_bits1(gb) != 0)
3066  skip_bits(gb, 8); /* constant_alpha_value */
3067  }
3068 
3069  // FIXME complexity estimation stuff
3070 
3071  if (ctx->shape != BIN_ONLY_SHAPE) {
3072  skip_bits_long(gb, ctx->cplx_estimation_trash_i);
3073  if (s->pict_type != AV_PICTURE_TYPE_I)
3074  skip_bits_long(gb, ctx->cplx_estimation_trash_p);
3075  if (s->pict_type == AV_PICTURE_TYPE_B)
3076  skip_bits_long(gb, ctx->cplx_estimation_trash_b);
3077 
3078  if (get_bits_left(gb) < 3) {
3079  av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
3080  return AVERROR_INVALIDDATA;
3081  }
3082  ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
3083  if (!s->progressive_sequence) {
3084  s->top_field_first = get_bits1(gb);
3085  s->alternate_scan = get_bits1(gb);
3086  } else
3087  s->alternate_scan = 0;
3088  }
3089 
3090  if (s->alternate_scan) {
3091  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
3092  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
3093  ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
3094  ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3095  } else {
3096  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
3097  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
3098  ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
3099  ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3100  }
3101 
3102  /* Skip at this point when only parsing since the remaining
3103  * data is not useful for a parser and requires the
3104  * sprite_trajectory VLC to be initialized. */
3105  if (parse_only)
3106  goto end;
3107 
3108  if (s->pict_type == AV_PICTURE_TYPE_S) {
3109  if((ctx->vol_sprite_usage == STATIC_SPRITE ||
3110  ctx->vol_sprite_usage == GMC_SPRITE)) {
3111  if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
3112  return AVERROR_INVALIDDATA;
3113  if (ctx->sprite_brightness_change)
3114  av_log(s->avctx, AV_LOG_ERROR,
3115  "sprite_brightness_change not supported\n");
3116  if (ctx->vol_sprite_usage == STATIC_SPRITE)
3117  av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
3118  } else {
3119  memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
3120  memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
3121  }
3122  }
3123 
3124  if (ctx->shape != BIN_ONLY_SHAPE) {
3125  s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
3126  if (s->qscale == 0) {
3127  av_log(s->avctx, AV_LOG_ERROR,
3128  "Error, header damaged or not MPEG-4 header (qscale=0)\n");
3129  return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3130  }
3131 
3132  if (s->pict_type != AV_PICTURE_TYPE_I) {
3133  s->f_code = get_bits(gb, 3); /* fcode_for */
3134  if (s->f_code == 0) {
3135  av_log(s->avctx, AV_LOG_ERROR,
3136  "Error, header damaged or not MPEG-4 header (f_code=0)\n");
3137  s->f_code = 1;
3138  return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3139  }
3140  } else
3141  s->f_code = 1;
3142 
3143  if (s->pict_type == AV_PICTURE_TYPE_B) {
3144  s->b_code = get_bits(gb, 3);
3145  if (s->b_code == 0) {
3146  av_log(s->avctx, AV_LOG_ERROR,
3147  "Error, header damaged or not MPEG4 header (b_code=0)\n");
3148  s->b_code=1;
3149  return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
3150  }
3151  } else
3152  s->b_code = 1;
3153 
3154  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
3155  av_log(s->avctx, AV_LOG_DEBUG,
3156  "qp:%d fc:%d,%d %c size:%d pro:%d alt:%d top:%d %cpel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
3157  s->qscale, s->f_code, s->b_code,
3158  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')),
3159  gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
3160  s->top_field_first, s->quarter_sample ? 'q' : 'h',
3161  s->data_partitioning, ctx->resync_marker,
3162  ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
3163  1 - s->no_rounding, ctx->vo_type,
3164  ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
3165  ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
3166  ctx->cplx_estimation_trash_b,
3167  s->time,
3168  time_increment
3169  );
3170  }
3171 
3172  if (!ctx->scalability) {
3173  if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
3174  skip_bits1(gb); // vop shape coding type
3175  } else {
3176  if (ctx->enhancement_type) {
3177  int load_backward_shape = get_bits1(gb);
3178  if (load_backward_shape)
3179  av_log(s->avctx, AV_LOG_ERROR,
3180  "load backward shape isn't supported\n");
3181  }
3182  skip_bits(gb, 2); // ref_select_code
3183  }
3184  }
3185 
3186 end:
3187  /* detect buggy encoders which don't set the low_delay flag
3188  * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
3189  * easily (although it's buggy too) */
3190  if (ctx->vo_type == 0 && ctx->vol_control_parameters == 0 &&
3191  ctx->divx_version == -1 && s->picture_number == 0) {
3192  av_log(s->avctx, AV_LOG_WARNING,
3193  "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
3194  s->low_delay = 1;
3195  }
3196 
3197  s->picture_number++; // better than pic number==0 always ;)
3198 
3199  // FIXME add short header support
3200  s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
3201  s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
3202 
3203  if (s->workaround_bugs & FF_BUG_EDGE) {
3204  s->h_edge_pos = s->width;
3205  s->v_edge_pos = s->height;
3206  }
3207  return 0;
3208 }
3209 
3211 {
3212  MpegEncContext *s = &ctx->m;
3213 
3214  skip_bits(gb, 16); /* Time_code[63..48] */
3215  check_marker(s->avctx, gb, "after Time_code[63..48]");
3216  skip_bits(gb, 16); /* Time_code[47..32] */
3217  check_marker(s->avctx, gb, "after Time_code[47..32]");
3218  skip_bits(gb, 16); /* Time_code[31..16] */
3219  check_marker(s->avctx, gb, "after Time_code[31..16]");
3220  skip_bits(gb, 16); /* Time_code[15..0] */
3221  check_marker(s->avctx, gb, "after Time_code[15..0]");
3222  skip_bits(gb, 4); /* reserved_bits */
3223 }
3224 
3225 /**
3226  * Decode the next studio vop header.
3227  * @return <0 if something went wrong
3228  */
3230 {
3231  MpegEncContext *s = &ctx->m;
3232 
3233  if (get_bits_left(gb) <= 32)
3234  return 0;
3235 
3236  s->partitioned_frame = 0;
3237  s->interlaced_dct = 0;
3238  s->decode_mb = mpeg4_decode_studio_mb;
3239 
3240  decode_smpte_tc(ctx, gb);
3241 
3242  skip_bits(gb, 10); /* temporal_reference */
3243  skip_bits(gb, 2); /* vop_structure */
3244  s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
3245  if (get_bits1(gb)) { /* vop_coded */
3246  skip_bits1(gb); /* top_field_first */
3247  skip_bits1(gb); /* repeat_first_field */
3248  s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
3249  }
3250 
3251  if (s->pict_type == AV_PICTURE_TYPE_I) {
3252  if (get_bits1(gb))
3254  }
3255 
3256  if (ctx->shape != BIN_ONLY_SHAPE) {
3257  s->alternate_scan = get_bits1(gb);
3258  s->frame_pred_frame_dct = get_bits1(gb);
3259  s->dct_precision = get_bits(gb, 2);
3260  s->intra_dc_precision = get_bits(gb, 2);
3261  s->q_scale_type = get_bits1(gb);
3262  }
3263 
3264  if (s->alternate_scan) {
3265  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
3266  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
3267  ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
3268  ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3269  } else {
3270  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
3271  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
3272  ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
3273  ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3274  }
3275 
3277 
3279  extension_and_user_data(s, gb, 4);
3280 
3281  return 0;
3282 }
3283 
3285 {
3286  MpegEncContext *s = &ctx->m;
3287  int visual_object_type;
3288 
3289  skip_bits(gb, 4); /* visual_object_verid */
3290  visual_object_type = get_bits(gb, 4);
3291  if (visual_object_type != VOT_VIDEO_ID) {
3292  avpriv_request_sample(s->avctx, "VO type %u", visual_object_type);
3293  return AVERROR_PATCHWELCOME;
3294  }
3295 
3297  extension_and_user_data(s, gb, 1);
3298 
3299  return 0;
3300 }
3301 
3302 /**
3303  * Decode MPEG-4 headers.
3304  *
3305  * @param header If set the absence of a VOP is not treated as error; otherwise, it is treated as such.
3306  * @param parse_only If set, things only relevant to a decoder may be skipped;
3307  * furthermore, the VLC tables may be uninitialized.
3308  * @return <0 if an error occurred
3309  * FRAME_SKIPPED if a not coded VOP is found
3310  * 0 else
3311  */
3313  int header, int parse_only)
3314 {
3315  MpegEncContext *s = &ctx->m;
3316  unsigned startcode, v;
3317  int ret;
3318  int vol = 0;
3319 
3320  /* search next start code */
3321  align_get_bits(gb);
3322 
3323  // If we have not switched to studio profile than we also did not switch bps
3324  // that means something else (like a previous instance) outside set bps which
3325  // would be inconsistant with the currect state, thus reset it
3326  if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3327  s->avctx->bits_per_raw_sample = 0;
3328 
3329  if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3330  skip_bits(gb, 24);
3331  if (get_bits(gb, 8) == 0xF0)
3332  goto end;
3333  }
3334 
3335  startcode = 0xff;
3336  for (;;) {
3337  if (get_bits_count(gb) >= gb->size_in_bits) {
3338  if (gb->size_in_bits == 8 &&
3339  (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3340  av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3341  return FRAME_SKIPPED; // divx bug
3342  } else if (header && get_bits_count(gb) == gb->size_in_bits) {
3343  return 0; // ordinary return value for parsing of extradata
3344  } else
3345  return AVERROR_INVALIDDATA; // end of stream
3346  }
3347 
3348  /* use the bits after the test */
3349  v = get_bits(gb, 8);
3350  startcode = ((startcode << 8) | v) & 0xffffffff;
3351 
3352  if ((startcode & 0xFFFFFF00) != 0x100)
3353  continue; // no startcode
3354 
3355  if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3356  const char *name;
3357  if (startcode <= 0x11F)
3358  name = "Video Object Start";
3359  else if (startcode <= 0x12F)
3360  name = "Video Object Layer Start";
3361  else if (startcode <= 0x13F)
3362  name = "Reserved";
3363  else if (startcode <= 0x15F)
3364  name = "FGS bp start";
3365  else if (startcode <= 0x1AF)
3366  name = "Reserved";
3367  else if (startcode == 0x1B0)
3368  name = "Visual Object Seq Start";
3369  else if (startcode == 0x1B1)
3370  name = "Visual Object Seq End";
3371  else if (startcode == 0x1B2)
3372  name = "User Data";
3373  else if (startcode == 0x1B3)
3374  name = "Group of VOP start";
3375  else if (startcode == 0x1B4)
3376  name = "Video Session Error";
3377  else if (startcode == 0x1B5)
3378  name = "Visual Object Start";
3379  else if (startcode == 0x1B6)
3380  name = "Video Object Plane start";
3381  else if (startcode == 0x1B7)
3382  name = "slice start";
3383  else if (startcode == 0x1B8)
3384  name = "extension start";
3385  else if (startcode == 0x1B9)
3386  name = "fgs start";
3387  else if (startcode == 0x1BA)
3388  name = "FBA Object start";
3389  else if (startcode == 0x1BB)
3390  name = "FBA Object Plane start";
3391  else if (startcode == 0x1BC)
3392  name = "Mesh Object start";
3393  else if (startcode == 0x1BD)
3394  name = "Mesh Object Plane start";
3395  else if (startcode == 0x1BE)
3396  name = "Still Texture Object start";
3397  else if (startcode == 0x1BF)
3398  name = "Texture Spatial Layer start";
3399  else if (startcode == 0x1C0)
3400  name = "Texture SNR Layer start";
3401  else if (startcode == 0x1C1)
3402  name = "Texture Tile start";
3403  else if (startcode == 0x1C2)
3404  name = "Texture Shape Layer start";
3405  else if (startcode == 0x1C3)
3406  name = "stuffing start";
3407  else if (startcode <= 0x1C5)
3408  name = "Reserved";
3409  else if (startcode <= 0x1FF)
3410  name = "System start";
3411  av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X %s at %d\n",
3412  startcode, name, get_bits_count(gb));
3413  }
3414 
3415  if (startcode >= 0x120 && startcode <= 0x12F) {
3416  if (vol) {
3417  av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3418  continue;
3419  }
3420  vol++;
3421  if ((ret = decode_vol_header(ctx, gb)) < 0)
3422  return ret;
3423  } else if (startcode == USER_DATA_STARTCODE) {
3424  decode_user_data(ctx, gb);
3425  } else if (startcode == GOP_STARTCODE) {
3427  } else if (startcode == VOS_STARTCODE) {
3428  int profile, level;
3431  (level > 0 && level < 9)) {
3432  s->studio_profile = 1;
3434  extension_and_user_data(s, gb, 0);
3435  } else if (s->studio_profile) {
3436  avpriv_request_sample(s->avctx, "Mix of studio and non studio profile");
3437  return AVERROR_PATCHWELCOME;
3438  }
3439  s->avctx->profile = profile;
3440  s->avctx->level = level;
3441  } else if (startcode == VISUAL_OBJ_STARTCODE) {
3442  if (s->studio_profile) {
3443  if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3444  return ret;
3445  } else
3447  } else if (startcode == VOP_STARTCODE) {
3448  break;
3449  }
3450 
3451  align_get_bits(gb);
3452  startcode = 0xff;
3453  }
3454 
3455 end:
3456  if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
3457  s->low_delay = 1;
3458  s->avctx->has_b_frames = !s->low_delay;
3459 
3460  if (s->studio_profile) {
3461  if (!s->avctx->bits_per_raw_sample) {
3462  av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n");
3463  return AVERROR_INVALIDDATA;
3464  }
3465  return decode_studio_vop_header(ctx, gb);
3466  } else
3467  return decode_vop_header(ctx, gb, parse_only);
3468 }
3469 
3470 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
3471 {
3472  Mpeg4DecContext *ctx = avctx->priv_data;
3473  MpegEncContext *s = &ctx->m;
3474 
3475  /* divx 5.01+ bitstream reorder stuff */
3476  /* Since this clobbers the input buffer and hwaccel codecs still need the
3477  * data during hwaccel->end_frame we should not do this any earlier */
3478  if (s->divx_packed) {
3479  int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
3480  int startcode_found = 0;
3481 
3482  if (buf_size - current_pos > 7) {
3483 
3484  int i;
3485  for (i = current_pos; i < buf_size - 4; i++)
3486 
3487  if (buf[i] == 0 &&
3488  buf[i + 1] == 0 &&
3489  buf[i + 2] == 1 &&
3490  buf[i + 3] == 0xB6) {
3491  startcode_found = !(buf[i + 4] & 0x40);
3492  break;
3493  }
3494  }
3495 
3496  if (startcode_found) {
3497  if (!ctx->showed_packed_warning) {
3498  av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
3499  "wasteful way to store B-frames ('packed B-frames'). "
3500  "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
3501  ctx->showed_packed_warning = 1;
3502  }
3503  av_fast_padded_malloc(&s->bitstream_buffer,
3504  &s->allocated_bitstream_buffer_size,
3505  buf_size - current_pos);
3506  if (!s->bitstream_buffer) {
3507  s->bitstream_buffer_size = 0;
3508  return AVERROR(ENOMEM);
3509  }
3510  memcpy(s->bitstream_buffer, buf + current_pos,
3511  buf_size - current_pos);
3512  s->bitstream_buffer_size = buf_size - current_pos;
3513  }
3514  }
3515 
3516  return 0;
3517 }
3518 
3519 #if HAVE_THREADS
3520 static int mpeg4_update_thread_context(AVCodecContext *dst,
3521  const AVCodecContext *src)
3522 {
3523  Mpeg4DecContext *s = dst->priv_data;
3524  const Mpeg4DecContext *s1 = src->priv_data;
3525  int init = s->m.context_initialized;
3526 
3528 
3529  if (ret < 0)
3530  return ret;
3531 
3532  // copy all the necessary fields explicitly
3533  s->time_increment_bits = s1->time_increment_bits;
3534  s->shape = s1->shape;
3535  s->vol_sprite_usage = s1->vol_sprite_usage;
3536  s->sprite_brightness_change = s1->sprite_brightness_change;
3537  s->num_sprite_warping_points = s1->num_sprite_warping_points;
3538  s->m.data_partitioning = s1->m.data_partitioning;
3539  s->rvlc = s1->rvlc;
3540  s->resync_marker = s1->resync_marker;
3541  s->t_frame = s1->t_frame;
3542  s->new_pred = s1->new_pred;
3543  s->enhancement_type = s1->enhancement_type;
3544  s->scalability = s1->scalability;
3545  s->intra_dc_threshold = s1->intra_dc_threshold;
3546  s->divx_version = s1->divx_version;
3547  s->divx_build = s1->divx_build;
3548  s->xvid_build = s1->xvid_build;
3549  s->lavc_build = s1->lavc_build;
3550  s->vo_type = s1->vo_type;
3551  s->showed_packed_warning = s1->showed_packed_warning;
3552  s->vol_control_parameters = s1->vol_control_parameters;
3553  s->cplx_estimation_trash_i = s1->cplx_estimation_trash_i;
3554  s->cplx_estimation_trash_p = s1->cplx_estimation_trash_p;
3555  s->cplx_estimation_trash_b = s1->cplx_estimation_trash_b;
3556  s->rgb = s1->rgb;
3557 
3558  memcpy(s->sprite_shift, s1->sprite_shift, sizeof(s1->sprite_shift));
3559  memcpy(s->sprite_traj, s1->sprite_traj, sizeof(s1->sprite_traj));
3560 
3561  if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
3562  ff_xvid_idct_init(&s->m.idsp, dst);
3563 
3564  return 0;
3565 }
3566 
3567 static int mpeg4_update_thread_context_for_user(AVCodecContext *dst,
3568  const AVCodecContext *src)
3569 {
3570  MpegEncContext *m = dst->priv_data;
3571  const MpegEncContext *m1 = src->priv_data;
3572 
3573  m->quarter_sample = m1->quarter_sample;
3574  m->divx_packed = m1->divx_packed;
3575 
3576  return 0;
3577 }
3578 #endif
3579 
3580 static av_cold void mpeg4_init_static(void)
3581 {
3582  static uint8_t mpeg4_rvlc_rl_tables[2][2][2 * MAX_RUN + MAX_LEVEL + 3];
3583 
3585  &ff_mpeg4_studio_dc_luma[0][1], 2,
3586  &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
3587  0, 0, 528);
3588 
3590  &ff_mpeg4_studio_dc_chroma[0][1], 2,
3591  &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
3592  0, 0, 528);
3593 
3594  for (unsigned i = 0, offset = 0; i < 12; i++) {
3595  static VLCElem vlc_buf[6498];
3596 
3600  STUDIO_INTRA_BITS, 24,
3601  &ff_mpeg4_studio_intra[i][0][1], 2,
3602  &ff_mpeg4_studio_intra[i][0][0], 2, 1,
3605  }
3606 
3608  ff_rl_init(&ff_rvlc_rl_inter, mpeg4_rvlc_rl_tables[0]);
3609  ff_rl_init(&ff_rvlc_rl_intra, mpeg4_rvlc_rl_tables[1]);
3613  INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
3614  &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3615  &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
3616  INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
3617  &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3618  &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
3621  NULL, 0, 0, 0, 0, 128);
3623  &ff_mb_type_b_tab[0][1], 2, 1,
3624  &ff_mb_type_b_tab[0][0], 2, 1, 16);
3625 }
3626 
3628 {
3629  static AVOnce init_static_once = AV_ONCE_INIT;
3630  Mpeg4DecContext *ctx = avctx->priv_data;
3631  MpegEncContext *s = &ctx->m;
3632  int ret;
3633 
3634  ctx->divx_version =
3635  ctx->divx_build =
3636  ctx->xvid_build =
3637  ctx->lavc_build = -1;
3638 
3639  if ((ret = ff_h263_decode_init(avctx)) < 0)
3640  return ret;
3641 
3642  s->h263_pred = 1;
3643  s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
3644  s->decode_mb = mpeg4_decode_mb;
3645  ctx->time_increment_bits = 4; /* default value for broken headers */
3646 
3648 
3649  ff_thread_once(&init_static_once, mpeg4_init_static);
3650 
3651  return 0;
3652 }
3653 
3654 #define OFFSET(x) offsetof(MpegEncContext, x)
3655 #define FLAGS AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY
3656 static const AVOption mpeg4_options[] = {
3657  {"quarter_sample", "1/4 subpel MC", OFFSET(quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
3658  {"divx_packed", "divx style packed b frames", OFFSET(divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
3659  {NULL}
3660 };
3661 
3662 static const AVClass mpeg4_class = {
3663  .class_name = "MPEG4 Video Decoder",
3664  .item_name = av_default_item_name,
3665  .option = mpeg4_options,
3666  .version = LIBAVUTIL_VERSION_INT,
3667 };
3668 
3670  .p.name = "mpeg4",
3671  CODEC_LONG_NAME("MPEG-4 part 2"),
3672  .p.type = AVMEDIA_TYPE_VIDEO,
3673  .p.id = AV_CODEC_ID_MPEG4,
3674  .priv_data_size = sizeof(Mpeg4DecContext),
3675  .init = decode_init,
3676  .close = ff_h263_decode_end,
3678  .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
3679 #if FF_API_FLAG_TRUNCATED
3680  AV_CODEC_CAP_TRUNCATED |
3681 #endif
3683  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
3685  .flush = ff_mpeg_flush,
3686  .p.max_lowres = 3,
3687  .p.pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
3689  UPDATE_THREAD_CONTEXT(mpeg4_update_thread_context),
3690  UPDATE_THREAD_CONTEXT_FOR_USER(mpeg4_update_thread_context_for_user),
3691  .p.priv_class = &mpeg4_class,
3692  .hw_configs = (const AVCodecHWConfigInternal *const []) {
3693 #if CONFIG_MPEG4_NVDEC_HWACCEL
3694  HWACCEL_NVDEC(mpeg4),
3695 #endif
3696 #if CONFIG_MPEG4_VAAPI_HWACCEL
3697  HWACCEL_VAAPI(mpeg4),
3698 #endif
3699 #if CONFIG_MPEG4_VDPAU_HWACCEL
3700  HWACCEL_VDPAU(mpeg4),
3701 #endif
3702 #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
3703  HWACCEL_VIDEOTOOLBOX(mpeg4),
3704 #endif
3705  NULL
3706  },
3707 };
SIMPLE_VO_TYPE
#define SIMPLE_VO_TYPE
Definition: mpeg4videodefs.h:32
hwconfig.h
VOT_VIDEO_ID
#define VOT_VIDEO_ID
Definition: mpeg4videodefs.h:42
IS_8X8
#define IS_8X8(a)
Definition: mpegutils.h:82
MB_TYPE_L0
#define MB_TYPE_L0
Definition: mpegutils.h:60
FF_BUG_DC_CLIP
#define FF_BUG_DC_CLIP
Definition: avcodec.h:1292
ff_mpeg4_video_profiles
const AVProfile ff_mpeg4_video_profiles[]
Definition: profiles.c:111
MV_TYPE_16X16
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:255
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
mpeg4_class
static const AVClass mpeg4_class
Definition: mpeg4videodec.c:3662
h263data.h
FF_ASPECT_EXTENDED
#define FF_ASPECT_EXTENDED
Definition: h263.h:26
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
level
uint8_t level
Definition: svq3.c:204
av_clip
#define av_clip
Definition: common.h:95
show_bits_long
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:602
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
IS_ACPRED
#define IS_ACPRED(a)
Definition: mpegutils.h:87
r
const char * r
Definition: vf_curves.c:116
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_xvid_idct_init
av_cold void ff_xvid_idct_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: xvididct.c:333
mpeg4_block_count
static const uint8_t mpeg4_block_count[4]
Definition: mpeg4videodec.c:66
AV_EF_COMPLIANT
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
Definition: defs.h:55
ff_sprite_trajectory_lens
const uint8_t ff_sprite_trajectory_lens[15]
Definition: mpeg4data.h:325
decode_vop_header
static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb, int parse_only)
Definition: mpeg4videodec.c:2927
thread.h
mpeg4videodec.h
INIT_VLC_STATIC
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: vlc.h:125
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
studio_chroma_dc
static VLC studio_chroma_dc
Definition: mpeg4videodec.c:64
MB_TYPE_16x8
#define MB_TYPE_16x8
Definition: mpegutils.h:48
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
MAX_RUN
#define MAX_RUN
Definition: rl.h:35
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
ac_state_tab
static const uint8_t ac_state_tab[22][2]
Definition: mpeg4videodec.c:1868
GetBitContext::size_in_bits
int size_in_bits
Definition: get_bits.h:68
MV_DIRECT
#define MV_DIRECT
bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4)
Definition: mpegvideo.h:253
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
w
uint8_t w
Definition: llviddspenc.c:38
FF_BUG_EDGE
#define FF_BUG_EDGE
Definition: avcodec.h:1290
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:630
STATIC_SPRITE
#define STATIC_SPRITE
Definition: mpeg4videodefs.h:49
mpeg4_decode_studio_mb
static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
Definition: mpeg4videodec.c:2092
VOS_STARTCODE
#define VOS_STARTCODE
Definition: mpeg4videodefs.h:55
ff_mpeg4_decode_studio_slice_header
int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
Decode the next video packet.
Definition: mpeg4videodec.c:618
OFFSET
#define OFFSET(x)
Definition: mpeg4videodec.c:3654
AVOption
AVOption.
Definition: opt.h:251
mpeg4_decode_gop_header
static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
Definition: mpeg4videodec.c:2140
CORE_STUDIO_VO_TYPE
#define CORE_STUDIO_VO_TYPE
Definition: mpeg4videodefs.h:39
MB_TYPE_16x16
#define MB_TYPE_16x16
Definition: mpegutils.h:47
decode_studiovisualobject
static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
Definition: mpeg4videodec.c:3284
FFCodec
Definition: codec_internal.h:119
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
ff_clean_intra_table_entries
void ff_clean_intra_table_entries(MpegEncContext *s)
Clean dc, ac, coded_block for the current non-intra MB.
Definition: mpegvideo.c:1267
ff_er_add_slice
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
Definition: error_resilience.c:822
ff_init_block_index
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:1569
max
#define max(a, b)
Definition: cuda_runtime.h:33
mpegvideo.h
MB_TYPE_L1
#define MB_TYPE_L1
Definition: mpegutils.h:61
UPDATE_CACHE
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:178
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
FF_BUG_HPEL_CHROMA
#define FF_BUG_HPEL_CHROMA
Definition: avcodec.h:1291
ER_DC_END
#define ER_DC_END
Definition: error_resilience.h:35
decode_smpte_tc
static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
Definition: mpeg4videodec.c:3210
mpegutils.h
FF_PROFILE_MPEG4_SIMPLE_STUDIO
#define FF_PROFILE_MPEG4_SIMPLE_STUDIO
Definition: avcodec.h:1624
SLICE_STARTCODE
#define SLICE_STARTCODE
Definition: mpeg4videodefs.h:60
FF_IDCT_AUTO
#define FF_IDCT_AUTO
Definition: avcodec.h:1421
mpeg4_decode_visual_object
static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
Definition: mpeg4videodec.c:2176
ff_mpeg4_init_rl_intra
av_cold void ff_mpeg4_init_rl_intra(void)
Definition: mpeg4video.c:36
ER_MV_ERROR
#define ER_MV_ERROR
Definition: error_resilience.h:33
ff_thread_await_progress
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before ff_thread_await_progress() has been called on them. reget_buffer() and buffer age optimizations no longer work. *The contents of buffers must not be written to after ff_thread_report_progress() has been called on them. This includes draw_edges(). Porting codecs to frame threading
ff_mpeg4_resync_prefix
const uint16_t ff_mpeg4_resync_prefix[8]
Definition: mpeg4data.h:365
FF_DEBUG_PICT_INFO
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1328
MV_DIR_BACKWARD
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:252
FF_IDCT_XVID
#define FF_IDCT_XVID
Definition: avcodec.h:1428
hsub
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:74
init
static int init
Definition: av_tx.c:47
GET_CACHE
#define GET_CACHE(name, gb)
Definition: get_bits.h:215
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
ff_mpeg4_frame_end
int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: mpeg4videodec.c:3470
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
rgb
Definition: rpzaenc.c:59
studio_intra_tab
static VLC studio_intra_tab[12]
Definition: mpeg4videodec.c:62
ff_mpeg4_DCtab_chrom
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
Definition: mpeg4data.h:40
decode_user_data
static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode the user data stuff in the header.
Definition: mpeg4videodec.c:2752
SKIP_CACHE
#define SKIP_CACHE(name, gb, num)
Definition: get_bits.h:180
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
get_amv
static int get_amv(Mpeg4DecContext *ctx, int n)
Get the average motion vector for a GMC MB.
Definition: mpeg4videodec.c:660
ff_h263_pred_motion
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:190
MB_TYPE_B_VLC_BITS
#define MB_TYPE_B_VLC_BITS
Definition: mpeg4videodec.c:56
STUDIO_INTRA_BITS
#define STUDIO_INTRA_BITS
Definition: mpeg4videodec.c:57
FF_BUG_QPEL_CHROMA2
#define FF_BUG_QPEL_CHROMA2
Definition: avcodec.h:1288
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:447
ff_mpeg4_pred_ac
void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
Predict the ac.
Definition: mpeg4videodec.c:143
ff_rvlc_rl_intra
RLTable ff_rvlc_rl_intra
Definition: mpeg4data.h:317
ff_h263_pixel_aspect
const AVRational ff_h263_pixel_aspect[16]
Definition: h263data.c:273
VOP_STARTCODE
#define VOP_STARTCODE
Definition: mpeg4videodefs.h:59
RLTable
RLTable.
Definition: rl.h:39
ff_mpeg4_get_video_packet_prefix_length
int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s)
Definition: mpeg4video.c:42
GetBitContext
Definition: get_bits.h:61
AV_EF_BITSTREAM
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition: defs.h:49
ff_rvlc_rl_inter
RLTable ff_rvlc_rl_inter
Definition: mpeg4data.h:213
USES_LIST
#define USES_LIST(a, list)
Definition: mpegutils.h:92
mpeg4_decode_profile_level
static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
Definition: mpeg4videodec.c:2162
HWACCEL_VDPAU
#define HWACCEL_VDPAU(codec)
Definition: hwconfig.h:75
AV_CODEC_FLAG_LOW_DELAY
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:280
pts
static int64_t pts
Definition: transcode_aac.c:654
VOT_STILL_TEXTURE_ID
#define VOT_STILL_TEXTURE_ID
Definition: mpeg4videodefs.h:43
SLICE_END
#define SLICE_END
end marker found
Definition: mpegvideo.h:481
DC_VLC_BITS
#define DC_VLC_BITS
Definition: mpeg4videodec.c:55
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:434
ff_h263_decode_motion
int ff_h263_decode_motion(MpegEncContext *s, int pred, int f_code)
Definition: ituh263dec.c:273
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:179
mpegvideodec.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
INIT_FIRST_VLC_RL
#define INIT_FIRST_VLC_RL(rl, static_size)
Definition: rl.h:83
ER_DC_ERROR
#define ER_DC_ERROR
Definition: error_resilience.h:32
h263dec.h
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
ff_mpeg4_decode_partitions
int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
Decode the first and second partition.
Definition: mpeg4videodec.c:1049
extension_and_user_data
static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
Definition: mpeg4videodec.c:2274
MB_TYPE_ACPRED
#define MB_TYPE_ACPRED
Definition: mpegutils.h:53
ff_mpeg4_set_direct_mv
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my)
Definition: mpeg4video.c:129
CLOSE_READER
#define CLOSE_READER(name, gb)
Definition: get_bits.h:149
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:279
RLTable::max_level
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:46
s
#define s(width, name)
Definition: cbs_vp9.c:256
CHROMA_422
#define CHROMA_422
Definition: mpegvideo.h:458
s1
#define s1
Definition: regdef.h:38
SHOW_SBITS
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:212
ff_h263_inter_MCBPC_vlc
VLC ff_h263_inter_MCBPC_vlc
Definition: ituh263dec.c:103
FF_BUG_NO_PADDING
#define FF_BUG_NO_PADDING
Definition: avcodec.h:1284
ff_mpeg4_rl_intra
RLTable ff_mpeg4_rl_intra
Definition: mpeg4data.h:108
vlc_buf
static VLCElem vlc_buf[16716]
Definition: clearvideo.c:87
bits
uint8_t bits
Definition: vp3data.h:141
AV_EF_IGNORE_ERR
#define AV_EF_IGNORE_ERR
ignore errors and continue
Definition: defs.h:53
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1551
dc_lum
static VLC dc_lum
Definition: mpeg4videodec.c:59
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_mpeg4_workaround_bugs
int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
Definition: mpeg4videodec.c:2809
IS_SKIP
#define IS_SKIP(a)
Definition: mpegutils.h:74
mpeg4_decode_mb
static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
Definition: mpeg4videodec.c:1474
DC_MARKER
#define DC_MARKER
Definition: mpeg4videodefs.h:53
SKIP_BITS
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:193
IS_INTRA
#define IS_INTRA(x, y)
MpegEncContext::divx_packed
int divx_packed
Definition: mpegvideo.h:397
xvididct.h
RSHIFT
#define RSHIFT(a, b)
Definition: common.h:46
FF_BUG_DIRECT_BLOCKSIZE
#define FF_BUG_DIRECT_BLOCKSIZE
Definition: avcodec.h:1289
ff_rl_init
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Initialize index_run, max_level and max_run from n, last, table_vlc, table_run and table_level.
Definition: rl.c:27
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
mpeg4_decode_sprite_trajectory
static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
Definition: mpeg4videodec.c:248
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
if
if(ret)
Definition: filter_design.txt:179
ff_mpeg4_DCtab_lum
const uint8_t ff_mpeg4_DCtab_lum[13][2]
Definition: mpeg4data.h:34
FRAME_SKIPPED
#define FRAME_SKIPPED
Return value for header parsers if frame is not coded.
Definition: mpegutils.h:33
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:113
threadframe.h
mb_type_b_map
static const int mb_type_b_map[4]
Definition: mpeg4videodec.c:68
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:177
mpeg4_load_default_matrices
static void mpeg4_load_default_matrices(MpegEncContext *s)
Definition: mpeg4videodec.c:2208
FF_BUG_UMP4
#define FF_BUG_UMP4
Definition: avcodec.h:1283
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
run
uint8_t run
Definition: svq3.c:203
GMC_SPRITE
#define GMC_SPRITE
Definition: mpeg4videodefs.h:50
FF_BUG_STD_QPEL
#define FF_BUG_STD_QPEL
Definition: avcodec.h:1287
ff_mpv_idct_init
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:331
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:651
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
INIT_VLC_STATIC_FROM_LENGTHS
#define INIT_VLC_STATIC_FROM_LENGTHS(vlc, bits, nb_codes, lens, len_wrap, symbols, symbols_wrap, symbols_size, offset, flags, static_size)
Definition: vlc.h:131
mpeg4_decode_partitioned_mb
static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
decode partition C of one MB.
Definition: mpeg4videodec.c:1386
MB_TYPE_GMC
#define MB_TYPE_GMC
Definition: mpegutils.h:54
mpeg4_decode_studio_block
static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n)
Definition: mpeg4videodec.c:1894
SLICE_NOEND
#define SLICE_NOEND
no end marker or error found but mb count exceeded
Definition: mpegvideo.h:482
MB_TYPE_8x8
#define MB_TYPE_8x8
Definition: mpegutils.h:50
ROUNDED_DIV
#define ROUNDED_DIV(a, b)
Definition: common.h:48
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
profiles.h
ff_init_vlc_from_lengths
int ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: vlc.c:328
ff_set_qscale
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:1605
ff_h263_decode_frame
int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict, int *got_frame, AVPacket *avpkt)
Definition: h263dec.c:430
LAST_SKIP_BITS
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:199
FF_BUG_IEDGE
#define FF_BUG_IEDGE
Definition: avcodec.h:1295
MOTION_MARKER
#define MOTION_MARKER
Definition: mpeg4videodefs.h:52
lowres
static int lowres
Definition: ffplay.c:335
SET_QPEL_FUNC
#define SET_QPEL_FUNC(postfix1, postfix2)
Mpeg4DecContext
Definition: mpeg4videodec.h:32
UPDATE_THREAD_CONTEXT
#define UPDATE_THREAD_CONTEXT(func)
Definition: codec_internal.h:273
ff_alternate_horizontal_scan
const uint8_t ff_alternate_horizontal_scan[64]
Definition: mpegvideodata.c:84
FASTDIV
#define FASTDIV(a, b)
Definition: mathops.h:205
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:432
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:787
VISUAL_OBJ_STARTCODE
#define VISUAL_OBJ_STARTCODE
Definition: mpeg4videodefs.h:58
ff_mpeg4_dc_threshold
const uint8_t ff_mpeg4_dc_threshold[8]
Definition: mpeg4data.h:369
AVOnce
#define AVOnce
Definition: thread.h:176
ff_h263_decode_end
av_cold int ff_h263_decode_end(AVCodecContext *avctx)
Definition: h263dec.c:161
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
MV_TYPE_8X8
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
Definition: mpegvideo.h:256
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
ADV_SIMPLE_VO_TYPE
#define ADV_SIMPLE_VO_TYPE
Definition: mpeg4videodefs.h:40
MAX_LEVEL
#define MAX_LEVEL
Definition: rl.h:36
VLC::table_allocated
int table_allocated
Definition: vlc.h:34
mpeg4_decode_partition_a
static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
Decode first partition.
Definition: mpeg4videodec.c:760
check_marker
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
Definition: mpegvideodec.h:72
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
ff_h263_rl_inter
RLTable ff_h263_rl_inter
Definition: h263data.c:159
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
ff_mpeg4_y_dc_scale_table
const uint8_t ff_mpeg4_y_dc_scale_table[32]
Definition: mpeg4data.h:356
RL_VLC_ELEM
Definition: vlc.h:37
codec_internal.h
ff_mpeg4_pred_dc
static int ff_mpeg4_pred_dc(MpegEncContext *s, int n, int level, int *dir_ptr, int encoding)
Predict the dc.
Definition: mpeg4video.h:52
shift
static int shift(int a, int b)
Definition: bonk.c:260
ff_update_block_index
static void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, int lowres, int chroma_x_shift)
Definition: mpegvideo.h:602
mpeg4_decode_block
static int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block, int n, int coded, int intra, int use_intra_dc_vlc, int rvlc)
Decode a block.
Definition: mpeg4videodec.c:1114
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
FF_DEBUG_BUGS
#define FF_DEBUG_BUGS
Definition: avcodec.h:1338
ff_mpeg4_default_intra_matrix
const int16_t ff_mpeg4_default_intra_matrix[64]
Definition: mpeg4data.h:334
VLCElem
Definition: vlc.h:27
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: codec_internal.h:69
read_quant_matrix_ext
static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
Definition: mpeg4videodec.c:2225
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
CBPY_VLC_BITS
#define CBPY_VLC_BITS
Definition: h263dec.h:33
FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: codec_internal.h:54
MB_TYPE_SKIP
#define MB_TYPE_SKIP
Definition: mpegutils.h:55
EXT_STARTCODE
#define EXT_STARTCODE
Definition: mpeg4videodefs.h:61
AVCodecHWConfigInternal
Definition: hwconfig.h:29
MpegEncContext::quarter_sample
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:386
header
static const uint8_t header[24]
Definition: sdr2.c:67
MB_TYPE_INTERLACED
#define MB_TYPE_INTERLACED
Definition: mpegutils.h:51
OPEN_READER
#define OPEN_READER(name, gb)
Definition: get_bits.h:138
ff_mpeg_flush
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo_dec.c:532
height
#define height
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
ff_mpeg4_default_non_intra_matrix
const int16_t ff_mpeg4_default_non_intra_matrix[64]
Definition: mpeg4data.h:345
mpegvideodata.h
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
MV_TYPE_FIELD
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:258
get_xbits
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
Definition: get_bits.h:321
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
HWACCEL_NVDEC
#define HWACCEL_NVDEC(codec)
Definition: hwconfig.h:71
IS_DIRECT
#define IS_DIRECT(a)
Definition: mpegutils.h:77
unary.h
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
BIN_ONLY_SHAPE
#define BIN_ONLY_SHAPE
Definition: mpeg4videodefs.h:29
MB_TYPE_L0L1
#define MB_TYPE_L0L1
Definition: mpegutils.h:62
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
mpeg4_decode_dc
static int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
Decode the dc value.
Definition: mpeg4videodec.c:713
SKIP_COUNTER
#define SKIP_COUNTER(name, gb, num)
Definition: get_bits.h:185
CHROMA_420
#define CHROMA_420
Definition: mpegvideo.h:457
RECT_SHAPE
#define RECT_SHAPE
Definition: mpeg4videodefs.h:27
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
ff_h263_hwaccel_pixfmt_list_420
enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[]
Definition: h263dec.c:717
ff_alternate_vertical_scan
const uint8_t ff_alternate_vertical_scan[64]
Definition: mpegvideodata.c:95
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
ff_mpeg4_decode_picture_header
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, int header, int parse_only)
Decode MPEG-4 headers.
Definition: mpeg4videodec.c:3312
ff_init_scantable
av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:30
RLTable::max_run
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
internal.h
studio_luma_dc
static VLC studio_luma_dc
Definition: mpeg4videodec.c:63
ff_mpeg4_decode_video_packet_header
int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
Decode the next video packet.
Definition: mpeg4videodec.c:517
ff_h263_intra_MCBPC_vlc
VLC ff_h263_intra_MCBPC_vlc
Definition: ituh263dec.c:102
INTRA_MCBPC_VLC_BITS
#define INTRA_MCBPC_VLC_BITS
Definition: h263dec.h:31
FF_BUG_AUTODETECT
#define FF_BUG_AUTODETECT
autodetection
Definition: avcodec.h:1281
delta
float delta
Definition: vorbis_enc_data.h:430
av_fast_padded_malloc
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:49
FF_DEBUG_STARTCODE
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:1335
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodecContext::idct_algo
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1420
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
ff_h263_decode_init
av_cold int ff_h263_decode_init(AVCodecContext *avctx)
Definition: h263dec.c:78
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:989
len
int len
Definition: vorbis_enc_data.h:426
profile
int profile
Definition: mxfenc.c:2006
FF_BUG_QPEL_CHROMA
#define FF_BUG_QPEL_CHROMA
Definition: avcodec.h:1286
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:613
HWACCEL_VIDEOTOOLBOX
#define HWACCEL_VIDEOTOOLBOX(codec)
Definition: hwconfig.h:77
idctdsp.h
SPRITE_TRAJ_VLC_BITS
#define SPRITE_TRAJ_VLC_BITS
Definition: mpeg4videodec.c:54
stride
#define stride
Definition: h264pred_template.c:537
ff_mpeg4_studio_dc_luma
const uint8_t ff_mpeg4_studio_dc_luma[19][2]
Definition: mpeg4data.h:374
GET_RL_VLC
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition: get_bits.h:728
FLAGS
#define FLAGS
Definition: mpeg4videodec.c:3655
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
av_uninit
#define av_uninit(x)
Definition: attributes.h:154
mpeg4videodefs.h
ret
ret
Definition: filter_design.txt:187
SLICE_OK
#define SLICE_OK
Definition: mpegvideo.h:479
AV_EF_AGGRESSIVE
#define AV_EF_AGGRESSIVE
consider things that a sane encoder/muxer should not do as an error
Definition: defs.h:56
INIT_VLC_STATIC_OVERLONG
#define INIT_VLC_STATIC_OVERLONG
Definition: vlc.h:101
mpeg4_init_static
static av_cold void mpeg4_init_static(void)
Definition: mpeg4videodec.c:3580
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
GRAY_SHAPE
#define GRAY_SHAPE
Definition: mpeg4videodefs.h:30
ff_mpeg4_studio_dc_chroma
const uint8_t ff_mpeg4_studio_dc_chroma[19][2]
Definition: mpeg4data.h:381
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:683
TEX_VLC_BITS
#define TEX_VLC_BITS
Definition: dvdec.c:146
dc_chrom
static VLC dc_chrom
Definition: mpeg4videodec.c:59
pos
unsigned int pos
Definition: spdifenc.c:412
ff_mpeg4_init_direct_mv
void ff_mpeg4_init_direct_mv(MpegEncContext *s)
Definition: mpeg4video.c:83
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
ff_mpeg_update_thread_context
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: mpegvideo_dec.c:51
ff_mpeg4_decode_studio
void ff_mpeg4_decode_studio(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int block_size, int uvlinesize, int dct_linesize, int dct_offset)
Definition: mpeg4videodec.c:75
AVCodecContext
main external API structure.
Definition: avcodec.h:398
mpeg4_decode_partition_b
static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
decode second partition.
Definition: mpeg4videodec.c:955
SHOW_UBITS
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:211
ff_mb_type_b_tab
const uint8_t ff_mb_type_b_tab[4][2]
Definition: mpeg4data.h:329
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
mpeg4video.h
error_resilience.h
VLC
Definition: vlc.h:31
mpeg4_options
static const AVOption mpeg4_options[]
Definition: mpeg4videodec.c:3656
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:82
decode_studio_vol_header
static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Definition: mpeg4videodec.c:2291
VLC::table
VLCElem * table
Definition: vlc.h:33
USER_DATA_STARTCODE
#define USER_DATA_STARTCODE
Definition: mpeg4videodefs.h:56
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
ff_mpeg4_decoder
const FFCodec ff_mpeg4_decoder
Definition: mpeg4videodec.c:3669
decode_vol_header
static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Definition: mpeg4videodec.c:2377
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1327
mpeg4_is_resync
static int mpeg4_is_resync(Mpeg4DecContext *ctx)
check if the next stuff is a resync marker or the end.
Definition: mpeg4videodec.c:198
VLC::table_size
int table_size
Definition: vlc.h:34
INIT_VLC_RL
#define INIT_VLC_RL(rl, static_size)
Definition: rl.h:73
mpeg4_decode_dpcm_macroblock
static int mpeg4_decode_dpcm_macroblock(MpegEncContext *s, int16_t macroblock[256], int n)
Definition: mpeg4videodec.c:2009
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
get_bitsz
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:415
overflow
Undefined Behavior In the C some operations are like signed integer overflow
Definition: undefined.txt:3
FF_BUG_XVID_ILACE
#define FF_BUG_XVID_ILACE
Definition: avcodec.h:1282
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
reset_studio_dc_predictors
static void reset_studio_dc_predictors(MpegEncContext *s)
Definition: mpeg4videodec.c:606
ff_mpeg4_c_dc_scale_table
const uint8_t ff_mpeg4_c_dc_scale_table[32]
Definition: mpeg4data.h:360
ER_MV_END
#define ER_MV_END
Definition: error_resilience.h:36
next_start_code_studio
static void next_start_code_studio(GetBitContext *gb)
Definition: mpeg4videodec.c:1858
ff_tlog
#define ff_tlog(ctx,...)
Definition: internal.h:162
MV_DIR_FORWARD
#define MV_DIR_FORWARD
Definition: mpegvideo.h:251
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
ff_h263_cbpy_vlc
VLC ff_h263_cbpy_vlc
Definition: ituh263dec.c:104
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
decode_studio_vop_header
static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode the next studio vop header.
Definition: mpeg4videodec.c:3229
mpeg4videodata.h
mb_type_b_vlc
static VLC mb_type_b_vlc
Definition: mpeg4videodec.c:61
mpeg_get_qscale
static int mpeg_get_qscale(MpegEncContext *s)
Definition: mpegvideodec.h:63
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
GOP_STARTCODE
#define GOP_STARTCODE
Definition: mpeg4videodefs.h:57
HWACCEL_VAAPI
#define HWACCEL_VAAPI(codec)
Definition: hwconfig.h:73
d
d
Definition: ffmpeg_filter.c:156
int32_t
int32_t
Definition: audioconvert.c:56
MB_TYPE_DIRECT2
#define MB_TYPE_DIRECT2
Definition: mpegutils.h:52
rgb
static const SheerTable rgb[2]
Definition: sheervideodata.h:32
AV_CODEC_CAP_DRAW_HORIZ_BAND
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: codec.h:44
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
UPDATE_THREAD_CONTEXT_FOR_USER
#define UPDATE_THREAD_CONTEXT_FOR_USER(func)
Definition: codec_internal.h:275
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
decode_new_pred
static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb)
Definition: mpeg4videodec.c:501
QUANT_MATRIX_EXT_ID
#define QUANT_MATRIX_EXT_ID
Definition: mpeg4videodefs.h:63
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: mpeg4videodec.c:3627
FF_BUG_AMV
#define FF_BUG_AMV
Definition: avcodec.h:1285
sprite_trajectory
static VLC sprite_trajectory
Definition: mpeg4videodec.c:60
AV_PICTURE_TYPE_S
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition: avutil.h:277
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:62
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
RLTable::rl_vlc
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
IS_3IV1
#define IS_3IV1
Definition: mpeg4video.h:42
MB_TYPE_INTRA
#define MB_TYPE_INTRA
Definition: mpegutils.h:66
INTER_MCBPC_VLC_BITS
#define INTER_MCBPC_VLC_BITS
Definition: h263dec.h:32
re
float re
Definition: fft.c:79
SIMPLE_STUDIO_VO_TYPE
#define SIMPLE_STUDIO_VO_TYPE
Definition: mpeg4videodefs.h:38
h263.h
ff_mpeg4_studio_intra
const uint8_t ff_mpeg4_studio_intra[12][24][2]
Definition: mpeg4data.h:388
min
float min
Definition: vorbis_enc_data.h:429
rl_vlc
static VLC rl_vlc[2]
Definition: mobiclip.c:277