Add missing afh_common.c.
[paraslash.git] / afh_common.c
1 /*
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /* \file afh_common.c: Common audio format handler functions. */
7
8 #include <sys/mman.h> /* mmap */
9 #include <sys/time.h> /* gettimeofday */
10 #include <sys/types.h>
11 #include <dirent.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "string.h"
16 #include "afh.h"
17
18 /* The mp3 audio format handler does not need any libs. */
19 void mp3_init(struct audio_format_handler *);
20
21 #ifdef HAVE_OGGVORBIS
22 void ogg_init(struct audio_format_handler *);
23 #endif
24 #ifdef HAVE_FAAD
25 void aac_afh_init(struct audio_format_handler *);
26 #endif
27
28 /**
29 * The list of supported audio formats.
30 *
31 * We always define the full array of audio formats even if some audio formats
32 * were not compiled in. This is because for each audio file the number of its
33 * audio format is stored in the database. We don't want that numbers to become
34 * stale just because the user installed a new version of paraslash that
35 * supports a different set of audio formats.
36 *
37 * It can still be easily detected whether an audio format is compiled in by
38 * checking if the init function pointer is not \p NULL.
39 */
40 static struct audio_format_handler afl[] = {
41 {
42 .name = "mp3",
43 .init = mp3_init,
44 },
45 {
46 .name = "ogg",
47 #ifdef HAVE_OGGVORBIS
48 .init = ogg_init,
49 #endif
50 },
51 {
52 .name = "aac",
53 #ifdef HAVE_FAAD
54 .init = aac_afh_init,
55 #endif
56 },
57 {
58 .name = NULL,
59 }
60 };
61
62 static inline int next_audio_format(int format)
63 {
64 for (;;) {
65 if (!afl[format].name)
66 return format;
67 format++;
68 if (afl[format].init)
69 return format;
70 }
71
72 }
73
74 /** Iterate over each supported audio format. */
75 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
76
77 void afh_init(void)
78 {
79 int i;
80
81 PARA_DEBUG_LOG("supported audio formats: %s\n",
82 SUPPORTED_AUDIO_FORMATS);
83 FOR_EACH_AUDIO_FORMAT(i) {
84 PARA_NOTICE_LOG("initializing %s handler\n",
85 audio_format_name(i));
86 afl[i].init(&afl[i]);
87 }
88 }
89
90
91 /**
92 * guess the audio format judging from filename
93 *
94 * \param name the filename
95 *
96 * \return This function returns -1 if it has no idea what kind of audio
97 * file this might be. Otherwise the (non-negative) number of the audio format
98 * is returned.
99 */
100 int guess_audio_format(const char *name)
101 {
102 int i,j, len = strlen(name);
103
104 FOR_EACH_AUDIO_FORMAT(i) {
105 for (j = 0; afl[i].suffixes[j]; j++) {
106 const char *p = afl[i].suffixes[j];
107 int plen = strlen(p);
108 if (len < plen + 1)
109 continue;
110 if (name[len - plen - 1] != '.')
111 continue;
112 if (strcasecmp(name + len - plen, p))
113 continue;
114 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
115 return i;
116 }
117 }
118 return -E_BAD_AUDIO_FILE_SUFFIX;
119 }
120
121 /**
122 * Call get_file_info() to obtain an afhi structure.
123 *
124 * \param path The full path of the audio file.
125 * \param data Pointer to the contents of the (mapped) file.
126 * \param size The file size in bytes.
127 * \param afhi Result pointer.
128 *
129 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
130 * compiled in audio format handler is able to handler the file.
131 *
132 * This function tries to find an audio format handler that can interpret the
133 * file given by \a data and \a size.
134 *
135 * It first tries to determine the audio format from the filename given by \a
136 * path. If this doesn't work, all other audio format handlers are tried until
137 * one is found that can handle the file.
138 */
139 int compute_afhi(const char *path, char *data, size_t size,
140 struct audio_format_info *afhi)
141 {
142 int ret, i, format = guess_audio_format(path);
143
144 if (format >= 0) {
145 ret = afl[format].get_file_info(data, size, afhi);
146 if (ret >= 0)
147 return format;
148 }
149 FOR_EACH_AUDIO_FORMAT(i) {
150 if (i == format) /* we already tried this one to no avail */
151 continue;
152 ret = afl[i].get_file_info(data, size, afhi);
153 if (ret >= 0)
154 return i;
155 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret));
156 }
157 return -E_AUDIO_FORMAT;
158 }
159
160 /**
161 * Get the name of the given audio format.
162 *
163 * \param i The audio format number.
164 *
165 * This returns a pointer to statically allocated memory so it
166 * must not be freed by the caller.
167 */
168 const char *audio_format_name(int i)
169 {
170 //PARA_NOTICE_LOG("array size: %u¸ requested: %d\n", ARRAY_SIZE(afl), i);
171 assert(i < 0 || i < ARRAY_SIZE(afl) - 1);
172 return i >= 0? afl[i].name : "(none)";
173 }
174