simplify definition of sqrthalf and capitalize it.
[paraslash.git] / afh_common.c
1 /*
2 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file afh_common.c Common audio format handler functions. */
8
9 #include <sys/mman.h> /* mmap */
10 #include <sys/time.h> /* gettimeofday */
11 #include <sys/types.h>
12 #include <dirent.h>
13 #include <regex.h>
14
15 #include "para.h"
16 #include "error.h"
17 #include "string.h"
18 #include "afh.h"
19
20 /* The mp3 audio format handler does not need any libs. */
21 void mp3_init(struct audio_format_handler *);
22
23 #ifdef HAVE_OGGVORBIS
24 void ogg_init(struct audio_format_handler *);
25 #endif
26 #ifdef HAVE_FAAD
27 void aac_afh_init(struct audio_format_handler *);
28 #endif
29
30 void wma_afh_init(struct audio_format_handler *);
31 /**
32 * The list of supported audio formats.
33 *
34 * We always define the full array of audio formats even if some audio formats
35 * were not compiled in. This is because for each audio file the number of its
36 * audio format is stored in the database. We don't want that numbers to become
37 * stale just because the user installed a new version of paraslash that
38 * supports a different set of audio formats.
39 *
40 * It can still be easily detected whether an audio format is compiled in by
41 * checking if the init function pointer is not \p NULL.
42 */
43 static struct audio_format_handler afl[] = {
44 {
45 .name = "mp3",
46 .init = mp3_init,
47 },
48 {
49 .name = "ogg",
50 #ifdef HAVE_OGGVORBIS
51 .init = ogg_init,
52 #endif
53 },
54 {
55 .name = "aac",
56 #ifdef HAVE_FAAD
57 .init = aac_afh_init,
58 #endif
59 },
60 {
61 .name = "wma",
62 .init = wma_afh_init,
63 },
64 {
65 .name = NULL,
66 }
67 };
68
69 static inline int next_audio_format(int format)
70 {
71 for (;;) {
72 if (!afl[format].name)
73 return format;
74 format++;
75 if (afl[format].init)
76 return format;
77 }
78
79 }
80
81 /** Iterate over each supported audio format. */
82 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
83
84 /**
85 * Call the init function of each supported audio format handler.
86 */
87 void afh_init(void)
88 {
89 int i;
90
91 PARA_INFO_LOG("supported audio formats: %s\n",
92 SUPPORTED_AUDIO_FORMATS);
93 FOR_EACH_AUDIO_FORMAT(i) {
94 PARA_NOTICE_LOG("initializing %s handler\n",
95 audio_format_name(i));
96 afl[i].init(&afl[i]);
97 }
98 }
99
100 /**
101 * Guess the audio format judging from filename.
102 *
103 * \param name The filename.
104 *
105 * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
106 * of audio file this might be. Otherwise the (non-negative) number of the
107 * audio format is returned.
108 */
109 int guess_audio_format(const char *name)
110 {
111 int i,j, len = strlen(name);
112
113 FOR_EACH_AUDIO_FORMAT(i) {
114 for (j = 0; afl[i].suffixes[j]; j++) {
115 const char *p = afl[i].suffixes[j];
116 int plen = strlen(p);
117 if (len < plen + 1)
118 continue;
119 if (name[len - plen - 1] != '.')
120 continue;
121 if (strcasecmp(name + len - plen, p))
122 continue;
123 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
124 return i;
125 }
126 }
127 return -E_AUDIO_FORMAT;
128 }
129
130 /**
131 * Call get_file_info() to obtain an afhi structure.
132 *
133 * \param path The full path of the audio file.
134 * \param data Pointer to the contents of the (mapped) file.
135 * \param size The file size in bytes.
136 * \param fd The open file descriptor.
137 * \param afhi Result pointer.
138 *
139 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
140 * compiled in audio format handler is able to handler the file.
141 *
142 * This function tries to find an audio format handler that can interpret the
143 * file given by \a data and \a size.
144 *
145 * It first tries to determine the audio format from the filename given by \a
146 * path. If this doesn't work, all other audio format handlers are tried until
147 * one is found that can handle the file.
148 */
149 int compute_afhi(const char *path, char *data, size_t size, int fd,
150 struct afh_info *afhi)
151 {
152 int ret, i, format;
153
154 afhi->header_offset = 0;
155 afhi->header_len = 0;
156 afhi->techinfo = NULL;
157 afhi->tags.artist = NULL;
158 afhi->tags.title = NULL;
159 afhi->tags.year = NULL;
160 afhi->tags.album = NULL;
161 afhi->tags.comment = NULL;
162 format = guess_audio_format(path);
163
164 if (format >= 0) {
165 ret = afl[format].get_file_info(data, size, fd, afhi);
166 if (ret >= 0) {
167 ret = format;
168 goto success;
169 }
170 }
171 FOR_EACH_AUDIO_FORMAT(i) {
172 if (i == format) /* we already tried this one to no avail */
173 continue;
174 ret = afl[i].get_file_info(data, size, fd, afhi);
175 if (ret >= 0) {
176 ret = i;
177 goto success;
178 }
179 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
180 }
181 return -E_AUDIO_FORMAT;
182 success:
183 if (!afhi->techinfo)
184 afhi->techinfo = para_strdup(NULL);
185 if (!afhi->tags.artist)
186 afhi->tags.artist = para_strdup(NULL);
187 if (!afhi->tags.title)
188 afhi->tags.title = para_strdup(NULL);
189 if (!afhi->tags.year)
190 afhi->tags.year = para_strdup(NULL);
191 if (!afhi->tags.album)
192 afhi->tags.album = para_strdup(NULL);
193 if (!afhi->tags.comment)
194 afhi->tags.comment = para_strdup(NULL);
195 PARA_DEBUG_LOG("techinfo: %s\n", afhi->techinfo);
196 PARA_DEBUG_LOG("artist: %s\n", afhi->tags.artist);
197 PARA_DEBUG_LOG("title: %s\n", afhi->tags.title);
198 PARA_DEBUG_LOG("year: %s\n", afhi->tags.year);
199 PARA_DEBUG_LOG("album: %s\n", afhi->tags.album);
200 PARA_DEBUG_LOG("comment: %s\n", afhi->tags.comment);
201 return ret;
202 }
203
204 /**
205 * Get the name of the given audio format.
206 *
207 * \param i The audio format number.
208 *
209 * This returns a pointer to statically allocated memory so it
210 * must not be freed by the caller.
211 */
212 const char *audio_format_name(int i)
213 {
214 if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
215 return "???";
216 return afl[i].name;
217 }
218
219 /**
220 * Get one chunk of audio data.
221 *
222 * \param chunk_num The number of the chunk to get.
223 * \param afhi Describes the audio file.
224 * \param map The memory mapped audio file.
225 * \param buf Result pointer.
226 * \param len The length of the chunk in bytes.
227 *
228 * Upon return, \a buf will point so memory inside \a map. The returned buffer
229 * must therefore not be freed by the caller.
230 */
231 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
232 void *map, const char **buf, size_t *len)
233 {
234 size_t pos = afhi->chunk_table[chunk_num];
235 *buf = map + pos;
236 *len = afhi->chunk_table[chunk_num + 1] - pos;
237 }
238
239 uint32_t afh_get_largest_chunk_size(struct afh_info *afhi)
240 {
241 uint32_t n, largest = 0, *ct = afhi->chunk_table;
242
243 for (n = 1; n <= afhi->chunks_total; n++)
244 largest = PARA_MAX(largest, ct[n] - ct[n - 1]);
245 return largest;
246 }
247
248 /**
249 * Get the header of an audio file.
250 *
251 * \param afhi The audio file handler data describing the file.
252 * \param map The data of the audio file.
253 * \param buf The length of the header is stored here.
254 * \param len Points to a buffer containing the header on return.
255 *
256 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
257 * afhi is \p NULL, or if the current audio format does not need special
258 * header treatment.
259 */
260 void afh_get_header(struct afh_info *afhi, void *map, const char **buf, size_t *len)
261 {
262 if (!map || !afhi || !afhi->header_len) {
263 *buf = NULL;
264 *len = 0;
265 return;
266 }
267 *len = afhi->header_len;
268 *buf = map + afhi->header_offset;
269 }