dmo.c 52.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * dmo.c : DirectMedia Object decoder module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2002, 2003 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Author: Gildas Bazin <gbazin@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32 33
#include <assert.h>

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
36
#include <vlc_codec.h>
Rafaël Carré's avatar
Rafaël Carré committed
37
#include <vlc_codecs.h>
38

39
#ifdef _WIN32
40
#   include <objbase.h>
41
#   include <vlc_charset.h>
42 43
#endif

Clément Stenac's avatar
Clément Stenac committed
44
#include <vlc_codecs.h>
45
#include "dmo.h"
46
#include "../../video_chroma/copy.h"
47

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
48 49 50
#ifndef NDEBUG
# define DMO_DEBUG 1
#endif
51

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
52 53 54 55 56 57
#ifdef UNICODE
# define PRIs "%ls"
#else
# define PRIs "%s"
#endif

58
typedef long (STDCALL *GETCLASS) ( const GUID*, const GUID*, void** );
59

60 61 62 63 64
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  DecoderOpen  ( vlc_object_t * );
static void DecoderClose ( vlc_object_t * );
65
static int DecodeBlock ( decoder_t *, block_t * );
66
static void *DecoderThread( void * );
67 68 69 70
static int  EncoderOpen  ( vlc_object_t * );
static void EncoderClose ( vlc_object_t * );
static block_t *EncodeBlock( encoder_t *, void * );

71 72
static int  EncOpen  ( vlc_object_t * );

73
static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
74
                    es_format_t *, bool );
75
static void CopyPicture( picture_t *, uint8_t * );
76

77 78 79
vlc_module_begin ()
    set_description( N_("DirectMedia Object decoder") )
    add_shortcut( "dmo" )
80
    set_capability( "video decoder", 1 )
81 82
    set_callbacks( DecoderOpen, DecoderClose )
    set_category( CAT_INPUT )
83
    set_subcategory( SUBCAT_INPUT_VCODEC )
84

85 86 87 88 89
    add_submodule()
    add_shortcut("dmo")
    set_capability( "audio decoder", 1 )
    set_callbacks(DecoderOpen, DecoderClose)

90
#   define ENC_CFG_PREFIX "sout-dmo-"
91 92
    add_submodule ()
    set_description( N_("DirectMedia Object encoder") )
93
    add_shortcut( "dmo" )
94 95
    set_capability( "encoder", 10 )
    set_callbacks( EncoderOpen, EncoderClose )
96

97
vlc_module_end ()
98 99 100 101 102 103 104 105

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

/****************************************************************************
 * Decoder descriptor declaration
 ****************************************************************************/
106
typedef struct
107 108 109 110 111 112 113 114 115
{
    HINSTANCE hmsdmo_dll;
    IMediaObject *p_dmo;

    int i_min_output;
    uint8_t *p_buffer;

    date_t end_date;

116 117 118
    vlc_thread_t thread;
    vlc_mutex_t  lock;
    vlc_cond_t   wait_input, wait_output;
Laurent Aimar's avatar
Laurent Aimar committed
119
    bool         b_ready, b_works;
120
    block_t     *p_input;
121
} decoder_sys_t;
122

123 124 125 126 127 128
const GUID IID_IWMCodecPrivateData = {0x73f0be8e, 0x57f7, 0x4f01, {0xaa, 0x66, 0x9f, 0x57, 0x34, 0xc, 0xfe, 0xe}};
const GUID IID_IMediaObject = {0xd8ad0f58, 0x5494, 0x4102, {0x97, 0xc5, 0xec, 0x79, 0x8e, 0x59, 0xbc, 0xf4}};
const GUID IID_IMediaBuffer = {0x59eff8b9, 0x938c, 0x4a26, {0x82, 0xf2, 0x95, 0xcb, 0x84, 0xcd, 0xc8, 0x37}};
const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
129
const GUID MEDIASUBTYPE_IEEE_FLOAT = {0x00000003, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
130 131 132 133 134 135 136 137 138
const GUID FORMAT_VideoInfo = {0x05589f80, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};


139
static const GUID guid_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
140 141 142
static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };

static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
143 144 145
static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };

146
static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
147
static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
148 149

static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
150
static const GUID guid_wmv_enc2 = { 0x96b57cdd, 0x8966, 0x410c,{ 0xbb, 0x1f, 0xc9, 0x7e, 0xea, 0x76, 0x5c, 0x04 } };
151
static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
152 153
static const GUID guid_wmv8_enc = { 0x7e320092, 0x596a, 0x41b2,{ 0xbb, 0xeb, 0x17, 0x5d, 0x10, 0x50, 0x4e, 0xb6 } };
static const GUID guid_wmv9_enc = { 0xd23b90d0, 0x144f, 0x46bd,{ 0x84, 0x1d, 0x59, 0xe4, 0xeb, 0x19, 0xdc, 0x59 } };
154

