mpeg_adec.c 8.08 KB
Newer Older
1
/*****************************************************************************
Henri Fallon's avatar
 
Henri Fallon committed
2
 * mpeg_adec.c: MPEG audio decoder thread
3
 *****************************************************************************
4
 * Copyright (C) 1999-2001 VideoLAN
5
 * $Id: mpeg_adec.c,v 1.24 2002/06/01 12:32:00 sam Exp $
6
 *
7 8 9
 * Authors: Michel Kaempf <maxx@via.ecp.fr>
 *          Michel Lespinasse <walken@via.ecp.fr>
 *          Samuel Hocevar <sam@via.ecp.fr>
10
 *          Cyril Deguet <asmax@via.ecp.fr>
11 12 13 14 15
 *
 * 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.
Sam Hocevar's avatar
 
Sam Hocevar committed
16
 * 
17 18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
21
 *
22 23 24
 * 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.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

Sam Hocevar's avatar
 
Sam Hocevar committed
27 28 29 30
/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
31
#include <string.h>
Michel Kaempf's avatar
Michel Kaempf committed
32

33 34 35
#include <vlc/vlc.h>
#include <vlc/decoder.h>
#include <vlc/aout.h>
Michel Kaempf's avatar
Michel Kaempf committed
36

Henri Fallon's avatar
 
Henri Fallon committed
37 38
#include "mpeg_adec_generic.h"
#include "mpeg_adec.h"
Michel Kaempf's avatar
Michel Kaempf committed
39

Sam Hocevar's avatar
 
Sam Hocevar committed
40
#define ADEC_FRAME_SIZE (2*1152)
41

Sam Hocevar's avatar
 
Sam Hocevar committed
42
/*****************************************************************************
Henri Fallon's avatar
 
Henri Fallon committed
43
 * Local Prototypes
Sam Hocevar's avatar
 
Sam Hocevar committed
44
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
45
static int   decoder_Probe ( u8 * );
46
static int   decoder_Run   ( decoder_fifo_t * );
Sam Hocevar's avatar
 
Sam Hocevar committed
47 48
static void  EndThread     ( adec_thread_t * );
static void  DecodeThread  ( adec_thread_t * );
Henri Fallon's avatar
 
Henri Fallon committed
49

Sam Hocevar's avatar
 
Sam Hocevar committed
50
/*****************************************************************************
Henri Fallon's avatar
 
Henri Fallon committed
51
 * Capabilities
Sam Hocevar's avatar
 
Sam Hocevar committed
52
 *****************************************************************************/
Henri Fallon's avatar
 
Henri Fallon committed
53
void _M( adec_getfunctions )( function_list_t * p_function_list )
Michel Kaempf's avatar
Michel Kaempf committed
54
{
Sam Hocevar's avatar
 
Sam Hocevar committed
55 56
    p_function_list->functions.dec.pf_probe = decoder_Probe;
    p_function_list->functions.dec.pf_run   = decoder_Run;
Henri Fallon's avatar
 
Henri Fallon committed
57 58 59 60 61 62 63 64 65
}

/*****************************************************************************
 * Build configuration tree.
 *****************************************************************************/
MODULE_CONFIG_START
MODULE_CONFIG_STOP

MODULE_INIT_START
Sam Hocevar's avatar
 
Sam Hocevar committed
66
    SET_DESCRIPTION( _("MPEG I/II layer 1/2 audio decoder") )
67
    ADD_CAPABILITY( DECODER, 50 )
Sam Hocevar's avatar
 
Sam Hocevar committed
68 69
    ADD_REQUIREMENT( FPU )
    ADD_SHORTCUT( "builtin" )
Henri Fallon's avatar
 
Henri Fallon committed
70 71 72 73 74
MODULE_INIT_STOP

MODULE_ACTIVATE_START
    _M( adec_getfunctions )( &p_module->p_functions->dec );
MODULE_ACTIVATE_STOP
Michel Kaempf's avatar
Michel Kaempf committed
75

Henri Fallon's avatar
 
Henri Fallon committed
76 77
MODULE_DEACTIVATE_START
MODULE_DEACTIVATE_STOP
78

Henri Fallon's avatar
 
Henri Fallon committed
79
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
80
 * decoder_Probe: probe the decoder and return score
Henri Fallon's avatar
 
Henri Fallon committed
81
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
82
static int decoder_Probe( u8 *pi_type )
Henri Fallon's avatar
 
Henri Fallon committed
83
{
Sam Hocevar's avatar
 
Sam Hocevar committed
84 85
    return( ( *pi_type == MPEG1_AUDIO_ES
               || *pi_type == MPEG2_AUDIO_ES ) ? 0 : -1 );
Henri Fallon's avatar
 
Henri Fallon committed
86 87 88
}

