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 "threadprogress.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 int16_t 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->cur_pic.qscale_table;
320 
321  /* find prediction */
322  ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
323  ac_val1 = ac_val;
324  if (s->ac_pred) {
325  if (dir == 0) {
326  const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
327  /* left prediction */
328  ac_val -= 16;
329 
330  if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
331  n == 1 || n == 3) {
332  /* same qscale */
333  for (i = 1; i < 8; i++)
334  block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
335  } else {
336  /* different qscale, we must rescale */
337  for (i = 1; i < 8; i++)
338  block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
339  }
340  } else {
341  const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
342  /* top prediction */
343  ac_val -= 16 * s->block_wrap[n];
344 
345  if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
346  n == 2 || n == 3) {
347  /* same qscale */
348  for (i = 1; i < 8; i++)
349  block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
350  } else {
351  /* different qscale, we must rescale */
352  for (i = 1; i < 8; i++)
353  block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
354  }
355  }
356  }
357  /* left copy */
358  for (i = 1; i < 8; i++)
359  ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
360 
361  /* top copy */
362  for (i = 1; i < 8; i++)
363  ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
364 }
365 
366 /**
367  * check if the next stuff is a resync marker or the end.
368  * @return 0 if not
369  */
371 {
372  MpegEncContext *s = &ctx->m;
373  int bits_count = get_bits_count(&s->gb);
374  int v = show_bits(&s->gb, 16);
375 
376  if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
377  return 0;
378 
379  while (v <= 0xFF) {
380  if (s->pict_type == AV_PICTURE_TYPE_B ||
381  (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
382  break;
383  skip_bits(&s->gb, 8 + s->pict_type);
384  bits_count += 8 + s->pict_type;
385  v = show_bits(&s->gb, 16);
386  }
387 
388  if (bits_count + 8 >= s->gb.size_in_bits) {
389  v >>= 8;
390  v |= 0x7F >> (7 - (bits_count & 7));
391 
392  if (v == 0x7F)
393  return s->mb_num;
394  } else {
395  static const uint16_t mpeg4_resync_prefix[8] = {
396  0x7F00, 0x7E00, 0x7C00, 0x7800, 0x7000, 0x6000, 0x4000, 0x0000
397  };
398 
399  if (v == mpeg4_resync_prefix[bits_count & 7]) {
400  int len, mb_num;
401  int mb_num_bits = av_log2(s->mb_num - 1) + 1;
402  GetBitContext gb = s->gb;
403 
404  skip_bits(&s->gb, 1);
405  align_get_bits(&s->gb);
406 
407  for (len = 0; len < 32; len++)
408  if (get_bits1(&s->gb))
409  break;
410 
411  mb_num = get_bits(&s->gb, mb_num_bits);
412  if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
413  mb_num= -1;
414 
415  s->gb = gb;
416 
418  return mb_num;
419  }
420  }
421  return 0;
422 }
423 
425 {
426  MpegEncContext *s = &ctx->m;
427  int a = 2 << ctx->sprite_warping_accuracy;
428  int rho = 3 - ctx->sprite_warping_accuracy;
429  int r = 16 / a;
430  int alpha = 1;
431  int beta = 0;
432  int w = s->width;
433  int h = s->height;
434  int min_ab, i, w2, h2, w3, h3;
435  int sprite_ref[4][2];
436  int virtual_ref[2][2];
437  int64_t sprite_offset[2][2];
438  int64_t sprite_delta[2][2];
439 
440  // only true for rectangle shapes
441  const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
442  { 0, s->height }, { s->width, s->height } };
443  int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
444 
445  if (w <= 0 || h <= 0)
446  return AVERROR_INVALIDDATA;
447 
448  for (i = 0; i < ctx->num_sprite_warping_points; i++) {
449  int length;
450  int x = 0, y = 0;
451 
453  if (length > 0)
454  x = get_xbits(gb, length);
455 
456  if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
457  check_marker(s->avctx, gb, "before sprite_trajectory");
458 
460  if (length > 0)
461  y = get_xbits(gb, length);
462 
463  check_marker(s->avctx, gb, "after sprite_trajectory");
464  ctx->sprite_traj[i][0] = d[i][0] = x;
465  ctx->sprite_traj[i][1] = d[i][1] = y;
466  }
467  for (; i < 4; i++)
468  ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
469 
470  while ((1 << alpha) < w)
471  alpha++;
472  while ((1 << beta) < h)
473  beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
474  w2 = 1 << alpha;
475  h2 = 1 << beta;
476 
477  // Note, the 4th point isn't used for GMC
478  if (ctx->divx_version == 500 && ctx->divx_build == 413) {
479  sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
480  sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
481  sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
482  sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
483  sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
484  sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
485  } else {
486  sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
487  sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
488  sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
489  sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
490  sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
491  sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
492  }
493  /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
494  * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
495 
496  /* This is mostly identical to the MPEG-4 std (and is totally unreadable
497  * because of that...). Perhaps it should be reordered to be more readable.
498  * The idea behind this virtual_ref mess is to be able to use shifts later
499  * per pixel instead of divides so the distance between points is converted
500  * from w&h based to w2&h2 based which are of the 2^x form. */
501  virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
502  ROUNDED_DIV(((w - w2) *
503  (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
504  w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
505  virtual_ref[0][1] = 16 * vop_ref[0][1] +
506  ROUNDED_DIV(((w - w2) *
507  (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
508  w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
509  virtual_ref[1][0] = 16 * vop_ref[0][0] +
510  ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
511  h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
512  virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
513  ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
514  h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
515 
516  switch (ctx->num_sprite_warping_points) {
517  case 0:
518  sprite_offset[0][0] =
519  sprite_offset[0][1] =
520  sprite_offset[1][0] =
521  sprite_offset[1][1] = 0;
522  sprite_delta[0][0] = a;
523  sprite_delta[0][1] =
524  sprite_delta[1][0] = 0;
525  sprite_delta[1][1] = a;
526  ctx->sprite_shift[0] =
527  ctx->sprite_shift[1] = 0;
528  break;
529  case 1: // GMC only
530  sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
531  sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
532  sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
533  a * (vop_ref[0][0] / 2);
534  sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
535  a * (vop_ref[0][1] / 2);
536  sprite_delta[0][0] = a;
537  sprite_delta[0][1] =
538  sprite_delta[1][0] = 0;
539  sprite_delta[1][1] = a;
540  ctx->sprite_shift[0] =
541  ctx->sprite_shift[1] = 0;
542  break;
543  case 2:
544  sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
545  ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
546  ((int64_t) -vop_ref[0][0]) +
547  ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
548  ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
549  sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
550  ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
551  ((int64_t) -vop_ref[0][0]) +
552  ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
553  ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
554  sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
555  ((int64_t)-2 * vop_ref[0][0] + 1) +
556  ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
557  ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
558  (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
559  sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
560  ((int64_t)-2 * vop_ref[0][0] + 1) +
561  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
562  ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
563  (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
564  sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
565  sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
566  sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
567  sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
568 
569  ctx->sprite_shift[0] = alpha + rho;
570  ctx->sprite_shift[1] = alpha + rho + 2;
571  break;
572  case 3:
573  min_ab = FFMIN(alpha, beta);
574  w3 = w2 >> min_ab;
575  h3 = h2 >> min_ab;
576  sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
577  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
578  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
579  ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
580  sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
581  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
582  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
583  ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
584  sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
585  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
586  (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
587  ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
588  sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
589  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
590  (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
591  ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
592  sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
593  sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
594  sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
595  sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
596 
597  ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
598  ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
599  break;
600  default:
601  av_assert0(0);
602  }
603  /* try to simplify the situation */
604  if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
605  sprite_delta[0][1] == 0 &&
606  sprite_delta[1][0] == 0 &&
607  sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
608  sprite_offset[0][0] >>= ctx->sprite_shift[0];
609  sprite_offset[0][1] >>= ctx->sprite_shift[0];
610  sprite_offset[1][0] >>= ctx->sprite_shift[1];
611  sprite_offset[1][1] >>= ctx->sprite_shift[1];
612  sprite_delta[0][0] = a;
613  sprite_delta[0][1] = 0;
614  sprite_delta[1][0] = 0;
615  sprite_delta[1][1] = a;
616  ctx->sprite_shift[0] = 0;
617  ctx->sprite_shift[1] = 0;
618  ctx->real_sprite_warping_points = 1;
619  } else {
620  int shift_y = 16 - ctx->sprite_shift[0];
621  int shift_c = 16 - ctx->sprite_shift[1];
622 
623  for (i = 0; i < 2; i++) {
624  if (shift_c < 0 || shift_y < 0 ||
625  FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
626  FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
627  FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
628  FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
629  ) {
630  avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
631  goto overflow;
632  }
633  }
634 
635  for (i = 0; i < 2; i++) {
636  sprite_offset[0][i] *= 1 << shift_y;
637  sprite_offset[1][i] *= 1 << shift_c;
638  sprite_delta[0][i] *= 1 << shift_y;
639  sprite_delta[1][i] *= 1 << shift_y;
640  ctx->sprite_shift[i] = 16;
641 
642  }
643  for (i = 0; i < 2; i++) {
644  int64_t sd[2] = {
645  sprite_delta[i][0] - a * (1LL<<16),
646  sprite_delta[i][1] - a * (1LL<<16)
647  };
648 
649  if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
650  llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
651  llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
652  llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
653  llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
654  llabs(sd[0]) >= INT_MAX ||
655  llabs(sd[1]) >= INT_MAX ||
656  llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
657  llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
658  llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
659  ) {
660  avpriv_request_sample(s->avctx, "Overflow on sprite points");
661  goto overflow;
662  }
663  }
664  ctx->real_sprite_warping_points = ctx->num_sprite_warping_points;
665  }
666 
667  for (i = 0; i < 4; i++) {
668  ctx->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
669  ctx->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
670  }
671 
672  return 0;
673 overflow:
674  memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset));
675  memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta));
676  return AVERROR_PATCHWELCOME;
677 }
678 
680  MpegEncContext *s = &ctx->m;
681  int len = FFMIN(ctx->time_increment_bits + 3, 15);
682 
683  get_bits(gb, len);
684  if (get_bits1(gb))
685  get_bits(gb, len);
686  check_marker(s->avctx, gb, "after new_pred");
687 
688  return 0;
689 }
690 
691 /**
692  * Decode the next video packet.
693  * @return <0 if something went wrong
694  */
696 {
697  MpegEncContext *s = &ctx->m;
698 
699  int mb_num_bits = av_log2(s->mb_num - 1) + 1;
700  int header_extension = 0, mb_num, len;
701 
702  /* is there enough space left for a video packet + header */
703  if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
704  return AVERROR_INVALIDDATA;
705 
706  for (len = 0; len < 32; len++)
707  if (get_bits1(&s->gb))
708  break;
709 
711  av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
712  return AVERROR_INVALIDDATA;
713  }
714 
715  if (ctx->shape != RECT_SHAPE) {
716  header_extension = get_bits1(&s->gb);
717  // FIXME more stuff here
718  }
719 
720  mb_num = get_bits(&s->gb, mb_num_bits);
721  if (mb_num >= s->mb_num || !mb_num) {
722  av_log(s->avctx, AV_LOG_ERROR,
723  "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
724  return AVERROR_INVALIDDATA;
725  }
726 
727  s->mb_x = mb_num % s->mb_width;
728  s->mb_y = mb_num / s->mb_width;
729 
730  if (ctx->shape != BIN_ONLY_SHAPE) {
731  int qscale = get_bits(&s->gb, ctx->quant_precision);
732  if (qscale)
733  s->chroma_qscale = s->qscale = qscale;
734  }
735 
736  if (ctx->shape == RECT_SHAPE)
737  header_extension = get_bits1(&s->gb);
738 
739  if (header_extension) {
740  while (get_bits1(&s->gb) != 0)
741  ;
742 
743  check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
744  skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
745  check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
746 
747  skip_bits(&s->gb, 2); /* vop coding type */
748  // FIXME not rect stuff here
749 
750  if (ctx->shape != BIN_ONLY_SHAPE) {
751  skip_bits(&s->gb, 3); /* intra dc vlc threshold */
752  // FIXME don't just ignore everything
753  if (s->pict_type == AV_PICTURE_TYPE_S &&
754  ctx->vol_sprite_usage == GMC_SPRITE) {
755  if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
756  return AVERROR_INVALIDDATA;
757  av_log(s->avctx, AV_LOG_ERROR, "untested\n");
758  }
759 
760  // FIXME reduced res stuff here
761 
762  if (s->pict_type != AV_PICTURE_TYPE_I) {
763  int f_code = get_bits(&s->gb, 3); /* fcode_for */
764  if (f_code == 0)
765  av_log(s->avctx, AV_LOG_ERROR,
766  "Error, video packet header damaged (f_code=0)\n");
767  }
768  if (s->pict_type == AV_PICTURE_TYPE_B) {
769  int b_code = get_bits(&s->gb, 3);
770  if (b_code == 0)
771  av_log(s->avctx, AV_LOG_ERROR,
772  "Error, video packet header damaged (b_code=0)\n");
773  }
774  }
775  }
776  if (ctx->new_pred)
777  decode_new_pred(ctx, &s->gb);
778 
779  return 0;
780 }
781 
783 {
784  /* Reset DC Predictors */
785  s->last_dc[0] =
786  s->last_dc[1] =
787  s->last_dc[2] = 1 << (s->avctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1);
788 }
789 
790 /**
791  * Decode the next video packet.
792  * @return <0 if something went wrong
793  */
795 {
796  MpegEncContext *s = &ctx->m;
797  GetBitContext *gb = &s->gb;
798  unsigned vlc_len;
799  uint16_t mb_num;
800 
801  if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_STARTCODE) {
802  vlc_len = av_log2(s->mb_width * s->mb_height) + 1;
803  mb_num = get_bits(gb, vlc_len);
804 
805  if (mb_num >= s->mb_num)
806  return AVERROR_INVALIDDATA;
807 
808  s->mb_x = mb_num % s->mb_width;
809  s->mb_y = mb_num / s->mb_width;
810 
811  if (ctx->shape != BIN_ONLY_SHAPE)
812  s->qscale = mpeg_get_qscale(s);
813 
814  if (get_bits1(gb)) { /* slice_extension_flag */
815  skip_bits1(gb); /* intra_slice */
816  skip_bits1(gb); /* slice_VOP_id_enable */
817  skip_bits(gb, 6); /* slice_VOP_id */
818  while (get_bits1(gb)) /* extra_bit_slice */
819  skip_bits(gb, 8); /* extra_information_slice */
820  }
821 
823  }
824  else {
825  return AVERROR_INVALIDDATA;
826  }
827 
828  return 0;
829 }
830 
831 /**
832  * Get the average motion vector for a GMC MB.
833  * @param n either 0 for the x component or 1 for y
834  * @return the average MV for a GMC MB
835  */
836 static inline int get_amv(Mpeg4DecContext *ctx, int n)
837 {
838  MpegEncContext *s = &ctx->m;
839  int x, y, mb_v, sum, dx, dy, shift;
840  int len = 1 << (s->f_code + 4);
841  const int a = ctx->sprite_warping_accuracy;
842 
843  if (s->workaround_bugs & FF_BUG_AMV)
844  len >>= s->quarter_sample;
845 
846  if (ctx->real_sprite_warping_points == 1) {
847  if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample)
848  sum = ctx->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
849  else
850  sum = RSHIFT(ctx->sprite_offset[0][n] * (1 << s->quarter_sample), a);
851  } else {
852  dx = ctx->sprite_delta[n][0];
853  dy = ctx->sprite_delta[n][1];
854  shift = ctx->sprite_shift[0];
855  if (n)
856  dy -= 1 << (shift + a + 1);
857  else
858  dx -= 1 << (shift + a + 1);
859  mb_v = ctx->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U;
860 
861  sum = 0;
862  for (y = 0; y < 16; y++) {
863  int v;
864 
865  v = mb_v + (unsigned)dy * y;
866  // FIXME optimize
867  for (x = 0; x < 16; x++) {
868  sum += v >> shift;
869  v += dx;
870  }
871  }
872  sum = RSHIFT(sum, a + 8 - s->quarter_sample);
873  }
874 
875  if (sum < -len)
876  sum = -len;
877  else if (sum >= len)
878  sum = len - 1;
879 
880  return sum;
881 }
882 
883 /**
884  * Decode the dc value.
885  * @param n block index (0-3 are luma, 4-5 are chroma)
886  * @param dir_ptr the prediction direction will be stored here
887  * @return the quantized dc
888  */
889 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
890 {
891  int level, code;
892 
893  if (n < 4)
894  code = get_vlc2(&s->gb, dc_lum, DC_VLC_BITS, 1);
895  else
896  code = get_vlc2(&s->gb, dc_chrom, DC_VLC_BITS, 1);
897 
898  if (code < 0) {
899  av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
900  return AVERROR_INVALIDDATA;
901  }
902 
903  if (code == 0) {
904  level = 0;
905  } else {
906  if (IS_3IV1) {
907  if (code == 1)
908  level = 2 * get_bits1(&s->gb) - 1;
909  else {
910  if (get_bits1(&s->gb))
911  level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
912  else
913  level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
914  }
915  } else {
916  level = get_xbits(&s->gb, code);
917  }
918 
919  if (code > 8) {
920  if (get_bits1(&s->gb) == 0) { /* marker */
921  if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
922  av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
923  return AVERROR_INVALIDDATA;
924  }
925  }
926  }
927  }
928 
929  return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
930 }
931 
932 /**
933  * Decode first partition.
934  * @return number of MBs decoded or <0 if an error occurred
935  */
937 {
938  MpegEncContext *s = &ctx->m;
939  int mb_num = 0;
940  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
941 
942  /* decode first partition */
943  s->first_slice_line = 1;
944  for (; s->mb_y < s->mb_height; s->mb_y++) {
946  for (; s->mb_x < s->mb_width; s->mb_x++) {
947  const int xy = s->mb_x + s->mb_y * s->mb_stride;
948  int cbpc;
949  int dir = 0;
950 
951  mb_num++;
952  ff_update_block_index(s, 8, s->avctx->lowres, 1);
953  if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
954  s->first_slice_line = 0;
955 
956  if (s->pict_type == AV_PICTURE_TYPE_I) {
957  int i;
958 
959  do {
960  if (show_bits(&s->gb, 19) == DC_MARKER)
961  return mb_num - 1;
962 
964  if (cbpc < 0) {
965  av_log(s->avctx, AV_LOG_ERROR,
966  "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
967  return AVERROR_INVALIDDATA;
968  }
969  } while (cbpc == 8);
970 
971  s->cbp_table[xy] = cbpc & 3;
972  s->cur_pic.mb_type[xy] = MB_TYPE_INTRA;
973  s->mb_intra = 1;
974 
975  if (cbpc & 4)
976  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
977 
978  s->cur_pic.qscale_table[xy] = s->qscale;
979 
980  s->mbintra_table[xy] = 1;
981  for (i = 0; i < 6; i++) {
982  int dc_pred_dir;
983  int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
984  if (dc < 0) {
985  av_log(s->avctx, AV_LOG_ERROR,
986  "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
987  return dc;
988  }
989  dir <<= 1;
990  if (dc_pred_dir)
991  dir |= 1;
992  }
993  s->pred_dir_table[xy] = dir;
994  } else { /* P/S_TYPE */
995  int mx, my, pred_x, pred_y, bits;
996  int16_t *const mot_val = s->cur_pic.motion_val[0][s->block_index[0]];
997  const int stride = s->b8_stride * 2;
998 
999 try_again:
1000  bits = show_bits(&s->gb, 17);
1001  if (bits == MOTION_MARKER)
1002  return mb_num - 1;
1003 
1004  skip_bits1(&s->gb);
1005  if (bits & 0x10000) {
1006  /* skip mb */
1007  if (s->pict_type == AV_PICTURE_TYPE_S &&
1008  ctx->vol_sprite_usage == GMC_SPRITE) {
1009  s->cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1010  MB_TYPE_16x16 |
1011  MB_TYPE_GMC |
1013  mx = get_amv(ctx, 0);
1014  my = get_amv(ctx, 1);
1015  } else {
1016  s->cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1017  MB_TYPE_16x16 |
1019  mx = my = 0;
1020  }
1021  mot_val[0] =
1022  mot_val[2] =
1023  mot_val[0 + stride] =
1024  mot_val[2 + stride] = mx;
1025  mot_val[1] =
1026  mot_val[3] =
1027  mot_val[1 + stride] =
1028  mot_val[3 + stride] = my;
1029 
1030  if (s->mbintra_table[xy])
1032  continue;
1033  }
1034 
1036  if (cbpc < 0) {
1037  av_log(s->avctx, AV_LOG_ERROR,
1038  "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
1039  return AVERROR_INVALIDDATA;
1040  }
1041  if (cbpc == 20)
1042  goto try_again;
1043 
1044  s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
1045 
1046  s->mb_intra = ((cbpc & 4) != 0);
1047 
1048  if (s->mb_intra) {
1049  s->cur_pic.mb_type[xy] = MB_TYPE_INTRA;
1050  s->mbintra_table[xy] = 1;
1051  mot_val[0] =
1052  mot_val[2] =
1053  mot_val[0 + stride] =
1054  mot_val[2 + stride] = 0;
1055  mot_val[1] =
1056  mot_val[3] =
1057  mot_val[1 + stride] =
1058  mot_val[3 + stride] = 0;
1059  } else {
1060  if (s->mbintra_table[xy])
1062 
1063  if (s->pict_type == AV_PICTURE_TYPE_S &&
1064  ctx->vol_sprite_usage == GMC_SPRITE &&
1065  (cbpc & 16) == 0)
1066  s->mcsel = get_bits1(&s->gb);
1067  else
1068  s->mcsel = 0;
1069 
1070  if ((cbpc & 16) == 0) {
1071  /* 16x16 motion prediction */
1072 
1073  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1074  if (!s->mcsel) {
1075  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1076  if (mx >= 0xffff)
1077  return AVERROR_INVALIDDATA;
1078 
1079  my = ff_h263_decode_motion(s, pred_y, s->f_code);
1080  if (my >= 0xffff)
1081  return AVERROR_INVALIDDATA;
1082  s->cur_pic.mb_type[xy] = MB_TYPE_16x16 |
1084  } else {
1085  mx = get_amv(ctx, 0);
1086  my = get_amv(ctx, 1);
1087  s->cur_pic.mb_type[xy] = MB_TYPE_16x16 |
1088  MB_TYPE_GMC |
1090  }
1091 
1092  mot_val[0] =
1093  mot_val[2] =
1094  mot_val[0 + stride] =
1095  mot_val[2 + stride] = mx;
1096  mot_val[1] =
1097  mot_val[3] =
1098  mot_val[1 + stride] =
1099  mot_val[3 + stride] = my;
1100  } else {
1101  int i;
1102  s->cur_pic.mb_type[xy] = MB_TYPE_8x8 |
1104  for (i = 0; i < 4; i++) {
1105  int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1106  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1107  if (mx >= 0xffff)
1108  return AVERROR_INVALIDDATA;
1109 
1110  my = ff_h263_decode_motion(s, pred_y, s->f_code);
1111  if (my >= 0xffff)
1112  return AVERROR_INVALIDDATA;
1113  mot_val[0] = mx;
1114  mot_val[1] = my;
1115  }
1116  }
1117  }
1118  }
1119  }
1120  s->mb_x = 0;
1121  }
1122 
1123  return mb_num;
1124 }
1125 
1126 /**
1127  * decode second partition.
1128  * @return <0 if an error occurred
1129  */
1130 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
1131 {
1132  int mb_num = 0;
1133  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1134 
1135  s->mb_x = s->resync_mb_x;
1136  s->first_slice_line = 1;
1137  for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
1139  for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
1140  const int xy = s->mb_x + s->mb_y * s->mb_stride;
1141 
1142  mb_num++;
1143  ff_update_block_index(s, 8, s->avctx->lowres, 1);
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->cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
1158  } else { /* P || S_TYPE */
1159  if (IS_INTRA(s->cur_pic.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->cur_pic.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->cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
1190  s->pred_dir_table[xy] = dir;
1191  } else if (IS_SKIP(s->cur_pic.mb_type[xy])) {
1192  s->cur_pic.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->cur_pic.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  const RLTable *rl;
1296  const RL_VLC_ELEM *rl_vlc;
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->cur_pic.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->cur_pic.qscale_table[xy] != s->qscale)
1574  ff_set_qscale(s, s->cur_pic.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->cur_pic.motion_val[0][s->block_index[i]][0];
1581  s->mv[0][i][1] = s->cur_pic.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  s->cur_pic.mbskip_table[xy] = 0;
1596  } else {
1597  s->mcsel = 0;
1598  s->mb_skipped = 1;
1599  s->cur_pic.mbskip_table[xy] = 1;
1600  }
1601  } else if (s->mb_intra) {
1602  s->ac_pred = IS_ACPRED(s->cur_pic.mb_type[xy]);
1603  } else if (!s->mb_intra) {
1604  // s->mcsel = 0; // FIXME do we need to init that?
1605 
1606  s->mv_dir = MV_DIR_FORWARD;
1607  if (IS_8X8(mb_type)) {
1608  s->mv_type = MV_TYPE_8X8;
1609  } else {
1610  s->mv_type = MV_TYPE_16X16;
1611  }
1612  }
1613  } else { /* I-Frame */
1614  s->mb_intra = 1;
1615  s->ac_pred = IS_ACPRED(s->cur_pic.mb_type[xy]);
1616  }
1617 
1618  if (!IS_SKIP(mb_type)) {
1619  int i;
1620  s->bdsp.clear_blocks(s->block[0]);
1621  /* decode each block */
1622  for (i = 0; i < 6; i++) {
1623  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra,
1624  use_intra_dc_vlc, ctx->rvlc) < 0) {
1625  av_log(s->avctx, AV_LOG_ERROR,
1626  "texture corrupted at %d %d %d\n",
1627  s->mb_x, s->mb_y, s->mb_intra);
1628  return AVERROR_INVALIDDATA;
1629  }
1630  cbp += cbp;
1631  }
1632  }
1633 
1634  /* per-MB end of slice check */
1635  if (--s->mb_num_left <= 0) {
1636  if (mpeg4_is_resync(ctx))
1637  return SLICE_END;
1638  else
1639  return SLICE_NOEND;
1640  } else {
1641  if (mpeg4_is_resync(ctx)) {
1642  const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1643  if (s->cbp_table[xy + delta])
1644  return SLICE_END;
1645  }
1646  return SLICE_OK;
1647  }
1648 }
1649 
1650 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1651 {
1652  Mpeg4DecContext *ctx = s->avctx->priv_data;
1653  int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1654  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1655  const int xy = s->mb_x + s->mb_y * s->mb_stride;
1656  int next;
1657 
1658  av_assert2(s == (void*)ctx);
1659  av_assert2(s->h263_pred);
1660 
1661  if (s->pict_type == AV_PICTURE_TYPE_P ||
1662  s->pict_type == AV_PICTURE_TYPE_S) {
1663  do {
1664  if (get_bits1(&s->gb)) {
1665  /* skip mb */
1666  s->mb_intra = 0;
1667  for (i = 0; i < 6; i++)
1668  s->block_last_index[i] = -1;
1669  s->mv_dir = MV_DIR_FORWARD;
1670  s->mv_type = MV_TYPE_16X16;
1671  if (s->pict_type == AV_PICTURE_TYPE_S &&
1672  ctx->vol_sprite_usage == GMC_SPRITE) {
1673  s->cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1674  MB_TYPE_GMC |
1675  MB_TYPE_16x16 |
1677  s->mcsel = 1;
1678  s->mv[0][0][0] = get_amv(ctx, 0);
1679  s->mv[0][0][1] = get_amv(ctx, 1);
1680  s->cur_pic.mbskip_table[xy] = 0;
1681  s->mb_skipped = 0;
1682  } else {
1683  s->cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 |
1685  s->mcsel = 0;
1686  s->mv[0][0][0] = 0;
1687  s->mv[0][0][1] = 0;
1688  s->cur_pic.mbskip_table[xy] = 1;
1689  s->mb_skipped = 1;
1690  }
1691  goto end;
1692  }
1694  if (cbpc < 0) {
1695  av_log(s->avctx, AV_LOG_ERROR,
1696  "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1697  return AVERROR_INVALIDDATA;
1698  }
1699  } while (cbpc == 20);
1700 
1701  s->bdsp.clear_blocks(s->block[0]);
1702  dquant = cbpc & 8;
1703  s->mb_intra = ((cbpc & 4) != 0);
1704  if (s->mb_intra)
1705  goto intra;
1706 
1707  if (s->pict_type == AV_PICTURE_TYPE_S &&
1708  ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1709  s->mcsel = get_bits1(&s->gb);
1710  else
1711  s->mcsel = 0;
1712  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1) ^ 0x0F;
1713  if (cbpy < 0) {
1714  av_log(s->avctx, AV_LOG_ERROR,
1715  "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1716  return AVERROR_INVALIDDATA;
1717  }
1718 
1719  cbp = (cbpc & 3) | (cbpy << 2);
1720  if (dquant)
1721  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1722  if ((!s->progressive_sequence) &&
1723  (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1724  s->interlaced_dct = get_bits1(&s->gb);
1725 
1726  s->mv_dir = MV_DIR_FORWARD;
1727  if ((cbpc & 16) == 0) {
1728  if (s->mcsel) {
1729  s->cur_pic.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 |
1731  /* 16x16 global motion prediction */
1732  s->mv_type = MV_TYPE_16X16;
1733  mx = get_amv(ctx, 0);
1734  my = get_amv(ctx, 1);
1735  s->mv[0][0][0] = mx;
1736  s->mv[0][0][1] = my;
1737  } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1738  s->cur_pic.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_FORWARD_MV |
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->cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
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->cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV;
1779  s->mv_type = MV_TYPE_8X8;
1780  for (i = 0; i < 4; i++) {
1781  int16_t *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_progress_await(&s->next_pic.ptr->progress, s->mb_y);
1812  }
1813 
1814  /* if we skipped it in the future P-frame than skip it now too */
1815  s->mb_skipped = s->next_pic.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->cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1829  MB_TYPE_16x16 |
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  if (modb2) {
1847  cbp = 0;
1848  } else {
1849  s->bdsp.clear_blocks(s->block[0]);
1850  cbp = get_bits(&s->gb, 6);
1851  }
1852 
1853  if ((!IS_DIRECT(mb_type)) && cbp) {
1854  if (get_bits1(&s->gb))
1855  ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1856  }
1857 
1858  if (!s->progressive_sequence) {
1859  if (cbp)
1860  s->interlaced_dct = get_bits1(&s->gb);
1861 
1862  if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1863  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1864  mb_type &= ~MB_TYPE_16x16;
1865 
1866  if (HAS_FORWARD_MV(mb_type)) {
1867  s->field_select[0][0] = get_bits1(&s->gb);
1868  s->field_select[0][1] = get_bits1(&s->gb);
1869  }
1870  if (HAS_BACKWARD_MV(mb_type)) {
1871  s->field_select[1][0] = get_bits1(&s->gb);
1872  s->field_select[1][1] = get_bits1(&s->gb);
1873  }
1874  }
1875  }
1876 
1877  s->mv_dir = 0;
1878  if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1879  s->mv_type = MV_TYPE_16X16;
1880 
1881  if (HAS_FORWARD_MV(mb_type)) {
1882  s->mv_dir = MV_DIR_FORWARD;
1883 
1884  mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1885  my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1886  s->last_mv[0][1][0] =
1887  s->last_mv[0][0][0] =
1888  s->mv[0][0][0] = mx;
1889  s->last_mv[0][1][1] =
1890  s->last_mv[0][0][1] =
1891  s->mv[0][0][1] = my;
1892  }
1893 
1894  if (HAS_BACKWARD_MV(mb_type)) {
1895  s->mv_dir |= MV_DIR_BACKWARD;
1896 
1897  mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1898  my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1899  s->last_mv[1][1][0] =
1900  s->last_mv[1][0][0] =
1901  s->mv[1][0][0] = mx;
1902  s->last_mv[1][1][1] =
1903  s->last_mv[1][0][1] =
1904  s->mv[1][0][1] = my;
1905  }
1906  } else if (!IS_DIRECT(mb_type)) {
1907  s->mv_type = MV_TYPE_FIELD;
1908 
1909  if (HAS_FORWARD_MV(mb_type)) {
1910  s->mv_dir = MV_DIR_FORWARD;
1911 
1912  for (i = 0; i < 2; i++) {
1913  mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1914  my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1915  s->last_mv[0][i][0] =
1916  s->mv[0][i][0] = mx;
1917  s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1918  }
1919  }
1920 
1921  if (HAS_BACKWARD_MV(mb_type)) {
1922  s->mv_dir |= MV_DIR_BACKWARD;
1923 
1924  for (i = 0; i < 2; i++) {
1925  mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1926  my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1927  s->last_mv[1][i][0] =
1928  s->mv[1][i][0] = mx;
1929  s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1930  }
1931  }
1932  }
1933  }
1934 
1935  if (IS_DIRECT(mb_type)) {
1936  if (IS_SKIP(mb_type)) {
1937  mx =
1938  my = 0;
1939  } else {
1940  mx = ff_h263_decode_motion(s, 0, 1);
1941  my = ff_h263_decode_motion(s, 0, 1);
1942  }
1943 
1944  s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1945  mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1946  }
1947  s->cur_pic.mb_type[xy] = mb_type;
1948  } else { /* I-Frame */
1949  int use_intra_dc_vlc;
1950 
1951  do {
1953  if (cbpc < 0) {
1954  av_log(s->avctx, AV_LOG_ERROR,
1955  "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1956  return AVERROR_INVALIDDATA;
1957  }
1958  } while (cbpc == 8);
1959 
1960  dquant = cbpc & 4;
1961  s->mb_intra = 1;
1962 
1963 intra:
1964  s->ac_pred = get_bits1(&s->gb);
1965  if (s->ac_pred)
1966  s->cur_pic.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1967  else
1968  s->cur_pic.mb_type[xy] = MB_TYPE_INTRA;
1969 
1970  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1971  if (cbpy < 0) {
1972  av_log(s->avctx, AV_LOG_ERROR,
1973  "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1974  return AVERROR_INVALIDDATA;
1975  }
1976  cbp = (cbpc & 3) | (cbpy << 2);
1977 
1978  use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1979 
1980  if (dquant)
1981  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1982 
1983  if (!s->progressive_sequence)
1984  s->interlaced_dct = get_bits1(&s->gb);
1985 
1986  s->bdsp.clear_blocks(s->block[0]);
1987  /* decode each block */
1988  for (i = 0; i < 6; i++) {
1989  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32,
1990  1, use_intra_dc_vlc, 0) < 0)
1991  return AVERROR_INVALIDDATA;
1992  cbp += cbp;
1993  }
1994  goto end;
1995  }
1996 
1997  /* decode each block */
1998  for (i = 0; i < 6; i++) {
1999  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0, 0) < 0)
2000  return AVERROR_INVALIDDATA;
2001  cbp += cbp;
2002  }
2003 
2004 end:
2005  /* per-MB end of slice check */
2006  next = mpeg4_is_resync(ctx);
2007  if (next) {
2008  if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
2009  return AVERROR_INVALIDDATA;
2010  } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
2011  return SLICE_END;
2012 
2013  if (s->pict_type == AV_PICTURE_TYPE_B) {
2014  const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
2015  ff_thread_progress_await(&s->next_pic.ptr->progress,
2016  (s->mb_x + delta >= s->mb_width)
2017  ? FFMIN(s->mb_y + 1, s->mb_height - 1)
2018  : s->mb_y);
2019  if (s->next_pic.mbskip_table[xy + delta])
2020  return SLICE_OK;
2021  }
2022 
2023  return SLICE_END;
2024  }
2025 
2026  return SLICE_OK;
2027 }
2028 
2029 /* As per spec, studio start code search isn't the same as the old type of start code */
2031 {
2032  align_get_bits(gb);
2033 
2034  while (get_bits_left(gb) >= 24 && show_bits(gb, 24) != 0x1) {
2035  get_bits(gb, 8);
2036  }
2037 }
2038 
2039 /* additional_code, vlc index */
2040 static const uint8_t ac_state_tab[22][2] =
2041 {
2042  {0, 0},
2043  {0, 1},
2044  {1, 1},
2045  {2, 1},
2046  {3, 1},
2047  {4, 1},
2048  {5, 1},
2049  {1, 2},
2050  {2, 2},
2051  {3, 2},
2052  {4, 2},
2053  {5, 2},
2054  {6, 2},
2055  {1, 3},
2056  {2, 4},
2057  {3, 5},
2058  {4, 6},
2059  {5, 7},
2060  {6, 8},
2061  {7, 9},
2062  {8, 10},
2063  {0, 11}
2064 };
2065 
2067 {
2068  Mpeg4DecContext *ctx = s->avctx->priv_data;
2069 
2070  int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
2071  additional_code_len, sign, mismatch;
2072  const VLCElem *cur_vlc = studio_intra_tab[0];
2073  const uint8_t *const scantable = s->intra_scantable.permutated;
2074  const uint16_t *quant_matrix;
2075  uint32_t flc;
2076  const int min = -1 * (1 << (s->avctx->bits_per_raw_sample + 6));
2077  const int max = ((1 << (s->avctx->bits_per_raw_sample + 6)) - 1);
2078  int shift = 3 - s->dct_precision;
2079 
2080  mismatch = 1;
2081 
2082  memset(block, 0, 64 * sizeof(int32_t));
2083 
2084  if (n < 4) {
2085  cc = 0;
2086  dct_dc_size = get_vlc2(&s->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2);
2087  quant_matrix = s->intra_matrix;
2088  } else {
2089  cc = (n & 1) + 1;
2090  if (ctx->rgb)
2091  dct_dc_size = get_vlc2(&s->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2);
2092  else
2093  dct_dc_size = get_vlc2(&s->gb, studio_chroma_dc, STUDIO_INTRA_BITS, 2);
2094  quant_matrix = s->chroma_intra_matrix;
2095  }
2096 
2097  if (dct_dc_size == 0) {
2098  dct_diff = 0;
2099  } else {
2100  dct_diff = get_xbits(&s->gb, dct_dc_size);
2101 
2102  if (dct_dc_size > 8) {
2103  if(!check_marker(s->avctx, &s->gb, "dct_dc_size > 8"))
2104  return AVERROR_INVALIDDATA;
2105  }
2106 
2107  }
2108 
2109  s->last_dc[cc] += dct_diff;
2110 
2111  if (s->mpeg_quant)
2112  block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision);
2113  else
2114  block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision) * (8 >> s->dct_precision);
2115  /* TODO: support mpeg_quant for AC coefficients */
2116 
2117  block[0] = av_clip(block[0], min, max);
2118  mismatch ^= block[0];
2119 
2120  /* AC Coefficients */
2121  while (1) {
2122  group = get_vlc2(&s->gb, cur_vlc, STUDIO_INTRA_BITS, 2);
2123 
2124  if (group < 0) {
2125  av_log(s->avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
2126  return AVERROR_INVALIDDATA;
2127  }
2128 
2129  additional_code_len = ac_state_tab[group][0];
2130  cur_vlc = studio_intra_tab[ac_state_tab[group][1]];
2131 
2132  if (group == 0) {
2133  /* End of Block */
2134  break;
2135  } else if (group >= 1 && group <= 6) {
2136  /* Zero run length (Table B.47) */
2137  run = 1 << additional_code_len;
2138  if (additional_code_len)
2139  run += get_bits(&s->gb, additional_code_len);
2140  idx += run;
2141  continue;
2142  } else if (group >= 7 && group <= 12) {
2143  /* Zero run length and +/-1 level (Table B.48) */
2144  code = get_bits(&s->gb, additional_code_len);
2145  sign = code & 1;
2146  code >>= 1;
2147  run = (1 << (additional_code_len - 1)) + code;
2148  idx += run;
2149  if (idx > 63)
2150  return AVERROR_INVALIDDATA;
2151  j = scantable[idx++];
2152  block[j] = sign ? 1 : -1;
2153  } else if (group >= 13 && group <= 20) {
2154  /* Level value (Table B.49) */
2155  if (idx > 63)
2156  return AVERROR_INVALIDDATA;
2157  j = scantable[idx++];
2158  block[j] = get_xbits(&s->gb, additional_code_len);
2159  } else if (group == 21) {
2160  /* Escape */
2161  if (idx > 63)
2162  return AVERROR_INVALIDDATA;
2163  j = scantable[idx++];
2164  additional_code_len = s->avctx->bits_per_raw_sample + s->dct_precision + 4;
2165  flc = get_bits(&s->gb, additional_code_len);
2166  if (flc >> (additional_code_len-1))
2167  block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
2168  else
2169  block[j] = flc;
2170  }
2171  block[j] = ((block[j] * quant_matrix[j] * s->qscale) * (1 << shift)) / 16;
2172  block[j] = av_clip(block[j], min, max);
2173  mismatch ^= block[j];
2174  }
2175 
2176  block[63] ^= mismatch & 1;
2177 
2178  return 0;
2179 }
2180 
2181 static int mpeg4_decode_dpcm_macroblock(MpegEncContext *s, int16_t macroblock[256], int n)
2182 {
2183  int i, j, w, h, idx = 0;
2184  int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code,
2185  dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output;
2186  h = 16 >> (n ? s->chroma_y_shift : 0);
2187  w = 16 >> (n ? s->chroma_x_shift : 0);
2188 
2189  block_mean = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
2190  if (block_mean == 0){
2191  av_log(s->avctx, AV_LOG_ERROR, "Forbidden block_mean\n");
2192  return AVERROR_INVALIDDATA;
2193  }
2194  s->last_dc[n] = block_mean * (1 << (s->dct_precision + s->intra_dc_precision));
2195 
2196  rice_parameter = get_bits(&s->gb, 4);
2197  if (rice_parameter == 0) {
2198  av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2199  return AVERROR_INVALIDDATA;
2200  }
2201 
2202  if (rice_parameter == 15)
2203  rice_parameter = 0;
2204 
2205  if (rice_parameter > 11) {
2206  av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2207  return AVERROR_INVALIDDATA;
2208  }
2209 
2210  for (i = 0; i < h; i++) {
2211  output = 1 << (s->avctx->bits_per_raw_sample - 1);
2212  top = 1 << (s->avctx->bits_per_raw_sample - 1);
2213 
2214  for (j = 0; j < w; j++) {
2215  left = output;
2216  topleft = top;
2217 
2218  rice_prefix_code = get_unary(&s->gb, 1, 12);
2219 
2220  /* Escape */
2221  if (rice_prefix_code == 11)
2222  dpcm_residual = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
2223  else {
2224  if (rice_prefix_code == 12) {
2225  av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n");
2226  return AVERROR_INVALIDDATA;
2227  }
2228  rice_suffix_code = get_bitsz(&s->gb, rice_parameter);
2229  dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code;
2230  }
2231 
2232  /* Map to a signed residual */
2233  if (dpcm_residual & 1)
2234  dpcm_residual = (-1 * dpcm_residual) >> 1;
2235  else
2236  dpcm_residual = (dpcm_residual >> 1);
2237 
2238  if (i != 0)
2239  top = macroblock[idx-w];
2240 
2241  p = left + top - topleft;
2242  min_left_top = FFMIN(left, top);
2243  if (p < min_left_top)
2244  p = min_left_top;
2245 
2246  max_left_top = FFMAX(left, top);
2247  if (p > max_left_top)
2248  p = max_left_top;
2249 
2250  p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1;
2251  if (p2 == p)
2252  p2 = block_mean;
2253 
2254  if (p2 > p)
2255  dpcm_residual *= -1;
2256 
2257  macroblock[idx++] = output = (dpcm_residual + p) & ((1 << s->avctx->bits_per_raw_sample) - 1);
2258  }
2259  }
2260 
2261  return 0;
2262 }
2263 
2264 static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
2265 {
2266  Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s;
2267  int i;
2268 
2269  ctx->dpcm_direction = 0;
2270 
2271  /* StudioMacroblock */
2272  /* Assumes I-VOP */
2273  s->mb_intra = 1;
2274  if (get_bits1(&s->gb)) { /* compression_mode */
2275  /* DCT */
2276  /* macroblock_type, 1 or 2-bit VLC */
2277  if (!get_bits1(&s->gb)) {
2278  skip_bits1(&s->gb);
2279  s->qscale = mpeg_get_qscale(s);
2280  }
2281 
2282  for (i = 0; i < mpeg4_block_count[s->chroma_format]; i++) {
2283  if (mpeg4_decode_studio_block(s, ctx->block32[i], i) < 0)
2284  return AVERROR_INVALIDDATA;
2285  }
2286  } else {
2287  /* DPCM */
2288  check_marker(s->avctx, &s->gb, "DPCM block start");
2289  ctx->dpcm_direction = get_bits1(&s->gb) ? -1 : 1;
2290  for (i = 0; i < 3; i++) {
2291  if (mpeg4_decode_dpcm_macroblock(s, ctx->dpcm_macroblock[i], i) < 0)
2292  return AVERROR_INVALIDDATA;
2293  }
2294  }
2295 
2296  if (get_bits_left(&s->gb) >= 24 && show_bits(&s->gb, 23) == 0) {
2297  next_start_code_studio(&s->gb);
2298  return SLICE_END;
2299  }
2300 
2301  //vcon-stp9L1.bits (first frame)
2302  if (get_bits_left(&s->gb) == 0)
2303  return SLICE_END;
2304 
2305  //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame)
2306  if (get_bits_left(&s->gb) < 8U && show_bits(&s->gb, get_bits_left(&s->gb)) == 0)
2307  return SLICE_END;
2308 
2309  return SLICE_OK;
2310 }
2311 
2313 {
2314  int hours, minutes, seconds;
2315 
2316  if (!show_bits(gb, 23)) {
2317  av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
2318  return AVERROR_INVALIDDATA;
2319  }
2320 
2321  hours = get_bits(gb, 5);
2322  minutes = get_bits(gb, 6);
2323  check_marker(s->avctx, gb, "in gop_header");
2324  seconds = get_bits(gb, 6);
2325 
2326  s->time_base = seconds + 60*(minutes + 60*hours);
2327 
2328  skip_bits1(gb);
2329  skip_bits1(gb);
2330 
2331  return 0;
2332 }
2333 
2335 {
2336 
2337  *profile = get_bits(gb, 4);
2338  *level = get_bits(gb, 4);
2339 
2340  // for Simple profile, level 0
2341  if (*profile == 0 && *level == 8) {
2342  *level = 0;
2343  }
2344 
2345  return 0;
2346 }
2347 
2349 {
2350  int visual_object_type;
2351  int is_visual_object_identifier = get_bits1(gb);
2352 
2353  if (is_visual_object_identifier) {
2354  skip_bits(gb, 4+3);
2355  }
2356  visual_object_type = get_bits(gb, 4);
2357 
2358  if (visual_object_type == VOT_VIDEO_ID ||
2359  visual_object_type == VOT_STILL_TEXTURE_ID) {
2360  int video_signal_type = get_bits1(gb);
2361  if (video_signal_type) {
2362  int video_range, color_description;
2363  skip_bits(gb, 3); // video_format
2364  video_range = get_bits1(gb);
2365  color_description = get_bits1(gb);
2366 
2367  s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2368 
2369  if (color_description) {
2370  s->avctx->color_primaries = get_bits(gb, 8);
2371  s->avctx->color_trc = get_bits(gb, 8);
2372  s->avctx->colorspace = get_bits(gb, 8);
2373  }
2374  }
2375  }
2376 
2377  return 0;
2378 }
2379 
2381 {
2382  int i, v;
2383 
2384  /* load default matrices */
2385  for (i = 0; i < 64; i++) {
2386  int j = s->idsp.idct_permutation[i];
2388  s->intra_matrix[j] = v;
2389  s->chroma_intra_matrix[j] = v;
2390 
2392  s->inter_matrix[j] = v;
2393  s->chroma_inter_matrix[j] = v;
2394  }
2395 }
2396 
2398 {
2399  int i, j, v;
2400 
2401  if (get_bits1(gb)) {
2402  if (get_bits_left(gb) < 64*8)
2403  return AVERROR_INVALIDDATA;
2404  /* intra_quantiser_matrix */
2405  for (i = 0; i < 64; i++) {
2406  v = get_bits(gb, 8);
2407  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2408  s->intra_matrix[j] = v;
2409  s->chroma_intra_matrix[j] = v;
2410  }
2411  }
2412 
2413  if (get_bits1(gb)) {
2414  if (get_bits_left(gb) < 64*8)
2415  return AVERROR_INVALIDDATA;
2416  /* non_intra_quantiser_matrix */
2417  for (i = 0; i < 64; i++) {
2418  get_bits(gb, 8);
2419  }
2420  }
2421 
2422  if (get_bits1(gb)) {
2423  if (get_bits_left(gb) < 64*8)
2424  return AVERROR_INVALIDDATA;
2425  /* chroma_intra_quantiser_matrix */
2426  for (i = 0; i < 64; i++) {
2427  v = get_bits(gb, 8);
2428  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2429  s->chroma_intra_matrix[j] = v;
2430  }
2431  }
2432 
2433  if (get_bits1(gb)) {
2434  if (get_bits_left(gb) < 64*8)
2435  return AVERROR_INVALIDDATA;
2436  /* chroma_non_intra_quantiser_matrix */
2437  for (i = 0; i < 64; i++) {
2438  get_bits(gb, 8);
2439  }
2440  }
2441 
2443  return 0;
2444 }
2445 
2447 {
2448  uint32_t startcode;
2449  uint8_t extension_type;
2450 
2451  startcode = show_bits_long(gb, 32);
2452  if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
2453 
2454  if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2455  skip_bits_long(gb, 32);
2456  extension_type = get_bits(gb, 4);
2457  if (extension_type == QUANT_MATRIX_EXT_ID)
2458  read_quant_matrix_ext(s, gb);
2459  }
2460  }
2461 }
2462 
2464 {
2465  MpegEncContext *s = &ctx->m;
2466  int width, height, aspect_ratio_info;
2467  int bits_per_raw_sample;
2468  int rgb, chroma_format;
2469 
2470  // random_accessible_vol and video_object_type_indication have already
2471  // been read by the caller decode_vol_header()
2472  skip_bits(gb, 4); /* video_object_layer_verid */
2473  ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
2474  skip_bits(gb, 4); /* video_object_layer_shape_extension */
2475  skip_bits1(gb); /* progressive_sequence */
2476  if (ctx->shape != RECT_SHAPE) {
2477  avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape");
2478  return AVERROR_PATCHWELCOME;
2479  }
2480  if (ctx->shape != BIN_ONLY_SHAPE) {
2481  rgb = get_bits1(gb); /* rgb_components */
2482  chroma_format = get_bits(gb, 2); /* chroma_format */
2483  if (!chroma_format || chroma_format == CHROMA_420 || (rgb && chroma_format == CHROMA_422)) {
2484  av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2485  return AVERROR_INVALIDDATA;
2486  }
2487 
2488  bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
2489  if (bits_per_raw_sample == 10) {
2490  if (rgb) {
2491  s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
2492  } else {
2493  s->avctx->pix_fmt = chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
2494  }
2495  } else {
2496  avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
2497  return AVERROR_PATCHWELCOME;
2498  }
2499  if (rgb != ctx->rgb || s->chroma_format != chroma_format)
2500  s->context_reinit = 1;
2501  s->avctx->bits_per_raw_sample = bits_per_raw_sample;
2502  ctx->rgb = rgb;
2503  s->chroma_format = chroma_format;
2504  }
2505  if (ctx->shape == RECT_SHAPE) {
2506  check_marker(s->avctx, gb, "before video_object_layer_width");
2507  width = get_bits(gb, 14); /* video_object_layer_width */
2508  check_marker(s->avctx, gb, "before video_object_layer_height");
2509  height = get_bits(gb, 14); /* video_object_layer_height */
2510  check_marker(s->avctx, gb, "after video_object_layer_height");
2511 
2512  /* Do the same check as non-studio profile */
2513  if (width && height) {
2514  if (s->width && s->height &&
2515  (s->width != width || s->height != height))
2516  s->context_reinit = 1;
2517  s->width = width;
2518  s->height = height;
2519  }
2520  }
2521  aspect_ratio_info = get_bits(gb, 4);
2522  if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2523  s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2524  s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2525  } else {
2526  s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2527  }
2528  skip_bits(gb, 4); /* frame_rate_code */
2529  skip_bits(gb, 15); /* first_half_bit_rate */
2530  check_marker(s->avctx, gb, "after first_half_bit_rate");
2531  skip_bits(gb, 15); /* latter_half_bit_rate */
2532  check_marker(s->avctx, gb, "after latter_half_bit_rate");
2533  skip_bits(gb, 15); /* first_half_vbv_buffer_size */
2534  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2535  skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
2536  skip_bits(gb, 11); /* first_half_vbv_buffer_size */
2537  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2538  skip_bits(gb, 15); /* latter_half_vbv_occupancy */
2539  check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2540  s->low_delay = get_bits1(gb);
2541  s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
2542 
2544  extension_and_user_data(s, gb, 2);
2545 
2546  return 0;
2547 }
2548 
2550 {
2551  MpegEncContext *s = &ctx->m;
2552  int width, height, vo_ver_id, aspect_ratio_info;
2553 
2554  /* vol header */
2555  skip_bits(gb, 1); /* random access */
2556  ctx->vo_type = get_bits(gb, 8);
2557 
2558  /* If we are in studio profile (per vo_type), check if its all consistent
2559  * and if so continue pass control to decode_studio_vol_header().
2560  * elIf something is inconsistent, error out
2561  * else continue with (non studio) vol header decpoding.
2562  */
2563  if (ctx->vo_type == CORE_STUDIO_VO_TYPE ||
2564  ctx->vo_type == SIMPLE_STUDIO_VO_TYPE) {
2565  if (s->avctx->profile != AV_PROFILE_UNKNOWN && s->avctx->profile != AV_PROFILE_MPEG4_SIMPLE_STUDIO)
2566  return AVERROR_INVALIDDATA;
2567  s->studio_profile = 1;
2568  s->avctx->profile = AV_PROFILE_MPEG4_SIMPLE_STUDIO;
2569  return decode_studio_vol_header(ctx, gb);
2570  } else if (s->studio_profile) {
2571  return AVERROR_PATCHWELCOME;
2572  }
2573 
2574  if (get_bits1(gb) != 0) { /* is_ol_id */
2575  vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
2576  skip_bits(gb, 3); /* vo_priority */
2577  } else {
2578  vo_ver_id = 1;
2579  }
2580  aspect_ratio_info = get_bits(gb, 4);
2581  if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2582  s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2583  s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2584  } else {
2585  s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2586  }
2587 
2588  if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2589  int chroma_format = get_bits(gb, 2);
2590  if (chroma_format != CHROMA_420)
2591  av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2592 
2593  s->low_delay = get_bits1(gb);
2594  if (get_bits1(gb)) { /* vbv parameters */
2595  get_bits(gb, 15); /* first_half_bitrate */
2596  check_marker(s->avctx, gb, "after first_half_bitrate");
2597  get_bits(gb, 15); /* latter_half_bitrate */
2598  check_marker(s->avctx, gb, "after latter_half_bitrate");
2599  get_bits(gb, 15); /* first_half_vbv_buffer_size */
2600  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2601  get_bits(gb, 3); /* latter_half_vbv_buffer_size */
2602  get_bits(gb, 11); /* first_half_vbv_occupancy */
2603  check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
2604  get_bits(gb, 15); /* latter_half_vbv_occupancy */
2605  check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2606  }
2607  } else {
2608  /* is setting low delay flag only once the smartest thing to do?
2609  * low delay detection will not be overridden. */
2610  if (s->picture_number == 0) {
2611  switch (ctx->vo_type) {
2612  case SIMPLE_VO_TYPE:
2613  case ADV_SIMPLE_VO_TYPE:
2614  s->low_delay = 1;
2615  break;
2616  default:
2617  s->low_delay = 0;
2618  }
2619  }
2620  }
2621 
2622  ctx->shape = get_bits(gb, 2); /* vol shape */
2623  if (ctx->shape != RECT_SHAPE)
2624  av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2625  if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2626  av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2627  skip_bits(gb, 4); /* video_object_layer_shape_extension */
2628  }
2629 
2630  check_marker(s->avctx, gb, "before time_increment_resolution");
2631 
2632  s->avctx->framerate.num = get_bits(gb, 16);
2633  if (!s->avctx->framerate.num) {
2634  av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
2635  return AVERROR_INVALIDDATA;
2636  }
2637 
2638  ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
2639  if (ctx->time_increment_bits < 1)
2640  ctx->time_increment_bits = 1;
2641 
2642  check_marker(s->avctx, gb, "before fixed_vop_rate");
2643 
2644  if (get_bits1(gb) != 0) /* fixed_vop_rate */
2645  s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
2646  else
2647  s->avctx->framerate.den = 1;
2648 
2649  ctx->t_frame = 0;
2650 
2651  if (ctx->shape != BIN_ONLY_SHAPE) {
2652  if (ctx->shape == RECT_SHAPE) {
2653  check_marker(s->avctx, gb, "before width");
2654  width = get_bits(gb, 13);
2655  check_marker(s->avctx, gb, "before height");
2656  height = get_bits(gb, 13);
2657  check_marker(s->avctx, gb, "after height");
2658  if (width && height && /* they should be non zero but who knows */
2659  !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
2660  if (s->width && s->height &&
2661  (s->width != width || s->height != height))
2662  s->context_reinit = 1;
2663  s->width = width;
2664  s->height = height;
2665  }
2666  }
2667 
2668  s->progressive_sequence =
2669  s->progressive_frame = get_bits1(gb) ^ 1;
2670  s->interlaced_dct = 0;
2671  if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
2672  av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
2673  "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2674  if (vo_ver_id == 1)
2675  ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
2676  else
2677  ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
2678 
2679  if (ctx->vol_sprite_usage == STATIC_SPRITE)
2680  av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2681  if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2682  ctx->vol_sprite_usage == GMC_SPRITE) {
2683  if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2684  skip_bits(gb, 13); // sprite_width
2685  check_marker(s->avctx, gb, "after sprite_width");
2686  skip_bits(gb, 13); // sprite_height
2687  check_marker(s->avctx, gb, "after sprite_height");
2688  skip_bits(gb, 13); // sprite_left
2689  check_marker(s->avctx, gb, "after sprite_left");
2690  skip_bits(gb, 13); // sprite_top
2691  check_marker(s->avctx, gb, "after sprite_top");
2692  }
2693  ctx->num_sprite_warping_points = get_bits(gb, 6);
2694  if (ctx->num_sprite_warping_points > 3) {
2695  av_log(s->avctx, AV_LOG_ERROR,
2696  "%d sprite_warping_points\n",
2697  ctx->num_sprite_warping_points);
2698  ctx->num_sprite_warping_points = 0;
2699  return AVERROR_INVALIDDATA;
2700  }
2701  ctx->sprite_warping_accuracy = get_bits(gb, 2);
2702  ctx->sprite_brightness_change = get_bits1(gb);
2703  if (ctx->vol_sprite_usage == STATIC_SPRITE)
2704  skip_bits1(gb); // low_latency_sprite
2705  }
2706  // FIXME sadct disable bit if verid!=1 && shape not rect
2707 
2708  if (get_bits1(gb) == 1) { /* not_8_bit */
2709  ctx->quant_precision = get_bits(gb, 4); /* quant_precision */
2710  if (get_bits(gb, 4) != 8) /* bits_per_pixel */
2711  av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
2712  if (ctx->quant_precision != 5)
2713  av_log(s->avctx, AV_LOG_ERROR,
2714  "quant precision %d\n", ctx->quant_precision);
2715  if (ctx->quant_precision < 3 || ctx->quant_precision > 9)
2716  ctx->quant_precision = 5;
2717  } else {
2718  ctx->quant_precision = 5;
2719  }
2720 
2721  // FIXME a bunch of grayscale shape things
2722 
2723  if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2724  int i, v;
2725 
2727 
2728  /* load custom intra matrix */
2729  if (get_bits1(gb)) {
2730  int last = 0;
2731  for (i = 0; i < 64; i++) {
2732  int j;
2733  if (get_bits_left(gb) < 8) {
2734  av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2735  return AVERROR_INVALIDDATA;
2736  }
2737  v = get_bits(gb, 8);
2738  if (v == 0)
2739  break;
2740 
2741  last = v;
2742  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2743  s->intra_matrix[j] = last;
2744  s->chroma_intra_matrix[j] = last;
2745  }
2746 
2747  /* replicate last value */
2748  for (; i < 64; i++) {
2749  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2750  s->intra_matrix[j] = last;
2751  s->chroma_intra_matrix[j] = last;
2752  }
2753  }
2754 
2755  /* load custom non intra matrix */
2756  if (get_bits1(gb)) {
2757  int last = 0;
2758  for (i = 0; i < 64; i++) {
2759  int j;
2760  if (get_bits_left(gb) < 8) {
2761  av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2762  return AVERROR_INVALIDDATA;
2763  }
2764  v = get_bits(gb, 8);
2765  if (v == 0)
2766  break;
2767 
2768  last = v;
2769  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2770  s->inter_matrix[j] = v;
2771  s->chroma_inter_matrix[j] = v;
2772  }
2773 
2774  /* replicate last value */
2775  for (; i < 64; i++) {
2776  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2777  s->inter_matrix[j] = last;
2778  s->chroma_inter_matrix[j] = last;
2779  }
2780  }
2781 
2782  // FIXME a bunch of grayscale shape things
2783  }
2784 
2785  if (vo_ver_id != 1)
2786  s->quarter_sample = get_bits1(gb);
2787  else
2788  s->quarter_sample = 0;
2789 
2790  if (get_bits_left(gb) < 4) {
2791  av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2792  return AVERROR_INVALIDDATA;
2793  }
2794 
2795  if (!get_bits1(gb)) {
2796  int pos = get_bits_count(gb);
2797  int estimation_method = get_bits(gb, 2);
2798  if (estimation_method < 2) {
2799  if (!get_bits1(gb)) {
2800  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2801  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2802  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2803  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2804  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2805  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2806  }
2807  if (!get_bits1(gb)) {
2808  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2809  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2810  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2811  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2812  }
2813  if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2814  skip_bits_long(gb, pos - get_bits_count(gb));
2815  goto no_cplx_est;
2816  }
2817  if (!get_bits1(gb)) {
2818  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2819  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2820  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2821  ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2822  }
2823  if (!get_bits1(gb)) {
2824  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2825  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2826  ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2827  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2828  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2829  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2830  }
2831  if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2832  skip_bits_long(gb, pos - get_bits_count(gb));
2833  goto no_cplx_est;
2834  }
2835  if (estimation_method == 1) {
2836  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2837  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2838  }
2839  } else
2840  av_log(s->avctx, AV_LOG_ERROR,
2841  "Invalid Complexity estimation method %d\n",
2842  estimation_method);
2843  } else {
2844 
2845 no_cplx_est:
2846  ctx->cplx_estimation_trash_i =
2847  ctx->cplx_estimation_trash_p =
2848  ctx->cplx_estimation_trash_b = 0;
2849  }
2850 
2851  ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2852 
2853  s->data_partitioning = get_bits1(gb);
2854  if (s->data_partitioning)
2855  ctx->rvlc = get_bits1(gb);
2856 
2857  if (vo_ver_id != 1) {
2858  ctx->new_pred = get_bits1(gb);
2859  if (ctx->new_pred) {
2860  av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2861  skip_bits(gb, 2); /* requested upstream message type */
2862  skip_bits1(gb); /* newpred segment type */
2863  }
2864  if (get_bits1(gb)) // reduced_res_vop
2865  av_log(s->avctx, AV_LOG_ERROR,
2866  "reduced resolution VOP not supported\n");
2867  } else {
2868  ctx->new_pred = 0;
2869  }
2870 
2871  ctx->scalability = get_bits1(gb);
2872 
2873  if (ctx->scalability) {
2874  GetBitContext bak = *gb;
2875  int h_sampling_factor_n;
2876  int h_sampling_factor_m;
2877  int v_sampling_factor_n;
2878  int v_sampling_factor_m;
2879 
2880  skip_bits1(gb); // hierarchy_type
2881  skip_bits(gb, 4); /* ref_layer_id */
2882  skip_bits1(gb); /* ref_layer_sampling_dir */
2883  h_sampling_factor_n = get_bits(gb, 5);
2884  h_sampling_factor_m = get_bits(gb, 5);
2885  v_sampling_factor_n = get_bits(gb, 5);
2886  v_sampling_factor_m = get_bits(gb, 5);
2887  ctx->enhancement_type = get_bits1(gb);
2888 
2889  if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2890  v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2891  /* illegal scalability header (VERY broken encoder),
2892  * trying to workaround */
2893  ctx->scalability = 0;
2894  *gb = bak;
2895  } else
2896  av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2897 
2898  // bin shape stuff FIXME
2899  }
2900  }
2901 
2902  if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2903  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",
2904  s->avctx->framerate.den, s->avctx->framerate.num,
2905  ctx->time_increment_bits,
2906  ctx->quant_precision,
2907  s->progressive_sequence,
2908  s->low_delay,
2909  ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2910  s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2911  );
2912  }
2913 
2914  return 0;
2915 }
2916 
2917 /**
2918  * Decode the user data stuff in the header.
2919  * Also initializes divx/xvid/lavc_version/build.
2920  */
2922 {
2923  MpegEncContext *s = &ctx->m;
2924  char buf[256];
2925  int i;
2926  int e;
2927  int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2928  char last;
2929 
2930  for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2931  if (show_bits(gb, 23) == 0)
2932  break;
2933  buf[i] = get_bits(gb, 8);
2934  }
2935  buf[i] = 0;
2936 
2937  /* divx detection */
2938  e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2939  if (e < 2)
2940  e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2941  if (e >= 2) {
2942  ctx->divx_version = ver;
2943  ctx->divx_build = build;
2944  s->divx_packed = e == 3 && last == 'p';
2945  }
2946 
2947  /* libavcodec detection */
2948  e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2949  if (e != 4)
2950  e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2951  if (e != 4) {
2952  e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2953  if (e > 1) {
2954  if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
2955  av_log(s->avctx, AV_LOG_WARNING,
2956  "Unknown Lavc version string encountered, %d.%d.%d; "
2957  "clamping sub-version values to 8-bits.\n",
2958  ver, ver2, ver3);
2959  }
2960  build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
2961  }
2962  }
2963  if (e != 4) {
2964  if (strcmp(buf, "ffmpeg") == 0)
2965  ctx->lavc_build = 4600;
2966  }
2967  if (e == 4)
2968  ctx->lavc_build = build;
2969 
2970  /* Xvid detection */
2971  e = sscanf(buf, "XviD%d", &build);
2972  if (e == 1)
2973  ctx->xvid_build = build;
2974 
2975  return 0;
2976 }
2977 
2979 {
2980  Mpeg4DecContext *ctx = avctx->priv_data;
2981  MpegEncContext *s = &ctx->m;
2982 
2983  if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2984  if (s->codec_tag == AV_RL32("XVID") ||
2985  s->codec_tag == AV_RL32("XVIX") ||
2986  s->codec_tag == AV_RL32("RMP4") ||
2987  s->codec_tag == AV_RL32("ZMP4") ||
2988  s->codec_tag == AV_RL32("SIPP"))
2989  ctx->xvid_build = 0;
2990  }
2991 
2992  if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2993  if (s->codec_tag == AV_RL32("DIVX") && ctx->vo_type == 0 &&
2994  ctx->vol_control_parameters == 0)
2995  ctx->divx_version = 400; // divx 4
2996 
2997  if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2998  ctx->divx_version =
2999  ctx->divx_build = -1;
3000  }
3001 
3002  if (s->workaround_bugs & FF_BUG_AUTODETECT) {
3003  if (s->codec_tag == AV_RL32("XVIX"))
3004  s->workaround_bugs |= FF_BUG_XVID_ILACE;
3005 
3006  if (s->codec_tag == AV_RL32("UMP4"))
3007  s->workaround_bugs |= FF_BUG_UMP4;
3008 
3009  if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
3010  s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
3011 
3012  if (ctx->divx_version > 502 && ctx->divx_build < 1814)
3013  s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
3014 
3015  if (ctx->xvid_build <= 3U)
3016  s->padding_bug_score = 256 * 256 * 256 * 64;
3017 
3018  if (ctx->xvid_build <= 1U)
3019  s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
3020 
3021  if (ctx->xvid_build <= 12U)
3022  s->workaround_bugs |= FF_BUG_EDGE;
3023 
3024  if (ctx->xvid_build <= 32U)
3025  s->workaround_bugs |= FF_BUG_DC_CLIP;
3026 
3027 #define SET_QPEL_FUNC(postfix1, postfix2) \
3028  s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
3029  s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
3030  s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
3031 
3032  if (ctx->lavc_build < 4653U)
3033  s->workaround_bugs |= FF_BUG_STD_QPEL;
3034 
3035  if (ctx->lavc_build < 4655U)
3036  s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
3037 
3038  if (ctx->lavc_build < 4670U)
3039  s->workaround_bugs |= FF_BUG_EDGE;
3040 
3041  if (ctx->lavc_build <= 4712U)
3042  s->workaround_bugs |= FF_BUG_DC_CLIP;
3043 
3044  if ((ctx->lavc_build&0xFF) >= 100) {
3045  if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
3046  (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
3047  )
3048  s->workaround_bugs |= FF_BUG_IEDGE;
3049  }
3050 
3051  if (ctx->divx_version >= 0)
3052  s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
3053  if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
3054  s->padding_bug_score = 256 * 256 * 256 * 64;
3055 
3056  if (ctx->divx_version < 500U)
3057  s->workaround_bugs |= FF_BUG_EDGE;
3058 
3059  if (ctx->divx_version >= 0)
3060  s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
3061  }
3062 
3063  if (s->workaround_bugs & FF_BUG_STD_QPEL) {
3064  SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
3065  SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
3066  SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
3067  SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
3068  SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
3069  SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
3070 
3071  SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
3072  SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
3073  SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
3074  SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
3075  SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
3076  SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
3077  }
3078 
3079  if (avctx->debug & FF_DEBUG_BUGS)
3080  av_log(s->avctx, AV_LOG_DEBUG,
3081  "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
3082  s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
3083  ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
3084 
3085  if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
3086  avctx->idct_algo == FF_IDCT_AUTO) {
3087  avctx->idct_algo = FF_IDCT_XVID;
3089  return 1;
3090  }
3091 
3092  return 0;
3093 }
3094 
3096  int parse_only)
3097 {
3098  MpegEncContext *s = &ctx->m;
3099  int time_incr, time_increment;
3100  int64_t pts;
3101 
3102  s->mcsel = 0;
3103  s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
3104  if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
3105  ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
3106  av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
3107  s->low_delay = 0;
3108  }
3109 
3110  s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
3111  if (s->partitioned_frame)
3112  s->decode_mb = mpeg4_decode_partitioned_mb;
3113  else
3114  s->decode_mb = mpeg4_decode_mb;
3115 
3116  time_incr = 0;
3117  while (get_bits1(gb) != 0)
3118  time_incr++;
3119 
3120  check_marker(s->avctx, gb, "before time_increment");
3121 
3122  if (ctx->time_increment_bits == 0 ||
3123  !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
3124  av_log(s->avctx, AV_LOG_WARNING,
3125  "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);
3126 
3127  for (ctx->time_increment_bits = 1;
3128  ctx->time_increment_bits < 16;
3129  ctx->time_increment_bits++) {
3130  if (s->pict_type == AV_PICTURE_TYPE_P ||
3131  (s->pict_type == AV_PICTURE_TYPE_S &&
3132  ctx->vol_sprite_usage == GMC_SPRITE)) {
3133  if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
3134  break;
3135  } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
3136  break;
3137  }
3138 
3139  av_log(s->avctx, AV_LOG_WARNING,
3140  "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
3141  }
3142 
3143  if (IS_3IV1)
3144  time_increment = get_bits1(gb); // FIXME investigate further
3145  else
3146  time_increment = get_bits(gb, ctx->time_increment_bits);
3147 
3148  if (s->pict_type != AV_PICTURE_TYPE_B) {
3149  s->last_time_base = s->time_base;
3150  s->time_base += time_incr;
3151  s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
3152  if (s->workaround_bugs & FF_BUG_UMP4) {
3153  if (s->time < s->last_non_b_time) {
3154  /* header is not mpeg-4-compatible, broken encoder,
3155  * trying to workaround */
3156  s->time_base++;
3157  s->time += s->avctx->framerate.num;
3158  }
3159  }
3160  s->pp_time = s->time - s->last_non_b_time;
3161  s->last_non_b_time = s->time;
3162  } else {
3163  s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
3164  s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
3165  if (s->pp_time <= s->pb_time ||
3166  s->pp_time <= s->pp_time - s->pb_time ||
3167  s->pp_time <= 0) {
3168  /* messed up order, maybe after seeking? skipping current B-frame */
3169  return FRAME_SKIPPED;
3170  }
3172 
3173  if (ctx->t_frame == 0)
3174  ctx->t_frame = s->pb_time;
3175  if (ctx->t_frame == 0)
3176  ctx->t_frame = 1; // 1/0 protection
3177  s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
3178  ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
3179  s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
3180  ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
3181  if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
3182  s->pb_field_time = 2;
3183  s->pp_field_time = 4;
3184  if (!s->progressive_sequence)
3185  return FRAME_SKIPPED;
3186  }
3187  }
3188 
3189  if (s->avctx->framerate.den)
3190  pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
3191  else
3192  pts = AV_NOPTS_VALUE;
3193  ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
3194 
3195  check_marker(s->avctx, gb, "before vop_coded");
3196 
3197  /* vop coded */
3198  if (get_bits1(gb) != 1) {
3199  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
3200  av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
3201  return FRAME_SKIPPED;
3202  }
3203  if (ctx->new_pred)
3204  decode_new_pred(ctx, gb);
3205 
3206  if (ctx->shape != BIN_ONLY_SHAPE &&
3207  (s->pict_type == AV_PICTURE_TYPE_P ||
3208  (s->pict_type == AV_PICTURE_TYPE_S &&
3209  ctx->vol_sprite_usage == GMC_SPRITE))) {
3210  /* rounding type for motion estimation */
3211  s->no_rounding = get_bits1(gb);
3212  } else {
3213  s->no_rounding = 0;
3214  }
3215  // FIXME reduced res stuff
3216 
3217  if (ctx->shape != RECT_SHAPE) {
3218  if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
3219  skip_bits(gb, 13); /* width */
3220  check_marker(s->avctx, gb, "after width");
3221  skip_bits(gb, 13); /* height */
3222  check_marker(s->avctx, gb, "after height");
3223  skip_bits(gb, 13); /* hor_spat_ref */
3224  check_marker(s->avctx, gb, "after hor_spat_ref");
3225  skip_bits(gb, 13); /* ver_spat_ref */
3226  }
3227  skip_bits1(gb); /* change_CR_disable */
3228 
3229  if (get_bits1(gb) != 0)
3230  skip_bits(gb, 8); /* constant_alpha_value */
3231  }
3232 
3233  // FIXME complexity estimation stuff
3234 
3235  if (ctx->shape != BIN_ONLY_SHAPE) {
3236  skip_bits_long(gb, ctx->cplx_estimation_trash_i);
3237  if (s->pict_type != AV_PICTURE_TYPE_I)
3238  skip_bits_long(gb, ctx->cplx_estimation_trash_p);
3239  if (s->pict_type == AV_PICTURE_TYPE_B)
3240  skip_bits_long(gb, ctx->cplx_estimation_trash_b);
3241 
3242  if (get_bits_left(gb) < 3) {
3243  av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
3244  return AVERROR_INVALIDDATA;
3245  }
3246  ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
3247  if (!s->progressive_sequence) {
3248  s->top_field_first = get_bits1(gb);
3249  s->alternate_scan = get_bits1(gb);
3250  } else
3251  s->alternate_scan = 0;
3252  }
3253  if (s->alternate_scan) {
3254  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
3255  ff_permute_scantable(s->permutated_intra_h_scantable, ff_alternate_vertical_scan,
3256  s->idsp.idct_permutation);
3257  } else {
3258  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
3259  ff_permute_scantable(s->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
3260  s->idsp.idct_permutation);
3261  }
3262  ff_permute_scantable(s->permutated_intra_v_scantable, ff_alternate_vertical_scan,
3263  s->idsp.idct_permutation);
3264 
3265  /* Skip at this point when only parsing since the remaining
3266  * data is not useful for a parser and requires the
3267  * sprite_trajectory VLC to be initialized. */
3268  if (parse_only)
3269  goto end;
3270 
3271  if (s->pict_type == AV_PICTURE_TYPE_S) {
3272  if((ctx->vol_sprite_usage == STATIC_SPRITE ||
3273  ctx->vol_sprite_usage == GMC_SPRITE)) {
3274  if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
3275  return AVERROR_INVALIDDATA;
3276  if (ctx->sprite_brightness_change)
3277  av_log(s->avctx, AV_LOG_ERROR,
3278  "sprite_brightness_change not supported\n");
3279  if (ctx->vol_sprite_usage == STATIC_SPRITE)
3280  av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
3281  } else {
3282  memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset));
3283  memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta));
3284  }
3285  }
3286 
3287  if (ctx->shape != BIN_ONLY_SHAPE) {
3288  s->chroma_qscale = s->qscale = get_bits(gb, ctx->quant_precision);
3289  if (s->qscale == 0) {
3290  av_log(s->avctx, AV_LOG_ERROR,
3291  "Error, header damaged or not MPEG-4 header (qscale=0)\n");
3292  return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3293  }
3294 
3295  if (s->pict_type != AV_PICTURE_TYPE_I) {
3296  s->f_code = get_bits(gb, 3); /* fcode_for */
3297  if (s->f_code == 0) {
3298  av_log(s->avctx, AV_LOG_ERROR,
3299  "Error, header damaged or not MPEG-4 header (f_code=0)\n");
3300  s->f_code = 1;
3301  return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3302  }
3303  } else
3304  s->f_code = 1;
3305 
3306  if (s->pict_type == AV_PICTURE_TYPE_B) {
3307  s->b_code = get_bits(gb, 3);
3308  if (s->b_code == 0) {
3309  av_log(s->avctx, AV_LOG_ERROR,
3310  "Error, header damaged or not MPEG4 header (b_code=0)\n");
3311  s->b_code=1;
3312  return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
3313  }
3314  } else
3315  s->b_code = 1;
3316 
3317  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
3318  av_log(s->avctx, AV_LOG_DEBUG,
3319  "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",
3320  s->qscale, s->f_code, s->b_code,
3321  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')),
3322  gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
3323  s->top_field_first, s->quarter_sample ? 'q' : 'h',
3324  s->data_partitioning, ctx->resync_marker,
3325  ctx->num_sprite_warping_points, ctx->sprite_warping_accuracy,
3326  1 - s->no_rounding, ctx->vo_type,
3327  ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
3328  ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
3329  ctx->cplx_estimation_trash_b,
3330  s->time,
3331  time_increment
3332  );
3333  }
3334 
3335  if (!ctx->scalability) {
3336  if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
3337  skip_bits1(gb); // vop shape coding type
3338  } else {
3339  if (ctx->enhancement_type) {
3340  int load_backward_shape = get_bits1(gb);
3341  if (load_backward_shape)
3342  av_log(s->avctx, AV_LOG_ERROR,
3343  "load backward shape isn't supported\n");
3344  }
3345  skip_bits(gb, 2); // ref_select_code
3346  }
3347  }
3348 
3349 end:
3350  /* detect buggy encoders which don't set the low_delay flag
3351  * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
3352  * easily (although it's buggy too) */
3353  if (ctx->vo_type == 0 && ctx->vol_control_parameters == 0 &&
3354  ctx->divx_version == -1 && s->picture_number == 0) {
3355  av_log(s->avctx, AV_LOG_WARNING,
3356  "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
3357  s->low_delay = 1;
3358  }
3359 
3360  s->picture_number++; // better than pic number==0 always ;)
3361 
3362  // FIXME add short header support
3363  s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
3364  s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
3365 
3366  if (s->workaround_bugs & FF_BUG_EDGE) {
3367  s->h_edge_pos = s->width;
3368  s->v_edge_pos = s->height;
3369  }
3370  return 0;
3371 }
3372 
3374 {
3375  MpegEncContext *s = &ctx->m;
3376 
3377  skip_bits(gb, 16); /* Time_code[63..48] */
3378  check_marker(s->avctx, gb, "after Time_code[63..48]");
3379  skip_bits(gb, 16); /* Time_code[47..32] */
3380  check_marker(s->avctx, gb, "after Time_code[47..32]");
3381  skip_bits(gb, 16); /* Time_code[31..16] */
3382  check_marker(s->avctx, gb, "after Time_code[31..16]");
3383  skip_bits(gb, 16); /* Time_code[15..0] */
3384  check_marker(s->avctx, gb, "after Time_code[15..0]");
3385  skip_bits(gb, 4); /* reserved_bits */
3386 }
3387 
3388 /**
3389  * Decode the next studio vop header.
3390  * @return <0 if something went wrong
3391  */
3393 {
3394  MpegEncContext *s = &ctx->m;
3395 
3396  if (get_bits_left(gb) <= 32)
3397  return 0;
3398 
3399  s->partitioned_frame = 0;
3400  s->interlaced_dct = 0;
3401  s->decode_mb = mpeg4_decode_studio_mb;
3402 
3403  decode_smpte_tc(ctx, gb);
3404 
3405  skip_bits(gb, 10); /* temporal_reference */
3406  skip_bits(gb, 2); /* vop_structure */
3407  s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
3408  if (get_bits1(gb)) { /* vop_coded */
3409  skip_bits1(gb); /* top_field_first */
3410  skip_bits1(gb); /* repeat_first_field */
3411  s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
3412  }
3413 
3414  if (s->pict_type == AV_PICTURE_TYPE_I) {
3415  if (get_bits1(gb))
3417  }
3418 
3419  if (ctx->shape != BIN_ONLY_SHAPE) {
3420  s->alternate_scan = get_bits1(gb);
3421  s->frame_pred_frame_dct = get_bits1(gb);
3422  s->dct_precision = get_bits(gb, 2);
3423  s->intra_dc_precision = get_bits(gb, 2);
3424  s->q_scale_type = get_bits1(gb);
3425  }
3426 
3427  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,
3428  s->alternate_scan ? ff_alternate_vertical_scan : ff_zigzag_direct);
3429 
3431 
3433  extension_and_user_data(s, gb, 4);
3434 
3435  return 0;
3436 }
3437 
3439 {
3440  MpegEncContext *s = &ctx->m;
3441  int visual_object_type;
3442 
3443  skip_bits(gb, 4); /* visual_object_verid */
3444  visual_object_type = get_bits(gb, 4);
3445  if (visual_object_type != VOT_VIDEO_ID) {
3446  avpriv_request_sample(s->avctx, "VO type %u", visual_object_type);
3447  return AVERROR_PATCHWELCOME;
3448  }
3449 
3451  extension_and_user_data(s, gb, 1);
3452 
3453  return 0;
3454 }
3455 
3456 /**
3457  * Decode MPEG-4 headers.
3458  *
3459  * @param header If set the absence of a VOP is not treated as error; otherwise, it is treated as such.
3460  * @param parse_only If set, things only relevant to a decoder may be skipped;
3461  * furthermore, the VLC tables may be uninitialized.
3462  * @return <0 if an error occurred
3463  * FRAME_SKIPPED if a not coded VOP is found
3464  * 0 else
3465  */
3467  int header, int parse_only)
3468 {
3469  MpegEncContext *s = &ctx->m;
3470  unsigned startcode, v;
3471  int ret;
3472  int vol = 0;
3473 
3474  /* search next start code */
3475  align_get_bits(gb);
3476 
3477  // If we have not switched to studio profile than we also did not switch bps
3478  // that means something else (like a previous instance) outside set bps which
3479  // would be inconsistant with the currect state, thus reset it
3480  if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3481  s->avctx->bits_per_raw_sample = 0;
3482 
3483  if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3484  skip_bits(gb, 24);
3485  if (get_bits(gb, 8) == 0xF0)
3486  goto end;
3487  }
3488 
3489  startcode = 0xff;
3490  for (;;) {
3491  if (get_bits_count(gb) >= gb->size_in_bits) {
3492  if (gb->size_in_bits == 8 &&
3493  (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3494  av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3495  return FRAME_SKIPPED; // divx bug
3496  } else if (header && get_bits_count(gb) == gb->size_in_bits) {
3497  return 0; // ordinary return value for parsing of extradata
3498  } else
3499  return AVERROR_INVALIDDATA; // end of stream
3500  }
3501 
3502  /* use the bits after the test */
3503  v = get_bits(gb, 8);
3504  startcode = ((startcode << 8) | v) & 0xffffffff;
3505 
3506  if ((startcode & 0xFFFFFF00) != 0x100)
3507  continue; // no startcode
3508 
3509  if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3510  const char *name;
3511  if (startcode <= 0x11F)
3512  name = "Video Object Start";
3513  else if (startcode <= 0x12F)
3514  name = "Video Object Layer Start";
3515  else if (startcode <= 0x13F)
3516  name = "Reserved";
3517  else if (startcode <= 0x15F)
3518  name = "FGS bp start";
3519  else if (startcode <= 0x1AF)
3520  name = "Reserved";
3521  else if (startcode == 0x1B0)
3522  name = "Visual Object Seq Start";
3523  else if (startcode == 0x1B1)
3524  name = "Visual Object Seq End";
3525  else if (startcode == 0x1B2)
3526  name = "User Data";
3527  else if (startcode == 0x1B3)
3528  name = "Group of VOP start";
3529  else if (startcode == 0x1B4)
3530  name = "Video Session Error";
3531  else if (startcode == 0x1B5)
3532  name = "Visual Object Start";
3533  else if (startcode == 0x1B6)
3534  name = "Video Object Plane start";
3535  else if (startcode == 0x1B7)
3536  name = "slice start";
3537  else if (startcode == 0x1B8)
3538  name = "extension start";
3539  else if (startcode == 0x1B9)
3540  name = "fgs start";
3541  else if (startcode == 0x1BA)
3542  name = "FBA Object start";
3543  else if (startcode == 0x1BB)
3544  name = "FBA Object Plane start";
3545  else if (startcode == 0x1BC)
3546  name = "Mesh Object start";
3547  else if (startcode == 0x1BD)
3548  name = "Mesh Object Plane start";
3549  else if (startcode == 0x1BE)
3550  name = "Still Texture Object start";
3551  else if (startcode == 0x1BF)
3552  name = "Texture Spatial Layer start";
3553  else if (startcode == 0x1C0)
3554  name = "Texture SNR Layer start";
3555  else if (startcode == 0x1C1)
3556  name = "Texture Tile start";
3557  else if (startcode == 0x1C2)
3558  name = "Texture Shape Layer start";
3559  else if (startcode == 0x1C3)
3560  name = "stuffing start";
3561  else if (startcode <= 0x1C5)
3562  name = "Reserved";
3563  else if (startcode <= 0x1FF)
3564  name = "System start";
3565  av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X %s at %d\n",
3566  startcode, name, get_bits_count(gb));
3567  }
3568 
3569  if (startcode >= 0x120 && startcode <= 0x12F) {
3570  if (vol) {
3571  av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3572  continue;
3573  }
3574  vol++;
3575  if ((ret = decode_vol_header(ctx, gb)) < 0)
3576  return ret;
3577  } else if (startcode == USER_DATA_STARTCODE) {
3578  decode_user_data(ctx, gb);
3579  } else if (startcode == GOP_STARTCODE) {
3581  } else if (startcode == VOS_STARTCODE) {
3582  int profile, level;
3585  (level > 0 && level < 9)) {
3586  s->studio_profile = 1;
3588  extension_and_user_data(s, gb, 0);
3589  } else if (s->studio_profile) {
3590  avpriv_request_sample(s->avctx, "Mix of studio and non studio profile");
3591  return AVERROR_PATCHWELCOME;
3592  }
3593  s->avctx->profile = profile;
3594  s->avctx->level = level;
3595  } else if (startcode == VISUAL_OBJ_STARTCODE) {
3596  if (s->studio_profile) {
3597  if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3598  return ret;
3599  } else
3601  } else if (startcode == VOP_STARTCODE) {
3602  break;
3603  }
3604 
3605  align_get_bits(gb);
3606  startcode = 0xff;
3607  }
3608 
3609 end:
3610  if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
3611  s->low_delay = 1;
3612  s->avctx->has_b_frames = !s->low_delay;
3613 
3614  if (s->studio_profile) {
3615  if (!s->avctx->bits_per_raw_sample) {
3616  av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n");
3617  return AVERROR_INVALIDDATA;
3618  }
3619  return decode_studio_vop_header(ctx, gb);
3620  } else
3621  return decode_vop_header(ctx, gb, parse_only);
3622 }
3623 
3624 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
3625 {
3626  Mpeg4DecContext *ctx = avctx->priv_data;
3627  MpegEncContext *s = &ctx->m;
3628 
3629  /* divx 5.01+ bitstream reorder stuff */
3630  /* Since this clobbers the input buffer and hwaccel codecs still need the
3631  * data during hwaccel->end_frame we should not do this any earlier */
3632  if (s->divx_packed) {
3633  int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
3634  int startcode_found = 0;
3635 
3636  if (buf_size - current_pos > 7) {
3637 
3638  int i;
3639  for (i = current_pos; i < buf_size - 4; i++)
3640 
3641  if (buf[i] == 0 &&
3642  buf[i + 1] == 0 &&
3643  buf[i + 2] == 1 &&
3644  buf[i + 3] == 0xB6) {
3645  startcode_found = !(buf[i + 4] & 0x40);
3646  break;
3647  }
3648  }
3649 
3650  if (startcode_found) {
3651  if (!ctx->showed_packed_warning) {
3652  av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
3653  "wasteful way to store B-frames ('packed B-frames'). "
3654  "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
3655  ctx->showed_packed_warning = 1;
3656  }
3657  av_fast_padded_malloc(&s->bitstream_buffer,
3658  &s->allocated_bitstream_buffer_size,
3659  buf_size - current_pos);
3660  if (!s->bitstream_buffer) {
3661  s->bitstream_buffer_size = 0;
3662  return AVERROR(ENOMEM);
3663  }
3664  memcpy(s->bitstream_buffer, buf + current_pos,
3665  buf_size - current_pos);
3666  s->bitstream_buffer_size = buf_size - current_pos;
3667  }
3668  }
3669 
3670  return 0;
3671 }
3672 
3673 #if CONFIG_MPEG4_DECODER
3674 #if HAVE_THREADS
3675 static int mpeg4_update_thread_context(AVCodecContext *dst,
3676  const AVCodecContext *src)
3677 {
3678  Mpeg4DecContext *s = dst->priv_data;
3679  const Mpeg4DecContext *s1 = src->priv_data;
3680  int init = s->m.context_initialized;
3681 
3683 
3684  if (ret < 0)
3685  return ret;
3686 
3687  // copy all the necessary fields explicitly
3688  s->time_increment_bits = s1->time_increment_bits;
3689  s->shape = s1->shape;
3690  s->vol_sprite_usage = s1->vol_sprite_usage;
3691  s->sprite_brightness_change = s1->sprite_brightness_change;
3692  s->sprite_warping_accuracy = s1->sprite_warping_accuracy;
3693  s->num_sprite_warping_points = s1->num_sprite_warping_points;
3694  s->m.data_partitioning = s1->m.data_partitioning;
3695  s->rvlc = s1->rvlc;
3696  s->resync_marker = s1->resync_marker;
3697  s->t_frame = s1->t_frame;
3698  s->new_pred = s1->new_pred;
3699  s->enhancement_type = s1->enhancement_type;
3700  s->scalability = s1->scalability;
3701  s->intra_dc_threshold = s1->intra_dc_threshold;
3702  s->divx_version = s1->divx_version;
3703  s->divx_build = s1->divx_build;
3704  s->xvid_build = s1->xvid_build;
3705  s->lavc_build = s1->lavc_build;
3706  s->vo_type = s1->vo_type;
3707  s->showed_packed_warning = s1->showed_packed_warning;
3708  s->vol_control_parameters = s1->vol_control_parameters;
3709  s->cplx_estimation_trash_i = s1->cplx_estimation_trash_i;
3710  s->cplx_estimation_trash_p = s1->cplx_estimation_trash_p;
3711  s->cplx_estimation_trash_b = s1->cplx_estimation_trash_b;
3712  s->rgb = s1->rgb;
3713 
3714  memcpy(s->sprite_shift, s1->sprite_shift, sizeof(s1->sprite_shift));
3715  memcpy(s->sprite_traj, s1->sprite_traj, sizeof(s1->sprite_traj));
3716 
3717  if (!init && s1->xvid_build >= 0)
3718  ff_xvid_idct_init(&s->m.idsp, dst);
3719 
3720  return 0;
3721 }
3722 
3723 static int mpeg4_update_thread_context_for_user(AVCodecContext *dst,
3724  const AVCodecContext *src)
3725 {
3726  MpegEncContext *m = dst->priv_data;
3727  const MpegEncContext *m1 = src->priv_data;
3728 
3729  m->quarter_sample = m1->quarter_sample;
3730  m->divx_packed = m1->divx_packed;
3731 
3732  return 0;
3733 }
3734 #endif
3735 
3736 static av_cold void mpeg4_init_static(void)
3737 {
3738  static uint8_t mpeg4_rvlc_rl_tables[2][2][2 * MAX_RUN + MAX_LEVEL + 3];
3739  static VLCElem vlc_buf[6498];
3740  VLCInitState state = VLC_INIT_STATE(vlc_buf);
3741 
3743  &ff_mpeg4_studio_dc_luma[0][1], 2,
3744  &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
3745  0, 0);
3746 
3748  &ff_mpeg4_studio_dc_chroma[0][1], 2,
3749  &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
3750  0, 0);
3751 
3752  for (unsigned i = 0; i < 12; i++) {
3753  studio_intra_tab[i] =
3755  &ff_mpeg4_studio_intra[i][0][1], 2,
3756  &ff_mpeg4_studio_intra[i][0][0], 2, 1,
3757  0, 0);
3758  }
3759 
3761  ff_rl_init(&ff_rvlc_rl_inter, mpeg4_rvlc_rl_tables[0]);
3762  ff_rl_init(&ff_rvlc_rl_intra, mpeg4_rvlc_rl_tables[1]);
3767  &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3768  &ff_mpeg4_DCtab_lum[0][0], 2, 1, 0);
3770  &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3771  &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 0);
3774  NULL, 0, 0, 0, 0);
3776  &ff_mb_type_b_tab[0][1], 2, 1,
3777  &ff_mb_type_b_tab[0][0], 2, 1,
3778  mb_type_b_map, 2, 2, 0);
3779 }
3780 
3781 static av_cold int decode_init(AVCodecContext *avctx)
3782 {
3783  static AVOnce init_static_once = AV_ONCE_INIT;
3784  Mpeg4DecContext *ctx = avctx->priv_data;
3785  MpegEncContext *s = &ctx->m;
3786  int ret;
3787 
3788  ctx->divx_version =
3789  ctx->divx_build =
3790  ctx->xvid_build =
3791  ctx->lavc_build = -1;
3792 
3793  if ((ret = ff_h263_decode_init(avctx)) < 0)
3794  return ret;
3795 
3796  s->h263_pred = 1;
3797  s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
3798  s->decode_mb = mpeg4_decode_mb;
3799  ctx->time_increment_bits = 4; /* default value for broken headers */
3800  ctx->quant_precision = 5;
3801 
3803 
3804  ff_qpeldsp_init(&s->qdsp);
3805  ff_mpeg4videodsp_init(&ctx->mdsp);
3806 
3807  ff_thread_once(&init_static_once, mpeg4_init_static);
3808 
3809  /* Must be after initializing the MPEG-4 static tables */
3810  if (avctx->extradata_size && !avctx->internal->is_copy) {
3811  GetBitContext gb;
3812 
3813  if (init_get_bits8(&gb, avctx->extradata, avctx->extradata_size) >= 0)
3814  ff_mpeg4_decode_picture_header(ctx, &gb, 1, 0);
3815  }
3816 
3817  return 0;
3818 }
3819 
3820 #define OFFSET(x) offsetof(MpegEncContext, x)
3821 #define FLAGS AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY
3822 static const AVOption mpeg4_options[] = {
3823  {"quarter_sample", "1/4 subpel MC", OFFSET(quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
3824  {"divx_packed", "divx style packed b frames", OFFSET(divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
3825  {NULL}
3826 };
3827 
3828 static const AVClass mpeg4_class = {
3829  .class_name = "MPEG4 Video Decoder",
3830  .item_name = av_default_item_name,
3831  .option = mpeg4_options,
3832  .version = LIBAVUTIL_VERSION_INT,
3833 };
3834 
3835 const FFCodec ff_mpeg4_decoder = {
3836  .p.name = "mpeg4",
3837  CODEC_LONG_NAME("MPEG-4 part 2"),
3838  .p.type = AVMEDIA_TYPE_VIDEO,
3839  .p.id = AV_CODEC_ID_MPEG4,
3840  .priv_data_size = sizeof(Mpeg4DecContext),
3841  .init = decode_init,
3843  .close = ff_mpv_decode_close,
3844  .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
3846  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
3848  .flush = ff_mpeg_flush,
3849  .p.max_lowres = 3,
3851  UPDATE_THREAD_CONTEXT(mpeg4_update_thread_context),
3852  UPDATE_THREAD_CONTEXT_FOR_USER(mpeg4_update_thread_context_for_user),
3853  .p.priv_class = &mpeg4_class,
3854  .hw_configs = (const AVCodecHWConfigInternal *const []) {
3855 #if CONFIG_MPEG4_NVDEC_HWACCEL
3856  HWACCEL_NVDEC(mpeg4),
3857 #endif
3858 #if CONFIG_MPEG4_VAAPI_HWACCEL
3859  HWACCEL_VAAPI(mpeg4),
3860 #endif
3861 #if CONFIG_MPEG4_VDPAU_HWACCEL
3862  HWACCEL_VDPAU(mpeg4),
3863 #endif
3864 #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
3865  HWACCEL_VIDEOTOOLBOX(mpeg4),
3866 #endif
3867  NULL
3868  },
3869 };
3870 #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:84
FF_BUG_DC_CLIP
#define FF_BUG_DC_CLIP
Definition: avcodec.h:1371
ff_mpeg4_video_profiles
const AVProfile ff_mpeg4_video_profiles[]
Definition: profiles.c:127
MV_TYPE_16X16
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:265
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:215
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
Mpeg4DecContext::sprite_warping_accuracy
int sprite_warping_accuracy
Definition: mpeg4videodec.h:42
level
uint8_t level
Definition: svq3.c:205
MpegEncContext::data_partitioning
int data_partitioning
data partitioning flag from header
Definition: mpegvideo.h:388
av_clip
#define av_clip
Definition: common.h:100
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
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:85
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
Mpeg4DecContext::vol_sprite_usage
int vol_sprite_usage
Definition: mpeg4videodec.h:40
opt.h
threadprogress.h
ff_xvid_idct_init
av_cold void ff_xvid_idct_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: xvididct.c:333
Mpeg4DecContext::showed_packed_warning
int showed_packed_warning
flag for having shown the warning about invalid Divx B-frames
Definition: mpeg4videodec.h:77
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:3095
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
int64_t
long long int64_t
Definition: coverity.c:34
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:2040
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:263
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:1369
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:717
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:2264
VOS_STARTCODE
#define VOS_STARTCODE
Definition: mpeg4videodefs.h:55
ff_mpeg4_decode_studio_slice_header
int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
Decode the next video packet.
Definition: mpeg4videodec.c:794
AVOption
AVOption.
Definition: opt.h:429
mpeg4_decode_gop_header
static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
Definition: mpeg4videodec.c:2312
Mpeg4DecContext::rvlc
int rvlc
Definition: mpeg4videodec.h:53
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:3438
FFCodec
Definition: codec_internal.h:127
FLAGS
#define FLAGS
Definition: cmdutils.c:595
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
ff_clean_intra_table_entries
void ff_clean_intra_table_entries(MpegEncContext *s)
Clean dc, ac for the current non-intra MB.
Definition: mpegvideo.c:796
Mpeg4DecContext::new_pred
int new_pred
Definition: mpeg4videodec.h:59
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:826
ff_init_block_index
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:820
max
#define max(a, b)
Definition: cuda_runtime.h:33
mpegvideo.h
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:1370
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:3373
mpegutils.h
SLICE_STARTCODE
#define SLICE_STARTCODE
Definition: mpeg4videodefs.h:60
FF_IDCT_AUTO
#define FF_IDCT_AUTO
Definition: avcodec.h:1559
mpeg4_decode_visual_object
static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
Definition: mpeg4videodec.c:2348
ff_mpeg4_init_rl_intra
av_cold void ff_mpeg4_init_rl_intra(void)
Definition: mpeg4video.c:36
ER_MV_ERROR
#define ER_MV_ERROR
Definition: error_resilience.h:32
FF_DEBUG_PICT_INFO
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1407
MV_DIR_BACKWARD
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:262
state
static struct @464 state
FF_IDCT_XVID
#define FF_IDCT_XVID
Definition: avcodec.h:1566
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:3624
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
mx
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition: dsp.h:53
decode_user_data
static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode the user data stuff in the header.
Definition: mpeg4videodec.c:2921
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:836
ff_h263_pred_motion
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:179
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:1367
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:518
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
mpeg4_decode_profile_level
static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
Definition: mpeg4videodec.c:2334
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:482
DC_VLC_BITS
#define DC_VLC_BITS
Definition: mpeg4videodec.c:57
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:505
ff_h263_decode_motion
int ff_h263_decode_motion(MpegEncContext *s, int pred, int f_code)
Definition: ituh263dec.c:274
HAS_FORWARD_MV
#define HAS_FORWARD_MV(a)
Definition: mpegutils.h:89
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
mpegvideodec.h
Mpeg4DecContext::sprite_brightness_change
int sprite_brightness_change
Definition: mpeg4videodec.h:41
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
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:2446
MB_TYPE_ACPRED
#define MB_TYPE_ACPRED
Definition: mpegutils.h:63
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:530
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
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:460
Mpeg4DecContext::cplx_estimation_trash_b
int cplx_estimation_trash_b
Definition: mpeg4videodec.h:83
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:1363
ff_mpeg4_rl_intra
RLTable ff_mpeg4_rl_intra
Definition: mpeg4data.h:108
bits
uint8_t bits
Definition: vp3data.h:128
AV_EF_IGNORE_ERR
#define AV_EF_IGNORE_ERR
ignore errors and continue
Definition: defs.h:53
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
ctx
AVFormatContext * ctx
Definition: movenc.c:49
ff_mpeg4_workaround_bugs
int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
Definition: mpeg4videodec.c:2978
IS_SKIP
#define IS_SKIP(a)
Definition: mpegutils.h:76
mpeg4_decode_mb
static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
Definition: mpeg4videodec.c:1650
DC_MARKER
#define DC_MARKER
Definition: mpeg4videodefs.h:53
SKIP_BITS
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:241
MpegEncContext::divx_packed
int divx_packed
Definition: mpegvideo.h:397
xvididct.h
RSHIFT
#define RSHIFT(a, b)
Definition: common.h:56
FF_BUG_DIRECT_BLOCKSIZE
#define FF_BUG_DIRECT_BLOCKSIZE
Definition: avcodec.h:1368
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:296
my
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition: dsp.h:53
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:74
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
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
ff_thread_progress_await
void ff_thread_progress_await(const ThreadProgress *pro_c, int n)
This function is a no-op in no-op mode; otherwise it waits until other threads have reached a certain...
Definition: threadprogress.c:65
mpeg4_load_default_matrices
static void mpeg4_load_default_matrices(MpegEncContext *s)
Definition: mpeg4videodec.c:2380
FF_BUG_UMP4
#define FF_BUG_UMP4
Definition: avcodec.h:1362
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
mb_type_b_map
static const int16_t mb_type_b_map[4]
Definition: mpeg4videodec.c:70
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
Mpeg4DecContext::rgb
int rgb
Definition: mpeg4videodec.h:85
Mpeg4DecContext::shape
int shape
Definition: mpeg4videodec.h:39
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:1366
ff_mpv_idct_init
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:307
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:738
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:61
mpeg4_decode_studio_block
static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n)
Definition: mpeg4videodec.c:2066
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
SLICE_NOEND
#define SLICE_NOEND
no end marker or error found but mb count exceeded
Definition: mpegvideo.h:483
MB_TYPE_8x8
#define MB_TYPE_8x8
Definition: mpegutils.h:45
ROUNDED_DIV
#define ROUNDED_DIV(a, b)
Definition: common.h:58
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:853
ff_h263_decode_frame
int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict, int *got_frame, AVPacket *avpkt)
Definition: h263dec.c:426
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:1374
ff_h263_intra_MCBPC_vlc
VLCElem ff_h263_intra_MCBPC_vlc[]
Definition: ituh263dec.c:102
MB_TYPE_BIDIR_MV
#define MB_TYPE_BIDIR_MV
Definition: mpegutils.h:52
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::cplx_estimation_trash_i
int cplx_estimation_trash_i
Definition: mpeg4videodec.h:81
Mpeg4DecContext
Definition: mpeg4videodec.h:34
UPDATE_THREAD_CONTEXT
#define UPDATE_THREAD_CONTEXT(func)
Definition: codec_internal.h:305
ff_alternate_horizontal_scan
const uint8_t ff_alternate_horizontal_scan[64]
Definition: mpegvideodata.c:52
FASTDIV
#define FASTDIV(a, b)
Definition: mathops.h:212
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:503
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
HAS_BACKWARD_MV
#define HAS_BACKWARD_MV(a)
Definition: mpegutils.h:90
AVOnce
#define AVOnce
Definition: thread.h:202
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:266
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
ADV_SIMPLE_VO_TYPE
#define ADV_SIMPLE_VO_TYPE
Definition: mpeg4videodefs.h:40
Mpeg4DecContext::scalability
int scalability
Definition: mpeg4videodec.h:61
MAX_LEVEL
#define MAX_LEVEL
Definition: rl.h:36
IS_INTRA
#define IS_INTRA(x, y)
mpeg4_decode_partition_a
static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
Decode first partition.
Definition: mpeg4videodec.c:936
Mpeg4DecContext::t_frame
int t_frame
time distance of first I -> B, used for interlaced B-frames
Definition: mpeg4videodec.h:57
check_marker
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
Definition: mpegvideodec.h:81
Mpeg4DecContext::divx_version
int divx_version
Definition: mpeg4videodec.h:69
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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
height
#define height
Definition: dsp.h:85
RL_VLC_ELEM
Definition: vlc.h:56
codec_internal.h
Mpeg4DecContext::lavc_build
int lavc_build
Definition: mpeg4videodec.h:72
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
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
Mpeg4DecContext::sprite_shift
int sprite_shift[2]
sprite shift [isChroma]
Definition: mpeg4videodec.h:50
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:609
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:1417
ff_mpeg4_default_intra_matrix
const int16_t ff_mpeg4_default_intra_matrix[64]
Definition: mpeg4data.h:334
VLCElem
Definition: vlc.h:32
read_quant_matrix_ext
static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
Definition: mpeg4videodec.c:2397
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:55
MB_TYPE_SKIP
#define MB_TYPE_SKIP
Definition: mpegutils.h:62
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:387
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:449
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:268
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
Mpeg4DecContext::sprite_traj
uint16_t sprite_traj[4][2]
sprite trajectory points
Definition: mpeg4videodec.h:48
Mpeg4DecContext::cplx_estimation_trash_p
int cplx_estimation_trash_p
Definition: mpeg4videodec.h:82
IS_DIRECT
#define IS_DIRECT(a)
Definition: mpegutils.h:79
unary.h
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
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
ff_init_scantable
av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: mpegvideo.c:293
Mpeg4DecContext::time_increment_bits
int time_increment_bits
number of bits to represent the fractional part of time
Definition: mpeg4videodec.h:38
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
mpeg4_decode_dc
static int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
Decode the dc value.
Definition: mpeg4videodec.c:889
SKIP_COUNTER
#define SKIP_COUNTER(name, gb, num)
Definition: get_bits.h:233
Mpeg4DecContext::num_sprite_warping_points
int num_sprite_warping_points
Definition: mpeg4videodec.h:43
CHROMA_420
#define CHROMA_420
Definition: mpegvideo.h:459
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:529
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:3466
RLTable::max_run
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
internal.h
ff_mpeg4_decode_video_packet_header
int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
Decode the next video packet.
Definition: mpeg4videodec.c:695
INTRA_MCBPC_VLC_BITS
#define INTRA_MCBPC_VLC_BITS
Definition: h263dec.h:36
FF_BUG_AUTODETECT
#define FF_BUG_AUTODETECT
autodetection
Definition: avcodec.h:1360
delta
float delta
Definition: vorbis_enc_data.h:430
MB_TYPE_BACKWARD_MV
#define MB_TYPE_BACKWARD_MV
Definition: mpegutils.h:51
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:1414
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:1558
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:708
len
int len
Definition: vorbis_enc_data.h:426
Mpeg4DecContext::vol_control_parameters
int vol_control_parameters
does the stream contain the low_delay flag, used to work around buggy encoders.
Definition: mpeg4videodec.h:80
AV_PROFILE_MPEG4_SIMPLE_STUDIO
#define AV_PROFILE_MPEG4_SIMPLE_STUDIO
Definition: defs.h:145
profile
int profile
Definition: mxfenc.c:2233
FF_BUG_QPEL_CHROMA
#define FF_BUG_QPEL_CHROMA
Definition: avcodec.h:1365
VLC_INIT_STATIC_SPARSE_TABLE
#define VLC_INIT_STATIC_SPARSE_TABLE(vlc_table, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, symbols, symbols_wrap, symbols_size, flags)
Definition: vlc.h:258
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
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:480
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:80
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:77
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:451
Mpeg4DecContext::m
MpegEncContext m
Definition: mpeg4videodec.h:35
mpeg4_decode_partition_b
static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
decode second partition.
Definition: mpeg4videodec.c:1130
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
Mpeg4DecContext::resync_marker
int resync_marker
could this stream contain resync markers
Definition: mpeg4videodec.h:55
Mpeg4DecContext::enhancement_type
int enhancement_type
Definition: mpeg4videodec.h:60
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:2463
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:2549
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1406
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:2181
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:1361
ff_h263_inter_MCBPC_vlc
VLCElem ff_h263_inter_MCBPC_vlc[]
Definition: ituh263dec.c:103
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
reset_studio_dc_predictors
static void reset_studio_dc_predictors(MpegEncContext *s)
Definition: mpeg4videodec.c:782
ff_mpeg4_c_dc_scale_table
const uint8_t ff_mpeg4_c_dc_scale_table[32]
Definition: mpeg4data.h:360
ER_MV_END
#define ER_MV_END
Definition: error_resilience.h:35
VLC_INIT_STATIC_TABLE_FROM_LENGTHS
#define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, lens, lens_wrap, syms, syms_wrap, syms_size, offset, flags)
Definition: vlc.h:280
VLC_INIT_RL
#define VLC_INIT_RL(rl, static_size)
Definition: rl.h:83
next_start_code_studio
static void next_start_code_studio(GetBitContext *gb)
Definition: mpeg4videodec.c:2030
ff_tlog
#define ff_tlog(ctx,...)
Definition: internal.h:141
MV_DIR_FORWARD
#define MV_DIR_FORWARD
Definition: mpegvideo.h:261
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:478
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
decode_studio_vop_header
static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode the next studio vop header.
Definition: mpeg4videodec.c:3392
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:72
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
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:307
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
Mpeg4DecContext::divx_build
int divx_build
Definition: mpeg4videodec.h:70
Mpeg4DecContext::xvid_build
int xvid_build
Definition: mpeg4videodec.h:71
h
h
Definition: vp9dsp_template.c:2070
decode_new_pred
static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb)
Definition: mpeg4videodec.c:679
width
#define width
Definition: dsp.h:85
QUANT_MATRIX_EXT_ID
#define QUANT_MATRIX_EXT_ID
Definition: mpeg4videodefs.h:63
FF_BUG_AMV
#define FF_BUG_AMV
Definition: avcodec.h:1364
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
MB_TYPE_FORWARD_MV
#define MB_TYPE_FORWARD_MV
Definition: mpegutils.h:50
src
#define src
Definition: vp8dsp.c:248
RLTable::rl_vlc
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
ff_mpv_decode_close
int ff_mpv_decode_close(AVCodecContext *avctx)
Definition: mpegvideo_dec.c:163
Mpeg4DecContext::intra_dc_threshold
int intra_dc_threshold
QP above which the ac VLC should be used for intra dc.
Definition: mpeg4videodec.h:66
IS_3IV1
#define IS_3IV1
Definition: mpeg4video.h:42
MB_TYPE_INTRA
#define MB_TYPE_INTRA
Definition: mpegutils.h:65
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
Mpeg4DecContext::vo_type
int vo_type
Definition: mpeg4videodec.h:74
min
float min
Definition: vorbis_enc_data.h:429