flac.c 29 KB
Newer Older
gbazin's avatar
 
gbazin committed
1
/*****************************************************************************
2
 * flac.c: flac decoder/encoder module making use of libflac
gbazin's avatar
 
gbazin committed
3
 *****************************************************************************
4
 * Copyright (C) 1999-2001 the VideoLAN team
5
 * $Id$
gbazin's avatar
 
gbazin committed
6
 *
7
 * Authors: Gildas Bazin <gbazin@videolan.org>
8
 *          Sigmund Augdal Helberg <dnumgis@videolan.org>
gbazin's avatar
 
gbazin committed
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.
14
 *
gbazin's avatar
 
gbazin committed
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.
gbazin's avatar
 
gbazin committed
23
24
25
26
27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28

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_codec.h>
#include <vlc_aout.h>
gbazin's avatar
 
gbazin committed
37

38
39
#include <stream_decoder.h>
#include <stream_encoder.h>
gbazin's avatar
 
gbazin committed
40

zorglub's avatar
zorglub committed
41
42
#include <vlc_block_helper.h>
#include <vlc_bits.h>
gbazin's avatar
 
gbazin committed
43

44
45
46
47
#if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT >= 8
#   define USE_NEW_FLAC_API
#endif

gbazin's avatar
 
gbazin committed
48
49
50
51
52
53
54
55
56
57
/*****************************************************************************
 * decoder_sys_t : FLAC decoder descriptor
 *****************************************************************************/
struct decoder_sys_t
{
    /*
     * Input/Output properties
     */
    block_t *p_block;
    aout_buffer_t *p_aout_buffer;
58
    date_t        end_date;
gbazin's avatar
 
gbazin committed
59
60
61
62
63
64

    /*
     * FLAC properties
     */
    FLAC__StreamDecoder *p_flac;
    FLAC__StreamMetadata_StreamInfo stream_info;
65
    bool b_stream_info;
gbazin's avatar
 
gbazin committed
66
67
};

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
68
static const int pi_channels_maps[9] =
gbazin's avatar
 
gbazin committed
69
70
71
72
73
74
75
76
{
    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
77
78
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
79
80
81
82
83
84
85
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
     | AOUT_CHAN_MIDDLERIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
     | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
     | AOUT_CHAN_LFE
gbazin's avatar
 
gbazin committed
86
87
88
89
90
91
92
93
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  OpenDecoder   ( vlc_object_t * );
static void CloseDecoder  ( vlc_object_t * );

gbazin's avatar
   
gbazin committed
94
95
96
static int OpenEncoder   ( vlc_object_t * );
static void CloseEncoder ( vlc_object_t * );

gbazin's avatar
 
gbazin committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );

static FLAC__StreamDecoderReadStatus
DecoderReadCallback( const FLAC__StreamDecoder *decoder,
                     FLAC__byte buffer[], unsigned *bytes, void *client_data );

static FLAC__StreamDecoderWriteStatus
DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
                      const FLAC__Frame *frame,
                      const FLAC__int32 *const buffer[], void *client_data );

static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
                                     const FLAC__StreamMetadata *metadata,
                                     void *client_data );
static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
                                  FLAC__StreamDecoderErrorStatus status,
                                  void *client_data);

static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
116
                          const int *pi_order, int i_nb_channels, int i_samples );
117
static void Interleave24( int8_t *p_out, const int32_t * const *pp_in,
118
                          const int *pi_order, int i_nb_channels, int i_samples );
gbazin's avatar
 
gbazin committed
119
static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
120
                          const int *pi_order, int i_nb_channels, int i_samples );
gbazin's avatar
 
gbazin committed
121
122
123
124
125
126
127

static void decoder_state_error( decoder_t *p_dec,
                                 FLAC__StreamDecoderState state );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
128
vlc_module_begin ()
gbazin's avatar
 
gbazin committed
129

130
131
132
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACODEC )
    add_shortcut( "flac" )
zorglub's avatar
zorglub committed
133

134
135
136
    set_description( N_("Flac audio decoder") )
    set_capability( "decoder", 100 )
    set_callbacks( OpenDecoder, CloseDecoder )
gbazin's avatar
 
gbazin committed
137

138
    add_submodule ()
