diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c
index 09aa1765fd..9f081a3879 100644
--- a/libavcodec/h264_metadata_bsf.c
+++ b/libavcodec/h264_metadata_bsf.c
@@ -49,7 +49,8 @@ enum {
 typedef struct H264MetadataContext {
     const AVClass *class;
 
-    CodedBitstreamContext *cbc;
+    CodedBitstreamContext *cbc_in;
+    CodedBitstreamContext *cbc_out;
     CodedBitstreamFragment access_unit;
 
     int done_first_au;
@@ -289,7 +290,7 @@ static int h264_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
     if (!side_data_size)
         return 0;
 
-    err = ff_cbs_read(ctx->cbc, au, side_data, side_data_size);
+    err = ff_cbs_read(ctx->cbc_in, au, side_data, side_data_size);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
         return err;
@@ -303,7 +304,7 @@ static int h264_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
         }
     }
 
-    err = ff_cbs_write_fragment_data(ctx->cbc, au);
+    err = ff_cbs_write_fragment_data(ctx->cbc_out, au);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
         return err;
@@ -334,7 +335,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
     if (err < 0)
         goto fail;
 
-    err = ff_cbs_read_packet(ctx->cbc, au, pkt);
+    err = ff_cbs_read_packet(ctx->cbc_in, au, pkt);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
         goto fail;
@@ -602,7 +603,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
         }
     }
 
-    err = ff_cbs_write_packet(ctx->cbc, pkt, au);
+    err = ff_cbs_write_packet(ctx->cbc_out, pkt, au);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
         goto fail;
@@ -626,12 +627,15 @@ static int h264_metadata_init(AVBSFContext *bsf)
     CodedBitstreamFragment *au = &ctx->access_unit;
     int err, i;
 
-    err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_H264, bsf);
+    err = ff_cbs_init(&ctx->cbc_in,  AV_CODEC_ID_H264, bsf);
+    if (err < 0)
+        return err;
+    err = ff_cbs_init(&ctx->cbc_out, AV_CODEC_ID_H264, bsf);
     if (err < 0)
         return err;
 
     if (bsf->par_in->extradata) {
-        err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in);
+        err = ff_cbs_read_extradata(ctx->cbc_in, au, bsf->par_in);
         if (err < 0) {
             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
             goto fail;
@@ -645,7 +649,7 @@ static int h264_metadata_init(AVBSFContext *bsf)
             }
         }
 
-        err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, au);
+        err = ff_cbs_write_extradata(ctx->cbc_out, bsf->par_out, au);
         if (err < 0) {
             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
             goto fail;
@@ -663,7 +667,8 @@ static void h264_metadata_close(AVBSFContext *bsf)
     H264MetadataContext *ctx = bsf->priv_data;
 
     ff_cbs_fragment_free(&ctx->access_unit);
-    ff_cbs_close(&ctx->cbc);
+    ff_cbs_close(&ctx->cbc_in);
+    ff_cbs_close(&ctx->cbc_out);
 }
 
 #define OFFSET(x) offsetof(H264MetadataContext, x)
diff --git a/libavcodec/h265_metadata_bsf.c b/libavcodec/h265_metadata_bsf.c
index b48a0bd3e9..57b248542c 100644
--- a/libavcodec/h265_metadata_bsf.c
+++ b/libavcodec/h265_metadata_bsf.c
@@ -40,7 +40,8 @@ enum {
 typedef struct H265MetadataContext {
     const AVClass *class;
 
-    CodedBitstreamContext *cbc;
+    CodedBitstreamContext *cbc_in;
+    CodedBitstreamContext *cbc_out;
     CodedBitstreamFragment access_unit;
 
     H265RawAUD aud_nal;
@@ -350,7 +351,7 @@ static int h265_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
     if (!side_data_size)
         return 0;
 
-    err = ff_cbs_read(ctx->cbc, au, side_data, side_data_size);
+    err = ff_cbs_read(ctx->cbc_in, au, side_data, side_data_size);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
         return err;
@@ -372,7 +373,7 @@ static int h265_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
         }
     }
 
-    err = ff_cbs_write_fragment_data(ctx->cbc, au);
+    err = ff_cbs_write_fragment_data(ctx->cbc_out, au);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
         return err;
@@ -402,7 +403,7 @@ static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
     if (err < 0)
         goto fail;
 
-    err = ff_cbs_read_packet(ctx->cbc, au, pkt);
+    err = ff_cbs_read_packet(ctx->cbc_in, au, pkt);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
         goto fail;
@@ -473,7 +474,7 @@ static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
         }
     }
 
-    err = ff_cbs_write_packet(ctx->cbc, pkt, au);
+    err = ff_cbs_write_packet(ctx->cbc_out, pkt, au);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
         goto fail;
@@ -495,12 +496,15 @@ static int h265_metadata_init(AVBSFContext *bsf)
     CodedBitstreamFragment *au = &ctx->access_unit;
     int err, i;
 
-    err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_HEVC, bsf);
+    err = ff_cbs_init(&ctx->cbc_in,  AV_CODEC_ID_HEVC, bsf);
+    if (err < 0)
+        return err;
+    err = ff_cbs_init(&ctx->cbc_out, AV_CODEC_ID_HEVC, bsf);
     if (err < 0)
         return err;
 
     if (bsf->par_in->extradata) {
-        err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in);
+        err = ff_cbs_read_extradata(ctx->cbc_in, au, bsf->par_in);
         if (err < 0) {
             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
             goto fail;
@@ -522,7 +526,7 @@ static int h265_metadata_init(AVBSFContext *bsf)
             }
         }
 
-        err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, au);
+        err = ff_cbs_write_extradata(ctx->cbc_out, bsf->par_out, au);
         if (err < 0) {
             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
             goto fail;
@@ -540,7 +544,8 @@ static void h265_metadata_close(AVBSFContext *bsf)
     H265MetadataContext *ctx = bsf->priv_data;
 
     ff_cbs_fragment_free(&ctx->access_unit);
-    ff_cbs_close(&ctx->cbc);
+    ff_cbs_close(&ctx->cbc_in);
+    ff_cbs_close(&ctx->cbc_out);
 }
 
 #define OFFSET(x) offsetof(H265MetadataContext, x)
