flac.c 7.58 KB
Newer Older
1
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
2
 * flac.c : FLAC demux module for vlc
3
 *****************************************************************************
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
4
 * Copyright (C) 2001-2003 VideoLAN
Gildas Bazin's avatar
 
Gildas Bazin committed
5
 * $Id: flac.c,v 1.13 2004/03/03 20:39:51 gbazin Exp $
6
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
7
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 9 10 11 12
 *
 * 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.
13
 *
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/input.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
29
#include <vlc_codec.h>
30 31

/*****************************************************************************
32
 * Module descriptor
33
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
34 35
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );
36 37 38 39 40 41 42 43 44 45 46 47 48

vlc_module_begin();
    set_description( _("FLAC demuxer") );
    set_capability( "demux2", 155 );
    set_callbacks( Open, Close );
    add_shortcut( "flac" );
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux  ( demux_t * );
static int Control( demux_t *, int, va_list );
49

Gildas Bazin's avatar
 
Gildas Bazin committed
50 51 52 53 54 55 56 57 58
struct demux_sys_t
{
    vlc_bool_t  b_start;
    es_out_id_t *p_es;

    /* Packetizer */
    decoder_t *p_packetizer;
};

59 60
#define STREAMINFO_SIZE 38
#define FLAC_PACKET_SIZE 16384
61 62

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
63
 * Open: initializes ES structures
64
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
65
static int Open( vlc_object_t * p_this )
66
{
67 68 69 70 71
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
    int          i_peek;
    byte_t      *p_peek;
    es_format_t  fmt;
72 73

    /* Have a peep at the show. */
74
    if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
75 76
    {
        /* Stream shorter than 4 bytes... */
77
        msg_Err( p_demux, "cannot peek()" );
Gildas Bazin's avatar
 
Gildas Bazin committed
78
        return VLC_EGENERIC;
79 80
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
81
    if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' )
82
    {
83
        if( strncmp( p_demux->psz_demux, "flac", 4 ) )
84
        {
85
            msg_Warn( p_demux, "flac module discarded (no startcode)" );
Gildas Bazin's avatar
 
Gildas Bazin committed
86
            return VLC_EGENERIC;
87
        }
88 89 90
        /* User forced */
        msg_Err( p_demux, "this doesn't look like a flac stream, "
                 "continuing anyway" );
91 92
    }

93 94 95
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
96 97 98 99
    es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', 'a', 'c' ) );
    p_sys->b_start = VLC_TRUE;

    /* We need to read and store the STREAMINFO metadata */
100
    i_peek = stream_Peek( p_demux->s, &p_peek, 8 );
Gildas Bazin's avatar
 
Gildas Bazin committed
101
    if( p_peek[4] & 0x7F )
Gildas Bazin's avatar
 
Gildas Bazin committed
102
    {
103
        msg_Err( p_demux, "this isn't a STREAMINFO metadata block" );
Gildas Bazin's avatar
 
Gildas Bazin committed
104 105 106
        return VLC_EGENERIC;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
107
    if( ((p_peek[5]<<16)+(p_peek[6]<<8)+p_peek[7]) != (STREAMINFO_SIZE - 4) )
108
    {
109
        msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" );
Gildas Bazin's avatar
 
Gildas Bazin committed
110
        return VLC_EGENERIC;
111
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
112 113 114 115

    /*
     * Load the FLAC packetizer
     */
116
    p_sys->p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_DECODER );
Gildas Bazin's avatar
 
Gildas Bazin committed
117 118 119 120 121 122 123 124 125
    p_sys->p_packetizer->pf_decode_audio = 0;
    p_sys->p_packetizer->pf_decode_video = 0;
    p_sys->p_packetizer->pf_decode_sub = 0;
    p_sys->p_packetizer->pf_packetize = 0;

    /* Initialization of decoder structure */
    es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
                    VLC_FOURCC( 'f', 'l', 'a', 'c' ) );

126
    /* Store STREAMINFO for the decoder and packetizer */
Gildas Bazin's avatar
 
Gildas Bazin committed
127 128
    p_sys->p_packetizer->fmt_in.i_extra = fmt.i_extra = STREAMINFO_SIZE + 4;
    p_sys->p_packetizer->fmt_in.p_extra = malloc( STREAMINFO_SIZE + 4 );
129
    stream_Read( p_demux->s, p_sys->p_packetizer->fmt_in.p_extra,
Gildas Bazin's avatar
 
Gildas Bazin committed
130
                 STREAMINFO_SIZE + 4 );
Gildas Bazin's avatar
 
Gildas Bazin committed
131

132
    /* Fake this as the last metadata block */
Gildas Bazin's avatar
 
Gildas Bazin committed
133 134 135 136
    ((uint8_t*)p_sys->p_packetizer->fmt_in.p_extra)[4] |= 0x80;
    fmt.p_extra = malloc( STREAMINFO_SIZE + 4 );
    memcpy( fmt.p_extra, p_sys->p_packetizer->fmt_in.p_extra,
            STREAMINFO_SIZE + 4 );
Gildas Bazin's avatar
 
Gildas Bazin committed
137 138

    p_sys->p_packetizer->p_module =
Gildas Bazin's avatar
 
Gildas Bazin committed
139
        module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
140 141 142 143 144 145
    if( !p_sys->p_packetizer->p_module )
    {
        if( p_sys->p_packetizer->fmt_in.p_extra )
            free( p_sys->p_packetizer->fmt_in.p_extra );

        vlc_object_destroy( p_sys->p_packetizer );
146
        msg_Err( p_demux, "cannot find flac packetizer" );
Gildas Bazin's avatar
 
Gildas Bazin committed
147 148
        return VLC_EGENERIC;
    }
149

150
    p_sys->p_es = es_out_Add( p_demux->out, &fmt );
Gildas Bazin's avatar
 
Gildas Bazin committed
151 152 153 154 155 156 157 158 159

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
160 161
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
162 163

    /* Unneed module */
164
    module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
Gildas Bazin's avatar
 
Gildas Bazin committed
165 166 167 168 169 170 171 172

    if( p_sys->p_packetizer->fmt_in.p_extra )
        free( p_sys->p_packetizer->fmt_in.p_extra );

    /* Delete the decoder */
    vlc_object_destroy( p_sys->p_packetizer );

    free( p_sys );
173 174 175 176 177 178 179
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
180
static int Demux( demux_t *p_demux )
181
{
182 183
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t     *p_block_in, *p_block_out;
184

185
    if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) )
186
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
187
        return 0;
188 189
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
190
    if( p_sys->b_start )
191
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
192 193
        p_block_in->i_pts = p_block_in->i_dts = 1;
        p_sys->b_start = VLC_FALSE;
194
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
195
    else
196
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
197
        p_block_in->i_pts = p_block_in->i_dts = 0;
198 199
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
200 201
    while( (p_block_out = p_sys->p_packetizer->pf_packetize(
                p_sys->p_packetizer, &p_block_in )) )
202
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
203 204 205
        while( p_block_out )
        {
            block_t *p_next = p_block_out->p_next;
206

207 208
            /* set PCR */
            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
Gildas Bazin's avatar
 
Gildas Bazin committed
209

210
            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
211

Gildas Bazin's avatar
 
Gildas Bazin committed
212 213 214
            p_block_out = p_next;
        }
    }
215 216 217

    return 1;
}
218 219 220 221 222 223 224 225 226 227 228 229 230

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    /* demux_sys_t *p_sys  = p_demux->p_sys; */
    /* FIXME bitrate */
    return demux2_vaControlHelper( p_demux->s,
                                   0, -1,
                                   8*0, 1, i_query, args );
}