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