diff mbox series

[FFmpeg-devel,2/4] avcodec/aac_ac3_parser: Untangle AAC and AC3 parsing error codes

Message ID GV1P250MB0737E625947ADBC97011909E8FE72@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State Accepted
Headers show
Series [FFmpeg-devel,1/4] avcodec/adts_parser: Don't presume buffer to be padded | expand

Commit Message

Andreas Rheinhardt May 10, 2024, 4:56 p.m. UTC
Also remove the (unused) AAC_AC3_PARSE_ERROR_CHANNEL_CFG while at it;
furthermore, fix the documentation of ff_ac3_parse_header()
and (ff|avpriv)_adts_header_parse().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/aac_ac3_parser.h      | 10 ----------
 libavcodec/ac3_parser.c          | 14 +++++++-------
 libavcodec/ac3_parser_internal.h | 13 ++++++++++---
 libavcodec/ac3dec.c              | 18 ++++++++----------
 libavcodec/adts_header.c         |  7 +++----
 libavcodec/adts_header.h         | 16 ++++++++++------
 libavcodec/eac3dec.c             |  6 +++---
 7 files changed, 41 insertions(+), 43 deletions(-)

Comments

Andrew Sayers May 11, 2024, 1:44 p.m. UTC | #1
On Fri, May 10, 2024 at 06:56:25PM +0200, Andreas Rheinhardt wrote:
> Also remove the (unused) AAC_AC3_PARSE_ERROR_CHANNEL_CFG while at it;
> furthermore, fix the documentation of ff_ac3_parse_header()
> and (ff|avpriv)_adts_header_parse().
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavcodec/aac_ac3_parser.h      | 10 ----------
>  libavcodec/ac3_parser.c          | 14 +++++++-------
>  libavcodec/ac3_parser_internal.h | 13 ++++++++++---
>  libavcodec/ac3dec.c              | 18 ++++++++----------
>  libavcodec/adts_header.c         |  7 +++----
>  libavcodec/adts_header.h         | 16 ++++++++++------
>  libavcodec/eac3dec.c             |  6 +++---
>  7 files changed, 41 insertions(+), 43 deletions(-)
> 
> diff --git a/libavcodec/aac_ac3_parser.h b/libavcodec/aac_ac3_parser.h
> index bc16181a19..e3259d1841 100644
> --- a/libavcodec/aac_ac3_parser.h
> +++ b/libavcodec/aac_ac3_parser.h
> @@ -28,16 +28,6 @@
>  #include "avcodec.h"
>  #include "parser.h"
>  
> -typedef enum {
> -    AAC_AC3_PARSE_ERROR_SYNC        = -0x1030c0a,
> -    AAC_AC3_PARSE_ERROR_BSID        = -0x2030c0a,
> -    AAC_AC3_PARSE_ERROR_SAMPLE_RATE = -0x3030c0a,
> -    AAC_AC3_PARSE_ERROR_FRAME_SIZE  = -0x4030c0a,
> -    AAC_AC3_PARSE_ERROR_FRAME_TYPE  = -0x5030c0a,
> -    AAC_AC3_PARSE_ERROR_CRC         = -0x6030c0a,
> -    AAC_AC3_PARSE_ERROR_CHANNEL_CFG = -0x7030c0a,
> -} AACAC3ParseError;
> -
>  typedef struct AACAC3ParseContext {
>      ParseContext pc;
>      int header_size;
> diff --git a/libavcodec/ac3_parser.c b/libavcodec/ac3_parser.c
> index 4e0ba73481..69989690dd 100644
> --- a/libavcodec/ac3_parser.c
> +++ b/libavcodec/ac3_parser.c
> @@ -81,12 +81,12 @@ int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
>  
>      hdr->sync_word = get_bits(gbc, 16);
>      if(hdr->sync_word != 0x0B77)
> -        return AAC_AC3_PARSE_ERROR_SYNC;
> +        return AC3_PARSE_ERROR_SYNC;
>  
>      /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
>      hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
>      if(hdr->bitstream_id > 16)
> -        return AAC_AC3_PARSE_ERROR_BSID;
> +        return AC3_PARSE_ERROR_BSID;
>  
>      hdr->num_blocks = 6;
>      hdr->ac3_bit_rate_code = -1;
> @@ -103,11 +103,11 @@ int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
>          hdr->crc1 = get_bits(gbc, 16);
>          hdr->sr_code = get_bits(gbc, 2);
>          if(hdr->sr_code == 3)
> -            return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
> +            return AC3_PARSE_ERROR_SAMPLE_RATE;
>  
>          frame_size_code = get_bits(gbc, 6);
>          if(frame_size_code > 37)
> -            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
> +            return AC3_PARSE_ERROR_FRAME_SIZE;
>  
>          hdr->ac3_bit_rate_code = (frame_size_code >> 1);
>  
> @@ -138,19 +138,19 @@ int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
>          hdr->crc1 = 0;
>          hdr->frame_type = get_bits(gbc, 2);
>          if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
> -            return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
> +            return AC3_PARSE_ERROR_FRAME_TYPE;
>  
>          hdr->substreamid = get_bits(gbc, 3);
>  
>          hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
>          if(hdr->frame_size < AC3_HEADER_SIZE)
> -            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
> +            return AC3_PARSE_ERROR_FRAME_SIZE;
>  
>          hdr->sr_code = get_bits(gbc, 2);
>          if (hdr->sr_code == 3) {
>              int sr_code2 = get_bits(gbc, 2);
>              if(sr_code2 == 3)
> -                return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
> +                return AC3_PARSE_ERROR_SAMPLE_RATE;
>              hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
>              hdr->sr_shift = 1;
>          } else {
> diff --git a/libavcodec/ac3_parser_internal.h b/libavcodec/ac3_parser_internal.h
> index 2ac0e67ec2..2c4eb546e5 100644
> --- a/libavcodec/ac3_parser_internal.h
> +++ b/libavcodec/ac3_parser_internal.h
> @@ -64,15 +64,22 @@ typedef struct AC3HeaderInfo {
>      /** @} */
>  } AC3HeaderInfo;
>  
> +typedef enum {
> +    AC3_PARSE_ERROR_SYNC        = -0x1030c0a,
> +    AC3_PARSE_ERROR_BSID        = -0x2030c0a,
> +    AC3_PARSE_ERROR_SAMPLE_RATE = -0x3030c0a,
> +    AC3_PARSE_ERROR_FRAME_SIZE  = -0x4030c0a,
> +    AC3_PARSE_ERROR_FRAME_TYPE  = -0x5030c0a,
> +    AC3_PARSE_ERROR_CRC         = -0x6030c0a,
> +} AC3ParseError;
> +
>  /**
>   * Parse AC-3 frame header.
>   * Parse the header up to the lfeon element, which is the first 52 or 54 bits
>   * depending on the audio coding mode.
>   * @param[in]  gbc BitContext containing the first 54 bits of the frame.
>   * @param[out] hdr Pointer to struct where header info is written.
> - * @return Returns 0 on success, -1 if there is a sync word mismatch,
> - * -2 if the bsid (version) element is invalid, -3 if the fscod (sample rate)
> - * element is invalid, or -4 if the frmsizecod (bit rate) element is invalid.
> + * @return Returns 0 on success and AC3_PARSE_ERROR_* values otherwise.

"@return Returns" is redundant - can this (and similar below) just be
"@return 0 on success ..."?
diff mbox series

Patch

diff --git a/libavcodec/aac_ac3_parser.h b/libavcodec/aac_ac3_parser.h
index bc16181a19..e3259d1841 100644
--- a/libavcodec/aac_ac3_parser.h
+++ b/libavcodec/aac_ac3_parser.h
@@ -28,16 +28,6 @@ 
 #include "avcodec.h"
 #include "parser.h"
 
-typedef enum {
-    AAC_AC3_PARSE_ERROR_SYNC        = -0x1030c0a,
-    AAC_AC3_PARSE_ERROR_BSID        = -0x2030c0a,
-    AAC_AC3_PARSE_ERROR_SAMPLE_RATE = -0x3030c0a,
-    AAC_AC3_PARSE_ERROR_FRAME_SIZE  = -0x4030c0a,
-    AAC_AC3_PARSE_ERROR_FRAME_TYPE  = -0x5030c0a,
-    AAC_AC3_PARSE_ERROR_CRC         = -0x6030c0a,
-    AAC_AC3_PARSE_ERROR_CHANNEL_CFG = -0x7030c0a,
-} AACAC3ParseError;
-
 typedef struct AACAC3ParseContext {
     ParseContext pc;
     int header_size;
diff --git a/libavcodec/ac3_parser.c b/libavcodec/ac3_parser.c
index 4e0ba73481..69989690dd 100644
--- a/libavcodec/ac3_parser.c
+++ b/libavcodec/ac3_parser.c
@@ -81,12 +81,12 @@  int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
 
     hdr->sync_word = get_bits(gbc, 16);
     if(hdr->sync_word != 0x0B77)
-        return AAC_AC3_PARSE_ERROR_SYNC;
+        return AC3_PARSE_ERROR_SYNC;
 
     /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
     hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
     if(hdr->bitstream_id > 16)
-        return AAC_AC3_PARSE_ERROR_BSID;
+        return AC3_PARSE_ERROR_BSID;
 
     hdr->num_blocks = 6;
     hdr->ac3_bit_rate_code = -1;
@@ -103,11 +103,11 @@  int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
         hdr->crc1 = get_bits(gbc, 16);
         hdr->sr_code = get_bits(gbc, 2);
         if(hdr->sr_code == 3)
-            return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
+            return AC3_PARSE_ERROR_SAMPLE_RATE;
 
         frame_size_code = get_bits(gbc, 6);
         if(frame_size_code > 37)
-            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
+            return AC3_PARSE_ERROR_FRAME_SIZE;
 
         hdr->ac3_bit_rate_code = (frame_size_code >> 1);
 
@@ -138,19 +138,19 @@  int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
         hdr->crc1 = 0;
         hdr->frame_type = get_bits(gbc, 2);
         if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
-            return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
+            return AC3_PARSE_ERROR_FRAME_TYPE;
 
         hdr->substreamid = get_bits(gbc, 3);
 
         hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
         if(hdr->frame_size < AC3_HEADER_SIZE)
-            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
+            return AC3_PARSE_ERROR_FRAME_SIZE;
 
         hdr->sr_code = get_bits(gbc, 2);
         if (hdr->sr_code == 3) {
             int sr_code2 = get_bits(gbc, 2);
             if(sr_code2 == 3)
-                return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
+                return AC3_PARSE_ERROR_SAMPLE_RATE;
             hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
             hdr->sr_shift = 1;
         } else {
diff --git a/libavcodec/ac3_parser_internal.h b/libavcodec/ac3_parser_internal.h
index 2ac0e67ec2..2c4eb546e5 100644
--- a/libavcodec/ac3_parser_internal.h
+++ b/libavcodec/ac3_parser_internal.h
@@ -64,15 +64,22 @@  typedef struct AC3HeaderInfo {
     /** @} */
 } AC3HeaderInfo;
 
+typedef enum {
+    AC3_PARSE_ERROR_SYNC        = -0x1030c0a,
+    AC3_PARSE_ERROR_BSID        = -0x2030c0a,
+    AC3_PARSE_ERROR_SAMPLE_RATE = -0x3030c0a,
+    AC3_PARSE_ERROR_FRAME_SIZE  = -0x4030c0a,
+    AC3_PARSE_ERROR_FRAME_TYPE  = -0x5030c0a,
+    AC3_PARSE_ERROR_CRC         = -0x6030c0a,
+} AC3ParseError;
+
 /**
  * Parse AC-3 frame header.
  * Parse the header up to the lfeon element, which is the first 52 or 54 bits
  * depending on the audio coding mode.
  * @param[in]  gbc BitContext containing the first 54 bits of the frame.
  * @param[out] hdr Pointer to struct where header info is written.
- * @return Returns 0 on success, -1 if there is a sync word mismatch,
- * -2 if the bsid (version) element is invalid, -3 if the fscod (sample rate)
- * element is invalid, or -4 if the frmsizecod (bit rate) element is invalid.
+ * @return Returns 0 on success and AC3_PARSE_ERROR_* values otherwise.
  */
 int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr);
 
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index 2d7e11c5b8..0a4d3375ee 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -39,7 +39,6 @@ 
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 #include "bswapdsp.h"
-#include "aac_ac3_parser.h"
 #include "ac3_parser_internal.h"
 #include "ac3dec.h"
 #include "ac3dec_data.h"
