File: mp3.c

package info (click to toggle)
moc 1%3A2.6.0~svn-r3005-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,656 kB
  • sloc: ansic: 31,749; sh: 929; cpp: 487; makefile: 240
file content (802 lines) | stat: -rw-r--r-- 18,255 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
 * MOC - music on console
 * Copyright (C) 2002 - 2006 Damian Pietras <daper@daper.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

/* This code was based on madlld.c (C) by Bertrand Petit including code
 * from xmms-mad (C) by Sam Clegg and winamp plugin for madlib (C) by
 * Robert Leslie. */

/* FIXME: there can be a bit of silence in mp3 at the end or at the
 * beginning. If you hear gaps between files, it's the file's fault.
 * Can we strip this silence? */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <mad.h>
#include <id3tag.h>
#include <assert.h>
#ifdef HAVE_ICONV
# include <iconv.h>
#endif

#define DEBUG

#include "common.h"
#include "log.h"
#include "xing.h"
#include "audio.h"
#include "decoder.h"
#include "io.h"
#include "options.h"
#include "files.h"
#include "utf8.h"
#include "rcc.h"

#define INPUT_BUFFER	(32 * 1024)

static iconv_t iconv_id3_fix;

struct mp3_data
{
	struct io_stream *io_stream;
	unsigned long bitrate;
	long avg_bitrate;

	unsigned int freq;
	short channels;
	signed long duration;	/* Total time of the file in seconds
	                           (used for seeking). */
	off_t size;				/* Size of the file */

	unsigned char in_buff[INPUT_BUFFER + MAD_BUFFER_GUARD];

	struct mad_stream stream;
	struct mad_frame frame;
	struct mad_synth synth;

	int skip_frames; /* how many frames to skip (after seeking) */

	int ok; /* was this stream successfully opened? */
	struct decoder_error error;
};

/* Fill in the mad buffer, return number of bytes read, 0 on eof or error */
static size_t fill_buff (struct mp3_data *data)
{
	size_t remaining;
	ssize_t read_size;
	unsigned char *read_start;

	if (data->stream.next_frame != NULL) {
		remaining = data->stream.bufend - data->stream.next_frame;
		memmove (data->in_buff, data->stream.next_frame, remaining);
		read_start = data->in_buff + remaining;
		read_size = INPUT_BUFFER - remaining;
	}
	else {
		read_start = data->in_buff;
		read_size = INPUT_BUFFER;
		remaining = 0;
	}

	read_size = io_read (data->io_stream, read_start, read_size);
	if (read_size < 0) {
		decoder_error (&data->error, ERROR_FATAL, 0,
				"read error: %s", io_strerror(data->io_stream));
		return 0;
	}
	else if (read_size == 0)
		return 0;

	if (io_eof (data->io_stream)) {
		memset (read_start + read_size, 0, MAD_BUFFER_GUARD);
		read_size += MAD_BUFFER_GUARD;
	}

	mad_stream_buffer(&data->stream, data->in_buff, read_size + remaining);
	data->stream.error = 0;

	return read_size;
}

static char *id3v1_fix (const char *str)
{
	if (iconv_id3_fix != (iconv_t)-1)
		return iconv_str (iconv_id3_fix, str);
	return xstrdup (str);
}

int __unique_frame (struct id3_tag *tag, struct id3_frame *frame)
{
    unsigned int i;

    for (i = 0; i < tag->nframes; i++) {
        if (tag->frames[i] == frame) {
            break;
        }
    }

    for (; i < tag->nframes; i++) {
        if (strcmp(tag->frames[i]->id, frame->id) == 0) {
            return 0;
        }
    }

    return 1;
}

static char *get_tag (struct id3_tag *tag, const char *what)
{
	struct id3_frame *frame;
	union id3_field *field;
	const id3_ucs4_t *ucs4;
	char *comm = NULL;

	frame = id3_tag_findframe (tag, what, 0);
	if (frame && (field = &frame->fields[1])) {
		ucs4 = id3_field_getstrings (field, 0);
		if (ucs4) {
			/* Workaround for ID3 tags v1/v1.1 where the encoding
			 * is latin1. */
			union id3_field *encoding_field = &frame->fields[0];
			if (((id3_tag_options(tag, 0, 0) & ID3_TAG_OPTION_ID3V1) &&
						__unique_frame(tag, frame))
					|| ((options_get_bool ("EnforceTagsEncoding") &&
							(id3_field_gettextencoding((encoding_field))
							 == ID3_FIELD_TEXTENCODING_ISO_8859_1))))
			{
				char *t;

				comm = (char *)id3_ucs4_latin1duplicate (ucs4);

#ifdef HAVE_RCC
				if (options_get_bool("UseRCC"))
					comm = rcc_reencode (comm);
				else {
#endif /* HAVE_RCC */
					t = comm;
					comm = id3v1_fix (comm);
					free (t);
#ifdef HAVE_RCC
				}
#endif /* HAVE_RCC */
			}
			else
				comm = (char *)id3_ucs4_utf8duplicate (ucs4);
		}
	}

	return comm;
}

