[FFmpeg-devel] [PATCH] avcodec/h2645_parse: replace three bool arguments in ff_h2645_packet_split with a single flags one

James Almer jamrial at gmail.com
Mon Aug 19 19:58:11 EEST 2024


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/bsf/extract_extradata.c |  2 +-
 libavcodec/cbs_h2645.c             | 17 +++++++++++------
 libavcodec/h2645_parse.c           | 15 ++++++++-------
 libavcodec/h2645_parse.h           | 15 ++++++++++-----
 libavcodec/h264_parse.c            |  3 ++-
 libavcodec/h264dec.c               |  4 ++--
 libavcodec/hevc/hevcdec.c          |  5 +++--
 libavcodec/hevc/parse.c            |  5 +++--
 libavcodec/hevc/parser.c           |  5 +++--
 9 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/libavcodec/bsf/extract_extradata.c b/libavcodec/bsf/extract_extradata.c
index b037990562..43f4d62855 100644
--- a/libavcodec/bsf/extract_extradata.c
+++ b/libavcodec/bsf/extract_extradata.c
@@ -193,7 +193,7 @@ static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
     }
 
     ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size,
-                                ctx, 0, 0, ctx->par_in->codec_id, 1, 0);
+                                ctx, 0, ctx->par_in->codec_id, H2645_FLAG_SMALL_PADDING);
     if (ret < 0)
         return ret;
 
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index d73d77a985..4abd9e0c2e 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -573,7 +573,8 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
 
         err = ff_h2645_packet_split(&priv->read_packet,
                                     frag->data + start, end - start,
-                                    ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
+                                    ctx->log_ctx, 2, AV_CODEC_ID_H264,
+                                    H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
         if (err < 0) {
             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
             return err;
@@ -597,7 +598,8 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
 
         err = ff_h2645_packet_split(&priv->read_packet,
                                     frag->data + start, end - start,
-                                    ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
+                                    ctx->log_ctx, 2, AV_CODEC_ID_H264,
+                                    H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
         if (err < 0) {
             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
             return err;
@@ -651,7 +653,8 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
 
             err = ff_h2645_packet_split(&priv->read_packet,
                                         frag->data + start, end - start,
-                                        ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, 1);
+                                        ctx->log_ctx, 2, AV_CODEC_ID_HEVC,
+                                        H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
             if (err < 0) {
                 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
                        "HVCC array %d (%d NAL units of type %d).\n",
@@ -721,7 +724,8 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
 
             err = ff_h2645_packet_split(&priv->read_packet,
                                         frag->data + start, end - start,
-                                        ctx->log_ctx, 1, 2, AV_CODEC_ID_VVC, 1, 1);
+                                        ctx->log_ctx, 2, AV_CODEC_ID_VVC,
+                                        H2645_FLAG_IS_NALFF | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF);
             if (err < 0) {
                 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
                        "VVCC array %d (%d NAL units of type %d).\n",
@@ -733,13 +737,14 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
                 return err;
         }
     } else {
+        int flags = (H2645_FLAG_IS_NALFF * !!priv->mp4) | H2645_FLAG_SMALL_PADDING | H2645_FLAG_USE_REF;
         // Annex B, or later MP4 with already-known parameters.
 
         err = ff_h2645_packet_split(&priv->read_packet,
                                     frag->data, frag->data_size,
                                     ctx->log_ctx,
-                                    priv->mp4, priv->nal_length_size,
-                                    codec_id, 1, 1);
+                                    priv->nal_length_size,
+                                    codec_id, flags);
         if (err < 0)
             return err;
 
diff --git a/libavcodec/h2645_parse.c b/libavcodec/h2645_parse.c
index 2341f0e0a7..4cb90ac0d0 100644
--- a/libavcodec/h2645_parse.c
+++ b/libavcodec/h2645_parse.c
@@ -463,16 +463,16 @@ fail:
 }
 
 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
-                          void *logctx, int is_nalff, int nal_length_size,
-                          enum AVCodecID codec_id, int small_padding, int use_ref)
+                          void *logctx, int nal_length_size,
+                          enum AVCodecID codec_id, int flags)
 {
     GetByteContext bc;
     int consumed, ret = 0;
-    int next_avc = is_nalff ? 0 : length;
-    int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
+    int next_avc = (flags & H2645_FLAG_IS_NALFF) ? 0 : length;
+    int64_t padding = (flags & H2645_FLAG_SMALL_PADDING) ? 0 : MAX_MBPAIR_SIZE;
 
     bytestream2_init(&bc, buf, length);
-    alloc_rbsp_buffer(&pkt->rbsp, length + padding, use_ref);
+    alloc_rbsp_buffer(&pkt->rbsp, length + padding, !!(flags & H2645_FLAG_USE_REF));
 
     if (!pkt->rbsp.rbsp_buffer)
         return AVERROR(ENOMEM);
@@ -549,11 +549,12 @@ int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
         }
         nal = &pkt->nals[pkt->nb_nals];
 
