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