vorbis.c 11.5 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3 4
/*****************************************************************************
 * vorbis.c: vorbis decoder module making use of libvorbis.
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
Gildas Bazin's avatar
 
Gildas Bazin committed
5
 * $Id: vorbis.c,v 1.3 2002/11/02 18:13:22 gbazin Exp $
Gildas Bazin's avatar
 
Gildas Bazin committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *
 * 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.
 * 
 * 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 <stdlib.h>                                      /* malloc(), free() */
#include <string.h>                                    /* memcpy(), memset() */

#include <vlc/vlc.h>
#include <vlc/aout.h>
#include <vlc/decoder.h>
#include <input_ext-dec.h>

#include <vlc/input.h>

#include <ogg/ogg.h>
#include <vorbis/codec.h>

/*****************************************************************************
 * dec_thread_t : vorbis decoder thread descriptor
 *****************************************************************************/
typedef struct dec_thread_t
{
    /*
     * Thread properties
     */
    vlc_thread_t        thread_id;                /* id for thread functions */

    /*
     * Vorbis properties
     */
    vorbis_info      vi; /* struct that stores all the static vorbis bitstream
                            settings */
    vorbis_comment   vc; /* struct that stores all the bitstream user
                          * comments */
    vorbis_dsp_state vd; /* central working state for the packet->PCM
                          * decoder */
    vorbis_block     vb; /* local working space for packet->PCM decode */

    /*
     * Input properties
     */
Gildas Bazin's avatar
 
Gildas Bazin committed
64 65
    decoder_fifo_t         *p_fifo;            /* stores the PES stream data */
    pes_packet_t           *p_pes;            /* current PES we are decoding */
Gildas Bazin's avatar
 
Gildas Bazin committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

    /*
     * Output properties
     */
    aout_instance_t        *p_aout;
    aout_input_t           *p_aout_input;
    audio_sample_format_t   output_format;
    audio_date_t            end_date;

} dec_thread_t;

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

static void DecodePacket ( dec_thread_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
85
static int  GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

static void Interleave   ( float *, const float **, int, int );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( _("Vorbis decoder module") );
    set_capability( "decoder", 100 );
    set_callbacks( OpenDecoder, NULL );
vlc_module_end();

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;

    if( p_fifo->i_fourcc != VLC_FOURCC('v','o','r','b') )
    {
        return VLC_EGENERIC;
    }

    p_fifo->pf_run = RunDecoder;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * RunDecoder: the vorbis decoder
 *****************************************************************************/
static int RunDecoder( decoder_fifo_t * p_fifo )
{
    dec_thread_t *p_dec;
    ogg_packet oggpacket;
    mtime_t i_pts;

    /* Allocate the memory needed to store the thread's structure */
    if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
            == NULL)
    {
        msg_Err( p_fifo, "out of memory" );
        goto error;
    }

    /* Initialize the thread properties */
    p_dec->p_fifo = p_fifo;
Gildas Bazin's avatar
 
Gildas Bazin committed
133
    p_dec->p_pes  = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
134 135 136 137 138

    /* Take care of the initial Vorbis header */
    vorbis_info_init( &p_dec->vi );
    vorbis_comment_init( &p_dec->vc );

Gildas Bazin's avatar
 
Gildas Bazin committed
139
    if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
140 141 142 143 144 145 146 147 148 149 150 151 152
        goto error;

    oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
    if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
    {
        msg_Err( p_dec->p_fifo, "This bitstream does not contain Vorbis "
                 "audio data");
        goto error;
    }

    /* The next two packets in order are the comment and codebook headers.
       We need to watch out that these packets are not missing as a
       missing or corrupted header is fatal. */
Gildas Bazin's avatar
 
Gildas Bazin committed
153
    if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
154 155 156 157 158 159 160 161
        goto error;

    if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
    {
        msg_Err( p_dec->p_fifo, "2nd Vorbis header is corrupted" );
        goto error;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
162
    if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        goto error;

    if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
    {
        msg_Err( p_dec->p_fifo, "3rd Vorbis header is corrupted" );
        goto error;
    }

    /* Initialize the Vorbis packet->PCM decoder */
    vorbis_synthesis_init( &p_dec->vd, &p_dec->vi );
    vorbis_block_init( &p_dec->vd, &p_dec->vb );

    p_dec->output_format.i_format = VLC_FOURCC('f','l','3','2');
    p_dec->output_format.i_channels = p_dec->vi.channels;
    p_dec->output_format.i_rate = p_dec->vi.rate;

    aout_DateInit( &p_dec->end_date, p_dec->vi.rate );
    p_dec->p_aout = NULL;
    p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
                                       &p_dec->p_aout,
                                       &p_dec->output_format );

    if( p_dec->p_aout_input == NULL )
    {
        msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
        goto error;
    }

    /* vorbis decoder thread's main loop */
    while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
    {
        DecodePacket( p_dec );
    }

    /* If b_error is set, the vorbis decoder thread enters the error loop */
    if( p_dec->p_fifo->b_error )
    {
        DecoderError( p_dec->p_fifo );
    }

    /* End of the vorbis decoder thread */
    CloseDecoder( p_dec );

    return 0;

 error:
    if( p_dec )
    {
        if( p_dec->p_fifo )
            p_dec->p_fifo->b_error = 1;
        free( p_dec );
    }

    DecoderError( p_fifo );
    return -1;

}

