#include "cabac_functions.h"
#include "error_resilience.h"
#include "avcodec.h"
+#include "h264.h"
#include "h264dec.h"
#include "h2645_parse.h"
#include "h264data.h"
#include "h264chroma.h"
#include "h264_mvpred.h"
+#include "h264_ps.h"
#include "golomb.h"
#include "mathops.h"
#include "me_cmp.h"
if (!h->cur_pic.f)
return AVERROR(ENOMEM);
+ h->output_frame = av_frame_alloc();
+ if (!h->output_frame)
+ return AVERROR(ENOMEM);
+
for (i = 0; i < h->nb_slice_ctx; i++)
h->slice_ctx[i].h264 = h;
ff_h264_unref_picture(h, &h->cur_pic);
av_frame_free(&h->cur_pic.f);
+ av_frame_free(&h->output_frame);
return 0;
}
return AVERROR_UNKNOWN;
}
- if (avctx->codec_id == AV_CODEC_ID_H264) {
- if (avctx->ticks_per_frame == 1)
- h->avctx->framerate.num *= 2;
- avctx->ticks_per_frame = 2;
- }
+ if (avctx->ticks_per_frame == 1)
+ h->avctx->framerate.num *= 2;
+ avctx->ticks_per_frame = 2;
if (avctx->extradata_size > 0 && avctx->extradata) {
ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
return 0;
}
-/**
- * Run setup operations that must be run after slice header decoding.
- * This includes finding the next displayed frame.
- *
- * @param h h264 master context
- * @param setup_finished enough NALs have been read that we can call
- * ff_thread_finish_setup()
- */
-static void decode_postinit(H264Context *h, int setup_finished)
-{
- const SPS *sps = h->ps.sps;
- H264Picture *out = h->cur_pic_ptr;
- H264Picture *cur = h->cur_pic_ptr;
- int i, pics, out_of_order, out_idx;
- int invalid = 0, cnt = 0;
-
- if (h->next_output_pic)
- return;
-
- if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
- /* FIXME: if we have two PAFF fields in one packet, we can't start
- * the next thread here. If we have one field per packet, we can.
- * The check in decode_nal_units() is not good enough to find this
- * yet, so we assume the worst for now. */
- // if (setup_finished)
- // ff_thread_finish_setup(h->avctx);
- return;
- }
-
- // FIXME do something with unavailable reference frames
-
- /* Sort B-frames into display order */
- if (sps->bitstream_restriction_flag ||
- h->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
- h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
- }
-
- pics = 0;
- while (h->delayed_pic[pics])
- pics++;
-
- assert(pics <= MAX_DELAYED_PIC_COUNT);
-
- h->delayed_pic[pics++] = cur;
- if (cur->reference == 0)
- cur->reference = DELAYED_PIC_REF;
-
- /* Frame reordering. This code takes pictures from coding order and sorts
- * them by their incremental POC value into display order. It supports POC
- * gaps, MMCO reset codes and random resets.
- * A "display group" can start either with a IDR frame (f.key_frame = 1),
- * and/or can be closed down with a MMCO reset code. In sequences where
- * there is no delay, we can't detect that (since the frame was already
- * output to the user), so we also set h->mmco_reset to detect the MMCO
- * reset code.
- * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
- * we increase the delay between input and output. All frames affected by
- * the lag (e.g. those that should have been output before another frame
- * that we already returned to the user) will be dropped. This is a bug
- * that we will fix later. */
- for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
- cnt += out->poc < h->last_pocs[i];
- invalid += out->poc == INT_MIN;
- }
- if (!h->mmco_reset && !cur->f->key_frame &&
- cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
- h->mmco_reset = 2;
- if (pics > 1)
- h->delayed_pic[pics - 2]->mmco_reset = 2;
- }
- if (h->mmco_reset || cur->f->key_frame) {
- for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
- h->last_pocs[i] = INT_MIN;
- cnt = 0;
- invalid = MAX_DELAYED_PIC_COUNT;
- }
- out = h->delayed_pic[0];
- out_idx = 0;
- for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
- h->delayed_pic[i] &&
- !h->delayed_pic[i - 1]->mmco_reset &&
- !h->delayed_pic[i]->f->key_frame;
- i++)
- if (h->delayed_pic[i]->poc < out->poc) {
- out = h->delayed_pic[i];
- out_idx = i;
- }
- if (h->avctx->has_b_frames == 0 &&
- (h->delayed_pic[0]->f->key_frame || h->mmco_reset))
- h->next_outputed_poc = INT_MIN;
- out_of_order = !out->f->key_frame && !h->mmco_reset &&
- (out->poc < h->next_outputed_poc);
-
- if (sps->bitstream_restriction_flag &&
- h->avctx->has_b_frames >= sps->num_reorder_frames) {
- } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
- h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
- if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
- h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
- }
- } else if (!h->avctx->has_b_frames &&
- ((h->next_outputed_poc != INT_MIN &&
- out->poc > h->next_outputed_poc + 2) ||
- cur->f->pict_type == AV_PICTURE_TYPE_B)) {
- h->avctx->has_b_frames++;
- }
-
- if (pics > h->avctx->has_b_frames) {
- out->reference &= ~DELAYED_PIC_REF;
- for (i = out_idx; h->delayed_pic[i]; i++)
- h->delayed_pic[i] = h->delayed_pic[i + 1];
- }
- memmove(h->last_pocs, &h->last_pocs[1],
- sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
- h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
- if (!out_of_order && pics > h->avctx->has_b_frames) {
- h->next_output_pic = out;
- if (out->mmco_reset) {
- if (out_idx > 0) {
- h->next_outputed_poc = out->poc;
- h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
- } else {
- h->next_outputed_poc = INT_MIN;
- }
- } else {
- if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f->key_frame) {
- h->next_outputed_poc = INT_MIN;
- } else {
- h->next_outputed_poc = out->poc;
- }
- }
- h->mmco_reset = 0;
- } else {
- 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);
-
- if (h->avctx->active_thread_type & FF_THREAD_FRAME)
- h->setup_finished = 1;
- }
-}
-
/**
* instantaneous decoder refresh.
*/
static int get_last_needed_nal(H264Context *h)
{
int nals_needed = 0;
- int i;
+ int i, ret;
for (i = 0; i < h->pkt.nb_nals; i++) {
H2645NAL *nal = &h->pkt.nals[i];
* which splits NALs strangely if so, when frame threading we
* can't start the next thread until we've read all of them */
switch (nal->type) {
- case NAL_SPS:
- case NAL_PPS:
+ case H264_NAL_SPS:
+ case H264_NAL_PPS:
nals_needed = i;
break;
- case NAL_DPA:
- case NAL_IDR_SLICE:
- case NAL_SLICE:
- init_get_bits(&gb, nal->data + 1, (nal->size - 1) * 8);
+ case H264_NAL_DPA:
+ case H264_NAL_IDR_SLICE:
+ case H264_NAL_SLICE:
+ ret = init_get_bits8(&gb, nal->data + 1, nal->size - 1);
+ if (ret < 0) {
+ av_log(h->avctx, AV_LOG_ERROR, "Invalid zero-sized VCL NAL unit\n");
+ if (h->avctx->err_recognition & AV_EF_EXPLODE)
+ return ret;
+
+ break;
+ }
if (!get_ue_golomb(&gb))
nals_needed = i;
}
static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
{
AVCodecContext *const avctx = h->avctx;
- unsigned context_count = 0;
int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
int i, ret = 0;
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR,
"Error splitting the input into NAL units.\n");
- return ret;
+
+ /* There are samples in the wild with mp4-style extradata, but Annex B
+ * data in the packets. If we fail parsing the packet as mp4, try it again
+ * as Annex B. */
+ if (h->is_avc && !(avctx->err_recognition & AV_EF_EXPLODE)) {
+ int err = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, 0, 0,
+ avctx->codec_id);
+ if (err >= 0) {
+ av_log(avctx, AV_LOG_WARNING,
+ "The stream seems to contain AVCC extradata with Annex B "
+ "formatted data, which is invalid.");
+ h->is_avc = 0;
+ ret = 0;
+ }
+ }
+
+ if (ret < 0)
+ return ret;
}
if (avctx->active_thread_type & FF_THREAD_FRAME)
for (i = 0; i < h->pkt.nb_nals; i++) {
H2645NAL *nal = &h->pkt.nals[i];
- H264SliceContext *sl = &h->slice_ctx[context_count];
- int err;
+ int max_slice_ctx, err;
if (avctx->skip_frame >= AVDISCARD_NONREF &&
- nal->ref_idc == 0 && nal->type != NAL_SEI)
+ nal->ref_idc == 0 && nal->type != H264_NAL_SEI)
continue;
// FIXME these should stop being context-global variables
err = 0;
switch (nal->type) {
- case NAL_IDR_SLICE:
- if (nal->type != NAL_IDR_SLICE) {
- av_log(h->avctx, AV_LOG_ERROR,
- "Invalid mix of idr and non-idr slices\n");
- ret = -1;
- goto end;
- }
+ case H264_NAL_IDR_SLICE:
idr(h); // FIXME ensure we don't lose some frames if there is reordering
- case NAL_SLICE:
- sl->gb = nal->gb;
-
- if ((err = ff_h264_decode_slice_header(h, sl, nal)))
+ case H264_NAL_SLICE:
+ if ((err = ff_h264_queue_decode_slice(h, nal)))
break;
- if (h->sei.recovery_point.recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
- h->recovery_frame = (h->poc.frame_num + h->sei.recovery_point.recovery_frame_cnt) &
- ((1 << h->ps.sps->log2_max_frame_num) - 1);
- }
-
- h->cur_pic_ptr->f->key_frame |=
- (nal->type == NAL_IDR_SLICE) || (h->sei.recovery_point.recovery_frame_cnt >= 0);
-
- if (nal->type == NAL_IDR_SLICE || h->recovery_frame == h->poc.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 (nal->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 & AV_CODEC_FLAG2_CHUNKS))
- decode_postinit(h, i >= nals_needed);
-
- if (h->avctx->hwaccel &&
- (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
- return ret;
+ if (avctx->active_thread_type & FF_THREAD_FRAME && !h->avctx->hwaccel &&
+ i >= nals_needed && !h->setup_finished && h->cur_pic_ptr) {
+ ff_thread_finish_setup(avctx);
+ h->setup_finished = 1;
}
- if (sl->redundant_pic_count == 0 &&
- (avctx->skip_frame < AVDISCARD_NONREF || nal->ref_idc) &&
- (avctx->skip_frame < AVDISCARD_BIDIR ||
- sl->slice_type_nos != AV_PICTURE_TYPE_B) &&
- (avctx->skip_frame < AVDISCARD_NONKEY ||
- h->cur_pic_ptr->f->key_frame) &&
- avctx->skip_frame < AVDISCARD_ALL) {
+ max_slice_ctx = avctx->hwaccel ? 1 : h->nb_slice_ctx;
+ if (h->nb_slice_ctx_queued == max_slice_ctx) {
if (avctx->hwaccel) {
ret = avctx->hwaccel->decode_slice(avctx, nal->raw_data, nal->raw_size);
- if (ret < 0)
- return ret;
+ h->nb_slice_ctx_queued = 0;
} else
- context_count++;
+ ret = ff_h264_execute_decode_slices(h);
+ if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
+ goto end;
}
break;
- case NAL_DPA:
- case NAL_DPB:
- case NAL_DPC:
+ case H264_NAL_DPA:
+ case H264_NAL_DPB:
+ case H264_NAL_DPC:
avpriv_request_sample(avctx, "data partitioning");
ret = AVERROR(ENOSYS);
goto end;
break;
- case NAL_SEI:
+ case H264_NAL_SEI:
ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
goto end;
break;
- case NAL_SPS:
+ case H264_NAL_SPS:
ret = ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps);
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
goto end;
break;
- case NAL_PPS:
+ case H264_NAL_PPS:
ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
nal->size_bits);
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
goto end;
break;
- case NAL_AUD:
- case NAL_END_SEQUENCE:
- case NAL_END_STREAM:
- case NAL_FILLER_DATA:
- case NAL_SPS_EXT:
- case NAL_AUXILIARY_SLICE:
+ case H264_NAL_AUD:
+ case H264_NAL_END_SEQUENCE:
+ case H264_NAL_END_STREAM:
+ case H264_NAL_FILLER_DATA:
+ case H264_NAL_SPS_EXT:
+ case H264_NAL_AUXILIARY_SLICE:
break;
default:
av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
nal->type, nal->size_bits);
}
- if (context_count == h->nb_slice_ctx) {
- ret = ff_h264_execute_decode_slices(h, context_count);
- if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
- goto end;
- context_count = 0;
- }
-
if (err < 0) {
av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
- sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
}
}
- if (context_count) {
- ret = ff_h264_execute_decode_slices(h, context_count);
- if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
- goto end;
- }
+
+ ret = ff_h264_execute_decode_slices(h);
+ if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
+ goto end;
ret = 0;
end:
h->flags = avctx->flags;
h->setup_finished = 0;
+ h->nb_slice_ctx_queued = 0;
/* end of stream, output what is still in the buffers */
out:
if (buf_index < 0)
return AVERROR_INVALIDDATA;
- if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
+ if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {
buf_size = 0;
goto out;
}
if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
(h->mb_y >= h->mb_height && h->mb_height)) {
- if (avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)
- decode_postinit(h, 1);
-
ff_h264_field_end(h, &h->slice_ctx[0], 0);
*got_frame = 0;
- if (h->next_output_pic && ((avctx->flags & AV_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 (h->output_frame->buf[0]) {
+ ret = output_frame(h, pict, h->output_frame) ;
+ av_frame_unref(h->output_frame);
if (ret < 0)
return ret;
*got_frame = 1;