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