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