aboutsummaryrefslogtreecommitdiffstats
path: root/src/inputPlugins/mp3_plugin.c
blob: b122a0b2357535ee632be5aa4b73b5dab0bd427f (plain) (blame)
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
/* the Music Player Daemon (MPD)
 * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
 * This project's homepage is: http://www.musicpd.org
 *
 * 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 program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "../inputPlugin.h"

#ifdef HAVE_MAD

#include "../pcm_utils.h"
#ifdef USE_MPD_MAD
#include "../libmad/mad.h"
#else
#include <mad.h>
#endif
#ifdef HAVE_ID3TAG
#ifdef USE_MPD_ID3TAG
#include "../libid3tag/id3tag.h"
#else
#include <id3tag.h>
#endif
#endif
#include "../log.h"
#include "../utils.h"
#include "../tag.h"

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#define FRAMES_CUSHION		2000

#define READ_BUFFER_SIZE	40960

#define DECODE_SKIP		-3
#define DECODE_BREAK		-2
#define DECODE_CONT		-1
#define DECODE_OK		0

#define MUTEFRAME_SKIP          1
#define MUTEFRAME_SEEK          2

/* this is stolen from mpg321! */
struct audio_dither {
	mad_fixed_t error[3];
	mad_fixed_t random;
};

unsigned long prng(unsigned long state) {
	return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
}

signed long audio_linear_dither(unsigned int bits, mad_fixed_t sample, struct audio_dither *dither) {
	unsigned int scalebits;
	mad_fixed_t output, mask, random;

	enum {
		MIN = -MAD_F_ONE,
		MAX =  MAD_F_ONE - 1
	};

	sample += dither->error[0] - dither->error[1] + dither->error[2];

	dither->error[2] = dither->error[1];
	dither->error[1] = dither->error[0] / 2;

	output = sample + (1L << (MAD_F_FRACBITS + 1 - bits - 1));

	scalebits = MAD_F_FRACBITS + 1 - bits;
	mask = (1L << scalebits) - 1;

	random  = prng(dither->random);
	output += (random & mask) - (dither->random & mask);

	dither->random = random;

	if (output > MAX) {
		output = MAX;

		if (sample > MAX)
			sample = MAX;
	}
	else if (output < MIN) {
	        output = MIN;

		if (sample < MIN)
			sample = MIN;
	}

	output &= ~mask;

	dither->error[0] = sample - output;

	return output >> scalebits;
}
/* end of stolen stuff from mpg321 */

/* decoder stuff is based on madlld */

#define MP3_DATA_OUTPUT_BUFFER_SIZE 4096

typedef struct _mp3DecodeData {
	struct mad_stream stream;
	struct mad_frame frame;
	struct mad_synth synth;
	mad_timer_t timer;
	unsigned char readBuffer[READ_BUFFER_SIZE];
	char outputBuffer[MP3_DATA_OUTPUT_BUFFER_SIZE];
	char * outputPtr;
	char * outputBufferEnd;
	float totalTime;
	float elapsedTime;
	int muteFrame;
	long * frameOffset;
	mad_timer_t * times;
	long highestFrame;
	long maxFrames;
	long currentFrame;
	int flush;
	unsigned long bitRate;
	InputStream * inStream;
} mp3DecodeData;

