demux.c 16.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * demux.c
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
5
 * $Id$
6
 *
7
 * Author: Laurent Aimar <fenrir@via.ecp.fr>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26
 *****************************************************************************/

#include <stdlib.h>
#include <vlc/vlc.h>

Laurent Aimar's avatar
Laurent Aimar committed
27
#include "input_internal.h"
28

29 30
static void SkipID3Tag( demux_t * );

31 32
/*****************************************************************************
 * demux2_New:
Laurent Aimar's avatar
Laurent Aimar committed
33
 *  if s is NULL then load a access_demux
34 35
 *****************************************************************************/
demux_t *__demux2_New( vlc_object_t *p_obj,
36 37
                       const char *psz_access, const char *psz_demux,
                       const char *psz_path,
38
                       stream_t *s, es_out_t *out, vlc_bool_t b_quick )
39
{
40
    demux_t *p_demux = vlc_object_create( p_obj, VLC_OBJECT_DEMUX );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
41
    const char *psz_module;
42

43
    if( p_demux == NULL ) return NULL;
44 45

    /* Parse URL */
46 47 48
    p_demux->psz_access = strdup( psz_access );
    p_demux->psz_demux  = strdup( psz_demux );
    p_demux->psz_path   = strdup( psz_path );
49

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
50
    /* Take into account "demux" to be able to do :demux=dump */
51 52 53 54 55 56
    if( *p_demux->psz_demux == '\0' )
    {
        free( p_demux->psz_demux );
        p_demux->psz_demux = var_GetString( p_obj, "demux" );
    }

57 58
    if( !b_quick )
    {
Clément Stenac's avatar
Clément Stenac committed
59
        msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
60 61
                 p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
    }
62 63 64 65 66 67 68

    p_demux->s          = s;
    p_demux->out        = out;

    p_demux->pf_demux   = NULL;
    p_demux->pf_control = NULL;
    p_demux->p_sys      = NULL;
69 70 71
    p_demux->info.i_update = 0;
    p_demux->info.i_title  = 0;
    p_demux->info.i_seekpoint = 0;
72

73 74
    if( s ) psz_module = p_demux->psz_demux;
    else psz_module = p_demux->psz_access;
Laurent Aimar's avatar
Laurent Aimar committed
75 76

    if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
77
    {
78 79 80 81
       /* XXX: add only file without any problem here and with strong detection.
        *  - no .mp3, .a52, ... (aac is added as it works only by file ext
        *     anyway
        *  - wav can't be added 'cause of a52 and dts in them as raw audio
82
         */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
83
         static const struct { char ext[5]; char demux[9]; } exttodemux[] =
84
         {
85 86 87 88 89 90 91
            { "aac",  "aac" },
            { "aiff", "aiff" },
            { "asf",  "asf" }, { "wmv",  "asf" }, { "wma",  "asf" },
            { "avi",  "avi" },
            { "au",   "au" },
            { "flac", "flac" },
            { "dv",   "dv" },
92
            { "m3u",  "playlist" },
93 94 95 96 97 98 99
            { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
            { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
            { "mod",  "mod" }, { "xm",   "mod" },
            { "nsv",  "nsv" },
            { "ogg",  "ogg" }, { "ogm",  "ogg" },
            { "pva",  "pva" },
            { "rm",   "rm" },
100 101
            { "m4v",  "m4v" },
            { "h264",  "h264" },
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102
            { "",  "" },
103
        };
104
        /* Here, we don't mind if it does not work, it must be quick */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
105
        static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
106 107 108 109
        {
            { "mp3", "mpga" },
            { "ogg", "ogg" },
            { "wma", "asf" },
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
110
            { "", "" }
111
        };
112

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
113
        const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
114 115
        int  i;

116 117
        if( !b_quick )
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
118
            for( i = 0; exttodemux[i].ext[0]; i++ )
119 120 121 122 123 124 125 126 127
            {
                if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
                {
                    psz_module = exttodemux[i].demux;
                    break;
                }
            }
        }
        else
128
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
129
            for( i = 0; exttodemux_quick[i].ext[0]; i++ )
130
            {
131 132 133 134 135
                if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
                {
                    psz_module = exttodemux_quick[i].demux;
                    break;
                }
136
            }
137

138 139 140
        }
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
141 142
    /* Before module_Need (for var_Create...) */
    vlc_object_attach( p_demux, p_obj );
143

Laurent Aimar's avatar
Laurent Aimar committed
144 145
    if( s )
    {
146 147 148 149 150
        /* ID3 tags will mess-up demuxer probing so we skip it here.
         * ID3 parsers will called later on in the demuxer to access the
         * skipped info. */
        SkipID3Tag( p_demux );

Laurent Aimar's avatar
Laurent Aimar committed
151 152
        p_demux->p_module =
            module_Need( p_demux, "demux2", psz_module,
153 154
                         !strcmp( psz_module, p_demux->psz_demux ) ?
                         VLC_TRUE : VLC_FALSE );
Laurent Aimar's avatar
Laurent Aimar committed
155 156 157 158 159
    }
    else
    {
        p_demux->p_module =
            module_Need( p_demux, "access_demux", psz_module,
160 161
                         !strcmp( psz_module, p_demux->psz_access ) ?
                         VLC_TRUE : VLC_FALSE );
Laurent Aimar's avatar
Laurent Aimar committed
162
    }
163

164 165 166
    if( p_demux->p_module == NULL )
    {
        vlc_object_detach( p_demux );
Gildas Bazin's avatar
 
Gildas Bazin committed
167 168 169
        free( p_demux->psz_path );
        free( p_demux->psz_demux );
        free( p_demux->psz_access );
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        vlc_object_destroy( p_demux );
        return NULL;
    }

    return p_demux;
}

/*****************************************************************************
 * demux2_Delete:
 *****************************************************************************/
void demux2_Delete( demux_t *p_demux )
{
    module_Unneed( p_demux, p_demux->p_module );
    vlc_object_detach( p_demux );

    free( p_demux->psz_path );
    free( p_demux->psz_demux );
    free( p_demux->psz_access );

    vlc_object_destroy( p_demux );
}
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 232 233 234 235 236 237 238 239 240 241 242 243 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

/*****************************************************************************
 * demux2_vaControlHelper:
 *****************************************************************************/
int demux2_vaControlHelper( stream_t *s,
                            int64_t i_start, int64_t i_end,
                            int i_bitrate, int i_align,
                            int i_query, va_list args )
{
    int64_t i_tell;
    double  f, *pf;
    int64_t i64, *pi64;

    if( i_end < 0 )    i_end   = stream_Size( s );
    if( i_start < 0 )  i_start = 0;
    if( i_align <= 0 ) i_align = 1;
    i_tell = stream_Tell( s );

    switch( i_query )
    {
        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( i_bitrate > 0 && i_end > i_start )
            {
                *pi64 = I64C(8000000) * (i_end - i_start) / i_bitrate;
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( i_bitrate > 0 && i_end > i_start )
            {
                *pi64 = I64C(8000000) * (i_tell - i_start) / i_bitrate;
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_GET_POSITION:
            pf = (double*)va_arg( args, double * );
            if( i_start < i_end )
            {
                *pf = (double)( i_tell - i_start ) /
                      (double)( i_end  - i_start );
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;


        case DEMUX_SET_POSITION:
            f = (double)va_arg( args, double );
            if( i_start < i_end && f >= 0.0 && f <= 1.0 )
            {
                int64_t i_block = (f * ( i_end - i_start )) / i_align;

                if( stream_Seek( s, i_start + i_block * i_align ) )
                {
                    return VLC_EGENERIC;
                }
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_SET_TIME:
            i64 = (int64_t)va_arg( args, int64_t );
            if( i_bitrate > 0 && i64 >= 0 )
            {
                int64_t i_block = i64 * i_bitrate / I64C(8000000) / i_align;
                if( stream_Seek( s, i_start + i_block * i_align ) )
                {
                    return VLC_EGENERIC;
                }
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_GET_FPS:
        case DEMUX_GET_META:
269 270
        case DEMUX_SET_NEXT_DEMUX_TIME:
        case DEMUX_GET_TITLE_INFO:
271
        case DEMUX_SET_GROUP:
272 273 274 275 276 277 278 279
            return VLC_EGENERIC;

        default:
            msg_Err( s, "unknown query in demux_vaControlDefault" );
            return VLC_EGENERIC;
    }
}

Laurent Aimar's avatar
Laurent Aimar committed
280 281 282 283 284 285
/****************************************************************************
 * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
 ****************************************************************************/
typedef struct
{
    /* Data buffer */
286 287
    block_fifo_t *p_fifo;
    block_t      *p_block;
Laurent Aimar's avatar
Laurent Aimar committed
288 289 290 291 292 293 294

    int64_t     i_pos;

    /* Demuxer */
    char        *psz_name;
    es_out_t    *out;
    demux_t     *p_demux;
295

Laurent Aimar's avatar
Laurent Aimar committed
296 297 298 299 300 301 302 303
} d_stream_sys_t;

static int DStreamRead   ( stream_t *, void *p_read, int i_read );
static int DStreamPeek   ( stream_t *, uint8_t **pp_peek, int i_peek );
static int DStreamControl( stream_t *, int i_query, va_list );
static int DStreamThread ( stream_t * );


304
stream_t *__stream_DemuxNew( vlc_object_t *p_obj, const char *psz_demux,
305
                             es_out_t *out )
Laurent Aimar's avatar
Laurent Aimar committed
306 307 308 309 310
{
    /* We create a stream reader, and launch a thread */
    stream_t       *s;
    d_stream_sys_t *p_sys;

311
    if( psz_demux == NULL || *psz_demux == '\0' ) return NULL;
Laurent Aimar's avatar
Laurent Aimar committed
312 313 314 315 316 317 318

    s = vlc_object_create( p_obj, VLC_OBJECT_STREAM );
    s->pf_block  = NULL;
    s->pf_read   = DStreamRead;
    s->pf_peek   = DStreamPeek;
    s->pf_control= DStreamControl;

319 320 321
    s->i_char_width = 1;
    s->b_little_endian = VLC_FALSE;

Laurent Aimar's avatar
Laurent Aimar committed
322 323 324 325 326 327
    s->p_sys = malloc( sizeof( d_stream_sys_t) );
    p_sys = (d_stream_sys_t*)s->p_sys;

    p_sys->i_pos = 0;
    p_sys->out = out;
    p_sys->p_demux = NULL;
328 329 330 331 332 333 334 335 336 337 338
    p_sys->p_block = NULL;
    p_sys->psz_name = strdup( psz_demux );

    /* decoder fifo */
    if( ( p_sys->p_fifo = block_FifoNew( s ) ) == NULL )
    {
        msg_Err( s, "out of memory" );
        vlc_object_destroy( s );
        free( p_sys );
        return NULL;
    }
Laurent Aimar's avatar
Laurent Aimar committed
339

340 341
    if( vlc_thread_create( s, "stream out", DStreamThread,
                           VLC_THREAD_PRIORITY_INPUT, VLC_FALSE ) )
Laurent Aimar's avatar
Laurent Aimar committed
342 343 344 345 346 347 348 349 350
    {
        vlc_object_destroy( s );
        free( p_sys );
        return NULL;
    }

    return s;
}

351
void stream_DemuxSend( stream_t *s, block_t *p_block )
Laurent Aimar's avatar
Laurent Aimar committed
352 353
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
354
    if( p_block ) block_FifoPut( p_sys->p_fifo, p_block );
Laurent Aimar's avatar
Laurent Aimar committed
355 356
}

357
void stream_DemuxDelete( stream_t *s )
Laurent Aimar's avatar
Laurent Aimar committed
358 359
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
360
    block_t *p_empty;
Laurent Aimar's avatar
Laurent Aimar committed
361 362

    s->b_die = VLC_TRUE;
363
    if( p_sys->p_demux ) p_sys->p_demux->b_die = VLC_TRUE;
364 365
    p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
    block_FifoPut( p_sys->p_fifo, p_empty );
Laurent Aimar's avatar
Laurent Aimar committed
366 367
    vlc_thread_join( s );

368
    if( p_sys->p_demux ) demux2_Delete( p_sys->p_demux );
369
    if( p_sys->p_block ) block_Release( p_sys->p_block );
370 371

    block_FifoRelease( p_sys->p_fifo );
Laurent Aimar's avatar
Laurent Aimar committed
372 373
    free( p_sys->psz_name );
    free( p_sys );
374

Laurent Aimar's avatar
Laurent Aimar committed
375 376 377 378
    vlc_object_destroy( s );
}


379
static int DStreamRead( stream_t *s, void *p_read, int i_read )
Laurent Aimar's avatar
Laurent Aimar committed
380 381
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
382 383
    uint8_t *p_out = p_read;
    int i_out = 0;
Laurent Aimar's avatar
Laurent Aimar committed
384 385 386

    //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );

387
    while( !s->b_die && !s->b_error && i_read )
Laurent Aimar's avatar
Laurent Aimar committed
388
    {
389 390
        block_t *p_block = p_sys->p_block;
        int i_copy;
391

392 393 394 395 396 397
        if( !p_block )
        {
            p_block = block_FifoGet( p_sys->p_fifo );
            if( !p_block ) s->b_error = 1;
            p_sys->p_block = p_block;
        }
Laurent Aimar's avatar
Laurent Aimar committed
398

399
        if( p_block && i_read )
Laurent Aimar's avatar
Laurent Aimar committed
400
        {
401 402 403 404 405 406 407 408 409 410 411 412
            i_copy = __MIN( i_read, p_block->i_buffer );
            if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
            i_read -= i_copy;
            i_out += i_copy;
            p_block->i_buffer -= i_copy;
            p_block->p_buffer += i_copy;

            if( !p_block->i_buffer )
            {
                block_Release( p_block );
                p_sys->p_block = NULL;
            }
Laurent Aimar's avatar
Laurent Aimar committed
413 414 415
        }
    }

416 417
    p_sys->i_pos += i_out;
    return i_out;
Laurent Aimar's avatar
Laurent Aimar committed
418
}
419 420

static int DStreamPeek( stream_t *s, uint8_t **pp_peek, int i_peek )
Laurent Aimar's avatar
Laurent Aimar committed
421 422
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
423 424 425
    block_t **pp_block = &p_sys->p_block;
    int i_out = 0;
    *pp_peek = 0;
Laurent Aimar's avatar
Laurent Aimar committed
426 427

    //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
428 429

    while( !s->b_die && !s->b_error && i_peek )
Laurent Aimar's avatar
Laurent Aimar committed
430
    {
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        int i_copy;

        if( !*pp_block )
        {
            *pp_block = block_FifoGet( p_sys->p_fifo );
            if( !*pp_block ) s->b_error = 1;
        }

        if( *pp_block && i_peek )
        {
            i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
            i_peek -= i_copy;
            i_out += i_copy;

            if( i_peek ) pp_block = &(*pp_block)->p_next;
        }
Laurent Aimar's avatar
Laurent Aimar committed
447 448
    }

449 450 451 452 453
    if( p_sys->p_block )
    {
        p_sys->p_block = block_ChainGather( p_sys->p_block );
        *pp_peek = p_sys->p_block->p_buffer;
    }
Laurent Aimar's avatar
Laurent Aimar committed
454

455
    return i_out;
Laurent Aimar's avatar
Laurent Aimar committed
456 457
}

458
static int DStreamControl( stream_t *s, int i_query, va_list args )
Laurent Aimar's avatar
Laurent Aimar committed
459 460 461 462 463
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
    int64_t    *p_i64;
    vlc_bool_t *p_b;
    int        *p_int;
464

Laurent Aimar's avatar
Laurent Aimar committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    switch( i_query )
    {
        case STREAM_GET_SIZE:
            p_i64 = (int64_t*) va_arg( args, int64_t * );
            *p_i64 = 0;
            return VLC_SUCCESS;

        case STREAM_CAN_SEEK:
            p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
            *p_b = VLC_FALSE;
            return VLC_SUCCESS;

        case STREAM_CAN_FASTSEEK:
            p_b = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
            *p_b = VLC_FALSE;
            return VLC_SUCCESS;

        case STREAM_GET_POSITION:
            p_i64 = (int64_t*) va_arg( args, int64_t * );
            *p_i64 = p_sys->i_pos;
            return VLC_SUCCESS;

        case STREAM_SET_POSITION:
488 489 490
        {
            int64_t i64 = (int64_t)va_arg( args, int64_t );
            int i_skip;
491
            if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
492 493 494 495 496
            i_skip = i64 - p_sys->i_pos;

            while( i_skip > 0 )
            {
                int i_read = DStreamRead( s, NULL, i_skip );
497
                if( i_read <= 0 ) return VLC_EGENERIC;
498 499 500 501
                i_skip -= i_read;
            }
            return VLC_SUCCESS;
        }
Laurent Aimar's avatar
Laurent Aimar committed
502 503 504 505 506 507

        case STREAM_GET_MTU:
            p_int = (int*) va_arg( args, int * );
            *p_int = 0;
            return VLC_SUCCESS;

508 509 510
        case STREAM_CONTROL_ACCESS:
            return VLC_EGENERIC;

Laurent Aimar's avatar
Laurent Aimar committed
511 512 513 514 515 516
        default:
            msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
            return VLC_EGENERIC;
    }
}

517
static int DStreamThread( stream_t *s )
Laurent Aimar's avatar
Laurent Aimar committed
518 519
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
520
    demux_t *p_demux;
Laurent Aimar's avatar
Laurent Aimar committed
521 522

    /* Create the demuxer */
523 524
    if( !(p_demux = demux2_New( s, "", p_sys->psz_name, "", s, p_sys->out,
                               VLC_FALSE )) )
Laurent Aimar's avatar
Laurent Aimar committed
525 526 527 528 529 530 531 532 533
    {
        return VLC_EGENERIC;
    }

    p_sys->p_demux = p_demux;

    /* Main loop */
    while( !s->b_die && !p_demux->b_die )
    {
534
        if( p_demux->pf_demux( p_demux ) <= 0 ) break;
Laurent Aimar's avatar
Laurent Aimar committed
535
    }
536

Laurent Aimar's avatar
Laurent Aimar committed
537 538 539
    p_demux->b_die = VLC_TRUE;
    return VLC_SUCCESS;
}
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

/****************************************************************************
 * Utility functions
 ****************************************************************************/
static void SkipID3Tag( demux_t *p_demux )
{
    uint8_t *p_peek;
    uint8_t version, revision;
    int i_size;
    int b_footer;

    if( !p_demux->s ) return;

    /* Get 10 byte id3 header */
    if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return;

    if( p_peek[0] != 'I' || p_peek[1] != 'D' || p_peek[2] != '3' ) return;

    version = p_peek[3];
    revision = p_peek[4];
    b_footer = p_peek[5] & 0x10;
    i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];

    if( b_footer ) i_size += 10;
    i_size += 10;

    /* Skip the entire tag */
    stream_Read( p_demux->s, NULL, i_size );

569
    msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
570 571 572 573
             version, revision, i_size );

    return;
}