2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2006-03-16 07:52:46 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common data structures and functions used by FLAC and OggFLAC
|
2006-03-16 07:52:46 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2006-03-16 07:52:46 +01:00
|
|
|
#include "_flac_common.h"
|
2009-11-11 07:50:40 +01:00
|
|
|
#include "flac_metadata.h"
|
2009-11-11 07:59:22 +01:00
|
|
|
#include "flac_pcm.h"
|
2009-11-10 19:01:38 +01:00
|
|
|
#include "audio_check.h"
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2009-01-01 18:09:24 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-01-15 22:45:28 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2009-01-15 19:50:28 +01:00
|
|
|
void
|
|
|
|
flac_data_init(struct flac_data *data, struct decoder * decoder,
|
|
|
|
struct input_stream *input_stream)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2009-11-10 21:46:10 +01:00
|
|
|
pcm_buffer_init(&data->buffer);
|
|
|
|
|
2009-11-11 19:25:15 +01:00
|
|
|
data->have_stream_info = false;
|
2009-11-11 20:18:39 +01:00
|
|
|
data->first_frame = 0;
|
2009-11-11 15:09:24 +01:00
|
|
|
data->next_frame = 0;
|
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
data->position = 0;
|
2008-08-26 08:27:04 +02:00
|
|
|
data->decoder = decoder;
|
2009-01-15 19:50:28 +01:00
|
|
|
data->input_stream = input_stream;
|
|
|
|
data->replay_gain_info = NULL;
|
2006-03-16 07:52:46 +01:00
|
|
|
data->tag = NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-10 21:42:15 +01:00
|
|
|
void
|
|
|
|
flac_data_deinit(struct flac_data *data)
|
|
|
|
{
|
2009-11-10 21:46:10 +01:00
|
|
|
pcm_buffer_deinit(&data->buffer);
|
|
|
|
|
2009-11-10 21:42:15 +01:00
|
|
|
if (data->replay_gain_info != NULL)
|
|
|
|
replay_gain_info_free(data->replay_gain_info);
|
|
|
|
|
|
|
|
if (data->tag != NULL)
|
|
|
|
tag_free(data->tag);
|
|
|
|
}
|
|
|
|
|
2009-11-11 20:34:59 +01:00
|
|
|
bool
|
|
|
|
flac_data_get_audio_format(struct flac_data *data,
|
|
|
|
struct audio_format *audio_format)
|
|
|
|
{
|
2009-11-10 19:01:38 +01:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2009-11-11 20:34:59 +01:00
|
|
|
if (!data->have_stream_info) {
|
|
|
|
g_warning("no STREAMINFO packet found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-10 19:01:38 +01:00
|
|
|
if (!audio_format_init_checked(audio_format,
|
|
|
|
data->stream_info.sample_rate,
|
|
|
|
data->stream_info.bits_per_sample,
|
|
|
|
data->stream_info.channels, &error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
2009-11-11 20:34:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->frame_size = audio_format_frame_size(audio_format);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (block->type) {
|
2006-03-16 07:52:46 +01:00
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
2009-11-11 19:25:15 +01:00
|
|
|
data->stream_info = block->data.stream_info;
|
|
|
|
data->have_stream_info = true;
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
2009-11-11 20:34:59 +01:00
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
2009-11-11 07:35:16 +01:00
|
|
|
if (data->replay_gain_info)
|
|
|
|
replay_gain_info_free(data->replay_gain_info);
|
|
|
|
data->replay_gain_info = flac_parse_replay_gain(block);
|
2009-03-01 14:07:23 +01:00
|
|
|
|
|
|
|
if (data->tag != NULL)
|
2009-11-10 21:58:19 +01:00
|
|
|
flac_vorbis_comments_to_tag(data->tag, NULL,
|
|
|
|
&block->data.vorbis_comment);
|
2009-03-01 14:07:23 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
default:
|
|
|
|
break;
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void flac_error_common_cb(const char *plugin,
|
|
|
|
const FLAC__StreamDecoderErrorStatus status,
|
2009-01-15 19:50:28 +01:00
|
|
|
struct flac_data *data)
|
2006-03-16 07:52:46 +01:00
|
|
|
{
|
2008-08-26 08:27:07 +02:00
|
|
|
if (decoder_get_command(data->decoder) == DECODE_COMMAND_STOP)
|
2006-07-20 18:02:40 +02:00
|
|
|
return;
|
2006-03-16 07:52:46 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (status) {
|
2006-03-16 07:52:46 +01:00
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("%s lost sync\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("bad %s header\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("%s crc mismatch\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
break;
|
|
|
|
default:
|
2008-11-21 20:15:50 +01:00
|
|
|
g_warning("unknown %s error\n", plugin);
|
2006-03-16 07:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 23:59:54 +02:00
|
|
|
FLAC__StreamDecoderWriteStatus
|
2009-01-15 19:50:28 +01:00
|
|
|
flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
|
2009-11-11 19:52:14 +01:00
|
|
|
const FLAC__int32 *const buf[],
|
|
|
|
FLAC__uint64 nbytes)
|
2008-09-23 23:59:54 +02:00
|
|
|
{
|
2008-09-23 23:59:55 +02:00
|
|
|
enum decoder_command cmd;
|
2009-11-11 20:34:59 +01:00
|
|
|
size_t buffer_size = frame->header.blocksize * data->frame_size;
|
2009-11-10 21:46:10 +01:00
|
|
|
void *buffer;
|
2009-11-11 20:18:39 +01:00
|
|
|
float position;
|
2009-11-11 19:52:14 +01:00
|
|
|
unsigned bit_rate;
|
2009-11-10 21:46:10 +01:00
|
|
|
|
|
|
|
buffer = pcm_buffer_get(&data->buffer, buffer_size);
|
|
|
|
|
2009-11-11 20:26:56 +01:00
|
|
|
flac_convert(buffer, frame->header.channels,
|
|
|
|
frame->header.bits_per_sample, buf,
|
2009-11-10 21:46:10 +01:00
|
|
|
0, frame->header.blocksize);
|
|
|
|
|
2009-11-11 20:18:39 +01:00
|
|
|
if (data->next_frame >= data->first_frame)
|
|
|
|
position = (float)(data->next_frame - data->first_frame) /
|
2009-11-11 20:26:56 +01:00
|
|
|
frame->header.sample_rate;
|
2009-11-11 20:18:39 +01:00
|
|
|
else
|
|
|
|
position = 0;
|
|
|
|
|
2009-11-11 19:52:14 +01:00
|
|
|
if (nbytes > 0)
|
|
|
|
bit_rate = nbytes * 8 * frame->header.sample_rate /
|
|
|
|
(1000 * frame->header.blocksize);
|
|
|
|
else
|
|
|
|
bit_rate = 0;
|
|
|
|
|
2009-11-10 21:46:10 +01:00
|
|
|
cmd = decoder_data(data->decoder, data->input_stream,
|
|
|
|
buffer, buffer_size,
|
2009-11-11 20:18:39 +01:00
|
|
|
position, bit_rate,
|
2009-11-10 21:46:10 +01:00
|
|
|
data->replay_gain_info);
|
2009-11-11 15:09:24 +01:00
|
|
|
data->next_frame += frame->header.blocksize;
|
2009-11-10 21:46:10 +01:00
|
|
|
switch (cmd) {
|
|
|
|
case DECODE_COMMAND_NONE:
|
|
|
|
case DECODE_COMMAND_START:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECODE_COMMAND_STOP:
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
2008-09-23 23:59:54 +02:00
|
|
|
|
2009-11-10 21:46:10 +01:00
|
|
|
case DECODE_COMMAND_SEEK:
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
2008-09-23 23:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|
2009-03-08 20:16:53 +01:00
|
|
|
|
|
|
|
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
|
|
|
|
|
|
|
|
char*
|
|
|
|
flac_cue_track( const char* pathname,
|
|
|
|
const unsigned int tnum)
|
|
|
|
{
|
2009-08-14 11:51:42 +02:00
|
|
|
FLAC__bool success;
|
|
|
|
FLAC__StreamMetadata* cs;
|
2009-03-08 20:16:53 +01:00
|
|
|
|
2009-08-14 11:51:42 +02:00
|
|
|
success = FLAC__metadata_get_cuesheet(pathname, &cs);
|
|
|
|
if (!success)
|
2009-03-08 20:16:53 +01:00
|
|
|
return NULL;
|
|
|
|
|
2009-08-14 11:51:42 +02:00
|
|
|
assert(cs != NULL);
|
|
|
|
|
2009-03-08 20:16:53 +01:00
|
|
|
if (cs->data.cue_sheet.num_tracks <= 1)
|
|
|
|
{
|
|
|
|
FLAC__metadata_object_delete(cs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tnum > 0 && tnum < cs->data.cue_sheet.num_tracks)
|
|
|
|
{
|
|
|
|
char* track = g_strdup_printf("track_%03u.flac", tnum);
|
|
|
|
|
|
|
|
FLAC__metadata_object_delete(cs);
|
|
|
|
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FLAC__metadata_object_delete(cs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
flac_vtrack_tnum(const char* fname)
|
|
|
|
{
|
|
|
|
/* find last occurrence of '_' in fname
|
|
|
|
* which is hopefully something like track_xxx.flac
|
|
|
|
* another/better way would be to use tag struct
|
|
|
|
*/
|
|
|
|
char* ptr = strrchr(fname, '_');
|
2009-11-18 19:55:38 +01:00
|
|
|
if (ptr == NULL)
|
|
|
|
return 0;
|
2009-03-08 20:16:53 +01:00
|
|
|
|
|
|
|
// copy ascii tracknumber to int
|
2009-11-19 19:49:44 +01:00
|
|
|
return (unsigned int)strtol(++ptr, NULL, 10);
|
2009-03-08 20:16:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FLAC_API_VERSION_CURRENT >= 7 */
|