155 156 157 158
#ifndef BI_RGB
# define BI_RGB 0x0
#endif

159 160 161
typedef struct
{
    vlc_fourcc_t i_fourcc;
162
    const TCHAR  *psz_dll;
163 164 165 166 167 168
    const GUID   *p_guid;

} codec_dll;

static const codec_dll decoders_table[] =
{
169
    /* WVC1 */
170
    { VLC_CODEC_VC1,    TEXT("wvc1dmod.dll"), &guid_wvc1 },
171
    /* WMV3 */
172
    { VLC_CODEC_WMV3,   TEXT("wmv9dmod.dll"), &guid_wmv9 },
173
    /* WMV2 */
174
    { VLC_CODEC_WMV2,   TEXT("wmvdmod.dll"), &guid_wmv },
175
    /* WMV1 */
176
    { VLC_CODEC_WMV1,   TEXT("wmvdmod.dll"), &guid_wmv },
177
    /* Screen codecs */
178 179 180 181
    { VLC_CODEC_MSS2,   TEXT("WMVSDECD.DLL"), &guid_wms },
    { VLC_CODEC_MSS2,   TEXT("wmsdmod.dll"),  &guid_wms },
    { VLC_CODEC_MSS1,   TEXT("WMVSDECD.DLL"), &guid_wms },
    { VLC_CODEC_MSS1,   TEXT("wmsdmod.dll"),  &guid_wms },
182
    /* Windows Media Video Adv */
183
    { VLC_CODEC_WMVA,   TEXT("wmvadvd.dll"), &guid_wmva },
184 185

    /* WMA 3 */
186 187
    { VLC_CODEC_WMAP,   TEXT("wma9dmod.dll"), &guid_wma9 },
    { VLC_CODEC_WMAL,   TEXT("wma9dmod.dll"), &guid_wma9 },
188

189
    /* WMA 2 */
190
    { VLC_CODEC_WMA2,   TEXT("wma9dmod.dll"), &guid_wma9 },
191

192
    /* WMA Speech */
193
    { VLC_CODEC_WMAS,   TEXT("wmspdmod.dll"), &guid_wma },
194

195 196 197 198 199 200
    /* */
    { 0, NULL, NULL }
};

static const codec_dll encoders_table[] =
{
201
    /* WMV3 */
202
    { VLC_CODEC_WMV3, TEXT("wmvdmoe2.dll"), &guid_wmv_enc2 },
203
    /* WMV2 */
204
    { VLC_CODEC_WMV2, TEXT("wmvdmoe2.dll"), &guid_wmv_enc2 },
205
    /* WMV1 */
206
    { VLC_CODEC_WMV1, TEXT("wmvdmoe2.dll"), &guid_wmv_enc2 },
207 208

    /* WMA 3 */
209
    { VLC_CODEC_WMAP, TEXT("wmadmoe.dll"), &guid_wma_enc },
210
    /* WMA 2 */
211
    { VLC_CODEC_WMA2, TEXT("wmadmoe.dll"), &guid_wma_enc },
212

213
    /* WMV3 v11 */
214
    { VLC_CODEC_WMV3, TEXT("wmvencod.dll"), &guid_wmv9_enc },
215
    /* WMV2 v11 */
216
    { VLC_CODEC_WMV2, TEXT("wmvxencd.dll"), &guid_wmv8_enc },
217
    /* WMV1 v11 */
218
    { VLC_CODEC_WMV1, TEXT("wmvxencd.dll"), &guid_wmv8_enc },
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    /* */
    { 0, NULL, NULL }
};

static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
{
    if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
    if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
    mt->cbFormat = 0;
    mt->pbFormat = NULL;
    mt->pUnk = NULL;
}

/*****************************************************************************
 * DecoderOpen: open dmo codec
 *****************************************************************************/
static int DecoderOpen( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;
239
    decoder_sys_t *p_sys;
240 241 242 243 244

    /* We can't open it now, because of ldt_keeper or something
     * Open/Decode/Close has to be done in the same thread */

    /* Probe if we support it */
245
    for( unsigned i = 0; decoders_table[i].i_fourcc != 0; i++ )
246 247 248
    {
        if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
249
            msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll="PRIs,
250 251
                     (char*)&p_dec->fmt_in.i_codec,
                     decoders_table[i].psz_dll );
252
            goto found;
253 254
        }
    }