/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
89
 * decoder_Run: initialize, go inside main loop, detroy
Henri Fallon's avatar
 
Henri Fallon committed
90
 *****************************************************************************/
91
static int decoder_Run ( decoder_fifo_t * p_fifo )
Henri Fallon's avatar
 
Henri Fallon committed
92 93 94
{
    adec_thread_t   * p_adec;
    
Sam Hocevar's avatar
 
Sam Hocevar committed
95 96
    /* Allocate the memory needed to store the thread's structure */
    if ( (p_adec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL ) 
Henri Fallon's avatar
 
Henri Fallon committed
97
    {
98 99
        msg_Err( p_fifo, "out of memory" );
        DecoderError( p_fifo );
Sam Hocevar's avatar
 
Sam Hocevar committed
100
        return 0;
Michel Kaempf's avatar
Michel Kaempf committed
101
    }
Henri Fallon's avatar
 
Henri Fallon committed
102
    
Sam Hocevar's avatar
 
Sam Hocevar committed
103 104 105
    /*
     * Initialize the thread properties
     */
106
    p_adec->p_fifo = p_fifo;
107

Henri Fallon's avatar
 
Henri Fallon committed
108 109
    /* 
     * Initilize the banks
Sam Hocevar's avatar
 
Sam Hocevar committed
110
     */
Henri Fallon's avatar
 
Henri Fallon committed
111 112 113 114 115
    p_adec->bank_0.actual = p_adec->bank_0.v1;
    p_adec->bank_0.pos = 0;
    p_adec->bank_1.actual = p_adec->bank_1.v1;
    p_adec->bank_1.pos = 0;
    
Sam Hocevar's avatar
 
Sam Hocevar committed
116
    /*
Henri Fallon's avatar
 
Henri Fallon committed
117
     * Initialize bit stream 
Sam Hocevar's avatar
 
Sam Hocevar committed
118
     */
119
    InitBitstream( &p_adec->bit_stream, p_adec->p_fifo, NULL, NULL );
Sam Hocevar's avatar
Sam Hocevar committed
120

121 122 123
    /* We do not create the audio output fifo now, but
       it will be created when the first frame is received */
    p_adec->p_aout_fifo = NULL;
124

Henri Fallon's avatar
 
Henri Fallon committed
125
    p_adec->i_sync = 0;
Michel Kaempf's avatar
Michel Kaempf committed
126

Henri Fallon's avatar
 
Henri Fallon committed
127 128
    /* Audio decoder thread's main loop */
    while( (!p_adec->p_fifo->b_die) && (!p_adec->p_fifo->b_error) )
Henri Fallon's avatar
 
Henri Fallon committed
129
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
130
        DecodeThread( p_adec );
Henri Fallon's avatar
 
Henri Fallon committed
131 132 133 134 135
    }
    
    /* If b_error is set, the audio decoder thread enters the error loop */
    if( p_adec->p_fifo->b_error ) 
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
136
        DecoderError( p_adec->p_fifo );
Michel Kaempf's avatar
Michel Kaempf committed
137 138
    }

Henri Fallon's avatar
 
Henri Fallon committed
139
    /* End of the audio decoder thread */
Sam Hocevar's avatar
 
Sam Hocevar committed
140
    EndThread( p_adec );
Henri Fallon's avatar
 
Henri Fallon committed
141 142

    return( 0 );
143 144
}

Henri Fallon's avatar
 
Henri Fallon committed
145
/*
146
 * Following functions are local to this module
Henri Fallon's avatar
 
Henri Fallon committed
147 148
 */

Sam Hocevar's avatar
 
Sam Hocevar committed
149
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
150
 * DecodeThread: decodes a mpeg frame
Sam Hocevar's avatar
 
Sam Hocevar committed
151
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
152
static void DecodeThread( adec_thread_t * p_adec )
153
{
Sam Hocevar's avatar
 
Sam Hocevar committed
154
    s16 *p_buffer;
Henri Fallon's avatar
 
Henri Fallon committed
155
    adec_sync_info_t sync_info;
Henri Fallon's avatar
 
Henri Fallon committed
156

Henri Fallon's avatar
 
Henri Fallon committed
157 158
    if( ! adec_SyncFrame (p_adec, &sync_info) )
    {
159 160 161 162 163 164
        
        /* TODO: check if audio type has changed */
        
        /* Create the output fifo if it doesn't exist yet */
        if( p_adec->p_aout_fifo == NULL )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
165
            int i_channels;
166
            
167
            if( !config_GetInt( p_adec->p_fifo, "mono" ) )
168
            {
169
                msg_Dbg( p_adec->p_fifo, "setting stereo output" );
Sam Hocevar's avatar
 
Sam Hocevar committed
170
                i_channels = 2;
171 172
            }
            else if( sync_info.b_stereo )
173
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
174
                i_channels = 2;
175 176 177
            }
            else
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
178
                i_channels = 1;
179
            }
