paraslash 0.7.3
[paraslash.git] / afh_common.c
index 0e9b13ca24cd0138a195c27633e37f16ca944ec2..7e8f63d22a2126494d53e94457278be06142d4f7 100644 (file)
@@ -1,8 +1,4 @@
-/*
- * Copyright (C) 1997-2014 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file afh_common.c Common audio format handler functions. */
 
 #include "string.h"
 #include "afh.h"
 
-/* The mp3 audio format handler does not need any libs. */
-void mp3_init(struct audio_format_handler *);
-
-#ifdef HAVE_OGGVORBIS
-       void ogg_init(struct audio_format_handler *);
-#endif
-#ifdef HAVE_FAAD
-       void aac_afh_init(struct audio_format_handler *);
-#endif
-#ifdef HAVE_SPEEX
-       void spx_afh_init(struct audio_format_handler *);
-#endif
-#ifdef HAVE_FLAC
-       void flac_afh_init(struct audio_format_handler *);
-#endif
-
-#ifdef HAVE_OPUS
-       void opus_afh_init(struct audio_format_handler *);
-#endif
+/** The list of all status items */
+const char *status_item_list[] = {STATUS_ITEMS};
 
-void wma_afh_init(struct audio_format_handler *);
+/**
+ * For each audio file the number of its audio format is stored in the
+ * database. Therefore this list, in particular its order, is part of the ABI.
+ * So it's only OK to append new audio formats. All audio formats are listed
+ * here, regardless of whether the audio format handler is compiled in.
+ */
+#define ALL_AUDIO_FORMATS \
+       AUDIO_FORMAT(mp3) \
+       AUDIO_FORMAT(ogg) \
+       AUDIO_FORMAT(aac) \
+       AUDIO_FORMAT(wma) \
+       AUDIO_FORMAT(spx) \
+       AUDIO_FORMAT(flac) \
+       AUDIO_FORMAT(opus) \
 
-/** The list of all status items */
-const char *status_item_list[] = {STATUS_ITEM_ARRAY};
+/** \cond audio_format_handler */
+#define AUDIO_FORMAT(_fmt) #_fmt,
+static const char * const audio_format_names[] = {ALL_AUDIO_FORMATS};
+#undef AUDIO_FORMAT
+/* Weak declarations must be public. */
+#define AUDIO_FORMAT(_fmt) \
+       struct audio_format_handler _fmt ## _afh __attribute__ ((weak)) \
+       = {.get_file_info = NULL};
+ALL_AUDIO_FORMATS
+#undef AUDIO_FORMAT
+#define AUDIO_FORMAT(_fmt) & _fmt ## _afh,
+static struct audio_format_handler *afl[] = {ALL_AUDIO_FORMATS};
+#undef AUDIO_FORMAT
+#define NUM_AUDIO_FORMATS (ARRAY_SIZE(afl))
+/** \endcond audio_format_handler */
 
 /**
- * The list of supported audio formats.
+ * Get the name of the given audio format.
  *
- * We always define the full array of audio formats even if some audio formats
- * were not compiled in. This is because for each audio file the number of its
- * audio format is stored in the database. We don't want that numbers to become
- * stale just because the user installed a new version of paraslash that
- * supports a different set of audio formats.
+ * \param i The audio format number.
  *
- * It can still be easily detected whether an audio format is compiled in by
- * checking if the init function pointer is not \p NULL.
+ * \return This returns a pointer to statically allocated memory so it
+ * must not be freed by the caller.
  */
-static struct audio_format_handler afl[] = {
-       {
-               .name = "mp3",
-               .init = mp3_init,
-       },
-       {
-               .name = "ogg",
-#ifdef HAVE_OGGVORBIS
-               .init = ogg_init,
-#endif
-       },
-       {
-               .name = "aac",
-#ifdef HAVE_FAAD
-               .init = aac_afh_init,
-#endif
-       },
-       {
-               .name = "wma",
-               .init = wma_afh_init,
-       },
-       {
-               .name = "spx",
-#ifdef HAVE_SPEEX
-               .init = spx_afh_init,
-#endif
-       },
-       {
-               .name = "flac",
-#ifdef HAVE_FLAC
-               .init = flac_afh_init,
-#endif
-       },
-       {
-               .name = "opus",
-#ifdef HAVE_OPUS
-               .init = opus_afh_init,
-#endif
-       },
-       {
-               .name = NULL,
-       }
-};
+const char *audio_format_name(int i)
+{
+       if (i < 0 || i >= NUM_AUDIO_FORMATS)
+               return "???";
+       return audio_format_names[i];
+}
 
 static inline int next_audio_format(int format)
 {
        for (;;) {
-               if (!afl[format].name)
-                       return format;
                format++;
-               if (afl[format].init)
+               if (format >= NUM_AUDIO_FORMATS)
+                       return format;
+               if (afl[format]->get_file_info)
                        return format;
        }
-
 }
 
 /** Iterate over each supported audio format. */
