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>
35 #include <vlc_codecs.h>
41 # include <vlc_charset.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_VCODEC )
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;
149 const GUID IID_IWMCodecPrivateData = {0x73f0be8e, 0x57f7, 0x4f01, {0xaa, 0x66, 0x9f, 0x57, 0x34, 0xc, 0xfe, 0xe}};
150 const GUID IID_IMediaObject = {0xd8ad0f58, 0x5494, 0x4102, {0x97, 0xc5, 0xec, 0x79, 0x8e, 0x59, 0xbc, 0xf4}};
151 const GUID IID_IMediaBuffer = {0x59eff8b9, 0x938c, 0x4a26, {0x82, 0xf2, 0x95, 0xcb, 0x84, 0xcd, 0xc8, 0x37}};
152 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
153 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
154 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
155 const GUID FORMAT_VideoInfo = {0x05589f80, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
156 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
157 const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
158 const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
159 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
160 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
161 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
164 static const GUID guid_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
165 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
167 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
168 static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
169 static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };
171 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
172 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
174 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
175 static const GUID guid_wmv_enc2 = { 0x96b57cdd, 0x8966, 0x410c,{ 0xbb, 0x1f, 0xc9, 0x7e, 0xea, 0x76, 0x5c, 0x04 } };
176 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
180 vlc_fourcc_t i_fourcc;
186 static const codec_dll decoders_table[] =
189 { VLC_CODEC_VC1, "wvc1dmod.dll", &guid_wvc1 },
191 { VLC_CODEC_WMV3, "wmv9dmod.dll", &guid_wmv9 },
193 { VLC_CODEC_WMV2, "wmvdmod.dll", &guid_wmv },
195 { VLC_CODEC_WMV1, "wmvdmod.dll", &guid_wmv },
197 { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
198 { VLC_FOURCC('m','s','s','2'), "wmsdmod.dll", &guid_wms },
199 { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
200 { VLC_FOURCC('m','s','s','1'), "wmsdmod.dll", &guid_wms },
201 /* Windows Media Video Adv */
202 { VLC_CODEC_WMVA, "wmvadvd.dll", &guid_wmva },
205 { VLC_CODEC_WMAP, "wma9dmod.dll", &guid_wma9 },
206 { VLC_CODEC_WMAL, "wma9dmod.dll", &guid_wma9 },
209 { VLC_CODEC_WMA2, "wma9dmod.dll", &guid_wma9 },
212 { VLC_CODEC_WMAS, "wmspdmod.dll", &guid_wma },
218 static const codec_dll encoders_table[] =
221 { VLC_CODEC_WMV3, "wmvdmoe2.dll", &guid_wmv_enc2 },
223 { VLC_CODEC_WMV2, "wmvdmoe2.dll", &guid_wmv_enc2 },
225 { VLC_CODEC_WMV1, "wmvdmoe2.dll", &guid_wmv_enc2 },
228 { VLC_CODEC_WMAP, "wmadmoe.dll", &guid_wma_enc },
230 { VLC_CODEC_WMA2, "wmadmoe.dll", &guid_wma_enc },
236 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
238 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
239 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
245 /*****************************************************************************
246 * DecoderOpen: open dmo codec
247 *****************************************************************************/
248 static int DecoderOpen( vlc_object_t *p_this )
250 decoder_t *p_dec = (decoder_t*)p_this;
251 decoder_sys_t *p_sys;
253 /* We can't open it now, because of ldt_keeper or something
254 * Open/Decode/Close has to be done in the same thread */
256 /* Probe if we support it */
257 for( unsigned i = 0; decoders_table[i].i_fourcc != 0; i++ )
259 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
261 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
262 (char*)&p_dec->fmt_in.i_codec,
263 decoders_table[i].psz_dll );
270 p_sys = p_dec->p_sys = malloc(sizeof(*p_sys));
275 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
277 p_dec->pf_decode_audio = (block_t *(*)(decoder_t *, block_t **))
280 vlc_mutex_init( &p_sys->lock );
281 vlc_cond_init( &p_sys->wait_input );
282 vlc_cond_init( &p_sys->wait_output );
284 p_sys->b_ready = false;
285 p_sys->pp_input = NULL;
286 TAB_INIT( p_sys->i_output, p_sys->pp_output );
288 if( vlc_clone( &p_sys->thread, DecoderThread, p_dec,
289 VLC_THREAD_PRIORITY_INPUT ) )
292 vlc_mutex_lock( &p_sys->lock );
293 while( !p_sys->b_ready )
294 vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
295 vlc_mutex_unlock( &p_sys->lock );
300 vlc_join( p_sys->thread, NULL );
302 vlc_cond_destroy( &p_sys->wait_input );
303 vlc_cond_destroy( &p_sys->wait_output );
304 vlc_mutex_destroy( &p_sys->lock );
309 /*****************************************************************************
310 * DecoderClose: close codec
311 *****************************************************************************/
312 static void DecoderClose( vlc_object_t *p_this )
314 decoder_t *p_dec = (decoder_t*)p_this;
315 decoder_sys_t *p_sys = p_dec->p_sys;
317 vlc_mutex_lock( &p_sys->lock );
318 p_sys->b_ready = false;
319 vlc_cond_signal( &p_sys->wait_input );
320 vlc_mutex_unlock( &p_sys->lock );
322 vlc_join( p_sys->thread, NULL );
323 TAB_CLEAN( p_sys->i_output, p_sys->pp_output );
324 vlc_cond_destroy( &p_sys->wait_input );
325 vlc_cond_destroy( &p_sys->wait_output );
326 vlc_mutex_destroy( &p_sys->lock );
330 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
332 decoder_sys_t *p_sys = p_dec->p_sys;
335 vlc_mutex_lock( &p_sys->lock );
336 if( p_sys->i_output <= 0 )
338 p_sys->pp_input = pp_block;
339 vlc_cond_signal( &p_sys->wait_input );
341 while( p_sys->pp_input )
342 vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
346 if( p_sys->i_output > 0 )
348 p_ret = p_sys->pp_output[0];
349 TAB_REMOVE( p_sys->i_output, p_sys->pp_output, p_ret );
352 vlc_mutex_unlock( &p_sys->lock );
357 /*****************************************************************************
358 * DecOpen: open dmo codec
359 *****************************************************************************/
360 static int DecOpen( decoder_t *p_dec )
362 decoder_sys_t *p_sys = p_dec->p_sys;
364 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
365 IMediaObject *p_dmo = NULL;
366 HINSTANCE hmsdmo_dll = NULL;
368 VIDEOINFOHEADER *p_vih = NULL;
369 WAVEFORMATEX *p_wf = NULL;
372 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
374 /* Initialize OLE/COM */
378 if( LoadDMO( VLC_OBJECT(p_dec), &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, false )
386 /* Setup input format */
387 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
388 dmo_input_type.pUnk = 0;
390 if( p_dec->fmt_in.i_cat == AUDIO_ES )
393 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
394 p_wf = malloc( i_size );
396 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
397 if( p_dec->fmt_in.i_extra )
398 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
400 dmo_input_type.majortype = MEDIATYPE_Audio;
401 dmo_input_type.subtype = dmo_input_type.majortype;
402 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec;
403 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
404 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
406 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
407 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
408 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
409 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
410 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
411 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
412 p_wf->cbSize = p_dec->fmt_in.i_extra;
414 dmo_input_type.formattype = FORMAT_WaveFormatEx;
415 dmo_input_type.cbFormat = i_size;
416 dmo_input_type.pbFormat = (char *)p_wf;
417 dmo_input_type.bFixedSizeSamples = 1;
418 dmo_input_type.bTemporalCompression = 0;
419 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
423 VLC_BITMAPINFOHEADER *p_bih;
425 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
426 p_vih = malloc( i_size );
428 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
429 if( p_dec->fmt_in.i_extra )
430 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
432 p_bih = &p_vih->bmiHeader;
433 p_bih->biCompression = p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec;
434 p_bih->biWidth = p_dec->fmt_in.video.i_width;
435 p_bih->biHeight = p_dec->fmt_in.video.i_height;
436 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
438 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
439 sizeof(VLC_BITMAPINFOHEADER);
441 p_vih->rcSource.left = p_vih->rcSource.top = 0;
442 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
443 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
444 p_vih->rcTarget = p_vih->rcSource;
446 dmo_input_type.majortype = MEDIATYPE_Video;
447 dmo_input_type.subtype = dmo_input_type.majortype;
448 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_original_fourcc ?: p_dec->fmt_in.i_codec;
449 dmo_input_type.formattype = FORMAT_VideoInfo;
450 dmo_input_type.bFixedSizeSamples = 0;
451 dmo_input_type.bTemporalCompression = 1;
452 dmo_input_type.cbFormat = i_size;
453 dmo_input_type.pbFormat = (char *)p_vih;
456 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
458 msg_Err( p_dec, "can't set DMO input type" );
461 msg_Dbg( p_dec, "DMO input type set" );
463 /* Setup output format */
464 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
465 dmo_output_type.pUnk = 0;
467 if( p_dec->fmt_in.i_cat == AUDIO_ES )
469 /* Setup the format */
470 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
471 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
472 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
473 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
474 p_dec->fmt_out.audio.i_physical_channels =
475 p_dec->fmt_out.audio.i_original_channels =
476 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
478 p_wf->wFormatTag = WAVE_FORMAT_PCM;
479 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
480 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
481 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
483 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
484 p_wf->nAvgBytesPerSec =
485 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
488 dmo_output_type.majortype = MEDIATYPE_Audio;
489 dmo_output_type.formattype = FORMAT_WaveFormatEx;
490 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
491 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
492 dmo_output_type.pbFormat = (char *)p_wf;
493 dmo_output_type.bFixedSizeSamples = 1;
494 dmo_output_type.bTemporalCompression = 0;
495 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
499 VLC_BITMAPINFOHEADER *p_bih;
501 unsigned i_chroma = VLC_CODEC_YUYV;
505 /* Find out which chroma to use */
506 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
508 if( mt.subtype.Data1 == VLC_CODEC_YV12 )
510 i_chroma = mt.subtype.Data1;
514 DMOFreeMediaType( &mt );
517 p_dec->fmt_out.i_codec = i_chroma == VLC_CODEC_YV12 ?
518 VLC_CODEC_I420 : i_chroma;
519 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
520 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
521 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
523 /* If an aspect-ratio was specified in the input format then force it */
524 if( p_dec->fmt_in.video.i_sar_num > 0 &&
525 p_dec->fmt_in.video.i_sar_den > 0 )
527 p_dec->fmt_out.video.i_sar_num = p_dec->fmt_in.video.i_sar_num;
528 p_dec->fmt_out.video.i_sar_den = p_dec->fmt_in.video.i_sar_den;
532 p_dec->fmt_out.video.i_sar_num = 1;
533 p_dec->fmt_out.video.i_sar_den = 1;
536 p_bih = &p_vih->bmiHeader;
537 p_bih->biCompression = i_chroma;
538 p_bih->biHeight *= -1;
539 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
540 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
541 p_dec->fmt_in.video.i_height *
542 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
544 p_bih->biPlanes = 1; /* http://msdn.microsoft.com/en-us/library/dd183376%28v=vs.85%29.aspx */
545 p_bih->biSize = sizeof(VLC_BITMAPINFOHEADER);
547 dmo_output_type.majortype = MEDIATYPE_Video;
548 dmo_output_type.formattype = FORMAT_VideoInfo;
549 dmo_output_type.subtype = dmo_output_type.majortype;
550 dmo_output_type.subtype.Data1 = p_bih->biCompression;
551 dmo_output_type.bFixedSizeSamples = true;
552 dmo_output_type.bTemporalCompression = 0;
553 dmo_output_type.lSampleSize = p_bih->biSizeImage;
554 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
555 dmo_output_type.pbFormat = (char *)p_vih;
559 /* Enumerate output types */
560 if( p_dec->fmt_in.i_cat == VIDEO_ES )
565 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
567 msg_Dbg( p_dec, "available output chroma: %4.4s",
568 (char *)&mt.subtype.Data1 );
569 DMOFreeMediaType( &mt );
574 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
576 msg_Err( p_dec, "can't set DMO output type" );
579 msg_Dbg( p_dec, "DMO output type set" );
581 /* Allocate the memory needed to store the decoder's structure */
582 p_sys->hmsdmo_dll = hmsdmo_dll;
583 p_sys->p_dmo = p_dmo;
585 p_sys->ldt_fs = ldt_fs;
588 /* Find out some properties of the output */
590 uint32_t i_size, i_align;
592 p_sys->i_min_output = 0;
593 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
595 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
600 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
602 p_sys->i_min_output = i_size;
603 p_sys->p_buffer = malloc( i_size );
604 if( !p_sys->p_buffer ) goto error;
608 /* Set output properties */
609 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
610 if( p_dec->fmt_out.i_cat == AUDIO_ES )
611 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
613 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
618 vlc_mutex_lock( &p_sys->lock );
620 p_sys->b_works = true;
621 vlc_cond_signal( &p_sys->wait_output );
622 vlc_mutex_unlock( &p_sys->lock );
628 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
629 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
632 Restore_LDT_Keeper( ldt_fs );
634 /* Uninitialize OLE/COM */
641 vlc_mutex_lock( &p_sys->lock );
642 p_sys->b_ready = true;
643 vlc_cond_signal( &p_sys->wait_output );
644 vlc_mutex_unlock( &p_sys->lock );
648 /*****************************************************************************
649 * LoadDMO: Load the DMO object
650 *****************************************************************************/
651 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
652 IMediaObject **pp_dmo, es_format_t *p_fmt,
655 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
659 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
660 const DMO_PARTIAL_MEDIATYPE *,
661 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
664 IEnumDMO *p_enum_dmo = NULL;
671 IClassFactory *cFactory = NULL;
672 IUnknown *cObject = NULL;
673 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
676 /* Look for a DMO which can handle the requested codec */
677 if( p_fmt->i_cat == AUDIO_ES )
680 dmo_partial_type.type = MEDIATYPE_Audio;
681 dmo_partial_type.subtype = dmo_partial_type.type;
682 dmo_partial_type.subtype.Data1 = p_fmt->i_original_fourcc ?: p_fmt->i_codec;
683 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
684 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
688 dmo_partial_type.type = MEDIATYPE_Video;
689 dmo_partial_type.subtype = dmo_partial_type.type;
690 dmo_partial_type.subtype.Data1 = p_fmt->i_original_fourcc ?: p_fmt->i_codec;
695 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
696 if( *p_hmsdmo_dll == NULL )
698 msg_Dbg( p_this, "failed loading msdmo.dll" );
701 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
702 if( OurDMOEnum == NULL )
704 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
705 FreeLibrary( *p_hmsdmo_dll );
711 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
712 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
716 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
717 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
721 FreeLibrary( *p_hmsdmo_dll );
722 /* return VLC_EGENERIC; */
723 /* Try loading the dll directly */
727 /* Pickup the first available codec */
729 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
730 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
732 char *psz_temp = FromWide( psz_dmo_name );
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 );
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,
793 if( i_err || cFactory == NULL )
795 msg_Dbg( p_this, "no such class object" );
796 FreeLibrary( *p_hmsdmo_dll );
800 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
802 cFactory->vt->Release( (IUnknown*)cFactory );
803 if( i_err || !cObject )
805 msg_Dbg( p_this, "class factory failure" );
806 FreeLibrary( *p_hmsdmo_dll );
809 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
811 cObject->vt->Release( (IUnknown*)cObject );
812 if( i_err || !*pp_dmo )
814 msg_Dbg( p_this, "QueryInterface failure" );
815 FreeLibrary( *p_hmsdmo_dll );
822 static void DecClose( decoder_t *p_dec )
824 decoder_sys_t *p_sys = p_dec->p_sys;
826 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
827 FreeLibrary( p_sys->hmsdmo_dll );
831 Restore_LDT_Keeper( p_sys->ldt_fs );
834 /* Uninitialize OLE/COM */
838 free( p_sys->p_buffer );
841 /****************************************************************************
842 * DecodeBlock: the whole thing
843 ****************************************************************************
844 * This function must be fed with packets.
845 ****************************************************************************/
846 static void *DecBlock( decoder_t *p_dec, block_t **pp_block )
848 decoder_sys_t *p_sys = p_dec->p_sys;
852 DMO_OUTPUT_DATA_BUFFER db;
857 if( !pp_block ) return NULL;
861 /* Won't work with streams with B-frames, but do we have any ? */
862 if( p_block && p_block->i_pts <= VLC_TS_INVALID )
863 p_block->i_pts = p_block->i_dts;
865 /* Date management */
866 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
867 p_block->i_pts != date_Get( &p_sys->end_date ) )
869 date_Set( &p_sys->end_date, p_block->i_pts );
872 #if 0 /* Breaks the video decoding */
873 if( !date_Get( &p_sys->end_date ) )
875 /* We've just started the stream, wait for the first PTS. */
876 if( p_block ) block_Release( p_block );
881 /* Feed input to the DMO */
882 if( p_block && p_block->i_buffer )
886 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, true );
888 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
889 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
892 p_in->vt->Release( (IUnknown *)p_in );
894 if( i_result == S_FALSE )
896 /* No output generated */
898 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
902 else if( i_result == (int)DMO_E_NOTACCEPTING )
904 /* Need to call ProcessOutput */
905 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
907 else if( i_result != S_OK )
909 msg_Dbg( p_dec, "ProcessInput(): failed" );
915 msg_Dbg( p_dec, "ProcessInput(): successful" );
920 else if( p_block && !p_block->i_buffer )
922 block_Release( p_block );
926 /* Get output from the DMO */
927 block_out.p_buffer = p_sys->p_buffer;
928 block_out.i_buffer = 0;
930 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, false );
931 memset( &db, 0, sizeof(db) );
932 db.pBuffer = (IMediaBuffer *)p_out;
934 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
935 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
938 if( i_result != S_OK )
940 if( i_result != S_FALSE )
941 msg_Dbg( p_dec, "ProcessOutput(): failed" );
944 msg_Dbg( p_dec, "ProcessOutput(): no output" );
947 p_out->vt->Release( (IUnknown *)p_out );
952 msg_Dbg( p_dec, "ProcessOutput(): success" );
955 if( !block_out.i_buffer )
958 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
960 p_out->vt->Release( (IUnknown *)p_out );
964 if( p_dec->fmt_out.i_cat == VIDEO_ES )
966 /* Get a new picture */
967 picture_t *p_pic = decoder_NewPicture( p_dec );
968 if( !p_pic ) return NULL;
970 CopyPicture( p_pic, block_out.p_buffer );
972 /* Date management */
973 p_pic->date = date_Get( &p_sys->end_date );
974 date_Increment( &p_sys->end_date, 1 );
976 p_out->vt->Release( (IUnknown *)p_out );
982 block_t *p_aout_buffer;
983 int i_samples = block_out.i_buffer /
984 ( p_dec->fmt_out.audio.i_bitspersample *
985 p_dec->fmt_out.audio.i_channels / 8 );
987 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
990 memcpy( p_aout_buffer->p_buffer,
991 block_out.p_buffer, block_out.i_buffer );
992 /* Date management */
993 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
994 p_aout_buffer->i_length =
995 date_Increment( &p_sys->end_date, i_samples )
996 - p_aout_buffer->i_pts;
998 p_out->vt->Release( (IUnknown *)p_out );
1000 return p_aout_buffer;
1006 static void CopyPicture( picture_t *p_pic, uint8_t *p_in )
1008 int i_plane, i_line, i_width, i_dst_stride;
1009 uint8_t *p_dst, *p_src = p_in;
1011 p_dst = p_pic->p[1].p_pixels;
1012 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1013 p_pic->p[2].p_pixels = p_dst;
1015 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1017 p_dst = p_pic->p[i_plane].p_pixels;
1018 i_width = p_pic->p[i_plane].i_visible_pitch;
1019 i_dst_stride = p_pic->p[i_plane].i_pitch;
1021 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
1023 vlc_memcpy( p_dst, p_src, i_width );
1025 p_dst += i_dst_stride;
1029 p_dst = p_pic->p[1].p_pixels;
1030 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1031 p_pic->p[2].p_pixels = p_dst;
1034 static void *DecoderThread( void *data )
1036 decoder_t *p_dec = data;
1037 decoder_sys_t *p_sys = p_dec->p_sys;
1039 if( DecOpen( p_dec ) )
1040 return NULL; /* failed */
1042 vlc_mutex_lock( &p_sys->lock );
1045 while( p_sys->b_ready && !p_sys->pp_input )
1046 vlc_cond_wait( &p_sys->wait_input, &p_sys->lock );
1047 if( !p_sys->b_ready )
1052 void *p_output = DecBlock( p_dec, p_sys->pp_input );
1055 TAB_APPEND( p_sys->i_output, p_sys->pp_output, p_output );
1057 p_sys->pp_input = NULL;
1058 vlc_cond_signal( &p_sys->wait_output );
1060 vlc_mutex_unlock( &p_sys->lock );
1067 /****************************************************************************
1068 * Encoder descriptor declaration
1069 ****************************************************************************/
1070 struct encoder_sys_t
1072 HINSTANCE hmsdmo_dll;
1073 IMediaObject *p_dmo;
1084 /*****************************************************************************
1085 * EncoderOpen: open dmo codec
1086 *****************************************************************************/
1087 static int EncoderOpen( vlc_object_t *p_this )
1089 encoder_t *p_enc = (encoder_t*)p_this;
1091 int i_ret = EncOpen( p_this );
1092 if( i_ret != VLC_SUCCESS ) return i_ret;
1095 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1097 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, block_t *))
1103 /*****************************************************************************
1104 * EncoderSetVideoType: configures the input and output types of the dmo
1105 *****************************************************************************/
1106 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1108 int i, i_selected, i_err;
1109 DMO_MEDIA_TYPE dmo_type;
1110 VIDEOINFOHEADER vih, *p_vih;
1111 VLC_BITMAPINFOHEADER *p_bih;
1114 p_enc->fmt_in.video.i_bits_per_pixel =
1115 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1117 /* Enumerate input format (for debug output) */
1119 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1121 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1123 msg_Dbg( p_enc, "available input chroma: %4.4s",
1124 (char *)&dmo_type.subtype.Data1 );
1125 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1126 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1127 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1128 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1130 DMOFreeMediaType( &dmo_type );
1133 /* Setup input format */
1134 memset( &dmo_type, 0, sizeof(dmo_type) );
1135 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1137 p_bih = &vih.bmiHeader;
1138 p_bih->biCompression = VLC_CODEC_I420;
1139 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1140 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1141 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1142 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1143 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1144 p_bih->biPlanes = 3;
1145 p_bih->biSize = sizeof(VLC_BITMAPINFOHEADER);
1147 vih.rcSource.left = vih.rcSource.top = 0;
1148 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1149 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1150 vih.rcTarget = vih.rcSource;
1152 vih.AvgTimePerFrame = INT64_C(10000000) / 25; //FIXME
1154 dmo_type.majortype = MEDIATYPE_Video;
1155 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1156 dmo_type.subtype = MEDIASUBTYPE_I420;
1157 //dmo_type.subtype.Data1 = p_bih->biCompression;
1158 dmo_type.formattype = FORMAT_VideoInfo;
1159 dmo_type.bFixedSizeSamples = TRUE;
1160 dmo_type.bTemporalCompression = FALSE;
1161 dmo_type.lSampleSize = p_bih->biSizeImage;
1162 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1163 dmo_type.pbFormat = (char *)&vih;
1165 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1167 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1168 return VLC_EGENERIC;
1171 msg_Dbg( p_enc, "successfully set input type" );
1173 /* Setup output format */
1174 memset( &dmo_type, 0, sizeof(dmo_type) );
1177 /* Enumerate output types */
1178 i = 0, i_selected = -1;
1179 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1181 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1183 msg_Dbg( p_enc, "available output codec: %4.4s",
1184 (char *)&dmo_type.subtype.Data1 );
1186 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1189 DMOFreeMediaType( &dmo_type );
1192 if( i_selected < 0 )
1194 msg_Err( p_enc, "couldn't find codec: %4.4s",
1195 (char *)&p_enc->fmt_out.i_codec );
1196 return VLC_EGENERIC;
1199 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1200 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1201 p_enc->fmt_out.i_bitrate;
1203 /* Get the private data for the codec */
1206 IWMCodecPrivateData *p_privdata;
1207 VIDEOINFOHEADER *p_vih;
1208 uint8_t *p_data = 0;
1209 uint32_t i_data = 0, i_vih;
1211 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1212 &IID_IWMCodecPrivateData,
1213 (void**)&p_privdata );
1216 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1219 msg_Err( p_enc, "SetPartialOutputType() failed" );
1220 p_privdata->vt->Release( (IUnknown *)p_privdata );
1224 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1227 msg_Err( p_enc, "GetPrivateData() failed" );
1228 p_privdata->vt->Release( (IUnknown *)p_privdata );
1232 p_data = malloc( i_data );
1233 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1235 /* Update the media type with the private data */
1236 i_vih = dmo_type.cbFormat + i_data;
1237 p_vih = CoTaskMemAlloc( i_vih );
1238 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1239 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1240 DMOFreeMediaType( &dmo_type );
1241 dmo_type.pbFormat = (char*)p_vih;
1242 dmo_type.cbFormat = i_vih;
1244 msg_Dbg( p_enc, "found extra data: %i", i_data );
1245 p_enc->fmt_out.i_extra = i_data;
1246 p_enc->fmt_out.p_extra = p_data;
1250 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1252 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1253 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
1255 DMOFreeMediaType( &dmo_type );
1258 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1259 return VLC_EGENERIC;
1262 msg_Dbg( p_enc, "successfully set output type" );
1266 /*****************************************************************************
1267 * EncoderSetAudioType: configures the input and output types of the dmo
1268 *****************************************************************************/
1269 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1271 int i, i_selected, i_err;
1272 unsigned int i_last_byterate;
1274 DMO_MEDIA_TYPE dmo_type;
1277 /* Setup the format structure */
1278 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1279 if( i_tag == 0 ) return VLC_EGENERIC;
1281 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
1282 p_enc->fmt_in.audio.i_bitspersample = 16;
1284 /* We first need to choose an output type from the predefined
1285 * list of choices (we cycle through the list to select the best match) */
1286 i = 0; i_selected = -1; i_last_byterate = 0;
1287 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1289 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1290 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1291 "bits per sample: %i, bitrate: %i, blockalign: %i",
1292 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1293 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1294 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1296 if( p_wf->wFormatTag == i_tag &&
1297 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1298 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1299 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1301 if( p_wf->nAvgBytesPerSec <
1302 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1303 p_wf->nAvgBytesPerSec > i_last_byterate )
1306 i_last_byterate = p_wf->nAvgBytesPerSec;
1307 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1308 i_selected, p_wf->nAvgBytesPerSec * 8 );
1312 DMOFreeMediaType( &dmo_type );
1315 if( i_selected < 0 )
1317 msg_Err( p_enc, "couldn't find a matching output" );
1318 return VLC_EGENERIC;
1321 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1322 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1324 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1325 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1326 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1327 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1328 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1330 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1331 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1332 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1333 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1334 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1338 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1339 p_enc->fmt_out.i_extra = p_wf->cbSize;
1340 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1341 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1344 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1345 DMOFreeMediaType( &dmo_type );
1349 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1350 return VLC_EGENERIC;
1353 msg_Dbg( p_enc, "successfully set output type" );
1355 /* Setup the input type */
1356 i = 0; i_selected = -1;
1357 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1359 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1360 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1361 "bits per sample: %i, bitrate: %i, blockalign: %i",
1362 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1363 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1364 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1366 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1367 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1368 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1369 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1374 DMOFreeMediaType( &dmo_type );
1377 if( i_selected < 0 )
1379 msg_Err( p_enc, "couldn't find a matching input" );
1380 return VLC_EGENERIC;
1383 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1384 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1385 DMOFreeMediaType( &dmo_type );
1388 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1389 return VLC_EGENERIC;
1391 msg_Dbg( p_enc, "successfully set input type" );
1396 /*****************************************************************************
1397 * EncOpen: open dmo codec
1398 *****************************************************************************/
1399 static int EncOpen( vlc_object_t *p_this )
1401 encoder_t *p_enc = (encoder_t*)p_this;
1402 encoder_sys_t *p_sys = NULL;
1403 IMediaObject *p_dmo = NULL;
1404 HINSTANCE hmsdmo_dll = NULL;
1407 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1409 /* Initialize OLE/COM */
1413 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, true )
1421 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1423 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1427 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1430 /* Allocate the memory needed to store the decoder's structure */
1431 if( ( p_enc->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
1436 p_sys->hmsdmo_dll = hmsdmo_dll;
1437 p_sys->p_dmo = p_dmo;
1439 p_sys->ldt_fs = ldt_fs;
1442 /* Find out some properties of the inputput */
1444 uint32_t i_size, i_align, dum;
1446 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1447 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1449 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1450 i_size, i_align, dum );
1453 /* Find out some properties of the output */
1455 uint32_t i_size, i_align;
1457 p_sys->i_min_output = 0;
1458 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1460 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1465 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1467 p_sys->i_min_output = i_size;
1471 /* Set output properties */
1472 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1473 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1474 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1476 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1482 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1483 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1486 Restore_LDT_Keeper( ldt_fs );
1488 /* Uninitialize OLE/COM */
1494 return VLC_EGENERIC;
1497 /****************************************************************************
1498 * Encode: the whole thing
1499 ****************************************************************************/
1500 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1502 encoder_sys_t *p_sys = p_enc->p_sys;
1504 block_t *p_chain = NULL;
1505 block_t *p_block_in;
1510 if( !p_data ) return NULL;
1512 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1514 /* Get picture data */
1515 int i_plane, i_line, i_width, i_src_stride;
1516 picture_t *p_pic = (picture_t *)p_data;
1519 int i_buffer = p_enc->fmt_in.video.i_width *
1520 p_enc->fmt_in.video.i_height *
1521 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1523 p_block_in = block_New( p_enc, i_buffer );
1525 /* Copy picture stride by stride */
1526 p_dst = p_block_in->p_buffer;
1527 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1529 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1530 i_width = p_pic->p[i_plane].i_visible_pitch;
1531 i_src_stride = p_pic->p[i_plane].i_pitch;
1533 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1536 vlc_memcpy( p_dst, p_src, i_width );
1538 p_src += i_src_stride;
1542 i_pts = p_pic->date;
1546 block_t *p_aout_buffer = (block_t *)p_data;
1547 p_block_in = block_New( p_enc, p_aout_buffer->i_buffer );
1548 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1549 p_block_in->i_buffer );
1551 i_pts = p_aout_buffer->i_pts;
1554 /* Feed input to the DMO */
1555 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, true );
1556 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1557 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1559 p_in->vt->Release( (IUnknown *)p_in );
1560 if( i_result == S_FALSE )
1562 /* No output generated */
1564 msg_Dbg( p_enc, "ProcessInput(): no output generated %"PRId64, i_pts );
1568 else if( i_result == (int)DMO_E_NOTACCEPTING )
1570 /* Need to call ProcessOutput */
1571 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1573 else if( i_result != S_OK )
1575 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1580 msg_Dbg( p_enc, "ProcessInput(): success" );
1583 /* Get output from the DMO */
1586 DMO_OUTPUT_DATA_BUFFER db;
1587 block_t *p_block_out;
1588 CMediaBuffer *p_out;
1590 p_block_out = block_New( p_enc, p_sys->i_min_output );
1591 p_block_out->i_buffer = 0;
1592 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, false);
1593 memset( &db, 0, sizeof(db) );
1594 db.pBuffer = (IMediaBuffer *)p_out;
1596 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1597 0, 1, &db, &i_status );
1599 if( i_result != S_OK )
1601 if( i_result != S_FALSE )
1602 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1605 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1608 p_out->vt->Release( (IUnknown *)p_out );
1609 block_Release( p_block_out );
1613 if( !p_block_out->i_buffer )
1616 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1618 p_out->vt->Release( (IUnknown *)p_out );
1619 block_Release( p_block_out );
1623 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1626 msg_Dbg( p_enc, "ProcessOutput(): pts: %"PRId64", %"PRId64,
1627 i_pts, db.rtTimestamp / 10 );
1629 i_pts = db.rtTimestamp / 10;
1632 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1634 p_block_out->i_length = db.rtTimelength / 10;
1636 msg_Dbg( p_enc, "ProcessOutput(): length: %"PRId64,
1637 p_block_out->i_length );
1641 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1643 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1644 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1646 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1649 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1650 block_ChainAppend( &p_chain, p_block_out );
1654 /*****************************************************************************
1655 * EncoderClose: close codec
1656 *****************************************************************************/
1657 void EncoderClose( vlc_object_t *p_this )
1659 encoder_t *p_enc = (encoder_t*)p_this;
1660 encoder_sys_t *p_sys = p_enc->p_sys;
1662 if( !p_sys ) return;
1664 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1665 FreeLibrary( p_sys->hmsdmo_dll );
1669 Restore_LDT_Keeper( p_sys->ldt_fs );
1672 /* Uninitialize OLE/COM */