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