[FFmpeg-devel] cbs: Add function to make content of a unit writable

Submitted by Andreas Rheinhardt on Dec. 3, 2018, 5:35 p.m.

Details

Message ID 20181203173528.736-1-andreas.rheinhardt@googlemail.com
State New
Headers show

Commit Message

Andreas Rheinhardt Dec. 3, 2018, 5:35 p.m.
This will enable us to change e.g. the parameter sets of H.2645 in ways
that would change the parsing process of future units. An example of
this is the h264_redundant_pps bsf.
The actual implementation of the underlying codec-dependent
make_writable functions is not contained in this commit.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
---

Unfortunately I messed up the function parameters and made the content
parameter pretty much unusable. This version fixes this.
Notice that it supposes that both pointer to void as well as pointer to
structures have the same size and object representation. Other functions
like several memory-related ones (that use a void * argument for a
pointer to a pointer) make the same assumption, so I guess that it is
ok.

 libavcodec/cbs.c          | 21 +++++++++++++++++++++
 libavcodec/cbs.h          | 10 ++++++++++
 libavcodec/cbs_internal.h |  4 ++++
 3 files changed, 35 insertions(+)

Patch hide | download patch | download mbox

diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index ecbf57c293..13bcbb722f 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -665,3 +665,24 @@  int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
 
     return 0;
 }
+
+int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
+                              CodedBitstreamUnit *unit,
+                              void *content)
+{
+    if (unit->content && (!unit->content_ref ||
+                          !av_buffer_is_writable(unit->content_ref))) {
+        int err;
+        if (!ctx->codec->make_writable)
+            return AVERROR_PATCHWELCOME;
+
+        err = ctx->codec->make_writable(ctx, unit);
+        if (err < 0)
+            return err;
+
+        if (content)
+            memcpy(content, &unit->content, sizeof(content));
+    }
+
+    return 0;
+}
diff --git a/libavcodec/cbs.h b/libavcodec/cbs.h
index 53ac360bb1..a17c43dae4 100644
--- a/libavcodec/cbs.h
+++ b/libavcodec/cbs.h
@@ -352,5 +352,15 @@  int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
                        CodedBitstreamFragment *frag,
                        int position);
 
+/**
+ * Make the content of a unit writable.
+ *
+ * If content is supplied, it is supposed to be a pointer to a pointer
+ * and *content will point to the content of the unit on success.
+ */
+int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
+                              CodedBitstreamUnit *unit,
+                              void *content);
+
 
 #endif /* AVCODEC_CBS_H */
diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h
index 53f2e5d187..62a836af90 100644
--- a/libavcodec/cbs_internal.h
+++ b/libavcodec/cbs_internal.h
@@ -44,6 +44,10 @@  typedef struct CodedBitstreamType {
     int (*read_unit)(CodedBitstreamContext *ctx,
                      CodedBitstreamUnit *unit);
 
+    // Make a unit's content writable.
+    int (*make_writable)(CodedBitstreamContext *ctx,
+                         CodedBitstreamUnit *unit);
+
     // Write the unit->data bitstream from unit->content.
     int (*write_unit)(CodedBitstreamContext *ctx,
                       CodedBitstreamUnit *unit);