255
    return VLC_EGENERIC;
256

257 258 259 260
found:
    p_sys = p_dec->p_sys = malloc(sizeof(*p_sys));
    if( !p_sys )
        return VLC_ENOMEM;
261 262

    /* Set callbacks */
263
    p_dec->pf_decode = DecodeBlock;
264

265 266 267
    vlc_mutex_init( &p_sys->lock );
    vlc_cond_init( &p_sys->wait_input );
    vlc_cond_init( &p_sys->wait_output );
Laurent Aimar's avatar
Laurent Aimar committed
268 269
    p_sys->b_works =
    p_sys->b_ready = false;
270
    p_sys->p_input = NULL;
271 272 273 274 275 276

    if( vlc_clone( &p_sys->thread, DecoderThread, p_dec,
                   VLC_THREAD_PRIORITY_INPUT ) )
        goto error;

    vlc_mutex_lock( &p_sys->lock );
Laurent Aimar's avatar
Laurent Aimar committed
277
    while( !p_sys->b_ready )
278 279 280
        vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
    vlc_mutex_unlock( &p_sys->lock );

Laurent Aimar's avatar
Laurent Aimar committed
281
    if( p_sys->b_works )
282 283 284 285 286 287 288 289 290
        return VLC_SUCCESS;

    vlc_join( p_sys->thread, NULL );
error:
    vlc_cond_destroy( &p_sys->wait_input );
    vlc_cond_destroy( &p_sys->wait_output );
    vlc_mutex_destroy( &p_sys->lock );
    free( p_sys );
    return VLC_ENOMEM;
291 292 293
}

/*****************************************************************************
294
 * DecoderClose: close codec
295
 *****************************************************************************/
296
static void DecoderClose( vlc_object_t *p_this )
297 298
{
    decoder_t *p_dec = (decoder_t*)p_this;
299 300 301
    decoder_sys_t *p_sys = p_dec->p_sys;

    vlc_mutex_lock( &p_sys->lock );
Laurent Aimar's avatar
Laurent Aimar committed
302
    p_sys->b_ready = false;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
303
    vlc_cond_signal( &p_sys->wait_input );
304 305 306 307 308 309 310 311 312
    vlc_mutex_unlock( &p_sys->lock );

    vlc_join( p_sys->thread, NULL );
    vlc_cond_destroy( &p_sys->wait_input );
    vlc_cond_destroy( &p_sys->wait_output );
    vlc_mutex_destroy( &p_sys->lock );
    free( p_sys );
}

313
static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
314 315
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
316

317 318
    if( p_block == NULL ) /* No Drain */
        return VLCDEC_SUCCESS;
319

320 321 322 323 324
    vlc_mutex_lock( &p_sys->lock );
    while( p_sys->p_input )
        vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
    p_sys->p_input = p_block;
    vlc_cond_signal( &p_sys->wait_input );
325 326
    vlc_mutex_unlock( &p_sys->lock );

327
    return VLCDEC_SUCCESS;
328 329 330 331 332 333 334 335
}

/*****************************************************************************
 * DecOpen: open dmo codec
 *****************************************************************************/
static int DecOpen( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
336 337 338 339 340 341 342 343 344

    DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
    IMediaObject *p_dmo = NULL;
    HINSTANCE hmsdmo_dll = NULL;

    VIDEOINFOHEADER *p_vih = NULL;
    WAVEFORMATEX *p_wf = NULL;

    /* Initialize OLE/COM */
345
    if( FAILED(CoInitializeEx( NULL, COINIT_MULTITHREADED )) )
346
        vlc_assert_unreachable();
347

348
    if( LoadDMO( VLC_OBJECT(p_dec), &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, false )
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        != VLC_SUCCESS )
    {
        hmsdmo_dll = 0;
        p_dmo = 0;
        goto error;
    }

    /* Setup input format */
    memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
    dmo_input_type.pUnk = 0;

    if( p_dec->fmt_in.i_cat == AUDIO_ES )
    {
        uint16_t i_tag;
        int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
        p_wf = malloc( i_size );

        memset( p_wf, 0, sizeof(WAVEFORMATEX) );
        if( p_dec->fmt_in.i_extra )
            memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );

        dmo_input_type.majortype  = MEDIATYPE_Audio;
        dmo_input_type.subtype    = dmo_input_type.majortype;
372 373
        dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_original_fourcc ?
                                p_dec->fmt_in.i_original_fourcc : p_dec->fmt_in.i_codec;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
        if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;

        p_wf->wFormatTag = dmo_input_type.subtype.Data1;
        p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
        p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
        p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
        p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
        p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
        p_wf->cbSize = p_dec->fmt_in.i_extra;

        dmo_input_type.formattype = FORMAT_WaveFormatEx;
        dmo_input_type.cbFormat   = i_size;
        dmo_input_type.pbFormat   = (char *)p_wf;
        dmo_input_type.bFixedSizeSamples = 1;
        dmo_input_type.bTemporalCompression = 0;
        dmo_input_type.lSampleSize = p_wf->nBlockAlign;
    }
    else
    {
Rafaël Carré's avatar
Rafaël Carré committed
394
        VLC_BITMAPINFOHEADER *p_bih;
395 396 397 398 399 400 401 402 403

        int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
        p_vih = malloc( i_size );

        memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
        if( p_dec->fmt_in.i_extra )
            memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );

        p_bih = &p_vih->bmiHeader;
