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