FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpeg4videodec.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 decoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #define UNCHECKED_BITSTREAM_READER 1
24 
25 #include "libavutil/internal.h"
26 #include "libavutil/opt.h"
27 #include "error_resilience.h"
28 #include "hwaccel.h"
29 #include "idctdsp.h"
30 #include "internal.h"
31 #include "mpegutils.h"
32 #include "mpegvideo.h"
33 #include "mpegvideodata.h"
34 #include "mpeg4video.h"
35 #include "h263.h"
36 #include "profiles.h"
37 #include "thread.h"
38 #include "xvididct.h"
39 
40 /* The defines below define the number of bits that are read at once for
41  * reading vlc values. Changing these may improve speed and data cache needs
42  * be aware though that decreasing them may need the number of stages that is
43  * passed to get_vlc* to be increased. */
44 #define SPRITE_TRAJ_VLC_BITS 6
45 #define DC_VLC_BITS 9
46 #define MB_TYPE_B_VLC_BITS 4
47 #define STUDIO_INTRA_BITS 9
48 
50 
54 
55 static const int mb_type_b_map[4] = {
60 };
61 
62 /**
63  * Predict the ac.
64  * @param n block index (0-3 are luma, 4-5 are chroma)
65  * @param dir the ac prediction direction
66  */
67 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
68 {
69  int i;
70  int16_t *ac_val, *ac_val1;
71  int8_t *const qscale_table = s->current_picture.qscale_table;
72 
73  /* find prediction */
74  ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
75  ac_val1 = ac_val;
76  if (s->ac_pred) {
77  if (dir == 0) {
78  const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
79  /* left prediction */
80  ac_val -= 16;
81 
82  if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
83  n == 1 || n == 3) {
84  /* same qscale */
85  for (i = 1; i < 8; i++)
86  block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
87  } else {
88  /* different qscale, we must rescale */
89  for (i = 1; i < 8; i++)
90  block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
91  }
92  } else {
93  const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
94  /* top prediction */
95  ac_val -= 16 * s->block_wrap[n];
96 
97  if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
98  n == 2 || n == 3) {
99  /* same qscale */
100  for (i = 1; i < 8; i++)
101  block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
102  } else {
103  /* different qscale, we must rescale */
104  for (i = 1; i < 8; i++)
105  block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
106  }
107  }
108  }
109  /* left copy */
110  for (i = 1; i < 8; i++)
111  ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
112 
113  /* top copy */
114  for (i = 1; i < 8; i++)
115  ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
116 }
117 
118 /**
119  * check if the next stuff is a resync marker or the end.
120  * @return 0 if not
121  */
123 {
124  MpegEncContext *s = &ctx->m;
125  int bits_count = get_bits_count(&s->gb);
126  int v = show_bits(&s->gb, 16);
127 
129  return 0;
130 
131  while (v <= 0xFF) {
132  if (s->pict_type == AV_PICTURE_TYPE_B ||
133  (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
134  break;
135  skip_bits(&s->gb, 8 + s->pict_type);
136  bits_count += 8 + s->pict_type;
137  v = show_bits(&s->gb, 16);
138  }
139 
140  if (bits_count + 8 >= s->gb.size_in_bits) {
141  v >>= 8;
142  v |= 0x7F >> (7 - (bits_count & 7));
143 
144  if (v == 0x7F)
145  return s->mb_num;
146  } else {
147  if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
148  int len, mb_num;
149  int mb_num_bits = av_log2(s->mb_num - 1) + 1;
150  GetBitContext gb = s->gb;
151 
152  skip_bits(&s->gb, 1);
153  align_get_bits(&s->gb);
154 
155  for (len = 0; len < 32; len++)
156  if (get_bits1(&s->gb))
157  break;
158 
159  mb_num = get_bits(&s->gb, mb_num_bits);
160  if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
161  mb_num= -1;
162 
163  s->gb = gb;
164 
166  return mb_num;
167  }
168  }
169  return 0;
170 }
171 
173 {
174  MpegEncContext *s = &ctx->m;
175  int a = 2 << s->sprite_warping_accuracy;
176  int rho = 3 - s->sprite_warping_accuracy;
177  int r = 16 / a;
178  int alpha = 1;
179  int beta = 0;
180  int w = s->width;
181  int h = s->height;
182  int min_ab, i, w2, h2, w3, h3;
183  int sprite_ref[4][2];
184  int virtual_ref[2][2];
185  int64_t sprite_offset[2][2];
186  int64_t sprite_delta[2][2];
187 
188  // only true for rectangle shapes
189  const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
190  { 0, s->height }, { s->width, s->height } };
191  int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
192 
193  if (w <= 0 || h <= 0)
194  return AVERROR_INVALIDDATA;
195 
196  /* the decoder was not properly initialized and we cannot continue */
197  if (sprite_trajectory.table == NULL)
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 
204  length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
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 
211  length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
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;
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] * (w+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  }
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) {
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)
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)
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_START_CODE) {
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)
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 * 16 + dy * s->mb_y * 16;
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 */
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_long(&s->gb, 19) == DC_MARKER)
713  return mb_num - 1;
714 
716  if (cbpc < 0) {
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;
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) {
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) {
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 {
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) {
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) {
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;
835  MB_TYPE_L0;
836  } else {
837  mx = get_amv(ctx, 0);
838  my = get_amv(ctx, 1);
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;
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) {
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) {
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) {
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) {
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) {
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");
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_long(&s->gb, 19) != DC_MARKER) {
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) {
1013  "marker missing after first P partition at %d %d\n",
1014  s->mb_x, s->mb_y);
1015  return AVERROR_INVALIDDATA;
1016  }
1017  }
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)
1025  s->mb_x, s->mb_y, ER_DC_ERROR);
1026  return ret;
1027  } else {
1028  if (s->pict_type == AV_PICTURE_TYPE_P)
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;
1047  RL_VLC_ELEM *rl_vlc;
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;
1078  rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1079  } else {
1080  rl = &ff_mpeg4_rl_intra;
1081  rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
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)
1110  rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1111  else
1112  rl_vlc = ff_h263_rl_inter.rl_vlc[0];
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) {
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) {
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) {
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) {
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) {
1237  if (level > 2560 || level < -2560) {
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)) {
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 {
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)
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;
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) {
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;
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) {
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 {
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) {
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 {
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) {
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) {
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) {
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)) {
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 {
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 {
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 
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;
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 
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) {
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)
1715  else
1717 
1718  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1719  if (cbpy < 0) {
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;
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_long(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 {
1818 
1819  int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
1820  additional_code_len, sign, mismatch;
1821  VLC *cur_vlc = &ctx->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 
1828  mismatch = 1;
1829 
1830  memset(block, 0, 64 * sizeof(int32_t));
1831 
1832  if (n < 4) {
1833  cc = 0;
1834  dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1835  quant_matrix = s->intra_matrix;
1836  } else {
1837  cc = (n & 1) + 1;
1838  if (ctx->rgb)
1839  dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1840  else
1841  dct_dc_size = get_vlc2(&s->gb, ctx->studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
1842  quant_matrix = s->chroma_intra_matrix;
1843  }
1844 
1845  if (dct_dc_size < 0) {
1846  av_log(s->avctx, AV_LOG_ERROR, "illegal dct_dc_size vlc\n");
1847  return AVERROR_INVALIDDATA;
1848  } else if (dct_dc_size == 0) {
1849  dct_diff = 0;
1850  } else {
1851  dct_diff = get_xbits(&s->gb, dct_dc_size);
1852 
1853  if (dct_dc_size > 8) {
1854  if(!check_marker(s->avctx, &s->gb, "dct_dc_size > 8"))
1855  return AVERROR_INVALIDDATA;
1856  }
1857 
1858  }
1859 
1860  s->last_dc[cc] += dct_diff;
1861 
1862  if (s->mpeg_quant)
1863  block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision);
1864  else
1865  block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision) * (8 >> s->dct_precision);
1866  /* TODO: support mpeg_quant for AC coefficients */
1867 
1868  block[0] = av_clip(block[0], min, max);
1869  mismatch ^= block[0];
1870 
1871  /* AC Coefficients */
1872  while (1) {
1873  group = get_vlc2(&s->gb, cur_vlc->table, STUDIO_INTRA_BITS, 2);
1874 
1875  if (group < 0) {
1876  av_log(s->avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
1877  return AVERROR_INVALIDDATA;
1878  }
1879 
1880  additional_code_len = ac_state_tab[group][0];
1881  cur_vlc = &ctx->studio_intra_tab[ac_state_tab[group][1]];
1882 
1883  if (group == 0) {
1884  /* End of Block */
1885  break;
1886  } else if (group >= 1 && group <= 6) {
1887  /* Zero run length (Table B.47) */
1888  run = 1 << additional_code_len;
1889  if (additional_code_len)
1890  run += get_bits(&s->gb, additional_code_len);
1891  idx += run;
1892  continue;
1893  } else if (group >= 7 && group <= 12) {
1894  /* Zero run length and +/-1 level (Table B.48) */
1895  code = get_bits(&s->gb, additional_code_len);
1896  sign = code & 1;
1897  code >>= 1;
1898  run = (1 << (additional_code_len - 1)) + code;
1899  idx += run;
1900  j = scantable[idx++];
1901  block[j] = sign ? 1 : -1;
1902  } else if (group >= 13 && group <= 20) {
1903  /* Level value (Table B.49) */
1904  j = scantable[idx++];
1905  block[j] = get_xbits(&s->gb, additional_code_len);
1906  } else if (group == 21) {
1907  /* Escape */
1908  j = scantable[idx++];
1909  additional_code_len = s->avctx->bits_per_raw_sample + s->dct_precision + 4;
1910  flc = get_bits(&s->gb, additional_code_len);
1911  if (flc >> (additional_code_len-1))
1912  block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
1913  else
1914  block[j] = flc;
1915  }
1916  block[j] = ((8 * 2 * block[j] * quant_matrix[j] * s->qscale) >> s->dct_precision) / 32;
1917  block[j] = av_clip(block[j], min, max);
1918  mismatch ^= block[j];
1919  }
1920 
1921  block[63] ^= mismatch & 1;
1922 
1923  return 0;
1924 }
1925 
1926 static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
1927 {
1928  int i;
1929 
1930  /* StudioMacroblock */
1931  /* Assumes I-VOP */
1932  s->mb_intra = 1;
1933  if (get_bits1(&s->gb)) { /* compression_mode */
1934  /* DCT */
1935  /* macroblock_type, 1 or 2-bit VLC */
1936  if (!get_bits1(&s->gb)) {
1937  skip_bits1(&s->gb);
1938  s->qscale = mpeg_get_qscale(s);
1939  }
1940 
1941  for (i = 0; i < mpeg4_block_count[s->chroma_format]; i++) {
1942  if (mpeg4_decode_studio_block(s, (*s->block32)[i], i) < 0)
1943  return AVERROR_INVALIDDATA;
1944  }
1945  } else {
1946  /* DPCM */
1947  check_marker(s->avctx, &s->gb, "DPCM block start");
1948  avpriv_request_sample(s->avctx, "DPCM encoded block");
1950  return SLICE_ERROR;
1951  }
1952 
1953  if (get_bits_left(&s->gb) >= 24 && show_bits(&s->gb, 23) == 0) {
1955  return SLICE_END;
1956  }
1957 
1958  return SLICE_OK;
1959 }
1960 
1962 {
1963  int hours, minutes, seconds;
1964 
1965  if (!show_bits(gb, 23)) {
1966  av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1967  return AVERROR_INVALIDDATA;
1968  }
1969 
1970  hours = get_bits(gb, 5);
1971  minutes = get_bits(gb, 6);
1972  check_marker(s->avctx, gb, "in gop_header");
1973  seconds = get_bits(gb, 6);
1974 
1975  s->time_base = seconds + 60*(minutes + 60*hours);
1976 
1977  skip_bits1(gb);
1978  skip_bits1(gb);
1979 
1980  return 0;
1981 }
1982 
1984 {
1985 
1986  *profile = get_bits(gb, 4);
1987  *level = get_bits(gb, 4);
1988 
1989  // for Simple profile, level 0
1990  if (*profile == 0 && *level == 8) {
1991  *level = 0;
1992  }
1993 
1994  return 0;
1995 }
1996 
1998 {
1999  int visual_object_type;
2000  int is_visual_object_identifier = get_bits1(gb);
2001 
2002  if (is_visual_object_identifier) {
2003  skip_bits(gb, 4+3);
2004  }
2005  visual_object_type = get_bits(gb, 4);
2006 
2007  if (visual_object_type == VOT_VIDEO_ID ||
2008  visual_object_type == VOT_STILL_TEXTURE_ID) {
2009  int video_signal_type = get_bits1(gb);
2010  if (video_signal_type) {
2011  int video_range, color_description;
2012  skip_bits(gb, 3); // video_format
2013  video_range = get_bits1(gb);
2014  color_description = get_bits1(gb);
2015 
2016  s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2017 
2018  if (color_description) {
2019  s->avctx->color_primaries = get_bits(gb, 8);
2020  s->avctx->color_trc = get_bits(gb, 8);
2021  s->avctx->colorspace = get_bits(gb, 8);
2022  }
2023  }
2024  }
2025 
2026  return 0;
2027 }
2028 
2030 {
2031  int i, v;
2032 
2033  /* load default matrices */
2034  for (i = 0; i < 64; i++) {
2035  int j = s->idsp.idct_permutation[i];
2037  s->intra_matrix[j] = v;
2038  s->chroma_intra_matrix[j] = v;
2039 
2041  s->inter_matrix[j] = v;
2042  s->chroma_inter_matrix[j] = v;
2043  }
2044 }
2045 
2047 {
2048  MpegEncContext *s = &ctx->m;
2049  int width, height, vo_ver_id;
2050 
2051  /* vol header */
2052  skip_bits(gb, 1); /* random access */
2053  s->vo_type = get_bits(gb, 8);
2054 
2055  /* If we are in studio profile (per vo_type), check if its all consistent
2056  * and if so continue pass control to decode_studio_vol_header().
2057  * elIf something is inconsistent, error out
2058  * else continue with (non studio) vol header decpoding.
2059  */
2060  if (s->vo_type == CORE_STUDIO_VO_TYPE ||
2063  return AVERROR_INVALIDDATA;
2064  s->studio_profile = 1;
2066  return decode_studio_vol_header(ctx, gb);
2067  } else if (s->studio_profile) {
2068  return AVERROR_PATCHWELCOME;
2069  }
2070 
2071  if (get_bits1(gb) != 0) { /* is_ol_id */
2072  vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
2073  skip_bits(gb, 3); /* vo_priority */
2074  } else {
2075  vo_ver_id = 1;
2076  }
2077  s->aspect_ratio_info = get_bits(gb, 4);
2079  s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2080  s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2081  } else {
2083  }
2084 
2085  if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2086  int chroma_format = get_bits(gb, 2);
2087  if (chroma_format != CHROMA_420)
2088  av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2089 
2090  s->low_delay = get_bits1(gb);
2091  if (get_bits1(gb)) { /* vbv parameters */
2092  get_bits(gb, 15); /* first_half_bitrate */
2093  check_marker(s->avctx, gb, "after first_half_bitrate");
2094  get_bits(gb, 15); /* latter_half_bitrate */
2095  check_marker(s->avctx, gb, "after latter_half_bitrate");
2096  get_bits(gb, 15); /* first_half_vbv_buffer_size */
2097  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2098  get_bits(gb, 3); /* latter_half_vbv_buffer_size */
2099  get_bits(gb, 11); /* first_half_vbv_occupancy */
2100  check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
2101  get_bits(gb, 15); /* latter_half_vbv_occupancy */
2102  check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2103  }
2104  } else {
2105  /* is setting low delay flag only once the smartest thing to do?
2106  * low delay detection will not be overridden. */
2107  if (s->picture_number == 0) {
2108  switch(s->vo_type) {
2109  case SIMPLE_VO_TYPE:
2110  case ADV_SIMPLE_VO_TYPE:
2111  s->low_delay = 1;
2112  break;
2113  default:
2114  s->low_delay = 0;
2115  }
2116  }
2117  }
2118 
2119  ctx->shape = get_bits(gb, 2); /* vol shape */
2120  if (ctx->shape != RECT_SHAPE)
2121  av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2122  if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2123  av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2124  skip_bits(gb, 4); /* video_object_layer_shape_extension */
2125  }
2126 
2127  check_marker(s->avctx, gb, "before time_increment_resolution");
2128 
2129  s->avctx->framerate.num = get_bits(gb, 16);
2130  if (!s->avctx->framerate.num) {
2131  av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
2132  return AVERROR_INVALIDDATA;
2133  }
2134 
2135  ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
2136  if (ctx->time_increment_bits < 1)
2137  ctx->time_increment_bits = 1;
2138 
2139  check_marker(s->avctx, gb, "before fixed_vop_rate");
2140 
2141  if (get_bits1(gb) != 0) /* fixed_vop_rate */
2143  else
2144  s->avctx->framerate.den = 1;
2145 
2147 
2148  ctx->t_frame = 0;
2149 
2150  if (ctx->shape != BIN_ONLY_SHAPE) {
2151  if (ctx->shape == RECT_SHAPE) {
2152  check_marker(s->avctx, gb, "before width");
2153  width = get_bits(gb, 13);
2154  check_marker(s->avctx, gb, "before height");
2155  height = get_bits(gb, 13);
2156  check_marker(s->avctx, gb, "after height");
2157  if (width && height && /* they should be non zero but who knows */
2158  !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
2159  if (s->width && s->height &&
2160  (s->width != width || s->height != height))
2161  s->context_reinit = 1;
2162  s->width = width;
2163  s->height = height;
2164  }
2165  }
2166 
2167  s->progressive_sequence =
2168  s->progressive_frame = get_bits1(gb) ^ 1;
2169  s->interlaced_dct = 0;
2170  if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
2171  av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
2172  "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2173  if (vo_ver_id == 1)
2174  ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
2175  else
2176  ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
2177 
2178  if (ctx->vol_sprite_usage == STATIC_SPRITE)
2179  av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2180  if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2181  ctx->vol_sprite_usage == GMC_SPRITE) {
2182  if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2183  skip_bits(gb, 13); // sprite_width
2184  check_marker(s->avctx, gb, "after sprite_width");
2185  skip_bits(gb, 13); // sprite_height
2186  check_marker(s->avctx, gb, "after sprite_height");
2187  skip_bits(gb, 13); // sprite_left
2188  check_marker(s->avctx, gb, "after sprite_left");
2189  skip_bits(gb, 13); // sprite_top
2190  check_marker(s->avctx, gb, "after sprite_top");
2191  }
2192  ctx->num_sprite_warping_points = get_bits(gb, 6);
2193  if (ctx->num_sprite_warping_points > 3) {
2194  av_log(s->avctx, AV_LOG_ERROR,
2195  "%d sprite_warping_points\n",
2196  ctx->num_sprite_warping_points);
2197  ctx->num_sprite_warping_points = 0;
2198  return AVERROR_INVALIDDATA;
2199  }
2200  s->sprite_warping_accuracy = get_bits(gb, 2);
2201  ctx->sprite_brightness_change = get_bits1(gb);
2202  if (ctx->vol_sprite_usage == STATIC_SPRITE)
2203  skip_bits1(gb); // low_latency_sprite
2204  }
2205  // FIXME sadct disable bit if verid!=1 && shape not rect
2206 
2207  if (get_bits1(gb) == 1) { /* not_8_bit */
2208  s->quant_precision = get_bits(gb, 4); /* quant_precision */
2209  if (get_bits(gb, 4) != 8) /* bits_per_pixel */
2210  av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
2211  if (s->quant_precision != 5)
2212  av_log(s->avctx, AV_LOG_ERROR,
2213  "quant precision %d\n", s->quant_precision);
2214  if (s->quant_precision<3 || s->quant_precision>9) {
2215  s->quant_precision = 5;
2216  }
2217  } else {
2218  s->quant_precision = 5;
2219  }
2220 
2221  // FIXME a bunch of grayscale shape things
2222 
2223  if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2224  int i, v;
2225 
2227 
2228  /* load custom intra matrix */
2229  if (get_bits1(gb)) {
2230  int last = 0;
2231  for (i = 0; i < 64; i++) {
2232  int j;
2233  if (get_bits_left(gb) < 8) {
2234  av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2235  return AVERROR_INVALIDDATA;
2236  }
2237  v = get_bits(gb, 8);
2238  if (v == 0)
2239  break;
2240 
2241  last = v;
2242  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2243  s->intra_matrix[j] = last;
2244  s->chroma_intra_matrix[j] = last;
2245  }
2246 
2247  /* replicate last value */
2248  for (; i < 64; i++) {
2249  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2250  s->intra_matrix[j] = last;
2251  s->chroma_intra_matrix[j] = last;
2252  }
2253  }
2254 
2255  /* load custom non intra matrix */
2256  if (get_bits1(gb)) {
2257  int last = 0;
2258  for (i = 0; i < 64; i++) {
2259  int j;
2260  if (get_bits_left(gb) < 8) {
2261  av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2262  return AVERROR_INVALIDDATA;
2263  }
2264  v = get_bits(gb, 8);
2265  if (v == 0)
2266  break;
2267 
2268  last = v;
2269  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2270  s->inter_matrix[j] = v;
2271  s->chroma_inter_matrix[j] = v;
2272  }
2273 
2274  /* replicate last value */
2275  for (; i < 64; i++) {
2276  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2277  s->inter_matrix[j] = last;
2278  s->chroma_inter_matrix[j] = last;
2279  }
2280  }
2281 
2282  // FIXME a bunch of grayscale shape things
2283  }
2284 
2285  if (vo_ver_id != 1)
2286  s->quarter_sample = get_bits1(gb);
2287  else
2288  s->quarter_sample = 0;
2289 
2290  if (get_bits_left(gb) < 4) {
2291  av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2292  return AVERROR_INVALIDDATA;
2293  }
2294 
2295  if (!get_bits1(gb)) {
2296  int pos = get_bits_count(gb);
2297  int estimation_method = get_bits(gb, 2);
2298  if (estimation_method < 2) {
2299  if (!get_bits1(gb)) {
2300  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2301  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2302  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2303  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2304  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2305  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2306  }
2307  if (!get_bits1(gb)) {
2308  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2309  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2310  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2311  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2312  }
2313  if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2314  skip_bits_long(gb, pos - get_bits_count(gb));
2315  goto no_cplx_est;
2316  }
2317  if (!get_bits1(gb)) {
2318  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2319  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2320  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2321  ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2322  }
2323  if (!get_bits1(gb)) {
2324  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2325  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2326  ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2327  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2328  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2329  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2330  }
2331  if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2332  skip_bits_long(gb, pos - get_bits_count(gb));
2333  goto no_cplx_est;
2334  }
2335  if (estimation_method == 1) {
2336  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2337  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2338  }
2339  } else
2340  av_log(s->avctx, AV_LOG_ERROR,
2341  "Invalid Complexity estimation method %d\n",
2342  estimation_method);
2343  } else {
2344 
2345 no_cplx_est:
2346  ctx->cplx_estimation_trash_i =
2347  ctx->cplx_estimation_trash_p =
2348  ctx->cplx_estimation_trash_b = 0;
2349  }
2350 
2351  ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2352 
2353  s->data_partitioning = get_bits1(gb);
2354  if (s->data_partitioning)
2355  ctx->rvlc = get_bits1(gb);
2356 
2357  if (vo_ver_id != 1) {
2358  ctx->new_pred = get_bits1(gb);
2359  if (ctx->new_pred) {
2360  av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2361  skip_bits(gb, 2); /* requested upstream message type */
2362  skip_bits1(gb); /* newpred segment type */
2363  }
2364  if (get_bits1(gb)) // reduced_res_vop
2365  av_log(s->avctx, AV_LOG_ERROR,
2366  "reduced resolution VOP not supported\n");
2367  } else {
2368  ctx->new_pred = 0;
2369  }
2370 
2371  ctx->scalability = get_bits1(gb);
2372 
2373  if (ctx->scalability) {
2374  GetBitContext bak = *gb;
2375  int h_sampling_factor_n;
2376  int h_sampling_factor_m;
2377  int v_sampling_factor_n;
2378  int v_sampling_factor_m;
2379 
2380  skip_bits1(gb); // hierarchy_type
2381  skip_bits(gb, 4); /* ref_layer_id */
2382  skip_bits1(gb); /* ref_layer_sampling_dir */
2383  h_sampling_factor_n = get_bits(gb, 5);
2384  h_sampling_factor_m = get_bits(gb, 5);
2385  v_sampling_factor_n = get_bits(gb, 5);
2386  v_sampling_factor_m = get_bits(gb, 5);
2387  ctx->enhancement_type = get_bits1(gb);
2388 
2389  if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2390  v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2391  /* illegal scalability header (VERY broken encoder),
2392  * trying to workaround */
2393  ctx->scalability = 0;
2394  *gb = bak;
2395  } else
2396  av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2397 
2398  // bin shape stuff FIXME
2399  }
2400  }
2401 
2402  if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2403  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",
2404  s->avctx->framerate.den, s->avctx->framerate.num,
2405  ctx->time_increment_bits,
2406  s->quant_precision,
2407  s->progressive_sequence,
2408  s->low_delay,
2409  ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2410  s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2411  );
2412  }
2413 
2414  return 0;
2415 }
2416 
2417 /**
2418  * Decode the user data stuff in the header.
2419  * Also initializes divx/xvid/lavc_version/build.
2420  */
2422 {
2423  MpegEncContext *s = &ctx->m;
2424  char buf[256];
2425  int i;
2426  int e;
2427  int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2428  char last;
2429 
2430  for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2431  if (show_bits(gb, 23) == 0)
2432  break;
2433  buf[i] = get_bits(gb, 8);
2434  }
2435  buf[i] = 0;
2436 
2437  /* divx detection */
2438  e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2439  if (e < 2)
2440  e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2441  if (e >= 2) {
2442  ctx->divx_version = ver;
2443  ctx->divx_build = build;
2444  s->divx_packed = e == 3 && last == 'p';
2445  }
2446 
2447  /* libavcodec detection */
2448  e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2449  if (e != 4)
2450  e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2451  if (e != 4) {
2452  e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2453  if (e > 1) {
2454  if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
2456  "Unknown Lavc version string encountered, %d.%d.%d; "
2457  "clamping sub-version values to 8-bits.\n",
2458  ver, ver2, ver3);
2459  }
2460  build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
2461  }
2462  }
2463  if (e != 4) {
2464  if (strcmp(buf, "ffmpeg") == 0)
2465  ctx->lavc_build = 4600;
2466  }
2467  if (e == 4)
2468  ctx->lavc_build = build;
2469 
2470  /* Xvid detection */
2471  e = sscanf(buf, "XviD%d", &build);
2472  if (e == 1)
2473  ctx->xvid_build = build;
2474 
2475  return 0;
2476 }
2477 
2479 {
2480  Mpeg4DecContext *ctx = avctx->priv_data;
2481  MpegEncContext *s = &ctx->m;
2482 
2483  if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2484  if (s->codec_tag == AV_RL32("XVID") ||
2485  s->codec_tag == AV_RL32("XVIX") ||
2486  s->codec_tag == AV_RL32("RMP4") ||
2487  s->codec_tag == AV_RL32("ZMP4") ||
2488  s->codec_tag == AV_RL32("SIPP"))
2489  ctx->xvid_build = 0;
2490  }
2491 
2492  if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2493  if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2494  ctx->vol_control_parameters == 0)
2495  ctx->divx_version = 400; // divx 4
2496 
2497  if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2498  ctx->divx_version =
2499  ctx->divx_build = -1;
2500  }
2501 
2503  if (s->codec_tag == AV_RL32("XVIX"))
2505 
2506  if (s->codec_tag == AV_RL32("UMP4"))
2508 
2509  if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2511 
2512  if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2514 
2515  if (ctx->xvid_build <= 3U)
2516  s->padding_bug_score = 256 * 256 * 256 * 64;
2517 
2518  if (ctx->xvid_build <= 1U)
2520 
2521  if (ctx->xvid_build <= 12U)
2523 
2524  if (ctx->xvid_build <= 32U)
2526 
2527 #define SET_QPEL_FUNC(postfix1, postfix2) \
2528  s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2529  s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2530  s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2531 
2532  if (ctx->lavc_build < 4653U)
2534 
2535  if (ctx->lavc_build < 4655U)
2537 
2538  if (ctx->lavc_build < 4670U)
2540 
2541  if (ctx->lavc_build <= 4712U)
2543 
2544  if ((ctx->lavc_build&0xFF) >= 100) {
2545  if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2546  (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2547  )
2549  }
2550 
2551  if (ctx->divx_version >= 0)
2553  if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2554  s->padding_bug_score = 256 * 256 * 256 * 64;
2555 
2556  if (ctx->divx_version < 500U)
2558 
2559  if (ctx->divx_version >= 0)
2561  }
2562 
2563  if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2564  SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2565  SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2566  SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2567  SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2568  SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2569  SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2570 
2571  SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2572  SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2573  SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2574  SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2575  SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2576  SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2577  }
2578 
2579  if (avctx->debug & FF_DEBUG_BUGS)
2581  "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2582  s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2583  ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2584 
2585  if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2586  s->codec_id == AV_CODEC_ID_MPEG4 &&
2587  avctx->idct_algo == FF_IDCT_AUTO) {
2588  avctx->idct_algo = FF_IDCT_XVID;
2589  ff_mpv_idct_init(s);
2590  return 1;
2591  }
2592 
2593  return 0;
2594 }
2595 
2597 {
2598  MpegEncContext *s = &ctx->m;
2599  int time_incr, time_increment;
2600  int64_t pts;
2601 
2602  s->mcsel = 0;
2603  s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2604  if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2606  av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2607  s->low_delay = 0;
2608  }
2609 
2611  if (s->partitioned_frame)
2613  else
2615 
2616  time_incr = 0;
2617  while (get_bits1(gb) != 0)
2618  time_incr++;
2619 
2620  check_marker(s->avctx, gb, "before time_increment");
2621 
2622  if (ctx->time_increment_bits == 0 ||
2623  !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2625  "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);
2626 
2627  for (ctx->time_increment_bits = 1;
2628  ctx->time_increment_bits < 16;
2629  ctx->time_increment_bits++) {
2630  if (s->pict_type == AV_PICTURE_TYPE_P ||
2631  (s->pict_type == AV_PICTURE_TYPE_S &&
2632  ctx->vol_sprite_usage == GMC_SPRITE)) {
2633  if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2634  break;
2635  } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2636  break;
2637  }
2638 
2640  "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2641  if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2642  s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2644  }
2645  }
2646 
2647  if (IS_3IV1)
2648  time_increment = get_bits1(gb); // FIXME investigate further
2649  else
2650  time_increment = get_bits(gb, ctx->time_increment_bits);
2651 
2652  if (s->pict_type != AV_PICTURE_TYPE_B) {
2653  s->last_time_base = s->time_base;
2654  s->time_base += time_incr;
2655  s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
2656  if (s->workaround_bugs & FF_BUG_UMP4) {
2657  if (s->time < s->last_non_b_time) {
2658  /* header is not mpeg-4-compatible, broken encoder,
2659  * trying to workaround */
2660  s->time_base++;
2661  s->time += s->avctx->framerate.num;
2662  }
2663  }
2664  s->pp_time = s->time - s->last_non_b_time;
2665  s->last_non_b_time = s->time;
2666  } else {
2667  s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
2668  s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2669  if (s->pp_time <= s->pb_time ||
2670  s->pp_time <= s->pp_time - s->pb_time ||
2671  s->pp_time <= 0) {
2672  /* messed up order, maybe after seeking? skipping current B-frame */
2673  return FRAME_SKIPPED;
2674  }
2676 
2677  if (ctx->t_frame == 0)
2678  ctx->t_frame = s->pb_time;
2679  if (ctx->t_frame == 0)
2680  ctx->t_frame = 1; // 1/0 protection
2681  s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2682  ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2683  s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2684  ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2685  if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
2686  s->pb_field_time = 2;
2687  s->pp_field_time = 4;
2688  if (!s->progressive_sequence)
2689  return FRAME_SKIPPED;
2690  }
2691  }
2692 
2693  if (s->avctx->framerate.den)
2694  pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
2695  else
2696  pts = AV_NOPTS_VALUE;
2697  ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
2698 
2699  check_marker(s->avctx, gb, "before vop_coded");
2700 
2701  /* vop coded */
2702  if (get_bits1(gb) != 1) {
2703  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2704  av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2705  return FRAME_SKIPPED;
2706  }
2707  if (ctx->new_pred)
2708  decode_new_pred(ctx, gb);
2709 
2710  if (ctx->shape != BIN_ONLY_SHAPE &&
2711  (s->pict_type == AV_PICTURE_TYPE_P ||
2712  (s->pict_type == AV_PICTURE_TYPE_S &&
2713  ctx->vol_sprite_usage == GMC_SPRITE))) {
2714  /* rounding type for motion estimation */
2715  s->no_rounding = get_bits1(gb);
2716  } else {
2717  s->no_rounding = 0;
2718  }
2719  // FIXME reduced res stuff
2720 
2721  if (ctx->shape != RECT_SHAPE) {
2722  if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2723  skip_bits(gb, 13); /* width */
2724  check_marker(s->avctx, gb, "after width");
2725  skip_bits(gb, 13); /* height */
2726  check_marker(s->avctx, gb, "after height");
2727  skip_bits(gb, 13); /* hor_spat_ref */
2728  check_marker(s->avctx, gb, "after hor_spat_ref");
2729  skip_bits(gb, 13); /* ver_spat_ref */
2730  }
2731  skip_bits1(gb); /* change_CR_disable */
2732 
2733  if (get_bits1(gb) != 0)
2734  skip_bits(gb, 8); /* constant_alpha_value */
2735  }
2736 
2737  // FIXME complexity estimation stuff
2738 
2739  if (ctx->shape != BIN_ONLY_SHAPE) {
2740  skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2741  if (s->pict_type != AV_PICTURE_TYPE_I)
2742  skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2743  if (s->pict_type == AV_PICTURE_TYPE_B)
2744  skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2745 
2746  if (get_bits_left(gb) < 3) {
2747  av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2748  return AVERROR_INVALIDDATA;
2749  }
2750  ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2751  if (!s->progressive_sequence) {
2752  s->top_field_first = get_bits1(gb);
2753  s->alternate_scan = get_bits1(gb);
2754  } else
2755  s->alternate_scan = 0;
2756  }
2757 
2758  if (s->alternate_scan) {
2759  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2760  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2761  ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2762  ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2763  } else {
2764  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2765  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2766  ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2767  ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2768  }
2769 
2770  if (s->pict_type == AV_PICTURE_TYPE_S) {
2771  if((ctx->vol_sprite_usage == STATIC_SPRITE ||
2772  ctx->vol_sprite_usage == GMC_SPRITE)) {
2773  if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2774  return AVERROR_INVALIDDATA;
2775  if (ctx->sprite_brightness_change)
2776  av_log(s->avctx, AV_LOG_ERROR,
2777  "sprite_brightness_change not supported\n");
2778  if (ctx->vol_sprite_usage == STATIC_SPRITE)
2779  av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2780  } else {
2781  memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
2782  memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
2783  }
2784  }
2785 
2786  if (ctx->shape != BIN_ONLY_SHAPE) {
2787  s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2788  if (s->qscale == 0) {
2789  av_log(s->avctx, AV_LOG_ERROR,
2790  "Error, header damaged or not MPEG-4 header (qscale=0)\n");
2791  return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2792  }
2793 
2794  if (s->pict_type != AV_PICTURE_TYPE_I) {
2795  s->f_code = get_bits(gb, 3); /* fcode_for */
2796  if (s->f_code == 0) {
2797  av_log(s->avctx, AV_LOG_ERROR,
2798  "Error, header damaged or not MPEG-4 header (f_code=0)\n");
2799  s->f_code = 1;
2800  return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2801  }
2802  } else
2803  s->f_code = 1;
2804 
2805  if (s->pict_type == AV_PICTURE_TYPE_B) {
2806  s->b_code = get_bits(gb, 3);
2807  if (s->b_code == 0) {
2808  av_log(s->avctx, AV_LOG_ERROR,
2809  "Error, header damaged or not MPEG4 header (b_code=0)\n");
2810  s->b_code=1;
2811  return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
2812  }
2813  } else
2814  s->b_code = 1;
2815 
2816  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2817  av_log(s->avctx, AV_LOG_DEBUG,
2818  "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",
2819  s->qscale, s->f_code, s->b_code,
2820  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")),
2821  gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
2822  s->top_field_first, s->quarter_sample ? "q" : "h",
2823  s->data_partitioning, ctx->resync_marker,
2824  ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2825  1 - s->no_rounding, s->vo_type,
2826  ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
2827  ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2828  ctx->cplx_estimation_trash_b,
2829  s->time,
2830  time_increment
2831  );
2832  }
2833 
2834  if (!ctx->scalability) {
2835  if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2836  skip_bits1(gb); // vop shape coding type
2837  } else {
2838  if (ctx->enhancement_type) {
2839  int load_backward_shape = get_bits1(gb);
2840  if (load_backward_shape)
2841  av_log(s->avctx, AV_LOG_ERROR,
2842  "load backward shape isn't supported\n");
2843  }
2844  skip_bits(gb, 2); // ref_select_code
2845  }
2846  }
2847  /* detect buggy encoders which don't set the low_delay flag
2848  * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
2849  * easily (although it's buggy too) */
2850  if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
2851  ctx->divx_version == -1 && s->picture_number == 0) {
2852  av_log(s->avctx, AV_LOG_WARNING,
2853  "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2854  s->low_delay = 1;
2855  }
2856 
2857  s->picture_number++; // better than pic number==0 always ;)
2858 
2859  // FIXME add short header support
2860  s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2861  s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2862 
2863  if (s->workaround_bugs & FF_BUG_EDGE) {
2864  s->h_edge_pos = s->width;
2865  s->v_edge_pos = s->height;
2866  }
2867  return 0;
2868 }
2869 
2871 {
2872  int i, j, v;
2873 
2874  if (get_bits1(gb)) {
2875  if (get_bits_left(gb) < 64*8)
2876  return AVERROR_INVALIDDATA;
2877  /* intra_quantiser_matrix */
2878  for (i = 0; i < 64; i++) {
2879  v = get_bits(gb, 8);
2881  s->intra_matrix[j] = v;
2882  s->chroma_intra_matrix[j] = v;
2883  }
2884  }
2885 
2886  if (get_bits1(gb)) {
2887  if (get_bits_left(gb) < 64*8)
2888  return AVERROR_INVALIDDATA;
2889  /* non_intra_quantiser_matrix */
2890  for (i = 0; i < 64; i++) {
2891  get_bits(gb, 8);
2892  }
2893  }
2894 
2895  if (get_bits1(gb)) {
2896  if (get_bits_left(gb) < 64*8)
2897  return AVERROR_INVALIDDATA;
2898  /* chroma_intra_quantiser_matrix */
2899  for (i = 0; i < 64; i++) {
2900  v = get_bits(gb, 8);
2902  s->chroma_intra_matrix[j] = v;
2903  }
2904  }
2905 
2906  if (get_bits1(gb)) {
2907  if (get_bits_left(gb) < 64*8)
2908  return AVERROR_INVALIDDATA;
2909  /* chroma_non_intra_quantiser_matrix */
2910  for (i = 0; i < 64; i++) {
2911  get_bits(gb, 8);
2912  }
2913  }
2914 
2916  return 0;
2917 }
2918 
2920 {
2921  uint32_t startcode;
2922  uint8_t extension_type;
2923 
2924  startcode = show_bits_long(gb, 32);
2925  if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
2926 
2927  if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2928  skip_bits_long(gb, 32);
2929  extension_type = get_bits(gb, 4);
2930  if (extension_type == QUANT_MATRIX_EXT_ID)
2931  read_quant_matrix_ext(s, gb);
2932  }
2933  }
2934 }
2935 
2937 {
2938  MpegEncContext *s = &ctx->m;
2939 
2940  skip_bits(gb, 16); /* Time_code[63..48] */
2941  check_marker(s->avctx, gb, "after Time_code[63..48]");
2942  skip_bits(gb, 16); /* Time_code[47..32] */
2943  check_marker(s->avctx, gb, "after Time_code[47..32]");
2944  skip_bits(gb, 16); /* Time_code[31..16] */
2945  check_marker(s->avctx, gb, "after Time_code[31..16]");
2946  skip_bits(gb, 16); /* Time_code[15..0] */
2947  check_marker(s->avctx, gb, "after Time_code[15..0]");
2948  skip_bits(gb, 4); /* reserved_bits */
2949 }
2950 
2951 /**
2952  * Decode the next studio vop header.
2953  * @return <0 if something went wrong
2954  */
2956 {
2957  MpegEncContext *s = &ctx->m;
2958 
2959  if (get_bits_left(gb) <= 32)
2960  return 0;
2961 
2963 
2964  decode_smpte_tc(ctx, gb);
2965 
2966  skip_bits(gb, 10); /* temporal_reference */
2967  skip_bits(gb, 2); /* vop_structure */
2968  s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
2969  if (get_bits1(gb)) { /* vop_coded */
2970  skip_bits1(gb); /* top_field_first */
2971  skip_bits1(gb); /* repeat_first_field */
2972  s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
2973  }
2974 
2975  if (s->pict_type == AV_PICTURE_TYPE_I) {
2976  if (get_bits1(gb))
2978  }
2979 
2980  if (ctx->shape != BIN_ONLY_SHAPE) {
2981  s->alternate_scan = get_bits1(gb);
2983  s->dct_precision = get_bits(gb, 2);
2984  s->intra_dc_precision = get_bits(gb, 2);
2985  s->q_scale_type = get_bits1(gb);
2986  }
2987 
2988  if (s->alternate_scan) {
2993  } else {
2998  }
2999 
3001 
3003  extension_and_user_data(s, gb, 4);
3004 
3005  return 0;
3006 }
3007 
3009 {
3010  MpegEncContext *s = &ctx->m;
3011  int visual_object_type;
3012 
3013  skip_bits(gb, 4); /* visual_object_verid */
3014  visual_object_type = get_bits(gb, 4);
3015  if (visual_object_type != VOT_VIDEO_ID) {
3016  avpriv_request_sample(s->avctx, "VO type %u", visual_object_type);
3017  return AVERROR_PATCHWELCOME;
3018  }
3019 
3021  extension_and_user_data(s, gb, 1);
3022 
3023  return 0;
3024 }
3025 
3027 {
3028  MpegEncContext *s = &ctx->m;
3029  int width, height;
3030  int bits_per_raw_sample;
3031 
3032  // random_accessible_vol and video_object_type_indication have already
3033  // been read by the caller decode_vol_header()
3034  skip_bits(gb, 4); /* video_object_layer_verid */
3035  ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
3036  skip_bits(gb, 4); /* video_object_layer_shape_extension */
3037  skip_bits1(gb); /* progressive_sequence */
3038  if (ctx->shape != BIN_ONLY_SHAPE) {
3039  ctx->rgb = get_bits1(gb); /* rgb_components */
3040  s->chroma_format = get_bits(gb, 2); /* chroma_format */
3041  if (!s->chroma_format) {
3042  av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
3043  return AVERROR_INVALIDDATA;
3044  }
3045 
3046  bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
3047  if (bits_per_raw_sample == 10) {
3048  if (ctx->rgb) {
3050  }
3051  else {
3053  }
3054  }
3055  else {
3056  avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
3057  return AVERROR_PATCHWELCOME;
3058  }
3059  s->avctx->bits_per_raw_sample = bits_per_raw_sample;
3060  }
3061  if (ctx->shape == RECT_SHAPE) {
3062  check_marker(s->avctx, gb, "before video_object_layer_width");
3063  width = get_bits(gb, 14); /* video_object_layer_width */
3064  check_marker(s->avctx, gb, "before video_object_layer_height");
3065  height = get_bits(gb, 14); /* video_object_layer_height */
3066  check_marker(s->avctx, gb, "after video_object_layer_height");
3067 
3068  /* Do the same check as non-studio profile */
3069  if (width && height) {
3070  if (s->width && s->height &&
3071  (s->width != width || s->height != height))
3072  s->context_reinit = 1;
3073  s->width = width;
3074  s->height = height;
3075  }
3076  }
3077  s->aspect_ratio_info = get_bits(gb, 4);
3079  s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
3080  s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
3081  } else {
3083  }
3084  skip_bits(gb, 4); /* frame_rate_code */
3085  skip_bits(gb, 15); /* first_half_bit_rate */
3086  check_marker(s->avctx, gb, "after first_half_bit_rate");
3087  skip_bits(gb, 15); /* latter_half_bit_rate */
3088  check_marker(s->avctx, gb, "after latter_half_bit_rate");
3089  skip_bits(gb, 15); /* first_half_vbv_buffer_size */
3090  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
3091  skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
3092  skip_bits(gb, 11); /* first_half_vbv_buffer_size */
3093  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
3094  skip_bits(gb, 15); /* latter_half_vbv_occupancy */
3095  check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
3096  s->low_delay = get_bits1(gb);
3097  s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
3098 
3100  extension_and_user_data(s, gb, 2);
3101 
3102  return 0;
3103 }
3104 
3105 /**
3106  * Decode MPEG-4 headers.
3107  * @return <0 if no VOP found (or a damaged one)
3108  * FRAME_SKIPPED if a not coded VOP is found
3109  * 0 if a VOP is found
3110  */
3112 {
3113  MpegEncContext *s = &ctx->m;
3114  unsigned startcode, v;
3115  int ret;
3116  int vol = 0;
3117 
3118  /* search next start code */
3119  align_get_bits(gb);
3120 
3121  // If we have not switched to studio profile than we also did not switch bps
3122  // that means something else (like a previous instance) outside set bps which
3123  // would be inconsistant with the currect state, thus reset it
3124  if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3125  s->avctx->bits_per_raw_sample = 0;
3126 
3127  if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3128  skip_bits(gb, 24);
3129  if (get_bits(gb, 8) == 0xF0)
3130  goto end;
3131  }
3132 
3133  startcode = 0xff;
3134  for (;;) {
3135  if (get_bits_count(gb) >= gb->size_in_bits) {
3136  if (gb->size_in_bits == 8 &&
3137  (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3138  av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3139  return FRAME_SKIPPED; // divx bug
3140  } else
3141  return AVERROR_INVALIDDATA; // end of stream
3142  }
3143 
3144  /* use the bits after the test */
3145  v = get_bits(gb, 8);
3146  startcode = ((startcode << 8) | v) & 0xffffffff;
3147 
3148  if ((startcode & 0xFFFFFF00) != 0x100)
3149  continue; // no startcode
3150 
3151  if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3152  av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
3153  if (startcode <= 0x11F)
3154  av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
3155  else if (startcode <= 0x12F)
3156  av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
3157  else if (startcode <= 0x13F)
3158  av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
3159  else if (startcode <= 0x15F)
3160  av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
3161  else if (startcode <= 0x1AF)
3162  av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
3163  else if (startcode == 0x1B0)
3164  av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
3165  else if (startcode == 0x1B1)
3166  av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
3167  else if (startcode == 0x1B2)
3168  av_log(s->avctx, AV_LOG_DEBUG, "User Data");
3169  else if (startcode == 0x1B3)
3170  av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
3171  else if (startcode == 0x1B4)
3172  av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
3173  else if (startcode == 0x1B5)
3174  av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
3175  else if (startcode == 0x1B6)
3176  av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
3177  else if (startcode == 0x1B7)
3178  av_log(s->avctx, AV_LOG_DEBUG, "slice start");
3179  else if (startcode == 0x1B8)
3180  av_log(s->avctx, AV_LOG_DEBUG, "extension start");
3181  else if (startcode == 0x1B9)
3182  av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
3183  else if (startcode == 0x1BA)
3184  av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
3185  else if (startcode == 0x1BB)
3186  av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
3187  else if (startcode == 0x1BC)
3188  av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
3189  else if (startcode == 0x1BD)
3190  av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
3191  else if (startcode == 0x1BE)
3192  av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
3193  else if (startcode == 0x1BF)
3194  av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
3195  else if (startcode == 0x1C0)
3196  av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
3197  else if (startcode == 0x1C1)
3198  av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
3199  else if (startcode == 0x1C2)
3200  av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
3201  else if (startcode == 0x1C3)
3202  av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
3203  else if (startcode <= 0x1C5)
3204  av_log(s->avctx, AV_LOG_DEBUG, "reserved");
3205  else if (startcode <= 0x1FF)
3206  av_log(s->avctx, AV_LOG_DEBUG, "System start");
3207  av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
3208  }
3209 
3210  if (startcode >= 0x120 && startcode <= 0x12F) {
3211  if (vol) {
3212  av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3213  continue;
3214  }
3215  vol++;
3216  if ((ret = decode_vol_header(ctx, gb)) < 0)
3217  return ret;
3218  } else if (startcode == USER_DATA_STARTCODE) {
3219  decode_user_data(ctx, gb);
3220  } else if (startcode == GOP_STARTCODE) {
3221  mpeg4_decode_gop_header(s, gb);
3222  } else if (startcode == VOS_STARTCODE) {
3223  int profile, level;
3224  mpeg4_decode_profile_level(s, gb, &profile, &level);
3225  if (profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO &&
3226  (level > 0 && level < 9)) {
3227  s->studio_profile = 1;
3229  extension_and_user_data(s, gb, 0);
3230  } else if (s->studio_profile) {
3231  avpriv_request_sample(s->avctx, "Mixes studio and non studio profile\n");
3232  return AVERROR_PATCHWELCOME;
3233  }
3234  s->avctx->profile = profile;
3235  s->avctx->level = level;
3236  } else if (startcode == VISUAL_OBJ_STARTCODE) {
3237  if (s->studio_profile) {
3238  if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3239  return ret;
3240  } else
3242  } else if (startcode == VOP_STARTCODE) {
3243  break;
3244  }
3245 
3246  align_get_bits(gb);
3247  startcode = 0xff;
3248  }
3249 
3250 end:
3252  s->low_delay = 1;
3253  s->avctx->has_b_frames = !s->low_delay;
3254 
3255  if (s->studio_profile) {
3256  if (!s->avctx->bits_per_raw_sample) {
3257  av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n");
3258  return AVERROR_INVALIDDATA;
3259  }
3260  return decode_studio_vop_header(ctx, gb);
3261  } else
3262  return decode_vop_header(ctx, gb);
3263 }
3264 
3266  static int done = 0;
3267 
3268  if (!done) {
3275  INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
3276  &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3277  &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
3278  INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
3279  &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3280  &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
3281  INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
3282  &ff_sprite_trajectory_tab[0][1], 4, 2,
3283  &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
3284  INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
3285  &ff_mb_type_b_tab[0][1], 2, 1,
3286  &ff_mb_type_b_tab[0][0], 2, 1, 16);
3287  done = 1;
3288  }
3289 }
3290 
3291 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
3292 {
3293  Mpeg4DecContext *ctx = avctx->priv_data;
3294  MpegEncContext *s = &ctx->m;
3295 
3296  /* divx 5.01+ bitstream reorder stuff */
3297  /* Since this clobbers the input buffer and hwaccel codecs still need the
3298  * data during hwaccel->end_frame we should not do this any earlier */
3299  if (s->divx_packed) {
3300  int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
3301  int startcode_found = 0;
3302 
3303  if (buf_size - current_pos > 7) {
3304 
3305  int i;
3306  for (i = current_pos; i < buf_size - 4; i++)
3307 
3308  if (buf[i] == 0 &&
3309  buf[i + 1] == 0 &&
3310  buf[i + 2] == 1 &&
3311  buf[i + 3] == 0xB6) {
3312  startcode_found = !(buf[i + 4] & 0x40);
3313  break;
3314  }
3315  }
3316 
3317  if (startcode_found) {
3318  if (!ctx->showed_packed_warning) {
3319  av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
3320  "wasteful way to store B-frames ('packed B-frames'). "
3321  "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
3322  ctx->showed_packed_warning = 1;
3323  }
3326  buf_size - current_pos);
3327  if (!s->bitstream_buffer) {
3328  s->bitstream_buffer_size = 0;
3329  return AVERROR(ENOMEM);
3330  }
3331  memcpy(s->bitstream_buffer, buf + current_pos,
3332  buf_size - current_pos);
3333  s->bitstream_buffer_size = buf_size - current_pos;
3334  }
3335  }
3336 
3337  return 0;
3338 }
3339 
3340 #if HAVE_THREADS
3341 static int mpeg4_update_thread_context(AVCodecContext *dst,
3342  const AVCodecContext *src)
3343 {
3344  Mpeg4DecContext *s = dst->priv_data;
3345  const Mpeg4DecContext *s1 = src->priv_data;
3346  int init = s->m.context_initialized;
3347 
3348  int ret = ff_mpeg_update_thread_context(dst, src);
3349 
3350  if (ret < 0)
3351  return ret;
3352 
3353  memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
3354 
3355  if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
3356  ff_xvid_idct_init(&s->m.idsp, dst);
3357 
3358  return 0;
3359 }
3360 #endif
3361 
3363 {
3364  int i, ret;
3365 
3366  for (i = 0; i < 12; i++) {
3367  ret = init_vlc(&ctx->studio_intra_tab[i], STUDIO_INTRA_BITS, 22,
3368  &ff_mpeg4_studio_intra[i][0][1], 4, 2,
3369  &ff_mpeg4_studio_intra[i][0][0], 4, 2,
3370  0);
3371 
3372  if (ret < 0)
3373  return ret;
3374  }
3375 
3376  ret = init_vlc(&ctx->studio_luma_dc, STUDIO_INTRA_BITS, 19,
3377  &ff_mpeg4_studio_dc_luma[0][1], 4, 2,
3378  &ff_mpeg4_studio_dc_luma[0][0], 4, 2,
3379  0);
3380  if (ret < 0)
3381  return ret;
3382 
3383  ret = init_vlc(&ctx->studio_chroma_dc, STUDIO_INTRA_BITS, 19,
3384  &ff_mpeg4_studio_dc_chroma[0][1], 4, 2,
3385  &ff_mpeg4_studio_dc_chroma[0][0], 4, 2,
3386  0);
3387  if (ret < 0)
3388  return ret;
3389 
3390  return 0;
3391 }
3392 
3394 {
3395  Mpeg4DecContext *ctx = avctx->priv_data;
3396  MpegEncContext *s = &ctx->m;
3397  int ret;
3398 
3399  ctx->divx_version =
3400  ctx->divx_build =
3401  ctx->xvid_build =
3402  ctx->lavc_build = -1;
3403 
3404  if ((ret = ff_h263_decode_init(avctx)) < 0)
3405  return ret;
3406 
3408  if ((ret = init_studio_vlcs(ctx)) < 0)
3409  return ret;
3410 
3411  s->h263_pred = 1;
3412  s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
3414  ctx->time_increment_bits = 4; /* default value for broken headers */
3415 
3417  avctx->internal->allocate_progress = 1;
3418 
3419  return 0;
3420 }
3421 
3423 {
3424  Mpeg4DecContext *ctx = avctx->priv_data;
3425  int i;
3426 
3427  if (!avctx->internal->is_copy) {
3428  for (i = 0; i < 12; i++)
3429  ff_free_vlc(&ctx->studio_intra_tab[i]);
3430 
3431  ff_free_vlc(&ctx->studio_luma_dc);
3433  }
3434 
3435  return ff_h263_decode_end(avctx);
3436 }
3437 
3438 static const AVOption mpeg4_options[] = {
3439  {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
3440  {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
3441  {NULL}
3442 };
3443 
3444 static const AVClass mpeg4_class = {
3445  .class_name = "MPEG4 Video Decoder",
3446  .item_name = av_default_item_name,
3447  .option = mpeg4_options,
3448  .version = LIBAVUTIL_VERSION_INT,
3449 };
3450 
3452  .name = "mpeg4",
3453  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
3454  .type = AVMEDIA_TYPE_VIDEO,
3455  .id = AV_CODEC_ID_MPEG4,
3456  .priv_data_size = sizeof(Mpeg4DecContext),
3457  .init = decode_init,
3458  .close = decode_end,
3463  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
3464  .flush = ff_mpeg_flush,
3465  .max_lowres = 3,
3468  .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
3469  .priv_class = &mpeg4_class,
3470  .hw_configs = (const AVCodecHWConfigInternal*[]) {
3471 #if CONFIG_MPEG4_NVDEC_HWACCEL
3472  HWACCEL_NVDEC(mpeg4),
3473 #endif
3474 #if CONFIG_MPEG4_VAAPI_HWACCEL
3475  HWACCEL_VAAPI(mpeg4),
3476 #endif
3477 #if CONFIG_MPEG4_VDPAU_HWACCEL
3478  HWACCEL_VDPAU(mpeg4),
3479 #endif
3480 #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
3481  HWACCEL_VIDEOTOOLBOX(mpeg4),
3482 #endif
3483  NULL
3484  },
3485 };
int bitstream_buffer_size
Definition: mpegvideo.h:416
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:405
uint16_t sprite_traj[4][2]
sprite trajectory points
Definition: mpeg4video.h:84
#define AV_EF_AGGRESSIVE
consider things that a sane encoder should not do as an error
Definition: avcodec.h:2660
#define ff_tlog(ctx,...)
Definition: internal.h:75
IDCTDSPContext idsp
Definition: mpegvideo.h:230
#define NULL
Definition: coverity.c:32
#define SLICE_ERROR
Definition: mpegvideo.h:514
AVRational framerate
Definition: avcodec.h:3042
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2233
static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
#define EXT_STARTCODE
Definition: mpeg4video.h:67
static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
int aspect_ratio_info
Definition: mpegvideo.h:402
int picture_number
Definition: mpegvideo.h:127
#define MB_TYPE_L1
Definition: mpegutils.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define FF_BUG_XVID_ILACE
Definition: avcodec.h:2551
ScanTable intra_v_scantable
Definition: mpegvideo.h:93
static int shift(int a, int b)
Definition: sonic.c:82
S(GMC)-VOP MPEG-4.
Definition: avutil.h:277
RLTable ff_mpeg4_rl_intra
Definition: mpeg4data.h:109
static float alpha(float a)
const uint8_t ff_mpeg4_c_dc_scale_table[32]
Definition: mpeg4data.h:363
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:524
static const uint8_t ac_state_tab[22][2]
static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
#define CBPY_VLC_BITS
Definition: h263.h:39
const uint16_t ff_mpeg4_studio_dc_chroma[19][2]
Definition: mpeg4data.h:385
AVOption.
Definition: opt.h:246
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:269
static void flush(AVCodecContext *avctx)
#define MB_TYPE_DIRECT2
Definition: mpegutils.h:59
#define HWACCEL_NVDEC(codec)
Definition: hwaccel.h:71
int sprite_warping_accuracy
Definition: mpegvideo.h:403
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG-1 & B-frame MPEG-4
Definition: mpegvideo.h:278
av_cold void ff_mpeg4videodec_static_init(void)
float re
Definition: fft.c:82
int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
#define SLICE_NOEND
no end marker or error found but mb count exceeded
Definition: mpegvideo.h:516
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
Decode the next video packet.
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:212
#define MB_TYPE_B_VLC_BITS
Definition: mpeg4videodec.c:46
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
VLC ff_h263_intra_MCBPC_vlc
Definition: ituh263dec.c:98
int resync_marker
could this stream contain resync markers
Definition: mpeg4video.h:91
static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:301
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: vlc.h:75
static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
int16_t(*[3] ac_val)[16]
used for MPEG-4 AC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:194
VLC ff_h263_cbpy_vlc
Definition: ituh263dec.c:100
#define SKIP_COUNTER(name, gb, num)
Definition: get_bits.h:168
uint16_t chroma_inter_matrix[64]
Definition: mpegvideo.h:303
RLTable ff_rvlc_rl_inter
Definition: mpeg4data.h:214
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2150
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
Definition: get_bits.h:415
int num
Numerator.
Definition: rational.h:59
uint8_t * bitstream_buffer
Definition: mpegvideo.h:415
enum AVCodecID codec_id
Definition: mpegvideo.h:112
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
Definition: avcodec.h:2659
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:388
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
void(* clear_blocks)(int16_t *blocks)
Definition: blockdsp.h:37
static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
#define SIMPLE_STUDIO_VO_TYPE
Definition: mpeg4video.h:44
#define FF_BUG_HPEL_CHROMA
Definition: avcodec.h:2560
const uint8_t * buffer
Definition: get_bits.h:57
void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
Predict the ac.
Definition: mpeg4videodec.c:67
int av_log2(unsigned v)
Definition: intmath.c:26
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1898
static av_cold int decode_init(AVCodecContext *avctx)
#define MB_TYPE_INTRA
Definition: mpegutils.h:73
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1729
#define FF_PROFILE_MPEG4_SIMPLE_STUDIO
Definition: avcodec.h:2919
#define MB_TYPE_16x8
Definition: mpegutils.h:55
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:70
int studio_profile
Definition: mpegvideo.h:384
int real_sprite_warping_points
Definition: mpegvideo.h:396
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition: avcodec.h:2653
mpegvideo header.
#define DC_VLC_BITS
Definition: mpeg4videodec.c:45
#define DC_MARKER
Definition: mpeg4video.h:59
uint8_t permutated[64]
Definition: idctdsp.h:33
uint8_t run
Definition: svq3.c:206
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2743
#define ER_MV_ERROR
int padding_bug_score
used to detect the VERY common padding bug in MPEG-4
Definition: mpegvideo.h:411
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
#define SLICE_OK
Definition: mpegvideo.h:513
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:133
#define src
Definition: vp8dsp.c:254
static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
int profile
profile
Definition: avcodec.h:2845
static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
AVCodec.
Definition: avcodec.h:3410
int time_base
time in seconds of last I,P,S Frame
Definition: mpegvideo.h:389
RLTable.
Definition: rl.h:39
int qscale
QP.
Definition: mpegvideo.h:204
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
const AVProfile ff_mpeg4_video_profiles[]
Definition: profiles.c:106
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:307
int field_select[2][2]
Definition: mpegvideo.h:277
int block_wrap[6]
Definition: mpegvideo.h:294
int quant_precision
Definition: mpegvideo.h:400
#define STUDIO_INTRA_BITS
Definition: mpeg4videodec.c:47
void ff_clean_intra_table_entries(MpegEncContext *s)
Clean dc, ac, coded_block for the current non-intra MB.
Definition: mpegvideo.c:1902
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1642
#define SLICE_START_CODE
Definition: mpegvideo.h:75
int vol_sprite_usage
Definition: mpeg4video.h:80
static int16_t block[64]
Definition: dct.c:115
int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
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:72
#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: avcodec.h:986
#define USES_LIST(a, list)
Definition: mpegutils.h:99
#define USER_DATA_STARTCODE
Definition: mpeg4video.h:62
int mb_num_left
number of MBs left in this video packet (for partitioned Slices only)
Definition: mpegvideo.h:359
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
int context_reinit
Definition: mpegvideo.h:561
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define MV_DIRECT
bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4) ...
Definition: mpegvideo.h:264
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
float delta
AVOptions.
const uint16_t ff_mpeg4_resync_prefix[8]
Definition: mpeg4data.h:368
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2601
const uint16_t ff_sprite_trajectory_tab[15][2]
Definition: mpeg4data.h:326
#define VOP_STARTCODE
Definition: mpeg4video.h:65
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
#define BIN_ONLY_SHAPE
Definition: mpeg4video.h:35
#define IS_3IV1
Definition: mpeg4video.h:192
uint8_t * pred_dir_table
used to store pred_dir for partitioned decoding
Definition: mpegvideo.h:200
Multithreading support functions.
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:880
#define FF_BUG_EDGE
Definition: avcodec.h:2559
int interlaced_dct
Definition: mpegvideo.h:490
static void mpeg4_load_default_matrices(MpegEncContext *s)
#define MB_TYPE_16x16
Definition: mpegutils.h:54
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:180
#define CHROMA_420
Definition: mpegvideo.h:482
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
Definition: mpeg4data.h:41
#define MB_TYPE_ACPRED
Definition: mpegutils.h:60
int intra_dc_precision
Definition: mpegvideo.h:463
#define RECT_SHAPE
Definition: mpeg4video.h:33
#define height
static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode the user data stuff in the header.
const uint16_t ff_mpeg4_studio_intra[12][22][2]
Definition: mpeg4data.h:393
#define FF_BUG_QPEL_CHROMA2
Definition: avcodec.h:2557
#define FF_BUG_STD_QPEL
Definition: avcodec.h:2556
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:200
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:46
#define ER_MV_END
#define ff_dlog(a,...)
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:330
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:129
int codec_tag
internal codec_tag upper case converted from avctx codec_tag
Definition: mpegvideo.h:120
int intra_dc_threshold
QP above which the ac VLC should be used for intra dc.
Definition: mpeg4video.h:101
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2157
#define MAX_LEVEL
Definition: rl.h:36
static int ff_mpeg4_pred_dc(MpegEncContext *s, int n, int level, int *dir_ptr, int encoding)
Predict the dc.
Definition: mpeg4video.h:202
#define FF_BUG_IEDGE
Definition: avcodec.h:2564
#define av_log(a,...)
#define MOTION_MARKER
Definition: mpeg4video.h:58
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:733
#define FF_IDCT_AUTO
Definition: avcodec.h:2716
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:2297
MpegEncContext m
Definition: mpeg4video.h:75
int sprite_offset[2][2]
sprite offset[isChroma][isMVY]
Definition: mpegvideo.h:397
#define ROUNDED_DIV(a, b)
Definition: common.h:56
ThreadFrame tf
Definition: mpegpicture.h:47
#define FF_BUG_DIRECT_BLOCKSIZE
Definition: avcodec.h:2558
av_cold void ff_xvid_idct_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: xvididct.c:333
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:596
#define FF_IDCT_XVID
Definition: avcodec.h:2723
int16_t * dc_val[3]
used for MPEG-4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:187
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:161
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1809
uint8_t * mbskip_table
Definition: mpegpicture.h:59
#define QUANT_MATRIX_EXT_ID
Definition: mpeg4video.h:69
int last_dc[3]
last DC values for MPEG-1
Definition: mpegvideo.h:185
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: vlc.h:38
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:195
int partitioned_frame
is current frame partitioned
Definition: mpegvideo.h:405
#define STATIC_SPRITE
Definition: mpeg4video.h:55
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:136
int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
Decode the first and second partition.
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
ERContext er
Definition: mpegvideo.h:563
static int get_amv(Mpeg4DecContext *ctx, int n)
Get the average motion vector for a GMC MB.
const char * r
Definition: vf_curves.c:111
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1600
#define SIMPLE_VO_TYPE
Definition: mpeg4video.h:38
#define INTER_MCBPC_VLC_BITS
Definition: h263.h:38
#define FF_BUG_NO_PADDING
Definition: avcodec.h:2553
uint16_t width
Definition: gdv.c:47
#define MB_TYPE_GMC
Definition: mpegutils.h:61
#define FF_DEBUG_BUGS
Definition: avcodec.h:2617
GLsizei GLsizei * length
Definition: opengl_enc.c:115
void ff_mpeg4_init_direct_mv(MpegEncContext *s)
Definition: mpeg4video.c:71
const char * name
Name of the codec implementation.
Definition: avcodec.h:3417
#define IS_SKIP(a)
Definition: mpegutils.h:81
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:375
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:401
int low_delay
no reordering needed / has no B-frames
Definition: mpegvideo.h:406
#define SET_QPEL_FUNC(postfix1, postfix2)
GetBitContext gb
Definition: mpegvideo.h:448
#define CLOSE_READER(name, gb)
Definition: get_bits.h:132
#define INIT_VLC_RL(rl, static_size)
Definition: rl.h:63
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1017
#define GET_RL_VLC(level, run, name, gb, table, bits,max_depth, need_update)
Definition: get_bits.h:517
Definition: vlc.h:26
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo.c:2266
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:176
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:225
int resync_mb_x
x position of last resync marker
Definition: mpegvideo.h:356
const uint8_t ff_alternate_horizontal_scan[64]
Definition: mpegvideodata.c:89
int dct_precision
number of bits to represent the fractional part of time (encoder only)
Definition: mpegvideo.h:385
int ff_h263_decode_init(AVCodecContext *avctx)
Definition: h263dec.c:68
common internal API header
int sprite_delta[2][2]
sprite_delta [isY][isMVY]
Definition: mpegvideo.h:398
static av_cold int decode_end(AVCodecContext *avctx)
#define ADV_SIMPLE_VO_TYPE
Definition: mpeg4video.h:46
const uint8_t ff_mpeg4_DCtab_lum[13][2]
Definition: mpeg4data.h:35
uint8_t ff_mpeg4_static_rl_table_store[3][2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: mpeg4video.c:28
VLC ff_h263_inter_MCBPC_vlc
Definition: ituh263dec.c:99
int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: h263dec.c:421
#define CHROMA_422
Definition: mpegvideo.h:483
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: avcodec.h:955
static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
int progressive_frame
Definition: mpegvideo.h:488
static const int mb_type_b_map[4]
Definition: mpeg4videodec.c:55
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2644
#define FFMIN(a, b)
Definition: common.h:96
const uint8_t ff_mpeg4_y_dc_scale_table[32]
Definition: mpeg4data.h:359
#define IS_DIRECT(a)
Definition: mpegutils.h:84
static VLC dc_lum
Definition: mpeg4videodec.c:51
static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
Decode first partition.
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:2715
static int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
Decode the dc value.
uint8_t w
Definition: llviddspenc.c:38
int16_t(*[2] motion_val)[2]
Definition: mpegpicture.h:53
#define GRAY_SHAPE
Definition: mpeg4video.h:36
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2846
#define ER_DC_END
enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[]
Definition: h263dec.c:729
int alternate_scan
Definition: mpegvideo.h:470
AVCodec ff_mpeg4_decoder
unsigned int allocated_bitstream_buffer_size
Definition: mpegvideo.h:417
int size_in_bits
Definition: get_bits.h:59
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
int use_intra_dc_vlc
Definition: mpeg4video.h:98
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2129
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:304
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
int level
level
Definition: avcodec.h:2955
int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
Decode the next video packet.
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:182
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:563
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:86
int n
Definition: avisynth_c.h:684
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:96
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1651
int vol_control_parameters
does the stream contain the low_delay flag, used to work around buggy encoders.
Definition: mpeg4video.h:113
const int16_t ff_mpeg4_default_non_intra_matrix[64]
Definition: mpeg4data.h:348
#define FRAME_SKIPPED
Return value for header parsers if frame is not coded.
Definition: mpegutils.h:34
uint8_t * mbintra_table
used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
Definition: mpegvideo.h:198
#define MB_TYPE_INTERLACED
Definition: mpegutils.h:58
#define FF_BUG_QPEL_CHROMA
Definition: avcodec.h:2555
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: mpegvideo.c:490
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:194
#define VOT_VIDEO_ID
Definition: mpeg4video.h:48
#define SPRITE_TRAJ_VLC_BITS
Definition: mpeg4videodec.c:44
#define FF_BUG_UMP4
Definition: avcodec.h:2552
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:293
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:503
static av_cold int init_studio_vlcs(Mpeg4DecContext *ctx)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define MB_TYPE_8x8
Definition: mpegutils.h:57
static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n)
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:266
int first_slice_line
used in MPEG-4 too to handle resync markers
Definition: mpegvideo.h:436
int frame_pred_frame_dct
Definition: mpegvideo.h:464
static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
decode partition C of one MB.
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:263
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
uint16_t inter_matrix[64]
Definition: mpegvideo.h:302
#define FF_ASPECT_EXTENDED
Definition: h263.h:30
static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
BlockDSPContext bdsp
Definition: mpegvideo.h:226
int debug
debug
Definition: avcodec.h:2600
main external API structure.
Definition: avcodec.h:1520
static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
#define FASTDIV(a, b)
Definition: mathops.h:202
#define RSHIFT(a, b)
Definition: common.h:54
ScanTable intra_scantable
Definition: mpegvideo.h:91
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:100
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
Definition: get_bits.h:226
#define OPEN_READER(name, gb)
Definition: get_bits.h:121
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: rl.c:39
void * buf
Definition: avisynth_c.h:690
#define SLICE_END
end marker found
Definition: mpegvideo.h:515
int data_partitioning
data partitioning flag from header
Definition: mpegvideo.h:404
int showed_packed_warning
flag for having shown the warning about invalid Divx B-frames
Definition: mpeg4video.h:110
int progressive_sequence
Definition: mpegvideo.h:456
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:321
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:346
Describe the class of an AVClass context structure.
Definition: log.h:67
ScanTable intra_h_scantable
Definition: mpegvideo.h:92
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:314
static const AVProfile profiles[]
uint8_t * cbp_table
used to store cbp, ac_pred for partitioned decoding
Definition: mpegvideo.h:199
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2143
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2136
static void reset_studio_dc_predictors(MpegEncContext *s)
#define VOT_STILL_TEXTURE_ID
Definition: mpeg4video.h:49
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode MPEG-4 headers.
int context_initialized
Definition: mpegvideo.h:124
#define GET_CACHE(name, gb)
Definition: get_bits.h:198
#define AV_EF_IGNORE_ERR
ignore errors and continue
Definition: avcodec.h:2657
RLTable ff_h263_rl_inter
Definition: h263data.c:161
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
#define s1
Definition: regdef.h:38
#define SKIP_CACHE(name, gb, num)
Definition: get_bits.h:163
const int16_t ff_mpeg4_default_intra_matrix[64]
Definition: mpeg4data.h:337
#define ER_DC_ERROR
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:151
int f_code
forward MV resolution
Definition: mpegvideo.h:238
#define MB_TYPE_SKIP
Definition: mpegutils.h:62
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:354
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my)
Definition: mpeg4video.c:117
int sprite_shift[2]
sprite shift [isChroma]
Definition: mpeg4video.h:86
#define MV_DIR_FORWARD
Definition: mpegvideo.h:262
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:212
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
mfxU16 profile
Definition: qsvenc.c:44
#define AV_CODEC_CAP_TRUNCATED
Definition: avcodec.h:962
const uint8_t ff_mpeg4_dc_threshold[8]
Definition: mpeg4data.h:372
#define FF_BUG_AUTODETECT
autodetection
Definition: avcodec.h:2550
int h263_pred
use MPEG-4/H.263 ac/dc predictions
Definition: mpegvideo.h:105
#define HWACCEL_VAAPI(codec)
Definition: hwaccel.h:73
static int64_t pts
int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s)
Definition: mpeg4video.c:30
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:373
const AVRational ff_h263_pixel_aspect[16]
Definition: h263data.c:275
uint8_t level
Definition: svq3.c:207
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:276
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:131
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:502
MpegEncContext.
Definition: mpegvideo.h:81
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:183
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int8_t * qscale_table
Definition: mpegpicture.h:50
#define MAX_RUN
Definition: rl.h:35
struct AVCodecContext * avctx
Definition: mpegvideo.h:98
static int mpeg4_is_resync(Mpeg4DecContext *ctx)
check if the next stuff is a resync marker or the end.
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:195
#define VISUAL_OBJ_STARTCODE
Definition: mpeg4video.h:64
static VLC sprite_trajectory
Definition: mpeg4videodec.c:52
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
#define FF_BUG_AMV
Definition: avcodec.h:2554
VLC studio_intra_tab[12]
Definition: mpeg4video.h:118
#define MB_TYPE_L0L1
Definition: mpegutils.h:69
static const AVOption mpeg4_options[]
common internal api header.
int32_t(* block32)[12][64]
Definition: mpegvideo.h:511
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:130
if(ret< 0)
Definition: vf_mcdeint.c:279
#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:60
#define TEX_VLC_BITS
Definition: dv.h:96
static VLC dc_chrom
Definition: mpeg4videodec.c:51
Bi-dir predicted.
Definition: avutil.h:276
static VLC mb_type_b_vlc
Definition: mpeg4videodec.c:53
static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
decode second partition.
int den
Denominator.
Definition: rational.h:60
const uint8_t ff_alternate_vertical_scan[64]
static const AVClass mpeg4_class
#define IS_INTRA(x, y)
int ff_h263_decode_end(AVCodecContext *avctx)
Definition: h263dec.c:155
void * priv_data
Definition: avcodec.h:1547
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:2614
static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode the next studio vop header.
#define IS_8X8(a)
Definition: mpegutils.h:89
int len
const uint16_t ff_mpeg4_studio_dc_luma[19][2]
Definition: mpeg4data.h:377
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1555
int resync_mb_y
y position of last resync marker
Definition: mpegvideo.h:357
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:507
#define CORE_STUDIO_VO_TYPE
Definition: mpeg4video.h:45
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:168
#define IS_ACPRED(a)
Definition: mpegutils.h:94
static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb)
static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:472
int chroma_qscale
chroma QP
Definition: mpegvideo.h:205
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
static const uint8_t mpeg4_block_count[4]
Definition: mpeg4video.h:125
int ff_h263_decode_motion(MpegEncContext *s, int pred, int f_code)
Definition: ituh263dec.c:270
#define av_uninit(x)
Definition: attributes.h:148
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
RLTable ff_rvlc_rl_intra
Definition: mpeg4data.h:318
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:300
uint32_t * mb_type
types and macros are defined in mpegutils.h
Definition: mpegpicture.h:56
#define HWACCEL_VDPAU(codec)
Definition: hwaccel.h:75
int time_increment_bits
number of bits to represent the fractional part of time
Definition: mpeg4video.h:78
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
Definition: mpegvideo.h:119
ScanTable inter_scantable
if inter == intra then intra should be used to reduce the cache usage
Definition: mpegvideo.h:90
static void next_start_code_studio(GetBitContext *gb)
int(* decode_mb)(struct MpegEncContext *s, int16_t block[12][64])
Definition: mpegvideo.h:509
static int mpeg_get_qscale(MpegEncContext *s)
Definition: mpegvideo.h:757
int num_sprite_warping_points
Definition: mpeg4video.h:82
#define stride
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
Definition: mpegvideo.h:267
int b_code
backward MV resolution for B-frames (MPEG-4)
Definition: mpegvideo.h:239
float min
#define FF_BUG_DC_CLIP
Definition: avcodec.h:2561
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
const uint8_t ff_mb_type_b_tab[4][2]
Definition: mpeg4data.h:332
static int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block, int n, int coded, int intra, int rvlc)
Decode a block.
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:961
#define MB_TYPE_L0
Definition: mpegutils.h:67
for(j=16;j >0;--j)
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
Predicted.
Definition: avutil.h:275
#define INTRA_MCBPC_VLC_BITS
Definition: h263.h:37
#define GOP_STARTCODE
Definition: mpeg4video.h:63
#define VOS_STARTCODE
Definition: mpeg4video.h:61
#define GMC_SPRITE
Definition: mpeg4video.h:56