file.c 11.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * file.c : audio output which writes the samples to a file
 *****************************************************************************
4
 * Copyright (C) 2002 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
gbazin's avatar
 
gbazin committed
8
 *          Gildas Bazin <gbazin@netcourrier.com>
9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
Sam Hocevar's avatar
Sam Hocevar committed
14
 *
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
35 36
#include <vlc_aout.h>
#include <vlc_codecs.h> /* WAVEHEADER */
37
#include <vlc_fs.h>
38

39
#define FRAME_SIZE 2048
40
#define A52_FRAME_NB 1536
41

gbazin's avatar
 
gbazin committed
42 43 44 45 46 47 48 49 50
/*****************************************************************************
 * aout_sys_t: audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the direct sound specific properties of an audio device.
 *****************************************************************************/
struct aout_sys_t
{
    FILE     * p_file;
51
    bool b_add_wav_header;
gbazin's avatar
 
gbazin committed
52 53 54 55

    WAVEHEADER waveh;                      /* Wave header of the output file */
};

gbazin's avatar
 
gbazin committed
56
#define CHANNELS_MAX 6
57
static const int pi_channels_maps[CHANNELS_MAX+1] =
gbazin's avatar
 
gbazin committed
58 59 60 61 62 63 64 65 66 67 68 69 70
{
    0,
    AOUT_CHAN_CENTER,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
    AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
     | AOUT_CHAN_REARRIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
};

71 72 73 74 75
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int     Open        ( vlc_object_t * );
static void    Close       ( vlc_object_t * );
76
static void    Play        ( audio_output_t * );
77 78 79 80

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
81
#define FORMAT_TEXT N_("Output format")
Christophe Massiot's avatar
Christophe Massiot committed
82
#define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
gbazin's avatar
 
gbazin committed
83 84
    "\"u16_le\", \"s16_le\", \"u16_be\", \"s16_be\", \"fixed32\", " \
    "\"float32\" or \"spdif\"")
85
#define CHANNELS_TEXT N_("Number of output channels")
gbazin's avatar
 
gbazin committed
86 87 88
#define CHANNELS_LONGTEXT N_("By default, all the channels of the incoming " \
    "will be saved but you can restrict the number of channels here.")

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[] = { "u8", "s8", "u16", "s16", "u16_le",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
94 95
                                     "s16_le", "u16_be", "s16_be", "fixed32",
                                     "float32", "spdif" };
96 97
static const int format_int[] = { VLC_CODEC_U8,
                                  VLC_CODEC_S8,
98
                                  VLC_CODEC_U16N, VLC_CODEC_S16N,
99 100 101 102 103 104 105
                                  VLC_CODEC_U16L,
                                  VLC_CODEC_S16L,
                                  VLC_CODEC_U16B,
                                  VLC_CODEC_S16B,
                                  VLC_CODEC_FI32,
                                  VLC_CODEC_FL32,
                                  VLC_CODEC_SPDIFL };
106

gbazin's avatar
 
gbazin committed
107
#define FILE_TEXT N_("Output file")
108
#define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
109

110 111 112 113 114
vlc_module_begin ()
    set_description( N_("File audio output") )
    set_shortname( N_("File") )
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_AOUT )
gbazin's avatar
 
gbazin committed
115

116
    add_string( "audiofile-format", "s16",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
117 118
                FORMAT_TEXT, FORMAT_LONGTEXT, true )
        change_string_list( format_list, 0, 0 )
