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