-        consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
+        consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal,
+                                         !!(flags & H2645_FLAG_SMALL_PADDING));
         if (consumed < 0)
             return consumed;
 
-        if (is_nalff && (extract_length != consumed) && extract_length)
+        if ((flags & H2645_FLAG_IS_NALFF) && extract_length && (extract_length != consumed))
             av_log(logctx, AV_LOG_DEBUG,
                    "NALFF: Consumed only %d bytes instead of %d\n",
                    consumed, extract_length);
diff --git a/libavcodec/h2645_parse.h b/libavcodec/h2645_parse.h
index 128dea09ef..849e772115 100644
--- a/libavcodec/h2645_parse.h
+++ b/libavcodec/h2645_parse.h
@@ -93,6 +93,10 @@ typedef struct H2645Packet {
 int ff_h2645_extract_rbsp(const uint8_t *src, int length, H2645RBSP *rbsp,
                           H2645NAL *nal, int small_padding);
 
+#define H2645_FLAG_IS_NALFF      (1 << 0)
+#define H2645_FLAG_SMALL_PADDING (1 << 1)
+#define H2645_FLAG_USE_REF       (1 << 2)
+
 /**
  * Split an input packet into NAL units.
  *
@@ -103,13 +107,14 @@ int ff_h2645_extract_rbsp(const uint8_t *src, int length, H2645RBSP *rbsp,
  * packet's H2645RBSP.
  *
  * If the packet's rbsp_buffer_ref is not NULL, the underlying AVBuffer must
- * own rbsp_buffer. If not and rbsp_buffer is not NULL, use_ref must be 0.
- * If use_ref is set, rbsp_buffer will be reference-counted and owned by
- * the underlying AVBuffer of rbsp_buffer_ref.
+ * own rbsp_buffer. If not and rbsp_buffer is not NULL, H2645_FLAG_USE_REF
+ * must not be set in flags.
+ * If H2645_FLAG_USE_REF is set in flags, rbsp_buffer will be reference-counted and
+ * owned by the underlying AVBuffer of rbsp_buffer_ref.
  */
 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
-                          void *logctx, int is_nalff, int nal_length_size,
-                          enum AVCodecID codec_id, int small_padding, int use_ref);
+                          void *logctx, int nal_length_size,
+                          enum AVCodecID codec_id, int flags);
 
 /**
  * Free all the allocated memory in the packet.
diff --git a/libavcodec/h264_parse.c b/libavcodec/h264_parse.c
index c13bc860ef..c6f9b3cea7 100644
--- a/libavcodec/h264_parse.c
+++ b/libavcodec/h264_parse.c
@@ -368,9 +368,10 @@ static int decode_extradata_ps(const uint8_t *data, int size, H264ParamSets *ps,
                                int is_avc, void *logctx)
 {
     H2645Packet pkt = { 0 };
+    int flags = (H2645_FLAG_IS_NALFF * !!is_avc) | H2645_FLAG_SMALL_PADDING;
     int i, ret = 0;
 
-    ret = ff_h2645_packet_split(&pkt, data, size, logctx, is_avc, 2, AV_CODEC_ID_H264, 1, 0);
+    ret = ff_h2645_packet_split(&pkt, data, size, logctx, 2, AV_CODEC_ID_H264, flags);
     if (ret < 0) {
         ret = 0;
         goto fail;
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index c77d8f42db..0154fe17b6 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -612,8 +612,8 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
             h->is_avc = 1;
     }
 
-    ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->is_avc, h->nal_length_size,
-                                avctx->codec_id, 0, 0);
+    ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->nal_length_size,
+                                avctx->codec_id, !!h->is_avc * H2645_FLAG_IS_NALFF);
     if (ret < 0) {
         av_log(avctx, AV_LOG_ERROR,
                "Error splitting the input into NAL units.\n");
diff --git a/libavcodec/hevc/hevcdec.c b/libavcodec/hevc/hevcdec.c
index 193d624fc9..45e20fc86a 100644
--- a/libavcodec/hevc/hevcdec.c
+++ b/libavcodec/hevc/hevcdec.c
@@ -3289,6 +3289,7 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
 {
     int i, ret = 0;
     int eos_at_start = 1;
+    int flags = (H2645_FLAG_IS_NALFF * !!s->is_nalff) | H2645_FLAG_SMALL_PADDING;
 
     s->cur_frame = s->collocated_ref = NULL;
     s->last_eos = s->eos;
@@ -3297,8 +3298,8 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
 
     /* split the input packet into NAL units, so we know the upper bound on the
      * number of slices in the frame */