119
    add_integer( "audiofile-channels", 0,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
120
                 CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
121
    add_savefile( "audiofile-file", "audiofile.wav", FILE_TEXT,
122
                  FILE_LONGTEXT, false )
123
    add_bool( "audiofile-wav", true, WAV_TEXT, WAV_LONGTEXT, true )
gbazin's avatar
 
gbazin committed
124

125
    set_capability( "audio output", 0 )
126
    add_shortcut( "file", "audiofile" )
127 128
    set_callbacks( Open, Close )
vlc_module_end ()
129 130 131 132 133 134

/*****************************************************************************
 * Open: open a dummy audio device
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
135
    audio_output_t * p_aout = (audio_output_t *)p_this;
gbazin's avatar
 
gbazin committed
136
    char * psz_name, * psz_format;
137
    const char * const * ppsz_compare = format_list;
gbazin's avatar
 
gbazin committed
138 139
    int i_channels, i = 0;

140
    psz_name = var_CreateGetString( p_this, "audiofile-file" );
gbazin's avatar
 
gbazin committed
141 142 143
    if( !psz_name || !*psz_name )
    {
        msg_Err( p_aout, "you need to specify an output file name" );
ivoire's avatar
ivoire committed
144
        free( psz_name );
gbazin's avatar
 
gbazin committed
145 146
        return VLC_EGENERIC;
    }
Christophe Massiot's avatar
Christophe Massiot committed
147

gbazin's avatar
 
gbazin committed
148
    /* Allocate structure */
149 150
    p_aout->sys = malloc( sizeof( aout_sys_t ) );
    if( p_aout->sys == NULL )
ivoire's avatar
ivoire committed
151
        return VLC_ENOMEM;
152

153
    if( !strcmp( psz_name, "-" ) )
154
        p_aout->sys->p_file = stdout;
155
    else
156
        p_aout->sys->p_file = vlc_fopen( psz_name, "wb" );
157

158
    free( psz_name );
159
    if ( p_aout->sys->p_file == NULL )
gbazin's avatar
 
gbazin committed
160
    {
161
        free( p_aout->sys );
gbazin's avatar
 
gbazin committed
162
        return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
163
    }
164

165 166
    p_aout->pf_play = Play;
    p_aout->pf_pause = NULL;
167
    p_aout->pf_flush = NULL;
168

gbazin's avatar
 
gbazin committed
169
    /* Audio format */
170
    psz_format = var_CreateGetString( p_this, "audiofile-format" );
gbazin's avatar
 
gbazin committed
171

172 173 174 175 176 177 178 179 180 181 182
    while ( *ppsz_compare != NULL )
    {
        if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
        {
            break;
        }
        ppsz_compare++; i++;
    }

    if ( *ppsz_compare == NULL )
    {
183
        msg_Err( p_aout, "cannot understand the format string (%s)",
184
                 psz_format );
185 186 187
        if( p_aout->sys->p_file != stdout )
            fclose( p_aout->sys->p_file );
        free( p_aout->sys );
ivoire's avatar
ivoire committed
188
        free( psz_format );
gbazin's avatar
 
gbazin committed
189
        return VLC_EGENERIC;
190
    }
ivoire's avatar
ivoire committed
191
    free( psz_format );
192

193 194
    p_aout->format.i_format = format_int[i];
    if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
195
    {
196 197 198
        p_aout->i_nb_samples = A52_FRAME_NB;
        p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
        p_aout->format.i_frame_length = A52_FRAME_NB;
199
        aout_VolumeNoneInit( p_aout );
200 201 202
    }
    else
    {
203
        p_aout->i_nb_samples = FRAME_SIZE;
204
        aout_VolumeSoftInit( p_aout );
205
    }
gbazin's avatar
 
gbazin committed
206

gbazin's avatar
 
gbazin committed
207
    /* Channels number */
208
    i_channels = var_CreateGetInteger( p_this, "audiofile-channels" );
gbazin's avatar
 
gbazin committed
209 210 211

    if( i_channels > 0 && i_channels <= CHANNELS_MAX )
    {
212
        p_aout->format.i_physical_channels =
gbazin's avatar
 
gbazin committed
213 214 215 216
            pi_channels_maps[i_channels];
    }

    /* WAV header */
217
    p_aout->sys->b_add_wav_header = var_CreateGetBool( p_this,
218
                                                        "audiofile-wav" );
gbazin's avatar
 
gbazin committed
219

220
    if( p_aout->sys->b_add_wav_header )
gbazin's avatar
 
gbazin committed
221 222
    {
        /* Write wave header */
223
        WAVEHEADER *wh = &p_aout->sys->waveh;
gbazin's avatar
 
gbazin committed
224

225
        memset( wh, 0, sizeof(*wh) );
gbazin's avatar
 
gbazin committed
226

227
        switch( p_aout->format.i_format )
gbazin's avatar
 
gbazin committed
228
        {
229
        case VLC_CODEC_FL32:
gbazin's avatar
 
gbazin committed
230 231 232
            wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
            wh->BitsPerSample = sizeof(float) * 8;
            break;
233
        case VLC_CODEC_U8:
gbazin's avatar
 
gbazin committed
234 235 236
            wh->Format     = WAVE_FORMAT_PCM;
            wh->BitsPerSample = 8;
            break;
237
        case VLC_CODEC_S16L:
gbazin's avatar
 
gbazin committed
238 239 240 241 242 243 244 245 246 247 248 249
        default:
            wh->Format     = WAVE_FORMAT_PCM;
            wh->BitsPerSample = 16;
            break;
        }

        wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
        wh->Length = 0;                    /* temp, to be filled in as we go */
        wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
        wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
        wh->SubChunkLength = 16;

250 251
        wh->Modus = aout_FormatNbChannels( &p_aout->format );
        wh->SampleFreq = p_aout->format.i_rate;
gbazin's avatar
 
gbazin committed
252 253 254 255 256 257
        wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
        wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;

        wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
        wh->DataLength = 0;                /* temp, to be filled in as we go */

gbazin's avatar
 
gbazin committed
258 259 260 261 262 263 264 265 266
        /* Header -> little endian format */
        SetWLE( &wh->Format, wh->Format );
        SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
        SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
        SetWLE( &wh->Modus, wh->Modus );
        SetDWLE( &wh->SampleFreq, wh->SampleFreq );
        SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
        SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );

gbazin's avatar
 
gbazin committed
267
        if( fwrite( wh, sizeof(WAVEHEADER), 1,
268
                    p_aout->sys->p_file ) != 1 )
gbazin's avatar
 
gbazin committed
269
        {
270
            msg_Err( p_aout, "write error (%m)" );
gbazin's avatar
 
gbazin committed
271 272 273
        }
    }