@@ -1538,19 +1537,19 @@  dependent_frame:
 
     if (err) {
         switch (err) {
-        case AAC_AC3_PARSE_ERROR_SYNC:
+        case AC3_PARSE_ERROR_SYNC:
             av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
             return AVERROR_INVALIDDATA;
-        case AAC_AC3_PARSE_ERROR_BSID:
+        case AC3_PARSE_ERROR_BSID:
             av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");
             break;
-        case AAC_AC3_PARSE_ERROR_SAMPLE_RATE:
+        case AC3_PARSE_ERROR_SAMPLE_RATE:
             av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
             break;
-        case AAC_AC3_PARSE_ERROR_FRAME_SIZE:
+        case AC3_PARSE_ERROR_FRAME_SIZE:
             av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");
             break;
-        case AAC_AC3_PARSE_ERROR_FRAME_TYPE:
+        case AC3_PARSE_ERROR_FRAME_TYPE:
             /* skip frame if CRC is ok. otherwise use error concealment. */
             /* TODO: add support for substreams */
             if (s->substreamid) {
@@ -1563,8 +1562,7 @@  dependent_frame:
                 av_log(avctx, AV_LOG_ERROR, "invalid frame type\n");
             }
             break;
-        case AAC_AC3_PARSE_ERROR_CRC:
-        case AAC_AC3_PARSE_ERROR_CHANNEL_CFG:
+        case AC3_PARSE_ERROR_CRC:
             break;
         default: // Normal AVERROR do not try to recover.
             *got_frame_ptr = 0;
@@ -1574,7 +1572,7 @@  dependent_frame:
         /* check that reported frame size fits in input buffer */
         if (s->frame_size > buf_size) {
             av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
-            err = AAC_AC3_PARSE_ERROR_FRAME_SIZE;
+            err = AC3_PARSE_ERROR_FRAME_SIZE;
         } else if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_CAREFUL)) {
             /* check for crc mismatch */
             if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2],
