[FFmpeg-devel] [PATCH] avcodec/cbs_h2645: Replace parameter sets generically
Andreas Rheinhardt
andreas.rheinhardt at outlook.com
Mon Jul 17 00:18:36 EEST 2023
This avoids having a function for each type of parameter set
for each of the modules.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
libavcodec/cbs_h2645.c | 134 ++++++++++++++++++++++-------------------
1 file changed, 72 insertions(+), 62 deletions(-)
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 34c5d1d372..5bbd74b977 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -736,55 +736,65 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
return 0;
}
-#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
-static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
- CodedBitstreamUnit *unit) \
-{ \
- CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
- H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
- unsigned int id = ps_var->id_element; \
- int err = ff_cbs_make_unit_refcounted(ctx, unit); \
- if (err < 0) \
- return err; \
- if (priv->ps_var[id] == priv->active_ ## ps_var) \
- priv->active_ ## ps_var = NULL ; \
- av_buffer_unref(&priv->ps_var ## _ref[id]); \
- av_assert0(unit->content_ref); \
- priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
- if (!priv->ps_var ## _ref[id]) \
- return AVERROR(ENOMEM); \
- priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
- return 0; \
+typedef struct PSOffsets {
+ unsigned id_element_offset; //< relative to the raw parameter set struct; must refer to uint8_t
+ unsigned ps_array_offset; //< relative to CodedBitstreamH26*Context
+ unsigned ps_ref_array_offset; //< relative to CodedBitstreamH26*Context
+ unsigned active_ps_offset; //< Zero means that active PS is unused
+} PSOffsets;
+
+#define CBS_H26456_OFFSET_TABLE(MODULE, module, PS_NAME, ps_var, id_element, _active_ps_offset) \
+static const PSOffsets module ## _ ## ps_var ## _offsets = { \
+ .id_element_offset = offsetof(MODULE ## Raw ## PS_NAME, id_element), \
+ .ps_array_offset = offsetof(CodedBitstream ## MODULE ## Context, ps_var), \
+ .ps_ref_array_offset = offsetof(CodedBitstream ## MODULE ## Context, ps_var ## _ref), \
+ .active_ps_offset = _active_ps_offset, \
}
-cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
-cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
-cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
-cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
-cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
-
-#define cbs_h266_replace_ps(h26n, ps_name, ps_var, id_element) \
-static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
- CodedBitstreamUnit *unit) \
-{ \
- CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
- H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
- unsigned int id = ps_var->id_element; \
- int err = ff_cbs_make_unit_refcounted(ctx, unit); \
- if (err < 0) \
- return err; \
- av_buffer_unref(&priv->ps_var ## _ref[id]); \
- av_assert0(unit->content_ref); \
- priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
- if (!priv->ps_var ## _ref[id]) \
- return AVERROR(ENOMEM); \
- priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
- return 0; \
-}
+#define CBS_H2645_OFFSET_TABLE(h26n, PS_NAME, ps_var, id_element) \
+ CBS_H26456_OFFSET_TABLE(H26 ## h26n, h26 ## h26n, PS_NAME, ps_var, id_element, \
+ offsetof(CodedBitstreamH26 ## h26n ## Context, active_ ## ps_var))
+
+CBS_H2645_OFFSET_TABLE(4, SPS, sps, seq_parameter_set_id);
+CBS_H2645_OFFSET_TABLE(4, PPS, pps, pic_parameter_set_id);
+CBS_H2645_OFFSET_TABLE(5, VPS, vps, vps_video_parameter_set_id);
+CBS_H2645_OFFSET_TABLE(5, SPS, sps, sps_seq_parameter_set_id);
+CBS_H2645_OFFSET_TABLE(5, PPS, pps, pps_pic_parameter_set_id);
+
+#define CBS_H266_OFFSET_TABLE(PS_NAME, ps_var, id_element) \
+ CBS_H26456_OFFSET_TABLE(H266, h266, PS_NAME, ps_var, id_element, 0)
+
+CBS_H266_OFFSET_TABLE(VPS, vps, vps_video_parameter_set_id);
+CBS_H266_OFFSET_TABLE(SPS, sps, sps_seq_parameter_set_id);
+CBS_H266_OFFSET_TABLE(PPS, pps, pps_pic_parameter_set_id);
+
+static int cbs_h26456_replace_ps(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit,
+ const PSOffsets *offsets)
+{
+ int err = ff_cbs_make_unit_refcounted(ctx, unit);
+ unsigned char *priv = ctx->priv_data;
+ struct ps **ps_array = (struct ps **)(priv + offsets->ps_array_offset);
+ AVBufferRef **ps_ref_array = (AVBufferRef**)(priv + offsets->ps_ref_array_offset);
+ unsigned id;
+
+ if (err < 0)
+ return err;
-cbs_h266_replace_ps(6, VPS, vps, vps_video_parameter_set_id)
-cbs_h266_replace_ps(6, SPS, sps, sps_seq_parameter_set_id)
-cbs_h266_replace_ps(6, PPS, pps, pps_pic_parameter_set_id)
+ id = *((const uint8_t*)unit->content + offsets->id_element_offset);
+ if (offsets->active_ps_offset) {
+ const struct ps **active_ps = (const struct ps**)(priv + offsets->active_ps_offset);
+ if (*active_ps == ps_array[id])
+ memcpy(active_ps, &(struct ps *){ NULL }, sizeof(*active_ps));
+ }
+ av_assert0(unit->content_ref);
+ err = av_buffer_replace(&ps_ref_array[id], unit->content_ref);
+ if (err < 0)
+ return err;
+
+ ps_array[id] = (struct ps *)ps_ref_array[id]->data;
+
+ return 0;
+}
static int cbs_h266_replace_ph(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit,
@@ -827,7 +837,7 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h264_replace_sps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h264_sps_offsets);
if (err < 0)
return err;
}
@@ -849,7 +859,7 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h264_replace_pps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h264_pps_offsets);
if (err < 0)
return err;
}
@@ -946,7 +956,7 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h265_replace_vps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h265_vps_offsets);
if (err < 0)
return err;
}
@@ -959,7 +969,7 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h265_replace_sps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h265_sps_offsets);
if (err < 0)
return err;
}
@@ -973,7 +983,7 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h265_replace_pps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h265_pps_offsets);
if (err < 0)
return err;
}
@@ -1083,7 +1093,7 @@ static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h266_replace_vps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h266_vps_offsets);
if (err < 0)
return err;
}
@@ -1096,7 +1106,7 @@ static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h266_replace_sps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h266_sps_offsets);
if (err < 0)
return err;
}
@@ -1110,7 +1120,7 @@ static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h266_replace_pps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h266_pps_offsets);
if (err < 0)
return err;
}
@@ -1273,7 +1283,7 @@ static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h264_replace_sps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h264_sps_offsets);
if (err < 0)
return err;
}
@@ -1297,7 +1307,7 @@ static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h264_replace_pps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h264_pps_offsets);
if (err < 0)
return err;
}
@@ -1390,7 +1400,7 @@ static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h265_replace_vps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h265_vps_offsets);
if (err < 0)
return err;
}
@@ -1404,7 +1414,7 @@ static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h265_replace_sps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h265_sps_offsets);
if (err < 0)
return err;
}
@@ -1418,7 +1428,7 @@ static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h265_replace_pps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h265_pps_offsets);
if (err < 0)
return err;
}
@@ -1645,7 +1655,7 @@ static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h266_replace_vps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h266_vps_offsets);
if (err < 0)
return err;
}
@@ -1658,7 +1668,7 @@ static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h266_replace_sps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h266_sps_offsets);
if (err < 0)
return err;
}
@@ -1672,7 +1682,7 @@ static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx,
if (err < 0)
return err;
- err = cbs_h266_replace_pps(ctx, unit);
+ err = cbs_h26456_replace_ps(ctx, unit, &h266_pps_offsets);
if (err < 0)
return err;
}
--
2.34.1
More information about the ffmpeg-devel
mailing list