]> git.sesse.net Git - vlc/blob - modules/demux/rawaud.c
Merge commit 'origin/1.0-bugfix'
[vlc] / modules / demux / rawaud.c
1 /*****************************************************************************
2  * rawaud.c : raw audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jarmo Torvinen <jarmo.torvinen@jutel.fi>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42
43 #define SAMPLERATE_TEXT N_("Audio samplerate (Hz)")
44 #define SAMPLERATE_LONGTEXT N_("Audio sample rate in Hertz. Default is 48000 Hz.")
45
46 #define CHANNELS_TEXT N_("Audio channels")
47 #define CHANNELS_LONGTEXT N_("Audio channels in input stream. Numeric value >0. Default is 2.")
48
49 #define FOURCC_TEXT N_("FOURCC code of raw input format")
50 #define FOURCC_LONGTEXT N_( \
51     "FOURCC code of the raw input format. This is a four character string. Default is s16l." )
52
53 #define LANG_TEXT N_("Forces the audio language.")
54 #define LANG_LONGTEXT N_("Forces the audio language for the output mux. Three letter ISO639 code. Default is 'eng'. ")
55
56 vlc_module_begin();
57     set_shortname( "Raw Audio" );
58     set_description( N_("Raw audio demuxer") );
59     set_capability( "demux", 0 );
60     set_category( CAT_INPUT );
61     set_subcategory( SUBCAT_INPUT_DEMUX );
62     set_callbacks( Open, Close );
63     add_shortcut( "rawaud" );
64     add_integer( "rawaud-channels", 2, 0, CHANNELS_TEXT, CHANNELS_LONGTEXT, false );
65     add_integer( "rawaud-samplerate", 48000, 0, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false );
66     add_string( "rawaud-fourcc", "s16l", NULL, FOURCC_TEXT, FOURCC_LONGTEXT, false );
67     add_string( "rawaud-lang", "eng", NULL, LANG_TEXT, LANG_LONGTEXT, false);
68 vlc_module_end();
69
70 /*****************************************************************************
71  * Definitions of structures used by this plugin
72  *****************************************************************************/
73 struct demux_sys_t
74 {
75     es_out_id_t *p_es;
76     es_format_t  fmt;
77     unsigned int i_frame_size;
78     unsigned int i_frame_samples;
79     unsigned int i_seek_step;
80     date_t       pts;
81 };
82
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 static int Demux( demux_t * );
88 static int Control( demux_t *, int i_query, va_list args );
89
90
91 /*****************************************************************************
92  * Open: initializes raw audio demuxer
93  *****************************************************************************/
94 static int Open( vlc_object_t * p_this )
95 {
96     demux_t     *p_demux = (demux_t*)p_this;
97     demux_sys_t *p_sys;
98
99     /* Set p_input field */
100     p_demux->pf_demux   = Demux;
101     p_demux->pf_control = Control;
102     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
103     if( !p_sys )
104         return VLC_ENOMEM;
105
106     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
107
108     char *psz_fourcc = var_CreateGetString( p_demux, "rawaud-fourcc" );
109     p_sys->fmt.i_codec = vlc_fourcc_GetCodecFromString( AUDIO_ES, psz_fourcc );
110     free( psz_fourcc );
111
112     if( !p_sys->fmt.i_codec )
113     {
114         msg_Err( p_demux, "rawaud-fourcc must be a 4 character string");
115         es_format_Clean( &p_sys->fmt );
116         free( p_sys );
117         return VLC_EGENERIC;
118     }
119
120     // get the bits per sample ratio based on codec
121     switch( p_sys->fmt.i_codec )
122     {
123
124         case VLC_CODEC_FL64:
125             p_sys->fmt.audio.i_bitspersample = 64;
126             break;
127
128         case VLC_CODEC_FL32:
129         case VLC_CODEC_S32L:
130         case VLC_CODEC_S32B:
131             p_sys->fmt.audio.i_bitspersample = 32;
132             break;
133
134         case VLC_CODEC_S24L:
135         case VLC_CODEC_S24B:
136             p_sys->fmt.audio.i_bitspersample = 24;
137             break;
138
139         case VLC_CODEC_S16L:
140         case VLC_CODEC_S16B:
141             p_sys->fmt.audio.i_bitspersample = 16;
142             break;
143
144         case VLC_CODEC_S8:
145         case VLC_CODEC_U8:
146             p_sys->fmt.audio.i_bitspersample = 8;
147             break;
148
149         default:
150             msg_Err( p_demux, "unknown fourcc format %4.4s",
151                      (char *)&p_sys->fmt.i_codec);
152             es_format_Clean( &p_sys->fmt );
153             free( p_sys );
154             return VLC_EGENERIC;
155
156     }
157
158
159     p_sys->fmt.psz_language = var_CreateGetString( p_demux, "rawaud-lang" );
160     p_sys->fmt.audio.i_channels = var_CreateGetInteger( p_demux, "rawaud-channels" );
161     p_sys->fmt.audio.i_rate = var_CreateGetInteger( p_demux, "rawaud-samplerate" );
162
163     if( p_sys->fmt.audio.i_rate <= 0 )
164     {
165       msg_Err( p_demux, "invalid sample rate");
166       es_format_Clean( &p_sys->fmt );
167       free( p_sys );
168       return VLC_EGENERIC;
169     }
170
171     if( p_sys->fmt.audio.i_channels <= 0 )
172     {
173       msg_Err( p_demux, "invalid number of channels");
174       es_format_Clean( &p_sys->fmt );
175       free( p_sys );
176       return VLC_EGENERIC;
177     }
178
179     p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate *
180                            p_sys->fmt.audio.i_channels *
181                            p_sys->fmt.audio.i_bitspersample;
182
183     msg_Dbg( p_demux,
184      "format initialized: channels=%d , samplerate=%d Hz, fourcc=%4.4s, bits per sample = %d, bitrate = %d bit/s",
185      p_sys->fmt.audio.i_channels,
186      p_sys->fmt.audio.i_rate,
187      (char*)&p_sys->fmt.i_codec,
188      p_sys->fmt.audio.i_bitspersample,
189      p_sys->fmt.i_bitrate);
190
191     /* add the es */
192     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
193     msg_Dbg( p_demux, "elementary stream added");
194
195     /* initialize timing */
196     date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
197     date_Set( &p_sys->pts, 1 );
198
199     /* calculate 50ms frame size/time */
200     p_sys->i_frame_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
201     p_sys->i_seek_step = p_sys->fmt.audio.i_channels *
202                           ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
203     p_sys->i_frame_size = p_sys->i_frame_samples * p_sys->i_seek_step;
204     msg_Dbg( p_demux, "frame size is %d bytes ", p_sys->i_frame_size);
205
206     return VLC_SUCCESS;
207 }
208
209 /*****************************************************************************
210  * Close: frees unused data
211  *****************************************************************************/
212 static void Close( vlc_object_t *p_this )
213 {
214     demux_t     *p_demux = (demux_t*)p_this;
215     demux_sys_t *p_sys  = p_demux->p_sys;
216
217     es_format_Clean( &p_sys->fmt );
218     free( p_sys );
219 }
220
221 /*****************************************************************************
222  * Demux: reads and demuxes data packets
223  *****************************************************************************
224  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
225  *****************************************************************************/
226 static int Demux( demux_t *p_demux )
227 {
228     demux_sys_t *p_sys  = p_demux->p_sys;
229     block_t     *p_block;
230
231     if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
232     {
233         /* EOF */
234         return 0;
235     }
236
237     p_block->i_dts =
238     p_block->i_pts = date_Increment( &p_sys->pts, p_sys->i_frame_samples );
239
240     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
241     es_out_Send( p_demux->out, p_sys->p_es, p_block );
242     return 1;
243 }
244
245 /*****************************************************************************
246  * Control:
247  *****************************************************************************/
248 static int Control( demux_t *p_demux, int i_query, va_list args )
249 {
250     demux_sys_t *p_sys  = p_demux->p_sys;
251
252     return demux_vaControlHelper( p_demux->s, 0, -1,
253                                    p_sys->fmt.i_bitrate, p_sys->i_seek_step, i_query, args );
254 }