quicktime.c 39.5 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * quicktime.c: a quicktime decoder that uses the QT library/dll
 *****************************************************************************
 * Copyright (C) 2003 VideoLAN
Gildas Bazin's avatar
 
Gildas Bazin committed
5
 * $Id: quicktime.c,v 1.9 2003/07/21 17:48:31 gbazin 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 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * Authors: Laurent Aimar <fenrir at via.ecp.fr>
 *          Derk-Jan Hartman <thedj at users.sf.net>
 *
 * 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 <vlc/vlc.h>
#include <vlc/aout.h>
#include <vlc/vout.h>
#include <vlc/decoder.h>
#include <vlc/input.h>

#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>                                              /* strdup() */
#include "codecs.h"

#ifdef SYS_DARWIN
#include <QuickTime/QuickTimeComponents.h>
#include <QuickTime/Movies.h>
#include <QuickTime/ImageCodec.h>
#endif

44 45 46 47 48 49
/* for windows do we require Quicktime compents header? */

#ifdef LOADER
#include "w32dll/loader/qtx/qtxsdk/components.h"
#include "w32dll/loader/wine/windef.h"
#include "w32dll/loader/ldt_keeper.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
50 51 52 53 54

HMODULE   WINAPI LoadLibraryA(LPCSTR);
FARPROC   WINAPI GetProcAddress(HMODULE,LPCSTR);
int       WINAPI FreeLibrary(HMODULE);

55 56
#endif

57 58 59 60 61 62
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  OpenDecoder    ( vlc_object_t * );

static int  RunDecoderAudio( decoder_fifo_t * );
63
static int  RunDecoderVideo( decoder_fifo_t * );
64 65 66 67 68 69

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

vlc_module_begin();
70
    set_description( _("QuickTime library decoder") );
71
    set_capability( "decoder", 10 );
72 73 74 75 76 77 78 79 80
    set_callbacks( OpenDecoder, NULL );

    /* create a mutex */
    var_Create( p_module->p_libvlc, "qt_mutex", VLC_VAR_MUTEX );
vlc_module_end();

#define FCC( a, b , c, d ) \
    ((uint32_t)( ((a)<<24)|((b)<<16)|((c)<<8)|(d)))

81
#ifndef SYS_DARWIN
82
typedef struct OpaqueSoundConverter*    SoundConverter;
Gildas Bazin's avatar
 
Gildas Bazin committed
83 84
typedef long                            OSType;
typedef int                             OSErr;
85
typedef unsigned long                   UnsignedFixed;
Gildas Bazin's avatar
 
Gildas Bazin committed
86 87
typedef uint8_t                         Byte;

88 89 90 91 92 93 94 95 96 97
typedef struct SoundComponentData {
    long                            flags;
    OSType                          format;
    short                           numChannels;
    short                           sampleSize;
    UnsignedFixed                   sampleRate;
    long                            sampleCount;
    Byte *                          buffer;
    long                            reserved;
} SoundComponentData;
Gildas Bazin's avatar
 
Gildas Bazin committed
98

99
#endif /* SYS_DARWIN */
100 101 102 103 104 105 106

typedef struct
{
    /* Input properties */
    decoder_fifo_t *p_fifo;

    /* library */
107
#ifndef SYS_DARWIN
108 109
#ifdef LOADER
    ldt_fs_t    *ldt_fs;
Gildas Bazin's avatar
 
Gildas Bazin committed
110

111
#endif /* LOADER */
Gildas Bazin's avatar
 
Gildas Bazin committed
112 113 114
    HMODULE qtml;
    OSErr (*InitializeQTML)             ( long flags );
    OSErr (*TerminateQTML)              ( void );
115
#endif /* SYS_DARWIN */
Gildas Bazin's avatar
 
Gildas Bazin committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    int (*SoundConverterOpen)           ( const SoundComponentData *,
                                          const SoundComponentData *,
                                          SoundConverter* );
    int (*SoundConverterClose)          ( SoundConverter );
    int (*SoundConverterSetInfo)        ( SoundConverter , OSType, void * );
    int (*SoundConverterGetBufferSizes) ( SoundConverter, unsigned long,
                                          unsigned long*, unsigned long*,
                                          unsigned long* );
    int (*SoundConverterBeginConversion)( SoundConverter );
    int (*SoundConverterEndConversion)  ( SoundConverter, void *,
                                          unsigned long *, unsigned long *);
    int (*SoundConverterConvertBuffer)  ( SoundConverter, const void *,
                                          unsigned long, void *,
                                          unsigned long *, unsigned long * );
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    SoundConverter      myConverter;
    SoundComponentData  InputFormatInfo, OutputFormatInfo;

    long            FramesToGet;
    unsigned int    InFrameSize;
    unsigned int    OutFrameSize;

    /* Output properties */
    aout_instance_t *   p_aout;       /* opaque */
    aout_input_t *      p_aout_input; /* opaque */
    audio_sample_format_t output_format;

    audio_date_t        date;
    mtime_t             pts;

    /* buffer */
    unsigned int        i_buffer;
    unsigned int        i_buffer_size;
    uint8_t             *p_buffer;

    uint8_t             buffer_out[1000000];    /* FIXME */
Gildas Bazin's avatar
 
Gildas Bazin committed
152

153 154
} adec_thread_t;

Gildas Bazin's avatar
 
