X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fhevc_parser.c;h=463d352055b971bdcc27a27b3fcd7aa4b609286e;hb=e83717e63eab1f1b78dc0990e5b8e927097fca29;hp=ff7e8a49d66d02f99ef7ea32586dd1ef8268ae0f;hpb=725ae0e2d0222f81b5cca3b0b226116ec6fd0494;p=ffmpeg diff --git a/libavcodec/hevc_parser.c b/libavcodec/hevc_parser.c index ff7e8a49d66..463d352055b 100644 --- a/libavcodec/hevc_parser.c +++ b/libavcodec/hevc_parser.c @@ -24,6 +24,7 @@ #include "golomb.h" #include "hevc.h" +#include "hevc_parse.h" #include "hevc_ps.h" #include "hevc_sei.h" #include "h2645_parse.h" @@ -41,8 +42,9 @@ typedef struct HEVCParserContext { H2645Packet pkt; HEVCParamSets ps; HEVCSEI sei; - SliceHeader sh; + int is_avc; + int nal_length_size; int parsed_extradata; int poc; @@ -55,26 +57,28 @@ static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal, HEVCParserContext *ctx = s->priv_data; HEVCParamSets *ps = &ctx->ps; HEVCSEI *sei = &ctx->sei; - SliceHeader *sh = &ctx->sh; GetBitContext *gb = &nal->gb; const HEVCWindow *ow; int i, num = 0, den = 0; - sh->first_slice_in_pic_flag = get_bits1(gb); + unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag; + enum HEVCSliceType slice_type; + + first_slice_in_pic_flag = get_bits1(gb); s->picture_structure = sei->picture_timing.picture_struct; s->field_order = sei->picture_timing.picture_struct; if (IS_IRAP_NAL(nal)) { s->key_frame = 1; - sh->no_output_of_prior_pics_flag = get_bits1(gb); + skip_bits1(gb); // no_output_of_prior_pics_flag } - sh->pps_id = get_ue_golomb(gb); - if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) { - av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id); + pps_id = get_ue_golomb(gb); + if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) { + av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id); return AVERROR_INVALIDDATA; } - ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data; + ps->pps = (HEVCPPS*)ps->pps_list[pps_id]->data; if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) { av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id); @@ -106,51 +110,53 @@ static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal, av_reduce(&avctx->framerate.den, &avctx->framerate.num, num, den, 1 << 30); - if (!sh->first_slice_in_pic_flag) { + if (!first_slice_in_pic_flag) { + unsigned int slice_segment_addr; int slice_address_length; if (ps->pps->dependent_slice_segments_enabled_flag) - sh->dependent_slice_segment_flag = get_bits1(gb); + dependent_slice_segment_flag = get_bits1(gb); else - sh->dependent_slice_segment_flag = 0; + dependent_slice_segment_flag = 0; slice_address_length = av_ceil_log2_c(ps->sps->ctb_width * ps->sps->ctb_height); - sh->slice_segment_addr = get_bitsz(gb, slice_address_length); - if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) { + slice_segment_addr = get_bitsz(gb, slice_address_length); + if (slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) { av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n", - sh->slice_segment_addr); + slice_segment_addr); return AVERROR_INVALIDDATA; } } else - sh->dependent_slice_segment_flag = 0; + dependent_slice_segment_flag = 0; - if (sh->dependent_slice_segment_flag) + if (dependent_slice_segment_flag) return 0; /* break; */ for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++) skip_bits(gb, 1); // slice_reserved_undetermined_flag[] - sh->slice_type = get_ue_golomb(gb); - if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P || - sh->slice_type == HEVC_SLICE_B)) { + slice_type = get_ue_golomb_31(gb); + if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P || + slice_type == HEVC_SLICE_B)) { av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n", - sh->slice_type); + slice_type); return AVERROR_INVALIDDATA; } - s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B : - sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P : - AV_PICTURE_TYPE_I; + s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B : + slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P : + AV_PICTURE_TYPE_I; if (ps->pps->output_flag_present_flag) - sh->pic_output_flag = get_bits1(gb); + skip_bits1(gb); // pic_output_flag if (ps->sps->separate_colour_plane_flag) - sh->colour_plane_id = get_bits(gb, 2); + skip_bits(gb, 2); // colour_plane_id if (!IS_IDR_NAL(nal)) { - sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb); - s->output_picture_number = ctx->poc = ff_hevc_compute_poc(ps->sps, ctx->pocTid0, sh->pic_order_cnt_lsb, nal->type); + int pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb); + s->output_picture_number = ctx->poc = + ff_hevc_compute_poc(ps->sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type); } else s->output_picture_number = ctx->poc = 0; @@ -181,7 +187,6 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, HEVCParserContext *ctx = s->priv_data; HEVCParamSets *ps = &ctx->ps; HEVCSEI *sei = &ctx->sei; - int is_global = buf == avctx->extradata; int ret, i; /* set some sane default values */ @@ -191,8 +196,8 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, ff_hevc_reset_sei(sei); - ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0, - AV_CODEC_ID_HEVC, 1); + ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, ctx->is_avc, + ctx->nal_length_size, AV_CODEC_ID_HEVC, 1, 0); if (ret < 0) return ret; @@ -200,6 +205,9 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, H2645NAL *nal = &ctx->pkt.nals[i]; GetBitContext *gb = &nal->gb; + if (nal->nuh_layer_id > 0) + continue; + switch (nal->type) { case HEVC_NAL_VPS: ff_hevc_decode_nal_vps(gb, avctx, ps); @@ -230,12 +238,11 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, case HEVC_NAL_RADL_R: case HEVC_NAL_RASL_N: case HEVC_NAL_RASL_R: - - if (is_global) { - av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type); - return AVERROR_INVALIDDATA; + if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) { + s->repeat_pict = 1; + } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) { + s->repeat_pict = 2; } - ret = hevc_parse_slice_header(s, nal, avctx); if (ret) return ret; @@ -243,8 +250,7 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, } } /* didn't find a picture! */ - if (!is_global) - av_log(avctx, AV_LOG_ERROR, "missing picture in access unit\n"); + av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size); return -1; } @@ -299,9 +305,13 @@ static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx, int next; HEVCParserContext *ctx = s->priv_data; ParseContext *pc = &ctx->pc; + int is_dummy_buf = !buf_size; + const uint8_t *dummy_buf = buf; if (avctx->extradata && !ctx->parsed_extradata) { - parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx); + ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei, + &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition, + 1, avctx); ctx->parsed_extradata = 1; } @@ -316,7 +326,10 @@ static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx, } } - parse_nal_units(s, buf, buf_size, avctx); + is_dummy_buf &= (dummy_buf == buf); + + if (!is_dummy_buf) + parse_nal_units(s, buf, buf_size, avctx); *poutbuf = buf; *poutbuf_size = buf_size; @@ -359,17 +372,8 @@ static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size) static void hevc_parser_close(AVCodecParserContext *s) { HEVCParserContext *ctx = s->priv_data; - int i; - - for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++) - av_buffer_unref(&ctx->ps.vps_list[i]); - for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++) - av_buffer_unref(&ctx->ps.sps_list[i]); - for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++) - av_buffer_unref(&ctx->ps.pps_list[i]); - - ctx->ps.sps = NULL; + ff_hevc_ps_uninit(&ctx->ps); ff_h2645_packet_uninit(&ctx->pkt); ff_hevc_reset_sei(&ctx->sei);