FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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 "cabac.h"
38 #include "cabac_functions.h"
39 #include "error_resilience.h"
40 #include "avcodec.h"
41 #include "h264.h"
42 #include "h264data.h"
43 #include "h264chroma.h"
44 #include "h264_mvpred.h"
45 #include "golomb.h"
46 #include "mathops.h"
47 #include "me_cmp.h"
48 #include "mpegutils.h"
49 #include "rectangle.h"
50 #include "svq3.h"
51 #include "thread.h"
52 #include "vdpau_internal.h"
53 
54 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
55 
57 {
58  H264Context *h = avctx->priv_data;
59  return h ? h->sps.num_reorder_frames : 0;
60 }
61 
62 static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
63  int (*mv)[2][4][2],
64  int mb_x, int mb_y, int mb_intra, int mb_skipped)
65 {
66  H264Context *h = opaque;
67 
68  h->mb_x = mb_x;
69  h->mb_y = mb_y;
70  h->mb_xy = mb_x + mb_y * h->mb_stride;
71  memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
72  av_assert1(ref >= 0);
73  /* FIXME: It is possible albeit uncommon that slice references
74  * differ between slices. We take the easy approach and ignore
75  * it for now. If this turns out to have any relevance in
76  * practice then correct remapping should be added. */
77  if (ref >= h->ref_count[0])
78  ref = 0;
79  if (!h->ref_list[0][ref].f.data[0]) {
80  av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
81  ref = 0;
82  }
83  if ((h->ref_list[0][ref].reference&3) != 3) {
84  av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
85  return;
86  }
87  fill_rectangle(&h->cur_pic.ref_index[0][4 * h->mb_xy],
88  2, 2, 2, ref, 1);
89  fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
90  fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
91  pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
92  h->mb_mbaff =
95 }
96 
98 {
99  AVCodecContext *avctx = h->avctx;
100  AVFrame *cur = &h->cur_pic.f;
101  AVFrame *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0].f : NULL;
102  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
103  int vshift = desc->log2_chroma_h;
104  const int field_pic = h->picture_structure != PICT_FRAME;
105  if (field_pic) {
106  height <<= 1;
107  y <<= 1;
108  }
109 
110  height = FFMIN(height, avctx->height - y);
111 
112  if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
113  return;
114 
115  if (avctx->draw_horiz_band) {
116  AVFrame *src;
118  int i;
119 
120  if (cur->pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
122  src = cur;
123  else if (last)
124  src = last;
125  else
126  return;
127 
128  offset[0] = y * src->linesize[0];
129  offset[1] =
130  offset[2] = (y >> vshift) * src->linesize[1];
131  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
132  offset[i] = 0;
133 
134  emms_c();
135 
136  avctx->draw_horiz_band(avctx, src, offset,
137  y, h->picture_structure, height);
138  }
139 }
140 
141 /**
142  * Check if the top & left blocks are available if needed and
143  * change the dc mode so it only uses the available blocks.
144  */
146 {
147  static const int8_t top[12] = {
148  -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
149  };
150  static const int8_t left[12] = {
151  0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
152  };
153  int i;
154 
155  if (!(h->top_samples_available & 0x8000)) {
156  for (i = 0; i < 4; i++) {
157  int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
158  if (status < 0) {
160  "top block unavailable for requested intra4x4 mode %d at %d %d\n",
161  status, h->mb_x, h->mb_y);
162  return AVERROR_INVALIDDATA;
163  } else if (status) {
164  h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
165  }
166  }
167  }
168 
169  if ((h->left_samples_available & 0x8888) != 0x8888) {
170  static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
171  for (i = 0; i < 4; i++)
172  if (!(h->left_samples_available & mask[i])) {
173  int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
174  if (status < 0) {
176  "left block unavailable for requested intra4x4 mode %d at %d %d\n",
177  status, h->mb_x, h->mb_y);
178  return AVERROR_INVALIDDATA;
179  } else if (status) {
180  h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
181  }
182  }
183  }
184 
185  return 0;
186 } // FIXME cleanup like ff_h264_check_intra_pred_mode
187 
188 /**
189  * Check if the top & left blocks are available if needed and
190  * change the dc mode so it only uses the available blocks.
191  */
192 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
193 {
194  static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
195  static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
196 
197  if (mode > 3U) {
199  "out of range intra chroma pred mode at %d %d\n",
200  h->mb_x, h->mb_y);
201  return AVERROR_INVALIDDATA;
202  }
203 
204  if (!(h->top_samples_available & 0x8000)) {
205  mode = top[mode];
206  if (mode < 0) {
208  "top block unavailable for requested intra mode at %d %d\n",
209  h->mb_x, h->mb_y);
210  return AVERROR_INVALIDDATA;
211  }
212  }
213 
214  if ((h->left_samples_available & 0x8080) != 0x8080) {
215  mode = left[mode];
216  if (mode < 0) {
218  "left block unavailable for requested intra mode at %d %d\n",
219  h->mb_x, h->mb_y);
220  return AVERROR_INVALIDDATA;
221  }
222  if (is_chroma && (h->left_samples_available & 0x8080)) {
223  // mad cow disease mode, aka MBAFF + constrained_intra_pred
224  mode = ALZHEIMER_DC_L0T_PRED8x8 +
225  (!(h->left_samples_available & 0x8000)) +
226  2 * (mode == DC_128_PRED8x8);
227  }
228  }
229 
230  return mode;
231 }
232 
234  int *dst_length, int *consumed, int length)
235 {
236  int i, si, di;
237  uint8_t *dst;
238  int bufidx;
239 
240  // src[0]&0x80; // forbidden bit
241  h->nal_ref_idc = src[0] >> 5;
242  h->nal_unit_type = src[0] & 0x1F;
243 
244  src++;
245  length--;
246 
247 #define STARTCODE_TEST \
248  if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
249  if (src[i + 2] != 3 && src[i + 2] != 0) { \
250  /* startcode, so we must be past the end */ \
251  length = i; \
252  } \
253  break; \
254  }
255 
256 #if HAVE_FAST_UNALIGNED
257 #define FIND_FIRST_ZERO \
258  if (i > 0 && !src[i]) \
259  i--; \
260  while (src[i]) \
261  i++
262 
263 #if HAVE_FAST_64BIT
264  for (i = 0; i + 1 < length; i += 9) {
265  if (!((~AV_RN64A(src + i) &
266  (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
267  0x8000800080008080ULL))
268  continue;
269  FIND_FIRST_ZERO;
271  i -= 7;
272  }
273 #else
274  for (i = 0; i + 1 < length; i += 5) {
275  if (!((~AV_RN32A(src + i) &
276  (AV_RN32A(src + i) - 0x01000101U)) &
277  0x80008080U))
278  continue;
279  FIND_FIRST_ZERO;
281  i -= 3;
282  }
283 #endif
284 #else
285  for (i = 0; i + 1 < length; i += 2) {
286  if (src[i])
287  continue;
288  if (i > 0 && src[i - 1] == 0)
289  i--;
291  }
292 #endif
293 
294  // use second escape buffer for inter data
295  bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
296 
297  av_fast_padded_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+MAX_MBPAIR_SIZE);
298  dst = h->rbsp_buffer[bufidx];
299 
300  if (!dst)
301  return NULL;
302 
303  if(i>=length-1){ //no escaped 0
304  *dst_length= length;
305  *consumed= length+1; //+1 for the header
306  if(h->avctx->flags2 & CODEC_FLAG2_FAST){
307  return src;
308  }else{
309  memcpy(dst, src, length);
310  return dst;
311  }
312  }
313 
314  memcpy(dst, src, i);
315  si = di = i;
316  while (si + 2 < length) {
317  // remove escapes (very rare 1:2^22)
318  if (src[si + 2] > 3) {
319  dst[di++] = src[si++];
320  dst[di++] = src[si++];
321  } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
322  if (src[si + 2] == 3) { // escape
323  dst[di++] = 0;
324  dst[di++] = 0;
325  si += 3;
326  continue;
327  } else // next start code
328  goto nsc;
329  }
330 
331  dst[di++] = src[si++];
332  }
333  while (si < length)
334  dst[di++] = src[si++];
335 
336 nsc:
337  memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
338 
339  *dst_length = di;
340  *consumed = si + 1; // +1 for the header
341  /* FIXME store exact number of bits in the getbitcontext
342  * (it is needed for decoding) */
343  return dst;
344 }
345 
346 /**
347  * Identify the exact end of the bitstream
348  * @return the length of the trailing, or 0 if damaged
349  */
351 {
352  int v = *src;
353  int r;
354 
355  tprintf(h->avctx, "rbsp trailing %X\n", v);
356 
357  for (r = 1; r < 9; r++) {
358  if (v & 1)
359  return r;
360  v >>= 1;
361  }
362  return 0;
363 }
364 
365 void ff_h264_free_tables(H264Context *h, int free_rbsp)
366 {
367  int i;
368  H264Context *hx;
369 
372  av_freep(&h->cbp_table);
373  av_freep(&h->mvd_table[0]);
374  av_freep(&h->mvd_table[1]);
375  av_freep(&h->direct_table);
378  h->slice_table = NULL;
379  av_freep(&h->list_counts);
380 
381  av_freep(&h->mb2b_xy);
382  av_freep(&h->mb2br_xy);
383 
388 
389  if (free_rbsp && h->DPB) {
390  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
391  ff_h264_unref_picture(h, &h->DPB[i]);
392  memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
393  av_freep(&h->DPB);
394  } else if (h->DPB) {
395  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
396  h->DPB[i].needs_realloc = 1;
397  }
398 
399  h->cur_pic_ptr = NULL;
400 
401  for (i = 0; i < H264_MAX_THREADS; i++) {
402  hx = h->thread_context[i];
403  if (!hx)
404  continue;
405  av_freep(&hx->top_borders[1]);
406  av_freep(&hx->top_borders[0]);
409  av_freep(&hx->dc_val_base);
410  av_freep(&hx->er.mb_index2xy);
412  av_freep(&hx->er.er_temp_buffer);
413  av_freep(&hx->er.mbintra_table);
414  av_freep(&hx->er.mbskip_table);
415 
416  if (free_rbsp) {
417  av_freep(&hx->rbsp_buffer[1]);
418  av_freep(&hx->rbsp_buffer[0]);
419  hx->rbsp_buffer_size[0] = 0;
420  hx->rbsp_buffer_size[1] = 0;
421  }
422  if (i)
423  av_freep(&h->thread_context[i]);
424  }
425 }
426 
428 {
429  const int big_mb_num = h->mb_stride * (h->mb_height + 1);
430  const int row_mb_num = 2*h->mb_stride*FFMAX(h->avctx->thread_count, 1);
431  int x, y, i;
432 
434  row_mb_num, 8 * sizeof(uint8_t), fail)
436  big_mb_num * 48 * sizeof(uint8_t), fail)
438  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
440  big_mb_num * sizeof(uint16_t), fail)
442  big_mb_num * sizeof(uint8_t), fail)
444  row_mb_num, 16 * sizeof(uint8_t), fail);
446  row_mb_num, 16 * sizeof(uint8_t), fail);
448  4 * big_mb_num * sizeof(uint8_t), fail);
450  big_mb_num * sizeof(uint8_t), fail)
451 
452  memset(h->slice_table_base, -1,
453  (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
454  h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
455 
457  big_mb_num * sizeof(uint32_t), fail);
459  big_mb_num * sizeof(uint32_t), fail);
460  for (y = 0; y < h->mb_height; y++)
461  for (x = 0; x < h->mb_width; x++) {
462  const int mb_xy = x + y * h->mb_stride;
463  const int b_xy = 4 * x + 4 * y * h->b_stride;
464 
465  h->mb2b_xy[mb_xy] = b_xy;
466  h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
467  }
468 
469  if (!h->dequant4_coeff[0])
471 
472  if (!h->DPB) {
473  h->DPB = av_mallocz_array(H264_MAX_PICTURE_COUNT, sizeof(*h->DPB));
474  if (!h->DPB)
475  goto fail;
476  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
477  av_frame_unref(&h->DPB[i].f);
478  av_frame_unref(&h->cur_pic.f);
479  }
480 
481  return 0;
482 
483 fail:
484  ff_h264_free_tables(h, 1);
485  return AVERROR(ENOMEM);
486 }
487 
488 /**
489  * Init context
490  * Allocate buffers which are not shared amongst multiple threads.
491  */
493 {
494  ERContext *er = &h->er;
495  int mb_array_size = h->mb_height * h->mb_stride;
496  int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
497  int c_size = h->mb_stride * (h->mb_height + 1);
498  int yc_size = y_size + 2 * c_size;
499  int x, y, i;
500 
502  h->mb_width, 16 * 3 * sizeof(uint8_t) * 2, fail)
504  h->mb_width, 16 * 3 * sizeof(uint8_t) * 2, fail)
505 
506  h->ref_cache[0][scan8[5] + 1] =
507  h->ref_cache[0][scan8[7] + 1] =
508  h->ref_cache[0][scan8[13] + 1] =
509  h->ref_cache[1][scan8[5] + 1] =
510  h->ref_cache[1][scan8[7] + 1] =
511  h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
512 
514  /* init ER */
515  er->avctx = h->avctx;
517  er->opaque = h;
518  er->quarter_sample = 1;
519 
520  er->mb_num = h->mb_num;
521  er->mb_width = h->mb_width;
522  er->mb_height = h->mb_height;
523  er->mb_stride = h->mb_stride;
524  er->b8_stride = h->mb_width * 2 + 1;
525 
526  // error resilience code looks cleaner with this
528  (h->mb_num + 1) * sizeof(int), fail);
529 
530  for (y = 0; y < h->mb_height; y++)
531  for (x = 0; x < h->mb_width; x++)
532  er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
533 
534  er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
535  h->mb_stride + h->mb_width;
536 
538  mb_array_size * sizeof(uint8_t), fail);
539 
540  FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
541  memset(er->mbintra_table, 1, mb_array_size);
542 
543  FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
544 
546  h->mb_height * h->mb_stride, fail);
547 
549  yc_size * sizeof(int16_t), fail);
550  er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
551  er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
552  er->dc_val[2] = er->dc_val[1] + c_size;
553  for (i = 0; i < yc_size; i++)
554  h->dc_val_base[i] = 1024;
555  }
556 
557  return 0;
558 
559 fail:
560  return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
561 }
562 
563 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
564  int parse_extradata);
565 
567 {
568  AVCodecContext *avctx = h->avctx;
569  int ret;
570 
571  if (!buf || size <= 0)
572  return -1;
573 
574  if (buf[0] == 1) {
575  int i, cnt, nalsize;
576  const unsigned char *p = buf;
577 
578  h->is_avc = 1;
579 
580  if (size < 7) {
581  av_log(avctx, AV_LOG_ERROR,
582  "avcC %d too short\n", size);
583  return AVERROR_INVALIDDATA;
584  }
585  /* sps and pps in the avcC always have length coded with 2 bytes,
586  * so put a fake nal_length_size = 2 while parsing them */
587  h->nal_length_size = 2;
588  // Decode sps from avcC
589  cnt = *(p + 5) & 0x1f; // Number of sps
590  p += 6;
591  for (i = 0; i < cnt; i++) {
592  nalsize = AV_RB16(p) + 2;
593  if(nalsize > size - (p-buf))
594  return AVERROR_INVALIDDATA;
595  ret = decode_nal_units(h, p, nalsize, 1);
596  if (ret < 0) {
597  av_log(avctx, AV_LOG_ERROR,
598  "Decoding sps %d from avcC failed\n", i);
599  return ret;
600  }
601  p += nalsize;
602  }
603  // Decode pps from avcC
604  cnt = *(p++); // Number of pps
605  for (i = 0; i < cnt; i++) {
606  nalsize = AV_RB16(p) + 2;
607  if(nalsize > size - (p-buf))
608  return AVERROR_INVALIDDATA;
609  ret = decode_nal_units(h, p, nalsize, 1);
610  if (ret < 0) {
611  av_log(avctx, AV_LOG_ERROR,
612  "Decoding pps %d from avcC failed\n", i);
613  return ret;
614  }
615  p += nalsize;
616  }
617  // Store right nal length size that will be used to parse all other nals
618  h->nal_length_size = (buf[4] & 0x03) + 1;
619  } else {
620  h->is_avc = 0;
621  ret = decode_nal_units(h, buf, size, 1);
622  if (ret < 0)
623  return ret;
624  }
625  return size;
626 }
627 
629 {
630  H264Context *h = avctx->priv_data;
631  int i;
632  int ret;
633 
634  h->avctx = avctx;
635 
636  h->bit_depth_luma = 8;
637  h->chroma_format_idc = 1;
638 
639  h->avctx->bits_per_raw_sample = 8;
640  h->cur_chroma_format_idc = 1;
641 
642  ff_h264dsp_init(&h->h264dsp, 8, 1);
645  ff_h264qpel_init(&h->h264qpel, 8);
646  ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
647 
648  h->dequant_coeff_pps = -1;
649  h->current_sps_id = -1;
650 
651  /* needed so that IDCT permutation is known early */
652  ff_videodsp_init(&h->vdsp, 8);
653 
654  memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
655  memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
656 
658  h->slice_context_count = 1;
659  h->workaround_bugs = avctx->workaround_bugs;
660  h->flags = avctx->flags;
661 
662  /* set defaults */
663  // s->decode_mb = ff_h263_decode_mb;
664  if (!avctx->has_b_frames)
665  h->low_delay = 1;
666 
668 
670 
672 
673  h->pixel_shift = 0;
674  h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
675 
676  h->thread_context[0] = h;
677  h->outputed_poc = h->next_outputed_poc = INT_MIN;
678  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
679  h->last_pocs[i] = INT_MIN;
680  h->prev_poc_msb = 1 << 16;
681  h->prev_frame_num = -1;
682  h->x264_build = -1;
685  if (avctx->codec_id == AV_CODEC_ID_H264) {
686  if (avctx->ticks_per_frame == 1) {
687  if(h->avctx->time_base.den < INT_MAX/2) {
688  h->avctx->time_base.den *= 2;
689  } else
690  h->avctx->time_base.num /= 2;
691  }
692  avctx->ticks_per_frame = 2;
693  }
694 
695  if (avctx->extradata_size > 0 && avctx->extradata) {
696  ret = ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
697  if (ret < 0) {
699  return ret;
700  }
701  }
702 
706  h->low_delay = 0;
707  }
708 
709  avctx->internal->allocate_progress = 1;
710 
712 
713  return 0;
714 }
715 
717 {
718  H264Context *h = avctx->priv_data;
719 
720  if (!avctx->internal->is_copy)
721  return 0;
722  memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
723  memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
724 
725  h->avctx = avctx;
726  h->rbsp_buffer[0] = NULL;
727  h->rbsp_buffer[1] = NULL;
728  h->rbsp_buffer_size[0] = 0;
729  h->rbsp_buffer_size[1] = 0;
730  h->context_initialized = 0;
731 
732  return 0;
733 }
734 
735 /**
736  * Run setup operations that must be run after slice header decoding.
737  * This includes finding the next displayed frame.
738  *
739  * @param h h264 master context
740  * @param setup_finished enough NALs have been read that we can call
741  * ff_thread_finish_setup()
742  */
743 static void decode_postinit(H264Context *h, int setup_finished)
744 {
746  H264Picture *cur = h->cur_pic_ptr;
747  int i, pics, out_of_order, out_idx;
748 
749  h->cur_pic_ptr->f.pict_type = h->pict_type;
750 
751  if (h->next_output_pic)
752  return;
753 
754  if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
755  /* FIXME: if we have two PAFF fields in one packet, we can't start
756  * the next thread here. If we have one field per packet, we can.
757  * The check in decode_nal_units() is not good enough to find this
758  * yet, so we assume the worst for now. */
759  // if (setup_finished)
760  // ff_thread_finish_setup(h->avctx);
761  if (cur->field_poc[0] == INT_MAX && cur->field_poc[1] == INT_MAX)
762  return;
763  if (h->avctx->hwaccel || h->missing_fields <=1)
764  return;
765  }
766 
767  cur->f.interlaced_frame = 0;
768  cur->f.repeat_pict = 0;
769 
770  /* Signal interlacing information externally. */
771  /* Prioritize picture timing SEI information over used
772  * decoding process if it exists. */
773 
774  if (h->sps.pic_struct_present_flag) {
775  switch (h->sei_pic_struct) {
777  break;
780  cur->f.interlaced_frame = 1;
781  break;
784  if (FIELD_OR_MBAFF_PICTURE(h))
785  cur->f.interlaced_frame = 1;
786  else
787  // try to flag soft telecine progressive
789  break;
792  /* Signal the possibility of telecined film externally
793  * (pic_struct 5,6). From these hints, let the applications
794  * decide if they apply deinterlacing. */
795  cur->f.repeat_pict = 1;
796  break;
798  cur->f.repeat_pict = 2;
799  break;
801  cur->f.repeat_pict = 4;
802  break;
803  }
804 
805  if ((h->sei_ct_type & 3) &&
807  cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
808  } else {
809  /* Derive interlacing flag from used decoding process. */
811  }
813 
814  if (cur->field_poc[0] != cur->field_poc[1]) {
815  /* Derive top_field_first from field pocs. */
816  cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
817  } else {
819  /* Use picture timing SEI information. Even if it is a
820  * information of a past frame, better than nothing. */
823  cur->f.top_field_first = 1;
824  else
825  cur->f.top_field_first = 0;
826  } else {
827  /* Most likely progressive */
828  cur->f.top_field_first = 0;
829  }
830  }
831 
832  if (h->sei_frame_packing_present &&
837  AVStereo3D *stereo = av_stereo3d_create_side_data(&cur->f);
838  if (stereo) {
839  switch (h->frame_packing_arrangement_type) {
840  case 0:
841  stereo->type = AV_STEREO3D_CHECKERBOARD;
842  break;
843  case 1:
844  stereo->type = AV_STEREO3D_COLUMNS;
845  break;
846  case 2:
847  stereo->type = AV_STEREO3D_LINES;
848  break;
849  case 3:
850  if (h->quincunx_subsampling)
852  else
853  stereo->type = AV_STEREO3D_SIDEBYSIDE;
854  break;
855  case 4:
856  stereo->type = AV_STEREO3D_TOPBOTTOM;
857  break;
858  case 5:
860  break;
861  case 6:
862  stereo->type = AV_STEREO3D_2D;
863  break;
864  }
865 
866  if (h->content_interpretation_type == 2)
867  stereo->flags = AV_STEREO3D_FLAG_INVERT;
868  }
869  }
870 
873  double angle = h->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
874  AVFrameSideData *rotation = av_frame_new_side_data(&cur->f,
876  sizeof(int32_t) * 9);
877  if (rotation) {
878  av_display_rotation_set((int32_t *)rotation->data, angle);
879  av_display_matrix_flip((int32_t *)rotation->data,
880  h->sei_hflip, h->sei_vflip);
881  }
882  }
883 
884  cur->mmco_reset = h->mmco_reset;
885  h->mmco_reset = 0;
886 
887  // FIXME do something with unavailable reference frames
888 
889  /* Sort B-frames into display order */
890 
894  h->low_delay = 0;
895  }
896 
900  h->low_delay = 0;
901  }
902 
903  for (i = 0; 1; i++) {
904  if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
905  if(i)
906  h->last_pocs[i-1] = cur->poc;
907  break;
908  } else if(i) {
909  h->last_pocs[i-1]= h->last_pocs[i];
910  }
911  }
912  out_of_order = MAX_DELAYED_PIC_COUNT - i;
913  if( cur->f.pict_type == AV_PICTURE_TYPE_B
915  out_of_order = FFMAX(out_of_order, 1);
916  if (out_of_order == MAX_DELAYED_PIC_COUNT) {
917  av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
918  for (i = 1; i < MAX_DELAYED_PIC_COUNT; i++)
919  h->last_pocs[i] = INT_MIN;
920  h->last_pocs[0] = cur->poc;
921  cur->mmco_reset = 1;
922  } else if(h->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
923  av_log(h->avctx, AV_LOG_VERBOSE, "Increasing reorder buffer to %d\n", out_of_order);
924  h->avctx->has_b_frames = out_of_order;
925  h->low_delay = 0;
926  }
927 
928  pics = 0;
929  while (h->delayed_pic[pics])
930  pics++;
931 
933 
934  h->delayed_pic[pics++] = cur;
935  if (cur->reference == 0)
936  cur->reference = DELAYED_PIC_REF;
937 
938  out = h->delayed_pic[0];
939  out_idx = 0;
940  for (i = 1; h->delayed_pic[i] &&
941  !h->delayed_pic[i]->f.key_frame &&
942  !h->delayed_pic[i]->mmco_reset;
943  i++)
944  if (h->delayed_pic[i]->poc < out->poc) {
945  out = h->delayed_pic[i];
946  out_idx = i;
947  }
948  if (h->avctx->has_b_frames == 0 &&
949  (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
950  h->next_outputed_poc = INT_MIN;
951  out_of_order = out->poc < h->next_outputed_poc;
952 
953  if (out_of_order || pics > h->avctx->has_b_frames) {
954  out->reference &= ~DELAYED_PIC_REF;
955  // for frame threading, the owner must be the second field's thread or
956  // else the first thread can release the picture and reuse it unsafely
957  for (i = out_idx; h->delayed_pic[i]; i++)
958  h->delayed_pic[i] = h->delayed_pic[i + 1];
959  }
960  if (!out_of_order && pics > h->avctx->has_b_frames) {
961  h->next_output_pic = out;
962  if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
963  h->next_outputed_poc = INT_MIN;
964  } else
965  h->next_outputed_poc = out->poc;
966  } else {
967  av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
968  }
969 
970  if (h->next_output_pic) {
971  if (h->next_output_pic->recovered) {
972  // We have reached an recovery point and all frames after it in
973  // display order are "recovered".
975  }
977  }
978 
979  if (setup_finished && !h->avctx->hwaccel)
981 }
982 
984 {
985  int list, i;
986  int luma_def, chroma_def;
987 
988  h->use_weight = 0;
989  h->use_weight_chroma = 0;
991  if (h->sps.chroma_format_idc)
993 
994  if (h->luma_log2_weight_denom > 7U) {
995  av_log(h->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is out of range\n", h->luma_log2_weight_denom);
996  h->luma_log2_weight_denom = 0;
997  }
998  if (h->chroma_log2_weight_denom > 7U) {
999  av_log(h->avctx, AV_LOG_ERROR, "chroma_log2_weight_denom %d is out of range\n", h->chroma_log2_weight_denom);
1000  h->chroma_log2_weight_denom = 0;
1001  }
1002 
1003  luma_def = 1 << h->luma_log2_weight_denom;
1004  chroma_def = 1 << h->chroma_log2_weight_denom;
1005 
1006  for (list = 0; list < 2; list++) {
1007  h->luma_weight_flag[list] = 0;
1008  h->chroma_weight_flag[list] = 0;
1009  for (i = 0; i < h->ref_count[list]; i++) {
1010  int luma_weight_flag, chroma_weight_flag;
1011 
1012  luma_weight_flag = get_bits1(&h->gb);
1013  if (luma_weight_flag) {
1014  h->luma_weight[i][list][0] = get_se_golomb(&h->gb);
1015  h->luma_weight[i][list][1] = get_se_golomb(&h->gb);
1016  if (h->luma_weight[i][list][0] != luma_def ||
1017  h->luma_weight[i][list][1] != 0) {
1018  h->use_weight = 1;
1019  h->luma_weight_flag[list] = 1;
1020  }
1021  } else {
1022  h->luma_weight[i][list][0] = luma_def;
1023  h->luma_weight[i][list][1] = 0;
1024  }
1025 
1026  if (h->sps.chroma_format_idc) {
1027  chroma_weight_flag = get_bits1(&h->gb);
1028  if (chroma_weight_flag) {
1029  int j;
1030  for (j = 0; j < 2; j++) {
1031  h->chroma_weight[i][list][j][0] = get_se_golomb(&h->gb);
1032  h->chroma_weight[i][list][j][1] = get_se_golomb(&h->gb);
1033  if (h->chroma_weight[i][list][j][0] != chroma_def ||
1034  h->chroma_weight[i][list][j][1] != 0) {
1035  h->use_weight_chroma = 1;
1036  h->chroma_weight_flag[list] = 1;
1037  }
1038  }
1039  } else {
1040  int j;
1041  for (j = 0; j < 2; j++) {
1042  h->chroma_weight[i][list][j][0] = chroma_def;
1043  h->chroma_weight[i][list][j][1] = 0;
1044  }
1045  }
1046  }
1047  }
1049  break;
1050  }
1051  h->use_weight = h->use_weight || h->use_weight_chroma;
1052  return 0;
1053 }
1054 
1055 /**
1056  * instantaneous decoder refresh.
1057  */
1058 static void idr(H264Context *h)
1059 {
1060  int i;
1062  h->prev_frame_num =
1063  h->prev_frame_num_offset = 0;
1064  h->prev_poc_msb = 1<<16;
1065  h->prev_poc_lsb = 0;
1066  for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1067  h->last_pocs[i] = INT_MIN;
1068 }
1069 
1070 /* forget old pics after a seek */
1072 {
1073  int i, j;
1074 
1075  h->outputed_poc = h->next_outputed_poc = INT_MIN;
1076  h->prev_interlaced_frame = 1;
1077  idr(h);
1078 
1079  h->prev_frame_num = -1;
1080  if (h->cur_pic_ptr) {
1081  h->cur_pic_ptr->reference = 0;
1082  for (j=i=0; h->delayed_pic[i]; i++)
1083  if (h->delayed_pic[i] != h->cur_pic_ptr)
1084  h->delayed_pic[j++] = h->delayed_pic[i];
1085  h->delayed_pic[j] = NULL;
1086  }
1088 
1089  h->first_field = 0;
1090  ff_h264_reset_sei(h);
1091  h->recovery_frame = -1;
1092  h->frame_recovered = 0;
1093  h->list_count = 0;
1094  h->current_slice = 0;
1095  h->mmco_reset = 1;
1096 }
1097 
1098 /* forget old pics after a seek */
1099 static void flush_dpb(AVCodecContext *avctx)
1100 {
1101  H264Context *h = avctx->priv_data;
1102  int i;
1103 
1104  memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
1105 
1107 
1108  if (h->DPB)
1109  for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
1110  ff_h264_unref_picture(h, &h->DPB[i]);
1111  h->cur_pic_ptr = NULL;
1113 
1114  h->mb_x = h->mb_y = 0;
1115 
1116  ff_h264_free_tables(h, 1);
1117  h->context_initialized = 0;
1118 }
1119 
1120 int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
1121 {
1122  const int max_frame_num = 1 << h->sps.log2_max_frame_num;
1123  int field_poc[2];
1124 
1126  if (h->frame_num < h->prev_frame_num)
1127  h->frame_num_offset += max_frame_num;
1128 
1129  if (h->sps.poc_type == 0) {
1130  const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
1131 
1132  if (h->poc_lsb < h->prev_poc_lsb &&
1133  h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
1134  h->poc_msb = h->prev_poc_msb + max_poc_lsb;
1135  else if (h->poc_lsb > h->prev_poc_lsb &&
1136  h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
1137  h->poc_msb = h->prev_poc_msb - max_poc_lsb;
1138  else
1139  h->poc_msb = h->prev_poc_msb;
1140  field_poc[0] =
1141  field_poc[1] = h->poc_msb + h->poc_lsb;
1142  if (h->picture_structure == PICT_FRAME)
1143  field_poc[1] += h->delta_poc_bottom;
1144  } else if (h->sps.poc_type == 1) {
1145  int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
1146  int i;
1147 
1148  if (h->sps.poc_cycle_length != 0)
1149  abs_frame_num = h->frame_num_offset + h->frame_num;
1150  else
1151  abs_frame_num = 0;
1152 
1153  if (h->nal_ref_idc == 0 && abs_frame_num > 0)
1154  abs_frame_num--;
1155 
1156  expected_delta_per_poc_cycle = 0;
1157  for (i = 0; i < h->sps.poc_cycle_length; i++)
1158  // FIXME integrate during sps parse
1159  expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
1160 
1161  if (abs_frame_num > 0) {
1162  int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
1163  int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
1164 
1165  expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
1166  for (i = 0; i <= frame_num_in_poc_cycle; i++)
1167  expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
1168  } else
1169  expectedpoc = 0;
1170 
1171  if (h->nal_ref_idc == 0)
1172  expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
1173 
1174  field_poc[0] = expectedpoc + h->delta_poc[0];
1175  field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
1176 
1177  if (h->picture_structure == PICT_FRAME)
1178  field_poc[1] += h->delta_poc[1];
1179  } else {
1180  int poc = 2 * (h->frame_num_offset + h->frame_num);
1181 
1182  if (!h->nal_ref_idc)
1183  poc--;
1184 
1185  field_poc[0] = poc;
1186  field_poc[1] = poc;
1187  }
1188 
1190  pic_field_poc[0] = field_poc[0];
1192  pic_field_poc[1] = field_poc[1];
1193  *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
1194 
1195  return 0;
1196 }
1197 
1198 /**
1199  * Compute profile from profile_idc and constraint_set?_flags.
1200  *
1201  * @param sps SPS
1202  *
1203  * @return profile as defined by FF_PROFILE_H264_*
1204  */
1206 {
1207  int profile = sps->profile_idc;
1208 
1209  switch (sps->profile_idc) {
1211  // constraint_set1_flag set to 1
1212  profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
1213  break;
1217  // constraint_set3_flag set to 1
1218  profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
1219  break;
1220  }
1221 
1222  return profile;
1223 }
1224 
1226 {
1227  if (h->flags & CODEC_FLAG_LOW_DELAY ||
1229  !h->sps.num_reorder_frames)) {
1230  if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
1231  av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
1232  "Reenabling low delay requires a codec flush.\n");
1233  else
1234  h->low_delay = 1;
1235  }
1236 
1237  if (h->avctx->has_b_frames < 2)
1238  h->avctx->has_b_frames = !h->low_delay;
1239 
1240  if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
1242  if (h->avctx->codec &&
1244  (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
1246  "VDPAU decoding does not support video colorspace.\n");
1247  return AVERROR_INVALIDDATA;
1248  }
1249  if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 14 &&
1250  h->sps.bit_depth_luma != 11 && h->sps.bit_depth_luma != 13) {
1253  h->pixel_shift = h->sps.bit_depth_luma > 8;
1254 
1256  h->sps.chroma_format_idc);
1260  h->sps.chroma_format_idc);
1261 
1263  } else {
1264  av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
1265  h->sps.bit_depth_luma);
1266  return AVERROR_INVALIDDATA;
1267  }
1268  }
1269  return 0;
1270 }
1271 
1273 {
1274  int ref_count[2], list_count;
1275  int num_ref_idx_active_override_flag;
1276 
1277  // set defaults, might be overridden a few lines later
1278  ref_count[0] = h->pps.ref_count[0];
1279  ref_count[1] = h->pps.ref_count[1];
1280 
1281  if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
1282  unsigned max[2];
1283  max[0] = max[1] = h->picture_structure == PICT_FRAME ? 15 : 31;
1284 
1287  num_ref_idx_active_override_flag = get_bits1(&h->gb);
1288 
1289  if (num_ref_idx_active_override_flag) {
1290  ref_count[0] = get_ue_golomb(&h->gb) + 1;
1291  if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
1292  ref_count[1] = get_ue_golomb(&h->gb) + 1;
1293  } else
1294  // full range is spec-ok in this case, even for frames
1295  ref_count[1] = 1;
1296  }
1297 
1298  if (ref_count[0]-1 > max[0] || ref_count[1]-1 > max[1]){
1299  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]);
1300  h->ref_count[0] = h->ref_count[1] = 0;
1301  h->list_count = 0;
1302  return AVERROR_INVALIDDATA;
1303  }
1304 
1306  list_count = 2;
1307  else
1308  list_count = 1;
1309  } else {
1310  list_count = 0;
1311  ref_count[0] = ref_count[1] = 0;
1312  }
1313 
1314  if (list_count != h->list_count ||
1315  ref_count[0] != h->ref_count[0] ||
1316  ref_count[1] != h->ref_count[1]) {
1317  h->ref_count[0] = ref_count[0];
1318  h->ref_count[1] = ref_count[1];
1319  h->list_count = list_count;
1320  return 1;
1321  }
1322 
1323  return 0;
1324 }
1325 
1326 static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
1327 
1328 static int get_bit_length(H264Context *h, const uint8_t *buf,
1329  const uint8_t *ptr, int dst_length,
1330  int i, int next_avc)
1331 {
1332  if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
1333  buf[i] == 0x00 && buf[i + 1] == 0x00 &&
1334  buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
1336 
1337  if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
1338  while (dst_length > 0 && ptr[dst_length - 1] == 0)
1339  dst_length--;
1340 
1341  if (!dst_length)
1342  return 0;
1343 
1344  return 8 * dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1);
1345 }
1346 
1347 static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
1348 {
1349  int next_avc = h->is_avc ? 0 : buf_size;
1350  int nal_index = 0;
1351  int buf_index = 0;
1352  int nals_needed = 0;
1353  int first_slice = 0;
1354 
1355  while(1) {
1356  int nalsize = 0;
1357  int dst_length, bit_length, consumed;
1358  const uint8_t *ptr;
1359 
1360  if (buf_index >= next_avc) {
1361  nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
1362  if (nalsize < 0)
1363  break;
1364  next_avc = buf_index + nalsize;
1365  } else {
1366  buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
1367  if (buf_index >= buf_size)
1368  break;
1369  if (buf_index >= next_avc)
1370  continue;
1371  }
1372 
1373  ptr = ff_h264_decode_nal(h, buf + buf_index, &dst_length, &consumed,
1374  next_avc - buf_index);
1375 
1376  if (!ptr || dst_length < 0)
1377  return AVERROR_INVALIDDATA;
1378 
1379  buf_index += consumed;
1380 
1381  bit_length = get_bit_length(h, buf, ptr, dst_length,
1382  buf_index, next_avc);
1383  nal_index++;
1384 
1385  /* packets can sometimes contain multiple PPS/SPS,
1386  * e.g. two PAFF field pictures in one packet, or a demuxer
1387  * which splits NALs strangely if so, when frame threading we
1388  * can't start the next thread until we've read all of them */
1389  switch (h->nal_unit_type) {
1390  case NAL_SPS:
1391  case NAL_PPS:
1392  nals_needed = nal_index;
1393  break;
1394  case NAL_DPA:
1395  case NAL_IDR_SLICE:
1396  case NAL_SLICE:
1397  init_get_bits(&h->gb, ptr, bit_length);
1398  if (!get_ue_golomb(&h->gb) ||
1399  !first_slice ||
1400  first_slice != h->nal_unit_type)
1401  nals_needed = nal_index;
1402  if (!first_slice)
1403  first_slice = h->nal_unit_type;
1404  }
1405  }
1406 
1407  return nals_needed;
1408 }
1409 
1410 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
1411  int parse_extradata)
1412 {
1413  AVCodecContext *const avctx = h->avctx;
1414  H264Context *hx; ///< thread context
1415  int buf_index;
1416  unsigned context_count;
1417  int next_avc;
1418  int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
1419  int nal_index;
1420  int idr_cleared=0;
1421  int ret = 0;
1422 
1423  h->nal_unit_type= 0;
1424 
1425  if(!h->slice_context_count)
1426  h->slice_context_count= 1;
1428  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS)) {
1429  h->current_slice = 0;
1430  if (!h->first_field)
1431  h->cur_pic_ptr = NULL;
1432  ff_h264_reset_sei(h);
1433  }
1434 
1435  if (h->nal_length_size == 4) {
1436  if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
1437  h->is_avc = 0;
1438  }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
1439  h->is_avc = 1;
1440  }
1441 
1442  if (avctx->active_thread_type & FF_THREAD_FRAME)
1443  nals_needed = get_last_needed_nal(h, buf, buf_size);
1444 
1445  {
1446  buf_index = 0;
1447  context_count = 0;
1448  next_avc = h->is_avc ? 0 : buf_size;
1449  nal_index = 0;
1450  for (;;) {
1451  int consumed;
1452  int dst_length;
1453  int bit_length;
1454  const uint8_t *ptr;
1455  int nalsize = 0;
1456  int err;
1457 
1458  if (buf_index >= next_avc) {
1459  nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
1460  if (nalsize < 0)
1461  break;
1462  next_avc = buf_index + nalsize;
1463  } else {
1464  buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
1465  if (buf_index >= buf_size)
1466  break;
1467  if (buf_index >= next_avc)
1468  continue;
1469  }
1470 
1471  hx = h->thread_context[context_count];
1472 
1473  ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
1474  &consumed, next_avc - buf_index);
1475  if (!ptr || dst_length < 0) {
1476  ret = -1;
1477  goto end;
1478  }
1479 
1480  bit_length = get_bit_length(h, buf, ptr, dst_length,
1481  buf_index + consumed, next_avc);
1482 
1483  if (h->avctx->debug & FF_DEBUG_STARTCODE)
1485  "NAL %d/%d at %d/%d length %d\n",
1486  hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length);
1487 
1488  if (h->is_avc && (nalsize != consumed) && nalsize)
1490  "AVC: Consumed only %d bytes instead of %d\n",
1491  consumed, nalsize);
1492 
1493  buf_index += consumed;
1494  nal_index++;
1495 
1496  if (avctx->skip_frame >= AVDISCARD_NONREF &&
1497  h->nal_ref_idc == 0 &&
1498  h->nal_unit_type != NAL_SEI)
1499  continue;
1500 
1501 again:
1502  /* Ignore per frame NAL unit type during extradata
1503  * parsing. Decoding slices is not possible in codec init
1504  * with frame-mt */
1505  if (parse_extradata) {
1506  switch (hx->nal_unit_type) {
1507  case NAL_IDR_SLICE:
1508  case NAL_SLICE:
1509  case NAL_DPA:
1510  case NAL_DPB:
1511  case NAL_DPC:
1513  "Ignoring NAL %d in global header/extradata\n",
1514  hx->nal_unit_type);
1515  // fall through to next case
1516  case NAL_AUXILIARY_SLICE:
1518  }
1519  }
1520 
1521  err = 0;
1522 
1523  switch (hx->nal_unit_type) {
1524  case NAL_IDR_SLICE:
1525  if ((ptr[0] & 0xFC) == 0x98) {
1526  av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
1527  h->next_outputed_poc = INT_MIN;
1528  ret = -1;
1529  goto end;
1530  }
1531  if (h->nal_unit_type != NAL_IDR_SLICE) {
1533  "Invalid mix of idr and non-idr slices\n");
1534  ret = -1;
1535  goto end;
1536  }
1537  if(!idr_cleared) {
1538  if (h->current_slice && (avctx->active_thread_type & FF_THREAD_SLICE)) {
1539  av_log(h, AV_LOG_ERROR, "invalid mixed IDR / non IDR frames cannot be decoded in slice multithreading mode\n");
1540  ret = AVERROR_INVALIDDATA;
1541  goto end;
1542  }
1543  idr(h); // FIXME ensure we don't lose some frames if there is reordering
1544  }
1545  idr_cleared = 1;
1546  h->has_recovery_point = 1;
1547  case NAL_SLICE:
1548  init_get_bits(&hx->gb, ptr, bit_length);
1549  hx->intra_gb_ptr =
1550  hx->inter_gb_ptr = &hx->gb;
1551 
1552  if ( nals_needed >= nal_index
1553  || (!(avctx->active_thread_type & FF_THREAD_FRAME) && !context_count))
1554  h->au_pps_id = -1;
1555 
1556  if ((err = ff_h264_decode_slice_header(hx, h)))
1557  break;
1558 
1559  if (h->sei_recovery_frame_cnt >= 0) {
1561  h->valid_recovery_point = 1;
1562 
1563  if ( h->recovery_frame < 0
1564  || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt) {
1566  ((1 << h->sps.log2_max_frame_num) - 1);
1567 
1568  if (!h->valid_recovery_point)
1569  h->recovery_frame = h->frame_num;
1570  }
1571  }
1572 
1573  h->cur_pic_ptr->f.key_frame |=
1574  (hx->nal_unit_type == NAL_IDR_SLICE);
1575 
1576  if (hx->nal_unit_type == NAL_IDR_SLICE ||
1577  h->recovery_frame == h->frame_num) {
1578  h->recovery_frame = -1;
1579  h->cur_pic_ptr->recovered = 1;
1580  }
1581  // If we have an IDR, all frames after it in decoded order are
1582  // "recovered".
1583  if (hx->nal_unit_type == NAL_IDR_SLICE)
1585  h->frame_recovered |= 3*!!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL);
1586  h->frame_recovered |= 3*!!(avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT);
1587 #if 1
1589 #else
1591 #endif
1592 
1593  if (h->current_slice == 1) {
1594  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
1595  decode_postinit(h, nal_index >= nals_needed);
1596 
1597  if (h->avctx->hwaccel &&
1598  (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
1599  return ret;
1603  }
1604 
1605  if (hx->redundant_pic_count == 0) {
1606  if (avctx->hwaccel) {
1607  ret = avctx->hwaccel->decode_slice(avctx,
1608  &buf[buf_index - consumed],
1609  consumed);
1610  if (ret < 0)
1611  return ret;
1612  } else if (CONFIG_H264_VDPAU_DECODER &&
1615  start_code,
1616  sizeof(start_code));
1618  &buf[buf_index - consumed],
1619  consumed);
1620  } else
1621  context_count++;
1622  }
1623  break;
1624  case NAL_DPA:
1625  case NAL_DPB:
1626  case NAL_DPC:
1627  avpriv_request_sample(avctx, "data partitioning");
1628  ret = AVERROR(ENOSYS);
1629  goto end;
1630  break;
1631  case NAL_SEI:
1632  init_get_bits(&h->gb, ptr, bit_length);
1633  ret = ff_h264_decode_sei(h);
1634  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1635  goto end;
1636  break;
1637  case NAL_SPS:
1638  init_get_bits(&h->gb, ptr, bit_length);
1639  if (ff_h264_decode_seq_parameter_set(h, 0) >= 0)
1640  break;
1641  if (h->is_avc ? nalsize : 1) {
1643  "SPS decoding failure, trying again with the complete NAL\n");
1644  if (h->is_avc)
1645  av_assert0(next_avc - buf_index + consumed == nalsize);
1646  if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
1647  break;
1648  init_get_bits(&h->gb, &buf[buf_index + 1 - consumed],
1649  8*(next_avc - buf_index + consumed - 1));
1650  if (ff_h264_decode_seq_parameter_set(h, 0) >= 0)
1651  break;
1652  }
1653  init_get_bits(&h->gb, ptr, bit_length);
1655 
1656  break;
1657  case NAL_PPS:
1658  init_get_bits(&h->gb, ptr, bit_length);
1659  ret = ff_h264_decode_picture_parameter_set(h, bit_length);
1660  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1661  goto end;
1662  break;
1663  case NAL_AUD:
1664  case NAL_END_SEQUENCE:
1665  case NAL_END_STREAM:
1666  case NAL_FILLER_DATA:
1667  case NAL_SPS_EXT:
1668  case NAL_AUXILIARY_SLICE:
1669  break;
1670  case NAL_FF_IGNORE:
1671  break;
1672  default:
1673  av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
1674  hx->nal_unit_type, bit_length);
1675  }
1676 
1677  if (context_count == h->max_contexts) {
1678  ret = ff_h264_execute_decode_slices(h, context_count);
1679  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1680  goto end;
1681  context_count = 0;
1682  }
1683 
1684  if (err < 0 || err == SLICE_SKIPED) {
1685  if (err < 0)
1686  av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
1687  hx->ref_count[0] = hx->ref_count[1] = hx->list_count = 0;
1688  } else if (err == SLICE_SINGLETHREAD) {
1689  if (context_count > 1) {
1690  ret = ff_h264_execute_decode_slices(h, context_count - 1);
1691  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1692  goto end;
1693  context_count = 0;
1694  }
1695  /* Slice could not be decoded in parallel mode, copy down
1696  * NAL unit stuff to context 0 and restart. Note that
1697  * rbsp_buffer is not transferred, but since we no longer
1698  * run in parallel mode this should not be an issue. */
1699  h->nal_unit_type = hx->nal_unit_type;
1700  h->nal_ref_idc = hx->nal_ref_idc;
1701  hx = h;
1702  goto again;
1703  }
1704  }
1705  }
1706  if (context_count) {
1707  ret = ff_h264_execute_decode_slices(h, context_count);
1708  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1709  goto end;
1710  }
1711 
1712  ret = 0;
1713 end:
1714  /* clean up */
1715  if (h->cur_pic_ptr && !h->droppable) {
1718  }
1719 
1720  return (ret < 0) ? ret : buf_index;
1721 }
1722 
1723 /**
1724  * Return the number of bytes consumed for building the current frame.
1725  */
1726 static int get_consumed_bytes(int pos, int buf_size)
1727 {
1728  if (pos == 0)
1729  pos = 1; // avoid infinite loops (I doubt that is needed but...)
1730  if (pos + 10 > buf_size)
1731  pos = buf_size; // oops ;)
1732 
1733  return pos;
1734 }
1735 
1737 {
1738  AVFrame *src = &srcp->f;
1739  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
1740  int i;
1741  int ret = av_frame_ref(dst, src);
1742  if (ret < 0)
1743  return ret;
1744 
1745  av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(h), 0);
1746 
1747  if (srcp->sei_recovery_frame_cnt == 0)
1748  dst->key_frame = 1;
1749  if (!srcp->crop)
1750  return 0;
1751 
1752  for (i = 0; i < desc->nb_components; i++) {
1753  int hshift = (i > 0) ? desc->log2_chroma_w : 0;
1754  int vshift = (i > 0) ? desc->log2_chroma_h : 0;
1755  int off = ((srcp->crop_left >> hshift) << h->pixel_shift) +
1756  (srcp->crop_top >> vshift) * dst->linesize[i];
1757  dst->data[i] += off;
1758  }
1759  return 0;
1760 }
1761 
1762 static int is_extra(const uint8_t *buf, int buf_size)
1763 {
1764  int cnt= buf[5]&0x1f;
1765  const uint8_t *p= buf+6;
1766  while(cnt--){
1767  int nalsize= AV_RB16(p) + 2;
1768  if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
1769  return 0;
1770  p += nalsize;
1771  }
1772  cnt = *(p++);
1773  if(!cnt)
1774  return 0;
1775  while(cnt--){
1776  int nalsize= AV_RB16(p) + 2;
1777  if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
1778  return 0;
1779  p += nalsize;
1780  }
1781  return 1;
1782 }
1783 
1784 static int h264_decode_frame(AVCodecContext *avctx, void *data,
1785  int *got_frame, AVPacket *avpkt)
1786 {
1787  const uint8_t *buf = avpkt->data;
1788  int buf_size = avpkt->size;
1789  H264Context *h = avctx->priv_data;
1790  AVFrame *pict = data;
1791  int buf_index = 0;
1792  H264Picture *out;
1793  int i, out_idx;
1794  int ret;
1795 
1796  h->flags = avctx->flags;
1797 
1799 
1800  /* end of stream, output what is still in the buffers */
1801  if (buf_size == 0) {
1802  out:
1803 
1804  h->cur_pic_ptr = NULL;
1805  h->first_field = 0;
1806 
1807  // FIXME factorize this with the output code below
1808  out = h->delayed_pic[0];
1809  out_idx = 0;
1810  for (i = 1;
1811  h->delayed_pic[i] &&
1812  !h->delayed_pic[i]->f.key_frame &&
1813  !h->delayed_pic[i]->mmco_reset;
1814  i++)
1815  if (h->delayed_pic[i]->poc < out->poc) {
1816  out = h->delayed_pic[i];
1817  out_idx = i;
1818  }
1819 
1820  for (i = out_idx; h->delayed_pic[i]; i++)
1821  h->delayed_pic[i] = h->delayed_pic[i + 1];
1822 
1823  if (out) {
1824  out->reference &= ~DELAYED_PIC_REF;
1825  ret = output_frame(h, pict, out);
1826  if (ret < 0)
1827  return ret;
1828  *got_frame = 1;
1829  }
1830 
1831  return buf_index;
1832  }
1834  int side_size;
1835  uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
1836  if (is_extra(side, side_size))
1837  ff_h264_decode_extradata(h, side, side_size);
1838  }
1839  if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
1840  if (is_extra(buf, buf_size))
1841  return ff_h264_decode_extradata(h, buf, buf_size);
1842  }
1843 
1844  buf_index = decode_nal_units(h, buf, buf_size, 0);
1845  if (buf_index < 0)
1846  return AVERROR_INVALIDDATA;
1847 
1848  if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
1849  av_assert0(buf_index <= buf_size);
1850  goto out;
1851  }
1852 
1853  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
1854  if (avctx->skip_frame >= AVDISCARD_NONREF ||
1855  buf_size >= 4 && !memcmp("Q264", buf, 4))
1856  return buf_size;
1857  av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1858  return AVERROR_INVALIDDATA;
1859  }
1860 
1861  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
1862  (h->mb_y >= h->mb_height && h->mb_height)) {
1863  if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
1864  decode_postinit(h, 1);
1865 
1866  ff_h264_field_end(h, 0);
1867 
1868  /* Wait for second field. */
1869  *got_frame = 0;
1870  if (h->next_output_pic && (
1871  h->next_output_pic->recovered)) {
1872  if (!h->next_output_pic->recovered)
1874 
1875  if (!h->avctx->hwaccel &&
1876  (h->next_output_pic->field_poc[0] == INT_MAX ||
1877  h->next_output_pic->field_poc[1] == INT_MAX)
1878  ) {
1879  int p;
1880  AVFrame *f = &h->next_output_pic->f;
1881  int field = h->next_output_pic->field_poc[0] == INT_MAX;
1882  uint8_t *dst_data[4];
1883  int linesizes[4];
1884  const uint8_t *src_data[4];
1885 
1886  av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
1887 
1888  for (p = 0; p<4; p++) {
1889  dst_data[p] = f->data[p] + (field^1)*f->linesize[p];
1890  src_data[p] = f->data[p] + field *f->linesize[p];
1891  linesizes[p] = 2*f->linesize[p];
1892  }
1893 
1894  av_image_copy(dst_data, linesizes, src_data, linesizes,
1895  f->format, f->width, f->height>>1);
1896  }
1897 
1898  ret = output_frame(h, pict, h->next_output_pic);
1899  if (ret < 0)
1900  return ret;
1901  *got_frame = 1;
1902  if (CONFIG_MPEGVIDEO) {
1907  &h->low_delay,
1908  h->mb_width, h->mb_height, h->mb_stride, 1);
1909  }
1910  }
1911  }
1912 
1913  av_assert0(pict->buf[0] || !*got_frame);
1914 
1916 
1917  return get_consumed_bytes(buf_index, buf_size);
1918 }
1919 
1921 {
1922  int i;
1923 
1924  ff_h264_free_tables(h, 1); // FIXME cleanup init stuff perhaps
1925 
1926  for (i = 0; i < MAX_SPS_COUNT; i++)
1927  av_freep(h->sps_buffers + i);
1928 
1929  for (i = 0; i < MAX_PPS_COUNT; i++)
1930  av_freep(h->pps_buffers + i);
1931 }
1932 
1934 {
1935  H264Context *h = avctx->priv_data;
1936 
1939 
1942 
1943  return 0;
1944 }
1945 
1946 static const AVProfile profiles[] = {
1947  { FF_PROFILE_H264_BASELINE, "Baseline" },
1948  { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
1949  { FF_PROFILE_H264_MAIN, "Main" },
1950  { FF_PROFILE_H264_EXTENDED, "Extended" },
1951  { FF_PROFILE_H264_HIGH, "High" },
1952  { FF_PROFILE_H264_HIGH_10, "High 10" },
1953  { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
1954  { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
1955  { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
1956  { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
1957  { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
1958  { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
1959  { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
1960  { FF_PROFILE_UNKNOWN },
1961 };
1962 
1963 static const AVOption h264_options[] = {
1964  {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
1965  {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
1966  {NULL}
1967 };
1968 
1969 static const AVClass h264_class = {
1970  .class_name = "H264 Decoder",
1971  .item_name = av_default_item_name,
1972  .option = h264_options,
1973  .version = LIBAVUTIL_VERSION_INT,
1974 };
1975 
1977  .name = "h264",
1978  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1979  .type = AVMEDIA_TYPE_VIDEO,
1980  .id = AV_CODEC_ID_H264,
1981  .priv_data_size = sizeof(H264Context),
1983  .close = h264_decode_end,
1985  .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
1988  .flush = flush_dpb,
1990  .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
1991  .profiles = NULL_IF_CONFIG_SMALL(profiles),
1992  .priv_class = &h264_class,
1993 };
1994 
1995 #if CONFIG_H264_VDPAU_DECODER
1996 static const AVClass h264_vdpau_class = {
1997  .class_name = "H264 VDPAU Decoder",
1998  .item_name = av_default_item_name,
1999  .option = h264_options,
2000  .version = LIBAVUTIL_VERSION_INT,
2001 };
2002 
2003 AVCodec ff_h264_vdpau_decoder = {
2004  .name = "h264_vdpau",
2005  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
2006  .type = AVMEDIA_TYPE_VIDEO,
2007  .id = AV_CODEC_ID_H264,
2008  .priv_data_size = sizeof(H264Context),
2010  .close = h264_decode_end,
2013  .flush = flush_dpb,
2014  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
2015  AV_PIX_FMT_NONE},
2016  .profiles = NULL_IF_CONFIG_SMALL(profiles),
2017  .priv_class = &h264_vdpau_class,
2018 };
2019 #endif
int chroma_format_idc
Definition: h264.h:177
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:47
#define FF_PROFILE_H264_HIGH_10
Definition: avcodec.h:2869
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1248
uint8_t * edge_emu_buffer
Definition: h264.h:756
void ff_h264_flush_change(H264Context *h)
Definition: h264.c:1071
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:3338
int workaround_bugs
Definition: h264.h:371
float v
unsigned int top_samples_available
Definition: h264.h:397
#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:1410
#define DC_128_PRED8x8
Definition: h264pred.h:76
void ff_h264_free_tables(H264Context *h, int free_rbsp)
Definition: h264.c:365
GetBitContext gb
Definition: h264.h:346
#define CODEC_FLAG2_FAST
Allow non spec compliant speedup tricks.
Definition: avcodec.h:763
int sei_recovery_frame_cnt
Definition: h264.h:329
Views are packed per line, as if interlaced.
Definition: stereo3d.h:97
#define AV_NUM_DATA_POINTERS
Definition: frame.h:164
int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
Call decode_slice() for each context.
Definition: h264_slice.c:2602
5: top field, bottom field, top field repeated, in that order
Definition: h264.h:151
int low_delay
Definition: h264.h:367
int mb_num
Definition: h264.h:543
GetBitContext * intra_gb_ptr
Definition: h264.h:494
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2029
mpeg2/4, h264 default
Definition: pixfmt.h:528
This structure describes decoded (raw) audio or video data.
Definition: frame.h:163
#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
Definition: internal.h:156
AVOption.
Definition: opt.h:255
static const AVClass h264_class
Definition: h264.c:1969
int delta_poc[2]
Definition: h264.h:581
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
Views are alternated temporally.
Definition: stereo3d.h:66
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:183
int quincunx_subsampling
Definition: h264.h:674
3: top field, bottom field, in that order
Definition: h264.h:149
#define FF_PROFILE_H264_HIGH_444
Definition: avcodec.h:2873
const uint8_t * ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length)
Decode a network abstraction layer unit.
Definition: h264.c:233
#define H264_MAX_PICTURE_COUNT
Definition: h264.h:46
int first_field
Definition: h264.h:458
misc image utilities
Views are next to each other, but when upscaling apply a checkerboard pattern.
Definition: stereo3d.h:87
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:181
Definition: h264.h:113
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:433
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
H264ChromaContext h264chroma
Definition: h264.h:344
uint16_t * cbp_table
Definition: h264.h:509
av_cold int ff_h264_decode_init(AVCodecContext *avctx)
Definition: h264.c:628
int luma_weight_flag[2]
7.4.3.2 luma_weight_lX_flag
Definition: h264.h:741
7: frame doubling
Definition: h264.h:153
#define MAX_PPS_COUNT
Definition: h264.h:50
Sequence parameter set.
Definition: h264.h:173
int mb_y
Definition: h264.h:536
int bitstream_restriction_flag
Definition: h264.h:213
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:242
#define FMO
Definition: h264.h:62
int num
numerator
Definition: rational.h:44
Definition: h264.h:115
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:354
H264Picture * DPB
Definition: h264.h:349
static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
Definition: h264.c:1347
int size
Definition: avcodec.h:1161
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: diracdec.c:74
AVBufferPool * mb_type_pool
Definition: h264.h:760
int outputed_poc
Definition: h264.h:607
int crop
Definition: h264.h:331
int16_t(*[2] motion_val)[2]
Definition: h264.h:301
int flags
Definition: h264.h:370
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1442
int mb_height
Definition: h264.h:541
int16_t * dc_val_base
Definition: h264.h:757
H264Picture * delayed_pic[MAX_DELAYED_PIC_COUNT+2]
Definition: h264.h:604
int is_avc
Used to parse AVC variant of h264.
Definition: h264.h:564
AVBufferPool * ref_index_pool
Definition: h264.h:762
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:139
av_cold void ff_h264chroma_init(H264ChromaContext *c, int bit_depth)
Definition: h264chroma.c:41
int ff_h264_get_profile(SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
Definition: h264.c:1205
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
Definition: h264.h:413
void ff_h264_decode_init_vlc(void)
Definition: h264_cavlc.c:326
H264Context.
Definition: h264.h:339
int prev_poc_msb
poc_msb of the last reference pic for POC type 0
Definition: h264.h:583
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2725
#define FF_PROFILE_H264_MAIN
Definition: avcodec.h:2866
4: bottom field, top field, in that order
Definition: h264.h:150
struct AVFrame f
Definition: h264.h:293
#define FRAME_RECOVERED_IDR
We have seen an IDR, so all the following frames in coded order are correctly decodable.
Definition: h264.h:728
AVCodec.
Definition: avcodec.h:3173
int picture_structure
Definition: h264.h:457
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition: h264.h:450
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
int profile_idc
Definition: h264.h:175
unsigned current_sps_id
id of the current SPS
Definition: h264.h:436
#define CODEC_FLAG_OUTPUT_CORRUPT
Output even those frames that might be corrupted.
Definition: avcodec.h:712
static av_always_inline uint32_t pack16to32(int a, int b)
Definition: h264.h:953
int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
Definition: h264.c:56
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1367
uint8_t * chroma_pred_mode_table
Definition: h264.h:514
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2939
#define AV_RN32A(p)
Definition: intreadwrite.h:526
BYTE int const BYTE * srcp
Definition: avisynth_c.h:714
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2642
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:71
static int decode_init_thread_copy(AVCodecContext *avctx)
Definition: h264.c:716
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t scaling_matrix4[6][16]
Definition: h264.h:252
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:444
#define CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries...
Definition: avcodec.h:769
av_cold void ff_h264qpel_init(H264QpelContext *c, int bit_depth)
Definition: h264qpel.c:49
if()
Definition: avfilter.c:975
uint8_t
#define av_cold
Definition: attributes.h:74
int prev_frame_num_offset
for POC type 2
Definition: h264.h:586
int use_weight
Definition: h264.h:463
int offset_for_non_ref_pic
Definition: h264.h:183
mode
Definition: f_perms.c:27
Definition: h264.h:114
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
int luma_weight[48][2][2]
Definition: h264.h:468
int bit_depth_chroma
bit_depth_chroma_minus8 + 8
Definition: h264.h:227
#define FF_PROFILE_H264_EXTENDED
Definition: avcodec.h:2867
int poc
frame POC
Definition: h264.h:313
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
AVCodec ff_h264_decoder
Definition: h264.c:1976
Multithreading support functions.
#define CODEC_CAP_HWACCEL_VDPAU
Codec can export data for HW decoding (VDPAU).
Definition: avcodec.h:832
#define TOP_DC_PRED8x8
Definition: h264pred.h:75
int mb_xy
Definition: h264.h:544
static int find_start_code(const uint8_t *buf, int buf_size, int buf_index, int next_avc)
Definition: h264.h:1103
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:278
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2834
#define emms_c()
Definition: internal.h:50
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:483
#define FF_PROFILE_H264_CONSTRAINED
Definition: avcodec.h:2861
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1353
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:787
#define FF_PROFILE_H264_HIGH_444_INTRA
Definition: avcodec.h:2875
int frame_recovered
Initial frame has been completely recovered.
Definition: h264.h:735
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:34
int mb_x
Definition: h264.h:536
uint8_t * data
Definition: avcodec.h:1160
static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
Identify the exact end of the bitstream.
Definition: h264.c:350
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:191
AVDictionary * metadata
metadata.
Definition: frame.h:535
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:359
#define MAX_DELAYED_PIC_COUNT
Definition: h264.h:54
static void fill_rectangle(SDL_Surface *screen, int x, int y, int w, int h, int color, int update)
Definition: ffplay.c:779
ptrdiff_t size
Definition: opengl_enc.c:101
Definition: h264.h:120
#define FF_PROFILE_H264_HIGH_422_INTRA
Definition: avcodec.h:2872
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...
high precision timer, useful to profile code
int recovered
picture at IDR or recovery point + recovery count
Definition: h264.h:327
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1965
int luma_log2_weight_denom
Definition: h264.h:465
#define av_log(a,...)
int sei_vflip
Definition: h264.h:681
int chroma_weight[48][2][2][2]
Definition: h264.h:469
int last_pocs[MAX_DELAYED_PIC_COUNT]
Definition: h264.h:605
H.264 / AVC / MPEG4 part10 codec.
#define U(x)
Definition: vp56_arith.h:37
int frame_num
Definition: h264.h:582
H264PredContext hpc
Definition: h264.h:395
int ff_h264_decode_slice_header(H264Context *h, H264Context *h0)
Decode a slice header.
Definition: h264_slice.c:1281
int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
Check if the top & left blocks are available if needed and change the dc mode so it only uses the ava...
Definition: h264.c:192
int width
width and height of the video frame
Definition: frame.h:212
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1531
int flags
Additional information about the frame packing.
Definition: stereo3d.h:132
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
static int get_consumed_bytes(int pos, int buf_size)
Return the number of bytes consumed for building the current frame.
Definition: h264.c:1726
int poc_type
pic_order_cnt_type
Definition: h264.h:180
int context_initialized
Definition: h264.h:369
int profile
Definition: mxfenc.c:1619
static const uint16_t mask[17]
Definition: lzw.c:38
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2621
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:822
int nal_unit_type
Definition: h264.h:557
int use_weight_chroma
Definition: h264.h:464
av_default_item_name
int num_reorder_frames
Definition: h264.h:214
#define AV_RB16
Definition: intreadwrite.h:53
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:74
#define AVERROR(e)
Definition: error.h:43
GetBitContext * inter_gb_ptr
Definition: h264.h:495
#define ALZHEIMER_DC_L0T_PRED8x8
Definition: h264pred.h:79
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2770
int mb_field_decoding_flag
Definition: h264.h:455
int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
Definition: h264.c:1120
const char * r
Definition: vf_curves.c:107
static void flush_dpb(AVCodecContext *avctx)
Definition: h264.c:1099
int capabilities
Codec capabilities.
Definition: avcodec.h:3192
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:196
static const AVOption h264_options[]
Definition: h264.c:1963
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:438
#define FF_BUG_TRUNCATED
Definition: avcodec.h:2527
uint8_t(*[2] mvd_table)[2]
Definition: h264.h:516
int prev_interlaced_frame
Complement sei_pic_struct SEI_PIC_STRUCT_TOP_BOTTOM and SEI_PIC_STRUCT_BOTTOM_TOP indicate interlaced...
Definition: h264.h:666
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1333
int direct_spatial_mv_pred
Definition: h264.h:472
#define FF_BUG_AUTODETECT
autodetection
Definition: avcodec.h:2508
ThreadFrame tf
Definition: h264.h:295
0: frame
Definition: h264.h:146
simple assert() macros that are a bit more flexible than ISO C assert().
#define PICT_TOP_FIELD
Definition: mpegutils.h:33
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3180
H264QpelContext h264qpel
Definition: h264.h:345
ERContext er
Definition: h264.h:347
void ff_init_cabac_states(void)
Definition: cabac.c:72
av_cold void ff_h264_pred_init(H264PredContext *h, int codec_id, const int bit_depth, int chroma_format_idc)
Set the intra prediction function pointers.
Definition: h264pred.c:411
int valid_recovery_point
Are the SEI recovery points looking valid.
Definition: h264.h:712
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:35
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:485
#define FFMAX(a, b)
Definition: common.h:79
Libavcodec external API header.
#define CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:755
uint8_t * mbintra_table
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:288
int * mb_index2xy
int offset_for_top_to_bottom_field
Definition: h264.h:184
#define FIELD_OR_MBAFF_PICTURE(h)
Definition: h264.h:91
static const uint8_t scan8[16 *3+3]
Definition: h264.h:937
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:219
int crop_left
Definition: h264.h:332
uint8_t * error_status_table
uint8_t * direct_table
Definition: h264.h:518
#define CODEC_FLAG2_SHOW_ALL
Show all frames before the first keyframe.
Definition: avcodec.h:770
int ff_pred_weight_table(H264Context *h)
Definition: h264.c:983
uint8_t scaling_matrix8[6][64]
Definition: h264.h:253
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:628
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:2577
int nal_length_size
Number of bytes used for nal length (1, 2 or 4)
Definition: h264.h:565
useful rectangle filling function
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
unsigned int left_samples_available
Definition: h264.h:399
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
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:62
int sei_anticlockwise_rotation
Definition: h264.h:680
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:1476
#define CONFIG_MPEGVIDEO
Definition: config.h:543
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:234
int frame_num_offset
for POC type 2
Definition: h264.h:585
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:474
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2610
FPA sei_fpa
Definition: h264.h:714
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int x264_build
Definition: h264.h:534
uint32_t * mb2br_xy
Definition: h264.h:430
uint8_t * er_temp_buffer
int needs_realloc
picture needs to be reallocated (eg due to a frame size change)
Definition: h264.h:325
#define FFMIN(a, b)
Definition: common.h:81
uint16_t * slice_table
slice_table_base + 2*mb_stride + 1
Definition: h264.h:448
av_cold void ff_h264dsp_init(H264DSPContext *c, const int bit_depth, const int chroma_format_idc)
Definition: h264dsp.c:67
#define H264_MAX_THREADS
Definition: h264.h:47
float y
int poc_cycle_length
num_ref_frames_in_pic_order_cnt_cycle
Definition: h264.h:185
int reference
Definition: h264.h:326
int redundant_pic_count
Definition: h264.h:599
ret
Definition: avfilter.c:974
int sei_frame_packing_present
frame_packing_arrangment SEI message
Definition: h264.h:671
#define FF_PROFILE_H264_HIGH_10_INTRA
Definition: avcodec.h:2870
uint32_t * mb_type
Definition: h264.h:304
#define AV_FRAME_FLAG_CORRUPT
The frame data may be corrupted, e.g.
Definition: frame.h:466
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:437
int32_t
PPS * pps_buffers[MAX_PPS_COUNT]
Definition: h264.h:571
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:114
int sei_hflip
Definition: h264.h:681
#define MAX_SPS_COUNT
Definition: h264.h:49
int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length)
Decode PPS.
Definition: h264_ps.c:586
Context Adaptive Binary Arithmetic Coder inline functions.
H264Picture ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264.h:486
int mmco_reset
Definition: h264.h:615
int8_t intra4x4_pred_mode_cache[5 *8]
Definition: h264.h:393
uint8_t * bipred_scratchpad
Definition: h264.h:749
int poc_lsb
Definition: h264.h:578
static int h264_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: h264.c:1784
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1376
int ff_set_ref_count(H264Context *h)
Definition: h264.c:1272
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264.c:1736
#define LEFT_DC_PRED8x8
Definition: h264pred.h:74
void av_display_rotation_set(int32_t matrix[9], double angle)
Initialize a transformation matrix describing a pure rotation by the specified angle (in degrees)...
Definition: display.c:50
#define PART_NOT_AVAILABLE
Definition: h264.h:416
unsigned int list_count
Definition: h264.h:484
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2751
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:502
#define SLICE_FLAG_ALLOW_FIELD
allow draw_horiz_band() with field slices (MPEG2 field pics)
Definition: avcodec.h:1757
int dequant_coeff_pps
reinit tables when pps changes
Definition: h264.h:573
SPS * sps_buffers[MAX_SPS_COUNT]
Definition: h264.h:570
struct H264Context * thread_context[H264_MAX_THREADS]
Definition: h264.h:626
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:224
int chroma_log2_weight_denom
Definition: h264.h:466
int bit_depth_luma
luma bit depth from sps to detect changes
Definition: h264.h:567
short offset_for_ref_frame[256]
Definition: h264.h:212
int chroma_format_idc
chroma format from sps to detect changes
Definition: h264.h:568
VideoDSPContext vdsp
Definition: h264.h:342
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:127
int mb_stride
Definition: h264.h:542
AVCodecContext * avctx
Definition: h264.h:341
AVS_Value src
Definition: avisynth_c.h:524
H264 / AVC / MPEG4 part10 codec data table
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2763
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: frame.h:84
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:1328
1: top field
Definition: h264.h:147
enum AVCodecID codec_id
Definition: avcodec.h:1256
void ff_h264_remove_all_refs(H264Context *h)
Definition: h264_refs.c:480
int prev_frame_num
frame_num of the last pic for POC type 1/2
Definition: h264.h:587
int ff_h264_set_parameter_from_sps(H264Context *h)
Definition: h264.c:1225
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:191
void ff_vdpau_add_data_chunk(uint8_t *data, const uint8_t *buf, int buf_size)
Definition: vdpau.c:428
int next_outputed_poc
Definition: h264.h:608
int ff_h264_decode_sei(H264Context *h)
Decode SEI.
Definition: h264_sei.c:282
int poc_msb
Definition: h264.h:579
int field_poc[2]
top/bottom POC
Definition: h264.h:312
int debug
debug
Definition: avcodec.h:2563
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: avcodec.h:2874
int max_contexts
Max number of threads / contexts.
Definition: h264.h:639
int recovery_frame
recovery_frame is the frame_num at which the next frame should be fully constructed.
Definition: h264.h:722
main external API structure.
Definition: avcodec.h:1239
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
int ff_h264_check_intra4x4_pred_mode(H264Context *h)
Check if the top & left blocks are available if needed and change the dc mode so it only uses the ava...
Definition: h264.c:145
static void decode_postinit(H264Context *h, int setup_finished)
Run setup operations that must be run after slice header decoding.
Definition: h264.c:743
int ff_h264_alloc_tables(H264Context *h)
Allocate tables.
Definition: h264.c:427
2: bottom field
Definition: h264.h:148
uint8_t * data
Definition: frame.h:129
void * buf
Definition: avisynth_c.h:595
int frame_packing_arrangement_type
Definition: h264.h:672
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:2226
int8_t * qscale_table
Definition: h264.h:298
int extradata_size
Definition: avcodec.h:1354
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:110
int constraint_set_flags
constraint_set[0-3]_flag
Definition: h264.h:229
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
SEI_PicStructType sei_pic_struct
pic_struct in picture timing SEI message
Definition: h264.h:658
BYTE int const BYTE int int int height
Definition: avisynth_c.h:714
int ff_h264_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: h264_slice.c:457
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2762
int slice_flags
slice flags
Definition: avcodec.h:1755
static int get_avc_nalsize(H264Context *h, const uint8_t *buf, int buf_size, int *buf_index)
Definition: h264.h:1113
Describe the class of an AVClass context structure.
Definition: log.h:66
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:564
static av_cold int h264_decode_end(AVCodecContext *avctx)
Definition: h264.c:1933
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:251
int8_t * ref_index[2]
Definition: h264.h:310
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:410
int pixel_shift
0 for 8-bit H264, 1 for high-bit-depth H264
Definition: h264.h:354
int mmco_reset
MMCO_RESET set this 1.
Definition: h264.h:315
H264Picture * cur_pic_ptr
Definition: h264.h:350
void ff_vdpau_h264_picture_start(H264Context *h)
int frame_packing_arrangement_cancel_flag
is previous arrangement canceled, -1 if never received
Definition: h264.h:263
#define FF_PROFILE_H264_CAVLC_444
Definition: avcodec.h:2876
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:89
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: h264.h:181
6: bottom field, top field, bottom field repeated, in that order
Definition: h264.h:152
#define FF_PROFILE_H264_INTRA
Definition: avcodec.h:2862
static int is_extra(const uint8_t *buf, int buf_size)
Definition: h264.c:1762
AVCodecContext * avctx
static const uint8_t start_code[]
Definition: h264.c:1326
void ff_h264_draw_horiz_band(H264Context *h, int y, int height)
Definition: h264.c:97
Views are on top of each other.
Definition: stereo3d.h:55
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:377
int pic_struct_present_flag
Definition: h264.h:220
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:32
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:1920
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:174
int has_recovery_point
Definition: h264.h:737
Views are next to each other.
Definition: stereo3d.h:45
#define MAX_MBPAIR_SIZE
Definition: h264.h:56
#define CONFIG_ERROR_RESILIENCE
Definition: config.h:484
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:520
static void idr(H264Context *h)
instantaneous decoder refresh.
Definition: h264.c:1058
int ff_h264_decode_seq_parameter_set(H264Context *h, int ignore_truncation)
Decode SPS.
Definition: h264_ps.c:300
discard all non reference
Definition: avcodec.h:663
AVBufferPool * qscale_table_pool
Definition: h264.h:759
H264Picture * next_output_pic
Definition: h264.h:606
int slice_context_count
Definition: h264.h:641
AVBufferPool * motion_val_pool
Definition: h264.h:761
#define FF_PROFILE_H264_HIGH
Definition: avcodec.h:2868
uint8_t * rbsp_buffer[2]
Definition: h264.h:558
#define SLICE_SINGLETHREAD
Definition: h264.h:1145
#define tprintf(p,...)
Definition: get_bits.h:692
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:868
common internal api header.
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:864
int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
Definition: h264.c:566
#define FRAME_RECOVERED_SEI
Sufficient number of frames have been decoded since a SEI recovery point, so all the following frames...
Definition: h264.h:733
int ff_h264_field_end(H264Context *h, int in_setup)
Definition: h264_picture.c:155
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:129
uint16_t * slice_table_base
Definition: h264.h:575
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:179
int missing_fields
Definition: h264.h:739
int16_t * dc_val[3]
H.264 / AVC / MPEG4 part10 motion vector predicion.
Bi-dir predicted.
Definition: avutil.h:269
const char * ff_h264_sei_stereo_mode(H264Context *h)
Get stereo_mode string from the h264 frame_packing_arrangement.
Definition: h264_sei.c:359
AVProfile.
Definition: avcodec.h:3161
int ff_h264_context_init(H264Context *h)
Init context Allocate buffers which are not shared amongst multiple threads.
Definition: h264.c:492
int workaround_bugs
Work around bugs in encoders which sometimes cannot be detected automatically.
Definition: avcodec.h:2507
Definition: h264.h:119
int cur_chroma_format_idc
Definition: h264.h:748
int den
denominator
Definition: rational.h:45
int sei_ct_type
Bit set of clock types for fields/frames in picture timing SEI message.
Definition: h264.h:688
int bit_depth_luma
bit_depth_luma_minus8 + 8
Definition: h264.h:226
#define SLICE_FLAG_CODED_ORDER
draw_horiz_band() is called in coded order instead of display
Definition: avcodec.h:1756
#define FF_PROFILE_H264_CONSTRAINED_BASELINE
Definition: avcodec.h:2865
void * priv_data
Definition: avcodec.h:1281
#define PICT_FRAME
Definition: mpegutils.h:35
int prev_poc_lsb
poc_lsb of the last reference pic for POC type 0
Definition: h264.h:584
#define CONFIG_H264_VDPAU_DECODER
Definition: config.h:643
#define SLICE_SKIPED
Definition: h264.h:1146
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:364
const uint16_t ff_h264_mb_sizes[4]
Definition: h264.c:54
uint8_t non_zero_count_cache[15 *8]
non zero coeff count cache.
Definition: h264.h:406
uint8_t(*[2] top_borders)[(16 *3)*2]
Definition: h264.h:400
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1289
#define FF_PROFILE_H264_BASELINE
Definition: avcodec.h:2864
Views are packed in a checkerboard-like structure per pixel.
Definition: stereo3d.h:76
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: avcodec.h:2543
H264Picture cur_pic
Definition: h264.h:351
int sei_display_orientation_present
display orientation SEI message
Definition: h264.h:679
int content_interpretation_type
Definition: h264.h:673
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:229
Views are packed per column.
Definition: stereo3d.h:107
int mb_width
Definition: h264.h:541
enum AVPictureType pict_type
Definition: h264.h:649
int current_slice
current slice number, used to initialize slice_num of each thread/context
Definition: h264.h:631
#define FF_PROFILE_H264_HIGH_422
Definition: avcodec.h:2871
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1340
uint32_t * mb2b_xy
Definition: h264.h:429
int delta_poc_bottom
Definition: h264.h:580
H264Picture last_pic_for_ec
Definition: h264.h:352
int au_pps_id
pps_id of current access unit
Definition: h264.h:440
Definition: h264.h:117
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
H264DSPContext h264dsp
Definition: h264.h:343
int height
Definition: frame.h:212
int crop_top
Definition: h264.h:333
#define av_freep(p)
static int init_thread_copy(AVCodecContext *avctx)
Definition: alac.c:645
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:325
int chroma_weight_flag[2]
7.4.3.2 chroma_weight_lX_flag
Definition: h264.h:742
Definition: h264.h:118
int8_t * intra4x4_pred_mode
Definition: h264.h:394
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:3352
8: frame tripling
Definition: h264.h:154
#define AV_RN64A(p)
Definition: intreadwrite.h:530
uint8_t(* non_zero_count)[48]
Definition: h264.h:408
exp golomb vlc stuff
uint8_t * mbskip_table
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
Definition: avcodec.h:1137
int sei_recovery_frame_cnt
recovery_frame_cnt from SEI message
Definition: h264.h:707
int droppable
Definition: h264.h:365
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2541
#define STARTCODE_TEST
int nal_ref_idc
Definition: h264.h:556
for(j=16;j >0;--j)
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:138
void ff_h264_hl_decode_mb(H264Context *h)
Definition: h264_mb.c:809
int b_stride
Definition: h264.h:431
unsigned int rbsp_buffer_size[2]
Definition: h264.h:559
Context Adaptive Binary Arithmetic Coder.
int8_t ref_cache[2][5 *8]
Definition: h264.h:414
int mb_mbaff
mb_aff_frame && mb_field_decoding_flag
Definition: h264.h:456
static const AVProfile profiles[]
Definition: h264.c:1946
void ff_h264_init_dequant_tables(H264Context *h)
Definition: h264_slice.c:365