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

James Almer jamrial at gmail.com
Mon Aug 10 21:06:15 EEST 2020


This function lets the API user reuse an AVBSFContext in case of stream
parameter changes.

Signed-off-by: James Almer <jamrial at gmail.com>
---
Missing version bump and APIChanges entry.

 libavcodec/bsf.c | 21 +++++++++++++++++++++
 libavcodec/bsf.h | 14 ++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c
index d71bc32584..667c40863d 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,26 @@ 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->filter->priv_class && ctx->priv_data) {
+        av_opt_free(ctx->priv_data);
+        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);
 
-- 
2.27.0



More information about the ffmpeg-devel mailing list