]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
aout: move packet FIFO management to plugins that need it
[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
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_aout.h>
36 #include <vlc_codecs.h> /* WAVEHEADER */
37 #include <vlc_fs.h>
38
39 #define A52_FRAME_NB 1536
40
41 /*****************************************************************************
42  * aout_sys_t: audio output method descriptor
43  *****************************************************************************
44  * This structure is part of the audio output thread descriptor.
45  * It describes the direct sound specific properties of an audio device.
46  *****************************************************************************/
47 struct aout_sys_t
48 {
49     FILE     * p_file;
50     bool b_add_wav_header;
51
52     WAVEHEADER waveh;                      /* Wave header of the output file */
53 };
54
55 #define CHANNELS_MAX 6
56 static const int pi_channels_maps[CHANNELS_MAX+1] =
57 {
58     0,
59     AOUT_CHAN_CENTER,
60     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
61     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
62     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
63      | AOUT_CHAN_REARRIGHT,
64     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
65      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
66     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
67      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
68 };
69
70 /*****************************************************************************
71  * Local prototypes.
72  *****************************************************************************/
73 static int     Open        ( vlc_object_t * );
74 static void    Close       ( vlc_object_t * );
75 static void    Play        ( audio_output_t *, block_t * );
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80 #define FORMAT_TEXT N_("Output format")
81 #define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
82     "\"u16_le\", \"s16_le\", \"u16_be\", \"s16_be\", \"fixed32\", " \
83     "\"float32\" or \"spdif\"")
84 #define CHANNELS_TEXT N_("Number of output channels")
85 #define CHANNELS_LONGTEXT N_("By default, all the channels of the incoming " \
86     "will be saved but you can restrict the number of channels here.")
87
88 #define WAV_TEXT N_("Add WAVE header")
89 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
90                         "header to the file.")
91
92 static const char *const format_list[] = { "u8", "s8", "u16", "s16", "u16_le",
93                                      "s16_le", "u16_be", "s16_be", "fixed32",
94                                      "float32", "spdif" };
95 static const int format_int[] = { VLC_CODEC_U8,
96                                   VLC_CODEC_S8,
97                                   VLC_CODEC_U16N, VLC_CODEC_S16N,
98                                   VLC_CODEC_U16L,
99                                   VLC_CODEC_S16L,
100                                   VLC_CODEC_U16B,
101                                   VLC_CODEC_S16B,
102                                   VLC_CODEC_FI32,
103                                   VLC_CODEC_FL32,
104                                   VLC_CODEC_SPDIFL };
105
106 #define FILE_TEXT N_("Output file")
107 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
108
109 vlc_module_begin ()
110     set_description( N_("File audio output") )
111     set_shortname( N_("File") )
112     set_category( CAT_AUDIO )
113     set_subcategory( SUBCAT_AUDIO_AOUT )
114
115     add_string( "audiofile-format", "s16",
116                 FORMAT_TEXT, FORMAT_LONGTEXT, true )
117         change_string_list( format_list, 0, 0 )
118     add_integer( "audiofile-channels", 0,
119                  CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
120     add_savefile( "audiofile-file", "audiofile.wav", FILE_TEXT,
121                   FILE_LONGTEXT, false )
122     add_bool( "audiofile-wav", true, WAV_TEXT, WAV_LONGTEXT, true )
123
124     set_capability( "audio output", 0 )
125     add_shortcut( "file", "audiofile" )
126     set_callbacks( Open, Close )
127 vlc_module_end ()
128
129 /*****************************************************************************
130  * Open: open a dummy audio device
131  *****************************************************************************/
132 static int Open( vlc_object_t * p_this )
133 {
134     audio_output_t * p_aout = (audio_output_t *)p_this;
135     char * psz_name, * psz_format;
136     const char * const * ppsz_compare = format_list;
137     int i_channels, i = 0;
138
139     psz_name = var_CreateGetString( p_this, "audiofile-file" );
140     if( !psz_name || !*psz_name )
141     {
142         msg_Err( p_aout, "you need to specify an output file name" );
143         free( psz_name );
144         return VLC_EGENERIC;
145     }
146
147     /* Allocate structure */
148     p_aout->sys = malloc( sizeof( aout_sys_t ) );
149     if( p_aout->sys == NULL )
150         return VLC_ENOMEM;
151
152     if( !strcmp( psz_name, "-" ) )
153         p_aout->sys->p_file = stdout;
154     else
155         p_aout->sys->p_file = vlc_fopen( psz_name, "wb" );
156
157     free( psz_name );
158     if ( p_aout->sys->p_file == NULL )
159     {
160         free( p_aout->sys );
161         return VLC_EGENERIC;
162     }
163
164     p_aout->pf_play = Play;
165     p_aout->pf_pause = NULL;
166     p_aout->pf_flush = NULL;
167
168     /* Audio format */
169     psz_format = var_CreateGetString( p_this, "audiofile-format" );
170
171     while ( *ppsz_compare != NULL )
172     {
173         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
174         {
175             break;
176         }
177         ppsz_compare++; i++;
178     }
179
180     if ( *ppsz_compare == NULL )
181     {
182         msg_Err( p_aout, "cannot understand the format string (%s)",
183                  psz_format );
184         if( p_aout->sys->p_file != stdout )
185             fclose( p_aout->sys->p_file );
186         free( p_aout->sys );
187         free( psz_format );
188         return VLC_EGENERIC;
189     }
190     free( psz_format );
191
192     p_aout->format.i_format = format_int[i];
193     if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
194     {
195         p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
196         p_aout->format.i_frame_length = A52_FRAME_NB;
197         aout_VolumeNoneInit( p_aout );
198     }
199     else
200         aout_VolumeSoftInit( p_aout );
201
202     /* Channels number */
203     i_channels = var_CreateGetInteger( p_this, "audiofile-channels" );
204
205     if( i_channels > 0 && i_channels <= CHANNELS_MAX )
206     {
207         p_aout->format.i_physical_channels =
208             pi_channels_maps[i_channels];
209     }
210
211     /* WAV header */
212     p_aout->sys->b_add_wav_header = var_CreateGetBool( p_this,
213                                                         "audiofile-wav" );
214
215     if( p_aout->sys->b_add_wav_header )
216     {
217         /* Write wave header */
218         WAVEHEADER *wh = &p_aout->sys->waveh;
219
220         memset( wh, 0, sizeof(*wh) );
221
222         switch( p_aout->format.i_format )
223         {
224         case VLC_CODEC_FL32:
225             wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
226             wh->BitsPerSample = sizeof(float) * 8;
227             break;
228         case VLC_CODEC_U8:
229             wh->Format     = WAVE_FORMAT_PCM;
230             wh->BitsPerSample = 8;
231             break;
232         case VLC_CODEC_S16L:
233         default:
234             wh->Format     = WAVE_FORMAT_PCM;
235             wh->BitsPerSample = 16;
236             break;
237         }
238
239         wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
240         wh->Length = 0;                    /* temp, to be filled in as we go */
241         wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
242         wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
243         wh->SubChunkLength = 16;
244
245         wh->Modus = aout_FormatNbChannels( &p_aout->format );
246         wh->SampleFreq = p_aout->format.i_rate;
247         wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
248         wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
249
250         wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
251         wh->DataLength = 0;                /* temp, to be filled in as we go */
252
253         /* Header -> little endian format */
254         SetWLE( &wh->Format, wh->Format );
255         SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
256         SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
257         SetWLE( &wh->Modus, wh->Modus );
258         SetDWLE( &wh->SampleFreq, wh->SampleFreq );
259         SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
260         SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
261
262         if( fwrite( wh, sizeof(WAVEHEADER), 1,
263                     p_aout->sys->p_file ) != 1 )
264         {
265             msg_Err( p_aout, "write error (%m)" );
266         }
267     }
268
269     return 0;
270 }
271
272 /*****************************************************************************
273  * Close: close our file
274  *****************************************************************************/
275 static void Close( vlc_object_t * p_this )
276 {
277     audio_output_t * p_aout = (audio_output_t *)p_this;
278
279     msg_Dbg( p_aout, "closing audio file" );
280
281     if( p_aout->sys->b_add_wav_header )
282     {
283         /* Update Wave Header */
284         p_aout->sys->waveh.Length =
285             p_aout->sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
286
287         /* Write Wave Header */
288         if( fseek( p_aout->sys->p_file, 0, SEEK_SET ) )
289         {
290             msg_Err( p_aout, "seek error (%m)" );
291         }
292
293         /* Header -> little endian format */
294         SetDWLE( &p_aout->sys->waveh.Length,
295                  p_aout->sys->waveh.Length );
296         SetDWLE( &p_aout->sys->waveh.DataLength,
297                  p_aout->sys->waveh.DataLength );
298
299         if( fwrite( &p_aout->sys->waveh, sizeof(WAVEHEADER), 1,
300                     p_aout->sys->p_file ) != 1 )
301         {
302             msg_Err( p_aout, "write error (%m)" );
303         }
304     }
305
306     if( p_aout->sys->p_file != stdout )
307         fclose( p_aout->sys->p_file );
308     free( p_aout->sys );
309 }
310
311 /*****************************************************************************
312  * Play: pretend to play a sound
313  *****************************************************************************/
314 static void Play( audio_output_t * p_aout, block_t *p_buffer )
315 {
316     if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
317                 p_aout->sys->p_file ) != 1 )
318     {
319         msg_Err( p_aout, "write error (%m)" );
320     }
321
322     if( p_aout->sys->b_add_wav_header )
323     {
324         /* Update Wave Header */
325         p_aout->sys->waveh.DataLength += p_buffer->i_buffer;
326     }
327
328     aout_BufferFree( p_buffer );
329 }