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