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