FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #define UNCHECKED_BITSTREAM_READER 1
29 
30 #include "libavutil/avassert.h"
31 #include "libavutil/display.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/stereo3d.h"
35 #include "libavutil/timer.h"
36 #include "internal.h"
37 #include "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 
513  if (CONFIG_ERROR_RESILIENCE) {
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  if ( (!(avctx->active_thread_type & FF_THREAD_FRAME) || nals_needed >= nal_index)
1503  && !h->current_slice)
1504  h->au_pps_id = -1;
1505  /* Ignore per frame NAL unit type during extradata
1506  * parsing. Decoding slices is not possible in codec init
1507  * with frame-mt */
1508  if (parse_extradata) {
1509  switch (hx->nal_unit_type) {
1510  case NAL_IDR_SLICE:
1511  case NAL_SLICE:
1512  case NAL_DPA:
1513  case NAL_DPB:
1514  case NAL_DPC:
1516  "Ignoring NAL %d in global header/extradata\n",
1517  hx->nal_unit_type);
1518  // fall through to next case
1519  case NAL_AUXILIARY_SLICE:
1521  }
1522  }
1523 
1524  err = 0;
1525 
1526  switch (hx->nal_unit_type) {
1527  case NAL_IDR_SLICE:
1528  if ((ptr[0] & 0xFC) == 0x98) {
1529  av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
1530  h->next_outputed_poc = INT_MIN;
1531  ret = -1;
1532  goto end;
1533  }
1534  if (h->nal_unit_type != NAL_IDR_SLICE) {
1536  "Invalid mix of idr and non-idr slices\n");
1537  ret = -1;
1538  goto end;
1539  }
1540  if(!idr_cleared)
1541  idr(h); // FIXME ensure we don't lose some frames if there is reordering
1542  idr_cleared = 1;
1543  h->has_recovery_point = 1;
1544  case NAL_SLICE:
1545  init_get_bits(&hx->gb, ptr, bit_length);
1546  hx->intra_gb_ptr =
1547  hx->inter_gb_ptr = &hx->gb;
1548 
1549  if ((err = ff_h264_decode_slice_header(hx, h)))
1550  break;
1551 
1552  if (h->sei_recovery_frame_cnt >= 0) {
1554  h->valid_recovery_point = 1;
1555 
1556  if ( h->recovery_frame < 0
1557  || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt) {
1559  ((1 << h->sps.log2_max_frame_num) - 1);
1560 
1561  if (!h->valid_recovery_point)
1562  h->recovery_frame = h->frame_num;
1563  }
1564  }
1565 
1566  h->cur_pic_ptr->f.key_frame |=
1567  (hx->nal_unit_type == NAL_IDR_SLICE);
1568 
1569  if (hx->nal_unit_type == NAL_IDR_SLICE ||
1570  h->recovery_frame == h->frame_num) {
1571  h->recovery_frame = -1;
1572  h->cur_pic_ptr->recovered = 1;
1573  }
1574  // If we have an IDR, all frames after it in decoded order are
1575  // "recovered".
1576  if (hx->nal_unit_type == NAL_IDR_SLICE)
1578  h->frame_recovered |= 3*!!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL);
1579  h->frame_recovered |= 3*!!(avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT);
1580 #if 1
1582 #else
1584 #endif
1585 
1586  if (h->current_slice == 1) {
1587  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
1588  decode_postinit(h, nal_index >= nals_needed);
1589 
1590  if (h->avctx->hwaccel &&
1591  (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
1592  return ret;
1593  if (CONFIG_H264_VDPAU_DECODER &&
1596  }
1597 
1598  if (hx->redundant_pic_count == 0) {
1599  if (avctx->hwaccel) {
1600  ret = avctx->hwaccel->decode_slice(avctx,
1601  &buf[buf_index - consumed],
1602  consumed);
1603  if (ret < 0)
1604  return ret;
1605  } else if (CONFIG_H264_VDPAU_DECODER &&
1608  start_code,
1609  sizeof(start_code));
1611  &buf[buf_index - consumed],
1612  consumed);
1613  } else
1614  context_count++;
1615  }
1616  break;
1617  case NAL_DPA:
1618  case NAL_DPB:
1619  case NAL_DPC:
1620  avpriv_request_sample(avctx, "data partitioning");
1621  ret = AVERROR(ENOSYS);
1622  goto end;
1623  break;
1624  case NAL_SEI:
1625  init_get_bits(&h->gb, ptr, bit_length);
1626  ret = ff_h264_decode_sei(h);
1627  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1628  goto end;
1629  break;
1630  case NAL_SPS:
1631  init_get_bits(&h->gb, ptr, bit_length);
1632  if (ff_h264_decode_seq_parameter_set(h) < 0 && (h->is_avc ? nalsize : 1)) {
1634  "SPS decoding failure, trying again with the complete NAL\n");
1635  if (h->is_avc)
1636  av_assert0(next_avc - buf_index + consumed == nalsize);
1637  if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
1638  break;
1639  init_get_bits(&h->gb, &buf[buf_index + 1 - consumed],
1640  8*(next_avc - buf_index + consumed - 1));
1642  }
1643 
1644  break;
1645  case NAL_PPS:
1646  init_get_bits(&h->gb, ptr, bit_length);
1647  ret = ff_h264_decode_picture_parameter_set(h, bit_length);
1648  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1649  goto end;
1650  break;
1651  case NAL_AUD:
1652  case NAL_END_SEQUENCE:
1653  case NAL_END_STREAM:
1654  case NAL_FILLER_DATA:
1655  case NAL_SPS_EXT:
1656  case NAL_AUXILIARY_SLICE:
1657  break;
1658  case NAL_FF_IGNORE:
1659  break;
1660  default:
1661  av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
1662  hx->nal_unit_type, bit_length);
1663  }
1664 
1665  if (context_count == h->max_contexts) {
1666  ret = ff_h264_execute_decode_slices(h, context_count);
1667  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1668  goto end;
1669  context_count = 0;
1670  }
1671 
1672  if (err < 0 || err == SLICE_SKIPED) {
1673  if (err < 0)
1674  av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
1675  h->ref_count[0] = h->ref_count[1] = h->list_count = 0;
1676  } else if (err == SLICE_SINGLETHREAD) {
1677  /* Slice could not be decoded in parallel mode, copy down
1678  * NAL unit stuff to context 0 and restart. Note that
1679  * rbsp_buffer is not transferred, but since we no longer
1680  * run in parallel mode this should not be an issue. */
1681  h->nal_unit_type = hx->nal_unit_type;
1682  h->nal_ref_idc = hx->nal_ref_idc;
1683  hx = h;
1684  goto again;
1685  }
1686  }
1687  }
1688  if (context_count) {
1689  ret = ff_h264_execute_decode_slices(h, context_count);
1690  if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1691  goto end;
1692  }
1693 
1694  ret = 0;
1695 end:
1696  /* clean up */
1697  if (h->cur_pic_ptr && !h->droppable) {
1700  }
1701 
1702  return (ret < 0) ? ret : buf_index;
1703 }
1704 
1705 /**
1706  * Return the number of bytes consumed for building the current frame.
1707  */
1708 static int get_consumed_bytes(int pos, int buf_size)
1709 {
1710  if (pos == 0)
1711  pos = 1; // avoid infinite loops (I doubt that is needed but...)
1712  if (pos + 10 > buf_size)
1713  pos = buf_size; // oops ;)
1714 
1715  return pos;
1716 }
1717 
1719 {
1720  AVFrame *src = &srcp->f;
1721  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
1722  int i;
1723  int ret = av_frame_ref(dst, src);
1724  if (ret < 0)
1725  return ret;
1726 
1727  av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(h), 0);
1728 
1729  if (srcp->sei_recovery_frame_cnt == 0)
1730  dst->key_frame = 1;
1731  if (!srcp->crop)
1732  return 0;
1733 
1734  for (i = 0; i < desc->nb_components; i++) {
1735  int hshift = (i > 0) ? desc->log2_chroma_w : 0;
1736  int vshift = (i > 0) ? desc->log2_chroma_h : 0;
1737  int off = ((srcp->crop_left >> hshift) << h->pixel_shift) +
1738  (srcp->crop_top >> vshift) * dst->linesize[i];
1739  dst->data[i] += off;
1740  }
1741  return 0;
1742 }
1743 
1744 static int is_extra(const uint8_t *buf, int buf_size)
1745 {
1746  int cnt= buf[5]&0x1f;
1747  const uint8_t *p= buf+6;
1748  while(cnt--){
1749  int nalsize= AV_RB16(p) + 2;
1750  if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
1751  return 0;
1752  p += nalsize;
1753  }
1754  cnt = *(p++);
1755  if(!cnt)
1756  return 0;
1757  while(cnt--){
1758  int nalsize= AV_RB16(p) + 2;
1759  if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
1760  return 0;
1761  p += nalsize;
1762  }
1763  return 1;
1764 }
1765 
1766 static int h264_decode_frame(AVCodecContext *avctx, void *data,
1767  int *got_frame, AVPacket *avpkt)
1768 {
1769  const uint8_t *buf = avpkt->data;
1770  int buf_size = avpkt->size;
1771  H264Context *h = avctx->priv_data;
1772  AVFrame *pict = data;
1773  int buf_index = 0;
1774  H264Picture *out;
1775  int i, out_idx;
1776  int ret;
1777 
1778  h->flags = avctx->flags;
1779 
1781 
1782  /* end of stream, output what is still in the buffers */
1783  if (buf_size == 0) {
1784  out:
1785 
1786  h->cur_pic_ptr = NULL;
1787  h->first_field = 0;
1788 
1789  // FIXME factorize this with the output code below
1790  out = h->delayed_pic[0];
1791  out_idx = 0;
1792  for (i = 1;
1793  h->delayed_pic[i] &&
1794  !h->delayed_pic[i]->f.key_frame &&
1795  !h->delayed_pic[i]->mmco_reset;
1796  i++)
1797  if (h->delayed_pic[i]->poc < out->poc) {
1798  out = h->delayed_pic[i];
1799  out_idx = i;
1800  }
1801 
1802  for (i = out_idx; h->delayed_pic[i]; i++)
1803  h->delayed_pic[i] = h->delayed_pic[i + 1];
1804 
1805  if (out) {
1806  out->reference &= ~DELAYED_PIC_REF;
1807  ret = output_frame(h, pict, out);
1808  if (ret < 0)
1809  return ret;
1810  *got_frame = 1;
1811  }
1812 
1813  return buf_index;
1814  }
1816  int side_size;
1817  uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
1818  if (is_extra(side, side_size))
1819  ff_h264_decode_extradata(h, side, side_size);
1820  }
1821  if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
1822  if (is_extra(buf, buf_size))
1823  return ff_h264_decode_extradata(h, buf, buf_size);
1824  }
1825 
1826  buf_index = decode_nal_units(h, buf, buf_size, 0);
1827  if (buf_index < 0)
1828  return AVERROR_INVALIDDATA;
1829 
1830  if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
1831  av_assert0(buf_index <= buf_size);
1832  goto out;
1833  }
1834 
1835  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
1836  if (avctx->skip_frame >= AVDISCARD_NONREF ||
1837  buf_size >= 4 && !memcmp("Q264", buf, 4))
1838  return buf_size;
1839  av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1840  return AVERROR_INVALIDDATA;
1841  }
1842 
1843  if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
1844  (h->mb_y >= h->mb_height && h->mb_height)) {
1845  if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
1846  decode_postinit(h, 1);
1847 
1848  ff_h264_field_end(h, 0);
1849 
1850  /* Wait for second field. */
1851  *got_frame = 0;
1852  if (h->next_output_pic && (
1853  h->next_output_pic->recovered)) {
1854  if (!h->next_output_pic->recovered)
1856 
1857  if (!h->avctx->hwaccel &&
1858  (h->next_output_pic->field_poc[0] == INT_MAX ||
1859  h->next_output_pic->field_poc[1] == INT_MAX)
1860  ) {
1861  int p;
1862  AVFrame *f = &h->next_output_pic->f;
1863  int field = h->next_output_pic->field_poc[0] == INT_MAX;
1864  uint8_t *dst_data[4];
1865  int linesizes[4];
1866  const uint8_t *src_data[4];
1867 
1868  av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
1869 
1870  for (p = 0; p<4; p++) {
1871  dst_data[p] = f->data[p] + (field^1)*f->linesize[p];
1872  src_data[p] = f->data[p] + field *f->linesize[p];
1873  linesizes[p] = 2*f->linesize[p];
1874  }
1875 
1876  av_image_copy(dst_data, linesizes, src_data, linesizes,
1877  f->format, f->width, f->height>>1);
1878  }
1879 
1880  ret = output_frame(h, pict, h->next_output_pic);
1881  if (ret < 0)
1882  return ret;
1883  *got_frame = 1;
1884  if (CONFIG_MPEGVIDEO) {
1889  &h->low_delay,
1890  h->mb_width, h->mb_height, h->mb_stride, 1);
1891  }
1892  }
1893  }
1894 
1895  av_assert0(pict->buf[0] || !*got_frame);
1896 
1898 
1899  return get_consumed_bytes(buf_index, buf_size);
1900 }
1901 
1903 {
1904  int i;
1905 
1906  ff_h264_free_tables(h, 1); // FIXME cleanup init stuff perhaps
1907 
1908  for (i = 0; i < MAX_SPS_COUNT; i++)
1909  av_freep(h->sps_buffers + i);
1910 
1911  for (i = 0; i < MAX_PPS_COUNT; i++)
1912  av_freep(h->pps_buffers + i);
1913 }
1914 
1916 {
1917  H264Context *h = avctx->priv_data;
1918 
1921 
1924 
1925  return 0;
1926 }
1927 
1928 static const AVProfile profiles[] = {
1929  { FF_PROFILE_H264_BASELINE, "Baseline" },
1930  { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
1931  { FF_PROFILE_H264_MAIN, "Main" },
1932  { FF_PROFILE_H264_EXTENDED, "Extended" },
1933  { FF_PROFILE_H264_HIGH, "High" },
1934  { FF_PROFILE_H264_HIGH_10, "High 10" },
1935  { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
1936  { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
1937  { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
1938  { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
1939  { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
1940  { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
1941  { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
1942  { FF_PROFILE_UNKNOWN },
1943 };
1944 
1945 static const AVOption h264_options[] = {
1946  {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
1947  {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
1948  {NULL}
1949 };
1950 
1951 static const AVClass h264_class = {
1952  .class_name = "H264 Decoder",
1953  .item_name = av_default_item_name,
1954  .option = h264_options,
1955  .version = LIBAVUTIL_VERSION_INT,
1956 };
1957 
1959  .name = "h264",
1960  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1961  .type = AVMEDIA_TYPE_VIDEO,
1962  .id = AV_CODEC_ID_H264,
1963  .priv_data_size = sizeof(H264Context),
1965  .close = h264_decode_end,
1967  .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
1970  .flush = flush_dpb,
1972  .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
1973  .profiles = NULL_IF_CONFIG_SMALL(profiles),
1974  .priv_class = &h264_class,
1975 };
1976 
1977 #if CONFIG_H264_VDPAU_DECODER
1978 static const AVClass h264_vdpau_class = {
1979  .class_name = "H264 VDPAU Decoder",
1980  .item_name = av_default_item_name,
1981  .option = h264_options,
1982  .version = LIBAVUTIL_VERSION_INT,
1983 };
1984 
1985 AVCodec ff_h264_vdpau_decoder = {
1986  .name = "h264_vdpau",
1987  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
1988  .type = AVMEDIA_TYPE_VIDEO,
1989  .id = AV_CODEC_ID_H264,
1990  .priv_data_size = sizeof(H264Context),
1992  .close = h264_decode_end,
1995  .flush = flush_dpb,
1996  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
1997  AV_PIX_FMT_NONE},
1998  .profiles = NULL_IF_CONFIG_SMALL(profiles),
1999  .priv_class = &h264_vdpau_class,
2000 };
2001 #endif