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 it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * 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>
33 #include <vlc_block_helper.h>
40 /*****************************************************************************
41 * decoder_sys_t : wma decoder descriptor
42 *****************************************************************************/
45 date_t end_date
; /* To set the PTS */
46 WMADecodeContext wmadec
; /* name is self explanative */
48 int32_t *p_output
; /* buffer where the frames are rendered */
50 /* to not give too much samples at once to the audio output */
51 int8_t *p_samples
; /* point into p_output */
52 unsigned int i_samples
; /* number of buffered samples available */
55 /* FIXME : check supported configurations */
56 /* channel configuration */
57 static unsigned int pi_channels_maps
[7] =
61 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
,
62 AOUT_CHAN_CENTER
| AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
,
63 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
,
64 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
| AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
,
65 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
| AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
| AOUT_CHAN_LFE
68 /*****************************************************************************
70 *****************************************************************************/
71 static int OpenDecoder ( vlc_object_t
* );
72 static void CloseDecoder ( vlc_object_t
* );
74 static int DecodeFrame( decoder_t
*, block_t
* );
75 static void Flush( decoder_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( "audio decoder", 80 );
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 block_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 );
99 if( i_samples
== 0 ) return NULL
;
101 if( decoder_UpdateAudioFormat( p_dec
) )
103 if( !( p_buffer
= decoder_NewAudioBuffer( p_dec
, i_samples
) ) )
106 p_buffer
->i_pts
= date_Get( &p_sys
->end_date
);
107 p_buffer
->i_length
= date_Increment( &p_sys
->end_date
, i_samples
)
110 memcpy( p_buffer
->p_buffer
, p_sys
->p_samples
, p_buffer
->i_buffer
);
111 p_sys
->p_samples
+= p_buffer
->i_buffer
;
112 p_sys
->i_samples
-= i_samples
;
117 /*****************************************************************************
118 * OpenDecoder: probe the decoder and return score
119 *****************************************************************************/
120 static int OpenDecoder( vlc_object_t
*p_this
)
122 decoder_t
*p_dec
= (decoder_t
*)p_this
;
123 decoder_sys_t
*p_sys
;
125 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_WMA1
&&
126 p_dec
->fmt_in
.i_codec
!= VLC_CODEC_WMA2
)
131 /* Allocate the memory needed to store the decoder's structure */
132 p_dec
->p_sys
= p_sys
= (decoder_sys_t
*)malloc(sizeof(decoder_sys_t
));
136 memset( p_sys
, 0, sizeof( decoder_sys_t
) );
139 date_Init( &p_sys
->end_date
, p_dec
->fmt_in
.audio
.i_rate
, 1 );
141 /* Set output properties */
142 p_dec
->fmt_out
.i_codec
= VLC_CODEC_S32N
;
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_physical_channels
=
152 pi_channels_maps
[p_dec
->fmt_out
.audio
.i_channels
];
154 /* aout core assumes this number is not 0 and uses it in divisions */
155 assert( p_dec
->fmt_out
.audio
.i_physical_channels
!= 0 );
157 asf_waveformatex_t wfx
;
158 wfx
.rate
= p_dec
->fmt_in
.audio
.i_rate
;
159 wfx
.bitrate
= p_dec
->fmt_in
.i_bitrate
;
160 wfx
.channels
= p_dec
->fmt_in
.audio
.i_channels
;
161 wfx
.blockalign
= p_dec
->fmt_in
.audio
.i_blockalign
;
162 wfx
.bitspersample
= p_dec
->fmt_in
.audio
.i_bitspersample
;
164 msg_Dbg( p_dec
, "samplerate %d bitrate %d channels %d align %d bps %d",
165 wfx
.rate
, wfx
.bitrate
, wfx
.channels
, wfx
.blockalign
,
168 if( p_dec
->fmt_in
.i_codec
== VLC_CODEC_WMA1
)
169 wfx
.codec_id
= ASF_CODEC_ID_WMAV1
;
170 else if( p_dec
->fmt_in
.i_codec
== VLC_CODEC_WMA2
)
171 wfx
.codec_id
= ASF_CODEC_ID_WMAV2
;
173 wfx
.datalen
= p_dec
->fmt_in
.i_extra
;
174 if( wfx
.datalen
> 6 ) wfx
.datalen
= 6;
175 if( wfx
.datalen
> 0 )
176 memcpy( wfx
.data
, p_dec
->fmt_in
.p_extra
, wfx
.datalen
);
179 if( wma_decode_init(&p_sys
->wmadec
, &wfx
) < 0 )
181 msg_Err( p_dec
, "codec init failed" );
187 p_dec
->pf_decode
= DecodeFrame
;
188 p_dec
->pf_flush
= Flush
;
193 /*****************************************************************************
195 *****************************************************************************/
196 static void Flush( decoder_t
*p_dec
)
198 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
200 date_Set( &p_sys
->end_date
, VLC_TS_INVALID
);
203 /*****************************************************************************
204 * DecodeFrame: decodes a wma frame.
205 *****************************************************************************/
206 static int DecodeFrame( decoder_t
*p_dec
, block_t
*p_block
)
208 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
209 block_t
*p_aout_buffer
= NULL
;
211 if( p_block
== NULL
) /* No Drain */
212 return VLCDEC_SUCCESS
;
214 if( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
217 if( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
219 block_Release( p_block
);
220 return VLCDEC_SUCCESS
;
224 /* Date management */
225 if( p_block
->i_pts
> VLC_TS_INVALID
&&
226 p_block
->i_pts
!= date_Get( &p_sys
->end_date
) )
228 date_Set( &p_sys
->end_date
, p_block
->i_pts
);
229 /* don't reuse the same pts */
230 p_block
->i_pts
= VLC_TS_INVALID
;
232 else if( !date_Get( &p_sys
->end_date
) )
234 /* We've just started the stream, wait for the first PTS. */
235 block_Release( p_block
);
236 return VLCDEC_SUCCESS
;
239 if( wma_decode_superframe_init( &p_sys
->wmadec
, p_block
->p_buffer
,
240 p_block
->i_buffer
) == 0 )
242 msg_Err( p_dec
, "failed initializing wmafixed decoder" );
243 block_Release( p_block
);
244 return VLCDEC_SUCCESS
;
247 if( p_sys
->wmadec
.nb_frames
<= 0 )
249 msg_Err( p_dec
, "can not decode, invalid ASF packet ?" );
250 block_Release( p_block
);
251 return VLCDEC_SUCCESS
;
255 size_t i_buffer
= BLOCK_MAX_SIZE
* MAX_CHANNELS
* p_sys
->wmadec
.nb_frames
;
256 free( p_sys
->p_output
);
257 p_sys
->p_output
= malloc(i_buffer
* sizeof(int32_t) );
258 p_sys
->p_samples
= (int8_t*)p_sys
->p_output
;
260 if( !p_sys
->p_output
)
262 /* OOM, will try a bit later if VLC hasn't been killed */
263 block_Release( p_block
);
264 return VLCDEC_SUCCESS
;
267 p_sys
->i_samples
= 0;
269 for( int i
= 0 ; i
< p_sys
->wmadec
.nb_frames
; i
++ )
273 i_samples
= wma_decode_superframe_frame( &p_sys
->wmadec
,
274 p_sys
->p_output
+ p_sys
->i_samples
* p_sys
->wmadec
.nb_channels
,
275 p_block
->p_buffer
, p_block
->i_buffer
);
280 "wma_decode_superframe_frame() failed for frame %d", i
);
281 free( p_sys
->p_output
);
282 p_sys
->p_output
= NULL
;
283 return VLCDEC_SUCCESS
;
285 p_sys
->i_samples
+= i_samples
; /* advance in the samples buffer */
288 block_Release( p_block
); /* this block has been decoded */
290 for( size_t s
= 0 ; s
< i_buffer
; s
++ )
291 p_sys
->p_output
[s
] <<= 2; /* Q30 -> Q32 translation */
293 while( ( p_aout_buffer
= SplitBuffer( p_dec
) ) != NULL
)
294 decoder_QueueAudio( p_dec
, p_aout_buffer
);
296 if( !p_sys
->i_samples
)
297 { /* we need to decode new samples now */
298 free( p_sys
->p_output
);
299 p_sys
->p_output
= NULL
;
302 return VLCDEC_SUCCESS
;
305 /*****************************************************************************
306 * CloseDecoder : wma decoder destruction
307 *****************************************************************************/
308 static void CloseDecoder( vlc_object_t
*p_this
)
310 decoder_sys_t
*p_sys
= ((decoder_t
*)p_this
)->p_sys
;
312 free( p_sys
->p_output
);