* practice then correct remapping should be added. */
if (ref >= sl->ref_count[0])
ref = 0;
- if (!sl->ref_list[0][ref].f.data[0]) {
+ if (!sl->ref_list[0][ref].data[0]) {
av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
ref = 0;
}
int y, int height)
{
AVCodecContext *avctx = h->avctx;
- const AVFrame *cur = &h->cur_pic.f;
- AVFrame *last = sl->ref_list[0][0].f.data[0] ? &sl->ref_list[0][0].f : NULL;
+ const AVFrame *src = &h->cur_pic.f;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
int vshift = desc->log2_chroma_h;
const int field_pic = h->picture_structure != PICT_FRAME;
return;
if (avctx->draw_horiz_band) {
- const AVFrame *src;
int offset[AV_NUM_DATA_POINTERS];
int i;
- if (cur->pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
- (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
- src = cur;
- else if (last)
- src = last;
- else
- return;
-
offset[0] = y * src->linesize[0];
offset[1] =
offset[2] = (y >> vshift) * src->linesize[1];
return mode;
}
-const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
+const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl,
+ const uint8_t *src,
int *dst_length, int *consumed, int length)
{
int i, si, di;
}
#endif
- av_fast_padded_malloc(&h->rbsp_buffer, &h->rbsp_buffer_size, length+MAX_MBPAIR_SIZE);
- dst = h->rbsp_buffer;
+ av_fast_padded_malloc(&sl->rbsp_buffer, &sl->rbsp_buffer_size, length+MAX_MBPAIR_SIZE);
+ dst = sl->rbsp_buffer;
if (!dst)
return NULL;
void ff_h264_free_tables(H264Context *h, int free_rbsp)
{
int i;
- H264Context *hx;
av_freep(&h->intra4x4_pred_mode);
av_freep(&h->chroma_pred_mode_table);
h->cur_pic_ptr = NULL;
- for (i = 0; i < H264_MAX_THREADS; i++) {
- hx = h->thread_context[i];
- if (!hx)
- continue;
-
- if (free_rbsp) {
- av_freep(&hx->rbsp_buffer);
- hx->rbsp_buffer_size = 0;
- }
- if (i)
- av_freep(&h->thread_context[i]);
- }
-
for (i = 0; i < h->nb_slice_ctx; i++) {
H264SliceContext *sl = &h->slice_ctx[i];
sl->edge_emu_buffer_allocated = 0;
sl->top_borders_allocated[0] = 0;
sl->top_borders_allocated[1] = 0;
+
+ if (free_rbsp) {
+ av_freep(&sl->rbsp_buffer);
+ sl->rbsp_buffer_size = 0;
+ }
}
}
h->bit_depth_luma = 8;
h->chroma_format_idc = 1;
- h->avctx->bits_per_raw_sample = 8;
- h->cur_chroma_format_idc = 1;
-
ff_h264dsp_init(&h->h264dsp, 8, 1);
av_assert0(h->sps.bit_depth_chroma == 0);
ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
/* needed so that IDCT permutation is known early */
ff_videodsp_init(&h->vdsp, 8);
+ h->cur_chroma_format_idc = -1;
memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
ff_init_cabac_states();
h->pixel_shift = 0;
+ h->cur_bit_depth_luma =
h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? H264_MAX_THREADS : 1;
return AVERROR(ENOMEM);
}
- h->thread_context[0] = h;
for (i = 0; i < h->nb_slice_ctx; i++)
- h->slice_ctx[i].h264 = h->thread_context[0];
+ h->slice_ctx[i].h264 = h;
h->outputed_poc = h->next_outputed_poc = INT_MIN;
for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
ff_h264_flush_change(h);
+ if (h->enable_er < 0 && (avctx->active_thread_type & FF_THREAD_SLICE))
+ h->enable_er = 0;
+
+ if (h->enable_er && (avctx->active_thread_type & FF_THREAD_SLICE)) {
+ av_log(avctx, AV_LOG_WARNING,
+ "Error resilience with slice threads is enabled. It is unsafe and unsupported and may crash. "
+ "Use it at your own risk\n");
+ }
+
return 0;
}
h->slice_ctx[i].h264 = h;
h->avctx = avctx;
- h->rbsp_buffer = NULL;
- h->rbsp_buffer_size = 0;
h->context_initialized = 0;
return 0;
if (h->avctx->has_b_frames < 2)
h->avctx->has_b_frames = !h->low_delay;
- if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
+ if (h->cur_bit_depth_luma != h->sps.bit_depth_luma ||
h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
if (h->avctx->codec &&
h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU &&
}
if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 14 &&
h->sps.bit_depth_luma != 11 && h->sps.bit_depth_luma != 13) {
+ h->cur_bit_depth_luma =
h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
h->cur_chroma_format_idc = h->sps.chroma_format_idc;
h->pixel_shift = h->sps.bit_depth_luma > 8;
continue;
}
- ptr = ff_h264_decode_nal(h, buf + buf_index, &dst_length, &consumed,
+ ptr = ff_h264_decode_nal(h, &h->slice_ctx[0], buf + buf_index, &dst_length, &consumed,
next_avc - buf_index);
if (!ptr || dst_length < 0)
int parse_extradata)
{
AVCodecContext *const avctx = h->avctx;
- H264Context *hx; ///< thread context
H264SliceContext *sl;
int buf_index;
unsigned context_count;
continue;
}
- hx = h->thread_context[context_count];
sl = &h->slice_ctx[context_count];
- ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
+ ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
&consumed, next_avc - buf_index);
if (!ptr || dst_length < 0) {
ret = -1;
if (h->avctx->debug & FF_DEBUG_STARTCODE)
av_log(h->avctx, AV_LOG_DEBUG,
"NAL %d/%d at %d/%d length %d\n",
- hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length);
+ h->nal_unit_type, h->nal_ref_idc, buf_index, buf_size, dst_length);
if (h->is_avc && (nalsize != consumed) && nalsize)
av_log(h->avctx, AV_LOG_DEBUG,
* parsing. Decoding slices is not possible in codec init
* with frame-mt */
if (parse_extradata) {
- switch (hx->nal_unit_type) {
+ switch (h->nal_unit_type) {
case NAL_IDR_SLICE:
case NAL_SLICE:
case NAL_DPA:
case NAL_DPC:
av_log(h->avctx, AV_LOG_WARNING,
"Ignoring NAL %d in global header/extradata\n",
- hx->nal_unit_type);
+ h->nal_unit_type);
// fall through to next case
case NAL_AUXILIARY_SLICE:
- hx->nal_unit_type = NAL_FF_IGNORE;
+ h->nal_unit_type = NAL_FF_IGNORE;
}
}
err = 0;
- switch (hx->nal_unit_type) {
+ switch (h->nal_unit_type) {
case NAL_IDR_SLICE:
if ((ptr[0] & 0xFC) == 0x98) {
av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
ret = -1;
goto end;
}
- if(!idr_cleared)
+ if(!idr_cleared) {
+ if (h->current_slice && (avctx->active_thread_type & FF_THREAD_SLICE)) {
+ av_log(h, AV_LOG_ERROR, "invalid mixed IDR / non IDR frames cannot be decoded in slice multithreading mode\n");
+ ret = AVERROR_INVALIDDATA;
+ goto end;
+ }
idr(h); // FIXME ensure we don't lose some frames if there is reordering
+ }
idr_cleared = 1;
h->has_recovery_point = 1;
case NAL_SLICE:
init_get_bits(&sl->gb, ptr, bit_length);
- if ((err = ff_h264_decode_slice_header(hx, sl, h)))
+ if ((err = ff_h264_decode_slice_header(h, sl)))
break;
if (h->sei_recovery_frame_cnt >= 0) {
}
h->cur_pic_ptr->f.key_frame |=
- (hx->nal_unit_type == NAL_IDR_SLICE);
+ (h->nal_unit_type == NAL_IDR_SLICE);
- if (hx->nal_unit_type == NAL_IDR_SLICE ||
+ if (h->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)
+ if (h->nal_unit_type == NAL_IDR_SLICE)
h->frame_recovered |= FRAME_RECOVERED_IDR;
h->frame_recovered |= 3*!!(avctx->flags2 & CODEC_FLAG2_SHOW_ALL);
h->frame_recovered |= 3*!!(avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT);
decode_postinit(h, nal_index >= nals_needed);
if (h->avctx->hwaccel &&
- (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
- return ret;
+ (ret = h->avctx->hwaccel->start_frame(h->avctx, buf, buf_size)) < 0)
+ goto end;
if (CONFIG_H264_VDPAU_DECODER &&
h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
ff_vdpau_h264_picture_start(h);
&buf[buf_index - consumed],
consumed);
if (ret < 0)
- return ret;
+ goto end;
} else if (CONFIG_H264_VDPAU_DECODER &&
h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
ff_vdpau_add_data_chunk(h->cur_pic_ptr->f.data[0],
break;
default:
av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
- hx->nal_unit_type, bit_length);
+ h->nal_unit_type, bit_length);
}
if (context_count == h->max_contexts) {
av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
} else if (err == SLICE_SINGLETHREAD) {
- /* Slice could not be decoded in parallel mode, copy down
- * NAL unit stuff to context 0 and restart. Note that
- * rbsp_buffer is not transferred, but since we no longer
+ /* Slice could not be decoded in parallel mode, restart. Note
+ * that rbsp_buffer is not transferred, but since we no longer
* run in parallel mode this should not be an issue. */
- h->nal_unit_type = hx->nal_unit_type;
- h->nal_ref_idc = hx->nal_ref_idc;
- hx = h;
sl = &h->slice_ctx[0];
goto again;
}
return 0;
}
+#define OFFSET(x) offsetof(H264Context, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+static const AVOption h264_options[] = {
+ {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
+ {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
+ { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VD },
+ { NULL },
+};
+
+static const AVClass h264_class = {
+ .class_name = "H264 Decoder",
+ .item_name = av_default_item_name,
+ .option = h264_options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
static const AVProfile profiles[] = {
{ FF_PROFILE_H264_BASELINE, "Baseline" },
{ FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
{ FF_PROFILE_UNKNOWN },
};
-static const AVOption h264_options[] = {
- {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
- {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
- {NULL}
-};
-
-static const AVClass h264_class = {
- .class_name = "H264 Decoder",
- .item_name = av_default_item_name,
- .option = h264_options,
- .version = LIBAVUTIL_VERSION_INT,
-};
-
AVCodec ff_h264_decoder = {
.name = "h264",
.long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),