void initMp3DecodeData(mp3DecodeData * data, InputStream * inStream) {
	data->outputPtr = data->outputBuffer;
	data->outputBufferEnd = data->outputBuffer+MP3_DATA_OUTPUT_BUFFER_SIZE;
	data->muteFrame = 0;
	data->highestFrame = 0;
	data->maxFrames = 0;
	data->frameOffset = NULL;
	data->times = NULL;
	data->currentFrame = 0;
	data->flush = 1;
        data->inStream = inStream;

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

int seekMp3InputBuffer(mp3DecodeData * data, long offset) {
	if(seekInputStream(data->inStream,offset,SEEK_SET) < 0) {
                return -1;
        }

	mad_stream_buffer(&data->stream,data->readBuffer,0);
	(data->stream).error = 0;

	return 0;
}

int fillMp3InputBuffer(mp3DecodeData * data) {
	size_t readSize;
	size_t remaining;
        size_t readed;
	unsigned char * readStart;

	if((data->stream).next_frame!=NULL) {
		remaining = (data->stream).bufend-(data->stream).next_frame;
		memmove(data->readBuffer,(data->stream).next_frame,remaining);
		readStart = (data->readBuffer)+remaining;
		readSize = READ_BUFFER_SIZE-remaining;
	}
	else {
		readSize = READ_BUFFER_SIZE;
		readStart = data->readBuffer,
		remaining = 0;
	}

	readed = readFromInputStream(data->inStream, readStart, (size_t)1, 
			readSize);
	if(readed <= 0 && inputStreamAtEOF(data->inStream)) return -1;
	/* sleep for a fraction of a second! */
	else if(readed <= 0) my_usleep(10000);

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

	return 0;
}

#ifdef HAVE_ID3TAG
static MpdTag * mp3_parseId3Tag(mp3DecodeData * data, signed long tagsize) {
	MpdTag * ret = NULL;
	struct id3_tag * id3Tag = NULL;
	id3_length_t count;
	id3_byte_t const *id3_data;
	id3_byte_t * allocated = NULL;

	count = data->stream.bufend - data->stream.this_frame;

	if(tagsize <= count) {
		id3_data = data->stream.this_frame;
		mad_stream_skip(&(data->stream), tagsize);
	}
	else {
		allocated = malloc(tagsize);
		if(!allocated) goto fail;

		memcpy(allocated, data->stream.this_frame, count);
		mad_stream_skip(&(data->stream), count);

		while(count < tagsize) {
			int len;

			len = readFromInputStream(data->inStream, 
				allocated+count, (size_t)1, 
				tagsize-count);
			if(len <= 0 && inputStreamAtEOF(data->inStream))				{
				break;
			}
			else if(len <= 0) my_usleep(10000);
			else count += len;
		}

		if(count != tagsize) {
			DEBUG("mp3_decode: error parsing ID3 tag\n");
			goto fail;
		}

		id3_data = allocated;
	}

	id3Tag = id3_tag_parse(id3_data, tagsize);
 
	ret = parseId3Tag(id3Tag);

fail:
	if(allocated) free(allocated);
	return ret;
}
#endif

int decodeNextFrameHeader(mp3DecodeData * data, MpdTag ** tag) {
	if((data->stream).buffer==NULL || (data->stream).error==MAD_ERROR_BUFLEN) {
		if(fillMp3InputBuffer(data) < 0) {
			return DECODE_BREAK;
		}
	}
	if(mad_header_decode(&data->frame.header,&data->stream)) {
#ifdef HAVE_ID3TAG
		if((data->stream).error==MAD_ERROR_LOSTSYNC && 
				(data->stream).this_frame) 
		{
			signed long tagsize = id3_tag_query(
					(data->stream).this_frame,
					(data->stream).bufend-
					(data->stream).this_frame);

			if(tagsize>0) {
				if(tag && !(*tag)) {
					*tag =mp3_parseId3Tag(data, tagsize);
				}
				else {
					mad_stream_skip(&(data->stream),
							tagsize);
				}
				return DECODE_CONT;
			}
		}
#endif
		if(MAD_RECOVERABLE((data->stream).error)) {
			return DECODE_SKIP;
		}
		else {
			if((data->stream).error==MAD_ERROR_BUFLEN) return DECODE_CONT;
			else
			{
				ERROR("unrecoverable frame level error "
					"(%s).\n",
					mad_stream_errorstr(&data->stream));
				data->flush = 0;
				return DECODE_BREAK;
			}
		}
	}

	return DECODE_OK;
}

int decodeNextFrame(mp3DecodeData * data) {
	if((data->stream).buffer==NULL || (data->stream).error==MAD_ERROR_BUFLEN) {
		if(fillMp3InputBuffer(data) < 0) {
			return DECODE_BREAK;
		}
	}
	if(mad_frame_decode(&data->frame,&data->stream)) {
#ifdef HAVE_ID3TAG
		if((data->stream).error==MAD_ERROR_LOSTSYNC) {
			signed long tagsize = id3_tag_query(
					(data->stream).this_frame,
					(data->stream).bufend-
					(data->stream).this_frame);
			if(tagsize>0) {
				mad_stream_skip(&(data->stream),tagsize);
				return DECODE_CONT;
			}
		}
#endif
		if(MAD_RECOVERABLE((data->stream).error)) {
			return DECODE_SKIP;
		}
		else {
			if((data->stream).error==MAD_ERROR_BUFLEN) return DECODE_CONT;
			else
			{
				ERROR("unrecoverable frame level error "
					"(%s).\n",
					mad_stream_errorstr(&data->stream));
				data->flush = 0;
				return DECODE_BREAK;
			}
		}
	}

	return DECODE_OK;
}

/* xing stuff stolen from alsaplayer */
# define XING_MAGIC	(('X' << 24) | ('i' << 16) | ('n' << 8) | 'g')

struct xing {
  	long flags;			/* valid fields (see below) */
  	unsigned long frames;		/* total number of frames */
  	unsigned long bytes;		/* total number of bytes */
  	unsigned char toc[100];		/* 100-point seek table */
  	long scale;			/* ?? */
};

enum {
  	XING_FRAMES = 0x00000001L,
  	XING_BYTES  = 0x00000002L,
  	XING_TOC    = 0x00000004L,
  	XING_SCALE  = 0x00000008L
};

int parse_xing(struct xing *xing, struct mad_bitptr ptr, unsigned int bitlen)
{
  	if (bitlen < 64 || mad_bit_read(&ptr, 32) != XING_MAGIC) goto fail;

  	xing->flags = mad_bit_read(&ptr, 32);
  	bitlen -= 64;

  	if (xing->flags & XING_FRAMES) {
    		if (bitlen < 32) goto fail;
    		xing->frames = mad_bit_read(&ptr, 32);
    		bitlen -= 32;
  	}

  	if (xing->flags & XING_BYTES) {
    		if (bitlen < 32) goto fail;
    		xing->bytes = mad_bit_read(&ptr, 32);
    		bitlen -= 32;
  	}

  	if (xing->flags & XING_TOC) {
    		int i;
    		if (bitlen < 800) goto fail;
      		for (i = 0; i < 100; ++i) xing->toc[i] = mad_bit_read(&ptr, 8);
    		bitlen -= 800;
  	}

  	if (xing->flags & XING_SCALE) {
    		if (bitlen < 32) goto fail;
    		xing->scale = mad_bit_read(&ptr, 32);
    		bitlen -= 32;
  	}

 	 return 1;

fail:
  	xing->flags = 0;
  	return 0;
}

int decodeFirstFrame(mp3DecodeData * data, DecoderControl * dc,
		MpdTag ** tag) 
{
	struct xing xing;
	int ret;
	int skip;

	memset(&xing,0,sizeof(struct xing));
	xing.flags = 0;

	while(1) {
		skip = 0;
		while((ret = decodeNextFrameHeader(data, tag))==DECODE_CONT && 
				(!dc || !dc->stop));
		if(ret==DECODE_SKIP) skip = 1;
		else if(ret==DECODE_BREAK || (dc && dc->stop)) return -1;
		while((ret = decodeNextFrame(data))==DECODE_CONT && 
				(!dc || !dc->stop));
		if(ret==DECODE_BREAK || (dc && dc->stop)) return -1;
		if(!skip && ret==DECODE_OK) break;
	}

	if(parse_xing(&xing,data->stream.anc_ptr,data->stream.anc_bitlen)) {
		if(xing.flags & XING_FRAMES) {
			mad_timer_t duration = data->frame.header.duration;
			mad_timer_multiply(&duration,xing.frames);
			data->muteFrame = MUTEFRAME_SKIP;
			data->totalTime = ((float)mad_timer_count(duration,
						MAD_UNITS_MILLISECONDS))/1000;
			data->maxFrames = xing.frames;
		}
	}
	else {
		size_t offset = data->inStream->offset;
		mad_timer_t duration = data->frame.header.duration;
		float frameTime = ((float)mad_timer_count(duration,
					MAD_UNITS_MILLISECONDS))/1000;
		if(data->stream.this_frame!=NULL) {
			offset-= data->stream.bufend-data->stream.this_frame;
		}
		else {
			offset-= data->stream.bufend-data->stream.buffer;
		}
		if(data->inStream->size >= offset) {
			data->totalTime = ((data->inStream->size-offset)*8.0)/
					(data->frame).header.bitrate;
			data->maxFrames = 
				data->totalTime/frameTime+FRAMES_CUSHION;
		}
		else {
			data->maxFrames = FRAMES_CUSHION;
			data->totalTime = 0;
		}
	}

	data->frameOffset = malloc(sizeof(long)*data->maxFrames);
	data->times = malloc(sizeof(mad_timer_t)*data->maxFrames);

	return 0;
}

void mp3DecodeDataFinalize(mp3DecodeData * data) {
	mad_synth_finish(&data->synth);
	mad_frame_finish(&data->frame);
	mad_stream_finish(&data->stream);

	if(data->frameOffset) free(data->frameOffset);
	if(data->times) free(data->times);
}

/* this is primarily used for getting total time for tags */
int getMp3TotalTime(char * file) {
        InputStream inStream;
	mp3DecodeData data;
	int ret;

        if(openInputStream(&inStream, file) < 0) return -1;
	initMp3DecodeData(&data,&inStream);
        data.stream.options |= MAD_OPTION_IGNORECRC;
	if(decodeFirstFrame(&data, NULL, NULL)<0) ret = -1;
	else ret = data.totalTime+0.5;
	mp3DecodeDataFinalize(&data);
	closeInputStream(&inStream);

	return ret;
}

int openMp3FromInputStream(InputStream * inStream, mp3DecodeData * data,
		DecoderControl * dc, MpdTag ** tag) 
{
	initMp3DecodeData(data, inStream);
        data->stream.options |= MAD_OPTION_IGNORECRC;
	*tag = NULL;
	if(decodeFirstFrame(data, dc, tag)<0) {
		mp3DecodeDataFinalize(data);
		if(tag && *tag) freeMpdTag(*tag);
		return -1;
	}

	return 0;
}

int mp3Read(mp3DecodeData * data, OutputBuffer * cb, DecoderControl * dc) {
	int i;
	int ret;
	struct audio_dither dither;
	int skip;

	if(data->currentFrame>=data->highestFrame) { 
		mad_timer_add(&data->timer,(data->frame).header.duration);
		data->bitRate = (data->frame).header.bitrate;
		if(data->currentFrame>=data->maxFrames) {
			data->currentFrame = data->maxFrames - 1;
		}
		else data->highestFrame++;
		data->frameOffset[data->currentFrame] = data->inStream->offset;
		if(data->stream.this_frame!=NULL) {
			data->frameOffset[data->currentFrame]-= 
					data->stream.bufend-
					data->stream.this_frame;
		}
		else {
			data->frameOffset[data->currentFrame]-= 
					data->stream.bufend-data->stream.buffer;
		}
		data->times[data->currentFrame] = data->timer;
	}
	else data->timer = data->times[data->currentFrame];
	data->currentFrame++;
	data->elapsedTime = ((float)mad_timer_count(data->timer,MAD_UNITS_MILLISECONDS))/1000;

	switch(data->muteFrame) {
        case MUTEFRAME_SKIP:
		data->muteFrame = 0;
                break;
        case MUTEFRAME_SEEK:
		if(dc->seekWhere<=data->elapsedTime) {
                        data->outputPtr = data->outputBuffer;
                        clearOutputBuffer(cb);
			data->muteFrame = 0;
			dc->seek = 0;
		}
                break;
        default:
		mad_synth_frame(&data->synth,&data->frame);

		if(data->inStream->metaTitle) {
			MpdTag * tag = newMpdTag();
			if(data->inStream->metaName) {
				tag->name = strdup(data->inStream->metaName);
			}
			tag->title = strdup(data->inStream->metaTitle);
			free(data->inStream->metaTitle);
			data->inStream->metaTitle = NULL;
			copyMpdTagToOutputBuffer(cb, tag);
			freeMpdTag(tag);
		}

		for(i=0;i<(data->synth).pcm.length;i++) {
			mpd_sint16 * sample;

			sample = (mpd_sint16 *)data->outputPtr;	
			*sample = (mpd_sint16) audio_linear_dither(16,
					(data->synth).pcm.samples[0][i],
					&dither);
			data->outputPtr+=2;

			if(MAD_NCHANNELS(&(data->frame).header)==2) {
				sample = (mpd_sint16 *)data->outputPtr;	
				*sample = (mpd_sint16) audio_linear_dither(16,
						(data->synth).pcm.samples[1][i],
						&dither);
				data->outputPtr+=2;
			}

			if(data->outputPtr==data->outputBufferEnd) {
                                long ret;
                                ret = sendDataToOutputBuffer(cb,
                                                data->inStream,
                                                dc,
                                                data->inStream->seekable,
                                                data->outputBuffer,
                                                MP3_DATA_OUTPUT_BUFFER_SIZE,
                                                data->elapsedTime,
                                                data->bitRate/1000);
                                data->outputPtr = data->outputBuffer;

                                if(ret == OUTPUT_BUFFER_DC_STOP) {
                                        return DECODE_BREAK;
                                }

                                if(ret == OUTPUT_BUFFER_DC_SEEK) break;
			}
		}

		if(dc->seek && data->inStream->seekable) {
			long i = 0;
			data->muteFrame = MUTEFRAME_SEEK;
			while(i<data->highestFrame && dc->seekWhere >
					((float)mad_timer_count(data->times[i],
					MAD_UNITS_MILLISECONDS))/1000) 
			{
				i++;
			}
			if(i<data->highestFrame) {
				if(seekMp3InputBuffer(data,
						data->frameOffset[i]) == 0)
                                {
                                        data->outputPtr = data->outputBuffer;
                                        clearOutputBuffer(cb);
				        data->currentFrame = i;
                                }
                                else dc->seekError = 1;
				data->muteFrame = 0;
				dc->seek = 0;
			}
		}
                else if(dc->seek && !data->inStream->seekable) {
                        dc->seek = 0;
                        dc->seekError = 1;
                }
	}

	while(1) {
		skip = 0;
		while((ret = decodeNextFrameHeader(data, NULL))==DECODE_CONT &&
				!dc->stop && !dc->seek);
		if(ret==DECODE_BREAK || dc->stop || dc->seek) break;
		else if(ret==DECODE_SKIP) skip = 1;
		if(!data->muteFrame) {
			while((ret = decodeNextFrame(data))==DECODE_CONT &&
					!dc->stop && !dc->seek);
			if(ret==DECODE_BREAK || dc->stop || dc->seek) break;
		}
		if(!skip && ret==DECODE_OK) break;
	}

	if(dc->stop) return DECODE_BREAK;

	return ret;
}

void initAudioFormatFromMp3DecodeData(mp3DecodeData * data, AudioFormat * af) {
	af->bits = 16;
	af->sampleRate = (data->frame).header.samplerate;
	af->channels = MAD_NCHANNELS(&(data->frame).header);
}

int mp3_decode(OutputBuffer * cb, DecoderControl * dc, InputStream * inStream) {
	mp3DecodeData data;
	MpdTag * tag = NULL;

	if(openMp3FromInputStream(inStream, &data, dc, &tag) < 0) {
		closeInputStream(inStream);
		if(!dc->stop) {
                        ERROR("Input does not appear to be a mp3 bit stream.\n");
		        return -1;
                }
                else {
                        dc->state = DECODE_STATE_STOP;
                        dc->stop = 0;
                }
                return 0;
	}

	initAudioFormatFromMp3DecodeData(&data, &(dc->audioFormat));
        getOutputAudioFormat(&(dc->audioFormat), &(cb->audioFormat));
        
	dc->totalTime = data.totalTime;

	/*if(inStream->metaTitle) {
		if(tag) freeMpdTag(tag);
		tag = newMpdTag();
		tag->title = strdup(inStream->metaTitle);
		free(inStream->metaTitle);
		inStream->metaTitle = NULL;
		if(inStream->metaName) {
			tag->name = strdup(inStream->metaName);
		}
		copyMpdTagToOutputBuffer(cb, tag);
		freeMpdTag(tag);
	}
	else if(tag) {
		if(inStream->metaName) {
			if(tag->name) free(tag->name);
			tag->name = strdup(inStream->metaName);
		}
		copyMpdTagToOutputBuffer(cb, tag);
		freeMpdTag(tag);
	}
	else if(inStream->metaName) {
		tag = newMpdTag();
		if(inStream->metaName) {
			tag->name = strdup(inStream->metaName);
		}
		copyMpdTagToOutputBuffer(cb, tag);
		freeMpdTag(tag);
	}*/

	dc->state = DECODE_STATE_DECODE;

	while(mp3Read(&data,cb,dc)!=DECODE_BREAK);
	/* send last little bit if not dc->stop */
	if(!dc->stop && data.outputPtr!=data.outputBuffer && data.flush)  {
        	sendDataToOutputBuffer(cb, NULL, dc, 
                                data.inStream->seekable,
                                data.outputBuffer,
                                data.outputPtr-data.outputBuffer,
                                data.elapsedTime,data.bitRate/1000);
	}

	flushOutputBuffer(cb);
	mp3DecodeDataFinalize(&data);
	closeInputStream(inStream);

	/*if(dc->seek) {
                dc->seekError = 1;
                dc->seek = 0;
        }*/

	if(dc->stop) {
		dc->state = DECODE_STATE_STOP;
		dc->stop = 0;
	}
	else dc->state = DECODE_STATE_STOP;
		
	return 0;
}

MpdTag * mp3_tagDup(char * file) {
	MpdTag * ret = NULL;
	int time;

	ret = id3Dup(file);

	time = getMp3TotalTime(file);

	if(time>=0) {
		if(!ret) ret = newMpdTag();
		ret->time = time;
	}

	return ret;
}

char * mp3_suffixes[] = {"mp3", NULL};
char * mp3_mimeTypes[] = {"audio/mpeg", NULL};

InputPlugin mp3Plugin = 
{
	"mp3",
	NULL,
	NULL,
	mp3_decode,
	NULL,
	mp3_tagDup,
	INPUT_PLUGIN_STREAM_FILE | INPUT_PLUGIN_STREAM_URL,
	mp3_suffixes,
	mp3_mimeTypes
};
#else

InputPlugin mp3Plugin = 
{
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	0,
	NULL,
	NULL
};

#endif