summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Ruggles <justin.ruggles@gmail.com>2007-03-11 02:13:05 +0000
committerJustin Ruggles <justin.ruggles@gmail.com>2007-03-11 02:13:05 +0000
commit050aa8b9bca6a2c29e1c4c914e05aca49007f1d7 (patch)
treed58da1f718db33a9d243668d4940a60a5d455388
parent755c18ae5d19f940dfd05d8278637635fd8eece5 (diff)
remove redundancy in AC-3 parser by using common tables from ac3tab.h
Originally committed as revision 8318 to svn://svn.ffmpeg.org/ffmpeg/trunk
-rw-r--r--libavcodec/ac3.c13
-rw-r--r--libavcodec/ac3.h2
-rw-r--r--libavcodec/ac3enc.c6
-rw-r--r--libavcodec/ac3tab.h14
-rw-r--r--libavcodec/parser.c72
5 files changed, 42 insertions, 65 deletions
diff --git a/libavcodec/ac3.c b/libavcodec/ac3.c
index 019c7769d6..a1c46c6d2e 100644
--- a/libavcodec/ac3.c
+++ b/libavcodec/ac3.c
@@ -181,6 +181,11 @@ void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
} while (end > bndtab[j++]);
}
+/**
+ * Initializes some tables.
+ * note: This function must remain thread safe because it is called by the
+ * AVParser init code.
+ */
void ac3_common_init(void)
{
int i, j, k, l, v;
@@ -194,4 +199,12 @@ void ac3_common_init(void)
l += v;
}
bndtab[50] = l;
+
+ /* generate ff_ac3_frame_sizes table */
+ for(i=0; i<38; i++) {
+ int br = ff_ac3_bitratetab[i >> 1];
+ ff_ac3_frame_sizes[i][0] = ( 2*br );
+ ff_ac3_frame_sizes[i][1] = (320*br / 147) + (i & 1);
+ ff_ac3_frame_sizes[i][2] = ( 3*br );
+ }
}
diff --git a/libavcodec/ac3.h b/libavcodec/ac3.h
index 21cc07044e..28c7f03956 100644
--- a/libavcodec/ac3.h
+++ b/libavcodec/ac3.h
@@ -45,6 +45,8 @@ typedef struct AC3BitAllocParameters {
int cplfleak, cplsleak;
} AC3BitAllocParameters;
+extern uint16_t ff_ac3_frame_sizes[38][3];
+extern const uint8_t ff_ac3_channels[8];
extern const uint16_t ff_ac3_freqs[3];
extern const uint16_t ff_ac3_bitratetab[19];
extern const int16_t ff_ac3_window[256];
diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index 4037b91528..623541d23b 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -611,6 +611,8 @@ static int AC3_encode_init(AVCodecContext *avctx)
avctx->frame_size = AC3_FRAME_SIZE;
+ ac3_common_init();
+
/* number of channels */
if (channels < 1 || channels > 6)
return -1;
@@ -644,7 +646,7 @@ static int AC3_encode_init(AVCodecContext *avctx)
return -1;
s->bit_rate = bitrate;
s->frmsizecod = i << 1;
- s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16);
+ s->frame_size_min = ff_ac3_frame_sizes[s->frmsizecod][s->fscod];
s->bits_written = 0;
s->samples_written = 0;
s->frame_size = s->frame_size_min;
@@ -663,8 +665,6 @@ static int AC3_encode_init(AVCodecContext *avctx)
/* initial snr offset */
s->csnroffst = 40;
- ac3_common_init();
-
/* mdct init */
fft_init(MDCT_NBITS - 2);
for(i=0;i<N/4;i++) {
diff --git a/libavcodec/ac3tab.h b/libavcodec/ac3tab.h
index 928f3c370c..e72984e170 100644
--- a/libavcodec/ac3tab.h
+++ b/libavcodec/ac3tab.h
@@ -24,6 +24,20 @@
* tables taken directly from AC3 spec.
*/
+/**
+ * Possible frame sizes.
+ * Generated at runtime to match ATSC A/52 Table 5.18 Frame Size Code Table.
+ */
+uint16_t ff_ac3_frame_sizes[38][3];
+
+/**
+ * Maps audio coding mode (acmod) to number of full-bandwidth channels.
+ * from ATSC A/52 Table 5.8 Audio Coding Mode
+ */
+const uint8_t ff_ac3_channels[8] = {
+ 2, 1, 2, 3, 3, 4, 4, 5
+};
+
/* possible frequencies */
const uint16_t ff_ac3_freqs[3] = { 48000, 44100, 32000 };
diff --git a/libavcodec/parser.c b/libavcodec/parser.c
index 1ef7339f19..bafe9c831e 100644
--- a/libavcodec/parser.c
+++ b/libavcodec/parser.c
@@ -22,6 +22,7 @@
#include "avcodec.h"
#include "mpegvideo.h"
#include "mpegaudio.h"
+#include "ac3.h"
#include "parser.h"
AVCodecParser *av_first_parser = NULL;
@@ -581,60 +582,6 @@ typedef struct AC3ParseContext {
#define AAC_HEADER_SIZE 7
#ifdef CONFIG_AC3_PARSER
-static const int ac3_sample_rates[4] = {
- 48000, 44100, 32000, 0
-};
-
-static const int ac3_frame_sizes[64][3] = {
- { 64, 69, 96 },
- { 64, 70, 96 },
- { 80, 87, 120 },
- { 80, 88, 120 },
- { 96, 104, 144 },
- { 96, 105, 144 },
- { 112, 121, 168 },
- { 112, 122, 168 },
- { 128, 139, 192 },
- { 128, 140, 192 },
- { 160, 174, 240 },
- { 160, 175, 240 },
- { 192, 208, 288 },
- { 192, 209, 288 },
- { 224, 243, 336 },
- { 224, 244, 336 },
- { 256, 278, 384 },
- { 256, 279, 384 },
- { 320, 348, 480 },
- { 320, 349, 480 },
- { 384, 417, 576 },
- { 384, 418, 576 },
- { 448, 487, 672 },
- { 448, 488, 672 },
- { 512, 557, 768 },
- { 512, 558, 768 },
- { 640, 696, 960 },
- { 640, 697, 960 },
- { 768, 835, 1152 },
- { 768, 836, 1152 },
- { 896, 975, 1344 },
- { 896, 976, 1344 },
- { 1024, 1114, 1536 },
- { 1024, 1115, 1536 },
- { 1152, 1253, 1728 },
- { 1152, 1254, 1728 },
- { 1280, 1393, 1920 },
- { 1280, 1394, 1920 },
-};
-
-static const int ac3_bitrates[64] = {
- 32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112,
- 128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384,
- 384, 448, 448, 512, 512, 576, 576, 640, 640,
-};
-
-static const uint8_t ac3_channels[8] = {
- 2, 1, 2, 3, 3, 4, 4, 5
-};
static const uint8_t eac3_blocks[4] = {
1, 2, 3, 6
@@ -672,7 +619,7 @@ static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
fscod = get_bits(&bits, 2);
frmsizecod = get_bits(&bits, 6);
- if(fscod == 3)
+ if(fscod == 3 && frmsizecod > 37)
return 0;
skip_bits(&bits, 5); /* bsid */
@@ -687,12 +634,12 @@ static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
lfeon = get_bits1(&bits);
halfratecod = FFMAX(bsid, 8) - 8;
- *sample_rate = ac3_sample_rates[fscod] >> halfratecod;
- *bit_rate = (ac3_bitrates[frmsizecod] * 1000) >> halfratecod;
- *channels = ac3_channels[acmod] + lfeon;
+ *sample_rate = ff_ac3_freqs[fscod] >> halfratecod;
+ *bit_rate = (ff_ac3_bitratetab[frmsizecod] * 1000) >> halfratecod;
+ *channels = ff_ac3_channels[acmod] + lfeon;
*samples = 6 * 256;
- return ac3_frame_sizes[frmsizecod][fscod] * 2;
+ return ff_ac3_frame_sizes[frmsizecod][fscod] * 2;
} else if (bsid > 10 && bsid <= 16) { /* Enhanced AC-3 */
strmtyp = get_bits(&bits, 2);
substreamid = get_bits(&bits, 3);
@@ -709,11 +656,11 @@ static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
if(fscod2 == 3)
return 0;
- *sample_rate = ac3_sample_rates[fscod2] / 2;
+ *sample_rate = ff_ac3_freqs[fscod2] / 2;
} else {
numblkscod = get_bits(&bits, 2);
- *sample_rate = ac3_sample_rates[fscod];
+ *sample_rate = ff_ac3_freqs[fscod];
}
acmod = get_bits(&bits, 3);
@@ -721,7 +668,7 @@ static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
*samples = eac3_blocks[numblkscod] * 256;
*bit_rate = frmsiz * (*sample_rate) * 16 / (*samples);
- *channels = ac3_channels[acmod] + lfeon;
+ *channels = ff_ac3_channels[acmod] + lfeon;
return frmsiz * 2;
}
@@ -780,6 +727,7 @@ static int ac3_parse_init(AVCodecParserContext *s1)
s->inbuf_ptr = s->inbuf;
s->header_size = AC3_HEADER_SIZE;
s->sync = ac3_sync;
+ ac3_common_init();
return 0;
}
#endif