1 /*****************************************************************************
2 * rawaud.c : raw audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
6 * Authors: Jarmo Torvinen <jarmo.torvinen@jutel.fi>
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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t
* );
39 static void Close( vlc_object_t
* );
42 #define SAMPLERATE_TEXT N_("Audio samplerate (Hz)")
43 #define SAMPLERATE_LONGTEXT N_("Audio sample rate in Hertz. Default is 48000 Hz.")
45 #define CHANNELS_TEXT N_("Audio channels")
46 #define CHANNELS_LONGTEXT N_("Audio channels in input stream. Numeric value >0. Default is 2.")
48 #define FOURCC_TEXT N_("FOURCC code of raw input format")
49 #define FOURCC_LONGTEXT N_( \
50 "FOURCC code of the raw input format. This is a four character string." )
52 #define LANG_TEXT N_("Forces the audio language")
53 #define LANG_LONGTEXT N_("Forces the audio language for the output mux. Three letter ISO639 code. Default is 'eng'.")
55 #ifdef WORDS_BIGENDIAN
56 # define FOURCC_DEFAULT "s16b"
58 # define FOURCC_DEFAULT "s16l"
62 set_shortname( "Raw Audio" );
63 set_description( N_("Raw audio demuxer") );
64 set_capability( "demux", 0 );
65 set_category( CAT_INPUT
);
66 set_subcategory( SUBCAT_INPUT_DEMUX
);
67 set_callbacks( Open
, Close
);
68 add_shortcut( "rawaud" );
69 add_integer( "rawaud-channels", 2, CHANNELS_TEXT
, CHANNELS_LONGTEXT
, false );
71 add_integer( "rawaud-samplerate", 48000, SAMPLERATE_TEXT
, SAMPLERATE_LONGTEXT
, false );
73 add_string( "rawaud-fourcc", FOURCC_DEFAULT
,
74 FOURCC_TEXT
, FOURCC_LONGTEXT
, false );
76 add_string( "rawaud-lang", "eng", LANG_TEXT
, LANG_LONGTEXT
, false);
79 /*****************************************************************************
80 * Definitions of structures used by this plugin
81 *****************************************************************************/
86 unsigned int i_frame_size
;
87 unsigned int i_frame_samples
;
88 unsigned int i_seek_step
;
93 /*****************************************************************************
95 *****************************************************************************/
96 static int Demux( demux_t
* );
97 static int Control( demux_t
*, int i_query
, va_list args
);
100 /*****************************************************************************
101 * Open: initializes raw audio demuxer
102 *****************************************************************************/
103 static int Open( vlc_object_t
* p_this
)
105 demux_t
*p_demux
= (demux_t
*)p_this
;
108 p_demux
->p_sys
= p_sys
= malloc( sizeof( demux_sys_t
) );
112 char *psz_fourcc
= var_CreateGetString( p_demux
, "rawaud-fourcc" );
113 es_format_Init( &p_sys
->fmt
, AUDIO_ES
,
114 vlc_fourcc_GetCodecFromString( AUDIO_ES
, psz_fourcc
) );
117 if( !p_sys
->fmt
.i_codec
)
119 msg_Err( p_demux
, "rawaud-fourcc must be a 4 character string");
120 es_format_Clean( &p_sys
->fmt
);
125 // get the bits per sample ratio based on codec
126 switch( p_sys
->fmt
.i_codec
)
130 p_sys
->fmt
.audio
.i_bitspersample
= 64;
136 p_sys
->fmt
.audio
.i_bitspersample
= 32;
141 p_sys
->fmt
.audio
.i_bitspersample
= 24;
146 p_sys
->fmt
.audio
.i_bitspersample
= 16;
151 p_sys
->fmt
.audio
.i_bitspersample
= 8;
155 msg_Err( p_demux
, "unknown fourcc format %4.4s",
156 (char *)&p_sys
->fmt
.i_codec
);
157 es_format_Clean( &p_sys
->fmt
);
164 p_sys
->fmt
.psz_language
= var_CreateGetString( p_demux
, "rawaud-lang" );
165 p_sys
->fmt
.audio
.i_channels
= var_CreateGetInteger( p_demux
, "rawaud-channels" );
166 p_sys
->fmt
.audio
.i_rate
= var_CreateGetInteger( p_demux
, "rawaud-samplerate" );
168 if( p_sys
->fmt
.audio
.i_rate
== 0 || p_sys
->fmt
.audio
.i_rate
> 384000 )
170 msg_Err( p_demux
, "invalid sample rate");
171 es_format_Clean( &p_sys
->fmt
);
176 if( p_sys
->fmt
.audio
.i_channels
== 0 || p_sys
->fmt
.audio
.i_channels
> 32 )
178 msg_Err( p_demux
, "invalid number of channels");
179 es_format_Clean( &p_sys
->fmt
);
184 p_sys
->fmt
.i_bitrate
= p_sys
->fmt
.audio
.i_rate
*
185 p_sys
->fmt
.audio
.i_channels
*
186 p_sys
->fmt
.audio
.i_bitspersample
;
188 if( p_sys
->fmt
.i_bitrate
> 50000000)
190 msg_Err( p_demux
, "invalid bitrate");
191 es_format_Clean( &p_sys
->fmt
);
197 "format initialized: channels=%d , samplerate=%d Hz, fourcc=%4.4s, bits per sample = %d, bitrate = %d bit/s",
198 p_sys
->fmt
.audio
.i_channels
,
199 p_sys
->fmt
.audio
.i_rate
,
200 (char*)&p_sys
->fmt
.i_codec
,
201 p_sys
->fmt
.audio
.i_bitspersample
,
202 p_sys
->fmt
.i_bitrate
);
205 p_sys
->p_es
= es_out_Add( p_demux
->out
, &p_sys
->fmt
);
206 msg_Dbg( p_demux
, "elementary stream added");
208 /* initialize timing */
209 date_Init( &p_sys
->pts
, p_sys
->fmt
.audio
.i_rate
, 1 );
210 date_Set( &p_sys
->pts
, VLC_TICK_0
);
212 /* calculate 50ms frame size/time */
213 p_sys
->i_frame_samples
= __MAX( p_sys
->fmt
.audio
.i_rate
/ 20, 1 );
214 p_sys
->i_seek_step
= p_sys
->fmt
.audio
.i_channels
*
215 ( (p_sys
->fmt
.audio
.i_bitspersample
+ 7) / 8 );
216 p_sys
->i_frame_size
= p_sys
->i_frame_samples
* p_sys
->i_seek_step
;
217 msg_Dbg( p_demux
, "frame size is %d bytes ", p_sys
->i_frame_size
);
219 p_demux
->pf_demux
= Demux
;
220 p_demux
->pf_control
= Control
;
224 /*****************************************************************************
225 * Close: frees unused data
226 *****************************************************************************/
227 static void Close( vlc_object_t
*p_this
)
229 demux_t
*p_demux
= (demux_t
*)p_this
;
230 demux_sys_t
*p_sys
= p_demux
->p_sys
;
232 es_format_Clean( &p_sys
->fmt
);
236 /*****************************************************************************
237 * Demux: reads and demuxes data packets
238 *****************************************************************************
239 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
240 *****************************************************************************/
241 static int Demux( demux_t
*p_demux
)
243 demux_sys_t
*p_sys
= p_demux
->p_sys
;
246 p_block
= vlc_stream_Block( p_demux
->s
, p_sys
->i_frame_size
);
247 if( p_block
== NULL
)
248 return VLC_DEMUXER_EOF
;
250 p_block
->i_dts
= p_block
->i_pts
= date_Get( &p_sys
->pts
);
252 es_out_SetPCR( p_demux
->out
, p_block
->i_pts
);
253 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
255 date_Increment( &p_sys
->pts
, p_sys
->i_frame_samples
);
257 return VLC_DEMUXER_SUCCESS
;
260 /*****************************************************************************
262 *****************************************************************************/
263 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
265 demux_sys_t
*p_sys
= p_demux
->p_sys
;
267 return demux_vaControlHelper( p_demux
->s
, 0, -1,
268 p_sys
->fmt
.i_bitrate
, p_sys
->i_seek_step
, i_query
, args
);