]> git.sesse.net Git - vlc/blob - modules/codec/wmafixed/wma.c
mediacodec: Handle VC1
[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 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.
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 Lesser General Public License for more details.
17  *
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  *****************************************************************************/
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_block_helper.h>
34 #include <vlc_bits.h>
35
36 #include <assert.h>
37
38 #include "wmadec.h"
39
40 /*****************************************************************************
41  * decoder_sys_t : wma decoder descriptor
42  *****************************************************************************/
43 struct decoder_sys_t
44 {
45     date_t end_date; /* To set the PTS */
46     WMADecodeContext wmadec; /* name is self explanative */
47
48     int32_t *p_output; /* buffer where the frames are rendered */
49
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 */
53 };
54
55 /* FIXME : check supported configurations */
56 /* channel configuration */
57 static unsigned int pi_channels_maps[7] =
58 {
59     0,
60     AOUT_CHAN_CENTER,
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
66 };
67
68 /*****************************************************************************
69  * Local prototypes
70  *****************************************************************************/
71 static int  OpenDecoder   ( vlc_object_t * );
72 static void CloseDecoder  ( vlc_object_t * );
73
74 static block_t *DecodeFrame( decoder_t *, block_t ** );
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 vlc_module_begin();
80     set_category( CAT_INPUT );
81     set_subcategory( SUBCAT_INPUT_ACODEC );
82     set_description( _("WMA v1/v2 fixed point audio decoder") );
83     set_capability( "decoder", 80 );
84     add_shortcut( "wmafixed" )
85     set_callbacks( OpenDecoder, CloseDecoder );
86 vlc_module_end();
87
88 /*****************************************************************************
89  * SplitBuffer: Needed because aout really doesn't like big audio chunk and
90  * wma produces easily > 30000 samples...
91  *****************************************************************************/
92 static block_t *SplitBuffer( decoder_t *p_dec )
93 {
94     decoder_sys_t *p_sys = p_dec->p_sys;
95     unsigned int i_samples = __MIN( p_sys->i_samples, 2048 );
96     block_t *p_buffer;
97
98     if( i_samples == 0 ) return NULL;
99
100     if( !( p_buffer = decoder_NewAudioBuffer( p_dec, i_samples ) ) )
101         return NULL;
102
103     p_buffer->i_pts = date_Get( &p_sys->end_date );
104     p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples )
105                          - p_buffer->i_pts;
106
107     memcpy( p_buffer->p_buffer, p_sys->p_samples, p_buffer->i_buffer );
108     p_sys->p_samples += p_buffer->i_buffer;
109     p_sys->i_samples -= i_samples;
110
111     return p_buffer;
112 }
113
114 /*****************************************************************************
115  * OpenDecoder: probe the decoder and return score
116  *****************************************************************************/
117 static int OpenDecoder( vlc_object_t *p_this )
118 {
119     decoder_t *p_dec = (decoder_t*)p_this;
120     decoder_sys_t *p_sys;
121
122     if( p_dec->fmt_in.i_codec != VLC_CODEC_WMA1 &&
123         p_dec->fmt_in.i_codec != VLC_CODEC_WMA2 )
124     {
125         return VLC_EGENERIC;
126     }
127
128     /* Allocate the memory needed to store the decoder's structure */
129     p_dec->p_sys = p_sys = (decoder_sys_t *)malloc(sizeof(decoder_sys_t));
130     if( !p_sys )
131         return VLC_ENOMEM;
132
133     memset( p_sys, 0, sizeof( decoder_sys_t ) );
134
135     /* Date */
136     date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
137
138     /* Set output properties */
139     p_dec->fmt_out.i_cat = AUDIO_ES;
140     p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
141     p_dec->fmt_out.audio.i_bitspersample = p_dec->fmt_in.audio.i_bitspersample;
142     p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
143
144     p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
145
146     assert( p_dec->fmt_out.audio.i_channels <
147         ( sizeof( pi_channels_maps ) / sizeof( pi_channels_maps[0] ) ) );
148
149     p_dec->fmt_out.audio.i_original_channels =
150         p_dec->fmt_out.audio.i_physical_channels =
151             pi_channels_maps[p_dec->fmt_out.audio.i_channels];
152
153     /* aout core assumes this number is not 0 and uses it in divisions */
154     assert( p_dec->fmt_out.audio.i_physical_channels != 0 );
155
156     asf_waveformatex_t wfx;
157     wfx.rate = p_dec->fmt_in.audio.i_rate;
158     wfx.bitrate = p_dec->fmt_in.i_bitrate;
159     wfx.channels = p_dec->fmt_in.audio.i_channels;
160     wfx.blockalign = p_dec->fmt_in.audio.i_blockalign;
161     wfx.bitspersample = p_dec->fmt_in.audio.i_bitspersample;
162
163     msg_Dbg( p_dec, "samplerate %d bitrate %d channels %d align %d bps %d",
164         wfx.rate, wfx.bitrate, wfx.channels, wfx.blockalign,
165         wfx.bitspersample );
166
167     if( p_dec->fmt_in.i_codec == VLC_CODEC_WMA1 )
168         wfx.codec_id = ASF_CODEC_ID_WMAV1;
169     else if( p_dec->fmt_in.i_codec == VLC_CODEC_WMA2 )
170         wfx.codec_id = ASF_CODEC_ID_WMAV2;
171
172     wfx.datalen = p_dec->fmt_in.i_extra;
173     if( wfx.datalen > 6 ) wfx.datalen = 6;
174     if( wfx.datalen > 0 )
175         memcpy( wfx.data, p_dec->fmt_in.p_extra, wfx.datalen );
176
177     /* Init codec */
178     if( wma_decode_init(&p_sys->wmadec, &wfx ) < 0 )
179     {
180         msg_Err( p_dec, "codec init failed" );
181         free( p_sys );
182         return VLC_EGENERIC;
183     }
184
185     /* Set callback */
186     p_dec->pf_decode_audio = DecodeFrame;
187
188     return VLC_SUCCESS;
189 }
190
191 /*****************************************************************************
192  * DecodeFrame: decodes a wma frame.
193  *****************************************************************************/
194 static block_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
195 {
196     decoder_sys_t *p_sys = p_dec->p_sys;
197     block_t       *p_block;
198     block_t       *p_aout_buffer = NULL;
199
200     if( !pp_block || !*pp_block ) return NULL;
201
202     p_block = *pp_block;
203
204     if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
205     {
206         date_Set( &p_sys->end_date, 0 );
207         block_Release( p_block );
208         *pp_block = NULL;
209         return NULL;
210     }
211
212     if( p_block->i_buffer <= 0 )
213     {
214         /* we already decoded the samples, just feed a few to aout */
215         if( p_sys->i_samples )
216             p_aout_buffer = SplitBuffer( p_dec );
217         if( !p_sys->i_samples )
218         {   /* we need to decode new samples now */
219             free( p_sys->p_output );
220             p_sys->p_output = NULL;
221             block_Release( p_block );
222             *pp_block = NULL;
223         }
224         return p_aout_buffer;
225     }
226
227     /* Date management */
228     if( p_block->i_pts > VLC_TS_INVALID &&
229         p_block->i_pts != date_Get( &p_sys->end_date ) )
230     {
231         date_Set( &p_sys->end_date, p_block->i_pts );
232         /* don't reuse the same pts */
233         p_block->i_pts = VLC_TS_INVALID;
234     }
235     else if( !date_Get( &p_sys->end_date ) )
236     {
237         /* We've just started the stream, wait for the first PTS. */
238         block_Release( p_block );
239         return NULL;
240     }
241
242     if( wma_decode_superframe_init( &p_sys->wmadec, p_block->p_buffer,
243             p_block->i_buffer ) == 0 )
244     {
245         msg_Err( p_dec, "failed initializing wmafixed decoder" );
246         block_Release( p_block );
247         *pp_block = NULL;
248         return NULL;
249     }
250
251     if( p_sys->wmadec.nb_frames <= 0 )
252     {
253         msg_Err( p_dec, "can not decode, invalid ASF packet ?" );
254         block_Release( p_block );
255         *pp_block = NULL;
256         return NULL;
257     }
258
259     /* worst case */
260     size_t i_buffer = BLOCK_MAX_SIZE * MAX_CHANNELS * p_sys->wmadec.nb_frames;
261     free( p_sys->p_output );
262     p_sys->p_output = malloc(i_buffer * sizeof(int32_t) );
263     p_sys->p_samples = (int8_t*)p_sys->p_output;
264
265     if( !p_sys->p_output )
266     {
267         /* OOM, will try a bit later if VLC hasn't been killed */
268         block_Release( p_block );
269         return NULL;
270     }
271
272     p_sys->i_samples = 0;
273
274     for( int i = 0 ; i < p_sys->wmadec.nb_frames; i++ )
275     {
276         int i_samples = 0;
277
278         i_samples = wma_decode_superframe_frame( &p_sys->wmadec,
279                  p_sys->p_output + p_sys->i_samples * p_sys->wmadec.nb_channels,
280                  p_block->p_buffer, p_block->i_buffer );
281
282         if( i_samples < 0 )
283         {
284             msg_Warn( p_dec,
285                 "wma_decode_superframe_frame() failed for frame %d", i );
286             free( p_sys->p_output );
287             p_sys->p_output = NULL;
288             return NULL;
289         }
290         p_sys->i_samples += i_samples; /* advance in the samples buffer */
291     }
292
293     p_block->i_buffer = 0; /* this block has been decoded */
294
295     for( size_t s = 0 ; s < i_buffer; s++ )
296         p_sys->p_output[s] <<= 2; /* Q30 -> Q32 translation */
297
298     p_aout_buffer = SplitBuffer( p_dec );
299     assert( p_aout_buffer );
300
301     return p_aout_buffer;
302 }
303
304 /*****************************************************************************
305  * CloseDecoder : wma decoder destruction
306  *****************************************************************************/
307 static void CloseDecoder( vlc_object_t *p_this )
308 {
309     decoder_sys_t *p_sys = ((decoder_t*)p_this)->p_sys;
310
311     free( p_sys->p_output );
312     free( p_sys );
313 }