2 * Copyright (C) 2006-2008 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 ssize_t aac_compute_chunk_table(struct afh_info *afhi,
46 unsigned char *map, size_t numbytes)
52 ret = aac_find_stsz(map, numbytes, &skip);
55 afhi->chunks_total = ret;
56 PARA_DEBUG_LOG("sz table has %lu entries\n", afhi->chunks_total);
57 afhi->chunk_table = para_malloc((afhi->chunks_total + 1) * sizeof(size_t));
58 for (i = 1; i <= afhi->chunks_total; i++) {
59 if (skip + 4 > numbytes)
61 sum += aac_read_int32(map + skip);
62 afhi->chunk_table[i] = sum;
64 // if (i < 10 || i + 10 > afhi->chunks_total)
65 // PARA_DEBUG_LOG("offset #%d: %zu\n", i, afhi->chunk_table[i]);
70 static int aac_set_chunk_tv(struct afh_info *afhi,
71 mp4AudioSpecificConfig *mp4ASC, long unsigned *seconds)
73 float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
75 long unsigned ms = 1000.0 * afhi->chunks_total * tmp
76 / mp4ASC->samplingFrequency;
78 if (!mp4ASC->samplingFrequency)
80 ms = 1000.0 * afhi->chunks_total * tmp / mp4ASC->samplingFrequency;
82 tv_divide(afhi->chunks_total, &total, &afhi->chunk_tv);
83 PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
84 mp4ASC->samplingFrequency, ms / 1000,
85 afhi->chunks_total, tv2ms(&afhi->chunk_tv));
93 * Init m4a file and write some tech data to given pointers.
95 static int aac_get_file_info(char *map, size_t numbytes,
96 struct afh_info *afhi)
101 unsigned long rate = 0, decoder_len;
102 unsigned char channels = 0;
103 mp4AudioSpecificConfig mp4ASC;
104 NeAACDecHandle handle = NULL;
105 unsigned char *umap = (unsigned char *) map;
107 ret = aac_find_esds(umap, numbytes, &skip, &decoder_len);
111 ret = -E_AAC_AFH_INIT;
112 if (NeAACDecInit(handle, umap + skip, decoder_len, &rate, &channels))
116 PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate, channels);
118 if (NeAACDecAudioSpecificConfig(umap + skip, numbytes - skip, &mp4ASC))
120 if (!mp4ASC.samplingFrequency)
122 ret = aac_compute_chunk_table(afhi, umap, numbytes);
126 ret = aac_set_chunk_tv(afhi, &mp4ASC, &afhi->seconds_total);
129 ret = aac_find_entry_point(umap + skip, numbytes - skip, &skip);
132 afhi->chunk_table[0] = ret;
133 for (i = 1; i<= afhi->chunks_total; i++)
134 afhi->chunk_table[i] += ret;
135 afhi->channels = channels;
136 afhi->frequency = rate;
137 ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
138 ret += (channels * afhi->seconds_total * 500); /* avoid rounding error */
139 afhi->bitrate = ret / (channels * afhi->seconds_total * 1000);
140 sprintf(afhi->info_string, "%s:\n%s:\n%s:\n",
141 status_item_list[SI_AUDIO_FILE_INFO],
142 status_item_list[SI_TAGINFO1],
143 status_item_list[SI_TAGINFO2]
145 tv_scale(20, &afhi->chunk_tv, &afhi->eof_tv);
149 NeAACDecClose(handle);
153 static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
155 * the init function of the aac audio format handler
157 * \param afh pointer to the struct to initialize
159 void aac_afh_init(struct audio_format_handler *afh)
161 afh->get_file_info = aac_get_file_info,
162 afh->suffixes = aac_suffixes;