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 "get_bits.h"
00027 #include "libavutil/audioconvert.h"
00028
00029
00030 #define AC3_HEADER_SIZE 7
00031
00032
00033 static const uint8_t eac3_blocks[4] = {
00034 1, 2, 3, 6
00035 };
00036
00037
00038 int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
00039 {
00040 int frame_size_code;
00041
00042 memset(hdr, 0, sizeof(*hdr));
00043
00044 hdr->sync_word = get_bits(gbc, 16);
00045 if(hdr->sync_word != 0x0B77)
00046 return AAC_AC3_PARSE_ERROR_SYNC;
00047
00048
00049 hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
00050 if(hdr->bitstream_id > 16)
00051 return AAC_AC3_PARSE_ERROR_BSID;
00052
00053 hdr->num_blocks = 6;
00054
00055
00056 hdr->center_mix_level = 1;
00057 hdr->surround_mix_level = 1;
00058
00059 if(hdr->bitstream_id <= 10) {
00060
00061 hdr->crc1 = get_bits(gbc, 16);
00062 hdr->sr_code = get_bits(gbc, 2);
00063 if(hdr->sr_code == 3)
00064 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
00065
00066 frame_size_code = get_bits(gbc, 6);
00067 if(frame_size_code > 37)
00068 return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
00069
00070 skip_bits(gbc, 5);
00071
00072 hdr->bitstream_mode = get_bits(gbc, 3);
00073 hdr->channel_mode = get_bits(gbc, 3);
00074
00075 if(hdr->channel_mode == AC3_CHMODE_STEREO) {
00076 skip_bits(gbc, 2);
00077 } else {
00078 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
00079 hdr->center_mix_level = get_bits(gbc, 2);
00080 if(hdr->channel_mode & 4)
00081 hdr->surround_mix_level = get_bits(gbc, 2);
00082 }
00083 hdr->lfe_on = get_bits1(gbc);
00084
00085 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
00086 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
00087 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
00088 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
00089 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
00090 hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT;
00091 hdr->substreamid = 0;
00092 } else {
00093
00094 hdr->crc1 = 0;
00095 hdr->frame_type = get_bits(gbc, 2);
00096 if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
00097 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00098
00099 hdr->substreamid = get_bits(gbc, 3);
00100
00101 hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
00102 if(hdr->frame_size < AC3_HEADER_SIZE)
00103 return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
00104
00105 hdr->sr_code = get_bits(gbc, 2);
00106 if (hdr->sr_code == 3) {
00107 int sr_code2 = get_bits(gbc, 2);
00108 if(sr_code2 == 3)
00109 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
00110 hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
00111 hdr->sr_shift = 1;
00112 } else {
00113 hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
00114 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
00115 hdr->sr_shift = 0;
00116 }
00117
00118 hdr->channel_mode = get_bits(gbc, 3);
00119 hdr->lfe_on = get_bits1(gbc);
00120
00121 hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
00122 (hdr->num_blocks * 256.0));
00123 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
00124 }
00125 hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];
00126 if (hdr->lfe_on)
00127 hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
00128
00129 return 0;
00130 }
00131
00132 static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
00133 int *need_next_header, int *new_frame_start)
00134 {
00135 int err;
00136 union {
00137 uint64_t u64;
00138 uint8_t u8[8];
00139 } tmp = { av_be2ne64(state) };
00140 AC3HeaderInfo hdr;
00141 GetBitContext gbc;
00142
00143 init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
00144 err = ff_ac3_parse_header(&gbc, &hdr);
00145
00146 if(err < 0)
00147 return 0;
00148
00149 hdr_info->sample_rate = hdr.sample_rate;
00150 hdr_info->bit_rate = hdr.bit_rate;
00151 hdr_info->channels = hdr.channels;
00152 hdr_info->channel_layout = hdr.channel_layout;
00153 hdr_info->samples = hdr.num_blocks * 256;
00154 hdr_info->service_type = hdr.bitstream_mode;
00155 if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
00156 hdr_info->service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
00157 if(hdr.bitstream_id>10)
00158 hdr_info->codec_id = CODEC_ID_EAC3;
00159 else if (hdr_info->codec_id == CODEC_ID_NONE)
00160 hdr_info->codec_id = CODEC_ID_AC3;
00161
00162 *need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
00163 *new_frame_start = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
00164 return hdr.frame_size;
00165 }
00166
00167 static av_cold int ac3_parse_init(AVCodecParserContext *s1)
00168 {
00169 AACAC3ParseContext *s = s1->priv_data;
00170 s->header_size = AC3_HEADER_SIZE;
00171 s->sync = ac3_sync;
00172 return 0;
00173 }
00174
00175
00176 AVCodecParser ff_ac3_parser = {
00177 { CODEC_ID_AC3, CODEC_ID_EAC3 },
00178 sizeof(AACAC3ParseContext),
00179 ac3_parse_init,
00180 ff_aac_ac3_parse,
00181 ff_parse_close,
00182 };