]> git.sesse.net Git - vlc/blob - modules/codec/dmo/dmo.c
A bit of headers cleanup
[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 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_codec.h>
33 #include <vlc_vout.h>
34 #include <vlc_aout.h>
35
36 #ifndef WIN32
37 #    define LOADER
38 #else
39 #   include <objbase.h>
40 #endif
41
42 #ifdef LOADER
43 /* Need the w32dll loader from mplayer */
44 #   include <wine/winerror.h>
45 #   include <ldt_keeper.h>
46 #   include <wine/windef.h>
47 #endif
48
49 #include <vlc_codecs.h>
50 #include "dmo.h"
51
52 //#define DMO_DEBUG 1
53
54 #ifdef LOADER
55 /* Not Needed */
56 long CoInitialize( void *pvReserved ) { return -1; }
57 void CoUninitialize( void ) { }
58
59 /* A few prototypes */
60 HMODULE WINAPI LoadLibraryA(LPCSTR);
61 #define LoadLibrary LoadLibraryA
62 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
63 int     WINAPI FreeLibrary(HMODULE);
64 #endif /* LOADER */
65
66 typedef long (STDCALL *GETCLASS) ( const GUID*, const GUID*, void** );
67
68 static int pi_channels_maps[7] =
69 {
70     0,
71     AOUT_CHAN_CENTER,
72     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
73     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
74     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
75      | AOUT_CHAN_REARRIGHT,
76     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
77      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
78     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
79      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
80 };
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 static int  DecoderOpen  ( vlc_object_t * );
86 static int  DecOpen      ( vlc_object_t * );
87 static void DecoderClose ( vlc_object_t * );
88 static void *DecodeBlock ( decoder_t *, block_t ** );
89
90 static int  EncoderOpen  ( vlc_object_t * );
91 static int  EncOpen      ( vlc_object_t * );
92 static void EncoderClose ( vlc_object_t * );
93 static block_t *EncodeBlock( encoder_t *, void * );
94
95 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
96                     es_format_t *, vlc_bool_t );
97 static void CopyPicture( decoder_t *, picture_t *, uint8_t * );
98
99 vlc_module_begin();
100     set_description( _("DirectMedia Object decoder") );
101     add_shortcut( "dmo" );
102     set_capability( "decoder", 1 );
103     set_callbacks( DecoderOpen, DecoderClose );
104     set_category( CAT_INPUT );
105     set_subcategory( SUBCAT_INPUT_SCODEC );
106
107 #   define ENC_CFG_PREFIX "sout-dmo-"
108     add_submodule();
109     set_description( _("DirectMedia Object encoder") );
110     set_capability( "encoder", 10 );
111     set_callbacks( EncoderOpen, EncoderClose );
112
113 vlc_module_end();
114
115 /*****************************************************************************
116  * Local prototypes
117  *****************************************************************************/
118
119 /****************************************************************************
120  * Decoder descriptor declaration
121  ****************************************************************************/
122 struct decoder_sys_t
123 {
124     HINSTANCE hmsdmo_dll;
125     IMediaObject *p_dmo;
126
127     int i_min_output;
128     uint8_t *p_buffer;
129
130     date_t end_date;
131
132 #ifdef LOADER
133     ldt_fs_t    *ldt_fs;
134 #endif
135 };
136
137 static const GUID guid_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
138 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
139
140 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
141 static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
142 static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };
143
144 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
145 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
146
147 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
148 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
149
150 typedef struct
151 {
152     vlc_fourcc_t i_fourcc;
153     const char   *psz_dll;
154     const GUID   *p_guid;
155
156 } codec_dll;
157
158 static const codec_dll decoders_table[] =
159 {
160     /* WVC1 */
161     { VLC_FOURCC('W','V','C','1'), "wvc1dmod.dll", &guid_wvc1 },
162     { VLC_FOURCC('w','v','c','1'), "wvc1dmod.dll", &guid_wvc1 },
163     /* WMV3 */
164     { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
165     { VLC_FOURCC('W','M','V','P'), "wmv9dmod.dll", &guid_wmv9 },
166     { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
167     /* WMV2 */
168     { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
169     { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
170     /* WMV1 */
171     { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
172     { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
173     /* Screen codecs */
174     { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
175     { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
176     /* Windows Media Video Adv */
177     { VLC_FOURCC('W','M','V','A'), "wmvadvd.dll", &guid_wmva },
178     { VLC_FOURCC('W','V','P','2'), "wmvadvd.dll", &guid_wmva },
179
180     /* WMA 3 */
181     { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
182     { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
183     { VLC_FOURCC('w','m','a','p'), "wma9dmod.dll", &guid_wma9 },
184     /* WMA 2 */
185     { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
186     { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
187
188     /* WMA Speech */
189     { VLC_FOURCC('w','m','a','s'), "wmspdmod.dll", &guid_wma },
190
191     /* */
192     { 0, NULL, NULL }
193 };
194
195 static const codec_dll encoders_table[] =
196 {
197     /* WMV2 */
198     { VLC_FOURCC('W','M','V','2'), "wmvdmoe.dll", &guid_wmv_enc },
199     { VLC_FOURCC('w','m','v','2'), "wmvdmoe.dll", &guid_wmv_enc },
200     /* WMV1 */
201     { VLC_FOURCC('W','M','V','1'), "wmvdmoe.dll", &guid_wmv_enc },
202     { VLC_FOURCC('w','m','v','1'), "wmvdmoe.dll", &guid_wmv_enc },
203
204     /* WMA 3 */
205     { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
206     { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
207     /* WMA 2 */
208     { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
209     { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
210
211     /* */
212     { 0, NULL, NULL }
213 };
214
215 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
216 {
217     if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
218     if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
219     mt->cbFormat = 0;
220     mt->pbFormat = NULL;
221     mt->pUnk = NULL;
222 }
223
224 /*****************************************************************************
225  * DecoderOpen: open dmo codec
226  *****************************************************************************/
227 static int DecoderOpen( vlc_object_t *p_this )
228 {
229     decoder_t *p_dec = (decoder_t*)p_this;
230
231 #ifndef LOADER
232     int i_ret = DecOpen( p_this );
233     if( i_ret != VLC_SUCCESS ) return i_ret;
234
235
236 #else
237     /* We can't open it now, because of ldt_keeper or something
238      * Open/Decode/Close has to be done in the same thread */
239     int i;
240
241     p_dec->p_sys = NULL;
242
243     /* Probe if we support it */
244     for( i = 0; decoders_table[i].i_fourcc != 0; i++ )
245     {
246         if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
247         {
248             msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
249                      (char*)&p_dec->fmt_in.i_codec,
250                      decoders_table[i].psz_dll );
251             break;
252         }
253     }
254
255     p_dec->p_sys = NULL;
256     if( !decoders_table[i].i_fourcc ) return VLC_EGENERIC;
257 #endif /* LOADER */
258
259     /* Set callbacks */
260     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
261         DecodeBlock;
262     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
263         DecodeBlock;
264
265     return VLC_SUCCESS;
266 }
267
268 /*****************************************************************************
269  * DecOpen: open dmo codec
270  *****************************************************************************/
271 static int DecOpen( vlc_object_t *p_this )
272 {
273     decoder_t *p_dec = (decoder_t*)p_this;
274     decoder_sys_t *p_sys = NULL;
275
276     DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
277     IMediaObject *p_dmo = NULL;
278     HINSTANCE hmsdmo_dll = NULL;
279
280     VIDEOINFOHEADER *p_vih = NULL;
281     WAVEFORMATEX *p_wf = NULL;
282
283 #ifdef LOADER
284     ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
285 #else
286     /* Initialize OLE/COM */
287     CoInitialize( 0 );
288 #endif /* LOADER */
289
290     if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, VLC_FALSE )
291         != VLC_SUCCESS )
292     {
293         hmsdmo_dll = 0;
294         p_dmo = 0;
295         goto error;
296     }
297
298     /* Setup input format */
299     memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
300     dmo_input_type.pUnk = 0;
301
302     if( p_dec->fmt_in.i_cat == AUDIO_ES )
303     {
304         uint16_t i_tag;
305         int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
306         p_wf = malloc( i_size );
307
308         memset( p_wf, 0, sizeof(WAVEFORMATEX) );
309         if( p_dec->fmt_in.i_extra )
310             memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
311
312         dmo_input_type.majortype  = MEDIATYPE_Audio;
313         dmo_input_type.subtype    = dmo_input_type.majortype;
314         dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
315         fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
316         if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
317
318         p_wf->wFormatTag = dmo_input_type.subtype.Data1;
319         p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
320         p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
321         p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
322         p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
323         p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
324         p_wf->cbSize = p_dec->fmt_in.i_extra;
325
326         dmo_input_type.formattype = FORMAT_WaveFormatEx;
327         dmo_input_type.cbFormat   = i_size;
328         dmo_input_type.pbFormat   = (char *)p_wf;
329         dmo_input_type.bFixedSizeSamples = 1;
330         dmo_input_type.bTemporalCompression = 0;
331         dmo_input_type.lSampleSize = p_wf->nBlockAlign;
332     }
333     else
334     {
335         BITMAPINFOHEADER *p_bih;
336
337         int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
338         p_vih = malloc( i_size );
339
340         memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
341         if( p_dec->fmt_in.i_extra )
342             memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
343
344         p_bih = &p_vih->bmiHeader;
345         p_bih->biCompression = p_dec->fmt_in.i_codec;
346         p_bih->biWidth = p_dec->fmt_in.video.i_width;
347         p_bih->biHeight = p_dec->fmt_in.video.i_height;
348         p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
349         p_bih->biPlanes = 1;
350         p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
351             sizeof(BITMAPINFOHEADER);
352
353         p_vih->rcSource.left = p_vih->rcSource.top = 0;
354         p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
355         p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
356         p_vih->rcTarget = p_vih->rcSource;
357
358         dmo_input_type.majortype  = MEDIATYPE_Video;
359         dmo_input_type.subtype    = dmo_input_type.majortype;
360         dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
361         dmo_input_type.formattype = FORMAT_VideoInfo;
362         dmo_input_type.bFixedSizeSamples = 0;
363         dmo_input_type.bTemporalCompression = 1;
364         dmo_input_type.cbFormat = i_size;
365         dmo_input_type.pbFormat = (char *)p_vih;
366     }
367
368     if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
369     {
370         msg_Err( p_dec, "can't set DMO input type" );
371         goto error;
372     }
373     msg_Dbg( p_dec, "DMO input type set" );
374
375     /* Setup output format */
376     memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
377     dmo_output_type.pUnk = 0;
378
379     if( p_dec->fmt_in.i_cat == AUDIO_ES )
380     {
381         /* Setup the format */
382         p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
383         p_dec->fmt_out.audio.i_rate     = p_dec->fmt_in.audio.i_rate;
384         p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
385         p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
386         p_dec->fmt_out.audio.i_physical_channels =
387             p_dec->fmt_out.audio.i_original_channels =
388                 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
389
390         p_wf->wFormatTag = WAVE_FORMAT_PCM;
391         p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
392         p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
393         p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
394         p_wf->nBlockAlign =
395             p_wf->wBitsPerSample / 8 * p_wf->nChannels;
396         p_wf->nAvgBytesPerSec =
397             p_wf->nSamplesPerSec * p_wf->nBlockAlign;
398         p_wf->cbSize = 0;
399
400         dmo_output_type.majortype  = MEDIATYPE_Audio;
401         dmo_output_type.formattype = FORMAT_WaveFormatEx;
402         dmo_output_type.subtype    = MEDIASUBTYPE_PCM;
403         dmo_output_type.cbFormat   = sizeof(WAVEFORMATEX);
404         dmo_output_type.pbFormat   = (char *)p_wf;
405         dmo_output_type.bFixedSizeSamples = 1;
406         dmo_output_type.bTemporalCompression = 0;
407         dmo_output_type.lSampleSize = p_wf->nBlockAlign;
408     }
409     else
410     {
411         BITMAPINFOHEADER *p_bih;
412         DMO_MEDIA_TYPE mt;
413         unsigned i_chroma = VLC_FOURCC('Y','U','Y','2');
414         int i_planes = 1, i_bpp = 16;
415         int i = 0;
416
417         /* Find out which chroma to use */
418         while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
419         {
420             if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
421             {
422                 i_chroma = mt.subtype.Data1;
423                 i_planes = 3; i_bpp = 12;
424             }
425
426             DMOFreeMediaType( &mt );
427         }
428
429         p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
430             VLC_FOURCC('I','4','2','0') : i_chroma;
431         p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
432         p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
433         p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
434
435         /* If an aspect-ratio was specified in the input format then force it */
436         if( p_dec->fmt_in.video.i_aspect )
437         {
438             p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
439         }
440         else
441         {
442             p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
443                 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
444         }
445
446         p_bih = &p_vih->bmiHeader;
447         p_bih->biCompression = i_chroma;
448         p_bih->biHeight *= -1;
449         p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
450         p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
451             p_dec->fmt_in.video.i_height *
452             (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
453
454         p_bih->biPlanes = i_planes;
455         p_bih->biSize = sizeof(BITMAPINFOHEADER);
456
457         dmo_output_type.majortype = MEDIATYPE_Video;
458         dmo_output_type.formattype = FORMAT_VideoInfo;
459         dmo_output_type.subtype = dmo_output_type.majortype;
460         dmo_output_type.subtype.Data1 = p_bih->biCompression;
461         dmo_output_type.bFixedSizeSamples = VLC_TRUE;
462         dmo_output_type.bTemporalCompression = 0;
463         dmo_output_type.lSampleSize = p_bih->biSizeImage;
464         dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
465         dmo_output_type.pbFormat = (char *)p_vih;
466     }
467
468 #ifdef DMO_DEBUG
469     /* Enumerate output types */
470     if( p_dec->fmt_in.i_cat == VIDEO_ES )
471     {
472         int i = 0;
473         DMO_MEDIA_TYPE mt;
474
475         while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
476         {
477             msg_Dbg( p_dec, "available output chroma: %4.4s",
478                      (char *)&mt.subtype.Data1 );
479             DMOFreeMediaType( &mt );
480         }
481     }
482 #endif
483
484     if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
485     {
486         msg_Err( p_dec, "can't set DMO output type" );
487         goto error;
488     }
489     msg_Dbg( p_dec, "DMO output type set" );
490
491     /* Allocate the memory needed to store the decoder's structure */
492     if( ( p_dec->p_sys = p_sys =
493           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
494     {
495         msg_Err( p_dec, "out of memory" );
496         goto error;
497     }
498
499     p_sys->hmsdmo_dll = hmsdmo_dll;
500     p_sys->p_dmo = p_dmo;
501 #ifdef LOADER
502     p_sys->ldt_fs = ldt_fs;
503 #endif
504
505     /* Find out some properties of the output */
506     {
507         uint32_t i_size, i_align;
508
509         p_sys->i_min_output = 0;
510         if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
511         {
512             msg_Err( p_dec, "GetOutputSizeInfo() failed" );
513             goto error;
514         }
515         else
516         {
517             msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
518                      i_size, i_align );
519             p_sys->i_min_output = i_size;
520             p_sys->p_buffer = malloc( i_size );
521             if( !p_sys->p_buffer ) goto error;
522         }
523     }
524
525     /* Set output properties */
526     p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
527     if( p_dec->fmt_out.i_cat == AUDIO_ES )
528         date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
529     else
530         date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
531
532     if( p_vih ) free( p_vih );
533     if( p_wf ) free( p_wf );
534
535     return VLC_SUCCESS;
536
537  error:
538
539     if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
540     if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
541
542 #ifdef LOADER
543     Restore_LDT_Keeper( ldt_fs );
544 #else
545     /* Uninitialize OLE/COM */
546     CoUninitialize();
547 #endif /* LOADER */
548
549     if( p_vih ) free( p_vih );
550     if( p_wf )  free( p_wf );
551     if( p_sys ) free( p_sys );
552
553     return VLC_EGENERIC;
554 }
555
556 /*****************************************************************************
557  * LoadDMO: Load the DMO object
558  *****************************************************************************/
559 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
560                     IMediaObject **pp_dmo, es_format_t *p_fmt,
561                     vlc_bool_t b_out )
562 {
563     DMO_PARTIAL_MEDIATYPE dmo_partial_type;
564     int i_err;
565
566 #ifndef LOADER
567     long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
568                                const DMO_PARTIAL_MEDIATYPE *,
569                                uint32_t, const DMO_PARTIAL_MEDIATYPE *,
570                                IEnumDMO ** );
571
572     IEnumDMO *p_enum_dmo = NULL;
573     WCHAR *psz_dmo_name;
574     GUID clsid_dmo;
575     uint32_t i_dummy;
576 #endif
577
578     GETCLASS GetClass;
579     IClassFactory *cFactory = NULL;
580     IUnknown *cObject = NULL;
581     const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
582     int i_codec;
583
584     /* Look for a DMO which can handle the requested codec */
585     if( p_fmt->i_cat == AUDIO_ES )
586     {
587         uint16_t i_tag;
588         dmo_partial_type.type = MEDIATYPE_Audio;
589         dmo_partial_type.subtype = dmo_partial_type.type;
590         dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
591         fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
592         if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
593     }
594     else
595     {
596         dmo_partial_type.type = MEDIATYPE_Video;
597         dmo_partial_type.subtype = dmo_partial_type.type;
598         dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
599     }
600
601 #ifndef LOADER
602     /* Load msdmo DLL */
603     *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
604     if( *p_hmsdmo_dll == NULL )
605     {
606         msg_Dbg( p_this, "failed loading msdmo.dll" );
607         return VLC_EGENERIC;
608     }
609     OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
610     if( OurDMOEnum == NULL )
611     {
612         msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
613         FreeLibrary( *p_hmsdmo_dll );
614         return VLC_EGENERIC;
615     }
616
617     if( !b_out )
618     {
619         i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
620                             1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
621     }
622     else
623     {
624         i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
625                             0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
626     }
627     if( i_err )
628     {
629         FreeLibrary( *p_hmsdmo_dll );
630         /* return VLC_EGENERIC; */
631         /* Try loading the dll directly */
632         goto loader;
633     }
634
635     /* Pickup the first available codec */
636     *pp_dmo = 0;
637     while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
638                      &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
639     {
640         char psz_temp[MAX_PATH];
641         wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
642         msg_Dbg( p_this, "found DMO: %s", psz_temp );
643         CoTaskMemFree( psz_dmo_name );
644
645         /* Create DMO */
646         if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
647                               &IID_IMediaObject, (void **)pp_dmo ) )
648         {
649             msg_Warn( p_this, "can't create DMO: %s", psz_temp );
650             *pp_dmo = 0;
651         }
652         else break;
653     }
654
655     p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
656
657     if( !*pp_dmo )
658     {
659         FreeLibrary( *p_hmsdmo_dll );
660         /* return VLC_EGENERIC; */
661         /* Try loading the dll directly */
662         goto loader;
663     }
664
665     return VLC_SUCCESS;
666
667 loader:
668 #endif   /* LOADER */
669
670     for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
671     {
672         if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
673             break;
674     }
675     if( codecs_table[i_codec].i_fourcc == 0 )
676         return VLC_EGENERIC;    /* Can't happen */
677
678     *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
679     if( *p_hmsdmo_dll == NULL )
680     {
681         msg_Dbg( p_this, "failed loading '%s'",
682                  codecs_table[i_codec].psz_dll );
683         return VLC_EGENERIC;
684     }
685
686     GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
687     if (!GetClass)
688     {
689         msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
690         FreeLibrary( *p_hmsdmo_dll );
691         return VLC_EGENERIC;
692     }
693
694     i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
695                       (void**)&cFactory );
696     if( i_err || cFactory == NULL )
697     {
698         msg_Dbg( p_this, "no such class object" );
699         FreeLibrary( *p_hmsdmo_dll );
700         return VLC_EGENERIC;
701     }
702
703     i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
704                                           (void**)&cObject );
705     cFactory->vt->Release( (IUnknown*)cFactory );
706     if( i_err || !cObject )
707     {
708         msg_Dbg( p_this, "class factory failure" );
709         FreeLibrary( *p_hmsdmo_dll );
710         return VLC_EGENERIC;
711     }
712     i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
713                                         (void**)pp_dmo );
714     cObject->vt->Release( (IUnknown*)cObject );
715     if( i_err || !*pp_dmo )
716     {
717         msg_Dbg( p_this, "QueryInterface failure" );
718         FreeLibrary( *p_hmsdmo_dll );
719         return VLC_EGENERIC;
720     }
721
722     return VLC_SUCCESS;
723 }
724
725 /*****************************************************************************
726  * DecoderClose: close codec
727  *****************************************************************************/
728 void DecoderClose( vlc_object_t *p_this )
729 {
730     decoder_t *p_dec = (decoder_t*)p_this;
731     decoder_sys_t *p_sys = p_dec->p_sys;
732
733     if( !p_sys ) return;
734
735     if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
736     FreeLibrary( p_sys->hmsdmo_dll );
737
738 #ifdef LOADER
739 #if 0
740     Restore_LDT_Keeper( p_sys->ldt_fs );
741 #endif
742 #else
743     /* Uninitialize OLE/COM */
744     CoUninitialize();
745 #endif
746
747     if( p_sys->p_buffer ) free( p_sys->p_buffer );
748     free( p_sys );
749 }
750
751 /****************************************************************************
752  * DecodeBlock: the whole thing
753  ****************************************************************************
754  * This function must be fed with ogg packets.
755  ****************************************************************************/
756 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
757 {
758     decoder_sys_t *p_sys = p_dec->p_sys;
759     block_t *p_block;
760     int i_result;
761
762     DMO_OUTPUT_DATA_BUFFER db;
763     CMediaBuffer *p_out;
764     block_t block_out;
765     uint32_t i_status;
766
767     if( p_sys == NULL )
768     {
769         if( DecOpen( VLC_OBJECT(p_dec) ) )
770         {
771             msg_Err( p_dec, "DecOpen failed" );
772             return NULL;
773         }
774         p_sys = p_dec->p_sys;
775     }
776
777     if( !pp_block ) return NULL;
778
779     p_block = *pp_block;
780
781     /* Won't work with streams with B-frames, but do we have any ? */
782     if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
783
784     /* Date management */
785     if( p_block && p_block->i_pts > 0 &&
786         p_block->i_pts != date_Get( &p_sys->end_date ) )
787     {
788         date_Set( &p_sys->end_date, p_block->i_pts );
789     }
790
791 #if 0 /* Breaks the video decoding */
792     if( !date_Get( &p_sys->end_date ) )
793     {
794         /* We've just started the stream, wait for the first PTS. */
795         if( p_block ) block_Release( p_block );
796         return NULL;
797     }
798 #endif
799
800     /* Feed input to the DMO */
801     if( p_block && p_block->i_buffer )
802     {
803         CMediaBuffer *p_in;
804
805         p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );
806
807         i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
808                        (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
809                        0, 0 );
810
811         p_in->vt->Release( (IUnknown *)p_in );
812
813         if( i_result == S_FALSE )
814         {
815             /* No output generated */
816 #ifdef DMO_DEBUG
817             msg_Dbg( p_dec, "ProcessInput(): no output generated" );
818 #endif
819             return NULL;
820         }
821         else if( i_result == (int)DMO_E_NOTACCEPTING )
822         {
823             /* Need to call ProcessOutput */
824             msg_Dbg( p_dec, "ProcessInput(): not accepting" );
825         }
826         else if( i_result != S_OK )
827         {
828             msg_Dbg( p_dec, "ProcessInput(): failed" );
829             return NULL;
830         }
831         else
832         {
833             //msg_Dbg( p_dec, "ProcessInput(): successful" );
834             *pp_block = 0;
835         }
836     }
837     else if( p_block && !p_block->i_buffer )
838     {
839         block_Release( p_block );
840         *pp_block = 0;
841     }
842
843     /* Get output from the DMO */
844     block_out.p_buffer = p_sys->p_buffer;
845     block_out.i_buffer = 0;
846
847     p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );
848     memset( &db, 0, sizeof(db) );
849     db.pBuffer = (IMediaBuffer *)p_out;
850
851     i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
852                    DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
853                    1, &db, &i_status );
854
855     if( i_result != S_OK )
856     {
857         if( i_result != S_FALSE )
858             msg_Dbg( p_dec, "ProcessOutput(): failed" );
859 #ifdef DMO_DEBUG
860         else
861             msg_Dbg( p_dec, "ProcessOutput(): no output" );
862 #endif
863
864         p_out->vt->Release( (IUnknown *)p_out );
865         return NULL;
866     }
867
868 #ifdef DMO_DEBUG
869     msg_Dbg( p_dec, "ProcessOutput(): success" );
870 #endif
871
872     if( !block_out.i_buffer )
873     {
874 #ifdef DMO_DEBUG
875         msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
876 #endif
877         p_out->vt->Release( (IUnknown *)p_out );
878         return NULL;
879     }
880
881     if( p_dec->fmt_out.i_cat == VIDEO_ES )
882     {
883         /* Get a new picture */
884         picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
885         if( !p_pic ) return NULL;
886
887         CopyPicture( p_dec, p_pic, block_out.p_buffer );
888
889         /* Date management */
890         p_pic->date = date_Get( &p_sys->end_date );
891         date_Increment( &p_sys->end_date, 1 );
892
893         p_out->vt->Release( (IUnknown *)p_out );
894
895         return p_pic;
896     }
897     else
898     {
899         aout_buffer_t *p_aout_buffer;
900         int i_samples = block_out.i_buffer /
901             ( p_dec->fmt_out.audio.i_bitspersample *
902               p_dec->fmt_out.audio.i_channels / 8 );
903
904         p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
905         memcpy( p_aout_buffer->p_buffer,
906                 block_out.p_buffer, block_out.i_buffer );
907
908         /* Date management */
909         p_aout_buffer->start_date = date_Get( &p_sys->end_date );
910         p_aout_buffer->end_date =
911             date_Increment( &p_sys->end_date, i_samples );
912
913         p_out->vt->Release( (IUnknown *)p_out );
914
915         return p_aout_buffer;
916     }
917
918     return NULL;
919 }
920
921 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )
922 {
923     int i_plane, i_line, i_width, i_dst_stride;
924     uint8_t *p_dst, *p_src = p_in;
925
926     p_dst = p_pic->p[1].p_pixels;
927     p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
928     p_pic->p[2].p_pixels = p_dst;
929
930     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
931     {
932         p_dst = p_pic->p[i_plane].p_pixels;
933         i_width = p_pic->p[i_plane].i_visible_pitch;
934         i_dst_stride  = p_pic->p[i_plane].i_pitch;
935
936         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
937         {
938             p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
939             p_src += i_width;
940             p_dst += i_dst_stride;
941         }
942     }
943
944     p_dst = p_pic->p[1].p_pixels;
945     p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
946     p_pic->p[2].p_pixels = p_dst;
947 }
948
949 /****************************************************************************
950  * Encoder descriptor declaration
951  ****************************************************************************/
952 struct encoder_sys_t
953 {
954     HINSTANCE hmsdmo_dll;
955     IMediaObject *p_dmo;
956
957     int i_min_output;
958
959     date_t end_date;
960
961 #ifdef LOADER
962     ldt_fs_t    *ldt_fs;
963 #endif
964 };
965
966 /*****************************************************************************
967  * EncoderOpen: open dmo codec
968  *****************************************************************************/
969 static int EncoderOpen( vlc_object_t *p_this )
970 {
971     encoder_t *p_enc = (encoder_t*)p_this;
972
973 #ifndef LOADER
974     int i_ret = EncOpen( p_this );
975     if( i_ret != VLC_SUCCESS ) return i_ret;
976
977 #else
978     /* We can't open it now, because of ldt_keeper or something
979      * Open/Encode/Close has to be done in the same thread */
980     int i;
981
982     /* Probe if we support it */
983     for( i = 0; encoders_table[i].i_fourcc != 0; i++ )
984     {
985         if( encoders_table[i].i_fourcc == p_enc->fmt_out.i_codec )
986         {
987             msg_Dbg( p_enc, "DMO codec for %4.4s may work with dll=%s",
988                      (char*)&p_enc->fmt_out.i_codec,
989                      encoders_table[i].psz_dll );
990             break;
991         }
992     }
993
994     p_enc->p_sys = NULL;
995     if( !encoders_table[i].i_fourcc ) return VLC_EGENERIC;
996 #endif /* LOADER */
997
998     /* Set callbacks */
999     p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1000         EncodeBlock;
1001     p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
1002         EncodeBlock;
1003
1004     return VLC_SUCCESS;
1005 }
1006
1007 /*****************************************************************************
1008  * EncoderSetVideoType: configures the input and output types of the dmo
1009  *****************************************************************************/
1010 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1011 {
1012     int i, i_selected, i_err;
1013     DMO_MEDIA_TYPE dmo_type;
1014     VIDEOINFOHEADER vih, *p_vih;
1015     BITMAPINFOHEADER *p_bih;
1016
1017     /* FIXME */
1018     p_enc->fmt_in.video.i_bits_per_pixel = 
1019         p_enc->fmt_out.video.i_bits_per_pixel = 12;
1020
1021     /* Enumerate input format (for debug output) */
1022     i = 0;
1023     while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1024     {
1025         p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1026
1027         msg_Dbg( p_enc, "available input chroma: %4.4s",
1028                  (char *)&dmo_type.subtype.Data1 );
1029         if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1030             msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1031         if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1032             msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1033
1034         DMOFreeMediaType( &dmo_type );
1035     }
1036
1037     /* Setup input format */
1038     memset( &dmo_type, 0, sizeof(dmo_type) );
1039     dmo_type.pUnk = 0;
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_sys == NULL )
1418     {
1419         if( EncOpen( VLC_OBJECT(p_enc) ) )
1420         {
1421             msg_Err( p_enc, "EncOpen failed" );
1422             return NULL;
1423         }
1424         p_sys = p_enc->p_sys;
1425     }
1426
1427     if( !p_data ) return NULL;
1428
1429     if( p_enc->fmt_out.i_cat == VIDEO_ES )
1430     {
1431         /* Get picture data */
1432         int i_plane, i_line, i_width, i_src_stride;
1433         picture_t *p_pic = (picture_t *)p_data;
1434         uint8_t *p_dst;
1435
1436         int i_buffer = p_enc->fmt_in.video.i_width *
1437             p_enc->fmt_in.video.i_height *
1438             p_enc->fmt_in.video.i_bits_per_pixel / 8;
1439
1440         p_block_in = block_New( p_enc, i_buffer );
1441
1442         /* Copy picture stride by stride */
1443         p_dst = p_block_in->p_buffer;
1444         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1445         {
1446             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1447             i_width = p_pic->p[i_plane].i_visible_pitch;
1448             i_src_stride = p_pic->p[i_plane].i_pitch;
1449
1450             for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1451                  i_line++ )
1452             {
1453                 p_enc->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
1454                 p_dst += i_width;
1455                 p_src += i_src_stride;
1456             }
1457         }
1458
1459         i_pts = p_pic->date;
1460     }
1461     else
1462     {
1463         aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1464         p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1465         memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1466                 p_block_in->i_buffer );
1467
1468         i_pts = p_aout_buffer->start_date;
1469     }
1470
1471     /* Feed input to the DMO */
1472     p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, VLC_TRUE );
1473     i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1474        (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1475
1476     p_in->vt->Release( (IUnknown *)p_in );
1477     if( i_result == S_FALSE )
1478     {
1479         /* No output generated */
1480 #ifdef DMO_DEBUG
1481         msg_Dbg( p_enc, "ProcessInput(): no output generated "I64Fd, i_pts );
1482 #endif
1483         return NULL;
1484     }
1485     else if( i_result == (int)DMO_E_NOTACCEPTING )
1486     {
1487         /* Need to call ProcessOutput */
1488         msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1489     }
1490     else if( i_result != S_OK )
1491     {
1492         msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1493         return NULL;
1494     }
1495
1496 #ifdef DMO_DEBUG
1497     msg_Dbg( p_enc, "ProcessInput(): success" );
1498 #endif
1499
1500     /* Get output from the DMO */
1501     while( 1 )
1502     {
1503         DMO_OUTPUT_DATA_BUFFER db;
1504         block_t *p_block_out;
1505         CMediaBuffer *p_out;
1506
1507         p_block_out = block_New( p_enc, p_sys->i_min_output );
1508         p_block_out->i_buffer = 0;
1509         p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, VLC_FALSE);
1510         memset( &db, 0, sizeof(db) );
1511         db.pBuffer = (IMediaBuffer *)p_out;
1512
1513         i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1514                                                     0, 1, &db, &i_status );
1515
1516         if( i_result != S_OK )
1517         {
1518             if( i_result != S_FALSE )
1519                 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1520 #ifdef DMO_DEBUG
1521             else
1522                 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1523 #endif
1524
1525             p_out->vt->Release( (IUnknown *)p_out );
1526             block_Release( p_block_out );
1527             return p_chain;
1528         }
1529
1530         if( !p_block_out->i_buffer )
1531         {
1532 #ifdef DMO_DEBUG
1533             msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1534 #endif
1535             p_out->vt->Release( (IUnknown *)p_out );
1536             block_Release( p_block_out );
1537             return p_chain;
1538         }
1539
1540         if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1541         {
1542 #ifdef DMO_DEBUG
1543             msg_Dbg( p_enc, "ProcessOutput(): pts: "I64Fd", "I64Fd,
1544                      i_pts, db.rtTimestamp / 10 );
1545 #endif
1546             i_pts = db.rtTimestamp / 10;
1547         }
1548
1549         if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1550         {
1551             p_block_out->i_length = db.rtTimelength / 10;
1552 #ifdef DMO_DEBUG
1553             msg_Dbg( p_enc, "ProcessOutput(): length: "I64Fd,
1554                      p_block_out->i_length );
1555 #endif
1556         }
1557
1558         if( p_enc->fmt_out.i_cat == VIDEO_ES )
1559         {
1560             if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1561                 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1562             else
1563                 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1564         }
1565
1566         p_block_out->i_dts = p_block_out->i_pts = i_pts;
1567         block_ChainAppend( &p_chain, p_block_out );
1568     }
1569 }
1570
1571 /*****************************************************************************
1572  * EncoderClose: close codec
1573  *****************************************************************************/
1574 void EncoderClose( vlc_object_t *p_this )
1575 {
1576     encoder_t *p_enc = (encoder_t*)p_this;
1577     encoder_sys_t *p_sys = p_enc->p_sys;
1578
1579     if( !p_sys ) return;
1580
1581     if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1582     FreeLibrary( p_sys->hmsdmo_dll );
1583
1584 #ifdef LOADER
1585 #if 0
1586     Restore_LDT_Keeper( p_sys->ldt_fs );
1587 #endif
1588 #else
1589     /* Uninitialize OLE/COM */
1590     CoUninitialize();
1591 #endif
1592
1593     free( p_sys );
1594 }