274 275 276
    return 0;
}

Christophe Massiot's avatar
Christophe Massiot committed
277 278 279 280 281
/*****************************************************************************
 * Close: close our file
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
282
    audio_output_t * p_aout = (audio_output_t *)p_this;
Christophe Massiot's avatar
Christophe Massiot committed
283

gbazin's avatar
 
gbazin committed
284 285
    msg_Dbg( p_aout, "closing audio file" );

286
    if( p_aout->sys->b_add_wav_header )
gbazin's avatar
 
gbazin committed
287 288
    {
        /* Update Wave Header */
289 290
        p_aout->sys->waveh.Length =
            p_aout->sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
gbazin's avatar
 
gbazin committed
291 292

        /* Write Wave Header */
293
        if( fseek( p_aout->sys->p_file, 0, SEEK_SET ) )
gbazin's avatar
 
gbazin committed
294
        {
295
            msg_Err( p_aout, "seek error (%m)" );
gbazin's avatar
 
gbazin committed
296
        }
gbazin's avatar
 
gbazin committed
297 298

        /* Header -> little endian format */
299 300 301 302
        SetDWLE( &p_aout->sys->waveh.Length,
                 p_aout->sys->waveh.Length );
        SetDWLE( &p_aout->sys->waveh.DataLength,
                 p_aout->sys->waveh.DataLength );
gbazin's avatar
 
gbazin committed
303

304 305
        if( fwrite( &p_aout->sys->waveh, sizeof(WAVEHEADER), 1,
                    p_aout->sys->p_file ) != 1 )
gbazin's avatar
 
gbazin committed
306
        {
307
            msg_Err( p_aout, "write error (%m)" );
gbazin's avatar
 
gbazin committed
308 309 310
        }
    }

311 312 313
    if( p_aout->sys->p_file != stdout )
        fclose( p_aout->sys->p_file );
    free( p_aout->sys );
Christophe Massiot's avatar
Christophe Massiot committed
314 315
}

316 317 318
/*****************************************************************************
 * Play: pretend to play a sound
 *****************************************************************************/
319
static void Play( audio_output_t * p_aout )
320
{
321 322
    aout_buffer_t * p_buffer;

323
    p_buffer = aout_FifoPop( &p_aout->fifo );
324

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
325
    if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
326
                p_aout->sys->p_file ) != 1 )
327
    {
328
        msg_Err( p_aout, "write error (%m)" );
329 330
    }

331
    if( p_aout->sys->b_add_wav_header )
gbazin's avatar
 
gbazin committed
332 333
    {
        /* Update Wave Header */
334
        p_aout->sys->waveh.DataLength += p_buffer->i_buffer;
gbazin's avatar
 
gbazin committed
335 336
    }

337 338
    aout_BufferFree( p_buffer );
}