1 /*****************************************************************************
2 * dmo.c : DirectMedia Object decoder module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 the VideoLAN team
7 * Author: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
45 /* Need the w32dll loader from mplayer */
46 # include <wine/winerror.h>
47 # include <ldt_keeper.h>
48 # include <wine/windef.h>
51 #include <vlc_codecs.h>
58 long CoInitialize( void *pvReserved ) { VLC_UNUSED(pvReserved); return -1; }
59 void CoUninitialize( void ) { }
61 /* A few prototypes */
62 HMODULE WINAPI LoadLibraryA(LPCSTR);
63 #define LoadLibrary LoadLibraryA
64 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
65 int WINAPI FreeLibrary(HMODULE);
68 typedef long (STDCALL *GETCLASS) ( const GUID*, const GUID*, void** );
70 static const int pi_channels_maps[7] =
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
84 /*****************************************************************************
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 * );
95 static int EncOpen ( vlc_object_t * );
97 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
98 es_format_t *, bool );
99 static void CopyPicture( picture_t *, uint8_t * );
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 )
109 # define ENC_CFG_PREFIX "sout-dmo-"
111 set_description( N_("DirectMedia Object encoder") )
112 add_shortcut( "dmo" )
113 set_capability( "encoder", 10 )
114 set_callbacks( EncoderOpen, EncoderClose )
118 /*****************************************************************************
120 *****************************************************************************/
122 /****************************************************************************
123 * Decoder descriptor declaration
124 ****************************************************************************/
127 HINSTANCE hmsdmo_dll;
141 vlc_cond_t wait_input, wait_output;
142 bool b_ready, b_works;
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}};
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 } };
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 } };
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 } };
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 } };
178 vlc_fourcc_t i_fourcc;
184 static const codec_dll decoders_table[] =
187 { VLC_FOURCC('W','V','C','1'), "wvc1dmod.dll", &guid_wvc1 },
188 { VLC_FOURCC('w','v','c','1'), "wvc1dmod.dll", &guid_wvc1 },
190 { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
191 { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
192 { VLC_FOURCC('W','M','V','P'), "wmv9dmod.dll", &guid_wmv9 },
193 { VLC_FOURCC('w','m','v','p'), "wmv9dmod.dll", &guid_wmv9 },
195 { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
196 { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
198 { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
199 { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
201 { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
202 { VLC_FOURCC('m','s','s','2'), "wmsdmod.dll", &guid_wms },
203 { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
204 { VLC_FOURCC('m','s','s','1'), "wmsdmod.dll", &guid_wms },
205 /* Windows Media Video Adv */
206 { VLC_FOURCC('W','M','V','A'), "wmvadvd.dll", &guid_wmva },
207 { VLC_FOURCC('w','m','v','a'), "wmvadvd.dll", &guid_wmva },
208 { VLC_FOURCC('W','V','P','2'), "wmvadvd.dll", &guid_wmva },
209 { VLC_FOURCC('w','v','p','2'), "wmvadvd.dll", &guid_wmva },
212 { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
213 { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
214 { VLC_FOURCC('W','M','A','P'), "wma9dmod.dll", &guid_wma9 },
215 { VLC_FOURCC('w','m','a','p'), "wma9dmod.dll", &guid_wma9 },
216 { VLC_FOURCC('W','M','A','L'), "wma9dmod.dll", &guid_wma9 },
217 { VLC_FOURCC('w','m','a','l'), "wma9dmod.dll", &guid_wma9 },
219 { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
220 { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
223 { VLC_FOURCC('W','M','A','S'), "wmspdmod.dll", &guid_wma },
224 { VLC_FOURCC('w','m','a','s'), "wmspdmod.dll", &guid_wma },
230 static const codec_dll encoders_table[] =
233 { VLC_FOURCC('W','M','V','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
234 { VLC_FOURCC('w','m','v','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
236 { VLC_FOURCC('W','M','V','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
237 { VLC_FOURCC('w','m','v','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
239 { VLC_FOURCC('W','M','V','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
240 { VLC_FOURCC('w','m','v','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
243 { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
244 { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
246 { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
247 { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
253 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
255 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
256 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
262 /*****************************************************************************
263 * DecoderOpen: open dmo codec
264 *****************************************************************************/
265 static int DecoderOpen( vlc_object_t *p_this )
267 decoder_t *p_dec = (decoder_t*)p_this;
268 decoder_sys_t *p_sys;
270 /* We can't open it now, because of ldt_keeper or something
271 * Open/Decode/Close has to be done in the same thread */
273 /* Probe if we support it */
274 for( unsigned i = 0; decoders_table[i].i_fourcc != 0; i++ )
276 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
278 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
279 (char*)&p_dec->fmt_in.i_codec,
280 decoders_table[i].psz_dll );
287 p_sys = p_dec->p_sys = malloc(sizeof(*p_sys));
292 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
294 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
297 vlc_mutex_init( &p_sys->lock );
298 vlc_cond_init( &p_sys->wait_input );
299 vlc_cond_init( &p_sys->wait_output );
301 p_sys->b_ready = false;
302 p_sys->pp_input = NULL;
303 p_sys->p_output = NULL;
305 if( vlc_clone( &p_sys->thread, DecoderThread, p_dec,
306 VLC_THREAD_PRIORITY_INPUT ) )
309 vlc_mutex_lock( &p_sys->lock );
310 while( !p_sys->b_ready )
311 vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
312 vlc_mutex_unlock( &p_sys->lock );
317 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 );
326 /*****************************************************************************
327 * DecoderClose: close codec
328 *****************************************************************************/
329 static void DecoderClose( vlc_object_t *p_this )
331 decoder_t *p_dec = (decoder_t*)p_this;
332 decoder_sys_t *p_sys = p_dec->p_sys;
334 vlc_mutex_lock( &p_sys->lock );
335 p_sys->b_ready = false;
336 vlc_cond_signal( &p_sys->wait_input );
337 vlc_mutex_unlock( &p_sys->lock );
339 vlc_join( p_sys->thread, NULL );
340 vlc_cond_destroy( &p_sys->wait_input );
341 vlc_cond_destroy( &p_sys->wait_output );
342 vlc_mutex_destroy( &p_sys->lock );
346 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
348 decoder_sys_t *p_sys = p_dec->p_sys;
351 vlc_mutex_lock( &p_sys->lock );
352 p_sys->pp_input = pp_block;
353 vlc_cond_signal( &p_sys->wait_input );
355 while( p_sys->pp_input )
356 vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
357 p_ret = p_sys->p_output;
358 vlc_mutex_unlock( &p_sys->lock );
363 /*****************************************************************************
364 * DecOpen: open dmo codec
365 *****************************************************************************/
366 static int DecOpen( decoder_t *p_dec )
368 decoder_sys_t *p_sys = p_dec->p_sys;
370 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
371 IMediaObject *p_dmo = NULL;
372 HINSTANCE hmsdmo_dll = NULL;
374 VIDEOINFOHEADER *p_vih = NULL;
375 WAVEFORMATEX *p_wf = NULL;
378 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
380 /* Initialize OLE/COM */
384 if( LoadDMO( VLC_OBJECT(p_dec), &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, false )
392 /* Setup input format */
393 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
394 dmo_input_type.pUnk = 0;
396 if( p_dec->fmt_in.i_cat == AUDIO_ES )
399 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
400 p_wf = malloc( i_size );
402 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
403 if( p_dec->fmt_in.i_extra )
404 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
406 dmo_input_type.majortype = MEDIATYPE_Audio;
407 dmo_input_type.subtype = dmo_input_type.majortype;
408 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
409 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
410 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
412 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
413 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
414 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
415 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
416 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
417 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
418 p_wf->cbSize = p_dec->fmt_in.i_extra;
420 dmo_input_type.formattype = FORMAT_WaveFormatEx;
421 dmo_input_type.cbFormat = i_size;
422 dmo_input_type.pbFormat = (char *)p_wf;
423 dmo_input_type.bFixedSizeSamples = 1;
424 dmo_input_type.bTemporalCompression = 0;
425 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
429 BITMAPINFOHEADER *p_bih;
431 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
432 p_vih = malloc( i_size );
434 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
435 if( p_dec->fmt_in.i_extra )
436 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
438 p_bih = &p_vih->bmiHeader;
439 p_bih->biCompression = p_dec->fmt_in.i_codec;
440 p_bih->biWidth = p_dec->fmt_in.video.i_width;
441 p_bih->biHeight = p_dec->fmt_in.video.i_height;
442 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
444 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
445 sizeof(BITMAPINFOHEADER);
447 p_vih->rcSource.left = p_vih->rcSource.top = 0;
448 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
449 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
450 p_vih->rcTarget = p_vih->rcSource;
452 dmo_input_type.majortype = MEDIATYPE_Video;
453 dmo_input_type.subtype = dmo_input_type.majortype;
454 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
455 dmo_input_type.formattype = FORMAT_VideoInfo;
456 dmo_input_type.bFixedSizeSamples = 0;
457 dmo_input_type.bTemporalCompression = 1;
458 dmo_input_type.cbFormat = i_size;
459 dmo_input_type.pbFormat = (char *)p_vih;
462 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
464 msg_Err( p_dec, "can't set DMO input type" );
467 msg_Dbg( p_dec, "DMO input type set" );
469 /* Setup output format */
470 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
471 dmo_output_type.pUnk = 0;
473 if( p_dec->fmt_in.i_cat == AUDIO_ES )
475 /* Setup the format */
476 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
477 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
478 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
479 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
480 p_dec->fmt_out.audio.i_physical_channels =
481 p_dec->fmt_out.audio.i_original_channels =
482 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
484 p_wf->wFormatTag = WAVE_FORMAT_PCM;
485 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
486 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
487 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
489 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
490 p_wf->nAvgBytesPerSec =
491 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
494 dmo_output_type.majortype = MEDIATYPE_Audio;
495 dmo_output_type.formattype = FORMAT_WaveFormatEx;
496 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
497 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
498 dmo_output_type.pbFormat = (char *)p_wf;
499 dmo_output_type.bFixedSizeSamples = 1;
500 dmo_output_type.bTemporalCompression = 0;
501 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
505 BITMAPINFOHEADER *p_bih;
507 unsigned i_chroma = VLC_FOURCC('Y','U','Y','2');
508 int i_planes = 1, i_bpp = 16;
511 /* Find out which chroma to use */
512 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
514 if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
516 i_chroma = mt.subtype.Data1;
517 i_planes = 3; i_bpp = 12;
520 DMOFreeMediaType( &mt );
523 p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
524 VLC_FOURCC('I','4','2','0') : i_chroma;
525 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
526 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
527 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
529 /* If an aspect-ratio was specified in the input format then force it */
530 if( p_dec->fmt_in.video.i_aspect )
532 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
536 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
537 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
540 p_bih = &p_vih->bmiHeader;
541 p_bih->biCompression = i_chroma;
542 p_bih->biHeight *= -1;
543 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
544 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
545 p_dec->fmt_in.video.i_height *
546 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
548 p_bih->biPlanes = i_planes;
549 p_bih->biSize = sizeof(BITMAPINFOHEADER);
551 dmo_output_type.majortype = MEDIATYPE_Video;
552 dmo_output_type.formattype = FORMAT_VideoInfo;
553 dmo_output_type.subtype = dmo_output_type.majortype;
554 dmo_output_type.subtype.Data1 = p_bih->biCompression;
555 dmo_output_type.bFixedSizeSamples = true;
556 dmo_output_type.bTemporalCompression = 0;
557 dmo_output_type.lSampleSize = p_bih->biSizeImage;
558 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
559 dmo_output_type.pbFormat = (char *)p_vih;
563 /* Enumerate output types */
564 if( p_dec->fmt_in.i_cat == VIDEO_ES )
569 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
571 msg_Dbg( p_dec, "available output chroma: %4.4s",
572 (char *)&mt.subtype.Data1 );
573 DMOFreeMediaType( &mt );
578 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
580 msg_Err( p_dec, "can't set DMO output type" );
583 msg_Dbg( p_dec, "DMO output type set" );
585 /* Allocate the memory needed to store the decoder's structure */
586 p_sys->hmsdmo_dll = hmsdmo_dll;
587 p_sys->p_dmo = p_dmo;
589 p_sys->ldt_fs = ldt_fs;
592 /* Find out some properties of the output */
594 uint32_t i_size, i_align;
596 p_sys->i_min_output = 0;
597 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
599 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
604 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
606 p_sys->i_min_output = i_size;
607 p_sys->p_buffer = malloc( i_size );
608 if( !p_sys->p_buffer ) goto error;
612 /* Set output properties */
613 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
614 if( p_dec->fmt_out.i_cat == AUDIO_ES )
615 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
617 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
622 vlc_mutex_lock( &p_sys->lock );
624 p_sys->b_works = true;
625 vlc_cond_signal( &p_sys->wait_output );
626 vlc_mutex_unlock( &p_sys->lock );
632 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
633 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
636 Restore_LDT_Keeper( ldt_fs );
638 /* Uninitialize OLE/COM */
645 vlc_mutex_lock( &p_sys->lock );
646 p_sys->b_ready = true;
647 vlc_cond_signal( &p_sys->wait_output );
648 vlc_mutex_unlock( &p_sys->lock );
652 /*****************************************************************************
653 * LoadDMO: Load the DMO object
654 *****************************************************************************/
655 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
656 IMediaObject **pp_dmo, es_format_t *p_fmt,
659 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
663 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
664 const DMO_PARTIAL_MEDIATYPE *,
665 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
668 IEnumDMO *p_enum_dmo = NULL;
675 IClassFactory *cFactory = NULL;
676 IUnknown *cObject = NULL;
677 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
680 /* Look for a DMO which can handle the requested codec */
681 if( p_fmt->i_cat == AUDIO_ES )
684 dmo_partial_type.type = MEDIATYPE_Audio;
685 dmo_partial_type.subtype = dmo_partial_type.type;
686 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
687 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
688 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
692 dmo_partial_type.type = MEDIATYPE_Video;
693 dmo_partial_type.subtype = dmo_partial_type.type;
694 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
699 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
700 if( *p_hmsdmo_dll == NULL )
702 msg_Dbg( p_this, "failed loading msdmo.dll" );
705 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
706 if( OurDMOEnum == NULL )
708 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
709 FreeLibrary( *p_hmsdmo_dll );
715 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
716 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
720 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
721 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
725 FreeLibrary( *p_hmsdmo_dll );
726 /* return VLC_EGENERIC; */
727 /* Try loading the dll directly */
731 /* Pickup the first available codec */
733 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
734 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
736 char psz_temp[MAX_PATH];
737 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
738 msg_Dbg( p_this, "found DMO: %s", psz_temp );
739 CoTaskMemFree( psz_dmo_name );
742 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
743 &IID_IMediaObject, (void **)pp_dmo ) )
745 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
751 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
755 FreeLibrary( *p_hmsdmo_dll );
756 /* return VLC_EGENERIC; */
757 /* Try loading the dll directly */
766 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
768 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
771 if( codecs_table[i_codec].i_fourcc == 0 )
772 return VLC_EGENERIC; /* Can't happen */
774 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
775 if( *p_hmsdmo_dll == NULL )
777 msg_Dbg( p_this, "failed loading '%s'",
778 codecs_table[i_codec].psz_dll );
782 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
785 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
786 FreeLibrary( *p_hmsdmo_dll );
790 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
792 if( i_err || cFactory == NULL )
794 msg_Dbg( p_this, "no such class object" );
795 FreeLibrary( *p_hmsdmo_dll );
799 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
801 cFactory->vt->Release( (IUnknown*)cFactory );
802 if( i_err || !cObject )
804 msg_Dbg( p_this, "class factory failure" );
805 FreeLibrary( *p_hmsdmo_dll );
808 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
810 cObject->vt->Release( (IUnknown*)cObject );
811 if( i_err || !*pp_dmo )
813 msg_Dbg( p_this, "QueryInterface failure" );
814 FreeLibrary( *p_hmsdmo_dll );
821 static void DecClose( decoder_t *p_dec )
823 decoder_sys_t *p_sys = p_dec->p_sys;
825 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
826 FreeLibrary( p_sys->hmsdmo_dll );
830 Restore_LDT_Keeper( p_sys->ldt_fs );
833 /* Uninitialize OLE/COM */
837 free( p_sys->p_buffer );
840 /****************************************************************************
841 * DecodeBlock: the whole thing
842 ****************************************************************************
843 * This function must be fed with packets.
844 ****************************************************************************/
845 static void *DecBlock( decoder_t *p_dec, block_t **pp_block )
847 decoder_sys_t *p_sys = p_dec->p_sys;
851 DMO_OUTPUT_DATA_BUFFER db;
856 if( !pp_block ) return NULL;
860 /* Won't work with streams with B-frames, but do we have any ? */
861 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
863 /* Date management */
864 if( p_block && p_block->i_pts > 0 &&
865 p_block->i_pts != date_Get( &p_sys->end_date ) )
867 date_Set( &p_sys->end_date, p_block->i_pts );
870 #if 0 /* Breaks the video decoding */
871 if( !date_Get( &p_sys->end_date ) )
873 /* We've just started the stream, wait for the first PTS. */
874 if( p_block ) block_Release( p_block );
879 /* Feed input to the DMO */
880 if( p_block && p_block->i_buffer )
884 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, true );
886 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
887 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
890 p_in->vt->Release( (IUnknown *)p_in );
892 if( i_result == S_FALSE )
894 /* No output generated */
896 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
900 else if( i_result == (int)DMO_E_NOTACCEPTING )
902 /* Need to call ProcessOutput */
903 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
905 else if( i_result != S_OK )
907 msg_Dbg( p_dec, "ProcessInput(): failed" );
912 //msg_Dbg( p_dec, "ProcessInput(): successful" );
916 else if( p_block && !p_block->i_buffer )
918 block_Release( p_block );
922 /* Get output from the DMO */
923 block_out.p_buffer = p_sys->p_buffer;
924 block_out.i_buffer = 0;
926 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, false );
927 memset( &db, 0, sizeof(db) );
928 db.pBuffer = (IMediaBuffer *)p_out;
930 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
931 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
934 if( i_result != S_OK )
936 if( i_result != S_FALSE )
937 msg_Dbg( p_dec, "ProcessOutput(): failed" );
940 msg_Dbg( p_dec, "ProcessOutput(): no output" );
943 p_out->vt->Release( (IUnknown *)p_out );
948 msg_Dbg( p_dec, "ProcessOutput(): success" );
951 if( !block_out.i_buffer )
954 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
956 p_out->vt->Release( (IUnknown *)p_out );
960 if( p_dec->fmt_out.i_cat == VIDEO_ES )
962 /* Get a new picture */
963 picture_t *p_pic = decoder_NewPicture( p_dec );
964 if( !p_pic ) return NULL;
966 CopyPicture( p_pic, block_out.p_buffer );
968 /* Date management */
969 p_pic->date = date_Get( &p_sys->end_date );
970 date_Increment( &p_sys->end_date, 1 );
972 p_out->vt->Release( (IUnknown *)p_out );
978 aout_buffer_t *p_aout_buffer;
979 int i_samples = block_out.i_buffer /
980 ( p_dec->fmt_out.audio.i_bitspersample *
981 p_dec->fmt_out.audio.i_channels / 8 );
983 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
986 memcpy( p_aout_buffer->p_buffer,
987 block_out.p_buffer, block_out.i_buffer );
988 /* Date management */
989 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
990 p_aout_buffer->end_date =
991 date_Increment( &p_sys->end_date, i_samples );
993 p_out->vt->Release( (IUnknown *)p_out );
995 return p_aout_buffer;
1001 static void CopyPicture( picture_t *p_pic, uint8_t *p_in )
1003 int i_plane, i_line, i_width, i_dst_stride;
1004 uint8_t *p_dst, *p_src = p_in;
1006 p_dst = p_pic->p[1].p_pixels;
1007 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1008 p_pic->p[2].p_pixels = p_dst;
1010 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1012 p_dst = p_pic->p[i_plane].p_pixels;
1013 i_width = p_pic->p[i_plane].i_visible_pitch;
1014 i_dst_stride = p_pic->p[i_plane].i_pitch;
1016 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
1018 vlc_memcpy( p_dst, p_src, i_width );
1020 p_dst += i_dst_stride;
1024 p_dst = p_pic->p[1].p_pixels;
1025 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1026 p_pic->p[2].p_pixels = p_dst;
1029 static void *DecoderThread( void *data )
1031 decoder_t *p_dec = data;
1032 decoder_sys_t *p_sys = p_dec->p_sys;
1034 if( DecOpen( p_dec ) )
1035 return NULL; /* failed */
1037 vlc_mutex_lock( &p_sys->lock );
1040 while( p_sys->b_ready && !p_sys->pp_input )
1041 vlc_cond_wait( &p_sys->wait_input, &p_sys->lock );
1042 if( !p_sys->b_ready )
1045 p_sys->p_output = DecBlock( p_dec, p_sys->pp_input );
1046 p_sys->pp_input = NULL;
1047 vlc_cond_signal( &p_sys->wait_output );
1049 vlc_mutex_unlock( &p_sys->lock );
1056 /****************************************************************************
1057 * Encoder descriptor declaration
1058 ****************************************************************************/
1059 struct encoder_sys_t
1061 HINSTANCE hmsdmo_dll;
1062 IMediaObject *p_dmo;
1073 /*****************************************************************************
1074 * EncoderOpen: open dmo codec
1075 *****************************************************************************/
1076 static int EncoderOpen( vlc_object_t *p_this )
1078 encoder_t *p_enc = (encoder_t*)p_this;
1080 int i_ret = EncOpen( p_this );
1081 if( i_ret != VLC_SUCCESS ) return i_ret;
1084 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1086 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
1092 /*****************************************************************************
1093 * EncoderSetVideoType: configures the input and output types of the dmo
1094 *****************************************************************************/
1095 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1097 int i, i_selected, i_err;
1098 DMO_MEDIA_TYPE dmo_type;
1099 VIDEOINFOHEADER vih, *p_vih;
1100 BITMAPINFOHEADER *p_bih;
1103 p_enc->fmt_in.video.i_bits_per_pixel =
1104 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1106 /* Enumerate input format (for debug output) */
1108 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1110 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1112 msg_Dbg( p_enc, "available input chroma: %4.4s",
1113 (char *)&dmo_type.subtype.Data1 );
1114 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1115 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1116 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1117 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1119 DMOFreeMediaType( &dmo_type );
1122 /* Setup input format */
1123 memset( &dmo_type, 0, sizeof(dmo_type) );
1124 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1126 p_bih = &vih.bmiHeader;
1127 p_bih->biCompression = VLC_FOURCC('I','4','2','0');
1128 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1129 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1130 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1131 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1132 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1133 p_bih->biPlanes = 3;
1134 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1136 vih.rcSource.left = vih.rcSource.top = 0;
1137 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1138 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1139 vih.rcTarget = vih.rcSource;
1141 vih.AvgTimePerFrame = INT64_C(10000000) / 25; //FIXME
1143 dmo_type.majortype = MEDIATYPE_Video;
1144 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1145 dmo_type.subtype = MEDIASUBTYPE_I420;
1146 //dmo_type.subtype.Data1 = p_bih->biCompression;
1147 dmo_type.formattype = FORMAT_VideoInfo;
1148 dmo_type.bFixedSizeSamples = TRUE;
1149 dmo_type.bTemporalCompression = FALSE;
1150 dmo_type.lSampleSize = p_bih->biSizeImage;
1151 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1152 dmo_type.pbFormat = (char *)&vih;
1154 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1156 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1157 return VLC_EGENERIC;
1160 msg_Dbg( p_enc, "successfully set input type" );
1162 /* Setup output format */
1163 memset( &dmo_type, 0, sizeof(dmo_type) );
1166 /* Enumerate output types */
1167 i = 0, i_selected = -1;
1168 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1170 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1172 msg_Dbg( p_enc, "available output codec: %4.4s",
1173 (char *)&dmo_type.subtype.Data1 );
1175 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1178 DMOFreeMediaType( &dmo_type );
1181 if( i_selected < 0 )
1183 msg_Err( p_enc, "couldn't find codec: %4.4s",
1184 (char *)&p_enc->fmt_out.i_codec );
1185 return VLC_EGENERIC;
1188 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1189 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1190 p_enc->fmt_out.i_bitrate;
1192 /* Get the private data for the codec */
1195 IWMCodecPrivateData *p_privdata;
1196 VIDEOINFOHEADER *p_vih;
1197 uint8_t *p_data = 0;
1198 uint32_t i_data = 0, i_vih;
1200 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1201 &IID_IWMCodecPrivateData,
1202 (void **)&p_privdata );
1205 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1208 msg_Err( p_enc, "SetPartialOutputType() failed" );
1209 p_privdata->vt->Release( (IUnknown *)p_privdata );
1213 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1216 msg_Err( p_enc, "GetPrivateData() failed" );
1217 p_privdata->vt->Release( (IUnknown *)p_privdata );
1221 p_data = malloc( i_data );
1222 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1224 /* Update the media type with the private data */
1225 i_vih = dmo_type.cbFormat + i_data;
1226 p_vih = CoTaskMemAlloc( i_vih );
1227 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1228 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1229 DMOFreeMediaType( &dmo_type );
1230 dmo_type.pbFormat = (char*)p_vih;
1231 dmo_type.cbFormat = i_vih;
1233 msg_Dbg( p_enc, "found extra data: %i", i_data );
1234 p_enc->fmt_out.i_extra = i_data;
1235 p_enc->fmt_out.p_extra = p_data;
1239 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1241 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1242 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
1244 DMOFreeMediaType( &dmo_type );
1247 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1248 return VLC_EGENERIC;
1251 msg_Dbg( p_enc, "successfully set output type" );
1255 /*****************************************************************************
1256 * EncoderSetAudioType: configures the input and output types of the dmo
1257 *****************************************************************************/
1258 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1260 int i, i_selected, i_err;
1261 unsigned int i_last_byterate;
1263 DMO_MEDIA_TYPE dmo_type;
1266 /* Setup the format structure */
1267 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1268 if( i_tag == 0 ) return VLC_EGENERIC;
1270 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1271 p_enc->fmt_in.audio.i_bitspersample = 16;
1273 /* We first need to choose an output type from the predefined
1274 * list of choices (we cycle through the list to select the best match) */
1275 i = 0; i_selected = -1; i_last_byterate = 0;
1276 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1278 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1279 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1280 "bits per sample: %i, bitrate: %i, blockalign: %i",
1281 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1282 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1283 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1285 if( p_wf->wFormatTag == i_tag &&
1286 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1287 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1288 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1290 if( p_wf->nAvgBytesPerSec <
1291 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1292 p_wf->nAvgBytesPerSec > i_last_byterate )
1295 i_last_byterate = p_wf->nAvgBytesPerSec;
1296 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1297 i_selected, p_wf->nAvgBytesPerSec * 8 );
1301 DMOFreeMediaType( &dmo_type );
1304 if( i_selected < 0 )
1306 msg_Err( p_enc, "couldn't find a matching output" );
1307 return VLC_EGENERIC;
1310 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1311 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1313 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1314 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1315 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1316 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1317 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1319 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1320 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1321 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1322 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1323 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1327 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1328 p_enc->fmt_out.i_extra = p_wf->cbSize;
1329 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1330 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1333 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1334 DMOFreeMediaType( &dmo_type );
1338 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1339 return VLC_EGENERIC;
1342 msg_Dbg( p_enc, "successfully set output type" );
1344 /* Setup the input type */
1345 i = 0; i_selected = -1;
1346 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1348 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1349 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1350 "bits per sample: %i, bitrate: %i, blockalign: %i",
1351 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1352 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1353 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1355 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1356 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1357 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1358 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1363 DMOFreeMediaType( &dmo_type );
1366 if( i_selected < 0 )
1368 msg_Err( p_enc, "couldn't find a matching input" );
1369 return VLC_EGENERIC;
1372 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1373 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1374 DMOFreeMediaType( &dmo_type );
1377 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1378 return VLC_EGENERIC;
1380 msg_Dbg( p_enc, "successfully set input type" );
1385 /*****************************************************************************
1386 * EncOpen: open dmo codec
1387 *****************************************************************************/
1388 static int EncOpen( vlc_object_t *p_this )
1390 encoder_t *p_enc = (encoder_t*)p_this;
1391 encoder_sys_t *p_sys = NULL;
1392 IMediaObject *p_dmo = NULL;
1393 HINSTANCE hmsdmo_dll = NULL;
1396 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1398 /* Initialize OLE/COM */
1402 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, true )
1410 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1412 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1416 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1419 /* Allocate the memory needed to store the decoder's structure */
1420 if( ( p_enc->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
1425 p_sys->hmsdmo_dll = hmsdmo_dll;
1426 p_sys->p_dmo = p_dmo;
1428 p_sys->ldt_fs = ldt_fs;
1431 /* Find out some properties of the inputput */
1433 uint32_t i_size, i_align, dum;
1435 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1436 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1438 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1439 i_size, i_align, dum );
1442 /* Find out some properties of the output */
1444 uint32_t i_size, i_align;
1446 p_sys->i_min_output = 0;
1447 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1449 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1454 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1456 p_sys->i_min_output = i_size;
1460 /* Set output properties */
1461 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1462 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1463 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1465 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1471 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1472 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1475 Restore_LDT_Keeper( ldt_fs );
1477 /* Uninitialize OLE/COM */
1483 return VLC_EGENERIC;
1486 /****************************************************************************
1487 * Encode: the whole thing
1488 ****************************************************************************/
1489 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1491 encoder_sys_t *p_sys = p_enc->p_sys;
1493 block_t *p_chain = NULL;
1494 block_t *p_block_in;
1499 if( !p_data ) return NULL;
1501 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1503 /* Get picture data */
1504 int i_plane, i_line, i_width, i_src_stride;
1505 picture_t *p_pic = (picture_t *)p_data;
1508 int i_buffer = p_enc->fmt_in.video.i_width *
1509 p_enc->fmt_in.video.i_height *
1510 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1512 p_block_in = block_New( p_enc, i_buffer );
1514 /* Copy picture stride by stride */
1515 p_dst = p_block_in->p_buffer;
1516 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1518 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1519 i_width = p_pic->p[i_plane].i_visible_pitch;
1520 i_src_stride = p_pic->p[i_plane].i_pitch;
1522 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1525 vlc_memcpy( p_dst, p_src, i_width );
1527 p_src += i_src_stride;
1531 i_pts = p_pic->date;
1535 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1536 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1537 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1538 p_block_in->i_buffer );
1540 i_pts = p_aout_buffer->start_date;
1543 /* Feed input to the DMO */
1544 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, true );
1545 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1546 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1548 p_in->vt->Release( (IUnknown *)p_in );
1549 if( i_result == S_FALSE )
1551 /* No output generated */
1553 msg_Dbg( p_enc, "ProcessInput(): no output generated %"PRId64, i_pts );
1557 else if( i_result == (int)DMO_E_NOTACCEPTING )
1559 /* Need to call ProcessOutput */
1560 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1562 else if( i_result != S_OK )
1564 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1569 msg_Dbg( p_enc, "ProcessInput(): success" );
1572 /* Get output from the DMO */
1575 DMO_OUTPUT_DATA_BUFFER db;
1576 block_t *p_block_out;
1577 CMediaBuffer *p_out;
1579 p_block_out = block_New( p_enc, p_sys->i_min_output );
1580 p_block_out->i_buffer = 0;
1581 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, false);
1582 memset( &db, 0, sizeof(db) );
1583 db.pBuffer = (IMediaBuffer *)p_out;
1585 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1586 0, 1, &db, &i_status );
1588 if( i_result != S_OK )
1590 if( i_result != S_FALSE )
1591 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1594 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1597 p_out->vt->Release( (IUnknown *)p_out );
1598 block_Release( p_block_out );
1602 if( !p_block_out->i_buffer )
1605 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1607 p_out->vt->Release( (IUnknown *)p_out );
1608 block_Release( p_block_out );
1612 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1615 msg_Dbg( p_enc, "ProcessOutput(): pts: %"PRId64", %"PRId64,
1616 i_pts, db.rtTimestamp / 10 );
1618 i_pts = db.rtTimestamp / 10;
1621 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1623 p_block_out->i_length = db.rtTimelength / 10;
1625 msg_Dbg( p_enc, "ProcessOutput(): length: %"PRId64,
1626 p_block_out->i_length );
1630 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1632 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1633 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1635 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1638 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1639 block_ChainAppend( &p_chain, p_block_out );
1643 /*****************************************************************************
1644 * EncoderClose: close codec
1645 *****************************************************************************/
1646 void EncoderClose( vlc_object_t *p_this )
1648 encoder_t *p_enc = (encoder_t*)p_this;
1649 encoder_sys_t *p_sys = p_enc->p_sys;
1651 if( !p_sys ) return;
1653 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1654 FreeLibrary( p_sys->hmsdmo_dll );
1658 Restore_LDT_Keeper( p_sys->ldt_fs );
1661 /* Uninitialize OLE/COM */