Sort errors alphabetically.
[paraslash.git] / afh_common.c
1 /*
2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file afh_common.c Common audio format handler functions. */
8
9 #include <sys/mman.h> /* mmap */
10 #include <sys/types.h>
11 #include <regex.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "string.h"
16 #include "afh.h"
17
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;
22
23 /** The list of all status items */
24 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
25
26 /**
27 * The list of supported audio formats.
28 *
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.
34 *
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.
37 */
38 static struct audio_format_handler afl[] = {
39 {
40 .name = "mp3",
41 .init = mp3_init,
42 },
43 {
44 .name = "ogg",
45 #if defined(HAVE_OGG) && defined(HAVE_VORBIS)
46 .init = ogg_init,
47 #endif
48 },
49 {
50 .name = "aac",
51 #if defined(HAVE_MP4V2)
52 .init = aac_afh_init,
53 #endif
54 },
55 {
56 .name = "wma",
57 .init = wma_afh_init,
58 },
59 {
60 .name = "spx",
61 #if defined(HAVE_OGG) && defined(HAVE_SPEEX)
62 .init = spx_afh_init,
63 #endif
64 },
65 {
66 .name = "flac",
67 #if defined(HAVE_OGG) && defined(HAVE_FLAC)
68 .init = flac_afh_init,
69 #endif
70 },
71 {
72 .name = "opus",
73 #if defined(HAVE_OGG) && defined(HAVE_OPUS)
74 .init = opus_afh_init,
75 #endif
76 },
77 {
78 .name = NULL,
79 }
80 };
81
82 static inline int next_audio_format(int format)
83 {
84 for (;;) {
85 if (!afl[format].name)
86 return format;
87 format++;
88 if (afl[format].init)
89 return format;
90 }
91
92 }
93
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))
96
97 /**
98 * Call the init function of each supported audio format handler.
99 */
100 void afh_init(void)
101 {
102 int i;
103
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]);
109 }
110 }
111
112 /**
113 * Guess the audio format judging from filename.
114 *
115 * \param name The filename.
116 *
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.
120 */
121 int guess_audio_format(const char *name)
122 {
123 int i,j, len = strlen(name);
124
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);
129 if (len < plen + 1)
130 continue;
131 if (name[len - plen - 1] != '.')
132 continue;
133 if (strcasecmp(name + len - plen, p))
134 continue;
135 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
136 return i;
137 }
138 }
139 return -E_AUDIO_FORMAT;
140 }
141
142 /**
143 * Get the name of the given audio format.
144 *
145 * \param i The audio format number.
146 *
147 * \return This returns a pointer to statically allocated memory so it
148 * must not be freed by the caller.
149 */
150 const char *audio_format_name(int i)
151 {
152 if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
153 return "???";
154 return afl[i].name;
155 }
156
157 static int get_file_info(int format, const char *path, char *data,
158 size_t size, int fd, struct afh_info *afhi)
159 {
160 int ret;
161 const char *fmt = audio_format_name(format);
162
163 memset(afhi, 0, sizeof(*afhi));
164 ret = afl[format].get_file_info(data, size, fd, afhi);
165 if (ret < 0) {
166 PARA_WARNING_LOG("%s: %s format not detected: %s\n",
167 path, fmt, para_strerror(-ret));
168 return ret;
169 }
170 PARA_NOTICE_LOG("%s: detected %s format\n", path, fmt);
171 return format;
172 }
173
174 /**
175 * Call get_file_info() to obtain an afhi structure.
176 *
177 * \param path The full path of the audio file.
178 * \param data Pointer to the contents of the (mapped) file.
179 * \param size The file size in bytes.
180 * \param fd The open file descriptor.
181 * \param afhi Result pointer.
182 *
183 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
184 * compiled in audio format handler is able to handler the file.
185 *
186 * This function tries to find an audio format handler that can interpret the
187 * file given by \a data and \a size.
188 *
189 * It first tries to determine the audio format from the filename given by \a
190 * path. If this doesn't work, all other audio format handlers are tried until
191 * one is found that can handle the file.
192 */
193 int compute_afhi(const char *path, char *data, size_t size, int fd,
194 struct afh_info *afhi)
195 {
196 int ret, i, format;
197
198 format = guess_audio_format(path);
199 if (format >= 0) {
200 ret = get_file_info(format, path, data, size, fd, afhi);
201 if (ret >= 0)
202 goto success;
203 }
204 FOR_EACH_AUDIO_FORMAT(i) {
205 if (i == format) /* we already tried this one to no avail */
206 continue;
207 ret = get_file_info(i, path, data, size, fd, afhi);
208 if (ret >= 0)
209 goto success;
210 }
211 return -E_AUDIO_FORMAT;
212 success:
213 if (!afhi->techinfo)
214 afhi->techinfo = para_strdup(NULL);
215 if (!afhi->tags.artist)
216 afhi->tags.artist = para_strdup(NULL);
217 if (!afhi->tags.title)
218 afhi->tags.title = para_strdup(NULL);
219 if (!afhi->tags.year)
220 afhi->tags.year = para_strdup(NULL);
221 if (!afhi->tags.album)
222 afhi->tags.album = para_strdup(NULL);
223 if (!afhi->tags.comment)
224 afhi->tags.comment = para_strdup(NULL);
225 PARA_DEBUG_LOG("techinfo: %s\n", afhi->techinfo);
226 PARA_DEBUG_LOG("artist: %s\n", afhi->tags.artist);
227 PARA_DEBUG_LOG("title: %s\n", afhi->tags.title);
228 PARA_DEBUG_LOG("year: %s\n", afhi->tags.year);
229 PARA_DEBUG_LOG("album: %s\n", afhi->tags.album);
230 PARA_DEBUG_LOG("comment: %s\n", afhi->tags.comment);
231 return ret;
232 }
233
234 /**
235 * Deallocate the contents of an afh_info structure.
236 *
237 * \param afhi The structure to clear.
238 *
239 * This only frees the memory the various pointer fields of \a afhi point to.
240 * It does *not* free \a afhi itself.
241 */
242 void clear_afhi(struct afh_info *afhi)
243 {
244 if (!afhi)
245 return;
246 free(afhi->chunk_table);
247 free(afhi->techinfo);
248 free(afhi->tags.artist);
249 free(afhi->tags.title);
250 free(afhi->tags.year);
251 free(afhi->tags.album);
252 free(afhi->tags.comment);
253 }
254
255 static inline size_t get_chunk_len(long unsigned chunk_num,
256 const struct afh_info *afhi)
257 {
258 return afhi->chunk_table[chunk_num + 1] - afhi->chunk_table[chunk_num];
259 }
260
261 /**
262 * Get one chunk of audio data.
263 *
264 * \param chunk_num The number of the chunk to get.
265 * \param afhi Describes the audio file.
266 * \param map The memory mapped audio file.
267 * \param buf Result pointer.
268 * \param len The length of the chunk in bytes.
269 *
270 * Upon return, \a buf will point so memory inside \a map. The returned buffer
271 * must therefore not be freed by the caller.
272 */
273 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
274 void *map, const char **buf, size_t *len)
275 {
276 size_t pos = afhi->chunk_table[chunk_num];
277 *buf = map + pos;
278 *len = get_chunk_len(chunk_num, afhi);
279 }
280
281 /**
282 * Find a suitable start chunk.
283 *
284 * \param approx_chunk_num Upper bound for the chunk number to return.
285 * \param afhi Needed for the chunk table.
286 *
287 * \return The first non-empty chunk <= \a approx_chunk_num.
288 *
289 * \sa \ref afh_get_chunk().
290 */
291 int32_t afh_get_start_chunk(int32_t approx_chunk_num,
292 const struct afh_info *afhi)
293 {
294 int32_t k;
295
296 for (k = PARA_MAX(0, approx_chunk_num); k >= 0; k--)
297 if (get_chunk_len(k, afhi) > 0)
298 break;
299 return k;
300 }
301
302 /**
303 * Get the header of an audio file.
304 *
305 * \param afhi The audio file handler data describing the file.
306 * \param audio_format_id Determines the audio format handler.
307 * \param map The data of the audio file.
308 * \param mapsize The amount of bytes of the mmapped audio file.
309 * \param buf The length of the header is stored here.
310 * \param len Points to a buffer containing the header on return.
311 *
312 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
313 * afhi is \p NULL, or if the current audio format does not need special
314 * header treatment.
315 *
316 * Otherwise, it is checked whether the audio format handler given by
317 * \a audio_format_id defines a ->get_header() method. If it does, this
318 * method is called to obtain the header. If ->get_header() is \p NULL,
319 * a reference to the first chunk of the audio file is returned.
320 *
321 * Once the header is no longer needed, the caller must call \ref
322 * afh_free_header() to free the resources allocated by this function.
323 */
324 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
325 void *map, size_t mapsize, char **buf, size_t *len)
326 {
327 struct audio_format_handler *afh = afl + audio_format_id;
328
329 if (!map || !afhi || !afhi->header_len) {
330 *buf = NULL;
331 *len = 0;
332 return;
333 }
334 if (!afh->get_header) {
335 *len = afhi->header_len;
336 *buf = map;
337 return;
338 }
339 afh->get_header(map, mapsize, buf, len);
340 }
341
342 /**
343 * Deallocate any resources obtained from afh_get_header().
344 *
345 * \param header_buf Pointer obtained via afh_get_header().
346 * \param audio_format_id Determines the audio format handler.
347 */
348 void afh_free_header(char *header_buf, uint8_t audio_format_id)
349 {
350 struct audio_format_handler *afh = afl + audio_format_id;
351
352 if (afh->get_header)
353 free(header_buf);
354 }
355
356 /**
357 * Pretty-print the contents of a struct afh_info into a buffer.
358 *
359 * \param audio_format_num The audio format number.
360 * \param afhi Pointer to the structure that contains the information.
361 * \param result Pretty-printed ahfi is here after the call.
362 *
363 * The \a result buffer is dynamically allocated and should be freed by the
364 * caller.
365 *
366 * \return The number of bytes. This function never fails.
367 */
368 unsigned afh_get_afhi_txt(int audio_format_num, struct afh_info *afhi, char **result)
369 {
370 return xasprintf(result, "%s: %dkbit/s\n" /* bitrate */
371 "%s: %s\n" /* format */
372 "%s: %dHz\n" /* frequency */
373 "%s: %d\n" /* channels */
374 "%s: %" PRIu32 "\n" /* seconds total */
375 "%s: %lu: %lu\n" /* chunk time */
376 "%s: %" PRIu32 "\n" /* num chunks */
377 "%s: %s\n" /* techinfo */
378 "%s: %s\n" /* artist */
379 "%s: %s\n" /* title */
380 "%s: %s\n" /* year */
381 "%s: %s\n" /* album */
382 "%s: %s\n", /* comment */
383 status_item_list[SI_BITRATE], afhi->bitrate,
384 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
385 status_item_list[SI_FREQUENCY], afhi->frequency,
386 status_item_list[SI_CHANNELS], afhi->channels,
387 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
388 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
389 (long unsigned)afhi->chunk_tv.tv_usec,
390 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
391 status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
392 status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
393 status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
394 status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
395 status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
396 status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""
397 );
398 }
399
400 /**
401 * Create a copy of the given file with altered meta tags.
402 *
403 * \param audio_format_id Specifies the audio format.
404 * \param map The (read-only) memory map of the input file.
405 * \param mapsize The size of the input file in bytes.
406 * \param tags The new tags.
407 * \param output_fd Altered file is created using this file descriptor.
408 * \param filename The name of the temporary output file.
409 *
410 * This calls the ->rewrite_tags method of the audio format handler associated
411 * with \a audio_format_id to create a copy of the memory-mapped file given
412 * by \a map and \a mapsize, but with altered tags according to \a tags. If
413 * the audio format handler for \a audio_format_id lacks this optional method,
414 * the function returns (the paraslash error code of) \p ENOTSUP.
415 *
416 * \return Standard.
417 */
418 int afh_rewrite_tags(int audio_format_id, void *map, size_t mapsize,
419 struct taginfo *tags, int output_fd, const char *filename)
420 {
421 struct audio_format_handler *afh = afl + audio_format_id;
422
423 if (!afh->rewrite_tags)
424 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
425 return afh->rewrite_tags(map, mapsize, tags, output_fd, filename);
426 }