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