static int count_time_internal (struct mp3_data *data)
{
	struct xing xing;
	unsigned long bitrate = 0;
	int has_xing = 0;
	int is_vbr = 0;
	int num_frames = 0;
	mad_timer_t duration = mad_timer_zero;
	struct mad_header header;
	int good_header = 0; /* Have we decoded any header? */

	mad_header_init (&header);
	xing_init (&xing);

	/* There are three ways of calculating the length of an mp3:
	  1) Constant bitrate: One frame can provide the information
		 needed: # of frames and duration. Just see how long it
		 is and do the division.
	  2) Variable bitrate: Xing tag. It provides the number of
		 frames. Each frame has the same number of samples, so
		 just use that.
	  3) All: Count up the frames and duration of each frame
		 by decoding each one. We do this if we've no other
		 choice, i.e. if it's a VBR file with no Xing tag.
	*/

	while (1) {

		/* Fill the input buffer if needed */
		if (data->stream.buffer == NULL ||
			data->stream.error == MAD_ERROR_BUFLEN) {
			if (!fill_buff(data))
				break;
		}

		if (mad_header_decode(&header, &data->stream) == -1) {
			if (MAD_RECOVERABLE(data->stream.error))
				continue;
			else if (data->stream.error == MAD_ERROR_BUFLEN)
				continue;
			else {
				debug ("Can't decode header: %s",
				        mad_stream_errorstr(&data->stream));
				break;
			}
		}

		good_header = 1;

		/* Limit xing testing to the first frame header */
		if (!num_frames++) {
			if (xing_parse(&xing, data->stream.anc_ptr,
						data->stream.anc_bitlen)
					!= -1) {
				is_vbr = 1;

				debug ("Has XING header");

				if (xing.flags & XING_FRAMES) {
					has_xing = 1;
					num_frames = xing.frames;
					break;
				}
				debug ("XING header doesn't contain number of frames.");
			}
		}

		/* Test the first n frames to see if this is a VBR file */
		if (!is_vbr && !(num_frames > 20)) {
			if (bitrate && header.bitrate != bitrate) {
				debug ("Detected VBR after %d frames", num_frames);
				is_vbr = 1;
			}
			else
				bitrate = header.bitrate;
		}

		/* We have to assume it's not a VBR file if it hasn't already
		 * been marked as one and we've checked n frames for different
		 * bitrates */
		else if (!is_vbr) {
			debug ("Fixed rate MP3");
			break;
		}

		mad_timer_add (&duration, header.duration);
	}

	if (!good_header)
		return -1;

	if (data->size == -1) {
		mad_header_finish(&header);
		return -1;
	}

	if (!is_vbr) {
		/* time in seconds */
		double time = (data->size * 8.0) / (header.bitrate);

		double timefrac = (double)time - ((long)(time));

		/* samples per frame */
		long nsamples = 32 * MAD_NSBSAMPLES(&header);

		/* samplerate is a constant */
		num_frames = (long) (time * header.samplerate / nsamples);

		/* the average bitrate is the constant bitrate */
		data->avg_bitrate = bitrate;

		mad_timer_set(&duration, (long)time, (long)(timefrac*100),
				100);
	}

	else if (has_xing) {
		mad_timer_multiply (&header.duration, num_frames);
		duration = header.duration;
	}
	else {
		/* the durations have been added up, and the number of frames
		   counted. We do nothing here. */
		debug ("Counted duration by counting frames durations in VBR file.");
	}

	if (data->avg_bitrate == -1
			&& mad_timer_count(duration, MAD_UNITS_SECONDS) > 0) {
		data->avg_bitrate = data->size
				/ mad_timer_count(duration, MAD_UNITS_SECONDS) * 8;
	}

	mad_header_finish(&header);

	debug ("MP3 time: %ld", mad_timer_count (duration, MAD_UNITS_SECONDS));

	return mad_timer_count (duration, MAD_UNITS_SECONDS);
}

