comparison ac3_parser.c @ 6117:01b1342e717b libavcodec

move E-AC3 header parsing to ff_ac3_parse_header()
author jbr
date Sat, 05 Jan 2008 18:39:55 +0000
parents 4f8fcb40bf2c
children 76801e61ddc4
comparison
equal deleted inserted replaced
6116:4f8fcb40bf2c 6117:01b1342e717b
36 36
37 int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr) 37 int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr)
38 { 38 {
39 GetBitContext gbc; 39 GetBitContext gbc;
40 int frame_size_code; 40 int frame_size_code;
41 int num_blocks;
41 42
42 memset(hdr, 0, sizeof(*hdr)); 43 memset(hdr, 0, sizeof(*hdr));
43 44
44 init_get_bits(&gbc, buf, 54); 45 init_get_bits(&gbc, buf, 54);
45 46
46 hdr->sync_word = get_bits(&gbc, 16); 47 hdr->sync_word = get_bits(&gbc, 16);
47 if(hdr->sync_word != 0x0B77) 48 if(hdr->sync_word != 0x0B77)
48 return AC3_PARSE_ERROR_SYNC; 49 return AC3_PARSE_ERROR_SYNC;
49 50
50 /* read ahead to bsid to make sure this is AC-3, not E-AC-3 */ 51 /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
51 hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F; 52 hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F;
52 if(hdr->bitstream_id > 10) 53 if(hdr->bitstream_id > 16)
53 return AC3_PARSE_ERROR_BSID; 54 return AC3_PARSE_ERROR_BSID;
54 55
56 if(hdr->bitstream_id <= 10) {
57 /* Normal AC-3 */
55 hdr->crc1 = get_bits(&gbc, 16); 58 hdr->crc1 = get_bits(&gbc, 16);
56 hdr->sr_code = get_bits(&gbc, 2); 59 hdr->sr_code = get_bits(&gbc, 2);
57 if(hdr->sr_code == 3) 60 if(hdr->sr_code == 3)
58 return AC3_PARSE_ERROR_SAMPLE_RATE; 61 return AC3_PARSE_ERROR_SAMPLE_RATE;
59 62
79 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8; 82 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
80 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift; 83 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
81 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift; 84 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
82 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on; 85 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
83 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2; 86 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
87 } else {
88 /* Enhanced AC-3 */
89 hdr->crc1 = 0;
90 skip_bits(&gbc, 2); // skip stream type
91 skip_bits(&gbc, 3); // skip substream id
92
93 hdr->frame_size = (get_bits(&gbc, 11) + 1) << 1;
94 if(hdr->frame_size < AC3_HEADER_SIZE)
95 return AC3_PARSE_ERROR_FRAME_SIZE;
96
97 hdr->sr_code = get_bits(&gbc, 2);
98 if (hdr->sr_code == 3) {
99 int sr_code2 = get_bits(&gbc, 2);
100 if(sr_code2 == 3)
101 return AC3_PARSE_ERROR_SAMPLE_RATE;
102 hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
103 hdr->sr_shift = 1;
104 num_blocks = 6;
105 } else {
106 num_blocks = eac3_blocks[get_bits(&gbc, 2)];
107 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
108 hdr->sr_shift = 0;
109 }
110
111 hdr->channel_mode = get_bits(&gbc, 3);
112 hdr->lfe_on = get_bits1(&gbc);
113
114 hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
115 (num_blocks * 256.0));
116 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
117 }
84 118
85 return 0; 119 return 0;
86 } 120 }
87 121
88 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate, 122 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
89 int *bit_rate, int *samples) 123 int *bit_rate, int *samples)
90 { 124 {
91 int err; 125 int err;
92 unsigned int sr_code, channel_mode, bitstream_id, lfe_on;
93 unsigned int stream_type, substream_id, frame_size, sr_code2, num_blocks_code;
94 GetBitContext bits;
95 AC3HeaderInfo hdr; 126 AC3HeaderInfo hdr;
96 127
97 err = ff_ac3_parse_header(buf, &hdr); 128 err = ff_ac3_parse_header(buf, &hdr);
98 129
99 if(err < 0 && err != -2) 130 if(err < 0)
100 return 0; 131 return 0;
101 132
102 bitstream_id = hdr.bitstream_id;
103 if(bitstream_id <= 10) { /* Normal AC-3 */
104 *sample_rate = hdr.sample_rate; 133 *sample_rate = hdr.sample_rate;
105 *bit_rate = hdr.bit_rate; 134 *bit_rate = hdr.bit_rate;
106 *channels = hdr.channels; 135 *channels = hdr.channels;
107 *samples = AC3_FRAME_SIZE; 136 *samples = AC3_FRAME_SIZE;
108 return hdr.frame_size; 137 return hdr.frame_size;
109 } else if (bitstream_id > 10 && bitstream_id <= 16) { /* Enhanced AC-3 */
110 init_get_bits(&bits, &buf[2], (AC3_HEADER_SIZE-2) * 8);
111 stream_type = get_bits(&bits, 2);
112 substream_id = get_bits(&bits, 3);
113
114 if (stream_type != 0 || substream_id != 0)
115 return 0; /* Currently don't support additional streams */
116
117 frame_size = get_bits(&bits, 11) + 1;
118 if(frame_size*2 < AC3_HEADER_SIZE)
119 return 0;
120
121 sr_code = get_bits(&bits, 2);
122 if (sr_code == 3) {
123 sr_code2 = get_bits(&bits, 2);
124 num_blocks_code = 3;
125
126 if(sr_code2 == 3)
127 return 0;
128
129 *sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
130 } else {
131 num_blocks_code = get_bits(&bits, 2);
132
133 *sample_rate = ff_ac3_sample_rate_tab[sr_code];
134 }
135
136 channel_mode = get_bits(&bits, 3);
137 lfe_on = get_bits1(&bits);
138
139 *samples = eac3_blocks[num_blocks_code] * 256;
140 *bit_rate = frame_size * (*sample_rate) * 16 / (*samples);
141 *channels = ff_ac3_channels_tab[channel_mode] + lfe_on;
142
143 return frame_size * 2;
144 }
145
146 /* Unsupported bitstream version */
147 return 0;
148 } 138 }
149 139
150 static int ac3_parse_init(AVCodecParserContext *s1) 140 static int ac3_parse_init(AVCodecParserContext *s1)
151 { 141 {
152 AACAC3ParseContext *s = s1->priv_data; 142 AACAC3ParseContext *s = s1->priv_data;