]> git.sesse.net Git - vlc/blob - modules/demux/rawaud.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[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." )
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, 0, CHANNELS_TEXT, CHANNELS_LONGTEXT, false );
71     add_integer( "rawaud-samplerate", 48000, 0, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false );
72     add_string( "rawaud-fourcc", FOURCC_DEFAULT, NULL,
73                 FOURCC_TEXT, FOURCC_LONGTEXT, false );
74     add_string( "rawaud-lang", "eng", NULL, LANG_TEXT, LANG_LONGTEXT, false);
75 vlc_module_end();
76
77 /*****************************************************************************
78  * Definitions of structures used by this plugin
79  *****************************************************************************/
80 struct demux_sys_t
81 {
82     es_out_id_t *p_es;
83     es_format_t  fmt;
84     unsigned int i_frame_size;
85     unsigned int i_frame_samples;
86     unsigned int i_seek_step;
87     date_t       pts;
88 };
89
90
91 /*****************************************************************************
92  * Local prototypes
93  *****************************************************************************/
94 static int Demux( demux_t * );
95 static int Control( demux_t *, int i_query, va_list args );
96
97
98 /*****************************************************************************
99  * Open: initializes raw audio demuxer
100  *****************************************************************************/
101 static int Open( vlc_object_t * p_this )
102 {
103     demux_t     *p_demux = (demux_t*)p_this;
104     demux_sys_t *p_sys;
105
106     /* Set p_input field */
107     p_demux->pf_demux   = Demux;
108     p_demux->pf_control = Control;
109     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
110     if( !p_sys )
111         return VLC_ENOMEM;
112
113     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
114
115     char *psz_fourcc = var_CreateGetString( p_demux, "rawaud-fourcc" );
116
117     if( ( psz_fourcc == NULL ?  0 : strlen( psz_fourcc ) ) != 4  )
118     {
119         msg_Err( p_demux, "rawaud-fourcc must be a 4 character string");
120         free( psz_fourcc );
121         es_format_Clean( &p_sys->fmt );
122         free( p_sys );
123         return VLC_EGENERIC;
124     }
125
126     p_sys->fmt.i_codec = VLC_FOURCC( psz_fourcc[0], psz_fourcc[1],
127                                      psz_fourcc[2], psz_fourcc[3] );
128
129     free( psz_fourcc );
130     // get the bits per sample ratio based on codec
131     switch( p_sys->fmt.i_codec )
132     {
133
134         case VLC_FOURCC( 'f', 'l', '6', '4' ):
135             p_sys->fmt.audio.i_bitspersample = 64;
136             break;
137
138         case VLC_FOURCC( 'f', 'l', '3', '2' ):
139         case VLC_FOURCC( 's', '3', '2', 'l' ):
140         case VLC_FOURCC( 's', '3', '2', 'b' ):
141         case VLC_FOURCC( 'i', 'n', '3', '2' ):
142             p_sys->fmt.audio.i_bitspersample = 32;
143             break;
144
145         case VLC_FOURCC( 's', '2', '4', 'l' ):
146         case VLC_FOURCC( 's', '2', '4', 'b' ):
147         case VLC_FOURCC( 'i', 'n', '2', '4' ):
148         case VLC_FOURCC( '4', '2', 'n', 'i' ):
149             p_sys->fmt.audio.i_bitspersample = 24;
150             break;
151
152         case VLC_FOURCC( 's', '1', '6', 'l' ):
153         case VLC_FOURCC( 's', '1', '6', 'b' ):
154             p_sys->fmt.audio.i_bitspersample = 16;
155             break;
156
157         case VLC_FOURCC( 's', '8', ' ', ' ' ):
158         case VLC_FOURCC( 'u', '8', ' ', ' ' ):
159             p_sys->fmt.audio.i_bitspersample = 8;
160             break;
161
162         default:
163             msg_Err( p_demux, "unknown fourcc format %4.4s",
164                      (char *)&p_sys->fmt.i_codec);
165             es_format_Clean( &p_sys->fmt );
166             free( p_sys );
167             return VLC_EGENERIC;
168
169     }
170
171
172     p_sys->fmt.psz_language = var_CreateGetString( p_demux, "rawaud-lang" );
173     p_sys->fmt.audio.i_channels = var_CreateGetInteger( p_demux, "rawaud-channels" );
174     p_sys->fmt.audio.i_rate = var_CreateGetInteger( p_demux, "rawaud-samplerate" );
175
176     if( p_sys->fmt.audio.i_rate <= 0 )
177     {
178       msg_Err( p_demux, "invalid sample rate");
179       es_format_Clean( &p_sys->fmt );
180       free( p_sys );
181       return VLC_EGENERIC;
182     }
183
184     if( p_sys->fmt.audio.i_channels <= 0 )
185     {
186       msg_Err( p_demux, "invalid number of channels");
187       es_format_Clean( &p_sys->fmt );
188       free( p_sys );
189       return VLC_EGENERIC;
190     }
191
192     p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate *
193                            p_sys->fmt.audio.i_channels *
194                            p_sys->fmt.audio.i_bitspersample;
195
196     msg_Dbg( p_demux,
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);
203
204     /* add the es */
205     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
206     msg_Dbg( p_demux, "elementary stream added");
207
208     /* initialize timing */
209     date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
210     date_Set( &p_sys->pts, 1 );
211
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);
218
219     return VLC_SUCCESS;
220 }
221
222 /*****************************************************************************
223  * Close: frees unused data
224  *****************************************************************************/
225 static void Close( vlc_object_t *p_this )
226 {
227     demux_t     *p_demux = (demux_t*)p_this;
228     demux_sys_t *p_sys  = p_demux->p_sys;
229
230     es_format_Clean( &p_sys->fmt );
231     free( p_sys );
232 }
233
234 /*****************************************************************************
235  * Demux: reads and demuxes data packets
236  *****************************************************************************
237  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
238  *****************************************************************************/
239 static int Demux( demux_t *p_demux )
240 {
241     demux_sys_t *p_sys  = p_demux->p_sys;
242     block_t     *p_block;
243
244     if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
245     {
246         /* EOF */
247         return 0;
248     }
249
250     p_block->i_dts =
251     p_block->i_pts = date_Increment( &p_sys->pts, p_sys->i_frame_samples );
252
253     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
254     es_out_Send( p_demux->out, p_sys->p_es, p_block );
255     return 1;
256 }
257
258 /*****************************************************************************
259  * Control:
260  *****************************************************************************/
261 static int Control( demux_t *p_demux, int i_query, va_list args )
262 {
263     demux_sys_t *p_sys  = p_demux->p_sys;
264
265     return demux_vaControlHelper( p_demux->s, 0, -1,
266                                    p_sys->fmt.i_bitrate, p_sys->i_seek_step, i_query, args );
267 }