[FFmpeg-devel] [PATCH 3/3 v2] avcodec/bsf: add av_bsf_close()

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Tue Aug 11 02:26:33 EEST 2020


James Almer:
> This function lets the API user reuse an AVBSFContext in case of stream
> parameter changes.
> 
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  libavcodec/bsf.c | 26 ++++++++++++++++++++++++++
>  libavcodec/bsf.h | 14 ++++++++++++++
>  2 files changed, 40 insertions(+)
> 
> diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c
> index d71bc32584..c8acd40ec0 100644
> --- a/libavcodec/bsf.c
> +++ b/libavcodec/bsf.c
> @@ -29,6 +29,7 @@
>  #include "bsf_internal.h"
>  #include "codec_desc.h"
>  #include "codec_par.h"
> +#include "internal.h"
>  
>  #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
>  
> @@ -197,6 +198,31 @@ void av_bsf_flush(AVBSFContext *ctx)
>          ctx->filter->flush(ctx);
>  }
>  
> +void av_bsf_close(AVBSFContext *ctx)
> +{
> +    AVBSFInternal *bsfi = ctx->internal;
> +
> +    if (ctx->filter->close)
> +        ctx->filter->close(ctx);
> +    if (ctx->priv_data) {
> +        if (ctx->filter->priv_class)
> +            av_opt_free(ctx->priv_data);
> +        memset(ctx->priv_data, 0, ctx->filter->priv_data_size);
> +        if (ctx->filter->priv_class) {
> +            *(const AVClass **)ctx->priv_data = ctx->filter->priv_class;
> +            av_opt_set_defaults(ctx->priv_data);
> +        }
> +    }
> +
> +    bsfi->eof = 0;
> +    av_packet_unref(bsfi->buffer_pkt);
> +
> +    ctx->time_base_out = ctx->time_base_in = (AVRational){ 0, 0 };
> +
> +    ff_codec_parameters_reset(ctx->par_in);
> +    ff_codec_parameters_reset(ctx->par_out);
> +}
> +
>  int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
>  {
>      AVBSFInternal *bsfi = ctx->internal;
> diff --git a/libavcodec/bsf.h b/libavcodec/bsf.h
> index 3b5faa85cb..f72c45f7e5 100644
> --- a/libavcodec/bsf.h
> +++ b/libavcodec/bsf.h
> @@ -216,12 +216,26 @@ int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt);
>  
>  /**
>   * Reset the internal bitstream filter state. Should be called e.g. when seeking.
> + *
> + * @see av_bsf_close().
>   */
>  void av_bsf_flush(AVBSFContext *ctx);
>  
> +/**
> + * Close a bitstream filter context, leaving it in the same state as when it was
> + * freshly allocated. May be called e.g. to reuse the context in case of stream
> + * parameter changes.
> + *
> + * @see av_bsf_flush().
> + * @see av_bsf_free().
> + */
> +void av_bsf_close(AVBSFContext *ctx);
> +
>  /**
>   * Free a bitstream filter context and everything associated with it; write NULL
>   * into the supplied pointer.
> + *
> + * @see av_bsf_close().
>   */
>  void av_bsf_free(AVBSFContext **ctx);
>  
> 
I just had a thought about this: What to do with a BSF of type bsf_list?
av_bsf_close() will close and free all of the bsf in the internal list,
leaving the thing completely unusable: It just passes through any
packets unmodified. One would need to resurrect/adapt Marton's patch
from a few months ago that allows to add a bsf to an already existing
bsf list to make av_bsf_close() usable with these bsf lists.

- Andreas


More information about the ffmpeg-devel mailing list