404 405
        p_bih->biCompression = p_dec->fmt_in.i_original_fourcc ?
                            p_dec->fmt_in.i_original_fourcc : p_dec->fmt_in.i_codec;
406 407 408 409 410
        p_bih->biWidth = p_dec->fmt_in.video.i_width;
        p_bih->biHeight = p_dec->fmt_in.video.i_height;
        p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
        p_bih->biPlanes = 1;
        p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
Rafaël Carré's avatar
Rafaël Carré committed
411
            sizeof(VLC_BITMAPINFOHEADER);
412 413 414 415 416 417 418 419

        p_vih->rcSource.left = p_vih->rcSource.top = 0;
        p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
        p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
        p_vih->rcTarget = p_vih->rcSource;

        dmo_input_type.majortype  = MEDIATYPE_Video;
        dmo_input_type.subtype    = dmo_input_type.majortype;
420 421
        dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_original_fourcc ?
                                p_dec->fmt_in.i_original_fourcc: p_dec->fmt_in.i_codec;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
        dmo_input_type.formattype = FORMAT_VideoInfo;
        dmo_input_type.bFixedSizeSamples = 0;
        dmo_input_type.bTemporalCompression = 1;
        dmo_input_type.cbFormat = i_size;
        dmo_input_type.pbFormat = (char *)p_vih;
    }

    if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
    {
        msg_Err( p_dec, "can't set DMO input type" );
        goto error;
    }
    msg_Dbg( p_dec, "DMO input type set" );

    /* Setup output format */
    memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
    dmo_output_type.pUnk = 0;

    if( p_dec->fmt_in.i_cat == AUDIO_ES )
    {
        /* Setup the format */
443
        p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
444 445 446
        p_dec->fmt_out.audio.i_rate     = p_dec->fmt_in.audio.i_rate;
        p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
        p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
447 448
        if( p_dec->fmt_in.audio.i_channels > 8 )
            goto error;
449
        p_dec->fmt_out.audio.i_physical_channels =
450
            vlc_chan_maps[p_dec->fmt_out.audio.i_channels];
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

        p_wf->wFormatTag = WAVE_FORMAT_PCM;
        p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
        p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
        p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
        p_wf->nBlockAlign =
            p_wf->wBitsPerSample / 8 * p_wf->nChannels;
        p_wf->nAvgBytesPerSec =
            p_wf->nSamplesPerSec * p_wf->nBlockAlign;
        p_wf->cbSize = 0;

        dmo_output_type.majortype  = MEDIATYPE_Audio;
        dmo_output_type.formattype = FORMAT_WaveFormatEx;
        dmo_output_type.subtype    = MEDIASUBTYPE_PCM;
        dmo_output_type.cbFormat   = sizeof(WAVEFORMATEX);
        dmo_output_type.pbFormat   = (char *)p_wf;
        dmo_output_type.bFixedSizeSamples = 1;
        dmo_output_type.bTemporalCompression = 0;
        dmo_output_type.lSampleSize = p_wf->nBlockAlign;
    }
    else
    {
Rafaël Carré's avatar
Rafaël Carré committed
473
        VLC_BITMAPINFOHEADER *p_bih;
474
        DMO_MEDIA_TYPE mt;
475
        unsigned i_chroma = VLC_CODEC_YUYV;
476
        int i_bpp = 16;
477 478 479 480 481
        int i = 0;

        /* Find out which chroma to use */
        while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
        {
482
            if( mt.subtype.Data1 == VLC_CODEC_YV12 )
483 484
            {
                i_chroma = mt.subtype.Data1;
485
                i_bpp = 12;
486 487 488 489 490 491 492 493 494
                DMOFreeMediaType( &mt );
                break;
            }
            else if( (p_dec->fmt_in.i_codec == VLC_CODEC_MSS1 ||
                      p_dec->fmt_in.i_codec == VLC_CODEC_MSS2 ) &&
                      guidcmp( &mt.subtype, &MEDIASUBTYPE_RGB24 ) )
            {
                i_chroma = VLC_CODEC_RGB24;
                i_bpp = 24;
495
            }
496

497 498
            DMOFreeMediaType( &mt );
        }
499

500
        p_dec->fmt_out.i_codec = i_chroma == VLC_CODEC_YV12 ? VLC_CODEC_I420 : i_chroma;
501 502
        p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
        p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
503
        p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
504 505

        /* If an aspect-ratio was specified in the input format then force it */
Laurent Aimar's avatar
Laurent Aimar committed
506 507
        if( p_dec->fmt_in.video.i_sar_num > 0 &&
            p_dec->fmt_in.video.i_sar_den > 0 )
508
        {
Laurent Aimar's avatar
Laurent Aimar committed
509 510
            p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
            p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
511 512 513
        }
        else
        {
Laurent Aimar's avatar
Laurent Aimar committed
514 515
            p_dec->fmt_out.video.i_sar_num = 1;
            p_dec->fmt_out.video.i_sar_den = 1;
516
        }
517 518

        p_bih = &p_vih->bmiHeader;
519
        p_bih->biCompression = i_chroma == VLC_CODEC_RGB24 ? BI_RGB : i_chroma;
520 521 522 523 524 525
        p_bih->biHeight *= -1;
        p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
        p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
            p_dec->fmt_in.video.i_height *
            (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;

526
        p_bih->biPlanes = 1; /* http://msdn.microsoft.com/en-us/library/dd183376%28v=vs.85%29.aspx */
Rafaël Carré's avatar
Rafaël Carré committed
527
        p_bih->biSize = sizeof(VLC_BITMAPINFOHEADER);
528 529

        dmo_output_type.majortype = MEDIATYPE_Video;
530
        dmo_output_type.formattype = FORMAT_VideoInfo;
531 532 533 534
        if( i_chroma == VLC_CODEC_RGB24 )
        {
            dmo_output_type.subtype = MEDIASUBTYPE_RGB24;
        }
535
        else
536 537 538 539
        {
            dmo_output_type.subtype = dmo_output_type.majortype;
            dmo_output_type.subtype.Data1 = p_bih->biCompression;
        }
540
        dmo_output_type.bFixedSizeSamples = true;
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
        dmo_output_type.bTemporalCompression = 0;
        dmo_output_type.lSampleSize = p_bih->biSizeImage;
        dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
        dmo_output_type.pbFormat = (char *)p_vih;
    }

#ifdef DMO_DEBUG
    /* Enumerate output types */
    if( p_dec->fmt_in.i_cat == VIDEO_ES )
    {
        int i = 0;
        DMO_MEDIA_TYPE mt;

        while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
        {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
556
            msg_Dbg( p_dec, "available output chroma: %4.4s", (char *)&mt.subtype.Data1 );
557 558 559 560 561
            DMOFreeMediaType( &mt );
        }
    }
#endif

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
562 563
    unsigned i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 );
    if( i_err )
