int off = offsetof(Picture, tf) + sizeof(pic->tf);
int i;
- if (!pic->f.data[0])
+ if (!pic->f.buf[0])
return;
ff_thread_release_buffer(h->avctx, &pic->tf);
/* release non reference frames */
for (i = 0; i < MAX_PICTURE_COUNT; i++) {
- if (h->DPB[i].f.data[0] && !h->DPB[i].reference &&
+ if (h->DPB[i].f.buf[0] && !h->DPB[i].reference &&
(remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
unref_picture(h, &h->DPB[i]);
}
dst->field_picture = src->field_picture;
dst->needs_realloc = src->needs_realloc;
dst->reference = src->reference;
+ dst->recovered = src->recovered;
return 0;
fail:
static inline int pic_is_unused(H264Context *h, Picture *pic)
{
- if (pic->f.data[0] == NULL)
+ if (!pic->f.buf[0])
return 1;
if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
return 1;
const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
const int luma_xy = (mx & 3) + ((my & 3) << 2);
- int offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
+ ptrdiff_t offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
uint8_t *src_y = pic->f.data[0] + offset;
uint8_t *src_cb, *src_cr;
int extra_width = 0;
full_my + 16 /*FIXME*/ > pic_height + extra_height) {
h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
- h->mb_linesize,
+ h->mb_linesize, h->mb_linesize,
16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
full_my - 2, pic_width, pic_height);
src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
if (emu) {
h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
- h->mb_linesize,
+ h->mb_linesize, h->mb_linesize,
16 + 5, 16 + 5 /*FIXME*/,
full_mx - 2, full_my - 2,
pic_width, pic_height);
if (emu) {
h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
- h->mb_linesize,
+ h->mb_linesize, h->mb_linesize,
16 + 5, 16 + 5 /*FIXME*/,
full_mx - 2, full_my - 2,
pic_width, pic_height);
(my >> ysh) * h->mb_uvlinesize;
if (emu) {
- h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb, h->mb_uvlinesize,
+ h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb,
+ h->mb_uvlinesize, h->mb_uvlinesize,
9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
src_cb = h->edge_emu_buffer;
mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
if (emu) {
- h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr, h->mb_uvlinesize,
+ h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr,
+ h->mb_uvlinesize, h->mb_uvlinesize,
9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
src_cr = h->edge_emu_buffer;
ff_h264_decode_init_vlc();
+ ff_init_cabac_states();
+
h->pixel_shift = 0;
h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
h->prev_poc_msb = 1 << 16;
h->x264_build = -1;
ff_h264_reset_sei(h);
+ h->recovery_frame = -1;
+ h->frame_recovered = 0;
if (avctx->codec_id == AV_CODEC_ID_H264) {
if (avctx->ticks_per_frame == 1)
h->avctx->time_base.den *= 2;
}
}
-static void copy_parameter_set(void **to, void **from, int count, int size)
+static int copy_parameter_set(void **to, void **from, int count, int size)
{
int i;
for (i = 0; i < count; i++) {
- if (to[i] && !from[i])
+ if (to[i] && !from[i]) {
av_freep(&to[i]);
- else if (from[i] && !to[i])
+ } else if (from[i] && !to[i]) {
to[i] = av_malloc(size);
+ if (!to[i])
+ return AVERROR(ENOMEM);
+ }
if (from[i])
memcpy(to[i], from[i], size);
}
+
+ return 0;
}
static int decode_init_thread_copy(AVCodecContext *avctx)
for (i = 0; i < MAX_PICTURE_COUNT; i++) {
unref_picture(h, &h->DPB[i]);
- if (h1->DPB[i].f.data[0] &&
+ if (h1->DPB[i].f.buf[0] &&
(ret = ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
return ret;
}
h->is_avc = h1->is_avc;
// SPS/PPS
- copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
- MAX_SPS_COUNT, sizeof(SPS));
+ if ((ret = copy_parameter_set((void **)h->sps_buffers,
+ (void **)h1->sps_buffers,
+ MAX_SPS_COUNT, sizeof(SPS))) < 0)
+ return ret;
h->sps = h1->sps;
- copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
- MAX_PPS_COUNT, sizeof(PPS));
+ if ((ret = copy_parameter_set((void **)h->pps_buffers,
+ (void **)h1->pps_buffers,
+ MAX_PPS_COUNT, sizeof(PPS))) < 0)
+ return ret;
h->pps = h1->pps;
// Dequantization matrices
h->prev_frame_num = h->frame_num;
h->outputed_poc = h->next_outputed_poc;
+ h->recovery_frame = h1->recovery_frame;
+ h->frame_recovered = h1->frame_recovered;
+
return err;
}
*/
pic->f.key_frame = 0;
pic->mmco_reset = 0;
+ pic->recovered = 0;
if ((ret = alloc_picture(h, pic)) < 0)
return ret;
av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
}
+ if (h->next_output_pic) {
+ if (h->next_output_pic->recovered) {
+ // We have reached an recovery point and all frames after it in
+ // display order are "recovered".
+ h->frame_recovered |= FRAME_RECOVERED_SEI;
+ }
+ h->next_output_pic->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
+ }
+
if (setup_finished && !h->avctx->hwaccel)
ff_thread_finish_setup(h->avctx);
}
hl_decode_mb_simple_8(h);
}
-static int pred_weight_table(H264Context *h)
+int ff_pred_weight_table(H264Context *h)
{
int list, i;
int luma_def, chroma_def;
memset(h->default_ref_list[0], 0, sizeof(h->default_ref_list[0]));
memset(h->default_ref_list[1], 0, sizeof(h->default_ref_list[1]));
ff_h264_reset_sei(h);
+ h->recovery_frame = -1;
+ h->frame_recovered = 0;
}
/* forget old pics after a seek */
h->parse_context.overread_index = 0;
h->parse_context.index = 0;
h->parse_context.last_index = 0;
+
+ free_tables(h, 1);
+ h->context_initialized = 0;
}
int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
h->sps.num_units_in_tick, den, 1 << 30);
}
- h->avctx->hwaccel = ff_find_hwaccel(h->avctx->codec->id, h->avctx->pix_fmt);
+ h->avctx->hwaccel = ff_find_hwaccel(h->avctx);
if (reinit)
free_tables(h, 0);
for (i = 1; i < h->slice_context_count; i++) {
H264Context *c;
c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
+ if (!c)
+ return AVERROR(ENOMEM);
c->avctx = h->avctx;
c->dsp = h->dsp;
c->vdsp = h->vdsp;
return 0;
}
+int ff_set_ref_count(H264Context *h)
+{
+ int num_ref_idx_active_override_flag, max_refs;
+
+ // set defaults, might be overridden a few lines later
+ h->ref_count[0] = h->pps.ref_count[0];
+ h->ref_count[1] = h->pps.ref_count[1];
+
+ if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
+ if (h->slice_type_nos == AV_PICTURE_TYPE_B)
+ h->direct_spatial_mv_pred = get_bits1(&h->gb);
+ num_ref_idx_active_override_flag = get_bits1(&h->gb);
+
+ if (num_ref_idx_active_override_flag) {
+ h->ref_count[0] = get_ue_golomb(&h->gb) + 1;
+ if (h->ref_count[0] < 1)
+ return AVERROR_INVALIDDATA;
+ if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
+ h->ref_count[1] = get_ue_golomb(&h->gb) + 1;
+ if (h->ref_count[1] < 1)
+ return AVERROR_INVALIDDATA;
+ }
+ }
+
+ if (h->slice_type_nos == AV_PICTURE_TYPE_B)
+ h->list_count = 2;
+ else
+ h->list_count = 1;
+ } else {
+ h->list_count = 0;
+ h->ref_count[0] = h->ref_count[1] = 0;
+ }
+
+ max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;
+
+ if (h->ref_count[0] > max_refs || h->ref_count[1] > max_refs) {
+ av_log(h->avctx, AV_LOG_ERROR, "reference overflow\n");
+ h->ref_count[0] = h->ref_count[1] = 0;
+ return AVERROR_INVALIDDATA;
+ }
+
+ return 0;
+}
+
/**
* Decode a slice header.
* This will also call ff_MPV_common_init() and frame_start() as needed.
{
unsigned int first_mb_in_slice;
unsigned int pps_id;
- int num_ref_idx_active_override_flag, max_refs, ret;
+ int ret;
unsigned int slice_type, tmp, i, j;
int default_ref_list_done = 0;
int last_pic_structure, last_pic_droppable;
* since that can modify s->current_picture_ptr. */
if (h0->first_field) {
assert(h0->cur_pic_ptr);
- assert(h0->cur_pic_ptr->f.data[0]);
+ assert(h0->cur_pic_ptr->f.buf[0]);
assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
/* figure out if we have a complementary field pair */
* frame, or to allocate a new one. */
if (h0->first_field) {
assert(h0->cur_pic_ptr);
- assert(h0->cur_pic_ptr->f.data[0]);
+ assert(h0->cur_pic_ptr->f.buf[0]);
assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
/* figure out if we have a complementary field pair */
if (h->pps.redundant_pic_cnt_present)
h->redundant_pic_count = get_ue_golomb(&h->gb);
- // set defaults, might be overridden a few lines later
- h->ref_count[0] = h->pps.ref_count[0];
- h->ref_count[1] = h->pps.ref_count[1];
-
- if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
- if (h->slice_type_nos == AV_PICTURE_TYPE_B)
- h->direct_spatial_mv_pred = get_bits1(&h->gb);
- num_ref_idx_active_override_flag = get_bits1(&h->gb);
-
- if (num_ref_idx_active_override_flag) {
- h->ref_count[0] = get_ue_golomb(&h->gb) + 1;
- if (h->ref_count[0] < 1)
- return AVERROR_INVALIDDATA;
- if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
- h->ref_count[1] = get_ue_golomb(&h->gb) + 1;
- if (h->ref_count[1] < 1)
- return AVERROR_INVALIDDATA;
- }
- }
-
- if (h->slice_type_nos == AV_PICTURE_TYPE_B)
- h->list_count = 2;
- else
- h->list_count = 1;
- } else {
- h->list_count = 0;
- h->ref_count[0] = h->ref_count[1] = 0;
- }
-
- max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;
-
- if (h->ref_count[0] > max_refs || h->ref_count[1] > max_refs) {
- av_log(h->avctx, AV_LOG_ERROR, "reference overflow\n");
- h->ref_count[0] = h->ref_count[1] = 0;
- return AVERROR_INVALIDDATA;
- }
+ ret = ff_set_ref_count(h);
+ if (ret < 0)
+ return ret;
if (!default_ref_list_done)
ff_h264_fill_default_ref_list(h);
if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
(h->pps.weighted_bipred_idc == 1 &&
h->slice_type_nos == AV_PICTURE_TYPE_B))
- pred_weight_table(h);
+ ff_pred_weight_table(h);
else if (h->pps.weighted_bipred_idc == 2 &&
h->slice_type_nos == AV_PICTURE_TYPE_B) {
implicit_weight_table(h, -1);
align_get_bits(&h->gb);
/* init cabac */
- ff_init_cabac_states();
ff_init_cabac_decoder(&h->cabac,
h->gb.buffer + get_bits_count(&h->gb) / 8,
(get_bits_left(&h->gb) + 7) / 8);
return 0;
}
-static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
-
static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
int parse_extradata)
{
h->workaround_bugs |= FF_BUG_TRUNCATED;
if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
- while (ptr[dst_length - 1] == 0 && dst_length > 0)
+ while (dst_length > 0 && ptr[dst_length - 1] == 0)
dst_length--;
bit_length = !dst_length ? 0
: (8 * dst_length -
continue;
}
- // FIXME do not discard SEI id
- if (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
+ if (avctx->skip_frame >= AVDISCARD_NONREF &&
+ h->nal_ref_idc == 0 &&
+ h->nal_unit_type != NAL_SEI)
continue;
again:
(h->avctx->active_thread_type & FF_THREAD_FRAME) &&
(hx->nal_unit_type != NAL_PPS &&
hx->nal_unit_type != NAL_SPS)) {
- av_log(avctx, AV_LOG_INFO, "Ignoring NAL unit %d during "
- "extradata parsing\n", hx->nal_unit_type);
+ if (hx->nal_unit_type < NAL_AUD ||
+ hx->nal_unit_type > NAL_AUXILIARY_SLICE)
+ av_log(avctx, AV_LOG_INFO,
+ "Ignoring NAL unit %d during extradata parsing\n",
+ hx->nal_unit_type);
hx->nal_unit_type = NAL_FF_IGNORE;
}
err = 0;
if ((err = decode_slice_header(hx, h)))
break;
+ if (h->sei_recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
+ h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
+ ((1 << h->sps.log2_max_frame_num) - 1);
+ }
+
h->cur_pic_ptr->f.key_frame |=
(hx->nal_unit_type == NAL_IDR_SLICE) ||
(h->sei_recovery_frame_cnt >= 0);
+ if (hx->nal_unit_type == NAL_IDR_SLICE ||
+ h->recovery_frame == h->frame_num) {
+ h->recovery_frame = -1;
+ h->cur_pic_ptr->recovered = 1;
+ }
+ // If we have an IDR, all frames after it in decoded order are
+ // "recovered".
+ if (hx->nal_unit_type == NAL_IDR_SLICE)
+ h->frame_recovered |= FRAME_RECOVERED_IDR;
+ h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
+
if (h->current_slice == 1) {
if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
decode_postinit(h, nal_index >= nals_needed);
field_end(h, 0);
- if (!h->next_output_pic) {
- /* Wait for second field. */
- *got_frame = 0;
- } else {
+ *got_frame = 0;
+ if (h->next_output_pic && ((avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT) ||
+ h->next_output_pic->recovered)) {
+ if (!h->next_output_pic->recovered)
+ h->next_output_pic->f.flags |= AV_FRAME_FLAG_CORRUPT;
+
ret = output_frame(h, pict, &h->next_output_pic->f);
if (ret < 0)
return ret;
}
}
- assert(pict->data[0] || !*got_frame);
+ assert(pict->buf[0] || !*got_frame);
return get_consumed_bytes(buf_index, buf_size);
}
AVCodec ff_h264_decoder = {
.name = "h264",
+ .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_H264,
.priv_data_size = sizeof(H264Context),
CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
CODEC_CAP_FRAME_THREADS,
.flush = flush_dpb,
- .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
.init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
.update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
.profiles = NULL_IF_CONFIG_SMALL(profiles),