Gildas Bazin committed
155
#ifndef WIN32
156 157 158 159 160 161 162 163 164 165 166
typedef struct
{
    /* Input properties */
    decoder_fifo_t *p_fifo;

    /* library */
#ifndef SYS_DARWIN
#ifdef LOADER
    ldt_fs_t          *ldt_fs;
#endif /* LOADER */
    HMODULE           qtml;
Gildas Bazin's avatar
 
Gildas Bazin committed
167 168
    OSErr             (*InitializeQTML)         ( long flags );

169
#endif /* SYS_DARWIN */
Gildas Bazin's avatar
 
Gildas Bazin committed
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
    Component         (*FindNextComponent)
        ( Component prev, ComponentDescription* desc );

    ComponentInstance (*OpenComponent)
        ( Component c );

    ComponentResult   (*ImageCodecInitialize)
        ( ComponentInstance ci, ImageSubCodecDecompressCapabilities * cap);

    ComponentResult   (*ImageCodecGetCodecInfo)
        ( ComponentInstance ci, CodecInfo *info );

    ComponentResult   (*ImageCodecPreDecompress)
        ( ComponentInstance ci, CodecDecompressParams * params );

    ComponentResult   (*ImageCodecBandDecompress)
        ( ComponentInstance ci, CodecDecompressParams * params );

    PixMapHandle      (*GetGWorldPixMap)
        ( GWorldPtr offscreenGWorld );

    OSErr             (*QTNewGWorldFromPtr)
        ( GWorldPtr *gw, OSType pixelFormat, const Rect *boundsRect,
          CTabHandle cTable, /*GDHandle*/ void *aGDevice, /*unused*/
          GWorldFlags flags, void *baseAddr, long rowBytes );

    OSErr             (*NewHandleClear)
        ( Size byteCount );
198 199 200 201 202 203 204 205

    ComponentInstance       ci;
    Rect                    OutBufferRect;   /* the dimensions of our GWorld */
    GWorldPtr               OutBufferGWorld; /* a GWorld is some kind of
                                                description for a drawing
                                                environment */
    ImageDescriptionHandle  framedescHandle;

Gildas Bazin's avatar
 
Gildas Bazin committed
206
    CodecDecompressParams   decpar;          /* for ImageCodecPreDecompress()*/
207 208 209 210 211 212 213 214 215 216 217 218 219
    CodecCapabilities       codeccap;        /* for decpar */


    /* Output properties */
    vout_thread_t *     p_vout;
    uint8_t *           plane;
    mtime_t             pts;

    /* buffer */
    unsigned int        i_buffer;
    uint8_t             *p_buffer;

} vdec_thread_t;
Gildas Bazin's avatar
 
Gildas Bazin committed
220
#endif
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

static int pi_channels_maps[6] =
{
    0,
    AOUT_CHAN_CENTER,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
    AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
     | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
};


static uint16_t GetWBE( uint8_t *p_buff )
{
    return( (p_buff[0]<<8) + p_buff[1] );
}

static uint32_t GetDWBE( uint8_t *p_buff )
{
    return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
            ( p_buff[2] <<8 ) + p_buff[3] );
}


static int GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
{
    int i_copy;
    int i_count;

    data_packet_t   *p_data;

    i_count = 0;
    p_data = p_pes->p_first;
    while( p_data != NULL && i_count < i_max )
    {

        i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
                        i_max - i_count );

        if( i_copy > 0 )
        {
            memcpy( p_buf,
                    p_data->p_payload_start,
                    i_copy );
        }

        p_data = p_data->p_next;
        i_count += i_copy;
        p_buf   += i_copy;
    }

    if( i_count < i_max )
    {
        memset( p_buf, 0, i_max - i_count );
    }
    return( i_count );
}

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able
 * to choose.
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;

    switch( p_fifo->i_fourcc )
    {
292
        case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
293
    /*    case VLC_FOURCC('S','V','Q','1'):  Sorenson v1 
294 295
        case VLC_FOURCC('Z','y','G','o'):
        case VLC_FOURCC('V','P','3','1'):
296
        case VLC_FOURCC('3','I','V','1'): */
297 298 299 300 301 302
        case VLC_FOURCC('r','l','e',' '): /* QuickTime animation (RLE) */
        case VLC_FOURCC('r','p','z','a'): /* QuickTime Apple Video */
        case VLC_FOURCC('a','z','p','r'): /* QuickTime animation (RLE) */
            p_fifo->pf_run = RunDecoderVideo;
            return VLC_SUCCESS;

Gildas Bazin's avatar
 
Gildas Bazin committed
303
        case VLC_FOURCC('m','p','4','a'): /* MPEG-4 audio */
304 305 306 307 308 309
        case VLC_FOURCC('Q','D','M','C'): /* QDesign */
        case VLC_FOURCC('Q','D','M','2'): /* QDesign* 2 */
        case VLC_FOURCC('Q','c','l','p'): /* Qualcomm Purevoice Codec */
        case VLC_FOURCC('Q','C','L','P'): /* Qualcomm Purevoice Codec */
        case VLC_FOURCC('M','A','C','3'): /* MACE3 audio decoder */
        case VLC_FOURCC('M','A','C','6'): /* MACE6 audio decoder */
310 311 312 313 314 315
        case VLC_FOURCC('d','v','c','a'): /* DV Audio */
        case VLC_FOURCC('s','o','w','t'): /* 16-bit Little Endian */
        case VLC_FOURCC('t','w','o','s'): /* 16-bit Big Endian */
        case VLC_FOURCC('a','l','a','w'): /* ALaw 2:1 */
        case VLC_FOURCC('u','l','a','w'): /* mu-Law 2:1 */
        case VLC_FOURCC('r','a','w',' '): /* 8-bit offset binaries */
316 317 318 319
        case VLC_FOURCC('f','l','3','2'): /* 32-bit Floating Point */
        case VLC_FOURCC('f','l','6','4'): /* 64-bit Floating Point */
        case VLC_FOURCC('i','n','2','4'): /* 24-bit Interger */
        case VLC_FOURCC('i','n','3','2'): /* 32-bit Integer */
Gildas Bazin's avatar
 
Gildas Bazin committed
320 321 322
        case 0x0011:                            /* DVI IMA */
        case 0x6D730002:                        /* Microsoft ADPCM-ACM */
        case 0x6D730011:                        /* DVI Intel IMAADPCM-ACM */
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

            p_fifo->pf_run = RunDecoderAudio;
            return VLC_SUCCESS;
        default:
            return VLC_EGENERIC;
    }
}

