diff mbox series

[FFmpeg-devel,v6,14/22] cbs_h264: Simplify SEI addition

Message ID 20200727163237.23371-15-sw@jkqxz.net
State New
Headers show
Series CBS unit type tables and assorted related stuff | expand

Checks

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

Commit Message

Mark Thompson July 27, 2020, 4:32 p.m. UTC
At the same time, move the H.264 SEI functions to a new file - the combined
H.26[45] CBS file is already very large, and these functions do not require
any of the common read/write elements.
---
 libavcodec/Makefile    |   2 +-
 libavcodec/cbs_h264.c  | 108 +++++++++++++++++++++++++++++++++++++++++
 libavcodec/cbs_h264.h  |  11 +++++
 libavcodec/cbs_h2645.c |  94 +----------------------------------
 4 files changed, 122 insertions(+), 93 deletions(-)
 create mode 100644 libavcodec/cbs_h264.c
diff mbox series

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index c48138d0ad..6394694617 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -70,7 +70,7 @@  OBJS-$(CONFIG_BSWAPDSP)                += bswapdsp.o
 OBJS-$(CONFIG_CABAC)                   += cabac.o
 OBJS-$(CONFIG_CBS)                     += cbs.o
 OBJS-$(CONFIG_CBS_AV1)                 += cbs_av1.o
-OBJS-$(CONFIG_CBS_H264)                += cbs_h2645.o h2645_parse.o
+OBJS-$(CONFIG_CBS_H264)                += cbs_h2645.o cbs_h264.o h2645_parse.o
 OBJS-$(CONFIG_CBS_H265)                += cbs_h2645.o h2645_parse.o
 OBJS-$(CONFIG_CBS_JPEG)                += cbs_jpeg.o
 OBJS-$(CONFIG_CBS_MPEG2)               += cbs_mpeg2.o
diff --git a/libavcodec/cbs_h264.c b/libavcodec/cbs_h264.c
new file mode 100644
index 0000000000..c86c6d565c
--- /dev/null
+++ b/libavcodec/cbs_h264.c
@@ -0,0 +1,108 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "cbs_h264.h"
+#include "cbs_internal.h"
+
+int ff_cbs_h264_add_sei_message(CodedBitstreamFragment *au,
+                                H264RawSEIPayload *payload)
+{
+    H264RawSEI *sei = NULL;
+    int err, i;
+
+    // Find an existing SEI NAL unit to add to.
+    for (i = 0; i < au->nb_units; i++) {
+        if (au->units[i].type == H264_NAL_SEI) {
+            sei = au->units[i].content;
+            if (sei->payload_count < H264_MAX_SEI_PAYLOADS)
+                break;
+
+            sei = NULL;
+        }
+    }
+
+    if (!sei) {
+        // Need to make a new SEI NAL unit.  Insert it before the first
+        // slice data NAL unit; if no slice data, add at the end.
+        CodedBitstreamUnit *unit;
+
+        for (i = 0; i < au->nb_units; i++) {
+            if (au->units[i].type == H264_NAL_SLICE ||
+                au->units[i].type == H264_NAL_IDR_SLICE)
+                break;
+        }
+
+        err = ff_cbs_insert_unit(au, i);
+        if (err < 0)
+            goto fail;
+        unit = &au->units[i];
+
+        // Needs a context for the type information but we don't have
+        // one, so forge one containing only the type information.
+        err = ff_cbs_alloc_unit_content2(&(CodedBitstreamContext) {
+                                       .codec = &ff_cbs_type_h264 }, unit);
+        if (err < 0) {
+            ff_cbs_delete_unit(au, i);
+            goto fail;
+        }
+        sei = unit->content;
+
+        *sei = (H264RawSEI) {
+            .nal_unit_header = {
+                .nal_unit_type = H264_NAL_SEI,
+            },
+        };
+    }
+
+    memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
+    ++sei->payload_count;
+
+    return 0;
+fail:
+    ff_cbs_h264_free_sei_payload(payload);
+    return err;
+}
+
+void ff_cbs_h264_delete_sei_message(CodedBitstreamFragment *au,
+                                    CodedBitstreamUnit *nal,
+                                    int position)
+{
+    H264RawSEI *sei = nal->content;
+
+    av_assert0(nal->type == H264_NAL_SEI);
+    av_assert0(position >= 0 && position < sei->payload_count);
+
+    if (position == 0 && sei->payload_count == 1) {
+        // Deleting NAL unit entirely.
+        int i;
+
+        for (i = 0; i < au->nb_units; i++) {
+            if (&au->units[i] == nal)
+                break;
+        }
+
+        ff_cbs_delete_unit(au, i);
+    } else {
+        ff_cbs_h264_free_sei_payload(&sei->payload[position]);
+
+        --sei->payload_count;
+        memmove(sei->payload + position,
+                sei->payload + position + 1,
+                (sei->payload_count - position) * sizeof(*sei->payload));
+    }
+}
diff --git a/libavcodec/cbs_h264.h b/libavcodec/cbs_h264.h
index a6fe0a6af2..88313629f5 100644
--- a/libavcodec/cbs_h264.h
+++ b/libavcodec/cbs_h264.h
@@ -466,11 +466,22 @@  typedef struct CodedBitstreamH264Context {
 } CodedBitstreamH264Context;
 
 
