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