[FFmpeg-devel] [PATCH] avcodec/bsf: Avoid allocation for AVBSFInternal
James Almer
jamrial at gmail.com
Mon Aug 10 17:20:09 EEST 2020
On 8/10/2020 10:55 AM, Andreas Rheinhardt wrote:
> by allocating it together with the AVBSFContext.
>
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
> ---
> Similar things can of course be done with other structures. I did only
> this one to see whether everyone is ok with this.
Personally, i don't like it. It's extra complexity to save a single 8 or
12 byte allocation that happens once during bsf alloc. It's kind of a
pointless micro-optimization.
>
> libavcodec/bsf.c | 25 +++++++++++++------------
> 1 file changed, 13 insertions(+), 12 deletions(-)
>
> diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c
> index d71bc32584..9781be78e7 100644
> --- a/libavcodec/bsf.c
> +++ b/libavcodec/bsf.c
> @@ -37,6 +37,11 @@ struct AVBSFInternal {
> int eof;
> };
>
> +typedef struct AVBSFCombined {
> + AVBSFContext bsf;
> + AVBSFInternal internal;
> +} AVBSFCombined;
> +
> void av_bsf_free(AVBSFContext **pctx)
> {
> AVBSFContext *ctx;
> @@ -50,9 +55,8 @@ void av_bsf_free(AVBSFContext **pctx)
> if (ctx->filter->priv_class && ctx->priv_data)
> av_opt_free(ctx->priv_data);
>
> - if (ctx->internal)
> - av_packet_free(&ctx->internal->buffer_pkt);
> - av_freep(&ctx->internal);
> + av_assert2(ctx->internal);
> + av_packet_free(&ctx->internal->buffer_pkt);
> av_freep(&ctx->priv_data);
>
> avcodec_parameters_free(&ctx->par_in);
> @@ -93,14 +97,18 @@ const AVClass *av_bsf_get_class(void)
>
> int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
> {
> + AVBSFCombined *com;
> AVBSFContext *ctx;
> AVBSFInternal *bsfi;
> int ret;
>
> - ctx = av_mallocz(sizeof(*ctx));
> - if (!ctx)
> + com = av_mallocz(sizeof(*com));
> + if (!com)
> return AVERROR(ENOMEM);
>
> + ctx = &com->bsf;
> + bsfi = ctx->internal = &com->internal;
> +
> ctx->av_class = &bsf_class;
> ctx->filter = filter;
>
> @@ -111,13 +119,6 @@ int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
> goto fail;
> }
>
> - bsfi = av_mallocz(sizeof(*bsfi));
> - if (!bsfi) {
> - ret = AVERROR(ENOMEM);
> - goto fail;
> - }
> - ctx->internal = bsfi;
> -
> bsfi->buffer_pkt = av_packet_alloc();
> if (!bsfi->buffer_pkt) {
> ret = AVERROR(ENOMEM);
>
More information about the ffmpeg-devel
mailing list