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