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