FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpeg4videodec.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 decoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #define UNCHECKED_BITSTREAM_READER 1
24 
25 #include "libavutil/internal.h"
26 #include "libavutil/opt.h"
27 #include "error_resilience.h"
28 #include "hwaccel.h"
29 #include "idctdsp.h"
30 #include "internal.h"
31 #include "mpegutils.h"
32 #include "mpegvideo.h"
33 #include "mpegvideodata.h"
34 #include "mpeg4video.h"
35 #include "h263.h"
36 #include "profiles.h"
37 #include "thread.h"
38 #include "xvididct.h"
39 
40 /* The defines below define the number of bits that are read at once for
41  * reading vlc values. Changing these may improve speed and data cache needs
42  * be aware though that decreasing them may need the number of stages that is
43  * passed to get_vlc* to be increased. */
44 #define SPRITE_TRAJ_VLC_BITS 6
45 #define DC_VLC_BITS 9
46 #define MB_TYPE_B_VLC_BITS 4
47 
51 
52 static const int mb_type_b_map[4] = {
57 };
58 
59 /**
60  * Predict the ac.
61  * @param n block index (0-3 are luma, 4-5 are chroma)
62  * @param dir the ac prediction direction
63  */
64 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
65 {
66  int i;
67  int16_t *ac_val, *ac_val1;
68  int8_t *const qscale_table = s->current_picture.qscale_table;
69 
70  /* find prediction */
71  ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
72  ac_val1 = ac_val;
73  if (s->ac_pred) {
74  if (dir == 0) {
75  const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
76  /* left prediction */
77  ac_val -= 16;
78 
79  if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
80  n == 1 || n == 3) {
81  /* same qscale */
82  for (i = 1; i < 8; i++)
83  block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
84  } else {
85  /* different qscale, we must rescale */
86  for (i = 1; i < 8; i++)
87  block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
88  }
89  } else {
90  const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
91  /* top prediction */
92  ac_val -= 16 * s->block_wrap[n];
93 
94  if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
95  n == 2 || n == 3) {
96  /* same qscale */
97  for (i = 1; i < 8; i++)
98  block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
99  } else {
100  /* different qscale, we must rescale */
101  for (i = 1; i < 8; i++)
102  block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
103  }
104  }
105  }
106  /* left copy */
107  for (i = 1; i < 8; i++)
108  ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
109 
110  /* top copy */
111  for (i = 1; i < 8; i++)
112  ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
113 }
114 
115 /**
116  * check if the next stuff is a resync marker or the end.
117  * @return 0 if not
118  */
120 {
121  MpegEncContext *s = &ctx->m;
122  int bits_count = get_bits_count(&s->gb);
123  int v = show_bits(&s->gb, 16);
124 
126  return 0;
127 
128  while (v <= 0xFF) {
129  if (s->pict_type == AV_PICTURE_TYPE_B ||
130  (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
131  break;
132  skip_bits(&s->gb, 8 + s->pict_type);
133  bits_count += 8 + s->pict_type;
134  v = show_bits(&s->gb, 16);
135  }
136 
137  if (bits_count + 8 >= s->gb.size_in_bits) {
138  v >>= 8;
139  v |= 0x7F >> (7 - (bits_count & 7));
140 
141  if (v == 0x7F)
142  return s->mb_num;
143  } else {
144  if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
145  int len, mb_num;
146  int mb_num_bits = av_log2(s->mb_num - 1) + 1;
147  GetBitContext gb = s->gb;
148 
149  skip_bits(&s->gb, 1);
150  align_get_bits(&s->gb);
151 
152  for (len = 0; len < 32; len++)
153  if (get_bits1(&s->gb))
154  break;
155 
156  mb_num = get_bits(&s->gb, mb_num_bits);
157  if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
158  mb_num= -1;
159 
160  s->gb = gb;
161 
163  return mb_num;
164  }
165  }
166  return 0;
167 }
168 
170 {
171  MpegEncContext *s = &ctx->m;
172  int a = 2 << s->sprite_warping_accuracy;
173  int rho = 3 - s->sprite_warping_accuracy;
174  int r = 16 / a;
175  int alpha = 1;
176  int beta = 0;
177  int w = s->width;
178  int h = s->height;
179  int min_ab, i, w2, h2, w3, h3;
180  int sprite_ref[4][2];
181  int virtual_ref[2][2];
182  int64_t sprite_offset[2][2];
183  int64_t sprite_delta[2][2];
184 
185  // only true for rectangle shapes
186  const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
187  { 0, s->height }, { s->width, s->height } };
188  int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
189 
190  if (w <= 0 || h <= 0)
191  return AVERROR_INVALIDDATA;
192 
193  /* the decoder was not properly initialized and we cannot continue */
194  if (sprite_trajectory.table == NULL)
195  return AVERROR_INVALIDDATA;
196 
197  for (i = 0; i < ctx->num_sprite_warping_points; i++) {
198  int length;
199  int x = 0, y = 0;
200 
201  length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
202  if (length > 0)
203  x = get_xbits(gb, length);
204 
205  if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
206  check_marker(s->avctx, gb, "before sprite_trajectory");
207 
208  length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
209  if (length > 0)
210  y = get_xbits(gb, length);
211 
212  check_marker(s->avctx, gb, "after sprite_trajectory");
213  ctx->sprite_traj[i][0] = d[i][0] = x;
214  ctx->sprite_traj[i][1] = d[i][1] = y;
215  }
216  for (; i < 4; i++)
217  ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
218 
219  while ((1 << alpha) < w)
220  alpha++;
221  while ((1 << beta) < h)
222  beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
223  w2 = 1 << alpha;
224  h2 = 1 << beta;
225 
226  // Note, the 4th point isn't used for GMC
227  if (ctx->divx_version == 500 && ctx->divx_build == 413) {
228  sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
229  sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
230  sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
231  sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
232  sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
233  sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
234  } else {
235  sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
236  sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
237  sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
238  sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
239  sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
240  sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
241  }
242  /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
243  * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
244 
245  /* This is mostly identical to the MPEG-4 std (and is totally unreadable
246  * because of that...). Perhaps it should be reordered to be more readable.
247  * The idea behind this virtual_ref mess is to be able to use shifts later
248  * per pixel instead of divides so the distance between points is converted
249  * from w&h based to w2&h2 based which are of the 2^x form. */
250  virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
251  ROUNDED_DIV(((w - w2) *
252  (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
253  w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
254  virtual_ref[0][1] = 16 * vop_ref[0][1] +
255  ROUNDED_DIV(((w - w2) *
256  (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
257  w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
258  virtual_ref[1][0] = 16 * vop_ref[0][0] +
259  ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
260  h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
261  virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
262  ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
263  h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
264 
265  switch (ctx->num_sprite_warping_points) {
266  case 0:
267  sprite_offset[0][0] =
268  sprite_offset[0][1] =
269  sprite_offset[1][0] =
270  sprite_offset[1][1] = 0;
271  sprite_delta[0][0] = a;
272  sprite_delta[0][1] =
273  sprite_delta[1][0] = 0;
274  sprite_delta[1][1] = a;
275  ctx->sprite_shift[0] =
276  ctx->sprite_shift[1] = 0;
277  break;
278  case 1: // GMC only
279  sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
280  sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
281  sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
282  a * (vop_ref[0][0] / 2);
283  sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
284  a * (vop_ref[0][1] / 2);
285  sprite_delta[0][0] = a;
286  sprite_delta[0][1] =
287  sprite_delta[1][0] = 0;
288  sprite_delta[1][1] = a;
289  ctx->sprite_shift[0] =
290  ctx->sprite_shift[1] = 0;
291  break;
292  case 2:
293  sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
294  ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
295  ((int64_t) -vop_ref[0][0]) +
296  ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
297  ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
298  sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
299  ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
300  ((int64_t) -vop_ref[0][0]) +
301  ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
302  ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
303  sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
304  ((int64_t)-2 * vop_ref[0][0] + 1) +
305  ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
306  ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
307  (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
308  sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
309  ((int64_t)-2 * vop_ref[0][0] + 1) +
310  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
311  ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
312  (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
313  sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
314  sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
315  sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
316  sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
317 
318  ctx->sprite_shift[0] = alpha + rho;
319  ctx->sprite_shift[1] = alpha + rho + 2;
320  break;
321  case 3:
322  min_ab = FFMIN(alpha, beta);
323  w3 = w2 >> min_ab;
324  h3 = h2 >> min_ab;
325  sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
326  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
327  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
328  ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
329  sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
330  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
331  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
332  ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
333  sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
334  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
335  (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
336  ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
337  sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
338  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
339  (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
340  ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
341  sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
342  sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
343  sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
344  sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
345 
346  ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
347  ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
348  break;
349  }
350  /* try to simplify the situation */
351  if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
352  sprite_delta[0][1] == 0 &&
353  sprite_delta[1][0] == 0 &&
354  sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
355  sprite_offset[0][0] >>= ctx->sprite_shift[0];
356  sprite_offset[0][1] >>= ctx->sprite_shift[0];
357  sprite_offset[1][0] >>= ctx->sprite_shift[1];
358  sprite_offset[1][1] >>= ctx->sprite_shift[1];
359  sprite_delta[0][0] = a;
360  sprite_delta[0][1] = 0;
361  sprite_delta[1][0] = 0;
362  sprite_delta[1][1] = a;
363  ctx->sprite_shift[0] = 0;
364  ctx->sprite_shift[1] = 0;
366  } else {
367  int shift_y = 16 - ctx->sprite_shift[0];
368  int shift_c = 16 - ctx->sprite_shift[1];
369 
370  for (i = 0; i < 2; i++) {
371  if (shift_c < 0 || shift_y < 0 ||
372  FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
373  FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
374  FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
375  FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
376  ) {
377  avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
378  goto overflow;
379  }
380  }
381 
382  for (i = 0; i < 2; i++) {
383  sprite_offset[0][i] *= 1 << shift_y;
384  sprite_offset[1][i] *= 1 << shift_c;
385  sprite_delta[0][i] *= 1 << shift_y;
386  sprite_delta[1][i] *= 1 << shift_y;
387  ctx->sprite_shift[i] = 16;
388 
389  }
390  for (i = 0; i < 2; i++) {
391  int64_t sd[2] = {
392  sprite_delta[i][0] - a * (1LL<<16),
393  sprite_delta[i][1] - a * (1LL<<16)
394  };
395 
396  if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
397  llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
398  llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
399  llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
400  llabs(sprite_delta[i][1] * (w+16LL)) >= INT_MAX ||
401  llabs(sd[0]) >= INT_MAX ||
402  llabs(sd[1]) >= INT_MAX ||
403  llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
404  llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
405  llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
406  ) {
407  avpriv_request_sample(s->avctx, "Overflow on sprite points");
408  goto overflow;
409  }
410  }
412  }
413 
414  for (i = 0; i < 4; i++) {
415  s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
416  s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
417  }
418 
419  return 0;
420 overflow:
421  memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
422  memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
423  return AVERROR_PATCHWELCOME;
424 }
425 
427  MpegEncContext *s = &ctx->m;
428  int len = FFMIN(ctx->time_increment_bits + 3, 15);
429 
430  get_bits(gb, len);
431  if (get_bits1(gb))
432  get_bits(gb, len);
433  check_marker(s->avctx, gb, "after new_pred");
434 
435  return 0;
436 }
437 
438 /**
439  * Decode the next video packet.
440  * @return <0 if something went wrong
441  */
443 {
444  MpegEncContext *s = &ctx->m;
445 
446  int mb_num_bits = av_log2(s->mb_num - 1) + 1;
447  int header_extension = 0, mb_num, len;
448 
449  /* is there enough space left for a video packet + header */
450  if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
451  return -1;
452 
453  for (len = 0; len < 32; len++)
454  if (get_bits1(&s->gb))
455  break;
456 
458  av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
459  return -1;
460  }
461 
462  if (ctx->shape != RECT_SHAPE) {
463  header_extension = get_bits1(&s->gb);
464  // FIXME more stuff here
465  }
466 
467  mb_num = get_bits(&s->gb, mb_num_bits);
468  if (mb_num >= s->mb_num) {
470  "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
471  return -1;
472  }
473 
474  s->mb_x = mb_num % s->mb_width;
475  s->mb_y = mb_num / s->mb_width;
476 
477  if (ctx->shape != BIN_ONLY_SHAPE) {
478  int qscale = get_bits(&s->gb, s->quant_precision);
479  if (qscale)
480  s->chroma_qscale = s->qscale = qscale;
481  }
482 
483  if (ctx->shape == RECT_SHAPE)
484  header_extension = get_bits1(&s->gb);
485 
486  if (header_extension) {
487  int time_incr = 0;
488 
489  while (get_bits1(&s->gb) != 0)
490  time_incr++;
491 
492  check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
493  skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
494  check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
495 
496  skip_bits(&s->gb, 2); /* vop coding type */
497  // FIXME not rect stuff here
498 
499  if (ctx->shape != BIN_ONLY_SHAPE) {
500  skip_bits(&s->gb, 3); /* intra dc vlc threshold */
501  // FIXME don't just ignore everything
502  if (s->pict_type == AV_PICTURE_TYPE_S &&
503  ctx->vol_sprite_usage == GMC_SPRITE) {
504  if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
505  return AVERROR_INVALIDDATA;
506  av_log(s->avctx, AV_LOG_ERROR, "untested\n");
507  }
508 
509  // FIXME reduced res stuff here
510 
511  if (s->pict_type != AV_PICTURE_TYPE_I) {
512  int f_code = get_bits(&s->gb, 3); /* fcode_for */
513  if (f_code == 0)
515  "Error, video packet header damaged (f_code=0)\n");
516  }
517  if (s->pict_type == AV_PICTURE_TYPE_B) {
518  int b_code = get_bits(&s->gb, 3);
519  if (b_code == 0)
521  "Error, video packet header damaged (b_code=0)\n");
522  }
523  }
524  }
525  if (ctx->new_pred)
526  decode_new_pred(ctx, &s->gb);
527 
528  return 0;
529 }
530 
531 /**
532  * Get the average motion vector for a GMC MB.
533  * @param n either 0 for the x component or 1 for y
534  * @return the average MV for a GMC MB
535  */
536 static inline int get_amv(Mpeg4DecContext *ctx, int n)
537 {
538  MpegEncContext *s = &ctx->m;
539  int x, y, mb_v, sum, dx, dy, shift;
540  int len = 1 << (s->f_code + 4);
541  const int a = s->sprite_warping_accuracy;
542 
543  if (s->workaround_bugs & FF_BUG_AMV)
544  len >>= s->quarter_sample;
545 
546  if (s->real_sprite_warping_points == 1) {
547  if (ctx->divx_version == 500 && ctx->divx_build == 413)
548  sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
549  else
550  sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
551  } else {
552  dx = s->sprite_delta[n][0];
553  dy = s->sprite_delta[n][1];
554  shift = ctx->sprite_shift[0];
555  if (n)
556  dy -= 1 << (shift + a + 1);
557  else
558  dx -= 1 << (shift + a + 1);
559  mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
560 
561  sum = 0;
562  for (y = 0; y < 16; y++) {
563  int v;
564 
565  v = mb_v + dy * y;
566  // FIXME optimize
567  for (x = 0; x < 16; x++) {
568  sum += v >> shift;
569  v += dx;
570  }
571  }
572  sum = RSHIFT(sum, a + 8 - s->quarter_sample);
573  }
574 
575  if (sum < -len)
576  sum = -len;
577  else if (sum >= len)
578  sum = len - 1;
579 
580  return sum;
581 }
582 
583 /**
584  * Decode the dc value.
585  * @param n block index (0-3 are luma, 4-5 are chroma)
586  * @param dir_ptr the prediction direction will be stored here
587  * @return the quantized dc
588  */
589 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
590 {
591  int level, code;
592 
593  if (n < 4)
594  code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
595  else
596  code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
597 
598  if (code < 0 || code > 9 /* && s->nbit < 9 */) {
599  av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
600  return -1;
601  }
602 
603  if (code == 0) {
604  level = 0;
605  } else {
606  if (IS_3IV1) {
607  if (code == 1)
608  level = 2 * get_bits1(&s->gb) - 1;
609  else {
610  if (get_bits1(&s->gb))
611  level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
612  else
613  level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
614  }
615  } else {
616  level = get_xbits(&s->gb, code);
617  }
618 
619  if (code > 8) {
620  if (get_bits1(&s->gb) == 0) { /* marker */
622  av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
623  return -1;
624  }
625  }
626  }
627  }
628 
629  return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
630 }
631 
632 /**
633  * Decode first partition.
634  * @return number of MBs decoded or <0 if an error occurred
635  */
637 {
638  MpegEncContext *s = &ctx->m;
639  int mb_num = 0;
640  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
641 
642  /* decode first partition */
643  s->first_slice_line = 1;
644  for (; s->mb_y < s->mb_height; s->mb_y++) {
646  for (; s->mb_x < s->mb_width; s->mb_x++) {
647  const int xy = s->mb_x + s->mb_y * s->mb_stride;
648  int cbpc;
649  int dir = 0;
650 
651  mb_num++;
653  if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
654  s->first_slice_line = 0;
655 
656  if (s->pict_type == AV_PICTURE_TYPE_I) {
657  int i;
658 
659  do {
660  if (show_bits_long(&s->gb, 19) == DC_MARKER)
661  return mb_num - 1;
662 
664  if (cbpc < 0) {
666  "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
667  return -1;
668  }
669  } while (cbpc == 8);
670 
671  s->cbp_table[xy] = cbpc & 3;
673  s->mb_intra = 1;
674 
675  if (cbpc & 4)
676  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
677 
678  s->current_picture.qscale_table[xy] = s->qscale;
679 
680  s->mbintra_table[xy] = 1;
681  for (i = 0; i < 6; i++) {
682  int dc_pred_dir;
683  int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
684  if (dc < 0) {
686  "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
687  return -1;
688  }
689  dir <<= 1;
690  if (dc_pred_dir)
691  dir |= 1;
692  }
693  s->pred_dir_table[xy] = dir;
694  } else { /* P/S_TYPE */
695  int mx, my, pred_x, pred_y, bits;
696  int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
697  const int stride = s->b8_stride * 2;
698 
699 try_again:
700  bits = show_bits(&s->gb, 17);
701  if (bits == MOTION_MARKER)
702  return mb_num - 1;
703 
704  skip_bits1(&s->gb);
705  if (bits & 0x10000) {
706  /* skip mb */
707  if (s->pict_type == AV_PICTURE_TYPE_S &&
708  ctx->vol_sprite_usage == GMC_SPRITE) {
710  MB_TYPE_16x16 |
711  MB_TYPE_GMC |
712  MB_TYPE_L0;
713  mx = get_amv(ctx, 0);
714  my = get_amv(ctx, 1);
715  } else {
717  MB_TYPE_16x16 |
718  MB_TYPE_L0;
719  mx = my = 0;
720  }
721  mot_val[0] =
722  mot_val[2] =
723  mot_val[0 + stride] =
724  mot_val[2 + stride] = mx;
725  mot_val[1] =
726  mot_val[3] =
727  mot_val[1 + stride] =
728  mot_val[3 + stride] = my;
729 
730  if (s->mbintra_table[xy])
732  continue;
733  }
734 
736  if (cbpc < 0) {
738  "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
739  return -1;
740  }
741  if (cbpc == 20)
742  goto try_again;
743 
744  s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
745 
746  s->mb_intra = ((cbpc & 4) != 0);
747 
748  if (s->mb_intra) {
750  s->mbintra_table[xy] = 1;
751  mot_val[0] =
752  mot_val[2] =
753  mot_val[0 + stride] =
754  mot_val[2 + stride] = 0;
755  mot_val[1] =
756  mot_val[3] =
757  mot_val[1 + stride] =
758  mot_val[3 + stride] = 0;
759  } else {
760  if (s->mbintra_table[xy])
762 
763  if (s->pict_type == AV_PICTURE_TYPE_S &&
764  ctx->vol_sprite_usage == GMC_SPRITE &&
765  (cbpc & 16) == 0)
766  s->mcsel = get_bits1(&s->gb);
767  else
768  s->mcsel = 0;
769 
770  if ((cbpc & 16) == 0) {
771  /* 16x16 motion prediction */
772 
773  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
774  if (!s->mcsel) {
775  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
776  if (mx >= 0xffff)
777  return -1;
778 
779  my = ff_h263_decode_motion(s, pred_y, s->f_code);
780  if (my >= 0xffff)
781  return -1;
783  MB_TYPE_L0;
784  } else {
785  mx = get_amv(ctx, 0);
786  my = get_amv(ctx, 1);
788  MB_TYPE_GMC |
789  MB_TYPE_L0;
790  }
791 
792  mot_val[0] =
793  mot_val[2] =
794  mot_val[0 + stride] =
795  mot_val[2 + stride] = mx;
796  mot_val[1] =
797  mot_val[3] =
798  mot_val[1 + stride] =
799  mot_val[3 + stride] = my;
800  } else {
801  int i;
803  MB_TYPE_L0;
804  for (i = 0; i < 4; i++) {
805  int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
806  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
807  if (mx >= 0xffff)
808  return -1;
809 
810  my = ff_h263_decode_motion(s, pred_y, s->f_code);
811  if (my >= 0xffff)
812  return -1;
813  mot_val[0] = mx;
814  mot_val[1] = my;
815  }
816  }
817  }
818  }
819  }
820  s->mb_x = 0;
821  }
822 
823  return mb_num;
824 }
825 
826 /**
827  * decode second partition.
828  * @return <0 if an error occurred
829  */
830 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
831 {
832  int mb_num = 0;
833  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
834 
835  s->mb_x = s->resync_mb_x;
836  s->first_slice_line = 1;
837  for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
839  for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
840  const int xy = s->mb_x + s->mb_y * s->mb_stride;
841 
842  mb_num++;
844  if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
845  s->first_slice_line = 0;
846 
847  if (s->pict_type == AV_PICTURE_TYPE_I) {
848  int ac_pred = get_bits1(&s->gb);
849  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
850  if (cbpy < 0) {
852  "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
853  return -1;
854  }
855 
856  s->cbp_table[xy] |= cbpy << 2;
857  s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
858  } else { /* P || S_TYPE */
859  if (IS_INTRA(s->current_picture.mb_type[xy])) {
860  int i;
861  int dir = 0;
862  int ac_pred = get_bits1(&s->gb);
863  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
864 
865  if (cbpy < 0) {
867  "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
868  return -1;
869  }
870 
871  if (s->cbp_table[xy] & 8)
872  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
873  s->current_picture.qscale_table[xy] = s->qscale;
874 
875  for (i = 0; i < 6; i++) {
876  int dc_pred_dir;
877  int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
878  if (dc < 0) {
880  "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
881  return -1;
882  }
883  dir <<= 1;
884  if (dc_pred_dir)
885  dir |= 1;
886  }
887  s->cbp_table[xy] &= 3; // remove dquant
888  s->cbp_table[xy] |= cbpy << 2;
889  s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
890  s->pred_dir_table[xy] = dir;
891  } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
892  s->current_picture.qscale_table[xy] = s->qscale;
893  s->cbp_table[xy] = 0;
894  } else {
895  int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
896 
897  if (cbpy < 0) {
899  "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
900  return -1;
901  }
902 
903  if (s->cbp_table[xy] & 8)
904  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
905  s->current_picture.qscale_table[xy] = s->qscale;
906 
907  s->cbp_table[xy] &= 3; // remove dquant
908  s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
909  }
910  }
911  }
912  if (mb_num >= mb_count)
913  return 0;
914  s->mb_x = 0;
915  }
916  return 0;
917 }
918 
919 /**
920  * Decode the first and second partition.
921  * @return <0 if error (and sets error type in the error_status_table)
922  */
924 {
925  MpegEncContext *s = &ctx->m;
926  int mb_num;
927  const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
928  const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
929 
930  mb_num = mpeg4_decode_partition_a(ctx);
931  if (mb_num <= 0) {
933  s->mb_x, s->mb_y, part_a_error);
934  return -1;
935  }
936 
937  if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
938  av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
940  s->mb_x, s->mb_y, part_a_error);
941  return -1;
942  }
943 
944  s->mb_num_left = mb_num;
945 
946  if (s->pict_type == AV_PICTURE_TYPE_I) {
947  while (show_bits(&s->gb, 9) == 1)
948  skip_bits(&s->gb, 9);
949  if (get_bits_long(&s->gb, 19) != DC_MARKER) {
951  "marker missing after first I partition at %d %d\n",
952  s->mb_x, s->mb_y);
953  return -1;
954  }
955  } else {
956  while (show_bits(&s->gb, 10) == 1)
957  skip_bits(&s->gb, 10);
958  if (get_bits(&s->gb, 17) != MOTION_MARKER) {
960  "marker missing after first P partition at %d %d\n",
961  s->mb_x, s->mb_y);
962  return -1;
963  }
964  }
966  s->mb_x - 1, s->mb_y, part_a_end);
967 
968  if (mpeg4_decode_partition_b(s, mb_num) < 0) {
969  if (s->pict_type == AV_PICTURE_TYPE_P)
971  s->mb_x, s->mb_y, ER_DC_ERROR);
972  return -1;
973  } else {
974  if (s->pict_type == AV_PICTURE_TYPE_P)
976  s->mb_x - 1, s->mb_y, ER_DC_END);
977  }
978 
979  return 0;
980 }
981 
982 /**
983  * Decode a block.
984  * @return <0 if an error occurred
985  */
986 static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
987  int n, int coded, int intra, int rvlc)
988 {
989  MpegEncContext *s = &ctx->m;
990  int level, i, last, run, qmul, qadd;
991  int av_uninit(dc_pred_dir);
992  RLTable *rl;
993  RL_VLC_ELEM *rl_vlc;
994  const uint8_t *scan_table;
995 
996  // Note intra & rvlc should be optimized away if this is inlined
997 
998  if (intra) {
999  if (ctx->use_intra_dc_vlc) {
1000  /* DC coef */
1001  if (s->partitioned_frame) {
1002  level = s->dc_val[0][s->block_index[n]];
1003  if (n < 4)
1004  level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
1005  else
1006  level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
1007  dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
1008  } else {
1009  level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1010  if (level < 0)
1011  return -1;
1012  }
1013  block[0] = level;
1014  i = 0;
1015  } else {
1016  i = -1;
1017  ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
1018  }
1019  if (!coded)
1020  goto not_coded;
1021 
1022  if (rvlc) {
1023  rl = &ff_rvlc_rl_intra;
1024  rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1025  } else {
1026  rl = &ff_mpeg4_rl_intra;
1027  rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1028  }
1029  if (s->ac_pred) {
1030  if (dc_pred_dir == 0)
1031  scan_table = s->intra_v_scantable.permutated; /* left */
1032  else
1033  scan_table = s->intra_h_scantable.permutated; /* top */
1034  } else {
1035  scan_table = s->intra_scantable.permutated;
1036  }
1037  qmul = 1;
1038  qadd = 0;
1039  } else {
1040  i = -1;
1041  if (!coded) {
1042  s->block_last_index[n] = i;
1043  return 0;
1044  }
1045  if (rvlc)
1046  rl = &ff_rvlc_rl_inter;
1047  else
1048  rl = &ff_h263_rl_inter;
1049 
1050  scan_table = s->intra_scantable.permutated;
1051 
1052  if (s->mpeg_quant) {
1053  qmul = 1;
1054  qadd = 0;
1055  if (rvlc)
1056  rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1057  else
1058  rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1059  } else {
1060  qmul = s->qscale << 1;
1061  qadd = (s->qscale - 1) | 1;
1062  if (rvlc)
1063  rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1064  else
1065  rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1066  }
1067  }
1068  {
1069  OPEN_READER(re, &s->gb);
1070  for (;;) {
1071  UPDATE_CACHE(re, &s->gb);
1072  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1073  if (level == 0) {
1074  /* escape */
1075  if (rvlc) {
1076  if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1078  "1. marker bit missing in rvlc esc\n");
1079  return -1;
1080  }
1081  SKIP_CACHE(re, &s->gb, 1);
1082 
1083  last = SHOW_UBITS(re, &s->gb, 1);
1084  SKIP_CACHE(re, &s->gb, 1);
1085  run = SHOW_UBITS(re, &s->gb, 6);
1086  SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1087  UPDATE_CACHE(re, &s->gb);
1088 
1089  if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1091  "2. marker bit missing in rvlc esc\n");
1092  return -1;
1093  }
1094  SKIP_CACHE(re, &s->gb, 1);
1095 
1096  level = SHOW_UBITS(re, &s->gb, 11);
1097  SKIP_CACHE(re, &s->gb, 11);
1098 
1099  if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1100  av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1101  return -1;
1102  }
1103  SKIP_CACHE(re, &s->gb, 5);
1104 
1105  level = level * qmul + qadd;
1106  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1107  SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1108 
1109  i += run + 1;
1110  if (last)
1111  i += 192;
1112  } else {
1113  int cache;
1114  cache = GET_CACHE(re, &s->gb);
1115 
1116  if (IS_3IV1)
1117  cache ^= 0xC0000000;
1118 
1119  if (cache & 0x80000000) {
1120  if (cache & 0x40000000) {
1121  /* third escape */
1122  SKIP_CACHE(re, &s->gb, 2);
1123  last = SHOW_UBITS(re, &s->gb, 1);
1124  SKIP_CACHE(re, &s->gb, 1);
1125  run = SHOW_UBITS(re, &s->gb, 6);
1126  SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1127  UPDATE_CACHE(re, &s->gb);
1128 
1129  if (IS_3IV1) {
1130  level = SHOW_SBITS(re, &s->gb, 12);
1131  LAST_SKIP_BITS(re, &s->gb, 12);
1132  } else {
1133  if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1135  "1. marker bit missing in 3. esc\n");
1136  if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1137  return -1;
1138  }
1139  SKIP_CACHE(re, &s->gb, 1);
1140 
1141  level = SHOW_SBITS(re, &s->gb, 12);
1142  SKIP_CACHE(re, &s->gb, 12);
1143 
1144  if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1146  "2. marker bit missing in 3. esc\n");
1147  if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1148  return -1;
1149  }
1150 
1151  SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1152  }
1153 
1154 #if 0
1155  if (s->error_recognition >= FF_ER_COMPLIANT) {
1156  const int abs_level= FFABS(level);
1157  if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1158  const int run1= run - rl->max_run[last][abs_level] - 1;
1159  if (abs_level <= rl->max_level[last][run]) {
1160  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1161  return -1;
1162  }
1163  if (s->error_recognition > FF_ER_COMPLIANT) {
1164  if (abs_level <= rl->max_level[last][run]*2) {
1165  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1166  return -1;
1167  }
1168  if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1169  av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1170  return -1;
1171  }
1172  }
1173  }
1174  }
1175 #endif
1176  if (level > 0)
1177  level = level * qmul + qadd;
1178  else
1179  level = level * qmul - qadd;
1180 
1181  if ((unsigned)(level + 2048) > 4095) {
1183  if (level > 2560 || level < -2560) {
1185  "|level| overflow in 3. esc, qp=%d\n",
1186  s->qscale);
1187  return -1;
1188  }
1189  }
1190  level = level < 0 ? -2048 : 2047;
1191  }
1192 
1193  i += run + 1;
1194  if (last)
1195  i += 192;
1196  } else {
1197  /* second escape */
1198  SKIP_BITS(re, &s->gb, 2);
1199  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1200  i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1201  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1202  LAST_SKIP_BITS(re, &s->gb, 1);
1203  }
1204  } else {
1205  /* first escape */
1206  SKIP_BITS(re, &s->gb, 1);
1207  GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1208  i += run;
1209  level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1210  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1211  LAST_SKIP_BITS(re, &s->gb, 1);
1212  }
1213  }
1214  } else {
1215  i += run;
1216  level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1217  LAST_SKIP_BITS(re, &s->gb, 1);
1218  }
1219  ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1220  if (i > 62) {
1221  i -= 192;
1222  if (i & (~63)) {
1224  "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1225  return -1;
1226  }
1227 
1228  block[scan_table[i]] = level;
1229  break;
1230  }
1231 
1232  block[scan_table[i]] = level;
1233  }
1234  CLOSE_READER(re, &s->gb);
1235  }
1236 
1237 not_coded:
1238  if (intra) {
1239  if (!ctx->use_intra_dc_vlc) {
1240  block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1241 
1242  i -= i >> 31; // if (i == -1) i = 0;
1243  }
1244 
1245  ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1246  if (s->ac_pred)
1247  i = 63; // FIXME not optimal
1248  }
1249  s->block_last_index[n] = i;
1250  return 0;
1251 }
1252 
1253 /**
1254  * decode partition C of one MB.
1255  * @return <0 if an error occurred
1256  */
1258 {
1260  int cbp, mb_type;
1261  const int xy = s->mb_x + s->mb_y * s->mb_stride;
1262 
1263  mb_type = s->current_picture.mb_type[xy];
1264  cbp = s->cbp_table[xy];
1265 
1266  ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1267 
1268  if (s->current_picture.qscale_table[xy] != s->qscale)
1270 
1271  if (s->pict_type == AV_PICTURE_TYPE_P ||
1272  s->pict_type == AV_PICTURE_TYPE_S) {
1273  int i;
1274  for (i = 0; i < 4; i++) {
1275  s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1276  s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1277  }
1278  s->mb_intra = IS_INTRA(mb_type);
1279 
1280  if (IS_SKIP(mb_type)) {
1281  /* skip mb */
1282  for (i = 0; i < 6; i++)
1283  s->block_last_index[i] = -1;
1284  s->mv_dir = MV_DIR_FORWARD;
1285  s->mv_type = MV_TYPE_16X16;
1287  && ctx->vol_sprite_usage == GMC_SPRITE) {
1288  s->mcsel = 1;
1289  s->mb_skipped = 0;
1290  } else {
1291  s->mcsel = 0;
1292  s->mb_skipped = 1;
1293  }
1294  } else if (s->mb_intra) {
1296  } else if (!s->mb_intra) {
1297  // s->mcsel = 0; // FIXME do we need to init that?
1298 
1299  s->mv_dir = MV_DIR_FORWARD;
1300  if (IS_8X8(mb_type)) {
1301  s->mv_type = MV_TYPE_8X8;
1302  } else {
1303  s->mv_type = MV_TYPE_16X16;
1304  }
1305  }
1306  } else { /* I-Frame */
1307  s->mb_intra = 1;
1309  }
1310 
1311  if (!IS_SKIP(mb_type)) {
1312  int i;
1313  s->bdsp.clear_blocks(s->block[0]);
1314  /* decode each block */
1315  for (i = 0; i < 6; i++) {
1316  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1318  "texture corrupted at %d %d %d\n",
1319  s->mb_x, s->mb_y, s->mb_intra);
1320  return -1;
1321  }
1322  cbp += cbp;
1323  }
1324  }
1325 
1326  /* per-MB end of slice check */
1327  if (--s->mb_num_left <= 0) {
1328  if (mpeg4_is_resync(ctx))
1329  return SLICE_END;
1330  else
1331  return SLICE_NOEND;
1332  } else {
1333  if (mpeg4_is_resync(ctx)) {
1334  const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1335  if (s->cbp_table[xy + delta])
1336  return SLICE_END;
1337  }
1338  return SLICE_OK;
1339  }
1340 }
1341 
1342 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1343 {
1345  int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1346  int16_t *mot_val;
1347  static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1348  const int xy = s->mb_x + s->mb_y * s->mb_stride;
1349 
1350  av_assert2(s->h263_pred);
1351 
1352  if (s->pict_type == AV_PICTURE_TYPE_P ||
1353  s->pict_type == AV_PICTURE_TYPE_S) {
1354  do {
1355  if (get_bits1(&s->gb)) {
1356  /* skip mb */
1357  s->mb_intra = 0;
1358  for (i = 0; i < 6; i++)
1359  s->block_last_index[i] = -1;
1360  s->mv_dir = MV_DIR_FORWARD;
1361  s->mv_type = MV_TYPE_16X16;
1362  if (s->pict_type == AV_PICTURE_TYPE_S &&
1363  ctx->vol_sprite_usage == GMC_SPRITE) {
1365  MB_TYPE_GMC |
1366  MB_TYPE_16x16 |
1367  MB_TYPE_L0;
1368  s->mcsel = 1;
1369  s->mv[0][0][0] = get_amv(ctx, 0);
1370  s->mv[0][0][1] = get_amv(ctx, 1);
1371  s->mb_skipped = 0;
1372  } else {
1374  MB_TYPE_16x16 |
1375  MB_TYPE_L0;
1376  s->mcsel = 0;
1377  s->mv[0][0][0] = 0;
1378  s->mv[0][0][1] = 0;
1379  s->mb_skipped = 1;
1380  }
1381  goto end;
1382  }
1384  if (cbpc < 0) {
1386  "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1387  return -1;
1388  }
1389  } while (cbpc == 20);
1390 
1391  s->bdsp.clear_blocks(s->block[0]);
1392  dquant = cbpc & 8;
1393  s->mb_intra = ((cbpc & 4) != 0);
1394  if (s->mb_intra)
1395  goto intra;
1396 
1397  if (s->pict_type == AV_PICTURE_TYPE_S &&
1398  ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1399  s->mcsel = get_bits1(&s->gb);
1400  else
1401  s->mcsel = 0;
1402  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1403  if (cbpy < 0) {
1405  "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1406  return AVERROR_INVALIDDATA;
1407  }
1408 
1409  cbp = (cbpc & 3) | (cbpy << 2);
1410  if (dquant)
1411  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1412  if ((!s->progressive_sequence) &&
1413  (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1414  s->interlaced_dct = get_bits1(&s->gb);
1415 
1416  s->mv_dir = MV_DIR_FORWARD;
1417  if ((cbpc & 16) == 0) {
1418  if (s->mcsel) {
1420  MB_TYPE_16x16 |
1421  MB_TYPE_L0;
1422  /* 16x16 global motion prediction */
1423  s->mv_type = MV_TYPE_16X16;
1424  mx = get_amv(ctx, 0);
1425  my = get_amv(ctx, 1);
1426  s->mv[0][0][0] = mx;
1427  s->mv[0][0][1] = my;
1428  } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1430  MB_TYPE_L0 |
1432  /* 16x8 field motion prediction */
1433  s->mv_type = MV_TYPE_FIELD;
1434 
1435  s->field_select[0][0] = get_bits1(&s->gb);
1436  s->field_select[0][1] = get_bits1(&s->gb);
1437 
1438  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1439 
1440  for (i = 0; i < 2; i++) {
1441  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1442  if (mx >= 0xffff)
1443  return -1;
1444 
1445  my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1446  if (my >= 0xffff)
1447  return -1;
1448 
1449  s->mv[0][i][0] = mx;
1450  s->mv[0][i][1] = my;
1451  }
1452  } else {
1454  /* 16x16 motion prediction */
1455  s->mv_type = MV_TYPE_16X16;
1456  ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1457  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1458 
1459  if (mx >= 0xffff)
1460  return -1;
1461 
1462  my = ff_h263_decode_motion(s, pred_y, s->f_code);
1463 
1464  if (my >= 0xffff)
1465  return -1;
1466  s->mv[0][0][0] = mx;
1467  s->mv[0][0][1] = my;
1468  }
1469  } else {
1471  s->mv_type = MV_TYPE_8X8;
1472  for (i = 0; i < 4; i++) {
1473  mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1474  mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1475  if (mx >= 0xffff)
1476  return -1;
1477 
1478  my = ff_h263_decode_motion(s, pred_y, s->f_code);
1479  if (my >= 0xffff)
1480  return -1;
1481  s->mv[0][i][0] = mx;
1482  s->mv[0][i][1] = my;
1483  mot_val[0] = mx;
1484  mot_val[1] = my;
1485  }
1486  }
1487  } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1488  int modb1; // first bit of modb
1489  int modb2; // second bit of modb
1490  int mb_type;
1491 
1492  s->mb_intra = 0; // B-frames never contain intra blocks
1493  s->mcsel = 0; // ... true gmc blocks
1494 
1495  if (s->mb_x == 0) {
1496  for (i = 0; i < 2; i++) {
1497  s->last_mv[i][0][0] =
1498  s->last_mv[i][0][1] =
1499  s->last_mv[i][1][0] =
1500  s->last_mv[i][1][1] = 0;
1501  }
1502 
1504  }
1505 
1506  /* if we skipped it in the future P-frame than skip it now too */
1507  s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1508 
1509  if (s->mb_skipped) {
1510  /* skip mb */
1511  for (i = 0; i < 6; i++)
1512  s->block_last_index[i] = -1;
1513 
1514  s->mv_dir = MV_DIR_FORWARD;
1515  s->mv_type = MV_TYPE_16X16;
1516  s->mv[0][0][0] =
1517  s->mv[0][0][1] =
1518  s->mv[1][0][0] =
1519  s->mv[1][0][1] = 0;
1521  MB_TYPE_16x16 |
1522  MB_TYPE_L0;
1523  goto end;
1524  }
1525 
1526  modb1 = get_bits1(&s->gb);
1527  if (modb1) {
1528  // like MB_TYPE_B_DIRECT but no vectors coded
1530  cbp = 0;
1531  } else {
1532  modb2 = get_bits1(&s->gb);
1533  mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1534  if (mb_type < 0) {
1535  av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1536  return -1;
1537  }
1538  mb_type = mb_type_b_map[mb_type];
1539  if (modb2) {
1540  cbp = 0;
1541  } else {
1542  s->bdsp.clear_blocks(s->block[0]);
1543  cbp = get_bits(&s->gb, 6);
1544  }
1545 
1546  if ((!IS_DIRECT(mb_type)) && cbp) {
1547  if (get_bits1(&s->gb))
1548  ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1549  }
1550 
1551  if (!s->progressive_sequence) {
1552  if (cbp)
1553  s->interlaced_dct = get_bits1(&s->gb);
1554 
1555  if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1556  mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1557  mb_type &= ~MB_TYPE_16x16;
1558 
1559  if (USES_LIST(mb_type, 0)) {
1560  s->field_select[0][0] = get_bits1(&s->gb);
1561  s->field_select[0][1] = get_bits1(&s->gb);
1562  }
1563  if (USES_LIST(mb_type, 1)) {
1564  s->field_select[1][0] = get_bits1(&s->gb);
1565  s->field_select[1][1] = get_bits1(&s->gb);
1566  }
1567  }
1568  }
1569 
1570  s->mv_dir = 0;
1571  if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1572  s->mv_type = MV_TYPE_16X16;
1573 
1574  if (USES_LIST(mb_type, 0)) {
1575  s->mv_dir = MV_DIR_FORWARD;
1576 
1577  mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1578  my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1579  s->last_mv[0][1][0] =
1580  s->last_mv[0][0][0] =
1581  s->mv[0][0][0] = mx;
1582  s->last_mv[0][1][1] =
1583  s->last_mv[0][0][1] =
1584  s->mv[0][0][1] = my;
1585  }
1586 
1587  if (USES_LIST(mb_type, 1)) {
1588  s->mv_dir |= MV_DIR_BACKWARD;
1589 
1590  mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1591  my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1592  s->last_mv[1][1][0] =
1593  s->last_mv[1][0][0] =
1594  s->mv[1][0][0] = mx;
1595  s->last_mv[1][1][1] =
1596  s->last_mv[1][0][1] =
1597  s->mv[1][0][1] = my;
1598  }
1599  } else if (!IS_DIRECT(mb_type)) {
1600  s->mv_type = MV_TYPE_FIELD;
1601 
1602  if (USES_LIST(mb_type, 0)) {
1603  s->mv_dir = MV_DIR_FORWARD;
1604 
1605  for (i = 0; i < 2; i++) {
1606  mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1607  my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1608  s->last_mv[0][i][0] =
1609  s->mv[0][i][0] = mx;
1610  s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1611  }
1612  }
1613 
1614  if (USES_LIST(mb_type, 1)) {
1615  s->mv_dir |= MV_DIR_BACKWARD;
1616 
1617  for (i = 0; i < 2; i++) {
1618  mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1619  my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1620  s->last_mv[1][i][0] =
1621  s->mv[1][i][0] = mx;
1622  s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1623  }
1624  }
1625  }
1626  }
1627 
1628  if (IS_DIRECT(mb_type)) {
1629  if (IS_SKIP(mb_type)) {
1630  mx =
1631  my = 0;
1632  } else {
1633  mx = ff_h263_decode_motion(s, 0, 1);
1634  my = ff_h263_decode_motion(s, 0, 1);
1635  }
1636 
1638  mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1639  }
1640  s->current_picture.mb_type[xy] = mb_type;
1641  } else { /* I-Frame */
1642  do {
1644  if (cbpc < 0) {
1646  "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1647  return -1;
1648  }
1649  } while (cbpc == 8);
1650 
1651  dquant = cbpc & 4;
1652  s->mb_intra = 1;
1653 
1654 intra:
1655  s->ac_pred = get_bits1(&s->gb);
1656  if (s->ac_pred)
1658  else
1660 
1661  cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1662  if (cbpy < 0) {
1664  "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1665  return -1;
1666  }
1667  cbp = (cbpc & 3) | (cbpy << 2);
1668 
1669  ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1670 
1671  if (dquant)
1672  ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1673 
1674  if (!s->progressive_sequence)
1675  s->interlaced_dct = get_bits1(&s->gb);
1676 
1677  s->bdsp.clear_blocks(s->block[0]);
1678  /* decode each block */
1679  for (i = 0; i < 6; i++) {
1680  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
1681  return -1;
1682  cbp += cbp;
1683  }
1684  goto end;
1685  }
1686 
1687  /* decode each block */
1688  for (i = 0; i < 6; i++) {
1689  if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
1690  return -1;
1691  cbp += cbp;
1692  }
1693 
1694 end:
1695  /* per-MB end of slice check */
1696  if (s->codec_id == AV_CODEC_ID_MPEG4) {
1697  int next = mpeg4_is_resync(ctx);
1698  if (next) {
1699  if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1700  return -1;
1701  } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1702  return SLICE_END;
1703 
1704  if (s->pict_type == AV_PICTURE_TYPE_B) {
1705  const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1707  (s->mb_x + delta >= s->mb_width)
1708  ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1709  : s->mb_y, 0);
1710  if (s->next_picture.mbskip_table[xy + delta])
1711  return SLICE_OK;
1712  }
1713 
1714  return SLICE_END;
1715  }
1716  }
1717 
1718  return SLICE_OK;
1719 }
1720 
1722 {
1723  int hours, minutes, seconds;
1724 
1725  if (!show_bits(gb, 23)) {
1726  av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1727  return -1;
1728  }
1729 
1730  hours = get_bits(gb, 5);
1731  minutes = get_bits(gb, 6);
1732  check_marker(s->avctx, gb, "in gop_header");
1733  seconds = get_bits(gb, 6);
1734 
1735  s->time_base = seconds + 60*(minutes + 60*hours);
1736 
1737  skip_bits1(gb);
1738  skip_bits1(gb);
1739 
1740  return 0;
1741 }
1742 
1744 {
1745 
1746  s->avctx->profile = get_bits(gb, 4);
1747  s->avctx->level = get_bits(gb, 4);
1748 
1749  // for Simple profile, level 0
1750  if (s->avctx->profile == 0 && s->avctx->level == 8) {
1751  s->avctx->level = 0;
1752  }
1753 
1754  return 0;
1755 }
1756 
1758 {
1759  int visual_object_type;
1760  int is_visual_object_identifier = get_bits1(gb);
1761 
1762  if (is_visual_object_identifier) {
1763  skip_bits(gb, 4+3);
1764  }
1765  visual_object_type = get_bits(gb, 4);
1766 
1767  if (visual_object_type == VOT_VIDEO_ID ||
1768  visual_object_type == VOT_STILL_TEXTURE_ID) {
1769  int video_signal_type = get_bits1(gb);
1770  if (video_signal_type) {
1771  int video_range, color_description;
1772  skip_bits(gb, 3); // video_format
1773  video_range = get_bits1(gb);
1774  color_description = get_bits1(gb);
1775 
1776  s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
1777 
1778  if (color_description) {
1779  s->avctx->color_primaries = get_bits(gb, 8);
1780  s->avctx->color_trc = get_bits(gb, 8);
1781  s->avctx->colorspace = get_bits(gb, 8);
1782  }
1783  }
1784  }
1785 
1786  return 0;
1787 }
1788 
1790 {
1791  MpegEncContext *s = &ctx->m;
1792  int width, height, vo_ver_id;
1793 
1794  /* vol header */
1795  skip_bits(gb, 1); /* random access */
1796  s->vo_type = get_bits(gb, 8);
1797  if (get_bits1(gb) != 0) { /* is_ol_id */
1798  vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1799  skip_bits(gb, 3); /* vo_priority */
1800  } else {
1801  vo_ver_id = 1;
1802  }
1803  s->aspect_ratio_info = get_bits(gb, 4);
1805  s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
1806  s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
1807  } else {
1809  }
1810 
1811  if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
1812  int chroma_format = get_bits(gb, 2);
1813  if (chroma_format != CHROMA_420)
1814  av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1815 
1816  s->low_delay = get_bits1(gb);
1817  if (get_bits1(gb)) { /* vbv parameters */
1818  get_bits(gb, 15); /* first_half_bitrate */
1819  check_marker(s->avctx, gb, "after first_half_bitrate");
1820  get_bits(gb, 15); /* latter_half_bitrate */
1821  check_marker(s->avctx, gb, "after latter_half_bitrate");
1822  get_bits(gb, 15); /* first_half_vbv_buffer_size */
1823  check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
1824  get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1825  get_bits(gb, 11); /* first_half_vbv_occupancy */
1826  check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
1827  get_bits(gb, 15); /* latter_half_vbv_occupancy */
1828  check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
1829  }
1830  } else {
1831  /* is setting low delay flag only once the smartest thing to do?
1832  * low delay detection will not be overridden. */
1833  if (s->picture_number == 0) {
1834  switch(s->vo_type) {
1835  case SIMPLE_VO_TYPE:
1836  case ADV_SIMPLE_VO_TYPE:
1837  s->low_delay = 1;
1838  break;
1839  default:
1840  s->low_delay = 0;
1841  }
1842  }
1843  }
1844 
1845  ctx->shape = get_bits(gb, 2); /* vol shape */
1846  if (ctx->shape != RECT_SHAPE)
1847  av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1848  if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
1849  av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1850  skip_bits(gb, 4); /* video_object_layer_shape_extension */
1851  }
1852 
1853  check_marker(s->avctx, gb, "before time_increment_resolution");
1854 
1855  s->avctx->framerate.num = get_bits(gb, 16);
1856  if (!s->avctx->framerate.num) {
1857  av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
1858  return AVERROR_INVALIDDATA;
1859  }
1860 
1861  ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
1862  if (ctx->time_increment_bits < 1)
1863  ctx->time_increment_bits = 1;
1864 
1865  check_marker(s->avctx, gb, "before fixed_vop_rate");
1866 
1867  if (get_bits1(gb) != 0) /* fixed_vop_rate */
1869  else
1870  s->avctx->framerate.den = 1;
1871 
1873 
1874  ctx->t_frame = 0;
1875 
1876  if (ctx->shape != BIN_ONLY_SHAPE) {
1877  if (ctx->shape == RECT_SHAPE) {
1878  check_marker(s->avctx, gb, "before width");
1879  width = get_bits(gb, 13);
1880  check_marker(s->avctx, gb, "before height");
1881  height = get_bits(gb, 13);
1882  check_marker(s->avctx, gb, "after height");
1883  if (width && height && /* they should be non zero but who knows */
1884  !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
1885  if (s->width && s->height &&
1886  (s->width != width || s->height != height))
1887  s->context_reinit = 1;
1888  s->width = width;
1889  s->height = height;
1890  }
1891  }
1892 
1893  s->progressive_sequence =
1894  s->progressive_frame = get_bits1(gb) ^ 1;
1895  s->interlaced_dct = 0;
1896  if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1897  av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
1898  "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
1899  if (vo_ver_id == 1)
1900  ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1901  else
1902  ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1903 
1904  if (ctx->vol_sprite_usage == STATIC_SPRITE)
1905  av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1906  if (ctx->vol_sprite_usage == STATIC_SPRITE ||
1907  ctx->vol_sprite_usage == GMC_SPRITE) {
1908  if (ctx->vol_sprite_usage == STATIC_SPRITE) {
1909  skip_bits(gb, 13); // sprite_width
1910  check_marker(s->avctx, gb, "after sprite_width");
1911  skip_bits(gb, 13); // sprite_height
1912  check_marker(s->avctx, gb, "after sprite_height");
1913  skip_bits(gb, 13); // sprite_left
1914  check_marker(s->avctx, gb, "after sprite_left");
1915  skip_bits(gb, 13); // sprite_top
1916  check_marker(s->avctx, gb, "after sprite_top");
1917  }
1918  ctx->num_sprite_warping_points = get_bits(gb, 6);
1919  if (ctx->num_sprite_warping_points > 3) {
1920  av_log(s->avctx, AV_LOG_ERROR,
1921  "%d sprite_warping_points\n",
1922  ctx->num_sprite_warping_points);
1923  ctx->num_sprite_warping_points = 0;
1924  return AVERROR_INVALIDDATA;
1925  }
1926  s->sprite_warping_accuracy = get_bits(gb, 2);
1927  ctx->sprite_brightness_change = get_bits1(gb);
1928  if (ctx->vol_sprite_usage == STATIC_SPRITE)
1929  skip_bits1(gb); // low_latency_sprite
1930  }
1931  // FIXME sadct disable bit if verid!=1 && shape not rect
1932 
1933  if (get_bits1(gb) == 1) { /* not_8_bit */
1934  s->quant_precision = get_bits(gb, 4); /* quant_precision */
1935  if (get_bits(gb, 4) != 8) /* bits_per_pixel */
1936  av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
1937  if (s->quant_precision != 5)
1938  av_log(s->avctx, AV_LOG_ERROR,
1939  "quant precision %d\n", s->quant_precision);
1940  if (s->quant_precision<3 || s->quant_precision>9) {
1941  s->quant_precision = 5;
1942  }
1943  } else {
1944  s->quant_precision = 5;
1945  }
1946 
1947  // FIXME a bunch of grayscale shape things
1948 
1949  if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
1950  int i, v;
1951 
1952  /* load default matrixes */
1953  for (i = 0; i < 64; i++) {
1954  int j = s->idsp.idct_permutation[i];
1956  s->intra_matrix[j] = v;
1957  s->chroma_intra_matrix[j] = v;
1958 
1960  s->inter_matrix[j] = v;
1961  s->chroma_inter_matrix[j] = v;
1962  }
1963 
1964  /* load custom intra matrix */
1965  if (get_bits1(gb)) {
1966  int last = 0;
1967  for (i = 0; i < 64; i++) {
1968  int j;
1969  if (get_bits_left(gb) < 8) {
1970  av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
1971  return AVERROR_INVALIDDATA;
1972  }
1973  v = get_bits(gb, 8);
1974  if (v == 0)
1975  break;
1976 
1977  last = v;
1978  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1979  s->intra_matrix[j] = last;
1980  s->chroma_intra_matrix[j] = last;
1981  }
1982 
1983  /* replicate last value */
1984  for (; i < 64; i++) {
1985  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1986  s->intra_matrix[j] = last;
1987  s->chroma_intra_matrix[j] = last;
1988  }
1989  }
1990 
1991  /* load custom non intra matrix */
1992  if (get_bits1(gb)) {
1993  int last = 0;
1994  for (i = 0; i < 64; i++) {
1995  int j;
1996  if (get_bits_left(gb) < 8) {
1997  av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
1998  return AVERROR_INVALIDDATA;
1999  }
2000  v = get_bits(gb, 8);
2001  if (v == 0)
2002  break;
2003 
2004  last = v;
2005  j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2006  s->inter_matrix[j] = v;
2007  s->chroma_inter_matrix[j] = v;
2008  }
2009 
2010  /* replicate last value */
2011  for (; i < 64; i++) {
2012  int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2013  s->inter_matrix[j] = last;
2014  s->chroma_inter_matrix[j] = last;
2015  }
2016  }
2017 
2018  // FIXME a bunch of grayscale shape things
2019  }
2020 
2021  if (vo_ver_id != 1)
2022  s->quarter_sample = get_bits1(gb);
2023  else
2024  s->quarter_sample = 0;
2025 
2026  if (get_bits_left(gb) < 4) {
2027  av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2028  return AVERROR_INVALIDDATA;
2029  }
2030 
2031  if (!get_bits1(gb)) {
2032  int pos = get_bits_count(gb);
2033  int estimation_method = get_bits(gb, 2);
2034  if (estimation_method < 2) {
2035  if (!get_bits1(gb)) {
2036  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2037  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2038  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2039  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2040  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2041  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2042  }
2043  if (!get_bits1(gb)) {
2044  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2045  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2046  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2047  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2048  }
2049  if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2050  skip_bits_long(gb, pos - get_bits_count(gb));
2051  goto no_cplx_est;
2052  }
2053  if (!get_bits1(gb)) {
2054  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2055  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2056  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2057  ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2058  }
2059  if (!get_bits1(gb)) {
2060  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2061  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2062  ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2063  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2064  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2065  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2066  }
2067  if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2068  skip_bits_long(gb, pos - get_bits_count(gb));
2069  goto no_cplx_est;
2070  }
2071  if (estimation_method == 1) {
2072  ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2073  ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2074  }
2075  } else
2076  av_log(s->avctx, AV_LOG_ERROR,
2077  "Invalid Complexity estimation method %d\n",
2078  estimation_method);
2079  } else {
2080 
2081 no_cplx_est:
2082  ctx->cplx_estimation_trash_i =
2083  ctx->cplx_estimation_trash_p =
2084  ctx->cplx_estimation_trash_b = 0;
2085  }
2086 
2087  ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2088 
2089  s->data_partitioning = get_bits1(gb);
2090  if (s->data_partitioning)
2091  ctx->rvlc = get_bits1(gb);
2092 
2093  if (vo_ver_id != 1) {
2094  ctx->new_pred = get_bits1(gb);
2095  if (ctx->new_pred) {
2096  av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2097  skip_bits(gb, 2); /* requested upstream message type */
2098  skip_bits1(gb); /* newpred segment type */
2099  }
2100  if (get_bits1(gb)) // reduced_res_vop
2101  av_log(s->avctx, AV_LOG_ERROR,
2102  "reduced resolution VOP not supported\n");
2103  } else {
2104  ctx->new_pred = 0;
2105  }
2106 
2107  ctx->scalability = get_bits1(gb);
2108 
2109  if (ctx->scalability) {
2110  GetBitContext bak = *gb;
2111  int h_sampling_factor_n;
2112  int h_sampling_factor_m;
2113  int v_sampling_factor_n;
2114  int v_sampling_factor_m;
2115 
2116  skip_bits1(gb); // hierarchy_type
2117  skip_bits(gb, 4); /* ref_layer_id */
2118  skip_bits1(gb); /* ref_layer_sampling_dir */
2119  h_sampling_factor_n = get_bits(gb, 5);
2120  h_sampling_factor_m = get_bits(gb, 5);
2121  v_sampling_factor_n = get_bits(gb, 5);
2122  v_sampling_factor_m = get_bits(gb, 5);
2123  ctx->enhancement_type = get_bits1(gb);
2124 
2125  if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2126  v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2127  /* illegal scalability header (VERY broken encoder),
2128  * trying to workaround */
2129  ctx->scalability = 0;
2130  *gb = bak;
2131  } else
2132  av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2133 
2134  // bin shape stuff FIXME
2135  }
2136  }
2137 
2138  if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2139  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",
2140  s->avctx->framerate.den, s->avctx->framerate.num,
2141  ctx->time_increment_bits,
2142  s->quant_precision,
2143  s->progressive_sequence,
2144  s->low_delay,
2145  ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2146  s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2147  );
2148  }
2149 
2150  return 0;
2151 }
2152 
2153 /**
2154  * Decode the user data stuff in the header.
2155  * Also initializes divx/xvid/lavc_version/build.
2156  */
2158 {
2159  MpegEncContext *s = &ctx->m;
2160  char buf[256];
2161  int i;
2162  int e;
2163  int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2164  char last;
2165 
2166  for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2167  if (show_bits(gb, 23) == 0)
2168  break;
2169  buf[i] = get_bits(gb, 8);
2170  }
2171  buf[i] = 0;
2172 
2173  /* divx detection */
2174  e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2175  if (e < 2)
2176  e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2177  if (e >= 2) {
2178  ctx->divx_version = ver;
2179  ctx->divx_build = build;
2180  s->divx_packed = e == 3 && last == 'p';
2181  }
2182 
2183  /* libavcodec detection */
2184  e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2185  if (e != 4)
2186  e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2187  if (e != 4) {
2188  e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2189  if (e > 1) {
2190  if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
2192  "Unknown Lavc version string encountered, %d.%d.%d; "
2193  "clamping sub-version values to 8-bits.\n",
2194  ver, ver2, ver3);
2195  }
2196  build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
2197  }
2198  }
2199  if (e != 4) {
2200  if (strcmp(buf, "ffmpeg") == 0)
2201  ctx->lavc_build = 4600;
2202  }
2203  if (e == 4)
2204  ctx->lavc_build = build;
2205 
2206  /* Xvid detection */
2207  e = sscanf(buf, "XviD%d", &build);
2208  if (e == 1)
2209  ctx->xvid_build = build;
2210 
2211  return 0;
2212 }
2213 
2215 {
2216  Mpeg4DecContext *ctx = avctx->priv_data;
2217  MpegEncContext *s = &ctx->m;
2218 
2219  if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2220  if (s->codec_tag == AV_RL32("XVID") ||
2221  s->codec_tag == AV_RL32("XVIX") ||
2222  s->codec_tag == AV_RL32("RMP4") ||
2223  s->codec_tag == AV_RL32("ZMP4") ||
2224  s->codec_tag == AV_RL32("SIPP"))
2225  ctx->xvid_build = 0;
2226  }
2227 
2228  if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2229  if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2230  ctx->vol_control_parameters == 0)
2231  ctx->divx_version = 400; // divx 4
2232 
2233  if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2234  ctx->divx_version =
2235  ctx->divx_build = -1;
2236  }
2237 
2239  if (s->codec_tag == AV_RL32("XVIX"))
2241 
2242  if (s->codec_tag == AV_RL32("UMP4"))
2244 
2245  if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2247 
2248  if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2250 
2251  if (ctx->xvid_build <= 3U)
2252  s->padding_bug_score = 256 * 256 * 256 * 64;
2253 
2254  if (ctx->xvid_build <= 1U)
2256 
2257  if (ctx->xvid_build <= 12U)
2259 
2260  if (ctx->xvid_build <= 32U)
2262 
2263 #define SET_QPEL_FUNC(postfix1, postfix2) \
2264  s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2265  s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2266  s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2267 
2268  if (ctx->lavc_build < 4653U)
2270 
2271  if (ctx->lavc_build < 4655U)
2273 
2274  if (ctx->lavc_build < 4670U)
2276 
2277  if (ctx->lavc_build <= 4712U)
2279 
2280  if ((ctx->lavc_build&0xFF) >= 100) {
2281  if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2282  (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2283  )
2285  }
2286 
2287  if (ctx->divx_version >= 0)
2289  if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2290  s->padding_bug_score = 256 * 256 * 256 * 64;
2291 
2292  if (ctx->divx_version < 500U)
2294 
2295  if (ctx->divx_version >= 0)
2297  }
2298 
2299  if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2300  SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2301  SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2302  SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2303  SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2304  SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2305  SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2306 
2307  SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2308  SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2309  SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2310  SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2311  SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2312  SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2313  }
2314 
2315  if (avctx->debug & FF_DEBUG_BUGS)
2317  "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2318  s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2319  ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2320 
2321  if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2322  s->codec_id == AV_CODEC_ID_MPEG4 &&
2323  avctx->idct_algo == FF_IDCT_AUTO) {
2324  avctx->idct_algo = FF_IDCT_XVID;
2325  ff_mpv_idct_init(s);
2326  return 1;
2327  }
2328 
2329  return 0;
2330 }
2331 
2333 {
2334  MpegEncContext *s = &ctx->m;
2335  int time_incr, time_increment;
2336  int64_t pts;
2337 
2338  s->mcsel = 0;
2339  s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2340  if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2342  av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2343  s->low_delay = 0;
2344  }
2345 
2347  if (s->partitioned_frame)
2349  else
2351 
2352  time_incr = 0;
2353  while (get_bits1(gb) != 0)
2354  time_incr++;
2355 
2356  check_marker(s->avctx, gb, "before time_increment");
2357 
2358  if (ctx->time_increment_bits == 0 ||
2359  !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2361  "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);
2362 
2363  for (ctx->time_increment_bits = 1;
2364  ctx->time_increment_bits < 16;
2365  ctx->time_increment_bits++) {
2366  if (s->pict_type == AV_PICTURE_TYPE_P ||
2367  (s->pict_type == AV_PICTURE_TYPE_S &&
2368  ctx->vol_sprite_usage == GMC_SPRITE)) {
2369  if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2370  break;
2371  } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2372  break;
2373  }
2374 
2376  "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2377  if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2378  s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2380  }
2381  }
2382 
2383  if (IS_3IV1)
2384  time_increment = get_bits1(gb); // FIXME investigate further
2385  else
2386  time_increment = get_bits(gb, ctx->time_increment_bits);
2387 
2388  if (s->pict_type != AV_PICTURE_TYPE_B) {
2389  s->last_time_base = s->time_base;
2390  s->time_base += time_incr;
2391  s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
2392  if (s->workaround_bugs & FF_BUG_UMP4) {
2393  if (s->time < s->last_non_b_time) {
2394  /* header is not mpeg-4-compatible, broken encoder,
2395  * trying to workaround */
2396  s->time_base++;
2397  s->time += s->avctx->framerate.num;
2398  }
2399  }
2400  s->pp_time = s->time - s->last_non_b_time;
2401  s->last_non_b_time = s->time;
2402  } else {
2403  s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
2404  s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2405  if (s->pp_time <= s->pb_time ||
2406  s->pp_time <= s->pp_time - s->pb_time ||
2407  s->pp_time <= 0) {
2408  /* messed up order, maybe after seeking? skipping current B-frame */
2409  return FRAME_SKIPPED;
2410  }
2412 
2413  if (ctx->t_frame == 0)
2414  ctx->t_frame = s->pb_time;
2415  if (ctx->t_frame == 0)
2416  ctx->t_frame = 1; // 1/0 protection
2417  s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2418  ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2419  s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2420  ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2421  if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
2422  s->pb_field_time = 2;
2423  s->pp_field_time = 4;
2424  if (!s->progressive_sequence)
2425  return FRAME_SKIPPED;
2426  }
2427  }
2428 
2429  if (s->avctx->framerate.den)
2430  pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
2431  else
2432  pts = AV_NOPTS_VALUE;
2433  ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
2434 
2435  check_marker(s->avctx, gb, "before vop_coded");
2436 
2437  /* vop coded */
2438  if (get_bits1(gb) != 1) {
2439  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2440  av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2441  return FRAME_SKIPPED;
2442  }
2443  if (ctx->new_pred)
2444  decode_new_pred(ctx, gb);
2445 
2446  if (ctx->shape != BIN_ONLY_SHAPE &&
2447  (s->pict_type == AV_PICTURE_TYPE_P ||
2448  (s->pict_type == AV_PICTURE_TYPE_S &&
2449  ctx->vol_sprite_usage == GMC_SPRITE))) {
2450  /* rounding type for motion estimation */
2451  s->no_rounding = get_bits1(gb);
2452  } else {
2453  s->no_rounding = 0;
2454  }
2455  // FIXME reduced res stuff
2456 
2457  if (ctx->shape != RECT_SHAPE) {
2458  if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2459  skip_bits(gb, 13); /* width */
2460  check_marker(s->avctx, gb, "after width");
2461  skip_bits(gb, 13); /* height */
2462  check_marker(s->avctx, gb, "after height");
2463  skip_bits(gb, 13); /* hor_spat_ref */
2464  check_marker(s->avctx, gb, "after hor_spat_ref");
2465  skip_bits(gb, 13); /* ver_spat_ref */
2466  }
2467  skip_bits1(gb); /* change_CR_disable */
2468 
2469  if (get_bits1(gb) != 0)
2470  skip_bits(gb, 8); /* constant_alpha_value */
2471  }
2472 
2473  // FIXME complexity estimation stuff
2474 
2475  if (ctx->shape != BIN_ONLY_SHAPE) {
2476  skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2477  if (s->pict_type != AV_PICTURE_TYPE_I)
2478  skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2479  if (s->pict_type == AV_PICTURE_TYPE_B)
2480  skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2481 
2482  if (get_bits_left(gb) < 3) {
2483  av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2484  return AVERROR_INVALIDDATA;
2485  }
2486  ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2487  if (!s->progressive_sequence) {
2488  s->top_field_first = get_bits1(gb);
2489  s->alternate_scan = get_bits1(gb);
2490  } else
2491  s->alternate_scan = 0;
2492  }
2493 
2494  if (s->alternate_scan) {
2495  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2496  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2497  ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2498  ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2499  } else {
2500  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2501  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2502  ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2503  ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2504  }
2505 
2506  if (s->pict_type == AV_PICTURE_TYPE_S) {
2507  if((ctx->vol_sprite_usage == STATIC_SPRITE ||
2508  ctx->vol_sprite_usage == GMC_SPRITE)) {
2509  if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2510  return AVERROR_INVALIDDATA;
2511  if (ctx->sprite_brightness_change)
2512  av_log(s->avctx, AV_LOG_ERROR,
2513  "sprite_brightness_change not supported\n");
2514  if (ctx->vol_sprite_usage == STATIC_SPRITE)
2515  av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2516  } else {
2517  memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
2518  memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
2519  }
2520  }
2521 
2522  if (ctx->shape != BIN_ONLY_SHAPE) {
2523  s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2524  if (s->qscale == 0) {
2525  av_log(s->avctx, AV_LOG_ERROR,
2526  "Error, header damaged or not MPEG-4 header (qscale=0)\n");
2527  return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2528  }
2529 
2530  if (s->pict_type != AV_PICTURE_TYPE_I) {
2531  s->f_code = get_bits(gb, 3); /* fcode_for */
2532  if (s->f_code == 0) {
2533  av_log(s->avctx, AV_LOG_ERROR,
2534  "Error, header damaged or not MPEG-4 header (f_code=0)\n");
2535  s->f_code = 1;
2536  return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2537  }
2538  } else
2539  s->f_code = 1;
2540 
2541  if (s->pict_type == AV_PICTURE_TYPE_B) {
2542  s->b_code = get_bits(gb, 3);
2543  if (s->b_code == 0) {
2544  av_log(s->avctx, AV_LOG_ERROR,
2545  "Error, header damaged or not MPEG4 header (b_code=0)\n");
2546  s->b_code=1;
2547  return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
2548  }
2549  } else
2550  s->b_code = 1;
2551 
2552  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2553  av_log(s->avctx, AV_LOG_DEBUG,
2554  "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",
2555  s->qscale, s->f_code, s->b_code,
2556  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")),
2557  gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
2558  s->top_field_first, s->quarter_sample ? "q" : "h",
2559  s->data_partitioning, ctx->resync_marker,
2560  ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2561  1 - s->no_rounding, s->vo_type,
2562  ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
2563  ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2564  ctx->cplx_estimation_trash_b,
2565  s->time,
2566  time_increment
2567  );
2568  }
2569 
2570  if (!ctx->scalability) {
2571  if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2572  skip_bits1(gb); // vop shape coding type
2573  } else {
2574  if (ctx->enhancement_type) {
2575  int load_backward_shape = get_bits1(gb);
2576  if (load_backward_shape)
2577  av_log(s->avctx, AV_LOG_ERROR,
2578  "load backward shape isn't supported\n");
2579  }
2580  skip_bits(gb, 2); // ref_select_code
2581  }
2582  }
2583  /* detect buggy encoders which don't set the low_delay flag
2584  * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
2585  * easily (although it's buggy too) */
2586  if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
2587  ctx->divx_version == -1 && s->picture_number == 0) {
2588  av_log(s->avctx, AV_LOG_WARNING,
2589  "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2590  s->low_delay = 1;
2591  }
2592 
2593  s->picture_number++; // better than pic number==0 always ;)
2594 
2595  // FIXME add short header support
2596  s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2597  s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2598 
2599  if (s->workaround_bugs & FF_BUG_EDGE) {
2600  s->h_edge_pos = s->width;
2601  s->v_edge_pos = s->height;
2602  }
2603  return 0;
2604 }
2605 
2606 /**
2607  * Decode MPEG-4 headers.
2608  * @return <0 if no VOP found (or a damaged one)
2609  * FRAME_SKIPPED if a not coded VOP is found
2610  * 0 if a VOP is found
2611  */
2613 {
2614  MpegEncContext *s = &ctx->m;
2615  unsigned startcode, v;
2616  int ret;
2617  int vol = 0;
2618 
2619  /* search next start code */
2620  align_get_bits(gb);
2621 
2622  if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
2623  skip_bits(gb, 24);
2624  if (get_bits(gb, 8) == 0xF0)
2625  goto end;
2626  }
2627 
2628  startcode = 0xff;
2629  for (;;) {
2630  if (get_bits_count(gb) >= gb->size_in_bits) {
2631  if (gb->size_in_bits == 8 &&
2632  (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
2633  av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
2634  return FRAME_SKIPPED; // divx bug
2635  } else
2636  return -1; // end of stream
2637  }
2638 
2639  /* use the bits after the test */
2640  v = get_bits(gb, 8);
2641  startcode = ((startcode << 8) | v) & 0xffffffff;
2642 
2643  if ((startcode & 0xFFFFFF00) != 0x100)
2644  continue; // no startcode
2645 
2646  if (s->avctx->debug & FF_DEBUG_STARTCODE) {
2647  av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2648  if (startcode <= 0x11F)
2649  av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2650  else if (startcode <= 0x12F)
2651  av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2652  else if (startcode <= 0x13F)
2653  av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2654  else if (startcode <= 0x15F)
2655  av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2656  else if (startcode <= 0x1AF)
2657  av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2658  else if (startcode == 0x1B0)
2659  av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2660  else if (startcode == 0x1B1)
2661  av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2662  else if (startcode == 0x1B2)
2663  av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2664  else if (startcode == 0x1B3)
2665  av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2666  else if (startcode == 0x1B4)
2667  av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2668  else if (startcode == 0x1B5)
2669  av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2670  else if (startcode == 0x1B6)
2671  av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2672  else if (startcode == 0x1B7)
2673  av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2674  else if (startcode == 0x1B8)
2675  av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2676  else if (startcode == 0x1B9)
2677  av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2678  else if (startcode == 0x1BA)
2679  av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2680  else if (startcode == 0x1BB)
2681  av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2682  else if (startcode == 0x1BC)
2683  av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2684  else if (startcode == 0x1BD)
2685  av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2686  else if (startcode == 0x1BE)
2687  av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2688  else if (startcode == 0x1BF)
2689  av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2690  else if (startcode == 0x1C0)
2691  av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2692  else if (startcode == 0x1C1)
2693  av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2694  else if (startcode == 0x1C2)
2695  av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2696  else if (startcode == 0x1C3)
2697  av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2698  else if (startcode <= 0x1C5)
2699  av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2700  else if (startcode <= 0x1FF)
2701  av_log(s->avctx, AV_LOG_DEBUG, "System start");
2702  av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2703  }
2704 
2705  if (startcode >= 0x120 && startcode <= 0x12F) {
2706  if (vol) {
2707  av_log(s->avctx, AV_LOG_ERROR, "Multiple VOL headers");
2708  return AVERROR_INVALIDDATA;
2709  }
2710  vol++;
2711  if ((ret = decode_vol_header(ctx, gb)) < 0)
2712  return ret;
2713  } else if (startcode == USER_DATA_STARTCODE) {
2714  decode_user_data(ctx, gb);
2715  } else if (startcode == GOP_STARTCODE) {
2716  mpeg4_decode_gop_header(s, gb);
2717  } else if (startcode == VOS_STARTCODE) {
2719  } else if (startcode == VISUAL_OBJ_STARTCODE) {
2721  } else if (startcode == VOP_STARTCODE) {
2722  break;
2723  }
2724 
2725  align_get_bits(gb);
2726  startcode = 0xff;
2727  }
2728 
2729 end:
2731  s->low_delay = 1;
2732  s->avctx->has_b_frames = !s->low_delay;
2733 
2734  return decode_vop_header(ctx, gb);
2735 }
2736 
2738  static int done = 0;
2739 
2740  if (!done) {
2747  INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2748  &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2749  &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2750  INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2751  &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2752  &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2753  INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2754  &ff_sprite_trajectory_tab[0][1], 4, 2,
2755  &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2756  INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2757  &ff_mb_type_b_tab[0][1], 2, 1,
2758  &ff_mb_type_b_tab[0][0], 2, 1, 16);
2759  done = 1;
2760  }
2761 }
2762 
2763 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
2764 {
2765  Mpeg4DecContext *ctx = avctx->priv_data;
2766  MpegEncContext *s = &ctx->m;
2767 
2768  /* divx 5.01+ bitstream reorder stuff */
2769  /* Since this clobbers the input buffer and hwaccel codecs still need the
2770  * data during hwaccel->end_frame we should not do this any earlier */
2771  if (s->divx_packed) {
2772  int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
2773  int startcode_found = 0;
2774 
2775  if (buf_size - current_pos > 7) {
2776 
2777  int i;
2778  for (i = current_pos; i < buf_size - 4; i++)
2779 
2780  if (buf[i] == 0 &&
2781  buf[i + 1] == 0 &&
2782  buf[i + 2] == 1 &&
2783  buf[i + 3] == 0xB6) {
2784  startcode_found = !(buf[i + 4] & 0x40);
2785  break;
2786  }
2787  }
2788 
2789  if (startcode_found) {
2790  if (!ctx->showed_packed_warning) {
2791  av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
2792  "wasteful way to store B-frames ('packed B-frames'). "
2793  "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
2794  ctx->showed_packed_warning = 1;
2795  }
2798  buf_size - current_pos);
2799  if (!s->bitstream_buffer) {
2800  s->bitstream_buffer_size = 0;
2801  return AVERROR(ENOMEM);
2802  }
2803  memcpy(s->bitstream_buffer, buf + current_pos,
2804  buf_size - current_pos);
2805  s->bitstream_buffer_size = buf_size - current_pos;
2806  }
2807  }
2808 
2809  return 0;
2810 }
2811 
2812 #if HAVE_THREADS
2813 static int mpeg4_update_thread_context(AVCodecContext *dst,
2814  const AVCodecContext *src)
2815 {
2816  Mpeg4DecContext *s = dst->priv_data;
2817  const Mpeg4DecContext *s1 = src->priv_data;
2818  int init = s->m.context_initialized;
2819 
2820  int ret = ff_mpeg_update_thread_context(dst, src);
2821 
2822  if (ret < 0)
2823  return ret;
2824 
2825  memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
2826 
2827  if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
2828  ff_xvid_idct_init(&s->m.idsp, dst);
2829 
2830  return 0;
2831 }
2832 #endif
2833 
2835 {
2836  Mpeg4DecContext *ctx = avctx->priv_data;
2837  MpegEncContext *s = &ctx->m;
2838  int ret;
2839 
2840  ctx->divx_version =
2841  ctx->divx_build =
2842  ctx->xvid_build =
2843  ctx->lavc_build = -1;
2844 
2845  if ((ret = ff_h263_decode_init(avctx)) < 0)
2846  return ret;
2847 
2849 
2850  s->h263_pred = 1;
2851  s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
2853  ctx->time_increment_bits = 4; /* default value for broken headers */
2854 
2856  avctx->internal->allocate_progress = 1;
2857 
2858  return 0;
2859 }
2860 
2861 static const AVOption mpeg4_options[] = {
2862  {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
2863  {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
2864  {NULL}
2865 };
2866 
2867 static const AVClass mpeg4_class = {
2868  .class_name = "MPEG4 Video Decoder",
2869  .item_name = av_default_item_name,
2870  .option = mpeg4_options,
2871  .version = LIBAVUTIL_VERSION_INT,
2872 };
2873 
2875  .name = "mpeg4",
2876  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2877  .type = AVMEDIA_TYPE_VIDEO,
2878  .id = AV_CODEC_ID_MPEG4,
2879  .priv_data_size = sizeof(Mpeg4DecContext),
2880  .init = decode_init,
2881  .close = ff_h263_decode_end,
2886  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2887  .flush = ff_mpeg_flush,
2888  .max_lowres = 3,
2891  .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
2892  .priv_class = &mpeg4_class,
2893  .hw_configs = (const AVCodecHWConfigInternal*[]) {
2894 #if CONFIG_MPEG4_NVDEC_HWACCEL
2895  HWACCEL_NVDEC(mpeg4),
2896 #endif
2897 #if CONFIG_MPEG4_VAAPI_HWACCEL
2898  HWACCEL_VAAPI(mpeg4),
2899 #endif
2900 #if CONFIG_MPEG4_VDPAU_HWACCEL
2901  HWACCEL_VDPAU(mpeg4),
2902 #endif
2903 #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
2904  HWACCEL_VIDEOTOOLBOX(mpeg4),
2905 #endif
2906  NULL
2907  },
2908 };
int bitstream_buffer_size
Definition: mpegvideo.h:411
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:397
uint16_t sprite_traj[4][2]
sprite trajectory points
Definition: mpeg4video.h:78
#define AV_EF_AGGRESSIVE
consider things that a sane encoder should not do as an error
Definition: avcodec.h:2642
#define ff_tlog(ctx,...)
Definition: internal.h:75
IDCTDSPContext idsp
Definition: mpegvideo.h:227
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3012
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2202
static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
int aspect_ratio_info
Definition: mpegvideo.h:397
int picture_number
Definition: mpegvideo.h:124
const char * s
Definition: avisynth_c.h:768
#define MB_TYPE_L1
Definition: mpegutils.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define FF_BUG_XVID_ILACE
Definition: avcodec.h:2533
ScanTable intra_v_scantable
Definition: mpegvideo.h:90
static int shift(int a, int b)
Definition: sonic.c:82
S(GMC)-VOP MPEG-4.
Definition: avutil.h:277
RLTable ff_mpeg4_rl_intra
Definition: mpeg4data.h:109
static float alpha(float a)
const uint8_t ff_mpeg4_c_dc_scale_table[32]
Definition: mpeg4data.h:363
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:520
#define CBPY_VLC_BITS
Definition: h263.h:39
AVOption.
Definition: opt.h:246
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:266
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:398
int last_mv[2][2][2]
last MV, used for MV prediction in MPEG-1 & B-frame MPEG-4
Definition: mpegvideo.h:275
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:501
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
#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)
Definition: get_bits.h:204
#define MB_TYPE_B_VLC_BITS
Definition: mpeg4videodec.c:46
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
VLC ff_h263_intra_MCBPC_vlc
Definition: ituh263dec.c:98
int resync_marker
could this stream contain resync markers
Definition: mpeg4video.h:85
#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:191
VLC ff_h263_cbpy_vlc
Definition: ituh263dec.c:100
#define SKIP_COUNTER(name, gb, num)
Definition: get_bits.h:167
RLTable ff_rvlc_rl_inter
Definition: mpeg4data.h:214
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2132
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
Definition: get_bits.h:407
int num
Numerator.
Definition: rational.h:59
uint8_t * bitstream_buffer
Definition: mpegvideo.h:410
enum AVCodecID codec_id
Definition: mpegvideo.h:109
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
Definition: avcodec.h:2641
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
#define FF_BUG_HPEL_CHROMA
Definition: avcodec.h:2542
const uint8_t * buffer
Definition: get_bits.h:56
void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
Predict the ac.
Definition: mpeg4videodec.c:64
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:1880
static av_cold int decode_init(AVCodecContext *avctx)
#define MB_TYPE_INTRA
Definition: mpegutils.h:73
#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:71
int real_sprite_warping_points
Definition: mpegvideo.h:391
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition: avcodec.h:2635
mpegvideo header.
#define DC_VLC_BITS
Definition: mpeg4videodec.c:45
#define DC_MARKER
Definition: mpeg4video.h:57
uint8_t permutated[64]
Definition: idctdsp.h:33
uint8_t run
Definition: svq3.c:206
#define ER_MV_ERROR
int padding_bug_score
used to detect the VERY common padding bug in MPEG-4
Definition: mpegvideo.h:406
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:498
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:130
#define src
Definition: vp8dsp.c:254
int profile
profile
Definition: avcodec.h:2827
AVCodec.
Definition: avcodec.h:3365
int time_base
time in seconds of last I,P,S Frame
Definition: mpegvideo.h:384
RLTable.
Definition: rl.h:39
int qscale
QP.
Definition: mpegvideo.h:201
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
const AVProfile ff_mpeg4_video_profiles[]
Definition: profiles.c:107
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:307
int field_select[2][2]
Definition: mpegvideo.h:274
int block_wrap[6]
Definition: mpegvideo.h:291
int quant_precision
Definition: mpegvideo.h:395
void ff_clean_intra_table_entries(MpegEncContext *s)
Clean dc, ac, coded_block for the current non-intra MB.
Definition: mpegvideo.c:1894
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1624
int vol_sprite_usage
Definition: mpeg4video.h:74
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:981
#define USES_LIST(a, list)
Definition: mpegutils.h:99
#define USER_DATA_STARTCODE
Definition: mpeg4video.h:60
int mb_num_left
number of MBs left in this video packet (for partitioned Slices only)
Definition: mpegvideo.h:356
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
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:261
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:2583
const uint16_t ff_sprite_trajectory_tab[15][2]
Definition: mpeg4data.h:326
#define VOP_STARTCODE
Definition: mpeg4video.h:63
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
#define BIN_ONLY_SHAPE
Definition: mpeg4video.h:35
#define IS_3IV1
Definition: mpeg4video.h:173
uint8_t * pred_dir_table
used to store pred_dir for partitioned decoding
Definition: mpegvideo.h:197
Multithreading support functions.
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:875
#define FF_BUG_EDGE
Definition: avcodec.h:2541
int interlaced_dct
Definition: mpegvideo.h:478
#define MB_TYPE_16x16
Definition: mpegutils.h:54
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:177
#define CHROMA_420
Definition: mpegvideo.h:470
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
Definition: mpeg4data.h:41
#define MB_TYPE_ACPRED
Definition: mpegutils.h:60
#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.
#define FF_BUG_QPEL_CHROMA2
Definition: avcodec.h:2539
#define FF_BUG_STD_QPEL
Definition: avcodec.h:2538
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
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:126
int codec_tag
internal codec_tag upper case converted from avctx codec_tag
Definition: mpegvideo.h:117
int intra_dc_threshold
QP above which the ac VLC should be used for intra dc.
Definition: mpeg4video.h:95
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2139
#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:183
#define FF_BUG_IEDGE
Definition: avcodec.h:2546
#define av_log(a,...)
#define MOTION_MARKER
Definition: mpeg4video.h:56
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:724
#define FF_IDCT_AUTO
Definition: avcodec.h:2698
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:2265
MpegEncContext m
Definition: mpeg4video.h:69
int sprite_offset[2][2]
sprite offset[isChroma][isMVY]
Definition: mpegvideo.h:392
#define ROUNDED_DIV(a, b)
Definition: common.h:56
ThreadFrame tf
Definition: mpegpicture.h:47
#define FF_BUG_DIRECT_BLOCKSIZE
Definition: avcodec.h:2540
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:587
#define FF_IDCT_XVID
Definition: avcodec.h:2705
int16_t * dc_val[3]
used for MPEG-4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:184
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
#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:1791
uint8_t * mbskip_table
Definition: mpegpicture.h:59
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:192
int partitioned_frame
is current frame partitioned
Definition: mpegvideo.h:400
#define STATIC_SPRITE
Definition: mpeg4video.h:53
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:548
static int get_amv(Mpeg4DecContext *ctx, int n)
Get the average motion vector for a GMC MB.
const char * r
Definition: vf_curves.c:111
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1582
#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:2535
uint16_t width
Definition: gdv.c:47
#define MB_TYPE_GMC
Definition: mpegutils.h:61
#define FF_DEBUG_BUGS
Definition: avcodec.h:2599
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:3372
#define IS_SKIP(a)
Definition: mpegutils.h:81
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:396
int low_delay
no reordering needed / has no B-frames
Definition: mpegvideo.h:401
#define SET_QPEL_FUNC(postfix1, postfix2)
GetBitContext gb
Definition: mpegvideo.h:443
int(* decode_mb)(struct MpegEncContext *s, int16_t block[6][64])
Definition: mpegvideo.h:497
#define CLOSE_READER(name, gb)
Definition: get_bits.h:131
#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:1012
#define GET_RL_VLC(level, run, name, gb, table, bits,max_depth, need_update)
Definition: get_bits.h:509
Definition: vlc.h:26
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo.c:2234
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:175
#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:353
const uint8_t ff_alternate_horizontal_scan[64]
Definition: mpegvideodata.c:89
int ff_h263_decode_init(AVCodecContext *avctx)
Definition: h263dec.c:62
common internal API header
int sprite_delta[2][2]
sprite_delta [isY][isMVY]
Definition: mpegvideo.h:393
#define ADV_SIMPLE_VO_TYPE
Definition: mpeg4video.h:44
const uint8_t ff_mpeg4_DCtab_lum[13][2]
Definition: mpeg4data.h:35
uint8_t ff_mpeg4_static_rl_table_store[3][2][2 *MAX_RUN+MAX_LEVEL+3]
Definition: mpeg4video.c:28
VLC ff_h263_inter_MCBPC_vlc
Definition: ituh263dec.c:99
int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: h263dec.c:409
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: avcodec.h:950
static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
static const int mb_type_b_map[4]
Definition: mpeg4videodec.c:52
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2626
#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:48
static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
Decode first partition.
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:2697
static int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
Decode the dc value.
int16_t(*[2] motion_val)[2]
Definition: mpegpicture.h:53
#define GRAY_SHAPE
Definition: mpeg4video.h:36
#define ER_DC_END
enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[]
Definition: h263dec.c:714
AVCodec ff_mpeg4_decoder
unsigned int allocated_bitstream_buffer_size
Definition: mpegvideo.h:412
int size_in_bits
Definition: get_bits.h:58
AVFormatContext * ctx
Definition: movenc.c:48
int use_intra_dc_vlc
Definition: mpeg4video.h:92
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2111
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:296
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int level
level
Definition: avcodec.h:2925
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:181
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:554
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:83
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:1633
int vol_control_parameters
does the stream contain the low_delay flag, used to work around buggy encoders.
Definition: mpeg4video.h:107
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:195
#define MB_TYPE_INTERLACED
Definition: mpegutils.h:58
#define FF_BUG_QPEL_CHROMA
Definition: avcodec.h:2537
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: mpegvideo.c:483
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:193
#define VOT_VIDEO_ID
Definition: mpeg4video.h:46
#define SPRITE_TRAJ_VLC_BITS
Definition: mpeg4videodec.c:44
#define FF_BUG_UMP4
Definition: avcodec.h:2534
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:290
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:499
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define MB_TYPE_8x8
Definition: mpegutils.h:57
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:263
int first_slice_line
used in MPEG-4 too to handle resync markers
Definition: mpegvideo.h:431
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:260
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
#define FF_ASPECT_EXTENDED
Definition: h263.h:30
BlockDSPContext bdsp
Definition: mpegvideo.h:223
int debug
debug
Definition: avcodec.h:2582
main external API structure.
Definition: avcodec.h:1502
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:88
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:97
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
Definition: get_bits.h:218
#define OPEN_READER(name, gb)
Definition: get_bits.h:120
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:500
int data_partitioning
data partitioning flag from header
Definition: mpegvideo.h:399
int showed_packed_warning
flag for having shown the warning about invalid Divx B-frames
Definition: mpeg4video.h:104
int progressive_sequence
Definition: mpegvideo.h:451
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:338
Describe the class of an AVClass context structure.
Definition: log.h:67
ScanTable intra_h_scantable
Definition: mpegvideo.h:89
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
static const AVProfile profiles[]
uint8_t * cbp_table
used to store cbp, ac_pred for partitioned decoding
Definition: mpegvideo.h:196
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2125
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2118
#define VOT_STILL_TEXTURE_ID
Definition: mpeg4video.h:47
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode MPEG-4 headers.
int context_initialized
Definition: mpegvideo.h:121
#define GET_CACHE(name, gb)
Definition: get_bits.h:197
#define AV_EF_IGNORE_ERR
ignore errors and continue
Definition: avcodec.h:2639
RLTable ff_h263_rl_inter
Definition: h263data.c:161
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
#define s1
Definition: regdef.h:38
#define SKIP_CACHE(name, gb, num)
Definition: get_bits.h:162
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:235
#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:346
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:80
#define MV_DIR_FORWARD
Definition: mpegvideo.h:259
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:209
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
#define AV_CODEC_CAP_TRUNCATED
Definition: avcodec.h:957
const uint8_t ff_mpeg4_dc_threshold[8]
Definition: mpeg4data.h:372
#define FF_BUG_AUTODETECT
autodetection
Definition: avcodec.h:2532
int h263_pred
use MPEG-4/H.263 ac/dc predictions
Definition: mpegvideo.h:102
#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
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:273
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:128
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:498
MpegEncContext.
Definition: mpegvideo.h:78
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:180
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:95
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:194
#define VISUAL_OBJ_STARTCODE
Definition: mpeg4video.h:62
static VLC sprite_trajectory
Definition: mpeg4videodec.c:49
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
#define FF_BUG_AMV
Definition: avcodec.h:2536
#define MB_TYPE_L0L1
Definition: mpegutils.h:69
static const AVOption mpeg4_options[]
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:127
if(ret< 0)
Definition: vf_mcdeint.c:279
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: internal.h:60
#define TEX_VLC_BITS
Definition: dv.h:96
static VLC dc_chrom
Definition: mpeg4videodec.c:48
Bi-dir predicted.
Definition: avutil.h:276
static VLC mb_type_b_vlc
Definition: mpeg4videodec.c:50
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:149
void * priv_data
Definition: avcodec.h:1529
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:2596
#define IS_8X8(a)
Definition: mpegutils.h:89
int len
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1537
int resync_mb_y
y position of last resync marker
Definition: mpegvideo.h:354
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:495
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:165
#define IS_ACPRED(a)
Definition: mpegutils.h:94
static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb)
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:464
static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
int chroma_qscale
chroma QP
Definition: mpegvideo.h:202
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
int ff_h263_decode_motion(MpegEncContext *s, int pred, int f_code)
Definition: ituh263dec.c:255
#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
uint32_t * mb_type
types and macros are defined in mpegutils.h
Definition: mpegpicture.h:56
#define HWACCEL_VDPAU(codec)
Definition: hwaccel.h:75
int time_increment_bits
number of bits to represent the fractional part of time
Definition: mpeg4video.h:72
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
Definition: mpegvideo.h:116
int num_sprite_warping_points
Definition: mpeg4video.h:76
#define stride
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
Definition: mpegvideo.h:264
int b_code
backward MV resolution for B-frames (MPEG-4)
Definition: mpegvideo.h:236
#define FF_BUG_DC_CLIP
Definition: avcodec.h:2543
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.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
#define MB_TYPE_L0
Definition: mpegutils.h:67
for(j=16;j >0;--j)
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
Predicted.
Definition: avutil.h:275
#define INTRA_MCBPC_VLC_BITS
Definition: h263.h:37
#define GOP_STARTCODE
Definition: mpeg4video.h:61
#define VOS_STARTCODE
Definition: mpeg4video.h:59
#define GMC_SPRITE
Definition: mpeg4video.h:54