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