]> git.tuebingen.mpg.de Git - paraslash.git/blob - mp3_afh.c
paraslash 0.7.3
[paraslash.git] / mp3_afh.c
1 /* Copyright (C) 2003 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file mp3_afh.c para_server's mp3 audio format handler */
4
5 /*
6  * This file is based in part on mp3tech.c and mp3tech.h, Copyright (C)
7  * 2000-2001 Cedric Tefft <cedric@earthling.net>, which in turn is based
8  * in part on
9  *
10  *      * MP3Info 0.5 by Ricardo Cerqueira <rmc@rccn.net>
11  *      * MP3Stat 0.9 by Ed Sweetman <safemode@voicenet.com> and
12  *                       Johannes Overmann <overmann@iname.com>
13  */
14
15 #include <regex.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "afh.h"
20 #include "string.h"
21 #include "fd.h"
22
23 /*
24  * MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames we need
25  * to see before we decide we are looking at a real MP3 file
26  */
27 #define MIN_CONSEC_GOOD_FRAMES 4
28 #define FRAME_HEADER_SIZE 4
29 #define MIN_FRAME_SIZE 21
30
31 struct mp3header {
32         unsigned long sync;
33         unsigned int version;
34         unsigned int layer;
35         unsigned int crc;
36         unsigned int bitrate;
37         unsigned int freq;
38         unsigned int padding;
39         unsigned int mode;
40 };
41
42 static const int frequencies[3][4] = {
43         {22050,24000,16000,50000}, /* MPEG 2.0 */
44         {44100,48000,32000,50000}, /* MPEG 1.0 */
45         {11025,12000,8000,50000} /* MPEG 2.5 */
46 };
47
48 static const int mp3info_bitrate[2][3][14] = {
49 { /* MPEG 2.0 */
50         {32,48,56,64,80,96,112,128,144,160,176,192,224,256}, /* layer 1 */
51         {8,16,24,32,40,48,56,64,80,96,112,128,144,160}, /* layer 2 */
52         {8,16,24,32,40,48,56,64,80,96,112,128,144,160} /* layer 3 */
53 },
54
55 { /* MPEG 1.0 */
56         {32,64,96,128,160,192,224,256,288,320,352,384,416,448}, /* layer 1 */
57         {32,48,56,64,80,96,112,128,160,192,224,256,320,384}, /* layer 2 */
58         {32,40,48,56,64,80,96,112,128,160,192,224,256,320} /* layer 3 */
59 }
60 };
61
62 static const int frame_size_index[] = {24000, 72000, 72000};
63 #ifdef HAVE_ID3TAG
64
65 #include <id3tag.h>
66
67 static char *get_utf8(id3_ucs4_t const *string)
68 {
69         if (!string)
70                 return NULL;
71         return (char *)id3_ucs4_utf8duplicate(string);
72 }
73
74 static char *get_stringlist(union id3_field *field)
75 {
76         unsigned int k, nstrings = id3_field_getnstrings(field);
77         char *result = NULL;
78
79         for (k = 0; k < nstrings; k++) {
80                 char *tmp = (char *)get_utf8(id3_field_getstrings(field, k));
81                 if (result) {
82                         char *tmp2 = result;
83                         result = make_message("%s %s", tmp2, tmp);
84                         free(tmp);
85                         free(tmp2);
86                 } else
87                         result = tmp;
88         }
89         return result;
90 }
91
92 static char *get_string(union id3_field *field)
93 {
94         id3_ucs4_t const *string = id3_field_getfullstring(field);
95
96         return get_utf8(string);
97 }
98
99 #define FOR_EACH_FIELD(f, j, fr) for (j = 0; j < (fr)->nfields && \
100         (f = id3_frame_field((fr), j)); j++)
101
102 static char *get_strings(struct id3_frame *fr)
103 {
104         int j;
105         union id3_field *field;
106
107         FOR_EACH_FIELD(field, j, fr) {
108                 enum id3_field_type type = id3_field_type(field);
109
110                 if (type == ID3_FIELD_TYPE_STRINGLIST)
111                         return get_stringlist(field);
112                 if (type == ID3_FIELD_TYPE_STRINGFULL)
113                         return get_string(field);
114         }
115         return NULL;
116 }
117
118 /* this only sets values which are undefined so far */
119 static void parse_frames(struct id3_tag *id3_t, struct taginfo *tags)
120 {
121         int i;
122
123         for (i = 0; i < id3_t->nframes; i++) {
124                 struct id3_frame *fr = id3_t->frames[i];
125                 if (!strcmp(fr->id, ID3_FRAME_TITLE)) {
126                         if (!tags->title)
127                                 tags->title = get_strings(fr);
128                         continue;
129                 }
130                 if (!strcmp(fr->id, ID3_FRAME_ARTIST)) {
131                         if (!tags->artist)
132                                 tags->artist = get_strings(fr);
133                         continue;
134                 }
135                 if (!strcmp(fr->id, ID3_FRAME_ALBUM)) {
136                         if (!tags->album)
137                                 tags->album = get_strings(fr);
138                         continue;
139                 }
140                 if (!strcmp(fr->id, ID3_FRAME_YEAR)) {
141                         if (!tags->year)
142                                 tags->year = get_strings(fr);
143                         continue;
144                 }
145                 if (!strcmp(fr->id, ID3_FRAME_COMMENT)) {
146                         if (!tags->comment)
147                                 tags->comment = get_strings(fr);
148                         continue;
149                 }
150         }
151 }
152
153 static int mp3_get_id3(unsigned char *map, size_t numbytes, __a_unused int fd,
154                 struct taginfo *tags)
155 {
156         int ret = 0;
157         struct id3_tag *id3_t;
158
159         /* id3v2 tags are usually located at the beginning. */
160         id3_t = id3_tag_parse(map, numbytes);
161         if (id3_t) {
162                 parse_frames(id3_t, tags);
163                 ret |= 2;
164                 id3_tag_delete(id3_t);
165         }
166         /* Also look for an id3v1 tag at the end of the file. */
167         if (numbytes >= 128) {
168                 id3_t = id3_tag_parse(map + numbytes - 128, 128);
169                 if (id3_t) {
170                         parse_frames(id3_t, tags);
171                         ret |= 1;
172                         id3_tag_delete(id3_t);
173                 }
174         }
175         return ret;
176 }
177
178 /* These helpers are not mentioned in the libid3tag header file. */
179 void id3_field_init(union id3_field *field, enum id3_field_type type);
180 void id3_field_finish(union id3_field *field);
181
182 /*
183  * Frames of type ID3_FRAME_COMMENT contain three fields of type language,
184  * string, and fullstring. The third field contains the actual comment.
185  */
186 static int set_comment_fields(struct id3_frame *fr, id3_ucs4_t *ucs4_val)
187 {
188         int ret;
189         union id3_field *field;
190
191         field = id3_frame_field(fr, 1);
192         id3_field_init(field, ID3_FIELD_TYPE_LANGUAGE);
193
194         field = id3_frame_field(fr, 2);
195         id3_field_init(field, ID3_FIELD_TYPE_STRING);
196
197         field = id3_frame_field(fr, 3);
198         id3_field_init(field, ID3_FIELD_TYPE_STRINGFULL);
199         ret = id3_field_setfullstring(field, ucs4_val);
200         if (ret < 0)
201                 return -E_ID3_SETSTRING;
202         return 1;
203 }
204
205 /*
206  * UCS-4 stands for Universal Character Set where each character uses exactly
207  * 32 bits. It is also known as UTF-32, see ISO 10646.
208  *
209  * We have to use UCS-4 as an intermediate representation because the functions
210  * of libid3tag which set the tag content expect an array of UCS-4 characters.
211  * Fortunately, libid3tag contains conversion functions from and to UTF-8.
212  */
213 static int replace_tag(char const *id, const char *val, struct id3_tag *id3_t,
214                 int options)
215 {
216         int ret;
217         struct id3_frame *fr;
218         union id3_field *field;
219         id3_ucs4_t *ucs4_val;
220
221         /* First, detach all frames that match the given id. */
222         while ((fr = id3_tag_findframe(id3_t, id, 0))) {
223                 PARA_INFO_LOG("deleting %s frame\n", id);
224                 ret = id3_tag_detachframe(id3_t, fr);
225                 if (ret < 0)
226                         return -E_ID3_DETACH;
227                 id3_frame_delete(fr);
228         }
229         if (!val || !*val)
230                 return 0;
231         fr = id3_frame_new(id);
232         PARA_DEBUG_LOG("frame desc: %s, %u fields\n", fr->description, fr->nfields);
233
234         /* Frame 0 contains the encoding. We always use UTF-8. */
235         field = id3_frame_field(fr, 0);
236         id3_field_init(field, ID3_FIELD_TYPE_TEXTENCODING);
237         ret = id3_field_settextencoding(field, ID3_FIELD_TEXTENCODING_UTF_8);
238         if (ret < 0)
239                 return -E_ID3_SETENCODING;
240         /* create UCS-4 representation */
241         ucs4_val = id3_utf8_ucs4duplicate((const id3_utf8_t *)val);
242
243         if (strcmp(id, ID3_FRAME_COMMENT) == 0)
244                 ret = set_comment_fields(fr, ucs4_val);
245         else {
246                 /* Non-comment frames contain the value in field 1. */
247                 field = id3_frame_field(fr, 1);
248                 if (options & ID3_TAG_OPTION_ID3V1) {
249                         id3_ucs4_t *ptr[] = {ucs4_val, NULL};
250                         id3_field_init(field, ID3_FIELD_TYPE_STRINGLIST);
251                         ret = id3_field_setstrings(field, 1, ptr);
252                 } else {
253                         id3_field_init(field, ID3_FIELD_TYPE_STRINGFULL);
254                         ret = id3_field_setfullstring(field, ucs4_val);
255                 }
256         }
257         free(ucs4_val);
258         if (ret < 0)
259                 return -E_ID3_SETSTRING;
260
261         PARA_INFO_LOG("attaching %s frame, value: %s\n", id, val);
262         ret = id3_tag_attachframe(id3_t, fr);
263         if (ret < 0)
264                 return -E_ID3_ATTACH;
265         return 1;
266 }
267
268 static int replace_tags(struct id3_tag *id3_t, struct taginfo *tags)
269 {
270         int ret, options = id3_tag_options(id3_t, 0, 0);
271
272         ret = replace_tag(ID3_FRAME_ARTIST, tags->artist, id3_t, options);
273         if (ret < 0)
274                 return ret;
275         ret = replace_tag(ID3_FRAME_TITLE, tags->title, id3_t, options);
276         if (ret < 0)
277                 return ret;
278         ret = replace_tag(ID3_FRAME_ALBUM, tags->album, id3_t, options);
279         if (ret < 0)
280                 return ret;
281         ret = replace_tag(ID3_FRAME_YEAR, tags->year, id3_t, options);
282         if (ret < 0)
283                 return ret;
284         ret = replace_tag(ID3_FRAME_COMMENT, tags->comment, id3_t, options);
285         if (ret < 0)
286                 return ret;
287         return 1;
288 }
289
290 /* id3_tag_delete() does not seem to work due to refcount issues. */
291 static void free_tag(struct id3_tag *id3_t)
292 {
293         int i, j;
294
295         if (!id3_t)
296                 return;
297         for (i = 0; i < id3_t->nframes; i++) {
298                 struct id3_frame *fr = id3_t->frames[i];
299                 for (j = 0; j < fr->nfields; j++) {
300                         union id3_field *field = &fr->fields[j];
301                         id3_field_finish(field);
302                 }
303                 free(fr);
304         }
305         free(id3_t->frames);
306         free(id3_t);
307 }
308
309 static int mp3_rewrite_tags(const char *map, size_t mapsize,
310                 struct taginfo *tags, int fd, __a_unused const char *filename)
311 {
312         int ret;
313         id3_length_t old_v2size, new_v2size;
314         id3_byte_t v1_buffer[128], *v2_buffer;
315         size_t data_sz;
316         struct id3_tag *v1_tag = NULL, *v2_tag = NULL;
317
318         if (mapsize >= 128) {
319                 v1_tag = id3_tag_parse((const id3_byte_t *)map + mapsize - 128,
320                         128);
321                 if (v1_tag) {
322                         PARA_NOTICE_LOG("replacing id3v1 tag\n");
323                         ret = replace_tags(v1_tag, tags);
324                         if (ret < 0)
325                                 goto out;
326                         id3_tag_render(v1_tag, v1_buffer);
327                 }
328         }
329         old_v2size = 0;
330         v2_tag = id3_tag_parse((const id3_byte_t *)map, mapsize);
331         if (v2_tag) {
332                 PARA_NOTICE_LOG("replacing id3v2 tag\n");
333                 old_v2size = v2_tag->paddedsize;
334         } else {
335                 PARA_NOTICE_LOG("adding id3v2 tag\n");
336                 v2_tag = id3_tag_new();
337                 assert(v2_tag);
338         }
339         /*
340          * Turn off all options to avoid creating an extended header.  id321
341          * does not understand it.
342          */
343         id3_tag_options(v2_tag, ~0U, 0);
344         ret = replace_tags(v2_tag, tags);
345         if (ret < 0)
346                 goto out;
347         new_v2size = id3_tag_render(v2_tag, NULL);
348         v2_buffer = alloc(new_v2size);
349         id3_tag_render(v2_tag, v2_buffer);
350         PARA_INFO_LOG("writing v2 tag (%lu bytes)\n", new_v2size);
351         ret = write_all(fd, (char *)v2_buffer, new_v2size);
352         free(v2_buffer);
353         if (ret < 0)
354                 goto out;
355         data_sz = mapsize - old_v2size;
356         if (v1_tag && data_sz >= 128)
357                 data_sz -= 128;
358         PARA_INFO_LOG("writing data part (%zu bytes)\n", data_sz);
359         ret = write_all(fd, map + old_v2size, data_sz);
360         if (ret < 0)
361                 goto out;
362         if (v1_tag) {
363                 PARA_INFO_LOG("writing v1 tag\n");
364                 ret = write_all(fd, (char *)v1_buffer, 128);
365         }
366 out:
367         free_tag(v1_tag);
368         free_tag(v2_tag);
369         return ret;
370 }
371
372 #else /* HAVE_ID3TAG */
373
374 /*
375  * Remove trailing whitespace from the end of a string
376  */
377 static char *unpad(char *string)
378 {
379         char *pos = string + strlen(string) - 1;
380         while (para_isspace(pos[0]))
381                 (pos--)[0] = 0;
382         return string;
383 }
384
385 static int mp3_get_id3(unsigned char *map, size_t numbytes, __a_unused int fd,
386         struct taginfo *tags)
387 {
388         char title[31], artist[31], album[31], year[5], comment[31];
389         off_t fpos;
390
391         if (numbytes < 128 || strncmp("TAG", (char *)map + numbytes - 128, 3)) {
392                 PARA_DEBUG_LOG("no id3 v1 tag\n");
393                 return 0;
394         }
395         fpos = numbytes - 125;
396         memcpy(title, map + fpos, 30);
397         fpos += 30;
398         title[30] = '\0';
399         memcpy(artist, map + fpos, 30);
400         fpos += 30;
401         artist[30] = '\0';
402         memcpy(album, map + fpos, 30);
403         fpos += 30;
404         album[30] = '\0';
405         memcpy(year, map + fpos, 4);
406         fpos += 4;
407         year[4] = '\0';
408         memcpy(comment, map + fpos, 30);
409         comment[30] = '\0';
410         unpad(title);
411         unpad(artist);
412         unpad(album);
413         unpad(year);
414         unpad(comment);
415         tags->artist = para_strdup(artist);
416         tags->title = para_strdup(title);
417         tags->year = para_strdup(year);
418         tags->album = para_strdup(album);
419         tags->comment = para_strdup(comment);
420         return 1;
421 }
422 #endif /* HAVE_ID3TAG */
423
424 static int header_frequency(struct mp3header *h)
425 {
426         if (h->version > 2 || h->freq > 3)
427                 return -E_HEADER_FREQ;
428         return frequencies[h->version][h->freq];
429 }
430
431 static const char *header_mode(const struct mp3header *h)
432 {
433         const char * const mode_text[] = {"stereo", "joint stereo",
434                 "dual channel", "mono"};
435
436         if (h->mode >= ARRAY_SIZE(mode_text))
437                 return "invalid";
438         return mode_text[h->mode];
439 }
440
441 static int header_channels(struct mp3header *h)
442 {
443         if (h->mode > 3)
444                 return 0;
445         if (h->mode < 3)
446                 return 2;
447         return 1;
448 }
449
450 static int header_bitrate(struct mp3header *h)
451 {
452         if (!h->layer || h->layer > 3 || h->bitrate > 14 || !h->bitrate)
453                 return -E_HEADER_BITRATE;
454         return mp3info_bitrate[h->version & 1][3 - h->layer][h->bitrate - 1];
455 }
456
457 static int frame_length(struct mp3header *header)
458 {
459         int hb, hf = header_frequency(header);
460
461         if (hf < 0)
462                 return hf;
463         hb = header_bitrate(header);
464         if (hb < 0)
465                 return hb;
466         if (header->sync != 0xFFE || header->layer > 3)
467                 return -E_FRAME;
468         return frame_size_index[3 - header->layer] *
469                 ((header->version & 1) + 1) * hb / hf
470                 + header->padding;
471 }
472
473 static int compare_headers(struct mp3header *h1, struct mp3header *h2)
474 {
475         if ((*(unsigned int*)h1) == (*(unsigned int*)h2))
476                 return 1;
477         if ((h1->version == h2->version) &&
478                         (h1->layer == h2->layer) &&
479                         (h1->crc == h2->crc) &&
480                         (h1->freq == h2->freq) &&
481                         (h1->mode == h2->mode))
482                 return 1;
483         return 0;
484 }
485
486 /*
487  * get next MP3 frame header.
488  *
489  * On success, the header frame length is returned and the given header
490  * structure that is filled in.  A return value of zero means that we did not
491  * retrieve a valid frame header, and a negative return value indicates an
492  * error.
493  */
494 static int get_header(unsigned char *map, size_t numbytes, off_t *fpos,
495         struct mp3header *header)
496 {
497         int fl, ret;
498
499         if (*fpos + FRAME_HEADER_SIZE > numbytes) {
500                 *fpos = numbytes - 1;
501                 header->sync = 0;
502                 return 0;
503         }
504         header->layer = (map[*fpos + 1] >> 1) & 3;
505         header->sync = (((int)map[*fpos]<<4) | ((int)(map[*fpos + 1]&0xE0)>>4));
506         if (map[*fpos + 1] & 0x10)
507                 header->version = (map[*fpos + 1] >> 3) & 1;
508         else
509                 header->version = 2;
510         if ((header->sync != 0xFFE) || (header->layer != 1)) {
511                 ret = 0;
512                 header->sync = 0;
513                 goto out;
514         }
515         header->crc = map[*fpos + 1] & 1;
516         header->bitrate = (map[*fpos + 2] >> 4) & 0x0F;
517         header->freq = (map[*fpos + 2] >> 2) & 0x3;
518         header->padding = (map[*fpos + 2] >>1) & 0x1;
519         header->mode = (map[*fpos + 3] >> 6) & 0x3;
520         fl = frame_length(header);
521         ret = (fl >= MIN_FRAME_SIZE)? fl : -E_FRAME_LENGTH;
522 out:
523         *fpos += FRAME_HEADER_SIZE;
524         return ret;
525 }
526
527 /*
528  * find the next mp3 header
529  *
530  * Return the length of the next frame header or zero if the end of the file is
531  * reached.
532  */
533 static int mp3_seek_next_header(unsigned char *map, size_t numbytes, off_t *fpos,
534         struct mp3header *result)
535 {
536         int k, l = 0, first_len;
537         struct mp3header h, h2;
538         long valid_start = 0;
539
540         for (; *fpos < numbytes; (*fpos)++) {
541                 if (map[*fpos] != 0xff)
542                         continue;
543                 valid_start = *fpos;
544                 first_len = get_header(map, numbytes, fpos, &h);
545                 if (first_len <= 0)
546                         continue;
547                 *fpos += first_len - FRAME_HEADER_SIZE;
548                 for (k = 1; k < MIN_CONSEC_GOOD_FRAMES; k++) {
549                         if ((l = get_header(map, numbytes, fpos, &h2)) <= 0)
550                                 break;
551                         if (!compare_headers(&h, &h2))
552                                 break;
553                         *fpos += l - FRAME_HEADER_SIZE;
554                 }
555                 if (k == MIN_CONSEC_GOOD_FRAMES) {
556                         *fpos = valid_start;
557                         *result = h2;
558                         return first_len;
559                 }
560         }
561         return 0;
562 }
563
564 static int find_valid_start(unsigned char *map, size_t numbytes, off_t *fpos,
565         struct mp3header *header)
566 {
567         int frame_len;
568
569         frame_len = get_header(map, numbytes, fpos, header);
570         if (frame_len < 0)
571                 return frame_len;
572         if (!frame_len) {
573                 frame_len = mp3_seek_next_header(map, numbytes, fpos, header);
574                 if (frame_len <= 0)
575                         return frame_len;
576         } else
577                 *fpos -= FRAME_HEADER_SIZE;
578         if (frame_len <= 1)
579                 return -E_FRAME_LENGTH;
580         return frame_len;
581 }
582
583 static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
584                 struct afh_info *afhi)
585 {
586         uint64_t freq_sum = 0, br_sum = 0;
587         int fl = 0, ret, len = 0, old_br = -1, vbr = 0;
588         struct timeval total_time = {0, 0};
589         unsigned chunk_table_size = 1000; /* gets increased on demand */
590         off_t fpos = 0;
591         struct mp3header header;
592         const char *tag_versions[] = {"no", "id3v1", "id3v2", "id3v1+id3v2"};
593
594         afhi->chunks_total = 0;
595         afhi->chunk_table = arr_alloc(chunk_table_size, sizeof(uint32_t));
596         while (1) {
597                 int freq, br;
598                 struct timeval tmp, cct; /* current chunk time */
599                 fpos += len;
600                 len = find_valid_start(map, numbytes, &fpos, &header);
601                 if (len <= 0) {
602                         size_t end;
603                         ret = -E_MP3_INFO;
604                         if (!afhi->chunks_total)
605                                 goto err_out;
606                         end = afhi->chunk_table[afhi->chunks_total - 1] + fl;
607                         afhi->chunk_table[afhi->chunks_total]
608                                 = PARA_MIN(end, numbytes);
609                         break;
610                 }
611                 ret = header_frequency(&header);
612                 if (ret < 0)
613                         continue;
614                 freq = ret;
615                 ret = header_bitrate(&header);
616                 if (ret < 0)
617                         continue;
618                 br = ret;
619                 ret = frame_length(&header);
620                 if (ret < 0)
621                         continue;
622                 fl = ret;
623                 tmp.tv_sec = fl - header.padding;
624                 tmp.tv_usec = 0;
625                 tv_divide(br * 125, &tmp, &cct);
626                 tv_add(&cct, &total_time, &tmp);
627                 total_time = tmp;
628                 if (afhi->chunks_total >= chunk_table_size) {
629                         chunk_table_size *= 2;
630                         afhi->chunk_table = arr_realloc(afhi->chunk_table,
631                                 chunk_table_size, sizeof(uint32_t));
632                 }
633                 afhi->chunk_table[afhi->chunks_total] = fpos;
634                 afhi->chunks_total++;
635                 freq_sum += freq;
636                 br_sum += br;
637                 if (afhi->chunks_total != 1 && old_br != br)
638                         vbr = 1;
639                 old_br = br;
640         }
641         ret = -E_MP3_INFO;
642         if (!freq_sum || !br_sum)
643                 goto err_out;
644         afhi->bitrate = br_sum / afhi->chunks_total;
645         afhi->frequency = freq_sum / afhi->chunks_total;
646         afhi->channels = header_channels(&header);
647         afhi->seconds_total = (tv2ms(&total_time) + 500) / 1000;
648         tv_divide(afhi->chunks_total, &total_time, &afhi->chunk_tv);
649         PARA_DEBUG_LOG("%" PRIu32 " chunks, each %lums\n", afhi->chunks_total,
650                 tv2ms(&afhi->chunk_tv));
651         set_max_chunk_size(afhi);
652         ret = mp3_get_id3(map, numbytes, fd, &afhi->tags);
653         afhi->techinfo = make_message("%cbr, %s, %s tags", vbr? 'v' : 'c',
654                 header_mode(&header), tag_versions[ret]);
655         return 1;
656 err_out:
657         free(afhi->chunk_table);
658         return ret;
659 }
660
661 /*
662  * Read mp3 information from audio file
663  */
664 static int mp3_get_file_info(char *map, size_t numbytes, int fd,
665                 struct afh_info *afhi)
666 {
667         int ret;
668
669         ret = mp3_read_info((unsigned char *)map, numbytes, fd, afhi);
670         if (ret < 0)
671                 return ret;
672         if (afhi->chunks_total == 0)
673                 return -E_MP3_INFO;
674         return 1;
675 }
676
677 static const char * const mp3_suffixes[] = {"mp3", NULL};
678
679 /**
680  * The mp3 audio format handler.
681  *
682  * It does not depend on any libraries and is hence always compiled in.
683  */
684 const struct audio_format_handler mp3_afh = {
685         .get_file_info = mp3_get_file_info,
686         .suffixes = mp3_suffixes,
687 #ifdef HAVE_LIBID3TAG
688         .rewrite_tags = mp3_rewrite_tags,
689 #endif /* HAVE_LIBID3TAG */
690 };