wav.c 17.9 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * wav.c : wav file input module for vlc
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: wav.c,v 1.4 2002/11/28 16:32:29 fenrir Exp $
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
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 * 
 * 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>                                              /* strdup() */

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

32
#include <codecs.h>
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include "wav.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int    WAVInit       ( vlc_object_t * );
static void __WAVEnd        ( vlc_object_t * );
static int    WAVDemux      ( input_thread_t * );
static int    WAVCallDemux  ( input_thread_t * );

#define WAVEnd(a) __WAVEnd(VLC_OBJECT(a))

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( "WAV demuxer" );
    set_capability( "demux", 142 );
    set_callbacks( WAVInit, __WAVEnd );
vlc_module_end();

/*****************************************************************************
 * Declaration of local function 
 *****************************************************************************/

#define FREE( p ) if( p ) free( p ); (p) = NULL

#define __EVEN( x ) ( (x)%2 != 0 ) ? ((x)+1) : (x)

/* Some functions to manipulate memory */
static u16 GetWLE( u8 *p_buff )
{
    return( (p_buff[0]) + ( p_buff[1] <<8 ) );
}

static u32 GetDWLE( u8 *p_buff )
{
    return( p_buff[0] + ( p_buff[1] <<8 ) +
            ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
}

static u32 CreateDWLE( int a, int b, int c, int d )
{
    return( a + ( b << 8 ) + ( c << 16 ) + ( d << 24 ) );
}


static off_t TellAbsolute( input_thread_t *p_input )
{
    off_t i_pos;
    
    vlc_mutex_lock( &p_input->stream.stream_lock );
    
Laurent Aimar's avatar
 
Laurent Aimar committed
86 87
    i_pos= p_input->stream.p_selected_area->i_tell;
//          - ( p_input->p_last_data - p_input->p_current_data  );
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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

    vlc_mutex_unlock( &p_input->stream.stream_lock );

    return( i_pos );
}
 
static int SeekAbsolute( input_thread_t *p_input,
                         off_t i_pos)
{
    off_t i_filepos;

    if( i_pos >= p_input->stream.p_selected_area->i_size )
    {
        return( 0 );
    }
            
    i_filepos = TellAbsolute( p_input );
    if( i_pos != i_filepos )
    {
        p_input->pf_seek( p_input, i_pos );
        input_AccessReinit( p_input );
    }
    return( 1 );
}

static int SkipBytes( input_thread_t *p_input, int i_skip )
{
    return( SeekAbsolute( p_input, TellAbsolute( p_input ) + i_skip ) );
}

/* return 1 if success, 0 if fail */
static int ReadData( input_thread_t *p_input, u8 *p_buff, int i_size )
{
    data_packet_t *p_data;

    int i_read;

                
    if( !i_size )
    {
        return( 1 );
    }

    do
    {
        i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
        if( i_read <= 0 )
        {
            return( 0 );
        }
        memcpy( p_buff, p_data->p_payload_start, i_read );
        input_DeletePacket( p_input->p_method_data, p_data );
        
        p_buff += i_read;
        i_size -= i_read;
                
    } while( i_size );
    
    return( 1 );
}


static int ReadPES( input_thread_t *p_input, 
                    pes_packet_t **pp_pes, 
                    int i_size )
{
    pes_packet_t *p_pes;

    *pp_pes = NULL;
        
    if( !(p_pes = input_NewPES( p_input->p_method_data )) )
    {
        msg_Err( p_input, "cannot allocate new PES" );
        return( 0 );
    }

    while( i_size > 0 )
    {
        data_packet_t   *p_data;
        int i_read;

        if( (i_read = input_SplitBuffer( p_input, 
                                         &p_data, 
                                         __MIN( i_size, 1024 ) ) ) <= 0 )
        {
            input_DeletePES( p_input->p_method_data, p_pes );
            return( 0 );
        }
        if( !p_pes->p_first )
        {
            p_pes->p_first = p_data;
            p_pes->i_nb_data = 1;
            p_pes->i_pes_size = i_read;
        }
        else
        {
            p_pes->p_last->p_next  = p_data;
            p_pes->i_nb_data++;
            p_pes->i_pes_size += i_read;
        }
        p_pes->p_last  = p_data;
        i_size -= i_read;
    }
    *pp_pes = p_pes;
    return( 1 );
}

static int FindTag( input_thread_t *p_input, u32 i_tag )
{
    u32   i_id;
    u32   i_size;
    u8    *p_peek;

    for( ;; )
    {

        if( input_Peek( p_input, &p_peek, 8 ) < 8 )
        {
            msg_Err( p_input, "cannot peek()" );
            return( 0 );
        }

        i_id   = GetDWLE( p_peek );
        i_size = GetDWLE( p_peek + 4 );

        msg_Dbg( p_input, "FindTag: tag:%4.4s size:%d", &i_id, i_size );
        if( i_id == i_tag )
        {
            /* Yes, we have found the good tag */
            return( 1 );
        }
        if( !SkipBytes( p_input, __EVEN( i_size ) + 8 ) )
        {
            return( 0 );
        }
    }
}

static int LoadTag_fmt( input_thread_t *p_input, 
                        demux_sys_t *p_demux )
{
    u8  *p_peek;
    u32 i_size;
231 232 233
    WAVEFORMATEX *p_wf;
            

234 235 236 237 238 239 240 241 242 243 244 245
    if( input_Peek( p_input, &p_peek , 8 ) < 8 )
    {
        return( 0 );
    }

    p_demux->i_wf = i_size = GetDWLE( p_peek + 4 );
    SkipBytes( p_input, 8 );
    if( i_size < 16 )
    {
        SkipBytes( p_input, i_size );
        return( 0 );
    }
246 247 248 249 250 251 252 253 254 255
    p_wf = p_demux->p_wf = malloc( __MAX( i_size, sizeof( WAVEFORMATEX) ) );
    ReadData( p_input, (uint8_t*)p_wf, __EVEN( i_size ) );

    p_wf->wFormatTag      = GetWLE( (uint8_t*)&p_demux->p_wf->wFormatTag );
    p_wf->nChannels       = GetWLE( (uint8_t*)&p_demux->p_wf->nChannels );
    p_wf->nSamplesPerSec  = GetWLE( (uint8_t*)&p_demux->p_wf->nSamplesPerSec );
    p_wf->nAvgBytesPerSec = GetWLE( (uint8_t*)&p_demux->p_wf->nAvgBytesPerSec );
    p_wf->nBlockAlign     = GetWLE( (uint8_t*)&p_demux->p_wf->nBlockAlign );
    p_wf->wBitsPerSample  = GetWLE( (uint8_t*)&p_demux->p_wf->wBitsPerSample );
    if( i_size >= sizeof( WAVEFORMATEX) )
256
    {
257
        p_wf->cbSize          = GetWLE( (uint8_t*)&p_demux->p_wf->cbSize );
258 259 260
    }
    else
    {
261
        p_wf->cbSize = 0;
262 263 264 265 266 267 268
    }

    msg_Dbg( p_input, "loaded \"fmt \" chunk" );
    return( 1 );
}

static int PCM_GetFrame( input_thread_t *p_input,
269 270 271
                         WAVEFORMATEX   *p_wf,
                         pes_packet_t   **pp_pes,
                         mtime_t        *pi_length )
272 273 274 275 276 277 278
{
    int i_samples;

    int i_bytes;
    int i_modulo;

    /* read samples for 50ms of */
279
    i_samples = __MAX( p_wf->nSamplesPerSec / 20, 1 );
280 281 282 283
        
    
    *pi_length = (mtime_t)1000000 * 
                 (mtime_t)i_samples / 
284
                 (mtime_t)p_wf->nSamplesPerSec;
285

286
    i_bytes = i_samples * p_wf->nChannels * ( p_wf->wBitsPerSample + 7 ) / 8;
287
    
288
    if( p_wf->nBlockAlign > 0 )
289
    {
290
        if( ( i_modulo = i_bytes % p_wf->nBlockAlign ) != 0 )
291
        {
292
            i_bytes += p_wf->nBlockAlign - i_modulo;
293 294 295 296 297 298 299 300 301 302 303 304 305 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 337 338 339 340 341 342 343 344 345 346 347 348 349
        }
    }

    return( ReadPES( p_input, pp_pes, i_bytes ) );
}

/*****************************************************************************
 * WAVInit: check file and initializes structures
 *****************************************************************************/
static int WAVInit( vlc_object_t * p_this )
{   
    input_thread_t *p_input = (input_thread_t *)p_this;
    u8  *p_peek;
    u32 i_size;
    
    demux_sys_t *p_demux;
    


    /* Initialize access plug-in structures. */
    if( p_input->i_mtu == 0 )
    {
        /* Improve speed. */
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
    }

    /* a little test to see if it's a wav file */
    if( input_Peek( p_input, &p_peek, 12 ) < 12 )
    {
        msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
        return( -1 );
    }

    if( ( GetDWLE( p_peek ) != CreateDWLE( 'R', 'I', 'F', 'F' ) )||
        ( GetDWLE( p_peek + 8 ) != CreateDWLE( 'W', 'A', 'V', 'E' ) ) )
    {
        msg_Warn( p_input, "WAV plugin discarded (not a valid file)" );
        return( -1 );
    }
    i_size = GetDWLE( p_peek + 4 );
    SkipBytes( p_input, 12 );

    if( !FindTag( p_input, CreateDWLE( 'f', 'm', 't' ,' ' ) ) )
    {
        msg_Err( p_input, "cannot find \"fmt \" tag" );
        return( -1 );
    }

    /* create our structure that will contains all data */
    if( !( p_input->p_demux_data = 
                p_demux = malloc( sizeof( demux_sys_t ) ) ) )
    {
        msg_Err( p_input, "out of memory" );
        return( -1 );
    }
    memset( p_demux, 0, sizeof( demux_sys_t ) );
       
350
    /* Load WAVEFORMATEX header */
351 352 353 354 355 356 357
    if( !LoadTag_fmt( p_input, p_demux ) )
    {
        msg_Err( p_input, "cannot load \"fmt \" tag" );
        FREE( p_demux );
        return( -1 );
    }
    msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
358 359 360 361 362 363 364 365
            p_demux->p_wf->wFormatTag,
            p_demux->p_wf->nChannels,
            p_demux->p_wf->nSamplesPerSec,
            p_demux->p_wf->nAvgBytesPerSec / 1024,
            p_demux->p_wf->nBlockAlign,
            p_demux->p_wf->wBitsPerSample,
            p_demux->p_wf->cbSize );
           
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    if( !FindTag( p_input, CreateDWLE( 'd', 'a', 't', 'a' ) ) )
    {
        msg_Err( p_input, "cannot find \"data\" tag" );
        FREE( p_demux->p_wf );
        FREE( p_demux );
        return( -1 );
    }
    if( input_Peek( p_input, &p_peek, 8 ) < 8 )
    {
        msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
        FREE( p_demux->p_wf );
        FREE( p_demux );
        return( -1 );
    }

    p_demux->i_data_pos = TellAbsolute( p_input ) + 8;
    p_demux->i_data_size = GetDWLE( p_peek + 4 );
    SkipBytes( p_input, 8 );

    /* XXX p_demux->psz_demux shouldn't be NULL ! */
386
    switch( p_demux->p_wf->wFormatTag )
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    {
        case( 0x01 ):
            msg_Dbg( p_input,"found raw pcm audio format" );
            p_demux->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
            p_demux->GetFrame = PCM_GetFrame;
            p_demux->psz_demux = strdup( "" );
            break;
        case( 0x50 ):
        case( 0x55 ):
            msg_Dbg( p_input, "found mpeg audio format" );
            p_demux->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
            p_demux->GetFrame = NULL;
            p_demux->psz_demux = strdup( "mpegaudio" );
            break;
        case( 0x2000 ):
            msg_Dbg( p_input,"found a52 audio format" );
            p_demux->i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
            p_demux->GetFrame = NULL;
            p_demux->psz_demux = strdup( "a52" );
            break;
        default:
            msg_Warn( p_input,"unrecognize audio format(0x%x)", 
409
                      p_demux->p_wf->wFormatTag );
410 411
            p_demux->i_fourcc = 
                VLC_FOURCC( 'm', 's', 
412 413
                            (p_demux->p_wf->wFormatTag >> 8)&0xff,
                            (p_demux->p_wf->wFormatTag )&0xff);
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
            p_demux->GetFrame = NULL;
            p_demux->psz_demux = strdup( "" );
            break;
    }
    
    if( p_demux->GetFrame )
    {
        msg_Dbg( p_input, "using internal demux" );

        p_input->pf_demux = WAVDemux;
        p_input->p_demux_data = p_demux;
        
        /*  create one program */
        vlc_mutex_lock( &p_input->stream.stream_lock );
        if( input_InitStream( p_input, 0 ) == -1)
        {
            vlc_mutex_unlock( &p_input->stream.stream_lock );
            msg_Err( p_input, "cannot init stream" );
            // FIXME 
            return( -1 );
        }
        if( input_AddProgram( p_input, 0, 0) == NULL )
        {
            vlc_mutex_unlock( &p_input->stream.stream_lock );
            msg_Err( p_input, "cannot add program" );
            // FIXME 
            return( -1 );
        }
        p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
        p_input->stream.i_mux_rate = 0 ; /* FIXME */

        p_demux->p_es = input_AddES( p_input,
                                     p_input->stream.p_selected_program, 1,
                                     p_demux->i_wf );
        p_demux->p_es->i_stream_id = 1;
        p_demux->p_es->i_fourcc = p_demux->i_fourcc;
        p_demux->p_es->i_cat = AUDIO_ES;
451 452 453
        memcpy( p_demux->p_es->p_demux_data,
                p_demux->p_wf,
                p_demux->i_wf );
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
        
        input_SelectES( p_input, p_demux->p_es );
        
        p_input->stream.p_selected_program->b_is_ok = 1;
        vlc_mutex_unlock( &p_input->stream.stream_lock );
    }
    else
    {
        char *psz_sav;
        /* call an external demux */
        msg_Warn( p_input, "unsupported formattag, using external demux" );
        
        psz_sav = p_input->psz_demux;
        p_input->psz_demux = p_demux->psz_demux;

        p_demux->p_demux = module_Need( p_input, "demux", NULL );
        
        p_input->psz_demux = psz_sav;
        
        if( !p_demux->p_demux )
        {
            msg_Err( p_input, 
                     "cannot get external demux for formattag 0x%x",
477
                     p_demux->p_wf->wFormatTag );
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
            FREE( p_demux->psz_demux );
            FREE( p_demux->p_wf );
            FREE( p_demux );
            return( -1 );
        }
        /* save value and switch back */
        p_demux->pf_demux = p_input->pf_demux;
        p_demux->p_demux_data = p_input->p_demux_data;

        p_input->pf_demux = WAVCallDemux;
        p_input->p_demux_data = p_demux;

    }

    return( 0 );    
}