180 181 182 183
            p_adec->p_aout_fifo =
               aout_CreateFifo( p_adec->p_fifo->p_this,
                                AOUT_FIFO_PCM, i_channels,
                                sync_info.sample_rate, ADEC_FRAME_SIZE, NULL );
184 185
            if( p_adec->p_aout_fifo == NULL)
            {
186
                msg_Err( p_adec->p_fifo, "failed to create aout fifo" );
Sam Hocevar's avatar
 
Sam Hocevar committed
187 188
                p_adec->p_fifo->b_error = 1;
                return;
189 190 191
            }
        }

Henri Fallon's avatar
 
Henri Fallon committed
192
        p_adec->i_sync = 1;
Henri Fallon's avatar
 
Henri Fallon committed
193

Sam Hocevar's avatar
 
Sam Hocevar committed
194 195
        p_buffer = ((s16 *)p_adec->p_aout_fifo->buffer)
                    + (p_adec->p_aout_fifo->i_end_frame * ADEC_FRAME_SIZE);
Henri Fallon's avatar
 
Henri Fallon committed
196

197
        CurrentPTS( &p_adec->bit_stream,
Sam Hocevar's avatar
 
Sam Hocevar committed
198
            &p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame],
199
            NULL );
Sam Hocevar's avatar
 
Sam Hocevar committed
200
        if( !p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame] )
Henri Fallon's avatar
 
Henri Fallon committed
201
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
202
            p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame] =
Henri Fallon's avatar
 
Henri Fallon committed
203 204
                LAST_MDATE;
        }
Henri Fallon's avatar
 
Henri Fallon committed
205

Sam Hocevar's avatar
 
Sam Hocevar committed
206
        if( adec_DecodeFrame (p_adec, p_buffer) )
Sam Hocevar's avatar
Sam Hocevar committed
207
        {
Henri Fallon's avatar
 
Henri Fallon committed
208 209
            /* Ouch, failed decoding... We'll have to resync */
            p_adec->i_sync = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
210
        }
Henri Fallon's avatar
 
Henri Fallon committed
211 212 213
        else
        {
            vlc_mutex_lock (&p_adec->p_aout_fifo->data_lock);
Sam Hocevar's avatar
 
Sam Hocevar committed
214 215
            p_adec->p_aout_fifo->i_end_frame =
                (p_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
Henri Fallon's avatar
 
Henri Fallon committed
216 217 218
            vlc_cond_signal (&p_adec->p_aout_fifo->data_wait);
            vlc_mutex_unlock (&p_adec->p_aout_fifo->data_lock);
        }
Sam Hocevar's avatar
Sam Hocevar committed
219
    }
220
}
Michel Kaempf's avatar
Michel Kaempf committed
221

Sam Hocevar's avatar
 
Sam Hocevar committed
222
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
223
 * EndThread : audio decoder thread destruction
Sam Hocevar's avatar
 
Sam Hocevar committed
224 225 226 227
 *****************************************************************************
 * This function is called when the thread ends after a sucessful
 * initialization.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
228
static void EndThread ( adec_thread_t *p_adec )
Sam Hocevar's avatar
 
Sam Hocevar committed
229 230 231
{
    /* If the audio output fifo was created, we destroy it */
    if ( p_adec->p_aout_fifo != NULL ) 
Sam Hocevar's avatar
Sam Hocevar committed
232
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
233
        aout_DestroyFifo ( p_adec->p_aout_fifo );
Michel Lespinasse's avatar
 
Michel Lespinasse committed
234

Sam Hocevar's avatar
 
Sam Hocevar committed
235 236 237 238
        /* Make sure the output thread leaves the NextFrame() function */
        vlc_mutex_lock (&(p_adec->p_aout_fifo->data_lock));
        vlc_cond_signal (&(p_adec->p_aout_fifo->data_wait));
        vlc_mutex_unlock (&(p_adec->p_aout_fifo->data_lock));
Sam Hocevar's avatar
Sam Hocevar committed
239
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
240
    /* Destroy descriptor */
241
    free( p_adec );
Michel Kaempf's avatar
Michel Kaempf committed
242 243
}