139
    add_shortcut( "flac" )
140
141
142
    set_description( N_("Flac audio encoder") )
    set_capability( "encoder", 100 )
    set_callbacks( OpenEncoder, CloseEncoder )
gbazin's avatar
   
gbazin committed
143

144
vlc_module_end ()
gbazin's avatar
 
gbazin committed
145
146
147
148
149
150
151
152
153

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;

154
    if( p_dec->fmt_in.i_codec != VLC_CODEC_FLAC )
gbazin's avatar
 
gbazin committed
155
156
157
158
159
    {
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the decoder's structure */
160
    if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
Christophe Mutricy's avatar
Christophe Mutricy committed
161
        return VLC_ENOMEM;
gbazin's avatar
 
gbazin committed
162
163

    /* Misc init */
164
    p_sys->b_stream_info = false;
165
    p_sys->p_block = NULL;
gbazin's avatar
 
gbazin committed
166
167
168
169
170
171
172
173
174

    /* Take care of flac init */
    if( !(p_sys->p_flac = FLAC__stream_decoder_new()) )
    {
        msg_Err( p_dec, "FLAC__stream_decoder_new() failed" );
        free( p_sys );
        return VLC_EGENERIC;
    }

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifdef USE_NEW_FLAC_API
    if( FLAC__stream_decoder_init_stream( p_sys->p_flac,
                                          DecoderReadCallback,
                                          NULL,
                                          NULL,
                                          NULL,
                                          NULL,
                                          DecoderWriteCallback,
                                          DecoderMetadataCallback,
                                          DecoderErrorCallback,
                                          p_dec )
        != FLAC__STREAM_DECODER_INIT_STATUS_OK )
    {
        msg_Err( p_dec, "FLAC__stream_decoder_init_stream() failed" );
        FLAC__stream_decoder_delete( p_sys->p_flac );
        free( p_sys );
        return VLC_EGENERIC;
    }
#else
gbazin's avatar
 
gbazin committed
194
195
196
197
198
199
200
201
202
203
204
    FLAC__stream_decoder_set_read_callback( p_sys->p_flac,
                                            DecoderReadCallback );
    FLAC__stream_decoder_set_write_callback( p_sys->p_flac,
                                             DecoderWriteCallback );
    FLAC__stream_decoder_set_metadata_callback( p_sys->p_flac,
                                                DecoderMetadataCallback );
    FLAC__stream_decoder_set_error_callback( p_sys->p_flac,
                                             DecoderErrorCallback );
    FLAC__stream_decoder_set_client_data( p_sys->p_flac, p_dec );

    FLAC__stream_decoder_init( p_sys->p_flac );
205
#endif
gbazin's avatar
 
gbazin committed
206
207
208

    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
209
    p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
gbazin's avatar
 
gbazin committed
210
211
212
213

    /* Set callbacks */
    p_dec->pf_decode_audio = DecodeBlock;

214
    /* */
215
    p_dec->b_need_packetized = true;
gbazin's avatar
 
gbazin committed
216

217
    return VLC_SUCCESS;
gbazin's avatar
 
gbazin committed
218
219
}

220
221
222
223
224
225
226
227
228
229
230
/*****************************************************************************
 * CloseDecoder: flac decoder destruction
 *****************************************************************************/
static void CloseDecoder( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t *)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;

    FLAC__stream_decoder_finish( p_sys->p_flac );
    FLAC__stream_decoder_delete( p_sys->p_flac );

231
232
    if( p_sys->p_block )
        block_Release( p_sys->p_block );
233
234
235
    free( p_sys );
}

236
/*****************************************************************************
237
 * ProcessHeader: process Flac header.
238
239
240
241
242
 *****************************************************************************/
static void ProcessHeader( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

243
244
    if( !p_dec->fmt_in.i_extra )
        return;
245
246
247
248
249
250
251
252
253
254

    /* Decode STREAMINFO */
    msg_Dbg( p_dec, "decode STREAMINFO" );
    p_sys->p_block = block_New( p_dec, p_dec->fmt_in.i_extra );
    memcpy( p_sys->p_block->p_buffer, p_dec->fmt_in.p_extra,
            p_dec->fmt_in.i_extra );
    FLAC__stream_decoder_process_until_end_of_metadata( p_sys->p_flac );
    msg_Dbg( p_dec, "STREAMINFO decoded" );
}

gbazin's avatar
 
gbazin committed
255
/****************************************************************************
256
 * DecodeBlock: the whole thing
gbazin's avatar
 
gbazin committed
257
 ****************************************************************************/
258
static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
gbazin's avatar
 
gbazin committed
259
260
261
{
    decoder_sys_t *p_sys = p_dec->p_sys;

262
263
    if( !pp_block || !*pp_block )
        return NULL;
264
265
266
267
268
269
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
    {
        block_Release( *pp_block );
        return NULL;
    }

270
271
    if( !p_sys->b_stream_info )
        ProcessHeader( p_dec );
272

273
274
    p_sys->p_block = *pp_block;
    *pp_block = NULL;
275

276
277
278
279
    if( p_sys->p_block->i_pts > VLC_TS_INVALID &&
        p_sys->p_block->i_pts != date_Get( &p_sys->end_date ) )
        date_Set( &p_sys->end_date, p_sys->p_block->i_pts );

280
    p_sys->p_aout_buffer = 0;
gbazin's avatar
 
gbazin committed
281

282
    if( !FLAC__stream_decoder_process_single( p_sys->p_flac ) )
gbazin's avatar
 
gbazin committed
283
    {
284
285
286
        decoder_state_error( p_dec,
                             FLAC__stream_decoder_get_state( p_sys->p_flac ) );
        FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
gbazin's avatar
 
gbazin committed
287
288
    }

289
290
291
292
    /* If the decoder is in the "aborted" state,
     * FLAC__stream_decoder_process_single() won't return an error. */
    if( FLAC__stream_decoder_get_state(p_dec->p_sys->p_flac)
        == FLAC__STREAM_DECODER_ABORTED )
gbazin's avatar
 
gbazin committed
293
    {
294
        FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
gbazin's avatar
 
gbazin committed
295
296
    }

297
298
299
    block_Release( p_sys->p_block );
    p_sys->p_block = NULL;

gbazin's avatar
 
gbazin committed
300
301
302
303
304
305
306
307
308
309
    return p_sys->p_aout_buffer;
}

/*****************************************************************************
 * DecoderReadCallback: called by libflac when it needs more data
 *****************************************************************************/
static FLAC__StreamDecoderReadStatus
DecoderReadCallback( const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
                     unsigned *bytes, void *client_data )
{
310
    VLC_UNUSED(decoder);
gbazin's avatar
 
gbazin committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
    decoder_t *p_dec = (decoder_t *)client_data;
    decoder_sys_t *p_sys = p_dec->p_sys;

    if( p_sys->p_block && p_sys->p_block->i_buffer )
    {
        *bytes = __MIN(*bytes, (unsigned)p_sys->p_block->i_buffer);
        memcpy( buffer, p_sys->p_block->p_buffer, *bytes );
        p_sys->p_block->i_buffer -= *bytes;
        p_sys->p_block->p_buffer += *bytes;
    }
    else
    {
        *bytes = 0;
        return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
    }

    return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}

/*****************************************************************************
 * DecoderWriteCallback: called by libflac to output decoded samples
 *****************************************************************************/
static FLAC__StreamDecoderWriteStatus
DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
                      const FLAC__Frame *frame,
                      const FLAC__int32 *const buffer[], void *client_data )
{
338
339
340
341
342
343
344
345
346
347
    /* XXX it supposes our internal format is WG4 */
    static const int ppi_reorder[1+8][8] = {
        {-1},
        { 0, },
        { 0, 1 },
        { 0, 1, 2 },
        { 0, 1, 2, 3 },
        { 0, 1, 3, 4, 2 },
        { 0, 1, 4, 5, 2, 3 },

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
348
349
        { 0, 1, 6, 4, 5, 2, 3 },    /* 7.0 Unspecified by flac, but following SMPTE */
        { 0, 1, 6, 7, 4, 5, 2, 3 }, /* 7.1 Unspecified by flac, but following SMPTE */
350
351
    };

352
    VLC_UNUSED(decoder);
gbazin's avatar
 
gbazin committed
353
354
355
    decoder_t *p_dec = (decoder_t *)client_data;
    decoder_sys_t *p_sys = p_dec->p_sys;

356
357
358
    if( p_dec->fmt_out.audio.i_channels <= 0 ||
        p_dec->fmt_out.audio.i_channels > 8 )
        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
359
360
361
    if( date_Get( &p_sys->end_date ) <= VLC_TS_INVALID )
        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;

362
363
    const int * const pi_reorder = ppi_reorder[p_dec->fmt_out.audio.i_channels];

gbazin's avatar
 
gbazin committed
364
    p_sys->p_aout_buffer =
365
        decoder_NewAudioBuffer( p_dec, frame->header.blocksize );
gbazin's avatar
 
gbazin committed
366
367
368
369
370
371
372

    if( p_sys->p_aout_buffer == NULL )
        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;

    switch( frame->header.bits_per_sample )
    {
    case 16:
373
        Interleave16( (int16_t *)p_sys->p_aout_buffer->p_buffer, buffer, pi_reorder,
gbazin's avatar
 
gbazin committed
374
375
                      frame->header.channels, frame->header.blocksize );
        break;
376
    case 24:
377
        Interleave24( (int8_t *)p_sys->p_aout_buffer->p_buffer, buffer, pi_reorder,
378
379
                      frame->header.channels, frame->header.blocksize );
        break;
gbazin's avatar
 
gbazin committed
380
    default:
381
        Interleave32( (int32_t *)p_sys->p_aout_buffer->p_buffer, buffer, pi_reorder,
gbazin's avatar
 
gbazin committed
382
383
384
                      frame->header.channels, frame->header.blocksize );
    }

gbazin's avatar
   
gbazin committed
385
    /* Date management (already done by packetizer) */
386
387
388
389
    p_sys->p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
    p_sys->p_aout_buffer->i_length =
        date_Increment( &p_sys->end_date, frame->header.blocksize ) -
        p_sys->p_aout_buffer->i_pts;
gbazin's avatar
 
gbazin committed
390
391
392
393
394
395
396
397
398
399
400

    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

/*****************************************************************************
 * DecoderMetadataCallback: called by libflac to when it encounters metadata
 *****************************************************************************/
static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
                                     const FLAC__StreamMetadata *metadata,
                                     void *client_data )
{
401
    VLC_UNUSED(decoder);
gbazin's avatar
 
gbazin committed
402
403
404
    decoder_t *p_dec = (decoder_t *)client_data;
    decoder_sys_t *p_sys = p_dec->p_sys;

405
    if( p_dec->pf_decode_audio )
gbazin's avatar
 
gbazin committed
406
    {
407
408
409
        switch( metadata->data.stream_info.bits_per_sample )
        {
        case 8:
410
            p_dec->fmt_out.i_codec = VLC_CODEC_S8;
411
412
            break;
        case 16:
413
            p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
414
            break;
415
        case 24:
416
            p_dec->fmt_out.i_codec = VLC_CODEC_S24N;
417
            break;
418
419
420
        default:
            msg_Dbg( p_dec, "strange bit/sample value: %d",
                     metadata->data.stream_info.bits_per_sample );
421
            p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
422
423
            break;
        }
gbazin's avatar
 
gbazin committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
    }

    /* Setup the format */
    p_dec->fmt_out.audio.i_rate     = metadata->data.stream_info.sample_rate;
    p_dec->fmt_out.audio.i_channels = metadata->data.stream_info.channels;
    p_dec->fmt_out.audio.i_physical_channels =
        p_dec->fmt_out.audio.i_original_channels =
            pi_channels_maps[metadata->data.stream_info.channels];
    p_dec->fmt_out.audio.i_bitspersample =
        metadata->data.stream_info.bits_per_sample;

    msg_Dbg( p_dec, "channels:%d samplerate:%d bitspersamples:%d",
             p_dec->fmt_out.audio.i_channels, p_dec->fmt_out.audio.i_rate,
             p_dec->fmt_out.audio.i_bitspersample );

439
    p_sys->b_stream_info = true;
gbazin's avatar
 
gbazin committed
440
441
    p_sys->stream_info = metadata->data.stream_info;

442
443
    date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
    date_Set( &p_sys->end_date, VLC_TS_INVALID );
gbazin's avatar
 
gbazin committed
444
445
446
447
448
449
450
451
452
}

/*****************************************************************************
 * DecoderErrorCallback: called when the libflac decoder encounters an error
 *****************************************************************************/
static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
                                  FLAC__StreamDecoderErrorStatus status,
                                  void *client_data )
{
453
    VLC_UNUSED(decoder);
gbazin's avatar
 
gbazin committed
454
455
456
457
458
    decoder_t *p_dec = (decoder_t *)client_data;

    switch( status )
    {
    case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
459
        msg_Warn( p_dec, "an error in the stream caused the decoder to "
trax's avatar
trax committed
460
                 "lose synchronization." );
gbazin's avatar
 
gbazin committed
461
462
        break;
    case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
bigben's avatar
bigben committed
463
        msg_Err( p_dec, "the decoder encountered a corrupted frame header." );
gbazin's avatar
 
gbazin committed
464
465
        break;
    case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
bigben's avatar
bigben committed
466
        msg_Err( p_dec, "frame's data did not match the CRC in the "
gbazin's avatar
 
gbazin committed
467
468
469
470
471
                 "footer." );
        break;
    default:
        msg_Err( p_dec, "got decoder error: %d", status );
    }
472
473

    FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
gbazin's avatar
 
gbazin committed
474
475
476
477
478
479
480
    return;
}

/*****************************************************************************
 * Interleave: helper function to interleave channels
 *****************************************************************************/
static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
481
                          const int pi_index[],
gbazin's avatar
 
gbazin committed
482
483
484
485
486
487
488
                          int i_nb_channels, int i_samples )
{
    int i, j;
    for ( j = 0; j < i_samples; j++ )
    {
        for ( i = 0; i < i_nb_channels; i++ )
        {
489
            p_out[j * i_nb_channels + i] = pp_in[pi_index[i]][j];
gbazin's avatar
 
gbazin committed
490
491
492
        }
    }
}
493
494

static void Interleave24( int8_t *p_out, const int32_t * const *pp_in,
495
                          const int pi_index[],
496
497
498
499
500
501
502
                          int i_nb_channels, int i_samples )
{
    int i, j;
    for ( j = 0; j < i_samples; j++ )
    {
        for ( i = 0; i < i_nb_channels; i++ )
        {
503
            const int i_index = pi_index[i];
504
#ifdef WORDS_BIGENDIAN
505
506
507
            p_out[3*(j * i_nb_channels + i)+0] = (pp_in[i_index][j] >> 16) & 0xff;
            p_out[3*(j * i_nb_channels + i)+1] = (pp_in[i_index][j] >> 8 ) & 0xff;
            p_out[3*(j * i_nb_channels + i)+2] = (pp_in[i_index][j] >> 0 ) & 0xff;
508
#else
509
510
511
            p_out[3*(j * i_nb_channels + i)+2] = (pp_in[i_index][j] >> 16) & 0xff;
            p_out[3*(j * i_nb_channels + i)+1] = (pp_in[i_index][j] >> 8 ) & 0xff;
            p_out[3*(j * i_nb_channels + i)+0] = (pp_in[i_index][j] >> 0 ) & 0xff;
512
513
514
515
516
#endif
        }
    }
}

gbazin's avatar
 
gbazin committed
517
static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
518
                          const int pi_index[],
gbazin's avatar
 
gbazin committed
519
520
521
522
523
524
525
                          int i_nb_channels, int i_samples )
{
    int i, j;
    for ( j = 0; j < i_samples; j++ )
    {
        for ( i = 0; i < i_nb_channels; i++ )
        {
526
            p_out[j * i_nb_channels + i] = (int32_t)(pp_in[pi_index[i]][j]);
gbazin's avatar
 
gbazin committed
527
528
529
530
531
532
533
534
535
536
537
538
539
        }
    }
}

/*****************************************************************************
 * decoder_state_error: print meaningful error messages
 *****************************************************************************/
static void decoder_state_error( decoder_t *p_dec,
                                 FLAC__StreamDecoderState state )
{
    switch ( state )
    {
    case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
540
        msg_Dbg( p_dec, "the decoder is ready to search for metadata." );
gbazin's avatar
 
gbazin committed
541
542
        break;
    case FLAC__STREAM_DECODER_READ_METADATA:
543
        msg_Dbg( p_dec, "the decoder is ready to or is in the process of "
gbazin's avatar
 
gbazin committed
544
545
546
                 "reading metadata." );
        break;
    case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
547
        msg_Dbg( p_dec, "the decoder is ready to or is in the process of "
gbazin's avatar
 
gbazin committed
548
549
550
                 "searching for the frame sync code." );
        break;
    case FLAC__STREAM_DECODER_READ_FRAME:
551
        msg_Dbg( p_dec, "the decoder is ready to or is in the process of "
gbazin's avatar
 
gbazin committed
552
553
554
                 "reading a frame." );
        break;
    case FLAC__STREAM_DECODER_END_OF_STREAM:
555
        msg_Dbg( p_dec, "the decoder has reached the end of the stream." );
gbazin's avatar
 
gbazin committed
556
        break;
557
558
559
560
561
562
563
564
#ifdef USE_NEW_FLAC_API
    case FLAC__STREAM_DECODER_OGG_ERROR:
        msg_Err( p_dec, "error occurred in the Ogg layer." );
        break;
    case FLAC__STREAM_DECODER_SEEK_ERROR:
        msg_Err( p_dec, "error occurred while seeking." );
        break;
#endif
gbazin's avatar
 
gbazin committed
565
    case FLAC__STREAM_DECODER_ABORTED:
566
        msg_Warn( p_dec, "the decoder was aborted by the read callback." );
gbazin's avatar
 
gbazin committed
567
        break;
568
#ifndef USE_NEW_FLAC_API
gbazin's avatar
 
gbazin committed
569
    case FLAC__STREAM_DECODER_UNPARSEABLE_STREAM:
570
        msg_Warn( p_dec, "the decoder encountered reserved fields in use "
gbazin's avatar
 
gbazin committed
571
572
                 "in the stream." );
        break;
573
#endif
gbazin's avatar
 
gbazin committed
574
    case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
bigben's avatar
bigben committed
575
        msg_Err( p_dec, "error when allocating memory." );
gbazin's avatar
 
gbazin committed
576
        break;
577
#ifndef USE_NEW_FLAC_API
gbazin's avatar
 
gbazin committed
578
579
580
581
582
583
584
585
586
    case FLAC__STREAM_DECODER_ALREADY_INITIALIZED:
        msg_Err( p_dec, "FLAC__stream_decoder_init() was called when the "
                 "decoder was already initialized, usually because "
                 "FLAC__stream_decoder_finish() was not called." );
        break;
    case FLAC__STREAM_DECODER_INVALID_CALLBACK:
        msg_Err( p_dec, "FLAC__stream_decoder_init() was called without "
                 "all callbacks being set." );
        break;
587
#endif
gbazin's avatar
 
gbazin committed
588
    case FLAC__STREAM_DECODER_UNINITIALIZED:
bigben's avatar
bigben committed
589
        msg_Err( p_dec, "decoder in uninitialized state." );
gbazin's avatar
 
gbazin committed
590
591
        break;
    default:
592
        msg_Warn(p_dec, "unknown error" );
gbazin's avatar
 
gbazin committed
593
594
595
    }
}

gbazin's avatar
   
gbazin committed
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*****************************************************************************
 * encoder_sys_t : flac encoder descriptor
 *****************************************************************************/
struct encoder_sys_t
{
    /*
     * Input properties
     */
    int i_headers;

    int i_samples_delay;
    int i_channels;

    FLAC__int32 *p_buffer;
610
    unsigned int i_buffer;
gbazin's avatar
   
gbazin committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647

    block_t *p_chain;

    /*
     * FLAC properties
     */
    FLAC__StreamEncoder *p_flac;
    FLAC__StreamMetadata_StreamInfo stream_info;

    /*
     * Common properties
     */
    mtime_t i_pts;
};

#define STREAMINFO_SIZE 38

static block_t *Encode( encoder_t *, aout_buffer_t * );

static FLAC__StreamEncoderWriteStatus
EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
                      const FLAC__byte buffer[],
                      unsigned bytes, unsigned samples,
                      unsigned current_frame, void *client_data );

