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