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)
51 #if defined(HAVE_MP4V2)
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 * Tell whether an audio format handler provides chunk tables.
115 * Each audio format handler either provides a chunk table or supports dynamic
118 * \param audio_format_id Offset in the afl array.
120 * \return True if dynamic chunks are supported, false if the audio format
121 * handler provides chunk tables.
123 bool afh_supports_dynamic_chunks(int audio_format_id)
125 return afl[audio_format_id].get_chunk;
129 * Guess the audio format judging from filename.
131 * \param name The filename.
133 * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
134 * of audio file this might be. Otherwise the (non-negative) number of the
135 * audio format is returned.
137 int guess_audio_format(const char *name)
139 int i,j, len = strlen(name);
141 FOR_EACH_AUDIO_FORMAT(i) {
142 for (j = 0; afl[i].suffixes[j]; j++) {
143 const char *p = afl[i].suffixes[j];
144 int plen = strlen(p);
147 if (name[len - plen - 1] != '.')
149 if (strcasecmp(name + len - plen, p))
151 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
155 return -E_AUDIO_FORMAT;
159 * Get the name of the given audio format.
161 * \param i The audio format number.
163 * \return This returns a pointer to statically allocated memory so it
164 * must not be freed by the caller.
166 const char *audio_format_name(int i)
168 if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
173 static int get_file_info(int format, const char *path, char *data,
174 size_t size, int fd, struct afh_info *afhi)
177 const char *fmt = audio_format_name(format);
179 memset(afhi, 0, sizeof(*afhi));
180 ret = afl[format].get_file_info(data, size, fd, afhi);
182 PARA_WARNING_LOG("%s: %s format not detected: %s\n",
183 path, fmt, para_strerror(-ret));
186 PARA_NOTICE_LOG("%s: detected %s format\n", path, fmt);
191 * Call get_file_info() to obtain an afhi structure.
193 * \param path The full path of the audio file.
194 * \param data Pointer to the contents of the (mapped) file.
195 * \param size The file size in bytes.
196 * \param fd The open file descriptor.
197 * \param afhi Result pointer.
199 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
200 * compiled in audio format handler is able to handler the file.
202 * This function tries to find an audio format handler that can interpret the
203 * file given by \a data and \a size.
205 * It first tries to determine the audio format from the filename given by \a
206 * path. If this doesn't work, all other audio format handlers are tried until
207 * one is found that can handle the file.
209 int compute_afhi(const char *path, char *data, size_t size, int fd,
210 struct afh_info *afhi)
214 format = guess_audio_format(path);
216 ret = get_file_info(format, path, data, size, fd, afhi);
220 FOR_EACH_AUDIO_FORMAT(i) {
221 if (i == format) /* we already tried this one to no avail */
223 ret = get_file_info(i, path, data, size, fd, afhi);
227 return -E_AUDIO_FORMAT;
230 afhi->techinfo = para_strdup(NULL);
231 if (!afhi->tags.artist)
232 afhi->tags.artist = para_strdup(NULL);
233 if (!afhi->tags.title)
234 afhi->tags.title = para_strdup(NULL);
235 if (!afhi->tags.year)
236 afhi->tags.year = para_strdup(NULL);
237 if (!afhi->tags.album)
238 afhi->tags.album = para_strdup(NULL);
239 if (!afhi->tags.comment)
240 afhi->tags.comment = para_strdup(NULL);
241 PARA_DEBUG_LOG("techinfo: %s\n", afhi->techinfo);
242 PARA_DEBUG_LOG("artist: %s\n", afhi->tags.artist);
243 PARA_DEBUG_LOG("title: %s\n", afhi->tags.title);
244 PARA_DEBUG_LOG("year: %s\n", afhi->tags.year);
245 PARA_DEBUG_LOG("album: %s\n", afhi->tags.album);
246 PARA_DEBUG_LOG("comment: %s\n", afhi->tags.comment);
251 * Deallocate the contents of an afh_info structure.
253 * \param afhi The structure to clear.
255 * This only frees the memory the various pointer fields of \a afhi point to.
256 * It does *not* free \a afhi itself.
258 void clear_afhi(struct afh_info *afhi)
262 free(afhi->chunk_table);
263 free(afhi->techinfo);
264 free(afhi->tags.artist);
265 free(afhi->tags.title);
266 free(afhi->tags.year);
267 free(afhi->tags.album);
268 free(afhi->tags.comment);
271 static inline size_t get_chunk_len(long unsigned chunk_num,
272 const struct afh_info *afhi)
274 return afhi->chunk_table[chunk_num + 1] - afhi->chunk_table[chunk_num];
278 * Get one chunk of audio data.
280 * This implicitly calls the ->open method of the audio format handler at the
283 * \param chunk_num The number of the chunk to get.
284 * \param afhi Describes the audio file.
285 * \param audio_format_id Determines the afh.
286 * \param map The memory mapped audio file.
287 * \param mapsize Passed to the afh's ->open() method.
288 * \param buf Result pointer.
289 * \param len The length of the chunk in bytes.
290 * \param afh_context Value/result, determines whether ->open() is called.
292 * Upon return, \a buf will point so memory inside \a map. The returned buffer
293 * must therefore not be freed by the caller.
297 __must_check int afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
298 uint8_t audio_format_id, const void *map, size_t mapsize,
299 const char **buf, size_t *len, void **afh_context)
301 struct audio_format_handler *afh = afl + audio_format_id;
303 if (afh_supports_dynamic_chunks(audio_format_id)) {
307 ret = afh->open(map, mapsize, afh_context);
311 ret = afl[audio_format_id].get_chunk(chunk_num, *afh_context,
314 afh->close(*afh_context);
319 size_t pos = afhi->chunk_table[chunk_num];
321 *len = get_chunk_len(chunk_num, afhi);
327 * Deallocate resources allocated due to dynamic chunk handling.
329 * This function should be called if afh_get_chunk() was called at least once.
330 * It is OK to call it even for audio formats which do not support dynamic
331 * chunks, in which case the function does nothing.
333 * \param afh_context As returned from the ->open method of the afh.
334 * \param audio_format_id Determines the afh.
336 void afh_close(void *afh_context, uint8_t audio_format_id)
338 struct audio_format_handler *afh = afl + audio_format_id;
340 if (!afh_supports_dynamic_chunks(audio_format_id))
346 afh->close(afh_context);
350 * Find a suitable start chunk.
352 * \param approx_chunk_num Upper bound for the chunk number to return.
353 * \param afhi Needed for the chunk table.
354 * \param audio_format_id Determines the afh.
356 * \return For audio format handlers which support dynamic chunks, the function
357 * returns the given chunk number. Otherwise it returns the first non-empty
358 * chunk <= \a approx_chunk_num.
360 * \sa \ref afh_get_chunk().
362 int32_t afh_get_start_chunk(int32_t approx_chunk_num,
363 const struct afh_info *afhi, uint8_t audio_format_id)
367 if (afh_supports_dynamic_chunks(audio_format_id))
368 return approx_chunk_num;
370 for (k = PARA_MAX(0, approx_chunk_num); k >= 0; k--)
371 if (get_chunk_len(k, afhi) > 0)
377 * Get the header of an audio file.
379 * \param afhi The audio file handler data describing the file.
380 * \param audio_format_id Determines the audio format handler.
381 * \param map The data of the audio file.
382 * \param mapsize The amount of bytes of the mmapped audio file.
383 * \param buf The length of the header is stored here.
384 * \param len Points to a buffer containing the header on return.
386 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
387 * afhi is \p NULL, or if the current audio format does not need special
390 * Otherwise, it is checked whether the audio format handler given by
391 * \a audio_format_id defines a ->get_header() method. If it does, this
392 * method is called to obtain the header. If ->get_header() is \p NULL,
393 * a reference to the first chunk of the audio file is returned.
395 * Once the header is no longer needed, the caller must call \ref
396 * afh_free_header() to free the resources allocated by this function.
398 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
399 void *map, size_t mapsize, char **buf, size_t *len)
401 struct audio_format_handler *afh = afl + audio_format_id;
403 if (!map || !afhi || !afhi->header_len) {
408 if (!afh->get_header) {
409 *len = afhi->header_len;
413 afh->get_header(map, mapsize, buf, len);
417 * Deallocate any resources obtained from afh_get_header().
419 * \param header_buf Pointer obtained via afh_get_header().
420 * \param audio_format_id Determines the audio format handler.
422 void afh_free_header(char *header_buf, uint8_t audio_format_id)
424 struct audio_format_handler *afh = afl + audio_format_id;
431 * Pretty-print the contents of a struct afh_info into a buffer.
433 * \param audio_format_num The audio format number.
434 * \param afhi Pointer to the structure that contains the information.
435 * \param result Pretty-printed ahfi is here after the call.
437 * The \a result buffer is dynamically allocated and should be freed by the
440 * \return The number of bytes. This function never fails.
442 unsigned afh_get_afhi_txt(int audio_format_num, struct afh_info *afhi, char **result)
444 return xasprintf(result, "%s: %dkbit/s\n" /* bitrate */
445 "%s: %s\n" /* format */
446 "%s: %dHz\n" /* frequency */
447 "%s: %d\n" /* channels */
448 "%s: %" PRIu32 "\n" /* seconds total */
449 "%s: %lu: %lu\n" /* chunk time */
450 "%s: %" PRIu32 "\n" /* num chunks */
451 "%s: %" PRIu32 "\n" /* max chunk size */
452 "%s: %s\n" /* techinfo */
453 "%s: %s\n" /* artist */
454 "%s: %s\n" /* title */
455 "%s: %s\n" /* year */
456 "%s: %s\n" /* album */
457 "%s: %s\n", /* comment */
458 status_item_list[SI_BITRATE], afhi->bitrate,
459 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
460 status_item_list[SI_FREQUENCY], afhi->frequency,
461 status_item_list[SI_CHANNELS], afhi->channels,
462 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
463 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
464 (long unsigned)afhi->chunk_tv.tv_usec,
465 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
466 status_item_list[SI_MAX_CHUNK_SIZE], afhi->max_chunk_size,
467 status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
468 status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
469 status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
470 status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
471 status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
472 status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""
477 * Determine the maximal chunk size by investigating the chunk table.
479 * \param afhi Value/result.
481 * This function iterates over the chunk table and sets ->max_chunk_size
482 * accordingly. The function exists only for backward compatibility since as of
483 * version 0.6.0, para_server stores the maximal chunk size in its database.
484 * This function is only called if the database value is zero, indicating that
485 * the file was added by an older server version.
487 void set_max_chunk_size(struct afh_info *afhi)
489 uint32_t n, max = 0, old = 0;
491 for (n = 0; n <= afhi->chunks_total; n++) {
492 uint32_t val = afhi->chunk_table[n];
494 * If the first chunk is the header, do not consider it for the
495 * calculation of the largest chunk size.
497 if (n == 0 || (n == 1 && afhi->header_len > 0)) {
501 max = PARA_MAX(max, val - old);
504 afhi->max_chunk_size = max;
508 * Create a copy of the given file with altered meta tags.
510 * \param audio_format_id Specifies the audio format.
511 * \param map The (read-only) memory map of the input file.
512 * \param mapsize The size of the input file in bytes.
513 * \param tags The new tags.
514 * \param output_fd Altered file is created using this file descriptor.
515 * \param filename The name of the temporary output file.
517 * This calls the ->rewrite_tags method of the audio format handler associated
518 * with \a audio_format_id to create a copy of the memory-mapped file given
519 * by \a map and \a mapsize, but with altered tags according to \a tags. If
520 * the audio format handler for \a audio_format_id lacks this optional method,
521 * the function returns (the paraslash error code of) \p ENOTSUP.
525 int afh_rewrite_tags(int audio_format_id, void *map, size_t mapsize,
526 struct taginfo *tags, int output_fd, const char *filename)
528 struct audio_format_handler *afh = afl + audio_format_id;
530 if (!afh->rewrite_tags)
531 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
532 return afh->rewrite_tags(map, mapsize, tags, output_fd, filename);