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>
44 /* Need the w32dll loader from mplayer */
45 # include <wine/winerror.h>
46 # include <ldt_keeper.h>
47 # include <wine/windef.h>
50 #include <vlc_codecs.h>
57 long CoInitialize( void *pvReserved ) { VLC_UNUSED(pvReserved); return -1; }
58 void CoUninitialize( void ) { }
60 /* A few prototypes */
61 HMODULE WINAPI LoadLibraryA(LPCSTR);
62 #define LoadLibrary LoadLibraryA
63 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
64 int WINAPI FreeLibrary(HMODULE);
67 typedef long (STDCALL *GETCLASS) ( const GUID*, const GUID*, void** );
69 static const int pi_channels_maps[7] =
73 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
74 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
75 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
76 | AOUT_CHAN_REARRIGHT,
77 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
78 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
79 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
80 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
83 /*****************************************************************************
85 *****************************************************************************/
86 static int DecoderOpen ( vlc_object_t * );
87 static void DecoderClose ( vlc_object_t * );
88 static void *DecodeBlock ( decoder_t *, block_t ** );
89 static void *DecoderThread( void * );
90 static int EncoderOpen ( vlc_object_t * );
91 static void EncoderClose ( vlc_object_t * );
92 static block_t *EncodeBlock( encoder_t *, void * );
94 static int EncOpen ( vlc_object_t * );
96 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
97 es_format_t *, bool );
98 static void CopyPicture( picture_t *, uint8_t * );
101 set_description( N_("DirectMedia Object decoder") )
102 add_shortcut( "dmo" )
103 set_capability( "decoder", 1 )
104 set_callbacks( DecoderOpen, DecoderClose )
105 set_category( CAT_INPUT )
106 set_subcategory( SUBCAT_INPUT_SCODEC )
108 # define ENC_CFG_PREFIX "sout-dmo-"
110 set_description( N_("DirectMedia Object encoder") )
111 add_shortcut( "dmo" )
112 set_capability( "encoder", 10 )
113 set_callbacks( EncoderOpen, EncoderClose )
117 /*****************************************************************************
119 *****************************************************************************/
121 /****************************************************************************
122 * Decoder descriptor declaration
123 ****************************************************************************/
126 HINSTANCE hmsdmo_dll;
140 vlc_cond_t wait_input, wait_output;
141 bool b_ready, b_works;
148 const GUID IID_IWMCodecPrivateData = {0x73f0be8e, 0x57f7, 0x4f01, {0xaa, 0x66, 0x9f, 0x57, 0x34, 0xc, 0xfe, 0xe}};
149 const GUID IID_IMediaObject = {0xd8ad0f58, 0x5494, 0x4102, {0x97, 0xc5, 0xec, 0x79, 0x8e, 0x59, 0xbc, 0xf4}};
150 const GUID IID_IMediaBuffer = {0x59eff8b9, 0x938c, 0x4a26, {0x82, 0xf2, 0x95, 0xcb, 0x84, 0xcd, 0xc8, 0x37}};
151 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
152 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
153 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
154 const GUID FORMAT_VideoInfo = {0x05589f80, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
155 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
156 const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
157 const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
158 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
159 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
160 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
163 static const GUID guid_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
164 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
166 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
167 static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
168 static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };
170 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
171 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
173 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
174 static const GUID guid_wmv_enc2 = { 0x96b57cdd, 0x8966, 0x410c,{ 0xbb, 0x1f, 0xc9, 0x7e, 0xea, 0x76, 0x5c, 0x04 } };
175 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
179 vlc_fourcc_t i_fourcc;
185 static const codec_dll decoders_table[] =
188 { VLC_CODEC_VC1, "wvc1dmod.dll", &guid_wvc1 },
190 { VLC_CODEC_WMV3, "wmv9dmod.dll", &guid_wmv9 },
192 { VLC_CODEC_WMV2, "wmvdmod.dll", &guid_wmv },
194 { VLC_CODEC_WMV1, "wmvdmod.dll", &guid_wmv },
196 { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
197 { VLC_FOURCC('m','s','s','2'), "wmsdmod.dll", &guid_wms },
198 { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
199 { VLC_FOURCC('m','s','s','1'), "wmsdmod.dll", &guid_wms },
200 /* Windows Media Video Adv */
201 { VLC_CODEC_WMVA, "wmvadvd.dll", &guid_wmva },
204 { VLC_CODEC_WMAP, "wma9dmod.dll", &guid_wma9 },
205 { VLC_CODEC_WMAL, "wma9dmod.dll", &guid_wma9 },
208 { VLC_CODEC_WMA2, "wma9dmod.dll", &guid_wma9 },
211 { VLC_CODEC_WMAS, "wmspdmod.dll", &guid_wma },
217 static const codec_dll encoders_table[] =
220 { VLC_CODEC_WMV3, "wmvdmoe2.dll", &guid_wmv_enc2 },
222 { VLC_CODEC_WMV2, "wmvdmoe2.dll", &guid_wmv_enc2 },
224 { VLC_CODEC_WMV1, "wmvdmoe2.dll", &guid_wmv_enc2 },
227 { VLC_CODEC_WMAP, "wmadmoe.dll", &guid_wma_enc },
229 { VLC_CODEC_WMA2, "wmadmoe.dll", &guid_wma_enc },
235 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
237 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
238 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
244 /*****************************************************************************
245 * DecoderOpen: open dmo codec
246 *****************************************************************************/
247 static int DecoderOpen( vlc_object_t *p_this )
249 decoder_t *p_dec = (decoder_t*)p_this;
250 decoder_sys_t *p_sys;
252 /* We can't open it now, because of ldt_keeper or something
253 * Open/Decode/Close has to be done in the same thread */
255 /* Probe if we support it */
256 for( unsigned i = 0; decoders_table[i].i_fourcc != 0; i++ )
258 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
260 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
261 (char*)&p_dec->fmt_in.i_codec,
262 decoders_table[i].psz_dll );
269 p_sys = p_dec->p_sys = malloc(sizeof(*p_sys));
274 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
276 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
279 vlc_mutex_init( &p_sys->lock );
280 vlc_cond_init( &p_sys->wait_input );
281 vlc_cond_init( &p_sys->wait_output );
283 p_sys->b_ready = false;
284 p_sys->pp_input = NULL;
285 TAB_INIT( p_sys->i_output, p_sys->pp_output );
287 if( vlc_clone( &p_sys->thread, DecoderThread, p_dec,
288 VLC_THREAD_PRIORITY_INPUT ) )
291 vlc_mutex_lock( &p_sys->lock );
292 while( !p_sys->b_ready )
293 vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
294 vlc_mutex_unlock( &p_sys->lock );
299 vlc_join( p_sys->thread, NULL );
301 vlc_cond_destroy( &p_sys->wait_input );
302 vlc_cond_destroy( &p_sys->wait_output );
303 vlc_mutex_destroy( &p_sys->lock );
308 /*****************************************************************************
309 * DecoderClose: close codec
310 *****************************************************************************/
311 static void DecoderClose( vlc_object_t *p_this )
313 decoder_t *p_dec = (decoder_t*)p_this;
314 decoder_sys_t *p_sys = p_dec->p_sys;
316 vlc_mutex_lock( &p_sys->lock );
317 p_sys->b_ready = false;
318 vlc_cond_signal( &p_sys->wait_input );
319 vlc_mutex_unlock( &p_sys->lock );
321 vlc_join( p_sys->thread, NULL );
322 TAB_CLEAN( p_sys->i_output, p_sys->pp_output );
323 vlc_cond_destroy( &p_sys->wait_input );
324 vlc_cond_destroy( &p_sys->wait_output );
325 vlc_mutex_destroy( &p_sys->lock );
329 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
331 decoder_sys_t *p_sys = p_dec->p_sys;
334 vlc_mutex_lock( &p_sys->lock );
335 if( p_sys->i_output <= 0 )
337 p_sys->pp_input = pp_block;
338 vlc_cond_signal( &p_sys->wait_input );
340 while( p_sys->pp_input )
341 vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
345 if( p_sys->i_output > 0 )
347 p_ret = p_sys->pp_output[0];
348 TAB_REMOVE( p_sys->i_output, p_sys->pp_output, p_ret );
351 vlc_mutex_unlock( &p_sys->lock );
356 /*****************************************************************************
357 * DecOpen: open dmo codec
358 *****************************************************************************/
359 static int DecOpen( decoder_t *p_dec )
361 decoder_sys_t *p_sys = p_dec->p_sys;
363 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
364 IMediaObject *p_dmo = NULL;
365 HINSTANCE hmsdmo_dll = NULL;
367 VIDEOINFOHEADER *p_vih = NULL;
368 WAVEFORMATEX *p_wf = NULL;
371 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
373 /* Initialize OLE/COM */
377 if( LoadDMO( VLC_OBJECT(p_dec), &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, false )
385 /* Setup input format */
386 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
387 dmo_input_type.pUnk = 0;
389 if( p_dec->fmt_in.i_cat == AUDIO_ES )
392 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
393 p_wf = malloc( i_size );
395 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
396 if( p_dec->fmt_in.i_extra )
397 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
399 dmo_input_type.majortype = MEDIATYPE_Audio;
400 dmo_input_type.subtype = dmo_input_type.majortype;
401 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec;
402 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
403 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
405 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
406 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
407 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
408 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
409 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
410 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
411 p_wf->cbSize = p_dec->fmt_in.i_extra;
413 dmo_input_type.formattype = FORMAT_WaveFormatEx;
414 dmo_input_type.cbFormat = i_size;
415 dmo_input_type.pbFormat = (char *)p_wf;
416 dmo_input_type.bFixedSizeSamples = 1;
417 dmo_input_type.bTemporalCompression = 0;
418 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
422 BITMAPINFOHEADER *p_bih;
424 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
425 p_vih = malloc( i_size );
427 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
428 if( p_dec->fmt_in.i_extra )
429 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
431 p_bih = &p_vih->bmiHeader;
432 p_bih->biCompression = p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec;
433 p_bih->biWidth = p_dec->fmt_in.video.i_width;
434 p_bih->biHeight = p_dec->fmt_in.video.i_height;
435 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
437 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
438 sizeof(BITMAPINFOHEADER);
440 p_vih->rcSource.left = p_vih->rcSource.top = 0;
441 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
442 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
443 p_vih->rcTarget = p_vih->rcSource;
445 dmo_input_type.majortype = MEDIATYPE_Video;
446 dmo_input_type.subtype = dmo_input_type.majortype;
447 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec;
448 dmo_input_type.formattype = FORMAT_VideoInfo;
449 dmo_input_type.bFixedSizeSamples = 0;
450 dmo_input_type.bTemporalCompression = 1;
451 dmo_input_type.cbFormat = i_size;
452 dmo_input_type.pbFormat = (char *)p_vih;
455 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
457 msg_Err( p_dec, "can't set DMO input type" );
460 msg_Dbg( p_dec, "DMO input type set" );
462 /* Setup output format */
463 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
464 dmo_output_type.pUnk = 0;
466 if( p_dec->fmt_in.i_cat == AUDIO_ES )
468 /* Setup the format */
469 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
470 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
471 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
472 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
473 p_dec->fmt_out.audio.i_physical_channels =
474 p_dec->fmt_out.audio.i_original_channels =
475 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
477 p_wf->wFormatTag = WAVE_FORMAT_PCM;
478 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
479 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
480 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
482 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
483 p_wf->nAvgBytesPerSec =
484 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
487 dmo_output_type.majortype = MEDIATYPE_Audio;
488 dmo_output_type.formattype = FORMAT_WaveFormatEx;
489 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
490 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
491 dmo_output_type.pbFormat = (char *)p_wf;
492 dmo_output_type.bFixedSizeSamples = 1;
493 dmo_output_type.bTemporalCompression = 0;
494 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
498 BITMAPINFOHEADER *p_bih;
500 unsigned i_chroma = VLC_CODEC_YUYV;
501 int i_planes = 1, i_bpp = 16;
504 /* Find out which chroma to use */
505 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
507 if( mt.subtype.Data1 == VLC_CODEC_YV12 )
509 i_chroma = mt.subtype.Data1;
510 i_planes = 3; i_bpp = 12;
513 DMOFreeMediaType( &mt );
516 p_dec->fmt_out.i_codec = i_chroma == VLC_CODEC_YV12 ?
517 VLC_CODEC_I420 : i_chroma;
518 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
519 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
520 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
522 /* If an aspect-ratio was specified in the input format then force it */
523 if( p_dec->fmt_in.video.i_sar_num > 0 &&
524 p_dec->fmt_in.video.i_sar_den > 0 )
526 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
527 p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
531 p_dec->fmt_out.video.i_sar_num = 1;
532 p_dec->fmt_out.video.i_sar_den = 1;
535 p_bih = &p_vih->bmiHeader;
536 p_bih->biCompression = i_chroma;
537 p_bih->biHeight *= -1;
538 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
539 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
540 p_dec->fmt_in.video.i_height *
541 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
543 p_bih->biPlanes = i_planes;
544 p_bih->biSize = sizeof(BITMAPINFOHEADER);
546 dmo_output_type.majortype = MEDIATYPE_Video;
547 dmo_output_type.formattype = FORMAT_VideoInfo;
548 dmo_output_type.subtype = dmo_output_type.majortype;
549 dmo_output_type.subtype.Data1 = p_bih->biCompression;
550 dmo_output_type.bFixedSizeSamples = true;
551 dmo_output_type.bTemporalCompression = 0;
552 dmo_output_type.lSampleSize = p_bih->biSizeImage;
553 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
554 dmo_output_type.pbFormat = (char *)p_vih;
558 /* Enumerate output types */
559 if( p_dec->fmt_in.i_cat == VIDEO_ES )
564 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
566 msg_Dbg( p_dec, "available output chroma: %4.4s",
567 (char *)&mt.subtype.Data1 );
568 DMOFreeMediaType( &mt );
573 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
575 msg_Err( p_dec, "can't set DMO output type" );
578 msg_Dbg( p_dec, "DMO output type set" );
580 /* Allocate the memory needed to store the decoder's structure */
581 p_sys->hmsdmo_dll = hmsdmo_dll;
582 p_sys->p_dmo = p_dmo;
584 p_sys->ldt_fs = ldt_fs;
587 /* Find out some properties of the output */
589 uint32_t i_size, i_align;
591 p_sys->i_min_output = 0;
592 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
594 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
599 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
601 p_sys->i_min_output = i_size;
602 p_sys->p_buffer = malloc( i_size );
603 if( !p_sys->p_buffer ) goto error;
607 /* Set output properties */
608 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
609 if( p_dec->fmt_out.i_cat == AUDIO_ES )
610 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
612 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
617 vlc_mutex_lock( &p_sys->lock );
619 p_sys->b_works = true;
620 vlc_cond_signal( &p_sys->wait_output );
621 vlc_mutex_unlock( &p_sys->lock );
627 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
628 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
631 Restore_LDT_Keeper( ldt_fs );
633 /* Uninitialize OLE/COM */
640 vlc_mutex_lock( &p_sys->lock );
641 p_sys->b_ready = true;
642 vlc_cond_signal( &p_sys->wait_output );
643 vlc_mutex_unlock( &p_sys->lock );
647 /*****************************************************************************
648 * LoadDMO: Load the DMO object
649 *****************************************************************************/
650 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
651 IMediaObject **pp_dmo, es_format_t *p_fmt,
654 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
658 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
659 const DMO_PARTIAL_MEDIATYPE *,
660 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
663 IEnumDMO *p_enum_dmo = NULL;
670 IClassFactory *cFactory = NULL;
671 IUnknown *cObject = NULL;
672 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
675 /* Look for a DMO which can handle the requested codec */
676 if( p_fmt->i_cat == AUDIO_ES )
679 dmo_partial_type.type = MEDIATYPE_Audio;
680 dmo_partial_type.subtype = dmo_partial_type.type;
681 dmo_partial_type.subtype.Data1 = p_fmt->i_original_fourcc ?: p_fmt->i_codec;
682 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
683 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
687 dmo_partial_type.type = MEDIATYPE_Video;
688 dmo_partial_type.subtype = dmo_partial_type.type;
689 dmo_partial_type.subtype.Data1 = p_fmt->i_original_fourcc ?: p_fmt->i_codec;
694 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
695 if( *p_hmsdmo_dll == NULL )
697 msg_Dbg( p_this, "failed loading msdmo.dll" );
700 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
701 if( OurDMOEnum == NULL )
703 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
704 FreeLibrary( *p_hmsdmo_dll );
710 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
711 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
715 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
716 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
720 FreeLibrary( *p_hmsdmo_dll );
721 /* return VLC_EGENERIC; */
722 /* Try loading the dll directly */
726 /* Pickup the first available codec */
728 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
729 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
731 char psz_temp[MAX_PATH];
732 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
733 msg_Dbg( p_this, "found DMO: %s", psz_temp );
734 CoTaskMemFree( psz_dmo_name );
737 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
738 &IID_IMediaObject, (void **)pp_dmo ) )
740 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
746 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
750 FreeLibrary( *p_hmsdmo_dll );
751 /* return VLC_EGENERIC; */
752 /* Try loading the dll directly */
761 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
763 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
766 if( codecs_table[i_codec].i_fourcc == 0 )
767 return VLC_EGENERIC; /* Can't happen */
769 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
770 if( *p_hmsdmo_dll == NULL )
772 msg_Dbg( p_this, "failed loading '%s'",
773 codecs_table[i_codec].psz_dll );
777 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
780 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
781 FreeLibrary( *p_hmsdmo_dll );
785 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
787 if( i_err || cFactory == NULL )
789 msg_Dbg( p_this, "no such class object" );
790 FreeLibrary( *p_hmsdmo_dll );
794 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
796 cFactory->vt->Release( (IUnknown*)cFactory );
797 if( i_err || !cObject )
799 msg_Dbg( p_this, "class factory failure" );
800 FreeLibrary( *p_hmsdmo_dll );
803 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
805 cObject->vt->Release( (IUnknown*)cObject );
806 if( i_err || !*pp_dmo )
808 msg_Dbg( p_this, "QueryInterface failure" );
809 FreeLibrary( *p_hmsdmo_dll );
816 static void DecClose( decoder_t *p_dec )
818 decoder_sys_t *p_sys = p_dec->p_sys;
820 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
821 FreeLibrary( p_sys->hmsdmo_dll );
825 Restore_LDT_Keeper( p_sys->ldt_fs );
828 /* Uninitialize OLE/COM */
832 free( p_sys->p_buffer );
835 /****************************************************************************
836 * DecodeBlock: the whole thing
837 ****************************************************************************
838 * This function must be fed with packets.
839 ****************************************************************************/
840 static void *DecBlock( decoder_t *p_dec, block_t **pp_block )
842 decoder_sys_t *p_sys = p_dec->p_sys;
846 DMO_OUTPUT_DATA_BUFFER db;
851 if( !pp_block ) return NULL;
855 /* Won't work with streams with B-frames, but do we have any ? */
856 if( p_block && p_block->i_pts <= VLC_TS_INVALID )
857 p_block->i_pts = p_block->i_dts;
859 /* Date management */
860 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
861 p_block->i_pts != date_Get( &p_sys->end_date ) )
863 date_Set( &p_sys->end_date, p_block->i_pts );
866 #if 0 /* Breaks the video decoding */
867 if( !date_Get( &p_sys->end_date ) )
869 /* We've just started the stream, wait for the first PTS. */
870 if( p_block ) block_Release( p_block );
875 /* Feed input to the DMO */
876 if( p_block && p_block->i_buffer )
880 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, true );
882 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
883 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
886 p_in->vt->Release( (IUnknown *)p_in );
888 if( i_result == S_FALSE )
890 /* No output generated */
892 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
896 else if( i_result == (int)DMO_E_NOTACCEPTING )
898 /* Need to call ProcessOutput */
899 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
901 else if( i_result != S_OK )
903 msg_Dbg( p_dec, "ProcessInput(): failed" );
909 msg_Dbg( p_dec, "ProcessInput(): successful" );
914 else if( p_block && !p_block->i_buffer )
916 block_Release( p_block );
920 /* Get output from the DMO */
921 block_out.p_buffer = p_sys->p_buffer;
922 block_out.i_buffer = 0;
924 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, false );
925 memset( &db, 0, sizeof(db) );
926 db.pBuffer = (IMediaBuffer *)p_out;
928 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
929 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
932 if( i_result != S_OK )
934 if( i_result != S_FALSE )
935 msg_Dbg( p_dec, "ProcessOutput(): failed" );
938 msg_Dbg( p_dec, "ProcessOutput(): no output" );
941 p_out->vt->Release( (IUnknown *)p_out );
946 msg_Dbg( p_dec, "ProcessOutput(): success" );
949 if( !block_out.i_buffer )
952 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
954 p_out->vt->Release( (IUnknown *)p_out );
958 if( p_dec->fmt_out.i_cat == VIDEO_ES )
960 /* Get a new picture */
961 picture_t *p_pic = decoder_NewPicture( p_dec );
962 if( !p_pic ) return NULL;
964 CopyPicture( p_pic, block_out.p_buffer );
966 /* Date management */
967 p_pic->date = date_Get( &p_sys->end_date );
968 date_Increment( &p_sys->end_date, 1 );
970 p_out->vt->Release( (IUnknown *)p_out );
976 aout_buffer_t *p_aout_buffer;
977 int i_samples = block_out.i_buffer /
978 ( p_dec->fmt_out.audio.i_bitspersample *
979 p_dec->fmt_out.audio.i_channels / 8 );
981 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
984 memcpy( p_aout_buffer->p_buffer,
985 block_out.p_buffer, block_out.i_buffer );
986 /* Date management */
987 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
988 p_aout_buffer->i_length =
989 date_Increment( &p_sys->end_date, i_samples )
990 - p_aout_buffer->i_pts;
992 p_out->vt->Release( (IUnknown *)p_out );
994 return p_aout_buffer;
1000 static void CopyPicture( picture_t *p_pic, uint8_t *p_in )
1002 int i_plane, i_line, i_width, i_dst_stride;
1003 uint8_t *p_dst, *p_src = p_in;
1005 p_dst = p_pic->p[1].p_pixels;
1006 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1007 p_pic->p[2].p_pixels = p_dst;
1009 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1011 p_dst = p_pic->p[i_plane].p_pixels;
1012 i_width = p_pic->p[i_plane].i_visible_pitch;
1013 i_dst_stride = p_pic->p[i_plane].i_pitch;
1015 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
1017 vlc_memcpy( p_dst, p_src, i_width );
1019 p_dst += i_dst_stride;
1023 p_dst = p_pic->p[1].p_pixels;
1024 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1025 p_pic->p[2].p_pixels = p_dst;
1028 static void *DecoderThread( void *data )
1030 decoder_t *p_dec = data;
1031 decoder_sys_t *p_sys = p_dec->p_sys;
1033 if( DecOpen( p_dec ) )
1034 return NULL; /* failed */
1036 vlc_mutex_lock( &p_sys->lock );
1039 while( p_sys->b_ready && !p_sys->pp_input )
1040 vlc_cond_wait( &p_sys->wait_input, &p_sys->lock );
1041 if( !p_sys->b_ready )
1046 void *p_output = DecBlock( p_dec, p_sys->pp_input );
1049 TAB_APPEND( p_sys->i_output, p_sys->pp_output, p_output );
1051 p_sys->pp_input = NULL;
1052 vlc_cond_signal( &p_sys->wait_output );
1054 vlc_mutex_unlock( &p_sys->lock );
1061 /****************************************************************************
1062 * Encoder descriptor declaration
1063 ****************************************************************************/
1064 struct encoder_sys_t
1066 HINSTANCE hmsdmo_dll;
1067 IMediaObject *p_dmo;
1078 /*****************************************************************************
1079 * EncoderOpen: open dmo codec
1080 *****************************************************************************/
1081 static int EncoderOpen( vlc_object_t *p_this )
1083 encoder_t *p_enc = (encoder_t*)p_this;
1085 int i_ret = EncOpen( p_this );
1086 if( i_ret != VLC_SUCCESS ) return i_ret;
1089 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1091 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
1097 /*****************************************************************************
1098 * EncoderSetVideoType: configures the input and output types of the dmo
1099 *****************************************************************************/
1100 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1102 int i, i_selected, i_err;
1103 DMO_MEDIA_TYPE dmo_type;
1104 VIDEOINFOHEADER vih, *p_vih;
1105 BITMAPINFOHEADER *p_bih;
1108 p_enc->fmt_in.video.i_bits_per_pixel =
1109 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1111 /* Enumerate input format (for debug output) */
1113 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1115 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1117 msg_Dbg( p_enc, "available input chroma: %4.4s",
1118 (char *)&dmo_type.subtype.Data1 );
1119 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1120 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1121 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1122 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1124 DMOFreeMediaType( &dmo_type );
1127 /* Setup input format */
1128 memset( &dmo_type, 0, sizeof(dmo_type) );
1129 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1131 p_bih = &vih.bmiHeader;
1132 p_bih->biCompression = VLC_CODEC_I420;
1133 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1134 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1135 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1136 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1137 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1138 p_bih->biPlanes = 3;
1139 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1141 vih.rcSource.left = vih.rcSource.top = 0;
1142 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1143 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1144 vih.rcTarget = vih.rcSource;
1146 vih.AvgTimePerFrame = INT64_C(10000000) / 25; //FIXME
1148 dmo_type.majortype = MEDIATYPE_Video;
1149 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1150 dmo_type.subtype = MEDIASUBTYPE_I420;
1151 //dmo_type.subtype.Data1 = p_bih->biCompression;
1152 dmo_type.formattype = FORMAT_VideoInfo;
1153 dmo_type.bFixedSizeSamples = TRUE;
1154 dmo_type.bTemporalCompression = FALSE;
1155 dmo_type.lSampleSize = p_bih->biSizeImage;
1156 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1157 dmo_type.pbFormat = (char *)&vih;
1159 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1161 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1162 return VLC_EGENERIC;
1165 msg_Dbg( p_enc, "successfully set input type" );
1167 /* Setup output format */
1168 memset( &dmo_type, 0, sizeof(dmo_type) );
1171 /* Enumerate output types */
1172 i = 0, i_selected = -1;
1173 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1175 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1177 msg_Dbg( p_enc, "available output codec: %4.4s",
1178 (char *)&dmo_type.subtype.Data1 );
1180 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1183 DMOFreeMediaType( &dmo_type );
1186 if( i_selected < 0 )
1188 msg_Err( p_enc, "couldn't find codec: %4.4s",
1189 (char *)&p_enc->fmt_out.i_codec );
1190 return VLC_EGENERIC;
1193 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1194 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1195 p_enc->fmt_out.i_bitrate;
1197 /* Get the private data for the codec */
1200 IWMCodecPrivateData *p_privdata;
1201 VIDEOINFOHEADER *p_vih;
1202 uint8_t *p_data = 0;
1203 uint32_t i_data = 0, i_vih;
1205 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1206 &IID_IWMCodecPrivateData,
1210 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1213 msg_Err( p_enc, "SetPartialOutputType() failed" );
1214 p_privdata->vt->Release( (IUnknown *)p_privdata );
1218 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1221 msg_Err( p_enc, "GetPrivateData() failed" );
1222 p_privdata->vt->Release( (IUnknown *)p_privdata );
1226 p_data = malloc( i_data );
1227 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1229 /* Update the media type with the private data */
1230 i_vih = dmo_type.cbFormat + i_data;
1231 p_vih = CoTaskMemAlloc( i_vih );
1232 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1233 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1234 DMOFreeMediaType( &dmo_type );
1235 dmo_type.pbFormat = (char*)p_vih;
1236 dmo_type.cbFormat = i_vih;
1238 msg_Dbg( p_enc, "found extra data: %i", i_data );
1239 p_enc->fmt_out.i_extra = i_data;
1240 p_enc->fmt_out.p_extra = p_data;
1244 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1246 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1247 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
1249 DMOFreeMediaType( &dmo_type );
1252 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1253 return VLC_EGENERIC;
1256 msg_Dbg( p_enc, "successfully set output type" );
1260 /*****************************************************************************
1261 * EncoderSetAudioType: configures the input and output types of the dmo
1262 *****************************************************************************/
1263 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1265 int i, i_selected, i_err;
1266 unsigned int i_last_byterate;
1268 DMO_MEDIA_TYPE dmo_type;
1271 /* Setup the format structure */
1272 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1273 if( i_tag == 0 ) return VLC_EGENERIC;
1275 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
1276 p_enc->fmt_in.audio.i_bitspersample = 16;
1278 /* We first need to choose an output type from the predefined
1279 * list of choices (we cycle through the list to select the best match) */
1280 i = 0; i_selected = -1; i_last_byterate = 0;
1281 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1283 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1284 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1285 "bits per sample: %i, bitrate: %i, blockalign: %i",
1286 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1287 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1288 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1290 if( p_wf->wFormatTag == i_tag &&
1291 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1292 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1293 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1295 if( p_wf->nAvgBytesPerSec <
1296 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1297 p_wf->nAvgBytesPerSec > i_last_byterate )
1300 i_last_byterate = p_wf->nAvgBytesPerSec;
1301 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1302 i_selected, p_wf->nAvgBytesPerSec * 8 );
1306 DMOFreeMediaType( &dmo_type );
1309 if( i_selected < 0 )
1311 msg_Err( p_enc, "couldn't find a matching output" );
1312 return VLC_EGENERIC;
1315 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1316 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1318 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1319 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1320 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1321 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1322 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1324 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1325 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1326 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1327 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1328 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1332 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1333 p_enc->fmt_out.i_extra = p_wf->cbSize;
1334 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1335 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1338 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1339 DMOFreeMediaType( &dmo_type );
1343 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1344 return VLC_EGENERIC;
1347 msg_Dbg( p_enc, "successfully set output type" );
1349 /* Setup the input type */
1350 i = 0; i_selected = -1;
1351 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1353 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1354 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1355 "bits per sample: %i, bitrate: %i, blockalign: %i",
1356 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1357 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1358 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1360 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1361 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1362 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1363 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1368 DMOFreeMediaType( &dmo_type );
1371 if( i_selected < 0 )
1373 msg_Err( p_enc, "couldn't find a matching input" );
1374 return VLC_EGENERIC;
1377 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1378 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1379 DMOFreeMediaType( &dmo_type );
1382 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1383 return VLC_EGENERIC;
1385 msg_Dbg( p_enc, "successfully set input type" );
1390 /*****************************************************************************
1391 * EncOpen: open dmo codec
1392 *****************************************************************************/
1393 static int EncOpen( vlc_object_t *p_this )
1395 encoder_t *p_enc = (encoder_t*)p_this;
1396 encoder_sys_t *p_sys = NULL;
1397 IMediaObject *p_dmo = NULL;
1398 HINSTANCE hmsdmo_dll = NULL;
1401 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1403 /* Initialize OLE/COM */
1407 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, true )
1415 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1417 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1421 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1424 /* Allocate the memory needed to store the decoder's structure */
1425 if( ( p_enc->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
1430 p_sys->hmsdmo_dll = hmsdmo_dll;
1431 p_sys->p_dmo = p_dmo;
1433 p_sys->ldt_fs = ldt_fs;
1436 /* Find out some properties of the inputput */
1438 uint32_t i_size, i_align, dum;
1440 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1441 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1443 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1444 i_size, i_align, dum );
1447 /* Find out some properties of the output */
1449 uint32_t i_size, i_align;
1451 p_sys->i_min_output = 0;
1452 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1454 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1459 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1461 p_sys->i_min_output = i_size;
1465 /* Set output properties */
1466 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1467 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1468 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1470 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1476 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1477 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1480 Restore_LDT_Keeper( ldt_fs );
1482 /* Uninitialize OLE/COM */
1488 return VLC_EGENERIC;
1491 /****************************************************************************
1492 * Encode: the whole thing
1493 ****************************************************************************/
1494 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1496 encoder_sys_t *p_sys = p_enc->p_sys;
1498 block_t *p_chain = NULL;
1499 block_t *p_block_in;
1504 if( !p_data ) return NULL;
1506 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1508 /* Get picture data */
1509 int i_plane, i_line, i_width, i_src_stride;
1510 picture_t *p_pic = (picture_t *)p_data;
1513 int i_buffer = p_enc->fmt_in.video.i_width *
1514 p_enc->fmt_in.video.i_height *
1515 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1517 p_block_in = block_New( p_enc, i_buffer );
1519 /* Copy picture stride by stride */
1520 p_dst = p_block_in->p_buffer;
1521 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1523 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1524 i_width = p_pic->p[i_plane].i_visible_pitch;
1525 i_src_stride = p_pic->p[i_plane].i_pitch;
1527 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1530 vlc_memcpy( p_dst, p_src, i_width );
1532 p_src += i_src_stride;
1536 i_pts = p_pic->date;
1540 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1541 p_block_in = block_New( p_enc, p_aout_buffer->i_buffer );
1542 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1543 p_block_in->i_buffer );
1545 i_pts = p_aout_buffer->i_pts;
1548 /* Feed input to the DMO */
1549 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, true );
1550 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1551 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1553 p_in->vt->Release( (IUnknown *)p_in );
1554 if( i_result == S_FALSE )
1556 /* No output generated */
1558 msg_Dbg( p_enc, "ProcessInput(): no output generated %"PRId64, i_pts );
1562 else if( i_result == (int)DMO_E_NOTACCEPTING )
1564 /* Need to call ProcessOutput */
1565 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1567 else if( i_result != S_OK )
1569 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1574 msg_Dbg( p_enc, "ProcessInput(): success" );
1577 /* Get output from the DMO */
1580 DMO_OUTPUT_DATA_BUFFER db;
1581 block_t *p_block_out;
1582 CMediaBuffer *p_out;
1584 p_block_out = block_New( p_enc, p_sys->i_min_output );
1585 p_block_out->i_buffer = 0;
1586 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, false);
1587 memset( &db, 0, sizeof(db) );
1588 db.pBuffer = (IMediaBuffer *)p_out;
1590 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1591 0, 1, &db, &i_status );
1593 if( i_result != S_OK )
1595 if( i_result != S_FALSE )
1596 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1599 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1602 p_out->vt->Release( (IUnknown *)p_out );
1603 block_Release( p_block_out );
1607 if( !p_block_out->i_buffer )
1610 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1612 p_out->vt->Release( (IUnknown *)p_out );
1613 block_Release( p_block_out );
1617 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1620 msg_Dbg( p_enc, "ProcessOutput(): pts: %"PRId64", %"PRId64,
1621 i_pts, db.rtTimestamp / 10 );
1623 i_pts = db.rtTimestamp / 10;
1626 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1628 p_block_out->i_length = db.rtTimelength / 10;
1630 msg_Dbg( p_enc, "ProcessOutput(): length: %"PRId64,
1631 p_block_out->i_length );
1635 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1637 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1638 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1640 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1643 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1644 block_ChainAppend( &p_chain, p_block_out );
1648 /*****************************************************************************
1649 * EncoderClose: close codec
1650 *****************************************************************************/
1651 void EncoderClose( vlc_object_t *p_this )
1653 encoder_t *p_enc = (encoder_t*)p_this;
1654 encoder_sys_t *p_sys = p_enc->p_sys;
1656 if( !p_sys ) return;
1658 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1659 FreeLibrary( p_sys->hmsdmo_dll );
1663 Restore_LDT_Keeper( p_sys->ldt_fs );
1666 /* Uninitialize OLE/COM */