[FFmpeg-devel] [PATCH 3/4] avcodec/adts_header: Add ff_adts_header_parse_buf()

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri May 10 19:56:26 EEST 2024


Most users of ff_adts_header_parse() don't already have
an opened GetBitContext for the header, so add a convenience
function for them.
Also use a forward declaration of GetBitContext in adts_header.h
as this avoids (implicit) inclusion of get_bits.h in some of
the users that now no longer use a GetBitContext of their own.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/aac_parser.c        | 16 +++++-----------
 libavcodec/adts_header.c       | 10 ++++++++++
 libavcodec/adts_header.h       | 14 ++++++++++++--
 libavcodec/adts_parser.c       | 18 ++++--------------
 libavcodec/bsf/aac_adtstoasc.c |  6 ++----
 libavcodec/ftr_parser.c        |  7 ++-----
 6 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/libavcodec/aac_parser.c b/libavcodec/aac_parser.c
index f295dfccdd..186fcd887a 100644
--- a/libavcodec/aac_parser.c
+++ b/libavcodec/aac_parser.c
@@ -24,24 +24,18 @@
 #include "aac_ac3_parser.h"
 #include "adts_header.h"
 #include "adts_parser.h"
-#include "get_bits.h"
-#include "mpeg4audio.h"
+#include "libavutil/intreadwrite.h"
 
 static int aac_sync(uint64_t state, int *need_next_header, int *new_frame_start)
 {
-    GetBitContext bits;
+    uint8_t tmp[8 + AV_INPUT_BUFFER_PADDING_SIZE];
     AACADTSHeaderInfo hdr;
     int size;
-    union {
-        uint64_t u64;
-        uint8_t  u8[8 + AV_INPUT_BUFFER_PADDING_SIZE];
-    } tmp;
 
-    tmp.u64 = av_be2ne64(state);
-    init_get_bits(&bits, tmp.u8 + 8 - AV_AAC_ADTS_HEADER_SIZE,
-                  AV_AAC_ADTS_HEADER_SIZE * 8);
+    AV_WB64(tmp, state);
 
-    if ((size = ff_adts_header_parse(&bits, &hdr)) < 0)
+    size = ff_adts_header_parse_buf(tmp + 8 - AV_AAC_ADTS_HEADER_SIZE, &hdr);
+    if (size < 0)
         return 0;
     *need_next_header = 0;
     *new_frame_start  = 1;
diff --git a/libavcodec/adts_header.c b/libavcodec/adts_header.c
index 8663d00f4c..0d6be1aa0e 100644
--- a/libavcodec/adts_header.c
+++ b/libavcodec/adts_header.c
@@ -25,6 +25,7 @@
 #include "adts_parser.h"
 #include "get_bits.h"
 #include "mpeg4audio.h"
+#include "libavutil/avassert.h"
 
 int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
 {
@@ -71,3 +72,12 @@ int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
 
     return size;
 }
+
+int ff_adts_header_parse_buf(const uint8_t buf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE],
+                             AACADTSHeaderInfo *hdr)
+{
+    GetBitContext gb;
+    av_unused int ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE);
+    av_assert1(ret >= 0);
+    return ff_adts_header_parse(&gb, hdr);
+}
diff --git a/libavcodec/adts_header.h b/libavcodec/adts_header.h
index d89d487025..4f62edcbac 100644
--- a/libavcodec/adts_header.h
+++ b/libavcodec/adts_header.h
@@ -23,7 +23,8 @@
 #ifndef AVCODEC_ADTS_HEADER_H
 #define AVCODEC_ADTS_HEADER_H
 
