6c161a7c7ea2a30c0abb6a87c91ef5075d5dbf06
[paraslash.git] / afh_common.c
1 /*
2 * Copyright (C) 1997-2013 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/types.h>
11 #include <regex.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "string.h"
16 #include "afh.h"
17
18 /* The mp3 audio format handler does not need any libs. */
19 void mp3_init(struct audio_format_handler *);
20
21 #ifdef HAVE_OGGVORBIS
22 void ogg_init(struct audio_format_handler *);
23 #endif
24 #ifdef HAVE_FAAD
25 void aac_afh_init(struct audio_format_handler *);
26 #endif
27 #ifdef HAVE_SPEEX
28 void spx_afh_init(struct audio_format_handler *);
29 #endif
30 #ifdef HAVE_FLAC
31 void flac_afh_init(struct audio_format_handler *);
32 #endif
33
34 void wma_afh_init(struct audio_format_handler *);
35
36 /** The list of all status items */
37 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
38
39 /**
40 * The list of supported audio formats.
41 *
42 * We always define the full array of audio formats even if some audio formats
43 * were not compiled in. This is because for each audio file the number of its
44 * audio format is stored in the database. We don't want that numbers to become
45 * stale just because the user installed a new version of paraslash that
46 * supports a different set of audio formats.
47 *
48 * It can still be easily detected whether an audio format is compiled in by
49 * checking if the init function pointer is not \p NULL.
50 */
51 static struct audio_format_handler afl[] = {
52 {
53 .name = "mp3",
54 .init = mp3_init,
55 },
56 {
57 .name = "ogg",
58 #ifdef HAVE_OGGVORBIS
59 .init = ogg_init,
60 #endif
61 },
62 {
63 .name = "aac",
64 #ifdef HAVE_FAAD
65 .init = aac_afh_init,
66 #endif
67 },
68 {
69 .name = "wma",
70 .init = wma_afh_init,
71 },
72 {
73 .name = "spx",
74 #ifdef HAVE_SPEEX
75 .init = spx_afh_init,
76 #endif
77 },
78 {
79 .name = "flac",
80 #ifdef HAVE_FLAC
81 .init = flac_afh_init,
82 #endif
83 },
84 {
85 .name = NULL,
86 }
87 };
88
89 static inline int next_audio_format(int format)
90 {
91 for (;;) {
92 if (!afl[format].name)
93 return format;
94 format++;
95 if (afl[format].init)
96 return format;
97 }
98
99 }
100
101 /** Iterate over each supported audio format. */
102 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
103
104 /**
105 * Call the init function of each supported audio format handler.
106 */
107 void afh_init(void)
108 {
109 int i;
110
111 PARA_INFO_LOG("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
112 FOR_EACH_AUDIO_FORMAT(i) {
113 PARA_NOTICE_LOG("initializing %s handler\n",
114 audio_format_name(i));
115 afl[i].init(&afl[i]);
116 }
117 }
118
119 /**
120 * Guess the audio format judging from filename.
121 *
122 * \param name The filename.
123 *
124 * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
125 * of audio file this might be. Otherwise the (non-negative) number of the
126 * audio format is returned.
127 */
128 int guess_audio_format(const char *name)
129 {
130 int i,j, len = strlen(name);
131
132 FOR_EACH_AUDIO_FORMAT(i) {
133 for (j = 0; afl[i].suffixes[j]; j++) {
134 const char *p = afl[i].suffixes[j];
135 int plen = strlen(p);
136 if (len < plen + 1)
137 continue;
138 if (name[len - plen - 1] != '.')
139 continue;
140 if (strcasecmp(name + len - plen, p))
141 continue;
142 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
143 return i;
144 }
145 }
146 return -E_AUDIO_FORMAT;
147 }
148
149 /**
150 * Call get_file_info() to obtain an afhi structure.
151 *
152 * \param path The full path of the audio file.
153 * \param data Pointer to the contents of the (mapped) file.
154 * \param size The file size in bytes.
155 * \param fd The open file descriptor.
156 * \param afhi Result pointer.
157 *
158 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
159 * compiled in audio format handler is able to handler the file.
160 *
161 * This function tries to find an audio format handler that can interpret the
162 * file given by \a data and \a size.
163 *
164 * It first tries to determine the audio format from the filename given by \a
165 * path. If this doesn't work, all other audio format handlers are tried until
166 * one is found that can handle the file.
167 */
168 int compute_afhi(const char *path, char *data, size_t size, int fd,
169 struct afh_info *afhi)
170 {
171 int ret, i, format;
172
173 afhi->header_len = 0;
174 afhi->techinfo = NULL;
175 afhi->tags.artist = NULL;
176 afhi->tags.title = NULL;
177 afhi->tags.year = NULL;
178 afhi->tags.album = NULL;
179 afhi->tags.comment = NULL;
180 format = guess_audio_format(path);
181
182 if (format >= 0) {
183 ret = afl[format].get_file_info(data, size, fd, afhi);
184 if (ret >= 0) {
185 ret = format;
186 goto success;
187 }
188 }
189 FOR_EACH_AUDIO_FORMAT(i) {
190 if (i == format) /* we already tried this one to no avail */
191 continue;
192 ret = afl[i].get_file_info(data, size, fd, afhi);
193 if (ret >= 0) {
194 ret = i;
195 goto success;
196 }
197 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
198 }
199 return -E_AUDIO_FORMAT;
200 success:
201 if (!afhi->techinfo)
202 afhi->techinfo = para_strdup(NULL);
203 if (!afhi->tags.artist)
204 afhi->tags.artist = para_strdup(NULL);
205 if (!afhi->tags.title)
206 afhi->tags.title = para_strdup(NULL);
207 if (!afhi->tags.year)
208 afhi->tags.year = para_strdup(NULL);
209 if (!afhi->tags.album)
210 afhi->tags.album = para_strdup(NULL);
211 if (!afhi->tags.comment)
212 afhi->tags.comment = para_strdup(NULL);
213 PARA_DEBUG_LOG("techinfo: %s\n", afhi->techinfo);
214 PARA_DEBUG_LOG("artist: %s\n", afhi->tags.artist);
215 PARA_DEBUG_LOG("title: %s\n", afhi->tags.title);
216 PARA_DEBUG_LOG("year: %s\n", afhi->tags.year);
217 PARA_DEBUG_LOG("album: %s\n", afhi->tags.album);
218 PARA_DEBUG_LOG("comment: %s\n", afhi->tags.comment);
219 return ret;
220 }
221
222 /**
223 * Deallocate contents of a filled-in ahi structure
224 *
225 * \param afhi The structure to clear.
226 *
227 * The given pointer is kept, everything else is freed.
228 */
229 void clear_afhi(struct afh_info *afhi)
230 {
231 if (!afhi)
232 return;
233 free(afhi->chunk_table);
234 free(afhi->techinfo);
235 free(afhi->tags.artist);
236 free(afhi->tags.title);
237 free(afhi->tags.year);
238 free(afhi->tags.album);
239 free(afhi->tags.comment);
240 }
241
242 /**
243 * Get the name of the given audio format.
244 *
245 * \param i The audio format number.
246 *
247 * \return This returns a pointer to statically allocated memory so it
248 * must not be freed by the caller.
249 */
250 const char *audio_format_name(int i)
251 {
252 if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
253 return "???";
254 return afl[i].name;
255 }
256
257 /**
258 * Get one chunk of audio data.
259 *
260 * \param chunk_num The number of the chunk to get.
261 * \param afhi Describes the audio file.
262 * \param map The memory mapped audio file.
263 * \param buf Result pointer.
264 * \param len The length of the chunk in bytes.
265 *
266 * Upon return, \a buf will point so memory inside \a map. The returned buffer
267 * must therefore not be freed by the caller.
268 */
269 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
270 void *map, const char **buf, size_t *len)
271 {
272 size_t pos = afhi->chunk_table[chunk_num];
273 *buf = map + pos;
274 *len = afhi->chunk_table[chunk_num + 1] - pos;
275 }
276
277 /**
278 * Get the header of an audio file.
279 *
280 * \param afhi The audio file handler data describing the file.
281 * \param audio_format_id Determines the audio format handler.
282 * \param map The data of the audio file.
283 * \param mapsize The amount of bytes of the mmapped audio file.
284 * \param buf The length of the header is stored here.
285 * \param len Points to a buffer containing the header on return.
286 *
287 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
288 * afhi is \p NULL, or if the current audio format does not need special
289 * header treatment.
290 *
291 * Otherwise, it is checked whether the audio format handler given by
292 * \a audio_format_id defines a ->get_header() method. If it does, this
293 * method is called to obtain the header. If ->get_header() is \p NULL,
294 * a reference to the first chunk of the audio file is returned.
295 *
296 * Once the header is no longer needed, the caller must call \ref
297 * afh_free_header() to free the resources allocated by this function.
298 */
299 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
300 void *map, size_t mapsize, char **buf, size_t *len)
301 {
302 struct audio_format_handler *afh = afl + audio_format_id;
303
304 if (!map || !afhi || !afhi->header_len) {
305 *buf = NULL;
306 *len = 0;
307 return;
308 }
309 if (!afh->get_header) {
310 *len = afhi->header_len;
311 *buf = map;
312 return;
313 }
314 afh->get_header(map, mapsize, buf, len);
315 }
316
317 /**
318 * Deallocate any resources obtained from afh_get_header().
319 *
320 * \param header_buf Pointer obtained via afh_get_header().
321 * \param audio_format_id Determines the audio format handler.
322 */
323 void afh_free_header(char *header_buf, uint8_t audio_format_id)
324 {
325 struct audio_format_handler *afh = afl + audio_format_id;
326
327 if (afh->get_header)
328 free(header_buf);
329 }
330
331 /**
332 * Pretty-print the contents of a struct afh_info into a buffer.
333 *
334 * \param audio_format_num The audio format number.
335 * \param afhi Pointer to the structure that contains the information.
336 * \param result Pretty-printed ahfi is here after the call.
337 *
338 * The \a result buffer is dynamically allocated and should be freed by the
339 * caller.
340 *
341 * \return The number of bytes. This function never fails.
342 */
343 unsigned afh_get_afhi_txt(int audio_format_num, struct afh_info *afhi, char **result)
344 {
345 return xasprintf(result, "%s: %dkbit/s\n" /* bitrate */
346 "%s: %s\n" /* format */
347 "%s: %dHz\n" /* frequency */
348 "%s: %d\n" /* channels */
349 "%s: %lu\n" /* seconds total */
350 "%s: %lu: %lu\n" /* chunk time */
351 "%s: %lu\n" /* num chunks */
352 "%s: %s\n" /* techinfo */
353 "%s: %s\n" /* artist */
354 "%s: %s\n" /* title */
355 "%s: %s\n" /* year */
356 "%s: %s\n" /* album */
357 "%s: %s\n", /* comment */
358 status_item_list[SI_BITRATE], afhi->bitrate,
359 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
360 status_item_list[SI_FREQUENCY], afhi->frequency,
361 status_item_list[SI_CHANNELS], afhi->channels,
362 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
363 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
364 (long unsigned)afhi->chunk_tv.tv_usec,
365 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
366 status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
367 status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
368 status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
369 status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
370 status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
371 status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""
372 );
373 }