changeset 6661:a409fbf1f42b libavcodec

change ff_ac3_parse_header() to take a GetBitContext instead of const char*
author bwolowiec
date Tue, 22 Apr 2008 11:14:01 +0000
parents 582712867474
children c5432b7e7bc1
files ac3.h ac3_parser.c ac3_parser.h ac3dec.c
diffstat 4 files changed, 53 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/ac3.h	Tue Apr 22 09:16:05 2008 +0000
+++ b/ac3.h	Tue Apr 22 11:14:01 2008 +0000
@@ -95,6 +95,8 @@
     uint32_t bit_rate;
     uint8_t channels;
     uint16_t frame_size;
+    int center_mix_level;                   ///< Center mix level index
+    int surround_mix_level;                 ///< Surround mix level index
     /** @} */
 } AC3HeaderInfo;
 
--- a/ac3_parser.c	Tue Apr 22 09:16:05 2008 +0000
+++ b/ac3_parser.c	Tue Apr 22 11:14:01 2008 +0000
@@ -33,51 +33,64 @@
     1, 2, 3, 6
 };
 
+/**
+ * Table for center mix levels
+ * reference: Section 5.4.2.4 cmixlev
+ */
+static const uint8_t center_levels[4] = { 2, 3, 4, 3 };
 
-int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr)
+/**
+ * Table for surround mix levels
+ * reference: Section 5.4.2.5 surmixlev
+ */
+static const uint8_t surround_levels[4] = { 2, 4, 0, 4 };
+
+
+int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
 {
-    GetBitContext gbc;
     int frame_size_code;
     int num_blocks;
 
     memset(hdr, 0, sizeof(*hdr));
 
-    init_get_bits(&gbc, buf, 54);
-
-    hdr->sync_word = get_bits(&gbc, 16);
+    hdr->sync_word = get_bits(gbc, 16);
     if(hdr->sync_word != 0x0B77)
         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;
+    hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
     if(hdr->bitstream_id > 16)
         return AC3_PARSE_ERROR_BSID;
 
     if(hdr->bitstream_id <= 10) {
         /* Normal AC-3 */
-        hdr->crc1 = get_bits(&gbc, 16);
-        hdr->sr_code = get_bits(&gbc, 2);
+        hdr->crc1 = get_bits(gbc, 16);
+        hdr->sr_code = get_bits(gbc, 2);
         if(hdr->sr_code == 3)
             return AC3_PARSE_ERROR_SAMPLE_RATE;
 
-        frame_size_code = get_bits(&gbc, 6);
+        frame_size_code = get_bits(gbc, 6);
         if(frame_size_code > 37)
             return AC3_PARSE_ERROR_FRAME_SIZE;
 
-        skip_bits(&gbc, 5); // skip bsid, already got it
+        skip_bits(gbc, 5); // skip bsid, already got it
+
+        skip_bits(gbc, 3); // skip bitstream mode
+        hdr->channel_mode = get_bits(gbc, 3);
 
-        skip_bits(&gbc, 3); // skip bitstream mode
-        hdr->channel_mode = get_bits(&gbc, 3);
-        if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) {
-            skip_bits(&gbc, 2); // skip center mix level
+        /* set default mix levels */
+        hdr->center_mix_level   = 3;  // -4.5dB
+        hdr->surround_mix_level = 4;  // -6.0dB
+
+        if(hdr->channel_mode == AC3_CHMODE_STEREO) {
+            skip_bits(gbc, 2); // skip dsurmod
+        } else {
+            if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
+                hdr->center_mix_level = center_levels[get_bits(gbc, 2)];
+            if(hdr->channel_mode & 4)
+                hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
         }
-        if(hdr->channel_mode & 4) {
-            skip_bits(&gbc, 2); // skip surround mix level
-        }
-        if(hdr->channel_mode == AC3_CHMODE_STEREO) {
-            skip_bits(&gbc, 2); // skip dolby surround mode
-        }
-        hdr->lfe_on = get_bits1(&gbc);
+        hdr->lfe_on = get_bits1(gbc);
 
         hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
         hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
@@ -88,32 +101,32 @@
     } else {
         /* Enhanced AC-3 */
         hdr->crc1 = 0;
-        hdr->frame_type = get_bits(&gbc, 2);
+        hdr->frame_type = get_bits(gbc, 2);
         if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
             return AC3_PARSE_ERROR_FRAME_TYPE;
 
-        skip_bits(&gbc, 3); // skip substream id
+        skip_bits(gbc, 3); // skip substream id
 
-        hdr->frame_size = (get_bits(&gbc, 11) + 1) << 1;
+        hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
         if(hdr->frame_size < AC3_HEADER_SIZE)
             return AC3_PARSE_ERROR_FRAME_SIZE;
 
-        hdr->sr_code = get_bits(&gbc, 2);
+        hdr->sr_code = get_bits(gbc, 2);
         if (hdr->sr_code == 3) {
-            int sr_code2 = get_bits(&gbc, 2);
+            int sr_code2 = get_bits(gbc, 2);
             if(sr_code2 == 3)
                 return AC3_PARSE_ERROR_SAMPLE_RATE;
             hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
             hdr->sr_shift = 1;
             num_blocks = 6;
         } else {
-            num_blocks = eac3_blocks[get_bits(&gbc, 2)];
+            num_blocks = eac3_blocks[get_bits(gbc, 2)];
             hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
             hdr->sr_shift = 0;
         }
 
-        hdr->channel_mode = get_bits(&gbc, 3);
-        hdr->lfe_on = get_bits1(&gbc);
+        hdr->channel_mode = get_bits(gbc, 3);
+        hdr->lfe_on = get_bits1(gbc);
 
         hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
                         (num_blocks * 256.0));
@@ -129,8 +142,10 @@
     int err;
     uint64_t tmp = be2me_64(state);
     AC3HeaderInfo hdr;
+    GetBitContext gbc;
 
-    err = ff_ac3_parse_header(((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, &hdr);
+    init_get_bits(&gbc, ((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, 54);
+    err = ff_ac3_parse_header(&gbc, &hdr);
 
     if(err < 0)
         return 0;
--- a/ac3_parser.h	Tue Apr 22 09:16:05 2008 +0000
+++ b/ac3_parser.h	Tue Apr 22 11:14:01 2008 +0000
@@ -24,6 +24,7 @@
 #define FFMPEG_AC3_PARSER_H
 
 #include "ac3.h"
+#include "bitstream.h"
 
 typedef enum {
     AC3_PARSE_ERROR_SYNC        = -1,
@@ -37,12 +38,12 @@
  * Parses AC-3 frame header.
  * Parses the header up to the lfeon element, which is the first 52 or 54 bits
  * depending on the audio coding mode.
- * @param buf[in] Array containing the first 7 bytes of the frame.
+ * @param gbc[in] BitContext containing the first 54 bits of the frame.
  * @param hdr[out] 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.
  */
-int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr);
+int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr);
 
 #endif /* FFMPEG_AC3_PARSER_H */
--- a/ac3dec.c	Tue Apr 22 09:16:05 2008 +0000
+++ b/ac3dec.c	Tue Apr 22 11:14:01 2008 +0000
@@ -89,18 +89,6 @@
 };
 
 /**
- * Table for center mix levels
- * reference: Section 5.4.2.4 cmixlev
- */
-static const uint8_t center_levels[4] = { 2, 3, 4, 3 };
-
-/**
- * Table for surround mix levels
- * reference: Section 5.4.2.5 surmixlev
- */
-static const uint8_t surround_levels[4] = { 2, 4, 0, 4 };
-
-/**
  * Table for default stereo downmixing coefficients
  * reference: Section 7.8.2 Downmixing Into Two Channels
  */
@@ -315,7 +303,7 @@
     GetBitContext *gbc = &s->gbc;
     int err, i;
 
-    err = ff_ac3_parse_header(gbc->buffer, &hdr);
+    err = ff_ac3_parse_header(gbc, &hdr);
     if(err)
         return err;
 
@@ -333,6 +321,8 @@
     s->fbw_channels                 = s->channels - s->lfe_on;
     s->lfe_ch                       = s->fbw_channels + 1;
     s->frame_size                   = hdr.frame_size;
+    s->center_mix_level             = hdr.center_mix_level;
+    s->surround_mix_level           = hdr.surround_mix_level;
 
     /* set default output to all source channels */
     s->out_channels = s->channels;
@@ -340,25 +330,6 @@
     if(s->lfe_on)
         s->output_mode |= AC3_OUTPUT_LFEON;
 
-    /* set default mix levels */
-    s->center_mix_level   = 3;  // -4.5dB
-    s->surround_mix_level = 4;  // -6.0dB
-
-    /* skip over portion of header which has already been read */
-    skip_bits(gbc, 16); // skip the sync_word
-    skip_bits(gbc, 16); // skip crc1
-    skip_bits(gbc, 8);  // skip fscod and frmsizecod
-    skip_bits(gbc, 11); // skip bsid, bsmod, and acmod
-    if(s->channel_mode == AC3_CHMODE_STEREO) {
-        skip_bits(gbc, 2); // skip dsurmod
-    } else {
-        if((s->channel_mode & 1) && s->channel_mode != AC3_CHMODE_MONO)
-            s->center_mix_level = center_levels[get_bits(gbc, 2)];
-        if(s->channel_mode & 4)
-            s->surround_mix_level = surround_levels[get_bits(gbc, 2)];
-    }
-    skip_bits1(gbc); // skip lfeon
-
     /* read the rest of the bsi. read twice for dual mono mode. */
     i = !(s->channel_mode);
     do {