file.c 11.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * file.c : audio output which writes the samples to a file
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2002 VLC authors and VideoLAN
5
 * $Id$
6 7
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
Gildas Bazin's avatar
 
Gildas Bazin committed
8
 *          Gildas Bazin <gbazin@netcourrier.com>
9
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13
 * (at your option) any later version.
Sam Hocevar's avatar
Sam Hocevar committed
14
 *
15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software 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 34 35
#include <stdio.h>
#include <errno.h>

36
#include <vlc_common.h>
37
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
38 39
#include <vlc_aout.h>
#include <vlc_codecs.h> /* WAVEHEADER */
40
#include <vlc_fs.h>
41

42
#define A52_FRAME_NB 1536
43

Gildas Bazin's avatar
 
Gildas Bazin committed
44 45 46 47 48 49 50 51 52
/*****************************************************************************
 * 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;
53
    bool b_add_wav_header;
Gildas Bazin's avatar
 
Gildas Bazin committed
54 55 56 57

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

Gildas Bazin's avatar
 
Gildas Bazin committed
58
#define CHANNELS_MAX 6
59
static const int pi_channels_maps[CHANNELS_MAX+1] =
Gildas Bazin's avatar
 
Gildas Bazin committed
60 61 62 63 64 65 66 67 68 69 70 71 72
{
    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
};

73 74 75 76
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
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 82

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
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 " \
Gildas Bazin's avatar
 
Gildas Bazin committed
87 88
    "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 94 95 96 97 98 99 100 101 102 103 104 105 106
static const char *const format_list[] = {
    "u8", "s16",
#ifndef WORDS_BIGENDIAN
    "float32",
#endif
    "spdif",
};
static const int format_int[] = {
    VLC_CODEC_U8, VLC_CODEC_S16N,
#ifndef WORDS_BIGENDIAN
    VLC_CODEC_FL32,
#endif
    VLC_CODEC_SPDIFL,
};
107

Gildas Bazin's avatar
 
Gildas Bazin committed
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 112 113 114 115
vlc_module_begin ()
    set_description( N_("File audio output") )
    set_shortname( N_("File") )
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_AOUT )
Gildas Bazin's avatar
 
Gildas Bazin committed
116

117 118
    add_savefile( "audiofile-file", "audiofile.wav", FILE_TEXT,
                  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,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
123
                 CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
124
        change_integer_range( 0, 6 )
125
    add_bool( "audiofile-wav", true, WAV_TEXT, WAV_LONGTEXT, true )
Gildas Bazin's avatar
 
Gildas Bazin committed
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
{
Gildas Bazin's avatar
 
Gildas Bazin committed
134
    char * psz_name, * psz_format;
135
    const char * const * ppsz_compare = format_list;
Gildas Bazin's avatar
 
Gildas Bazin committed
136 137
    int i_channels, i = 0;

Thomas Guillem's avatar
Thomas Guillem committed
138 139 140
    if( aout_FormatNbChannels( fmt ) == 0 )
        return VLC_EGENERIC;

141 142
    psz_name = var_InheritString( p_aout, "audiofile-file" );
    if( !psz_name )
Gildas Bazin's avatar
 
Gildas Bazin committed
143 144
    {
        msg_Err( p_aout, "you need to specify an output file name" );
145
        free( psz_name );
Gildas Bazin's avatar
 
Gildas Bazin committed
146 147
        return VLC_EGENERIC;
    }
Christophe Massiot's avatar
Christophe Massiot committed
148

Gildas Bazin's avatar
 
Gildas Bazin committed
149
    /* Allocate structure */
150 151
    p_aout->sys = malloc( sizeof( aout_sys_t ) );
    if( p_aout->sys == NULL )
Rémi Duraffort's avatar
Rémi Duraffort committed
152
        return VLC_ENOMEM;
153

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

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

166
    p_aout->time_get = NULL;
167 168
    p_aout->play = Play;
    p_aout->pause = NULL;
169
    p_aout->flush = Flush;
170

Gildas Bazin's avatar
 
Gildas Bazin committed
171
    /* Audio format */
172
    psz_format = var_InheritString( p_aout, "audiofile-format" );
