[FFmpeg-devel] [PATCH 5/6] avcodec: add h266 parser

Nuo Mi nuomi2021 at gmail.com
Tue Dec 22 12:56:41 EET 2020


On Tue, Dec 22, 2020 at 2:43 AM James Almer <jamrial at gmail.com> wrote:

> On 12/21/2020 3:07 AM, Nuo Mi wrote:
> > ---
> >   configure                |   3 +
> >   libavcodec/Makefile      |   1 +
> >   libavcodec/h2645_parse.c |  73 +++++++++-
> >   libavcodec/h266_parser.c | 284 +++++++++++++++++++++++++++++++++++++++
> >   libavcodec/parsers.c     |   1 +
> >   5 files changed, 360 insertions(+), 2 deletions(-)
> >   create mode 100644 libavcodec/h266_parser.c
>
> [...]
>
> > +/**
> > + * Parse NAL units of found picture and decode some basic information.
> > + *
> > + * @param s parser context.
> > + * @param avctx codec context.
> > + * @param buf buffer with field/frame data.
> > + * @param buf_size size of the buffer.
> > + */
> > +static int parse_nal_units(AVCodecParserContext *ctx, const uint8_t
> *buf,
> > +                           int buf_size, AVCodecContext *avctx)
> > +{
> > +    H266ParserContext *s = ctx->priv_data;
> > +    CodedBitstreamFragment *pu = &s->picture_unit;
> > +    CodedBitstreamH266Context *h266 = s->cbc->priv_data;
> > +    const H266RawSPS *sps;
> > +    const H266RawPPS *pps;
> > +    const H266RawPH *ph;
> > +    int ret, num = 0, den = 0;
> > +
> > +    /* set some sane default values */
> > +    ctx->pict_type         = AV_PICTURE_TYPE_I;
> > +    ctx->key_frame         = 0;
> > +    ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
> > +
> > +    s->cbc->log_ctx = avctx;
> > +
> > +    if (avctx->extradata_size && !s->parsed_extradata) {
> > +        s->parsed_extradata = 1;
> > +
> > +        if ((ret = ff_cbs_read(s->cbc, pu, avctx->extradata,
> avctx->extradata_size)) < 0)
> > +            av_log(avctx, AV_LOG_WARNING, "Failed to parse
> extradata.\n");
> > +
> > +        ff_cbs_fragment_reset(pu);
> > +    }
> > +
> > +    if ((ret = ff_cbs_read(s->cbc, pu, buf, buf_size))< 0) {
> > +        av_log(avctx, AV_LOG_ERROR, "Failed to parse picture unit.\n");
> > +        goto end;
> > +    }
> > +
> > +    sps = h266->active_sps;
> > +    pps = h266->active_pps;
> > +    ph = h266->active_ph;
> > +    if (!h266->active_pps || !h266->active_ph) {
> > +        av_log(avctx, AV_LOG_ERROR, "No pss or ph header available\n");
> > +        goto end;
> > +    }
> > +
> > +    ctx->key_frame = ph->ph_gdr_or_irap_pic_flag;
> > +
> > +    ctx->coded_width  = pps->pps_pic_width_in_luma_samples;
> > +    ctx->coded_height = pps->pps_pic_height_in_luma_samples;
> > +    ctx->width        = pps->pps_pic_width_in_luma_samples  -
> pps->pps_conf_win_left_offset - pps->pps_conf_win_right_offset;
> > +    ctx->height       = pps->pps_pic_height_in_luma_samples -
> pps->pps_conf_win_top_offset  - pps->pps_conf_win_bottom_offset;
> > +    ctx->pict_type    = get_pict_type(pu);
> > +    ctx->format       = get_format(sps);
> > +    avctx->profile  = sps->profile_tier_level.general_profile_idc;
> > +    avctx->level    = sps->profile_tier_level.general_level_idc;
> > +
> > +
> > +
> > +    if(sps->sps_ptl_dpb_hrd_params_present_flag &&
> sps->sps_timing_hrd_params_present_flag) {
> > +        num = sps->sps_general_timing_hrd_parameters.num_units_in_tick;
> > +        den = sps->sps_general_timing_hrd_parameters.time_scale;
> > +    } else {
> > +        av_log(avctx, AV_LOG_INFO, "No
> sps_timing_hrd_params_present_flag in sps, the fps may not right.\n");
> > +        goto end;
> > +    }
> > +    if (num != 0 && den != 0)
> > +        av_reduce(&avctx->framerate.den, &avctx->framerate.num,
> > +                  num, den, 1 << 30);
> > +end:
> > +
> > +    ff_cbs_fragment_reset(pu);
> > +    s->cbc->log_ctx = NULL;
> > +    return 0;
> > +}
> > +
> > +static int h266_parser_parse(AVCodecParserContext *s, AVCodecContext
> *avctx,
> > +                      const uint8_t **poutbuf, int *poutbuf_size,
> > +                      const uint8_t *buf, int buf_size)
> > +{
> > +    int next;
> > +    H266ParserContext *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) {
> > +        av_log(avctx, AV_LOG_INFO, "extra data is not supported
> yet.\n");
> > +        return AVERROR_PATCHWELCOME;
>
> You're aborting here if extradata is present, but then check again in
> parse_nal_units() and actually try to parse it? Either parse it here or
> in parse_nal_units().
>
> Judging by this check here, i assume in your tests you didn't handle
> streams with extradata, which is expected from raw bitstreams parsed by
> the demuxer you added in patch 3/6.
> To workaround this, you need to add VVC support to the extract_extradata
> bitstream filter. It should be trivial to do.
>
Currently, I just focus on the raw bitstream. I only return an error for
extra data.
After we got a decent pass rate for the raw bitstream, I will find some
container clips and use them to test codec data.
We still have some works to support container format, like support vvcC
codec data. add a flush function to the vvdec decoder...


> > +    }
> > +
> > +    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
> > +        next = buf_size;
> > +    } else {
> > +        next = find_frame_end(s, buf, buf_size);
> > +        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
> > +            *poutbuf      = NULL;
> > +            *poutbuf_size = 0;
> > +            return buf_size;
> > +        }
> > +    }
> > +
> > +    is_dummy_buf &= (dummy_buf == buf);
> > +
> > +    if (!is_dummy_buf)
> > +        parse_nal_units(s, buf, buf_size, avctx);
> > +
> > +    *poutbuf      = buf;
> > +    *poutbuf_size = buf_size;
> > +    return next;
> > +
> > +}
> > +
> > +static const CodedBitstreamUnitType decompose_unit_types[] = {
> > +    H266_NAL_TRAIL,
> > +    H266_NAL_STSA,
> > +    H266_NAL_RADL,
> > +    H266_NAL_RASL,
> > +    H266_NAL_IDR_W_RADL,
> > +    H266_NAL_IDR_N_LP,
> > +    H266_NAL_CRA_NUT,
> > +    H266_NAL_GDR_NUT,
> > +    H266_NAL_VPS,
> > +    H266_NAL_SPS,
> > +    H266_NAL_PPS,
> > +    H266_NAL_PH,
> > +    H266_NAL_AUD,
> > +};
> > +
> > +static av_cold int h266_parser_init(AVCodecParserContext *ctx)
> > +{
> > +    H266ParserContext *s = ctx->priv_data;
> > +    int ret;
> > +
> > +    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VVC, NULL);
> > +    if (ret < 0)
> > +        return ret;
> > +
> > +    s->cbc->decompose_unit_types    = (CodedBitstreamUnitType
> *)decompose_unit_types;
> > +    s->cbc->nb_decompose_unit_types =
> FF_ARRAY_ELEMS(decompose_unit_types);
> > +
> > +    return 0;
> > +}
> > +
> > +static void h266_parser_close(AVCodecParserContext *ctx)
> > +{
> > +    H266ParserContext *s = ctx->priv_data;
> > +
> > +    ff_cbs_fragment_free(&s->picture_unit);
> > +    ff_cbs_close(&s->cbc);
> > +    av_freep(&s->pc.buffer);
> > +}
> > +
> > +AVCodecParser ff_h266_parser = {
> > +    .codec_ids      = { AV_CODEC_ID_VVC },
> > +    .priv_data_size = sizeof(H266ParserContext),
> > +    .parser_init    = h266_parser_init,
> > +    .parser_close   = h266_parser_close,
> > +    .parser_parse   = h266_parser_parse,
> > +};
> > diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c
> > index 83271d95a3..155aec71aa 100644
> > --- a/libavcodec/parsers.c
> > +++ b/libavcodec/parsers.c
> > @@ -49,6 +49,7 @@ extern AVCodecParser ff_gsm_parser;
> >   extern AVCodecParser ff_h261_parser;
> >   extern AVCodecParser ff_h263_parser;
> >   extern AVCodecParser ff_h264_parser;
> > +extern AVCodecParser ff_h266_parser;
> >   extern AVCodecParser ff_hevc_parser;
> >   extern AVCodecParser ff_ipu_parser;
> >   extern AVCodecParser ff_jpeg2000_parser;
> >
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".


More information about the ffmpeg-devel mailing list