]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
Remove stdlib.h
[vlc] / modules / audio_output / file.c
1 /*****************************************************************************
2  * file.c : audio output which writes the samples to a file
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <string.h>
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_aout.h>
33 #include <vlc_codecs.h> /* WAVEHEADER */
34 #include <vlc_charset.h>
35
36 #define FRAME_SIZE 2048
37 #define A52_FRAME_NB 1536
38
39 /*****************************************************************************
40  * aout_sys_t: audio output method descriptor
41  *****************************************************************************
42  * This structure is part of the audio output thread descriptor.
43  * It describes the direct sound specific properties of an audio device.
44  *****************************************************************************/
45 struct aout_sys_t
46 {
47     FILE     * p_file;
48     vlc_bool_t b_add_wav_header;
49
50     WAVEHEADER waveh;                      /* Wave header of the output file */
51 };
52
53 #define CHANNELS_MAX 6
54 static int pi_channels_maps[CHANNELS_MAX+1] =
55 {
56     0,
57     AOUT_CHAN_CENTER,
58     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
59     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
60     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
61      | AOUT_CHAN_REARRIGHT,
62     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
63      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
64     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
65      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
66 };
67
68 /*****************************************************************************
69  * Local prototypes.
70  *****************************************************************************/
71 static int     Open        ( vlc_object_t * );
72 static void    Close       ( vlc_object_t * );
73 static void    Play        ( aout_instance_t * );
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 #define FORMAT_TEXT N_("Output format")
79 #define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
80     "\"u16_le\", \"s16_le\", \"u16_be\", \"s16_be\", \"fixed32\", " \
81     "\"float32\" or \"spdif\"")
82 #define CHANNELS_TEXT N_("Number of output channels")
83 #define CHANNELS_LONGTEXT N_("By default, all the channels of the incoming " \
84     "will be saved but you can restrict the number of channels here.")
85
86 #define WAV_TEXT N_("Add WAVE header")
87 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
88                         "header to the file.")
89
90 static const char *format_list[] = { "u8", "s8", "u16", "s16", "u16_le",
91                                      "s16_le", "u16_be", "s16_be", "fixed32",
92                                      "float32", "spdif" };
93 static int format_int[] = { VLC_FOURCC('u','8',' ',' '),
94                             VLC_FOURCC('s','8',' ',' '),
95                             AOUT_FMT_U16_NE, AOUT_FMT_S16_NE,
96                             VLC_FOURCC('u','1','6','l'),
97                             VLC_FOURCC('s','1','6','l'),
98                             VLC_FOURCC('u','1','6','b'),
99                             VLC_FOURCC('s','1','6','b'),
100                             VLC_FOURCC('f','i','3','2'),
101                             VLC_FOURCC('f','l','3','2'),
102                             VLC_FOURCC('s','p','i','f') };
103
104 #define FILE_TEXT N_("Output file")
105 #define FILE_LONGTEXT N_("File to which the audio samples will be written to.")
106
107 vlc_module_begin();
108     set_description( _("File audio output") );
109     set_shortname( _("File") );
110     set_category( CAT_AUDIO );
111     set_subcategory( SUBCAT_AUDIO_AOUT );
112
113     add_string( "audiofile-format", "s16", NULL,
114                 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_TRUE );
115         change_string_list( format_list, 0, 0 );
116     add_integer( "audiofile-channels", 0, NULL,
117                  CHANNELS_TEXT, CHANNELS_LONGTEXT, VLC_TRUE );
118     add_file( "audiofile-file", "audiofile.wav", NULL, FILE_TEXT,
119               FILE_LONGTEXT, VLC_FALSE );
120     add_bool( "audiofile-wav", 1, NULL, WAV_TEXT, WAV_LONGTEXT, VLC_TRUE );
121
122     set_capability( "audio output", 0 );
123     add_shortcut( "file" );
124     add_shortcut( "audiofile" );
125     set_callbacks( Open, Close );
126 vlc_module_end();
127
128 /*****************************************************************************
129  * Open: open a dummy audio device
130  *****************************************************************************/
131 static int Open( vlc_object_t * p_this )
132 {
133     aout_instance_t * p_aout = (aout_instance_t *)p_this;
134     char * psz_name, * psz_format;
135     const char ** ppsz_compare = format_list;
136     vlc_value_t val;
137     int i_channels, i = 0;
138
139     var_Create( p_this, "audiofile-file", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
140     var_Get( p_this, "audiofile-file", &val );
141     psz_name = val.psz_string;
142     if( !psz_name || !*psz_name )
143     {
144         msg_Err( p_aout, "you need to specify an output file name" );
145         if( psz_name ) free( psz_name );
146         return VLC_EGENERIC;
147     }
148
149     /* Allocate structure */
150     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
151     if( p_aout->output.p_sys == NULL )
152     {
153         msg_Err( p_aout, "out of memory" );
154         return VLC_EGENERIC;
155     }
156
157     p_aout->output.p_sys->p_file = utf8_fopen( psz_name, "wb" );
158     free( psz_name );
159     if ( p_aout->output.p_sys->p_file == NULL )
160     {
161         free( p_aout->output.p_sys );
162         return VLC_EGENERIC;
163     }
164
165     p_aout->output.pf_play = Play;
166
167     /* Audio format */
168     var_Create( p_this, "audiofile-format", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
169     var_Get( p_this, "audiofile-format", &val );
170     psz_format = val.psz_string;
171
172     while ( *ppsz_compare != NULL )
173     {
174         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
175         {
176             break;
177         }
178         ppsz_compare++; i++;
179     }
180
181     if ( *ppsz_compare == NULL )
182     {
183         msg_Err( p_aout, "cannot understand the format string (%s)",
184                  psz_format );
185         fclose( p_aout->output.p_sys->p_file );
186         free( p_aout->output.p_sys );
187         return VLC_EGENERIC;
188     }
189
190     p_aout->output.output.i_format = format_int[i];
191     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
192     {
193         p_aout->output.i_nb_samples = A52_FRAME_NB;
194         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
195         p_aout->output.output.i_frame_length = A52_FRAME_NB;
196         aout_VolumeNoneInit( p_aout );
197     }
198     else
199     {
200         p_aout->output.i_nb_samples = FRAME_SIZE;
201         aout_VolumeSoftInit( p_aout );
202     }
203
204     /* Channels number */
205     var_Create( p_this, "audiofile-channels",
206                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
207     var_Get( p_this, "audiofile-channels", &val );
208     i_channels = val.i_int;
209
210     if( i_channels > 0 && i_channels <= CHANNELS_MAX )
211     {
212         p_aout->output.output.i_physical_channels =
213             pi_channels_maps[i_channels];
214     }
215
216     /* WAV header */
217     var_Create( p_this, "audiofile-wav", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
218     var_Get( p_this, "audiofile-wav", &val );
219     p_aout->output.p_sys->b_add_wav_header = val.b_bool;
220
221     if( p_aout->output.p_sys->b_add_wav_header )
222     {
223         /* Write wave header */
224         WAVEHEADER *wh = &p_aout->output.p_sys->waveh;
225
226         memset( wh, 0, sizeof(wh) );
227
228         switch( p_aout->output.output.i_format )
229         {
230         case VLC_FOURCC('f','l','3','2'):
231             wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
232             wh->BitsPerSample = sizeof(float) * 8;
233             break;
234         case VLC_FOURCC('u','8',' ',' '):
235             wh->Format     = WAVE_FORMAT_PCM;
236             wh->BitsPerSample = 8;
237             break;
238         case VLC_FOURCC('s','1','6','l'):
239         default:
240             wh->Format     = WAVE_FORMAT_PCM;
241             wh->BitsPerSample = 16;
242             break;
243         }
244
245         wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
246         wh->Length = 0;                    /* temp, to be filled in as we go */
247         wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
248         wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
249         wh->SubChunkLength = 16;
250
251         wh->Modus = aout_FormatNbChannels( &p_aout->output.output );
252         wh->SampleFreq = p_aout->output.output.i_rate;
253         wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
254         wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
255
256         wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
257         wh->DataLength = 0;                /* temp, to be filled in as we go */
258
259         /* Header -> little endian format */
260         SetWLE( &wh->Format, wh->Format );
261         SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
262         SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
263         SetWLE( &wh->Modus, wh->Modus );
264         SetDWLE( &wh->SampleFreq, wh->SampleFreq );
265         SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
266         SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
267
268         if( fwrite( wh, sizeof(WAVEHEADER), 1,
269                     p_aout->output.p_sys->p_file ) != 1 )
270         {
271             msg_Err( p_aout, "write error (%s)", strerror(errno) );
272         }
273     }
274
275     return 0;
276 }
277
278 /*****************************************************************************
279  * Close: close our file
280  *****************************************************************************/
281 static void Close( vlc_object_t * p_this )
282 {
283     aout_instance_t * p_aout = (aout_instance_t *)p_this;
284
285     msg_Dbg( p_aout, "closing audio file" );
286
287     if( p_aout->output.p_sys->b_add_wav_header )
288     {
289         /* Update Wave Header */
290         p_aout->output.p_sys->waveh.Length =
291             p_aout->output.p_sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
292
293         /* Write Wave Header */
294         if( fseek( p_aout->output.p_sys->p_file, 0, SEEK_SET ) )
295         {
296             msg_Err( p_aout, "seek error (%s)", strerror(errno) );
297         }
298
299         /* Header -> little endian format */
300         SetDWLE( &p_aout->output.p_sys->waveh.Length,
301                  p_aout->output.p_sys->waveh.Length );
302         SetDWLE( &p_aout->output.p_sys->waveh.DataLength,
303                  p_aout->output.p_sys->waveh.DataLength );
304
305         if( fwrite( &p_aout->output.p_sys->waveh, sizeof(WAVEHEADER), 1,
306                     p_aout->output.p_sys->p_file ) != 1 )
307         {
308             msg_Err( p_aout, "write error (%s)", strerror(errno) );
309         }
310     }
311
312     fclose( p_aout->output.p_sys->p_file );
313     free( p_aout->output.p_sys );
314 }
315
316 /*****************************************************************************
317  * Play: pretend to play a sound
318  *****************************************************************************/
319 static void Play( aout_instance_t * p_aout )
320 {
321     aout_buffer_t * p_buffer;
322
323     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
324
325     if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
326                 p_aout->output.p_sys->p_file ) != 1 )
327     {
328         msg_Err( p_aout, "write error (%s)", strerror(errno) );
329     }
330
331     if( p_aout->output.p_sys->b_add_wav_header )
332     {
333         /* Update Wave Header */
334         p_aout->output.p_sys->waveh.DataLength += p_buffer->i_nb_bytes;
335     }
336
337     aout_BufferFree( p_buffer );
338 }