564
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
565
        msg_Err( p_dec, "can't set DMO output type for decoder: 0x%x", i_err );
566 567
        goto error;
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
568
    msg_Dbg( p_dec, "DMO output type set for decoder" );
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

    /* Allocate the memory needed to store the decoder's structure */
    p_sys->hmsdmo_dll = hmsdmo_dll;
    p_sys->p_dmo = p_dmo;

    /* Find out some properties of the output */
    {
        uint32_t i_size, i_align;

        p_sys->i_min_output = 0;
        if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
        {
            msg_Err( p_dec, "GetOutputSizeInfo() failed" );
            goto error;
        }
        else
        {
            msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
                     i_size, i_align );
            p_sys->i_min_output = i_size;
            p_sys->p_buffer = malloc( i_size );
            if( !p_sys->p_buffer ) goto error;
        }
    }

    /* Set output properties */
    p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
    if( p_dec->fmt_out.i_cat == AUDIO_ES )
        date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
    else
        date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
600
    date_Set( &p_sys->end_date, VLC_TS_0 );
601

602 603
    free( p_vih );
    free( p_wf );
604

605
    vlc_mutex_lock( &p_sys->lock );
Laurent Aimar's avatar
Laurent Aimar committed
606 607
    p_sys->b_ready =
    p_sys->b_works = true;