173 174 175 176 177 178 179
    if ( !psz_format ) /* FIXME */
    {
        if( p_aout->sys->p_file != stdout )
            fclose( p_aout->sys->p_file );
        free( p_aout->sys );
        return VLC_EGENERIC;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
180

181 182 183 184 185 186 187 188 189 190 191
    while ( *ppsz_compare != NULL )
    {
        if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
        {
            break;
        }
        ppsz_compare++; i++;
    }

    if ( *ppsz_compare == NULL )
    {
192
        msg_Err( p_aout, "cannot understand the format string (%s)",
193
                 psz_format );
194 195 196
        if( p_aout->sys->p_file != stdout )
            fclose( p_aout->sys->p_file );
        free( p_aout->sys );
Rémi Duraffort's avatar
Rémi Duraffort committed
197
        free( psz_format );
Gildas Bazin's avatar
 
Gildas Bazin committed
198
        return VLC_EGENERIC;
199
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
200
    free( psz_format );
201

202 203
    fmt->i_format = format_int[i];
    if ( AOUT_FMT_SPDIF( fmt ) )
204
    {
205 206
        fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
        fmt->i_frame_length = A52_FRAME_NB;
207
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
208

Gildas Bazin's avatar
 
Gildas Bazin committed
209
    /* Channels number */
210
    i_channels = var_InheritInteger( p_aout, "audiofile-channels" );
Gildas Bazin's avatar
 
Gildas Bazin committed
211 212
    if( i_channels > 0 && i_channels <= CHANNELS_MAX )
    {
213
        fmt->i_physical_channels = pi_channels_maps[i_channels];
Gildas Bazin's avatar
 
Gildas Bazin committed
214
    }
215
    fmt->channel_type = AUDIO_CHANNEL_TYPE_BITMAP;
Gildas Bazin's avatar
 
Gildas Bazin committed
216 217

    /* WAV header */
218
    p_aout->sys->b_add_wav_header = var_InheritBool( p_aout, "audiofile-wav" );
219
    if( p_aout->sys->b_add_wav_header )
Gildas Bazin's avatar
 
Gildas Bazin committed
220 221
    {
        /* Write wave header */
222
        WAVEHEADER *wh = &p_aout->sys->waveh;
Gildas Bazin's avatar
 
Gildas Bazin committed
223

224
        memset( wh, 0, sizeof(*wh) );
Gildas Bazin's avatar
 
Gildas Bazin committed
225

226
        switch( fmt->i_format )
Gildas Bazin's avatar
 
Gildas Bazin committed
227
        {
228 229
#ifndef WORDS_BIGENDIAN
        case VLC_CODEC_FL32:
Gildas Bazin's avatar
 
Gildas Bazin committed
230 231 232
            wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
            wh->BitsPerSample = sizeof(float) * 8;
            break;
233
#endif
234
        case VLC_CODEC_U8:
Gildas Bazin's avatar
 
Gildas Bazin committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            wh->Format     = WAVE_FORMAT_PCM;
            wh->BitsPerSample = 8;
            break;
        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( fmt );
        wh->SampleFreq = fmt->i_rate;
Gildas Bazin's avatar
 
Gildas Bazin 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 */

Gildas Bazin's avatar
 
Gildas Bazin 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 );

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

274 275 276
    return 0;
}

Christophe Massiot's avatar
Christophe Massiot committed
277 278 279
/*****************************************************************************
 * Close: close our file
 *****************************************************************************/
280
static void Stop( audio_output_t *p_aout )
Christophe Massiot's avatar
Christophe Massiot committed
281
{
Gildas Bazin's avatar
 
Gildas Bazin committed
282 283
    msg_Dbg( p_aout, "closing audio file" );

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

        /* Write Wave Header */
291
        if( fseek( p_aout->sys->p_file, 0, SEEK_SET ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
292
        {
293
            msg_Err( p_aout, "seek error: %s", vlc_strerror_c(errno) );
Gildas Bazin's avatar
 
Gildas Bazin committed
294
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
295 296

        /* Header -> little endian format */
297 298 299 300
        SetDWLE( &p_aout->sys->waveh.Length,
                 p_aout->sys->waveh.Length );
        SetDWLE( &p_aout->sys->waveh.DataLength,
                 p_aout->sys->waveh.DataLength );
Gildas Bazin's avatar
 
Gildas Bazin committed
301

302 303
        if( fwrite( &p_aout->sys->waveh, sizeof(WAVEHEADER), 1,
                    p_aout->sys->p_file ) != 1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
304
        {
305
            msg_Err( p_aout, "write error: %s", vlc_strerror_c(errno) );
Gildas Bazin's avatar
 
Gildas Bazin committed
306 307 308
        }
    }

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

314 315 316
/*****************************************************************************
 * Play: pretend to play a sound
 *****************************************************************************/
317
static void Play( audio_output_t * p_aout, block_t *p_buffer )
318
{
319
    if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
320
                p_aout->sys->p_file ) != 1 )
321
    {
322
        msg_Err( p_aout, "write error: %s", vlc_strerror_c(errno) );
323 324
    }

325
    if( p_aout->sys->b_add_wav_header )
Gildas Bazin's avatar
 
Gildas Bazin committed
326 327
    {
        /* Update Wave Header */
328
        p_aout->sys->waveh.DataLength += p_buffer->i_buffer;
Gildas Bazin's avatar
 
Gildas Bazin committed
329 330
    }

331
    block_Release( p_buffer );
332
}
333

334 335 336
static void Flush( audio_output_t *aout, bool wait )
{
    if( fflush( aout->sys->p_file ) )
337
        msg_Err( aout, "flush error: %s", vlc_strerror_c(errno) );
338 339 340
    (void) wait;
}

341 342 343 344 345 346 347 348 349 350
static int Open(vlc_object_t *obj)
{
    audio_output_t *aout = (audio_output_t *)obj;

    aout->start = Start;
    aout->stop = Stop;
    aout->volume_set = NULL;
    aout->mute_set = NULL;
    return VLC_SUCCESS;
}