@@ -1582,7 +1580,7 @@  dependent_frame:
                 av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
                 if (avctx->err_recognition & AV_EF_EXPLODE)
                     return AVERROR_INVALIDDATA;
-                err = AAC_AC3_PARSE_ERROR_CRC;
+                err = AC3_PARSE_ERROR_CRC;
             }
         }
     }
diff --git a/libavcodec/adts_header.c b/libavcodec/adts_header.c
index 00fa0a5a99..8663d00f4c 100644
--- a/libavcodec/adts_header.c
+++ b/libavcodec/adts_header.c
@@ -21,7 +21,6 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "aac_ac3_parser.h"
 #include "adts_header.h"
 #include "adts_parser.h"
 #include "get_bits.h"
@@ -35,7 +34,7 @@  int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
     memset(hdr, 0, sizeof(*hdr));
 
     if (get_bits(gbc, 12) != 0xfff)
-        return AAC_AC3_PARSE_ERROR_SYNC;
+        return AAC_PARSE_ERROR_SYNC;
 
     skip_bits1(gbc);             /* id */
     skip_bits(gbc, 2);           /* layer */
@@ -43,7 +42,7 @@  int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
     aot     = get_bits(gbc, 2);  /* profile_objecttype */
     sr      = get_bits(gbc, 4);  /* sample_frequency_index */
     if (!ff_mpeg4audio_sample_rates[sr])
