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_CODEC_VC1, "wvc1dmod.dll", &guid_wvc1 },
189 { VLC_CODEC_WMV3, "wmv9dmod.dll", &guid_wmv9 },
191 { VLC_CODEC_WMV2, "wmvdmod.dll", &guid_wmv },
193 { VLC_CODEC_WMV1, "wmvdmod.dll", &guid_wmv },
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 },
203 { VLC_CODEC_WMAP, "wma9dmod.dll", &guid_wma9 },
205 { VLC_CODEC_WMA2, "wma9dmod.dll", &guid_wma9 },
208 { VLC_CODEC_WMAS, "wmspdmod.dll", &guid_wma },
214 static const codec_dll encoders_table[] =
217 { VLC_CODEC_WMV3, "wmvdmoe2.dll", &guid_wmv_enc2 },
219 { VLC_CODEC_WMV2, "wmvdmoe2.dll", &guid_wmv_enc2 },
221 { VLC_CODEC_WMV1, "wmvdmoe2.dll", &guid_wmv_enc2 },
224 { VLC_CODEC_WMAP, "wmadmoe.dll", &guid_wma_enc },
226 { VLC_CODEC_WMA2, "wmadmoe.dll", &guid_wma_enc },
232 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
234 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
235 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
241 /*****************************************************************************
242 * DecoderOpen: open dmo codec
243 *****************************************************************************/
244 static int DecoderOpen( vlc_object_t *p_this )
246 decoder_t *p_dec = (decoder_t*)p_this;
247 decoder_sys_t *p_sys;
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 */
252 /* Probe if we support it */
253 for( unsigned i = 0; decoders_table[i].i_fourcc != 0; i++ )
255 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
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 );
266 p_sys = p_dec->p_sys = malloc(sizeof(*p_sys));
271 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
273 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
276 vlc_mutex_init( &p_sys->lock );
277 vlc_cond_init( &p_sys->wait_input );
278 vlc_cond_init( &p_sys->wait_output );
280 p_sys->b_ready = false;
281 p_sys->pp_input = NULL;
282 p_sys->p_output = NULL;
284 if( vlc_clone( &p_sys->thread, DecoderThread, p_dec,
285 VLC_THREAD_PRIORITY_INPUT ) )
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 );
296 vlc_join( p_sys->thread, NULL );
298 vlc_cond_destroy( &p_sys->wait_input );
299 vlc_cond_destroy( &p_sys->wait_output );
300 vlc_mutex_destroy( &p_sys->lock );
305 /*****************************************************************************
306 * DecoderClose: close codec
307 *****************************************************************************/
308 static void DecoderClose( vlc_object_t *p_this )
310 decoder_t *p_dec = (decoder_t*)p_this;
311 decoder_sys_t *p_sys = p_dec->p_sys;
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 );
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 );
325 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
327 decoder_sys_t *p_sys = p_dec->p_sys;
330 vlc_mutex_lock( &p_sys->lock );
331 p_sys->pp_input = pp_block;
332 vlc_cond_signal( &p_sys->wait_input );
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 );
342 /*****************************************************************************
343 * DecOpen: open dmo codec
344 *****************************************************************************/
345 static int DecOpen( decoder_t *p_dec )
347 decoder_sys_t *p_sys = p_dec->p_sys;
349 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
350 IMediaObject *p_dmo = NULL;
351 HINSTANCE hmsdmo_dll = NULL;
353 VIDEOINFOHEADER *p_vih = NULL;
354 WAVEFORMATEX *p_wf = NULL;
357 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
359 /* Initialize OLE/COM */
363 if( LoadDMO( VLC_OBJECT(p_dec), &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, false )
371 /* Setup input format */
372 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
373 dmo_input_type.pUnk = 0;
375 if( p_dec->fmt_in.i_cat == AUDIO_ES )
378 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
379 p_wf = malloc( i_size );
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 );
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;
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;
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;
408 BITMAPINFOHEADER *p_bih;
410 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
411 p_vih = malloc( i_size );
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 );
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;
423 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
424 sizeof(BITMAPINFOHEADER);
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;
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;
441 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
443 msg_Err( p_dec, "can't set DMO input type" );
446 msg_Dbg( p_dec, "DMO input type set" );
448 /* Setup output format */
449 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
450 dmo_output_type.pUnk = 0;
452 if( p_dec->fmt_in.i_cat == AUDIO_ES )
454 /* Setup the format */
455 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
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];
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;
468 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
469 p_wf->nAvgBytesPerSec =
470 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
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;
484 BITMAPINFOHEADER *p_bih;
486 unsigned i_chroma = VLC_CODEC_YUYV;
487 int i_planes = 1, i_bpp = 16;
490 /* Find out which chroma to use */
491 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
493 if( mt.subtype.Data1 == VLC_CODEC_YV12 )
495 i_chroma = mt.subtype.Data1;
496 i_planes = 3; i_bpp = 12;
499 DMOFreeMediaType( &mt );
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;
508 /* If an aspect-ratio was specified in the input format then force it */
509 if( p_dec->fmt_in.video.i_aspect )
511 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
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;
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;
527 p_bih->biPlanes = i_planes;
528 p_bih->biSize = sizeof(BITMAPINFOHEADER);
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;
542 /* Enumerate output types */
543 if( p_dec->fmt_in.i_cat == VIDEO_ES )
548 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
550 msg_Dbg( p_dec, "available output chroma: %4.4s",
551 (char *)&mt.subtype.Data1 );
552 DMOFreeMediaType( &mt );
557 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
559 msg_Err( p_dec, "can't set DMO output type" );
562 msg_Dbg( p_dec, "DMO output type set" );
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;
568 p_sys->ldt_fs = ldt_fs;
571 /* Find out some properties of the output */
573 uint32_t i_size, i_align;
575 p_sys->i_min_output = 0;
576 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
578 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
583 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
585 p_sys->i_min_output = i_size;
586 p_sys->p_buffer = malloc( i_size );
587 if( !p_sys->p_buffer ) goto error;
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 );
596 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
601 vlc_mutex_lock( &p_sys->lock );
603 p_sys->b_works = true;
604 vlc_cond_signal( &p_sys->wait_output );
605 vlc_mutex_unlock( &p_sys->lock );
611 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
612 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
615 Restore_LDT_Keeper( ldt_fs );
617 /* Uninitialize OLE/COM */
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 );
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,
638 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
642 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
643 const DMO_PARTIAL_MEDIATYPE *,
644 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
647 IEnumDMO *p_enum_dmo = NULL;
654 IClassFactory *cFactory = NULL;
655 IUnknown *cObject = NULL;
656 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
659 /* Look for a DMO which can handle the requested codec */
660 if( p_fmt->i_cat == AUDIO_ES )
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;
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;
678 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
679 if( *p_hmsdmo_dll == NULL )
681 msg_Dbg( p_this, "failed loading msdmo.dll" );
684 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
685 if( OurDMOEnum == NULL )
687 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
688 FreeLibrary( *p_hmsdmo_dll );
694 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
695 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
699 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
700 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
704 FreeLibrary( *p_hmsdmo_dll );
705 /* return VLC_EGENERIC; */
706 /* Try loading the dll directly */
710 /* Pickup the first available codec */
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 */ ) ) )
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 );
721 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
722 &IID_IMediaObject, (void **)pp_dmo ) )
724 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
730 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
734 FreeLibrary( *p_hmsdmo_dll );
735 /* return VLC_EGENERIC; */
736 /* Try loading the dll directly */
745 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
747 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
750 if( codecs_table[i_codec].i_fourcc == 0 )
751 return VLC_EGENERIC; /* Can't happen */
753 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
754 if( *p_hmsdmo_dll == NULL )
756 msg_Dbg( p_this, "failed loading '%s'",
757 codecs_table[i_codec].psz_dll );
761 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
764 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
765 FreeLibrary( *p_hmsdmo_dll );
769 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
771 if( i_err || cFactory == NULL )
773 msg_Dbg( p_this, "no such class object" );
774 FreeLibrary( *p_hmsdmo_dll );
778 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
780 cFactory->vt->Release( (IUnknown*)cFactory );
781 if( i_err || !cObject )
783 msg_Dbg( p_this, "class factory failure" );
784 FreeLibrary( *p_hmsdmo_dll );
787 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
789 cObject->vt->Release( (IUnknown*)cObject );
790 if( i_err || !*pp_dmo )
792 msg_Dbg( p_this, "QueryInterface failure" );
793 FreeLibrary( *p_hmsdmo_dll );
800 static void DecClose( decoder_t *p_dec )
802 decoder_sys_t *p_sys = p_dec->p_sys;
804 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
805 FreeLibrary( p_sys->hmsdmo_dll );
809 Restore_LDT_Keeper( p_sys->ldt_fs );
812 /* Uninitialize OLE/COM */
816 free( p_sys->p_buffer );
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 )
826 decoder_sys_t *p_sys = p_dec->p_sys;
830 DMO_OUTPUT_DATA_BUFFER db;
835 if( !pp_block ) return NULL;
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;
842 /* Date management */
843 if( p_block && p_block->i_pts > 0 &&
844 p_block->i_pts != date_Get( &p_sys->end_date ) )
846 date_Set( &p_sys->end_date, p_block->i_pts );
849 #if 0 /* Breaks the video decoding */
850 if( !date_Get( &p_sys->end_date ) )
852 /* We've just started the stream, wait for the first PTS. */
853 if( p_block ) block_Release( p_block );
858 /* Feed input to the DMO */
859 if( p_block && p_block->i_buffer )
863 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, true );
865 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
866 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
869 p_in->vt->Release( (IUnknown *)p_in );
871 if( i_result == S_FALSE )
873 /* No output generated */
875 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
879 else if( i_result == (int)DMO_E_NOTACCEPTING )
881 /* Need to call ProcessOutput */
882 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
884 else if( i_result != S_OK )
886 msg_Dbg( p_dec, "ProcessInput(): failed" );
891 //msg_Dbg( p_dec, "ProcessInput(): successful" );
895 else if( p_block && !p_block->i_buffer )
897 block_Release( p_block );
901 /* Get output from the DMO */
902 block_out.p_buffer = p_sys->p_buffer;
903 block_out.i_buffer = 0;
905 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, false );
906 memset( &db, 0, sizeof(db) );
907 db.pBuffer = (IMediaBuffer *)p_out;
909 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
910 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
913 if( i_result != S_OK )
915 if( i_result != S_FALSE )
916 msg_Dbg( p_dec, "ProcessOutput(): failed" );
919 msg_Dbg( p_dec, "ProcessOutput(): no output" );
922 p_out->vt->Release( (IUnknown *)p_out );
927 msg_Dbg( p_dec, "ProcessOutput(): success" );
930 if( !block_out.i_buffer )
933 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
935 p_out->vt->Release( (IUnknown *)p_out );
939 if( p_dec->fmt_out.i_cat == VIDEO_ES )
941 /* Get a new picture */
942 picture_t *p_pic = decoder_NewPicture( p_dec );
943 if( !p_pic ) return NULL;
945 CopyPicture( p_pic, block_out.p_buffer );
947 /* Date management */
948 p_pic->date = date_Get( &p_sys->end_date );
949 date_Increment( &p_sys->end_date, 1 );
951 p_out->vt->Release( (IUnknown *)p_out );
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 );
962 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
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 );
972 p_out->vt->Release( (IUnknown *)p_out );
974 return p_aout_buffer;
980 static void CopyPicture( picture_t *p_pic, uint8_t *p_in )
982 int i_plane, i_line, i_width, i_dst_stride;
983 uint8_t *p_dst, *p_src = p_in;
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;
989 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
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;
995 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
997 vlc_memcpy( p_dst, p_src, i_width );
999 p_dst += i_dst_stride;
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;
1008 static void *DecoderThread( void *data )
1010 decoder_t *p_dec = data;
1011 decoder_sys_t *p_sys = p_dec->p_sys;
1013 if( DecOpen( p_dec ) )
1014 return NULL; /* failed */
1016 vlc_mutex_lock( &p_sys->lock );
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 )
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 );
1028 vlc_mutex_unlock( &p_sys->lock );
1035 /****************************************************************************
1036 * Encoder descriptor declaration
1037 ****************************************************************************/
1038 struct encoder_sys_t
1040 HINSTANCE hmsdmo_dll;
1041 IMediaObject *p_dmo;
1052 /*****************************************************************************
1053 * EncoderOpen: open dmo codec
1054 *****************************************************************************/
1055 static int EncoderOpen( vlc_object_t *p_this )
1057 encoder_t *p_enc = (encoder_t*)p_this;
1059 int i_ret = EncOpen( p_this );
1060 if( i_ret != VLC_SUCCESS ) return i_ret;
1063 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1065 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
1071 /*****************************************************************************
1072 * EncoderSetVideoType: configures the input and output types of the dmo
1073 *****************************************************************************/
1074 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1076 int i, i_selected, i_err;
1077 DMO_MEDIA_TYPE dmo_type;
1078 VIDEOINFOHEADER vih, *p_vih;
1079 BITMAPINFOHEADER *p_bih;
1082 p_enc->fmt_in.video.i_bits_per_pixel =
1083 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1085 /* Enumerate input format (for debug output) */
1087 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1089 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
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" );
1098 DMOFreeMediaType( &dmo_type );
1101 /* Setup input format */
1102 memset( &dmo_type, 0, sizeof(dmo_type) );
1103 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
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);
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;
1120 vih.AvgTimePerFrame = INT64_C(10000000) / 25; //FIXME
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;
1133 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1135 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1136 return VLC_EGENERIC;
1139 msg_Dbg( p_enc, "successfully set input type" );
1141 /* Setup output format */
1142 memset( &dmo_type, 0, sizeof(dmo_type) );
1145 /* Enumerate output types */
1146 i = 0, i_selected = -1;
1147 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1149 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1151 msg_Dbg( p_enc, "available output codec: %4.4s",
1152 (char *)&dmo_type.subtype.Data1 );
1154 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1157 DMOFreeMediaType( &dmo_type );
1160 if( i_selected < 0 )
1162 msg_Err( p_enc, "couldn't find codec: %4.4s",
1163 (char *)&p_enc->fmt_out.i_codec );
1164 return VLC_EGENERIC;
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;
1171 /* Get the private data for the codec */
1174 IWMCodecPrivateData *p_privdata;
1175 VIDEOINFOHEADER *p_vih;
1176 uint8_t *p_data = 0;
1177 uint32_t i_data = 0, i_vih;
1179 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1180 &IID_IWMCodecPrivateData,
1181 (void **)&p_privdata );
1184 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1187 msg_Err( p_enc, "SetPartialOutputType() failed" );
1188 p_privdata->vt->Release( (IUnknown *)p_privdata );
1192 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1195 msg_Err( p_enc, "GetPrivateData() failed" );
1196 p_privdata->vt->Release( (IUnknown *)p_privdata );
1200 p_data = malloc( i_data );
1201 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
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;
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;
1218 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1220 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1221 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
1223 DMOFreeMediaType( &dmo_type );
1226 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1227 return VLC_EGENERIC;
1230 msg_Dbg( p_enc, "successfully set output type" );
1234 /*****************************************************************************
1235 * EncoderSetAudioType: configures the input and output types of the dmo
1236 *****************************************************************************/
1237 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1239 int i, i_selected, i_err;
1240 unsigned int i_last_byterate;
1242 DMO_MEDIA_TYPE dmo_type;
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;
1249 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1250 p_enc->fmt_in.audio.i_bitspersample = 16;
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 ) )
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 );
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 )
1269 if( p_wf->nAvgBytesPerSec <
1270 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1271 p_wf->nAvgBytesPerSec > i_last_byterate )
1274 i_last_byterate = p_wf->nAvgBytesPerSec;
1275 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1276 i_selected, p_wf->nAvgBytesPerSec * 8 );
1280 DMOFreeMediaType( &dmo_type );
1283 if( i_selected < 0 )
1285 msg_Err( p_enc, "couldn't find a matching output" );
1286 return VLC_EGENERIC;
1289 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1290 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
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 );
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;
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 );
1312 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1313 DMOFreeMediaType( &dmo_type );
1317 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1318 return VLC_EGENERIC;
1321 msg_Dbg( p_enc, "successfully set output type" );
1323 /* Setup the input type */
1324 i = 0; i_selected = -1;
1325 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
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 );
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 )
1342 DMOFreeMediaType( &dmo_type );
1345 if( i_selected < 0 )
1347 msg_Err( p_enc, "couldn't find a matching input" );
1348 return VLC_EGENERIC;
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 );
1356 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1357 return VLC_EGENERIC;
1359 msg_Dbg( p_enc, "successfully set input type" );
1364 /*****************************************************************************
1365 * EncOpen: open dmo codec
1366 *****************************************************************************/
1367 static int EncOpen( vlc_object_t *p_this )
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;
1375 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1377 /* Initialize OLE/COM */
1381 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, true )
1389 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1391 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1395 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1398 /* Allocate the memory needed to store the decoder's structure */
1399 if( ( p_enc->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
1404 p_sys->hmsdmo_dll = hmsdmo_dll;
1405 p_sys->p_dmo = p_dmo;
1407 p_sys->ldt_fs = ldt_fs;
1410 /* Find out some properties of the inputput */
1412 uint32_t i_size, i_align, dum;
1414 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1415 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1417 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1418 i_size, i_align, dum );
1421 /* Find out some properties of the output */
1423 uint32_t i_size, i_align;
1425 p_sys->i_min_output = 0;
1426 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1428 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1433 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1435 p_sys->i_min_output = i_size;
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 );
1444 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1450 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1451 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1454 Restore_LDT_Keeper( ldt_fs );
1456 /* Uninitialize OLE/COM */
1462 return VLC_EGENERIC;
1465 /****************************************************************************
1466 * Encode: the whole thing
1467 ****************************************************************************/
1468 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1470 encoder_sys_t *p_sys = p_enc->p_sys;
1472 block_t *p_chain = NULL;
1473 block_t *p_block_in;
1478 if( !p_data ) return NULL;
1480 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1482 /* Get picture data */
1483 int i_plane, i_line, i_width, i_src_stride;
1484 picture_t *p_pic = (picture_t *)p_data;
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;
1491 p_block_in = block_New( p_enc, i_buffer );
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++ )
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;
1501 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1504 vlc_memcpy( p_dst, p_src, i_width );
1506 p_src += i_src_stride;
1510 i_pts = p_pic->date;
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 );
1519 i_pts = p_aout_buffer->start_date;
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 );
1527 p_in->vt->Release( (IUnknown *)p_in );
1528 if( i_result == S_FALSE )
1530 /* No output generated */
1532 msg_Dbg( p_enc, "ProcessInput(): no output generated %"PRId64, i_pts );
1536 else if( i_result == (int)DMO_E_NOTACCEPTING )
1538 /* Need to call ProcessOutput */
1539 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1541 else if( i_result != S_OK )
1543 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1548 msg_Dbg( p_enc, "ProcessInput(): success" );
1551 /* Get output from the DMO */
1554 DMO_OUTPUT_DATA_BUFFER db;
1555 block_t *p_block_out;
1556 CMediaBuffer *p_out;
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;
1564 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1565 0, 1, &db, &i_status );
1567 if( i_result != S_OK )
1569 if( i_result != S_FALSE )
1570 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1573 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1576 p_out->vt->Release( (IUnknown *)p_out );
1577 block_Release( p_block_out );
1581 if( !p_block_out->i_buffer )
1584 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1586 p_out->vt->Release( (IUnknown *)p_out );
1587 block_Release( p_block_out );
1591 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1594 msg_Dbg( p_enc, "ProcessOutput(): pts: %"PRId64", %"PRId64,
1595 i_pts, db.rtTimestamp / 10 );
1597 i_pts = db.rtTimestamp / 10;
1600 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1602 p_block_out->i_length = db.rtTimelength / 10;
1604 msg_Dbg( p_enc, "ProcessOutput(): length: %"PRId64,
1605 p_block_out->i_length );
1609 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1611 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1612 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1614 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1617 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1618 block_ChainAppend( &p_chain, p_block_out );
1622 /*****************************************************************************
1623 * EncoderClose: close codec
1624 *****************************************************************************/
1625 void EncoderClose( vlc_object_t *p_this )
1627 encoder_t *p_enc = (encoder_t*)p_this;
1628 encoder_sys_t *p_sys = p_enc->p_sys;
1630 if( !p_sys ) return;
1632 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1633 FreeLibrary( p_sys->hmsdmo_dll );
1637 Restore_LDT_Keeper( p_sys->ldt_fs );
1640 /* Uninitialize OLE/COM */