2 * Copyright (C) 1997-2010 Andre Noll <maan@systemlinux.org>
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/time.h> /* gettimeofday */
11 #include <sys/types.h>
20 /* The mp3 audio format handler does not need any libs. */
21 void mp3_init(struct audio_format_handler
*);
24 void ogg_init(struct audio_format_handler
*);
27 void aac_afh_init(struct audio_format_handler
*);
30 void spx_afh_init(struct audio_format_handler
*);
33 void wma_afh_init(struct audio_format_handler
*);
35 * The list of supported audio formats.
37 * We always define the full array of audio formats even if some audio formats
38 * were not compiled in. This is because for each audio file the number of its
39 * audio format is stored in the database. We don't want that numbers to become
40 * stale just because the user installed a new version of paraslash that
41 * supports a different set of audio formats.
43 * It can still be easily detected whether an audio format is compiled in by
44 * checking if the init function pointer is not \p NULL.
46 static struct audio_format_handler afl
[] = {
78 static inline int next_audio_format(int format
)
81 if (!afl
[format
].name
)
90 /** Iterate over each supported audio format. */
91 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
94 * Call the init function of each supported audio format handler.
100 PARA_INFO_LOG("supported audio formats: %s\n",
101 SERVER_AUDIO_FORMATS
);
102 FOR_EACH_AUDIO_FORMAT(i
) {
103 PARA_NOTICE_LOG("initializing %s handler\n",
104 audio_format_name(i
));
105 afl
[i
].init(&afl
[i
]);
110 * Guess the audio format judging from filename.
112 * \param name The filename.
114 * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
115 * of audio file this might be. Otherwise the (non-negative) number of the
116 * audio format is returned.
118 int guess_audio_format(const char *name
)
120 int i
,j
, len
= strlen(name
);
122 FOR_EACH_AUDIO_FORMAT(i
) {
123 for (j
= 0; afl
[i
].suffixes
[j
]; j
++) {
124 const char *p
= afl
[i
].suffixes
[j
];
125 int plen
= strlen(p
);
128 if (name
[len
- plen
- 1] != '.')
130 if (strcasecmp(name
+ len
- plen
, p
))
132 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
136 return -E_AUDIO_FORMAT
;
140 * Call get_file_info() to obtain an afhi structure.
142 * \param path The full path of the audio file.
143 * \param data Pointer to the contents of the (mapped) file.
144 * \param size The file size in bytes.
145 * \param fd The open file descriptor.
146 * \param afhi Result pointer.
148 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
149 * compiled in audio format handler is able to handler the file.
151 * This function tries to find an audio format handler that can interpret the
152 * file given by \a data and \a size.
154 * It first tries to determine the audio format from the filename given by \a
155 * path. If this doesn't work, all other audio format handlers are tried until
156 * one is found that can handle the file.
158 int compute_afhi(const char *path
, char *data
, size_t size
, int fd
,
159 struct afh_info
*afhi
)
163 afhi
->header_offset
= 0;
164 afhi
->header_len
= 0;
165 afhi
->techinfo
= NULL
;
166 afhi
->tags
.artist
= NULL
;
167 afhi
->tags
.title
= NULL
;
168 afhi
->tags
.year
= NULL
;
169 afhi
->tags
.album
= NULL
;
170 afhi
->tags
.comment
= NULL
;
171 format
= guess_audio_format(path
);
174 ret
= afl
[format
].get_file_info(data
, size
, fd
, afhi
);
180 FOR_EACH_AUDIO_FORMAT(i
) {
181 if (i
== format
) /* we already tried this one to no avail */
183 ret
= afl
[i
].get_file_info(data
, size
, fd
, afhi
);
188 PARA_WARNING_LOG("%s\n", para_strerror(-ret
));
190 return -E_AUDIO_FORMAT
;
193 afhi
->techinfo
= para_strdup(NULL
);
194 if (!afhi
->tags
.artist
)
195 afhi
->tags
.artist
= para_strdup(NULL
);
196 if (!afhi
->tags
.title
)
197 afhi
->tags
.title
= para_strdup(NULL
);
198 if (!afhi
->tags
.year
)
199 afhi
->tags
.year
= para_strdup(NULL
);
200 if (!afhi
->tags
.album
)
201 afhi
->tags
.album
= para_strdup(NULL
);
202 if (!afhi
->tags
.comment
)
203 afhi
->tags
.comment
= para_strdup(NULL
);
204 PARA_DEBUG_LOG("techinfo: %s\n", afhi
->techinfo
);
205 PARA_DEBUG_LOG("artist: %s\n", afhi
->tags
.artist
);
206 PARA_DEBUG_LOG("title: %s\n", afhi
->tags
.title
);
207 PARA_DEBUG_LOG("year: %s\n", afhi
->tags
.year
);
208 PARA_DEBUG_LOG("album: %s\n", afhi
->tags
.album
);
209 PARA_DEBUG_LOG("comment: %s\n", afhi
->tags
.comment
);
214 * Get the name of the given audio format.
216 * \param i The audio format number.
218 * This returns a pointer to statically allocated memory so it
219 * must not be freed by the caller.
221 const char *audio_format_name(int i
)
223 if (i
< 0 || i
>= ARRAY_SIZE(afl
) - 1)
229 * Get one chunk of audio data.
231 * \param chunk_num The number of the chunk to get.
232 * \param afhi Describes the audio file.
233 * \param map The memory mapped audio file.
234 * \param buf Result pointer.
235 * \param len The length of the chunk in bytes.
237 * Upon return, \a buf will point so memory inside \a map. The returned buffer
238 * must therefore not be freed by the caller.
240 void afh_get_chunk(long unsigned chunk_num
, struct afh_info
*afhi
,
241 void *map
, const char **buf
, size_t *len
)
243 size_t pos
= afhi
->chunk_table
[chunk_num
];
245 *len
= afhi
->chunk_table
[chunk_num
+ 1] - pos
;
249 * Compute the size of the largest chunk of an audio file.
251 * \param afhi The audio format handler struct containing the chunk table.
253 * \return The number of bytes of the largest chunk.
255 uint32_t afh_get_largest_chunk_size(struct afh_info
*afhi
)
257 uint32_t n
, largest
= 0, *ct
= afhi
->chunk_table
;
259 for (n
= 1; n
<= afhi
->chunks_total
; n
++)
260 largest
= PARA_MAX(largest
, ct
[n
] - ct
[n
- 1]);
265 * Get the header of an audio file.
267 * \param afhi The audio file handler data describing the file.
268 * \param map The data of the audio file.
269 * \param buf The length of the header is stored here.
270 * \param len Points to a buffer containing the header on return.
272 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
273 * afhi is \p NULL, or if the current audio format does not need special
276 void afh_get_header(struct afh_info
*afhi
, void *map
, const char **buf
, size_t *len
)
278 if (!map
|| !afhi
|| !afhi
->header_len
) {
283 *len
= afhi
->header_len
;
284 *buf
= map
+ afhi
->header_offset
;