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