-#define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
+#define FOR_EACH_AUDIO_FORMAT(i) \
+       for (i = 0; i < NUM_AUDIO_FORMATS; i = next_audio_format(i))
 
 /**
- * Call the init function of each supported audio format handler.
+ * Tell whether an audio format handler provides chunk tables.
+ *
+ * Each audio format handler either provides a chunk table or supports dynamic
+ * chunks.
+ *
+ * \param audio_format_id Offset in the afl array.
+ *
+ * \return True if dynamic chunks are supported, false if the audio format
+ * handler provides chunk tables.
  */
-void afh_init(void)
+bool afh_supports_dynamic_chunks(int audio_format_id)
 {
-       int i;
-
-       PARA_INFO_LOG("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
-       FOR_EACH_AUDIO_FORMAT(i) {
-               PARA_NOTICE_LOG("initializing %s handler\n",
-                       audio_format_name(i));
-               afl[i].init(&afl[i]);
-       }
+       return afl[audio_format_id]->get_chunk;
 }
 
 /**
@@ -140,8 +105,8 @@ int guess_audio_format(const char *name)
        int i,j, len = strlen(name);
 
        FOR_EACH_AUDIO_FORMAT(i) {
-               for (j = 0; afl[i].suffixes[j]; j++) {
-                       const char *p = afl[i].suffixes[j];
+               for (j = 0; afl[i]->suffixes[j]; j++) {
+                       const char *p = afl[i]->suffixes[j];
                        int plen = strlen(p);
                        if (len < plen + 1)
                                continue;
@@ -156,6 +121,23 @@ int guess_audio_format(const char *name)
        return -E_AUDIO_FORMAT;
 }
 
+static int get_file_info(int format, const char *path, char *data,
+               size_t size, int fd, struct afh_info *afhi)
+{
+       int ret;
+       const char *fmt = audio_format_name(format);
+
+       memset(afhi, 0, sizeof(*afhi));
+       ret = afl[format]->get_file_info(data, size, fd, afhi);
+       if (ret < 0) {
+               PARA_WARNING_LOG("%s: %s format not detected: %s\n",
+                       path, fmt, para_strerror(-ret));
+               return ret;
+       }
+       PARA_NOTICE_LOG("%s: detected %s format\n", path, fmt);
+       return format;
+}
+
 /**
  * Call get_file_info() to obtain an afhi structure.
  *
@@ -180,31 +162,18 @@ int compute_afhi(const char *path, char *data, size_t size, int fd,
 {
        int ret, i, format;
 
-       afhi->header_len = 0;
-       afhi->techinfo = NULL;
-       afhi->tags.artist = NULL;
-       afhi->tags.title = NULL;
-       afhi->tags.year = NULL;
-       afhi->tags.album = NULL;
-       afhi->tags.comment = NULL;
        format = guess_audio_format(path);
-
        if (format >= 0) {
-               ret = afl[format].get_file_info(data, size, fd, afhi);
-               if (ret >= 0) {
-                       ret = format;
+               ret = get_file_info(format, path, data, size, fd, afhi);
+               if (ret >= 0)
                        goto success;
-               }
        }
        FOR_EACH_AUDIO_FORMAT(i) {
                if (i == format) /* we already tried this one to no avail */
                        continue;
-               ret = afl[i].get_file_info(data, size, fd, afhi);
-               if (ret >= 0) {
-                       ret = i;
+               ret = get_file_info(i, path, data, size, fd, afhi);
+               if (ret >= 0)
                        goto success;
-               }
-               PARA_WARNING_LOG("%s\n", para_strerror(-ret));
        }
        return -E_AUDIO_FORMAT;
 success:
@@ -250,22 +219,7 @@ void clear_afhi(struct afh_info *afhi)
        free(afhi->tags.comment);
 }
 