/****************************************************************************
 ****************************************************************************
 **
 **     audio part
 **
 **************************************************************************** 
 ****************************************************************************/
static int  InitThreadAudio     ( adec_thread_t * );
static void DecodeThreadAudio   ( adec_thread_t * );
static void EndThreadAudio      ( adec_thread_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
341
static int  QTAudioInit         ( adec_thread_t * );
342

343
static int RunDecoderAudio( decoder_fifo_t *p_fifo )
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
{
    adec_thread_t *p_dec;
    vlc_bool_t    b_error;

    p_dec = malloc( sizeof( adec_thread_t ) );
    if( !p_dec )
    {
        msg_Err( p_fifo, "out of memory" );
        DecoderError( p_fifo );
        return VLC_EGENERIC;
    }
    p_dec->p_fifo = p_fifo;

    if( InitThreadAudio( p_dec ) != 0 )
    {
        DecoderError( p_fifo );
        return VLC_EGENERIC;
    }

    while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
    {
        DecodeThreadAudio( p_dec );
    }


    if( ( b_error = p_dec->p_fifo->b_error ) )
    {
        DecoderError( p_dec->p_fifo );
    }

    EndThreadAudio( p_dec );
    if( b_error )
    {
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}

383
static int InitThreadAudio( adec_thread_t *p_dec )
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
{
    vlc_value_t     lockval;
    int             i_error;
    char            fcc[4];
    unsigned long   WantedBufferSize;
    unsigned long   InputBufferSize = 0;
    unsigned long   OutputBufferSize = 0;

    WAVEFORMATEX    *p_wf;

    if( !( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) )
    {
        msg_Err( p_dec->p_fifo, "missing WAVEFORMATEX");
        return VLC_EGENERIC;
    }
    memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );

    /* get lock, avoid segfault */
    var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
    vlc_mutex_lock( lockval.p_address );
Gildas Bazin's avatar
 
Gildas Bazin committed
404

405 406
#ifdef SYS_DARWIN
    EnterMovies();
Gildas Bazin's avatar
 
Gildas Bazin committed
407
#endif
408

Gildas Bazin's avatar
 
Gildas Bazin committed
409
    if( QTAudioInit( p_dec ) != VLC_SUCCESS )
410
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
411
        msg_Err( p_dec->p_fifo, "cannot initialize QT");
412 413 414
        goto exit_error;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
415
#ifndef SYS_DARWIN
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
    if( ( i_error = p_dec->InitializeQTML( 6 + 16 ) ) )
    {
        msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_error );
        goto exit_error;
    }
#endif

    /* input format settings */
    p_dec->InputFormatInfo.flags       = 0;
    p_dec->InputFormatInfo.sampleCount = 0;
    p_dec->InputFormatInfo.buffer      = NULL;
    p_dec->InputFormatInfo.reserved    = 0;
    p_dec->InputFormatInfo.numChannels = p_wf->nChannels;
    p_dec->InputFormatInfo.sampleSize  = p_wf->wBitsPerSample;
    p_dec->InputFormatInfo.sampleRate  = p_wf->nSamplesPerSec;
    p_dec->InputFormatInfo.format      = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );

    /* output format settings */
    p_dec->OutputFormatInfo.flags       = 0;
    p_dec->OutputFormatInfo.sampleCount = 0;
    p_dec->OutputFormatInfo.buffer      = NULL;
    p_dec->OutputFormatInfo.reserved    = 0;
    p_dec->OutputFormatInfo.numChannels = p_wf->nChannels;
    p_dec->OutputFormatInfo.sampleSize  = 16;
    p_dec->OutputFormatInfo.sampleRate  = p_wf->nSamplesPerSec;
    p_dec->OutputFormatInfo.format      = FCC( 'N', 'O', 'N', 'E' );

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
443 444 445 446 447
#ifdef SYS_DARWIN
/* on OS X QT is not threadsafe */
    vlc_mutex_lock( &p_dec->p_fifo->p_vlc->quicktime_lock );
#endif

448 449 450 451 452
    i_error = p_dec->SoundConverterOpen( &p_dec->InputFormatInfo,
                                         &p_dec->OutputFormatInfo,
                                         &p_dec->myConverter );
    if( i_error )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
453 454
        msg_Err( p_dec->p_fifo,
                 "error while SoundConverterOpen = %d", i_error );
455 456 457
        goto exit_error;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
458 459 460 461 462 463
#if 0
    /* tell the sound converter we accept VBR formats */
    i_error = SoundConverterSetInfo( p_dec->myConverter, siClientAcceptsVBR,
                                     (void *)true );
#endif

464 465
    if( p_wf->cbSize > 36 + 8 )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
466 467 468 469 470 471
        i_error =
            p_dec->SoundConverterSetInfo( p_dec->myConverter,
                                          FCC( 'w', 'a', 'v', 'e' ),
                                          ((uint8_t*)&p_wf[1]) + 36 + 8 );
        msg_Dbg( p_dec->p_fifo,
                 "error while SoundConverterSetInfo = %d", i_error );
472 473
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
474 475
    WantedBufferSize = p_dec->OutputFormatInfo.numChannels *
                         p_dec->OutputFormatInfo.sampleRate * 2;
476
    p_dec->FramesToGet = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
477

478
    i_error = p_dec->SoundConverterGetBufferSizes( p_dec->myConverter,
Gildas Bazin's avatar
 
Gildas Bazin committed
479 480
                  WantedBufferSize, &p_dec->FramesToGet,
                  &InputBufferSize, &OutputBufferSize );
481

Gildas Bazin's avatar
 
Gildas Bazin committed
482 483 484
    msg_Dbg( p_dec->p_fifo, "WantedBufferSize=%li InputBufferSize=%li "
             "OutputBufferSize=%li FramesToGet=%li", WantedBufferSize,
             InputBufferSize, OutputBufferSize, p_dec->FramesToGet );
