[FFmpeg-devel,5/7] cbs_h265: Use table-based alloc/free

Submitted by Mark Thompson on Jan. 23, 2019, 11:01 p.m.

Details

Message ID 20190123230114.1086-6-sw@jkqxz.net
State New
Headers show

Commit Message

Mark Thompson Jan. 23, 2019, 11:01 p.m.
---
 libavcodec/cbs_h2645.c | 186 ++++++++++++++++++-----------------------
 1 file changed, 83 insertions(+), 103 deletions(-)

Comments

Andreas Rheinhardt Jan. 24, 2019, 4:19 p.m.
Mark Thompson:
> +    CBS_UNIT_TYPE_POD(H264_NAL_AUD, H265RawAUD),
Typo.

Patch hide | download patch | download mbox

diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index caf8a1062f..4fda12adb7 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -414,70 +414,6 @@  static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
 #undef allocate
 
 
-static void cbs_h265_free_vps(void *unit, uint8_t *content)
-{
-    H265RawVPS *vps = (H265RawVPS*)content;
-    av_buffer_unref(&vps->extension_data.data_ref);
-    av_freep(&content);
-}
-
-static void cbs_h265_free_sps(void *unit, uint8_t *content)
-{
-    H265RawSPS *sps = (H265RawSPS*)content;
-    av_buffer_unref(&sps->extension_data.data_ref);
-    av_freep(&content);
-}
-
-static void cbs_h265_free_pps(void *unit, uint8_t *content)
-{
-    H265RawPPS *pps = (H265RawPPS*)content;
-    av_buffer_unref(&pps->extension_data.data_ref);
-    av_freep(&content);
-}
-
-static void cbs_h265_free_slice(void *unit, uint8_t *content)
-{
-    H265RawSlice *slice = (H265RawSlice*)content;
-    av_buffer_unref(&slice->data_ref);
-    av_freep(&content);
-}
-
-static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
-{
-    switch (payload->payload_type) {
-    case HEVC_SEI_TYPE_BUFFERING_PERIOD:
-    case HEVC_SEI_TYPE_PICTURE_TIMING:
-    case HEVC_SEI_TYPE_PAN_SCAN_RECT:
-    case HEVC_SEI_TYPE_RECOVERY_POINT:
-    case HEVC_SEI_TYPE_DISPLAY_ORIENTATION:
-    case HEVC_SEI_TYPE_ACTIVE_PARAMETER_SETS:
-    case HEVC_SEI_TYPE_DECODED_PICTURE_HASH:
-    case HEVC_SEI_TYPE_TIME_CODE:
-    case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
-    case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
-    case HEVC_SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
-        break;
-    case HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
-        av_buffer_unref(&payload->payload.user_data_registered.data_ref);
-        break;
-    case HEVC_SEI_TYPE_USER_DATA_UNREGISTERED:
-        av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
-        break;
-    default:
-        av_buffer_unref(&payload->payload.other.data_ref);
-        break;
-    }
-}
-
-static void cbs_h265_free_sei(void *unit, uint8_t *content)
-{
-    H265RawSEI *sei = (H265RawSEI*)content;
-    int i;
-    for (i = 0; i < sei->payload_count; i++)
-        cbs_h265_free_sei_payload(&sei->payload[i]);
-    av_freep(&content);
-}
-
 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
                                        CodedBitstreamFragment *frag,
                                        const H2645Packet *packet)
@@ -831,16 +767,14 @@  static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
     if (err < 0)
         return err;
 
