2004-02-24 00:41:20 +01:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2004-05-31 03:54:10 +02:00
|
|
|
#include "../inputPlugin.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-03-18 04:29:25 +01:00
|
|
|
#ifdef HAVE_FLAC
|
|
|
|
|
2004-05-31 03:54:10 +02:00
|
|
|
#include "../utils.h"
|
|
|
|
#include "../log.h"
|
|
|
|
#include "../pcm_utils.h"
|
|
|
|
#include "../inputStream.h"
|
|
|
|
#include "../outputBuffer.h"
|
|
|
|
#include "../replayGain.h"
|
|
|
|
#include "../audio.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2004-05-05 01:39:02 +02:00
|
|
|
#include <FLAC/seekable_stream_decoder.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
#include <FLAC/metadata.h>
|
|
|
|
|
|
|
|
typedef struct {
|
2004-05-11 00:31:23 +02:00
|
|
|
#define FLAC_CHUNK_SIZE 4080
|
|
|
|
unsigned char chunk[FLAC_CHUNK_SIZE];
|
2004-02-24 00:41:20 +01:00
|
|
|
int chunk_length;
|
|
|
|
float time;
|
2004-02-26 04:21:24 +01:00
|
|
|
int bitRate;
|
|
|
|
FLAC__uint64 position;
|
2004-05-07 17:58:04 +02:00
|
|
|
OutputBuffer * cb;
|
2004-02-24 00:41:20 +01:00
|
|
|
DecoderControl * dc;
|
2005-01-30 00:46:06 +01:00
|
|
|
InputStream * inStream;
|
2004-11-02 20:56:59 +01:00
|
|
|
ReplayGainInfo * replayGainInfo;
|
2004-02-24 00:41:20 +01:00
|
|
|
} FlacData;
|
|
|
|
|
|
|
|
/* this code is based on flac123, from flac-tools */
|
|
|
|
|
|
|
|
int flacSendChunk(FlacData * data);
|
2004-05-05 01:39:02 +02:00
|
|
|
void flacError(const FLAC__SeekableStreamDecoder *,
|
|
|
|
FLAC__StreamDecoderErrorStatus, void *);
|
2005-01-30 00:46:06 +01:00
|
|
|
void flacPrintErroredState(FLAC__SeekableStreamDecoderState state);
|
2004-05-05 01:39:02 +02:00
|
|
|
void flacMetadata(const FLAC__SeekableStreamDecoder *,
|
|
|
|
const FLAC__StreamMetadata *, void *);
|
|
|
|
FLAC__StreamDecoderWriteStatus flacWrite(const FLAC__SeekableStreamDecoder *,
|
|
|
|
const FLAC__Frame *, const FLAC__int32 * const buf[], void *);
|
|
|
|
FLAC__SeekableStreamDecoderReadStatus flacRead(
|
|
|
|
const FLAC__SeekableStreamDecoder *, FLAC__byte buf[],
|
|
|
|
unsigned *, void *);
|
|
|
|
FLAC__SeekableStreamDecoderSeekStatus flacSeek(
|
|
|
|
const FLAC__SeekableStreamDecoder *, FLAC__uint64, void *);
|
|
|
|
FLAC__SeekableStreamDecoderTellStatus flacTell(
|
|
|
|
const FLAC__SeekableStreamDecoder *, FLAC__uint64 *, void *);
|
|
|
|
FLAC__SeekableStreamDecoderLengthStatus flacLength(
|
|
|
|
const FLAC__SeekableStreamDecoder *, FLAC__uint64 *, void *);
|
|
|
|
FLAC__bool flacEOF(const FLAC__SeekableStreamDecoder *, void *);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2005-01-30 00:46:06 +01:00
|
|
|
int flac_decode(OutputBuffer * cb, DecoderControl * dc, InputStream * inStream)
|
|
|
|
{
|
2004-06-28 14:51:26 +02:00
|
|
|
FLAC__SeekableStreamDecoder * flacDec = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
FlacData data;
|
|
|
|
int status = 1;
|
2004-06-28 14:51:26 +02:00
|
|
|
int ret =0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
data.chunk_length = 0;
|
|
|
|
data.time = 0;
|
2004-02-26 04:21:24 +01:00
|
|
|
data.position = 0;
|
|
|
|
data.bitRate = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
data.cb = cb;
|
|
|
|
data.dc = dc;
|
2005-01-30 00:46:06 +01:00
|
|
|
data.inStream = inStream;
|
2004-11-02 20:56:59 +01:00
|
|
|
data.replayGainInfo = NULL;
|
2004-05-05 01:39:02 +02:00
|
|
|
|
2004-06-28 14:51:26 +02:00
|
|
|
if(!(flacDec = FLAC__seekable_stream_decoder_new())) {
|
|
|
|
ret = -1;
|
|
|
|
goto fail;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
/*status&=FLAC__file_decoder_set_md5_checking(flacDec,1);*/
|
2004-05-05 01:39:02 +02:00
|
|
|
status&=FLAC__seekable_stream_decoder_set_read_callback(flacDec,
|
|
|
|
flacRead);
|
|
|
|
status&=FLAC__seekable_stream_decoder_set_seek_callback(flacDec,
|
|
|
|
flacSeek);
|
|
|
|
status&=FLAC__seekable_stream_decoder_set_tell_callback(flacDec,
|
|
|
|
flacTell);
|
|
|
|
status&=FLAC__seekable_stream_decoder_set_length_callback(flacDec,
|
|
|
|
flacLength);
|
|
|
|
status&=FLAC__seekable_stream_decoder_set_eof_callback(flacDec,flacEOF);
|
|
|
|
status&=FLAC__seekable_stream_decoder_set_write_callback(flacDec,
|
|
|
|
flacWrite);
|
|
|
|
status&=FLAC__seekable_stream_decoder_set_metadata_callback(flacDec,
|
|
|
|
flacMetadata);
|
2004-06-29 01:40:08 +02:00
|
|
|
status&=FLAC__seekable_stream_decoder_set_metadata_respond(flacDec,
|
|
|
|
FLAC__METADATA_TYPE_VORBIS_COMMENT);
|
2004-05-05 01:39:02 +02:00
|
|
|
status&=FLAC__seekable_stream_decoder_set_error_callback(flacDec,
|
|
|
|
flacError);
|
|
|
|
status&=FLAC__seekable_stream_decoder_set_client_data(flacDec,
|
|
|
|
(void *)&data);
|
2004-02-24 00:41:20 +01:00
|
|
|
if(!status) {
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac problem before init()\n");
|
2004-05-05 01:39:02 +02:00
|
|
|
flacPrintErroredState(
|
2005-01-30 00:46:06 +01:00
|
|
|
FLAC__seekable_stream_decoder_get_state(flacDec));
|
2004-06-28 14:51:26 +02:00
|
|
|
ret = -1;
|
|
|
|
goto fail;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2004-05-05 01:39:02 +02:00
|
|
|
if(FLAC__seekable_stream_decoder_init(flacDec)!=
|
2004-02-24 00:41:20 +01:00
|
|
|
FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
|
|
|
|
{
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac problem doing init()\n");
|
2004-05-05 01:39:02 +02:00
|
|
|
flacPrintErroredState(
|
2005-01-30 00:46:06 +01:00
|
|
|
FLAC__seekable_stream_decoder_get_state(flacDec));
|
2004-06-28 14:51:26 +02:00
|
|
|
ret = -1;
|
|
|
|
goto fail;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2004-05-05 01:39:02 +02:00
|
|
|
if(!FLAC__seekable_stream_decoder_process_until_end_of_metadata(flacDec)) {
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac problem reading metadata\n");
|
2004-05-05 01:39:02 +02:00
|
|
|
flacPrintErroredState(
|
2005-01-30 00:46:06 +01:00
|
|
|
FLAC__seekable_stream_decoder_get_state(flacDec));
|
2004-06-28 14:51:26 +02:00
|
|
|
ret = -1;
|
|
|
|
goto fail;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-08 00:42:54 +02:00
|
|
|
|
|
|
|
dc->state = DECODE_STATE_DECODE;
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
while(1) {
|
2004-05-05 01:39:02 +02:00
|
|
|
FLAC__seekable_stream_decoder_process_single(flacDec);
|
|
|
|
if(FLAC__seekable_stream_decoder_get_state(flacDec)!=
|
|
|
|
FLAC__SEEKABLE_STREAM_DECODER_OK)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(dc->seek) {
|
|
|
|
FLAC__uint64 sampleToSeek = dc->seekWhere*
|
2004-05-10 14:35:18 +02:00
|
|
|
dc->audioFormat.sampleRate+0.5;
|
2004-05-05 01:39:02 +02:00
|
|
|
if(FLAC__seekable_stream_decoder_seek_absolute(flacDec,
|
2004-02-24 00:41:20 +01:00
|
|
|
sampleToSeek))
|
|
|
|
{
|
2004-05-30 15:33:13 +02:00
|
|
|
clearOutputBuffer(cb);
|
2004-02-24 00:41:20 +01:00
|
|
|
data.time = ((float)sampleToSeek)/
|
2004-05-10 14:35:18 +02:00
|
|
|
dc->audioFormat.sampleRate;
|
2004-02-26 04:21:24 +01:00
|
|
|
data.position = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-05-30 15:33:13 +02:00
|
|
|
else dc->seekError = 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
dc->seek = 0;
|
|
|
|
}
|
|
|
|
}
|
2004-04-22 23:14:41 +02:00
|
|
|
/* I don't think we need this bit here! -shank */
|
|
|
|
/*FLAC__file_decoder_process_until_end_of_file(flacDec);*/
|
2004-02-24 00:41:20 +01:00
|
|
|
if(!dc->stop) {
|
2004-05-05 01:39:02 +02:00
|
|
|
flacPrintErroredState(
|
2005-01-30 00:46:06 +01:00
|
|
|
FLAC__seekable_stream_decoder_get_state(flacDec));
|
2004-05-05 01:39:02 +02:00
|
|
|
FLAC__seekable_stream_decoder_finish(flacDec);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
/* send last little bit */
|
2004-05-07 21:35:39 +02:00
|
|
|
if(data.chunk_length>0 && !dc->stop) {
|
|
|
|
flacSendChunk(&data);
|
|
|
|
flushOutputBuffer(data.cb);
|
|
|
|
}
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2004-05-30 15:33:13 +02:00
|
|
|
/*if(dc->seek) {
|
|
|
|
dc->seekError = 1;
|
|
|
|
dc->seek = 0;
|
|
|
|
} */
|
2004-05-08 00:42:54 +02:00
|
|
|
|
|
|
|
if(dc->stop) {
|
|
|
|
dc->state = DECODE_STATE_STOP;
|
|
|
|
dc->stop = 0;
|
|
|
|
}
|
|
|
|
else dc->state = DECODE_STATE_STOP;
|
|
|
|
|
2004-06-28 14:51:26 +02:00
|
|
|
fail:
|
2004-11-02 20:56:59 +01:00
|
|
|
if(data.replayGainInfo) freeReplayGainInfo(data.replayGainInfo);
|
|
|
|
|
2005-01-30 00:46:06 +01:00
|
|
|
closeInputStream(inStream);
|
|
|
|
|
2004-06-28 14:51:26 +02:00
|
|
|
if(flacDec) FLAC__seekable_stream_decoder_delete(flacDec);
|
|
|
|
|
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-05-05 01:39:02 +02:00
|
|
|
FLAC__SeekableStreamDecoderReadStatus flacRead(
|
|
|
|
const FLAC__SeekableStreamDecoder * flacDec, FLAC__byte buf[],
|
|
|
|
unsigned * bytes, void * fdata) {
|
|
|
|
FlacData * data = (FlacData *) fdata;
|
|
|
|
|
2005-01-30 00:46:06 +01:00
|
|
|
*bytes = readFromInputStream(data->inStream,(void *)buf,1,*bytes);
|
2004-05-05 01:39:02 +02:00
|
|
|
|
|
|
|
if(*bytes==0) return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
|
|
|
|
|
|
|
|
return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__SeekableStreamDecoderSeekStatus flacSeek(
|
|
|
|
const FLAC__SeekableStreamDecoder * flacDec,
|
|
|
|
FLAC__uint64 offset, void * fdata)
|
|
|
|
{
|
|
|
|
FlacData * data = (FlacData *) fdata;
|
|
|
|
|
2005-01-30 00:46:06 +01:00
|
|
|
if(seekInputStream(data->inStream,offset,SEEK_SET)<0) {
|
2004-05-05 01:39:02 +02:00
|
|
|
return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__SeekableStreamDecoderTellStatus flacTell(
|
|
|
|
const FLAC__SeekableStreamDecoder * flacDec,
|
|
|
|
FLAC__uint64 * offset, void * fdata)
|
|
|
|
{
|
|
|
|
FlacData * data = (FlacData *) fdata;
|
|
|
|
|
2005-01-30 00:46:06 +01:00
|
|
|
*offset = (long)(data->inStream->offset);
|
2004-05-05 01:39:02 +02:00
|
|
|
|
|
|
|
return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__SeekableStreamDecoderLengthStatus flacLength(
|
|
|
|
const FLAC__SeekableStreamDecoder * flacDec,
|
|
|
|
FLAC__uint64 * length, void * fdata)
|
|
|
|
{
|
|
|
|
FlacData * data = (FlacData *) fdata;
|
|
|
|
|
2005-01-30 00:46:06 +01:00
|
|
|
*length = (size_t)(data->inStream->size);
|
2004-05-05 01:39:02 +02:00
|
|
|
|
|
|
|
return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__bool flacEOF(const FLAC__SeekableStreamDecoder * flacDec, void * fdata) {
|
|
|
|
FlacData * data = (FlacData *) fdata;
|
|
|
|
|
2005-01-30 00:46:06 +01:00
|
|
|
switch(inputStreamAtEOF(data->inStream)) {
|
2004-05-05 01:39:02 +02:00
|
|
|
case 1:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void flacError(const FLAC__SeekableStreamDecoder *dec,
|
|
|
|
FLAC__StreamDecoderErrorStatus status, void *fdata)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
FlacData * data = (FlacData *) fdata;
|
|
|
|
if(data->dc->stop) return;
|
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac lost sync\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("bad header\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("crc mismatch\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("unknown flac error\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-30 00:46:06 +01:00
|
|
|
void flacPrintErroredState(FLAC__SeekableStreamDecoderState state)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
switch(state) {
|
2004-05-05 01:39:02 +02:00
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
|
2004-02-24 00:41:20 +01:00
|
|
|
ERROR("flac allocation error\n");
|
|
|
|
break;
|
2004-05-05 01:39:02 +02:00
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac read error\n");
|
2004-05-05 01:39:02 +02:00
|
|
|
break;
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac seek error\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2004-05-05 01:39:02 +02:00
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac seekable stream error\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2004-05-05 01:39:02 +02:00
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac decoder already initialized\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2004-05-05 01:39:02 +02:00
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK:
|
2004-02-24 00:41:20 +01:00
|
|
|
ERROR("invalid flac callback\n");
|
|
|
|
break;
|
2004-05-05 01:39:02 +02:00
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED:
|
2005-01-30 00:46:06 +01:00
|
|
|
ERROR("flac decoder uninitialized\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2004-05-05 01:39:02 +02:00
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_OK:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_SEEKING:
|
|
|
|
case FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-08 00:42:54 +02:00
|
|
|
int flacFindVorbisCommentFloat(const FLAC__StreamMetadata * block, char * cmnt,
|
|
|
|
float * fl)
|
|
|
|
{
|
|
|
|
int offset = FLAC__metadata_object_vorbiscomment_find_entry_from(
|
|
|
|
block,0,cmnt);
|
|
|
|
|
|
|
|
if(offset >= 0) {
|
|
|
|
int pos = strlen(cmnt)+1; /* 1 is for '=' */
|
|
|
|
int len = block->data.vorbis_comment.comments[offset].length
|
|
|
|
-pos;
|
|
|
|
if(len > 0) {
|
|
|
|
char * dup = malloc(len+1);
|
|
|
|
memcpy(dup,&(block->data.vorbis_comment.comments[offset].entry[pos]),len);
|
|
|
|
dup[len] = '\0';
|
|
|
|
*fl = atof(dup);
|
|
|
|
free(dup);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* replaygain stuff by AliasMrJones */
|
|
|
|
void flacParseReplayGain(const FLAC__StreamMetadata *block, FlacData * data) {
|
2004-11-02 20:56:59 +01:00
|
|
|
if(NULL == data->replayGainInfo) {
|
|
|
|
freeReplayGainInfo(data->replayGainInfo);
|
|
|
|
data->replayGainInfo = NULL;
|
|
|
|
}
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2004-11-02 20:56:59 +01:00
|
|
|
data->replayGainInfo = newReplayGainInfo();
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2004-11-02 20:56:59 +01:00
|
|
|
int found = 0;
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2004-11-02 20:56:59 +01:00
|
|
|
found &= flacFindVorbisCommentFloat(block,"replaygain_album_gain",
|
|
|
|
&data->replayGainInfo->albumGain);
|
|
|
|
found &= flacFindVorbisCommentFloat(block,"replaygain_album_peak",
|
|
|
|
&data->replayGainInfo->albumPeak);
|
|
|
|
found &= flacFindVorbisCommentFloat(block,"replaygain_track_gain",
|
|
|
|
&data->replayGainInfo->trackGain);
|
|
|
|
found &= flacFindVorbisCommentFloat(block,"replaygain_track_peak",
|
|
|
|
&data->replayGainInfo->trackPeak);
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2004-11-02 20:56:59 +01:00
|
|
|
if(!found) {
|
|
|
|
freeReplayGainInfo(data->replayGainInfo);
|
|
|
|
data->replayGainInfo = NULL;
|
|
|
|
}
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|
|
|
|
|
2004-05-05 01:39:02 +02:00
|
|
|
void flacMetadata(const FLAC__SeekableStreamDecoder *dec,
|
2004-05-08 00:42:54 +02:00
|
|
|
const FLAC__StreamMetadata *block, void *vdata)
|
2004-05-05 01:39:02 +02:00
|
|
|
{
|
2004-05-08 00:42:54 +02:00
|
|
|
FlacData * data = (FlacData *)vdata;
|
|
|
|
|
|
|
|
switch(block->type) {
|
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
2004-05-10 14:35:18 +02:00
|
|
|
data->dc->audioFormat.bits =
|
|
|
|
block->data.stream_info.bits_per_sample;
|
|
|
|
data->dc->audioFormat.sampleRate =
|
|
|
|
block->data.stream_info.sample_rate;
|
|
|
|
data->dc->audioFormat.channels =
|
|
|
|
block->data.stream_info.channels;
|
|
|
|
data->dc->totalTime =
|
2004-05-08 00:42:54 +02:00
|
|
|
((float)block->data.stream_info.total_samples)/
|
2004-05-10 14:35:18 +02:00
|
|
|
data->dc->audioFormat.sampleRate;
|
|
|
|
getOutputAudioFormat(&(data->dc->audioFormat),
|
|
|
|
&(data->cb->audioFormat));
|
2004-05-08 00:42:54 +02:00
|
|
|
break;
|
|
|
|
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
|
|
|
flacParseReplayGain(block,data);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int flacSendChunk(FlacData * data) {
|
2004-05-29 14:05:49 +02:00
|
|
|
switch(sendDataToOutputBuffer(data->cb, NULL, data->dc, 1, data->chunk,
|
2004-11-02 20:56:59 +01:00
|
|
|
data->chunk_length, data->time, data->bitRate,
|
|
|
|
data->replayGainInfo))
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2004-05-07 21:35:39 +02:00
|
|
|
case OUTPUT_BUFFER_DC_STOP:
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-05 01:39:02 +02:00
|
|
|
FLAC__StreamDecoderWriteStatus flacWrite(const FLAC__SeekableStreamDecoder *dec,
|
|
|
|
const FLAC__Frame *frame, const FLAC__int32 * const buf[],
|
|
|
|
void * vdata)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
FlacData * data = (FlacData *)vdata;
|
|
|
|
FLAC__uint32 samples = frame->header.blocksize;
|
2005-02-02 05:22:16 +01:00
|
|
|
int c_samp;
|
|
|
|
int c_chan;
|
2004-02-26 04:21:24 +01:00
|
|
|
float timeChange;
|
|
|
|
FLAC__uint64 newPosition = 0;
|
2005-02-02 05:22:16 +01:00
|
|
|
int bytesPerSample = data->dc->audioFormat.bits/8;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-02-26 04:21:24 +01:00
|
|
|
timeChange = ((float)samples)/frame->header.sample_rate;
|
|
|
|
data->time+= timeChange;
|
|
|
|
|
2004-05-05 01:39:02 +02:00
|
|
|
FLAC__seekable_stream_decoder_get_decode_position(dec,&newPosition);
|
2004-02-26 04:21:24 +01:00
|
|
|
if(data->position) {
|
|
|
|
data->bitRate = ((newPosition-data->position)*8.0/timeChange)
|
2004-04-03 06:01:31 +02:00
|
|
|
/1000+0.5;
|
2004-02-26 04:21:24 +01:00
|
|
|
}
|
|
|
|
data->position = newPosition;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2005-02-02 05:22:16 +01:00
|
|
|
for(c_samp = 0; c_samp < frame->header.blocksize; c_samp++) {
|
|
|
|
for(c_chan = 0; c_chan < frame->header.channels; c_chan++) {
|
|
|
|
memcpy(data->chunk+data->chunk_length,
|
|
|
|
&buf[c_chan][c_samp], bytesPerSample);
|
|
|
|
data->chunk_length++;
|
|
|
|
if(FLAC_CHUNK_SIZE-data->chunk_length < bytesPerSample)
|
|
|
|
{
|
|
|
|
if(flacSendChunk(data)<0) {
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
|
}
|
|
|
|
data->chunk_length = 0;
|
|
|
|
if(data->dc->seek) {
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
static int commentMatchesAddToTag(
|
|
|
|
char * str,
|
|
|
|
FLAC__StreamMetadata_VorbisComment_Entry * entry,
|
|
|
|
int itemType,
|
|
|
|
MpdTag ** tag)
|
|
|
|
{
|
|
|
|
int slen = strlen(str);
|
|
|
|
int vlen = entry->length - slen;
|
|
|
|
|
|
|
|
if( vlen <= 0 ) return 0;
|
|
|
|
|
|
|
|
if( 0 == strncasecmp(str, entry->entry, slen) ) {
|
|
|
|
if(*tag == NULL) *tag = newMpdTag();
|
|
|
|
addItemToMpdTagWithLen(*tag, itemType,
|
|
|
|
entry->entry+slen, vlen);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MpdTag * copyVorbisCommentBlockToMpdTag(FLAC__StreamMetadata * block,
|
|
|
|
MpdTag * tag)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < block->data.vorbis_comment.num_comments; i++) {
|
|
|
|
if(commentMatchesAddToTag(
|
|
|
|
"artist=",
|
|
|
|
block->data.vorbis_comment.comments+i,
|
|
|
|
TAG_ITEM_ARTIST,
|
|
|
|
&tag));
|
|
|
|
else if(commentMatchesAddToTag(
|
|
|
|
"title=",
|
|
|
|
block->data.vorbis_comment.comments+i,
|
|
|
|
TAG_ITEM_TITLE,
|
|
|
|
&tag));
|
|
|
|
else if(commentMatchesAddToTag(
|
|
|
|
"album=",
|
|
|
|
block->data.vorbis_comment.comments+i,
|
|
|
|
TAG_ITEM_ALBUM,
|
|
|
|
&tag));
|
|
|
|
else if(commentMatchesAddToTag(
|
|
|
|
"tracknumber=",
|
|
|
|
block->data.vorbis_comment.comments+i,
|
|
|
|
TAG_ITEM_TRACK,
|
|
|
|
&tag));
|
|
|
|
else if(commentMatchesAddToTag(
|
|
|
|
"genre=",
|
|
|
|
block->data.vorbis_comment.comments+i,
|
|
|
|
TAG_ITEM_GENRE,
|
|
|
|
&tag));
|
|
|
|
else if(commentMatchesAddToTag(
|
|
|
|
"date=",
|
|
|
|
block->data.vorbis_comment.comments+i,
|
|
|
|
TAG_ITEM_DATE,
|
|
|
|
&tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2004-05-31 04:21:06 +02:00
|
|
|
MpdTag * flacMetadataDup(char * file, int * vorbisCommentFound) {
|
2004-05-31 03:54:10 +02:00
|
|
|
MpdTag * ret = NULL;
|
|
|
|
FLAC__Metadata_SimpleIterator * it;
|
|
|
|
FLAC__StreamMetadata * block = NULL;
|
|
|
|
|
|
|
|
*vorbisCommentFound = 0;
|
|
|
|
|
|
|
|
it = FLAC__metadata_simple_iterator_new();
|
2004-05-31 04:21:06 +02:00
|
|
|
if(!FLAC__metadata_simple_iterator_init(it, file ,1,0)) {
|
2004-05-31 03:54:10 +02:00
|
|
|
FLAC__metadata_simple_iterator_delete(it);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
block = FLAC__metadata_simple_iterator_get_block(it);
|
|
|
|
if(!block) break;
|
|
|
|
if(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
|
2004-11-10 22:58:27 +01:00
|
|
|
ret = copyVorbisCommentBlockToMpdTag(block, ret);
|
|
|
|
|
|
|
|
if(ret) *vorbisCommentFound = 1;
|
2004-05-31 03:54:10 +02:00
|
|
|
}
|
|
|
|
else if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
|
|
|
if(!ret) ret = newMpdTag();
|
|
|
|
ret->time = ((float)block->data.stream_info.
|
|
|
|
total_samples) /
|
|
|
|
block->data.stream_info.sample_rate +
|
|
|
|
0.5;
|
|
|
|
}
|
|
|
|
FLAC__metadata_object_delete(block);
|
|
|
|
} while(FLAC__metadata_simple_iterator_next(it));
|
|
|
|
|
|
|
|
FLAC__metadata_simple_iterator_delete(it);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-05-31 04:21:06 +02:00
|
|
|
MpdTag * flacTagDup(char * file) {
|
2004-05-31 03:54:10 +02:00
|
|
|
MpdTag * ret = NULL;
|
|
|
|
int foundVorbisComment = 0;
|
|
|
|
|
2004-05-31 04:21:06 +02:00
|
|
|
ret = flacMetadataDup(file, &foundVorbisComment);
|
2004-05-31 03:54:10 +02:00
|
|
|
if(!ret) return NULL;
|
|
|
|
if(!foundVorbisComment) {
|
2004-05-31 04:21:06 +02:00
|
|
|
MpdTag * temp = id3Dup(file);
|
2004-05-31 03:54:10 +02:00
|
|
|
if(temp) {
|
|
|
|
temp->time = ret->time;
|
|
|
|
freeMpdTag(ret);
|
|
|
|
ret = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * flacSuffixes[] = {"flac", NULL};
|
2005-01-30 00:46:06 +01:00
|
|
|
char * flac_mime_types[] = {"application/x-flac", NULL};
|
2004-05-31 03:54:10 +02:00
|
|
|
|
|
|
|
InputPlugin flacPlugin =
|
|
|
|
{
|
|
|
|
"flac",
|
|
|
|
NULL,
|
2004-05-31 22:59:55 +02:00
|
|
|
NULL,
|
2004-05-31 03:54:10 +02:00
|
|
|
flac_decode,
|
2005-01-30 00:46:06 +01:00
|
|
|
NULL,
|
2004-05-31 03:54:10 +02:00
|
|
|
flacTagDup,
|
2005-01-30 00:46:06 +01:00
|
|
|
INPUT_PLUGIN_STREAM_URL | INPUT_PLUGIN_STREAM_FILE,
|
2004-05-31 03:54:10 +02:00
|
|
|
flacSuffixes,
|
2005-01-30 00:46:06 +01:00
|
|
|
flac_mime_types
|
2004-05-31 03:54:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
InputPlugin flacPlugin =
|
|
|
|
{
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2004-05-31 22:59:55 +02:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2004-05-31 03:54:10 +02:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|