static struct mp3_data *mp3_open_internal (const char *file,
		const int buffered)
{
	struct mp3_data *data;

	data = (struct mp3_data *)xmalloc (sizeof(struct mp3_data));
	data->ok = 0;
	decoder_error_init (&data->error);

	/* Reset information about the file */
	data->freq = 0;
	data->channels = 0;
	data->skip_frames = 0;
	data->bitrate = -1;
	data->avg_bitrate = -1;

	/* Open the file */
	data->io_stream = io_open (file, buffered);
	if (io_ok(data->io_stream)) {
		data->ok = 1;

		data->size = io_file_size (data->io_stream);

		mad_stream_init (&data->stream);
		mad_frame_init (&data->frame);
		mad_synth_init (&data->synth);

		if (options_get_bool ("MP3IgnoreCRCErrors"))
				mad_stream_options (&data->stream,
					MAD_OPTION_IGNORECRC);

		data->duration = count_time_internal (data);
		mad_frame_mute (&data->frame);
		data->stream.next_frame = NULL;
		data->stream.sync = 0;
		data->stream.error = MAD_ERROR_NONE;

		if (io_seek(data->io_stream, 0, SEEK_SET) == -1) {
			decoder_error (&data->error, ERROR_FATAL, 0, "seek failed");
			mad_stream_finish (&data->stream);
			mad_frame_finish (&data->frame);
			mad_synth_finish (&data->synth);
			data->ok = 0;
		}

		data->stream.error = MAD_ERROR_BUFLEN;
	}
	else {
		decoder_error (&data->error, ERROR_FATAL, 0, "Can't open: %s",
				io_strerror(data->io_stream));
	}

	return data;
}

static void *mp3_open (const char *file)
{
	return mp3_open_internal (file, 1);
}

static void *mp3_open_stream (struct io_stream *stream)
{
	struct mp3_data *data;

	data = (struct mp3_data *)xmalloc (sizeof(struct mp3_data));
	data->ok = 1;
	decoder_error_init (&data->error);

	/* Reset information about the file */
	data->freq = 0;
	data->channels = 0;
	data->skip_frames = 0;
	data->bitrate = -1;
	data->io_stream = stream;
	data->duration = -1;
	data->size = -1;

	mad_stream_init (&data->stream);
	mad_frame_init (&data->frame);
	mad_synth_init (&data->synth);

	if (options_get_bool ("MP3IgnoreCRCErrors"))
			mad_stream_options (&data->stream,
				MAD_OPTION_IGNORECRC);

	return data;
}

static void mp3_close (void *void_data)
{
	struct mp3_data *data = (struct mp3_data *)void_data;

	if (data->ok) {
		mad_stream_finish (&data->stream);
		mad_frame_finish (&data->frame);
		mad_synth_finish (&data->synth);
	}
	io_close (data->io_stream);
	decoder_error_clear (&data->error);
	free (data);
}

/* Get the time for mp3 file, return -1 on error.
 * Adapted from mpg321. */
static int count_time (const char *file)
{
	struct mp3_data *data;
	int time;

	debug ("Processing file %s", file);

	data = mp3_open_internal (file, 0);

	if (!data->ok)
		time = -1;
	else
		time = data->duration;

	mp3_close (data);

	return time;
}

/* Fill info structure with data from the id3 tag */
static void mp3_info (const char *file_name, struct file_tags *info,
		const int tags_sel)
{
	if (tags_sel & TAGS_COMMENTS) {
		struct id3_tag *tag;
		struct id3_file *id3file;
		char *track = NULL;

		id3file = id3_file_open (file_name, ID3_FILE_MODE_READONLY);
		if (!id3file)
			return;
		tag = id3_file_tag (id3file);
		if (tag) {
			info->artist = get_tag (tag, ID3_FRAME_ARTIST);
			info->title = get_tag (tag, ID3_FRAME_TITLE);
			info->album = get_tag (tag, ID3_FRAME_ALBUM);
			track = get_tag (tag, ID3_FRAME_TRACK);

			if (track) {
				char *end;

				info->track = strtol (track, &end, 10);
				if (end == track)
					info->track = -1;
				free (track);
			}
		}
		id3_file_close (id3file);
	}

	if (tags_sel & TAGS_TIME)
		info->time = count_time (file_name);
}

