2000-12-10 04:09:52 +00:00
/* libxmms-flac - XMMS FLAC input plugin
2006-04-25 06:59:33 +00:00
* Copyright ( C ) 2000 , 2001 , 2002 , 2003 , 2004 , 2005 , 2006 Josh Coalson
2000-12-10 04:09:52 +00: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 .
*
* 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 .
*/
2006-05-24 04:41:36 +00:00
# if HAVE_CONFIG_H
# include <config.h>
# endif
# include <limits.h>
2000-12-10 04:09:52 +00:00
# include <pthread.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <glib.h>
2004-10-07 00:23:51 +00:00
# include <pwd.h>
# include <sys/types.h>
# include <unistd.h>
2000-12-10 04:09:52 +00:00
2002-07-11 06:15:30 +00:00
# include <xmms/plugin.h>
# include <xmms/util.h>
# include <xmms/configfile.h>
# include <xmms/titlestring.h>
# ifdef HAVE_LANGINFO_CODESET
# include <langinfo.h>
# endif
2000-12-10 04:09:52 +00:00
# include "FLAC/all.h"
2002-08-29 08:13:42 +00:00
# include "plugin_common/all.h"
2002-11-07 05:07:30 +00:00
# include "share/grabbag.h"
2003-12-17 04:51:06 +00:00
# include "share/replaygain_synthesis.h"
2002-07-11 06:15:30 +00:00
# include "configure.h"
# include "charset.h"
2004-10-07 00:23:51 +00:00
# include "http.h"
2004-09-28 00:23:57 +00:00
# include "tag.h"
2000-12-10 04:09:52 +00:00
2001-04-18 23:02:08 +00:00
# ifdef min
# undef min
# endif
# define min(x,y) ((x)<(y)?(x):(y))
2002-11-19 06:17:04 +00:00
extern void FLAC_XMMS__file_info_box ( char * filename ) ;
2002-08-29 08:13:42 +00:00
2000-12-10 04:09:52 +00:00
typedef struct {
2001-06-23 03:03:24 +00:00
FLAC__bool abort_flag ;
FLAC__bool is_playing ;
2006-09-13 01:42:27 +00:00
FLAC__bool is_http_source ;
2001-06-23 03:03:24 +00:00
FLAC__bool eof ;
2001-12-04 01:27:32 +00:00
FLAC__bool play_thread_open ; /* if true, is_playing must also be true */
2006-05-24 04:41:36 +00:00
FLAC__uint64 total_samples ;
2000-12-10 04:09:52 +00:00
unsigned bits_per_sample ;
unsigned channels ;
unsigned sample_rate ;
2006-05-24 04:41:36 +00:00
int length_in_msec ; /* int (instead of FLAC__uint64) only because that's what XMMS uses; seeking won't work right if this maxes out */
2002-11-14 04:56:57 +00:00
gchar * title ;
2001-03-30 23:27:44 +00:00
AFormat sample_format ;
2003-02-27 06:12:55 +00:00
unsigned sample_format_bytes_per_sample ;
2000-12-10 04:09:52 +00:00
int seek_to_in_sec ;
2002-11-05 07:27:28 +00:00
FLAC__bool has_replaygain ;
double replay_scale ;
DitherContext dither_context ;
2006-09-13 01:42:27 +00:00
} stream_data_struct ;
2004-10-07 00:23:51 +00:00
2000-12-10 04:09:52 +00:00
static void FLAC_XMMS__init ( ) ;
static int FLAC_XMMS__is_our_file ( char * filename ) ;
static void FLAC_XMMS__play_file ( char * filename ) ;
static void FLAC_XMMS__stop ( ) ;
static void FLAC_XMMS__pause ( short p ) ;
static void FLAC_XMMS__seek ( int time ) ;
static int FLAC_XMMS__get_time ( ) ;
static void FLAC_XMMS__cleanup ( ) ;
static void FLAC_XMMS__get_song_info ( char * filename , char * * title , int * length ) ;
static void * play_loop_ ( void * arg ) ;
2004-10-07 00:23:51 +00:00
2006-09-13 01:42:27 +00:00
static FLAC__bool safe_decoder_init_ ( const char * filename , FLAC__StreamDecoder * decoder ) ;
static void safe_decoder_finish_ ( FLAC__StreamDecoder * decoder ) ;
static void safe_decoder_delete_ ( FLAC__StreamDecoder * decoder ) ;
2004-10-07 00:23:51 +00:00
2006-09-13 01:42:27 +00:00
static FLAC__StreamDecoderReadStatus http_read_callback_ ( const FLAC__StreamDecoder * decoder , FLAC__byte buffer [ ] , unsigned * bytes , void * client_data ) ;
static FLAC__StreamDecoderWriteStatus write_callback_ ( const FLAC__StreamDecoder * decoder , const FLAC__Frame * frame , const FLAC__int32 * const buffer [ ] , void * client_data ) ;
static void metadata_callback_ ( const FLAC__StreamDecoder * decoder , const FLAC__StreamMetadata * metadata , void * client_data ) ;
static void error_callback_ ( const FLAC__StreamDecoder * decoder , FLAC__StreamDecoderErrorStatus status , void * client_data ) ;
2000-12-10 04:09:52 +00:00
InputPlugin flac_ip =
{
NULL ,
NULL ,
2005-02-02 05:50:52 +00:00
" FLAC Player v " VERSION ,
2000-12-10 04:09:52 +00:00
FLAC_XMMS__init ,
2002-07-11 06:15:30 +00:00
FLAC_XMMS__aboutbox ,
FLAC_XMMS__configure ,
2000-12-10 04:09:52 +00:00
FLAC_XMMS__is_our_file ,
NULL ,
FLAC_XMMS__play_file ,
FLAC_XMMS__stop ,
FLAC_XMMS__pause ,
FLAC_XMMS__seek ,
NULL ,
FLAC_XMMS__get_time ,
NULL ,
NULL ,
FLAC_XMMS__cleanup ,
NULL ,
NULL ,
NULL ,
NULL ,
FLAC_XMMS__get_song_info ,
2002-11-19 06:17:04 +00:00
FLAC_XMMS__file_info_box ,
2000-12-10 04:09:52 +00:00
NULL
} ;
2001-04-17 18:01:22 +00:00
# define SAMPLES_PER_WRITE 512
2003-02-27 06:12:55 +00:00
# define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24 / 8))
static FLAC__byte sample_buffer_ [ SAMPLE_BUFFER_SIZE ] ;
static unsigned sample_buffer_first_ , sample_buffer_last_ ;
2002-11-14 04:56:57 +00:00
2006-09-13 01:42:27 +00:00
static FLAC__StreamDecoder * decoder_ = 0 ;
static stream_data_struct stream_data_ ;
2000-12-10 04:09:52 +00:00
static pthread_t decode_thread_ ;
2001-06-23 03:03:24 +00:00
static FLAC__bool audio_error_ = false ;
2003-09-15 20:28:08 +00:00
static FLAC__bool is_big_endian_host_ ;
2000-12-10 04:09:52 +00:00
2002-11-14 04:56:57 +00:00
# define BITRATE_HIST_SEGMENT_MSEC 500
/* 500ms * 50 = 25s should be enough */
# define BITRATE_HIST_SIZE 50
static unsigned bitrate_history_ [ BITRATE_HIST_SIZE ] ;
2000-12-10 04:09:52 +00:00
InputPlugin * get_iplugin_info ( )
{
2002-08-30 05:42:25 +00:00
flac_ip . description = g_strdup_printf ( " Reference FLAC Player v%s " , FLAC__VERSION_STRING ) ;
2000-12-10 04:09:52 +00:00
return & flac_ip ;
}
2004-10-07 00:23:51 +00:00
void set_track_info ( const char * title , int length_in_msec )
{
2006-09-13 01:42:27 +00:00
if ( stream_data_ . is_playing ) {
flac_ip . set_info ( ( char * ) title , length_in_msec , stream_data_ . sample_rate * stream_data_ . channels * stream_data_ . bits_per_sample , stream_data_ . sample_rate , stream_data_ . channels ) ;
2004-10-07 00:23:51 +00:00
}
}
static gchar * homedir ( )
{
gchar * result ;
char * env_home = getenv ( " HOME " ) ;
if ( env_home ) {
result = g_strdup ( env_home ) ;
} else {
uid_t uid = getuid ( ) ;
struct passwd * pwent ;
do {
pwent = getpwent ( ) ;
} while ( pwent & & pwent - > pw_uid ! = uid ) ;
result = pwent ? g_strdup ( pwent - > pw_dir ) : NULL ;
endpwent ( ) ;
}
return result ;
}
2006-09-13 01:42:27 +00:00
static FLAC__bool is_http_source ( const char * source )
{
return 0 = = strncasecmp ( source , " http:// " , 7 ) ;
}
2000-12-10 04:09:52 +00:00
void FLAC_XMMS__init ( )
{
2002-07-11 06:15:30 +00:00
ConfigFile * cfg ;
2003-09-15 20:28:08 +00:00
FLAC__uint32 test = 1 ;
2004-10-07 00:23:51 +00:00
2003-09-15 20:28:08 +00:00
is_big_endian_host_ = ( * ( ( FLAC__byte * ) ( & test ) ) ) ? false : true ;
2002-07-11 06:15:30 +00:00
2002-11-05 07:27:28 +00:00
flac_cfg . title . tag_override = FALSE ;
2005-09-02 05:00:31 +00:00
if ( flac_cfg . title . tag_format )
g_free ( flac_cfg . title . tag_format ) ;
2002-11-05 07:27:28 +00:00
flac_cfg . title . convert_char_set = FALSE ;
2002-08-29 08:13:42 +00:00
2002-07-11 06:15:30 +00:00
cfg = xmms_cfg_open_default_file ( ) ;
2002-08-23 06:45:23 +00:00
2002-11-05 07:27:28 +00:00
/* title */
2002-08-23 06:45:23 +00:00
2002-11-05 07:27:28 +00:00
xmms_cfg_read_boolean ( cfg , " flac " , " title.tag_override " , & flac_cfg . title . tag_override ) ;
2002-07-11 06:15:30 +00:00
2002-11-05 07:27:28 +00:00
if ( ! xmms_cfg_read_string ( cfg , " flac " , " title.tag_format " , & flac_cfg . title . tag_format ) )
flac_cfg . title . tag_format = g_strdup ( " %p - %t " ) ;
2002-08-23 06:45:23 +00:00
2002-11-05 07:27:28 +00:00
xmms_cfg_read_boolean ( cfg , " flac " , " title.convert_char_set " , & flac_cfg . title . convert_char_set ) ;
2002-08-23 06:45:23 +00:00
2002-11-05 07:27:28 +00:00
if ( ! xmms_cfg_read_string ( cfg , " flac " , " title.user_char_set " , & flac_cfg . title . user_char_set ) )
flac_cfg . title . user_char_set = FLAC_plugin__charset_get_current ( ) ;
2002-07-11 06:15:30 +00:00
2002-11-01 08:03:47 +00:00
/* replaygain */
2002-11-05 07:27:28 +00:00
xmms_cfg_read_boolean ( cfg , " flac " , " output.replaygain.enable " , & flac_cfg . output . replaygain . enable ) ;
xmms_cfg_read_boolean ( cfg , " flac " , " output.replaygain.album_mode " , & flac_cfg . output . replaygain . album_mode ) ;
if ( ! xmms_cfg_read_int ( cfg , " flac " , " output.replaygain.preamp " , & flac_cfg . output . replaygain . preamp ) )
flac_cfg . output . replaygain . preamp = 0 ;
2002-11-01 08:03:47 +00:00
2002-11-05 07:27:28 +00:00
xmms_cfg_read_boolean ( cfg , " flac " , " output.replaygain.hard_limit " , & flac_cfg . output . replaygain . hard_limit ) ;
2002-11-01 08:03:47 +00:00
2002-11-05 07:27:28 +00:00
xmms_cfg_read_boolean ( cfg , " flac " , " output.resolution.normal.dither_24_to_16 " , & flac_cfg . output . resolution . normal . dither_24_to_16 ) ;
xmms_cfg_read_boolean ( cfg , " flac " , " output.resolution.replaygain.dither " , & flac_cfg . output . resolution . replaygain . dither ) ;
2002-11-01 08:03:47 +00:00
2002-11-05 07:27:28 +00:00
if ( ! xmms_cfg_read_int ( cfg , " flac " , " output.resolution.replaygain.noise_shaping " , & flac_cfg . output . resolution . replaygain . noise_shaping ) )
flac_cfg . output . resolution . replaygain . noise_shaping = 1 ;
2002-11-01 08:03:47 +00:00
2002-11-05 07:27:28 +00:00
if ( ! xmms_cfg_read_int ( cfg , " flac " , " output.resolution.replaygain.bps_out " , & flac_cfg . output . resolution . replaygain . bps_out ) )
flac_cfg . output . resolution . replaygain . bps_out = 16 ;
2002-11-01 08:03:47 +00:00
2004-10-07 00:23:51 +00:00
/* stream */
xmms_cfg_read_int ( cfg , " flac " , " stream.http_buffer_size " , & flac_cfg . stream . http_buffer_size ) ;
xmms_cfg_read_int ( cfg , " flac " , " stream.http_prebuffer " , & flac_cfg . stream . http_prebuffer ) ;
xmms_cfg_read_boolean ( cfg , " flac " , " stream.use_proxy " , & flac_cfg . stream . use_proxy ) ;
2005-09-02 05:00:31 +00:00
if ( flac_cfg . stream . proxy_host )
g_free ( flac_cfg . stream . proxy_host ) ;
if ( ! xmms_cfg_read_string ( cfg , " flac " , " stream.proxy_host " , & flac_cfg . stream . proxy_host ) )
flac_cfg . stream . proxy_host = g_strdup ( " " ) ;
2004-10-07 00:23:51 +00:00
xmms_cfg_read_int ( cfg , " flac " , " stream.proxy_port " , & flac_cfg . stream . proxy_port ) ;
xmms_cfg_read_boolean ( cfg , " flac " , " stream.proxy_use_auth " , & flac_cfg . stream . proxy_use_auth ) ;
2005-09-02 05:00:31 +00:00
if ( flac_cfg . stream . proxy_user )
g_free ( flac_cfg . stream . proxy_user ) ;
flac_cfg . stream . proxy_user = NULL ;
2004-10-07 00:23:51 +00:00
xmms_cfg_read_string ( cfg , " flac " , " stream.proxy_user " , & flac_cfg . stream . proxy_user ) ;
2005-09-02 05:00:31 +00:00
if ( flac_cfg . stream . proxy_pass )
g_free ( flac_cfg . stream . proxy_pass ) ;
flac_cfg . stream . proxy_pass = NULL ;
2004-10-07 00:23:51 +00:00
xmms_cfg_read_string ( cfg , " flac " , " stream.proxy_pass " , & flac_cfg . stream . proxy_pass ) ;
xmms_cfg_read_boolean ( cfg , " flac " , " stream.save_http_stream " , & flac_cfg . stream . save_http_stream ) ;
2005-09-02 05:00:31 +00:00
if ( flac_cfg . stream . save_http_path )
g_free ( flac_cfg . stream . save_http_path ) ;
if ( ! xmms_cfg_read_string ( cfg , " flac " , " stream.save_http_path " , & flac_cfg . stream . save_http_path ) | | ! * flac_cfg . stream . save_http_path ) {
2004-10-07 00:23:51 +00:00
if ( flac_cfg . stream . save_http_path )
g_free ( flac_cfg . stream . save_http_path ) ;
flac_cfg . stream . save_http_path = homedir ( ) ;
}
xmms_cfg_read_boolean ( cfg , " flac " , " stream.cast_title_streaming " , & flac_cfg . stream . cast_title_streaming ) ;
xmms_cfg_read_boolean ( cfg , " flac " , " stream.use_udp_channel " , & flac_cfg . stream . use_udp_channel ) ;
2006-09-13 01:42:27 +00:00
decoder_ = FLAC__stream_decoder_new ( ) ;
2003-05-19 04:27:13 +00:00
xmms_cfg_free ( cfg ) ;
2000-12-10 04:09:52 +00:00
}
int FLAC_XMMS__is_our_file ( char * filename )
{
char * ext ;
ext = strrchr ( filename , ' . ' ) ;
2001-12-04 01:27:32 +00:00
if ( ext )
if ( ! strcasecmp ( ext , " .flac " ) | | ! strcasecmp ( ext , " .fla " ) )
2000-12-10 04:09:52 +00:00
return 1 ;
return 0 ;
}
void FLAC_XMMS__play_file ( char * filename )
{
FILE * f ;
2003-02-27 06:12:55 +00:00
sample_buffer_first_ = sample_buffer_last_ = 0 ;
2001-12-04 01:27:32 +00:00
audio_error_ = false ;
2006-09-13 01:42:27 +00:00
stream_data_ . abort_flag = false ;
stream_data_ . is_playing = false ;
stream_data_ . is_http_source = is_http_source ( filename ) ;
stream_data_ . eof = false ;
stream_data_ . play_thread_open = false ;
stream_data_ . has_replaygain = false ;
if ( ! is_http_source ( filename ) ) {
2004-10-07 00:23:51 +00:00
if ( 0 = = ( f = fopen ( filename , " r " ) ) )
return ;
fclose ( f ) ;
}
2000-12-10 04:09:52 +00:00
2002-06-07 05:27:37 +00:00
if ( decoder_ = = 0 )
return ;
2006-09-13 01:42:27 +00:00
if ( ! safe_decoder_init_ ( filename , decoder_ ) )
2000-12-10 04:09:52 +00:00
return ;
2006-09-13 01:42:27 +00:00
if ( stream_data_ . has_replaygain & & flac_cfg . output . replaygain . enable ) {
2003-02-27 06:12:55 +00:00
if ( flac_cfg . output . resolution . replaygain . bps_out = = 8 ) {
2006-09-13 01:42:27 +00:00
stream_data_ . sample_format = FMT_U8 ;
stream_data_ . sample_format_bytes_per_sample = 1 ;
2003-02-27 06:12:55 +00:00
}
else if ( flac_cfg . output . resolution . replaygain . bps_out = = 16 ) {
2006-09-13 01:42:27 +00:00
stream_data_ . sample_format = ( is_big_endian_host_ ) ? FMT_S16_BE : FMT_S16_LE ;
stream_data_ . sample_format_bytes_per_sample = 2 ;
2003-02-27 06:12:55 +00:00
}
else {
/*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */
fprintf ( stderr , " libxmms-flac: can't handle %d bit output \n " , flac_cfg . output . resolution . replaygain . bps_out ) ;
2006-09-13 01:42:27 +00:00
safe_decoder_finish_ ( decoder_ ) ;
2003-02-27 06:12:55 +00:00
return ;
}
}
else {
2006-09-13 01:42:27 +00:00
if ( stream_data_ . bits_per_sample = = 8 ) {
stream_data_ . sample_format = FMT_U8 ;
stream_data_ . sample_format_bytes_per_sample = 1 ;
2003-02-27 06:12:55 +00:00
}
2006-09-13 01:42:27 +00:00
else if ( stream_data_ . bits_per_sample = = 16 | | ( stream_data_ . bits_per_sample = = 24 & & flac_cfg . output . resolution . normal . dither_24_to_16 ) ) {
stream_data_ . sample_format = ( is_big_endian_host_ ) ? FMT_S16_BE : FMT_S16_LE ;
stream_data_ . sample_format_bytes_per_sample = 2 ;
2003-02-27 06:12:55 +00:00
}
else {
/*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */
2006-09-13 01:42:27 +00:00
fprintf ( stderr , " libxmms-flac: can't handle %d bit output \n " , stream_data_ . bits_per_sample ) ;
safe_decoder_finish_ ( decoder_ ) ;
2003-02-27 06:12:55 +00:00
return ;
}
}
2006-09-13 01:42:27 +00:00
FLAC__replaygain_synthesis__init_dither_context ( & stream_data_ . dither_context , stream_data_ . sample_format_bytes_per_sample * 8 , flac_cfg . output . resolution . replaygain . noise_shaping ) ;
stream_data_ . is_playing = true ;
2000-12-10 04:09:52 +00:00
2006-09-13 01:42:27 +00:00
if ( flac_ip . output - > open_audio ( stream_data_ . sample_format , stream_data_ . sample_rate , stream_data_ . channels ) = = 0 ) {
2000-12-10 04:09:52 +00:00
audio_error_ = true ;
2006-09-13 01:42:27 +00:00
safe_decoder_finish_ ( decoder_ ) ;
2000-12-10 04:09:52 +00:00
return ;
}
2006-09-13 01:42:27 +00:00
stream_data_ . title = flac_format_song_title ( filename ) ;
flac_ip . set_info ( stream_data_ . title , stream_data_ . length_in_msec , stream_data_ . sample_rate * stream_data_ . channels * stream_data_ . bits_per_sample , stream_data_ . sample_rate , stream_data_ . channels ) ;
2000-12-10 04:09:52 +00:00
2006-09-13 01:42:27 +00:00
stream_data_ . seek_to_in_sec = - 1 ;
stream_data_ . play_thread_open = true ;
2000-12-10 04:09:52 +00:00
pthread_create ( & decode_thread_ , NULL , play_loop_ , NULL ) ;
}
void FLAC_XMMS__stop ( )
{
2006-09-13 01:42:27 +00:00
if ( stream_data_ . is_playing ) {
stream_data_ . is_playing = false ;
if ( stream_data_ . play_thread_open ) {
stream_data_ . play_thread_open = false ;
2001-12-04 01:27:32 +00:00
pthread_join ( decode_thread_ , NULL ) ;
}
2000-12-10 04:09:52 +00:00
flac_ip . output - > close_audio ( ) ;
2006-09-13 01:42:27 +00:00
safe_decoder_finish_ ( decoder_ ) ;
2000-12-10 04:09:52 +00:00
}
}
void FLAC_XMMS__pause ( short p )
{
flac_ip . output - > pause ( p ) ;
}
void FLAC_XMMS__seek ( int time )
{
2006-09-13 01:42:27 +00:00
if ( ! stream_data_ . is_http_source ) {
stream_data_ . seek_to_in_sec = time ;
stream_data_ . eof = false ;
2000-12-10 04:09:52 +00:00
2006-09-13 01:42:27 +00:00
while ( stream_data_ . seek_to_in_sec ! = - 1 )
2004-10-07 00:23:51 +00:00
xmms_usleep ( 10000 ) ;
}
2000-12-10 04:09:52 +00:00
}
int FLAC_XMMS__get_time ( )
{
if ( audio_error_ )
return - 2 ;
2006-09-13 01:42:27 +00:00
if ( ! stream_data_ . is_playing | | ( stream_data_ . eof & & ! flac_ip . output - > buffer_playing ( ) ) )
2000-12-10 04:09:52 +00:00
return - 1 ;
else
return flac_ip . output - > output_time ( ) ;
}
void FLAC_XMMS__cleanup ( )
{
2006-09-13 01:42:27 +00:00
safe_decoder_delete_ ( decoder_ ) ;
2002-06-07 05:27:37 +00:00
decoder_ = 0 ;
2000-12-10 04:09:52 +00:00
}
void FLAC_XMMS__get_song_info ( char * filename , char * * title , int * length_in_msec )
{
2002-06-10 04:42:35 +00:00
FLAC__StreamMetadata streaminfo ;
2002-06-07 05:27:37 +00:00
if ( 0 = = filename )
filename = " " ;
if ( ! FLAC__metadata_get_streaminfo ( filename , & streaminfo ) ) {
/* @@@ how to report the error? */
if ( title ) {
2006-09-13 01:42:27 +00:00
if ( ! is_http_source ( filename ) ) {
2004-10-07 00:23:51 +00:00
static const char * errtitle = " Invalid FLAC File: " ;
* title = g_malloc ( strlen ( errtitle ) + 1 + strlen ( filename ) + 1 + 1 ) ;
sprintf ( * title , " %s \" %s \" " , errtitle , filename ) ;
} else {
* title = NULL ;
}
2002-06-07 05:27:37 +00:00
}
if ( length_in_msec )
* length_in_msec = - 1 ;
return ;
}
2000-12-10 04:09:52 +00:00
if ( title ) {
2002-07-11 06:15:30 +00:00
* title = flac_format_song_title ( filename ) ;
2000-12-10 04:09:52 +00:00
}
2006-05-24 04:41:36 +00:00
if ( length_in_msec ) {
FLAC__uint64 l = ( FLAC__uint64 ) ( ( double ) streaminfo . data . stream_info . total_samples / ( double ) streaminfo . data . stream_info . sample_rate * 1000.0 + 0.5 ) ;
if ( l > INT_MAX )
l = INT_MAX ;
* length_in_msec = ( int ) l ;
}
2000-12-10 04:09:52 +00:00
}
/***********************************************************************
* local routines
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-02-02 00:52:47 +00:00
void * play_loop_ ( void * arg )
{
2003-01-04 01:04:41 +00:00
unsigned written_time_last = 0 , bh_index_last_w = 0 , bh_index_last_o = BITRATE_HIST_SIZE , blocksize = 1 ;
FLAC__uint64 decode_position_last = 0 , decode_position_frame_last = 0 , decode_position_frame = 0 ;
2002-11-14 04:56:57 +00:00
2001-02-02 00:52:47 +00:00
( void ) arg ;
2006-09-13 01:42:27 +00:00
while ( stream_data_ . is_playing ) {
if ( ! stream_data_ . eof ) {
2003-02-27 06:12:55 +00:00
while ( sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE ) {
2003-01-04 01:04:41 +00:00
unsigned s ;
2003-02-27 06:12:55 +00:00
s = sample_buffer_last_ - sample_buffer_first_ ;
2006-09-13 01:42:27 +00:00
if ( FLAC__stream_decoder_get_state ( decoder_ ) = = FLAC__STREAM_DECODER_END_OF_STREAM ) {
stream_data_ . eof = true ;
2001-02-02 00:52:47 +00:00
break ;
}
2006-09-13 01:42:27 +00:00
else if ( ! FLAC__stream_decoder_process_single ( decoder_ ) ) {
2002-06-18 16:08:36 +00:00
/*@@@ this should probably be a dialog */
fprintf ( stderr , " libxmms-flac: READ ERROR processing frame \n " ) ;
2006-09-13 01:42:27 +00:00
stream_data_ . eof = true ;
2001-02-02 00:52:47 +00:00
break ;
2002-06-18 16:08:36 +00:00
}
2003-02-27 06:12:55 +00:00
blocksize = sample_buffer_last_ - sample_buffer_first_ - s ;
2003-01-04 01:04:41 +00:00
decode_position_frame_last = decode_position_frame ;
2006-09-13 01:42:27 +00:00
if ( stream_data_ . is_http_source | | ! FLAC__stream_decoder_get_decode_position ( decoder_ , & decode_position_frame ) )
2003-01-04 01:04:41 +00:00
decode_position_frame = 0 ;
2001-02-02 00:52:47 +00:00
}
2003-02-27 06:12:55 +00:00
if ( sample_buffer_last_ - sample_buffer_first_ > 0 ) {
const unsigned n = min ( sample_buffer_last_ - sample_buffer_first_ , SAMPLES_PER_WRITE ) ;
2006-09-13 01:42:27 +00:00
int bytes = n * stream_data_ . channels * stream_data_ . sample_format_bytes_per_sample ;
FLAC__byte * sample_buffer_start = sample_buffer_ + sample_buffer_first_ * stream_data_ . channels * stream_data_ . sample_format_bytes_per_sample ;
2003-02-27 06:12:55 +00:00
unsigned written_time , bh_index_w ;
2002-11-14 04:56:57 +00:00
FLAC__uint64 decode_position ;
2002-08-29 08:13:42 +00:00
2003-02-27 06:12:55 +00:00
sample_buffer_first_ + = n ;
2006-09-13 01:42:27 +00:00
flac_ip . add_vis_pcm ( flac_ip . output - > written_time ( ) , stream_data_ . sample_format , stream_data_ . channels , bytes , sample_buffer_start ) ;
while ( flac_ip . output - > buffer_free ( ) < ( int ) bytes & & stream_data_ . is_playing & & stream_data_ . seek_to_in_sec = = - 1 )
2001-02-02 00:52:47 +00:00
xmms_usleep ( 10000 ) ;
2006-09-13 01:42:27 +00:00
if ( stream_data_ . is_playing & & stream_data_ . seek_to_in_sec = = - 1 )
2003-02-27 06:12:55 +00:00
flac_ip . output - > write_audio ( sample_buffer_start , bytes ) ;
2002-11-14 04:56:57 +00:00
/* compute current bitrate */
written_time = flac_ip . output - > written_time ( ) ;
2003-01-04 01:04:41 +00:00
bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE ;
if ( bh_index_w ! = bh_index_last_w ) {
2002-11-14 04:56:57 +00:00
bh_index_last_w = bh_index_w ;
2003-02-27 06:12:55 +00:00
decode_position = decode_position_frame - ( double ) ( sample_buffer_last_ - sample_buffer_first_ ) * ( double ) ( decode_position_frame - decode_position_frame_last ) / ( double ) blocksize ;
2002-11-14 04:56:57 +00:00
bitrate_history_ [ ( bh_index_w + BITRATE_HIST_SIZE - 1 ) % BITRATE_HIST_SIZE ] =
decode_position > decode_position_last & & written_time > written_time_last ?
8000 * ( decode_position - decode_position_last ) / ( written_time - written_time_last ) :
2006-09-13 01:42:27 +00:00
stream_data_ . sample_rate * stream_data_ . channels * stream_data_ . bits_per_sample ;
2002-11-14 04:56:57 +00:00
decode_position_last = decode_position ;
written_time_last = written_time ;
}
2001-02-02 00:52:47 +00:00
}
else {
2006-09-13 01:42:27 +00:00
stream_data_ . eof = true ;
2001-02-02 00:52:47 +00:00
xmms_usleep ( 10000 ) ;
}
}
else
xmms_usleep ( 10000 ) ;
2006-09-13 01:42:27 +00:00
if ( ! stream_data_ . is_http_source & & stream_data_ . seek_to_in_sec ! = - 1 ) {
const double distance = ( double ) stream_data_ . seek_to_in_sec * 1000.0 / ( double ) stream_data_ . length_in_msec ;
FLAC__uint64 target_sample = ( FLAC__uint64 ) ( distance * ( double ) stream_data_ . total_samples ) ;
if ( stream_data_ . total_samples > 0 & & target_sample > = stream_data_ . total_samples )
target_sample = stream_data_ . total_samples - 1 ;
if ( FLAC__stream_decoder_seek_absolute ( decoder_ , target_sample ) ) {
flac_ip . output - > flush ( stream_data_ . seek_to_in_sec * 1000 ) ;
2002-11-15 05:48:10 +00:00
bh_index_last_w = bh_index_last_o = flac_ip . output - > output_time ( ) / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE ;
2006-09-13 01:42:27 +00:00
if ( ! FLAC__stream_decoder_get_decode_position ( decoder_ , & decode_position_frame ) )
2003-01-04 01:04:41 +00:00
decode_position_frame = 0 ;
2006-09-13 01:42:27 +00:00
stream_data_ . eof = false ;
2003-02-27 06:12:55 +00:00
sample_buffer_first_ = sample_buffer_last_ = 0 ;
2001-02-02 00:52:47 +00:00
}
2006-09-13 01:42:27 +00:00
else if ( FLAC__stream_decoder_get_state ( decoder_ ) = = FLAC__STREAM_DECODER_SEEK_ERROR ) {
/*@@@ this should probably be a dialog */
fprintf ( stderr , " libxmms-flac: SEEK ERROR \n " ) ;
FLAC__stream_decoder_flush ( decoder_ ) ;
stream_data_ . eof = false ;
sample_buffer_first_ = sample_buffer_last_ = 0 ;
}
stream_data_ . seek_to_in_sec = - 1 ;
2001-02-02 00:52:47 +00:00
}
2002-11-14 04:56:57 +00:00
else {
/* display the right bitrate from history */
unsigned bh_index_o = flac_ip . output - > output_time ( ) / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE ;
2002-11-15 05:48:10 +00:00
if ( bh_index_o ! = bh_index_last_o & & bh_index_o ! = bh_index_last_w & & bh_index_o ! = ( bh_index_last_w + 1 ) % BITRATE_HIST_SIZE ) {
2002-11-14 04:56:57 +00:00
bh_index_last_o = bh_index_o ;
2006-09-13 01:42:27 +00:00
flac_ip . set_info ( stream_data_ . title , stream_data_ . length_in_msec , bitrate_history_ [ bh_index_o ] , stream_data_ . sample_rate , stream_data_ . channels ) ;
2002-11-14 04:56:57 +00:00
}
}
2001-02-02 00:52:47 +00:00
}
2001-12-04 01:27:32 +00:00
2006-09-13 01:42:27 +00:00
safe_decoder_finish_ ( decoder_ ) ;
2001-02-02 00:52:47 +00:00
/* are these two calls necessary? */
flac_ip . output - > buffer_free ( ) ;
flac_ip . output - > buffer_free ( ) ;
2006-09-13 01:42:27 +00:00
g_free ( stream_data_ . title ) ;
2002-11-14 04:56:57 +00:00
2001-02-02 00:52:47 +00:00
pthread_exit ( NULL ) ;
return 0 ; /* to silence the compiler warning about not returning a value */
}
2000-12-10 04:09:52 +00:00
2006-09-13 01:42:27 +00:00
FLAC__bool safe_decoder_init_ ( const char * filename , FLAC__StreamDecoder * decoder )
2000-12-10 04:09:52 +00:00
{
2006-09-13 01:42:27 +00:00
if ( decoder = = 0 )
return false ;
2000-12-10 04:09:52 +00:00
2006-09-13 01:42:27 +00:00
safe_decoder_finish_ ( decoder ) ;
2000-12-10 04:09:52 +00:00
2006-09-13 01:42:27 +00:00
FLAC__stream_decoder_set_md5_checking ( decoder , false ) ;
FLAC__stream_decoder_set_metadata_ignore_all ( decoder ) ;
FLAC__stream_decoder_set_metadata_respond ( decoder , FLAC__METADATA_TYPE_STREAMINFO ) ;
FLAC__stream_decoder_set_metadata_respond ( decoder , FLAC__METADATA_TYPE_VORBIS_COMMENT ) ;
if ( stream_data_ . is_http_source ) {
flac_http_open ( filename , 0 ) ;
if ( FLAC__stream_decoder_init_stream ( decoder , http_read_callback_ , /*seek_callback=*/ 0 , /*tell_callback=*/ 0 , /*length_callback=*/ 0 , /*eof_callback=*/ 0 , write_callback_ , metadata_callback_ , error_callback_ , /*client_data=*/ & stream_data_ ) ! = FLAC__STREAM_DECODER_INIT_STATUS_OK )
return false ;
}
else {
if ( FLAC__stream_decoder_init_file ( decoder , filename , write_callback_ , metadata_callback_ , error_callback_ , /*client_data=*/ & stream_data_ ) ! = FLAC__STREAM_DECODER_INIT_STATUS_OK )
return false ;
2004-10-07 00:23:51 +00:00
}
2006-09-13 01:42:27 +00:00
if ( ! FLAC__stream_decoder_process_until_end_of_metadata ( decoder ) )
return false ;
2004-10-07 00:23:51 +00:00
2000-12-10 04:09:52 +00:00
return true ;
}
2006-09-13 01:42:27 +00:00
void safe_decoder_finish_ ( FLAC__StreamDecoder * decoder )
2004-10-07 00:23:51 +00:00
{
2006-09-13 01:42:27 +00:00
if ( decoder & & FLAC__stream_decoder_get_state ( decoder ) ! = FLAC__STREAM_DECODER_UNINITIALIZED )
FLAC__stream_decoder_finish ( decoder ) ;
if ( stream_data_ . is_http_source )
2004-10-07 00:23:51 +00:00
flac_http_close ( ) ;
}
2006-09-13 01:42:27 +00:00
void safe_decoder_delete_ ( FLAC__StreamDecoder * decoder )
2002-06-07 05:27:37 +00:00
{
if ( decoder ) {
2006-09-13 01:42:27 +00:00
safe_decoder_finish_ ( decoder ) ;
FLAC__stream_decoder_delete ( decoder ) ;
2002-06-07 05:27:37 +00:00
}
}
2006-09-13 01:42:27 +00:00
FLAC__StreamDecoderReadStatus http_read_callback_ ( const FLAC__StreamDecoder * decoder , FLAC__byte buffer [ ] , unsigned * bytes , void * client_data )
2004-10-07 00:23:51 +00:00
{
2006-09-13 01:42:27 +00:00
( void ) decoder ;
( void ) client_data ;
* bytes = flac_http_read ( buffer , * bytes ) ;
return * bytes ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ;
2004-10-07 00:23:51 +00:00
}
2006-09-13 01:42:27 +00:00
FLAC__StreamDecoderWriteStatus write_callback_ ( const FLAC__StreamDecoder * decoder , const FLAC__Frame * frame , const FLAC__int32 * const buffer [ ] , void * client_data )
2001-02-02 00:52:47 +00:00
{
2006-09-13 01:42:27 +00:00
stream_data_struct * stream_data = ( stream_data_struct * ) client_data ;
const unsigned channels = stream_data - > channels , wide_samples = frame - > header . blocksize ;
const unsigned bits_per_sample = stream_data - > bits_per_sample ;
2003-02-27 06:12:55 +00:00
FLAC__byte * sample_buffer_start ;
2001-02-02 00:52:47 +00:00
( void ) decoder ;
2006-09-13 01:42:27 +00:00
if ( stream_data - > abort_flag )
2002-06-04 05:44:32 +00:00
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT ;
2001-02-02 00:52:47 +00:00
2006-09-13 01:42:27 +00:00
if ( ( sample_buffer_last_ + wide_samples ) > ( SAMPLE_BUFFER_SIZE / ( channels * stream_data - > sample_format_bytes_per_sample ) ) ) {
memmove ( sample_buffer_ , sample_buffer_ + sample_buffer_first_ * channels * stream_data - > sample_format_bytes_per_sample , ( sample_buffer_last_ - sample_buffer_first_ ) * channels * stream_data - > sample_format_bytes_per_sample ) ;
2003-02-27 06:12:55 +00:00
sample_buffer_last_ - = sample_buffer_first_ ;
sample_buffer_first_ = 0 ;
}
2006-09-13 01:42:27 +00:00
sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * stream_data - > sample_format_bytes_per_sample ;
if ( stream_data - > has_replaygain & & flac_cfg . output . replaygain . enable ) {
2003-12-17 04:51:06 +00:00
FLAC__replaygain_synthesis__apply_gain (
2003-02-27 06:12:55 +00:00
sample_buffer_start ,
2003-09-15 20:28:08 +00:00
! is_big_endian_host_ ,
2006-09-13 01:42:27 +00:00
stream_data - > sample_format_bytes_per_sample = = 1 , /* unsigned_data_out */
2003-02-27 06:12:55 +00:00
buffer ,
wide_samples ,
channels ,
bits_per_sample ,
2006-09-13 01:42:27 +00:00
stream_data - > sample_format_bytes_per_sample * 8 ,
stream_data - > replay_scale ,
2003-02-27 06:12:55 +00:00
flac_cfg . output . replaygain . hard_limit ,
flac_cfg . output . resolution . replaygain . dither ,
2006-09-13 01:42:27 +00:00
& stream_data - > dither_context
2003-02-27 06:12:55 +00:00
) ;
}
2003-09-15 20:28:08 +00:00
else if ( is_big_endian_host_ ) {
FLAC__plugin_common__pack_pcm_signed_big_endian (
sample_buffer_start ,
buffer ,
wide_samples ,
channels ,
bits_per_sample ,
2006-09-13 01:42:27 +00:00
stream_data - > sample_format_bytes_per_sample * 8
2003-09-15 20:28:08 +00:00
) ;
}
2003-02-27 06:12:55 +00:00
else {
FLAC__plugin_common__pack_pcm_signed_little_endian (
sample_buffer_start ,
buffer ,
wide_samples ,
channels ,
bits_per_sample ,
2006-09-13 01:42:27 +00:00
stream_data - > sample_format_bytes_per_sample * 8
2003-02-27 06:12:55 +00:00
) ;
}
2001-02-02 00:52:47 +00:00
2003-02-27 06:12:55 +00:00
sample_buffer_last_ + = wide_samples ;
2001-02-02 00:52:47 +00:00
2002-06-04 05:44:32 +00:00
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE ;
2001-02-02 00:52:47 +00:00
}
2000-12-10 04:09:52 +00:00
2006-09-13 01:42:27 +00:00
void metadata_callback_ ( const FLAC__StreamDecoder * decoder , const FLAC__StreamMetadata * metadata , void * client_data )
2000-12-10 04:09:52 +00:00
{
2006-09-13 01:42:27 +00:00
stream_data_struct * stream_data = ( stream_data_struct * ) client_data ;
2000-12-10 04:09:52 +00:00
( void ) decoder ;
2001-03-05 18:36:59 +00:00
if ( metadata - > type = = FLAC__METADATA_TYPE_STREAMINFO ) {
2006-09-13 01:42:27 +00:00
stream_data - > total_samples = metadata - > data . stream_info . total_samples ;
stream_data - > bits_per_sample = metadata - > data . stream_info . bits_per_sample ;
stream_data - > channels = metadata - > data . stream_info . channels ;
stream_data - > sample_rate = metadata - > data . stream_info . sample_rate ;
2006-05-24 04:41:36 +00:00
{
2006-09-13 01:42:27 +00:00
FLAC__uint64 l = ( FLAC__uint64 ) ( ( double ) stream_data - > total_samples / ( double ) stream_data - > sample_rate * 1000.0 + 0.5 ) ;
2006-05-24 04:41:36 +00:00
if ( l > INT_MAX )
l = INT_MAX ;
2006-09-13 01:42:27 +00:00
stream_data - > length_in_msec = ( int ) l ;
2006-05-24 04:41:36 +00:00
}
2000-12-10 04:09:52 +00:00
}
2002-11-05 07:27:28 +00:00
else if ( metadata - > type = = FLAC__METADATA_TYPE_VORBIS_COMMENT ) {
2006-09-14 00:40:03 +00:00
double reference , gain , peak ;
if ( grabbag__replaygain_load_from_vorbiscomment ( metadata , flac_cfg . output . replaygain . album_mode , /*strict=*/ false , & reference , & gain , & peak ) ) {
2006-09-13 01:42:27 +00:00
stream_data - > has_replaygain = true ;
stream_data - > replay_scale = grabbag__replaygain_compute_scale_factor ( peak , gain , ( double ) flac_cfg . output . replaygain . preamp , /*prevent_clipping=*/ ! flac_cfg . output . replaygain . hard_limit ) ;
2002-11-05 07:27:28 +00:00
}
}
2000-12-10 04:09:52 +00:00
}
2006-09-13 01:42:27 +00:00
void error_callback_ ( const FLAC__StreamDecoder * decoder , FLAC__StreamDecoderErrorStatus status , void * client_data )
2000-12-10 04:09:52 +00:00
{
2006-09-13 01:42:27 +00:00
stream_data_struct * stream_data = ( stream_data_struct * ) client_data ;
2000-12-10 04:09:52 +00:00
( void ) decoder ;
2002-06-04 05:44:32 +00:00
if ( status ! = FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC )
2006-09-13 01:42:27 +00:00
stream_data - > abort_flag = true ;
2000-12-10 04:09:52 +00:00
}