485

Gildas Bazin's avatar
 
Gildas Bazin committed
486 487
    p_dec->InFrameSize  = (InputBufferSize + p_dec->FramesToGet - 1 ) /
                            p_dec->FramesToGet;
488 489
    p_dec->OutFrameSize = OutputBufferSize / p_dec->FramesToGet;

Gildas Bazin's avatar
 
Gildas Bazin committed
490 491
    msg_Dbg( p_dec->p_fifo, "frame size %d -> %d",
             p_dec->InFrameSize, p_dec->OutFrameSize );
492 493 494 495

    i_error = p_dec->SoundConverterBeginConversion( p_dec->myConverter );
    if( i_error )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
496 497
        msg_Err( p_dec->p_fifo,
                 "error while SoundConverterBeginConversion = %d", i_error );
498 499 500
        goto exit_error;
    }

501 502 503 504
#ifdef SYS_DARWIN
    vlc_mutex_unlock( &p_dec->p_fifo->p_vlc->quicktime_lock );
#endif

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
    p_dec->output_format.i_format   = AOUT_FMT_S16_NE;
    p_dec->output_format.i_rate     = p_wf->nSamplesPerSec;
    p_dec->output_format.i_physical_channels =
        p_dec->output_format.i_original_channels =
            pi_channels_maps[p_wf->nChannels];
    aout_DateInit( &p_dec->date, p_dec->output_format.i_rate );
    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 )
    {
        msg_Err( p_dec->p_fifo, "cannot create aout" );
        goto exit_error;
    }

    p_dec->i_buffer      = 0;
    p_dec->i_buffer_size = 100*1000;
    p_dec->p_buffer      = malloc( p_dec->i_buffer_size );

    p_dec->pts = -1;

    vlc_mutex_unlock( lockval.p_address );
    return VLC_SUCCESS;

exit_error:
530
#ifdef LOADER
531 532 533 534 535
    Restore_LDT_Keeper( p_dec->ldt_fs );
#endif
    vlc_mutex_unlock( lockval.p_address );
    return VLC_EGENERIC;
}
Gildas Bazin's avatar
 
Gildas Bazin committed
536 537

static void DecodeThreadAudio( adec_thread_t *p_dec )
538 539 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 569 570 571 572 573 574
{
    pes_packet_t    *p_pes;
    vlc_value_t     lockval;
    int             i_error;

    var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );

    input_ExtractPES( p_dec->p_fifo, &p_pes );
    if( !p_pes )
    {
        msg_Err( p_dec->p_fifo, "cannot get PES" );
        p_dec->p_fifo->b_error = 1;
        return;
    }
    /*if( p_dec->pts <= 0 )*/
    {
        p_dec->pts = p_pes->i_pts;
    }

    if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
    {

        if( p_dec->i_buffer_size < p_dec->i_buffer + p_pes->i_pes_size )
        {
            p_dec->i_buffer_size = p_dec->i_buffer + p_pes->i_pes_size + 1024;
            p_dec->p_buffer = realloc( p_dec->p_buffer,
                                       p_dec->i_buffer_size );
        }

        GetPESData( &p_dec->p_buffer[p_dec->i_buffer],
                    p_dec->i_buffer_size - p_dec->i_buffer, p_pes );
        p_dec->i_buffer += p_pes->i_pes_size;

        if( p_dec->i_buffer > p_dec->InFrameSize )
        {
            int i_frames = p_dec->i_buffer / p_dec->InFrameSize;
            long i_out_frames, i_out_bytes;
575
            /* enough data */
576 577 578 579 580 581

            vlc_mutex_lock( lockval.p_address );
            i_error = p_dec->SoundConverterConvertBuffer( p_dec->myConverter,
                                                          p_dec->p_buffer,
                                                          i_frames,
                                                          p_dec->buffer_out,
Gildas Bazin's avatar
 
Gildas Bazin committed
582 583
                                                          &i_out_frames,
                                                          &i_out_bytes );
584 585
            vlc_mutex_unlock( lockval.p_address );

Gildas Bazin's avatar
 
Gildas Bazin committed
586 587 588
            /*
            msg_Dbg( p_dec->p_fifo,
                     "decoded %d frames -> %ld frames (error=%d)",
589 590
                     i_frames, i_out_frames, i_error );

Gildas Bazin's avatar
 
Gildas Bazin committed
591 592 593 594
            msg_Dbg( p_dec->p_fifo, "decoded %ld frames = %ld bytes",
                     i_out_frames, i_out_bytes );
            */

595 596 597 598 599 600 601 602
            p_dec->i_buffer -= i_frames * p_dec->InFrameSize;
            if( p_dec->i_buffer > 0 )
            {
                memmove( &p_dec->p_buffer[0],
                         &p_dec->p_buffer[i_frames * p_dec->InFrameSize],
                         p_dec->i_buffer );
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
603
            if( !i_error && i_out_frames > 0 )
604 605 606 607 608 609 610
            {
                aout_buffer_t   *p_aout_buffer;
                uint8_t         *p_buff = p_dec->buffer_out;

                /*msg_Dbg( p_dec->p_fifo, "pts=%lld date=%lld dateget=%lld",
                         p_dec->pts, mdate(), aout_DateGet( &p_dec->date ) );*/

Gildas Bazin's avatar
 
Gildas Bazin committed
611 612
                if( p_dec->pts != 0 &&
                    p_dec->pts != aout_DateGet( &p_dec->date ) )
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
                {
                    aout_DateSet( &p_dec->date, p_dec->pts );
                }
                else if( !aout_DateGet( &p_dec->date ) )
                {
                    input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
                    return;
                }

                while( i_out_frames > 0 )
                {
                    int i_frames;

                    i_frames = __MIN( i_out_frames, 1000 );
                    p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
                                                       p_dec->p_aout_input,
                                                       i_frames );
                    if( !p_aout_buffer )
                    {
                        msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
                        p_dec->p_fifo->b_error = 1;
                        return;
                    }
                    p_aout_buffer->start_date = aout_DateGet( &p_dec->date );
                    p_aout_buffer->end_date = aout_DateIncrement( &p_dec->date,
                                                                  i_frames );

                    memcpy( p_aout_buffer->p_buffer,
                            p_buff,
                            p_aout_buffer->i_nb_bytes );

Gildas Bazin's avatar
 
Gildas Bazin committed
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
                    /*
                    msg_Dbg( p_dec->p_fifo,
                             "==> start=%lld end=%lld date=%lld",
                             p_aout_buffer->start_date,
                             p_aout_buffer->end_date, mdate() );
                    */

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

                    /*
                    msg_Dbg( p_dec->p_fifo, "s1=%d s2=%d", i_framesperchannels,
                             p_aout_buffer->i_nb_samples );

                    msg_Dbg( p_dec->p_fifo, "i_nb_bytes=%d i_nb_samples*4=%d",
                             p_aout_buffer->i_nb_bytes,
                             p_aout_buffer->i_nb_samples * 4 );
                    */
662 663 664 665 666 667 668 669 670 671 672 673

                    p_buff += i_frames * 4;
                    i_out_frames -= i_frames;
                }

                p_dec->pts = -1;
            }
        }
    }

    input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
}
674 675

