1 /*****************************************************************************
2 * rawaud.c : raw audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
5 * $Id: 375bdb12444cbf50677c665692e81986182029e2 $
6 *
7 * Authors: Jarmo Torvinen <jarmo.torvinen@jutel.fi>
8 *
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.
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 Lesser General Public License for more details.
18 *
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 *****************************************************************************/
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." )
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 #ifdef WORDS_BIGENDIAN
57 # define FOURCC_DEFAULT "s16b"
58 #else
59 # define FOURCC_DEFAULT "s16l"
60 #endif
61
62 vlc_module_begin();
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 change_safe()
72 add_integer( "rawaud-samplerate", 48000, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false );
73 change_safe()
74 add_string( "rawaud-fourcc", FOURCC_DEFAULT,
75 FOURCC_TEXT, FOURCC_LONGTEXT, false );
76 change_safe()
77 add_string( "rawaud-lang", "eng", LANG_TEXT, LANG_LONGTEXT, false);
78 vlc_module_end();
79
80 /*****************************************************************************
81 * Definitions of structures used by this plugin
82 *****************************************************************************/
83 struct demux_sys_t
84 {
85 es_out_id_t *p_es;
86 es_format_t fmt;
87 unsigned int i_frame_size;
88 unsigned int i_frame_samples;
89 unsigned int i_seek_step;
90 date_t pts;
91 };
92
93
94 /*****************************************************************************
95 * Local prototypes
96 *****************************************************************************/
97 static int Demux( demux_t * );
98 static int Control( demux_t *, int i_query, va_list args );
99
100
101 /*****************************************************************************
102 * Open: initializes raw audio demuxer
103 *****************************************************************************/
Open(vlc_object_t * p_this)104 static int Open( vlc_object_t * p_this )
105 {
106 demux_t *p_demux = (demux_t*)p_this;
107 demux_sys_t *p_sys;
108
109 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
110 if( !p_sys )
111 return VLC_ENOMEM;
112
113 char *psz_fourcc = var_CreateGetString( p_demux, "rawaud-fourcc" );
114 es_format_Init( &p_sys->fmt, AUDIO_ES,
115 vlc_fourcc_GetCodecFromString( AUDIO_ES, psz_fourcc ) );
116 free( psz_fourcc );
117
118 if( !p_sys->fmt.i_codec )
119 {
120 msg_Err( p_demux, "rawaud-fourcc must be a 4 character string");
121 es_format_Clean( &p_sys->fmt );
122 free( p_sys );
123 return VLC_EGENERIC;
124 }
125
126 // get the bits per sample ratio based on codec
127 switch( p_sys->fmt.i_codec )
128 {
129
130 case VLC_CODEC_FL64:
131 p_sys->fmt.audio.i_bitspersample = 64;
132 break;
133
134 case VLC_CODEC_FL32:
135 case VLC_CODEC_S32L:
136 case VLC_CODEC_S32B:
137 p_sys->fmt.audio.i_bitspersample = 32;
138 break;
139
140 case VLC_CODEC_S24L:
141 case VLC_CODEC_S24B:
142 p_sys->fmt.audio.i_bitspersample = 24;
143 break;
144
145 case VLC_CODEC_S16L:
146 case VLC_CODEC_S16B:
147 p_sys->fmt.audio.i_bitspersample = 16;
148 break;
149
150 case VLC_CODEC_S8:
151 case VLC_CODEC_U8:
152 p_sys->fmt.audio.i_bitspersample = 8;
153 break;
154
155 default:
156 msg_Err( p_demux, "unknown fourcc format %4.4s",
157 (char *)&p_sys->fmt.i_codec);
158 es_format_Clean( &p_sys->fmt );
159 free( p_sys );
160 return VLC_EGENERIC;
161
162 }
163
164
165 p_sys->fmt.psz_language = var_CreateGetString( p_demux, "rawaud-lang" );
166 p_sys->fmt.audio.i_channels = var_CreateGetInteger( p_demux, "rawaud-channels" );
167 p_sys->fmt.audio.i_rate = var_CreateGetInteger( p_demux, "rawaud-samplerate" );
168
169 if( p_sys->fmt.audio.i_rate == 0 || p_sys->fmt.audio.i_rate > 384000 )
170 {
171 msg_Err( p_demux, "invalid sample rate");
172 es_format_Clean( &p_sys->fmt );
173 free( p_sys );
174 return VLC_EGENERIC;
175 }
176
177 if( p_sys->fmt.audio.i_channels == 0 || p_sys->fmt.audio.i_channels > 32 )
178 {
179 msg_Err( p_demux, "invalid number of channels");
180 es_format_Clean( &p_sys->fmt );
181 free( p_sys );
182 return VLC_EGENERIC;
183 }
184
185 p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate *
186 p_sys->fmt.audio.i_channels *
187 p_sys->fmt.audio.i_bitspersample;
188
189 if( p_sys->fmt.i_bitrate > 50000000)
190 {
191 msg_Err( p_demux, "invalid bitrate");
192 es_format_Clean( &p_sys->fmt );
193 free( p_sys );
194 return VLC_EGENERIC;
195 }
196
197 msg_Dbg( p_demux,
198 "format initialized: channels=%d , samplerate=%d Hz, fourcc=%4.4s, bits per sample = %d, bitrate = %d bit/s",
199 p_sys->fmt.audio.i_channels,
200 p_sys->fmt.audio.i_rate,
201 (char*)&p_sys->fmt.i_codec,
202 p_sys->fmt.audio.i_bitspersample,
203 p_sys->fmt.i_bitrate);
204
205 /* add the es */
206 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
207 msg_Dbg( p_demux, "elementary stream added");
208
209 /* initialize timing */
210 date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
211 date_Set( &p_sys->pts, 0 );
212
213 /* calculate 50ms frame size/time */
214 p_sys->i_frame_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
215 p_sys->i_seek_step = p_sys->fmt.audio.i_channels *
216 ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
217 p_sys->i_frame_size = p_sys->i_frame_samples * p_sys->i_seek_step;
218 msg_Dbg( p_demux, "frame size is %d bytes ", p_sys->i_frame_size);
219
220 p_demux->pf_demux = Demux;
221 p_demux->pf_control = Control;
222 return VLC_SUCCESS;
223 }
224
225 /*****************************************************************************
226 * Close: frees unused data
227 *****************************************************************************/
Close(vlc_object_t * p_this)228 static void Close( vlc_object_t *p_this )
229 {
230 demux_t *p_demux = (demux_t*)p_this;
231 demux_sys_t *p_sys = p_demux->p_sys;
232
233 es_format_Clean( &p_sys->fmt );
234 free( p_sys );
235 }
236
237 /*****************************************************************************
238 * Demux: reads and demuxes data packets
239 *****************************************************************************
240 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
241 *****************************************************************************/
Demux(demux_t * p_demux)242 static int Demux( demux_t *p_demux )
243 {
244 demux_sys_t *p_sys = p_demux->p_sys;
245 block_t *p_block;
246
247 p_block = vlc_stream_Block( p_demux->s, p_sys->i_frame_size );
248 if( p_block == NULL )
249 {
250 /* EOF */
251 return 0;
252 }
253
254 p_block->i_dts = p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
255
256 es_out_SetPCR( p_demux->out, p_block->i_pts );
257 es_out_Send( p_demux->out, p_sys->p_es, p_block );
258
259 date_Increment( &p_sys->pts, p_sys->i_frame_samples );
260
261 return 1;
262 }
263
264 /*****************************************************************************
265 * Control:
266 *****************************************************************************/
Control(demux_t * p_demux,int i_query,va_list args)267 static int Control( demux_t *p_demux, int i_query, va_list args )
268 {
269 demux_sys_t *p_sys = p_demux->p_sys;
270
271 return demux_vaControlHelper( p_demux->s, 0, -1,
272 p_sys->fmt.i_bitrate, p_sys->i_seek_step, i_query, args );
273 }
274