mpga.c 12.2 KB
Newer Older
1 2 3
/*****************************************************************************
 * mpga.c : MPEG-I/II Audio input module for vlc
 *****************************************************************************
4
 * Copyright (C) 2001-2004 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Gildas Bazin <gbazin@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30 31
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/input.h>
32
#include "vlc_codec.h"
33 34
#include "vlc_meta.h"

35
#define MPGA_PACKET_SIZE 1024
36

37 38 39
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
40 41
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
42 43

vlc_module_begin();
Clément Stenac's avatar
Clément Stenac committed
44 45
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
Clément Stenac's avatar
Clément Stenac committed
46
    set_description( _("MPEG audio / MP3 demuxer" ) );
Laurent Aimar's avatar
Laurent Aimar committed
47
    set_capability( "demux2", 100 );
48 49 50 51 52 53 54 55
    set_callbacks( Open, Close );
    add_shortcut( "mpga" );
    add_shortcut( "mp3" );
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
56 57
static int Demux  ( demux_t * );
static int Control( demux_t *, int, va_list );
58 59 60

struct demux_sys_t
{
61 62
    es_out_id_t *p_es;
    vlc_meta_t  *meta;
63

64 65
    vlc_bool_t  b_start;
    decoder_t   *p_packetizer;
66

67 68 69
    mtime_t     i_pts;
    mtime_t     i_time_offset;
    int         i_bitrate_avg;  /* extracted from Xing header */
70

71 72
    vlc_bool_t b_initial_sync_failed;

73 74 75 76
    int i_xing_frames;
    int i_xing_bytes;
    int i_xing_bitrate_avg;
    int i_xing_frame_samples;
77
    block_t *p_block_in, *p_block_out;
78 79 80 81
};

static int HeaderCheck( uint32_t h )
{
82 83
    if( ((( h >> 21 )&0x07FF) != 0x07FF )   /* header sync */
        || (((h >> 17)&0x03) == 0 )         /* valid layer ?*/
84
        || (((h >> 12)&0x0F) == 0x0F )
85 86 87
        || (((h >> 12)&0x0F) == 0x00 )      /* valid bitrate ? */
        || (((h >> 10) & 0x03) == 0x03 )    /* valide sampling freq ? */
        || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
88
    {
89
        return VLC_FALSE;
90
    }
91
    return VLC_TRUE;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

#define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
#define MPGA_LAYER( h )     ( 3 - (((h)>>17)&0x03) )
#define MPGA_MODE(h)        (((h)>> 6)&0x03)

static int mpga_frame_samples( uint32_t h )
{
    switch( MPGA_LAYER(h) )
    {
        case 0:
            return 384;
        case 1:
            return 1152;
        case 2:
            return MPGA_VERSION(h) ? 576 : 1152;
        default:
            return 0;
    }
}

113

114 115 116 117 118
/*****************************************************************************
 * Open: initializes demux structures
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
Laurent Aimar's avatar
Laurent Aimar committed
119 120
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
121
    vlc_bool_t   b_forced = VLC_FALSE;
122

Laurent Aimar's avatar
Laurent Aimar committed
123 124 125
    uint32_t     header;
    uint8_t     *p_peek;
    module_t    *p_id3;
126
    block_t     *p_block_in, *p_block_out;
127

Laurent Aimar's avatar
Laurent Aimar committed
128
    if( p_demux->psz_path )
129
    {
Laurent Aimar's avatar
Laurent Aimar committed
130 131
        int  i_len = strlen( p_demux->psz_path );
        if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
132
        {
133
            b_forced = VLC_TRUE;
134 135 136
        }
    }

137
    if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
138 139 140 141 142 143

    if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
    {
        vlc_bool_t b_ok = VLC_FALSE;
        int i_peek;

144
        if( !p_demux->b_force && !b_forced ) return VLC_EGENERIC;
145

Laurent Aimar's avatar
Laurent Aimar committed
146
        i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
147 148 149 150 151 152 153
        while( i_peek > 4 )
        {
            if( HeaderCheck( header = GetDWBE( p_peek ) ) )
            {
                b_ok = VLC_TRUE;
                break;
            }
154 155
            p_peek += 1;
            i_peek -= 1;
156
        }
157
        if( !b_ok && !p_demux->b_force ) return VLC_EGENERIC;
158 159
    }

160
    STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
161 162 163
    memset( p_sys, 0, sizeof( demux_sys_t ) );
    p_sys->p_es = 0;
    p_sys->b_start = VLC_TRUE;
164
    p_sys->meta = 0;
165

166 167 168 169
    /* Load the mpeg audio packetizer */
    INIT_APACKETIZER( p_sys->p_packetizer, 'm', 'p', 'g', 'a' );
    es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
    LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mpga" );
170

171
    /* Xing header */
172 173
    if( HeaderCheck( header ) )
    {
174
        int i_xing, i_skip;
175 176
        uint8_t *p_xing;

177 178 179 180
        if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
            return VLC_SUCCESS; /* No header */

        if( MPGA_VERSION( header ) == 0 )
181
        {
182 183 184 185 186 187 188
            i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
        }
        else
        {
            i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
        }

189
        if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
190 191 192 193 194
        {
            unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );

            p_xing += i_skip + 8;
            i_xing -= i_skip + 8;
195

196 197
            i_skip = 0;
            if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
198
            {
199 200
                p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
                i_skip += 4;
201
            }
202
            if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
203
            {
204 205
                p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
                i_skip += 4;
206
            }
207
            if( i_flags&0x04 )   /* XING_TOC */
208
            {
209 210
                i_skip += 100;
            }
211

212 213 214 215 216 217 218 219
            // FIXME: doesn't return the right bitrage average, at least
            // with some MP3's
            if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
            {
                p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
                msg_Dbg( p_demux, "xing vbr value present (%d)",
                         p_sys->i_xing_bitrate_avg );
            }
220

221 222 223 224 225 226 227
            if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
            {
                p_sys->i_xing_frame_samples = mpga_frame_samples( header );
                msg_Dbg( p_demux, "xing frames&bytes value present "
                         "(%d bytes, %d frames, %d samples/frame)",
                         p_sys->i_xing_bytes, p_sys->i_xing_frames,
                         p_sys->i_xing_frame_samples );
228 229 230 231
            }
        }
    }

232 233 234 235 236 237 238
    if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
    {
        return VLC_EGENERIC;
    }
    p_block_in->i_pts = p_block_in->i_dts = 1;
    p_block_out = p_sys->p_packetizer->pf_packetize(
        p_sys->p_packetizer, &p_block_in );
239 240 241 242 243 244 245 246 247

    if( p_block_out == NULL )
    {
        msg_Dbg( p_demux, "did not sync on first block" );
        p_sys->b_initial_sync_failed = VLC_TRUE;
    }
    else
        p_sys->b_initial_sync_failed = VLC_FALSE;

248 249 250 251
    p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
    p_sys->p_es = es_out_Add( p_demux->out,
                              &p_sys->p_packetizer->fmt_out);
    p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
252

253 254 255 256 257 258 259 260 261 262 263
    if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
        p_sys->i_xing_frame_samples )
    {
        p_sys->i_bitrate_avg = p_sys->i_xing_bytes * I64C(8) *
            p_sys->p_packetizer->fmt_out.audio.i_rate /
            p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
    }

    p_sys->p_block_in = p_block_in;
    p_sys->p_block_out = p_block_out;

