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