Re: [libav-devel] [PATCH 01/16] cbs: Allocate the context inside the init function

2018-02-18 Thread Luca Barbato

On 11/02/2018 19:14, Mark Thompson wrote:

... instead of making the caller allocate it themselves.  This is
more consistent with other APIs in libav.
---
This one and the following two have been hanging around for a while (they came 
from the merge into the other tine).


  libavcodec/cbs.c| 20 +---
  libavcodec/cbs.h|  6 +++---
  libavcodec/h264_metadata_bsf.c  | 20 ++--
  libavcodec/h264_redundant_pps_bsf.c | 18 +-
  libavcodec/h265_metadata_bsf.c  | 18 +-
  libavcodec/mpeg2_metadata_bsf.c | 16 
  libavcodec/trace_headers_bsf.c  | 14 +++---
  libavcodec/vaapi_encode_h264.c  | 14 +++---
  libavcodec/vaapi_encode_h265.c  | 10 +-
  libavcodec/vaapi_encode_mpeg2.c | 10 +-
  10 files changed, 80 insertions(+), 66 deletions(-)



Probably ok.
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

[libav-devel] [PATCH 01/16] cbs: Allocate the context inside the init function

2018-02-11 Thread Mark Thompson
... instead of making the caller allocate it themselves.  This is
more consistent with other APIs in libav.
---
This one and the following two have been hanging around for a while (they came 
from the merge into the other tine).


 libavcodec/cbs.c| 20 +---
 libavcodec/cbs.h|  6 +++---
 libavcodec/h264_metadata_bsf.c  | 20 ++--
 libavcodec/h264_redundant_pps_bsf.c | 18 +-
 libavcodec/h265_metadata_bsf.c  | 18 +-
 libavcodec/mpeg2_metadata_bsf.c | 16 
 libavcodec/trace_headers_bsf.c  | 14 +++---
 libavcodec/vaapi_encode_h264.c  | 14 +++---
 libavcodec/vaapi_encode_h265.c  | 10 +-
 libavcodec/vaapi_encode_mpeg2.c | 10 +-
 10 files changed, 80 insertions(+), 66 deletions(-)

diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index 3baa31a4d..fd9baa299 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -39,9 +39,10 @@ static const CodedBitstreamType *cbs_type_table[] = {
 #endif
 };
 
-int ff_cbs_init(CodedBitstreamContext *ctx,
+int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
 enum AVCodecID codec_id, void *log_ctx)
 {
+CodedBitstreamContext *ctx;
 const CodedBitstreamType *type;
 int i;
 
@@ -55,27 +56,40 @@ int ff_cbs_init(CodedBitstreamContext *ctx,
 if (!type)
 return AVERROR(EINVAL);
 
+ctx = av_mallocz(sizeof(*ctx));
+if (!ctx)
+return AVERROR(ENOMEM);
+
 ctx->log_ctx = log_ctx;
 ctx->codec   = type;
 
 ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
-if (!ctx->priv_data)
+if (!ctx->priv_data) {
+av_freep();
 return AVERROR(ENOMEM);
+}
 
 ctx->decompose_unit_types = NULL;
 
 ctx->trace_enable = 0;
 ctx->trace_level  = AV_LOG_TRACE;
 
+*ctx_ptr = ctx;
 return 0;
 }
 
-void ff_cbs_close(CodedBitstreamContext *ctx)
+void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
 {
+CodedBitstreamContext *ctx = *ctx_ptr;
+
+if (!ctx)
+return;
+
 if (ctx->codec && ctx->codec->close)
 ctx->codec->close(ctx);
 
 av_freep(>priv_data);
+av_freep(ctx_ptr);
 }
 
 static void cbs_unit_uninit(CodedBitstreamContext *ctx,
diff --git a/libavcodec/cbs.h b/libavcodec/cbs.h
index 01b2239b7..34ee78be3 100644
--- a/libavcodec/cbs.h
+++ b/libavcodec/cbs.h
@@ -169,15 +169,15 @@ typedef struct CodedBitstreamContext {
 
 
 /**
- * Initialise a new context for the given codec.
+ * Create and initialise a new context for the given codec.
  */
-int ff_cbs_init(CodedBitstreamContext *ctx,
+int ff_cbs_init(CodedBitstreamContext **ctx,
 enum AVCodecID codec_id, void *log_ctx);
 
 /**
  * Close a context and free all internal state.
  */
-void ff_cbs_close(CodedBitstreamContext *ctx);
+void ff_cbs_close(CodedBitstreamContext **ctx);
 
 
 /**
diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c
index ac0b9823b..2b579e9d3 100644
--- a/libavcodec/h264_metadata_bsf.c
+++ b/libavcodec/h264_metadata_bsf.c
@@ -35,7 +35,7 @@ enum {
 typedef struct H264MetadataContext {
 const AVClass *class;
 
-CodedBitstreamContext cbc;
+CodedBitstreamContext *cbc;
 CodedBitstreamFragment access_unit;
 
 H264RawAUD aud_nal;
@@ -214,7 +214,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket 
*out)
 if (err < 0)
 goto fail;
 
-err = ff_cbs_read_packet(>cbc, au, in);
+err = ff_cbs_read_packet(ctx->cbc, au, in);
 if (err < 0) {
 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
 goto fail;
@@ -229,7 +229,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket 
*out)
 // If an AUD is present, it must be the first NAL unit.
 if (au->units[0].type == H264_NAL_AUD) {
 if (ctx->aud == REMOVE)
-ff_cbs_delete_unit(>cbc, au, 0);
+ff_cbs_delete_unit(ctx->cbc, au, 0);
 } else {
 if (ctx->aud == INSERT) {
 static const int primary_pic_type_table[] = {
@@ -269,7 +269,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket 
*out)
 aud->nal_unit_header.nal_unit_type = H264_NAL_AUD;
 aud->primary_pic_type = j;
 
-err = ff_cbs_insert_unit_content(>cbc, au,
+err = ff_cbs_insert_unit_content(ctx->cbc, au,
  0, H264_NAL_AUD, aud);
 if (err < 0) {
 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
@@ -314,7 +314,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket 
*out)
 
 sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
 
-err = ff_cbs_insert_unit_content(>cbc, au,
+err = ff_cbs_insert_unit_content(ctx->cbc, au,
  sei_pos, H264_NAL_SEI, sei);
 if (err < 0) {
 av_log(bsf, AV_LOG_ERROR, "Failed to insert SEI.\n");
@@