+/**
+ * Free an SEI payload structure.
+ *
+ * This is useful to have standalone when manipulating SEI messages inside
+ * an access unit.
+ */
+void ff_cbs_h264_free_sei_payload(H264RawSEIPayload *payload);
+
 /**
  * Add an SEI message to an access unit.
  *
  * On success, the payload will be owned by a unit in access_unit;
  * on failure, the content of the payload will be freed.
+ *
+ * Adds at the end of an existing SEI NAL unit if one is present, otherwise
+ * creates a new SEI NAL unit to contain the message.
  */
 int ff_cbs_h264_add_sei_message(CodedBitstreamFragment *access_unit,
                                 H264RawSEIPayload *payload);
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index b9233f4df7..603017d8fe 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -1324,7 +1324,7 @@  static void cbs_h265_close(CodedBitstreamContext *ctx)
         av_buffer_unref(&h265->pps_ref[i]);
 }
 
-static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
+void ff_cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
 {
     switch (payload->payload_type) {
     case H264_SEI_TYPE_BUFFERING_PERIOD:
@@ -1352,7 +1352,7 @@  static void cbs_h264_free_sei(void *opaque, uint8_t *content)
     H264RawSEI *sei = (H264RawSEI*)content;
     int i;
     for (i = 0; i < sei->payload_count; i++)
-        cbs_h264_free_sei_payload(&sei->payload[i]);
+        ff_cbs_h264_free_sei_payload(&sei->payload[i]);
     av_freep(&content);
 }
 
@@ -1497,93 +1497,3 @@  const CodedBitstreamType ff_cbs_type_h265 = {
 
     .close             = &cbs_h265_close,
 };
-
-int ff_cbs_h264_add_sei_message(CodedBitstreamFragment *au,
-                                H264RawSEIPayload *payload)
-{
-    H264RawSEI *sei = NULL;
-    int err, i;
-
-    // Find an existing SEI NAL unit to add to.
-    for (i = 0; i < au->nb_units; i++) {
-        if (au->units[i].type == H264_NAL_SEI) {
-            sei = au->units[i].content;
-            if (sei->payload_count < H264_MAX_SEI_PAYLOADS)
-                break;
-
-            sei = NULL;
-        }
-    }
-
-    if (!sei) {
-        // Need to make a new SEI NAL unit.  Insert it before the first
-        // slice data NAL unit; if no slice data, add at the end.
-        AVBufferRef *sei_ref;
-
-        sei = av_mallocz(sizeof(*sei));
-        if (!sei) {
-            err = AVERROR(ENOMEM);
-            goto fail;
-        }
-
-        sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
-        sei->nal_unit_header.nal_ref_idc   = 0;
-
-        sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
-                                   &cbs_h264_free_sei, NULL, 0);
-        if (!sei_ref) {
-            av_freep(&sei);
-            err = AVERROR(ENOMEM);
-            goto fail;
-        }
-
-        for (i = 0; i < au->nb_units; i++) {
-            if (au->units[i].type == H264_NAL_SLICE ||
-                au->units[i].type == H264_NAL_IDR_SLICE)
-                break;
-        }
-
-        err = ff_cbs_insert_unit_content(au, i, H264_NAL_SEI,
-                                         sei, sei_ref);
-        av_buffer_unref(&sei_ref);
-        if (err < 0)
-            goto fail;
-    }
-
-    memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
-    ++sei->payload_count;
-
-    return 0;
-fail:
-    cbs_h264_free_sei_payload(payload);
-    return err;
-}
-
-void ff_cbs_h264_delete_sei_message(CodedBitstreamFragment *au,
-                                    CodedBitstreamUnit *nal,
-                                    int position)
-{
-    H264RawSEI *sei = nal->content;
-
-    av_assert0(nal->type == H264_NAL_SEI);
-    av_assert0(position >= 0 && position < sei->payload_count);
-
-    if (position == 0 && sei->payload_count == 1) {
-        // Deleting NAL unit entirely.
-        int i;
-
-        for (i = 0; i < au->nb_units; i++) {
-            if (&au->units[i] == nal)
-                break;
-        }
-
-        ff_cbs_delete_unit(au, i);
-    } else {
-        cbs_h264_free_sei_payload(&sei->payload[position]);
-
-        --sei->payload_count;
-        memmove(sei->payload + position,
-                sei->payload + position + 1,
-                (sei->payload_count - position) * sizeof(*sei->payload));
-    }
-}