FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #define UNCHECKED_BITSTREAM_READER 1
29 
30 #include "libavutil/avassert.h"
31 #include "libavutil/display.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/stereo3d.h"
35 #include "libavutil/timer.h"
36 #include "internal.h"
37 #include "bytestream.h"
38 #include "cabac.h"
39 #include "cabac_functions.h"
40 #include "error_resilience.h"
41 #include "avcodec.h"
42 #include "h264.h"
43 #include "h2645_parse.h"
44 #include "h264data.h"
45 #include "h264chroma.h"
46 #include "h264_mvpred.h"
47 #include "golomb.h"
48 #include "mathops.h"
49 #include "me_cmp.h"
50 #include "mpegutils.h"
51 #include "profiles.h"
52 #include "rectangle.h"
53 #include "thread.h"
54 #include "vdpau_compat.h"
55 
56 static int h264_decode_end(AVCodecContext *avctx);
57 
58 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
59 
61 {
62  H264Context *h = avctx->priv_data;
63  return h ? h->sps.num_reorder_frames : 0;
64 }
65 
66 static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
67  int (*mv)[2][4][2],
68  int mb_x, int mb_y, int mb_intra, int mb_skipped)
69 {
70  H264Context *h = opaque;
71  H264SliceContext *sl = &h->slice_ctx[0];
72 
73  sl->mb_x = mb_x;
74  sl->mb_y = mb_y;
75  sl->mb_xy = mb_x + mb_y * h->mb_stride;
76  memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
77  av_assert1(ref >= 0);
78  /* FIXME: It is possible albeit uncommon that slice references
79  * differ between slices. We take the easy approach and ignore
80  * it for now. If this turns out to have any relevance in
81  * practice then correct remapping should be added. */
82  if (ref >= sl->ref_count[0])
83  ref = 0;
84  if (!sl->ref_list[0][ref].data[0]) {
85  av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
86  ref = 0;
87  }
88  if ((sl->ref_list[0][ref].reference&3) != 3) {
89  av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
90  return;
91  }
92  fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
93  2, 2, 2, ref, 1);
94  fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
95  fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
96  pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
97  sl->mb_mbaff =
98  sl->mb_field_decoding_flag = 0;
100 }
101 
103  int y, int height)
104 {
105  AVCodecContext *avctx = h->avctx;
106  const AVFrame *src = h->cur_pic.f;
108  int vshift = desc->log2_chroma_h;
109  const int field_pic = h->picture_structure != PICT_FRAME;
110  if (field_pic) {
111  height <<= 1;
112  y <<= 1;
113  }
114 
115  height = FFMIN(height, avctx->height - y);
116 
117  if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
118  return;
119 
120  if (avctx->draw_horiz_band) {
122  int i;
123 
124  offset[0] = y * src->linesize[0];
125  offset[1] =
126  offset[2] = (y >> vshift) * src->linesize[1];
127  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
128  offset[i] = 0;
129 
130  emms_c();
131 
132  avctx->draw_horiz_band(avctx, src, offset,
133  y, h->picture_structure, height);
134  }
135 }
136 
138 {
139  int i;
140 
143  av_freep(&h->cbp_table);
144  av_freep(&h->mvd_table[0]);
145  av_freep(&h->mvd_table[1]);
146  av_freep(&h->direct_table);
149  h->slice_table = NULL;
150  av_freep(&h->list_counts);
151 
152  av_freep(&h->mb2b_xy);
153  av_freep(&h->mb2br_xy);
154 
159 
160  for (i = 0; i < h->nb_slice_ctx; i++) {
161  H264SliceContext *sl = &h->slice_ctx[i];
162 
163  av_freep(&sl->dc_val_base);
164  av_freep(&sl->er.mb_index2xy);
166  av_freep(&sl->er.er_temp_buffer);
167 
170  av_freep(&sl->top_borders[0]);
171  av_freep(&sl->top_borders[1]);
172 
175  sl->top_borders_allocated[0] = 0;
176  sl->top_borders_allocated[1] = 0;
177  }
178 }
179 
181 {
182  const int big_mb_num = h->mb_stride * (h->mb_height + 1);
183  const int row_mb_num = 2*h->mb_stride*FFMAX(h->avctx->thread_count, 1);
184  int x, y;
185 
187  row_mb_num, 8 * sizeof(uint8_t), fail)
189 
191  big_mb_num * 48 * sizeof(uint8_t), fail)
193  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
195  big_mb_num * sizeof(uint16_t), fail)
197  big_mb_num * sizeof(uint8_t), fail)
199  row_mb_num, 16 * sizeof(uint8_t), fail);
201  row_mb_num, 16 * sizeof(uint8_t), fail);
202  h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
203  h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
204 
206  4 * big_mb_num * sizeof(uint8_t), fail);
208  big_mb_num * sizeof(uint8_t), fail)
209 
210  memset(h->slice_table_base, -1,
211  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
212  h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
213 
215  big_mb_num * sizeof(uint32_t), fail);
217  big_mb_num * sizeof(uint32_t), fail);
218  for (y = 0; y < h->mb_height; y++)
219  for (x = 0; x < h->mb_width; x++) {
220  const int mb_xy = x + y * h->mb_stride;
221  const int b_xy = 4 * x + 4 * y * h->b_stride;
222 
223  h->mb2b_xy[mb_xy] = b_xy;
224  h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
225  }
226 
227  if (!h->dequant4_coeff[0])
229 
230  return 0;
231 
232 fail:
234  return AVERROR(ENOMEM);
235 }
236 
237 /**
238  * Init context
239  * Allocate buffers which are not shared amongst multiple threads.
240  */
242 {
243  ERContext *er = &sl->er;
244  int mb_array_size = h->mb_height * h->mb_stride;
245  int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
246  int c_size = h->mb_stride * (h->mb_height + 1);
247  int yc_size = y_size + 2 * c_size;
248  int x, y, i;
249 
250  sl->ref_cache[0][scan8[5] + 1] =
251  sl->ref_cache[0][scan8[7] + 1] =
252  sl->ref_cache[0][scan8[13] + 1] =
253  sl->ref_cache[1][scan8[5] + 1] =
254  sl->ref_cache[1][scan8[7] + 1] =
255  sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
256 
257  if (sl != h->slice_ctx) {
258  memset(er, 0, sizeof(*er));
259  } else
260  if (CONFIG_ERROR_RESILIENCE) {
261 
262  /* init ER */
263  er->avctx = h->avctx;
265  er->opaque = h;
266  er->quarter_sample = 1;
267 
268  er->mb_num = h->mb_num;
269  er->mb_width = h->mb_width;
270  er->mb_height = h->mb_height;
271  er->mb_stride = h->mb_stride;
272  er->b8_stride = h->mb_width * 2 + 1;
273 
274  // error resilience code looks cleaner with this
276  (h->mb_num + 1) * sizeof(int), fail);
277 
278  for (y = 0; y < h->mb_height; y++)
279  for (x = 0; x < h->mb_width; x++)
280  er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
281 
282  er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
283  h->mb_stride + h->mb_width;
284 
286  mb_array_size * sizeof(uint8_t), fail);
287 
289  h->mb_height * h->mb_stride, fail);
290 
292  yc_size * sizeof(int16_t), fail);
293  er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
294  er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
295  er->dc_val[2] = er->dc_val[1] + c_size;
296  for (i = 0; i < yc_size; i++)
297  sl->dc_val_base[i] = 1024;
298  }
299 
300  return 0;
301 
302 fail:
303  return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
304 }
305 
306 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
307  int parse_extradata);
308 
309 /* There are (invalid) samples in the wild with mp4-style extradata, where the
310  * parameter sets are stored unescaped (i.e. as RBSP).
311  * This function catches the parameter set decoding failure and tries again
312  * after escaping it */
313 static int decode_extradata_ps_mp4(H264Context *h, const uint8_t *buf, int buf_size)
314 {
315  int ret;
316 
317  ret = decode_nal_units(h, buf, buf_size, 1);
318  if (ret < 0 && !(h->avctx->err_recognition & AV_EF_EXPLODE)) {
319  GetByteContext gbc;
320  PutByteContext pbc;
321  uint8_t *escaped_buf;
322  int escaped_buf_size;
323 
325  "SPS decoding failure, trying again after escaping the NAL\n");
326 
327  if (buf_size / 2 >= (INT16_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 3)
328  return AVERROR(ERANGE);
329  escaped_buf_size = buf_size * 3 / 2 + AV_INPUT_BUFFER_PADDING_SIZE;
330  escaped_buf = av_mallocz(escaped_buf_size);
331  if (!escaped_buf)
332  return AVERROR(ENOMEM);
333 
334  bytestream2_init(&gbc, buf, buf_size);
335  bytestream2_init_writer(&pbc, escaped_buf, escaped_buf_size);
336 
337  while (bytestream2_get_bytes_left(&gbc)) {
338  if (bytestream2_get_bytes_left(&gbc) >= 3 &&
339  bytestream2_peek_be24(&gbc) <= 3) {
340  bytestream2_put_be24(&pbc, 3);
341  bytestream2_skip(&gbc, 2);
342  } else
343  bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
344  }
345 
346  escaped_buf_size = bytestream2_tell_p(&pbc);
347  AV_WB16(escaped_buf, escaped_buf_size - 2);
348 
349  ret = decode_nal_units(h, escaped_buf, escaped_buf_size, 1);
350  av_freep(&escaped_buf);
351  if (ret < 0)
352  return ret;
353  }
354 
355  return 0;
356 }
357 
359 {
360  AVCodecContext *avctx = h->avctx;
361  int ret;
362 
363  if (!buf || size <= 0)
364  return -1;
365 
366  if (buf[0] == 1) {
367  int i, cnt, nalsize;
368  const unsigned char *p = buf;
369 
370  h->is_avc = 1;
371 
372  if (size < 7) {
373  av_log(avctx, AV_LOG_ERROR,
374  "avcC %d too short\n", size);
375  return AVERROR_INVALIDDATA;
376  }
377  /* sps and pps in the avcC always have length coded with 2 bytes,
378  * so put a fake nal_length_size = 2 while parsing them */
379  h->nal_length_size = 2;
380  // Decode sps from avcC
381  cnt = *(p + 5) & 0x1f; // Number of sps
382  p += 6;
383  for (i = 0; i < cnt; i++) {
384  nalsize = AV_RB16(p) + 2;
385  if(nalsize > size - (p-buf))
386  return AVERROR_INVALIDDATA;
387  ret = decode_extradata_ps_mp4(h, p, nalsize);
388  if (ret < 0) {
389  av_log(avctx, AV_LOG_ERROR,
390  "Decoding sps %d from avcC failed\n", i);
391  return ret;
392  }
393  p += nalsize;
394  }
395  // Decode pps from avcC
396  cnt = *(p++); // Number of pps
397  for (i = 0; i < cnt; i++) {
398  nalsize = AV_RB16(p) + 2;
399  if(nalsize > size - (p-buf))
400  return AVERROR_INVALIDDATA;
401  ret = decode_extradata_ps_mp4(h, p, nalsize);
402  if (ret < 0) {
403  av_log(avctx, AV_LOG_ERROR,
404  "Decoding pps %d from avcC failed\n", i);
405  return ret;
406  }
407  p += nalsize;
408  }
409  // Store right nal length size that will be used to parse all other nals
410  h->nal_length_size = (buf[4] & 0x03) + 1;
411  } else {
412  h->is_avc = 0;
413  ret = decode_nal_units(h, buf, size, 1);
414  if (ret < 0)
415  return ret;
416  }
417  return size;
418 }
419 
421 {
422  int i;
423 
424  h->avctx = avctx;
425  h->backup_width = -1;
426  h->backup_height = -1;
428  h->dequant_coeff_pps = -1;
429  h->current_sps_id = -1;
430  h->cur_chroma_format_idc = -1;
431 
433  h->slice_context_count = 1;
434  h->workaround_bugs = avctx->workaround_bugs;
435  h->flags = avctx->flags;
436  h->prev_poc_msb = 1 << 16;
437  h->x264_build = -1;
438  h->recovery_frame = -1;
439  h->frame_recovered = 0;
440  h->prev_frame_num = -1;
442 
443  h->next_outputed_poc = INT_MIN;
444  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
445  h->last_pocs[i] = INT_MIN;
446 
448 
450 
452  h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
453  if (!h->slice_ctx) {
454  h->nb_slice_ctx = 0;
455  return AVERROR(ENOMEM);
456  }
457 
458  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
459  h->DPB[i].f = av_frame_alloc();
460  if (!h->DPB[i].f)
461  return AVERROR(ENOMEM);
462  }
463 
464  h->cur_pic.f = av_frame_alloc();
465  if (!h->cur_pic.f)
466  return AVERROR(ENOMEM);
467 
469  if (!h->last_pic_for_ec.f)
470  return AVERROR(ENOMEM);
471 
472  for (i = 0; i < h->nb_slice_ctx; i++)
473  h->slice_ctx[i].h264 = h;
474 
475  return 0;
476 }
477 
479 
481 {
482  H264Context *h = avctx->priv_data;
483  int ret;
484 
485  ret = h264_init_context(avctx, h);
486  if (ret < 0)
487  return ret;
488 
489  /* set defaults */
490  if (!avctx->has_b_frames)
491  h->low_delay = 1;
492 
493  ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
494  if (ret != 0) {
495  av_log(avctx, AV_LOG_ERROR, "pthread_once has failed.");
496  return AVERROR_UNKNOWN;
497  }
498 
499  if (avctx->codec_id == AV_CODEC_ID_H264) {
500  if (avctx->ticks_per_frame == 1) {
501  if(h->avctx->time_base.den < INT_MAX/2) {
502  h->avctx->time_base.den *= 2;
503  } else
504  h->avctx->time_base.num /= 2;
505  }
506  avctx->ticks_per_frame = 2;
507  }
508 
509  if (avctx->extradata_size > 0 && avctx->extradata) {
510  ret = ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
511  if (ret < 0) {
512  h264_decode_end(avctx);
513  return ret;
514  }
515  }
516 
520  h->low_delay = 0;
521  }
522 
523  avctx->internal->allocate_progress = 1;
524 
526 
527  if (h->enable_er < 0 && (avctx->active_thread_type & FF_THREAD_SLICE))
528  h->enable_er = 0;
529 
530  if (h->enable_er && (avctx->active_thread_type & FF_THREAD_SLICE)) {
531  av_log(avctx, AV_LOG_WARNING,
532  "Error resilience with slice threads is enabled. It is unsafe and unsupported and may crash. "
533  "Use it at your own risk\n");
534  }
535 
536  return 0;
537 }
538 
539 #if HAVE_THREADS
540 static int decode_init_thread_copy(AVCodecContext *avctx)
541 {
542  H264Context *h = avctx->priv_data;
543  int ret;
544 
545  if (!avctx->internal->is_copy)
546  return 0;
547 
548  memset(h, 0, sizeof(*h));
549 
550  ret = h264_init_context(avctx, h);
551  if (ret < 0)
552  return ret;
553 
554  h->context_initialized = 0;
555 
556  return 0;
557 }
558 #endif
559 
560 /**
561  * Run setup operations that must be run after slice header decoding.
562  * This includes finding the next displayed frame.
563  *
564  * @param h h264 master context
565  * @param setup_finished enough NALs have been read that we can call
566  * ff_thread_finish_setup()
567  */
568 static void decode_postinit(H264Context *h, int setup_finished)
569 {
571  H264Picture *cur = h->cur_pic_ptr;
572  int i, pics, out_of_order, out_idx;
573 
574  h->cur_pic_ptr->f->pict_type = h->pict_type;
575 
576  if (h->next_output_pic)
577  return;
578 
579  if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
580  /* FIXME: if we have two PAFF fields in one packet, we can't start
581  * the next thread here. If we have one field per packet, we can.
582  * The check in decode_nal_units() is not good enough to find this
583  * yet, so we assume the worst for now. */
584  // if (setup_finished)
585  // ff_thread_finish_setup(h->avctx);
586  if (cur->field_poc[0] == INT_MAX && cur->field_poc[1] == INT_MAX)
587  return;
588  if (h->avctx->hwaccel || h->missing_fields <=1)
589  return;
590  }
591 
592  cur->f->interlaced_frame = 0;
593  cur->f->repeat_pict = 0;
594 
595  /* Signal interlacing information externally. */
596  /* Prioritize picture timing SEI information over used
597  * decoding process if it exists. */
598 
599  if (h->sps.pic_struct_present_flag) {
600  switch (h->sei_pic_struct) {
602  break;
605  cur->f->interlaced_frame = 1;
606  break;
609  if (FIELD_OR_MBAFF_PICTURE(h))
610  cur->f->interlaced_frame = 1;
611  else
612  // try to flag soft telecine progressive
614  break;
617  /* Signal the possibility of telecined film externally
618  * (pic_struct 5,6). From these hints, let the applications
619  * decide if they apply deinterlacing. */
620  cur->f->repeat_pict = 1;
621  break;
623  cur->f->repeat_pict = 2;
624  break;
626  cur->f->repeat_pict = 4;
627  break;
628  }
629 
630  if ((h->sei_ct_type & 3) &&
632  cur->f->interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
633  } else {
634  /* Derive interlacing flag from used decoding process. */
636  }
638 
639  if (cur->field_poc[0] != cur->field_poc[1]) {
640  /* Derive top_field_first from field pocs. */
641  cur->f->top_field_first = cur->field_poc[0] < cur->field_poc[1];
642  } else {
643  if (h->sps.pic_struct_present_flag) {
644  /* Use picture timing SEI information. Even if it is a
645  * information of a past frame, better than nothing. */
648  cur->f->top_field_first = 1;
649  else
650  cur->f->top_field_first = 0;
651  } else if (cur->f->interlaced_frame) {
652  /* Default to top field first when pic_struct_present_flag
653  * is not set but interlaced frame detected */
654  cur->f->top_field_first = 1;
655  } else {
656  /* Most likely progressive */
657  cur->f->top_field_first = 0;
658  }
659  }
660 
661  if (h->sei_frame_packing_present &&
666  AVStereo3D *stereo = av_stereo3d_create_side_data(cur->f);
667  if (stereo) {
668  switch (h->frame_packing_arrangement_type) {
669  case 0:
670  stereo->type = AV_STEREO3D_CHECKERBOARD;
671  break;
672  case 1:
673  stereo->type = AV_STEREO3D_COLUMNS;
674  break;
675  case 2:
676  stereo->type = AV_STEREO3D_LINES;
677  break;
678  case 3:
679  if (h->quincunx_subsampling)
681  else
682  stereo->type = AV_STEREO3D_SIDEBYSIDE;
683  break;
684  case 4:
685  stereo->type = AV_STEREO3D_TOPBOTTOM;
686  break;
687  case 5:
689  break;
690  case 6:
691  stereo->type = AV_STEREO3D_2D;
692  break;
693  }
694 
695  if (h->content_interpretation_type == 2)
696  stereo->flags = AV_STEREO3D_FLAG_INVERT;
697  }
698  }
699 
702  double angle = h->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
703  AVFrameSideData *rotation = av_frame_new_side_data(cur->f,
705  sizeof(int32_t) * 9);
706  if (rotation) {
707  av_display_rotation_set((int32_t *)rotation->data, angle);
708  av_display_matrix_flip((int32_t *)rotation->data,
709  h->sei_hflip, h->sei_vflip);
710  }
711  }
712 
715  sizeof(uint8_t));
716 
717  if (sd) {
720  }
721  }
722 
723  if (h->a53_caption) {
726  h->a53_caption_size);
727  if (sd)
728  memcpy(sd->data, h->a53_caption, h->a53_caption_size);
729  av_freep(&h->a53_caption);
730  h->a53_caption_size = 0;
732  }
733 
734  cur->mmco_reset = h->mmco_reset;
735  h->mmco_reset = 0;
736 
737  // FIXME do something with unavailable reference frames
738 
739  /* Sort B-frames into display order */
743  }
744  h->low_delay = !h->avctx->has_b_frames;
745 
746  for (i = 0; 1; i++) {
747  if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
748  if(i)
749  h->last_pocs[i-1] = cur->poc;
750  break;
751  } else if(i) {
752  h->last_pocs[i-1]= h->last_pocs[i];
753  }
754  }
755  out_of_order = MAX_DELAYED_PIC_COUNT - i;
756  if( cur->f->pict_type == AV_PICTURE_TYPE_B
758  out_of_order = FFMAX(out_of_order, 1);
759  if (out_of_order == MAX_DELAYED_PIC_COUNT) {
760  av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
761  for (i = 1; i < MAX_DELAYED_PIC_COUNT; i++)
762  h->last_pocs[i] = INT_MIN;
763  h->last_pocs[0] = cur->poc;
764  cur->mmco_reset = 1;
765  } else if(h->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
766  av_log(h->avctx, AV_LOG_INFO, "Increasing reorder buffer to %d\n", out_of_order);
767  h->avctx->has_b_frames = out_of_order;
768  h->low_delay = 0;
769  }
770 
771  pics = 0;
772  while (h->delayed_pic[pics])
773  pics++;
774 
776 
777  h->delayed_pic[pics++] = cur;
778  if (cur->reference == 0)
779  cur->reference = DELAYED_PIC_REF;
780 
781  out = h->delayed_pic[0];
782  out_idx = 0;
783  for (i = 1; h->delayed_pic[i] &&
784  !h->delayed_pic[i]->f->key_frame &&
785  !h->delayed_pic[i]->mmco_reset;
786  i++)
787  if (h->delayed_pic[i]->poc < out->poc) {
788  out = h->delayed_pic[i];
789  out_idx = i;
790  }
791  if (h->avctx->has_b_frames == 0 &&
792  (h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset))
793  h->next_outputed_poc = INT_MIN;
794  out_of_order = out->poc < h->next_outputed_poc;
795 
796  if (out_of_order || pics > h->avctx->has_b_frames) {
797  out->reference &= ~DELAYED_PIC_REF;
798  // for frame threading, the owner must be the second field's thread or
799  // else the first thread can release the picture and reuse it unsafely
800  for (i = out_idx; h->delayed_pic[i]; i++)
801  h->delayed_pic[i] = h->delayed_pic[i + 1];
802  }
803  if (!out_of_order && pics > h->avctx->has_b_frames) {
804  h->next_output_pic = out;
805  if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset)) {
806  h->next_outputed_poc = INT_MIN;
807  } else
808  h->next_outputed_poc = out->poc;
809  } else {
810  av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
811  }
812 
813  if (h->next_output_pic) {
814  if (h->next_output_pic->recovered) {
815  // We have reached an recovery point and all frames after it in
816  // display order are "recovered".
818  }
820  }
821 
822  if (setup_finished && !h->avctx->hwaccel) {
824 
826  h->setup_finished = 1;
827  }
828 }
829 
830 /**
831  * instantaneous decoder refresh.
832  */
833 static void idr(H264Context *h)
834 {
835  int i;
837  h->prev_frame_num =
838  h->prev_frame_num_offset = 0;
839  h->prev_poc_msb = 1<<16;
840  h->prev_poc_lsb = 0;
841  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
842  h->last_pocs[i] = INT_MIN;
843 }
844 
845 /* forget old pics after a seek */
847 {
848  int i, j;
849 
850  h->next_outputed_poc = INT_MIN;
851  h->prev_interlaced_frame = 1;
852  idr(h);
853 
854  h->prev_frame_num = -1;
855  if (h->cur_pic_ptr) {
856  h->cur_pic_ptr->reference = 0;
857  for (j=i=0; h->delayed_pic[i]; i++)
858  if (h->delayed_pic[i] != h->cur_pic_ptr)
859  h->delayed_pic[j++] = h->delayed_pic[i];
860  h->delayed_pic[j] = NULL;
861  }
863 
864  h->first_field = 0;
866  h->recovery_frame = -1;
867  h->frame_recovered = 0;
868  h->current_slice = 0;
869  h->mmco_reset = 1;
870  for (i = 0; i < h->nb_slice_ctx; i++)
871  h->slice_ctx[i].list_count = 0;
872 }
873 
874 /* forget old pics after a seek */
875 static void flush_dpb(AVCodecContext *avctx)
876 {
877  H264Context *h = avctx->priv_data;
878  int i;
879 
880  memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
881 
883 
884  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
885  ff_h264_unref_picture(h, &h->DPB[i]);
886  h->cur_pic_ptr = NULL;
888 
889  h->mb_y = 0;
890 
892  h->context_initialized = 0;
893 }
894 
895 int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
896 {
897  const int max_frame_num = 1 << h->sps.log2_max_frame_num;
898  int field_poc[2];
899 
901  if (h->frame_num < h->prev_frame_num)
902  h->frame_num_offset += max_frame_num;
903 
904  if (h->sps.poc_type == 0) {
905  const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
906 
907  if (h->poc_lsb < h->prev_poc_lsb &&
908  h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
909  h->poc_msb = h->prev_poc_msb + max_poc_lsb;
910  else if (h->poc_lsb > h->prev_poc_lsb &&
911  h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
912  h->poc_msb = h->prev_poc_msb - max_poc_lsb;
913  else
914  h->poc_msb = h->prev_poc_msb;
915  field_poc[0] =
916  field_poc[1] = h->poc_msb + h->poc_lsb;
917  if (h->picture_structure == PICT_FRAME)
918  field_poc[1] += h->delta_poc_bottom;
919  } else if (h->sps.poc_type == 1) {
920  int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
921  int i;
922 
923  if (h->sps.poc_cycle_length != 0)
924  abs_frame_num = h->frame_num_offset + h->frame_num;
925  else
926  abs_frame_num = 0;
927 
928  if (h->nal_ref_idc == 0 && abs_frame_num > 0)
929  abs_frame_num--;
930 
931  expected_delta_per_poc_cycle = 0;
932  for (i = 0; i < h->sps.poc_cycle_length; i++)
933  // FIXME integrate during sps parse
934  expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
935 
936  if (abs_frame_num > 0) {
937  int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
938  int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
939 
940  expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
941  for (i = 0; i <= frame_num_in_poc_cycle; i++)
942  expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
943  } else
944  expectedpoc = 0;
945 
946  if (h->nal_ref_idc == 0)
947  expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
948 
949  field_poc[0] = expectedpoc + h->delta_poc[0];
950  field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
951 
952  if (h->picture_structure == PICT_FRAME)
953  field_poc[1] += h->delta_poc[1];
954  } else {
955  int poc = 2 * (h->frame_num_offset + h->frame_num);
956 
957  if (!h->nal_ref_idc)
958  poc--;
959 
960  field_poc[0] = poc;
961  field_poc[1] = poc;
962  }
963 
965  pic_field_poc[0] = field_poc[0];
967  pic_field_poc[1] = field_poc[1];
968  *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
969 
970  return 0;
971 }
972 
973 /**
974  * Compute profile from profile_idc and constraint_set?_flags.
975  *
976  * @param sps SPS
977  *
978  * @return profile as defined by FF_PROFILE_H264_*
979  */
981 {
982  int profile = sps->profile_idc;
983 
984  switch (sps->profile_idc) {
986  // constraint_set1_flag set to 1
987  profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
988  break;
992  // constraint_set3_flag set to 1
993  profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
994  break;
995  }
996 
997  return profile;
998 }
999 
1000 
1001 #if FF_API_CAP_VDPAU
1002 static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
1003 #endif
1004 
1006 {
1007  int nals_needed = 0;
1008  int first_slice = 0;
1009  int i;
1010  int ret;
1011 
1012  for (i = 0; i < h->pkt.nb_nals; i++) {
1013  H2645NAL *nal = &h->pkt.nals[i];
1014  GetBitContext gb;
1015 
1016  /* packets can sometimes contain multiple PPS/SPS,
1017  * e.g. two PAFF field pictures in one packet, or a demuxer
1018  * which splits NALs strangely if so, when frame threading we
1019  * can't start the next thread until we've read all of them */
1020  switch (nal->type) {
1021  case NAL_SPS:
1022  case NAL_PPS:
1023  nals_needed = i;
1024  break;
1025  case NAL_DPA:
1026  case NAL_IDR_SLICE:
1027  case NAL_SLICE:
1028  ret = init_get_bits8(&gb, nal->data + 1, (nal->size - 1));
1029  if (ret < 0)
1030  return ret;
1031  if (!get_ue_golomb_long(&gb) || // first_mb_in_slice
1032  !first_slice ||
1033  first_slice != nal->type)
1034  nals_needed = i;
1035  if (!first_slice)
1036  first_slice = nal->type;
1037  }
1038  }
1039 
1040  return nals_needed;
1041 }
1042 
1043 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
1044  int parse_extradata)
1045 {
1046  AVCodecContext *const avctx = h->avctx;
1047  unsigned context_count = 0;
1048  int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
1049  int idr_cleared=0;
1050  int i, ret = 0;
1051 
1052  h->nal_unit_type= 0;
1053 
1054  if(!h->slice_context_count)
1055  h->slice_context_count= 1;
1057  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
1058  h->current_slice = 0;
1059  if (!h->first_field)
1060  h->cur_pic_ptr = NULL;
1061  ff_h264_reset_sei(h);
1062  }
1063 
1064  if (h->nal_length_size == 4) {
1065  if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
1066  h->is_avc = 0;
1067  }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
1068  h->is_avc = 1;
1069  }
1070 
1071  ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->is_avc,
1072  h->nal_length_size, avctx->codec_id);
1073  if (ret < 0) {
1074  av_log(avctx, AV_LOG_ERROR,
1075  "Error splitting the input into NAL units.\n");
1076  /* don't consider NAL parsing failure a fatal error when parsing extradata, as the stream may work without it */
1077  return parse_extradata ? buf_size : ret;
1078  }
1079 
1080  if (avctx->active_thread_type & FF_THREAD_FRAME)
1081  nals_needed = get_last_needed_nal(h);
1082  if (nals_needed < 0)
1083  return nals_needed;
1084 
1085  for (i = 0; i < h->pkt.nb_nals; i++) {
1086  H2645NAL *nal = &h->pkt.nals[i];
1087  H264SliceContext *sl = &h->slice_ctx[context_count];
1088  int err;
1089 
1090  if (avctx->skip_frame >= AVDISCARD_NONREF &&
1091  nal->ref_idc == 0 && nal->type != NAL_SEI)
1092  continue;
1093 
1094 again:
1095  /* Ignore per frame NAL unit type during extradata
1096  * parsing. Decoding slices is not possible in codec init
1097  * with frame-mt */
1098  if (parse_extradata) {
1099  switch (nal->type) {
1100  case NAL_IDR_SLICE:
1101  case NAL_SLICE:
1102  case NAL_DPA:
1103  case NAL_DPB:
1104  case NAL_DPC:
1106  "Ignoring NAL %d in global header/extradata\n",
1107  nal->type);
1108  // fall through to next case
1109  case NAL_AUXILIARY_SLICE:
1110  nal->type = NAL_FF_IGNORE;
1111  }
1112  }
1113 
1114  // FIXME these should stop being context-global variables
1115  h->nal_ref_idc = nal->ref_idc;
1116  h->nal_unit_type = nal->type;
1117 
1118  err = 0;
1119  switch (nal->type) {
1120  case NAL_IDR_SLICE:
1121  if ((nal->data[1] & 0xFC) == 0x98) {
1122  av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
1123  h->next_outputed_poc = INT_MIN;
1124  ret = -1;
1125  goto end;
1126  }
1127  if (nal->type != NAL_IDR_SLICE) {
1129  "Invalid mix of idr and non-idr slices\n");
1130  ret = -1;
1131  goto end;
1132  }
1133  if(!idr_cleared) {
1134  if (h->current_slice && (avctx->active_thread_type & FF_THREAD_SLICE)) {
1135  av_log(h, AV_LOG_ERROR, "invalid mixed IDR / non IDR frames cannot be decoded in slice multithreading mode\n");
1136  ret = AVERROR_INVALIDDATA;
1137  goto end;
1138  }
1139  idr(h); // FIXME ensure we don't lose some frames if there is reordering
1140  }
1141  idr_cleared = 1;
1142  h->has_recovery_point = 1;
1143  case NAL_SLICE:
1144  sl->gb = nal->gb;
1145  if ( nals_needed >= i
1146  || (!(avctx->active_thread_type & FF_THREAD_FRAME) && !context_count))
1147  h->au_pps_id = -1;
1148 
1149  if ((err = ff_h264_decode_slice_header(h, sl)))
1150  break;
1151 
1152  if (h->sei_recovery_frame_cnt >= 0) {
1154  h->valid_recovery_point = 1;
1155 
1156  if ( h->recovery_frame < 0
1157  || av_mod_uintp2(h->recovery_frame - h->frame_num, h->sps.log2_max_frame_num) > h->sei_recovery_frame_cnt) {
1158  h->recovery_frame = av_mod_uintp2(h->frame_num + h->sei_recovery_frame_cnt, h->sps.log2_max_frame_num);
1159 
1160  if (!h->valid_recovery_point)
1161  h->recovery_frame = h->frame_num;
1162  }
1163  }
1164 
1165  h->cur_pic_ptr->f->key_frame |= (nal->type == NAL_IDR_SLICE);
1166 
1167  if (nal->type == NAL_IDR_SLICE ||
1168  (h->recovery_frame == h->frame_num && nal->ref_idc)) {
1169  h->recovery_frame = -1;
1170  h->cur_pic_ptr->recovered = 1;
1171  }
1172  // If we have an IDR, all frames after it in decoded order are
1173  // "recovered".
1174  if (nal->type == NAL_IDR_SLICE)
1176 #if 1
1178 #else
1180 #endif
1181 
1182  if (h->current_slice == 1) {
1183  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS))
1184  decode_postinit(h, i >= nals_needed);
1185 
1186  if (h->avctx->hwaccel &&
1187  (ret = h->avctx->hwaccel->start_frame(h->avctx, buf, buf_size)) < 0)
1188  goto end;
1189 #if FF_API_CAP_VDPAU
1190  if (CONFIG_H264_VDPAU_DECODER &&
1193 #endif
1194  }
1195 
1196  if (sl->redundant_pic_count == 0) {
1197  if (avctx->hwaccel) {
1198  ret = avctx->hwaccel->decode_slice(avctx,
1199  nal->raw_data,
1200  nal->raw_size);
1201  if (ret < 0)
1202  goto end;
1203 #if FF_API_CAP_VDPAU
1204  } else if (CONFIG_H264_VDPAU_DECODER &&
1207  start_code,
1208  sizeof(start_code));
1210  nal->raw_data,
1211  nal->raw_size);
1212 #endif
1213  } else
1214  context_count++;
1215  }
1216  break;
1217  case NAL_DPA:
1218  case NAL_DPB:
1219  case NAL_DPC:
1220  avpriv_request_sample(avctx, "data partitioning");
1221  break;
1222  case NAL_SEI:
1223  h->gb = nal->gb;
1224  ret = ff_h264_decode_sei(h);
1225  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1226  goto end;
1227  break;
1228  case NAL_SPS:
1229  h->gb = nal->gb;
1230  if (ff_h264_decode_seq_parameter_set(h, 0) >= 0)
1231  break;
1233  "SPS decoding failure, trying again with the complete NAL\n");
1234  init_get_bits8(&h->gb, nal->raw_data + 1, nal->raw_size - 1);
1235  if (ff_h264_decode_seq_parameter_set(h, 0) >= 0)
1236  break;
1237  h->gb = nal->gb;
1239 
1240  break;
1241  case NAL_PPS:
1242  h->gb = nal->gb;
1244  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1245  goto end;
1246  break;
1247  case NAL_AUD:
1248  case NAL_END_SEQUENCE:
1249  case NAL_END_STREAM:
1250  case NAL_FILLER_DATA:
1251  case NAL_SPS_EXT:
1252  case NAL_AUXILIARY_SLICE:
1253  break;
1254  case NAL_FF_IGNORE:
1255  break;
1256  default:
1257  av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
1258  nal->type, nal->size_bits);
1259  }
1260 
1261  if (context_count == h->max_contexts) {
1262  ret = ff_h264_execute_decode_slices(h, context_count);
1263  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1264  goto end;
1265  context_count = 0;
1266  }
1267 
1268  if (err < 0 || err == SLICE_SKIPED) {
1269  if (err < 0)
1270  av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
1271  sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
1272  } else if (err == SLICE_SINGLETHREAD) {
1273  if (context_count > 1) {
1274  ret = ff_h264_execute_decode_slices(h, context_count - 1);
1275  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1276  goto end;
1277  context_count = 0;
1278  }
1279  /* Slice could not be decoded in parallel mode, restart. Note
1280  * that rbsp_buffer is not transferred, but since we no longer
1281  * run in parallel mode this should not be an issue. */
1282  sl = &h->slice_ctx[0];
1283  goto again;
1284  }
1285  }
1286  if (context_count) {
1287  ret = ff_h264_execute_decode_slices(h, context_count);
1288  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1289  goto end;
1290  }
1291 
1292  ret = 0;
1293 end:
1294 
1295 #if CONFIG_ERROR_RESILIENCE
1296  /*
1297  * FIXME: Error handling code does not seem to support interlaced
1298  * when slices span multiple rows
1299  * The ff_er_add_slice calls don't work right for bottom
1300  * fields; they cause massive erroneous error concealing
1301  * Error marking covers both fields (top and bottom).
1302  * This causes a mismatched s->error_count
1303  * and a bad error table. Further, the error count goes to
1304  * INT_MAX when called for bottom field, because mb_y is
1305  * past end by one (callers fault) and resync_mb_y != 0
1306  * causes problems for the first MB line, too.
1307  */
1308  if (!FIELD_PICTURE(h) && h->current_slice && !h->sps.new && h->enable_er) {
1309  H264SliceContext *sl = h->slice_ctx;
1310  int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
1311 
1313 
1314  if (use_last_pic) {
1316  sl->ref_list[0][0].parent = &h->last_pic_for_ec;
1317  memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
1318  memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
1320  } else if (sl->ref_count[0]) {
1321  ff_h264_set_erpic(&sl->er.last_pic, sl->ref_list[0][0].parent);
1322  } else
1324 
1325  if (sl->ref_count[1])
1326  ff_h264_set_erpic(&sl->er.next_pic, sl->ref_list[1][0].parent);
1327 
1328  sl->er.ref_count = sl->ref_count[0];
1329 
1330  ff_er_frame_end(&sl->er);
1331  if (use_last_pic)
1332  memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
1333  }
1334 #endif /* CONFIG_ERROR_RESILIENCE */
1335  /* clean up */
1336  if (h->cur_pic_ptr && !h->droppable) {
1339  }
1340 
1341  return (ret < 0) ? ret : buf_size;
1342 }
1343 
1344 /**
1345  * Return the number of bytes consumed for building the current frame.
1346  */
1347 static int get_consumed_bytes(int pos, int buf_size)
1348 {
1349  if (pos == 0)
1350  pos = 1; // avoid infinite loops (I doubt that is needed but...)
1351  if (pos + 10 > buf_size)
1352  pos = buf_size; // oops ;)
1353 
1354  return pos;
1355 }
1356 
1358 {
1359  AVFrame *src = srcp->f;
1361  int i;
1362  int ret = av_frame_ref(dst, src);
1363  if (ret < 0)
1364  return ret;
1365 
1366  av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(h), 0);
1367 
1368  h->backup_width = h->avctx->width;
1369  h->backup_height = h->avctx->height;
1370  h->backup_pix_fmt = h->avctx->pix_fmt;
1371 
1372  h->avctx->width = dst->width;
1373  h->avctx->height = dst->height;
1374  h->avctx->pix_fmt = dst->format;
1375 
1376  if (srcp->sei_recovery_frame_cnt == 0)
1377  dst->key_frame = 1;
1378  if (!srcp->crop)
1379  return 0;
1380 
1381  for (i = 0; i < desc->nb_components; i++) {
1382  int hshift = (i > 0) ? desc->log2_chroma_w : 0;
1383  int vshift = (i > 0) ? desc->log2_chroma_h : 0;
1384  int off = ((srcp->crop_left >> hshift) << h->pixel_shift) +
1385  (srcp->crop_top >> vshift) * dst->linesize[i];
1386  dst->data[i] += off;
1387  }
1388  return 0;
1389 }
1390 
1391 static int is_extra(const uint8_t *buf, int buf_size)
1392 {
1393  int cnt= buf[5]&0x1f;
1394  const uint8_t *p= buf+6;
1395  while(cnt--){
1396  int nalsize= AV_RB16(p) + 2;
1397  if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
1398  return 0;
1399  p += nalsize;
1400  }
1401  cnt = *(p++);
1402  if(!cnt)
1403  return 0;
1404  while(cnt--){
1405  int nalsize= AV_RB16(p) + 2;
1406  if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
1407  return 0;
1408  p += nalsize;
1409  }
1410  return 1;
1411 }
1412 
1413 static int h264_decode_frame(AVCodecContext *avctx, void *data,
1414  int *got_frame, AVPacket *avpkt)
1415 {
1416  const uint8_t *buf = avpkt->data;
1417  int buf_size = avpkt->size;
1418  H264Context *h = avctx->priv_data;
1419  AVFrame *pict = data;
1420  int buf_index = 0;
1421  H264Picture *out;
1422  int i, out_idx;
1423  int ret;
1424 
1425  h->flags = avctx->flags;
1426  h->setup_finished = 0;
1427 
1428  if (h->backup_width != -1) {
1429  avctx->width = h->backup_width;
1430  h->backup_width = -1;
1431  }
1432  if (h->backup_height != -1) {
1433  avctx->height = h->backup_height;
1434  h->backup_height = -1;
1435  }
1436  if (h->backup_pix_fmt != AV_PIX_FMT_NONE) {
1437  avctx->pix_fmt = h->backup_pix_fmt;
1439  }
1440 
1442 
1443  /* end of stream, output what is still in the buffers */
1444  if (buf_size == 0) {
1445  out:
1446 
1447  h->cur_pic_ptr = NULL;
1448  h->first_field = 0;
1449 
1450  // FIXME factorize this with the output code below
1451  out = h->delayed_pic[0];
1452  out_idx = 0;
1453  for (i = 1;
1454  h->delayed_pic[i] &&
1455  !h->delayed_pic[i]->f->key_frame &&
1456  !h->delayed_pic[i]->mmco_reset;
1457  i++)
1458  if (h->delayed_pic[i]->poc < out->poc) {
1459  out = h->delayed_pic[i];
1460  out_idx = i;
1461  }
1462 
1463  for (i = out_idx; h->delayed_pic[i]; i++)
1464  h->delayed_pic[i] = h->delayed_pic[i + 1];
1465 
1466  if (out) {
1467  out->reference &= ~DELAYED_PIC_REF;
1468  ret = output_frame(h, pict, out);
1469  if (ret < 0)
1470  return ret;
1471  *got_frame = 1;
1472  }
1473 
1474  return buf_index;
1475  }
1477  int side_size;
1478  uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
1479  if (is_extra(side, side_size))
1480  ff_h264_decode_extradata(h, side, side_size);
1481  }
1482  if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
1483  if (is_extra(buf, buf_size))
1484  return ff_h264_decode_extradata(h, buf, buf_size);
1485  }
1486 
1487  buf_index = decode_nal_units(h, buf, buf_size, 0);
1488  if (buf_index < 0)
1489  return AVERROR_INVALIDDATA;
1490 
1491  if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
1492  av_assert0(buf_index <= buf_size);
1493  goto out;
1494  }
1495 
1496  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
1497  if (avctx->skip_frame >= AVDISCARD_NONREF ||
1498  buf_size >= 4 && !memcmp("Q264", buf, 4))
1499  return buf_size;
1500  av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1501  return AVERROR_INVALIDDATA;
1502  }
1503 
1504  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
1505  (h->mb_y >= h->mb_height && h->mb_height)) {
1506  if (avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)
1507  decode_postinit(h, 1);
1508 
1509  if ((ret = ff_h264_field_end(h, &h->slice_ctx[0], 0)) < 0)
1510  return ret;
1511 
1512  /* Wait for second field. */
1513  *got_frame = 0;
1514  if (h->next_output_pic && ((avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||
1515  (avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL) ||
1516  h->next_output_pic->recovered)) {
1517  if (!h->next_output_pic->recovered)
1519 
1520  if (!h->avctx->hwaccel &&
1521  (h->next_output_pic->field_poc[0] == INT_MAX ||
1522  h->next_output_pic->field_poc[1] == INT_MAX)
1523  ) {
1524  int p;
1525  AVFrame *f = h->next_output_pic->f;
1526  int field = h->next_output_pic->field_poc[0] == INT_MAX;
1527  uint8_t *dst_data[4];
1528  int linesizes[4];
1529  const uint8_t *src_data[4];
1530 
1531  av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
1532 
1533  for (p = 0; p<4; p++) {
1534  dst_data[p] = f->data[p] + (field^1)*f->linesize[p];
1535  src_data[p] = f->data[p] + field *f->linesize[p];
1536  linesizes[p] = 2*f->linesize[p];
1537  }
1538 
1539  av_image_copy(dst_data, linesizes, src_data, linesizes,
1540  f->format, f->width, f->height>>1);
1541  }
1542 
1543  ret = output_frame(h, pict, h->next_output_pic);
1544  if (ret < 0)
1545  return ret;
1546  *got_frame = 1;
1547  if (CONFIG_MPEGVIDEO) {
1548  ff_print_debug_info2(h->avctx, pict, NULL,
1552  &h->low_delay,
1553  h->mb_width, h->mb_height, h->mb_stride, 1);
1554  }
1555  }
1556  }
1557 
1558  av_assert0(pict->buf[0] || !*got_frame);
1559 
1561 
1562  return get_consumed_bytes(buf_index, buf_size);
1563 }
1564 
1566 {
1567  int i;
1568 
1570 
1571  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
1572  ff_h264_unref_picture(h, &h->DPB[i]);
1573  av_frame_free(&h->DPB[i].f);
1574  }
1575  memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
1576 
1577  h->cur_pic_ptr = NULL;
1578 
1579  for (i = 0; i < h->nb_slice_ctx; i++)
1580  av_freep(&h->slice_ctx[i].rbsp_buffer);
1581  av_freep(&h->slice_ctx);
1582  h->nb_slice_ctx = 0;
1583 
1584  h->a53_caption_size = 0;
1585  av_freep(&h->a53_caption);
1586 
1587  for (i = 0; i < MAX_SPS_COUNT; i++)
1588  av_freep(h->sps_buffers + i);
1589 
1590  for (i = 0; i < MAX_PPS_COUNT; i++)
1591  av_freep(h->pps_buffers + i);
1592 
1594 }
1595 
1597 {
1598  H264Context *h = avctx->priv_data;
1599 
1602 
1604  av_frame_free(&h->cur_pic.f);
1607 
1608  return 0;
1609 }
1610 
1611 #define OFFSET(x) offsetof(H264Context, x)
1612 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1613 static const AVOption h264_options[] = {
1614  {"is_avc", "is avc", offsetof(H264Context, is_avc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
1615  {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
1616  { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
1617  { NULL },
1618 };
1619 
1620 static const AVClass h264_class = {
1621  .class_name = "H264 Decoder",
1622  .item_name = av_default_item_name,
1623  .option = h264_options,
1624  .version = LIBAVUTIL_VERSION_INT,
1625 };
1626 
1628  .name = "h264",
1629  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1630  .type = AVMEDIA_TYPE_VIDEO,
1631  .id = AV_CODEC_ID_H264,
1632  .priv_data_size = sizeof(H264Context),
1634  .close = h264_decode_end,
1636  .capabilities = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
1639  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1640  .flush = flush_dpb,
1641  .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1642  .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
1644  .priv_class = &h264_class,
1645 };
1646 
1647 #if CONFIG_H264_VDPAU_DECODER && FF_API_VDPAU
1648 static const AVClass h264_vdpau_class = {
1649  .class_name = "H264 VDPAU Decoder",
1650  .item_name = av_default_item_name,
1651  .option = h264_options,
1652  .version = LIBAVUTIL_VERSION_INT,
1653 };
1654 
1655 AVCodec ff_h264_vdpau_decoder = {
1656  .name = "h264_vdpau",
1657  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
1658  .type = AVMEDIA_TYPE_VIDEO,
1659  .id = AV_CODEC_ID_H264,
1660  .priv_data_size = sizeof(H264Context),
1662  .close = h264_decode_end,
1665  .flush = flush_dpb,
1666  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
1667  AV_PIX_FMT_NONE},
1669  .priv_class = &h264_vdpau_class,
1670 };
1671 #endif
struct H264Context * h264
Definition: h264.h:370
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:47
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1651
void ff_h264_flush_change(H264Context *h)
Definition: h264.c:846
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:3718
int workaround_bugs
Definition: h264.h:553
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size, int parse_extradata)
Definition: h264.c:1043
GetBitContext gb
Definition: h264.h:521
int sei_recovery_frame_cnt
Definition: h264.h:351
#define AV_NUM_DATA_POINTERS
Definition: frame.h:181
#define SLICE_FLAG_ALLOW_FIELD
allow draw_horiz_band() with field slices (MPEG2 field pics)
Definition: avcodec.h:2186
enum AVPixelFormat backup_pix_fmt
Definition: h264.h:545
int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
Call decode_slice() for each context.
Definition: h264_slice.c:2500
5: top field, bottom field, top field repeated, in that order
Definition: h264.h:155
int low_delay
Definition: h264.h:549
int mb_num
Definition: h264.h:620
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2193
mpeg2/4 4:2:0, h264 default for 4:2:0
Definition: pixfmt.h:473
This structure describes decoded (raw) audio or video data.
Definition: frame.h:180
#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
Definition: internal.h:157
static int get_last_needed_nal(H264Context *h)
Definition: h264.c:1005
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
Definition: h264.h:483
AVOption.
Definition: opt.h:245
static const AVClass h264_class
Definition: h264.c:1620
int delta_poc[2]
Definition: h264.h:648
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
int size
Definition: h2645_parse.h:33
int quincunx_subsampling
Definition: h264.h:732
int edge_emu_buffer_allocated
Definition: h264.h:471
int a53_caption_size
Definition: h264.h:746
3: top field, bottom field, in that order
Definition: h264.h:153
#define H264_MAX_PICTURE_COUNT
Definition: h264.h:49
static int decode_extradata_ps_mp4(H264Context *h, const uint8_t *buf, int buf_size)
Definition: h264.c:313
int first_field
Definition: h264.h:590
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:390
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:359
const char * desc
Definition: nvenc.c:237
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint16_t * cbp_table
Definition: h264.h:595
av_cold int ff_h264_decode_init(AVCodecContext *avctx)
Definition: h264.c:480
7: frame doubling
Definition: h264.h:157
void ff_er_frame_end(ERContext *s)
#define MAX_PPS_COUNT
Definition: h264.h:53
Sequence parameter set.
Definition: h264.h:177
int mb_y
Definition: h264.h:617
int bitstream_restriction_flag
Definition: h264.h:217
#define FF_PROFILE_H264_INTRA
Definition: avcodec.h:3176
#define FMO
Definition: h264.h:65
int num
numerator
Definition: rational.h:44
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:309
int bipred_scratchpad_allocated
Definition: h264.h:470
int size
Definition: avcodec.h:1578
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: diracdec.c:64
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:143
AVBufferPool * mb_type_pool
Definition: h264.h:828
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int is_nalff, int nal_length_size, enum AVCodecID codec_id)
Split an input packet into NAL units.
Definition: h2645_parse.c:248
int crop
Definition: h264.h:353
void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl, int y, int height)
Definition: h264.c:102
int16_t(*[2] motion_val)[2]
Definition: h264.h:324
int flags
Definition: h264.h:552
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1870
int mb_height
Definition: h264.h:618
H264Picture * delayed_pic[MAX_DELAYED_PIC_COUNT+2]
Definition: h264.h:669
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
int is_avc
Used to parse AVC variant of h264.
Definition: h264.h:631
AVBufferPool * ref_index_pool
Definition: h264.h:830
void ff_h264_free_tables(H264Context *h)
Definition: h264.c:137
ERPicture last_pic
int ff_h264_get_profile(SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
Definition: h264.c:980
int sei_reguserdata_afd_present
User data registered by Rec.
Definition: h264.h:744
void ff_h264_decode_init_vlc(void)
Definition: h264_cavlc.c:327
H264Context.
Definition: h264.h:514
AVFrame * f
Definition: h264.h:317
#define AV_CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries...
Definition: avcodec.h:914
int prev_poc_msb
poc_msb of the last reference pic for POC type 0
Definition: h264.h:650
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
4: bottom field, top field, in that order
Definition: h264.h:154
#define FRAME_RECOVERED_IDR
We have seen an IDR, so all the following frames in coded order are correctly decodable.
Definition: h264.h:794
Views are next to each other.
Definition: stereo3d.h:45
AVCodec.
Definition: avcodec.h:3535
int picture_structure
Definition: h264.h:589
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
int profile_idc
Definition: h264.h:179
unsigned current_sps_id
id of the current SPS
Definition: h264.h:574
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:457
int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
Definition: h264.c:60
int size_bits
Size, in bits, of just the data, excluding the stop bit and any trailing padding. ...
Definition: h2645_parse.h:40
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:3188
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1779
uint8_t * chroma_pred_mode_table
Definition: h264.h:598
int setup_finished
Definition: h264.h:811
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:3257
BYTE int const BYTE * srcp
Definition: avisynth_c.h:676
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2961
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:978
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:3178
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint32_t(*[6] dequant4_coeff)[16]
Definition: h264.h:582
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t * a53_caption
Definition: h264.h:747
uint8_t
#define av_cold
Definition: attributes.h:82
int prev_frame_num_offset
for POC type 2
Definition: h264.h:653
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
int offset_for_non_ref_pic
Definition: h264.h:187
AVOptions.
void ff_h264_reset_sei(H264Context *h)
Reset SEI values at the beginning of the frame.
Definition: h264_sei.c:37
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:123
static int h264_init_context(AVCodecContext *avctx, H264Context *h)
Definition: h264.c:420
int poc
frame POC
Definition: h264.h:336
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVCodec ff_h264_decoder
Definition: h264.c:1627
Multithreading support functions.
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:374
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1757
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
uint8_t(*[2] top_borders)[(16 *3)*2]
Definition: h264.h:469
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:367
ERPicture cur_pic
int frame_recovered
Initial frame has been completely recovered.
Definition: h264.h:801
Structure to hold side data for an AVFrame.
Definition: frame.h:143
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
uint8_t * data
Definition: avcodec.h:1577
#define AV_CODEC_CAP_HWACCEL_VDPAU
Codec can export data for HW decoding (VDPAU).
Definition: avcodec.h:989
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
AVDictionary * metadata
metadata.
Definition: frame.h:467
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:314
#define MAX_DELAYED_PIC_COUNT
Definition: h264.h:57
static void fill_rectangle(SDL_Surface *screen, int x, int y, int w, int h, int color, int update)
Definition: ffplay.c:805
ptrdiff_t size
Definition: opengl_enc.c:101
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...
H264Picture * parent
Definition: h264.h:366
high precision timer, useful to profile code
Views are alternated temporally.
Definition: stereo3d.h:66
int recovered
picture at IDR or recovery point + recovery count
Definition: h264.h:349
Active Format Description data consisting of a single byte as specified in ETSI TS 101 154 using AVAc...
Definition: frame.h:88
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define AVOnce
Definition: thread.h:158
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2385
#define av_log(a,...)
Definition: h264.h:122
int sei_vflip
Definition: h264.h:739
int last_pocs[MAX_DELAYED_PIC_COUNT]
Definition: h264.h:670
static int ref[MAX_W *MAX_W]
H.264 / AVC / MPEG4 part10 codec.
int frame_num
Definition: h264.h:649
void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl)
Definition: h264_mb.c:798
H264Picture DPB[H264_MAX_PICTURE_COUNT]
Definition: h264.h:523
int width
width and height of the video frame
Definition: frame.h:232
#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:1964
int flags
Additional information about the frame packing.
Definition: stereo3d.h:132
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
static int get_consumed_bytes(int pos, int buf_size)
Return the number of bytes consumed for building the current frame.
Definition: h264.c:1347
int16_t * dc_val_base
Definition: h264.h:465
int poc_type
pic_order_cnt_type
Definition: h264.h:184
int context_initialized
Definition: h264.h:551
ERContext er
Definition: h264.h:372
int nal_unit_type
Definition: h264.h:626
av_default_item_name
int num_reorder_frames
Definition: h264.h:218
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:111
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
int backup_height
Definition: h264.h:544
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:3091
int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
Definition: h264.c:895
int backup_width
Backup frame properties: needed, because they can be different between returned frame and last decode...
Definition: h264.h:543
H2645Packet pkt
Definition: h264.h:531
static void flush_dpb(AVCodecContext *avctx)
Definition: h264.c:875
int capabilities
Codec capabilities.
Definition: avcodec.h:3554
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:57
static const AVOption h264_options[]
Definition: h264.c:1613
void(* decode_mb)(void *opaque, int ref, int mv_dir, int mv_type, int(*mv)[2][4][2], int mb_x, int mb_y, int mb_intra, int mb_skipped)
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
uint8_t(*[2] mvd_table)[2]
Definition: h264.h:599
#define FF_PROFILE_H264_HIGH_422
Definition: avcodec.h:3185
int prev_interlaced_frame
Complement sei_pic_struct SEI_PIC_STRUCT_TOP_BOTTOM and SEI_PIC_STRUCT_BOTTOM_TOP indicate interlaced...
Definition: h264.h:724
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1737
ThreadFrame tf
Definition: h264.h:318
0: frame
Definition: h264.h:150
simple assert() macros that are a bit more flexible than ISO C assert().
#define PICT_TOP_FIELD
Definition: mpegutils.h:37
const char * name
Name of the codec implementation.
Definition: avcodec.h:3542
static AVOnce h264_vlc_init
Definition: h264.c:478
static av_always_inline uint32_t pack16to32(unsigned a, unsigned b)
Definition: h264.h:990
int valid_recovery_point
Are the SEI recovery points looking valid.
Definition: h264.h:778
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
uint8_t * list_counts
Array of list_count per MB specifying the slice type.
Definition: h264.h:592
#define FFMAX(a, b)
Definition: common.h:94
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:193
#define fail()
Definition: checkasm.h:81
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1016
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:302
int new
flag to keep track if the decoder context needs re-init due to changed SPS
Definition: h264.h:234
int * mb_index2xy
int raw_size
Definition: h2645_parse.h:42
int offset_for_top_to_bottom_field
Definition: h264.h:188
#define FIELD_OR_MBAFF_PICTURE(h)
Definition: h264.h:94
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition: h264.h:376
int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl)
Decode a slice header.
Definition: h264_slice.c:1110
static const uint8_t scan8[16 *3+3]
Definition: h264.h:974
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:215
int crop_left
Definition: h264.h:354
uint8_t * error_status_table
uint8_t * direct_table
Definition: h264.h:600
int nal_length_size
Number of bytes used for nal length (1, 2 or 4)
Definition: h264.h:632
useful rectangle filling function
uint8_t * data[3]
Definition: h264.h:359
void ff_vdpau_h264_picture_start(H264Context *h)
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type, int(*mv)[2][4][2], int mb_x, int mb_y, int mb_intra, int mb_skipped)
Definition: h264.c:66
Views are packed per line, as if interlaced.
Definition: stereo3d.h:97
int sei_anticlockwise_rotation
Definition: h264.h:738
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, 'draw_horiz_band' is called by the libavcodec decoder to draw a horizontal band...
Definition: avcodec.h:1903
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:254
int frame_num_offset
for POC type 2
Definition: h264.h:652
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:400
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2929
FPA sei_fpa
Definition: h264.h:780
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int x264_build
Definition: h264.h:615
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:3083
uint32_t * mb2br_xy
Definition: h264.h:570
uint8_t * er_temp_buffer
#define OFFSET(x)
Definition: h264.c:1611
#define FFMIN(a, b)
Definition: common.h:96
uint16_t * slice_table
slice_table_base + 2*mb_stride + 1
Definition: h264.h:585
#define H264_MAX_THREADS
Definition: h264.h:50
int poc_cycle_length
num_ref_frames_in_pic_order_cnt_cycle
Definition: h264.h:189
int reference
Definition: h264.h:348
#define FIELD_PICTURE(h)
Definition: h264.h:77
int sei_frame_packing_present
frame_packing_arrangment SEI message
Definition: h264.h:729
int width
picture width / height.
Definition: avcodec.h:1829
int redundant_pic_count
Definition: h264.h:434
int nb_slice_ctx
Definition: h264.h:529
uint32_t * mb_type
Definition: h264.h:327
#define AV_FRAME_FLAG_CORRUPT
The frame data may be corrupted, e.g.
Definition: frame.h:392
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
SPS sps
current sps
Definition: h264.h:575
int32_t
PPS * pps_buffers[MAX_PPS_COUNT]
Definition: h264.h:638
int sei_hflip
Definition: h264.h:739
#define MAX_SPS_COUNT
Definition: h264.h:52
Definition: h264.h:123
int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length)
Decode PPS.
Definition: h264_ps.c:587
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:3084
Context Adaptive Binary Arithmetic Coder inline functions.
int mmco_reset
Definition: h264.h:679
H264SliceContext * slice_ctx
Definition: h264.h:528
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2940
int poc_lsb
Definition: h264.h:645
int reference
Definition: h264.h:362
static int h264_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: h264.c:1413
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1788
int top_borders_allocated[2]
Definition: h264.h:472
int ref_idc
H264 only, nal_ref_idc.
Definition: h2645_parse.h:63
uint8_t active_format_description
Definition: h264.h:745
#define src
Definition: vp9dsp.c:530
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264.c:1357
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:114
int type
NAL unit type.
Definition: h2645_parse.h:50
void av_display_rotation_set(int32_t matrix[9], double angle)
Initialize a transformation matrix describing a pure counterclockwise rotation by the specified angle...
Definition: display.c:50
#define PART_NOT_AVAILABLE
Definition: h264.h:561
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3072
uint8_t * edge_emu_buffer
Definition: h264.h:468
Views are packed per column.
Definition: stereo3d.h:107
FILE * out
Definition: movenc-test.c:54
int dequant_coeff_pps
reinit tables when pps changes
Definition: h264.h:640
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:85
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1020
SPS * sps_buffers[MAX_SPS_COUNT]
Definition: h264.h:637
static const int8_t mv[256][2]
Definition: 4xm.c:77
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:244
short offset_for_ref_frame[256]
Definition: h264.h:216
Definition: h264.h:120
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:127
int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
Init context Allocate buffers which are not shared amongst multiple threads.
Definition: h264.c:241
int mb_stride
Definition: h264.h:619
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVCodecContext * avctx
Definition: h264.h:516
#define AV_ONCE_INIT
Definition: thread.h:159
Libavcodec external API header.
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: frame.h:83
1: top field
Definition: h264.h:151
enum AVCodecID codec_id
Definition: avcodec.h:1659
void ff_h264_remove_all_refs(H264Context *h)
Definition: h264_refs.c:511
int prev_frame_num
frame_num of the last pic for POC type 1/2
Definition: h264.h:654
ERPicture next_pic
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:211
int next_outputed_poc
Definition: h264.h:672
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:449
int ff_h264_decode_sei(H264Context *h)
Decode SEI.
Definition: h264_sei.c:421
int poc_msb
Definition: h264.h:646
Views are next to each other, but when upscaling apply a checkerboard pattern.
Definition: stereo3d.h:87
int field_poc[2]
top/bottom POC
Definition: h264.h:335
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int max_contexts
Max number of threads / contexts.
Definition: h264.h:699
int recovery_frame
recovery_frame is the frame_num at which the next frame should be fully constructed.
Definition: h264.h:788
main external API structure.
Definition: avcodec.h:1642
void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip)
Flip the input matrix horizontally and/or vertically.
Definition: display.c:65
Definition: h264.h:118
static void decode_postinit(H264Context *h, int setup_finished)
Run setup operations that must be run after slice header decoding.
Definition: h264.c:568
int ff_h264_alloc_tables(H264Context *h)
Allocate tables.
Definition: h264.c:180
2: bottom field
Definition: h264.h:152
const uint8_t * data
Definition: h2645_parse.h:34
uint8_t * data
Definition: frame.h:145
void * buf
Definition: avisynth_c.h:553
int frame_packing_arrangement_type
Definition: h264.h:730
void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table, uint32_t *mbtype_table, int8_t *qscale_table, int16_t(*motion_val[2])[2], int *low_delay, int mb_width, int mb_height, int mb_stride, int quarter_sample)
Print debugging info for the given picture.
Definition: mpegvideo.c:1580
int8_t * qscale_table
Definition: h264.h:321
int extradata_size
Definition: avcodec.h:1758
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
H.264 HW decoding with VDPAU, data[0] contains a vdpau_render_state struct which contains the bitstre...
Definition: pixfmt.h:104
int constraint_set_flags
constraint_set[0-3]_flag
Definition: h264.h:233
SEI_PicStructType sei_pic_struct
pic_struct in picture timing SEI message
Definition: h264.h:716
int ff_h264_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: h264_slice.c:393
int slice_flags
slice flags
Definition: avcodec.h:2184
Describe the class of an AVClass context structure.
Definition: log.h:67
#define FF_CODEC_PROPERTY_CLOSED_CAPTIONS
Definition: avcodec.h:3466
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:610
uint8_t non_zero_count_cache[15 *8]
non zero coeff count cache.
Definition: h264.h:478
static int h264_decode_end(AVCodecContext *avctx)
Definition: h264.c:1596
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:274
Definition: h264.h:116
int8_t * ref_index[2]
Definition: h264.h:333
int pixel_shift
0 for 8-bit H264, 1 for high-bit-depth H264
Definition: h264.h:533
int mmco_reset
MMCO_RESET set this 1.
Definition: h264.h:338
H264Picture * cur_pic_ptr
Definition: h264.h:524
int mb_mbaff
mb_aff_frame && mb_field_decoding_flag
Definition: h264.h:432
int enable_er
Definition: h264.h:825
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: avcodec.h:2861
int frame_packing_arrangement_cancel_flag
is previous arrangement canceled, -1 if never received
Definition: h264.h:271
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:126
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264.h:185
6: bottom field, top field, bottom field repeated, in that order
Definition: h264.h:156
static int is_extra(const uint8_t *buf, int buf_size)
Definition: h264.c:1391
AVCodecContext * avctx
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
static const uint8_t start_code[]
Definition: h264.c:1002
mfxU16 profile
Definition: qsvenc.c:42
int linesize[3]
Definition: h264.h:360
int pic_struct_present_flag
Definition: h264.h:224
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:32
unsigned int list_count
Definition: h264.h:458
av_cold void ff_h264_free_context(H264Context *h)
Free any data that may have been allocated in the H264 context like SPS, PPS etc. ...
Definition: h264.c:1565
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:194
int has_recovery_point
Definition: h264.h:803
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:722
static void idr(H264Context *h)
instantaneous decoder refresh.
Definition: h264.c:833
int ff_h264_decode_seq_parameter_set(H264Context *h, int ignore_truncation)
Decode SPS.
Definition: h264_ps.c:280
discard all non reference
Definition: avcodec.h:777
AVBufferPool * qscale_table_pool
Definition: h264.h:827
H264Picture * next_output_pic
Definition: h264.h:671
int slice_context_count
Definition: h264.h:701
AVBufferPool * motion_val_pool
Definition: h264.h:829
#define SLICE_SINGLETHREAD
Definition: h264.h:1187
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
Definition: h264.c:358
#define FRAME_RECOVERED_SEI
Sufficient number of frames have been decoded since a SEI recovery point, so all the following frames...
Definition: h264.h:799
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:35
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:130
uint16_t * slice_table_base
Definition: h264.h:642
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:183
int missing_fields
Definition: h264.h:805
Views are packed in a checkerboard-like structure per pixel.
Definition: stereo3d.h:76
int16_t * dc_val[3]
H.264 / AVC / MPEG4 part10 motion vector predicion.
Bi-dir predicted.
Definition: avutil.h:268
const char * ff_h264_sei_stereo_mode(H264Context *h)
Get stereo_mode string from the h264 frame_packing_arrangement.
Definition: h264_sei.c:491
int workaround_bugs
Work around bugs in encoders which sometimes cannot be detected automatically.
Definition: avcodec.h:2825
const AVProfile ff_h264_profiles[]
Definition: profiles.c:49
int cur_chroma_format_idc
Definition: h264.h:817
int8_t * intra4x4_pred_mode
Definition: h264.h:398
unsigned properties
Definition: avcodec.h:3464
int den
denominator
Definition: rational.h:45
uint8_t * rbsp_buffer
Definition: h264.h:507
int sei_ct_type
Bit set of clock types for fields/frames in picture timing SEI message.
Definition: h264.h:754
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
GetBitContext gb
Definition: h2645_parse.h:45
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:728
void * priv_data
Definition: avcodec.h:1684
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:154
const uint8_t * raw_data
Definition: h2645_parse.h:43
#define PICT_FRAME
Definition: mpegutils.h:39
int prev_poc_lsb
poc_lsb of the last reference pic for POC type 0
Definition: h264.h:651
void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
Definition: h264_picture.c:131
int8_t ref_cache[2][5 *8]
Definition: h264.h:484
#define AV_CODEC_FLAG_OUTPUT_CORRUPT
Output even those frames that might be corrupted.
Definition: avcodec.h:835
#define SLICE_SKIPED
Definition: h264.h:1188
#define VD
Definition: h264.c:1612
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:319
const uint16_t ff_h264_mb_sizes[4]
Definition: h264.c:58
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1692
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:161
H2645NAL * nals
Definition: h2645_parse.h:68
H264Picture cur_pic
Definition: h264.h:525
int sei_display_orientation_present
display orientation SEI message
Definition: h264.h:737
Views are on top of each other.
Definition: stereo3d.h:55
int content_interpretation_type
Definition: h264.h:731
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:249
int mb_width
Definition: h264.h:618
enum AVPictureType pict_type
Definition: h264.h:709
int current_slice
current slice number, used to initialize slice_num of each thread/context
Definition: h264.h:691
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1744
uint32_t * mb2b_xy
Definition: h264.h:569
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264.h:459
int delta_poc_bottom
Definition: h264.h:647
H264Picture last_pic_for_ec
Definition: h264.h:526
int au_pps_id
pps_id of current access unit
Definition: h264.h:578
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
int height
Definition: frame.h:232
int crop_top
Definition: h264.h:355
#define AV_CODEC_FLAG2_SHOW_ALL
Show all frames before the first keyframe.
Definition: avcodec.h:923
uint8_t(*[2] mvd_table)[2]
Definition: h264.h:497
#define av_freep(p)
Definition: h264.h:117
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:320
void ff_vdpau_add_data_chunk(uint8_t *data, const uint8_t *buf, int buf_size)
int8_t * intra4x4_pred_mode
Definition: h264.h:555
#define FF_PROFILE_H264_CONSTRAINED
Definition: avcodec.h:3175
#define height
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:3732
8: frame tripling
Definition: h264.h:158
int mb_field_decoding_flag
Definition: h264.h:431
uint8_t(* non_zero_count)[48]
Definition: h264.h:558
#define FF_PROFILE_H264_HIGH_10
Definition: avcodec.h:3183
exp golomb vlc stuff
uint8_t * bipred_scratchpad
Definition: h264.h:467
Definition: h264.h:121
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1554
int sei_recovery_frame_cnt
recovery_frame_cnt from SEI message
Definition: h264.h:773
int droppable
Definition: h264.h:547
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:953
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2859
int nal_ref_idc
Definition: h264.h:625
GetBitContext gb
Definition: h264.h:371
for(j=16;j >0;--j)
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:139
int b_stride
Definition: h264.h:571
Context Adaptive Binary Arithmetic Coder.
void ff_h264_init_dequant_tables(H264Context *h)
Definition: h264_slice.c:327