2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file afh_common.c Common audio format handler functions. */
9 #include <sys/mman.h> /* mmap */
10 #include <sys/types.h>
18 typedef void afh_init_func(struct audio_format_handler *);
19 /* It does not hurt to declare init functions which are not available. */
20 extern afh_init_func mp3_init, ogg_init, aac_afh_init, wma_afh_init,
21 spx_afh_init, flac_afh_init, opus_afh_init;
23 /** The list of all status items */
24 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
27 * The list of supported audio formats.
29 * We always define the full array of audio formats even if some audio formats
30 * were not compiled in. This is because for each audio file the number of its
31 * audio format is stored in the database. We don't want that numbers to become
32 * stale just because the user installed a new version of paraslash that
33 * supports a different set of audio formats.
35 * It can still be easily detected whether an audio format is compiled in by
36 * checking if the init function pointer is not \p NULL.
38 static struct audio_format_handler afl[] = {
45 #if defined(HAVE_OGG) && defined(HAVE_VORBIS)
61 #if defined(HAVE_OGG) && defined(HAVE_SPEEX)
67 #if defined(HAVE_OGG) && defined(HAVE_FLAC)
68 .init = flac_afh_init,
73 #if defined(HAVE_OGG) && defined(HAVE_OPUS)
74 .init = opus_afh_init,
82 static inline int next_audio_format(int format)
85 if (!afl[format].name)
94 /** Iterate over each supported audio format. */
95 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
98 * Call the init function of each supported audio format handler.
104 PARA_NOTICE_LOG("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
105 FOR_EACH_AUDIO_FORMAT(i) {
106 PARA_INFO_LOG("initializing %s handler\n",
107 audio_format_name(i));
108 afl[i].init(&afl[i]);
113 * Guess the audio format judging from filename.
115 * \param name The filename.
117 * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
118 * of audio file this might be. Otherwise the (non-negative) number of the
119 * audio format is returned.
121 int guess_audio_format(const char *name)
123 int i,j, len = strlen(name);
125 FOR_EACH_AUDIO_FORMAT(i) {
126 for (j = 0; afl[i].suffixes[j]; j++) {
127 const char *p = afl[i].suffixes[j];
128 int plen = strlen(p);
131 if (name[len - plen - 1] != '.')
133 if (strcasecmp(name + len - plen, p))
135 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
139 return -E_AUDIO_FORMAT;
143 * Call get_file_info() to obtain an afhi structure.
145 * \param path The full path of the audio file.
146 * \param data Pointer to the contents of the (mapped) file.
147 * \param size The file size in bytes.
148 * \param fd The open file descriptor.
149 * \param afhi Result pointer.
151 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
152 * compiled in audio format handler is able to handler the file.
154 * This function tries to find an audio format handler that can interpret the
155 * file given by \a data and \a size.
157 * It first tries to determine the audio format from the filename given by \a
158 * path. If this doesn't work, all other audio format handlers are tried until
159 * one is found that can handle the file.
161 int compute_afhi(const char *path, char *data, size_t size, int fd,
162 struct afh_info *afhi)
166 afhi->header_len = 0;
167 afhi->techinfo = NULL;
168 afhi->tags.artist = NULL;
169 afhi->tags.title = NULL;
170 afhi->tags.year = NULL;
171 afhi->tags.album = NULL;
172 afhi->tags.comment = NULL;
173 format = guess_audio_format(path);
176 ret = afl[format].get_file_info(data, size, fd, afhi);
182 FOR_EACH_AUDIO_FORMAT(i) {
183 if (i == format) /* we already tried this one to no avail */
185 ret = afl[i].get_file_info(data, size, fd, afhi);
190 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
192 return -E_AUDIO_FORMAT;
195 afhi->techinfo = para_strdup(NULL);
196 if (!afhi->tags.artist)
197 afhi->tags.artist = para_strdup(NULL);
198 if (!afhi->tags.title)
199 afhi->tags.title = para_strdup(NULL);
200 if (!afhi->tags.year)
201 afhi->tags.year = para_strdup(NULL);
202 if (!afhi->tags.album)
203 afhi->tags.album = para_strdup(NULL);
204 if (!afhi->tags.comment)
205 afhi->tags.comment = para_strdup(NULL);
206 PARA_DEBUG_LOG("techinfo: %s\n", afhi->techinfo);
207 PARA_DEBUG_LOG("artist: %s\n", afhi->tags.artist);
208 PARA_DEBUG_LOG("title: %s\n", afhi->tags.title);
209 PARA_DEBUG_LOG("year: %s\n", afhi->tags.year);
210 PARA_DEBUG_LOG("album: %s\n", afhi->tags.album);
211 PARA_DEBUG_LOG("comment: %s\n", afhi->tags.comment);
216 * Deallocate the contents of an afh_info structure.
218 * \param afhi The structure to clear.
220 * This only frees the memory the various pointer fields of \a afhi point to.
221 * It does *not* free \a afhi itself.
223 void clear_afhi(struct afh_info *afhi)
227 free(afhi->chunk_table);
228 free(afhi->techinfo);
229 free(afhi->tags.artist);
230 free(afhi->tags.title);
231 free(afhi->tags.year);
232 free(afhi->tags.album);
233 free(afhi->tags.comment);
237 * Get the name of the given audio format.
239 * \param i The audio format number.
241 * \return This returns a pointer to statically allocated memory so it
242 * must not be freed by the caller.
244 const char *audio_format_name(int i)
246 if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
251 static inline size_t get_chunk_len(long unsigned chunk_num,
252 const struct afh_info *afhi)
254 return afhi->chunk_table[chunk_num + 1] - afhi->chunk_table[chunk_num];
258 * Get one chunk of audio data.
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.
266 * Upon return, \a buf will point so memory inside \a map. The returned buffer
267 * must therefore not be freed by the caller.
269 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
270 void *map, const char **buf, size_t *len)
272 size_t pos = afhi->chunk_table[chunk_num];
274 *len = get_chunk_len(chunk_num, afhi);
278 * Find a suitable start chunk.
280 * \param approx_chunk_num Upper bound for the chunk number to return.
281 * \param afhi Needed for the chunk table.
283 * \return The first non-empty chunk <= \a approx_chunk_num.
285 * \sa \ref afh_get_chunk().
287 int32_t afh_get_start_chunk(int32_t approx_chunk_num,
288 const struct afh_info *afhi)
292 for (k = PARA_MAX(0, approx_chunk_num); k >= 0; k--)
293 if (get_chunk_len(k, afhi) > 0)
299 * Get the header of an audio file.
301 * \param afhi The audio file handler data describing the file.
302 * \param audio_format_id Determines the audio format handler.
303 * \param map The data of the audio file.
304 * \param mapsize The amount of bytes of the mmapped audio file.
305 * \param buf The length of the header is stored here.
306 * \param len Points to a buffer containing the header on return.
308 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
309 * afhi is \p NULL, or if the current audio format does not need special
312 * Otherwise, it is checked whether the audio format handler given by
313 * \a audio_format_id defines a ->get_header() method. If it does, this
314 * method is called to obtain the header. If ->get_header() is \p NULL,
315 * a reference to the first chunk of the audio file is returned.
317 * Once the header is no longer needed, the caller must call \ref
318 * afh_free_header() to free the resources allocated by this function.
320 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
321 void *map, size_t mapsize, char **buf, size_t *len)
323 struct audio_format_handler *afh = afl + audio_format_id;
325 if (!map || !afhi || !afhi->header_len) {
330 if (!afh->get_header) {
331 *len = afhi->header_len;
335 afh->get_header(map, mapsize, buf, len);
339 * Deallocate any resources obtained from afh_get_header().
341 * \param header_buf Pointer obtained via afh_get_header().
342 * \param audio_format_id Determines the audio format handler.
344 void afh_free_header(char *header_buf, uint8_t audio_format_id)
346 struct audio_format_handler *afh = afl + audio_format_id;
353 * Pretty-print the contents of a struct afh_info into a buffer.
355 * \param audio_format_num The audio format number.
356 * \param afhi Pointer to the structure that contains the information.
357 * \param result Pretty-printed ahfi is here after the call.
359 * The \a result buffer is dynamically allocated and should be freed by the
362 * \return The number of bytes. This function never fails.
364 unsigned afh_get_afhi_txt(int audio_format_num, struct afh_info *afhi, char **result)
366 return xasprintf(result, "%s: %dkbit/s\n" /* bitrate */
367 "%s: %s\n" /* format */
368 "%s: %dHz\n" /* frequency */
369 "%s: %d\n" /* channels */
370 "%s: %lu\n" /* seconds total */
371 "%s: %lu: %lu\n" /* chunk time */
372 "%s: %lu\n" /* num chunks */
373 "%s: %s\n" /* techinfo */
374 "%s: %s\n" /* artist */
375 "%s: %s\n" /* title */
376 "%s: %s\n" /* year */
377 "%s: %s\n" /* album */
378 "%s: %s\n", /* comment */
379 status_item_list[SI_BITRATE], afhi->bitrate,
380 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
381 status_item_list[SI_FREQUENCY], afhi->frequency,
382 status_item_list[SI_CHANNELS], afhi->channels,
383 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
384 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
385 (long unsigned)afhi->chunk_tv.tv_usec,
386 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
387 status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
388 status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
389 status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
390 status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
391 status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
392 status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""