]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
direct3d11: catch texture mapping errors
[vlc] / modules / audio_output / file.c
1 /*****************************************************************************
2  * file.c : audio output which writes the samples to a file
3  *****************************************************************************
4  * Copyright (C) 2002 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <errno.h>
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_aout.h>
39 #include <vlc_codecs.h> /* WAVEHEADER */
40 #include <vlc_fs.h>
41
42 #define A52_FRAME_NB 1536
43
44 /*****************************************************************************
45  * aout_sys_t: audio output method descriptor
46  *****************************************************************************
47  * This structure is part of the audio output thread descriptor.
48  * It describes the direct sound specific properties of an audio device.
49  *****************************************************************************/
50 struct aout_sys_t
51 {
52     FILE     * p_file;
53     bool b_add_wav_header;
54
55     WAVEHEADER waveh;                      /* Wave header of the output file */
56 };
57
58 #define CHANNELS_MAX 6
59 static const int pi_channels_maps[CHANNELS_MAX+1] =
60 {
61     0,
62     AOUT_CHAN_CENTER,
63     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
64     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
65     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
66      | AOUT_CHAN_REARRIGHT,
67     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
68      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
69     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
70      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
71 };
72
73 /*****************************************************************************
74  * Local prototypes.
75  *****************************************************************************/
76 static int     Open        ( vlc_object_t * );
77 static void    Play        ( audio_output_t *, block_t * );
78 static void    Flush       ( audio_output_t *, bool );
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 #define FORMAT_TEXT N_("Output format")
84
85 #define CHANNELS_TEXT N_("Number of output channels")
86 #define CHANNELS_LONGTEXT N_("By default (0), all the channels of the incoming " \
87     "will be saved but you can restrict the number of channels here.")
88
89 #define WAV_TEXT N_("Add WAVE header")
90 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
91                         "header to the file.")
92
93 static const char *const format_list[] = {
94     "u8", "s16",
95 #ifndef WORDS_BIGENDIAN
96     "float32",
97 #endif
98     "spdif",
99 };
100 static const int format_int[] = {
101     VLC_CODEC_U8, VLC_CODEC_S16N,
102 #ifndef WORDS_BIGENDIAN
103     VLC_CODEC_FL32,
104 #endif
105     VLC_CODEC_SPDIFL,
106 };
107
108 #define FILE_TEXT N_("Output file")
109 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
110
111 vlc_module_begin ()
112     set_description( N_("File audio output") )
113     set_shortname( N_("File") )
114     set_category( CAT_AUDIO )
115     set_subcategory( SUBCAT_AUDIO_AOUT )
116
117     add_savefile( "audiofile-file", "audiofile.wav", FILE_TEXT,
118                   FILE_LONGTEXT, false )
119     add_string( "audiofile-format", "s16",
120                 FORMAT_TEXT, FORMAT_TEXT, true )
121         change_string_list( format_list, format_list )
122     add_integer( "audiofile-channels", 0,
123                  CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
124         change_integer_range( 0, 6 )
125     add_bool( "audiofile-wav", true, WAV_TEXT, WAV_LONGTEXT, true )
126
127     set_capability( "audio output", 0 )
128     add_shortcut( "file", "audiofile" )
129     set_callbacks( Open, NULL )
130 vlc_module_end ()
131
132 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
133 {
134     char * psz_name, * psz_format;
135     const char * const * ppsz_compare = format_list;
136     int i_channels, i = 0;
137
138     psz_name = var_InheritString( p_aout, "audiofile-file" );
139     if( !psz_name )
140     {
141         msg_Err( p_aout, "you need to specify an output file name" );
142         free( psz_name );
143         return VLC_EGENERIC;
144     }
145
146     /* Allocate structure */
147     p_aout->sys = malloc( sizeof( aout_sys_t ) );
148     if( p_aout->sys == NULL )
149         return VLC_ENOMEM;
150
151     if( !strcmp( psz_name, "-" ) )
152         p_aout->sys->p_file = stdout;
153     else
154         p_aout->sys->p_file = vlc_fopen( psz_name, "wb" );
155
156     free( psz_name );
157     if ( p_aout->sys->p_file == NULL )
158     {
159         free( p_aout->sys );
160         return VLC_EGENERIC;
161     }
162
163     p_aout->time_get = NULL;
164     p_aout->play = Play;
165     p_aout->pause = NULL;
166     p_aout->flush = Flush;
167
168     /* Audio format */
169     psz_format = var_InheritString( p_aout, "audiofile-format" );
170     if ( !psz_format ) abort(); /* FIXME */
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         if( p_aout->sys->p_file != stdout )
186             fclose( p_aout->sys->p_file );
187         free( p_aout->sys );
188         free( psz_format );
189         return VLC_EGENERIC;
190     }
191     free( psz_format );
192
193     fmt->i_format = format_int[i];
194     if ( AOUT_FMT_SPDIF( fmt ) )
195     {
196         fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
197         fmt->i_frame_length = A52_FRAME_NB;
198     }
199
200     /* Channels number */
201     i_channels = var_InheritInteger( p_aout, "audiofile-channels" );
202     if( i_channels > 0 && i_channels <= CHANNELS_MAX )
203     {
204         fmt->i_physical_channels = pi_channels_maps[i_channels];
205     }
206
207     /* WAV header */
208     p_aout->sys->b_add_wav_header = var_InheritBool( p_aout, "audiofile-wav" );
209     if( p_aout->sys->b_add_wav_header )
210     {
211         /* Write wave header */
212         WAVEHEADER *wh = &p_aout->sys->waveh;
213
214         memset( wh, 0, sizeof(*wh) );
215
216         switch( fmt->i_format )
217         {
218 #ifndef WORDS_BIGENDIAN
219         case VLC_CODEC_FL32:
220             wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
221             wh->BitsPerSample = sizeof(float) * 8;
222             break;
223 #endif
224         case VLC_CODEC_U8:
225             wh->Format     = WAVE_FORMAT_PCM;
226             wh->BitsPerSample = 8;
227             break;
228         default:
229             wh->Format     = WAVE_FORMAT_PCM;
230             wh->BitsPerSample = 16;
231             break;
232         }
233
234         wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
235         wh->Length = 0;                    /* temp, to be filled in as we go */
236         wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
237         wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
238         wh->SubChunkLength = 16;
239
240         wh->Modus = aout_FormatNbChannels( fmt );
241         wh->SampleFreq = fmt->i_rate;
242         wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
243         wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
244
245         wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
246         wh->DataLength = 0;                /* temp, to be filled in as we go */
247
248         /* Header -> little endian format */
249         SetWLE( &wh->Format, wh->Format );
250         SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
251         SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
252         SetWLE( &wh->Modus, wh->Modus );
253         SetDWLE( &wh->SampleFreq, wh->SampleFreq );
254         SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
255         SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
256
257         if( fwrite( wh, sizeof(WAVEHEADER), 1,
258                     p_aout->sys->p_file ) != 1 )
259         {
260             msg_Err( p_aout, "write error: %s", vlc_strerror_c(errno) );
261         }
262     }
263
264     return 0;
265 }
266
267 /*****************************************************************************
268  * Close: close our file
269  *****************************************************************************/
270 static void Stop( audio_output_t *p_aout )
271 {
272     msg_Dbg( p_aout, "closing audio file" );
273
274     if( p_aout->sys->b_add_wav_header )
275     {
276         /* Update Wave Header */
277         p_aout->sys->waveh.Length =
278             p_aout->sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
279
280         /* Write Wave Header */
281         if( fseek( p_aout->sys->p_file, 0, SEEK_SET ) )
282         {
283             msg_Err( p_aout, "seek error: %s", vlc_strerror_c(errno) );
284         }
285
286         /* Header -> little endian format */
287         SetDWLE( &p_aout->sys->waveh.Length,
288                  p_aout->sys->waveh.Length );
289         SetDWLE( &p_aout->sys->waveh.DataLength,
290                  p_aout->sys->waveh.DataLength );
291
292         if( fwrite( &p_aout->sys->waveh, sizeof(WAVEHEADER), 1,
293                     p_aout->sys->p_file ) != 1 )
294         {
295             msg_Err( p_aout, "write error: %s", vlc_strerror_c(errno) );
296         }
297     }
298
299     if( p_aout->sys->p_file != stdout )
300         fclose( p_aout->sys->p_file );
301     free( p_aout->sys );
302 }
303
304 /*****************************************************************************
305  * Play: pretend to play a sound
306  *****************************************************************************/
307 static void Play( audio_output_t * p_aout, block_t *p_buffer )
308 {
309     if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
310                 p_aout->sys->p_file ) != 1 )
311     {
312         msg_Err( p_aout, "write error: %s", vlc_strerror_c(errno) );
313     }
314
315     if( p_aout->sys->b_add_wav_header )
316     {
317         /* Update Wave Header */
318         p_aout->sys->waveh.DataLength += p_buffer->i_buffer;
319     }
320
321     block_Release( p_buffer );
322 }
323
324 static void Flush( audio_output_t *aout, bool wait )
325 {
326     if( fflush( aout->sys->p_file ) )
327         msg_Err( aout, "flush error: %s", vlc_strerror_c(errno) );
328     (void) wait;
329 }
330
331 static int Open(vlc_object_t *obj)
332 {
333     audio_output_t *aout = (audio_output_t *)obj;
334
335     aout->start = Start;
336     aout->stop = Stop;
337     aout->volume_set = NULL;
338     aout->mute_set = NULL;
339     return VLC_SUCCESS;
340 }