-        return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
+        return AAC_PARSE_ERROR_SAMPLE_RATE;
     skip_bits1(gbc);             /* private_bit */
     ch = get_bits(gbc, 3);       /* channel_configuration */
 
@@ -55,7 +54,7 @@  int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
     skip_bits1(gbc);             /* copyright_identification_start */
     size = get_bits(gbc, 13);    /* aac_frame_length */
     if (size < AV_AAC_ADTS_HEADER_SIZE)
-        return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
+        return AAC_PARSE_ERROR_FRAME_SIZE;
 
     skip_bits(gbc, 11);          /* adts_buffer_fullness */
     rdb = get_bits(gbc, 2);      /* number_of_raw_data_blocks_in_frame */
diff --git a/libavcodec/adts_header.h b/libavcodec/adts_header.h
index 354d07e1f8..d89d487025 100644
--- a/libavcodec/adts_header.h
+++ b/libavcodec/adts_header.h
@@ -25,6 +25,12 @@ 
 
 #include "get_bits.h"
 
+typedef enum {
+    AAC_PARSE_ERROR_SYNC        = -0x1030c0a,
+    AAC_PARSE_ERROR_SAMPLE_RATE = -0x3030c0a,
+    AAC_PARSE_ERROR_FRAME_SIZE  = -0x4030c0a,
+} AACParseError;
+
 typedef struct AACADTSHeaderInfo {
     uint32_t sample_rate;
     uint32_t samples;
@@ -42,9 +48,8 @@  typedef struct AACADTSHeaderInfo {
  * the first 54 bits.
  * @param[in]  gbc BitContext containing the first 54 bits of the frame.
  * @param[out] hdr Pointer to struct where header info is written.
- * @return Returns 0 on success, -1 if there is a sync word mismatch,
- * -2 if the version element is invalid, -3 if the sample rate
- * element is invalid, or -4 if the bit rate element is invalid.
+ * @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);
 
@@ -56,9 +61,8 @@  int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr);
  * @param[out] phdr Pointer to pointer to struct AACADTSHeaderInfo for which
  * memory is allocated and header info is written into it. After using the header
  * information, the allocated memory must be freed by using av_free.
