1 /*****************************************************************************
2 * dmo.c : DirectMedia Object decoder module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/decoder.h>
42 /* Need the w32dll loader from mplayer */
43 # include <wine/winerror.h>
44 # include <ldt_keeper.h>
45 # include <wine/windef.h>
55 long CoInitialize( void *pvReserved ) { return -1; }
56 void CoUninitialize( void ) { }
58 /* A few prototypes */
59 HMODULE WINAPI LoadLibraryA(LPCSTR);
60 #define LoadLibrary LoadLibraryA
61 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
62 int WINAPI FreeLibrary(HMODULE);
63 typedef long STDCALL (*GETCLASS) ( const GUID*, const GUID*, void** );
66 static int pi_channels_maps[7] =
70 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
71 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
72 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
73 | AOUT_CHAN_REARRIGHT,
74 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
75 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
76 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
77 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
80 /*****************************************************************************
82 *****************************************************************************/
83 static int DecoderOpen ( vlc_object_t * );
84 static int DecOpen ( vlc_object_t * );
85 static void DecoderClose ( vlc_object_t * );
86 static void *DecodeBlock ( decoder_t *, block_t ** );
88 static int EncoderOpen ( vlc_object_t * );
89 static int EncOpen ( vlc_object_t * );
90 static void EncoderClose ( vlc_object_t * );
91 static block_t *EncodeBlock( encoder_t *, void * );
93 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
94 es_format_t *, vlc_bool_t );
95 static void CopyPicture( decoder_t *, picture_t *, uint8_t * );
98 set_description( _("DirectMedia Object decoder") );
99 add_shortcut( "dmo" );
100 set_capability( "decoder", 1 );
101 set_callbacks( DecoderOpen, DecoderClose );
103 # define ENC_CFG_PREFIX "sout-dmo-"
105 set_description( _("DirectMedia Object encoder") );
106 set_capability( "encoder", 10 );
107 set_callbacks( EncoderOpen, EncoderClose );
111 /*****************************************************************************
113 *****************************************************************************/
115 /****************************************************************************
116 * Decoder descriptor declaration
117 ****************************************************************************/
120 HINSTANCE hmsdmo_dll;
134 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
135 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
137 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
138 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
140 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
141 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
145 vlc_fourcc_t i_fourcc;
151 static const codec_dll decoders_table[] =
154 { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
155 { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
157 { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
158 { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
160 { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
161 { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
164 { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
165 { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
167 { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
168 { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
174 static const codec_dll encoders_table[] =
177 { VLC_FOURCC('W','M','V','2'), "wmvdmoe.dll", &guid_wmv_enc },
178 { VLC_FOURCC('w','m','v','1'), "wmvdmoe.dll", &guid_wmv_enc },
180 { VLC_FOURCC('W','M','V','1'), "wmvdmoe.dll", &guid_wmv_enc },
181 { VLC_FOURCC('w','m','v','1'), "wmvdmoe.dll", &guid_wmv_enc },
184 { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
185 { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
187 { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
188 { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
196 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
198 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
199 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
205 /*****************************************************************************
206 * DecoderOpen: open dmo codec
207 *****************************************************************************/
208 static int DecoderOpen( vlc_object_t *p_this )
210 decoder_t *p_dec = (decoder_t*)p_this;
213 int i_ret = DecOpen( p_this );
214 if( i_ret != VLC_SUCCESS ) return i_ret;
218 /* We can't open it now, because of ldt_keeper or something
219 * Open/Decode/Close has to be done in the same thread */
224 /* Probe if we support it */
225 for( i = 0; decoders_table[i].i_fourcc != 0; i++ )
227 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
229 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
230 (char*)&p_dec->fmt_in.i_codec,
231 decoders_table[i].psz_dll );
237 if( !decoders_table[i].i_fourcc ) return VLC_EGENERIC;
241 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
243 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
249 /*****************************************************************************
250 * DecOpen: open dmo codec
251 *****************************************************************************/
252 static int DecOpen( vlc_object_t *p_this )
254 decoder_t *p_dec = (decoder_t*)p_this;
255 decoder_sys_t *p_sys = NULL;
257 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
258 IMediaObject *p_dmo = NULL;
259 HINSTANCE hmsdmo_dll = NULL;
261 VIDEOINFOHEADER *p_vih = NULL;
262 WAVEFORMATEX *p_wf = NULL;
265 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
267 /* Initialize OLE/COM */
271 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, VLC_FALSE )
279 /* Setup input format */
280 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
281 dmo_input_type.pUnk = 0;
283 if( p_dec->fmt_in.i_cat == AUDIO_ES )
286 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
287 p_wf = malloc( i_size );
289 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
290 if( p_dec->fmt_in.i_extra )
291 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
293 dmo_input_type.majortype = MEDIATYPE_Audio;
294 dmo_input_type.subtype = dmo_input_type.majortype;
295 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
296 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
297 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
299 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
300 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
301 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
302 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
303 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
304 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
305 p_wf->cbSize = p_dec->fmt_in.i_extra;
307 dmo_input_type.formattype = FORMAT_WaveFormatEx;
308 dmo_input_type.cbFormat = i_size;
309 dmo_input_type.pbFormat = (char *)p_wf;
310 dmo_input_type.bFixedSizeSamples = 1;
311 dmo_input_type.bTemporalCompression = 0;
312 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
316 BITMAPINFOHEADER *p_bih;
318 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
319 p_vih = malloc( i_size );
321 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
322 if( p_dec->fmt_in.i_extra )
323 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
325 p_bih = &p_vih->bmiHeader;
326 p_bih->biCompression = p_dec->fmt_in.i_codec;
327 p_bih->biWidth = p_dec->fmt_in.video.i_width;
328 p_bih->biHeight = p_dec->fmt_in.video.i_height;
329 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
331 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
332 sizeof(BITMAPINFOHEADER);
334 p_vih->rcSource.left = p_vih->rcSource.top = 0;
335 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
336 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
337 p_vih->rcTarget = p_vih->rcSource;
339 dmo_input_type.majortype = MEDIATYPE_Video;
340 dmo_input_type.subtype = dmo_input_type.majortype;
341 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
342 dmo_input_type.formattype = FORMAT_VideoInfo;
343 dmo_input_type.bFixedSizeSamples = 0;
344 dmo_input_type.bTemporalCompression = 1;
345 dmo_input_type.cbFormat = i_size;
346 dmo_input_type.pbFormat = (char *)p_vih;
349 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
351 msg_Err( p_dec, "can't set DMO input type" );
354 msg_Dbg( p_dec, "DMO input type set" );
356 /* Setup output format */
357 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
358 dmo_output_type.pUnk = 0;
360 if( p_dec->fmt_in.i_cat == AUDIO_ES )
362 /* Setup the format */
363 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
364 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
365 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
366 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
367 p_dec->fmt_out.audio.i_physical_channels =
368 p_dec->fmt_out.audio.i_original_channels =
369 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
371 p_wf->wFormatTag = WAVE_FORMAT_PCM;
372 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
373 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
374 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
376 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
377 p_wf->nAvgBytesPerSec =
378 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
381 dmo_output_type.majortype = MEDIATYPE_Audio;
382 dmo_output_type.formattype = FORMAT_WaveFormatEx;
383 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
384 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
385 dmo_output_type.pbFormat = (char *)p_wf;
386 dmo_output_type.bFixedSizeSamples = 1;
387 dmo_output_type.bTemporalCompression = 0;
388 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
392 BITMAPINFOHEADER *p_bih;
394 p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
395 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
396 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
397 p_dec->fmt_out.video.i_bits_per_pixel = 12;
398 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
399 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
401 dmo_output_type.formattype = FORMAT_VideoInfo;
402 dmo_output_type.subtype = MEDIASUBTYPE_YV12;
404 p_bih = &p_vih->bmiHeader;
405 p_bih->biCompression = VLC_FOURCC('Y','V','1','2');
406 p_bih->biHeight *= -1;
407 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
408 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
409 p_dec->fmt_in.video.i_height *
410 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
412 //p_bih->biPlanes = 1;
413 p_bih->biSize = sizeof(BITMAPINFOHEADER);
415 dmo_output_type.majortype = MEDIATYPE_Video;
416 dmo_output_type.bFixedSizeSamples = VLC_TRUE;
417 dmo_output_type.bTemporalCompression = 0;
418 dmo_output_type.lSampleSize = p_bih->biSizeImage;
419 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
420 dmo_output_type.pbFormat = (char *)p_vih;
424 /* Enumerate output types */
425 if( p_dec->fmt_in.i_cat == VIDEO_ES )
430 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
432 msg_Dbg( p_dec, "available output chroma: %4.4s",
433 (char *)&mt.subtype.Data1 );
434 DMOFreeMediaType( &mt );
439 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
441 msg_Err( p_dec, "can't set DMO output type" );
444 msg_Dbg( p_dec, "DMO output type set" );
446 /* Allocate the memory needed to store the decoder's structure */
447 if( ( p_dec->p_sys = p_sys =
448 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
450 msg_Err( p_dec, "out of memory" );
454 p_sys->hmsdmo_dll = hmsdmo_dll;
455 p_sys->p_dmo = p_dmo;
457 p_sys->ldt_fs = ldt_fs;
460 /* Find out some properties of the output */
462 uint32_t i_size, i_align;
464 p_sys->i_min_output = 0;
465 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
467 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
472 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
474 p_sys->i_min_output = i_size;
475 p_sys->p_buffer = malloc( i_size );
476 if( !p_sys->p_buffer ) goto error;
480 /* Set output properties */
481 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
482 if( p_dec->fmt_out.i_cat == AUDIO_ES )
483 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
485 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
487 if( p_vih ) free( p_vih );
488 if( p_wf ) free( p_wf );
494 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
495 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
498 Restore_LDT_Keeper( ldt_fs );
500 /* Uninitialize OLE/COM */
504 if( p_vih ) free( p_vih );
505 if( p_wf ) free( p_wf );
506 if( p_sys ) free( p_sys );
511 /*****************************************************************************
512 * LoadDMO: Load the DMO object
513 *****************************************************************************/
514 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
515 IMediaObject **pp_dmo, es_format_t *p_fmt,
518 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
522 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
523 const DMO_PARTIAL_MEDIATYPE *,
524 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
527 IEnumDMO *p_enum_dmo = NULL;
533 IClassFactory *cFactory = NULL;
534 IUnknown *cObject = NULL;
535 codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
539 /* Look for a DMO which can handle the requested codec */
540 if( p_fmt->i_cat == AUDIO_ES )
543 dmo_partial_type.type = MEDIATYPE_Audio;
544 dmo_partial_type.subtype = dmo_partial_type.type;
545 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
546 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
547 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
551 dmo_partial_type.type = MEDIATYPE_Video;
552 dmo_partial_type.subtype = dmo_partial_type.type;
553 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
558 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
559 if( *p_hmsdmo_dll == NULL )
561 msg_Dbg( p_this, "failed loading msdmo.dll" );
564 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
565 if( OurDMOEnum == NULL )
567 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
568 FreeLibrary( *p_hmsdmo_dll );
574 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
575 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
579 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
580 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
585 FreeLibrary( *p_hmsdmo_dll );
589 /* Pickup the first available codec */
590 if( p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
591 &psz_dmo_name, NULL ) )
594 FreeLibrary( *p_hmsdmo_dll );
597 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
601 char psz_temp[MAX_PATH];
602 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
603 msg_Dbg( p_this, "found DMO: %s", psz_temp );
607 CoTaskMemFree( psz_dmo_name );
610 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
611 &IID_IMediaObject, (void **)pp_dmo ) )
613 msg_Err( p_this, "can't create DMO" );
615 FreeLibrary( *p_hmsdmo_dll );
620 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
622 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
625 if( codecs_table[i_codec].i_fourcc == 0 )
626 return VLC_EGENERIC; /* Can't happen */
628 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
629 if( *p_hmsdmo_dll == NULL )
631 msg_Dbg( p_this, "failed loading '%s'",
632 codecs_table[i_codec].psz_dll );
636 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
639 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
640 FreeLibrary( *p_hmsdmo_dll );
644 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
646 if( i_err || cFactory == NULL )
648 msg_Dbg( p_this, "no such class object" );
649 FreeLibrary( *p_hmsdmo_dll );
653 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
655 cFactory->vt->Release( (IUnknown*)cFactory );
656 if( i_err || !cObject )
658 msg_Dbg( p_this, "class factory failure" );
659 FreeLibrary( *p_hmsdmo_dll );
662 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
664 cObject->vt->Release( (IUnknown*)cObject );
665 if( i_err || !*pp_dmo )
667 msg_Dbg( p_this, "QueryInterface failure" );
668 FreeLibrary( *p_hmsdmo_dll );
676 /*****************************************************************************
677 * DecoderClose: close codec
678 *****************************************************************************/
679 void DecoderClose( vlc_object_t *p_this )
681 decoder_t *p_dec = (decoder_t*)p_this;
682 decoder_sys_t *p_sys = p_dec->p_sys;
686 FreeLibrary( p_sys->hmsdmo_dll );
690 Restore_LDT_Keeper( p_sys->ldt_fs );
693 /* Uninitialize OLE/COM */
697 if( p_sys->p_buffer ) free( p_sys->p_buffer );
701 /****************************************************************************
702 * DecodeBlock: the whole thing
703 ****************************************************************************
704 * This function must be fed with ogg packets.
705 ****************************************************************************/
706 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
708 decoder_sys_t *p_sys = p_dec->p_sys;
712 DMO_OUTPUT_DATA_BUFFER db;
719 if( DecOpen( VLC_OBJECT(p_dec) ) )
721 msg_Err( p_dec, "DecOpen failed" );
724 p_sys = p_dec->p_sys;
727 if( !pp_block ) return NULL;
731 /* Won't work with streams with B-frames, but do we have any ? */
732 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
734 /* Date management */
735 if( p_block && p_block->i_pts > 0 &&
736 p_block->i_pts != date_Get( &p_sys->end_date ) )
738 date_Set( &p_sys->end_date, p_block->i_pts );
741 #if 0 /* Breaks the video decoding */
742 if( !date_Get( &p_sys->end_date ) )
744 /* We've just started the stream, wait for the first PTS. */
745 if( p_block ) block_Release( p_block );
750 /* Feed input to the DMO */
751 if( p_block && p_block->i_buffer )
755 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );
757 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
758 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
761 p_in->vt->Release( (IUnknown *)p_in );
763 if( i_result == S_FALSE )
765 /* No output generated */
767 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
771 else if( i_result == DMO_E_NOTACCEPTING )
773 /* Need to call ProcessOutput */
774 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
776 else if( i_result != S_OK )
778 msg_Dbg( p_dec, "ProcessInput(): failed" );
783 //msg_Dbg( p_dec, "ProcessInput(): successful" );
787 else if( p_block && !p_block->i_buffer )
789 block_Release( p_block );
793 /* Get output from the DMO */
794 block_out.p_buffer = p_sys->p_buffer;
795 block_out.i_buffer = 0;
797 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );
798 memset( &db, 0, sizeof(db) );
799 db.pBuffer = (IMediaBuffer *)p_out;
801 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
802 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
805 if( i_result != S_OK )
807 if( i_result != S_FALSE )
808 msg_Dbg( p_dec, "ProcessOutput(): failed" );
811 msg_Dbg( p_dec, "ProcessOutput(): no output" );
814 p_out->vt->Release( (IUnknown *)p_out );
819 msg_Dbg( p_dec, "ProcessOutput(): success" );
822 if( !block_out.i_buffer )
825 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
827 p_out->vt->Release( (IUnknown *)p_out );
831 if( p_dec->fmt_out.i_cat == VIDEO_ES )
833 /* Get a new picture */
834 picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
835 if( !p_pic ) return NULL;
837 CopyPicture( p_dec, p_pic, block_out.p_buffer );
839 /* Date management */
840 p_pic->date = date_Get( &p_sys->end_date );
841 date_Increment( &p_sys->end_date, 1 );
843 p_out->vt->Release( (IUnknown *)p_out );
849 aout_buffer_t *p_aout_buffer;
850 int i_samples = block_out.i_buffer /
851 ( p_dec->fmt_out.audio.i_bitspersample *
852 p_dec->fmt_out.audio.i_channels / 8 );
854 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
855 memcpy( p_aout_buffer->p_buffer,
856 block_out.p_buffer, block_out.i_buffer );
858 /* Date management */
859 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
860 p_aout_buffer->end_date =
861 date_Increment( &p_sys->end_date, i_samples );
863 p_out->vt->Release( (IUnknown *)p_out );
865 return p_aout_buffer;
871 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )
873 int i_plane, i_line, i_width, i_dst_stride;
874 uint8_t *p_dst, *p_src = p_in;
876 p_dst = p_pic->p[1].p_pixels;
877 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
878 p_pic->p[2].p_pixels = p_dst;
880 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
882 p_dst = p_pic->p[i_plane].p_pixels;
883 i_width = p_pic->p[i_plane].i_visible_pitch;
884 i_dst_stride = p_pic->p[i_plane].i_pitch;
886 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
888 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
890 p_dst += i_dst_stride;
894 p_dst = p_pic->p[1].p_pixels;
895 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
896 p_pic->p[2].p_pixels = p_dst;
899 /****************************************************************************
900 * Encoder descriptor declaration
901 ****************************************************************************/
904 HINSTANCE hmsdmo_dll;
916 /*****************************************************************************
917 * EncoderOpen: open dmo codec
918 *****************************************************************************/
919 static int EncoderOpen( vlc_object_t *p_this )
921 encoder_t *p_enc = (encoder_t*)p_this;
924 int i_ret = EncOpen( p_this );
925 if( i_ret != VLC_SUCCESS ) return i_ret;
928 /* We can't open it now, because of ldt_keeper or something
929 * Open/Encode/Close has to be done in the same thread */
932 /* Probe if we support it */
933 for( i = 0; encoders_table[i].i_fourcc != 0; i++ )
935 if( encoders_table[i].i_fourcc == p_enc->fmt_out.i_codec )
937 msg_Dbg( p_enc, "DMO codec for %4.4s may work with dll=%s",
938 (char*)&p_enc->fmt_out.i_codec,
939 encoders_table[i].psz_dll );
945 if( !encoders_table[i].i_fourcc ) return VLC_EGENERIC;
949 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
951 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
957 /*****************************************************************************
958 * EncoderSetVideoType: configures the input and output types of the dmo
959 *****************************************************************************/
960 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
962 int i, i_selected, i_err;
963 DMO_MEDIA_TYPE dmo_type;
964 VIDEOINFOHEADER vih, *p_vih;
965 BITMAPINFOHEADER *p_bih;
968 p_enc->fmt_in.video.i_bits_per_pixel =
969 p_enc->fmt_out.video.i_bits_per_pixel = 12;
971 /* Enumerate input format (for debug output) */
973 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
975 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
977 msg_Dbg( p_enc, "available input chroma: %4.4s",
978 (char *)&dmo_type.subtype.Data1 );
979 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
980 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
981 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
982 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
984 DMOFreeMediaType( &dmo_type );
987 /* Setup input format */
988 memset( &dmo_type, 0, sizeof(dmo_type) );
990 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
992 p_bih = &vih.bmiHeader;
993 p_bih->biCompression = VLC_FOURCC('I','4','2','0');
994 p_bih->biWidth = p_enc->fmt_in.video.i_width;
995 p_bih->biHeight = p_enc->fmt_in.video.i_height;
996 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
997 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
998 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1000 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1002 vih.rcSource.left = vih.rcSource.top = 0;
1003 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1004 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1005 vih.rcTarget = vih.rcSource;
1007 vih.AvgTimePerFrame = I64C(10000000) / 25; //FIXME
1009 dmo_type.majortype = MEDIATYPE_Video;
1010 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1011 dmo_type.subtype = MEDIASUBTYPE_I420;
1012 //dmo_type.subtype.Data1 = p_bih->biCompression;
1013 dmo_type.formattype = FORMAT_VideoInfo;
1014 dmo_type.bFixedSizeSamples = TRUE;
1015 dmo_type.bTemporalCompression = FALSE;
1016 dmo_type.lSampleSize = p_bih->biSizeImage;
1017 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1018 dmo_type.pbFormat = (char *)&vih;
1020 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1022 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1023 return VLC_EGENERIC;
1026 msg_Dbg( p_enc, "successfully set input type" );
1028 /* Setup output format */
1029 memset( &dmo_type, 0, sizeof(dmo_type) );
1032 /* Enumerate output types */
1033 i = 0, i_selected = -1;
1034 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1036 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1038 msg_Dbg( p_enc, "available output codec: %4.4s",
1039 (char *)&dmo_type.subtype.Data1 );
1041 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1044 DMOFreeMediaType( &dmo_type );
1047 if( i_selected < 0 )
1049 msg_Err( p_enc, "couldn't find codec: %4.4s",
1050 (char *)&p_enc->fmt_out.i_codec );
1051 return VLC_EGENERIC;
1054 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1055 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1056 p_enc->fmt_out.i_bitrate;
1058 /* Get the private data for the codec */
1061 IWMCodecPrivateData *p_privdata;
1062 VIDEOINFOHEADER *p_vih;
1063 uint8_t *p_data = 0;
1064 uint32_t i_data = 0, i_vih;
1066 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1067 &IID_IWMCodecPrivateData,
1068 (void **)&p_privdata );
1071 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1074 msg_Err( p_enc, "SetPartialOutputType() failed" );
1075 p_privdata->vt->Release( (IUnknown *)p_privdata );
1079 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1082 msg_Err( p_enc, "GetPrivateData() failed" );
1083 p_privdata->vt->Release( (IUnknown *)p_privdata );
1087 p_data = malloc( i_data );
1088 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1090 /* Update the media type with the private data */
1091 i_vih = dmo_type.cbFormat + i_data;
1092 p_vih = CoTaskMemAlloc( i_vih );
1093 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1094 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1095 DMOFreeMediaType( &dmo_type );
1096 dmo_type.pbFormat = p_vih;
1097 dmo_type.cbFormat = i_vih;
1099 msg_Dbg( p_enc, "found extra data: %i", i_data );
1100 p_enc->fmt_out.i_extra = i_data;
1101 p_enc->fmt_out.p_extra = p_data;
1105 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1107 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1108 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
1110 DMOFreeMediaType( &dmo_type );
1113 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1114 return VLC_EGENERIC;
1117 msg_Dbg( p_enc, "successfully set output type" );
1121 /*****************************************************************************
1122 * EncoderSetAudioType: configures the input and output types of the dmo
1123 *****************************************************************************/
1124 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1126 int i, i_selected, i_err;
1127 unsigned int i_last_byterate;
1129 DMO_MEDIA_TYPE dmo_type;
1132 /* Setup the format structure */
1133 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1134 if( i_tag == 0 ) return VLC_EGENERIC;
1136 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1137 p_enc->fmt_in.audio.i_bitspersample = 16;
1139 /* We first need to choose an output type from the predefined
1140 * list of choices (we cycle through the list to select the best match) */
1141 i = 0; i_selected = -1; i_last_byterate = 0;
1142 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1144 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1145 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1146 "bits per sample: %i, bitrate: %i, blockalign: %i",
1147 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1148 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1149 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1151 if( p_wf->wFormatTag == i_tag &&
1152 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1153 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1154 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1156 if( (int)p_wf->nAvgBytesPerSec <
1157 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1158 p_wf->nAvgBytesPerSec > i_last_byterate )
1161 i_last_byterate = p_wf->nAvgBytesPerSec;
1162 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1163 i_selected, p_wf->nAvgBytesPerSec * 8 );
1167 DMOFreeMediaType( &dmo_type );
1170 if( i_selected < 0 )
1172 msg_Err( p_enc, "couldn't find a matching ouput" );
1173 return VLC_EGENERIC;
1176 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1177 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1179 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1180 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1181 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1182 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1183 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1185 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1186 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1187 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1188 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1189 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1193 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1194 p_enc->fmt_out.i_extra = p_wf->cbSize;
1195 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1196 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1199 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1200 DMOFreeMediaType( &dmo_type );
1204 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1205 return VLC_EGENERIC;
1208 msg_Dbg( p_enc, "successfully set output type" );
1210 /* Setup the input type */
1211 i = 0; i_selected = -1;
1212 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1214 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1215 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1216 "bits per sample: %i, bitrate: %i, blockalign: %i",
1217 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1218 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1219 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1221 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1222 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1223 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1224 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1229 DMOFreeMediaType( &dmo_type );
1232 if( i_selected < 0 )
1234 msg_Err( p_enc, "couldn't find a matching input" );
1235 return VLC_EGENERIC;
1238 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1239 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1240 DMOFreeMediaType( &dmo_type );
1243 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1244 return VLC_EGENERIC;
1246 msg_Dbg( p_enc, "successfully set input type" );
1251 /*****************************************************************************
1252 * EncOpen: open dmo codec
1253 *****************************************************************************/
1254 static int EncOpen( vlc_object_t *p_this )
1256 encoder_t *p_enc = (encoder_t*)p_this;
1257 encoder_sys_t *p_sys = NULL;
1258 IMediaObject *p_dmo = NULL;
1259 HINSTANCE hmsdmo_dll = NULL;
1262 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1264 /* Initialize OLE/COM */
1268 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, VLC_TRUE )
1276 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1278 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1282 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1285 /* Allocate the memory needed to store the decoder's structure */
1286 if( ( p_enc->p_sys = p_sys =
1287 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1289 msg_Err( p_enc, "out of memory" );
1293 p_sys->hmsdmo_dll = hmsdmo_dll;
1294 p_sys->p_dmo = p_dmo;
1296 p_sys->ldt_fs = ldt_fs;
1299 /* Find out some properties of the inputput */
1301 uint32_t i_size, i_align, dum;
1303 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1304 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1306 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1307 i_size, i_align, dum );
1310 /* Find out some properties of the output */
1312 uint32_t i_size, i_align;
1314 p_sys->i_min_output = 0;
1315 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1317 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1322 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1324 p_sys->i_min_output = i_size;
1328 /* Set output properties */
1329 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1330 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1331 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1333 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1339 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1340 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1343 Restore_LDT_Keeper( ldt_fs );
1345 /* Uninitialize OLE/COM */
1349 if( p_sys ) free( p_sys );
1351 return VLC_EGENERIC;
1354 /****************************************************************************
1355 * Encode: the whole thing
1356 ****************************************************************************/
1357 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1359 encoder_sys_t *p_sys = p_enc->p_sys;
1361 block_t *p_chain = NULL;
1362 block_t *p_block_in;
1369 if( EncOpen( VLC_OBJECT(p_enc) ) )
1371 msg_Err( p_enc, "EncOpen failed" );
1374 p_sys = p_enc->p_sys;
1377 if( !p_data ) return NULL;
1379 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1381 /* Get picture data */
1382 int i_plane, i_line, i_width, i_src_stride;
1383 picture_t *p_pic = (picture_t *)p_data;
1386 int i_buffer = p_enc->fmt_in.video.i_width *
1387 p_enc->fmt_in.video.i_height *
1388 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1390 p_block_in = block_New( p_enc, i_buffer );
1392 /* Copy picture stride by stride */
1393 p_dst = p_block_in->p_buffer;
1394 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1396 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1397 i_width = p_pic->p[i_plane].i_visible_pitch;
1398 i_src_stride = p_pic->p[i_plane].i_pitch;
1400 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1403 p_enc->p_vlc->pf_memcpy( p_dst, p_src, i_width );
1405 p_src += i_src_stride;
1409 i_pts = p_pic->date;
1413 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1414 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1415 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1416 p_block_in->i_buffer );
1418 i_pts = p_aout_buffer->start_date;
1421 /* Feed input to the DMO */
1422 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, VLC_TRUE );
1423 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1424 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1426 p_in->vt->Release( (IUnknown *)p_in );
1427 if( i_result == S_FALSE )
1429 /* No output generated */
1431 msg_Dbg( p_enc, "ProcessInput(): no output generated "I64Fd, i_pts );
1435 else if( i_result == DMO_E_NOTACCEPTING )
1437 /* Need to call ProcessOutput */
1438 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1440 else if( i_result != S_OK )
1442 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1447 msg_Dbg( p_enc, "ProcessInput(): success" );
1450 /* Get output from the DMO */
1453 DMO_OUTPUT_DATA_BUFFER db;
1454 block_t *p_block_out;
1455 CMediaBuffer *p_out;
1457 p_block_out = block_New( p_enc, p_sys->i_min_output );
1458 p_block_out->i_buffer = 0;
1459 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, VLC_FALSE);
1460 memset( &db, 0, sizeof(db) );
1461 db.pBuffer = (IMediaBuffer *)p_out;
1463 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1464 0, 1, &db, &i_status );
1466 if( i_result != S_OK )
1468 if( i_result != S_FALSE )
1469 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1472 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1475 p_out->vt->Release( (IUnknown *)p_out );
1476 block_Release( p_block_out );
1480 if( !p_block_out->i_buffer )
1483 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1485 p_out->vt->Release( (IUnknown *)p_out );
1486 block_Release( p_block_out );
1490 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1493 msg_Dbg( p_enc, "ProcessOutput(): pts: "I64Fd", "I64Fd,
1494 i_pts, db.rtTimestamp / 10 );
1496 i_pts = db.rtTimestamp / 10;
1499 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1501 p_block_out->i_length = db.rtTimelength / 10;
1503 msg_Dbg( p_enc, "ProcessOutput(): length: "I64Fd,
1504 p_block_out->i_length );
1508 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1510 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1511 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1513 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1516 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1517 block_ChainAppend( &p_chain, p_block_out );
1521 /*****************************************************************************
1522 * EncoderClose: close codec
1523 *****************************************************************************/
1524 void EncoderClose( vlc_object_t *p_this )
1526 encoder_t *p_enc = (encoder_t*)p_this;
1527 encoder_sys_t *p_sys = p_enc->p_sys;
1529 if( !p_sys ) return;
1531 FreeLibrary( p_sys->hmsdmo_dll );
1535 Restore_LDT_Keeper( p_sys->ldt_fs );
1538 /* Uninitialize OLE/COM */