flac.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:40k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * flac.c: flac decoder/packetizer/encoder module making use of libflac
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2001 VideoLAN
  5.  * $Id: flac.c 8546 2004-08-28 11:02:51Z gbazin $
  6.  *
  7.  * Authors: Gildas Bazin <gbazin@videolan.org>
  8.  *          Sigmund Augdal <sigmunau@idi.ntnu.no>
  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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <vlc/vlc.h>
  28. #include <vlc/decoder.h>
  29. #include <FLAC/stream_decoder.h>
  30. #include <FLAC/stream_encoder.h>
  31. #include "vlc_block_helper.h"
  32. #define MAX_FLAC_HEADER_SIZE 16
  33. /*****************************************************************************
  34.  * decoder_sys_t : FLAC decoder descriptor
  35.  *****************************************************************************/
  36. struct decoder_sys_t
  37. {
  38.     /*
  39.      * Input properties
  40.      */
  41.     int i_state;
  42.     block_bytestream_t bytestream;
  43.     /*
  44.      * Input/Output properties
  45.      */
  46.     block_t *p_block;
  47.     aout_buffer_t *p_aout_buffer;
  48.     /*
  49.      * FLAC properties
  50.      */
  51.     FLAC__StreamDecoder *p_flac;
  52.     vlc_bool_t b_stream_info;
  53.     FLAC__StreamMetadata_StreamInfo stream_info;
  54.     /*
  55.      * Common properties
  56.      */
  57.     audio_date_t end_date;
  58.     mtime_t i_pts;
  59.     int i_frame_size, i_frame_length, i_bits_per_sample;
  60.     unsigned int i_rate, i_channels, i_channels_conf;
  61. };
  62. enum {
  63.     STATE_NOSYNC,
  64.     STATE_SYNC,
  65.     STATE_HEADER,
  66.     STATE_NEXT_SYNC,
  67.     STATE_GET_DATA,
  68.     STATE_SEND_DATA
  69. };
  70. static int pi_channels_maps[6] =
  71. {
  72.     0,
  73.     AOUT_CHAN_CENTER,
  74.     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
  75.     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
  76.     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
  77.      | AOUT_CHAN_REARRIGHT,
  78.     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
  79.      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  80. };
  81. /*****************************************************************************
  82.  * Local prototypes
  83.  *****************************************************************************/
  84. static int  OpenDecoder   ( vlc_object_t * );
  85. static int  OpenPacketizer( vlc_object_t * );
  86. static void CloseDecoder  ( vlc_object_t * );
  87. static int OpenEncoder   ( vlc_object_t * );
  88. static void CloseEncoder ( vlc_object_t * );
  89. static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
  90. static block_t *PacketizeBlock( decoder_t *, block_t ** );
  91. static int SyncInfo( decoder_t *, uint8_t *, int *, int *, int *,int * );
  92. static FLAC__StreamDecoderReadStatus
  93. DecoderReadCallback( const FLAC__StreamDecoder *decoder,
  94.                      FLAC__byte buffer[], unsigned *bytes, void *client_data );
  95. static FLAC__StreamDecoderWriteStatus
  96. DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
  97.                       const FLAC__Frame *frame,
  98.                       const FLAC__int32 *const buffer[], void *client_data );
  99. static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
  100.                                      const FLAC__StreamMetadata *metadata,
  101.                                      void *client_data );
  102. static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
  103.                                   FLAC__StreamDecoderErrorStatus status,
  104.                                   void *client_data);
  105. static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
  106.                           int i_nb_channels, int i_samples );
  107. static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
  108.                           int i_nb_channels, int i_samples );
  109. static void decoder_state_error( decoder_t *p_dec,
  110.                                  FLAC__StreamDecoderState state );
  111. static uint64_t read_utf8( const uint8_t *p_buf, int *pi_read );
  112. static uint8_t flac_crc8( const uint8_t *data, unsigned len );
  113. /*****************************************************************************
  114.  * Module descriptor
  115.  *****************************************************************************/
  116. vlc_module_begin();
  117.     set_description( _("Flac audio decoder") );
  118.     set_capability( "decoder", 100 );
  119.     set_callbacks( OpenDecoder, CloseDecoder );
  120.     add_submodule();
  121.     set_description( _("Flac audio packetizer") );
  122.     set_capability( "packetizer", 100 );
  123.     set_callbacks( OpenPacketizer, CloseDecoder );
  124.     add_submodule();
  125.     set_description( _("Flac audio encoder") );
  126.     set_capability( "encoder", 100 );
  127.     set_callbacks( OpenEncoder, CloseEncoder );
  128.     add_shortcut( "flac" );
  129. vlc_module_end();
  130. /*****************************************************************************
  131.  * OpenDecoder: probe the decoder and return score
  132.  *****************************************************************************/
  133. static int OpenDecoder( vlc_object_t *p_this )
  134. {
  135.     decoder_t *p_dec = (decoder_t*)p_this;
  136.     decoder_sys_t *p_sys;
  137.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','l','a','c') )
  138.     {
  139.         return VLC_EGENERIC;
  140.     }
  141.     /* Allocate the memory needed to store the decoder's structure */
  142.     if( ( p_dec->p_sys = p_sys =
  143.           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
  144.     {
  145.         msg_Err( p_dec, "out of memory" );
  146.         return VLC_EGENERIC;
  147.     }
  148.     /* Misc init */
  149.     aout_DateSet( &p_sys->end_date, 0 );
  150.     p_sys->i_state = STATE_NOSYNC;
  151.     p_sys->b_stream_info = VLC_FALSE;
  152.     p_sys->bytestream = block_BytestreamInit( p_dec );
  153.     /* Take care of flac init */
  154.     if( !(p_sys->p_flac = FLAC__stream_decoder_new()) )
  155.     {
  156.         msg_Err( p_dec, "FLAC__stream_decoder_new() failed" );
  157.         free( p_sys );
  158.         return VLC_EGENERIC;
  159.     }
  160.     FLAC__stream_decoder_set_read_callback( p_sys->p_flac,
  161.                                             DecoderReadCallback );
  162.     FLAC__stream_decoder_set_write_callback( p_sys->p_flac,
  163.                                              DecoderWriteCallback );
  164.     FLAC__stream_decoder_set_metadata_callback( p_sys->p_flac,
  165.                                                 DecoderMetadataCallback );
  166.     FLAC__stream_decoder_set_error_callback( p_sys->p_flac,
  167.                                              DecoderErrorCallback );
  168.     FLAC__stream_decoder_set_client_data( p_sys->p_flac, p_dec );
  169.     FLAC__stream_decoder_init( p_sys->p_flac );
  170.     /* Set output properties */
  171.     p_dec->fmt_out.i_cat = AUDIO_ES;
  172.     p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
  173.     /* Set callbacks */
  174.     p_dec->pf_decode_audio = DecodeBlock;
  175.     p_dec->pf_packetize    = PacketizeBlock;
  176.     return VLC_SUCCESS;
  177. }
  178. static int OpenPacketizer( vlc_object_t *p_this )
  179. {
  180.     decoder_t *p_dec = (decoder_t*)p_this;
  181.     int i_ret;
  182.     /* Hmmm, mem leak ?*/
  183.     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
  184.     i_ret = OpenDecoder( p_this );
  185.     /* Set output properties */
  186.     p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','a','c');
  187.     if( i_ret != VLC_SUCCESS ) return i_ret;
  188.     return i_ret;
  189. }
  190. /*****************************************************************************
  191.  * ProcessHeader: processe Flac header.
  192.  *****************************************************************************/
  193. static void ProcessHeader( decoder_t *p_dec )
  194. {
  195.     decoder_sys_t *p_sys = p_dec->p_sys;
  196.     if( !p_dec->fmt_in.i_extra ) return;
  197.     /* Decode STREAMINFO */
  198.     msg_Dbg( p_dec, "decode STREAMINFO" );
  199.     p_sys->p_block = block_New( p_dec, p_dec->fmt_in.i_extra );
  200.     memcpy( p_sys->p_block->p_buffer, p_dec->fmt_in.p_extra,
  201.             p_dec->fmt_in.i_extra );
  202.     FLAC__stream_decoder_process_until_end_of_metadata( p_sys->p_flac );
  203.     msg_Dbg( p_dec, "STREAMINFO decoded" );
  204.     if( !p_sys->b_stream_info ) return;
  205.     if( p_dec->fmt_out.i_codec == VLC_FOURCC('f','l','a','c') )
  206.     {
  207.         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
  208.         p_dec->fmt_out.p_extra =
  209.             realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
  210.         memcpy( p_dec->fmt_out.p_extra,
  211.                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
  212.     }
  213. }
  214. /****************************************************************************
  215.  * PacketizeBlock: the whole thing
  216.  ****************************************************************************
  217.  * This function is called just after the thread is launched.
  218.  ****************************************************************************/
  219. static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
  220. {
  221.     decoder_sys_t *p_sys = p_dec->p_sys;
  222.     uint8_t p_header[MAX_FLAC_HEADER_SIZE];
  223.     block_t *p_sout_block;
  224.     if( !pp_block || !*pp_block ) return NULL;
  225.     if( !p_sys->b_stream_info ) ProcessHeader( p_dec );
  226.     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
  227.     {
  228.         /* We've just started the stream, wait for the first PTS. */
  229.         block_Release( *pp_block );
  230.         return NULL;
  231.     }
  232.     else if( !aout_DateGet( &p_sys->end_date ) )
  233.     {
  234.         /* The first PTS is as good as anything else. */
  235.         aout_DateSet( &p_sys->end_date, (*pp_block)->i_pts );
  236.     }
  237.     if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY )
  238.     {
  239.         p_sys->i_state = STATE_NOSYNC;
  240.     }
  241.     block_BytestreamPush( &p_sys->bytestream, *pp_block );
  242.     while( 1 )
  243.     {
  244.         switch( p_sys->i_state )
  245.         {
  246.         case STATE_NOSYNC:
  247.             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
  248.                    == VLC_SUCCESS )
  249.             {
  250.                 if( p_header[0] == 0xFF && p_header[1] == 0xF8 )
  251.                 {
  252.                     p_sys->i_state = STATE_SYNC;
  253.                     break;
  254.                 }
  255.                 block_SkipByte( &p_sys->bytestream );
  256.             }
  257.             if( p_sys->i_state != STATE_SYNC )
  258.             {
  259.                 block_BytestreamFlush( &p_sys->bytestream );
  260.                 /* Need more data */
  261.                 return NULL;
  262.             }
  263.         case STATE_SYNC:
  264.             /* New frame, set the Presentation Time Stamp */
  265.             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
  266.             if( p_sys->i_pts != 0 &&
  267.                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
  268.             {
  269.                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
  270.             }
  271.             p_sys->i_state = STATE_HEADER;
  272.         case STATE_HEADER:
  273.             /* Get FLAC frame header (MAX_FLAC_HEADER_SIZE bytes) */
  274.             if( block_PeekBytes( &p_sys->bytestream, p_header,
  275.                                  MAX_FLAC_HEADER_SIZE ) != VLC_SUCCESS )
  276.             {
  277.                 /* Need more data */
  278.                 return NULL;
  279.             }
  280.             /* Check if frame is valid and get frame info */
  281.             p_sys->i_frame_length = SyncInfo( p_dec, p_header,
  282.                                               &p_sys->i_channels,
  283.                                               &p_sys->i_channels_conf,
  284.                                               &p_sys->i_rate,
  285.                                               &p_sys->i_bits_per_sample );
  286.             if( !p_sys->i_frame_length )
  287.             {
  288.                 msg_Dbg( p_dec, "emulated sync word" );
  289.                 block_SkipByte( &p_sys->bytestream );
  290.                 p_sys->i_state = STATE_NOSYNC;
  291.                 break;
  292.             }
  293.             if( p_sys->i_rate != p_dec->fmt_out.audio.i_rate )
  294.             {
  295.                 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
  296.                 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
  297.             }
  298.             p_sys->i_state = STATE_NEXT_SYNC;
  299.             p_sys->i_frame_size = 1;
  300.         case STATE_NEXT_SYNC:
  301.             /* TODO: If pp_block == NULL, flush the buffer without checking the
  302.              * next sync word */
  303.             /* Check if next expected frame contains the sync word */
  304.             while( block_PeekOffsetBytes( &p_sys->bytestream,
  305.                                           p_sys->i_frame_size, p_header,
  306.                                           MAX_FLAC_HEADER_SIZE )
  307.                    == VLC_SUCCESS )
  308.             {
  309.                 if( p_header[0] == 0xFF && p_header[1] == 0xF8 )
  310.                 {
  311.                     /* Check if frame is valid and get frame info */
  312.                     int i_frame_length =
  313.                         SyncInfo( p_dec, p_header,
  314.                                   &p_sys->i_channels,
  315.                                   &p_sys->i_channels_conf,
  316.                                   &p_sys->i_rate,
  317.                                   &p_sys->i_bits_per_sample );
  318.                     if( i_frame_length )
  319.                     {
  320.                         p_sys->i_state = STATE_SEND_DATA;
  321.                         break;
  322.                     }
  323.                 }
  324.                 p_sys->i_frame_size++;
  325.             }
  326.             if( p_sys->i_state != STATE_SEND_DATA )
  327.             {
  328.                 /* Need more data */
  329.                 return NULL;
  330.             }
  331.         case STATE_SEND_DATA:
  332.             p_sout_block = block_New( p_dec, p_sys->i_frame_size );
  333.             /* Copy the whole frame into the buffer. When we reach this point
  334.              * we already know we have enough data available. */
  335.             block_GetBytes( &p_sys->bytestream, p_sout_block->p_buffer,
  336.                             p_sys->i_frame_size );
  337.             /* Make sure we don't reuse the same pts twice */
  338.             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
  339.                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
  340.             /* So p_block doesn't get re-added several times */
  341.             *pp_block = block_BytestreamPop( &p_sys->bytestream );
  342.             p_sys->i_state = STATE_NOSYNC;
  343.             /* Date management */
  344.             p_sout_block->i_pts =
  345.                 p_sout_block->i_dts = aout_DateGet( &p_sys->end_date );
  346.             aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
  347.             p_sout_block->i_length =
  348.                 aout_DateGet( &p_sys->end_date ) - p_sout_block->i_pts;
  349.             return p_sout_block;
  350.         }
  351.     }
  352.     return NULL;
  353. }
  354. /****************************************************************************
  355.  * DecodeBlock: the whole thing
  356.  ****************************************************************************/
  357. static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  358. {
  359.     decoder_sys_t *p_sys = p_dec->p_sys;
  360.     if( !pp_block || !*pp_block ) return NULL;
  361.     p_sys->p_aout_buffer = 0;
  362.     if( ( p_sys->p_block = PacketizeBlock( p_dec, pp_block ) ) )
  363.     {
  364.         if( !FLAC__stream_decoder_process_single( p_sys->p_flac ) )
  365.         {
  366.             decoder_state_error( p_dec,
  367.                 FLAC__stream_decoder_get_state( p_sys->p_flac ) );
  368.             FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
  369.         }
  370.         /* If the decoder is in the "aborted" state,
  371.          * FLAC__stream_decoder_process_single() won't return an error. */
  372.         if( FLAC__stream_decoder_get_state(p_dec->p_sys->p_flac)
  373.             == FLAC__STREAM_DECODER_ABORTED )
  374.         {
  375.             FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
  376.         }
  377.         block_Release( p_sys->p_block );
  378.         p_sys->p_block = NULL;
  379.     }
  380.     return p_sys->p_aout_buffer;
  381. }
  382. /*****************************************************************************
  383.  * CloseDecoder: flac decoder destruction
  384.  *****************************************************************************/
  385. static void CloseDecoder( vlc_object_t *p_this )
  386. {
  387.     decoder_t *p_dec = (decoder_t *)p_this;
  388.     decoder_sys_t *p_sys = p_dec->p_sys;
  389.     FLAC__stream_decoder_finish( p_sys->p_flac );
  390.     FLAC__stream_decoder_delete( p_sys->p_flac );
  391.     if( p_sys->p_block ) free( p_sys->p_block );
  392.     free( p_sys );
  393. }
  394.     
  395. /*****************************************************************************
  396.  * DecoderReadCallback: called by libflac when it needs more data
  397.  *****************************************************************************/
  398. static FLAC__StreamDecoderReadStatus
  399. DecoderReadCallback( const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
  400.                      unsigned *bytes, void *client_data )
  401. {
  402.     decoder_t *p_dec = (decoder_t *)client_data;
  403.     decoder_sys_t *p_sys = p_dec->p_sys;
  404.     if( p_sys->p_block && p_sys->p_block->i_buffer )
  405.     {
  406.         *bytes = __MIN(*bytes, (unsigned)p_sys->p_block->i_buffer);
  407.         memcpy( buffer, p_sys->p_block->p_buffer, *bytes );
  408.         p_sys->p_block->i_buffer -= *bytes;
  409.         p_sys->p_block->p_buffer += *bytes;
  410.     }
  411.     else
  412.     {
  413.         *bytes = 0;
  414.         return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  415.     }
  416.     return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  417. }
  418. /*****************************************************************************
  419.  * DecoderWriteCallback: called by libflac to output decoded samples
  420.  *****************************************************************************/
  421. static FLAC__StreamDecoderWriteStatus
  422. DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
  423.                       const FLAC__Frame *frame,
  424.                       const FLAC__int32 *const buffer[], void *client_data )
  425. {
  426.     decoder_t *p_dec = (decoder_t *)client_data;
  427.     decoder_sys_t *p_sys = p_dec->p_sys;
  428.     p_sys->p_aout_buffer =
  429.         p_dec->pf_aout_buffer_new( p_dec, frame->header.blocksize );
  430.     if( p_sys->p_aout_buffer == NULL )
  431.         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  432.     switch( frame->header.bits_per_sample )
  433.     {
  434.     case 16:
  435.         Interleave16( (int16_t *)p_sys->p_aout_buffer->p_buffer, buffer,
  436.                       frame->header.channels, frame->header.blocksize );
  437.         break;
  438.     default:
  439.         Interleave32( (int32_t *)p_sys->p_aout_buffer->p_buffer, buffer,
  440.                       frame->header.channels, frame->header.blocksize );
  441.     }
  442.     /* Date management (already done by packetizer) */
  443.     p_sys->p_aout_buffer->start_date = p_sys->p_block->i_pts;
  444.     p_sys->p_aout_buffer->end_date =
  445.         p_sys->p_block->i_pts + p_sys->p_block->i_length;
  446.     return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  447. }
  448. /*****************************************************************************
  449.  * DecoderMetadataCallback: called by libflac to when it encounters metadata
  450.  *****************************************************************************/
  451. static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
  452.                                      const FLAC__StreamMetadata *metadata,
  453.                                      void *client_data )
  454. {
  455.     decoder_t *p_dec = (decoder_t *)client_data;
  456.     decoder_sys_t *p_sys = p_dec->p_sys;
  457.     switch( metadata->data.stream_info.bits_per_sample )
  458.     {
  459.     case 8:
  460.         p_dec->fmt_out.i_codec = VLC_FOURCC('s','8',' ',' ');
  461.         break;
  462.     case 16:
  463.         p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
  464.         break;
  465.     default:
  466.         msg_Dbg( p_dec, "strange bit/sample value: %d",
  467.                  metadata->data.stream_info.bits_per_sample );
  468.         p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
  469.         break;
  470.     }
  471.     /* Setup the format */
  472.     p_dec->fmt_out.audio.i_rate     = metadata->data.stream_info.sample_rate;
  473.     p_dec->fmt_out.audio.i_channels = metadata->data.stream_info.channels;
  474.     p_dec->fmt_out.audio.i_physical_channels =
  475.         p_dec->fmt_out.audio.i_original_channels =
  476.             pi_channels_maps[metadata->data.stream_info.channels];
  477.     p_dec->fmt_out.audio.i_bitspersample =
  478.         metadata->data.stream_info.bits_per_sample;
  479.     aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
  480.     msg_Dbg( p_dec, "channels:%d samplerate:%d bitspersamples:%d",
  481.              p_dec->fmt_out.audio.i_channels, p_dec->fmt_out.audio.i_rate,
  482.              p_dec->fmt_out.audio.i_bitspersample );
  483.     p_sys->b_stream_info = VLC_TRUE;
  484.     p_sys->stream_info = metadata->data.stream_info;
  485.     return;
  486. }
  487. /*****************************************************************************
  488.  * DecoderErrorCallback: called when the libflac decoder encounters an error
  489.  *****************************************************************************/
  490. static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
  491.                                   FLAC__StreamDecoderErrorStatus status,
  492.                                   void *client_data )
  493. {
  494.     decoder_t *p_dec = (decoder_t *)client_data;
  495.     switch( status )
  496.     {
  497.     case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
  498.         msg_Err( p_dec, "an error in the stream caused the decoder to "
  499.                  "lose synchronization." );
  500.         break;
  501.     case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
  502.         msg_Err( p_dec, "the decoder encountered a corrupted frame header." );
  503.         break;
  504.     case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
  505.         msg_Err( p_dec, "frame's data did not match the CRC in the "
  506.                  "footer." );
  507.         break;
  508.     default:
  509.         msg_Err( p_dec, "got decoder error: %d", status );
  510.     }
  511.     FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
  512.     return;
  513. }
  514. /*****************************************************************************
  515.  * Interleave: helper function to interleave channels
  516.  *****************************************************************************/
  517. static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
  518.                           int i_nb_channels, int i_samples )
  519. {
  520.     int i, j;
  521.     for ( j = 0; j < i_samples; j++ )
  522.     {
  523.         for ( i = 0; i < i_nb_channels; i++ )
  524.         {
  525.             p_out[j * i_nb_channels + i] = pp_in[i][j];
  526.         }
  527.     }
  528. }
  529. static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
  530.                           int i_nb_channels, int i_samples )
  531. {
  532.     int i, j;
  533.     for ( j = 0; j < i_samples; j++ )
  534.     {
  535.         for ( i = 0; i < i_nb_channels; i++ )
  536.         {
  537.             p_out[j * i_nb_channels + i] = (int32_t)(pp_in[i][j]);
  538.         }
  539.     }
  540. }
  541. /*****************************************************************************
  542.  * decoder_state_error: print meaningful error messages
  543.  *****************************************************************************/
  544. static void decoder_state_error( decoder_t *p_dec,
  545.                                  FLAC__StreamDecoderState state )
  546. {
  547.     switch ( state )
  548.     {
  549.     case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  550.         msg_Err( p_dec, "the decoder is ready to search for metadata." );
  551.         break;
  552.     case FLAC__STREAM_DECODER_READ_METADATA:
  553.         msg_Err( p_dec, "the decoder is ready to or is in the process of "
  554.                  "reading metadata." );
  555.         break;
  556.     case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  557.         msg_Err( p_dec, "the decoder is ready to or is in the process of "
  558.                  "searching for the frame sync code." );
  559.         break;
  560.     case FLAC__STREAM_DECODER_READ_FRAME:
  561.         msg_Err( p_dec, "the decoder is ready to or is in the process of "
  562.                  "reading a frame." );
  563.         break;
  564.     case FLAC__STREAM_DECODER_END_OF_STREAM:
  565.         msg_Err( p_dec, "the decoder has reached the end of the stream." );
  566.         break;
  567.     case FLAC__STREAM_DECODER_ABORTED:
  568.         msg_Err( p_dec, "the decoder was aborted by the read callback." );
  569.         break;
  570.     case FLAC__STREAM_DECODER_UNPARSEABLE_STREAM:
  571.         msg_Err( p_dec, "the decoder encountered reserved fields in use "
  572.                  "in the stream." );
  573.         break;
  574.     case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
  575.         msg_Err( p_dec, "error when allocating memory." );
  576.         break;
  577.     case FLAC__STREAM_DECODER_ALREADY_INITIALIZED:
  578.         msg_Err( p_dec, "FLAC__stream_decoder_init() was called when the "
  579.                  "decoder was already initialized, usually because "
  580.                  "FLAC__stream_decoder_finish() was not called." );
  581.         break;
  582.     case FLAC__STREAM_DECODER_INVALID_CALLBACK:
  583.         msg_Err( p_dec, "FLAC__stream_decoder_init() was called without "
  584.                  "all callbacks being set." );
  585.         break;
  586.     case FLAC__STREAM_DECODER_UNINITIALIZED:
  587.         msg_Err( p_dec, "decoder in uninitialized state." );
  588.         break;
  589.     default:
  590.         msg_Err(p_dec, "unknown error" );
  591.     }
  592. }
  593. /*****************************************************************************
  594.  * SyncInfo: parse FLAC sync info
  595.  *****************************************************************************/
  596. static int SyncInfo( decoder_t *p_dec, uint8_t *p_buf,
  597.                      int * pi_channels, int * pi_channels_conf,
  598.                      int * pi_sample_rate, int * pi_bits_per_sample )
  599. {
  600.     decoder_sys_t *p_sys = p_dec->p_sys;
  601.     int i_header, i_temp, i_read;
  602.     int i_blocksize = 0, i_blocksize_hint = 0, i_sample_rate_hint = 0;
  603.     uint64_t i_sample_number = 0;
  604.     vlc_bool_t b_variable_blocksize = ( p_sys->b_stream_info &&
  605.         p_sys->stream_info.min_blocksize != p_sys->stream_info.max_blocksize );
  606.     vlc_bool_t b_fixed_blocksize = ( p_sys->b_stream_info &&
  607.         p_sys->stream_info.min_blocksize == p_sys->stream_info.max_blocksize );
  608.     /* Check syncword */
  609.     if( p_buf[0] != 0xFF || p_buf[1] != 0xF8 ) return 0;
  610.     /* Check there is no emulated sync code in the rest of the header */
  611.     if( p_buf[2] == 0xff || p_buf[3] == 0xFF ) return 0;
  612.     /* Find blocksize (framelength) */
  613.     switch( i_temp = p_buf[2] >> 4 )
  614.     {
  615.     case 0:
  616.         if( b_fixed_blocksize )
  617.             i_blocksize = p_sys->stream_info.min_blocksize;
  618.         else return 0; /* We can't do anything with this */
  619.         break;
  620.     case 1:
  621.         i_blocksize = 192;
  622.         break;
  623.     case 2:
  624.     case 3:
  625.     case 4:
  626.     case 5:
  627.         i_blocksize = 576 << (i_temp - 2);
  628.         break;
  629.     case 6:
  630.     case 7:
  631.         i_blocksize_hint = i_temp;
  632.         break;
  633.     case 8:
  634.     case 9:
  635.     case 10:
  636.     case 11:
  637.     case 12:
  638.     case 13:
  639.     case 14:
  640.     case 15:
  641.         i_blocksize = 256 << (i_temp - 8);
  642.         break;
  643.     }
  644.     /* Find samplerate */
  645.     switch( i_temp = p_buf[2] & 0x0f )
  646.     {
  647.     case 0:
  648.         if( p_sys->b_stream_info )
  649.             *pi_sample_rate = p_sys->stream_info.sample_rate;
  650.         else return 0; /* We can't do anything with this */
  651.         break;
  652.     case 1:
  653.     case 2:
  654.     case 3:
  655.         return 0;
  656.         break;
  657.     case 4:
  658.         *pi_sample_rate = 8000;
  659.         break;
  660.     case 5:
  661.         *pi_sample_rate = 16000;
  662.         break;
  663.     case 6:
  664.         *pi_sample_rate = 22050;
  665.         break;
  666.     case 7:
  667.         *pi_sample_rate = 24000;
  668.         break;
  669.     case 8:
  670.         *pi_sample_rate = 32000;
  671.         break;
  672.     case 9:
  673.         *pi_sample_rate = 44100;
  674.         break;
  675.     case 10:
  676.         *pi_sample_rate = 48000;
  677.         break;
  678.     case 11:
  679.         *pi_sample_rate = 96000;
  680.         break;
  681.     case 12:
  682.     case 13:
  683.     case 14:
  684.         i_sample_rate_hint = i_temp;
  685.         break;
  686.     case 15:
  687.         return 0;
  688.     }
  689.     /* Find channels */
  690.     i_temp = (unsigned)(p_buf[3] >> 4);
  691.     if( i_temp & 8 )
  692.     {
  693.         int i_channel_assignment; /* ??? */
  694.         *pi_channels = 2;
  695.         switch( i_temp & 7 )
  696.         {
  697.         case 0:
  698.             i_channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
  699.             break;
  700.         case 1:
  701.             i_channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
  702.             break;
  703.         case 2:
  704.             i_channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
  705.             break;
  706.         default:
  707.             return 0;
  708.             break;
  709.         }
  710.     }
  711.     else
  712.     {
  713.         *pi_channels = i_temp + 1;
  714.         *pi_channels_conf = pi_channels_maps[ *pi_channels ];
  715.     }
  716.     /* Find bits per sample */
  717.     switch( i_temp = (unsigned)(p_buf[3] & 0x0e) >> 1 )
  718.     {
  719.     case 0:
  720.         if( p_sys->b_stream_info )
  721.             *pi_bits_per_sample = p_sys->stream_info.bits_per_sample;
  722.         else
  723.             return 0;
  724.         break;
  725.     case 1:
  726.         *pi_bits_per_sample = 8;
  727.         break;
  728.     case 2:
  729.         *pi_bits_per_sample = 12;
  730.         break;
  731.     case 4:
  732.         *pi_bits_per_sample = 16;
  733.         break;
  734.     case 5:
  735.         *pi_bits_per_sample = 20;
  736.         break;
  737.     case 6:
  738.         *pi_bits_per_sample = 24;
  739.         break;
  740.     case 3:
  741.     case 7:
  742.         return 0;
  743.         break;
  744.     }
  745.     /* Zero padding bit */
  746.     if( p_buf[3] & 0x01 ) return 0;
  747.     /* End of fixed size header */
  748.     i_header = 4;
  749.     /* Find Sample/Frame number */
  750.     if( i_blocksize_hint && b_variable_blocksize )
  751.     {
  752.         i_sample_number = read_utf8( &p_buf[i_header++], &i_read );
  753.         if( i_sample_number == I64C(0xffffffffffffffff) ) return 0;
  754.     }
  755.     else
  756.     {
  757.         i_sample_number = read_utf8( &p_buf[i_header++], &i_read );
  758.         if( i_sample_number == I64C(0xffffffffffffffff) ) return 0;
  759.         if( p_sys->b_stream_info )
  760.             i_sample_number *= p_sys->stream_info.min_blocksize;
  761.     }
  762.     i_header += i_read;
  763.     /* Read blocksize */
  764.     if( i_blocksize_hint )
  765.     {
  766.         int i_val1 = p_buf[i_header++];
  767.         if( i_blocksize_hint == 7 )
  768.         {
  769.             int i_val2 = p_buf[i_header++];
  770.             i_val1 = (i_val1 << 8) | i_val2;
  771.         }
  772.         i_blocksize = i_val1 + 1;
  773.     }
  774.     /* Read sample rate */
  775.     if( i_sample_rate_hint )
  776.     {
  777.         int i_val1 = p_buf[i_header++];
  778.         if( i_sample_rate_hint != 12 )
  779.         {
  780.             int i_val2 = p_buf[i_header++];
  781.             i_val1 = (i_val1 << 8) | i_val2;
  782.         }
  783.         if( i_sample_rate_hint == 12 ) *pi_sample_rate = i_val1 * 1000;
  784.         else if( i_sample_rate_hint == 13 ) *pi_sample_rate = i_val1;
  785.         else *pi_sample_rate = i_val1 * 10;
  786.     }
  787.     /* Check the CRC-8 byte */
  788.     if( flac_crc8( p_buf, i_header ) != p_buf[i_header] )
  789.     {
  790.         return 0;
  791.     }
  792.     return i_blocksize;
  793. }
  794. /* Will return 0xffffffffffffffff for an invalid utf-8 sequence */
  795. static uint64_t read_utf8( const uint8_t *p_buf, int *pi_read )
  796. {
  797.     uint64_t i_result = 0;
  798.     unsigned i, j;
  799.     if( !(p_buf[0] & 0x80) ) /* 0xxxxxxx */
  800.     {
  801.         i_result = p_buf[0];
  802.         i = 0;
  803.     }
  804.     else if( p_buf[0] & 0xC0 && !(p_buf[0] & 0x20) ) /* 110xxxxx */
  805.     {
  806.         i_result = p_buf[0] & 0x1F;
  807.         i = 1;
  808.     }
  809.     else if( p_buf[0] & 0xE0 && !(p_buf[0] & 0x10) ) /* 1110xxxx */
  810.     {
  811.         i_result = p_buf[0] & 0x0F;
  812.         i = 2;
  813.     }
  814.     else if( p_buf[0] & 0xF0 && !(p_buf[0] & 0x08) ) /* 11110xxx */
  815.     {
  816.         i_result = p_buf[0] & 0x07;
  817.         i = 3;
  818.     }
  819.     else if( p_buf[0] & 0xF8 && !(p_buf[0] & 0x04) ) /* 111110xx */
  820.     {
  821.         i_result = p_buf[0] & 0x03;
  822.         i = 4;
  823.     }
  824.     else if( p_buf[0] & 0xFC && !(p_buf[0] & 0x02) ) /* 1111110x */
  825.     {
  826.         i_result = p_buf[0] & 0x01;
  827.         i = 5;
  828.     }
  829.     else if( p_buf[0] & 0xFE && !(p_buf[0] & 0x01) ) /* 11111110 */
  830.     {
  831.         i_result = 0;
  832.         i = 6;
  833.     }
  834.     else {
  835.         return I64C(0xffffffffffffffff);
  836.     }
  837.     for( j = 1; j <= i; j++ )
  838.     {
  839.         if( !(p_buf[j] & 0x80) || (p_buf[j] & 0x40) ) /* 10xxxxxx */
  840.         {
  841.             return I64C(0xffffffffffffffff);
  842.         }
  843.         i_result <<= 6;
  844.         i_result |= (p_buf[j] & 0x3F);
  845.     }
  846.     *pi_read = i;
  847.     return i_result;
  848. }
  849. /* CRC-8, poly = x^8 + x^2 + x^1 + x^0, init = 0 */
  850. static uint8_t const flac_crc8_table[256] = {
  851.         0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
  852.         0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
  853.         0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
  854.         0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
  855.         0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5,
  856.         0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
  857.         0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85,
  858.         0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
  859.         0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
  860.         0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
  861.         0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2,
  862.         0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
  863.         0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32,
  864.         0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
  865.         0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
  866.         0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
  867.         0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C,
  868.         0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
  869.         0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC,
  870.         0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
  871.         0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
  872.         0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
  873.         0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C,
  874.         0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
  875.         0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B,
  876.         0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
  877.         0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
  878.         0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
  879.         0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
  880.         0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
  881.         0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
  882.         0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
  883. };
  884. static uint8_t flac_crc8( const uint8_t *data, unsigned len )
  885. {
  886.     uint8_t crc = 0;
  887.     while(len--)
  888.         crc = flac_crc8_table[crc ^ *data++];
  889.     return crc;
  890. }
  891. /*****************************************************************************
  892.  * encoder_sys_t : flac encoder descriptor
  893.  *****************************************************************************/
  894. struct encoder_sys_t
  895. {
  896.     /*
  897.      * Input properties
  898.      */
  899.     int i_headers;
  900.     int i_samples_delay;
  901.     int i_channels;
  902.     FLAC__int32 *p_buffer;
  903.     int         i_buffer;
  904.     block_t *p_chain;
  905.     /*
  906.      * FLAC properties
  907.      */
  908.     FLAC__StreamEncoder *p_flac;
  909.     FLAC__StreamMetadata_StreamInfo stream_info;
  910.     /*
  911.      * Common properties
  912.      */
  913.     mtime_t i_pts;
  914. };
  915. #define STREAMINFO_SIZE 38
  916. static block_t *Encode( encoder_t *, aout_buffer_t * );
  917. static FLAC__StreamEncoderWriteStatus
  918. EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
  919.                       const FLAC__byte buffer[],
  920.                       unsigned bytes, unsigned samples,
  921.                       unsigned current_frame, void *client_data );
  922. static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
  923.                                      const FLAC__StreamMetadata *metadata,
  924.                                      void *client_data );
  925. /*****************************************************************************
  926.  * OpenEncoder: probe the encoder and return score
  927.  *****************************************************************************/
  928. static int OpenEncoder( vlc_object_t *p_this )
  929. {
  930.     encoder_t *p_enc = (encoder_t *)p_this;
  931.     encoder_sys_t *p_sys;
  932.     if( p_enc->fmt_out.i_codec != VLC_FOURCC('f','l','a','c') &&
  933.         !p_enc->b_force )
  934.     {
  935.         return VLC_EGENERIC;
  936.     }
  937.     /* Allocate the memory needed to store the decoder's structure */
  938.     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
  939.     {
  940.         msg_Err( p_enc, "out of memory" );
  941.         return VLC_EGENERIC;
  942.     }
  943.     p_enc->p_sys = p_sys;
  944.     p_enc->pf_encode_audio = Encode;
  945.     p_enc->fmt_out.i_codec = VLC_FOURCC('f','l','a','c');
  946.     p_sys->i_headers = 0;
  947.     p_sys->p_buffer = 0;
  948.     p_sys->i_buffer = 0;
  949.     p_sys->i_samples_delay = 0;
  950.     /* Create flac encoder */
  951.     p_sys->p_flac = FLAC__stream_encoder_new();
  952.     FLAC__stream_encoder_set_streamable_subset( p_sys->p_flac, 1 );
  953.     FLAC__stream_encoder_set_channels( p_sys->p_flac,
  954.                                        p_enc->fmt_in.audio.i_channels );
  955.     FLAC__stream_encoder_set_sample_rate( p_sys->p_flac,
  956.                                           p_enc->fmt_in.audio.i_rate );
  957.     FLAC__stream_encoder_set_bits_per_sample( p_sys->p_flac, 16 );
  958.     p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
  959.     FLAC__stream_encoder_set_write_callback( p_sys->p_flac,
  960.         EncoderWriteCallback );
  961.     FLAC__stream_encoder_set_metadata_callback( p_sys->p_flac,
  962.         EncoderMetadataCallback );
  963.     FLAC__stream_encoder_set_client_data( p_sys->p_flac, p_enc );
  964.     /* Get and store the STREAMINFO metadata block as a p_extra */
  965.     p_sys->p_chain = 0;
  966.     FLAC__stream_encoder_init( p_sys->p_flac );
  967.     return VLC_SUCCESS;
  968. }
  969. /****************************************************************************
  970.  * Encode: the whole thing
  971.  ****************************************************************************
  972.  * This function spits out ogg packets.
  973.  ****************************************************************************/
  974. static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
  975. {
  976.     encoder_sys_t *p_sys = p_enc->p_sys;
  977.     block_t *p_chain;
  978.     int i;
  979.     p_sys->i_pts = p_aout_buf->start_date -
  980.                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
  981.                 (mtime_t)p_enc->fmt_in.audio.i_rate;
  982.     p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
  983.     /* Convert samples to FLAC__int32 */
  984.     if( p_sys->i_buffer < p_aout_buf->i_nb_bytes * 2 )
  985.     {
  986.         p_sys->p_buffer =
  987.             realloc( p_sys->p_buffer, p_aout_buf->i_nb_bytes * 2 );
  988.         p_sys->i_buffer = p_aout_buf->i_nb_bytes * 2;
  989.     }
  990.     for( i = 0 ; i < p_aout_buf->i_nb_bytes / 2 ; i++ )
  991.     {
  992.         p_sys->p_buffer[i]= ((int16_t *)p_aout_buf->p_buffer)[i];
  993.     }
  994.     FLAC__stream_encoder_process_interleaved( p_sys->p_flac, p_sys->p_buffer,
  995.                                               p_aout_buf->i_nb_samples );
  996.     p_chain = p_sys->p_chain;
  997.     p_sys->p_chain = 0;
  998.     return p_chain;
  999. }
  1000. /*****************************************************************************
  1001.  * CloseEncoder: encoder destruction
  1002.  *****************************************************************************/
  1003. static void CloseEncoder( vlc_object_t *p_this )
  1004. {
  1005.     encoder_t *p_enc = (encoder_t *)p_this;
  1006.     encoder_sys_t *p_sys = p_enc->p_sys;
  1007.     FLAC__stream_encoder_delete( p_sys->p_flac );
  1008.     if( p_sys->p_buffer ) free( p_sys->p_buffer );
  1009.     free( p_sys );
  1010. }
  1011. /*****************************************************************************
  1012.  * EncoderMetadataCallback: called by libflac to output metadata
  1013.  *****************************************************************************/
  1014. static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
  1015.                                      const FLAC__StreamMetadata *metadata,
  1016.                                      void *client_data )
  1017. {
  1018.     encoder_t *p_enc = (encoder_t *)client_data;
  1019.     msg_Err( p_enc, "MetadataCallback: %i", metadata->type );
  1020.     return;
  1021. }
  1022. /*****************************************************************************
  1023.  * EncoderWriteCallback: called by libflac to output encoded samples
  1024.  *****************************************************************************/
  1025. static FLAC__StreamEncoderWriteStatus
  1026. EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
  1027.                       const FLAC__byte buffer[],
  1028.                       unsigned bytes, unsigned samples,
  1029.                       unsigned current_frame, void *client_data )
  1030. {
  1031.     encoder_t *p_enc = (encoder_t *)client_data;
  1032.     encoder_sys_t *p_sys = p_enc->p_sys;
  1033.     block_t *p_block;
  1034.     if( samples == 0 )
  1035.     {
  1036.         if( p_sys->i_headers == 1 )
  1037.         {
  1038.             msg_Dbg( p_enc, "Writing STREAMINFO: %i", bytes );
  1039.             /* Backup the STREAMINFO metadata block */
  1040.             p_enc->fmt_out.i_extra = STREAMINFO_SIZE + 4;
  1041.             p_enc->fmt_out.p_extra = malloc( STREAMINFO_SIZE + 4 );
  1042.             memcpy( p_enc->fmt_out.p_extra, "fLaC", 4 );
  1043.             memcpy( ((uint8_t *)p_enc->fmt_out.p_extra) + 4, buffer,
  1044.                     STREAMINFO_SIZE );
  1045.             /* Fake this as the last metadata block */
  1046.             ((uint8_t*)p_enc->fmt_out.p_extra)[4] |= 0x80;
  1047.         }
  1048.         p_sys->i_headers++;
  1049.         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  1050.     }
  1051.     p_block = block_New( p_enc, bytes );
  1052.     memcpy( p_block->p_buffer, buffer, bytes );
  1053.     p_block->i_dts = p_block->i_pts = p_sys->i_pts;
  1054.     p_sys->i_samples_delay -= samples;
  1055.     p_block->i_length = (mtime_t)1000000 *
  1056.         (mtime_t)samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
  1057.     /* Update pts */
  1058.     p_sys->i_pts += p_block->i_length;
  1059.     block_ChainAppend( &p_sys->p_chain, p_block );
  1060.     return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  1061. }