- * @return Returns 0 on success, -1 if there is a sync word mismatch,
- * -2 if the version element is invalid, -3 if the sample rate
- * element is invalid, or -4 if the bit rate element is invalid.
+ * @return Returns 0 on success, AAC_PARSE_ERROR_* values on invalid input
+ *         and ordinary AVERROR codes otherwise.
  */
 int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_t size);
 
diff --git a/libavcodec/eac3dec.c b/libavcodec/eac3dec.c
index 5c71751a0c..2b3bffda6e 100644
--- a/libavcodec/eac3dec.c
+++ b/libavcodec/eac3dec.c
@@ -39,8 +39,8 @@ 
 
 
 #include "avcodec.h"
-#include "aac_ac3_parser.h"
 #include "ac3.h"
+#include "ac3_parser_internal.h"
 #include "ac3dec.h"
 #include "ac3dec_data.h"
 #include "eac3_data.h"
@@ -300,7 +300,7 @@  static int ff_eac3_parse_header(AC3DecodeContext *s)
        dependent streams which are used to add or replace channels. */
     if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
         av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
-        return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
+        return AC3_PARSE_ERROR_FRAME_TYPE;
     }
 
     /* The substream id indicates which substream this frame belongs to. each
@@ -312,7 +312,7 @@  static int ff_eac3_parse_header(AC3DecodeContext *s)
             s->eac3_subsbtreamid_found = 1;
             avpriv_request_sample(s->avctx, "Additional substreams");
         }
-        return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
+        return AC3_PARSE_ERROR_FRAME_TYPE;
     }
 
     if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {