1 /*****************************************************************************
2 * wma.c: wma decoder using integer decoder from Rockbox, based on FFmpeg
3 *****************************************************************************
4 * Copyright (C) 2008-2009 M2X
6 * Authors: Rafaël Carré <rcarre@m2x.nl>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
34 #include <vlc_block_helper.h>
41 /*****************************************************************************
42 * decoder_sys_t : wma decoder descriptor
43 *****************************************************************************/
46 audio_date_t end_date; /* To set the PTS */
47 WMADecodeContext wmadec; /* name is self explanative */
49 int32_t *p_output; /* buffer where the frames are rendered */
51 /* to not give too much samples at once to the audio output */
52 int8_t *p_samples; /* point into p_output */
53 unsigned int i_samples; /* number of buffered samples available */
56 /* FIXME : check supported configurations */
57 /* channel configuration */
58 static unsigned int pi_channels_maps[7] =
62 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
63 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
64 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
65 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
66 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
69 /*****************************************************************************
71 *****************************************************************************/
72 static int OpenDecoder ( vlc_object_t * );
73 static void CloseDecoder ( vlc_object_t * );
75 static aout_buffer_t *DecodeFrame ( decoder_t *, block_t ** );
77 /*****************************************************************************
79 *****************************************************************************/
81 set_category( CAT_INPUT );
82 set_subcategory( SUBCAT_INPUT_ACODEC );
83 set_description( _("WMA v1/v2 fixed point audio decoder") );
84 set_capability( "decoder", 50 );
85 add_shortcut( "wmafixed" )
86 set_callbacks( OpenDecoder, CloseDecoder );
89 /*****************************************************************************
90 * SplitBuffer: Needed because aout really doesn't like big audio chunk and
91 * wma produces easily > 30000 samples...
92 *****************************************************************************/
93 static aout_buffer_t *SplitBuffer( decoder_t *p_dec )
95 decoder_sys_t *p_sys = p_dec->p_sys;
96 unsigned int i_samples = __MIN( p_sys->i_samples, 2048 );
97 aout_buffer_t *p_buffer;
99 if( i_samples == 0 ) return NULL;
101 if( !( p_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples ) ) )
104 p_buffer->start_date = aout_DateGet( &p_sys->end_date );
105 p_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_samples );
107 memcpy( p_buffer->p_buffer, p_sys->p_samples, p_buffer->i_nb_bytes );
108 p_sys->p_samples += p_buffer->i_nb_bytes;
109 p_sys->i_samples -= i_samples;
114 /*****************************************************************************
115 * OpenDecoder: probe the decoder and return score
116 *****************************************************************************/
117 static int OpenDecoder( vlc_object_t *p_this )
119 decoder_t *p_dec = (decoder_t*)p_this;
120 decoder_sys_t *p_sys;
122 if( p_dec->fmt_in.i_codec != VLC_FOURCC('w','m','a','1') &&
123 p_dec->fmt_in.i_codec != VLC_CODEC_WMA1 &&
124 p_dec->fmt_in.i_codec != VLC_FOURCC('w','m','a','2') &&
125 p_dec->fmt_in.i_codec != VLC_CODEC_WMA2 )
130 /* Allocate the memory needed to store the decoder's structure */
131 p_dec->p_sys = p_sys = (decoder_sys_t *)malloc(sizeof(decoder_sys_t));
135 memset( p_sys, 0, sizeof( decoder_sys_t ) );
138 aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
140 /* Set output properties */
141 p_dec->fmt_out.i_cat = AUDIO_ES;
142 p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
143 p_dec->fmt_out.audio.i_bitspersample = p_dec->fmt_in.audio.i_bitspersample;
144 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
146 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
148 assert( p_dec->fmt_out.audio.i_channels <
149 ( sizeof( pi_channels_maps ) / sizeof( pi_channels_maps[0] ) ) );
151 p_dec->fmt_out.audio.i_original_channels =
152 p_dec->fmt_out.audio.i_physical_channels =
153 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
155 /* aout core assumes this number is not 0 and uses it in divisions */
156 assert( p_dec->fmt_out.audio.i_physical_channels != 0 );
158 asf_waveformatex_t wfx;
159 wfx.rate = p_dec->fmt_in.audio.i_rate;
160 wfx.bitrate = p_dec->fmt_in.i_bitrate;
161 wfx.channels = p_dec->fmt_in.audio.i_channels;
162 wfx.blockalign = p_dec->fmt_in.audio.i_blockalign;
163 wfx.bitspersample = p_dec->fmt_in.audio.i_bitspersample;
165 msg_Dbg( p_dec, "samplerate %d bitrate %d channels %d align %d bps %d",
166 wfx.rate, wfx.bitrate, wfx.channels, wfx.blockalign,
169 if( p_dec->fmt_in.i_codec == VLC_FOURCC('w','m','a','1')
170 || p_dec->fmt_in.i_codec == VLC_CODEC_WMA1 )
171 wfx.codec_id = ASF_CODEC_ID_WMAV1;
172 else if( p_dec->fmt_in.i_codec == VLC_CODEC_WMA2
173 || p_dec->fmt_in.i_codec == VLC_FOURCC('w','m','a','2') )
174 wfx.codec_id = ASF_CODEC_ID_WMAV2;
176 wfx.datalen = p_dec->fmt_in.i_extra;
177 if( wfx.datalen > 6 ) wfx.datalen = 6;
178 if( wfx.datalen > 0 )
179 memcpy( wfx.data, p_dec->fmt_in.p_extra, wfx.datalen );
182 if( wma_decode_init(&p_sys->wmadec, &wfx ) < 0 )
184 msg_Err( p_dec, "codec init failed" );
190 p_dec->pf_decode_audio = DecodeFrame;
195 /*****************************************************************************
196 * DecodeFrame: decodes a wma frame.
197 *****************************************************************************/
198 static aout_buffer_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
200 decoder_sys_t *p_sys = p_dec->p_sys;
202 aout_buffer_t *p_aout_buffer = NULL;
204 mtime_t start = mdate(); /* for statistics */
207 if( !pp_block || !*pp_block ) return NULL;
211 if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
213 aout_DateSet( &p_sys->end_date, 0 );
214 block_Release( p_block );
219 if( p_block->i_buffer <= 0 )
221 /* we already decoded the samples, just feed a few to aout */
222 if( p_sys->i_samples )
223 p_aout_buffer = SplitBuffer( p_dec );
224 if( !p_sys->i_samples )
225 { /* we need to decode new samples now */
226 free( p_sys->p_output );
227 p_sys->p_output = NULL;
228 block_Release( p_block );
231 return p_aout_buffer;
234 /* Date management */
235 if( p_block->i_pts > 0 &&
236 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
238 aout_DateSet( &p_sys->end_date, p_block->i_pts );
239 /* don't reuse the same pts */
242 else if( !aout_DateGet( &p_sys->end_date ) )
244 /* We've just started the stream, wait for the first PTS. */
245 block_Release( p_block );
249 if( wma_decode_superframe_init( &p_sys->wmadec, p_block->p_buffer,
250 p_block->i_buffer ) == 0 )
252 msg_Err( p_dec, "failed initializing wmafixed decoder" );
253 block_Release( p_block );
258 if( p_sys->wmadec.nb_frames <= 0 )
260 msg_Err( p_dec, "can not decode, invalid ASF packet ?" );
261 block_Release( p_block );
267 size_t i_buffer = BLOCK_MAX_SIZE * MAX_CHANNELS * p_sys->wmadec.nb_frames;
268 if( p_sys->p_output )
269 free( p_sys->p_output );
270 p_sys->p_output = malloc(i_buffer * sizeof(int32_t) );
271 p_sys->p_samples = (int8_t*)p_sys->p_output;
273 if( !p_sys->p_output )
275 /* OOM, will try a bit later if VLC hasn't been killed */
276 block_Release( p_block );
280 p_sys->i_samples = 0;
282 for( int i = 0 ; i < p_sys->wmadec.nb_frames; i++ )
286 i_samples = wma_decode_superframe_frame( &p_sys->wmadec,
287 p_sys->p_output + p_sys->i_samples * p_sys->wmadec.nb_channels,
288 p_block->p_buffer, p_block->i_buffer );
293 "wma_decode_superframe_frame() failed for frame %d", i );
294 free( p_sys->p_output );
295 p_sys->p_output = NULL;
298 p_sys->i_samples += i_samples; /* advance in the samples buffer */
301 p_block->i_buffer = 0; /* this block has been decoded */
303 for( size_t s = 0 ; s < i_buffer; s++ )
304 p_sys->p_output[s] >>= 2; /* Q30 -> Q28 translation */
306 p_aout_buffer = SplitBuffer( p_dec );
307 assert( p_aout_buffer );
310 msg_Dbg( p_dec, "%s took %"PRIi64" us",__func__,mdate()-start);
312 return p_aout_buffer;
315 /*****************************************************************************
316 * CloseDecoder : wma decoder destruction
317 *****************************************************************************/
318 static void CloseDecoder( vlc_object_t *p_this )
320 decoder_sys_t *p_sys = ((decoder_t*)p_this)->p_sys;
322 free( p_sys->p_output );