/*****************************************************************************
 * WAVCallDemux: call true demux
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int WAVCallDemux( input_thread_t *p_input )
{
    demux_sys_t  *p_demux = p_input->p_demux_data;
    int i_status;
    char *psz_sav;
    
    /* save context */
    psz_sav = p_input->psz_demux;

    /* switch context */
    p_input->pf_demux = p_demux->pf_demux;
    p_input->p_demux_data = p_demux->p_demux_data;
    p_input->psz_demux = p_demux->psz_demux;

    /* call demux */
    i_status = p_input->pf_demux( p_input );

    /* save (new?) state */
    p_demux->pf_demux = p_input->pf_demux;
    p_demux->p_demux_data = p_input->p_demux_data;
    
    /* switch back */
    p_input->psz_demux = psz_sav;
    p_input->pf_demux = WAVCallDemux;
    p_input->p_demux_data = p_demux;

    return( i_status );
}

/*****************************************************************************
 * WAVDemux: read packet and send them to decoders
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int WAVDemux( input_thread_t *p_input )
{
    demux_sys_t  *p_demux = p_input->p_demux_data;
    pes_packet_t *p_pes;
    mtime_t      i_length;

    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
    {
        off_t   i_offset;

        i_offset = TellAbsolute( p_input ) - p_demux->i_data_pos;
        if( i_offset < 0 )
        {
            SeekAbsolute( p_input, p_demux->i_data_pos );
        }
        else
550
        if( p_demux->p_wf->nBlockAlign != 0 )
551 552
        {
            
553
            i_offset = i_offset - i_offset % p_demux->p_wf->nBlockAlign;
554 555 556 557 558 559 560 561 562 563 564 565 566
            SeekAbsolute( p_input, p_demux->i_data_pos + i_offset );
        }
    }
    
    input_ClockManageRef( p_input,
                          p_input->stream.p_selected_program,
                          p_demux->i_pcr );

    if( TellAbsolute( p_input ) >= p_demux->i_data_pos + p_demux->i_data_size )
    {
        return( 0 ); // EOF
    }

567
    if( !p_demux->GetFrame( p_input, p_demux->p_wf, &p_pes, &i_length ) )
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    {
        msg_Warn( p_input, "failed to get one frame" );
        return( 0 );
    }

    p_pes->i_dts = 
        p_pes->i_pts = input_ClockGetTS( p_input, 
                                         p_input->stream.p_selected_program,
                                         p_demux->i_pcr );
   
    if( !p_demux->p_es->p_decoder_fifo )
    {
        msg_Err( p_input, "no audio decoder" );
        input_DeletePES( p_input->p_method_data, p_pes );
        return( -1 );
    }
    else
    {
        input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
    }
    
    p_demux->i_pcr += i_length * 9 / 100;
    return( 1 );
}

/*****************************************************************************
 * WAVEnd: frees unused data
 *****************************************************************************/
static void __WAVEnd ( vlc_object_t * p_this )
{   
    input_thread_t *  p_input = (input_thread_t *)p_this;
    demux_sys_t *p_demux = p_input->p_demux_data;
    
    FREE( p_demux->p_wf );
    FREE( p_demux->psz_demux );
    
    if( p_demux->p_demux )
    {
        module_Unneed( p_input, p_demux->p_demux );
    }

}