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