608 609 610
    vlc_cond_signal( &p_sys->wait_output );
    vlc_mutex_unlock( &p_sys->lock );

611 612 613 614 615 616 617 618 619 620
    return VLC_SUCCESS;

 error:

    if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
    if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );

    /* Uninitialize OLE/COM */
    CoUninitialize();

621 622
    free( p_vih );
    free( p_wf );
623

624
    vlc_mutex_lock( &p_sys->lock );
Laurent Aimar's avatar
Laurent Aimar committed
625
    p_sys->b_ready = true;
626 627
    vlc_cond_signal( &p_sys->wait_output );
    vlc_mutex_unlock( &p_sys->lock );
628 629 630 631 632 633 634 635
    return VLC_EGENERIC;
}

/*****************************************************************************
 * LoadDMO: Load the DMO object
 *****************************************************************************/
static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
                    IMediaObject **pp_dmo, es_format_t *p_fmt,
636
                    bool b_out )
637 638 639 640
{
    DMO_PARTIAL_MEDIATYPE dmo_partial_type;
    int i_err;

641 642 643 644 645
    long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
                               const DMO_PARTIAL_MEDIATYPE *,
                               uint32_t, const DMO_PARTIAL_MEDIATYPE *,
                               IEnumDMO ** );

646 647 648
    IEnumDMO *p_enum_dmo = NULL;
    WCHAR *psz_dmo_name;
    GUID clsid_dmo;
649
    uint32_t i_dummy;
650 651 652 653

    GETCLASS GetClass;
    IClassFactory *cFactory = NULL;
    IUnknown *cObject = NULL;
654
    const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
655 656 657 658 659 660 661 662
    int i_codec;

    /* Look for a DMO which can handle the requested codec */
    if( p_fmt->i_cat == AUDIO_ES )
    {
        uint16_t i_tag;
        dmo_partial_type.type = MEDIATYPE_Audio;
        dmo_partial_type.subtype = dmo_partial_type.type;
663 664
        dmo_partial_type.subtype.Data1 = p_fmt->i_original_fourcc ?
                                        p_fmt->i_original_fourcc : p_fmt->i_codec;
665 666 667 668 669 670 671
        fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
        if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
    }
    else
    {
        dmo_partial_type.type = MEDIATYPE_Video;
        dmo_partial_type.subtype = dmo_partial_type.type;
672 673
        dmo_partial_type.subtype.Data1 = p_fmt->i_original_fourcc ?
                                        p_fmt->i_original_fourcc : p_fmt->i_codec;
674 675 676
    }

    /* Load msdmo DLL */
677
    *p_hmsdmo_dll = LoadLibrary( TEXT( "msdmo.dll" ) );
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
    if( *p_hmsdmo_dll == NULL )
    {
        msg_Dbg( p_this, "failed loading msdmo.dll" );
        return VLC_EGENERIC;
    }
    OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
    if( OurDMOEnum == NULL )
    {
        msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
        FreeLibrary( *p_hmsdmo_dll );
        return VLC_EGENERIC;
    }

    if( !b_out )
    {
        i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
                            1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
    }
    else
    {
        i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
699
                            0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
700 701 702 703
    }
    if( i_err )
    {
        FreeLibrary( *p_hmsdmo_dll );
704 705 706
        /* return VLC_EGENERIC; */
        /* Try loading the dll directly */
        goto loader;
707 708 709
    }

    /* Pickup the first available codec */
710 711 712
    *pp_dmo = 0;
    while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
                     &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
713
    {
714
        msg_Dbg( p_this, "found DMO: %ls", psz_dmo_name );
715 716 717 718 719 720
        CoTaskMemFree( psz_dmo_name );

        /* Create DMO */
        if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
                              &IID_IMediaObject, (void **)pp_dmo ) )
        {
721
            msg_Warn( p_this, "can't create DMO" );
722 723
            *pp_dmo = 0;
        }
724 725
        else
            break;
726 727
    }

728
    p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
729

730
    if( !*pp_dmo )
731 732
    {
        FreeLibrary( *p_hmsdmo_dll );
733 734 735
        /* return VLC_EGENERIC; */
        /* Try loading the dll directly */
        goto loader;
736 737
    }

738
    return VLC_SUCCESS;
739 740

loader:
741

742 743 744 745 746 747 748 749
    for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
    {
        if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
            break;
    }
    if( codecs_table[i_codec].i_fourcc == 0 )
        return VLC_EGENERIC;    /* Can't happen */

