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