-    ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
-                                s->nal_length_size, s->avctx->codec_id, 1, 0);
+    ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx,
+                                s->nal_length_size, s->avctx->codec_id, flags);
     if (ret < 0) {
         av_log(s->avctx, AV_LOG_ERROR,
                "Error splitting the input into NAL units.\n");
diff --git a/libavcodec/hevc/parse.c b/libavcodec/hevc/parse.c
index 53b040d964..ec8d1aeacf 100644
--- a/libavcodec/hevc/parse.c
+++ b/libavcodec/hevc/parse.c
@@ -27,10 +27,11 @@ static int hevc_decode_nal_units(const uint8_t *buf, int buf_size, HEVCParamSets
 {
     int i;
     int ret = 0;
+    int flags = (H2645_FLAG_IS_NALFF * !!is_nalff) | H2645_FLAG_SMALL_PADDING;
     H2645Packet pkt = { 0 };
 
-    ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx, is_nalff,
-                                nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
+    ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx,
+                                nal_length_size, AV_CODEC_ID_HEVC, flags);
     if (ret < 0) {
         goto done;
     }
diff --git a/libavcodec/hevc/parser.c b/libavcodec/hevc/parser.c
index f4e6e3c36d..8db56e259e 100644
--- a/libavcodec/hevc/parser.c
+++ b/libavcodec/hevc/parser.c
@@ -182,6 +182,7 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
     HEVCParserContext *ctx = s->priv_data;
     HEVCParamSets *ps = &ctx->ps;
     HEVCSEI *sei = &ctx->sei;
+    int flags = (H2645_FLAG_IS_NALFF * !!ctx->is_avc) | H2645_FLAG_SMALL_PADDING;
     int ret, i;
 
     /* set some sane default values */
@@ -191,8 +192,8 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
 
     ff_hevc_reset_sei(sei);
 
-    ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, ctx->is_avc,
-                                ctx->nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
+    ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx,
+                                ctx->nal_length_size, AV_CODEC_ID_HEVC, flags);
     if (ret < 0)
         return ret;
 
-- 
2.46.0



More information about the ffmpeg-devel mailing list