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