]> git.sesse.net Git - vlc/blob - modules/codec/dmo/dmo.c
236015f86be24ba8dc560c2abe648a88238579e1
[vlc] / modules / codec / dmo / dmo.c
1 /*****************************************************************************
2  * dmo.c : DirectMedia Object decoder module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 the VideoLAN team
5  * $Id$
6  *
7  * Author: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_vout.h>
36 #include <vlc_aout.h>
37
38 #ifndef WIN32
39 #    define LOADER
40 #else
41 #   include <objbase.h>
42 #endif
43
44 #ifdef LOADER
45 /* Need the w32dll loader from mplayer */
46 #   include <wine/winerror.h>
47 #   include <ldt_keeper.h>
48 #   include <wine/windef.h>
49 #endif
50
51 #include <vlc_codecs.h>
52 #include "dmo.h"
53
54 //#define DMO_DEBUG 1
55
56 #ifdef LOADER
57 /* Not Needed */
58 long CoInitialize( void *pvReserved ) { VLC_UNUSED(pvReserved); return -1; }
59 void CoUninitialize( void ) { }
60
61 /* A few prototypes */
62 HMODULE WINAPI LoadLibraryA(LPCSTR);
63 #define LoadLibrary LoadLibraryA
64 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
65 int     WINAPI FreeLibrary(HMODULE);
66 #endif /* LOADER */
67
68 typedef long (STDCALL *GETCLASS) ( const GUID*, const GUID*, void** );
69
70 static const int pi_channels_maps[7] =
71 {
72     0,
73     AOUT_CHAN_CENTER,
74     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
75     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
76     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
77      | AOUT_CHAN_REARRIGHT,
78     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
79      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
80     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
81      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
82 };
83
84 /*****************************************************************************
85  * Module descriptor
86  *****************************************************************************/
87 static int  DecoderOpen  ( vlc_object_t * );
88 static void DecoderClose ( vlc_object_t * );
89 static void *DecodeBlock ( decoder_t *, block_t ** );
90 static void *DecoderThread( void * );
91 static int  EncoderOpen  ( vlc_object_t * );
92 static void EncoderClose ( vlc_object_t * );
93 static block_t *EncodeBlock( encoder_t *, void * );
94
95 static int  EncOpen  ( vlc_object_t * );
96
97 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
98                     es_format_t *, bool );
99 static void CopyPicture( picture_t *, uint8_t * );
100
101 vlc_module_begin ()
102     set_description( N_("DirectMedia Object decoder") )
103     add_shortcut( "dmo" )
104     set_capability( "decoder", 1 )
105     set_callbacks( DecoderOpen, DecoderClose )
106     set_category( CAT_INPUT )
107     set_subcategory( SUBCAT_INPUT_SCODEC )
108
109 #   define ENC_CFG_PREFIX "sout-dmo-"
110     add_submodule ()
111     set_description( N_("DirectMedia Object encoder") )
112     add_shortcut( "dmo" )
113     set_capability( "encoder", 10 )
114     set_callbacks( EncoderOpen, EncoderClose )
115
116 vlc_module_end ()
117
118 /*****************************************************************************
119  * Local prototypes
120  *****************************************************************************/
121
122 /****************************************************************************
123  * Decoder descriptor declaration
124  ****************************************************************************/
125 struct decoder_sys_t
126 {
127     HINSTANCE hmsdmo_dll;
128     IMediaObject *p_dmo;
129
130     int i_min_output;
131     uint8_t *p_buffer;
132
133     date_t end_date;
134
135 #ifdef LOADER
136     ldt_fs_t    *ldt_fs;
137 #endif
138
139     vlc_thread_t thread;
140     vlc_mutex_t  lock;
141     vlc_cond_t   wait_input, wait_output;
142     bool         b_ready, b_works;
143     block_t    **pp_input;
144     void        *p_output;
145 };
146
147 const GUID IID_IWMCodecPrivateData = {0x73f0be8e, 0x57f7, 0x4f01, {0xaa, 0x66, 0x9f, 0x57, 0x34, 0xc, 0xfe, 0xe}};
148 const GUID IID_IMediaObject = {0xd8ad0f58, 0x5494, 0x4102, {0x97, 0xc5, 0xec, 0x79, 0x8e, 0x59, 0xbc, 0xf4}};
149 const GUID IID_IMediaBuffer = {0x59eff8b9, 0x938c, 0x4a26, {0x82, 0xf2, 0x95, 0xcb, 0x84, 0xcd, 0xc8, 0x37}};
150 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
151 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
152 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
153 const GUID FORMAT_VideoInfo = {0x05589f80, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
154 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
155 const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
156 const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
157 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
158 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
159 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
160
161
162 static const GUID guid_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
163 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
164
165 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
166 static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
167 static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };
168
169 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
170 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
171
172 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
173 static const GUID guid_wmv_enc2 = { 0x96b57cdd, 0x8966, 0x410c,{ 0xbb, 0x1f, 0xc9, 0x7e, 0xea, 0x76, 0x5c, 0x04 } };
174 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
175
176 typedef struct
177 {
178     vlc_fourcc_t i_fourcc;
179     const char   *psz_dll;
180     const GUID   *p_guid;
181
182 } codec_dll;
183
184 static const codec_dll decoders_table[] =
185 {
186     /* WVC1 */
187     { VLC_FOURCC('W','V','C','1'), "wvc1dmod.dll", &guid_wvc1 },
188     { VLC_FOURCC('w','v','c','1'), "wvc1dmod.dll", &guid_wvc1 },
189     /* WMV3 */
190     { VLC_CODEC_WMV3, "wmv9dmod.dll", &guid_wmv9 },
191     { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
192     { VLC_FOURCC('W','M','V','P'), "wmv9dmod.dll", &guid_wmv9 },
193     { VLC_FOURCC('w','m','v','p'), "wmv9dmod.dll", &guid_wmv9 },
194     /* WMV2 */
195     { VLC_CODEC_WMV2, "wmvdmod.dll", &guid_wmv },
196     { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
197     /* WMV1 */
198     { VLC_CODEC_WMV1, "wmvdmod.dll", &guid_wmv },
199     { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
200     /* Screen codecs */
201     { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
202     { VLC_FOURCC('m','s','s','2'), "wmsdmod.dll", &guid_wms },
203     { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
204     { VLC_FOURCC('m','s','s','1'), "wmsdmod.dll", &guid_wms },
205     /* Windows Media Video Adv */
206     { VLC_FOURCC('W','M','V','A'), "wmvadvd.dll", &guid_wmva },
207     { VLC_FOURCC('w','m','v','a'), "wmvadvd.dll", &guid_wmva },
208     { VLC_FOURCC('W','V','P','2'), "wmvadvd.dll", &guid_wmva },
209     { VLC_FOURCC('w','v','p','2'), "wmvadvd.dll", &guid_wmva },
210
211     /* WMA 3 */
212     { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
213     { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
214     { VLC_CODEC_WMAP, "wma9dmod.dll", &guid_wma9 },
215     { VLC_FOURCC('w','m','a','p'), "wma9dmod.dll", &guid_wma9 },
216     /* WMA 2 */
217     { VLC_CODEC_WMA2, "wma9dmod.dll", &guid_wma9 },
218     { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
219
220     /* WMA Speech */
221     { VLC_CODEC_WMAS, "wmspdmod.dll", &guid_wma },
222     { VLC_FOURCC('w','m','a','s'), "wmspdmod.dll", &guid_wma },
223
224     /* */
225     { 0, NULL, NULL }
226 };
227
228 static const codec_dll encoders_table[] =
229 {
230     /* WMV3 */
231     { VLC_CODEC_WMV3, "wmvdmoe2.dll", &guid_wmv_enc2 },
232     { VLC_FOURCC('w','m','v','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
233     /* WMV2 */
234     { VLC_CODEC_WMV2, "wmvdmoe2.dll", &guid_wmv_enc2 },
235     { VLC_FOURCC('w','m','v','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
236     /* WMV1 */
237     { VLC_CODEC_WMV1, "wmvdmoe2.dll", &guid_wmv_enc2 },
238     { VLC_FOURCC('w','m','v','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
239
240     /* WMA 3 */
241     { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
242     { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
243     /* WMA 2 */
244     { VLC_CODEC_WMA2, "wmadmoe.dll", &guid_wma_enc },
245     { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
246
247     /* */
248     { 0, NULL, NULL }
249 };
250
251 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
252 {
253     if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
254     if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
255     mt->cbFormat = 0;
256     mt->pbFormat = NULL;
257     mt->pUnk = NULL;
258 }
259
260 /*****************************************************************************
261  * DecoderOpen: open dmo codec
262  *****************************************************************************/
263 static int DecoderOpen( vlc_object_t *p_this )
264 {
265     decoder_t *p_dec = (decoder_t*)p_this;
266     decoder_sys_t *p_sys;
267
268     /* We can't open it now, because of ldt_keeper or something
269      * Open/Decode/Close has to be done in the same thread */
270
271     /* Probe if we support it */
272     for( unsigned i = 0; decoders_table[i].i_fourcc != 0; i++ )
273     {
274         if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
275         {
276             msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
277                      (char*)&p_dec->fmt_in.i_codec,
278                      decoders_table[i].psz_dll );
279             goto found;
280         }
281     }
282     return VLC_EGENERIC;
283
284 found:
285     p_sys = p_dec->p_sys = malloc(sizeof(*p_sys));
286     if( !p_sys )
287         return VLC_ENOMEM;
288
289     /* Set callbacks */
290     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
291         DecodeBlock;
292     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
293         DecodeBlock;
294
295     vlc_mutex_init( &p_sys->lock );
296     vlc_cond_init( &p_sys->wait_input );
297     vlc_cond_init( &p_sys->wait_output );
298     p_sys->b_works =
299     p_sys->b_ready = false;
300     p_sys->pp_input = NULL;
301     p_sys->p_output = NULL;
302
303     if( vlc_clone( &p_sys->thread, DecoderThread, p_dec,
304                    VLC_THREAD_PRIORITY_INPUT ) )
305         goto error;
306
307     vlc_mutex_lock( &p_sys->lock );
308     while( !p_sys->b_ready )
309         vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
310     vlc_mutex_unlock( &p_sys->lock );
311
312     if( p_sys->b_works )
313         return VLC_SUCCESS;
314
315     vlc_join( p_sys->thread, NULL );
316 error:
317     vlc_cond_destroy( &p_sys->wait_input );
318     vlc_cond_destroy( &p_sys->wait_output );
319     vlc_mutex_destroy( &p_sys->lock );
320     free( p_sys );
321     return VLC_ENOMEM;
322 }
323
324 /*****************************************************************************
325  * DecoderClose: close codec
326  *****************************************************************************/
327 static void DecoderClose( vlc_object_t *p_this )
328 {
329     decoder_t *p_dec = (decoder_t*)p_this;
330     decoder_sys_t *p_sys = p_dec->p_sys;
331
332     vlc_mutex_lock( &p_sys->lock );
333     p_sys->b_ready = false;
334     vlc_cond_signal( &p_sys->wait_input );
335     vlc_mutex_unlock( &p_sys->lock );
336
337     vlc_join( p_sys->thread, NULL );
338     vlc_cond_destroy( &p_sys->wait_input );
339     vlc_cond_destroy( &p_sys->wait_output );
340     vlc_mutex_destroy( &p_sys->lock );
341     free( p_sys );
342 }
343
344 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
345 {
346     decoder_sys_t *p_sys = p_dec->p_sys;
347     void *p_ret;
348
349     vlc_mutex_lock( &p_sys->lock );
350     p_sys->pp_input = pp_block;
351     vlc_cond_signal( &p_sys->wait_input );
352
353     while( p_sys->pp_input )
354         vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
355     p_ret = p_sys->p_output;
356     vlc_mutex_unlock( &p_sys->lock );
357
358     return p_ret;
359 }
360
361 /*****************************************************************************
362  * DecOpen: open dmo codec
363  *****************************************************************************/
364 static int DecOpen( decoder_t *p_dec )
365 {
366     decoder_sys_t *p_sys = p_dec->p_sys;
367
368     DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
369     IMediaObject *p_dmo = NULL;
370     HINSTANCE hmsdmo_dll = NULL;
371
372     VIDEOINFOHEADER *p_vih = NULL;
373     WAVEFORMATEX *p_wf = NULL;
374
375 #ifdef LOADER
376     ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
377 #else
378     /* Initialize OLE/COM */
379     CoInitialize( 0 );
380 #endif /* LOADER */
381
382     if( LoadDMO( VLC_OBJECT(p_dec), &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, false )
383         != VLC_SUCCESS )
384     {
385         hmsdmo_dll = 0;
386         p_dmo = 0;
387         goto error;
388     }
389
390     /* Setup input format */
391     memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
392     dmo_input_type.pUnk = 0;
393
394     if( p_dec->fmt_in.i_cat == AUDIO_ES )
395     {
396         uint16_t i_tag;
397         int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
398         p_wf = malloc( i_size );
399
400         memset( p_wf, 0, sizeof(WAVEFORMATEX) );
401         if( p_dec->fmt_in.i_extra )
402             memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
403
404         dmo_input_type.majortype  = MEDIATYPE_Audio;
405         dmo_input_type.subtype    = dmo_input_type.majortype;
406         dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
407         fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
408         if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
409
410         p_wf->wFormatTag = dmo_input_type.subtype.Data1;
411         p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
412         p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
413         p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
414         p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
415         p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
416         p_wf->cbSize = p_dec->fmt_in.i_extra;
417
418         dmo_input_type.formattype = FORMAT_WaveFormatEx;
419         dmo_input_type.cbFormat   = i_size;
420         dmo_input_type.pbFormat   = (char *)p_wf;
421         dmo_input_type.bFixedSizeSamples = 1;
422         dmo_input_type.bTemporalCompression = 0;
423         dmo_input_type.lSampleSize = p_wf->nBlockAlign;
424     }
425     else
426     {
427         BITMAPINFOHEADER *p_bih;
428
429         int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
430         p_vih = malloc( i_size );
431
432         memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
433         if( p_dec->fmt_in.i_extra )
434             memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
435
436         p_bih = &p_vih->bmiHeader;
437         p_bih->biCompression = p_dec->fmt_in.i_codec;
438         p_bih->biWidth = p_dec->fmt_in.video.i_width;
439         p_bih->biHeight = p_dec->fmt_in.video.i_height;
440         p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
441         p_bih->biPlanes = 1;
442         p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
443             sizeof(BITMAPINFOHEADER);
444
445         p_vih->rcSource.left = p_vih->rcSource.top = 0;
446         p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
447         p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
448         p_vih->rcTarget = p_vih->rcSource;
449
450         dmo_input_type.majortype  = MEDIATYPE_Video;
451         dmo_input_type.subtype    = dmo_input_type.majortype;
452         dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
453         dmo_input_type.formattype = FORMAT_VideoInfo;
454         dmo_input_type.bFixedSizeSamples = 0;
455         dmo_input_type.bTemporalCompression = 1;
456         dmo_input_type.cbFormat = i_size;
457         dmo_input_type.pbFormat = (char *)p_vih;
458     }
459
460     if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
461     {
462         msg_Err( p_dec, "can't set DMO input type" );
463         goto error;
464     }
465     msg_Dbg( p_dec, "DMO input type set" );
466
467     /* Setup output format */
468     memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
469     dmo_output_type.pUnk = 0;
470
471     if( p_dec->fmt_in.i_cat == AUDIO_ES )
472     {
473         /* Setup the format */
474         p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
475         p_dec->fmt_out.audio.i_rate     = p_dec->fmt_in.audio.i_rate;
476         p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
477         p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
478         p_dec->fmt_out.audio.i_physical_channels =
479             p_dec->fmt_out.audio.i_original_channels =
480                 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
481
482         p_wf->wFormatTag = WAVE_FORMAT_PCM;
483         p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
484         p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
485         p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
486         p_wf->nBlockAlign =
487             p_wf->wBitsPerSample / 8 * p_wf->nChannels;
488         p_wf->nAvgBytesPerSec =
489             p_wf->nSamplesPerSec * p_wf->nBlockAlign;
490         p_wf->cbSize = 0;
491
492         dmo_output_type.majortype  = MEDIATYPE_Audio;
493         dmo_output_type.formattype = FORMAT_WaveFormatEx;
494         dmo_output_type.subtype    = MEDIASUBTYPE_PCM;
495         dmo_output_type.cbFormat   = sizeof(WAVEFORMATEX);
496         dmo_output_type.pbFormat   = (char *)p_wf;
497         dmo_output_type.bFixedSizeSamples = 1;
498         dmo_output_type.bTemporalCompression = 0;
499         dmo_output_type.lSampleSize = p_wf->nBlockAlign;
500     }
501     else
502     {
503         BITMAPINFOHEADER *p_bih;
504         DMO_MEDIA_TYPE mt;
505         unsigned i_chroma = VLC_CODEC_YUYV;
506         int i_planes = 1, i_bpp = 16;
507         int i = 0;
508
509         /* Find out which chroma to use */
510         while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
511         {
512             if( mt.subtype.Data1 == VLC_CODEC_YV12 )
513             {
514                 i_chroma = mt.subtype.Data1;
515                 i_planes = 3; i_bpp = 12;
516             }
517
518             DMOFreeMediaType( &mt );
519         }
520
521         p_dec->fmt_out.i_codec = i_chroma == VLC_CODEC_YV12 ?
522             VLC_CODEC_I420 : i_chroma;
523         p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
524         p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
525         p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
526
527         /* If an aspect-ratio was specified in the input format then force it */
528         if( p_dec->fmt_in.video.i_aspect )
529         {
530             p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
531         }
532         else
533         {
534             p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
535                 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
536         }
537
538         p_bih = &p_vih->bmiHeader;
539         p_bih->biCompression = i_chroma;
540         p_bih->biHeight *= -1;
541         p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
542         p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
543             p_dec->fmt_in.video.i_height *
544             (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
545
546         p_bih->biPlanes = i_planes;
547         p_bih->biSize = sizeof(BITMAPINFOHEADER);
548
549         dmo_output_type.majortype = MEDIATYPE_Video;
550         dmo_output_type.formattype = FORMAT_VideoInfo;
551         dmo_output_type.subtype = dmo_output_type.majortype;
552         dmo_output_type.subtype.Data1 = p_bih->biCompression;
553         dmo_output_type.bFixedSizeSamples = true;
554         dmo_output_type.bTemporalCompression = 0;
555         dmo_output_type.lSampleSize = p_bih->biSizeImage;
556         dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
557         dmo_output_type.pbFormat = (char *)p_vih;
558     }
559
560 #ifdef DMO_DEBUG
561     /* Enumerate output types */
562     if( p_dec->fmt_in.i_cat == VIDEO_ES )
563     {
564         int i = 0;
565         DMO_MEDIA_TYPE mt;
566
567         while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
568         {
569             msg_Dbg( p_dec, "available output chroma: %4.4s",
570                      (char *)&mt.subtype.Data1 );
571             DMOFreeMediaType( &mt );
572         }
573     }
574 #endif
575
576     if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
577     {
578         msg_Err( p_dec, "can't set DMO output type" );
579         goto error;
580     }
581     msg_Dbg( p_dec, "DMO output type set" );
582
583     /* Allocate the memory needed to store the decoder's structure */
584     p_sys->hmsdmo_dll = hmsdmo_dll;
585     p_sys->p_dmo = p_dmo;
586 #ifdef LOADER
587     p_sys->ldt_fs = ldt_fs;
588 #endif
589
590     /* Find out some properties of the output */
591     {
592         uint32_t i_size, i_align;
593
594         p_sys->i_min_output = 0;
595         if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
596         {
597             msg_Err( p_dec, "GetOutputSizeInfo() failed" );
598             goto error;
599         }
600         else
601         {
602             msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
603                      i_size, i_align );
604             p_sys->i_min_output = i_size;
605             p_sys->p_buffer = malloc( i_size );
606             if( !p_sys->p_buffer ) goto error;
607         }
608     }
609
610     /* Set output properties */
611     p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
612     if( p_dec->fmt_out.i_cat == AUDIO_ES )
613         date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
614     else
615         date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
616
617     free( p_vih );
618     free( p_wf );
619
620     vlc_mutex_lock( &p_sys->lock );
621     p_sys->b_ready =
622     p_sys->b_works = true;
623     vlc_cond_signal( &p_sys->wait_output );
624     vlc_mutex_unlock( &p_sys->lock );
625
626     return VLC_SUCCESS;
627
628  error:
629
630     if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
631     if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
632
633 #ifdef LOADER
634     Restore_LDT_Keeper( ldt_fs );
635 #else
636     /* Uninitialize OLE/COM */
637     CoUninitialize();
638 #endif /* LOADER */
639
640     free( p_vih );
641     free( p_wf );
642
643     vlc_mutex_lock( &p_sys->lock );
644     p_sys->b_ready = true;
645     vlc_cond_signal( &p_sys->wait_output );
646     vlc_mutex_unlock( &p_sys->lock );
647     return VLC_EGENERIC;
648 }
649
650 /*****************************************************************************
651  * LoadDMO: Load the DMO object
652  *****************************************************************************/
653 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
654                     IMediaObject **pp_dmo, es_format_t *p_fmt,
655                     bool b_out )
656 {
657     DMO_PARTIAL_MEDIATYPE dmo_partial_type;
658     int i_err;
659
660 #ifndef LOADER
661     long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
662                                const DMO_PARTIAL_MEDIATYPE *,
663                                uint32_t, const DMO_PARTIAL_MEDIATYPE *,
664                                IEnumDMO ** );
665
666     IEnumDMO *p_enum_dmo = NULL;
667     WCHAR *psz_dmo_name;
668     GUID clsid_dmo;
669     uint32_t i_dummy;
670 #endif
671
672     GETCLASS GetClass;
673     IClassFactory *cFactory = NULL;
674     IUnknown *cObject = NULL;
675     const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
676     int i_codec;
677
678     /* Look for a DMO which can handle the requested codec */
679     if( p_fmt->i_cat == AUDIO_ES )
680     {
681         uint16_t i_tag;
682         dmo_partial_type.type = MEDIATYPE_Audio;
683         dmo_partial_type.subtype = dmo_partial_type.type;
684         dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
685         fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
686         if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
687     }
688     else
689     {
690         dmo_partial_type.type = MEDIATYPE_Video;
691         dmo_partial_type.subtype = dmo_partial_type.type;
692         dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
693     }
694
695 #ifndef LOADER
696     /* Load msdmo DLL */
697     *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
698     if( *p_hmsdmo_dll == NULL )
699     {
700         msg_Dbg( p_this, "failed loading msdmo.dll" );
701         return VLC_EGENERIC;
702     }
703     OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
704     if( OurDMOEnum == NULL )
705     {
706         msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
707         FreeLibrary( *p_hmsdmo_dll );
708         return VLC_EGENERIC;
709     }
710
711     if( !b_out )
712     {
713         i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
714                             1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
715     }
716     else
717     {
718         i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
719                             0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
720     }
721     if( i_err )
722     {
723         FreeLibrary( *p_hmsdmo_dll );
724         /* return VLC_EGENERIC; */
725         /* Try loading the dll directly */
726         goto loader;
727     }
728
729     /* Pickup the first available codec */
730     *pp_dmo = 0;
731     while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
732                      &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
733     {
734         char psz_temp[MAX_PATH];
735         wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
736         msg_Dbg( p_this, "found DMO: %s", psz_temp );
737         CoTaskMemFree( psz_dmo_name );
738
739         /* Create DMO */
740         if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
741                               &IID_IMediaObject, (void **)pp_dmo ) )
742         {
743             msg_Warn( p_this, "can't create DMO: %s", psz_temp );
744             *pp_dmo = 0;
745         }
746         else break;
747     }
748
749     p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
750
751     if( !*pp_dmo )
752     {
753         FreeLibrary( *p_hmsdmo_dll );
754         /* return VLC_EGENERIC; */
755         /* Try loading the dll directly */
756         goto loader;
757     }
758
759     return VLC_SUCCESS;
760
761 loader:
762 #endif   /* LOADER */
763
764     for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
765     {
766         if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
767             break;
768     }
769     if( codecs_table[i_codec].i_fourcc == 0 )
770         return VLC_EGENERIC;    /* Can't happen */
771
772     *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
773     if( *p_hmsdmo_dll == NULL )
774     {
775         msg_Dbg( p_this, "failed loading '%s'",
776                  codecs_table[i_codec].psz_dll );
777         return VLC_EGENERIC;
778     }
779
780     GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
781     if (!GetClass)
782     {
783         msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
784         FreeLibrary( *p_hmsdmo_dll );
785         return VLC_EGENERIC;
786     }
787
788     i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
789                       (void**)&cFactory );
790     if( i_err || cFactory == NULL )
791     {
792         msg_Dbg( p_this, "no such class object" );
793         FreeLibrary( *p_hmsdmo_dll );
794         return VLC_EGENERIC;
795     }
796
797     i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
798                                           (void**)&cObject );
799     cFactory->vt->Release( (IUnknown*)cFactory );
800     if( i_err || !cObject )
801     {
802         msg_Dbg( p_this, "class factory failure" );
803         FreeLibrary( *p_hmsdmo_dll );
804         return VLC_EGENERIC;
805     }
806     i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
807                                         (void**)pp_dmo );
808     cObject->vt->Release( (IUnknown*)cObject );
809     if( i_err || !*pp_dmo )
810     {
811         msg_Dbg( p_this, "QueryInterface failure" );
812         FreeLibrary( *p_hmsdmo_dll );
813         return VLC_EGENERIC;
814     }
815
816     return VLC_SUCCESS;
817 }
818
819 static void DecClose( decoder_t *p_dec )
820 {
821     decoder_sys_t *p_sys = p_dec->p_sys;
822
823     if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
824     FreeLibrary( p_sys->hmsdmo_dll );
825
826 #ifdef LOADER
827 #if 0
828     Restore_LDT_Keeper( p_sys->ldt_fs );
829 #endif
830 #else
831     /* Uninitialize OLE/COM */
832     CoUninitialize();
833 #endif
834
835     free( p_sys->p_buffer );
836 }
837
838 /****************************************************************************
839  * DecodeBlock: the whole thing
840  ****************************************************************************
841  * This function must be fed with packets.
842  ****************************************************************************/
843 static void *DecBlock( decoder_t *p_dec, block_t **pp_block )
844 {
845     decoder_sys_t *p_sys = p_dec->p_sys;
846     block_t *p_block;
847     int i_result;
848
849     DMO_OUTPUT_DATA_BUFFER db;
850     CMediaBuffer *p_out;
851     block_t block_out;
852     uint32_t i_status;
853
854     if( !pp_block ) return NULL;
855
856     p_block = *pp_block;
857
858     /* Won't work with streams with B-frames, but do we have any ? */
859     if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
860
861     /* Date management */
862     if( p_block && p_block->i_pts > 0 &&
863         p_block->i_pts != date_Get( &p_sys->end_date ) )
864     {
865         date_Set( &p_sys->end_date, p_block->i_pts );
866     }
867
868 #if 0 /* Breaks the video decoding */
869     if( !date_Get( &p_sys->end_date ) )
870     {
871         /* We've just started the stream, wait for the first PTS. */
872         if( p_block ) block_Release( p_block );
873         return NULL;
874     }
875 #endif
876
877     /* Feed input to the DMO */
878     if( p_block && p_block->i_buffer )
879     {
880         CMediaBuffer *p_in;
881
882         p_in = CMediaBufferCreate( p_block, p_block->i_buffer, true );
883
884         i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
885                        (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
886                        0, 0 );
887
888         p_in->vt->Release( (IUnknown *)p_in );
889
890         if( i_result == S_FALSE )
891         {
892             /* No output generated */
893 #ifdef DMO_DEBUG
894             msg_Dbg( p_dec, "ProcessInput(): no output generated" );
895 #endif
896             return NULL;
897         }
898         else if( i_result == (int)DMO_E_NOTACCEPTING )
899         {
900             /* Need to call ProcessOutput */
901             msg_Dbg( p_dec, "ProcessInput(): not accepting" );
902         }
903         else if( i_result != S_OK )
904         {
905             msg_Dbg( p_dec, "ProcessInput(): failed" );
906             return NULL;
907         }
908         else
909         {
910             //msg_Dbg( p_dec, "ProcessInput(): successful" );
911             *pp_block = NULL;
912         }
913     }
914     else if( p_block && !p_block->i_buffer )
915     {
916         block_Release( p_block );
917         *pp_block = NULL;
918     }
919
920     /* Get output from the DMO */
921     block_out.p_buffer = p_sys->p_buffer;
922     block_out.i_buffer = 0;
923
924     p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, false );
925     memset( &db, 0, sizeof(db) );
926     db.pBuffer = (IMediaBuffer *)p_out;
927
928     i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
929                    DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
930                    1, &db, &i_status );
931
932     if( i_result != S_OK )
933     {
934         if( i_result != S_FALSE )
935             msg_Dbg( p_dec, "ProcessOutput(): failed" );
936 #ifdef DMO_DEBUG
937         else
938             msg_Dbg( p_dec, "ProcessOutput(): no output" );
939 #endif
940
941         p_out->vt->Release( (IUnknown *)p_out );
942         return NULL;
943     }
944
945 #ifdef DMO_DEBUG
946     msg_Dbg( p_dec, "ProcessOutput(): success" );
947 #endif
948
949     if( !block_out.i_buffer )
950     {
951 #ifdef DMO_DEBUG
952         msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
953 #endif
954         p_out->vt->Release( (IUnknown *)p_out );
955         return NULL;
956     }
957
958     if( p_dec->fmt_out.i_cat == VIDEO_ES )
959     {
960         /* Get a new picture */
961         picture_t *p_pic = decoder_NewPicture( p_dec );
962         if( !p_pic ) return NULL;
963
964         CopyPicture( p_pic, block_out.p_buffer );
965
966         /* Date management */
967         p_pic->date = date_Get( &p_sys->end_date );
968         date_Increment( &p_sys->end_date, 1 );
969
970         p_out->vt->Release( (IUnknown *)p_out );
971
972         return p_pic;
973     }
974     else
975     {
976         aout_buffer_t *p_aout_buffer;
977         int i_samples = block_out.i_buffer /
978             ( p_dec->fmt_out.audio.i_bitspersample *
979               p_dec->fmt_out.audio.i_channels / 8 );
980
981         p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
982         if( p_aout_buffer )
983         {
984             memcpy( p_aout_buffer->p_buffer,
985                     block_out.p_buffer, block_out.i_buffer );
986             /* Date management */
987             p_aout_buffer->start_date = date_Get( &p_sys->end_date );
988             p_aout_buffer->end_date =
989                 date_Increment( &p_sys->end_date, i_samples );
990         }
991         p_out->vt->Release( (IUnknown *)p_out );
992
993         return p_aout_buffer;
994     }
995
996     return NULL;
997 }
998
999 static void CopyPicture( picture_t *p_pic, uint8_t *p_in )
1000 {
1001     int i_plane, i_line, i_width, i_dst_stride;
1002     uint8_t *p_dst, *p_src = p_in;
1003
1004     p_dst = p_pic->p[1].p_pixels;
1005     p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1006     p_pic->p[2].p_pixels = p_dst;
1007
1008     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1009     {
1010         p_dst = p_pic->p[i_plane].p_pixels;
1011         i_width = p_pic->p[i_plane].i_visible_pitch;
1012         i_dst_stride  = p_pic->p[i_plane].i_pitch;
1013
1014         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
1015         {
1016             vlc_memcpy( p_dst, p_src, i_width );
1017             p_src += i_width;
1018             p_dst += i_dst_stride;
1019         }
1020     }
1021
1022     p_dst = p_pic->p[1].p_pixels;
1023     p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1024     p_pic->p[2].p_pixels = p_dst;
1025 }
1026
1027 static void *DecoderThread( void *data )
1028 {
1029     decoder_t *p_dec = data;
1030     decoder_sys_t *p_sys = p_dec->p_sys;
1031
1032     if( DecOpen( p_dec ) )
1033         return NULL; /* failed */
1034
1035     vlc_mutex_lock( &p_sys->lock );
1036     for( ;; )
1037     {
1038         while( p_sys->b_ready && !p_sys->pp_input )
1039             vlc_cond_wait( &p_sys->wait_input, &p_sys->lock );
1040         if( !p_sys->b_ready )
1041             break;
1042
1043         p_sys->p_output = DecBlock( p_dec, p_sys->pp_input );
1044         p_sys->pp_input = NULL;
1045         vlc_cond_signal( &p_sys->wait_output );
1046     }
1047     vlc_mutex_unlock( &p_sys->lock );
1048
1049     DecClose( p_dec );
1050     return NULL;
1051 }
1052
1053
1054 /****************************************************************************
1055  * Encoder descriptor declaration
1056  ****************************************************************************/
1057 struct encoder_sys_t
1058 {
1059     HINSTANCE hmsdmo_dll;
1060     IMediaObject *p_dmo;
1061
1062     int i_min_output;
1063
1064     date_t end_date;
1065
1066 #ifdef LOADER
1067     ldt_fs_t    *ldt_fs;
1068 #endif
1069 };
1070
1071 /*****************************************************************************
1072  * EncoderOpen: open dmo codec
1073  *****************************************************************************/
1074 static int EncoderOpen( vlc_object_t *p_this )
1075 {
1076     encoder_t *p_enc = (encoder_t*)p_this;
1077
1078     int i_ret = EncOpen( p_this );
1079     if( i_ret != VLC_SUCCESS ) return i_ret;
1080
1081     /* Set callbacks */
1082     p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1083         EncodeBlock;
1084     p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
1085         EncodeBlock;
1086
1087     return VLC_SUCCESS;
1088 }
1089
1090 /*****************************************************************************
1091  * EncoderSetVideoType: configures the input and output types of the dmo
1092  *****************************************************************************/
1093 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1094 {
1095     int i, i_selected, i_err;
1096     DMO_MEDIA_TYPE dmo_type;
1097     VIDEOINFOHEADER vih, *p_vih;
1098     BITMAPINFOHEADER *p_bih;
1099
1100     /* FIXME */
1101     p_enc->fmt_in.video.i_bits_per_pixel =
1102         p_enc->fmt_out.video.i_bits_per_pixel = 12;
1103
1104     /* Enumerate input format (for debug output) */
1105     i = 0;
1106     while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1107     {
1108         p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1109
1110         msg_Dbg( p_enc, "available input chroma: %4.4s",
1111                  (char *)&dmo_type.subtype.Data1 );
1112         if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1113             msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1114         if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1115             msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1116
1117         DMOFreeMediaType( &dmo_type );
1118     }
1119
1120     /* Setup input format */
1121     memset( &dmo_type, 0, sizeof(dmo_type) );
1122     memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1123
1124     p_bih = &vih.bmiHeader;
1125     p_bih->biCompression = VLC_CODEC_I420;
1126     p_bih->biWidth = p_enc->fmt_in.video.i_width;
1127     p_bih->biHeight = p_enc->fmt_in.video.i_height;
1128     p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1129     p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1130         p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1131     p_bih->biPlanes = 3;
1132     p_bih->biSize = sizeof(BITMAPINFOHEADER);
1133
1134     vih.rcSource.left = vih.rcSource.top = 0;
1135     vih.rcSource.right = p_enc->fmt_in.video.i_width;
1136     vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1137     vih.rcTarget = vih.rcSource;
1138
1139     vih.AvgTimePerFrame = INT64_C(10000000) / 25; //FIXME
1140
1141     dmo_type.majortype = MEDIATYPE_Video;
1142     //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1143     dmo_type.subtype = MEDIASUBTYPE_I420;
1144     //dmo_type.subtype.Data1 = p_bih->biCompression;
1145     dmo_type.formattype = FORMAT_VideoInfo;
1146     dmo_type.bFixedSizeSamples = TRUE;
1147     dmo_type.bTemporalCompression = FALSE;
1148     dmo_type.lSampleSize = p_bih->biSizeImage;
1149     dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1150     dmo_type.pbFormat = (char *)&vih;
1151
1152     if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1153     {
1154         msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1155         return VLC_EGENERIC;
1156     }
1157
1158     msg_Dbg( p_enc, "successfully set input type" );
1159
1160     /* Setup output format */
1161     memset( &dmo_type, 0, sizeof(dmo_type) );
1162     dmo_type.pUnk = 0;
1163
1164     /* Enumerate output types */
1165     i = 0, i_selected = -1;
1166     while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1167     {
1168         p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1169
1170         msg_Dbg( p_enc, "available output codec: %4.4s",
1171                  (char *)&dmo_type.subtype.Data1 );
1172
1173         if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1174             i_selected = i - 1;
1175
1176         DMOFreeMediaType( &dmo_type );
1177     }
1178
1179     if( i_selected < 0 )
1180     {
1181         msg_Err( p_enc, "couldn't find codec: %4.4s",
1182                  (char *)&p_enc->fmt_out.i_codec );
1183         return VLC_EGENERIC;
1184     }
1185
1186     p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1187     ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1188         p_enc->fmt_out.i_bitrate;
1189
1190     /* Get the private data for the codec */
1191     while( 1 )
1192     {
1193         IWMCodecPrivateData *p_privdata;
1194         VIDEOINFOHEADER *p_vih;
1195         uint8_t *p_data = 0;
1196         uint32_t i_data = 0, i_vih;
1197
1198         i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1199                                            &IID_IWMCodecPrivateData,
1200                                            (void **)&p_privdata );
1201         if( i_err ) break;
1202
1203         i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1204         if( i_err )
1205         {
1206             msg_Err( p_enc, "SetPartialOutputType() failed" );
1207             p_privdata->vt->Release( (IUnknown *)p_privdata );
1208             break;
1209         }
1210
1211         i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1212         if( i_err )
1213         {
1214             msg_Err( p_enc, "GetPrivateData() failed" );
1215             p_privdata->vt->Release( (IUnknown *)p_privdata );
1216             break;
1217         }
1218
1219         p_data = malloc( i_data );
1220         i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1221
1222         /* Update the media type with the private data */
1223         i_vih = dmo_type.cbFormat + i_data;
1224         p_vih = CoTaskMemAlloc( i_vih );
1225         memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1226         memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1227         DMOFreeMediaType( &dmo_type );
1228         dmo_type.pbFormat = (char*)p_vih;
1229         dmo_type.cbFormat = i_vih;
1230
1231         msg_Dbg( p_enc, "found extra data: %i", i_data );
1232         p_enc->fmt_out.i_extra = i_data;
1233         p_enc->fmt_out.p_extra = p_data;
1234         break;
1235     }
1236
1237     i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1238
1239     p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1240     p_enc->fmt_in.i_codec = VLC_CODEC_I420;
1241
1242     DMOFreeMediaType( &dmo_type );
1243     if( i_err )
1244     {
1245         msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1246         return VLC_EGENERIC;
1247     }
1248
1249     msg_Dbg( p_enc, "successfully set output type" );
1250     return VLC_SUCCESS;
1251 }
1252
1253 /*****************************************************************************
1254  * EncoderSetAudioType: configures the input and output types of the dmo
1255  *****************************************************************************/
1256 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1257 {
1258     int i, i_selected, i_err;
1259     unsigned int i_last_byterate;
1260     uint16_t i_tag;
1261     DMO_MEDIA_TYPE dmo_type;
1262     WAVEFORMATEX *p_wf;
1263
1264     /* Setup the format structure */
1265     fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1266     if( i_tag == 0 ) return VLC_EGENERIC;
1267
1268     p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1269     p_enc->fmt_in.audio.i_bitspersample = 16;
1270
1271     /* We first need to choose an output type from the predefined
1272      * list of choices (we cycle through the list to select the best match) */
1273     i = 0; i_selected = -1; i_last_byterate = 0;
1274     while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1275     {
1276         p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1277         msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1278                  "bits per sample: %i, bitrate: %i, blockalign: %i",
1279                  (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1280                  (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1281                  (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1282
1283         if( p_wf->wFormatTag == i_tag &&
1284             p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1285             p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1286             p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1287         {
1288             if( p_wf->nAvgBytesPerSec <
1289                 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1290                 p_wf->nAvgBytesPerSec > i_last_byterate )
1291             {
1292                 i_selected = i - 1;
1293                 i_last_byterate = p_wf->nAvgBytesPerSec;
1294                 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1295                          i_selected, p_wf->nAvgBytesPerSec * 8 );
1296             }
1297         }
1298
1299         DMOFreeMediaType( &dmo_type );
1300     }
1301
1302     if( i_selected < 0 )
1303     {
1304         msg_Err( p_enc, "couldn't find a matching output" );
1305         return VLC_EGENERIC;
1306     }
1307
1308     p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1309     p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1310
1311     msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1312              "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1313              (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1314              (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1315              (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1316
1317     p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1318     p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1319     p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1320     p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1321     p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1322
1323     if( p_wf->cbSize )
1324     {
1325         msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1326         p_enc->fmt_out.i_extra = p_wf->cbSize;
1327         p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1328         memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1329     }
1330
1331     i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1332     DMOFreeMediaType( &dmo_type );
1333
1334     if( i_err )
1335     {
1336         msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1337         return VLC_EGENERIC;
1338     }
1339
1340     msg_Dbg( p_enc, "successfully set output type" );
1341
1342     /* Setup the input type */
1343     i = 0; i_selected = -1;
1344     while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1345     {
1346         p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1347         msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1348                  "bits per sample: %i, bitrate: %i, blockalign: %i",
1349                  (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1350                  (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1351                  (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1352
1353         if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1354             p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1355             p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1356             p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1357         {
1358             i_selected = i - 1;
1359         }
1360
1361         DMOFreeMediaType( &dmo_type );
1362     }
1363
1364     if( i_selected < 0 )
1365     {
1366         msg_Err( p_enc, "couldn't find a matching input" );
1367         return VLC_EGENERIC;
1368     }
1369
1370     p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1371     i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1372     DMOFreeMediaType( &dmo_type );
1373     if( i_err )
1374     {
1375         msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1376         return VLC_EGENERIC;
1377     }
1378     msg_Dbg( p_enc, "successfully set input type" );
1379
1380     return VLC_SUCCESS;
1381 }
1382
1383 /*****************************************************************************
1384  * EncOpen: open dmo codec
1385  *****************************************************************************/
1386 static int EncOpen( vlc_object_t *p_this )
1387 {
1388     encoder_t *p_enc = (encoder_t*)p_this;
1389     encoder_sys_t *p_sys = NULL;
1390     IMediaObject *p_dmo = NULL;
1391     HINSTANCE hmsdmo_dll = NULL;
1392
1393 #ifdef LOADER
1394     ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1395 #else
1396     /* Initialize OLE/COM */
1397     CoInitialize( 0 );
1398 #endif /* LOADER */
1399
1400     if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, true )
1401         != VLC_SUCCESS )
1402     {
1403         hmsdmo_dll = 0;
1404         p_dmo = 0;
1405         goto error;
1406     }
1407
1408     if( p_enc->fmt_in.i_cat == VIDEO_ES )
1409     {
1410         if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1411     }
1412     else
1413     {
1414         if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1415     }
1416
1417     /* Allocate the memory needed to store the decoder's structure */
1418     if( ( p_enc->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
1419     {
1420         goto error;
1421     }
1422
1423     p_sys->hmsdmo_dll = hmsdmo_dll;
1424     p_sys->p_dmo = p_dmo;
1425 #ifdef LOADER
1426     p_sys->ldt_fs = ldt_fs;
1427 #endif
1428
1429     /* Find out some properties of the inputput */
1430     {
1431         uint32_t i_size, i_align, dum;
1432
1433         if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1434             msg_Err( p_enc, "GetInputSizeInfo() failed" );
1435         else
1436             msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1437                      i_size, i_align, dum );
1438     }
1439
1440     /* Find out some properties of the output */
1441     {
1442         uint32_t i_size, i_align;
1443
1444         p_sys->i_min_output = 0;
1445         if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1446         {
1447             msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1448             goto error;
1449         }
1450         else
1451         {
1452             msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1453                      i_size, i_align );
1454             p_sys->i_min_output = i_size;
1455         }
1456     }
1457
1458     /* Set output properties */
1459     p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1460     if( p_enc->fmt_out.i_cat == AUDIO_ES )
1461         date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1462     else
1463         date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1464
1465     return VLC_SUCCESS;
1466
1467  error:
1468
1469     if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1470     if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1471
1472 #ifdef LOADER
1473     Restore_LDT_Keeper( ldt_fs );
1474 #else
1475     /* Uninitialize OLE/COM */
1476     CoUninitialize();
1477 #endif /* LOADER */
1478
1479     free( p_sys );
1480
1481     return VLC_EGENERIC;
1482 }
1483
1484 /****************************************************************************
1485  * Encode: the whole thing
1486  ****************************************************************************/
1487 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1488 {
1489     encoder_sys_t *p_sys = p_enc->p_sys;
1490     CMediaBuffer *p_in;
1491     block_t *p_chain = NULL;
1492     block_t *p_block_in;
1493     uint32_t i_status;
1494     int i_result;
1495     mtime_t i_pts;
1496
1497     if( !p_data ) return NULL;
1498
1499     if( p_enc->fmt_out.i_cat == VIDEO_ES )
1500     {
1501         /* Get picture data */
1502         int i_plane, i_line, i_width, i_src_stride;
1503         picture_t *p_pic = (picture_t *)p_data;
1504         uint8_t *p_dst;
1505
1506         int i_buffer = p_enc->fmt_in.video.i_width *
1507             p_enc->fmt_in.video.i_height *
1508             p_enc->fmt_in.video.i_bits_per_pixel / 8;
1509
1510         p_block_in = block_New( p_enc, i_buffer );
1511
1512         /* Copy picture stride by stride */
1513         p_dst = p_block_in->p_buffer;
1514         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1515         {
1516             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1517             i_width = p_pic->p[i_plane].i_visible_pitch;
1518             i_src_stride = p_pic->p[i_plane].i_pitch;
1519
1520             for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1521                  i_line++ )
1522             {
1523                 vlc_memcpy( p_dst, p_src, i_width );
1524                 p_dst += i_width;
1525                 p_src += i_src_stride;
1526             }
1527         }
1528
1529         i_pts = p_pic->date;
1530     }
1531     else
1532     {
1533         aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1534         p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1535         memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1536                 p_block_in->i_buffer );
1537
1538         i_pts = p_aout_buffer->start_date;
1539     }
1540
1541     /* Feed input to the DMO */
1542     p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, true );
1543     i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1544        (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1545
1546     p_in->vt->Release( (IUnknown *)p_in );
1547     if( i_result == S_FALSE )
1548     {
1549         /* No output generated */
1550 #ifdef DMO_DEBUG
1551         msg_Dbg( p_enc, "ProcessInput(): no output generated %"PRId64, i_pts );
1552 #endif
1553         return NULL;
1554     }
1555     else if( i_result == (int)DMO_E_NOTACCEPTING )
1556     {
1557         /* Need to call ProcessOutput */
1558         msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1559     }
1560     else if( i_result != S_OK )
1561     {
1562         msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1563         return NULL;
1564     }
1565
1566 #ifdef DMO_DEBUG
1567     msg_Dbg( p_enc, "ProcessInput(): success" );
1568 #endif
1569
1570     /* Get output from the DMO */
1571     while( 1 )
1572     {
1573         DMO_OUTPUT_DATA_BUFFER db;
1574         block_t *p_block_out;
1575         CMediaBuffer *p_out;
1576
1577         p_block_out = block_New( p_enc, p_sys->i_min_output );
1578         p_block_out->i_buffer = 0;
1579         p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, false);
1580         memset( &db, 0, sizeof(db) );
1581         db.pBuffer = (IMediaBuffer *)p_out;
1582
1583         i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1584                                                     0, 1, &db, &i_status );
1585
1586         if( i_result != S_OK )
1587         {
1588             if( i_result != S_FALSE )
1589                 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1590 #ifdef DMO_DEBUG
1591             else
1592                 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1593 #endif
1594
1595             p_out->vt->Release( (IUnknown *)p_out );
1596             block_Release( p_block_out );
1597             return p_chain;
1598         }
1599
1600         if( !p_block_out->i_buffer )
1601         {
1602 #ifdef DMO_DEBUG
1603             msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1604 #endif
1605             p_out->vt->Release( (IUnknown *)p_out );
1606             block_Release( p_block_out );
1607             return p_chain;
1608         }
1609
1610         if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1611         {
1612 #ifdef DMO_DEBUG
1613             msg_Dbg( p_enc, "ProcessOutput(): pts: %"PRId64", %"PRId64,
1614                      i_pts, db.rtTimestamp / 10 );
1615 #endif
1616             i_pts = db.rtTimestamp / 10;
1617         }
1618
1619         if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1620         {
1621             p_block_out->i_length = db.rtTimelength / 10;
1622 #ifdef DMO_DEBUG
1623             msg_Dbg( p_enc, "ProcessOutput(): length: %"PRId64,
1624                      p_block_out->i_length );
1625 #endif
1626         }
1627
1628         if( p_enc->fmt_out.i_cat == VIDEO_ES )
1629         {
1630             if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1631                 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1632             else
1633                 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1634         }
1635
1636         p_block_out->i_dts = p_block_out->i_pts = i_pts;
1637         block_ChainAppend( &p_chain, p_block_out );
1638     }
1639 }
1640
1641 /*****************************************************************************
1642  * EncoderClose: close codec
1643  *****************************************************************************/
1644 void EncoderClose( vlc_object_t *p_this )
1645 {
1646     encoder_t *p_enc = (encoder_t*)p_this;
1647     encoder_sys_t *p_sys = p_enc->p_sys;
1648
1649     if( !p_sys ) return;
1650
1651     if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1652     FreeLibrary( p_sys->hmsdmo_dll );
1653
1654 #ifdef LOADER
1655 #if 0
1656     Restore_LDT_Keeper( p_sys->ldt_fs );
1657 #endif
1658 #else
1659     /* Uninitialize OLE/COM */
1660     CoUninitialize();
1661 #endif
1662
1663     free( p_sys );
1664 }