d0d437e90a31bade448b3a31165bf82dfe15d2ea
[paraslash.git] / aac_afh.c
1 /*
2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /*
7 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
8 * Ahead Software AG
9 */
10
11 /** \file aac_afh.c para_server's aac audio format handler */
12
13 #include "para.h"
14 #include "afh.h"
15 #include "server.h"
16 #include "error.h"
17 #include "string.h"
18 #include "aac.h"
19
20 static int aac_find_stsz(unsigned char *buf, size_t buflen, off_t *skip)
21 {
22 int i;
23
24 for (i = 0; i + 16 < buflen; i++) {
25 unsigned char *p = buf + i;
26 unsigned sample_count, sample_size;
27
28 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
29 continue;
30 PARA_DEBUG_LOG("found stsz@%d\n", i);
31 i += 8;
32 sample_size = aac_read_int32(buf + i);
33 PARA_DEBUG_LOG("sample size: %d\n", sample_size);
34 i += 4;
35 sample_count = aac_read_int32(buf + i);
36 i += 4;
37 PARA_DEBUG_LOG("sample count: %d\n", sample_count);
38 *skip = i;
39 return sample_count;
40 }
41 return -E_STSZ;
42 }
43
44 static ssize_t aac_compute_chunk_table(struct audio_format_info *afi,
45 unsigned char *map, size_t numbytes)
46 {
47 int ret, i;
48 size_t sum = 0;
49 off_t skip;
50
51 ret = aac_find_stsz(map, numbytes, &skip);
52 if (ret < 0)
53 return ret;
54 afi->chunks_total = ret;
55 PARA_DEBUG_LOG("sz table has %lu entries\n", afi->chunks_total);
56 afi->chunk_table = para_malloc((afi->chunks_total + 1) * sizeof(size_t));
57 for (i = 1; i <= afi->chunks_total; i++) {
58 if (skip + 4 > numbytes)
59 break;
60 sum += aac_read_int32(map + skip);
61 afi->chunk_table[i] = sum;
62 skip += 4;
63 // if (i < 10 || i + 10 > afi->chunks_total)
64 // PARA_DEBUG_LOG("offset #%d: %zu\n", i, afi->chunk_table[i]);
65 }
66 return skip;
67 }
68
69 static int aac_set_chunk_tv(struct audio_format_info *afi,
70 mp4AudioSpecificConfig *mp4ASC, long unsigned *seconds)
71 {
72 float tmp = mp4ASC->sbr_present_flag == 1? 2047 : 1023;
73 struct timeval total;
74 long unsigned ms = 1000.0 * afi->chunks_total * tmp
75 / mp4ASC->samplingFrequency;
76
77 if (!mp4ASC->samplingFrequency)
78 return -E_MP4ASC;
79 ms = 1000.0 * afi->chunks_total * tmp / mp4ASC->samplingFrequency;
80 ms2tv(ms, &total);
81 tv_divide(afi->chunks_total, &total, &afi->chunk_tv);
82 PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
83 mp4ASC->samplingFrequency, ms / 1000,
84 afi->chunks_total, tv2ms(&afi->chunk_tv));
85 if (ms < 1000)
86 return -E_MP4ASC;
87 *seconds = ms / 1000;
88 return 1;
89 }
90
91 /*
92 * Init m4a file and write some tech data to given pointers.
93 */
94 static int aac_get_file_info(char *map, size_t numbytes,
95 struct audio_format_info *afi)
96 {
97 int i;
98 size_t skip;
99 ssize_t ret;
100 unsigned long rate = 0, decoder_len;
101 unsigned char channels = 0;
102 mp4AudioSpecificConfig mp4ASC;
103 NeAACDecHandle handle = NULL;
104 unsigned char *umap = (unsigned char *) map;
105
106 ret = aac_find_esds(umap, numbytes, &skip, &decoder_len);
107 if (ret < 0)
108 goto out;
109 handle = aac_open();
110 ret = -E_AAC_AFH_INIT;
111 if (NeAACDecInit(handle, umap + skip, decoder_len, &rate, &channels))
112 goto out;
113 if (!channels)
114 goto out;
115 PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate, channels);
116 ret = -E_MP4ASC;
117 if (NeAACDecAudioSpecificConfig(umap + skip, numbytes - skip, &mp4ASC))
118 goto out;
119 if (!mp4ASC.samplingFrequency)
120 goto out;
121 ret = aac_compute_chunk_table(afi, umap, numbytes);
122 if (ret < 0)
123 goto out;
124 skip = ret;
125 ret = aac_set_chunk_tv(afi, &mp4ASC, &afi->seconds_total);
126 if (ret < 0)
127 goto out;
128 ret = aac_find_entry_point(umap + skip, numbytes - skip, &skip);
129 if (ret < 0)
130 goto out;
131 afi->chunk_table[0] = ret;
132 for (i = 1; i<= afi->chunks_total; i++)
133 afi->chunk_table[i] += ret;
134 afi->channels = channels;
135 afi->frequency = rate;
136 ret = (afi->chunk_table[afi->chunks_total] - afi->chunk_table[0]) * 8; /* bits */
137 ret += (channels * afi->seconds_total * 500); /* avoid rounding error */
138 afi->bitrate = ret / (channels * afi->seconds_total * 1000);
139 sprintf(afi->info_string, "audio_file_info1:%lu x %lums, "
140 "%uHz, %d channel%s, %ukb/s\n"
141 "audio_file_info2:\n"
142 "audio_file_info3:\n",
143 afi->chunks_total, tv2ms(&afi->chunk_tv),
144 afi->frequency, channels, channels == 1? "" : "s", afi->bitrate
145 );
146 tv_scale(20, &afi->chunk_tv, &afi->eof_tv);
147 ret = 1;
148 out:
149 if (handle)
150 NeAACDecClose(handle);
151 return ret;
152 }
153
154 static const char* aac_suffixes[] = {"m4a", "mp4", NULL};
155 /**
156 * the init function of the aac audio format handler
157 *
158 * \param afh pointer to the struct to initialize
159 */
160 void aac_afh_init(struct audio_format_handler *afh)
161 {
162 afh->get_file_info = aac_get_file_info,
163 afh->suffixes = aac_suffixes;
164 }