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