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