FFmpeg  4.4
h263dec.c
Go to the documentation of this file.
1 /*
2  * H.263 decoder
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 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 /**
24  * @file
25  * H.263 decoder.
26  */
27 
28 #define UNCHECKED_BITSTREAM_READER 1
29 
30 #include "libavutil/cpu.h"
32 
33 #include "avcodec.h"
34 #include "error_resilience.h"
35 #include "flv.h"
36 #include "h263.h"
37 #include "h263_parser.h"
38 #include "hwconfig.h"
39 #include "internal.h"
40 #include "mpeg_er.h"
41 #include "mpeg4video.h"
42 #include "mpeg4video_parser.h"
43 #include "mpegutils.h"
44 #include "mpegvideo.h"
45 #include "msmpeg4.h"
46 #include "qpeldsp.h"
47 #include "thread.h"
48 #include "wmv2.h"
49 
51 {
52  /* MPEG-4 Studio Profile only, not supported by hardware */
53  if (avctx->bits_per_raw_sample > 8) {
54  av_assert1(((MpegEncContext *)avctx->priv_data)->studio_profile);
55  return avctx->pix_fmt;
56  }
57 
58  if (avctx->codec->id == AV_CODEC_ID_MSS2)
59  return AV_PIX_FMT_YUV420P;
60 
61  if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY)) {
64  return AV_PIX_FMT_GRAY8;
65  }
66 
67  return avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
68 }
69 
71 {
72  MpegEncContext *s = avctx->priv_data;
73  int ret;
74 
75  s->out_format = FMT_H263;
76 
77  // set defaults
78  ff_mpv_decode_init(s, avctx);
79 
80  s->quant_precision = 5;
81  s->decode_mb = ff_h263_decode_mb;
82  s->low_delay = 1;
83  s->unrestricted_mv = 1;
84 
85  /* select sub codec */
86  switch (avctx->codec->id) {
87  case AV_CODEC_ID_H263:
88  case AV_CODEC_ID_H263P:
89  s->unrestricted_mv = 0;
91  break;
92  case AV_CODEC_ID_MPEG4:
93  break;
95  s->h263_pred = 1;
96  s->msmpeg4_version = 1;
97  break;
99  s->h263_pred = 1;
100  s->msmpeg4_version = 2;
101  break;
103  s->h263_pred = 1;
104  s->msmpeg4_version = 3;
105  break;
106  case AV_CODEC_ID_WMV1:
107  s->h263_pred = 1;
108  s->msmpeg4_version = 4;
109  break;
110  case AV_CODEC_ID_WMV2:
111  s->h263_pred = 1;
112  s->msmpeg4_version = 5;
113  break;
114  case AV_CODEC_ID_VC1:
115  case AV_CODEC_ID_WMV3:
118  case AV_CODEC_ID_MSS2:
119  s->h263_pred = 1;
120  s->msmpeg4_version = 6;
122  break;
123  case AV_CODEC_ID_H263I:
124  break;
125  case AV_CODEC_ID_FLV1:
126  s->h263_flv = 1;
127  break;
128  default:
129  av_log(avctx, AV_LOG_ERROR, "Unsupported codec %d\n",
130  avctx->codec->id);
131  return AVERROR(ENOSYS);
132  }
133  s->codec_id = avctx->codec->id;
134 
135  if (avctx->codec_tag == AV_RL32("L263") || avctx->codec_tag == AV_RL32("S263"))
136  if (avctx->extradata_size == 56 && avctx->extradata[0] == 1)
137  s->ehc_mode = 1;
138 
139  /* for H.263, we allocate the images after having read the header */
140  if (avctx->codec->id != AV_CODEC_ID_H263 &&
141  avctx->codec->id != AV_CODEC_ID_H263P &&
142  avctx->codec->id != AV_CODEC_ID_MPEG4) {
143  avctx->pix_fmt = h263_get_format(avctx);
145  if ((ret = ff_mpv_common_init(s)) < 0)
146  return ret;
147  }
148 
149  ff_h263dsp_init(&s->h263dsp);
150  ff_qpeldsp_init(&s->qdsp);
152 
153  return 0;
154 }
155 
157 {
158  MpegEncContext *s = avctx->priv_data;
159 
161  return 0;
162 }
163 
164 /**
165  * Return the number of bytes consumed for building the current frame.
166  */
167 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
168 {
169  int pos = (get_bits_count(&s->gb) + 7) >> 3;
170 
171  if (s->divx_packed || s->avctx->hwaccel) {
172  /* We would have to scan through the whole buf to handle the weird
173  * reordering ... */
174  return buf_size;
175  } else if (s->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
176  pos -= s->parse_context.last_index;
177  // padding is not really read so this might be -1
178  if (pos < 0)
179  pos = 0;
180  return pos;
181  } else {
182  // avoid infinite loops (maybe not needed...)
183  if (pos == 0)
184  pos = 1;
185  // oops ;)
186  if (pos + 10 > buf_size)
187  pos = buf_size;
188 
189  return pos;
190  }
191 }
192 
194 {
195  const int part_mask = s->partitioned_frame
196  ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
197  const int mb_size = 16 >> s->avctx->lowres;
198  int ret;
199 
200  s->last_resync_gb = s->gb;
201  s->first_slice_line = 1;
202  s->resync_mb_x = s->mb_x;
203  s->resync_mb_y = s->mb_y;
204 
205  ff_set_qscale(s, s->qscale);
206 
207  if (s->studio_profile) {
208  if ((ret = ff_mpeg4_decode_studio_slice_header(s->avctx->priv_data)) < 0)
209  return ret;
210  }
211 
212  if (s->avctx->hwaccel) {
213  const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
214  ret = s->avctx->hwaccel->decode_slice(s->avctx, start, s->gb.buffer_end - start);
215  // ensure we exit decode loop
216  s->mb_y = s->mb_height;
217  return ret;
218  }
219 
220  if (s->partitioned_frame) {
221  const int qscale = s->qscale;
222 
223  if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
224  if ((ret = ff_mpeg4_decode_partitions(s->avctx->priv_data)) < 0)
225  return ret;
226 
227  /* restore variables which were modified */
228  s->first_slice_line = 1;
229  s->mb_x = s->resync_mb_x;
230  s->mb_y = s->resync_mb_y;
231  ff_set_qscale(s, qscale);
232  }
233 
234  for (; s->mb_y < s->mb_height; s->mb_y++) {
235  /* per-row end of slice checks */
236  if (s->msmpeg4_version) {
237  if (s->resync_mb_y + s->slice_height == s->mb_y) {
238  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
239  s->mb_x - 1, s->mb_y, ER_MB_END);
240 
241  return 0;
242  }
243  }
244 
245  if (s->msmpeg4_version == 1) {
246  s->last_dc[0] =
247  s->last_dc[1] =
248  s->last_dc[2] = 128;
249  }
250 
252  for (; s->mb_x < s->mb_width; s->mb_x++) {
253  int ret;
254 
256 
257  if (s->resync_mb_x == s->mb_x && s->resync_mb_y + 1 == s->mb_y)
258  s->first_slice_line = 0;
259 
260  /* DCT & quantize */
261 
262  s->mv_dir = MV_DIR_FORWARD;
263  s->mv_type = MV_TYPE_16X16;
264  ff_dlog(s, "%d %06X\n",
265  get_bits_count(&s->gb), show_bits(&s->gb, 24));
266 
267  ff_tlog(NULL, "Decoding MB at %dx%d\n", s->mb_x, s->mb_y);
268  ret = s->decode_mb(s, s->block);
269 
270  if (s->pict_type != AV_PICTURE_TYPE_B)
272 
273  if (ret < 0) {
274  const int xy = s->mb_x + s->mb_y * s->mb_stride;
275  if (ret == SLICE_END) {
276  ff_mpv_reconstruct_mb(s, s->block);
277  if (s->loop_filter)
279 
280  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
281  s->mb_x, s->mb_y, ER_MB_END & part_mask);
282 
283  s->padding_bug_score--;
284 
285  if (++s->mb_x >= s->mb_width) {
286  s->mb_x = 0;
287  ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
289  s->mb_y++;
290  }
291  return 0;
292  } else if (ret == SLICE_NOEND) {
293  av_log(s->avctx, AV_LOG_ERROR,
294  "Slice mismatch at MB: %d\n", xy);
295  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
296  s->mb_x + 1, s->mb_y,
297  ER_MB_END & part_mask);
298  return AVERROR_INVALIDDATA;
299  }
300  av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
301  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
302  s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
303 
304  if (s->avctx->err_recognition & AV_EF_IGNORE_ERR)
305  continue;
306  return AVERROR_INVALIDDATA;
307  }
308 
309  ff_mpv_reconstruct_mb(s, s->block);
310  if (s->loop_filter)
312  }
313 
314  ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
316 
317  s->mb_x = 0;
318  }
319 
320  av_assert1(s->mb_x == 0 && s->mb_y == s->mb_height);
321 
322  // Detect incorrect padding with wrong stuffing codes used by NEC N-02B
323  if (s->codec_id == AV_CODEC_ID_MPEG4 &&
324  (s->workaround_bugs & FF_BUG_AUTODETECT) &&
325  get_bits_left(&s->gb) >= 48 &&
326  show_bits(&s->gb, 24) == 0x4010 &&
327  !s->data_partitioning)
328  s->padding_bug_score += 32;
329 
330  /* try to detect the padding bug */
331  if (s->codec_id == AV_CODEC_ID_MPEG4 &&
332  (s->workaround_bugs & FF_BUG_AUTODETECT) &&
333  get_bits_left(&s->gb) >= 0 &&
334  get_bits_left(&s->gb) < 137 &&
335  !s->data_partitioning) {
336  const int bits_count = get_bits_count(&s->gb);
337  const int bits_left = s->gb.size_in_bits - bits_count;
338 
339  if (bits_left == 0) {
340  s->padding_bug_score += 16;
341  } else if (bits_left != 1) {
342  int v = show_bits(&s->gb, 8);
343  v |= 0x7F >> (7 - (bits_count & 7));
344 
345  if (v == 0x7F && bits_left <= 8)
346  s->padding_bug_score--;
347  else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
348  bits_left <= 16)
349  s->padding_bug_score += 4;
350  else
351  s->padding_bug_score++;
352  }
353  }
354 
355  if (s->codec_id == AV_CODEC_ID_H263 &&
356  (s->workaround_bugs & FF_BUG_AUTODETECT) &&
357  get_bits_left(&s->gb) >= 8 &&
358  get_bits_left(&s->gb) < 300 &&
359  s->pict_type == AV_PICTURE_TYPE_I &&
360  show_bits(&s->gb, 8) == 0 &&
361  !s->data_partitioning) {
362 
363  s->padding_bug_score += 32;
364  }
365 
366  if (s->codec_id == AV_CODEC_ID_H263 &&
367  (s->workaround_bugs & FF_BUG_AUTODETECT) &&
368  get_bits_left(&s->gb) >= 64 &&
369  AV_RB64(s->gb.buffer_end - 8) == 0xCDCDCDCDFC7F0000) {
370 
371  s->padding_bug_score += 32;
372  }
373 
374  if (s->workaround_bugs & FF_BUG_AUTODETECT) {
375  if (
376  (s->padding_bug_score > -2 && !s->data_partitioning))
377  s->workaround_bugs |= FF_BUG_NO_PADDING;
378  else
379  s->workaround_bugs &= ~FF_BUG_NO_PADDING;
380  }
381 
382  // handle formats which don't have unique end markers
383  if (s->msmpeg4_version || (s->workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
384  int left = get_bits_left(&s->gb);
385  int max_extra = 7;
386 
387  /* no markers in M$ crap */
388  if (s->msmpeg4_version && s->pict_type == AV_PICTURE_TYPE_I)
389  max_extra += 17;
390 
391  /* buggy padding but the frame should still end approximately at
392  * the bitstream end */
393  if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
394  (s->avctx->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
395  max_extra += 48;
396  else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
397  max_extra += 256 * 256 * 256 * 64;
398 
399  if (left > max_extra)
400  av_log(s->avctx, AV_LOG_ERROR,
401  "discarding %d junk bits at end, next would be %X\n",
402  left, show_bits(&s->gb, 24));
403  else if (left < 0)
404  av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
405  else
406  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
407  s->mb_x - 1, s->mb_y, ER_MB_END);
408 
409  return 0;
410  }
411 
412  av_log(s->avctx, AV_LOG_ERROR,
413  "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
414  get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
415 
416  ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
417  ER_MB_END & part_mask);
418 
419  return AVERROR_INVALIDDATA;
420 }
421 
422 int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
423  AVPacket *avpkt)
424 {
425  const uint8_t *buf = avpkt->data;
426  int buf_size = avpkt->size;
427  MpegEncContext *s = avctx->priv_data;
428  int ret;
429  int slice_ret = 0;
430  AVFrame *pict = data;
431 
432  /* no supplementary picture */
433  if (buf_size == 0) {
434  /* special case for last picture */
435  if (s->low_delay == 0 && s->next_picture_ptr) {
436  if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
437  return ret;
438  s->next_picture_ptr = NULL;
439 
440  *got_frame = 1;
441  }
442 
443  return 0;
444  }
445 
446  if (s->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
447  int next;
448 
449  if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) {
450  next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
451  } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) {
452  next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
453  } else if (CONFIG_H263P_DECODER && s->codec_id == AV_CODEC_ID_H263P) {
454  next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
455  } else {
456  av_log(s->avctx, AV_LOG_ERROR,
457  "this codec does not support truncated bitstreams\n");
458  return AVERROR(ENOSYS);
459  }
460 
461  if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf,
462  &buf_size) < 0)
463  return buf_size;
464  }
465 
466 retry:
467  if (s->divx_packed && s->bitstream_buffer_size) {
468  int i;
469  for(i=0; i < buf_size-3; i++) {
470  if (buf[i]==0 && buf[i+1]==0 && buf[i+2]==1) {
471  if (buf[i+3]==0xB0) {
472  av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
473  s->bitstream_buffer_size = 0;
474  }
475  break;
476  }
477  }
478  }
479 
480  if (s->bitstream_buffer_size && (s->divx_packed || buf_size <= MAX_NVOP_SIZE)) // divx 5.01+/xvid frame reorder
481  ret = init_get_bits8(&s->gb, s->bitstream_buffer,
482  s->bitstream_buffer_size);
483  else
484  ret = init_get_bits8(&s->gb, buf, buf_size);
485 
486  s->bitstream_buffer_size = 0;
487  if (ret < 0)
488  return ret;
489 
490  if (!s->context_initialized)
491  // we need the idct permutation for reading a custom matrix
493 
494  /* let's go :-) */
495  if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
497  } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
499  } else if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
500  if (s->avctx->extradata_size && s->picture_number == 0) {
501  GetBitContext gb;
502 
503  if (init_get_bits8(&gb, s->avctx->extradata, s->avctx->extradata_size) >= 0 )
505  }
506  ret = ff_mpeg4_decode_picture_header(avctx->priv_data, &s->gb, 0);
507  } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
509  } else if (CONFIG_FLV_DECODER && s->h263_flv) {
511  } else {
513  }
514 
515  if (ret < 0 || ret == FRAME_SKIPPED) {
516  if ( s->width != avctx->coded_width
517  || s->height != avctx->coded_height) {
518  av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
519  s->width = avctx->coded_width;
520  s->height= avctx->coded_height;
521  }
522  }
523  if (ret == FRAME_SKIPPED)
524  return get_consumed_bytes(s, buf_size);
525 
526  /* skip if the header was thrashed */
527  if (ret < 0) {
528  av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
529  return ret;
530  }
531 
532  if (!s->context_initialized) {
533  avctx->pix_fmt = h263_get_format(avctx);
534  if ((ret = ff_mpv_common_init(s)) < 0)
535  return ret;
536  }
537 
538  if (!s->current_picture_ptr || s->current_picture_ptr->f->data[0]) {
539  int i = ff_find_unused_picture(s->avctx, s->picture, 0);
540  if (i < 0)
541  return i;
542  s->current_picture_ptr = &s->picture[i];
543  }
544 
545  avctx->has_b_frames = !s->low_delay;
546 
548  if (ff_mpeg4_workaround_bugs(avctx) == 1)
549  goto retry;
550  if (s->studio_profile != (s->idsp.idct == NULL))
552  }
553 
554  /* After H.263 & MPEG-4 header decode we have the height, width,
555  * and other parameters. So then we could init the picture.
556  * FIXME: By the way H.263 decoder is evolving it should have
557  * an H263EncContext */
558  if (s->width != avctx->coded_width ||
559  s->height != avctx->coded_height ||
560  s->context_reinit) {
561  /* H.263 could change picture size any time */
562  s->context_reinit = 0;
563 
564  ret = ff_set_dimensions(avctx, s->width, s->height);
565  if (ret < 0)
566  return ret;
567 
568  ff_set_sar(avctx, avctx->sample_aspect_ratio);
569 
570  if ((ret = ff_mpv_common_frame_size_change(s)))
571  return ret;
572 
573  if (avctx->pix_fmt != h263_get_format(avctx)) {
574  av_log(avctx, AV_LOG_ERROR, "format change not supported\n");
575  avctx->pix_fmt = AV_PIX_FMT_NONE;
576  return AVERROR_UNKNOWN;
577  }
578  }
579 
580  if (s->codec_id == AV_CODEC_ID_H263 ||
581  s->codec_id == AV_CODEC_ID_H263P ||
582  s->codec_id == AV_CODEC_ID_H263I)
583  s->gob_index = H263_GOB_HEIGHT(s->height);
584 
585  // for skipping the frame
586  s->current_picture.f->pict_type = s->pict_type;
587  s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
588 
589  /* skip B-frames if we don't have reference frames */
590  if (!s->last_picture_ptr &&
591  (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
592  return get_consumed_bytes(s, buf_size);
593  if ((avctx->skip_frame >= AVDISCARD_NONREF &&
594  s->pict_type == AV_PICTURE_TYPE_B) ||
595  (avctx->skip_frame >= AVDISCARD_NONKEY &&
596  s->pict_type != AV_PICTURE_TYPE_I) ||
597  avctx->skip_frame >= AVDISCARD_ALL)
598  return get_consumed_bytes(s, buf_size);
599 
600  if (s->next_p_frame_damaged) {
601  if (s->pict_type == AV_PICTURE_TYPE_B)
602  return get_consumed_bytes(s, buf_size);
603  else
604  s->next_p_frame_damaged = 0;
605  }
606 
607  if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
608  s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
609  s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
610  } else {
611  s->me.qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
612  s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
613  }
614 
615  if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
616  return ret;
617 
618  if (!s->divx_packed && !avctx->hwaccel)
619  ff_thread_finish_setup(avctx);
620 
621  if (avctx->hwaccel) {
622  ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer,
623  s->gb.buffer_end - s->gb.buffer);
624  if (ret < 0 )
625  return ret;
626  }
627 
629 
630  /* the second part of the wmv2 header contains the MB skip bits which
631  * are stored in current_picture->mb_type which is not available before
632  * ff_mpv_frame_start() */
633  if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
635  if (ret < 0)
636  return ret;
637  if (ret == 1)
638  goto frame_end;
639  }
640 
641  /* decode each macroblock */
642  s->mb_x = 0;
643  s->mb_y = 0;
644 
645  slice_ret = decode_slice(s);
646  while (s->mb_y < s->mb_height) {
647  if (s->msmpeg4_version) {
648  if (s->slice_height == 0 || s->mb_x != 0 || slice_ret < 0 ||
649  (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
650  break;
651  } else {
652  int prev_x = s->mb_x, prev_y = s->mb_y;
653  if (ff_h263_resync(s) < 0)
654  break;
655  if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
656  s->er.error_occurred = 1;
657  }
658 
659  if (s->msmpeg4_version < 4 && s->h263_pred)
661 
662  if (decode_slice(s) < 0)
663  slice_ret = AVERROR_INVALIDDATA;
664  }
665 
666  if (s->msmpeg4_version && s->msmpeg4_version < 4 &&
667  s->pict_type == AV_PICTURE_TYPE_I)
668  if (!CONFIG_MSMPEG4_DECODER ||
669  ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
670  s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
671 
672  av_assert1(s->bitstream_buffer_size == 0);
673 frame_end:
674  if (!s->studio_profile)
675  ff_er_frame_end(&s->er);
676 
677  if (avctx->hwaccel) {
678  ret = avctx->hwaccel->end_frame(avctx);
679  if (ret < 0)
680  return ret;
681  }
682 
684 
686  ff_mpeg4_frame_end(avctx, buf, buf_size);
687 
688  if (!s->divx_packed && avctx->hwaccel)
689  ff_thread_finish_setup(avctx);
690 
691  av_assert1(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
692  av_assert1(s->current_picture.f->pict_type == s->pict_type);
693  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
694  if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
695  return ret;
696  ff_print_debug_info(s, s->current_picture_ptr, pict);
697  ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
698  } else if (s->last_picture_ptr) {
699  if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
700  return ret;
701  ff_print_debug_info(s, s->last_picture_ptr, pict);
702  ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
703  }
704 
705  if (s->last_picture_ptr || s->low_delay) {
706  if ( pict->format == AV_PIX_FMT_YUV420P
707  && (s->codec_tag == AV_RL32("GEOV") || s->codec_tag == AV_RL32("GEOX"))) {
708  int x, y, p;
710  for (p=0; p<3; p++) {
711  int w = AV_CEIL_RSHIFT(pict-> width, !!p);
712  int h = AV_CEIL_RSHIFT(pict->height, !!p);
713  int linesize = pict->linesize[p];
714  for (y=0; y<(h>>1); y++)
715  for (x=0; x<w; x++)
716  FFSWAP(int,
717  pict->data[p][x + y*linesize],
718  pict->data[p][x + (h-1-y)*linesize]);
719  }
720  }
721  *got_frame = 1;
722  }
723 
724  if (slice_ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
725  return slice_ret;
726  else
727  return get_consumed_bytes(s, buf_size);
728 }
729 
731 #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
733 #endif
734 #if CONFIG_MPEG4_NVDEC_HWACCEL
736 #endif
737 #if CONFIG_MPEG4_VDPAU_HWACCEL
739 #endif
740 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL || CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
742 #endif
745 };
746 
748 #if CONFIG_H263_VAAPI_HWACCEL
749  HWACCEL_VAAPI(h263),
750 #endif
751 #if CONFIG_MPEG4_NVDEC_HWACCEL
752  HWACCEL_NVDEC(mpeg4),
753 #endif
754 #if CONFIG_MPEG4_VDPAU_HWACCEL
755  HWACCEL_VDPAU(mpeg4),
756 #endif
757 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
758  HWACCEL_VIDEOTOOLBOX(h263),
759 #endif
760  NULL
761 };
762 
764  .name = "h263",
765  .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
766  .type = AVMEDIA_TYPE_VIDEO,
767  .id = AV_CODEC_ID_H263,
768  .priv_data_size = sizeof(MpegEncContext),
770  .close = ff_h263_decode_end,
774  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
775  .flush = ff_mpeg_flush,
776  .max_lowres = 3,
778  .hw_configs = ff_h263_hw_config_list,
779 };
780 
782  .name = "h263p",
783  .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
784  .type = AVMEDIA_TYPE_VIDEO,
785  .id = AV_CODEC_ID_H263P,
786  .priv_data_size = sizeof(MpegEncContext),
788  .close = ff_h263_decode_end,
792  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
793  .flush = ff_mpeg_flush,
794  .max_lowres = 3,
796  .hw_configs = ff_h263_hw_config_list,
797 };
static void flush(AVCodecContext *avctx)
#define av_cold
Definition: attributes.h:88
uint8_t
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
Libavcodec external API header.
#define AV_EF_IGNORE_ERR
ignore errors and continue
Definition: avcodec.h:1658
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1656
#define AV_EF_BUFFER
detect improper bitstream length
Definition: avcodec.h:1655
#define AV_EF_AGGRESSIVE
consider things that a sane encoder should not do as an error
Definition: avcodec.h:1661
#define FF_BUG_AUTODETECT
autodetection
Definition: avcodec.h:1573
#define FF_BUG_NO_PADDING
Definition: avcodec.h:1576
#define AV_RL32
Definition: intreadwrite.h:146
#define AV_RB64
Definition: intreadwrite.h:164
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFSWAP(type, a, b)
Definition: common.h:108
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define CONFIG_H263_DECODER
Definition: config.h:821
#define CONFIG_MPEG4_DECODER
Definition: config.h:871
#define CONFIG_WMV2_DECODER
Definition: config.h:1005
#define CONFIG_FLV_DECODER
Definition: config.h:812
#define CONFIG_GRAY
Definition: config.h:556
#define CONFIG_H263P_DECODER
Definition: config.h:823
#define CONFIG_H263I_DECODER
Definition: config.h:822
#define NULL
Definition: coverity.c:32
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1317
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
void ff_er_frame_end(ERContext *s)
#define ER_AC_ERROR
#define ER_MB_END
#define ER_AC_END
#define ER_MB_ERROR
@ HWACCEL_VIDEOTOOLBOX
Definition: ffmpeg.h:62
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: codec.h:44
#define AV_CODEC_CAP_TRUNCATED
Definition: codec.h:53
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_CODEC_FLAG_TRUNCATED
Input bitstream might be truncated at a random location instead of only at frame boundaries.
Definition: avcodec.h:317
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:308
@ AV_CODEC_ID_MSMPEG4V1
Definition: codec_id.h:63
@ AV_CODEC_ID_FLV1
Definition: codec_id.h:70
@ AV_CODEC_ID_WMV2
Definition: codec_id.h:67
@ AV_CODEC_ID_MSMPEG4V2
Definition: codec_id.h:64
@ AV_CODEC_ID_VC1IMAGE
Definition: codec_id.h:201
@ AV_CODEC_ID_VC1
Definition: codec_id.h:119
@ AV_CODEC_ID_WMV3IMAGE
Definition: codec_id.h:200
@ AV_CODEC_ID_WMV1
Definition: codec_id.h:66
@ AV_CODEC_ID_MSS2
Definition: codec_id.h:216
@ AV_CODEC_ID_H263
Definition: codec_id.h:53
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:61
@ AV_CODEC_ID_WMV3
Definition: codec_id.h:120
@ AV_CODEC_ID_H263I
Definition: codec_id.h:69
@ AV_CODEC_ID_H263P
Definition: codec_id.h:68
@ AV_CODEC_ID_MSMPEG4V3
Definition: codec_id.h:65
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: avcodec.h:235
@ AVDISCARD_NONREF
discard all non reference
Definition: avcodec.h:232
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:611
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
void ff_h263_update_motion_val(MpegEncContext *s)
Definition: h263.c:42
void ff_h263_loop_filter(MpegEncContext *s)
Definition: h263.c:135
int ff_intel_h263_decode_picture_header(MpegEncContext *s)
Definition: intelh263dec.c:27
int ff_h263_resync(MpegEncContext *s)
Decode the group of blocks / video packet header / slice header (MPEG-4 Studio).
Definition: ituh263dec.c:212
void ff_h263_decode_init_vlc(void)
Definition: ituh263dec.c:108
int ff_h263_decode_mb(MpegEncContext *s, int16_t block[6][64])
Definition: ituh263dec.c:696
#define H263_GOB_HEIGHT(h)
Definition: h263.h:43
int ff_h263_decode_picture_header(MpegEncContext *s)
Definition: ituh263dec.c:998
int ff_h263_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
Definition: h263_parser.c:30
av_cold void ff_h263dsp_init(H263DSPContext *ctx)
Definition: h263dsp.c:117
#define HWACCEL_VDPAU(codec)
Definition: hwconfig.h:75
#define HWACCEL_NVDEC(codec)
Definition: hwconfig.h:71
#define HWACCEL_VAAPI(codec)
Definition: hwconfig.h:73
int i
Definition: input.c:407
int ff_flv_decode_picture_header(MpegEncContext *s)
Definition: flvdec.c:28
av_cold int ff_h263_decode_init(AVCodecContext *avctx)
Definition: h263dec.c:70
static enum AVPixelFormat h263_get_format(AVCodecContext *avctx)
Definition: h263dec.c:50
const AVCodecHWConfigInternal *const ff_h263_hw_config_list[]
Definition: h263dec.c:747
static int get_consumed_bytes(MpegEncContext *s, int buf_size)
Return the number of bytes consumed for building the current frame.
Definition: h263dec.c:167
enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[]
Definition: h263dec.c:730
av_cold int ff_h263_decode_end(AVCodecContext *avctx)
Definition: h263dec.c:156
static int decode_slice(MpegEncContext *s)
Definition: h263dec.c:193
AVCodec ff_h263_decoder
Definition: h263dec.c:763
int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: h263dec.c:422
AVCodec ff_h263p_decoder
Definition: h263dec.c:781
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:99
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: internal.h:61
#define ff_tlog(ctx,...)
Definition: internal.h:91
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
#define FF_QSCALE_TYPE_MPEG1
Definition: internal.h:97
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
uint8_t w
Definition: llviddspenc.c:39
void ff_mpeg4_clean_buffers(MpegEncContext *s)
Definition: mpeg4video.c:45
int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
Decode the next video packet.
int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
#define MAX_NVOP_SIZE
Definition: mpeg4video.h:72
int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
Decode the first and second partition.
int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, int header)
Decode MPEG-4 headers.
int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
void ff_mpeg_er_frame_start(MpegEncContext *s)
Definition: mpeg_er.c:46
int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
Definition: mpegpicture.c:440
#define FRAME_SKIPPED
Return value for header parsers if frame is not coded.
Definition: mpegutils.h:34
@ FMT_H263
Definition: mpegutils.h:126
int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
generic function called after decoding the header and before a frame is decoded.
Definition: mpegvideo.c:1181
void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1111
void ff_mpv_frame_end(MpegEncContext *s)
Definition: mpegvideo.c:1405
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition: mpegvideo.c:913
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo.c:2300
int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
Definition: mpegvideo.c:1420
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:331
void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
Definition: mpegvideo.c:1413
void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mpegvideo.c:2248
void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
Initialize the given MpegEncContext for decoding.
Definition: mpegvideo.c:699
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:2331
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2267
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:2260
int ff_mpv_common_frame_size_change(MpegEncContext *s)
Definition: mpegvideo.c:1055
void ff_mpv_report_decode_progress(MpegEncContext *s)
Definition: mpegvideo.c:2345
mpegvideo header.
#define SLICE_END
end marker found
Definition: mpegvideo.h:523
#define MV_DIR_FORWARD
Definition: mpegvideo.h:262
#define SLICE_NOEND
no end marker or error found but mb count exceeded
Definition: mpegvideo.h:524
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:266
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:750
static void frame_end(MpegEncContext *s)
#define CONFIG_MSMPEG4_DECODER
Definition: msmpeg4.h:69
int ff_msmpeg4_decode_ext_header(MpegEncContext *s, int buf_size)
Definition: msmpeg4dec.c:560
int ff_msmpeg4_decode_picture_header(MpegEncContext *s)
Definition: msmpeg4dec.c:404
const char data[16]
Definition: mxf.c:142
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:238
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:607
@ AVCHROMA_LOC_CENTER
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:608
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:569
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:552
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_VIDEOTOOLBOX
hardware decoding through Videotoolbox
Definition: pixfmt.h:282
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:235
@ AV_PIX_FMT_VAAPI
Definition: pixfmt.h:122
@ AV_PIX_FMT_VDPAU
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:197
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
av_cold void ff_qpeldsp_init(QpelDSPContext *c)
Definition: qpeldsp.c:783
quarterpel DSP functions
unsigned int pos
Definition: spdifenc.c:412
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1171
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:561
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:915
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1680
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:826
const struct AVCodec * codec
Definition: avcodec.h:545
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1747
int coded_height
Definition: avcodec.h:724
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1178
enum AVCodecID codec_id
Definition: avcodec.h:546
int extradata_size
Definition: avcodec.h:638
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:724
void * priv_data
Definition: avcodec.h:563
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2006
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1645
AVCodec.
Definition: codec.h:197
enum AVCodecID id
Definition: codec.h:211
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:218
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int height
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
int(* start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Called at the beginning of each frame or field picture.
Definition: avcodec.h:2500
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:2539
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
MpegEncContext.
Definition: mpegvideo.h:81
#define ff_dlog(a,...)
#define av_log(a,...)
#define width
int ff_wmv2_decode_picture_header(MpegEncContext *s)
Definition: wmv2dec.c:136
int ff_wmv2_decode_secondary_picture_header(MpegEncContext *s)
Definition: wmv2dec.c:171