2000-12-10 04:09:52 +00:00
/* libxmms-flac - XMMS FLAC input plugin
2002-01-26 17:36:39 +00:00
* Copyright ( C ) 2000 , 2001 , 2002 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 .
*/
# include <pthread.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <glib.h>
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-07-11 06:15:30 +00:00
# include "configure.h"
# include "wrap_id3.h"
# include "charset.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-08-29 08:13:42 +00:00
# define FLAC__DO_DITHER
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 ;
FLAC__bool eof ;
2001-12-04 01:27:32 +00:00
FLAC__bool play_thread_open ; /* if true, is_playing must also be true */
2000-12-10 04:09:52 +00:00
unsigned total_samples ;
unsigned bits_per_sample ;
unsigned channels ;
unsigned sample_rate ;
unsigned length_in_msec ;
2001-03-30 23:27:44 +00:00
AFormat sample_format ;
2000-12-10 04:09:52 +00:00
int seek_to_in_sec ;
} file_info_struct ;
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 ) ;
2002-06-07 05:27:37 +00:00
static FLAC__bool safe_decoder_init_ ( const char * filename , FLAC__FileDecoder * decoder ) ;
2002-06-10 18:25:43 +00:00
static void safe_decoder_finish_ ( FLAC__FileDecoder * decoder ) ;
static void safe_decoder_delete_ ( FLAC__FileDecoder * decoder ) ;
static FLAC__StreamDecoderWriteStatus write_callback_ ( const FLAC__FileDecoder * decoder , const FLAC__Frame * frame , const FLAC__int32 * const buffer [ ] , void * client_data ) ;
2002-06-08 04:53:42 +00:00
static void metadata_callback_ ( const FLAC__FileDecoder * decoder , const FLAC__StreamMetadata * metadata , void * client_data ) ;
2000-12-10 04:09:52 +00:00
static void error_callback_ ( const FLAC__FileDecoder * decoder , FLAC__StreamDecoderErrorStatus status , void * client_data ) ;
InputPlugin flac_ip =
{
NULL ,
NULL ,
2002-08-29 08:13:42 +00:00
" Reference FLAC Player v " FLAC__VERSION_STRING ,
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-07-11 06:15:30 +00:00
NULL , /* 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
2002-08-29 08:13:42 +00:00
static FLAC__int32 reservoir_ [ FLAC__MAX_BLOCK_SIZE * 2 /*for overflow*/ * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS ] ;
static FLAC__byte sample_buffer_ [ SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * ( 24 / 8 ) ] ; /* (24/8) for max bytes per sample */
static unsigned wide_samples_in_reservoir_ = 0 ;
2001-12-04 01:27:32 +00:00
static FLAC__FileDecoder * decoder_ = 0 ;
2000-12-10 04:09:52 +00:00
static file_info_struct file_info_ ;
static pthread_t decode_thread_ ;
2001-06-23 03:03:24 +00:00
static FLAC__bool audio_error_ = false ;
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 ;
}
void FLAC_XMMS__init ( )
{
2002-07-11 06:15:30 +00:00
ConfigFile * cfg ;
flac_cfg . tag_override = FALSE ;
g_free ( flac_cfg . tag_format ) ;
flac_cfg . 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-07-11 06:15:30 +00:00
xmms_cfg_read_boolean ( cfg , " flac " , " tag_override " , & flac_cfg . tag_override ) ;
2002-08-23 06:45:23 +00:00
if ( ! xmms_cfg_read_string ( cfg , " flac " , " tag_format " , & flac_cfg . tag_format ) )
2002-07-11 06:15:30 +00:00
flac_cfg . tag_format = g_strdup ( " %p - %t " ) ;
xmms_cfg_read_boolean ( cfg , " flac " , " convert_char_set " , & flac_cfg . convert_char_set ) ;
2002-08-23 06:45:23 +00:00
if ( ! xmms_cfg_read_string ( cfg , " flac " , " file_char_set " , & flac_cfg . file_char_set ) )
2002-08-29 08:13:42 +00:00
flac_cfg . file_char_set = FLAC_plugin__charset_get_current ( ) ;
2002-08-23 06:45:23 +00:00
if ( ! xmms_cfg_read_string ( cfg , " flac " , " user_char_set " , & flac_cfg . user_char_set ) )
2002-08-29 08:13:42 +00:00
flac_cfg . user_char_set = FLAC_plugin__charset_get_current ( ) ;
2002-07-11 06:15:30 +00:00
2001-06-13 18:21:03 +00:00
decoder_ = FLAC__file_decoder_new ( ) ;
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 ;
2002-07-11 06:15:30 +00:00
gchar * ret ;
2002-08-29 08:13:42 +00:00
# ifdef FLAC__DO_DITHER
const AFormat output_format = FMT_S16_NE ;
# else
const AFormat output_format = file_info_ . sample_format ;
# endif
2000-12-10 04:09:52 +00:00
2002-08-29 08:13:42 +00:00
wide_samples_in_reservoir_ = 0 ;
2001-12-04 01:27:32 +00:00
audio_error_ = false ;
file_info_ . abort_flag = false ;
file_info_ . is_playing = false ;
file_info_ . eof = false ;
file_info_ . play_thread_open = false ;
2000-12-10 04:09:52 +00:00
if ( 0 = = ( f = fopen ( filename , " r " ) ) )
return ;
fclose ( f ) ;
2002-06-07 05:27:37 +00:00
if ( decoder_ = = 0 )
return ;
if ( ! safe_decoder_init_ ( filename , decoder_ ) )
2000-12-10 04:09:52 +00:00
return ;
file_info_ . is_playing = true ;
2002-08-29 08:13:42 +00:00
if ( flac_ip . output - > open_audio ( output_format , file_info_ . sample_rate , file_info_ . channels ) = = 0 ) {
2000-12-10 04:09:52 +00:00
audio_error_ = true ;
2002-06-07 05:27:37 +00:00
safe_decoder_finish_ ( decoder_ ) ;
2000-12-10 04:09:52 +00:00
return ;
}
2002-07-11 06:15:30 +00:00
ret = flac_format_song_title ( filename ) ;
flac_ip . set_info ( ret , file_info_ . length_in_msec , file_info_ . sample_rate * file_info_ . channels * file_info_ . bits_per_sample , file_info_ . sample_rate , file_info_ . channels ) ;
g_free ( ret ) ;
2000-12-10 04:09:52 +00:00
file_info_ . seek_to_in_sec = - 1 ;
2001-12-04 01:27:32 +00:00
file_info_ . play_thread_open = true ;
2000-12-10 04:09:52 +00:00
pthread_create ( & decode_thread_ , NULL , play_loop_ , NULL ) ;
}
void FLAC_XMMS__stop ( )
{
if ( file_info_ . is_playing ) {
file_info_ . is_playing = false ;
2001-12-04 01:27:32 +00:00
if ( file_info_ . play_thread_open ) {
file_info_ . play_thread_open = false ;
pthread_join ( decode_thread_ , NULL ) ;
}
2000-12-10 04:09:52 +00:00
flac_ip . output - > close_audio ( ) ;
2002-06-07 05:27:37 +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 )
{
file_info_ . seek_to_in_sec = time ;
file_info_ . eof = false ;
while ( file_info_ . seek_to_in_sec ! = - 1 )
xmms_usleep ( 10000 ) ;
}
int FLAC_XMMS__get_time ( )
{
if ( audio_error_ )
return - 2 ;
if ( ! file_info_ . is_playing | | ( file_info_ . eof & & ! flac_ip . output - > buffer_playing ( ) ) )
return - 1 ;
else
return flac_ip . output - > output_time ( ) ;
}
void FLAC_XMMS__cleanup ( )
{
2002-06-10 18:25:43 +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 ) {
2002-06-10 18:25:43 +00:00
static const char * errtitle = " Invalid FLAC File: " ;
2002-06-07 05:27:37 +00:00
* title = g_malloc ( strlen ( errtitle ) + 1 + strlen ( filename ) + 1 + 1 ) ;
sprintf ( * title , " %s \" %s \" " , errtitle , filename ) ;
}
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
}
2002-06-07 05:27:37 +00:00
if ( length_in_msec )
2002-06-10 18:25:43 +00:00
* length_in_msec = streaminfo . data . stream_info . total_samples * 10 / ( streaminfo . data . stream_info . sample_rate / 100 ) ;
2000-12-10 04:09:52 +00:00
}
/***********************************************************************
* local routines
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-02-02 00:52:47 +00:00
void * play_loop_ ( void * arg )
{
( void ) arg ;
while ( file_info_ . is_playing ) {
if ( ! file_info_ . eof ) {
2002-08-29 08:13:42 +00:00
while ( wide_samples_in_reservoir_ < SAMPLES_PER_WRITE ) {
2001-06-16 07:32:25 +00:00
if ( FLAC__file_decoder_get_state ( decoder_ ) = = FLAC__FILE_DECODER_END_OF_FILE ) {
2001-02-02 00:52:47 +00:00
file_info_ . eof = true ;
break ;
}
2002-08-12 07:19:59 +00:00
else if ( ! FLAC__file_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 " ) ;
file_info_ . eof = true ;
2001-02-02 00:52:47 +00:00
break ;
2002-06-18 16:08:36 +00:00
}
2001-02-02 00:52:47 +00:00
}
2002-08-29 08:13:42 +00:00
if ( wide_samples_in_reservoir_ > 0 ) {
2001-02-02 00:52:47 +00:00
const unsigned channels = file_info_ . channels ;
2002-08-29 08:13:42 +00:00
const unsigned bits_per_sample = file_info_ . bits_per_sample ;
# ifdef FLAC__DO_DITHER
const unsigned target_bps = 16 ;
# else
const unsigned target_bps = bits_per_sample ;
# endif
const unsigned n = min ( wide_samples_in_reservoir_ , SAMPLES_PER_WRITE ) ;
const unsigned delta = n * channels ;
int bytes = ( int ) FLAC__plugin_common__pack_pcm_signed_little_endian ( sample_buffer_ , reservoir_ , n , channels , bits_per_sample , target_bps ) ;
unsigned i ;
for ( i = delta ; i < wide_samples_in_reservoir_ * channels ; i + + )
2001-04-18 23:02:08 +00:00
reservoir_ [ i - delta ] = reservoir_ [ i ] ;
2002-08-29 08:13:42 +00:00
wide_samples_in_reservoir_ - = n ;
2001-02-02 00:52:47 +00:00
2002-08-29 08:13:42 +00:00
flac_ip . add_vis_pcm ( flac_ip . output - > written_time ( ) , file_info_ . sample_format , channels , bytes , sample_buffer_ ) ;
2001-02-02 00:52:47 +00:00
while ( flac_ip . output - > buffer_free ( ) < ( int ) bytes & & file_info_ . is_playing & & file_info_ . seek_to_in_sec = = - 1 )
xmms_usleep ( 10000 ) ;
if ( file_info_ . is_playing & & file_info_ . seek_to_in_sec = = - 1 )
2002-08-29 08:13:42 +00:00
flac_ip . output - > write_audio ( sample_buffer_ , bytes ) ;
2001-02-02 00:52:47 +00:00
}
else {
file_info_ . eof = true ;
xmms_usleep ( 10000 ) ;
}
}
else
xmms_usleep ( 10000 ) ;
2001-12-04 01:27:32 +00:00
if ( file_info_ . seek_to_in_sec ! = - 1 ) {
2001-02-02 00:52:47 +00:00
const double distance = ( double ) file_info_ . seek_to_in_sec * 1000.0 / ( double ) file_info_ . length_in_msec ;
unsigned target_sample = ( unsigned ) ( distance * ( double ) file_info_ . total_samples ) ;
2001-06-23 03:03:24 +00:00
if ( FLAC__file_decoder_seek_absolute ( decoder_ , ( FLAC__uint64 ) target_sample ) ) {
2001-02-02 00:52:47 +00:00
flac_ip . output - > flush ( file_info_ . seek_to_in_sec * 1000 ) ;
file_info_ . seek_to_in_sec = - 1 ;
file_info_ . eof = false ;
2002-08-29 08:13:42 +00:00
wide_samples_in_reservoir_ = 0 ;
2001-02-02 00:52:47 +00:00
}
}
}
2001-12-04 01:27:32 +00:00
2002-06-07 05:27:37 +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 ( ) ;
pthread_exit ( NULL ) ;
return 0 ; /* to silence the compiler warning about not returning a value */
}
2000-12-10 04:09:52 +00:00
2002-06-07 05:27:37 +00:00
FLAC__bool safe_decoder_init_ ( const char * filename , FLAC__FileDecoder * decoder )
2000-12-10 04:09:52 +00:00
{
2002-06-07 05:27:37 +00:00
if ( decoder = = 0 )
2000-12-10 04:09:52 +00:00
return false ;
2002-06-07 05:27:37 +00:00
safe_decoder_finish_ ( decoder ) ;
FLAC__file_decoder_set_md5_checking ( decoder , false ) ;
FLAC__file_decoder_set_filename ( decoder , filename ) ;
FLAC__file_decoder_set_write_callback ( decoder , write_callback_ ) ;
FLAC__file_decoder_set_metadata_callback ( decoder , metadata_callback_ ) ;
FLAC__file_decoder_set_error_callback ( decoder , error_callback_ ) ;
FLAC__file_decoder_set_client_data ( decoder , & file_info_ ) ;
if ( FLAC__file_decoder_init ( decoder ) ! = FLAC__FILE_DECODER_OK )
2000-12-10 04:09:52 +00:00
return false ;
2002-08-12 07:19:59 +00:00
if ( ! FLAC__file_decoder_process_until_end_of_metadata ( decoder ) )
2000-12-10 04:09:52 +00:00
return false ;
return true ;
}
2002-06-07 05:27:37 +00:00
void safe_decoder_finish_ ( FLAC__FileDecoder * decoder )
{
if ( decoder & & FLAC__file_decoder_get_state ( decoder ) ! = FLAC__FILE_DECODER_UNINITIALIZED )
FLAC__file_decoder_finish ( decoder ) ;
}
void safe_decoder_delete_ ( FLAC__FileDecoder * decoder )
{
if ( decoder ) {
safe_decoder_finish_ ( decoder ) ;
FLAC__file_decoder_delete ( decoder ) ;
}
}
2002-06-10 18:25:43 +00:00
FLAC__StreamDecoderWriteStatus write_callback_ ( const FLAC__FileDecoder * decoder , const FLAC__Frame * frame , const FLAC__int32 * const buffer [ ] , void * client_data )
2001-02-02 00:52:47 +00:00
{
file_info_struct * file_info = ( file_info_struct * ) client_data ;
2002-08-29 08:13:42 +00:00
const unsigned channels = file_info - > channels , wide_samples = frame - > header . blocksize ;
unsigned wide_sample , offset_sample , channel ;
2001-02-02 00:52:47 +00:00
( void ) decoder ;
if ( file_info - > 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
2002-08-29 08:13:42 +00:00
for ( offset_sample = wide_samples_in_reservoir_ * channels , wide_sample = 0 ; wide_sample < wide_samples ; wide_sample + + )
for ( channel = 0 ; channel < channels ; channel + + , offset_sample + + )
reservoir_ [ offset_sample ] = buffer [ channel ] [ wide_sample ] ;
2001-02-02 00:52:47 +00:00
2002-08-29 08:13:42 +00:00
wide_samples_in_reservoir_ + = 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
2002-06-08 04:53:42 +00:00
void metadata_callback_ ( const FLAC__FileDecoder * decoder , const FLAC__StreamMetadata * metadata , void * client_data )
2000-12-10 04:09:52 +00:00
{
file_info_struct * file_info = ( file_info_struct * ) client_data ;
( void ) decoder ;
2001-03-05 18:36:59 +00:00
if ( metadata - > type = = FLAC__METADATA_TYPE_STREAMINFO ) {
2001-05-31 20:11:02 +00:00
FLAC__ASSERT ( metadata - > data . stream_info . total_samples < 0x100000000 ) ; /* this plugin can only handle < 4 gigasamples */
2001-03-05 18:36:59 +00:00
file_info - > total_samples = ( unsigned ) ( metadata - > data . stream_info . total_samples & 0xffffffff ) ;
file_info - > bits_per_sample = metadata - > data . stream_info . bits_per_sample ;
file_info - > channels = metadata - > data . stream_info . channels ;
file_info - > sample_rate = metadata - > data . stream_info . sample_rate ;
2000-12-10 04:09:52 +00:00
2002-08-29 08:13:42 +00:00
# ifdef FLAC__DO_DITHER
if ( file_info - > bits_per_sample = = 16 | | file_info - > bits_per_sample = = 24 ) {
file_info - > sample_format = FMT_S16_LE ;
}
else {
/*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 16/24-bit samples\n", "ERROR: plugin can only handle 16/24-bit samples", 0); */
file_info - > abort_flag = true ;
return ;
}
# else
2001-03-30 23:27:44 +00:00
if ( file_info - > bits_per_sample = = 8 ) {
file_info - > sample_format = FMT_S8 ;
}
else if ( file_info - > bits_per_sample = = 16 ) {
2002-08-29 08:13:42 +00:00
file_info - > sample_format = FMT_S16_LE ;
2001-03-30 23:27:44 +00:00
}
else {
2002-08-29 08:13:42 +00:00
/*@@@ 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); */
2000-12-10 04:09:52 +00:00
file_info - > abort_flag = true ;
return ;
}
2002-08-29 08:13:42 +00:00
# endif
2000-12-10 04:09:52 +00:00
file_info - > length_in_msec = file_info - > total_samples * 10 / ( file_info - > sample_rate / 100 ) ;
}
}
void error_callback_ ( const FLAC__FileDecoder * decoder , FLAC__StreamDecoderErrorStatus status , void * client_data )
{
file_info_struct * file_info = ( file_info_struct * ) client_data ;
( void ) decoder ;
2002-06-04 05:44:32 +00:00
if ( status ! = FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC )
2000-12-10 04:09:52 +00:00
file_info - > abort_flag = true ;
}