-#include "get_bits.h"
+#include "adts_parser.h"
+#include "defs.h"
 
 typedef enum {
     AAC_PARSE_ERROR_SYNC        = -0x1030c0a,
@@ -43,6 +44,8 @@ typedef struct AACADTSHeaderInfo {
     uint32_t frame_length;
 } AACADTSHeaderInfo;
 
+struct GetBitContext;
+
 /**
  * Parse the ADTS frame header to the end of the variable header, which is
  * the first 54 bits.
@@ -51,7 +54,14 @@ typedef struct AACADTSHeaderInfo {
  * @return Returns the size in bytes of the header parsed on success
  * and AAC_PARSE_ERROR_* values otherwise.
  */
-int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr);
+int ff_adts_header_parse(struct GetBitContext *gbc, AACADTSHeaderInfo *hdr);
+
+/**
+ * Wrapper around ff_adts_header_parse() for users that already have
+ * a proper GetBitContext.
+ */
+int ff_adts_header_parse_buf(const uint8_t buf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE],
+                             AACADTSHeaderInfo *hdr);
 
 /**
  * Parse the ADTS frame header contained in the buffer, which is
diff --git a/libavcodec/adts_parser.c b/libavcodec/adts_parser.c
index 81e2669149..66b988d6f6 100644
--- a/libavcodec/adts_parser.c
+++ b/libavcodec/adts_parser.c
@@ -20,7 +20,9 @@
 
 #include <stddef.h>
 #include <stdint.h>
+#include <string.h>
 
+#include "libavutil/error.h"
 #include "libavutil/mem.h"
 #include "adts_header.h"
 #include "adts_parser.h"
@@ -29,16 +31,12 @@ int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, uint8_t *frames)
 {
 #if CONFIG_ADTS_HEADER
     uint8_t tmpbuf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
-    GetBitContext gb;
     AACADTSHeaderInfo hdr;
     int err;
     if (!buf)
         return AVERROR(EINVAL);
     memcpy(tmpbuf, buf, AV_AAC_ADTS_HEADER_SIZE);
-    err = init_get_bits8(&gb, tmpbuf, AV_AAC_ADTS_HEADER_SIZE);
-    if (err < 0)
-        return err;
-    err = ff_adts_header_parse(&gb, &hdr);
+    err = ff_adts_header_parse_buf(tmpbuf, &hdr);
     if (err < 0)
         return err;
     *samples = hdr.samples;
@@ -54,7 +52,6 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_
 #if CONFIG_ADTS_HEADER
     int ret = 0;
     int allocated = 0;
-    GetBitContext gb;
 
     if (!phdr || !buf || size < AV_AAC_ADTS_HEADER_SIZE)
         return AVERROR_INVALIDDATA;
@@ -66,14 +63,7 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_
     if (!*phdr)
         return AVERROR(ENOMEM);
 
-    ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE);
-    if (ret < 0) {
-        if (allocated)
-            av_freep(phdr);
-        return ret;
-    }
-
-    ret = ff_adts_header_parse(&gb, *phdr);
+    ret = ff_adts_header_parse_buf(buf, *phdr);
     if (ret < 0) {
         if (allocated)
             av_freep(phdr);
diff --git a/libavcodec/bsf/aac_adtstoasc.c b/libavcodec/bsf/aac_adtstoasc.c
index dd5e8b2a31..b821414f2a 100644
--- a/libavcodec/bsf/aac_adtstoasc.c
+++ b/libavcodec/bsf/aac_adtstoasc.c
@@ -40,7 +40,6 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
 {
     AACBSFContext *ctx = bsfc->priv_data;
 
-    GetBitContext gb;
     PutBitContext pb;
     AACADTSHeaderInfo hdr;
     int ret;
@@ -55,9 +54,7 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
     if (pkt->size < AV_AAC_ADTS_HEADER_SIZE)
         goto packet_too_small;
 
-    init_get_bits(&gb, pkt->data, AV_AAC_ADTS_HEADER_SIZE * 8);
-
-    if (ff_adts_header_parse(&gb, &hdr) < 0) {
+    if (ff_adts_header_parse_buf(pkt->data, &hdr) < 0) {
         av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
         ret = AVERROR_INVALIDDATA;
         goto fail;
@@ -81,6 +78,7 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
         uint8_t       *extradata;
 
         if (!hdr.chan_config) {
+            GetBitContext gb;
             init_get_bits(&gb, pkt->data, pkt->size * 8);
             if (get_bits(&gb, 3) != 5) {
                 avpriv_report_missing_feature(bsfc,
diff --git a/libavcodec/ftr_parser.c b/libavcodec/ftr_parser.c
index 05e6cfed98..656fd289f6 100644
--- a/libavcodec/ftr_parser.c
+++ b/libavcodec/ftr_parser.c
@@ -25,7 +25,6 @@
  */
 
 #include "parser.h"
-#include "get_bits.h"
 #include "adts_header.h"
 #include "adts_parser.h"
 #include "mpeg4audio.h"
@@ -45,7 +44,6 @@ static int ftr_parse(AVCodecParserContext *s, AVCodecContext *avctx,
     FTRParseContext *ftr = s->priv_data;
     uint64_t state = ftr->pc.state64;
     int next = END_NOT_FOUND;
-    GetBitContext bits;
     AACADTSHeaderInfo hdr;
     int size;
 
@@ -71,10 +69,9 @@ static int ftr_parse(AVCodecParserContext *s, AVCodecContext *avctx,
 
             state = (state << 8) | buf[i];
             AV_WB64(tmp, state);
-            init_get_bits(&bits, tmp + 8 - AV_AAC_ADTS_HEADER_SIZE,
-                          AV_AAC_ADTS_HEADER_SIZE * 8);
+            size = ff_adts_header_parse_buf(tmp + 8 - AV_AAC_ADTS_HEADER_SIZE, &hdr);
 
-            if ((size = ff_adts_header_parse(&bits, &hdr)) > 0) {
+            if (size > 0) {
                 ftr->skip = size - 6;
                 ftr->frame_index += ff_mpeg4audio_channels[hdr.chan_config];
                 if (ftr->frame_index >= avctx->ch_layout.nb_channels) {
-- 
2.40.1



More information about the ffmpeg-devel mailing list