static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
                                     const FLAC__StreamMetadata *metadata,
                                     void *client_data );

/*****************************************************************************
 * OpenEncoder: probe the encoder and return score
 *****************************************************************************/
static int OpenEncoder( vlc_object_t *p_this )
{
    encoder_t *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys;

648
    if( p_enc->fmt_out.i_codec != VLC_CODEC_FLAC &&
649
        !p_enc->b_force )
gbazin's avatar
   
gbazin committed
650
651
652
653
654
655
    {
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
656
        return VLC_ENOMEM;
gbazin's avatar
   
gbazin committed
657
658
    p_enc->p_sys = p_sys;
    p_enc->pf_encode_audio = Encode;
659
    p_enc->fmt_out.i_codec = VLC_CODEC_FLAC;
660

gbazin's avatar
   
gbazin committed
661
662
663
    p_sys->i_headers = 0;
    p_sys->p_buffer = 0;
    p_sys->i_buffer = 0;
gbazin's avatar
   
gbazin committed
664
    p_sys->i_samples_delay = 0;
gbazin's avatar
   
gbazin committed
665
666

    /* Create flac encoder */
667
668
669
670
671
672
    if( !(p_sys->p_flac = FLAC__stream_encoder_new()) )
    {
        msg_Err( p_enc, "FLAC__stream_encoder_new() failed" );
        free( p_sys );
        return VLC_EGENERIC;
    }
gbazin's avatar
   
gbazin committed
673
674
675
676
677
678
679

    FLAC__stream_encoder_set_streamable_subset( p_sys->p_flac, 1 );
    FLAC__stream_encoder_set_channels( p_sys->p_flac,
                                       p_enc->fmt_in.audio.i_channels );
    FLAC__stream_encoder_set_sample_rate( p_sys->p_flac,
                                          p_enc->fmt_in.audio.i_rate );
    FLAC__stream_encoder_set_bits_per_sample( p_sys->p_flac, 16 );
680
    p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
gbazin's avatar
   
gbazin committed
681

682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
    /* Get and store the STREAMINFO metadata block as a p_extra */
    p_sys->p_chain = 0;

#ifdef USE_NEW_FLAC_API
    if( FLAC__stream_encoder_init_stream( p_sys->p_flac,
                                          EncoderWriteCallback,
                                          NULL,
                                          NULL,
                                          EncoderMetadataCallback,
                                          p_enc )
        != FLAC__STREAM_ENCODER_INIT_STATUS_OK )
    {
        msg_Err( p_enc, "FLAC__stream_encoder_init_stream() failed" );
        FLAC__stream_encoder_delete( p_sys->p_flac );
        free( p_sys );
        return VLC_EGENERIC;
    }
#else
gbazin's avatar
   
gbazin committed
700
701
702
703
704
705
706
    FLAC__stream_encoder_set_write_callback( p_sys->p_flac,
        EncoderWriteCallback );
    FLAC__stream_encoder_set_metadata_callback( p_sys->p_flac,
        EncoderMetadataCallback );
    FLAC__stream_encoder_set_client_data( p_sys->p_flac, p_enc );

    FLAC__stream_encoder_init( p_sys->p_flac );
707
#endif
gbazin's avatar
   
gbazin committed
708
709
710
711
712
713
714
715
716
717
718
719
720

    return VLC_SUCCESS;
}

/****************************************************************************
 * Encode: the whole thing
 ****************************************************************************
 * This function spits out ogg packets.
 ****************************************************************************/
static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
{
    encoder_sys_t *p_sys = p_enc->p_sys;
    block_t *p_chain;
721
    unsigned int i;
gbazin's avatar
   
gbazin committed
722

723
    p_sys->i_pts = p_aout_buf->i_pts -
gbazin's avatar
   
gbazin committed
724
725
726
727
728
729
                (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
                (mtime_t)p_enc->fmt_in.audio.i_rate;

    p_sys->i_samples_delay += p_aout_buf->i_nb_samples;

    /* Convert samples to FLAC__int32 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
730
    if( p_sys->i_buffer < p_aout_buf->i_buffer * 2 )
gbazin's avatar
   
gbazin committed
731
732
    {
        p_sys->p_buffer =
733
            xrealloc( p_sys->p_buffer, p_aout_buf->i_buffer * 2 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
734
        p_sys->i_buffer = p_aout_buf->i_buffer * 2;
gbazin's avatar
   
gbazin committed
735
736
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
737
    for( i = 0 ; i < p_aout_buf->i_buffer / 2 ; i++ )
gbazin's avatar
   
gbazin committed
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
    {
        p_sys->p_buffer[i]= ((int16_t *)p_aout_buf->p_buffer)[i];
    }

    FLAC__stream_encoder_process_interleaved( p_sys->p_flac, p_sys->p_buffer,
                                              p_aout_buf->i_nb_samples );

    p_chain = p_sys->p_chain;
    p_sys->p_chain = 0;

    return p_chain;
}

/*****************************************************************************
 * CloseEncoder: encoder destruction
 *****************************************************************************/
static void CloseEncoder( vlc_object_t *p_this )
{
    encoder_t *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys = p_enc->p_sys;

    FLAC__stream_encoder_delete( p_sys->p_flac );

761
    free( p_sys->p_buffer );
gbazin's avatar
   
gbazin committed
762
763
764
765
766
767
768
769
770
771
    free( p_sys );
}

/*****************************************************************************
 * EncoderMetadataCallback: called by libflac to output metadata
 *****************************************************************************/
static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
                                     const FLAC__StreamMetadata *metadata,
                                     void *client_data )
{
772
    VLC_UNUSED(encoder);
gbazin's avatar
   
gbazin committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
    encoder_t *p_enc = (encoder_t *)client_data;

    msg_Err( p_enc, "MetadataCallback: %i", metadata->type );
    return;
}

/*****************************************************************************
 * EncoderWriteCallback: called by libflac to output encoded samples
 *****************************************************************************/
static FLAC__StreamEncoderWriteStatus
EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
                      const FLAC__byte buffer[],
                      unsigned bytes, unsigned samples,
                      unsigned current_frame, void *client_data )
{
788
    VLC_UNUSED(encoder); VLC_UNUSED(current_frame);
gbazin's avatar
   
gbazin committed
789
790
791
792
    encoder_t *p_enc = (encoder_t *)client_data;
    encoder_sys_t *p_sys = p_enc->p_sys;
    block_t *p_block;

gbazin's avatar
   
gbazin committed
793
    if( samples == 0 )
gbazin's avatar
   
gbazin committed
794
795
796
    {
        if( p_sys->i_headers == 1 )
        {
gbazin's avatar
   
gbazin committed
797
            msg_Dbg( p_enc, "Writing STREAMINFO: %i", bytes );
gbazin's avatar
   
gbazin committed
798
799
800

            /* Backup the STREAMINFO metadata block */
            p_enc->fmt_out.i_extra = STREAMINFO_SIZE + 4;
801
            p_enc->fmt_out.p_extra = xmalloc( STREAMINFO_SIZE + 4 );
gbazin's avatar
   
gbazin committed
802
803
            memcpy( p_enc->fmt_out.p_extra, "fLaC", 4 );
            memcpy( ((uint8_t *)p_enc->fmt_out.p_extra) + 4, buffer,
gbazin's avatar
   
gbazin committed
804
                    STREAMINFO_SIZE );
gbazin's avatar
   
gbazin committed
805
806
807
808
809
810
811
812
813
814
815

            /* Fake this as the last metadata block */
            ((uint8_t*)p_enc->fmt_out.p_extra)[4] |= 0x80;
        }
        p_sys->i_headers++;
        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
    }

    p_block = block_New( p_enc, bytes );
    memcpy( p_block->p_buffer, buffer, bytes );

gbazin's avatar
   
gbazin committed
816
817
818
819
820
821
822
823
824
    p_block->i_dts = p_block->i_pts = p_sys->i_pts;

    p_sys->i_samples_delay -= samples;

    p_block->i_length = (mtime_t)1000000 *
        (mtime_t)samples / (mtime_t)p_enc->fmt_in.audio.i_rate;

    /* Update pts */
    p_sys->i_pts += p_block->i_length;
gbazin's avatar
   
gbazin committed
825
826
827
828
829

    block_ChainAppend( &p_sys->p_chain, p_block );

    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
830