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 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;
146 const GUID IID_IWMCodecPrivateData = {0x73f0be8e, 0x57f7, 0x4f01, {0xaa, 0x66, 0x9f, 0x57, 0x34, 0xc, 0xfe, 0xe}};
147 const GUID IID_IMediaObject = {0xd8ad0f58, 0x5494, 0x4102, {0x97, 0xc5, 0xec, 0x79, 0x8e, 0x59, 0xbc, 0xf4}};
148 const GUID IID_IMediaBuffer = {0x59eff8b9, 0x938c, 0x4a26, {0x82, 0xf2, 0x95, 0xcb, 0x84, 0xcd, 0xc8, 0x37}};
149 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
150 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
151 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
152 const GUID FORMAT_VideoInfo = {0x05589f80, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
153 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
154 const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
155 const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
156 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
157 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
158 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
161 static const GUID guid_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
162 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
164 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
165 static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
166 static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };
168 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
169 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
171 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
172 static const GUID guid_wmv_enc2 = { 0x96b57cdd, 0x8966, 0x410c,{ 0xbb, 0x1f, 0xc9, 0x7e, 0xea, 0x76, 0x5c, 0x04 } };
173 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
177 vlc_fourcc_t i_fourcc;
183 static const codec_dll decoders_table[] =
186 { VLC_FOURCC('W','V','C','1'), "wvc1dmod.dll", &guid_wvc1 },
187 { VLC_FOURCC('w','v','c','1'), "wvc1dmod.dll", &guid_wvc1 },
189 { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
190 { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
191 { VLC_FOURCC('W','M','V','P'), "wmv9dmod.dll", &guid_wmv9 },
192 { VLC_FOURCC('w','m','v','p'), "wmv9dmod.dll", &guid_wmv9 },
194 { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
195 { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
197 { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
198 { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
200 { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
201 { VLC_FOURCC('m','s','s','2'), "wmsdmod.dll", &guid_wms },
202 { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
203 { VLC_FOURCC('m','s','s','1'), "wmsdmod.dll", &guid_wms },
204 /* Windows Media Video Adv */
205 { VLC_FOURCC('W','M','V','A'), "wmvadvd.dll", &guid_wmva },
206 { VLC_FOURCC('w','m','v','a'), "wmvadvd.dll", &guid_wmva },
207 { VLC_FOURCC('W','V','P','2'), "wmvadvd.dll", &guid_wmva },
208 { VLC_FOURCC('w','v','p','2'), "wmvadvd.dll", &guid_wmva },
211 { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
212 { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
213 { VLC_FOURCC('W','M','A','P'), "wma9dmod.dll", &guid_wma9 },
214 { VLC_FOURCC('w','m','a','p'), "wma9dmod.dll", &guid_wma9 },
216 { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
217 { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
220 { VLC_FOURCC('W','M','A','S'), "wmspdmod.dll", &guid_wma },
221 { VLC_FOURCC('w','m','a','s'), "wmspdmod.dll", &guid_wma },
227 static const codec_dll encoders_table[] =
230 { VLC_FOURCC('W','M','V','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
231 { VLC_FOURCC('w','m','v','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
233 { VLC_FOURCC('W','M','V','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
234 { VLC_FOURCC('w','m','v','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
236 { VLC_FOURCC('W','M','V','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
237 { VLC_FOURCC('w','m','v','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
240 { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
241 { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
243 { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
244 { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
250 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
252 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
253 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
259 /*****************************************************************************
260 * DecoderOpen: open dmo codec
261 *****************************************************************************/
262 static int DecoderOpen( vlc_object_t *p_this )
264 decoder_t *p_dec = (decoder_t*)p_this;
265 decoder_sys_t *p_sys;
267 /* We can't open it now, because of ldt_keeper or something
268 * Open/Decode/Close has to be done in the same thread */
270 /* Probe if we support it */
271 for( unsigned i = 0; decoders_table[i].i_fourcc != 0; i++ )
273 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
275 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
276 (char*)&p_dec->fmt_in.i_codec,
277 decoders_table[i].psz_dll );
284 p_sys = p_dec->p_sys = malloc(sizeof(*p_sys));
289 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
291 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
294 vlc_mutex_init( &p_sys->lock );
295 vlc_cond_init( &p_sys->wait_input );
296 vlc_cond_init( &p_sys->wait_output );
297 p_sys->works = p_sys->ready = false;
299 if( vlc_clone( &p_sys->thread, DecoderThread, p_dec,
300 VLC_THREAD_PRIORITY_INPUT ) )
303 vlc_mutex_lock( &p_sys->lock );
304 while( !p_sys->ready )
305 vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
306 vlc_mutex_unlock( &p_sys->lock );
311 vlc_join( p_sys->thread, NULL );
313 vlc_cond_destroy( &p_sys->wait_input );
314 vlc_cond_destroy( &p_sys->wait_output );
315 vlc_mutex_destroy( &p_sys->lock );
320 /*****************************************************************************
321 * DecoderClose: close codec
322 *****************************************************************************/
323 static void DecoderClose( vlc_object_t *p_this )
325 decoder_t *p_dec = (decoder_t*)p_this;
326 decoder_sys_t *p_sys = p_dec->p_sys;
328 vlc_mutex_lock( &p_sys->lock );
329 p_sys->ready = false;
330 vlc_cond_signal( &p_sys->wait_input );
331 vlc_mutex_unlock( &p_sys->lock );
333 vlc_join( p_sys->thread, NULL );
334 vlc_cond_destroy( &p_sys->wait_input );
335 vlc_cond_destroy( &p_sys->wait_output );
336 vlc_mutex_destroy( &p_sys->lock );
340 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
342 decoder_sys_t *p_sys = p_dec->p_sys;
345 vlc_mutex_lock( &p_sys->lock );
346 p_sys->input = pp_block;
347 vlc_cond_signal( &p_sys->wait_input );
349 while( !(ret = p_sys->output) )
350 vlc_cond_wait( &p_sys->wait_output, &p_sys->lock );
351 p_sys->output = NULL;
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_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 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_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(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_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 = AOUT_FMT_S16_NE;
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 BITMAPINFOHEADER *p_bih;
501 unsigned i_chroma = VLC_FOURCC('Y','U','Y','2');
502 int i_planes = 1, i_bpp = 16;
505 /* Find out which chroma to use */
506 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
508 if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
510 i_chroma = mt.subtype.Data1;
511 i_planes = 3; i_bpp = 12;
514 DMOFreeMediaType( &mt );
517 p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
518 VLC_FOURCC('I','4','2','0') : 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_aspect )
526 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
530 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
531 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
534 p_bih = &p_vih->bmiHeader;
535 p_bih->biCompression = i_chroma;
536 p_bih->biHeight *= -1;
537 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
538 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
539 p_dec->fmt_in.video.i_height *
540 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
542 p_bih->biPlanes = i_planes;
543 p_bih->biSize = sizeof(BITMAPINFOHEADER);
545 dmo_output_type.majortype = MEDIATYPE_Video;
546 dmo_output_type.formattype = FORMAT_VideoInfo;
547 dmo_output_type.subtype = dmo_output_type.majortype;
548 dmo_output_type.subtype.Data1 = p_bih->biCompression;
549 dmo_output_type.bFixedSizeSamples = true;
550 dmo_output_type.bTemporalCompression = 0;
551 dmo_output_type.lSampleSize = p_bih->biSizeImage;
552 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
553 dmo_output_type.pbFormat = (char *)p_vih;
557 /* Enumerate output types */
558 if( p_dec->fmt_in.i_cat == VIDEO_ES )
563 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
565 msg_Dbg( p_dec, "available output chroma: %4.4s",
566 (char *)&mt.subtype.Data1 );
567 DMOFreeMediaType( &mt );
572 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
574 msg_Err( p_dec, "can't set DMO output type" );
577 msg_Dbg( p_dec, "DMO output type set" );
579 /* Allocate the memory needed to store the decoder's structure */
580 p_sys->hmsdmo_dll = hmsdmo_dll;
581 p_sys->p_dmo = p_dmo;
583 p_sys->ldt_fs = ldt_fs;
586 /* Find out some properties of the output */
588 uint32_t i_size, i_align;
590 p_sys->i_min_output = 0;
591 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
593 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
598 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
600 p_sys->i_min_output = i_size;
601 p_sys->p_buffer = malloc( i_size );
602 if( !p_sys->p_buffer ) goto error;
606 /* Set output properties */
607 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
608 if( p_dec->fmt_out.i_cat == AUDIO_ES )
609 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
611 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
616 vlc_mutex_lock( &p_sys->lock );
617 p_sys->ready = p_sys->works = true;
618 vlc_cond_signal( &p_sys->wait_output );
619 vlc_mutex_unlock( &p_sys->lock );
625 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
626 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
629 Restore_LDT_Keeper( ldt_fs );
631 /* Uninitialize OLE/COM */
638 vlc_mutex_lock( &p_sys->lock );
640 vlc_cond_signal( &p_sys->wait_output );
641 vlc_mutex_unlock( &p_sys->lock );
645 /*****************************************************************************
646 * LoadDMO: Load the DMO object
647 *****************************************************************************/
648 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
649 IMediaObject **pp_dmo, es_format_t *p_fmt,
652 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
656 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
657 const DMO_PARTIAL_MEDIATYPE *,
658 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
661 IEnumDMO *p_enum_dmo = NULL;
668 IClassFactory *cFactory = NULL;
669 IUnknown *cObject = NULL;
670 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
673 /* Look for a DMO which can handle the requested codec */
674 if( p_fmt->i_cat == AUDIO_ES )
677 dmo_partial_type.type = MEDIATYPE_Audio;
678 dmo_partial_type.subtype = dmo_partial_type.type;
679 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
680 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
681 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
685 dmo_partial_type.type = MEDIATYPE_Video;
686 dmo_partial_type.subtype = dmo_partial_type.type;
687 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
692 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
693 if( *p_hmsdmo_dll == NULL )
695 msg_Dbg( p_this, "failed loading msdmo.dll" );
698 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
699 if( OurDMOEnum == NULL )
701 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
702 FreeLibrary( *p_hmsdmo_dll );
708 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
709 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
713 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
714 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
718 FreeLibrary( *p_hmsdmo_dll );
719 /* return VLC_EGENERIC; */
720 /* Try loading the dll directly */
724 /* Pickup the first available codec */
726 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
727 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
729 char psz_temp[MAX_PATH];
730 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
731 msg_Dbg( p_this, "found DMO: %s", psz_temp );
732 CoTaskMemFree( psz_dmo_name );
735 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
736 &IID_IMediaObject, (void **)pp_dmo ) )
738 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
744 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
748 FreeLibrary( *p_hmsdmo_dll );
749 /* return VLC_EGENERIC; */
750 /* Try loading the dll directly */
759 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
761 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
764 if( codecs_table[i_codec].i_fourcc == 0 )
765 return VLC_EGENERIC; /* Can't happen */
767 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
768 if( *p_hmsdmo_dll == NULL )
770 msg_Dbg( p_this, "failed loading '%s'",
771 codecs_table[i_codec].psz_dll );
775 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
778 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
779 FreeLibrary( *p_hmsdmo_dll );
783 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
785 if( i_err || cFactory == NULL )
787 msg_Dbg( p_this, "no such class object" );
788 FreeLibrary( *p_hmsdmo_dll );
792 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
794 cFactory->vt->Release( (IUnknown*)cFactory );
795 if( i_err || !cObject )
797 msg_Dbg( p_this, "class factory failure" );
798 FreeLibrary( *p_hmsdmo_dll );
801 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
803 cObject->vt->Release( (IUnknown*)cObject );
804 if( i_err || !*pp_dmo )
806 msg_Dbg( p_this, "QueryInterface failure" );
807 FreeLibrary( *p_hmsdmo_dll );
814 static void DecClose( decoder_t *p_dec )
816 decoder_sys_t *p_sys = p_dec->p_sys;
818 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
819 FreeLibrary( p_sys->hmsdmo_dll );
823 Restore_LDT_Keeper( p_sys->ldt_fs );
826 /* Uninitialize OLE/COM */
830 free( p_sys->p_buffer );
833 /****************************************************************************
834 * DecodeBlock: the whole thing
835 ****************************************************************************
836 * This function must be fed with ogg packets.
837 ****************************************************************************/
838 static void *DecBlock( decoder_t *p_dec, block_t **pp_block )
840 decoder_sys_t *p_sys = p_dec->p_sys;
844 DMO_OUTPUT_DATA_BUFFER db;
849 if( !pp_block ) return NULL;
853 /* Won't work with streams with B-frames, but do we have any ? */
854 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
856 /* Date management */
857 if( p_block && p_block->i_pts > 0 &&
858 p_block->i_pts != date_Get( &p_sys->end_date ) )
860 date_Set( &p_sys->end_date, p_block->i_pts );
863 #if 0 /* Breaks the video decoding */
864 if( !date_Get( &p_sys->end_date ) )
866 /* We've just started the stream, wait for the first PTS. */
867 if( p_block ) block_Release( p_block );
872 /* Feed input to the DMO */
873 if( p_block && p_block->i_buffer )
877 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, true );
879 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
880 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
883 p_in->vt->Release( (IUnknown *)p_in );
885 if( i_result == S_FALSE )
887 /* No output generated */
889 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
893 else if( i_result == (int)DMO_E_NOTACCEPTING )
895 /* Need to call ProcessOutput */
896 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
898 else if( i_result != S_OK )
900 msg_Dbg( p_dec, "ProcessInput(): failed" );
905 //msg_Dbg( p_dec, "ProcessInput(): successful" );
909 else if( p_block && !p_block->i_buffer )
911 block_Release( p_block );
915 /* Get output from the DMO */
916 block_out.p_buffer = p_sys->p_buffer;
917 block_out.i_buffer = 0;
919 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, false );
920 memset( &db, 0, sizeof(db) );
921 db.pBuffer = (IMediaBuffer *)p_out;
923 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
924 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
927 if( i_result != S_OK )
929 if( i_result != S_FALSE )
930 msg_Dbg( p_dec, "ProcessOutput(): failed" );
933 msg_Dbg( p_dec, "ProcessOutput(): no output" );
936 p_out->vt->Release( (IUnknown *)p_out );
941 msg_Dbg( p_dec, "ProcessOutput(): success" );
944 if( !block_out.i_buffer )
947 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
949 p_out->vt->Release( (IUnknown *)p_out );
953 if( p_dec->fmt_out.i_cat == VIDEO_ES )
955 /* Get a new picture */
956 picture_t *p_pic = decoder_NewPicture( p_dec );
957 if( !p_pic ) return NULL;
959 CopyPicture( p_pic, block_out.p_buffer );
961 /* Date management */
962 p_pic->date = date_Get( &p_sys->end_date );
963 date_Increment( &p_sys->end_date, 1 );
965 p_out->vt->Release( (IUnknown *)p_out );
971 aout_buffer_t *p_aout_buffer;
972 int i_samples = block_out.i_buffer /
973 ( p_dec->fmt_out.audio.i_bitspersample *
974 p_dec->fmt_out.audio.i_channels / 8 );
976 p_aout_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
979 memcpy( p_aout_buffer->p_buffer,
980 block_out.p_buffer, block_out.i_buffer );
981 /* Date management */
982 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
983 p_aout_buffer->end_date =
984 date_Increment( &p_sys->end_date, i_samples );
986 p_out->vt->Release( (IUnknown *)p_out );
988 return p_aout_buffer;
994 static void CopyPicture( picture_t *p_pic, uint8_t *p_in )
996 int i_plane, i_line, i_width, i_dst_stride;
997 uint8_t *p_dst, *p_src = p_in;
999 p_dst = p_pic->p[1].p_pixels;
1000 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1001 p_pic->p[2].p_pixels = p_dst;
1003 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1005 p_dst = p_pic->p[i_plane].p_pixels;
1006 i_width = p_pic->p[i_plane].i_visible_pitch;
1007 i_dst_stride = p_pic->p[i_plane].i_pitch;
1009 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
1011 vlc_memcpy( p_dst, p_src, i_width );
1013 p_dst += i_dst_stride;
1017 p_dst = p_pic->p[1].p_pixels;
1018 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
1019 p_pic->p[2].p_pixels = p_dst;
1022 static void *DecoderThread( void *data )
1024 decoder_t *p_dec = data;
1025 decoder_sys_t *p_sys = p_dec->p_sys;
1027 if( DecOpen( p_dec ) )
1028 return NULL; /* failed */
1030 vlc_mutex_lock( &p_sys->lock );
1033 while( p_sys->ready && !p_sys->input )
1034 vlc_cond_wait( &p_sys->wait_input, &p_sys->lock );
1038 p_sys->output = DecBlock( p_dec, p_sys->input );
1039 p_sys->input = NULL;
1041 vlc_mutex_unlock( &p_sys->lock );
1048 /****************************************************************************
1049 * Encoder descriptor declaration
1050 ****************************************************************************/
1051 struct encoder_sys_t
1053 HINSTANCE hmsdmo_dll;
1054 IMediaObject *p_dmo;
1065 /*****************************************************************************
1066 * EncoderOpen: open dmo codec
1067 *****************************************************************************/
1068 static int EncoderOpen( vlc_object_t *p_this )
1070 encoder_t *p_enc = (encoder_t*)p_this;
1072 int i_ret = EncOpen( p_this );
1073 if( i_ret != VLC_SUCCESS ) return i_ret;
1076 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1078 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
1084 /*****************************************************************************
1085 * EncoderSetVideoType: configures the input and output types of the dmo
1086 *****************************************************************************/
1087 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1089 int i, i_selected, i_err;
1090 DMO_MEDIA_TYPE dmo_type;
1091 VIDEOINFOHEADER vih, *p_vih;
1092 BITMAPINFOHEADER *p_bih;
1095 p_enc->fmt_in.video.i_bits_per_pixel =
1096 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1098 /* Enumerate input format (for debug output) */
1100 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1102 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1104 msg_Dbg( p_enc, "available input chroma: %4.4s",
1105 (char *)&dmo_type.subtype.Data1 );
1106 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1107 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1108 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1109 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1111 DMOFreeMediaType( &dmo_type );
1114 /* Setup input format */
1115 memset( &dmo_type, 0, sizeof(dmo_type) );
1116 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1118 p_bih = &vih.bmiHeader;
1119 p_bih->biCompression = VLC_FOURCC('I','4','2','0');
1120 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1121 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1122 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1123 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1124 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1125 p_bih->biPlanes = 3;
1126 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1128 vih.rcSource.left = vih.rcSource.top = 0;
1129 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1130 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1131 vih.rcTarget = vih.rcSource;
1133 vih.AvgTimePerFrame = INT64_C(10000000) / 25; //FIXME
1135 dmo_type.majortype = MEDIATYPE_Video;
1136 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1137 dmo_type.subtype = MEDIASUBTYPE_I420;
1138 //dmo_type.subtype.Data1 = p_bih->biCompression;
1139 dmo_type.formattype = FORMAT_VideoInfo;
1140 dmo_type.bFixedSizeSamples = TRUE;
1141 dmo_type.bTemporalCompression = FALSE;
1142 dmo_type.lSampleSize = p_bih->biSizeImage;
1143 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1144 dmo_type.pbFormat = (char *)&vih;
1146 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1148 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1149 return VLC_EGENERIC;
1152 msg_Dbg( p_enc, "successfully set input type" );
1154 /* Setup output format */
1155 memset( &dmo_type, 0, sizeof(dmo_type) );
1158 /* Enumerate output types */
1159 i = 0, i_selected = -1;
1160 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1162 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1164 msg_Dbg( p_enc, "available output codec: %4.4s",
1165 (char *)&dmo_type.subtype.Data1 );
1167 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1170 DMOFreeMediaType( &dmo_type );
1173 if( i_selected < 0 )
1175 msg_Err( p_enc, "couldn't find codec: %4.4s",
1176 (char *)&p_enc->fmt_out.i_codec );
1177 return VLC_EGENERIC;
1180 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1181 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1182 p_enc->fmt_out.i_bitrate;
1184 /* Get the private data for the codec */
1187 IWMCodecPrivateData *p_privdata;
1188 VIDEOINFOHEADER *p_vih;
1189 uint8_t *p_data = 0;
1190 uint32_t i_data = 0, i_vih;
1192 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1193 &IID_IWMCodecPrivateData,
1194 (void **)&p_privdata );
1197 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1200 msg_Err( p_enc, "SetPartialOutputType() failed" );
1201 p_privdata->vt->Release( (IUnknown *)p_privdata );
1205 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1208 msg_Err( p_enc, "GetPrivateData() failed" );
1209 p_privdata->vt->Release( (IUnknown *)p_privdata );
1213 p_data = malloc( i_data );
1214 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1216 /* Update the media type with the private data */
1217 i_vih = dmo_type.cbFormat + i_data;
1218 p_vih = CoTaskMemAlloc( i_vih );
1219 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1220 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1221 DMOFreeMediaType( &dmo_type );
1222 dmo_type.pbFormat = (char*)p_vih;
1223 dmo_type.cbFormat = i_vih;
1225 msg_Dbg( p_enc, "found extra data: %i", i_data );
1226 p_enc->fmt_out.i_extra = i_data;
1227 p_enc->fmt_out.p_extra = p_data;
1231 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1233 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1234 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
1236 DMOFreeMediaType( &dmo_type );
1239 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1240 return VLC_EGENERIC;
1243 msg_Dbg( p_enc, "successfully set output type" );
1247 /*****************************************************************************
1248 * EncoderSetAudioType: configures the input and output types of the dmo
1249 *****************************************************************************/
1250 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1252 int i, i_selected, i_err;
1253 unsigned int i_last_byterate;
1255 DMO_MEDIA_TYPE dmo_type;
1258 /* Setup the format structure */
1259 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1260 if( i_tag == 0 ) return VLC_EGENERIC;
1262 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1263 p_enc->fmt_in.audio.i_bitspersample = 16;
1265 /* We first need to choose an output type from the predefined
1266 * list of choices (we cycle through the list to select the best match) */
1267 i = 0; i_selected = -1; i_last_byterate = 0;
1268 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1270 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1271 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1272 "bits per sample: %i, bitrate: %i, blockalign: %i",
1273 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1274 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1275 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1277 if( p_wf->wFormatTag == i_tag &&
1278 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1279 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1280 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1282 if( p_wf->nAvgBytesPerSec <
1283 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1284 p_wf->nAvgBytesPerSec > i_last_byterate )
1287 i_last_byterate = p_wf->nAvgBytesPerSec;
1288 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1289 i_selected, p_wf->nAvgBytesPerSec * 8 );
1293 DMOFreeMediaType( &dmo_type );
1296 if( i_selected < 0 )
1298 msg_Err( p_enc, "couldn't find a matching output" );
1299 return VLC_EGENERIC;
1302 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1303 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1305 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1306 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1307 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1308 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1309 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1311 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1312 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1313 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1314 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1315 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1319 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1320 p_enc->fmt_out.i_extra = p_wf->cbSize;
1321 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1322 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1325 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1326 DMOFreeMediaType( &dmo_type );
1330 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1331 return VLC_EGENERIC;
1334 msg_Dbg( p_enc, "successfully set output type" );
1336 /* Setup the input type */
1337 i = 0; i_selected = -1;
1338 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1340 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1341 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1342 "bits per sample: %i, bitrate: %i, blockalign: %i",
1343 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1344 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1345 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1347 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1348 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1349 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1350 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1355 DMOFreeMediaType( &dmo_type );
1358 if( i_selected < 0 )
1360 msg_Err( p_enc, "couldn't find a matching input" );
1361 return VLC_EGENERIC;
1364 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1365 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1366 DMOFreeMediaType( &dmo_type );
1369 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1370 return VLC_EGENERIC;
1372 msg_Dbg( p_enc, "successfully set input type" );
1377 /*****************************************************************************
1378 * EncOpen: open dmo codec
1379 *****************************************************************************/
1380 static int EncOpen( vlc_object_t *p_this )
1382 encoder_t *p_enc = (encoder_t*)p_this;
1383 encoder_sys_t *p_sys = NULL;
1384 IMediaObject *p_dmo = NULL;
1385 HINSTANCE hmsdmo_dll = NULL;
1388 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1390 /* Initialize OLE/COM */
1394 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, true )
1402 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1404 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1408 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1411 /* Allocate the memory needed to store the decoder's structure */
1412 if( ( p_enc->p_sys = p_sys =
1413 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1418 p_sys->hmsdmo_dll = hmsdmo_dll;
1419 p_sys->p_dmo = p_dmo;
1421 p_sys->ldt_fs = ldt_fs;
1424 /* Find out some properties of the inputput */
1426 uint32_t i_size, i_align, dum;
1428 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1429 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1431 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1432 i_size, i_align, dum );
1435 /* Find out some properties of the output */
1437 uint32_t i_size, i_align;
1439 p_sys->i_min_output = 0;
1440 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1442 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1447 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1449 p_sys->i_min_output = i_size;
1453 /* Set output properties */
1454 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1455 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1456 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1458 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1464 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1465 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1468 Restore_LDT_Keeper( ldt_fs );
1470 /* Uninitialize OLE/COM */
1476 return VLC_EGENERIC;
1479 /****************************************************************************
1480 * Encode: the whole thing
1481 ****************************************************************************/
1482 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1484 encoder_sys_t *p_sys = p_enc->p_sys;
1486 block_t *p_chain = NULL;
1487 block_t *p_block_in;
1492 if( !p_data ) return NULL;
1494 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1496 /* Get picture data */
1497 int i_plane, i_line, i_width, i_src_stride;
1498 picture_t *p_pic = (picture_t *)p_data;
1501 int i_buffer = p_enc->fmt_in.video.i_width *
1502 p_enc->fmt_in.video.i_height *
1503 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1505 p_block_in = block_New( p_enc, i_buffer );
1507 /* Copy picture stride by stride */
1508 p_dst = p_block_in->p_buffer;
1509 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1511 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1512 i_width = p_pic->p[i_plane].i_visible_pitch;
1513 i_src_stride = p_pic->p[i_plane].i_pitch;
1515 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1518 vlc_memcpy( p_dst, p_src, i_width );
1520 p_src += i_src_stride;
1524 i_pts = p_pic->date;
1528 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1529 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1530 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1531 p_block_in->i_buffer );
1533 i_pts = p_aout_buffer->start_date;
1536 /* Feed input to the DMO */
1537 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, true );
1538 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1539 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1541 p_in->vt->Release( (IUnknown *)p_in );
1542 if( i_result == S_FALSE )
1544 /* No output generated */
1546 msg_Dbg( p_enc, "ProcessInput(): no output generated %"PRId64, i_pts );
1550 else if( i_result == (int)DMO_E_NOTACCEPTING )
1552 /* Need to call ProcessOutput */
1553 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1555 else if( i_result != S_OK )
1557 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1562 msg_Dbg( p_enc, "ProcessInput(): success" );
1565 /* Get output from the DMO */
1568 DMO_OUTPUT_DATA_BUFFER db;
1569 block_t *p_block_out;
1570 CMediaBuffer *p_out;
1572 p_block_out = block_New( p_enc, p_sys->i_min_output );
1573 p_block_out->i_buffer = 0;
1574 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, false);
1575 memset( &db, 0, sizeof(db) );
1576 db.pBuffer = (IMediaBuffer *)p_out;
1578 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1579 0, 1, &db, &i_status );
1581 if( i_result != S_OK )
1583 if( i_result != S_FALSE )
1584 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1587 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1590 p_out->vt->Release( (IUnknown *)p_out );
1591 block_Release( p_block_out );
1595 if( !p_block_out->i_buffer )
1598 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1600 p_out->vt->Release( (IUnknown *)p_out );
1601 block_Release( p_block_out );
1605 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1608 msg_Dbg( p_enc, "ProcessOutput(): pts: %"PRId64", %"PRId64,
1609 i_pts, db.rtTimestamp / 10 );
1611 i_pts = db.rtTimestamp / 10;
1614 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1616 p_block_out->i_length = db.rtTimelength / 10;
1618 msg_Dbg( p_enc, "ProcessOutput(): length: %"PRId64,
1619 p_block_out->i_length );
1623 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1625 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1626 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1628 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1631 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1632 block_ChainAppend( &p_chain, p_block_out );
1636 /*****************************************************************************
1637 * EncoderClose: close codec
1638 *****************************************************************************/
1639 void EncoderClose( vlc_object_t *p_this )
1641 encoder_t *p_enc = (encoder_t*)p_this;
1642 encoder_sys_t *p_sys = p_enc->p_sys;
1644 if( !p_sys ) return;
1646 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1647 FreeLibrary( p_sys->hmsdmo_dll );
1651 Restore_LDT_Keeper( p_sys->ldt_fs );
1654 /* Uninitialize OLE/COM */