00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "parser.h"
00024 #include "ac3_parser.h"
00025 #include "aac_ac3_parser.h"
00026 #include "bitstream.h"
00027
00028
00029 #define AC3_HEADER_SIZE 7
00030
00031
00032 static const uint8_t eac3_blocks[4] = {
00033 1, 2, 3, 6
00034 };
00035
00036
00037 int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr)
00038 {
00039 GetBitContext gbc;
00040 int frame_size_code;
00041 int num_blocks;
00042
00043 memset(hdr, 0, sizeof(*hdr));
00044
00045 init_get_bits(&gbc, buf, 54);
00046
00047 hdr->sync_word = get_bits(&gbc, 16);
00048 if(hdr->sync_word != 0x0B77)
00049 return AC3_PARSE_ERROR_SYNC;
00050
00051
00052 hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F;
00053 if(hdr->bitstream_id > 16)
00054 return AC3_PARSE_ERROR_BSID;
00055
00056 if(hdr->bitstream_id <= 10) {
00057
00058 hdr->crc1 = get_bits(&gbc, 16);
00059 hdr->sr_code = get_bits(&gbc, 2);
00060 if(hdr->sr_code == 3)
00061 return AC3_PARSE_ERROR_SAMPLE_RATE;
00062
00063 frame_size_code = get_bits(&gbc, 6);
00064 if(frame_size_code > 37)
00065 return AC3_PARSE_ERROR_FRAME_SIZE;
00066
00067 skip_bits(&gbc, 5);
00068
00069 skip_bits(&gbc, 3);
00070 hdr->channel_mode = get_bits(&gbc, 3);
00071 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) {
00072 skip_bits(&gbc, 2);
00073 }
00074 if(hdr->channel_mode & 4) {
00075 skip_bits(&gbc, 2);
00076 }
00077 if(hdr->channel_mode == AC3_CHMODE_STEREO) {
00078 skip_bits(&gbc, 2);
00079 }
00080 hdr->lfe_on = get_bits1(&gbc);
00081
00082 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
00083 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
00084 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
00085 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
00086 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
00087 } else {
00088
00089 hdr->crc1 = 0;
00090 skip_bits(&gbc, 2);
00091 skip_bits(&gbc, 3);
00092
00093 hdr->frame_size = (get_bits(&gbc, 11) + 1) << 1;
00094 if(hdr->frame_size < AC3_HEADER_SIZE)
00095 return AC3_PARSE_ERROR_FRAME_SIZE;
00096
00097 hdr->sr_code = get_bits(&gbc, 2);
00098 if (hdr->sr_code == 3) {
00099 int sr_code2 = get_bits(&gbc, 2);
00100 if(sr_code2 == 3)
00101 return AC3_PARSE_ERROR_SAMPLE_RATE;
00102 hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
00103 hdr->sr_shift = 1;
00104 num_blocks = 6;
00105 } else {
00106 num_blocks = eac3_blocks[get_bits(&gbc, 2)];
00107 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
00108 hdr->sr_shift = 0;
00109 }
00110
00111 hdr->channel_mode = get_bits(&gbc, 3);
00112 hdr->lfe_on = get_bits1(&gbc);
00113
00114 hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
00115 (num_blocks * 256.0));
00116 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
00117 }
00118
00119 return 0;
00120 }
00121
00122 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
00123 int *bit_rate, int *samples)
00124 {
00125 int err;
00126 AC3HeaderInfo hdr;
00127
00128 err = ff_ac3_parse_header(buf, &hdr);
00129
00130 if(err < 0)
00131 return 0;
00132
00133 *sample_rate = hdr.sample_rate;
00134 *bit_rate = hdr.bit_rate;
00135 *channels = hdr.channels;
00136 *samples = AC3_FRAME_SIZE;
00137 return hdr.frame_size;
00138 }
00139
00140 static int ac3_parse_init(AVCodecParserContext *s1)
00141 {
00142 AACAC3ParseContext *s = s1->priv_data;
00143 s->inbuf_ptr = s->inbuf;
00144 s->header_size = AC3_HEADER_SIZE;
00145 s->sync = ac3_sync;
00146 return 0;
00147 }
00148
00149
00150 AVCodecParser ac3_parser = {
00151 { CODEC_ID_AC3 },
00152 sizeof(AACAC3ParseContext),
00153 ac3_parse_init,
00154 ff_aac_ac3_parse,
00155 NULL,
00156 };