1 /*****************************************************************************
2 * rawaud.c : raw audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
7 * Authors: Jarmo Torvinen <jarmo.torvinen@jutel.fi>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
43 #define SAMPLERATE_TEXT N_("Audio samplerate (Hz)")
44 #define SAMPLERATE_LONGTEXT N_("Audio sample rate in Hertz. Default is 48000 Hz.")
46 #define CHANNELS_TEXT N_("Audio channels")
47 #define CHANNELS_LONGTEXT N_("Audio channels in input stream. Numeric value >0. Default is 2.")
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." )
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'. ")
56 #ifdef WORDS_BIGENDIAN
57 # define FOURCC_DEFAULT "s16b"
59 # define FOURCC_DEFAULT "s16l"
63 set_shortname( "Raw Audio" );
64 set_description( N_("Raw audio demuxer") );
65 set_capability( "demux", 0 );
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_DEMUX );
68 set_callbacks( Open, Close );
69 add_shortcut( "rawaud" );
70 add_integer( "rawaud-channels", 2, CHANNELS_TEXT, CHANNELS_LONGTEXT, false );
71 add_integer( "rawaud-samplerate", 48000, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false );
72 add_string( "rawaud-fourcc", FOURCC_DEFAULT,
73 FOURCC_TEXT, FOURCC_LONGTEXT, false );
74 add_string( "rawaud-lang", "eng", LANG_TEXT, LANG_LONGTEXT, false);
77 /*****************************************************************************
78 * Definitions of structures used by this plugin
79 *****************************************************************************/
84 unsigned int i_frame_size;
85 unsigned int i_frame_samples;
86 unsigned int i_seek_step;
91 /*****************************************************************************
93 *****************************************************************************/
94 static int Demux( demux_t * );
95 static int Control( demux_t *, int i_query, va_list args );
98 /*****************************************************************************
99 * Open: initializes raw audio demuxer
100 *****************************************************************************/
101 static int Open( vlc_object_t * p_this )
103 demux_t *p_demux = (demux_t*)p_this;
106 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
110 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
112 char *psz_fourcc = var_CreateGetString( p_demux, "rawaud-fourcc" );
113 p_sys->fmt.i_codec = vlc_fourcc_GetCodecFromString( AUDIO_ES, psz_fourcc );
116 if( !p_sys->fmt.i_codec )
118 msg_Err( p_demux, "rawaud-fourcc must be a 4 character string");
119 es_format_Clean( &p_sys->fmt );
124 // get the bits per sample ratio based on codec
125 switch( p_sys->fmt.i_codec )
129 p_sys->fmt.audio.i_bitspersample = 64;
135 p_sys->fmt.audio.i_bitspersample = 32;
140 p_sys->fmt.audio.i_bitspersample = 24;
145 p_sys->fmt.audio.i_bitspersample = 16;
150 p_sys->fmt.audio.i_bitspersample = 8;
154 msg_Err( p_demux, "unknown fourcc format %4.4s",
155 (char *)&p_sys->fmt.i_codec);
156 es_format_Clean( &p_sys->fmt );
163 p_sys->fmt.psz_language = var_CreateGetString( p_demux, "rawaud-lang" );
164 p_sys->fmt.audio.i_channels = var_CreateGetInteger( p_demux, "rawaud-channels" );
165 p_sys->fmt.audio.i_rate = var_CreateGetInteger( p_demux, "rawaud-samplerate" );
167 if( p_sys->fmt.audio.i_rate <= 0 )
169 msg_Err( p_demux, "invalid sample rate");
170 es_format_Clean( &p_sys->fmt );
175 if( p_sys->fmt.audio.i_channels <= 0 )
177 msg_Err( p_demux, "invalid number of channels");
178 es_format_Clean( &p_sys->fmt );
183 p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate *
184 p_sys->fmt.audio.i_channels *
185 p_sys->fmt.audio.i_bitspersample;
188 "format initialized: channels=%d , samplerate=%d Hz, fourcc=%4.4s, bits per sample = %d, bitrate = %d bit/s",
189 p_sys->fmt.audio.i_channels,
190 p_sys->fmt.audio.i_rate,
191 (char*)&p_sys->fmt.i_codec,
192 p_sys->fmt.audio.i_bitspersample,
193 p_sys->fmt.i_bitrate);
196 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
197 msg_Dbg( p_demux, "elementary stream added");
199 /* initialize timing */
200 date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
201 date_Set( &p_sys->pts, 0 );
203 /* calculate 50ms frame size/time */
204 p_sys->i_frame_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
205 p_sys->i_seek_step = p_sys->fmt.audio.i_channels *
206 ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
207 p_sys->i_frame_size = p_sys->i_frame_samples * p_sys->i_seek_step;
208 msg_Dbg( p_demux, "frame size is %d bytes ", p_sys->i_frame_size);
210 p_demux->pf_demux = Demux;
211 p_demux->pf_control = Control;
215 /*****************************************************************************
216 * Close: frees unused data
217 *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
220 demux_t *p_demux = (demux_t*)p_this;
221 demux_sys_t *p_sys = p_demux->p_sys;
223 es_format_Clean( &p_sys->fmt );
227 /*****************************************************************************
228 * Demux: reads and demuxes data packets
229 *****************************************************************************
230 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
231 *****************************************************************************/
232 static int Demux( demux_t *p_demux )
234 demux_sys_t *p_sys = p_demux->p_sys;
237 if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
244 p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
246 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
247 es_out_Send( p_demux->out, p_sys->p_es, p_block );
249 date_Increment( &p_sys->pts, p_sys->i_frame_samples );
254 /*****************************************************************************
256 *****************************************************************************/
257 static int Control( demux_t *p_demux, int i_query, va_list args )
259 demux_sys_t *p_sys = p_demux->p_sys;
261 return demux_vaControlHelper( p_demux->s, 0, -1,
262 p_sys->fmt.i_bitrate, p_sys->i_seek_step, i_query, args );