750
    *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
751 752
    if( *p_hmsdmo_dll == NULL )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
753
        msg_Dbg( p_this, "failed loading '"PRIs"'",
754 755 756 757 758 759 760 761 762 763 764 765 766
                 codecs_table[i_codec].psz_dll );
        return VLC_EGENERIC;
    }

    GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
    if (!GetClass)
    {
        msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
        FreeLibrary( *p_hmsdmo_dll );
        return VLC_EGENERIC;
    }

    i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
767 768
                      (void**)&cFactory );

769 770 771 772 773 774 775 776
    if( i_err || cFactory == NULL )
    {
        msg_Dbg( p_this, "no such class object" );
        FreeLibrary( *p_hmsdmo_dll );
        return VLC_EGENERIC;
    }

    i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
777
                                          (void**)&cObject );
778 779 780 781 782 783 784 785
    cFactory->vt->Release( (IUnknown*)cFactory );
    if( i_err || !cObject )
    {
        msg_Dbg( p_this, "class factory failure" );
        FreeLibrary( *p_hmsdmo_dll );
        return VLC_EGENERIC;
    }
    i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
786
                                        (void**)pp_dmo );
787 788 789 790 791 792 793 794 795 796 797
    cObject->vt->Release( (IUnknown*)cObject );
    if( i_err || !*pp_dmo )
    {
        msg_Dbg( p_this, "QueryInterface failure" );
        FreeLibrary( *p_hmsdmo_dll );
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}

798
static void DecClose( decoder_t *p_dec )
799 800 801
{
    decoder_sys_t *p_sys = p_dec->p_sys;

802
    if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
803 804 805 806
    FreeLibrary( p_sys->hmsdmo_dll );

    CoUninitialize();

807
    free( p_sys->p_buffer );
808 809 810 811 812
}

/****************************************************************************
 * DecodeBlock: the whole thing
 ****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
813
 * This function must be fed with packets.
814
 ****************************************************************************/
815
static int DecBlock( decoder_t *p_dec, block_t **pp_block )
816 817 818 819 820 821 822 823 824 825 826 827 828
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_block;
    int i_result;

    DMO_OUTPUT_DATA_BUFFER db;
    CMediaBuffer *p_out;
    block_t block_out;
    uint32_t i_status;

    p_block = *pp_block;

    /* Won't work with streams with B-frames, but do we have any ? */
829
    if( p_block && p_block->i_pts == VLC_TS_INVALID )
830
        p_block->i_pts = p_block->i_dts;
831 832

    /* Date management */
833
    if( p_block && p_block->i_pts != VLC_TS_INVALID &&
834 835 836 837 838 839
        p_block->i_pts != date_Get( &p_sys->end_date ) )
    {
        date_Set( &p_sys->end_date, p_block->i_pts );
    }

#if 0 /* Breaks the video decoding */
840
    if( date_Get( &p_sys->end_date ) == VLC_TS_INVALID )
841 842 843
    {
        /* We've just started the stream, wait for the first PTS. */
        if( p_block ) block_Release( p_block );
844
        return -1;
845 846 847 848 849 850 851 852
    }
#endif

    /* Feed input to the DMO */
    if( p_block && p_block->i_buffer )
    {
        CMediaBuffer *p_in;

853
        p_in = CMediaBufferCreate( p_block, p_block->i_buffer, true );
854 855

        i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
856
                       (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
857 858 859 860 861 862 863 864 865 866
                       0, 0 );

        p_in->vt->Release( (IUnknown *)p_in );

        if( i_result == S_FALSE )
        {
            /* No output generated */
#ifdef DMO_DEBUG
            msg_Dbg( p_dec, "ProcessInput(): no output generated" );
#endif
867
            return -1;
868
        }
869
        else if( i_result == (int)DMO_E_NOTACCEPTING )
870 871 872 873 874 875 876
        {
            /* Need to call ProcessOutput */
            msg_Dbg( p_dec, "ProcessInput(): not accepting" );
        }
        else if( i_result != S_OK )
        {
            msg_Dbg( p_dec, "ProcessInput(): failed" );
877
            return -1;
878 879 880
        }
        else
        {
881 882 883
#ifdef DMO_DEBUG
            msg_Dbg( p_dec, "ProcessInput(): successful" );
#endif
884
            block_Release( p_block );
Laurent Aimar's avatar
Laurent Aimar committed
885
            *pp_block = NULL;
886 887 888 889 890 891 892
        }
    }

    /* Get output from the DMO */
    block_out.p_buffer = p_sys->p_buffer;
    block_out.i_buffer = 0;

