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