-/**
- * Get the name of the given audio format.
- *
- * \param i The audio format number.
- *
- * \return This returns a pointer to statically allocated memory so it
- * must not be freed by the caller.
- */
-const char *audio_format_name(int i)
-{
-       if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
-               return "???";
-       return afl[i].name;
-}
-
-static inline size_t get_chunk_len(long unsigned chunk_num,
+static inline uint32_t get_chunk_len(long unsigned chunk_num,
                const struct afh_info *afhi)
 {
        return afhi->chunk_table[chunk_num + 1] - afhi->chunk_table[chunk_num];
@@ -274,21 +228,73 @@ static inline size_t get_chunk_len(long unsigned chunk_num,
 /**
  * Get one chunk of audio data.
  *
+ * This implicitly calls the ->open method of the audio format handler at the
+ * first call.
+ *
  * \param chunk_num The number of the chunk to get.
  * \param afhi Describes the audio file.
+ * \param audio_format_id Determines the afh.
  * \param map The memory mapped audio file.
+ * \param mapsize Passed to the afh's ->open() method.
  * \param buf Result pointer.
  * \param len The length of the chunk in bytes.
+ * \param afh_context Value/result, determines whether ->open() is called.
  *
  * Upon return, \a buf will point so memory inside \a map. The returned buffer
  * must therefore not be freed by the caller.
+ *
+ * \return Standard.
+ */
+__must_check int afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
+               uint8_t audio_format_id, const void *map, size_t mapsize,
+               const char **buf, uint32_t *len, void **afh_context)
+{
+       struct audio_format_handler *afh = afl[audio_format_id];
+
+       if (afh_supports_dynamic_chunks(audio_format_id)) {
+               int ret;
+
+               if (!*afh_context) {
+                       ret = afh->open(map, mapsize, afh_context);
+                       if (ret < 0)
+                               return ret;
+               }
+               ret = afh->get_chunk(chunk_num, *afh_context,
+                       buf, len);
+               if (ret < 0) {
+                       afh->close(*afh_context);
+                       *afh_context = NULL;
+               }
+               return ret;
+       } else {
+               size_t pos = afhi->chunk_table[chunk_num];
+               *buf = map + pos;
+               *len = get_chunk_len(chunk_num, afhi);
+               return 0;
+       }
+}
+
+/**
+ * Deallocate resources allocated due to dynamic chunk handling.
+ *
+ * This function should be called if afh_get_chunk() was called at least once.
+ * It is OK to call it even for audio formats which do not support dynamic
+ * chunks, in which case the function does nothing.
+ *
+ * \param afh_context As returned from the ->open method of the afh.
+ * \param audio_format_id Determines the afh.
  */
-void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
-               void *map, const char **buf, size_t *len)
+void afh_close(void *afh_context, uint8_t audio_format_id)
 {
-       size_t pos = afhi->chunk_table[chunk_num];
-       *buf = map + pos;
-       *len = get_chunk_len(chunk_num, afhi);
+       struct audio_format_handler *afh = afl[audio_format_id];
+
+       if (!afh_supports_dynamic_chunks(audio_format_id))
+               return;
+       if (!afh->close)
+               return;
+       if (!afh_context)
+               return;
+       afh->close(afh_context);
 }
 
 /**
@@ -296,20 +302,26 @@ void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
  *
  * \param approx_chunk_num Upper bound for the chunk number to return.
  * \param afhi Needed for the chunk table.
+ * \param audio_format_id Determines the afh.
  *
- * \return The first non-empty chunk <= \a approx_chunk_num.
+ * \return For audio format handlers which support dynamic chunks, the function
+ * returns the given chunk number. Otherwise it returns the first non-empty
+ * chunk <= \a approx_chunk_num.
  *
  * \sa \ref afh_get_chunk().
  */
 int32_t afh_get_start_chunk(int32_t approx_chunk_num,
-               const struct afh_info *afhi)
+               const struct afh_info *afhi, uint8_t audio_format_id)
 {
        int32_t k;
 
+       if (afh_supports_dynamic_chunks(audio_format_id))
+               return approx_chunk_num;
+
        for (k = PARA_MAX(0, approx_chunk_num); k >= 0; k--)
                if (get_chunk_len(k, afhi) > 0)
-                       break;
-       return k;
+                       return k;
+       return 0;
 }
 
 /**
@@ -337,7 +349,7 @@ int32_t afh_get_start_chunk(int32_t approx_chunk_num,
 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
                void *map, size_t mapsize, char **buf, size_t *len)
 {
-       struct audio_format_handler *afh = afl + audio_format_id;
+       struct audio_format_handler *afh = afl[audio_format_id];
 
        if (!map || !afhi || !afhi->header_len) {
                *buf = NULL;
@@ -360,7 +372,7 @@ void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
  */
 void afh_free_header(char *header_buf, uint8_t audio_format_id)
 {
-       struct audio_format_handler *afh = afl + audio_format_id;
+       struct audio_format_handler *afh = afl[audio_format_id];
 
        if (afh->get_header)
                free(header_buf);
@@ -384,28 +396,89 @@ unsigned afh_get_afhi_txt(int audio_format_num, struct afh_info *afhi, char **re
                "%s: %s\n" /* format */
                "%s: %dHz\n" /* frequency */
                "%s: %d\n" /* channels */
-               "%s: %lu\n" /* seconds total */
+               "%s: %" PRIu32 "\n" /* seconds total */
                "%s: %lu: %lu\n" /* chunk time */
-               "%s: %lu\n" /* num chunks */
+               "%s: %" PRIu32 "\n" /* num chunks */
+               "%s: %" PRIu32 "\n" /* max chunk size */
                "%s: %s\n" /* techinfo */
                "%s: %s\n" /* artist */
                "%s: %s\n" /* title */
                "%s: %s\n" /* year */
                "%s: %s\n" /* album */
                "%s: %s\n", /* comment */
-               status_item_list[SI_BITRATE], afhi->bitrate,
-               status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
-               status_item_list[SI_FREQUENCY], afhi->frequency,
-               status_item_list[SI_CHANNELS], afhi->channels,
-               status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
-               status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
+               status_item_list[SI_bitrate], afhi->bitrate,
+               status_item_list[SI_format], audio_format_name(audio_format_num),
+               status_item_list[SI_frequency], afhi->frequency,
+               status_item_list[SI_channels], afhi->channels,
+               status_item_list[SI_seconds_total], afhi->seconds_total,
+               status_item_list[SI_chunk_time], (long unsigned)afhi->chunk_tv.tv_sec,
                        (long unsigned)afhi->chunk_tv.tv_usec,
-               status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
-               status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
-               status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
-               status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
-               status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
-               status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
-               status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""
+               status_item_list[SI_num_chunks], afhi->chunks_total,
+               status_item_list[SI_max_chunk_size], afhi->max_chunk_size,
+               status_item_list[SI_techinfo], afhi->techinfo? afhi->techinfo : "",
+               status_item_list[SI_artist], afhi->tags.artist? afhi->tags.artist : "",
+               status_item_list[SI_title], afhi->tags.title? afhi->tags.title : "",
+               status_item_list[SI_year], afhi->tags.year? afhi->tags.year : "",
+               status_item_list[SI_album], afhi->tags.album? afhi->tags.album : "",
+               status_item_list[SI_comment], afhi->tags.comment? afhi->tags.comment : ""
        );
 }
