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