static inline int32_t round_sample (mad_fixed_t sample)
{
	sample += 1L << (MAD_F_FRACBITS - 24);

	sample = CLAMP(-MAD_F_ONE, sample, MAD_F_ONE - 1);

	return sample >> (MAD_F_FRACBITS + 1 - 24);
}

static int put_output (char *buf, int buf_len, struct mad_pcm *pcm,
		struct mad_header *header)
{
	unsigned int nsamples;
	mad_fixed_t const *left_ch, *right_ch;
	int olen;

	nsamples = pcm->length;
	left_ch = pcm->samples[0];
	right_ch = pcm->samples[1];
	olen = nsamples * MAD_NCHANNELS (header) * 4;

	if (olen > buf_len) {
		logit ("PCM buffer to small!");
		return 0;
	}

	while (nsamples--) {
		long sample0 = round_sample (*left_ch++);

		buf[0] = 0;
		buf[1] = sample0;
		buf[2] = sample0 >> 8;
		buf[3] = sample0 >> 16;
		buf += 4;

		if (MAD_NCHANNELS(header) == 2) {
			long sample1;

			sample1 = round_sample (*right_ch++);

			buf[0] = 0;
			buf[1] = sample1;
			buf[2] = sample1 >> 8;
			buf[3] = sample1 >> 16;

			buf += 4;
		}
	}

	return olen;
}

/* If the current frame in the stream is an ID3 tag, then swallow it. */
static ssize_t flush_id3_tag (struct mp3_data *data)
{
	size_t remaining;
	ssize_t tag_size;

	remaining = data->stream.bufend - data->stream.next_frame;
	tag_size = id3_tag_query (data->stream.this_frame, remaining);
	if (tag_size > 0) {
		mad_stream_skip (&data->stream, tag_size);
		mad_stream_sync (&data->stream);
	}

	return tag_size;
}

static int mp3_decode (void *void_data, char *buf, int buf_len,
		struct sound_params *sound_params)
{
	struct mp3_data *data = (struct mp3_data *)void_data;

	decoder_error_clear (&data->error);

	while (1) {

		/* Fill the input buffer if needed */
		if (data->stream.buffer == NULL ||
			data->stream.error == MAD_ERROR_BUFLEN) {
			if (!fill_buff(data))
				return 0;
		}

		if (mad_frame_decode (&data->frame, &data->stream)) {
			if (flush_id3_tag (data))
				continue;
			else if (MAD_RECOVERABLE(data->stream.error)) {

				/* Ignore LOSTSYNC */
				if (data->stream.error == MAD_ERROR_LOSTSYNC)
					continue;

				if (!data->skip_frames)
					decoder_error (&data->error, ERROR_STREAM, 0,
							"Broken frame: %s",
							mad_stream_errorstr(&data->stream));
				continue;
			}
			else if (data->stream.error == MAD_ERROR_BUFLEN)
				continue;
			else {
				decoder_error (&data->error, ERROR_FATAL, 0,
						"Broken frame: %s",
						mad_stream_errorstr(&data->stream));
				return 0;
			}
		}

		if (data->skip_frames) {
			data->skip_frames--;
			continue;
		}

		/* Sound parameters. */
		if (!(sound_params->rate = data->frame.header.samplerate)) {
			decoder_error (&data->error, ERROR_FATAL, 0,
					"Broken file: information about the"
					" frequency couldn't be read.");
			return 0;
		}

		sound_params->channels = MAD_NCHANNELS(&data->frame.header);
		sound_params->fmt = SFMT_S32 | SFMT_LE;

		/* Change of the bitrate? */
		if (data->frame.header.bitrate != data->bitrate) {
			if ((data->bitrate = data->frame.header.bitrate) == 0) {
				decoder_error (&data->error, ERROR_FATAL, 0,
						"Broken file: information about the"
						" bitrate couldn't be read.");
				return 0;
			}
		}

		mad_synth_frame (&data->synth, &data->frame);
		mad_stream_sync (&data->stream);

		return put_output (buf, buf_len, &data->synth.pcm,
				&data->frame.header);
	}
}

