[FFmpeg-devel] [PATCH] avcodec/vvc_parser: Don't store state unnecessarily

James Almer jamrial at gmail.com
Fri Jun 30 00:58:41 EEST 2023


On 6/29/2023 6:53 PM, Andreas Rheinhardt wrote:
> VVCParserContext.au_info is only used once (and in a read-only manner);
> but this happens immediately after au_info has been completely
> overwritten. Therefore one can just the src structure used to overwrite
> au_info directly and remove au_info.
> 
> This also means that the whole referencing and unreferncing of au_info
> (which duplicates AVBufferRefs CodedBitstreamH266Context and is
> therefore of dubious gain) can be removed, as can the AVBufferRef*
> contained in PuInfo; this also removes a certain uglyness: Sometimes
> these AVBufferRef* were ownership pointers and sometimes not.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
> ---
>   libavcodec/vvc_parser.c | 63 +----------------------------------------
>   1 file changed, 1 insertion(+), 62 deletions(-)
> 
> diff --git a/libavcodec/vvc_parser.c b/libavcodec/vvc_parser.c
> index efea833565..69696eef57 100644
> --- a/libavcodec/vvc_parser.c
> +++ b/libavcodec/vvc_parser.c
> @@ -29,11 +29,6 @@
>   #define IS_H266_SLICE(nut) (nut <= VVC_RASL_NUT || (nut >= VVC_IDR_W_RADL && nut <= VVC_GDR_NUT))
>   
>   typedef struct PuInfo {
> -    AVBufferRef *sps_ref;
> -    AVBufferRef *pps_ref;
> -    AVBufferRef *slice_ref;
> -    AVBufferRef *ph_ref;
> -
>       const H266RawPPS *pps;
>       const H266RawSPS *sps;
>       const H266RawPH *ph;
> @@ -53,7 +48,6 @@ typedef struct VVCParserContext {
>   
>       CodedBitstreamFragment picture_unit;
>   
> -    PuInfo   au_info;
>       AVPacket au;
>       AVPacket last_au;
>   
> @@ -150,41 +144,6 @@ static int get_pict_type(const CodedBitstreamFragment *pu)
>       return has_p ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
>   }
>   
> -static void pu_info_unref(PuInfo *info)
> -{
> -    av_buffer_unref(&info->slice_ref);
> -    av_buffer_unref(&info->ph_ref);
> -    av_buffer_unref(&info->pps_ref);
> -    av_buffer_unref(&info->sps_ref);
> -    info->slice = NULL;
> -    info->ph = NULL;
> -    info->pps = NULL;
> -    info->sps = NULL;
> -    info->pic_type = AV_PICTURE_TYPE_NONE;
> -}
> -
> -static int pu_info_ref(PuInfo *dest, const PuInfo *src)
> -{
> -    pu_info_unref(dest);
> -    dest->sps_ref = av_buffer_ref(src->sps_ref);
> -    dest->pps_ref = av_buffer_ref(src->pps_ref);
> -    if (src->ph_ref)
> -        dest->ph_ref = av_buffer_ref(src->ph_ref);
> -    dest->slice_ref = av_buffer_ref(src->slice_ref);
> -    if (!dest->sps_ref || !dest->pps_ref || (src->ph_ref && !dest->ph_ref)
> -        || !dest->slice_ref) {
> -        pu_info_unref(dest);
> -        return AVERROR(ENOMEM);
> -    }
> -
> -    dest->sps = src->sps;
> -    dest->pps = src->pps;
> -    dest->ph = src->ph;
> -    dest->slice = src->slice;
> -    dest->pic_type = src->pic_type;
> -    return 0;
> -}
> -
>   static void set_parser_ctx(AVCodecParserContext *s, AVCodecContext *avctx,
>                              const PuInfo *pu)
>   {
> @@ -241,20 +200,6 @@ static void set_parser_ctx(AVCodecParserContext *s, AVCodecContext *avctx,
>       }
>   }
>   
> -static int set_ctx(AVCodecParserContext *s, AVCodecContext *avctx,
> -                   const PuInfo *next_pu)
> -{
> -    VVCParserContext *ctx = s->priv_data;
> -
> -    int ret = pu_info_ref(&ctx->au_info, next_pu);
> -    if (ret < 0)
> -        return ret;
> -
> -    set_parser_ctx(s, avctx, &ctx->au_info);
> -
> -    return 0;
> -}
> -
>   //8.3.1 Decoding process for picture order count.
>   //VTM did not follow the spec, and it's much simpler than spec.
>   //We follow the VTM.
> @@ -338,10 +283,8 @@ static int get_pu_info(PuInfo *info, const CodedBitstreamH266Context *h266,
>               continue;
>           if ( nal->nal_unit_type == VVC_PH_NUT ) {
>               info->ph = pu->units[i].content;
> -            info->ph_ref = pu->units[i].content_ref;
>           } else if (IS_H266_SLICE(nal->nal_unit_type)) {
>               info->slice = pu->units[i].content;
> -            info->slice_ref = pu->units[i].content_ref;
>               if (info->slice->header.sh_picture_header_in_slice_header_flag)
>                   info->ph = &info->slice->header.sh_picture_header;
>               if (!info->ph) {
> @@ -365,7 +308,6 @@ static int get_pu_info(PuInfo *info, const CodedBitstreamH266Context *h266,
>           ret = AVERROR_INVALIDDATA;
>           goto error;
>       }
> -    info->pps_ref = h266->pps_ref[info->ph->ph_pic_parameter_set_id];
>       info->sps = h266->sps[info->pps->pps_seq_parameter_set_id];
>       if (!info->sps) {
>           av_log(logctx, AV_LOG_ERROR, "SPS id %d is not avaliable.\n",
> @@ -373,7 +315,6 @@ static int get_pu_info(PuInfo *info, const CodedBitstreamH266Context *h266,
>           ret = AVERROR_INVALIDDATA;
>           goto error;
>       }
> -    info->sps_ref = h266->sps_ref[info->pps->pps_seq_parameter_set_id];
>       info->pic_type = get_pict_type(pu);
>       return 0;
>     error:
> @@ -430,8 +371,7 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
>           goto end;
>       }
>       if (is_au_start(ctx, &info, avctx)) {
> -        if ((ret = set_ctx(s, avctx, &info)) < 0)
> -            goto end;
> +        set_parser_ctx(s, avctx, &info);
>           av_packet_move_ref(&ctx->last_au, &ctx->au);
>       } else {
>           ret = 1; //not a completed au
> @@ -560,7 +500,6 @@ static void vvc_parser_close(AVCodecParserContext *s)
>   {
>       VVCParserContext *ctx = s->priv_data;
>   
> -    pu_info_unref(&ctx->au_info);
>       av_packet_unref(&ctx->au);
>       av_packet_unref(&ctx->last_au);
>       ff_cbs_fragment_free(&ctx->picture_unit);

Should be ok.


More information about the ffmpeg-devel mailing list