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