static int mp3_seek (void *void_data, int sec)
{
	struct mp3_data *data = (struct mp3_data *)void_data;
	off_t new_position;

	assert (sec >= 0);

	if (data->size == -1)
		return -1;

	if (sec >= data->duration)
		return -1;

	new_position = ((double) sec /
			(double) data->duration) * data->size;

	debug ("Seeking to %d (byte %"PRId64")", sec, new_position);

	if (new_position < 0)
		new_position = 0;
	else if (new_position >= data->size)
		return -1;

	if (io_seek(data->io_stream, new_position, SEEK_SET) == -1) {
		logit ("seek to %"PRId64" failed", new_position);
		return -1;
	}

	data->stream.error = MAD_ERROR_BUFLEN;

	mad_frame_mute (&data->frame);
	mad_synth_mute (&data->synth);

	data->stream.sync = 0;
	data->stream.next_frame = NULL;

	data->skip_frames = 2;

	return sec;
}

static int mp3_get_bitrate (void *void_data)
{
	struct mp3_data *data = (struct mp3_data *)void_data;

	return data->bitrate / 1000;
}

static int mp3_get_avg_bitrate (void *void_data)
{
	struct mp3_data *data = (struct mp3_data *)void_data;

	return data->avg_bitrate / 1000;
}

static int mp3_get_duration (void *void_data)
{
	struct mp3_data *data = (struct mp3_data *)void_data;

	return data->duration;
}

static void mp3_get_name (const char *file, char buf[4])
{
	char *ext;

	strcpy (buf, "MPx");

	ext = ext_pos (file);
	if (ext) {
		if (!strcasecmp (ext, "mp3"))
			strcpy (buf, "MP3");
		else if (!strcasecmp (ext, "mp2"))
			strcpy (buf, "MP2");
		else if (!strcasecmp (ext, "mp1"))
			strcpy (buf, "MP1");
		else if (!strcasecmp (ext, "mpga"))
			strcpy (buf, "MPG");
	}
}

static int mp3_our_format_ext (const char *ext)
{
	return !strcasecmp (ext, "mp3")
		|| !strcasecmp (ext, "mpga")
		|| !strcasecmp (ext, "mp2")
		|| !strcasecmp (ext, "mp1");
}

static void mp3_get_error (void *prv_data, struct decoder_error *error)
{
	struct mp3_data *data = (struct mp3_data *)prv_data;

	decoder_error_copy (error, &data->error);
}

static struct io_stream *mp3_get_stream (void *prv_data)
{
	struct mp3_data *data = (struct mp3_data *)prv_data;

	return data->io_stream;
}

static int mp3_our_mime (const char *mime)
{
	return !strcasecmp (mime, "audio/mpeg")
		|| !strncasecmp (mime, "audio/mpeg;", 11);
}

static int mp3_can_decode (struct io_stream *stream)
{
	unsigned char buf[16 * 1024];

	/* We must use such a sophisticated test, because there are Shoutcast
	 * servers that can start broadcasting in the middle of a frame, so we
	 * can't use any fewer bytes for magic values. */
	if (io_peek(stream, buf, sizeof(buf)) == sizeof(buf)) {
		struct mad_stream stream;
		struct mad_header header;
		int dec_res;

		mad_stream_init (&stream);
		mad_header_init (&header);

		mad_stream_buffer (&stream, buf, sizeof(buf));
		stream.error = 0;

		while ((dec_res = mad_header_decode(&header, &stream)) == -1
				&& MAD_RECOVERABLE(stream.error))
			;

		return dec_res != -1 ? 1 : 0;
	}

	return 0;
}

static void mp3_init ()
{
	iconv_id3_fix = iconv_open ("UTF-8",
			options_get_str("ID3v1TagsEncoding"));
	if (iconv_id3_fix == (iconv_t)(-1))
		log_errno ("iconv_open() failed", errno);
}

static void mp3_destroy ()
{
	if (iconv_close(iconv_id3_fix) == -1)
		log_errno ("iconv_close() failed", errno);
}

static struct decoder mp3_decoder = {
	DECODER_API_VERSION,
	mp3_init,
	mp3_destroy,
	mp3_open,
	mp3_open_stream,
	mp3_can_decode,
	mp3_close,
	mp3_decode,
	mp3_seek,
	mp3_info,
	mp3_get_bitrate,
	mp3_get_duration,
	mp3_get_error,
	mp3_our_format_ext,
	mp3_our_mime,
	mp3_get_name,
	NULL,
	mp3_get_stream,
	mp3_get_avg_bitrate
};

struct decoder *plugin_init ()
{
	return &mp3_decoder;
}