893
    p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, false );
894 895 896 897 898 899 900 901 902 903 904
    memset( &db, 0, sizeof(db) );
    db.pBuffer = (IMediaBuffer *)p_out;

    i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
                   DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
                   1, &db, &i_status );

    if( i_result != S_OK )
    {
        if( i_result != S_FALSE )
            msg_Dbg( p_dec, "ProcessOutput(): failed" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
905
#ifdef DMO_DEBUG
906 907 908 909 910
        else
            msg_Dbg( p_dec, "ProcessOutput(): no output" );
#endif

        p_out->vt->Release( (IUnknown *)p_out );
911
        return -1;
912 913
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
914
#ifdef DMO_DEBUG
915 916 917 918 919
    msg_Dbg( p_dec, "ProcessOutput(): success" );
#endif

    if( !block_out.i_buffer )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
920
#ifdef DMO_DEBUG
921 922 923
        msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
#endif
        p_out->vt->Release( (IUnknown *)p_out );
924
        return -1;
925 926 927 928 929
    }

    if( p_dec->fmt_out.i_cat == VIDEO_ES )
    {
        /* Get a new picture */
930
        if( decoder_UpdateVideoFormat( p_dec ) )
931
            return -1;
932
        picture_t *p_pic = decoder_NewPicture( p_dec );
933
        if( !p_pic ) return -1;
934

935
        CopyPicture( p_pic, block_out.p_buffer );
936 937 938 939 940 941 942

        /* Date management */
        p_pic->date = date_Get( &p_sys->end_date );
        date_Increment( &p_sys->end_date, 1 );

        p_out->vt->Release( (IUnknown *)p_out );

943 944
        decoder_QueueVideo( p_dec, p_pic );
        return 0;
945 946 947
    }
    else
    {
948 949 950
        if( decoder_UpdateAudioFormat( p_dec ) )
        {
            p_out->vt->Release( (IUnknown *)p_out );
951
            return -1;
952 953
        }

954
        block_t *p_aout_buffer;
955 956 957 958
        int i_samples = block_out.i_buffer /
            ( p_dec->fmt_out.audio.i_bitspersample *
              p_dec->fmt_out.audio.i_channels / 8 );

959 960 961 962 963 964
        p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
        if( p_aout_buffer )
        {
            memcpy( p_aout_buffer->p_buffer,
                    block_out.p_buffer, block_out.i_buffer );
            /* Date management */
965
            p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
966 967 968
            p_aout_buffer->i_length =
                date_Increment( &p_sys->end_date, i_samples )
                - p_aout_buffer->i_pts;
969
        }
970 971
        p_out->vt->Release( (IUnknown *)p_out );

972 973
        decoder_QueueAudio( p_dec, p_aout_buffer );
        return 0;
974 975 976
    }
}

977
static void CopyPicture( picture_t *p_pic, uint8_t *p_in )
978 979 980 981
{
    int i_plane, i_line, i_width, i_dst_stride;
    uint8_t *p_dst, *p_src = p_in;

982
    picture_SwapUV( p_pic );
983 984 985 986 987 988 989

    for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
    {
        p_dst = p_pic->p[i_plane].p_pixels;
        i_width = p_pic->p[i_plane].i_visible_pitch;
        i_dst_stride  = p_pic->p[i_plane].i_pitch;

990
        for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
991
        {
Rafaël Carré's avatar
Rafaël Carré committed
992
            memcpy( p_dst, p_src, i_width );
993 994 995 996 997
            p_src += i_width;
            p_dst += i_dst_stride;
        }
    }

998
    picture_SwapUV( p_pic );
999 1000
}

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
static void *DecoderThread( void *data )
{
    decoder_t *p_dec = data;
    decoder_sys_t *p_sys = p_dec->p_sys;

    if( DecOpen( p_dec ) )
        return NULL; /* failed */

    vlc_mutex_lock( &p_sys->lock );
    for( ;; )
    {
1012
        while( p_sys->b_ready && !p_sys->p_input )
1013
            vlc_cond_wait( &p_sys->wait_input, &p_sys->lock );
Laurent Aimar's avatar
Laurent Aimar committed
1014
        if( !p_sys->b_ready )
1015 1016
            break;

1017 1018 1019 1020 1021 1022
        while( DecBlock( p_dec, &p_sys->p_input ) == 0 );

        if( p_sys->p_input != NULL )
            block_Release( p_sys->p_input );
        p_sys->p_input = NULL;

Laurent Aimar's avatar
Laurent Aimar committed
1023