1 /* Castus/ISP AAC audio ADTS frame synchronization and detection */
2 /* (C) 2010-2012 Jonathan Campbell */
12 static const unsigned int mpeg_aac_adts_audio_sample_rates[16] = {
13 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
14 16000, 12000, 11025, 8000, 7350, 0, 0, 0
17 static const unsigned char mpeg_aac_adts_audio_channels[8] = {
18 0, 1, 2, 3, 4, 5, 6, 8
21 void mpeg_aac_adts_audio_get_sync_info(mpeg_aac_adts_audio_sync_raw_pattern sp,mpeg_aac_adts_audio_sync_info *i) {
22 i->id = (unsigned char)((sp >> (56ULL-(12ULL+ 1ULL))) & 0x0001);
23 i->layer = (unsigned char)((sp >> (56ULL-(13ULL+ 2ULL))) & 0x0003);
24 i->protection = (unsigned char)((sp >> (56ULL-(15ULL+ 1ULL))) & 0x0001);
25 i->object_type = (unsigned char)((sp >> (56ULL-(16ULL+ 2ULL))) & 0x0003);
26 i->sample_rate_index = (unsigned char)((sp >> (56ULL-(18ULL+ 4ULL))) & 0x000F);
27 i->private_bit = (unsigned char)((sp >> (56ULL-(22ULL+ 1ULL))) & 0x0001);
28 i->channel_configuration = (unsigned char)((sp >> (56ULL-(23ULL+ 3ULL))) & 0x0007);
29 i->original_copy = (unsigned char)((sp >> (56ULL-(26ULL+ 1ULL))) & 0x0001);
30 i->home = (unsigned char)((sp >> (56ULL-(27ULL+ 1ULL))) & 0x0001);
31 i->copyright_id = (unsigned char)((sp >> (56ULL-(28ULL+ 1ULL))) & 0x0001);
32 i->copyright_start = (unsigned char)((sp >> (56ULL-(29ULL+ 1ULL))) & 0x0001);
33 i->aac_frame_length = (unsigned short)((sp >> (56ULL-(30ULL+13ULL))) & 0x1FFF);
34 i->buffer_fullness = (unsigned short)((sp >> (56ULL-(43ULL+11ULL))) & 0x07FF);
35 i->add_raw_data_blocks = (unsigned char)((sp >> (56ULL-(54ULL+ 2ULL))) & 0x0003);
36 i->samples_per_frame = 1024; /* FIXME: Digital Radio Mondale has 960 samples/frame? Do we need to support it? */
38 i->sample_rate = mpeg_aac_adts_audio_sample_rates[i->sample_rate_index];
39 i->channels = mpeg_aac_adts_audio_channels[i->channel_configuration];
40 i->total_raw_data_blocks = i->add_raw_data_blocks+1;
41 i->total_samples = i->samples_per_frame * i->total_raw_data_blocks;
46 uint8_t *mpeg_aac_adts_audio_sync(uint8_t *ptr,uint8_t *fence,mpeg_aac_adts_audio_sync_raw_pattern *p,mpeg_aac_adts_audio_sync_info *i) {
47 mpeg_aac_adts_audio_sync_raw_pattern sp = 0;
52 memset(i,0,sizeof(*i));
53 while ((ptr+7) <= fence) {
55 sp |= (mpeg_aac_adts_audio_sync_raw_pattern)(*ptr++);
57 if (sp == (mpeg_aac_adts_audio_sync_raw_pattern)0xFFFFFFFFFFFFFFFFULL) {
61 if ((sp&0x00FFFF0000000000ULL) == 0x00FFFF0000000000ULL) {
64 if ((sp&0x00FFF00000000000ULL) != 0x00FFF00000000000ULL) /* 12-bit sync pattern */
68 mpeg_aac_adts_audio_get_sync_info(sp,i);
70 /* NTS: aac_frame_length includes the ADTS header */
71 if (i->aac_frame_length < 7)
74 i->sample_rate = mpeg_aac_adts_audio_sample_rates[i->sample_rate_index];
75 if (i->sample_rate == 0)
78 i->channels = mpeg_aac_adts_audio_channels[i->channel_configuration];
82 i->total_raw_data_blocks = i->add_raw_data_blocks+1;
83 i->total_samples = i->samples_per_frame * i->total_raw_data_blocks;
91 int mpeg_aac_adts_audio_sync_same_format(mpeg_aac_adts_audio_sync_info *a,mpeg_aac_adts_audio_sync_info *b) {
92 return (a->id == b->id &&
93 a->layer == b->layer &&
94 a->object_type == b->object_type &&
95 a->sample_rate_index == b->sample_rate_index &&
96 a->channel_configuration == b->channel_configuration);