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