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