]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/faad/aacaudio.c
wwww
[16.git] / src / lib / doslib / ext / faad / aacaudio.c
1 /* Castus/ISP AAC audio ADTS frame synchronization and detection */
2 /* (C) 2010-2012 Jonathan Campbell */
3
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9
10 #include "aacaudio.h"
11
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
15 };
16
17 static const unsigned char mpeg_aac_adts_audio_channels[8] = {
18     0, 1, 2, 3, 4, 5, 6, 8
19 };
20
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? */
37
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;
42         i->sync = sp;
43 }
44
45
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;
48
49         if ((ptr+7) > fence)
50                 return ptr;
51
52         memset(i,0,sizeof(*i));
53         while ((ptr+7) <= fence) {
54                 sp <<= 8ULL;
55                 sp  |= (mpeg_aac_adts_audio_sync_raw_pattern)(*ptr++);
56
57                 if (sp == (mpeg_aac_adts_audio_sync_raw_pattern)0xFFFFFFFFFFFFFFFFULL) {
58                         ptr += 6-1;
59                         continue;
60                 }
61                 if ((sp&0x00FFFF0000000000ULL) == 0x00FFFF0000000000ULL) {
62                         continue;
63                 }
64                 if ((sp&0x00FFF00000000000ULL) != 0x00FFF00000000000ULL) /* 12-bit sync pattern */
65                         continue;
66                 if (p) *p = sp;
67
68                 mpeg_aac_adts_audio_get_sync_info(sp,i);
69
70                 /* NTS: aac_frame_length includes the ADTS header */
71                 if (i->aac_frame_length < 7)
72                         continue;
73
74                 i->sample_rate = mpeg_aac_adts_audio_sample_rates[i->sample_rate_index];
75                 if (i->sample_rate == 0)
76                         continue;
77
78                 i->channels = mpeg_aac_adts_audio_channels[i->channel_configuration];
79                 if (i->channels == 0)
80                         continue;
81
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;
84                 i->sync = sp;
85                 return ptr-7;
86         }
87
88         return fence-7;
89 }
90
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);
97 }
98
99