/*****************************************************************************
 * DecodePacket: decodes a Vorbis packet.
 *****************************************************************************/
static void DecodePacket( dec_thread_t *p_dec )
{
    aout_buffer_t *p_aout_buffer;
    ogg_packet    oggpacket;
    float         **pp_pcm;
    int           i_samples;
    mtime_t       i_pts;

Gildas Bazin's avatar
 
Gildas Bazin committed
232
    if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
233 234 235 236 237
    {
        /* This should mean an eos */
        return;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
238 239 240 241 242 243
    /* Date management */
    if( i_pts > 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
    {
        aout_DateSet( &p_dec->end_date, i_pts );
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    if( vorbis_synthesis( &p_dec->vb, &oggpacket ) == 0 )
        vorbis_synthesis_blockin( &p_dec->vd, &p_dec->vb );

    /* **pp_pcm is a multichannel float vector. In stereo, for
     * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
     * the size of each channel. Convert the float values
     * (-1.<=range<=1.) to whatever PCM format and write it out */

    while( ( i_samples = vorbis_synthesis_pcmout( &p_dec->vd, &pp_pcm ) ) > 0 )
    {

        p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
                                           i_samples );
        if( !p_aout_buffer )
        {
            msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
            p_dec->p_fifo->b_error = 1;
            return;
        }

        /* Interleave the samples */
        Interleave( (float *)p_aout_buffer->p_buffer, (const float **)pp_pcm,
                    p_dec->vi.channels, i_samples );

        /* Tell libvorbis how many samples we actually consumed */
        vorbis_synthesis_read( &p_dec->vd, i_samples );

        /* Date management */
        p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
        p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
                                                      i_samples );

        aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
    }

}

/*****************************************************************************
 * GetOggPacket: get the following vorbis packet from the stream and send back
 *               the result in an ogg packet (for easy decoding by libvorbis).
 *****************************************************************************
 * Returns VLC_EGENERIC in case of eof.
 *****************************************************************************/
static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
Gildas Bazin's avatar
 
Gildas Bazin committed
288
                         mtime_t *p_pts )
Gildas Bazin's avatar
 
Gildas Bazin committed
289
{
Gildas Bazin's avatar
 
Gildas Bazin committed
290 291
    if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
                                        p_dec->p_pes );
Gildas Bazin's avatar
 
Gildas Bazin committed
292

Gildas Bazin's avatar
 
Gildas Bazin committed
293 294
    input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
    if( !p_dec->p_pes ) return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
295

Gildas Bazin's avatar
 
Gildas Bazin committed
296 297 298
    p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
    p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
    p_oggpacket->granulepos = p_dec->p_pes->i_dts;
Gildas Bazin's avatar
 
Gildas Bazin committed
299 300 301 302
    p_oggpacket->b_o_s = 0;
    p_oggpacket->e_o_s = 0;
    p_oggpacket->packetno = 0;

Gildas Bazin's avatar
 
Gildas Bazin committed
303
    *p_pts = p_dec->p_pes->i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
304

Gildas Bazin's avatar
 
Gildas Bazin committed
305
    return VLC_SUCCESS;
Gildas Bazin's avatar
 
Gildas Bazin committed
306 307 308 309 310 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
}

/*****************************************************************************
 * Interleave: helper function to interleave channels
 *****************************************************************************/
static void Interleave( float *p_out, const float **pp_in, int i_channels,
                        int i_samples )
{
    int i, j;

    for ( j = 0; j < i_samples; j++ )
    {
        for ( i = 0; i < i_channels; i++ )
        {
            p_out[j * i_channels + i] = pp_in[i][j];
        }
    }
}

/*****************************************************************************
 * CloseDecoder: vorbis decoder destruction
 *****************************************************************************/
static void CloseDecoder( dec_thread_t * p_dec )
{
    if( p_dec->p_aout_input != NULL )
    {
        aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
    }

    if( p_dec )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
337 338
        if( p_dec->p_pes )
            input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
Gildas Bazin's avatar
 
Gildas Bazin committed
339 340 341 342 343 344 345
        vorbis_block_clear( &p_dec->vb );
        vorbis_dsp_clear( &p_dec->vd );
        vorbis_comment_clear( &p_dec->vc );
        vorbis_info_clear( &p_dec->vi );  /* must be called last */
        free( p_dec );
    }
}