2 * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
11 /** \file aac_afh.c para_server's aac audio format handler */
21 static int aac_find_stsz(unsigned char *buf
, size_t buflen
, off_t
*skip
)
25 for (i
= 0; i
+ 16 < buflen
; i
++) {
26 unsigned char *p
= buf
+ i
;
27 unsigned sample_count
, sample_size
;
29 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 's' || p
[3] != 'z')
31 PARA_DEBUG_LOG("found stsz@%d\n", i
);
33 sample_size
= aac_read_int32(buf
+ i
);
34 PARA_DEBUG_LOG("sample size: %d\n", sample_size
);
36 sample_count
= aac_read_int32(buf
+ i
);
38 PARA_DEBUG_LOG("sample count: %d\n", sample_count
);
45 static int atom_cmp(const unsigned char *buf1
, const char *buf2
)
47 const unsigned char *b2
= (unsigned char *)buf2
;
60 static int read_atom_header(unsigned char *buf
, uint64_t *subsize
, unsigned char type
[5])
63 uint64_t size
= (buf
[0] << 24) + (buf
[1] << 16) + (buf
[2] << 8) + buf
[3];
65 memcpy(type
, buf
+ 4, 4);
68 PARA_DEBUG_LOG("size: %llu, type: %s\n", (long long unsigned)size
, type
);
75 for (i
= 0; i
< 8; i
++)
76 size
|= ((uint64_t)buf
[i
]) << ((7 - i
) * 8);
81 static char *get_tag(unsigned char *p
, int size
)
86 buf
= para_malloc(size
+ 1);
90 PARA_DEBUG_LOG("size: %d: %s\n", size
, buf
);
94 static void read_tags(unsigned char *buf
, size_t buflen
, struct afh_info
*afhi
)
96 unsigned char *p
= buf
;
98 while (p
+ 32 < buf
+ buflen
) {
99 unsigned char *q
, type1
[5], type2
[5];
100 uint64_t size1
, size2
;
103 ret
= read_atom_header(p
, &size1
, type1
);
104 ret2
= read_atom_header(p
+ ret
, &size2
, type2
);
106 if (size2
<= 16 || atom_cmp(type2
, "data")) {
111 q
= p
+ ret
+ ret2
+ 8;
112 if (q
+ size2
> buf
+ buflen
)
114 if (!atom_cmp(type1
, "©ART"))
115 afhi
->tags
.artist
= get_tag(q
, size2
);
116 else if (!atom_cmp(type1
, "©alb"))
117 afhi
->tags
.album
= get_tag(q
, size2
);
118 else if (!atom_cmp(type1
, "©nam"))
119 afhi
->tags
.title
= get_tag(q
, size2
);
120 else if (!atom_cmp(type1
, "©cmt"))
121 afhi
->tags
.comment
= get_tag(q
, size2
);
122 else if (!atom_cmp(type1
, "©day"))
123 afhi
->tags
.year
= get_tag(q
, size2
);
128 static void read_meta(unsigned char *buf
, size_t buflen
, struct afh_info
*afhi
)
130 unsigned char *p
= buf
;
132 while (p
+ 4 < buf
+ buflen
) {
134 if (p
[0] != 'i' || p
[1] != 'l' || p
[2] != 's' || p
[3] != 't') {
139 return read_tags(p
, buflen
- (p
- buf
), afhi
);
143 static void aac_get_taginfo(unsigned char *buf
, size_t buflen
,
144 struct afh_info
*afhi
)
148 unsigned char type
[5];
150 for (i
= 0; i
+ 24 < buflen
; i
++) {
151 unsigned char *p
= buf
+ i
;
152 if (p
[0] != 'm' || p
[1] != 'e' || p
[2] != 't' || p
[3] != 'a')
154 PARA_INFO_LOG("found metadata at offset %d\n", i
);
157 i
+= read_atom_header(p
, &subsize
, type
);
159 return read_meta(p
, buflen
- i
, afhi
);
161 PARA_INFO_LOG("no meta data\n");
164 static ssize_t
aac_compute_chunk_table(struct afh_info
*afhi
,
165 unsigned char *map
, size_t numbytes
)
171 ret
= aac_find_stsz(map
, numbytes
, &skip
);
174 afhi
->chunks_total
= ret
;
175 PARA_DEBUG_LOG("sz table has %lu entries\n", afhi
->chunks_total
);
176 afhi
->chunk_table
= para_malloc((afhi
->chunks_total
+ 1) * sizeof(size_t));
177 for (i
= 1; i
<= afhi
->chunks_total
; i
++) {
178 if (skip
+ 4 > numbytes
)
180 sum
+= aac_read_int32(map
+ skip
);
181 afhi
->chunk_table
[i
] = sum
;
183 // if (i < 10 || i + 10 > afhi->chunks_total)
184 // PARA_DEBUG_LOG("offset #%d: %zu\n", i, afhi->chunk_table[i]);
189 static int aac_set_chunk_tv(struct afh_info
*afhi
,
190 mp4AudioSpecificConfig
*mp4ASC
, long unsigned *seconds
)
192 float tmp
= mp4ASC
->sbr_present_flag
== 1? 2047 : 1023;
193 struct timeval total
;
194 long unsigned ms
= 1000.0 * afhi
->chunks_total
* tmp
195 / mp4ASC
->samplingFrequency
;
197 if (!mp4ASC
->samplingFrequency
)
199 ms
= 1000.0 * afhi
->chunks_total
* tmp
/ mp4ASC
->samplingFrequency
;
201 tv_divide(afhi
->chunks_total
, &total
, &afhi
->chunk_tv
);
202 PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
203 mp4ASC
->samplingFrequency
, ms
/ 1000,
204 afhi
->chunks_total
, tv2ms(&afhi
->chunk_tv
));
207 *seconds
= ms
/ 1000;
212 * Init m4a file and write some tech data to given pointers.
214 static int aac_get_file_info(char *map
, size_t numbytes
, __a_unused
int fd
,
215 struct afh_info
*afhi
)
220 unsigned long rate
= 0, decoder_len
;
221 unsigned char channels
= 0;
222 mp4AudioSpecificConfig mp4ASC
;
223 NeAACDecHandle handle
= NULL
;
224 unsigned char *umap
= (unsigned char *) map
;
226 ret
= aac_find_esds(umap
, numbytes
, &skip
, &decoder_len
);
229 aac_get_taginfo(umap
, numbytes
, afhi
);
231 ret
= -E_AAC_AFH_INIT
;
232 if (NeAACDecInit(handle
, umap
+ skip
, decoder_len
, &rate
, &channels
))
236 PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate
, channels
);
238 if (NeAACDecAudioSpecificConfig(umap
+ skip
, numbytes
- skip
, &mp4ASC
))
240 if (!mp4ASC
.samplingFrequency
)
242 ret
= aac_compute_chunk_table(afhi
, umap
, numbytes
);
246 ret
= aac_set_chunk_tv(afhi
, &mp4ASC
, &afhi
->seconds_total
);
249 ret
= aac_find_entry_point(umap
+ skip
, numbytes
- skip
, &skip
);
252 afhi
->chunk_table
[0] = ret
;
253 for (i
= 1; i
<= afhi
->chunks_total
; i
++)
254 afhi
->chunk_table
[i
] += ret
;
255 afhi
->channels
= channels
;
256 afhi
->frequency
= rate
;
257 ret
= (afhi
->chunk_table
[afhi
->chunks_total
] - afhi
->chunk_table
[0]) * 8; /* bits */
258 ret
+= (channels
* afhi
->seconds_total
* 500); /* avoid rounding error */
259 afhi
->bitrate
= ret
/ (channels
* afhi
->seconds_total
* 1000);
263 NeAACDecClose(handle
);
267 static const char* aac_suffixes
[] = {"m4a", "mp4", NULL
};
269 * the init function of the aac audio format handler
271 * \param afh pointer to the struct to initialize
273 void aac_afh_init(struct audio_format_handler
*afh
)
275 afh
->get_file_info
= aac_get_file_info
,
276 afh
->suffixes
= aac_suffixes
;