+
+/**
+ * Determine the maximal chunk size by investigating the chunk table.
+ *
+ * \param afhi Value/result.
+ *
+ * This function iterates over the chunk table and sets ->max_chunk_size
+ * accordingly. The function exists only for backward compatibility since as of
+ * version 0.6.0, para_server stores the maximal chunk size in its database.
+ * This function is only called if the database value is zero, indicating that
+ * the file was added by an older server version.
+ */
+void set_max_chunk_size(struct afh_info *afhi)
+{
+       uint32_t n, max = 0, old = 0;
+
+       for (n = 0; n <= afhi->chunks_total; n++) {
+               uint32_t val = afhi->chunk_table[n];
+               /*
+                * If the first chunk is the header, do not consider it for the
+                * calculation of the largest chunk size.
+                */
+               if (n == 0 || (n == 1 && afhi->header_len > 0)) {
+                       old = val;
+                       continue;
+               }
+               max = PARA_MAX(max, val - old);
+               old = val;
+       }
+       afhi->max_chunk_size = max;
+}
+
+/**
+ * Create a copy of the given file with altered meta tags.
+ *
+ * \param audio_format_id Specifies the audio format.
+ * \param map The (read-only) memory map of the input file.
+ * \param mapsize The size of the input file in bytes.
+ * \param tags The new tags.
+ * \param output_fd Altered file is created using this file descriptor.
+ * \param filename The name of the temporary output file.
+ *
+ * This calls the ->rewrite_tags method of the audio format handler associated
+ * with \a audio_format_id to create a copy of the memory-mapped file given
+ * by \a map and \a mapsize, but with altered tags according to \a tags. If
+ * the audio format handler for \a audio_format_id lacks this optional method,
+ * the function returns (the paraslash error code of) \p ENOTSUP.
+ *
+ * \return Standard.
+ */
+int afh_rewrite_tags(int audio_format_id, void *map, size_t mapsize,
+               struct taginfo *tags, int output_fd, const char *filename)
+{
+       struct audio_format_handler *afh = afl[audio_format_id];
+
+       if (!afh->rewrite_tags)
+               return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+       return afh->rewrite_tags(map, mapsize, tags, output_fd, filename);
+}