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 "h264data.h"
44 #include "h264chroma.h"
45 #include "h264_mvpred.h"
46 #include "golomb.h"
47 #include "mathops.h"
48 #include "me_cmp.h"
49 #include "mpegutils.h"
50 #include "profiles.h"
51 #include "rectangle.h"
52 #include "thread.h"
53 #include "vdpau_compat.h"
54 
55 static int h264_decode_end(AVCodecContext *avctx);
56 
57 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
58 
60 {
61  H264Context *h = avctx->priv_data;
62  return h ? h->sps.num_reorder_frames : 0;
63 }
64 
65 static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
66  int (*mv)[2][4][2],
67  int mb_x, int mb_y, int mb_intra, int mb_skipped)
68 {
69  H264Context *h = opaque;
70  H264SliceContext *sl = &h->slice_ctx[0];
71 
72  sl->mb_x = mb_x;
73  sl->mb_y = mb_y;
74  sl->mb_xy = mb_x + mb_y * h->mb_stride;
75  memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
76  av_assert1(ref >= 0);
77  /* FIXME: It is possible albeit uncommon that slice references
78  * differ between slices. We take the easy approach and ignore
79  * it for now. If this turns out to have any relevance in
80  * practice then correct remapping should be added. */
81  if (ref >= sl->ref_count[0])
82  ref = 0;
83  if (!sl->ref_list[0][ref].data[0]) {
84  av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
85  ref = 0;
86  }
87  if ((sl->ref_list[0][ref].reference&3) != 3) {
88  av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
89  return;
90  }
91  fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
92  2, 2, 2, ref, 1);
93  fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
94  fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
95  pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
96  sl->mb_mbaff =
97  sl->mb_field_decoding_flag = 0;
99 }
100 
102  int y, int height)
103 {
104  AVCodecContext *avctx = h->avctx;
105  const AVFrame *src = h->cur_pic.f;
106  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
107  int vshift = desc->log2_chroma_h;
108  const int field_pic = h->picture_structure != PICT_FRAME;
109  if (field_pic) {
110  height <<= 1;
111  y <<= 1;
112  }
113 
114  height = FFMIN(height, avctx->height - y);
115 
116  if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
117  return;
118 
119  if (avctx->draw_horiz_band) {
121  int i;
122 
123  offset[0] = y * src->linesize[0];
124  offset[1] =
125  offset[2] = (y >> vshift) * src->linesize[1];
126  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
127  offset[i] = 0;
128 
129  emms_c();
130 
131  avctx->draw_horiz_band(avctx, src, offset,
132  y, h->picture_structure, height);
133  }
134 }
135 
137  const uint8_t *src,
138  int *dst_length, int *consumed, int length)
139 {
140  int i, si, di;
141  uint8_t *dst;
142 
143  // src[0]&0x80; // forbidden bit
144  h->nal_ref_idc = src[0] >> 5;
145  h->nal_unit_type = src[0] & 0x1F;
146 
147  src++;
148  length--;
149 
150 #define STARTCODE_TEST \
151  if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
152  if (src[i + 2] != 3 && src[i + 2] != 0) { \
153  /* startcode, so we must be past the end */ \
154  length = i; \
155  } \
156  break; \
157  }
158 
159 #if HAVE_FAST_UNALIGNED
160 #define FIND_FIRST_ZERO \
161  if (i > 0 && !src[i]) \
162  i--; \
163  while (src[i]) \
164  i++
165 
166 #if HAVE_FAST_64BIT
167  for (i = 0; i + 1 < length; i += 9) {
168  if (!((~AV_RN64A(src + i) &
169  (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
170  0x8000800080008080ULL))
171  continue;
172  FIND_FIRST_ZERO;
174  i -= 7;
175  }
176 #else
177  for (i = 0; i + 1 < length; i += 5) {
178  if (!((~AV_RN32A(src + i) &
179  (AV_RN32A(src + i) - 0x01000101U)) &
180  0x80008080U))
181  continue;
182  FIND_FIRST_ZERO;
184  i -= 3;
185  }
186 #endif
187 #else
188  for (i = 0; i + 1 < length; i += 2) {
189  if (src[i])
190  continue;
191  if (i > 0 && src[i - 1] == 0)
192  i--;
194  }
195 #endif
196 
198  dst = sl->rbsp_buffer;
199 
200  if (!dst)
201  return NULL;
202 
203  if(i>=length-1){ //no escaped 0
204  *dst_length= length;
205  *consumed= length+1; //+1 for the header
206  if(h->avctx->flags2 & AV_CODEC_FLAG2_FAST){
207  return src;
208  }else{
209  memcpy(dst, src, length);
210  return dst;
211  }
212  }
213 
214  memcpy(dst, src, i);
215  si = di = i;
216  while (si + 2 < length) {
217  // remove escapes (very rare 1:2^22)
218  if (src[si + 2] > 3) {
219  dst[di++] = src[si++];
220  dst[di++] = src[si++];
221  } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
222  if (src[si + 2] == 3) { // escape
223  dst[di++] = 0;
224  dst[di++] = 0;
225  si += 3;
226  continue;
227  } else // next start code
228  goto nsc;
229  }
230 
231  dst[di++] = src[si++];
232  }
233  while (si < length)
234  dst[di++] = src[si++];
235 
236 nsc:
237  memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
238 
239  *dst_length = di;
240  *consumed = si + 1; // +1 for the header
241  /* FIXME store exact number of bits in the getbitcontext
242  * (it is needed for decoding) */
243  return dst;
244 }
245 
246 /**
247  * Identify the exact end of the bitstream
248  * @return the length of the trailing, or 0 if damaged
249  */
251 {
252  int v = *src;
253  int r;
254 
255  ff_tlog(h->avctx, "rbsp trailing %X\n", v);
256 
257  for (r = 1; r < 9; r++) {
258  if (v & 1)
259  return r;
260  v >>= 1;
261  }
262  return 0;
263 }
264 
266 {
267  int i;
268 
271  av_freep(&h->cbp_table);
272  av_freep(&h->mvd_table[0]);
273  av_freep(&h->mvd_table[1]);
274  av_freep(&h->direct_table);
277  h->slice_table = NULL;
278  av_freep(&h->list_counts);
279 
280  av_freep(&h->mb2b_xy);
281  av_freep(&h->mb2br_xy);
282 
287 
288  for (i = 0; i < h->nb_slice_ctx; i++) {
289  H264SliceContext *sl = &h->slice_ctx[i];
290 
291  av_freep(&sl->dc_val_base);
292  av_freep(&sl->er.mb_index2xy);
294  av_freep(&sl->er.er_temp_buffer);
295 
298  av_freep(&sl->top_borders[0]);
299  av_freep(&sl->top_borders[1]);
300 
303  sl->top_borders_allocated[0] = 0;
304  sl->top_borders_allocated[1] = 0;
305  }
306 }
307 
309 {
310  const int big_mb_num = h->mb_stride * (h->mb_height + 1);
311  const int row_mb_num = 2*h->mb_stride*FFMAX(h->avctx->thread_count, 1);
312  int x, y;
313 
315  row_mb_num, 8 * sizeof(uint8_t), fail)
317 
319  big_mb_num * 48 * sizeof(uint8_t), fail)
321  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
323  big_mb_num * sizeof(uint16_t), fail)
325  big_mb_num * sizeof(uint8_t), fail)
327  row_mb_num, 16 * sizeof(uint8_t), fail);
329  row_mb_num, 16 * sizeof(uint8_t), fail);
330  h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
331  h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
332 
334  4 * big_mb_num * sizeof(uint8_t), fail);
336  big_mb_num * sizeof(uint8_t), fail)
337 
338  memset(h->slice_table_base, -1,
339  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
340  h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
341 
343  big_mb_num * sizeof(uint32_t), fail);
345  big_mb_num * sizeof(uint32_t), fail);
346  for (y = 0; y < h->mb_height; y++)
347  for (x = 0; x < h->mb_width; x++) {
348  const int mb_xy = x + y * h->mb_stride;
349  const int b_xy = 4 * x + 4 * y * h->b_stride;
350 
351  h->mb2b_xy[mb_xy] = b_xy;
352  h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
353  }
354 
355  if (!h->dequant4_coeff[0])
357 
358  return 0;
359 
360 fail:
362  return AVERROR(ENOMEM);
363 }
364 
365 /**
366  * Init context
367  * Allocate buffers which are not shared amongst multiple threads.
368  */
370 {
371  ERContext *er = &sl->er;
372  int mb_array_size = h->mb_height * h->mb_stride;
373  int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
374  int c_size = h->mb_stride * (h->mb_height + 1);
375  int yc_size = y_size + 2 * c_size;
376  int x, y, i;
377 
378  sl->ref_cache[0][scan8[5] + 1] =
379  sl->ref_cache[0][scan8[7] + 1] =
380  sl->ref_cache[0][scan8[13] + 1] =
381  sl->ref_cache[1][scan8[5] + 1] =
382  sl->ref_cache[1][scan8[7] + 1] =
383  sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
384 
385  if (sl != h->slice_ctx) {
386  memset(er, 0, sizeof(*er));
387  } else
388  if (CONFIG_ERROR_RESILIENCE) {
389 
390  /* init ER */
391  er->avctx = h->avctx;
393  er->opaque = h;
394  er->quarter_sample = 1;
395 
396  er->mb_num = h->mb_num;
397  er->mb_width = h->mb_width;
398  er->mb_height = h->mb_height;
399  er->mb_stride = h->mb_stride;
400  er->b8_stride = h->mb_width * 2 + 1;
401 
402  // error resilience code looks cleaner with this
404  (h->mb_num + 1) * sizeof(int), fail);
405 
406  for (y = 0; y < h->mb_height; y++)
407  for (x = 0; x < h->mb_width; x++)
408  er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
409 
410  er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
411  h->mb_stride + h->mb_width;
412 
414  mb_array_size * sizeof(uint8_t), fail);
415 
417  h->mb_height * h->mb_stride, fail);
418 
420  yc_size * sizeof(int16_t), fail);
421  er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
422  er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
423  er->dc_val[2] = er->dc_val[1] + c_size;
424  for (i = 0; i < yc_size; i++)
425  sl->dc_val_base[i] = 1024;
426  }
427 
428  return 0;
429 
430 fail:
431  return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
432 }
433 
434 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
435  int parse_extradata);
436 
437 /* There are (invalid) samples in the wild with mp4-style extradata, where the
438  * parameter sets are stored unescaped (i.e. as RBSP).
439  * This function catches the parameter set decoding failure and tries again
440  * after escaping it */
441 static int decode_extradata_ps_mp4(H264Context *h, const uint8_t *buf, int buf_size)
442 {
443  int ret;
444 
445  ret = decode_nal_units(h, buf, buf_size, 1);
446  if (ret < 0 && !(h->avctx->err_recognition & AV_EF_EXPLODE)) {
447  GetByteContext gbc;
448  PutByteContext pbc;
449  uint8_t *escaped_buf;
450  int escaped_buf_size;
451 
453  "SPS decoding failure, trying again after escaping the NAL\n");
454 
455  if (buf_size / 2 >= (INT16_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 3)
456  return AVERROR(ERANGE);
457  escaped_buf_size = buf_size * 3 / 2 + AV_INPUT_BUFFER_PADDING_SIZE;
458  escaped_buf = av_mallocz(escaped_buf_size);
459  if (!escaped_buf)
460  return AVERROR(ENOMEM);
461 
462  bytestream2_init(&gbc, buf, buf_size);
463  bytestream2_init_writer(&pbc, escaped_buf, escaped_buf_size);
464 
465  while (bytestream2_get_bytes_left(&gbc)) {
466  if (bytestream2_get_bytes_left(&gbc) >= 3 &&
467  bytestream2_peek_be24(&gbc) <= 3) {
468  bytestream2_put_be24(&pbc, 3);
469  bytestream2_skip(&gbc, 2);
470  } else
471  bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
472  }
473 
474  escaped_buf_size = bytestream2_tell_p(&pbc);
475  AV_WB16(escaped_buf, escaped_buf_size - 2);
476 
477  ret = decode_nal_units(h, escaped_buf, escaped_buf_size, 1);
478  av_freep(&escaped_buf);
479  if (ret < 0)
480  return ret;
481  }
482 
483  return 0;
484 }
485 
487 {
488  AVCodecContext *avctx = h->avctx;
489  int ret;
490 
491  if (!buf || size <= 0)
492  return -1;
493 
494  if (buf[0] == 1) {
495  int i, cnt, nalsize;
496  const unsigned char *p = buf;
497 
498  h->is_avc = 1;
499 
500  if (size < 7) {
501  av_log(avctx, AV_LOG_ERROR,
502  "avcC %d too short\n", size);
503  return AVERROR_INVALIDDATA;
504  }
505  /* sps and pps in the avcC always have length coded with 2 bytes,
506  * so put a fake nal_length_size = 2 while parsing them */
507  h->nal_length_size = 2;
508  // Decode sps from avcC
509  cnt = *(p + 5) & 0x1f; // Number of sps
510  p += 6;
511  for (i = 0; i < cnt; i++) {
512  nalsize = AV_RB16(p) + 2;
513  if(nalsize > size - (p-buf))
514  return AVERROR_INVALIDDATA;
515  ret = decode_extradata_ps_mp4(h, p, nalsize);
516  if (ret < 0) {
517  av_log(avctx, AV_LOG_ERROR,
518  "Decoding sps %d from avcC failed\n", i);
519  return ret;
520  }
521  p += nalsize;
522  }
523  // Decode pps from avcC
524  cnt = *(p++); // Number of pps
525  for (i = 0; i < cnt; i++) {
526  nalsize = AV_RB16(p) + 2;
527  if(nalsize > size - (p-buf))
528  return AVERROR_INVALIDDATA;
529  ret = decode_extradata_ps_mp4(h, p, nalsize);
530  if (ret < 0) {
531  av_log(avctx, AV_LOG_ERROR,
532  "Decoding pps %d from avcC failed\n", i);
533  return ret;
534  }
535  p += nalsize;
536  }
537  // Store right nal length size that will be used to parse all other nals
538  h->nal_length_size = (buf[4] & 0x03) + 1;
539  } else {
540  h->is_avc = 0;
541  ret = decode_nal_units(h, buf, size, 1);
542  if (ret < 0)
543  return ret;
544  }
545  return size;
546 }
547 
549 {
550  int i;
551 
552  h->avctx = avctx;
553  h->backup_width = -1;
554  h->backup_height = -1;
556  h->dequant_coeff_pps = -1;
557  h->current_sps_id = -1;
558  h->cur_chroma_format_idc = -1;
559 
561  h->slice_context_count = 1;
562  h->workaround_bugs = avctx->workaround_bugs;
563  h->flags = avctx->flags;
564  h->prev_poc_msb = 1 << 16;
565  h->x264_build = -1;
566  h->recovery_frame = -1;
567  h->frame_recovered = 0;
568  h->prev_frame_num = -1;
570 
571  h->next_outputed_poc = INT_MIN;
572  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
573  h->last_pocs[i] = INT_MIN;
574 
576 
578 
580  h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
581  if (!h->slice_ctx) {
582  h->nb_slice_ctx = 0;
583  return AVERROR(ENOMEM);
584  }
585 
586  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
587  h->DPB[i].f = av_frame_alloc();
588  if (!h->DPB[i].f)
589  return AVERROR(ENOMEM);
590  }
591 
592  h->cur_pic.f = av_frame_alloc();
593  if (!h->cur_pic.f)
594  return AVERROR(ENOMEM);
595 
597  if (!h->last_pic_for_ec.f)
598  return AVERROR(ENOMEM);
599 
600  for (i = 0; i < h->nb_slice_ctx; i++)
601  h->slice_ctx[i].h264 = h;
602 
603  return 0;
604 }
605 
607 
609 {
610  H264Context *h = avctx->priv_data;
611  int ret;
612 
613  ret = h264_init_context(avctx, h);
614  if (ret < 0)
615  return ret;
616 
617  /* set defaults */
618  if (!avctx->has_b_frames)
619  h->low_delay = 1;
620 
621  ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
622  if (ret != 0) {
623  av_log(avctx, AV_LOG_ERROR, "pthread_once has failed.");
624  return AVERROR_UNKNOWN;
625  }
626 
627  if (avctx->codec_id == AV_CODEC_ID_H264) {
628  if (avctx->ticks_per_frame == 1) {
629  if(h->avctx->time_base.den < INT_MAX/2) {
630  h->avctx->time_base.den *= 2;
631  } else
632  h->avctx->time_base.num /= 2;
633  }
634  avctx->ticks_per_frame = 2;
635  }
636 
637  if (avctx->extradata_size > 0 && avctx->extradata) {
638  ret = ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
639  if (ret < 0) {
640  h264_decode_end(avctx);
641  return ret;
642  }
643  }
644 
648  h->low_delay = 0;
649  }
650 
651  avctx->internal->allocate_progress = 1;
652 
654 
655  if (h->enable_er < 0 && (avctx->active_thread_type & FF_THREAD_SLICE))
656  h->enable_er = 0;
657 
658  if (h->enable_er && (avctx->active_thread_type & FF_THREAD_SLICE)) {
659  av_log(avctx, AV_LOG_WARNING,
660  "Error resilience with slice threads is enabled. It is unsafe and unsupported and may crash. "
661  "Use it at your own risk\n");
662  }
663 
664  return 0;
665 }
666 
667 #if HAVE_THREADS
668 static int decode_init_thread_copy(AVCodecContext *avctx)
669 {
670  H264Context *h = avctx->priv_data;
671  int ret;
672 
673  if (!avctx->internal->is_copy)
674  return 0;
675 
676  memset(h, 0, sizeof(*h));
677 
678  ret = h264_init_context(avctx, h);
679  if (ret < 0)
680  return ret;
681 
682  h->context_initialized = 0;
683 
684  return 0;
685 }
686 #endif
687 
688 /**
689  * Run setup operations that must be run after slice header decoding.
690  * This includes finding the next displayed frame.
691  *
692  * @param h h264 master context
693  * @param setup_finished enough NALs have been read that we can call
694  * ff_thread_finish_setup()
695  */
696 static void decode_postinit(H264Context *h, int setup_finished)
697 {
699  H264Picture *cur = h->cur_pic_ptr;
700  int i, pics, out_of_order, out_idx;
701 
702  h->cur_pic_ptr->f->pict_type = h->pict_type;
703 
704  if (h->next_output_pic)
705  return;
706 
707  if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
708  /* FIXME: if we have two PAFF fields in one packet, we can't start
709  * the next thread here. If we have one field per packet, we can.
710  * The check in decode_nal_units() is not good enough to find this
711  * yet, so we assume the worst for now. */
712  // if (setup_finished)
713  // ff_thread_finish_setup(h->avctx);
714  if (cur->field_poc[0] == INT_MAX && cur->field_poc[1] == INT_MAX)
715  return;
716  if (h->avctx->hwaccel || h->missing_fields <=1)
717  return;
718  }
719 
720  cur->f->interlaced_frame = 0;
721  cur->f->repeat_pict = 0;
722 
723  /* Signal interlacing information externally. */
724  /* Prioritize picture timing SEI information over used
725  * decoding process if it exists. */
726 
727  if (h->sps.pic_struct_present_flag) {
728  switch (h->sei_pic_struct) {
730  break;
733  cur->f->interlaced_frame = 1;
734  break;
737  if (FIELD_OR_MBAFF_PICTURE(h))
738  cur->f->interlaced_frame = 1;
739  else
740  // try to flag soft telecine progressive
742  break;
745  /* Signal the possibility of telecined film externally
746  * (pic_struct 5,6). From these hints, let the applications
747  * decide if they apply deinterlacing. */
748  cur->f->repeat_pict = 1;
749  break;
751  cur->f->repeat_pict = 2;
752  break;
754  cur->f->repeat_pict = 4;
755  break;
756  }
757 
758  if ((h->sei_ct_type & 3) &&
760  cur->f->interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
761  } else {
762  /* Derive interlacing flag from used decoding process. */
764  }
766 
767  if (cur->field_poc[0] != cur->field_poc[1]) {
768  /* Derive top_field_first from field pocs. */
769  cur->f->top_field_first = cur->field_poc[0] < cur->field_poc[1];
770  } else {
771  if (h->sps.pic_struct_present_flag) {
772  /* Use picture timing SEI information. Even if it is a
773  * information of a past frame, better than nothing. */
776  cur->f->top_field_first = 1;
777  else
778  cur->f->top_field_first = 0;
779  } else if (cur->f->interlaced_frame) {
780  /* Default to top field first when pic_struct_present_flag
781  * is not set but interlaced frame detected */
782  cur->f->top_field_first = 1;
783  } else {
784  /* Most likely progressive */
785  cur->f->top_field_first = 0;
786  }
787  }
788 
789  if (h->sei_frame_packing_present &&
794  AVStereo3D *stereo = av_stereo3d_create_side_data(cur->f);
795  if (stereo) {
796  switch (h->frame_packing_arrangement_type) {
797  case 0:
798  stereo->type = AV_STEREO3D_CHECKERBOARD;
799  break;
800  case 1:
801  stereo->type = AV_STEREO3D_COLUMNS;
802  break;
803  case 2:
804  stereo->type = AV_STEREO3D_LINES;
805  break;
806  case 3:
807  if (h->quincunx_subsampling)
809  else
810  stereo->type = AV_STEREO3D_SIDEBYSIDE;
811  break;
812  case 4:
813  stereo->type = AV_STEREO3D_TOPBOTTOM;
814  break;
815  case 5:
817  break;
818  case 6:
819  stereo->type = AV_STEREO3D_2D;
820  break;
821  }
822 
823  if (h->content_interpretation_type == 2)
824  stereo->flags = AV_STEREO3D_FLAG_INVERT;
825  }
826  }
827 
830  double angle = h->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
831  AVFrameSideData *rotation = av_frame_new_side_data(cur->f,
833  sizeof(int32_t) * 9);
834  if (rotation) {
835  av_display_rotation_set((int32_t *)rotation->data, angle);
836  av_display_matrix_flip((int32_t *)rotation->data,
837  h->sei_hflip, h->sei_vflip);
838  }
839  }
840 
843  sizeof(uint8_t));
844 
845  if (sd) {
848  }
849  }
850 
851  if (h->a53_caption) {
854  h->a53_caption_size);
855  if (sd)
856  memcpy(sd->data, h->a53_caption, h->a53_caption_size);
857  av_freep(&h->a53_caption);
858  h->a53_caption_size = 0;
860  }
861 
862  cur->mmco_reset = h->mmco_reset;
863  h->mmco_reset = 0;
864 
865  // FIXME do something with unavailable reference frames
866 
867  /* Sort B-frames into display order */
871  }
872  h->low_delay = !h->avctx->has_b_frames;
873 
874  for (i = 0; 1; i++) {
875  if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
876  if(i)
877  h->last_pocs[i-1] = cur->poc;
878  break;
879  } else if(i) {
880  h->last_pocs[i-1]= h->last_pocs[i];
881  }
882  }
883  out_of_order = MAX_DELAYED_PIC_COUNT - i;
884  if( cur->f->pict_type == AV_PICTURE_TYPE_B
886  out_of_order = FFMAX(out_of_order, 1);
887  if (out_of_order == MAX_DELAYED_PIC_COUNT) {
888  av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
889  for (i = 1; i < MAX_DELAYED_PIC_COUNT; i++)
890  h->last_pocs[i] = INT_MIN;
891  h->last_pocs[0] = cur->poc;
892  cur->mmco_reset = 1;
893  } else if(h->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
894  av_log(h->avctx, AV_LOG_INFO, "Increasing reorder buffer to %d\n", out_of_order);
895  h->avctx->has_b_frames = out_of_order;
896  h->low_delay = 0;
897  }
898 
899  pics = 0;
900  while (h->delayed_pic[pics])
901  pics++;
902 
904 
905  h->delayed_pic[pics++] = cur;
906  if (cur->reference == 0)
907  cur->reference = DELAYED_PIC_REF;
908 
909  out = h->delayed_pic[0];
910  out_idx = 0;
911  for (i = 1; h->delayed_pic[i] &&
912  !h->delayed_pic[i]->f->key_frame &&
913  !h->delayed_pic[i]->mmco_reset;
914  i++)
915  if (h->delayed_pic[i]->poc < out->poc) {
916  out = h->delayed_pic[i];
917  out_idx = i;
918  }
919  if (h->avctx->has_b_frames == 0 &&
920  (h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset))
921  h->next_outputed_poc = INT_MIN;
922  out_of_order = out->poc < h->next_outputed_poc;
923 
924  if (out_of_order || pics > h->avctx->has_b_frames) {
925  out->reference &= ~DELAYED_PIC_REF;
926  // for frame threading, the owner must be the second field's thread or
927  // else the first thread can release the picture and reuse it unsafely
928  for (i = out_idx; h->delayed_pic[i]; i++)
929  h->delayed_pic[i] = h->delayed_pic[i + 1];
930  }
931  if (!out_of_order && pics > h->avctx->has_b_frames) {
932  h->next_output_pic = out;
933  if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset)) {
934  h->next_outputed_poc = INT_MIN;
935  } else
936  h->next_outputed_poc = out->poc;
937  } else {
938  av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
939  }
940 
941  if (h->next_output_pic) {
942  if (h->next_output_pic->recovered) {
943  // We have reached an recovery point and all frames after it in
944  // display order are "recovered".
946  }
948  }
949 
950  if (setup_finished && !h->avctx->hwaccel) {
952 
954  h->setup_finished = 1;
955  }
956 }
957 
958 /**
959  * instantaneous decoder refresh.
960  */
961 static void idr(H264Context *h)
962 {
963  int i;
965  h->prev_frame_num =
966  h->prev_frame_num_offset = 0;
967  h->prev_poc_msb = 1<<16;
968  h->prev_poc_lsb = 0;
969  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
970  h->last_pocs[i] = INT_MIN;
971 }
972 
973 /* forget old pics after a seek */
975 {
976  int i, j;
977 
978  h->next_outputed_poc = INT_MIN;
979  h->prev_interlaced_frame = 1;
980  idr(h);
981 
982  h->prev_frame_num = -1;
983  if (h->cur_pic_ptr) {
984  h->cur_pic_ptr->reference = 0;
985  for (j=i=0; h->delayed_pic[i]; i++)
986  if (h->delayed_pic[i] != h->cur_pic_ptr)
987  h->delayed_pic[j++] = h->delayed_pic[i];
988  h->delayed_pic[j] = NULL;
989  }
991 
992  h->first_field = 0;
994  h->recovery_frame = -1;
995  h->frame_recovered = 0;
996  h->current_slice = 0;
997  h->mmco_reset = 1;
998  for (i = 0; i < h->nb_slice_ctx; i++)
999  h->slice_ctx[i].list_count = 0;
1000 }
1001 
1002 /* forget old pics after a seek */
1003 static void flush_dpb(AVCodecContext *avctx)
1004 {
1005  H264Context *h = avctx->priv_data;
1006  int i;
1007 
1008  memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
1009 
1011 
1012  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
1013  ff_h264_unref_picture(h, &h->DPB[i]);
1014  h->cur_pic_ptr = NULL;
1016 
1017  h->mb_y = 0;
1018 
1020  h->context_initialized = 0;
1021 }
1022 
1023 int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
1024 {
1025  const int max_frame_num = 1 << h->sps.log2_max_frame_num;
1026  int field_poc[2];
1027 
1029  if (h->frame_num < h->prev_frame_num)
1030  h->frame_num_offset += max_frame_num;
1031 
1032  if (h->sps.poc_type == 0) {
1033  const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
1034 
1035  if (h->poc_lsb < h->prev_poc_lsb &&
1036  h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
1037  h->poc_msb = h->prev_poc_msb + max_poc_lsb;
1038  else if (h->poc_lsb > h->prev_poc_lsb &&
1039  h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
1040  h->poc_msb = h->prev_poc_msb - max_poc_lsb;
1041  else
1042  h->poc_msb = h->prev_poc_msb;
1043  field_poc[0] =
1044  field_poc[1] = h->poc_msb + h->poc_lsb;
1045  if (h->picture_structure == PICT_FRAME)
1046  field_poc[1] += h->delta_poc_bottom;
1047  } else if (h->sps.poc_type == 1) {
1048  int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
1049  int i;
1050 
1051  if (h->sps.poc_cycle_length != 0)
1052  abs_frame_num = h->frame_num_offset + h->frame_num;
1053  else
1054  abs_frame_num = 0;
1055 
1056  if (h->nal_ref_idc == 0 && abs_frame_num > 0)
1057  abs_frame_num--;
1058 
1059  expected_delta_per_poc_cycle = 0;
1060  for (i = 0; i < h->sps.poc_cycle_length; i++)
1061  // FIXME integrate during sps parse
1062  expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
1063 
1064  if (abs_frame_num > 0) {
1065  int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
1066  int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
1067 
1068  expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
1069  for (i = 0; i <= frame_num_in_poc_cycle; i++)
1070  expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
1071  } else
1072  expectedpoc = 0;
1073 
1074  if (h->nal_ref_idc == 0)
1075  expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
1076 
1077  field_poc[0] = expectedpoc + h->delta_poc[0];
1078  field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
1079 
1080  if (h->picture_structure == PICT_FRAME)
1081  field_poc[1] += h->delta_poc[1];
1082  } else {
1083  int poc = 2 * (h->frame_num_offset + h->frame_num);
1084 
1085  if (!h->nal_ref_idc)
1086  poc--;
1087 
1088  field_poc[0] = poc;
1089  field_poc[1] = poc;
1090  }
1091 
1093  pic_field_poc[0] = field_poc[0];
1095  pic_field_poc[1] = field_poc[1];
1096  *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
1097 
1098  return 0;
1099 }
1100 
1101 /**
1102  * Compute profile from profile_idc and constraint_set?_flags.
1103  *
1104  * @param sps SPS
1105  *
1106  * @return profile as defined by FF_PROFILE_H264_*
1107  */
1109 {
1110  int profile = sps->profile_idc;
1111 
1112  switch (sps->profile_idc) {
1114  // constraint_set1_flag set to 1
1115  profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
1116  break;
1120  // constraint_set3_flag set to 1
1121  profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
1122  break;
1123  }
1124 
1125  return profile;
1126 }
1127 
1129 {
1130  int ref_count[2], list_count;
1131  int num_ref_idx_active_override_flag;
1132 
1133  // set defaults, might be overridden a few lines later
1134  ref_count[0] = h->pps.ref_count[0];
1135  ref_count[1] = h->pps.ref_count[1];
1136 
1137  if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1138  unsigned max[2];
1139  max[0] = max[1] = h->picture_structure == PICT_FRAME ? 15 : 31;
1140 
1141  if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1142  sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1143  num_ref_idx_active_override_flag = get_bits1(&sl->gb);
1144 
1145  if (num_ref_idx_active_override_flag) {
1146  ref_count[0] = get_ue_golomb(&sl->gb) + 1;
1147  if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
1148  ref_count[1] = get_ue_golomb(&sl->gb) + 1;
1149  } else
1150  // full range is spec-ok in this case, even for frames
1151  ref_count[1] = 1;
1152  }
1153 
1154  if (ref_count[0]-1 > max[0] || ref_count[1]-1 > max[1]){
1155  av_log(h->avctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n", ref_count[0]-1, max[0], ref_count[1]-1, max[1]);
1156  sl->ref_count[0] = sl->ref_count[1] = 0;
1157  sl->list_count = 0;
1158  return AVERROR_INVALIDDATA;
1159  }
1160 
1161  if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1162  list_count = 2;
1163  else
1164  list_count = 1;
1165  } else {
1166  list_count = 0;
1167  ref_count[0] = ref_count[1] = 0;
1168  }
1169 
1170  if (list_count != sl->list_count ||
1171  ref_count[0] != sl->ref_count[0] ||
1172  ref_count[1] != sl->ref_count[1]) {
1173  sl->ref_count[0] = ref_count[0];
1174  sl->ref_count[1] = ref_count[1];
1175  sl->list_count = list_count;
1176  return 1;
1177  }
1178 
1179  return 0;
1180 }
1181 
1182 static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
1183 
1184 static int get_bit_length(H264Context *h, const uint8_t *buf,
1185  const uint8_t *ptr, int dst_length,
1186  int i, int next_avc)
1187 {
1188  if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
1189  buf[i] == 0x00 && buf[i + 1] == 0x00 &&
1190  buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
1192 
1193  if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
1194  while (dst_length > 0 && ptr[dst_length - 1] == 0)
1195  dst_length--;
1196 
1197  if (!dst_length)
1198  return 0;
1199 
1200  return 8 * dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1);
1201 }
1202 
1203 static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
1204 {
1205  int next_avc = h->is_avc ? 0 : buf_size;
1206  int nal_index = 0;
1207  int buf_index = 0;
1208  int nals_needed = 0;
1209  int first_slice = 0;
1210 
1211  while(1) {
1212  GetBitContext gb;
1213  int nalsize = 0;
1214  int dst_length, bit_length, consumed;
1215  const uint8_t *ptr;
1216 
1217  if (buf_index >= next_avc) {
1218  nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
1219  if (nalsize < 0)
1220  break;
1221  next_avc = buf_index + nalsize;
1222  } else {
1223  buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
1224  if (buf_index >= buf_size)
1225  break;
1226  if (buf_index >= next_avc)
1227  continue;
1228  }
1229 
1230  ptr = ff_h264_decode_nal(h, &h->slice_ctx[0], buf + buf_index, &dst_length, &consumed,
1231  next_avc - buf_index);
1232 
1233  if (!ptr || dst_length < 0)
1234  return AVERROR_INVALIDDATA;
1235 
1236  buf_index += consumed;
1237 
1238  bit_length = get_bit_length(h, buf, ptr, dst_length,
1239  buf_index, next_avc);
1240  nal_index++;
1241 
1242  /* packets can sometimes contain multiple PPS/SPS,
1243  * e.g. two PAFF field pictures in one packet, or a demuxer
1244  * which splits NALs strangely if so, when frame threading we
1245  * can't start the next thread until we've read all of them */
1246  switch (h->nal_unit_type) {
1247  case NAL_SPS:
1248  case NAL_PPS:
1249  nals_needed = nal_index;
1250  break;
1251  case NAL_DPA:
1252  case NAL_IDR_SLICE:
1253  case NAL_SLICE:
1254  init_get_bits(&gb, ptr, bit_length);
1255  if (!get_ue_golomb_long(&gb) || // first_mb_in_slice
1256  !first_slice ||
1257  first_slice != h->nal_unit_type)
1258  nals_needed = nal_index;
1259  if (!first_slice)
1260  first_slice = h->nal_unit_type;
1261  }
1262  }
1263 
1264  return nals_needed;
1265 }
1266 
1267 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
1268  int parse_extradata)
1269 {
1270  AVCodecContext *const avctx = h->avctx;
1271  H264SliceContext *sl;
1272  int buf_index;
1273  unsigned context_count;
1274  int next_avc;
1275  int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
1276  int nal_index;
1277  int idr_cleared=0;
1278  int ret = 0;
1279 
1280  h->nal_unit_type= 0;
1281 
1282  if(!h->slice_context_count)
1283  h->slice_context_count= 1;
1285  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
1286  h->current_slice = 0;
1287  if (!h->first_field)
1288  h->cur_pic_ptr = NULL;
1289  ff_h264_reset_sei(h);
1290  }
1291 
1292  if (h->nal_length_size == 4) {
1293  if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
1294  h->is_avc = 0;
1295  }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
1296  h->is_avc = 1;
1297  }
1298 
1299  if (avctx->active_thread_type & FF_THREAD_FRAME)
1300  nals_needed = get_last_needed_nal(h, buf, buf_size);
1301 
1302  {
1303  buf_index = 0;
1304  context_count = 0;
1305  next_avc = h->is_avc ? 0 : buf_size;
1306  nal_index = 0;
1307  for (;;) {
1308  int consumed;
1309  int dst_length;
1310  int bit_length;
1311  const uint8_t *ptr;
1312  int nalsize = 0;
1313  int err;
1314 
1315  if (buf_index >= next_avc) {
1316  nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
1317  if (nalsize < 0)
1318  break;
1319  next_avc = buf_index + nalsize;
1320  } else {
1321  buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
1322  if (buf_index >= buf_size)
1323  break;
1324  if (buf_index >= next_avc)
1325  continue;
1326  }
1327 
1328  sl = &h->slice_ctx[context_count];
1329 
1330  ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
1331  &consumed, next_avc - buf_index);
1332  if (!ptr || dst_length < 0) {
1333  ret = -1;
1334  goto end;
1335  }
1336 
1337  bit_length = get_bit_length(h, buf, ptr, dst_length,
1338  buf_index + consumed, next_avc);
1339 
1340  if (h->avctx->debug & FF_DEBUG_STARTCODE)
1342  "NAL %d/%d at %d/%d length %d\n",
1343  h->nal_unit_type, h->nal_ref_idc, buf_index, buf_size, dst_length);
1344 
1345  if (h->is_avc && (nalsize != consumed) && nalsize)
1347  "AVC: Consumed only %d bytes instead of %d\n",
1348  consumed, nalsize);
1349 
1350  buf_index += consumed;
1351  nal_index++;
1352 
1353  if (avctx->skip_frame >= AVDISCARD_NONREF &&
1354  h->nal_ref_idc == 0 &&
1355  h->nal_unit_type != NAL_SEI)
1356  continue;
1357 
1358 again:
1359  /* Ignore per frame NAL unit type during extradata
1360  * parsing. Decoding slices is not possible in codec init
1361  * with frame-mt */
1362  if (parse_extradata) {
1363  switch (h->nal_unit_type) {
1364  case NAL_IDR_SLICE:
1365  case NAL_SLICE:
1366  case NAL_DPA:
1367  case NAL_DPB:
1368  case NAL_DPC:
1370  "Ignoring NAL %d in global header/extradata\n",
1371  h->nal_unit_type);
1372  // fall through to next case
1373  case NAL_AUXILIARY_SLICE:
1375  }
1376  }
1377 
1378  err = 0;
1379 
1380  switch (h->nal_unit_type) {
1381  case NAL_IDR_SLICE:
1382  if ((ptr[0] & 0xFC) == 0x98) {
1383  av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
1384  h->next_outputed_poc = INT_MIN;
1385  ret = -1;
1386  goto end;
1387  }
1388  if (h->nal_unit_type != NAL_IDR_SLICE) {
1390  "Invalid mix of idr and non-idr slices\n");
1391  ret = -1;
1392  goto end;
1393  }
1394  if(!idr_cleared) {
1395  if (h->current_slice && (avctx->active_thread_type & FF_THREAD_SLICE)) {
1396  av_log(h, AV_LOG_ERROR, "invalid mixed IDR / non IDR frames cannot be decoded in slice multithreading mode\n");
1397  ret = AVERROR_INVALIDDATA;
1398  goto end;
1399  }
1400  idr(h); // FIXME ensure we don't lose some frames if there is reordering
1401  }
1402  idr_cleared = 1;
1403  h->has_recovery_point = 1;
1404  case NAL_SLICE:
1405  init_get_bits(&sl->gb, ptr, bit_length);
1406 
1407  if ( nals_needed >= nal_index
1408  || (!(avctx->active_thread_type & FF_THREAD_FRAME) && !context_count))
1409  h->au_pps_id = -1;
1410 
1411  if ((err = ff_h264_decode_slice_header(h, sl)))
1412  break;
1413 
1414  if (h->sei_recovery_frame_cnt >= 0) {
1416  h->valid_recovery_point = 1;
1417 
1418  if ( h->recovery_frame < 0
1419  || av_mod_uintp2(h->recovery_frame - h->frame_num, h->sps.log2_max_frame_num) > h->sei_recovery_frame_cnt) {
1420  h->recovery_frame = av_mod_uintp2(h->frame_num + h->sei_recovery_frame_cnt, h->sps.log2_max_frame_num);
1421 
1422  if (!h->valid_recovery_point)
1423  h->recovery_frame = h->frame_num;
1424  }
1425  }
1426 
1427  h->cur_pic_ptr->f->key_frame |=
1428  (h->nal_unit_type == NAL_IDR_SLICE);
1429 
1430  if (h->nal_unit_type == NAL_IDR_SLICE ||
1431  (h->recovery_frame == h->frame_num && h->nal_ref_idc)) {
1432  h->recovery_frame = -1;
1433  h->cur_pic_ptr->recovered = 1;
1434  }
1435  // If we have an IDR, all frames after it in decoded order are
1436  // "recovered".
1437  if (h->nal_unit_type == NAL_IDR_SLICE)
1439 #if 1
1441 #else
1443 #endif
1444 
1445  if (h->current_slice == 1) {
1446  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS))
1447  decode_postinit(h, nal_index >= nals_needed);
1448 
1449  if (h->avctx->hwaccel &&
1450  (ret = h->avctx->hwaccel->start_frame(h->avctx, buf, buf_size)) < 0)
1451  goto end;
1452 #if FF_API_CAP_VDPAU
1453  if (CONFIG_H264_VDPAU_DECODER &&
1456 #endif
1457  }
1458 
1459  if (sl->redundant_pic_count == 0) {
1460  if (avctx->hwaccel) {
1461  ret = avctx->hwaccel->decode_slice(avctx,
1462  &buf[buf_index - consumed],
1463  consumed);
1464  if (ret < 0)
1465  goto end;
1466 #if FF_API_CAP_VDPAU
1467  } else if (CONFIG_H264_VDPAU_DECODER &&
1470  start_code,
1471  sizeof(start_code));
1473  &buf[buf_index - consumed],
1474  consumed);
1475 #endif
1476  } else
1477  context_count++;
1478  }
1479  break;
1480  case NAL_DPA:
1481  case NAL_DPB:
1482  case NAL_DPC:
1483  avpriv_request_sample(avctx, "data partitioning");
1484  break;
1485  case NAL_SEI:
1486  init_get_bits(&h->gb, ptr, bit_length);
1487  ret = ff_h264_decode_sei(h);
1488  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1489  goto end;
1490  break;
1491  case NAL_SPS:
1492  init_get_bits(&h->gb, ptr, bit_length);
1493  if (ff_h264_decode_seq_parameter_set(h, 0) >= 0)
1494  break;
1495  if (h->is_avc ? nalsize : 1) {
1497  "SPS decoding failure, trying again with the complete NAL\n");
1498  if (h->is_avc)
1499  av_assert0(next_avc - buf_index + consumed == nalsize);
1500  if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
1501  break;
1502  init_get_bits(&h->gb, &buf[buf_index + 1 - consumed],
1503  8*(next_avc - buf_index + consumed - 1));
1504  if (ff_h264_decode_seq_parameter_set(h, 0) >= 0)
1505  break;
1506  }
1507  init_get_bits(&h->gb, ptr, bit_length);
1509 
1510  break;
1511  case NAL_PPS:
1512  init_get_bits(&h->gb, ptr, bit_length);
1513  ret = ff_h264_decode_picture_parameter_set(h, bit_length);
1514  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1515  goto end;
1516  break;
1517  case NAL_AUD:
1518  case NAL_END_SEQUENCE:
1519  case NAL_END_STREAM:
1520  case NAL_FILLER_DATA:
1521  case NAL_SPS_EXT:
1522  case NAL_AUXILIARY_SLICE:
1523  break;
1524  case NAL_FF_IGNORE:
1525  break;
1526  default:
1527  av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
1528  h->nal_unit_type, bit_length);
1529  }
1530 
1531  if (context_count == h->max_contexts) {
1532  ret = ff_h264_execute_decode_slices(h, context_count);
1533  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1534  goto end;
1535  context_count = 0;
1536  }
1537 
1538  if (err < 0 || err == SLICE_SKIPED) {
1539  if (err < 0)
1540  av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
1541  sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
1542  } else if (err == SLICE_SINGLETHREAD) {
1543  if (context_count > 1) {
1544  ret = ff_h264_execute_decode_slices(h, context_count - 1);
1545  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1546  goto end;
1547  context_count = 0;
1548  }
1549  /* Slice could not be decoded in parallel mode, restart. Note
1550  * that rbsp_buffer is not transferred, but since we no longer
1551  * run in parallel mode this should not be an issue. */
1552  sl = &h->slice_ctx[0];
1553  goto again;
1554  }
1555  }
1556  }
1557  if (context_count) {
1558  ret = ff_h264_execute_decode_slices(h, context_count);
1559  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1560  goto end;
1561  }
1562 
1563  ret = 0;
1564 end:
1565 
1566 #if CONFIG_ERROR_RESILIENCE
1567  sl = h->slice_ctx;
1568  /*
1569  * FIXME: Error handling code does not seem to support interlaced
1570  * when slices span multiple rows
1571  * The ff_er_add_slice calls don't work right for bottom
1572  * fields; they cause massive erroneous error concealing
1573  * Error marking covers both fields (top and bottom).
1574  * This causes a mismatched s->error_count
1575  * and a bad error table. Further, the error count goes to
1576  * INT_MAX when called for bottom field, because mb_y is
1577  * past end by one (callers fault) and resync_mb_y != 0
1578  * causes problems for the first MB line, too.
1579  */
1580  if (!FIELD_PICTURE(h) && h->current_slice && !h->sps.new && h->enable_er) {
1581  int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
1582 
1584 
1585  if (use_last_pic) {
1587  sl->ref_list[0][0].parent = &h->last_pic_for_ec;
1588  memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
1589  memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
1591  } else if (sl->ref_count[0]) {
1592  ff_h264_set_erpic(&sl->er.last_pic, sl->ref_list[0][0].parent);
1593  } else
1595 
1596  if (sl->ref_count[1])
1597  ff_h264_set_erpic(&sl->er.next_pic, sl->ref_list[1][0].parent);
1598 
1599  sl->er.ref_count = sl->ref_count[0];
1600 
1601  ff_er_frame_end(&sl->er);
1602  if (use_last_pic)
1603  memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
1604  }
1605 #endif /* CONFIG_ERROR_RESILIENCE */
1606  /* clean up */
1607  if (h->cur_pic_ptr && !h->droppable) {
1610  }
1611 
1612  return (ret < 0) ? ret : buf_index;
1613 }
1614 
1615 /**
1616  * Return the number of bytes consumed for building the current frame.
1617  */
1618 static int get_consumed_bytes(int pos, int buf_size)
1619 {
1620  if (pos == 0)
1621  pos = 1; // avoid infinite loops (I doubt that is needed but...)
1622  if (pos + 10 > buf_size)
1623  pos = buf_size; // oops ;)
1624 
1625  return pos;
1626 }
1627 
1629 {
1630  AVFrame *src = srcp->f;
1631  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
1632  int i;
1633  int ret = av_frame_ref(dst, src);
1634  if (ret < 0)
1635  return ret;
1636 
1637  av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(h), 0);
1638 
1639  h->backup_width = h->avctx->width;
1640  h->backup_height = h->avctx->height;
1641  h->backup_pix_fmt = h->avctx->pix_fmt;
1642 
1643  h->avctx->width = dst->width;
1644  h->avctx->height = dst->height;
1645  h->avctx->pix_fmt = dst->format;
1646 
1647  if (srcp->sei_recovery_frame_cnt == 0)
1648  dst->key_frame = 1;
1649  if (!srcp->crop)
1650  return 0;
1651 
1652  for (i = 0; i < desc->nb_components; i++) {
1653  int hshift = (i > 0) ? desc->log2_chroma_w : 0;
1654  int vshift = (i > 0) ? desc->log2_chroma_h : 0;
1655  int off = ((srcp->crop_left >> hshift) << h->pixel_shift) +
1656  (srcp->crop_top >> vshift) * dst->linesize[i];
1657  dst->data[i] += off;
1658  }
1659  return 0;
1660 }
1661 
1662 static int is_extra(const uint8_t *buf, int buf_size)
1663 {
1664  int cnt= buf[5]&0x1f;
1665  const uint8_t *p= buf+6;
1666  while(cnt--){
1667  int nalsize= AV_RB16(p) + 2;
1668  if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
1669  return 0;
1670  p += nalsize;
1671  }
1672  cnt = *(p++);
1673  if(!cnt)
1674  return 0;
1675  while(cnt--){
1676  int nalsize= AV_RB16(p) + 2;
1677  if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
1678  return 0;
1679  p += nalsize;
1680  }
1681  return 1;
1682 }
1683 
1684 static int h264_decode_frame(AVCodecContext *avctx, void *data,
1685  int *got_frame, AVPacket *avpkt)
1686 {
1687  const uint8_t *buf = avpkt->data;
1688  int buf_size = avpkt->size;
1689  H264Context *h = avctx->priv_data;
1690  AVFrame *pict = data;
1691  int buf_index = 0;
1692  H264Picture *out;
1693  int i, out_idx;
1694  int ret;
1695 
1696  h->flags = avctx->flags;
1697  h->setup_finished = 0;
1698 
1699  if (h->backup_width != -1) {
1700  avctx->width = h->backup_width;
1701  h->backup_width = -1;
1702  }
1703  if (h->backup_height != -1) {
1704  avctx->height = h->backup_height;
1705  h->backup_height = -1;
1706  }
1707  if (h->backup_pix_fmt != AV_PIX_FMT_NONE) {
1708  avctx->pix_fmt = h->backup_pix_fmt;
1710  }
1711 
1713 
1714  /* end of stream, output what is still in the buffers */
1715  if (buf_size == 0) {
1716  out:
1717 
1718  h->cur_pic_ptr = NULL;
1719  h->first_field = 0;
1720 
1721  // FIXME factorize this with the output code below
1722  out = h->delayed_pic[0];
1723  out_idx = 0;
1724  for (i = 1;
1725  h->delayed_pic[i] &&
1726  !h->delayed_pic[i]->f->key_frame &&
1727  !h->delayed_pic[i]->mmco_reset;
1728  i++)
1729  if (h->delayed_pic[i]->poc < out->poc) {
1730  out = h->delayed_pic[i];
1731  out_idx = i;
1732  }
1733 
1734  for (i = out_idx; h->delayed_pic[i]; i++)
1735  h->delayed_pic[i] = h->delayed_pic[i + 1];
1736 
1737  if (out) {
1738  out->reference &= ~DELAYED_PIC_REF;
1739  ret = output_frame(h, pict, out);
1740  if (ret < 0)
1741  return ret;
1742  *got_frame = 1;
1743  }
1744 
1745  return buf_index;
1746  }
1748  int side_size;
1749  uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
1750  if (is_extra(side, side_size))
1751  ff_h264_decode_extradata(h, side, side_size);
1752  }
1753  if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
1754  if (is_extra(buf, buf_size))
1755  return ff_h264_decode_extradata(h, buf, buf_size);
1756  }
1757 
1758  buf_index = decode_nal_units(h, buf, buf_size, 0);
1759  if (buf_index < 0)
1760  return AVERROR_INVALIDDATA;
1761 
1762  if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
1763  av_assert0(buf_index <= buf_size);
1764  goto out;
1765  }
1766 
1767  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
1768  if (avctx->skip_frame >= AVDISCARD_NONREF ||
1769  buf_size >= 4 && !memcmp("Q264", buf, 4))
1770  return buf_size;
1771  av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1772  return AVERROR_INVALIDDATA;
1773  }
1774 
1775  if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
1776  (h->mb_y >= h->mb_height && h->mb_height)) {
1777  if (avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)
1778  decode_postinit(h, 1);
1779 
1780  if ((ret = ff_h264_field_end(h, &h->slice_ctx[0], 0)) < 0)
1781  return ret;
1782 
1783  /* Wait for second field. */
1784  *got_frame = 0;
1785  if (h->next_output_pic && ((avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||
1786  (avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL) ||
1787  h->next_output_pic->recovered)) {
1788  if (!h->next_output_pic->recovered)
1790 
1791  if (!h->avctx->hwaccel &&
1792  (h->next_output_pic->field_poc[0] == INT_MAX ||
1793  h->next_output_pic->field_poc[1] == INT_MAX)
1794  ) {
1795  int p;
1796  AVFrame *f = h->next_output_pic->f;
1797  int field = h->next_output_pic->field_poc[0] == INT_MAX;
1798  uint8_t *dst_data[4];
1799  int linesizes[4];
1800  const uint8_t *src_data[4];
1801 
1802  av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
1803 
1804  for (p = 0; p<4; p++) {
1805  dst_data[p] = f->data[p] + (field^1)*f->linesize[p];
1806  src_data[p] = f->data[p] + field *f->linesize[p];
1807  linesizes[p] = 2*f->linesize[p];
1808  }
1809 
1810  av_image_copy(dst_data, linesizes, src_data, linesizes,
1811  f->format, f->width, f->height>>1);
1812  }
1813 
1814  ret = output_frame(h, pict, h->next_output_pic);
1815  if (ret < 0)
1816  return ret;
1817  *got_frame = 1;
1818  if (CONFIG_MPEGVIDEO) {
1819  ff_print_debug_info2(h->avctx, pict, NULL,
1823  &h->low_delay,
1824  h->mb_width, h->mb_height, h->mb_stride, 1);
1825  }
1826  }
1827  }
1828 
1829  av_assert0(pict->buf[0] || !*got_frame);
1830 
1832 
1833  return get_consumed_bytes(buf_index, buf_size);
1834 }
1835 
1837 {
1838  int i;
1839 
1841 
1842  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
1843  ff_h264_unref_picture(h, &h->DPB[i]);
1844  av_frame_free(&h->DPB[i].f);
1845  }
1846  memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
1847 
1848  h->cur_pic_ptr = NULL;
1849 
1850  for (i = 0; i < h->nb_slice_ctx; i++)
1851  av_freep(&h->slice_ctx[i].rbsp_buffer);
1852  av_freep(&h->slice_ctx);
1853  h->nb_slice_ctx = 0;
1854 
1855  h->a53_caption_size = 0;
1856  av_freep(&h->a53_caption);
1857 
1858  for (i = 0; i < MAX_SPS_COUNT; i++)
1859  av_freep(h->sps_buffers + i);
1860 
1861  for (i = 0; i < MAX_PPS_COUNT; i++)
1862  av_freep(h->pps_buffers + i);
1863 }
1864 
1866 {
1867  H264Context *h = avctx->priv_data;
1868 
1871 
1873  av_frame_free(&h->cur_pic.f);
1876 
1877  return 0;
1878 }
1879 
1880 #define OFFSET(x) offsetof(H264Context, x)
1881 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1882 static const AVOption h264_options[] = {
1883  {"is_avc", "is avc", offsetof(H264Context, is_avc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
1884  {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
1885  { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
1886  { NULL },
1887 };
1888 
1889 static const AVClass h264_class = {
1890  .class_name = "H264 Decoder",
1891  .item_name = av_default_item_name,
1892  .option = h264_options,
1893  .version = LIBAVUTIL_VERSION_INT,
1894 };
1895 
1897  .name = "h264",
1898  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1899  .type = AVMEDIA_TYPE_VIDEO,
1900  .id = AV_CODEC_ID_H264,
1901  .priv_data_size = sizeof(H264Context),
1903  .close = h264_decode_end,
1905  .capabilities = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
1908  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1909  .flush = flush_dpb,
1910  .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1911  .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
1913  .priv_class = &h264_class,
1914 };
1915 
1916 #if CONFIG_H264_VDPAU_DECODER && FF_API_VDPAU
1917 static const AVClass h264_vdpau_class = {
1918  .class_name = "H264 VDPAU Decoder",
1919  .item_name = av_default_item_name,
1920  .option = h264_options,
1921  .version = LIBAVUTIL_VERSION_INT,
1922 };
1923 
1924 AVCodec ff_h264_vdpau_decoder = {
1925  .name = "h264_vdpau",
1926  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
1927  .type = AVMEDIA_TYPE_VIDEO,
1928  .id = AV_CODEC_ID_H264,
1929  .priv_data_size = sizeof(H264Context),
1931  .close = h264_decode_end,
1934  .flush = flush_dpb,
1935  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
1936  AV_PIX_FMT_NONE},
1938  .priv_class = &h264_vdpau_class,
1939 };
1940 #endif
struct H264Context * h264
Definition: h264.h:369
#define ff_tlog(ctx,...)
Definition: internal.h:65
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:1648
void ff_h264_flush_change(H264Context *h)
Definition: h264.c:974
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:3714
int workaround_bugs
Definition: h264.h:550
#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:1267
GetBitContext gb
Definition: h264.h:520
int sei_recovery_frame_cnt
Definition: h264.h:350
#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:2183
enum AVPixelFormat backup_pix_fmt
Definition: h264.h:542
int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
Call decode_slice() for each context.
Definition: h264_slice.c:2495
5: top field, bottom field, top field repeated, in that order
Definition: h264.h:154
int low_delay
Definition: h264.h:546
int mb_num
Definition: h264.h:617
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
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
Definition: h264.h:482
AVOption.
Definition: opt.h:245
static const AVClass h264_class
Definition: h264.c:1889
int delta_poc[2]
Definition: h264.h:645
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
int quincunx_subsampling
Definition: h264.h:729
int edge_emu_buffer_allocated
Definition: h264.h:470
Definition: h264.h:121
int a53_caption_size
Definition: h264.h:743
3: top field, bottom field, in that order
Definition: h264.h:152
#define H264_MAX_PICTURE_COUNT
Definition: h264.h:48
static int decode_extradata_ps_mp4(H264Context *h, const uint8_t *buf, int buf_size)
Definition: h264.c:441
int first_field
Definition: h264.h:587
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
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint16_t * cbp_table
Definition: h264.h:592
av_cold int ff_h264_decode_init(AVCodecContext *avctx)
Definition: h264.c:608
const uint8_t * ff_h264_decode_nal(H264Context *h, H264SliceContext *sl, const uint8_t *src, int *dst_length, int *consumed, int length)
Decode a network abstraction layer unit.
Definition: h264.c:136
7: frame doubling
Definition: h264.h:156
void ff_er_frame_end(ERContext *s)
#define MAX_PPS_COUNT
Definition: h264.h:52
Sequence parameter set.
Definition: h264.h:176
int mb_y
Definition: h264.h:614
int bitstream_restriction_flag
Definition: h264.h:216
#define FF_PROFILE_H264_INTRA
Definition: avcodec.h:3173
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:247
#define FMO
Definition: h264.h:64
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:469
static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
Definition: h264.c:1203
int size
Definition: avcodec.h:1575
#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:825
int crop
Definition: h264.h:352
void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl, int y, int height)
Definition: h264.c:101
int16_t(*[2] motion_val)[2]
Definition: h264.h:323
int flags
Definition: h264.h:549
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1867
int mb_height
Definition: h264.h:615
H264Picture * delayed_pic[MAX_DELAYED_PIC_COUNT+2]
Definition: h264.h:666
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:628
AVBufferPool * ref_index_pool
Definition: h264.h:827
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:120
void ff_h264_free_tables(H264Context *h)
Definition: h264.c:265
ERPicture last_pic
int ff_h264_get_profile(SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
Definition: h264.c:1108
int sei_reguserdata_afd_present
User data registered by Rec.
Definition: h264.h:741
void ff_h264_decode_init_vlc(void)
Definition: h264_cavlc.c:327
H264Context.
Definition: h264.h:513
AVFrame * f
Definition: h264.h:316
#define AV_CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries...
Definition: avcodec.h:911
int prev_poc_msb
poc_msb of the last reference pic for POC type 0
Definition: h264.h:647
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:153
#define FRAME_RECOVERED_IDR
We have seen an IDR, so all the following frames in coded order are correctly decodable.
Definition: h264.h:791
Views are next to each other.
Definition: stereo3d.h:45
AVCodec.
Definition: avcodec.h:3531
int picture_structure
Definition: h264.h:586
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:178
unsigned current_sps_id
id of the current SPS
Definition: h264.h:571
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:456
int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
Definition: h264.c:59
int ff_set_ref_count(H264Context *h, H264SliceContext *sl)
Definition: h264.c:1128
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:3185
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1776
uint8_t * chroma_pred_mode_table
Definition: h264.h:595
int setup_finished
Definition: h264.h:808
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:3254
#define AV_RN32A(p)
Definition: intreadwrite.h:526
BYTE int const BYTE * srcp
Definition: avisynth_c.h:676
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2958
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:975
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:3175
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:579
#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:744
uint8_t
#define av_cold
Definition: attributes.h:82
int prev_frame_num_offset
for POC type 2
Definition: h264.h:650
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:186
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:548
int poc
frame POC
Definition: h264.h:335
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVCodec ff_h264_decoder
Definition: h264.c:1896
Multithreading support functions.
static int find_start_code(const uint8_t *buf, int buf_size, int buf_index, int next_avc)
Definition: h264.h:1154
Definition: h264.h:115
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:1754
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:468
ERPicture cur_pic
int frame_recovered
Initial frame has been completely recovered.
Definition: h264.h:798
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:1574
static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
Identify the exact end of the bitstream.
Definition: h264.c:250
#define AV_CODEC_CAP_HWACCEL_VDPAU
Codec can export data for HW decoding (VDPAU).
Definition: avcodec.h:986
#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:56
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:365
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:348
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:2382
#define av_log(a,...)
int sei_vflip
Definition: h264.h:736
unsigned int rbsp_buffer_size
Definition: h264.h:507
int last_pocs[MAX_DELAYED_PIC_COUNT]
Definition: h264.h:667
H.264 / AVC / MPEG4 part10 codec.
Definition: h264.h:122
int frame_num
Definition: h264.h:646
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:522
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:1961
int flags
Additional information about the frame packing.
Definition: stereo3d.h:132
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
Definition: golomb.h:53
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:1618
int16_t * dc_val_base
Definition: h264.h:464
int poc_type
pic_order_cnt_type
Definition: h264.h:183
int context_initialized
Definition: h264.h:548
ERContext er
Definition: h264.h:371
int nal_unit_type
Definition: h264.h:623
av_default_item_name
int num_reorder_frames
Definition: h264.h:217
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:541
#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:3088
int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
Definition: h264.c:1023
const char * r
Definition: vf_curves.c:107
int backup_width
Backup frame properties: needed, because they can be different between returned frame and last decode...
Definition: h264.h:540
static void flush_dpb(AVCodecContext *avctx)
Definition: h264.c:1003
int capabilities
Codec capabilities.
Definition: avcodec.h:3550
#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:1882
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)
PPS pps
current pps
Definition: h264.h:573
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:596
#define FF_PROFILE_H264_HIGH_422
Definition: avcodec.h:3182
int prev_interlaced_frame
Complement sei_pic_struct SEI_PIC_STRUCT_TOP_BOTTOM and SEI_PIC_STRUCT_BOTTOM_TOP indicate interlaced...
Definition: h264.h:721
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1734
ThreadFrame tf
Definition: h264.h:317
0: frame
Definition: h264.h:149
simple assert() macros that are a bit more flexible than ISO C assert().
#define PICT_TOP_FIELD
Definition: mpegutils.h:37
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3538
int direct_spatial_mv_pred
Definition: h264.h:440
static AVOnce h264_vlc_init
Definition: h264.c:606
static av_always_inline uint32_t pack16to32(unsigned a, unsigned b)
Definition: h264.h:998
int valid_recovery_point
Are the SEI recovery points looking valid.
Definition: h264.h:775
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:589
#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:1013
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:233
int * mb_index2xy
int offset_for_top_to_bottom_field
Definition: h264.h:187
#define FIELD_OR_MBAFF_PICTURE(h)
Definition: h264.h:93
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition: h264.h:375
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:982
#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:353
uint8_t * error_status_table
uint8_t * direct_table
Definition: h264.h:597
int nal_length_size
Number of bytes used for nal length (1, 2 or 4)
Definition: h264.h:629
useful rectangle filling function
uint8_t * data[3]
Definition: h264.h:358
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:65
Views are packed per line, as if interlaced.
Definition: stereo3d.h:97
int sei_anticlockwise_rotation
Definition: h264.h:735
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:1900
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:254
int frame_num_offset
for POC type 2
Definition: h264.h:649
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:2926
FPA sei_fpa
Definition: h264.h:777
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int x264_build
Definition: h264.h:612
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:3080
uint32_t * mb2br_xy
Definition: h264.h:567
uint8_t * er_temp_buffer
#define OFFSET(x)
Definition: h264.c:1880
#define FFMIN(a, b)
Definition: common.h:96
uint16_t * slice_table
slice_table_base + 2*mb_stride + 1
Definition: h264.h:582
#define H264_MAX_THREADS
Definition: h264.h:49
int poc_cycle_length
num_ref_frames_in_pic_order_cnt_cycle
Definition: h264.h:188
int reference
Definition: h264.h:347
#define FIELD_PICTURE(h)
Definition: h264.h:76
int sei_frame_packing_present
frame_packing_arrangment SEI message
Definition: h264.h:726
int width
picture width / height.
Definition: avcodec.h:1826
int redundant_pic_count
Definition: h264.h:433
int nb_slice_ctx
Definition: h264.h:528
uint32_t * mb_type
Definition: h264.h:326
#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:572
int32_t
PPS * pps_buffers[MAX_PPS_COUNT]
Definition: h264.h:635
int sei_hflip
Definition: h264.h:736
#define MAX_SPS_COUNT
Definition: h264.h:51
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:3081
Context Adaptive Binary Arithmetic Coder inline functions.
int mmco_reset
Definition: h264.h:676
H264SliceContext * slice_ctx
Definition: h264.h:527
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2937
int poc_lsb
Definition: h264.h:642
int reference
Definition: h264.h:361
static int h264_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: h264.c:1684
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1785
int top_borders_allocated[2]
Definition: h264.h:471
uint8_t active_format_description
Definition: h264.h:742
#define src
Definition: vp9dsp.c:530
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264.c:1628
Definition: h264.h:120
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:114
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:558
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:3069
uint8_t * edge_emu_buffer
Definition: h264.h:467
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:637
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:1017
SPS * sps_buffers[MAX_SPS_COUNT]
Definition: h264.h:634
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:215
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:369
int mb_stride
Definition: h264.h:616
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVCodecContext * avctx
Definition: h264.h:515
#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
static int get_bit_length(H264Context *h, const uint8_t *buf, const uint8_t *ptr, int dst_length, int i, int next_avc)
Definition: h264.c:1184
1: top field
Definition: h264.h:150
enum AVCodecID codec_id
Definition: avcodec.h:1656
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:651
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:669
int ff_h264_decode_sei(H264Context *h)
Decode SEI.
Definition: h264_sei.c:421
int poc_msb
Definition: h264.h:643
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:334
#define AV_CODEC_FLAG2_FAST
Allow non spec compliant speedup tricks.
Definition: avcodec.h:892
int debug
debug
Definition: avcodec.h:2878
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:696
int recovery_frame
recovery_frame is the frame_num at which the next frame should be fully constructed.
Definition: h264.h:785
main external API structure.
Definition: avcodec.h:1639
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
static void decode_postinit(H264Context *h, int setup_finished)
Run setup operations that must be run after slice header decoding.
Definition: h264.c:696
int ff_h264_alloc_tables(H264Context *h)
Allocate tables.
Definition: h264.c:308
2: bottom field
Definition: h264.h:151
uint8_t * data
Definition: frame.h:145
void * buf
Definition: avisynth_c.h:553
int frame_packing_arrangement_type
Definition: h264.h:727
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:320
int extradata_size
Definition: avcodec.h:1755
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:232
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:312
SEI_PicStructType sei_pic_struct
pic_struct in picture timing SEI message
Definition: h264.h:713
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
int ff_h264_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: h264_slice.c:393
int slice_flags
slice flags
Definition: avcodec.h:2181
static int get_avc_nalsize(H264Context *h, const uint8_t *buf, int buf_size, int *buf_index)
Definition: h264.h:1164
Describe the class of an AVClass context structure.
Definition: log.h:67
#define FF_CODEC_PROPERTY_CLOSED_CAPTIONS
Definition: avcodec.h:3462
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:477
Definition: h264.h:117
static int h264_decode_end(AVCodecContext *avctx)
Definition: h264.c:1865
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:274
int8_t * ref_index[2]
Definition: h264.h:332
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:418
int pixel_shift
0 for 8-bit H264, 1 for high-bit-depth H264
Definition: h264.h:530
int mmco_reset
MMCO_RESET set this 1.
Definition: h264.h:337
H264Picture * cur_pic_ptr
Definition: h264.h:523
int mb_mbaff
mb_aff_frame && mb_field_decoding_flag
Definition: h264.h:431
int enable_er
Definition: h264.h:822
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: avcodec.h:2858
int frame_packing_arrangement_cancel_flag
is previous arrangement canceled, -1 if never received
Definition: h264.h:270
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:184
6: bottom field, top field, bottom field repeated, in that order
Definition: h264.h:155
static int is_extra(const uint8_t *buf, int buf_size)
Definition: h264.c:1662
AVCodecContext * avctx
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
static const uint8_t start_code[]
Definition: h264.c:1182
mfxU16 profile
Definition: qsvenc.c:42
#define FF_BUG_AUTODETECT
autodetection
Definition: avcodec.h:2823
int linesize[3]
Definition: h264.h:359
int pic_struct_present_flag
Definition: h264.h:223
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:457
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:1836
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:194
int has_recovery_point
Definition: h264.h:800
#define MAX_MBPAIR_SIZE
Definition: h264.h:58
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:961
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:774
AVBufferPool * qscale_table_pool
Definition: h264.h:824
H264Picture * next_output_pic
Definition: h264.h:668
int slice_context_count
Definition: h264.h:698
AVBufferPool * motion_val_pool
Definition: h264.h:826
#define SLICE_SINGLETHREAD
Definition: h264.h:1196
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:486
#define FRAME_RECOVERED_SEI
Sufficient number of frames have been decoded since a SEI recovery point, so all the following frames...
Definition: h264.h:796
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:639
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:182
int missing_fields
Definition: h264.h:802
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:2822
const AVProfile ff_h264_profiles[]
Definition: profiles.c:49
int cur_chroma_format_idc
Definition: h264.h:814
int8_t * intra4x4_pred_mode
Definition: h264.h:397
unsigned properties
Definition: avcodec.h:3460
int den
denominator
Definition: rational.h:45
uint8_t * rbsp_buffer
Definition: h264.h:506
int sei_ct_type
Bit set of clock types for fields/frames in picture timing SEI message.
Definition: h264.h:751
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
Definition: h264.h:116
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:725
void * priv_data
Definition: avcodec.h:1681
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:154
#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:648
void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
Definition: h264_picture.c:131
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:2892
int8_t ref_cache[2][5 *8]
Definition: h264.h:483
#define AV_CODEC_FLAG_OUTPUT_CORRUPT
Output even those frames that might be corrupted.
Definition: avcodec.h:832
#define SLICE_SKIPED
Definition: h264.h:1197
#define VD
Definition: h264.c:1881
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:57
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1689
#define FF_BUG_TRUNCATED
Definition: avcodec.h:2842
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:161
H264Picture cur_pic
Definition: h264.h:524
int sei_display_orientation_present
display orientation SEI message
Definition: h264.h:734
Views are on top of each other.
Definition: stereo3d.h:55
int content_interpretation_type
Definition: h264.h:728
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:249
int mb_width
Definition: h264.h:615
enum AVPictureType pict_type
Definition: h264.h:706
int current_slice
current slice number, used to initialize slice_num of each thread/context
Definition: h264.h:688
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1741
uint32_t * mb2b_xy
Definition: h264.h:566
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264.h:458
int delta_poc_bottom
Definition: h264.h:644
H264Picture last_pic_for_ec
Definition: h264.h:525
int au_pps_id
pps_id of current access unit
Definition: h264.h:575
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:354
#define AV_CODEC_FLAG2_SHOW_ALL
Show all frames before the first keyframe.
Definition: avcodec.h:920
uint8_t(*[2] mvd_table)[2]
Definition: h264.h:496
Definition: h264.h:119
#define av_freep(p)
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:552
#define FF_PROFILE_H264_CONSTRAINED
Definition: avcodec.h:3172
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:3728
8: frame tripling
Definition: h264.h:157
#define AV_RN64A(p)
Definition: intreadwrite.h:530
int mb_field_decoding_flag
Definition: h264.h:430
uint8_t(* non_zero_count)[48]
Definition: h264.h:555
#define FF_PROFILE_H264_HIGH_10
Definition: avcodec.h:3180
exp golomb vlc stuff
uint8_t * bipred_scratchpad
Definition: h264.h:466
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1551
int sei_recovery_frame_cnt
recovery_frame_cnt from SEI message
Definition: h264.h:770
int droppable
Definition: h264.h:544
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:950
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2856
#define STARTCODE_TEST
int nal_ref_idc
Definition: h264.h:622
GetBitContext gb
Definition: h264.h:370
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:568
Context Adaptive Binary Arithmetic Coder.
void ff_h264_init_dequant_tables(H264Context *h)
Definition: h264_slice.c:327