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