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