diff mbox series

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

Message ID 20200810180615.5222-3-jamrial@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,1/3] avcodec/bsf: improve the doxy for av_bsf_flush()
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

James Almer Aug. 10, 2020, 6:06 p.m. UTC
This function lets the API user reuse an AVBSFContext in case of stream
parameter changes.

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

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

Comments

Andreas Rheinhardt Aug. 10, 2020, 6:33 p.m. UTC | #1
James Almer:
> This function lets the API user reuse an AVBSFContext in case of stream
> parameter changes.
> 
> Signed-off-by: James Almer <jamrial@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);

ctx->priv_data needs to be zeroed here (and this also has to happen if
the bsf does not have a private 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);

The advantages of this approach instead of just using a new context seem
to be very tiny.

> +
>  /**
>   * 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);
>  
>
James Almer Aug. 10, 2020, 6:39 p.m. UTC | #2
On 8/10/2020 3:33 PM, Andreas Rheinhardt wrote:
> James Almer:
>> This function lets the API user reuse an AVBSFContext in case of stream
>> parameter changes.
>>
>> Signed-off-by: James Almer <jamrial@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);
> 
> ctx->priv_data needs to be zeroed here (and this also has to happen if
> the bsf does not have a private class).

Good catch. Will fix it.

> 
>> +        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);
> 
> The advantages of this approach instead of just using a new context seem
> to be very tiny.

There's currently no part of our code that would benefit from this
function, as far as i could see, but i figured some library user may
find it useful to slightly simplify their code (a single close() call
instead of free() then alloc(), which also needs a return code check,
before the required par_in setup and init() calls).

> 
>> +
>>  /**
>>   * 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);
>>  
>>
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
diff mbox series

Patch

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);