From 7e29d932257d0bf6dca42ffecf9b0dce523ca92e Mon Sep 17 00:00:00 2001 From: Laurent Aimar Date: Sun, 17 Jun 2007 13:56:57 +0000 Subject: [PATCH] Added replay gain support for: - ogg/vorbis (close #125) - flac (and not ogg/flac yet) - mp3/aac (id3/ape) - mpc (internal/ape) --- modules/codec/vorbis.c | 31 +++++++++++++++++ modules/demux/flac.c | 9 +++-- modules/demux/mpc.c | 70 ++++++++++----------------------------- modules/demux/mpeg/m4a.c | 4 ++- modules/demux/mpeg/mpga.c | 11 +++--- 5 files changed, 66 insertions(+), 59 deletions(-) diff --git a/modules/codec/vorbis.c b/modules/codec/vorbis.c index e56ae9fc85..5dbdfb2589 100644 --- a/modules/codec/vorbis.c +++ b/modules/codec/vorbis.c @@ -672,6 +672,37 @@ static void ParseVorbisComments( decoder_t *p_dec ) psz_value ); } } + else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) || + !strcasecmp( psz_name, "RG_RADIO" ) ) + { + audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain; + + r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE; + r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value ); + } + else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) || + !strcasecmp( psz_name, "RG_PEAK" ) ) + { + audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain; + + r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE; + r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value ); + } + else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) || + !strcasecmp( psz_name, "RG_AUDIOPHILE" ) ) + { + audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain; + + r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE; + r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value ); + } + else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) ) + { + audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain; + + r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE; + r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value ); + } #if 0 //not used else if( !strcasecmp( psz_name, "musicbrainz_artistid" ) ) { diff --git a/modules/demux/flac.c b/modules/demux/flac.c index dc9fbfc71f..2b3050ea9f 100644 --- a/modules/demux/flac.c +++ b/modules/demux/flac.c @@ -63,6 +63,7 @@ struct demux_sys_t /* Packetizer */ decoder_t *p_packetizer; vlc_meta_t *p_meta; + audio_replay_gain_t replay_gain; int64_t i_time_offset; int64_t i_pts; @@ -114,6 +115,7 @@ static int Open( vlc_object_t * p_this ) p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); p_sys->b_start = VLC_TRUE; p_sys->p_meta = NULL; + memset( &p_sys->replay_gain, 0, sizeof(p_sys->replay_gain) ); p_sys->i_length = 0; p_sys->i_time_offset = 0; p_sys->i_pts = 0; @@ -178,6 +180,7 @@ static int Open( vlc_object_t * p_this ) p_sys->attachment[p_sys->i_cover_idx]->psz_name ); vlc_meta_SetArtURL( p_sys->p_meta, psz_url ); } + vlc_audio_replay_gain_MergeFromMeta( &p_sys->replay_gain, p_sys->p_meta ); return VLC_SUCCESS; } @@ -230,6 +233,7 @@ static int Demux( demux_t *p_demux ) if( p_sys->p_es == NULL ) { p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE; + p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain; p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out); } @@ -245,7 +249,6 @@ static int Demux( demux_t *p_demux ) p_block_out = p_next; } } - return 1; } @@ -636,7 +639,9 @@ static void ParseComment( demux_t *p_demux, uint8_t *p_data, int i_data ) else IF_EXTRACT("DATE=", psz_date ) else if( strchr( psz, '=' ) ) { - /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC and undocumented tags) */ + /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC, + * undocumented tags and replay gain ) */ + audio_replay_gain_t *r = &p_sys->replay_gain; char *p = strchr( psz, '=' ); *p++ = '\0'; vlc_meta_AddExtra( p_sys->p_meta, psz, p ); diff --git a/modules/demux/mpc.c b/modules/demux/mpc.c index 1db8b57e2d..e11be5d012 100644 --- a/modules/demux/mpc.c +++ b/modules/demux/mpc.c @@ -26,7 +26,7 @@ *****************************************************************************/ #include #include -#include +#include #include #include @@ -52,20 +52,12 @@ static int Open ( vlc_object_t * ); static void Close ( vlc_object_t * ); -static int pi_replaygain_type[] = { 0, 1, 2 }; -static const char *ppsz_replaygain_type[] = { N_("None"), N_("Title"), N_("Album") }; - vlc_module_begin(); - set_shortname( "MPC" ); - set_description( _("MusePack demuxer") ); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_DEMUX ); + set_description( _("MusePack demuxer") ); set_capability( "demux2", 145 ); - add_integer( "mpc-replaygain-type", 2, NULL, - REPLAYGAIN_TYPE_TEXT, REPLAYGAIN_TYPE_LONGTEXT, VLC_FALSE ); - change_integer_list( pi_replaygain_type, ppsz_replaygain_type, 0 ); - set_callbacks( Open, Close ); add_shortcut( "mpc" ); vlc_module_end(); @@ -104,7 +96,6 @@ static int Open( vlc_object_t * p_this ) { demux_t *p_demux = (demux_t*)p_this; demux_sys_t *p_sys; - char psz_info[4096]; es_format_t fmt; uint8_t *p_peek; module_t *p_id3; @@ -167,41 +158,6 @@ static int Open( vlc_object_t * p_this ) return VLC_EGENERIC; } - /* Handle reaply gain */ - if( p_sys->info.peak_title != 32767 ) - { - int i_type = var_CreateGetInteger( p_demux, "mpc-replaygain-type" ); - int gain; - int peak; - - if( i_type == 2 ) // album - { - gain = p_sys->info.gain_album; - peak = p_sys->info.peak_album; - } - else if( i_type == 1 ) // title - { - gain = p_sys->info.gain_title; - peak = p_sys->info.peak_title; - } - else - { - gain = 0; - peak = 0; - } - - if( gain ) - { - double g = pow( 10, (double)gain / 2000.0 ); - double gmax = (double)32767.0 / (peak+1); - if( g > gmax ) - g = gmax; - - msg_Dbg( p_demux, "Using reaply gain factor %f", g ); - mpc_decoder_scale_output( &p_sys->decoder, g ); - } - } - /* Fill p_demux fields */ p_demux->pf_demux = Demux; p_demux->pf_control = Control; @@ -223,8 +179,20 @@ static int Open( vlc_object_t * p_this ) fmt.audio.i_bitspersample = 32; fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels * fmt.audio.i_bitspersample; - p_sys->p_es = es_out_Add( p_demux->out, &fmt ); - + if( p_sys->info.peak_title > 0 ) + { + fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE; + fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.peak_title / 32767.0; + fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE; + fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_TRACK] = (float)p_sys->info.gain_title / 100.0; + } + if( p_sys->info.peak_album > 0 ) + { + fmt.audio_replay_gain.pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE; + fmt.audio_replay_gain.pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.peak_album / 32767.0; + fmt.audio_replay_gain.pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE; + fmt.audio_replay_gain.pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = (float)p_sys->info.gain_album / 100.0; + } /* Parse possible id3 header */ if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) ) @@ -237,10 +205,8 @@ static int Open( vlc_object_t * p_this ) if( !p_sys->p_meta ) p_sys->p_meta = vlc_meta_New(); - sprintf( psz_info, "Musepack v%d", p_sys->info.stream_version ); - //vlc_meta_SetCodecName( p_sys->p_meta, psz_info ); - // ^^ doesn't exist (yet?) to set VLC_META_CODEC_NAME, so... - fprintf( stderr, "***** WARNING: Unhandled child meta\n"); + vlc_audio_replay_gain_MergeFromMeta( &fmt.audio_replay_gain, p_sys->p_meta ); + p_sys->p_es = es_out_Add( p_demux->out, &fmt ); return VLC_SUCCESS; } diff --git a/modules/demux/mpeg/m4a.c b/modules/demux/mpeg/m4a.c index c2fed1176d..b263e90ec9 100644 --- a/modules/demux/mpeg/m4a.c +++ b/modules/demux/mpeg/m4a.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include "vlc_codec.h" /***************************************************************************** @@ -175,6 +175,8 @@ static int Demux( demux_t *p_demux) if( p_sys->p_es == NULL ) { p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE; + vlc_audio_replay_gain_MergeFromMeta( &p_sys->p_packetizer->fmt_out.audio_replay_gain, + p_sys->meta ); p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out); } diff --git a/modules/demux/mpeg/mpga.c b/modules/demux/mpeg/mpga.c index ec5cb291eb..48a645a969 100644 --- a/modules/demux/mpeg/mpga.c +++ b/modules/demux/mpeg/mpga.c @@ -30,7 +30,7 @@ #include #include #include -#include +#include #define MPGA_PACKET_SIZE 1024 @@ -245,9 +245,6 @@ static int Open( vlc_object_t * p_this ) else p_sys->b_initial_sync_failed = VLC_FALSE; - p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE; - p_sys->p_es = es_out_Add( p_demux->out, - &p_sys->p_packetizer->fmt_out); p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate; if( p_sys->i_xing_bytes && p_sys->i_xing_frames && @@ -269,6 +266,12 @@ static int Open( vlc_object_t * p_this ) module_Unneed( p_demux, p_id3 ); } + /* */ + p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE; + vlc_audio_replay_gain_MergeFromMeta( &p_sys->p_packetizer->fmt_out.audio_replay_gain, + p_sys->meta ); + p_sys->p_es = es_out_Add( p_demux->out, + &p_sys->p_packetizer->fmt_out); return VLC_SUCCESS; } -- 2.39.5