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