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