static void EndThreadAudio( adec_thread_t *p_dec )
676 677 678 679 680 681 682 683 684 685
{
    vlc_value_t             lockval;
    int i_error;
    unsigned long ConvertedFrames=0;
    unsigned long ConvertedBytes=0;

    /* get lock, avoid segfault */
    var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
    vlc_mutex_lock( lockval.p_address );

Gildas Bazin's avatar
 
Gildas Bazin committed
686 687 688
    i_error = p_dec->SoundConverterEndConversion( p_dec->myConverter, NULL,
                                                  &ConvertedFrames,
                                                  &ConvertedBytes );
689 690 691 692 693
    msg_Dbg( p_dec->p_fifo, "SoundConverterEndConversion => %d", i_error );

    i_error = p_dec->SoundConverterClose( p_dec->myConverter );
    msg_Dbg( p_dec->p_fifo, "SoundConverterClose => %d", i_error );

694 695
#ifndef SYS_DARWIN
    FreeLibrary( p_dec->qtml );
Gildas Bazin's avatar
 
Gildas Bazin committed
696
    msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
697
#endif
698 699
    vlc_mutex_unlock( lockval.p_address );

700 701
#ifdef LOADER
    Restore_LDT_Keeper( p_dec->ldt_fs );
Gildas Bazin's avatar
 
Gildas Bazin committed
702
    msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );
703
#endif
704 705 706 707 708 709
#ifdef SYS_DARWIN
    ExitMovies();
#endif
    aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
}

Gildas Bazin's avatar
 
Gildas Bazin committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
static int QTAudioInit( adec_thread_t *p_dec )
{

#ifdef SYS_DARWIN
    p_dec->SoundConverterOpen       = (void*)SoundConverterOpen;
    p_dec->SoundConverterClose      = (void*)SoundConverterClose;
    p_dec->SoundConverterSetInfo    = (void*)SoundConverterSetInfo;
    p_dec->SoundConverterGetBufferSizes = (void*)SoundConverterGetBufferSizes;
    p_dec->SoundConverterConvertBuffer  = (void*)SoundConverterConvertBuffer;
    p_dec->SoundConverterBeginConversion= (void*)SoundConverterBeginConversion;
    p_dec->SoundConverterEndConversion  = (void*)SoundConverterEndConversion;
#else

#ifdef LOADER
    p_dec->ldt_fs = Setup_LDT_Keeper();
#endif /* LOADER */

    p_dec->qtml = LoadLibraryA( "qtmlClient.dll" );
    if( p_dec->qtml == NULL )
    {
        msg_Dbg( p_dec->p_fifo, "failed loading qtmlClient.dll" );
        return VLC_EGENERIC;
    }

    p_dec->InitializeQTML =
        (void *)GetProcAddress( p_dec->qtml, "InitializeQTML" );
    if( p_dec->InitializeQTML == NULL )
    {
        msg_Dbg( p_dec->p_fifo, "failed geting proc address InitializeQTML" );
        return VLC_EGENERIC;
    }

    p_dec->SoundConverterOpen =
        (void *)GetProcAddress( p_dec->qtml, "SoundConverterOpen" );
    if( p_dec->SoundConverterOpen == NULL )
    {
        msg_Dbg( p_dec->p_fifo,
                 "failed getting proc address SoundConverterOpen");
        return VLC_EGENERIC;
    }

    p_dec->SoundConverterClose =
        (void *)GetProcAddress( p_dec->qtml, "SoundConverterClose" );
    if( p_dec->SoundConverterClose == NULL )
    {
        msg_Dbg( p_dec->p_fifo,
                 "failed getting proc address SoundConverterClose");
        return VLC_EGENERIC;
    }

    p_dec->TerminateQTML =
        (void *)GetProcAddress( p_dec->qtml, "TerminateQTML" );
    if( p_dec->TerminateQTML == NULL )
    {
        msg_Dbg( p_dec->p_fifo, "failed getting proc address TerminateQTML");
        return VLC_EGENERIC;
    }

    p_dec->SoundConverterSetInfo =
        (void *)GetProcAddress( p_dec->qtml, "SoundConverterSetInfo" );
    if( p_dec->SoundConverterSetInfo == NULL )
    {
        msg_Dbg( p_dec->p_fifo,
                 "failed getting proc address SoundConverterSetInfo");
        return VLC_EGENERIC;
    }

    p_dec->SoundConverterGetBufferSizes =
        (void *)GetProcAddress( p_dec->qtml, "SoundConverterGetBufferSizes" );
    if( p_dec->SoundConverterGetBufferSizes == NULL )
    {
        msg_Dbg( p_dec->p_fifo,
                 "failed getting proc address SoundConverterGetBufferSizes");
        return VLC_EGENERIC;
    }

    p_dec->SoundConverterConvertBuffer =
        (void *)GetProcAddress( p_dec->qtml, "SoundConverterConvertBuffer" );
    if( p_dec->SoundConverterConvertBuffer == NULL )
    {
        msg_Dbg( p_dec->p_fifo,
                 "failed getting proc address SoundConverterConvertBuffer" );
        return VLC_EGENERIC;
    }

    p_dec->SoundConverterEndConversion =
        (void *)GetProcAddress( p_dec->qtml, "SoundConverterEndConversion" );
    if( p_dec->SoundConverterEndConversion == NULL )
    {
        msg_Dbg( p_dec->p_fifo,
                 "failed getting proc address SoundConverterEndConversion" );
        return VLC_EGENERIC;
    }

    p_dec->SoundConverterBeginConversion =
        (void *)GetProcAddress( p_dec->qtml, "SoundConverterBeginConversion");
    if( p_dec->SoundConverterBeginConversion == NULL )
    {
        msg_Dbg( p_dec->p_fifo,
                 "failed getting proc address SoundConverterBeginConversion" );
        return VLC_EGENERIC;
    }

    msg_Dbg( p_dec->p_fifo,
             "Standard init done you may now call supported functions" );
#endif /* else SYS_DARWIN */

    return VLC_SUCCESS;
}
819

