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