]> git.sesse.net Git - vlc/blob - modules/codec/wmafixed/wma.c
60e49195e823ce0fd84c5e9ac95bb63674dbd64e
[vlc] / modules / codec / wmafixed / wma.c
1 /*****************************************************************************
2  * wma.c: wma decoder using integer decoder from Rockbox, based on FFmpeg
3  *****************************************************************************
4  * Copyright (C) 2008-2009 M2X
5  *
6  * Authors: Rafaël Carré <rcarre@m2x.nl>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
33 #include <vlc_aout.h>
34 #include <vlc_block_helper.h>
35 #include <vlc_bits.h>
36
37 #include <assert.h>
38
39 #include "wmadec.h"
40
41 /*****************************************************************************
42  * decoder_sys_t : wma decoder descriptor
43  *****************************************************************************/
44 struct decoder_sys_t
45 {
46     date_t end_date; /* To set the PTS */
47     WMADecodeContext wmadec; /* name is self explanative */
48
49     int32_t *p_output; /* buffer where the frames are rendered */
50
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 */
54 };
55
56 /* FIXME : check supported configurations */
57 /* channel configuration */
58 static unsigned int pi_channels_maps[7] =
59 {
60     0,
61     AOUT_CHAN_CENTER,
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
67 };
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int  OpenDecoder   ( vlc_object_t * );
73 static void CloseDecoder  ( vlc_object_t * );
74
75 static aout_buffer_t *DecodeFrame  ( decoder_t *, block_t ** );
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80 vlc_module_begin();
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", 80 );
85     add_shortcut( "wmafixed" )
86     set_callbacks( OpenDecoder, CloseDecoder );
87 vlc_module_end();
88
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 )
94 {
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;
98
99     if( i_samples == 0 ) return NULL;
100
101     if( !( p_buffer = decoder_NewAudioBuffer( p_dec, i_samples ) ) )
102         return NULL;
103
104     p_buffer->i_pts = date_Get( &p_sys->end_date );
105     p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples )
106                          - p_buffer->i_pts;
107
108     memcpy( p_buffer->p_buffer, p_sys->p_samples, p_buffer->i_buffer );
109     p_sys->p_samples += p_buffer->i_buffer;
110     p_sys->i_samples -= i_samples;
111
112     return p_buffer;
113 }
114
115 /*****************************************************************************
116  * OpenDecoder: probe the decoder and return score
117  *****************************************************************************/
118 static int OpenDecoder( vlc_object_t *p_this )
119 {
120     decoder_t *p_dec = (decoder_t*)p_this;
121     decoder_sys_t *p_sys;
122
123     if( p_dec->fmt_in.i_codec != VLC_CODEC_WMA1 &&
124         p_dec->fmt_in.i_codec != VLC_CODEC_WMA2 )
125     {
126         return VLC_EGENERIC;
127     }
128
129     /* Allocate the memory needed to store the decoder's structure */
130     p_dec->p_sys = p_sys = (decoder_sys_t *)malloc(sizeof(decoder_sys_t));
131     if( !p_sys )
132         return VLC_ENOMEM;
133
134     memset( p_sys, 0, sizeof( decoder_sys_t ) );
135
136     /* Date */
137     date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
138
139     /* Set output properties */
140     p_dec->fmt_out.i_cat = AUDIO_ES;
141     p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
142     p_dec->fmt_out.audio.i_bitspersample = p_dec->fmt_in.audio.i_bitspersample;
143     p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
144
145     p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
146
147     assert( p_dec->fmt_out.audio.i_channels <
148         ( sizeof( pi_channels_maps ) / sizeof( pi_channels_maps[0] ) ) );
149
150     p_dec->fmt_out.audio.i_original_channels =
151         p_dec->fmt_out.audio.i_physical_channels =
152             pi_channels_maps[p_dec->fmt_out.audio.i_channels];
153
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 );
156
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;
163
164     msg_Dbg( p_dec, "samplerate %d bitrate %d channels %d align %d bps %d",
165         wfx.rate, wfx.bitrate, wfx.channels, wfx.blockalign,
166         wfx.bitspersample );
167
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;
172
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 );
177
178     /* Init codec */
179     if( wma_decode_init(&p_sys->wmadec, &wfx ) < 0 )
180     {
181         msg_Err( p_dec, "codec init failed" );
182         free( p_sys );
183         return VLC_EGENERIC;
184     }
185
186     /* Set callback */
187     p_dec->pf_decode_audio = DecodeFrame;
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * DecodeFrame: decodes a wma frame.
194  *****************************************************************************/
195 static aout_buffer_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
196 {
197     decoder_sys_t *p_sys = p_dec->p_sys;
198     block_t       *p_block;
199     aout_buffer_t *p_aout_buffer = NULL;
200
201     if( !pp_block || !*pp_block ) return NULL;
202
203     p_block = *pp_block;
204
205     if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
206     {
207         date_Set( &p_sys->end_date, 0 );
208         block_Release( p_block );
209         *pp_block = NULL;
210         return NULL;
211     }
212
213     if( p_block->i_buffer <= 0 )
214     {
215         /* we already decoded the samples, just feed a few to aout */
216         if( p_sys->i_samples )
217             p_aout_buffer = SplitBuffer( p_dec );
218         if( !p_sys->i_samples )
219         {   /* we need to decode new samples now */
220             free( p_sys->p_output );
221             p_sys->p_output = NULL;
222             block_Release( p_block );
223             *pp_block = NULL;
224         }
225         return p_aout_buffer;
226     }
227
228     /* Date management */
229     if( p_block->i_pts > VLC_TS_INVALID &&
230         p_block->i_pts != date_Get( &p_sys->end_date ) )
231     {
232         date_Set( &p_sys->end_date, p_block->i_pts );
233         /* don't reuse the same pts */
234         p_block->i_pts = VLC_TS_INVALID;
235     }
236     else if( !date_Get( &p_sys->end_date ) )
237     {
238         /* We've just started the stream, wait for the first PTS. */
239         block_Release( p_block );
240         return NULL;
241     }
242
243     if( wma_decode_superframe_init( &p_sys->wmadec, p_block->p_buffer,
244             p_block->i_buffer ) == 0 )
245     {
246         msg_Err( p_dec, "failed initializing wmafixed decoder" );
247         block_Release( p_block );
248         *pp_block = NULL;
249         return NULL;
250     }
251
252     if( p_sys->wmadec.nb_frames <= 0 )
253     {
254         msg_Err( p_dec, "can not decode, invalid ASF packet ?" );
255         block_Release( p_block );
256         *pp_block = NULL;
257         return NULL;
258     }
259
260     /* worst case */
261     size_t i_buffer = BLOCK_MAX_SIZE * MAX_CHANNELS * p_sys->wmadec.nb_frames;
262     free( p_sys->p_output );
263     p_sys->p_output = malloc(i_buffer * sizeof(int32_t) );
264     p_sys->p_samples = (int8_t*)p_sys->p_output;
265
266     if( !p_sys->p_output )
267     {
268         /* OOM, will try a bit later if VLC hasn't been killed */
269         block_Release( p_block );
270         return NULL;
271     }
272
273     p_sys->i_samples = 0;
274
275     for( int i = 0 ; i < p_sys->wmadec.nb_frames; i++ )
276     {
277         int i_samples = 0;
278
279         i_samples = wma_decode_superframe_frame( &p_sys->wmadec,
280                  p_sys->p_output + p_sys->i_samples * p_sys->wmadec.nb_channels,
281                  p_block->p_buffer, p_block->i_buffer );
282
283         if( i_samples < 0 )
284         {
285             msg_Warn( p_dec,
286                 "wma_decode_superframe_frame() failed for frame %d", i );
287             free( p_sys->p_output );
288             p_sys->p_output = NULL;
289             return NULL;
290         }
291         p_sys->i_samples += i_samples; /* advance in the samples buffer */
292     }
293
294     p_block->i_buffer = 0; /* this block has been decoded */
295
296     for( size_t s = 0 ; s < i_buffer; s++ )
297         p_sys->p_output[s] >>= 2; /* Q30 -> Q28 translation */
298
299     p_aout_buffer = SplitBuffer( p_dec );
300     assert( p_aout_buffer );
301
302     return p_aout_buffer;
303 }
304
305 /*****************************************************************************
306  * CloseDecoder : wma decoder destruction
307  *****************************************************************************/
308 static void CloseDecoder( vlc_object_t *p_this )
309 {
310     decoder_sys_t *p_sys = ((decoder_t*)p_this)->p_sys;
311
312     free( p_sys->p_output );
313     free( p_sys );
314 }