820 821 822 823 824 825 826 827
/****************************************************************************
 ****************************************************************************
 **
 **     video part
 **
 **************************************************************************** 
 ****************************************************************************/

Gildas Bazin's avatar
 
Gildas Bazin committed
828 829 830 831
#ifdef WIN32
static int  RunDecoderVideo( decoder_fifo_t *p_fifo ){ return VLC_EGENERIC; }

#else
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
static int  InitThreadVideo     ( vdec_thread_t * );
static void DecodeThreadVideo   ( vdec_thread_t * );
static void EndThreadVideo      ( vdec_thread_t * );

static int  RunDecoderVideo( decoder_fifo_t *p_fifo )
{
    vdec_thread_t *p_dec;
    vlc_bool_t    b_error;

    p_dec = malloc( sizeof( vdec_thread_t ) );
    if( !p_dec )
    {
        msg_Err( p_fifo, "out of memory" );
        DecoderError( p_fifo );
        return VLC_EGENERIC;
    }
    p_dec->p_fifo = p_fifo;

    if( InitThreadVideo( p_dec ) != 0 )
    {
        DecoderError( p_fifo );
        return VLC_EGENERIC;
    }

    while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
    {
        DecodeThreadVideo( p_dec );
    }


    if( ( b_error = p_dec->p_fifo->b_error ) )
    {
        DecoderError( p_dec->p_fifo );
    }
866

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
    EndThreadVideo( p_dec );
    if( b_error )
    {
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}

/*
 * InitThreadVideo: load and init library
 *
 */
static int InitThreadVideo( vdec_thread_t *p_dec )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
882 883 884 885 886 887 888 889
    vlc_value_t                         lockval;
    long                                i_result;
    ComponentDescription                desc;
    Component                           prev;
    ComponentResult                     cres;
    ImageSubCodecDecompressCapabilities icap;   /* for ImageCodecInitialize() */
    CodecInfo                           cinfo;  /* for ImageCodecGetCodecInfo() */
    ImageDescription                    *id;
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

    BITMAPINFOHEADER    *p_bih;
    int                 i_vide;
    uint8_t             *p_vide;
    char                fcc[4];

    if( !( p_bih  = (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader ) )
    {
        msg_Err( p_dec->p_fifo, "missing BITMAPINFOHEADER !!" );
        return VLC_EGENERIC;
    }
    i_vide = p_bih->biSize - sizeof( BITMAPINFOHEADER );
    p_vide = (uint8_t*)&p_bih[1];
    if( i_vide <= 0 || p_vide == NULL )
    {
        msg_Err( p_dec->p_fifo, "invalid BITMAPINFOHEADER !!" );
        return VLC_EGENERIC;
    }
    memcpy( fcc, &p_dec->p_fifo->i_fourcc, 4 );
Gildas Bazin's avatar
 
Gildas Bazin committed
909 910
    msg_Dbg( p_dec->p_fifo, "quicktime_video %4.4s %dx%d", fcc,
             p_bih->biWidth, p_bih->biHeight );
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

    /* get lock, avoid segfault */
    var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );
    vlc_mutex_lock( lockval.p_address );
#ifdef SYS_DARWIN
    EnterMovies();
#else
#ifdef LOADER
    p_dec->ldt_fs = Setup_LDT_Keeper();
#endif /* LOADER */
    msg_Dbg( p_dec->p_fifo, "trying to load `qtmlClient.dll'" );
    if( !( p_dec->qtml = LoadLibraryA("qtmlClient.dll") ) )
    {
        msg_Err( p_dec->p_fifo, "cannot load qtmlClient.dll");
        goto exit_error;
    }

    msg_Dbg( p_dec->p_fifo, "qtmlClient.dll loaded" );

    /* (void*) to shut up gcc */
    p_dec->InitializeQTML           = (void*)InitializeQTML;
#endif /* SYS_DARWIN */
    p_dec->FindNextComponent        = (void*)FindNextComponent;
    p_dec->OpenComponent            = (void*)OpenComponent;
    p_dec->ImageCodecInitialize     = (void*)ImageCodecInitialize;
    p_dec->ImageCodecGetCodecInfo   = (void*)ImageCodecGetCodecInfo;
    p_dec->ImageCodecPreDecompress  = (void*)ImageCodecPreDecompress;
    p_dec->ImageCodecBandDecompress = (void*)ImageCodecBandDecompress;
    p_dec->GetGWorldPixMap          = (void*)GetGWorldPixMap;
    p_dec->QTNewGWorldFromPtr       = (void*)QTNewGWorldFromPtr;
    p_dec->NewHandleClear           = (void*)NewHandleClear;

#ifndef SYS_DARWIN
    /* some sanity check */
    if( !p_dec->InitializeQTML ||
        !p_dec->FindNextComponent ||
        !p_dec->OpenComponent ||
        !p_dec->ImageCodecBandDecompress )
    {
        msg_Err( p_dec->p_fifo, "error getting qtmlClient.dll symbols");
        goto exit_error;
    }

    if( ( i_result = p_dec->InitializeQTML( 6 + 16 ) ) )
    {
        msg_Dbg( p_dec->p_fifo, "error while InitializeQTML = %d", i_result );
        goto exit_error;
    }
#endif

    /* init ComponentDescription */
    memset( &desc, 0, sizeof( ComponentDescription ) );
    desc.componentType      = FCC( 'i', 'm', 'd', 'c' );
    desc.componentSubType   = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
    desc.componentManufacturer = 0;
    desc.componentFlags        = 0;
    desc.componentFlagsMask    = 0;

    if( !( prev = p_dec->FindNextComponent( NULL, &desc ) ) )
    {
        msg_Err( p_dec->p_fifo, "cannot find requested component" );
        goto exit_error;
    }
    msg_Dbg( p_dec->p_fifo, "component id=0x%p", prev );

    p_dec->ci =  p_dec->OpenComponent( prev );
    msg_Dbg( p_dec->p_fifo, "component instance p=0x%p", p_dec->ci );

    memset( &icap, 0, sizeof( ImageSubCodecDecompressCapabilities ) );
    cres =  p_dec->ImageCodecInitialize( p_dec->ci, &icap );
/*    msg_Dbg( p_dec->p_fifo, "ImageCodecInitialize->%p  size=%d (%d)\n",cres,icap.recordSize,icap.decompressRecordSize); */


    memset( &cinfo, 0, sizeof( CodecInfo ) );
985
    cres =  p_dec->ImageCodecGetCodecInfo( p_dec->ci, &cinfo );
Gildas Bazin's avatar
 
Gildas Bazin committed
986 987 988 989 990 991
    msg_Dbg( p_dec->p_fifo,
             "Flags: compr: 0x%lx  decomp: 0x%lx format: 0x%lx\n",
             cinfo.compressFlags, cinfo.decompressFlags, cinfo.formatFlags );
    msg_Dbg( p_dec->p_fifo, "quicktime_video: Codec name: %.*s\n",
             ((unsigned char*)&cinfo.typeName)[0],
             ((unsigned char*)&cinfo.typeName)+1 );
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

    /* make a yuy2 gworld */
    p_dec->OutBufferRect.top    = 0;
    p_dec->OutBufferRect.left   = 0;
    p_dec->OutBufferRect.right  = p_bih->biWidth;
    p_dec->OutBufferRect.bottom = p_bih->biHeight;


    /* codec data FIXME use codec not SVQ3 */
    msg_Dbg( p_dec->p_fifo, "vide = %d", i_vide );
    id = malloc( sizeof( ImageDescription ) + ( i_vide - 70 ) );
    id->idSize          = sizeof( ImageDescription ) + ( i_vide - 70 );
    id->cType           = FCC( fcc[0], fcc[1], fcc[2], fcc[3] );
    id->version         = GetWBE ( p_vide +  0 );
    id->revisionLevel   = GetWBE ( p_vide +  2 );
    id->vendor          = GetDWBE( p_vide +  4 );
    id->temporalQuality = GetDWBE( p_vide +  8 );
    id->spatialQuality  = GetDWBE( p_vide + 12 );
    id->width           = GetWBE ( p_vide + 16 );
    id->height          = GetWBE ( p_vide + 18 );
    id->hRes            = GetDWBE( p_vide + 20 );
    id->vRes            = GetDWBE( p_vide + 24 );
    id->dataSize        = GetDWBE( p_vide + 28 );
    id->frameCount      = GetWBE ( p_vide + 32 );
    memcpy( &id->name, p_vide + 34, 32 );
    id->depth           = GetWBE ( p_vide + 66 );
    id->clutID          = GetWBE ( p_vide + 68 );
    if( i_vide > 70 )
    {
        memcpy( ((char*)&id->clutID) + 2, p_vide + 70, i_vide - 70 );
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1024 1025
    msg_Dbg( p_dec->p_fifo, "idSize=%ld ver=%d rev=%d vendor=%ld tempQ=%d "
             "spaQ=%d w=%d h=%d dpi=%d%d dataSize=%d frameCount=%d clutID=%d",
1026 1027 1028 1029 1030 1031 1032 1033
             id->idSize, id->version, id->revisionLevel, id->vendor,
             (int)id->temporalQuality, (int)id->spatialQuality,
             id->width, id->height,
             (int)id->hRes, (int)id->vRes,
             (int)id->dataSize,
             id->frameCount,
             id->clutID );

Gildas Bazin's avatar
 
Gildas Bazin committed
1034 1035
    p_dec->framedescHandle =
        (ImageDescriptionHandle) p_dec->NewHandleClear( id->idSize );
1036
    memcpy( *p_dec->framedescHandle, id, id->idSize );
1037 1038 1039

    p_dec->plane = malloc( p_bih->biWidth * p_bih->biHeight * 3 );

Gildas Bazin's avatar
 
Gildas Bazin committed
1040 1041 1042 1043 1044 1045 1046 1047 1048
    i_result = p_dec->QTNewGWorldFromPtr( &p_dec->OutBufferGWorld,
                                          /*pixel format of new GWorld==YUY2 */
                                          kYUVSPixelFormat,
                                          /* we should benchmark if yvu9 is
                                           * faster for svq3, too */
                                          &p_dec->OutBufferRect,
                                          0, 0, 0,
                                          p_dec->plane,
                                          p_bih->biWidth * 2 );
1049

Gildas Bazin's avatar
 
Gildas Bazin committed
1050 1051
    msg_Dbg( p_dec->p_fifo, "NewGWorldFromPtr returned:%ld\n",
             65536 - ( i_result&0xffff ) );
1052 1053 1054 1055

    memset( &p_dec->decpar, 0, sizeof( CodecDecompressParams ) );
    p_dec->decpar.imageDescription = p_dec->framedescHandle;
    p_dec->decpar.startLine        = 0;
1056
    p_dec->decpar.stopLine         = ( **p_dec->framedescHandle ).height;
1057 1058 1059 1060 1061 1062 1063 1064
    p_dec->decpar.frameNumber      = 1;
    p_dec->decpar.matrixFlags      = 0;
    p_dec->decpar.matrixType       = 0;
    p_dec->decpar.matrix           = 0;
    p_dec->decpar.capabilities     = &p_dec->codeccap;
    p_dec->decpar.accuracy         = codecNormalQuality;
    p_dec->decpar.srcRect          = p_dec->OutBufferRect;
    p_dec->decpar.transferMode     = srcCopy;
1065
    p_dec->decpar.dstPixMap        = **p_dec->GetGWorldPixMap( p_dec->OutBufferGWorld );/*destPixmap;  */
1066

1067
    cres =  p_dec->ImageCodecPreDecompress( p_dec->ci, &p_dec->decpar );
Gildas Bazin's avatar
 
Gildas Bazin committed
1068 1069 1070
    msg_Dbg( p_dec->p_fifo,
             "quicktime_video: ImageCodecPreDecompress cres=0x%X\n",
             (int)cres );
1071 1072 1073 1074

    p_dec->p_vout = vout_Request( p_dec->p_fifo, NULL,
                                  p_bih->biWidth, p_bih->biHeight,
                                  VLC_FOURCC( 'Y', 'U', 'Y', '2' ),
Gildas Bazin's avatar
 
Gildas Bazin committed
1075 1076
                                  VOUT_ASPECT_FACTOR * p_bih->biWidth /
                                  p_bih->biHeight );
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

    if( !p_dec->p_vout )
    {
        msg_Err( p_dec->p_fifo, "cannot get a vout" );
        goto exit_error;
    }

    p_dec->i_buffer = 1000*1000;
    p_dec->p_buffer = malloc( p_dec->i_buffer );

    vlc_mutex_unlock( lockval.p_address );
    return VLC_SUCCESS;

exit_error:
#ifdef LOADER
    Restore_LDT_Keeper( p_dec->ldt_fs );
#endif
    vlc_mutex_unlock( lockval.p_address );
    return VLC_EGENERIC;

}

1099
static void DecodeThreadVideo( vdec_thread_t *p_dec )
1100
{
Gildas Bazin's avatar
 
Gildas Bazin committed
1101 1102 1103
    BITMAPINFOHEADER *p_bih =
        (BITMAPINFOHEADER*)p_dec->p_fifo->p_bitmapinfoheader;

1104 1105 1106
    pes_packet_t    *p_pes;
    vlc_value_t     lockval;
    picture_t       *p_pic;
Gildas Bazin's avatar
 
Gildas Bazin committed
1107
    ComponentResult cres;
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150

    var_Get( p_dec->p_fifo->p_libvlc, "qt_mutex", &lockval );

    input_ExtractPES( p_dec->p_fifo, &p_pes );
    if( !p_pes )
    {
        msg_Err( p_dec->p_fifo, "cannot get PES" );
        p_dec->p_fifo->b_error = 1;
        return;
    }

    if( p_pes->i_pes_size > p_dec->i_buffer )
    {
        p_dec->i_buffer = 3 * p_pes->i_pes_size / 2;
        free( p_dec->p_buffer );
        p_dec->p_buffer = malloc( p_dec->i_buffer );
    }

    if( p_pes->i_pes_size > 0 && p_pes->i_pts > mdate() )
    {
        GetPESData( p_dec->p_buffer, p_dec->i_buffer, p_pes );

        while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
        {
            if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
            {
                break;
            }
            msleep( VOUT_OUTMEM_SLEEP );
        }

        p_dec->decpar.data                  = p_dec->p_buffer;
        p_dec->decpar.bufferSize            = p_pes->i_pes_size;
        (**p_dec->framedescHandle).dataSize = p_pes->i_pes_size;

        vlc_mutex_lock( lockval.p_address );
        cres = p_dec->ImageCodecBandDecompress( p_dec->ci, &p_dec->decpar );
        vlc_mutex_unlock( lockval.p_address );

        ++p_dec->decpar.frameNumber;

        if( cres &0xFFFF )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1151 1152
            msg_Dbg( p_dec->p_fifo, "quicktime_video: ImageCodecBandDecompress"
                     " cres=0x%X (-0x%X) %d :(\n",
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
                     (int)cres,(int)-cres, (int)cres );
        }

        memcpy( p_pic->p[0].p_pixels,
                p_dec->plane,
                p_bih->biWidth * p_bih->biHeight * 2 );

        vout_DatePicture( p_dec->p_vout, p_pic, p_pes->i_pts );
        vout_DisplayPicture( p_dec->p_vout, p_pic );
    }

    input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_pes );
}

1167
static void EndThreadVideo( vdec_thread_t *p_dec )
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
{
    msg_Dbg( p_dec->p_fifo, "QuickTime library video decoder closing" );
    free( p_dec->plane );
    vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );

#ifndef SYS_DARWIN
    FreeLibrary( p_dec->qtml );
    msg_Dbg( p_dec->p_fifo, "FreeLibrary ok." );
#endif

#ifdef LOADER
    Restore_LDT_Keeper( p_dec->ldt_fs );
    msg_Dbg( p_dec->p_fifo, "Restore_LDT_Keeper" );
#endif
#ifdef SYS_DARWIN
    ExitMovies();
#endif
}
Gildas Bazin's avatar
 
Gildas Bazin committed
1186
#endif