#include "libavutil/display.h"
#include "libavutil/imgutils.h"
#include "libavutil/stereo3d.h"
-#include "libavutil/timer.h"
#include "internal.h"
#include "cabac.h"
#include "cabac_functions.h"
pic->ref_index[i] = pic->ref_index_buf[i]->data;
}
+ pic->pps_buf = av_buffer_ref(h->ps.pps_ref);
+ if (!pic->pps_buf)
+ goto fail;
+ pic->pps = (const PPS*)pic->pps_buf->data;
+
+ pic->mb_width = h->mb_width;
+ pic->mb_height = h->mb_height;
+ pic->mb_stride = h->mb_stride;
+
return 0;
fail:
ff_h264_unref_picture(h, pic);
// SPS/PPS
for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++) {
- av_buffer_unref(&h->ps.sps_list[i]);
- if (h1->ps.sps_list[i]) {
- h->ps.sps_list[i] = av_buffer_ref(h1->ps.sps_list[i]);
- if (!h->ps.sps_list[i])
- return AVERROR(ENOMEM);
- }
+ ret = av_buffer_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
+ if (ret < 0)
+ return ret;
}
for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
- av_buffer_unref(&h->ps.pps_list[i]);
- if (h1->ps.pps_list[i]) {
- h->ps.pps_list[i] = av_buffer_ref(h1->ps.pps_list[i]);
- if (!h->ps.pps_list[i])
- return AVERROR(ENOMEM);
- }
+ ret = av_buffer_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
+ if (ret < 0)
+ return ret;
}
- av_buffer_unref(&h->ps.pps_ref);
- av_buffer_unref(&h->ps.sps_ref);
+ ret = av_buffer_replace(&h->ps.pps_ref, h1->ps.pps_ref);
+ if (ret < 0)
+ return ret;
h->ps.pps = NULL;
h->ps.sps = NULL;
if (h1->ps.pps_ref) {
- h->ps.pps_ref = av_buffer_ref(h1->ps.pps_ref);
- if (!h->ps.pps_ref)
- return AVERROR(ENOMEM);
h->ps.pps = (const PPS*)h->ps.pps_ref->data;
- }
- if (h1->ps.sps_ref) {
- h->ps.sps_ref = av_buffer_ref(h1->ps.sps_ref);
- if (!h->ps.sps_ref)
- return AVERROR(ENOMEM);
- h->ps.sps = (const SPS*)h->ps.sps_ref->data;
+ h->ps.sps = h->ps.pps->sps;
}
if (need_reinit || !inited) {
h->mb_num = h1->mb_num;
h->mb_stride = h1->mb_stride;
h->b_stride = h1->b_stride;
+ h->x264_build = h1->x264_build;
if (h->context_initialized || h1->context_initialized) {
if ((err = h264_slice_header_init(h)) < 0) {
h->enable_er = h1->enable_er;
h->workaround_bugs = h1->workaround_bugs;
- h->x264_build = h1->x264_build;
h->droppable = h1->droppable;
// extradata/NAL handling
h->frame_recovered = h1->frame_recovered;
+ ret = av_buffer_replace(&h->sei.a53_caption.buf_ref, h1->sei.a53_caption.buf_ref);
+ if (ret < 0)
+ return ret;
+
+ for (i = 0; i < h->sei.unregistered.nb_buf_ref; i++)
+ av_buffer_unref(&h->sei.unregistered.buf_ref[i]);
+ h->sei.unregistered.nb_buf_ref = 0;
+
+ if (h1->sei.unregistered.nb_buf_ref) {
+ ret = av_reallocp_array(&h->sei.unregistered.buf_ref,
+ h1->sei.unregistered.nb_buf_ref,
+ sizeof(*h->sei.unregistered.buf_ref));
+ if (ret < 0)
+ return ret;
+
+ for (i = 0; i < h1->sei.unregistered.nb_buf_ref; i++) {
+ h->sei.unregistered.buf_ref[i] = av_buffer_ref(h1->sei.unregistered.buf_ref[i]);
+ if (!h->sei.unregistered.buf_ref[i])
+ return AVERROR(ENOMEM);
+ h->sei.unregistered.nb_buf_ref++;
+ }
+ }
+ h->sei.unregistered.x264_build = h1->sei.unregistered.x264_build;
+
if (!h->cur_pic_ptr)
return 0;
H264Picture *pic;
int i, ret;
const int pixel_shift = h->pixel_shift;
- int c[4] = {
- 1<<(h->ps.sps->bit_depth_luma-1),
- 1<<(h->ps.sps->bit_depth_chroma-1),
- 1<<(h->ps.sps->bit_depth_chroma-1),
- -1
- };
if (!ff_thread_can_start_frame(h->avctx)) {
av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
if ((ret = alloc_picture(h, pic)) < 0)
return ret;
- if(!h->frame_recovered && !h->avctx->hwaccel)
- ff_color_frame(pic->f, c);
h->cur_pic_ptr = pic;
ff_h264_unref_picture(h, &h->cur_pic);
cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
}
if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
- sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2 * cur_poc) {
+ sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2LL * cur_poc) {
sl->pwt.use_weight = 0;
sl->pwt.use_weight_chroma = 0;
return;
}
/* export coded and cropped frame dimensions to AVCodecContext */
-static int init_dimensions(H264Context *h)
+static void init_dimensions(H264Context *h)
{
const SPS *sps = (const SPS*)h->ps.sps;
int cr = sps->crop_right;
h->crop_left = cl;
h->crop_top = ct;
h->crop_bottom = cb;
-
- return 0;
}
static int h264_slice_header_init(H264Context *h)
h->ps.pps = (const PPS*)h->ps.pps_ref->data;
}
- if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
- av_buffer_unref(&h->ps.sps_ref);
- h->ps.sps = NULL;
- h->ps.sps_ref = av_buffer_ref(h->ps.sps_list[h->ps.pps->sps_id]);
- if (!h->ps.sps_ref)
- return AVERROR(ENOMEM);
- h->ps.sps = (const SPS*)h->ps.sps_ref->data;
+ if (h->ps.sps != h->ps.pps->sps) {
+ h->ps.sps = (const SPS*)h->ps.pps->sps;
if (h->mb_width != h->ps.sps->mb_width ||
h->mb_height != h->ps.sps->mb_height ||
h->width = 16 * h->mb_width;
h->height = 16 * h->mb_height;
- ret = init_dimensions(h);
- if (ret < 0)
- return ret;
+ init_dimensions(h);
if (sps->video_signal_type_present_flag) {
h->avctx->color_range = sps->full_range > 0 ? AVCOL_RANGE_JPEG
h->avctx->colorspace = sps->colorspace;
}
}
+
+ if (h->sei.alternative_transfer.present &&
+ av_color_transfer_name(h->sei.alternative_transfer.preferred_transfer_characteristics) &&
+ h->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
+ h->avctx->color_trc = h->sei.alternative_transfer.preferred_transfer_characteristics;
+ }
}
+ h->avctx->chroma_sample_location = sps->chroma_location;
if (!h->context_initialized || must_reinit || needs_reinit) {
int flush_changes = h->context_initialized;
{
const SPS *sps = h->ps.sps;
H264Picture *cur = h->cur_pic_ptr;
+ AVFrame *out = cur->f;
- cur->f->interlaced_frame = 0;
- cur->f->repeat_pict = 0;
+ out->interlaced_frame = 0;
+ out->repeat_pict = 0;
/* Signal interlacing information externally. */
/* Prioritize picture timing SEI information over used
* decoding process if it exists. */
+ if (h->sei.picture_timing.present) {
+ int ret = ff_h264_sei_process_picture_timing(&h->sei.picture_timing, sps,
+ h->avctx);
+ if (ret < 0) {
+ av_log(h->avctx, AV_LOG_ERROR, "Error processing a picture timing SEI\n");
+ if (h->avctx->err_recognition & AV_EF_EXPLODE)
+ return ret;
+ h->sei.picture_timing.present = 0;
+ }
+ }
if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
H264SEIPictureTiming *pt = &h->sei.picture_timing;
break;
case H264_SEI_PIC_STRUCT_TOP_FIELD:
case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
- cur->f->interlaced_frame = 1;
+ out->interlaced_frame = 1;
break;
case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
if (FIELD_OR_MBAFF_PICTURE(h))
- cur->f->interlaced_frame = 1;
+ out->interlaced_frame = 1;
else
// try to flag soft telecine progressive
- cur->f->interlaced_frame = h->prev_interlaced_frame;
+ out->interlaced_frame = h->prev_interlaced_frame;
break;
case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
/* Signal the possibility of telecined film externally
* (pic_struct 5,6). From these hints, let the applications
* decide if they apply deinterlacing. */
- cur->f->repeat_pict = 1;
+ out->repeat_pict = 1;
break;
case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
- cur->f->repeat_pict = 2;
+ out->repeat_pict = 2;
break;
case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
- cur->f->repeat_pict = 4;
+ out->repeat_pict = 4;
break;
}
if ((pt->ct_type & 3) &&
pt->pic_struct <= H264_SEI_PIC_STRUCT_BOTTOM_TOP)
- cur->f->interlaced_frame = (pt->ct_type & (1 << 1)) != 0;
+ out->interlaced_frame = (pt->ct_type & (1 << 1)) != 0;
} else {
/* Derive interlacing flag from used decoding process. */
- cur->f->interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
+ out->interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
}
- h->prev_interlaced_frame = cur->f->interlaced_frame;
+ h->prev_interlaced_frame = out->interlaced_frame;
if (cur->field_poc[0] != cur->field_poc[1]) {
/* Derive top_field_first from field pocs. */
- cur->f->top_field_first = cur->field_poc[0] < cur->field_poc[1];
+ out->top_field_first = cur->field_poc[0] < cur->field_poc[1];
} else {
if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
/* Use picture timing SEI information. Even if it is a
* information of a past frame, better than nothing. */
if (h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM ||
h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
- cur->f->top_field_first = 1;
+ out->top_field_first = 1;
else
- cur->f->top_field_first = 0;
- } else if (cur->f->interlaced_frame) {
+ out->top_field_first = 0;
+ } else if (out->interlaced_frame) {
/* Default to top field first when pic_struct_present_flag
* is not set but interlaced frame detected */
- cur->f->top_field_first = 1;
+ out->top_field_first = 1;
} else {
/* Most likely progressive */
- cur->f->top_field_first = 0;
+ out->top_field_first = 0;
}
}
h->sei.frame_packing.content_interpretation_type > 0 &&
h->sei.frame_packing.content_interpretation_type < 3) {
H264SEIFramePacking *fp = &h->sei.frame_packing;
- AVStereo3D *stereo = av_stereo3d_create_side_data(cur->f);
+ AVStereo3D *stereo = av_stereo3d_create_side_data(out);
if (stereo) {
switch (fp->arrangement_type) {
case H264_SEI_FPA_TYPE_CHECKERBOARD:
h->sei.display_orientation.vflip)) {
H264SEIDisplayOrientation *o = &h->sei.display_orientation;
double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
- AVFrameSideData *rotation = av_frame_new_side_data(cur->f,
+ AVFrameSideData *rotation = av_frame_new_side_data(out,
AV_FRAME_DATA_DISPLAYMATRIX,
sizeof(int32_t) * 9);
if (rotation) {
}
if (h->sei.afd.present) {
- AVFrameSideData *sd = av_frame_new_side_data(cur->f, AV_FRAME_DATA_AFD,
+ AVFrameSideData *sd = av_frame_new_side_data(out, AV_FRAME_DATA_AFD,
sizeof(uint8_t));
if (sd) {
}
}
- if (h->sei.a53_caption.a53_caption) {
+ if (h->sei.a53_caption.buf_ref) {
H264SEIA53Caption *a53 = &h->sei.a53_caption;
- AVFrameSideData *sd = av_frame_new_side_data(cur->f,
- AV_FRAME_DATA_A53_CC,
- a53->a53_caption_size);
- if (sd)
- memcpy(sd->data, a53->a53_caption, a53->a53_caption_size);
- av_freep(&a53->a53_caption);
- a53->a53_caption_size = 0;
+
+ AVFrameSideData *sd = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_A53_CC, a53->buf_ref);
+ if (!sd)
+ av_buffer_unref(&a53->buf_ref);
+ a53->buf_ref = NULL;
+
h->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
}
- if (h->sei.alternative_transfer.present &&
- av_color_transfer_name(h->sei.alternative_transfer.preferred_transfer_characteristics) &&
- h->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
- h->avctx->color_trc = cur->f->color_trc = h->sei.alternative_transfer.preferred_transfer_characteristics;
+ for (int i = 0; i < h->sei.unregistered.nb_buf_ref; i++) {
+ H264SEIUnregistered *unreg = &h->sei.unregistered;
+
+ if (unreg->buf_ref[i]) {
+ AVFrameSideData *sd = av_frame_new_side_data_from_buf(out,
+ AV_FRAME_DATA_SEI_UNREGISTERED,
+ unreg->buf_ref[i]);
+ if (!sd)
+ av_buffer_unref(&unreg->buf_ref[i]);
+ unreg->buf_ref[i] = NULL;
+ }
+ }
+ h->sei.unregistered.nb_buf_ref = 0;
+
+ if (h->sei.picture_timing.timecode_cnt > 0) {
+ uint32_t *tc_sd;
+ char tcbuf[AV_TIMECODE_STR_SIZE];
+
+ AVFrameSideData *tcside = av_frame_new_side_data(out,
+ AV_FRAME_DATA_S12M_TIMECODE,
+ sizeof(uint32_t)*4);
+ if (!tcside)
+ return AVERROR(ENOMEM);
+
+ tc_sd = (uint32_t*)tcside->data;
+ tc_sd[0] = h->sei.picture_timing.timecode_cnt;
+
+ for (int i = 0; i < tc_sd[0]; i++) {
+ int drop = h->sei.picture_timing.timecode[i].dropframe;
+ int hh = h->sei.picture_timing.timecode[i].hours;
+ int mm = h->sei.picture_timing.timecode[i].minutes;
+ int ss = h->sei.picture_timing.timecode[i].seconds;
+ int ff = h->sei.picture_timing.timecode[i].frame;
+
+ tc_sd[i + 1] = av_timecode_get_smpte(h->avctx->framerate, drop, hh, mm, ss, ff);
+ av_timecode_make_smpte_tc_string2(tcbuf, h->avctx->framerate, tc_sd[i + 1], 0, 0);
+ av_dict_set(&out->metadata, "timecode", tcbuf, 0);
+ }
+ h->sei.picture_timing.timecode_cnt = 0;
}
return 0;
}
out_of_order = MAX_DELAYED_PIC_COUNT - i;
if( cur->f->pict_type == AV_PICTURE_TYPE_B
- || (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))
+ || (h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > INT_MIN && h->last_pocs[MAX_DELAYED_PIC_COUNT-1] - (int64_t)h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > 2))
out_of_order = FFMAX(out_of_order, 1);
if (out_of_order == MAX_DELAYED_PIC_COUNT) {
av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
sps = h->ps.sps;
+ if (sps && sps->bitstream_restriction_flag &&
+ h->avctx->has_b_frames < sps->num_reorder_frames) {
+ h->avctx->has_b_frames = sps->num_reorder_frames;
+ }
+
last_pic_droppable = h->droppable;
last_pic_structure = h->picture_structure;
h->droppable = (nal->ref_idc == 0);
* vectors. Given we are concealing a lost frame, this probably
* is not noticeable by comparison, but it should be fixed. */
if (h->short_ref_count) {
+ int c[4] = {
+ 1<<(h->ps.sps->bit_depth_luma-1),
+ 1<<(h->ps.sps->bit_depth_chroma-1),
+ 1<<(h->ps.sps->bit_depth_chroma-1),
+ -1
+ };
+
if (prev &&
h->short_ref[0]->f->width == prev->f->width &&
h->short_ref[0]->f->height == prev->f->height &&
prev->f->format,
prev->f->width,
prev->f->height);
- h->short_ref[0]->poc = prev->poc + 2;
- }
+ h->short_ref[0]->poc = prev->poc + 2U;
+ } else if (!h->frame_recovered && !h->avctx->hwaccel)
+ ff_color_frame(h->short_ref[0]->f, c);
h->short_ref[0]->frame_num = h->poc.prev_frame_num;
}
}
(h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
}
- ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
+ ret = ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
+ if (ret < 0)
+ return ret;
memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
h->nb_mmco = sl->nb_mmco;
return AVERROR_INVALIDDATA;
}
pps = (const PPS*)h->ps.pps_list[sl->pps_id]->data;
-
- if (!h->ps.sps_list[pps->sps_id]) {
- av_log(h->avctx, AV_LOG_ERROR,
- "non-existing SPS %u referenced\n", pps->sps_id);
- return AVERROR_INVALIDDATA;
- }
- sps = (const SPS*)h->ps.sps_list[pps->sps_id]->data;
+ sps = pps->sps;
sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
if (!first_slice) {
av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
return AVERROR_INVALIDDATA;
}
- if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
+ if (h->ps.sps != pps->sps) {
av_log(h->avctx, AV_LOG_ERROR,
"SPS changed in the middle of the frame\n");
return AVERROR_INVALIDDATA;
ff_h264_init_cabac_states(h, sl);
for (;;) {
- // START_TIMER
int ret, eos;
if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
}
ret = ff_h264_decode_mb_cabac(h, sl);
- // STOP_TIMER("decode_mb_cabac")
if (ret >= 0)
ff_h264_hl_decode_mb(h, sl);