264 265 266 267 268 269 270 271
    /* Parse possible id3 header */
    if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
    {
        p_sys->meta = (vlc_meta_t *)p_demux->p_private;
        p_demux->p_private = NULL;
        module_Unneed( p_demux, p_id3 );
    }

Laurent Aimar's avatar
Laurent Aimar committed
272
    return VLC_SUCCESS;
273 274 275 276 277 278 279
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
280
static int Demux( demux_t *p_demux )
281
{
Laurent Aimar's avatar
Laurent Aimar committed
282
    demux_sys_t *p_sys = p_demux->p_sys;
283
    block_t *p_block_in, *p_block_out;
284
    if( p_sys->b_start )
285
    {
286 287 288 289 290 291 292 293 294 295 296
        p_sys->b_start = VLC_FALSE;
        p_block_in = p_sys->p_block_in;
        p_block_out = p_sys->p_block_out;
    }
    else
    {
        if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
            == NULL )
        {
            return 0;
        }
297 298 299 300 301 302 303 304
        if( p_demux->p_sys->b_initial_sync_failed == VLC_TRUE )
        {
            p_block_in->i_pts = p_block_in->i_dts = 1;
            /* Only try to resync once */
            p_demux->p_sys->b_initial_sync_failed = 0;
        }
        else
            p_block_in->i_pts = p_block_in->i_dts = 0;
305 306
        p_block_out = p_sys->p_packetizer->pf_packetize(
            p_sys->p_packetizer, &p_block_in );
307 308 309
    }


310
    while( p_block_out )
311 312
    {
        while( p_block_out )
313
        {
314
            block_t *p_next = p_block_out->p_next;
315

316
            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
317

318 319 320
            p_block_out->p_next = NULL;
            p_sys->i_pts = p_block_out->i_pts;
            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
321

322 323
            p_block_out = p_next;
        }
324 325
        p_block_out = p_sys->p_packetizer->pf_packetize(
            p_sys->p_packetizer, &p_block_in );
326
    }
327
    return 1;
328 329 330 331 332 333 334
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
Laurent Aimar's avatar
Laurent Aimar committed
335 336
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
337

338
    DESTROY_PACKETIZER( p_sys->p_packetizer );
339 340
    if( p_sys->meta ) vlc_meta_Delete( p_sys->meta );

341 342 343
    free( p_sys );
}

Laurent Aimar's avatar
Laurent Aimar committed
344 345 346 347 348 349
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys  = p_demux->p_sys;
350
    int64_t *pi64;
351
    vlc_meta_t *p_meta;
352
    int i_ret;
353 354 355 356

    switch( i_query )
    {
        case DEMUX_GET_META:
357 358
            p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
            vlc_meta_Merge( p_meta, p_sys->meta );
359 360
            return VLC_SUCCESS;

361 362
        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
363
            *pi64 = p_sys->i_pts + p_sys->i_time_offset;
364 365 366 367 368
            return VLC_SUCCESS;

        case DEMUX_SET_TIME:
            /* FIXME TODO: implement a high precision seek (with mp3 parsing)
             * needed for multi-input */
369

370
        default:
371
            i_ret = demux2_vaControlHelper( p_demux->s, 0, -1,
372 373 374
                                            p_sys->i_bitrate_avg, 1, i_query,
                                            args );
            if( !i_ret && p_sys->i_bitrate_avg > 0 &&
375
                (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
376
            {
377 378
                int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) /
                    p_sys->i_bitrate_avg;
379

380 381
                /* Fix time_offset */
                if( i_time >= 0 ) p_sys->i_time_offset = i_time - p_sys->i_pts;
382 383
            }
            return i_ret;
384
    }
Laurent Aimar's avatar
Laurent Aimar committed
385
}