2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
24 * H.264 / AVC / MPEG4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #define UNCHECKED_BITSTREAM_READER 1
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"
38 #include "cabac_functions.h"
39 #include "error_resilience.h"
43 #include "h264chroma.h"
44 #include "h264_mvpred.h"
48 #include "mpegutils.h"
49 #include "rectangle.h"
52 #include "vdpau_internal.h"
54 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
56 int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
58 H264Context *h = avctx->priv_data;
59 return h ? h->sps.num_reorder_frames : 0;
62 static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
64 int mb_x, int mb_y, int mb_intra, int mb_skipped)
66 H264Context *h = opaque;
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));
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])
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");
83 if ((h->ref_list[0][ref].reference&3) != 3) {
84 av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
87 fill_rectangle(&h->cur_pic.ref_index[0][4 * h->mb_xy],
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);
93 h->mb_field_decoding_flag = 0;
94 ff_h264_hl_decode_mb(h);
97 void ff_h264_draw_horiz_band(H264Context *h, int y, int height)
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;
110 height = FFMIN(height, avctx->height - y);
112 if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
115 if (avctx->draw_horiz_band) {
117 int offset[AV_NUM_DATA_POINTERS];
120 if (cur->pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
121 (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
128 offset[0] = y * src->linesize[0];
130 offset[2] = (y >> vshift) * src->linesize[1];
131 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
136 avctx->draw_horiz_band(avctx, src, offset,
137 y, h->picture_structure, height);
142 * Check if the top & left blocks are available if needed and
143 * change the dc mode so it only uses the available blocks.
145 int ff_h264_check_intra4x4_pred_mode(H264Context *h)
147 static const int8_t top[12] = {
148 -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
150 static const int8_t left[12] = {
151 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
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]];
159 av_log(h->avctx, AV_LOG_ERROR,
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;
164 h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
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]];
175 av_log(h->avctx, AV_LOG_ERROR,
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;
180 h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
186 } // FIXME cleanup like ff_h264_check_intra_pred_mode
189 * Check if the top & left blocks are available if needed and
190 * change the dc mode so it only uses the available blocks.
192 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
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 };
198 av_log(h->avctx, AV_LOG_ERROR,
199 "out of range intra chroma pred mode at %d %d\n",
201 return AVERROR_INVALIDDATA;
204 if (!(h->top_samples_available & 0x8000)) {
207 av_log(h->avctx, AV_LOG_ERROR,
208 "top block unavailable for requested intra mode at %d %d\n",
210 return AVERROR_INVALIDDATA;
214 if ((h->left_samples_available & 0x8080) != 0x8080) {
217 av_log(h->avctx, AV_LOG_ERROR,
218 "left block unavailable for requested intra mode at %d %d\n",
220 return AVERROR_INVALIDDATA;
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);
233 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
234 int *dst_length, int *consumed, int length)
240 // src[0]&0x80; // forbidden bit
241 h->nal_ref_idc = src[0] >> 5;
242 h->nal_unit_type = src[0] & 0x1F;
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 */ \
256 #if HAVE_FAST_UNALIGNED
257 #define FIND_FIRST_ZERO \
258 if (i > 0 && !src[i]) \
264 for (i = 0; i + 1 < length; i += 9) {
265 if (!((~AV_RN64A(src + i) &
266 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
267 0x8000800080008080ULL))
274 for (i = 0; i + 1 < length; i += 5) {
275 if (!((~AV_RN32A(src + i) &
276 (AV_RN32A(src + i) - 0x01000101U)) &
285 for (i = 0; i + 1 < length; i += 2) {
288 if (i > 0 && src[i - 1] == 0)
294 // use second escape buffer for inter data
295 bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
297 av_fast_padded_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+MAX_MBPAIR_SIZE);
298 dst = h->rbsp_buffer[bufidx];
303 if(i>=length-1){ //no escaped 0
305 *consumed= length+1; //+1 for the header
306 if(h->avctx->flags2 & CODEC_FLAG2_FAST){
309 memcpy(dst, src, length);
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
327 } else // next start code
331 dst[di++] = src[si++];
334 dst[di++] = src[si++];
337 memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
340 *consumed = si + 1; // +1 for the header
341 /* FIXME store exact number of bits in the getbitcontext
342 * (it is needed for decoding) */
347 * Identify the exact end of the bitstream
348 * @return the length of the trailing, or 0 if damaged
350 static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
355 tprintf(h->avctx, "rbsp trailing %X\n", v);
357 for (r = 1; r < 9; r++) {
365 void ff_h264_free_tables(H264Context *h, int free_rbsp)
370 av_freep(&h->intra4x4_pred_mode);
371 av_freep(&h->chroma_pred_mode_table);
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);
376 av_freep(&h->non_zero_count);
377 av_freep(&h->slice_table_base);
378 h->slice_table = NULL;
379 av_freep(&h->list_counts);
381 av_freep(&h->mb2b_xy);
382 av_freep(&h->mb2br_xy);
384 av_buffer_pool_uninit(&h->qscale_table_pool);
385 av_buffer_pool_uninit(&h->mb_type_pool);
386 av_buffer_pool_uninit(&h->motion_val_pool);
387 av_buffer_pool_uninit(&h->ref_index_pool);
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));
395 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
396 h->DPB[i].needs_realloc = 1;
399 h->cur_pic_ptr = NULL;
401 for (i = 0; i < H264_MAX_THREADS; i++) {
402 hx = h->thread_context[i];
405 av_freep(&hx->top_borders[1]);
406 av_freep(&hx->top_borders[0]);
407 av_freep(&hx->bipred_scratchpad);
408 av_freep(&hx->edge_emu_buffer);
409 av_freep(&hx->dc_val_base);
410 av_freep(&hx->er.mb_index2xy);
411 av_freep(&hx->er.error_status_table);
412 av_freep(&hx->er.er_temp_buffer);
413 av_freep(&hx->er.mbintra_table);
414 av_freep(&hx->er.mbskip_table);
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;
423 av_freep(&h->thread_context[i]);
427 int ff_h264_alloc_tables(H264Context *h)
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);
433 FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
434 row_mb_num, 8 * sizeof(uint8_t), fail)
435 FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
436 big_mb_num * 48 * sizeof(uint8_t), fail)
437 FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
438 (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
439 FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
440 big_mb_num * sizeof(uint16_t), fail)
441 FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
442 big_mb_num * sizeof(uint8_t), fail)
443 FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->mvd_table[0],
444 row_mb_num, 16 * sizeof(uint8_t), fail);
445 FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->mvd_table[1],
446 row_mb_num, 16 * sizeof(uint8_t), fail);
447 FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
448 4 * big_mb_num * sizeof(uint8_t), fail);
449 FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
450 big_mb_num * sizeof(uint8_t), fail)
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;
456 FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
457 big_mb_num * sizeof(uint32_t), fail);
458 FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
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;
465 h->mb2b_xy[mb_xy] = b_xy;
466 h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
469 if (!h->dequant4_coeff[0])
470 h264_init_dequant_tables(h);
473 h->DPB = av_mallocz_array(H264_MAX_PICTURE_COUNT, sizeof(*h->DPB));
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);
484 ff_h264_free_tables(h, 1);
485 return AVERROR(ENOMEM);
490 * Allocate buffers which are not shared amongst multiple threads.
492 int ff_h264_context_init(H264Context *h)
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;
501 FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->top_borders[0],
502 h->mb_width, 16 * 3 * sizeof(uint8_t) * 2, fail)
503 FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->top_borders[1],
504 h->mb_width, 16 * 3 * sizeof(uint8_t) * 2, fail)
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;
513 if (CONFIG_ERROR_RESILIENCE) {
515 er->avctx = h->avctx;
516 er->decode_mb = h264_er_decode_mb;
518 er->quarter_sample = 1;
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;
526 // error resilience code looks cleaner with this
527 FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy,
528 (h->mb_num + 1) * sizeof(int), fail);
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;
534 er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
535 h->mb_stride + h->mb_width;
537 FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
538 mb_array_size * sizeof(uint8_t), fail);
540 FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
541 memset(er->mbintra_table, 1, mb_array_size);
543 FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
545 FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
546 h->mb_height * h->mb_stride, fail);
548 FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base,
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;
560 return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
563 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
564 int parse_extradata);
566 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
568 AVCodecContext *avctx = h->avctx;
571 if (!buf || size <= 0)
576 const unsigned char *p = buf;
581 av_log(avctx, AV_LOG_ERROR,
582 "avcC %d too short\n", size);
583 return AVERROR_INVALIDDATA;
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
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);
597 av_log(avctx, AV_LOG_ERROR,
598 "Decoding sps %d from avcC failed\n", i);
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);
611 av_log(avctx, AV_LOG_ERROR,
612 "Decoding pps %d from avcC failed\n", i);
617 // Store right nal length size that will be used to parse all other nals
618 h->nal_length_size = (buf[4] & 0x03) + 1;
621 ret = decode_nal_units(h, buf, size, 1);
628 av_cold int ff_h264_decode_init(AVCodecContext *avctx)
630 H264Context *h = avctx->priv_data;
636 h->bit_depth_luma = 8;
637 h->chroma_format_idc = 1;
639 h->avctx->bits_per_raw_sample = 8;
640 h->cur_chroma_format_idc = 1;
642 ff_h264dsp_init(&h->h264dsp, 8, 1);
643 av_assert0(h->sps.bit_depth_chroma == 0);
644 ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
645 ff_h264qpel_init(&h->h264qpel, 8);
646 ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
648 h->dequant_coeff_pps = -1;
649 h->current_sps_id = -1;
651 /* needed so that IDCT permutation is known early */
652 ff_videodsp_init(&h->vdsp, 8);
654 memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
655 memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
657 h->picture_structure = PICT_FRAME;
658 h->slice_context_count = 1;
659 h->workaround_bugs = avctx->workaround_bugs;
660 h->flags = avctx->flags;
663 // s->decode_mb = ff_h263_decode_mb;
664 if (!avctx->has_b_frames)
667 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
669 ff_h264_decode_init_vlc();
671 ff_init_cabac_states();
674 h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
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;
683 h->sei_fpa.frame_packing_arrangement_cancel_flag = -1;
684 ff_h264_reset_sei(h);
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;
690 h->avctx->time_base.num /= 2;
692 avctx->ticks_per_frame = 2;
695 if (avctx->extradata_size > 0 && avctx->extradata) {
696 ret = ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
698 ff_h264_free_context(h);
703 if (h->sps.bitstream_restriction_flag &&
704 h->avctx->has_b_frames < h->sps.num_reorder_frames) {
705 h->avctx->has_b_frames = h->sps.num_reorder_frames;
709 avctx->internal->allocate_progress = 1;
711 ff_h264_flush_change(h);
716 static int decode_init_thread_copy(AVCodecContext *avctx)
718 H264Context *h = avctx->priv_data;
720 if (!avctx->internal->is_copy)
722 memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
723 memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
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;
736 * Run setup operations that must be run after slice header decoding.
737 * This includes finding the next displayed frame.
739 * @param h h264 master context
740 * @param setup_finished enough NALs have been read that we can call
741 * ff_thread_finish_setup()
743 static void decode_postinit(H264Context *h, int setup_finished)
745 H264Picture *out = h->cur_pic_ptr;
746 H264Picture *cur = h->cur_pic_ptr;
747 int i, pics, out_of_order, out_idx;
749 h->cur_pic_ptr->f.pict_type = h->pict_type;
751 if (h->next_output_pic)
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)
763 if (h->avctx->hwaccel || h->missing_fields <=1)
767 cur->f.interlaced_frame = 0;
768 cur->f.repeat_pict = 0;
770 /* Signal interlacing information externally. */
771 /* Prioritize picture timing SEI information over used
772 * decoding process if it exists. */
774 if (h->sps.pic_struct_present_flag) {
775 switch (h->sei_pic_struct) {
776 case SEI_PIC_STRUCT_FRAME:
778 case SEI_PIC_STRUCT_TOP_FIELD:
779 case SEI_PIC_STRUCT_BOTTOM_FIELD:
780 cur->f.interlaced_frame = 1;
782 case SEI_PIC_STRUCT_TOP_BOTTOM:
783 case SEI_PIC_STRUCT_BOTTOM_TOP:
784 if (FIELD_OR_MBAFF_PICTURE(h))
785 cur->f.interlaced_frame = 1;
787 // try to flag soft telecine progressive
788 cur->f.interlaced_frame = h->prev_interlaced_frame;
790 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
791 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
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;
797 case SEI_PIC_STRUCT_FRAME_DOUBLING:
798 cur->f.repeat_pict = 2;
800 case SEI_PIC_STRUCT_FRAME_TRIPLING:
801 cur->f.repeat_pict = 4;
805 if ((h->sei_ct_type & 3) &&
806 h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
807 cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
809 /* Derive interlacing flag from used decoding process. */
810 cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
812 h->prev_interlaced_frame = cur->f.interlaced_frame;
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];
818 if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
819 /* Use picture timing SEI information. Even if it is a
820 * information of a past frame, better than nothing. */
821 if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
822 h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
823 cur->f.top_field_first = 1;
825 cur->f.top_field_first = 0;
827 /* Most likely progressive */
828 cur->f.top_field_first = 0;
832 if (h->sei_frame_packing_present &&
833 h->frame_packing_arrangement_type >= 0 &&
834 h->frame_packing_arrangement_type <= 6 &&
835 h->content_interpretation_type > 0 &&
836 h->content_interpretation_type < 3) {
837 AVStereo3D *stereo = av_stereo3d_create_side_data(&cur->f);
839 switch (h->frame_packing_arrangement_type) {
841 stereo->type = AV_STEREO3D_CHECKERBOARD;
844 stereo->type = AV_STEREO3D_COLUMNS;
847 stereo->type = AV_STEREO3D_LINES;
850 if (h->quincunx_subsampling)
851 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
853 stereo->type = AV_STEREO3D_SIDEBYSIDE;
856 stereo->type = AV_STEREO3D_TOPBOTTOM;
859 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
862 stereo->type = AV_STEREO3D_2D;
866 if (h->content_interpretation_type == 2)
867 stereo->flags = AV_STEREO3D_FLAG_INVERT;
871 if (h->sei_display_orientation_present &&
872 (h->sei_anticlockwise_rotation || h->sei_hflip || h->sei_vflip)) {
873 double angle = h->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
874 AVFrameSideData *rotation = av_frame_new_side_data(&cur->f,
875 AV_FRAME_DATA_DISPLAYMATRIX,
876 sizeof(int32_t) * 9);
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);
884 cur->mmco_reset = h->mmco_reset;
887 // FIXME do something with unavailable reference frames
889 /* Sort B-frames into display order */
891 if (h->sps.bitstream_restriction_flag &&
892 h->avctx->has_b_frames < h->sps.num_reorder_frames) {
893 h->avctx->has_b_frames = h->sps.num_reorder_frames;
897 if (h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
898 !h->sps.bitstream_restriction_flag) {
899 h->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
903 for (i = 0; 1; i++) {
904 if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
906 h->last_pocs[i-1] = cur->poc;
909 h->last_pocs[i-1]= h->last_pocs[i];
912 out_of_order = MAX_DELAYED_PIC_COUNT - i;
913 if( cur->f.pict_type == AV_PICTURE_TYPE_B
914 || (h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > INT_MIN && h->last_pocs[MAX_DELAYED_PIC_COUNT-1] - h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > 2))
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;
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;
929 while (h->delayed_pic[pics])
932 av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
934 h->delayed_pic[pics++] = cur;
935 if (cur->reference == 0)
936 cur->reference = DELAYED_PIC_REF;
938 out = h->delayed_pic[0];
940 for (i = 1; h->delayed_pic[i] &&
941 !h->delayed_pic[i]->f.key_frame &&
942 !h->delayed_pic[i]->mmco_reset;
944 if (h->delayed_pic[i]->poc < out->poc) {
945 out = h->delayed_pic[i];
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;
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];
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;
965 h->next_outputed_poc = out->poc;
967 av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
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".
974 h->frame_recovered |= FRAME_RECOVERED_SEI;
976 h->next_output_pic->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
979 if (setup_finished && !h->avctx->hwaccel)
980 ff_thread_finish_setup(h->avctx);
983 int ff_pred_weight_table(H264Context *h)
986 int luma_def, chroma_def;
989 h->use_weight_chroma = 0;
990 h->luma_log2_weight_denom = get_ue_golomb(&h->gb);
991 if (h->sps.chroma_format_idc)
992 h->chroma_log2_weight_denom = get_ue_golomb(&h->gb);
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;
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;
1003 luma_def = 1 << h->luma_log2_weight_denom;
1004 chroma_def = 1 << h->chroma_log2_weight_denom;
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;
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) {
1019 h->luma_weight_flag[list] = 1;
1022 h->luma_weight[i][list][0] = luma_def;
1023 h->luma_weight[i][list][1] = 0;
1026 if (h->sps.chroma_format_idc) {
1027 chroma_weight_flag = get_bits1(&h->gb);
1028 if (chroma_weight_flag) {
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;
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;
1048 if (h->slice_type_nos != AV_PICTURE_TYPE_B)
1051 h->use_weight = h->use_weight || h->use_weight_chroma;
1056 * instantaneous decoder refresh.
1058 static void idr(H264Context *h)
1061 ff_h264_remove_all_refs(h);
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;
1070 /* forget old pics after a seek */
1071 void ff_h264_flush_change(H264Context *h)
1075 h->outputed_poc = h->next_outputed_poc = INT_MIN;
1076 h->prev_interlaced_frame = 1;
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;
1087 ff_h264_unref_picture(h, &h->last_pic_for_ec);
1090 ff_h264_reset_sei(h);
1091 h->recovery_frame = -1;
1092 h->frame_recovered = 0;
1094 h->current_slice = 0;
1098 /* forget old pics after a seek */
1099 static void flush_dpb(AVCodecContext *avctx)
1101 H264Context *h = avctx->priv_data;
1104 memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
1106 ff_h264_flush_change(h);
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;
1112 ff_h264_unref_picture(h, &h->cur_pic);
1114 h->mb_x = h->mb_y = 0;
1116 ff_h264_free_tables(h, 1);
1117 h->context_initialized = 0;
1120 int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
1122 const int max_frame_num = 1 << h->sps.log2_max_frame_num;
1125 h->frame_num_offset = h->prev_frame_num_offset;
1126 if (h->frame_num < h->prev_frame_num)
1127 h->frame_num_offset += max_frame_num;
1129 if (h->sps.poc_type == 0) {
1130 const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
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;
1139 h->poc_msb = h->prev_poc_msb;
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;
1148 if (h->sps.poc_cycle_length != 0)
1149 abs_frame_num = h->frame_num_offset + h->frame_num;
1153 if (h->nal_ref_idc == 0 && abs_frame_num > 0)
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];
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;
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];
1171 if (h->nal_ref_idc == 0)
1172 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
1174 field_poc[0] = expectedpoc + h->delta_poc[0];
1175 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
1177 if (h->picture_structure == PICT_FRAME)
1178 field_poc[1] += h->delta_poc[1];
1180 int poc = 2 * (h->frame_num_offset + h->frame_num);
1182 if (!h->nal_ref_idc)
1189 if (h->picture_structure != PICT_BOTTOM_FIELD)
1190 pic_field_poc[0] = field_poc[0];
1191 if (h->picture_structure != PICT_TOP_FIELD)
1192 pic_field_poc[1] = field_poc[1];
1193 *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
1199 * Compute profile from profile_idc and constraint_set?_flags.
1203 * @return profile as defined by FF_PROFILE_H264_*
1205 int ff_h264_get_profile(SPS *sps)
1207 int profile = sps->profile_idc;
1209 switch (sps->profile_idc) {
1210 case FF_PROFILE_H264_BASELINE:
1211 // constraint_set1_flag set to 1
1212 profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
1214 case FF_PROFILE_H264_HIGH_10:
1215 case FF_PROFILE_H264_HIGH_422:
1216 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
1217 // constraint_set3_flag set to 1
1218 profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
1225 int ff_h264_set_parameter_from_sps(H264Context *h)
1227 if (h->flags & CODEC_FLAG_LOW_DELAY ||
1228 (h->sps.bitstream_restriction_flag &&
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");
1237 if (h->avctx->has_b_frames < 2)
1238 h->avctx->has_b_frames = !h->low_delay;
1240 if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
1241 h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
1242 if (h->avctx->codec &&
1243 h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU &&
1244 (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
1245 av_log(h->avctx, AV_LOG_ERROR,
1246 "VDPAU decoding does not support video colorspace.\n");
1247 return AVERROR_INVALIDDATA;
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) {
1251 h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
1252 h->cur_chroma_format_idc = h->sps.chroma_format_idc;
1253 h->pixel_shift = h->sps.bit_depth_luma > 8;
1255 ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma,
1256 h->sps.chroma_format_idc);
1257 ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
1258 ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma);
1259 ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma,
1260 h->sps.chroma_format_idc);
1262 ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma);
1264 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
1265 h->sps.bit_depth_luma);
1266 return AVERROR_INVALIDDATA;
1272 int ff_set_ref_count(H264Context *h)
1274 int ref_count[2], list_count;
1275 int num_ref_idx_active_override_flag;
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];
1281 if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
1283 max[0] = max[1] = h->picture_structure == PICT_FRAME ? 15 : 31;
1285 if (h->slice_type_nos == AV_PICTURE_TYPE_B)
1286 h->direct_spatial_mv_pred = get_bits1(&h->gb);
1287 num_ref_idx_active_override_flag = get_bits1(&h->gb);
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;
1294 // full range is spec-ok in this case, even for frames
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;
1302 return AVERROR_INVALIDDATA;
1305 if (h->slice_type_nos == AV_PICTURE_TYPE_B)
1311 ref_count[0] = ref_count[1] = 0;
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;
1326 static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
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)
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)
1335 h->workaround_bugs |= FF_BUG_TRUNCATED;
1337 if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
1338 while (dst_length > 0 && ptr[dst_length - 1] == 0)
1344 return 8 * dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1);
1347 static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
1349 int next_avc = h->is_avc ? 0 : buf_size;
1352 int nals_needed = 0;
1353 int first_slice = 0;
1357 int dst_length, bit_length, consumed;
1360 if (buf_index >= next_avc) {
1361 nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
1364 next_avc = buf_index + nalsize;
1366 buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
1367 if (buf_index >= buf_size)
1369 if (buf_index >= next_avc)
1373 ptr = ff_h264_decode_nal(h, buf + buf_index, &dst_length, &consumed,
1374 next_avc - buf_index);
1376 if (!ptr || dst_length < 0)
1377 return AVERROR_INVALIDDATA;
1379 buf_index += consumed;
1381 bit_length = get_bit_length(h, buf, ptr, dst_length,
1382 buf_index, next_avc);
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) {
1392 nals_needed = nal_index;
1397 init_get_bits(&h->gb, ptr, bit_length);
1398 if (!get_ue_golomb(&h->gb) ||
1400 first_slice != h->nal_unit_type)
1401 nals_needed = nal_index;
1403 first_slice = h->nal_unit_type;
1410 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
1411 int parse_extradata)
1413 AVCodecContext *const avctx = h->avctx;
1414 H264Context *hx; ///< thread context
1416 unsigned context_count;
1418 int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
1423 h->nal_unit_type= 0;
1425 if(!h->slice_context_count)
1426 h->slice_context_count= 1;
1427 h->max_contexts = h->slice_context_count;
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);
1435 if (h->nal_length_size == 4) {
1436 if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
1438 }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
1442 if (avctx->active_thread_type & FF_THREAD_FRAME)
1443 nals_needed = get_last_needed_nal(h, buf, buf_size);
1448 next_avc = h->is_avc ? 0 : buf_size;
1458 if (buf_index >= next_avc) {
1459 nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
1462 next_avc = buf_index + nalsize;
1464 buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
1465 if (buf_index >= buf_size)
1467 if (buf_index >= next_avc)
1471 hx = h->thread_context[context_count];
1473 ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
1474 &consumed, next_avc - buf_index);
1475 if (!ptr || dst_length < 0) {
1480 bit_length = get_bit_length(h, buf, ptr, dst_length,
1481 buf_index + consumed, next_avc);
1483 if (h->avctx->debug & FF_DEBUG_STARTCODE)
1484 av_log(h->avctx, AV_LOG_DEBUG,
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);
1488 if (h->is_avc && (nalsize != consumed) && nalsize)
1489 av_log(h->avctx, AV_LOG_DEBUG,
1490 "AVC: Consumed only %d bytes instead of %d\n",
1493 buf_index += consumed;
1496 if (avctx->skip_frame >= AVDISCARD_NONREF &&
1497 h->nal_ref_idc == 0 &&
1498 h->nal_unit_type != NAL_SEI)
1502 if ( (!(avctx->active_thread_type & FF_THREAD_FRAME) || nals_needed >= nal_index)
1503 && !h->current_slice)
1505 /* Ignore per frame NAL unit type during extradata
1506 * parsing. Decoding slices is not possible in codec init
1508 if (parse_extradata) {
1509 switch (hx->nal_unit_type) {
1515 av_log(h->avctx, AV_LOG_WARNING,
1516 "Ignoring NAL %d in global header/extradata\n",
1518 // fall through to next case
1519 case NAL_AUXILIARY_SLICE:
1520 hx->nal_unit_type = NAL_FF_IGNORE;
1526 switch (hx->nal_unit_type) {
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;
1534 if (h->nal_unit_type != NAL_IDR_SLICE) {
1535 av_log(h->avctx, AV_LOG_ERROR,
1536 "Invalid mix of idr and non-idr slices\n");
1541 idr(h); // FIXME ensure we don't lose some frames if there is reordering
1543 h->has_recovery_point = 1;
1545 init_get_bits(&hx->gb, ptr, bit_length);
1547 hx->inter_gb_ptr = &hx->gb;
1549 if ((err = ff_h264_decode_slice_header(hx, h)))
1552 if (h->sei_recovery_frame_cnt >= 0) {
1553 if (h->frame_num != h->sei_recovery_frame_cnt || hx->slice_type_nos != AV_PICTURE_TYPE_I)
1554 h->valid_recovery_point = 1;
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) {
1558 h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
1559 ((1 << h->sps.log2_max_frame_num) - 1);
1561 if (!h->valid_recovery_point)
1562 h->recovery_frame = h->frame_num;
1566 h->cur_pic_ptr->f.key_frame |=
1567 (hx->nal_unit_type == NAL_IDR_SLICE);
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;
1574 // If we have an IDR, all frames after it in decoded order are
1576 if (hx->nal_unit_type == NAL_IDR_SLICE)
1577 h->frame_recovered |= FRAME_RECOVERED_IDR;
1578 h->frame_recovered |= 3*!!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL);
1579 h->frame_recovered |= 3*!!(avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT);
1581 h->cur_pic_ptr->recovered |= h->frame_recovered;
1583 h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
1586 if (h->current_slice == 1) {
1587 if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
1588 decode_postinit(h, nal_index >= nals_needed);
1590 if (h->avctx->hwaccel &&
1591 (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
1593 if (CONFIG_H264_VDPAU_DECODER &&
1594 h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
1595 ff_vdpau_h264_picture_start(h);
1598 if (hx->redundant_pic_count == 0) {
1599 if (avctx->hwaccel) {
1600 ret = avctx->hwaccel->decode_slice(avctx,
1601 &buf[buf_index - consumed],
1605 } else if (CONFIG_H264_VDPAU_DECODER &&
1606 h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
1607 ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0],
1609 sizeof(start_code));
1610 ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0],
1611 &buf[buf_index - consumed],
1620 avpriv_request_sample(avctx, "data partitioning");
1621 ret = AVERROR(ENOSYS);
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))
1631 init_get_bits(&h->gb, ptr, bit_length);
1632 if (ff_h264_decode_seq_parameter_set(h) < 0 && (h->is_avc ? nalsize : 1)) {
1633 av_log(h->avctx, AV_LOG_DEBUG,
1634 "SPS decoding failure, trying again with the complete NAL\n");
1636 av_assert0(next_avc - buf_index + consumed == nalsize);
1637 if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
1639 init_get_bits(&h->gb, &buf[buf_index + 1 - consumed],
1640 8*(next_avc - buf_index + consumed - 1));
1641 ff_h264_decode_seq_parameter_set(h);
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))
1652 case NAL_END_SEQUENCE:
1653 case NAL_END_STREAM:
1654 case NAL_FILLER_DATA:
1656 case NAL_AUXILIARY_SLICE:
1661 av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
1662 hx->nal_unit_type, bit_length);
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))
1672 if (err < 0 || err == SLICE_SKIPED) {
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;
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))
1697 if (h->cur_pic_ptr && !h->droppable) {
1698 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1699 h->picture_structure == PICT_BOTTOM_FIELD);
1702 return (ret < 0) ? ret : buf_index;
1706 * Return the number of bytes consumed for building the current frame.
1708 static int get_consumed_bytes(int pos, int buf_size)
1711 pos = 1; // avoid infinite loops (I doubt that is needed but...)
1712 if (pos + 10 > buf_size)
1713 pos = buf_size; // oops ;)
1718 static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
1720 AVFrame *src = &srcp->f;
1721 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
1723 int ret = av_frame_ref(dst, src);
1727 av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(h), 0);
1729 if (srcp->sei_recovery_frame_cnt == 0)
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;
1744 static int is_extra(const uint8_t *buf, int buf_size)
1746 int cnt= buf[5]&0x1f;
1747 const uint8_t *p= buf+6;
1749 int nalsize= AV_RB16(p) + 2;
1750 if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
1758 int nalsize= AV_RB16(p) + 2;
1759 if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
1766 static int h264_decode_frame(AVCodecContext *avctx, void *data,
1767 int *got_frame, AVPacket *avpkt)
1769 const uint8_t *buf = avpkt->data;
1770 int buf_size = avpkt->size;
1771 H264Context *h = avctx->priv_data;
1772 AVFrame *pict = data;
1778 h->flags = avctx->flags;
1780 ff_h264_unref_picture(h, &h->last_pic_for_ec);
1782 /* end of stream, output what is still in the buffers */
1783 if (buf_size == 0) {
1786 h->cur_pic_ptr = NULL;
1789 // FIXME factorize this with the output code below
1790 out = h->delayed_pic[0];
1793 h->delayed_pic[i] &&
1794 !h->delayed_pic[i]->f.key_frame &&
1795 !h->delayed_pic[i]->mmco_reset;
1797 if (h->delayed_pic[i]->poc < out->poc) {
1798 out = h->delayed_pic[i];
1802 for (i = out_idx; h->delayed_pic[i]; i++)
1803 h->delayed_pic[i] = h->delayed_pic[i + 1];
1806 out->reference &= ~DELAYED_PIC_REF;
1807 ret = output_frame(h, pict, out);
1815 if (h->is_avc && av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, NULL)) {
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);
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);
1826 buf_index = decode_nal_units(h, buf, buf_size, 0);
1828 return AVERROR_INVALIDDATA;
1830 if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
1831 av_assert0(buf_index <= buf_size);
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))
1839 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1840 return AVERROR_INVALIDDATA;
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);
1848 ff_h264_field_end(h, 0);
1850 /* Wait for second field. */
1852 if (h->next_output_pic && (
1853 h->next_output_pic->recovered)) {
1854 if (!h->next_output_pic->recovered)
1855 h->next_output_pic->f.flags |= AV_FRAME_FLAG_CORRUPT;
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)
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];
1866 const uint8_t *src_data[4];
1868 av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
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];
1876 av_image_copy(dst_data, linesizes, src_data, linesizes,
1877 f->format, f->width, f->height>>1);
1880 ret = output_frame(h, pict, h->next_output_pic);
1884 if (CONFIG_MPEGVIDEO) {
1885 ff_print_debug_info2(h->avctx, pict, h->er.mbskip_table,
1886 h->next_output_pic->mb_type,
1887 h->next_output_pic->qscale_table,
1888 h->next_output_pic->motion_val,
1890 h->mb_width, h->mb_height, h->mb_stride, 1);
1895 av_assert0(pict->buf[0] || !*got_frame);
1897 ff_h264_unref_picture(h, &h->last_pic_for_ec);
1899 return get_consumed_bytes(buf_index, buf_size);
1902 av_cold void ff_h264_free_context(H264Context *h)
1906 ff_h264_free_tables(h, 1); // FIXME cleanup init stuff perhaps
1908 for (i = 0; i < MAX_SPS_COUNT; i++)
1909 av_freep(h->sps_buffers + i);
1911 for (i = 0; i < MAX_PPS_COUNT; i++)
1912 av_freep(h->pps_buffers + i);
1915 static av_cold int h264_decode_end(AVCodecContext *avctx)
1917 H264Context *h = avctx->priv_data;
1919 ff_h264_remove_all_refs(h);
1920 ff_h264_free_context(h);
1922 ff_h264_unref_picture(h, &h->cur_pic);
1923 ff_h264_unref_picture(h, &h->last_pic_for_ec);
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 },
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},
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,
1958 AVCodec ff_h264_decoder = {
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),
1964 .init = ff_h264_decode_init,
1965 .close = h264_decode_end,
1966 .decode = h264_decode_frame,
1967 .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
1968 CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
1969 CODEC_CAP_FRAME_THREADS,
1971 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
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,
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,
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),
1991 .init = ff_h264_decode_init,
1992 .close = h264_decode_end,
1993 .decode = h264_decode_frame,
1994 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
1996 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
1998 .profiles = NULL_IF_CONFIG_SMALL(profiles),
1999 .priv_class = &h264_vdpau_class,