+    err = ff_cbs_alloc_unit_content2(ctx, unit);
+    if (err < 0)
+        return err;
+
     switch (unit->type) {
     case HEVC_NAL_VPS:
         {
-            H265RawVPS *vps;
-
-            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
-                                            &cbs_h265_free_vps);
-            if (err < 0)
-                return err;
-            vps = unit->content;
+            H265RawVPS *vps = unit->content;
 
             err = cbs_h265_read_vps(ctx, &gbc, vps);
             if (err < 0)
@@ -853,13 +787,7 @@  static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
         break;
     case HEVC_NAL_SPS:
         {
-            H265RawSPS *sps;
-
-            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
-                                            &cbs_h265_free_sps);
-            if (err < 0)
-                return err;
-            sps = unit->content;
+            H265RawSPS *sps = unit->content;
 
             err = cbs_h265_read_sps(ctx, &gbc, sps);
             if (err < 0)
@@ -873,13 +801,7 @@  static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
 
     case HEVC_NAL_PPS:
         {
-            H265RawPPS *pps;
-
-            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
-                                            &cbs_h265_free_pps);
-            if (err < 0)
-                return err;
-            pps = unit->content;
+            H265RawPPS *pps = unit->content;
 
             err = cbs_h265_read_pps(ctx, &gbc, pps);
             if (err < 0)
@@ -908,15 +830,9 @@  static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
     case HEVC_NAL_IDR_N_LP:
     case HEVC_NAL_CRA_NUT:
         {
-            H265RawSlice *slice;
+            H265RawSlice *slice = unit->content;
             int pos, len;
 
-            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
-                                            &cbs_h265_free_slice);
-            if (err < 0)
-                return err;
-            slice = unit->content;
-
             err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
             if (err < 0)
                 return err;
@@ -942,11 +858,6 @@  static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
 
     case HEVC_NAL_AUD:
         {
-            err = ff_cbs_alloc_unit_content(ctx, unit,
-                                            sizeof(H265RawAUD), NULL);
-            if (err < 0)
-                return err;
-
             err = cbs_h265_read_aud(ctx, &gbc, unit->content);
             if (err < 0)
                 return err;
@@ -956,12 +867,6 @@  static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
     case HEVC_NAL_SEI_PREFIX:
     case HEVC_NAL_SEI_SUFFIX:
         {
-            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
-                                            &cbs_h265_free_sei);
-
-            if (err < 0)
-                return err;
-
             err = cbs_h265_read_sei(ctx, &gbc, unit->content,
                                     unit->type == HEVC_NAL_SEI_PREFIX);
 
@@ -1497,6 +1402,79 @@  static const CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[] = {
     CBS_UNIT_TYPE_END_OF_LIST
 };
 
+static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
+{
+    switch (payload->payload_type) {
+    case HEVC_SEI_TYPE_BUFFERING_PERIOD:
+    case HEVC_SEI_TYPE_PICTURE_TIMING:
+    case HEVC_SEI_TYPE_PAN_SCAN_RECT:
+    case HEVC_SEI_TYPE_RECOVERY_POINT:
+    case HEVC_SEI_TYPE_DISPLAY_ORIENTATION:
+    case HEVC_SEI_TYPE_ACTIVE_PARAMETER_SETS:
+    case HEVC_SEI_TYPE_DECODED_PICTURE_HASH:
+    case HEVC_SEI_TYPE_TIME_CODE:
+    case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
+    case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
+    case HEVC_SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
+        break;
+    case HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
+        av_buffer_unref(&payload->payload.user_data_registered.data_ref);
+        break;
+    case HEVC_SEI_TYPE_USER_DATA_UNREGISTERED:
+        av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
+        break;
+    default:
+        av_buffer_unref(&payload->payload.other.data_ref);
+        break;
+    }
+}
+
+static void cbs_h265_free_sei(void *unit, uint8_t *content)
+{
+    H265RawSEI *sei = (H265RawSEI*)content;
+    int i;
+    for (i = 0; i < sei->payload_count; i++)
+        cbs_h265_free_sei_payload(&sei->payload[i]);
+    av_freep(&content);
+}
+
+#define CBS_UNIT_TYPE_H265_SLICE(nal) \
+    CBS_UNIT_TYPE_INTERNAL_REFS(HEVC_NAL_ ## nal, H265RawSlice, \
+                                1, { offsetof(H265RawSlice, data_ref) })
+
+static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = {
+    CBS_UNIT_TYPE_INTERNAL_REFS(HEVC_NAL_VPS, H265RawVPS,
+                                1, { offsetof(H265RawVPS, extension_data.data_ref) }),
+    CBS_UNIT_TYPE_INTERNAL_REFS(HEVC_NAL_SPS, H265RawSPS,
+                                1, { offsetof(H265RawSPS, extension_data.data_ref) }),
+    CBS_UNIT_TYPE_INTERNAL_REFS(HEVC_NAL_PPS, H265RawPPS,
+                                1, { offsetof(H265RawPPS, extension_data.data_ref) }),
+
+    CBS_UNIT_TYPE_H265_SLICE(TRAIL_N),
+    CBS_UNIT_TYPE_H265_SLICE(TRAIL_R),
+    CBS_UNIT_TYPE_H265_SLICE(TSA_N),
+    CBS_UNIT_TYPE_H265_SLICE(TSA_R),
+    CBS_UNIT_TYPE_H265_SLICE(STSA_N),
+    CBS_UNIT_TYPE_H265_SLICE(STSA_R),
+    CBS_UNIT_TYPE_H265_SLICE(RADL_N),
+    CBS_UNIT_TYPE_H265_SLICE(RADL_R),
+    CBS_UNIT_TYPE_H265_SLICE(RASL_N),
+    CBS_UNIT_TYPE_H265_SLICE(RASL_R),
+    CBS_UNIT_TYPE_H265_SLICE(BLA_W_LP),
+    CBS_UNIT_TYPE_H265_SLICE(BLA_W_RADL),
+    CBS_UNIT_TYPE_H265_SLICE(BLA_N_LP),
+    CBS_UNIT_TYPE_H265_SLICE(IDR_W_RADL),
+    CBS_UNIT_TYPE_H265_SLICE(IDR_N_LP),
+    CBS_UNIT_TYPE_H265_SLICE(CRA_NUT),
+
+    CBS_UNIT_TYPE_POD(H264_NAL_AUD, H265RawAUD),
+
+    CBS_UNIT_TYPE_COMPLEX(HEVC_NAL_SEI_PREFIX, H265RawSEI, &cbs_h265_free_sei),
+    CBS_UNIT_TYPE_COMPLEX(HEVC_NAL_SEI_SUFFIX, H265RawSEI, &cbs_h265_free_sei),
+
+    CBS_UNIT_TYPE_END_OF_LIST
+};
+
 const CodedBitstreamType ff_cbs_type_h264 = {
     .codec_id          = AV_CODEC_ID_H264,
 
@@ -1517,6 +1495,8 @@  const CodedBitstreamType ff_cbs_type_h265 = {
 
     .priv_data_size    = sizeof(CodedBitstreamH265Context),
 
+    .unit_types        = cbs_h265_unit_types,
+
     .split_fragment    = &cbs_h2645_split_fragment,
     .read_unit         = &cbs_h265_read_nal_unit,
     .write_unit        = &cbs_h2645_write_nal_unit,