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);
65 typedef long STDCALL (*GETCLASS) ( const GUID*, const GUID*, void** );
67 static int pi_channels_maps[7] =
71 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
72 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
73 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
74 | AOUT_CHAN_REARRIGHT,
75 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
76 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
77 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
78 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
81 /*****************************************************************************
83 *****************************************************************************/
84 static int DecoderOpen ( vlc_object_t * );
85 static int DecOpen ( vlc_object_t * );
86 static void DecoderClose ( vlc_object_t * );
87 static void *DecodeBlock ( decoder_t *, block_t ** );
89 static int EncoderOpen ( vlc_object_t * );
90 static int EncOpen ( vlc_object_t * );
91 static void EncoderClose ( vlc_object_t * );
92 static block_t *EncodeBlock( encoder_t *, void * );
94 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
95 es_format_t *, vlc_bool_t );
96 static void CopyPicture( decoder_t *, picture_t *, uint8_t * );
99 set_description( _("DirectMedia Object decoder") );
100 add_shortcut( "dmo" );
101 set_capability( "decoder", 1 );
102 set_callbacks( DecoderOpen, DecoderClose );
104 # define ENC_CFG_PREFIX "sout-dmo-"
106 set_description( _("DirectMedia Object encoder") );
107 set_capability( "encoder", 10 );
108 set_callbacks( EncoderOpen, EncoderClose );
112 /*****************************************************************************
114 *****************************************************************************/
116 /****************************************************************************
117 * Decoder descriptor declaration
118 ****************************************************************************/
121 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 },
194 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
196 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
197 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
203 /*****************************************************************************
204 * DecoderOpen: open dmo codec
205 *****************************************************************************/
206 static int DecoderOpen( vlc_object_t *p_this )
208 decoder_t *p_dec = (decoder_t*)p_this;
211 int i_ret = DecOpen( p_this );
212 if( i_ret != VLC_SUCCESS ) return i_ret;
216 /* We can't open it now, because of ldt_keeper or something
217 * Open/Decode/Close has to be done in the same thread */
222 /* Probe if we support it */
223 for( i = 0; decoders_table[i].i_fourcc != 0; i++ )
225 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
227 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
228 (char*)&p_dec->fmt_in.i_codec,
229 decoders_table[i].psz_dll );
235 if( !decoders_table[i].i_fourcc ) return VLC_EGENERIC;
239 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
241 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
247 /*****************************************************************************
248 * DecOpen: open dmo codec
249 *****************************************************************************/
250 static int DecOpen( vlc_object_t *p_this )
252 decoder_t *p_dec = (decoder_t*)p_this;
253 decoder_sys_t *p_sys = NULL;
255 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
256 IMediaObject *p_dmo = NULL;
257 HINSTANCE hmsdmo_dll = NULL;
259 VIDEOINFOHEADER *p_vih = NULL;
260 WAVEFORMATEX *p_wf = NULL;
263 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
265 /* Initialize OLE/COM */
269 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, VLC_FALSE )
277 /* Setup input format */
278 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
279 dmo_input_type.pUnk = 0;
281 if( p_dec->fmt_in.i_cat == AUDIO_ES )
284 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
285 p_wf = malloc( i_size );
287 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
288 if( p_dec->fmt_in.i_extra )
289 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
291 dmo_input_type.majortype = MEDIATYPE_Audio;
292 dmo_input_type.subtype = dmo_input_type.majortype;
293 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
294 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
295 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
297 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
298 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
299 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
300 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
301 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
302 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
303 p_wf->cbSize = p_dec->fmt_in.i_extra;
305 dmo_input_type.formattype = FORMAT_WaveFormatEx;
306 dmo_input_type.cbFormat = i_size;
307 dmo_input_type.pbFormat = (char *)p_wf;
308 dmo_input_type.bFixedSizeSamples = 1;
309 dmo_input_type.bTemporalCompression = 0;
310 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
314 BITMAPINFOHEADER *p_bih;
316 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
317 p_vih = malloc( i_size );
319 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
320 if( p_dec->fmt_in.i_extra )
321 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
323 p_bih = &p_vih->bmiHeader;
324 p_bih->biCompression = p_dec->fmt_in.i_codec;
325 p_bih->biWidth = p_dec->fmt_in.video.i_width;
326 p_bih->biHeight = p_dec->fmt_in.video.i_height;
327 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
329 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
330 sizeof(BITMAPINFOHEADER);
332 p_vih->rcSource.left = p_vih->rcSource.top = 0;
333 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
334 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
335 p_vih->rcTarget = p_vih->rcSource;
337 dmo_input_type.majortype = MEDIATYPE_Video;
338 dmo_input_type.subtype = dmo_input_type.majortype;
339 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
340 dmo_input_type.formattype = FORMAT_VideoInfo;
341 dmo_input_type.bFixedSizeSamples = 0;
342 dmo_input_type.bTemporalCompression = 1;
343 dmo_input_type.cbFormat = i_size;
344 dmo_input_type.pbFormat = (char *)p_vih;
347 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
349 msg_Err( p_dec, "can't set DMO input type" );
352 msg_Dbg( p_dec, "DMO input type set" );
354 /* Setup output format */
355 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
356 dmo_output_type.pUnk = 0;
358 if( p_dec->fmt_in.i_cat == AUDIO_ES )
360 /* Setup the format */
361 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
362 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
363 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
364 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
365 p_dec->fmt_out.audio.i_physical_channels =
366 p_dec->fmt_out.audio.i_original_channels =
367 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
369 p_wf->wFormatTag = WAVE_FORMAT_PCM;
370 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
371 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
372 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
374 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
375 p_wf->nAvgBytesPerSec =
376 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
379 dmo_output_type.majortype = MEDIATYPE_Audio;
380 dmo_output_type.formattype = FORMAT_WaveFormatEx;
381 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
382 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
383 dmo_output_type.pbFormat = (char *)p_wf;
384 dmo_output_type.bFixedSizeSamples = 1;
385 dmo_output_type.bTemporalCompression = 0;
386 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
390 BITMAPINFOHEADER *p_bih;
392 int i_chroma = VLC_FOURCC('Y','U','Y','2'), i_planes = 1, i_bpp = 16;
395 /* Find out which chroma to use */
396 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
398 if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
400 i_chroma = mt.subtype.Data1;
401 i_planes = 3; i_bpp = 12;
404 DMOFreeMediaType( &mt );
407 p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
408 VLC_FOURCC('I','4','2','0') : i_chroma;
409 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
410 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
411 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
412 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
413 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
415 p_bih = &p_vih->bmiHeader;
416 p_bih->biCompression = i_chroma;
417 p_bih->biHeight *= -1;
418 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
419 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
420 p_dec->fmt_in.video.i_height *
421 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
423 p_bih->biPlanes = i_planes;
424 p_bih->biSize = sizeof(BITMAPINFOHEADER);
426 dmo_output_type.majortype = MEDIATYPE_Video;
427 dmo_output_type.formattype = FORMAT_VideoInfo;
428 dmo_output_type.subtype = dmo_output_type.majortype;
429 dmo_output_type.subtype.Data1 = p_bih->biCompression;
430 dmo_output_type.bFixedSizeSamples = VLC_TRUE;
431 dmo_output_type.bTemporalCompression = 0;
432 dmo_output_type.lSampleSize = p_bih->biSizeImage;
433 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
434 dmo_output_type.pbFormat = (char *)p_vih;
438 /* Enumerate output types */
439 if( p_dec->fmt_in.i_cat == VIDEO_ES )
444 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
446 msg_Dbg( p_dec, "available output chroma: %4.4s",
447 (char *)&mt.subtype.Data1 );
448 DMOFreeMediaType( &mt );
453 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
455 msg_Err( p_dec, "can't set DMO output type" );
458 msg_Dbg( p_dec, "DMO output type set" );
460 /* Allocate the memory needed to store the decoder's structure */
461 if( ( p_dec->p_sys = p_sys =
462 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
464 msg_Err( p_dec, "out of memory" );
468 p_sys->hmsdmo_dll = hmsdmo_dll;
469 p_sys->p_dmo = p_dmo;
471 p_sys->ldt_fs = ldt_fs;
474 /* Find out some properties of the output */
476 uint32_t i_size, i_align;
478 p_sys->i_min_output = 0;
479 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
481 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
486 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
488 p_sys->i_min_output = i_size;
489 p_sys->p_buffer = malloc( i_size );
490 if( !p_sys->p_buffer ) goto error;
494 /* Set output properties */
495 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
496 if( p_dec->fmt_out.i_cat == AUDIO_ES )
497 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
499 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
501 if( p_vih ) free( p_vih );
502 if( p_wf ) free( p_wf );
508 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
509 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
512 Restore_LDT_Keeper( ldt_fs );
514 /* Uninitialize OLE/COM */
518 if( p_vih ) free( p_vih );
519 if( p_wf ) free( p_wf );
520 if( p_sys ) free( p_sys );
525 /*****************************************************************************
526 * LoadDMO: Load the DMO object
527 *****************************************************************************/
528 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
529 IMediaObject **pp_dmo, es_format_t *p_fmt,
532 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
536 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
537 const DMO_PARTIAL_MEDIATYPE *,
538 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
541 IEnumDMO *p_enum_dmo = NULL;
547 IClassFactory *cFactory = NULL;
548 IUnknown *cObject = NULL;
549 codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
552 /* Look for a DMO which can handle the requested codec */
553 if( p_fmt->i_cat == AUDIO_ES )
556 dmo_partial_type.type = MEDIATYPE_Audio;
557 dmo_partial_type.subtype = dmo_partial_type.type;
558 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
559 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
560 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
564 dmo_partial_type.type = MEDIATYPE_Video;
565 dmo_partial_type.subtype = dmo_partial_type.type;
566 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
571 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
572 if( *p_hmsdmo_dll == NULL )
574 msg_Dbg( p_this, "failed loading msdmo.dll" );
577 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
578 if( OurDMOEnum == NULL )
580 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
581 FreeLibrary( *p_hmsdmo_dll );
587 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
588 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
592 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
593 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
597 FreeLibrary( *p_hmsdmo_dll );
598 /* return VLC_EGENERIC; */
599 /* Try loading the dll directly */
603 /* Pickup the first available codec */
604 if( p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
605 &psz_dmo_name, NULL ) )
607 FreeLibrary( *p_hmsdmo_dll );
610 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
614 char psz_temp[MAX_PATH];
615 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
616 msg_Dbg( p_this, "found DMO: %s", psz_temp );
620 CoTaskMemFree( psz_dmo_name );
623 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
624 &IID_IMediaObject, (void **)pp_dmo ) )
626 msg_Err( p_this, "can't create DMO" );
627 FreeLibrary( *p_hmsdmo_dll );
635 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
637 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
640 if( codecs_table[i_codec].i_fourcc == 0 )
641 return VLC_EGENERIC; /* Can't happen */
643 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
644 if( *p_hmsdmo_dll == NULL )
646 msg_Dbg( p_this, "failed loading '%s'",
647 codecs_table[i_codec].psz_dll );
651 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
654 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
655 FreeLibrary( *p_hmsdmo_dll );
659 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
661 if( i_err || cFactory == NULL )
663 msg_Dbg( p_this, "no such class object" );
664 FreeLibrary( *p_hmsdmo_dll );
668 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
670 cFactory->vt->Release( (IUnknown*)cFactory );
671 if( i_err || !cObject )
673 msg_Dbg( p_this, "class factory failure" );
674 FreeLibrary( *p_hmsdmo_dll );
677 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
679 cObject->vt->Release( (IUnknown*)cObject );
680 if( i_err || !*pp_dmo )
682 msg_Dbg( p_this, "QueryInterface failure" );
683 FreeLibrary( *p_hmsdmo_dll );
690 /*****************************************************************************
691 * DecoderClose: close codec
692 *****************************************************************************/
693 void DecoderClose( vlc_object_t *p_this )
695 decoder_t *p_dec = (decoder_t*)p_this;
696 decoder_sys_t *p_sys = p_dec->p_sys;
700 FreeLibrary( p_sys->hmsdmo_dll );
704 Restore_LDT_Keeper( p_sys->ldt_fs );
707 /* Uninitialize OLE/COM */
711 if( p_sys->p_buffer ) free( p_sys->p_buffer );
715 /****************************************************************************
716 * DecodeBlock: the whole thing
717 ****************************************************************************
718 * This function must be fed with ogg packets.
719 ****************************************************************************/
720 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
722 decoder_sys_t *p_sys = p_dec->p_sys;
726 DMO_OUTPUT_DATA_BUFFER db;
733 if( DecOpen( VLC_OBJECT(p_dec) ) )
735 msg_Err( p_dec, "DecOpen failed" );
738 p_sys = p_dec->p_sys;
741 if( !pp_block ) return NULL;
745 /* Won't work with streams with B-frames, but do we have any ? */
746 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
748 /* Date management */
749 if( p_block && p_block->i_pts > 0 &&
750 p_block->i_pts != date_Get( &p_sys->end_date ) )
752 date_Set( &p_sys->end_date, p_block->i_pts );
755 #if 0 /* Breaks the video decoding */
756 if( !date_Get( &p_sys->end_date ) )
758 /* We've just started the stream, wait for the first PTS. */
759 if( p_block ) block_Release( p_block );
764 /* Feed input to the DMO */
765 if( p_block && p_block->i_buffer )
769 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );
771 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
772 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
775 p_in->vt->Release( (IUnknown *)p_in );
777 if( i_result == S_FALSE )
779 /* No output generated */
781 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
785 else if( i_result == DMO_E_NOTACCEPTING )
787 /* Need to call ProcessOutput */
788 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
790 else if( i_result != S_OK )
792 msg_Dbg( p_dec, "ProcessInput(): failed" );
797 //msg_Dbg( p_dec, "ProcessInput(): successful" );
801 else if( p_block && !p_block->i_buffer )
803 block_Release( p_block );
807 /* Get output from the DMO */
808 block_out.p_buffer = p_sys->p_buffer;
809 block_out.i_buffer = 0;
811 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );
812 memset( &db, 0, sizeof(db) );
813 db.pBuffer = (IMediaBuffer *)p_out;
815 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
816 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
819 if( i_result != S_OK )
821 if( i_result != S_FALSE )
822 msg_Dbg( p_dec, "ProcessOutput(): failed" );
825 msg_Dbg( p_dec, "ProcessOutput(): no output" );
828 p_out->vt->Release( (IUnknown *)p_out );
833 msg_Dbg( p_dec, "ProcessOutput(): success" );
836 if( !block_out.i_buffer )
839 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
841 p_out->vt->Release( (IUnknown *)p_out );
845 if( p_dec->fmt_out.i_cat == VIDEO_ES )
847 /* Get a new picture */
848 picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
849 if( !p_pic ) return NULL;
851 CopyPicture( p_dec, p_pic, block_out.p_buffer );
853 /* Date management */
854 p_pic->date = date_Get( &p_sys->end_date );
855 date_Increment( &p_sys->end_date, 1 );
857 p_out->vt->Release( (IUnknown *)p_out );
863 aout_buffer_t *p_aout_buffer;
864 int i_samples = block_out.i_buffer /
865 ( p_dec->fmt_out.audio.i_bitspersample *
866 p_dec->fmt_out.audio.i_channels / 8 );
868 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
869 memcpy( p_aout_buffer->p_buffer,
870 block_out.p_buffer, block_out.i_buffer );
872 /* Date management */
873 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
874 p_aout_buffer->end_date =
875 date_Increment( &p_sys->end_date, i_samples );
877 p_out->vt->Release( (IUnknown *)p_out );
879 return p_aout_buffer;
885 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )
887 int i_plane, i_line, i_width, i_dst_stride;
888 uint8_t *p_dst, *p_src = p_in;
890 p_dst = p_pic->p[1].p_pixels;
891 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
892 p_pic->p[2].p_pixels = p_dst;
894 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
896 p_dst = p_pic->p[i_plane].p_pixels;
897 i_width = p_pic->p[i_plane].i_visible_pitch;
898 i_dst_stride = p_pic->p[i_plane].i_pitch;
900 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
902 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
904 p_dst += i_dst_stride;
908 p_dst = p_pic->p[1].p_pixels;
909 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
910 p_pic->p[2].p_pixels = p_dst;
913 /****************************************************************************
914 * Encoder descriptor declaration
915 ****************************************************************************/
918 HINSTANCE hmsdmo_dll;
930 /*****************************************************************************
931 * EncoderOpen: open dmo codec
932 *****************************************************************************/
933 static int EncoderOpen( vlc_object_t *p_this )
935 encoder_t *p_enc = (encoder_t*)p_this;
938 int i_ret = EncOpen( p_this );
939 if( i_ret != VLC_SUCCESS ) return i_ret;
942 /* We can't open it now, because of ldt_keeper or something
943 * Open/Encode/Close has to be done in the same thread */
946 /* Probe if we support it */
947 for( i = 0; encoders_table[i].i_fourcc != 0; i++ )
949 if( encoders_table[i].i_fourcc == p_enc->fmt_out.i_codec )
951 msg_Dbg( p_enc, "DMO codec for %4.4s may work with dll=%s",
952 (char*)&p_enc->fmt_out.i_codec,
953 encoders_table[i].psz_dll );
959 if( !encoders_table[i].i_fourcc ) return VLC_EGENERIC;
963 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
965 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
971 /*****************************************************************************
972 * EncoderSetVideoType: configures the input and output types of the dmo
973 *****************************************************************************/
974 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
976 int i, i_selected, i_err;
977 DMO_MEDIA_TYPE dmo_type;
978 VIDEOINFOHEADER vih, *p_vih;
979 BITMAPINFOHEADER *p_bih;
982 p_enc->fmt_in.video.i_bits_per_pixel =
983 p_enc->fmt_out.video.i_bits_per_pixel = 12;
985 /* Enumerate input format (for debug output) */
987 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
989 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
991 msg_Dbg( p_enc, "available input chroma: %4.4s",
992 (char *)&dmo_type.subtype.Data1 );
993 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
994 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
995 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
996 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
998 DMOFreeMediaType( &dmo_type );
1001 /* Setup input format */
1002 memset( &dmo_type, 0, sizeof(dmo_type) );
1004 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1006 p_bih = &vih.bmiHeader;
1007 p_bih->biCompression = VLC_FOURCC('I','4','2','0');
1008 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1009 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1010 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1011 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1012 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1013 p_bih->biPlanes = 3;
1014 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1016 vih.rcSource.left = vih.rcSource.top = 0;
1017 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1018 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1019 vih.rcTarget = vih.rcSource;
1021 vih.AvgTimePerFrame = I64C(10000000) / 25; //FIXME
1023 dmo_type.majortype = MEDIATYPE_Video;
1024 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1025 dmo_type.subtype = MEDIASUBTYPE_I420;
1026 //dmo_type.subtype.Data1 = p_bih->biCompression;
1027 dmo_type.formattype = FORMAT_VideoInfo;
1028 dmo_type.bFixedSizeSamples = TRUE;
1029 dmo_type.bTemporalCompression = FALSE;
1030 dmo_type.lSampleSize = p_bih->biSizeImage;
1031 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1032 dmo_type.pbFormat = (char *)&vih;
1034 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1036 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1037 return VLC_EGENERIC;
1040 msg_Dbg( p_enc, "successfully set input type" );
1042 /* Setup output format */
1043 memset( &dmo_type, 0, sizeof(dmo_type) );
1046 /* Enumerate output types */
1047 i = 0, i_selected = -1;
1048 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1050 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1052 msg_Dbg( p_enc, "available output codec: %4.4s",
1053 (char *)&dmo_type.subtype.Data1 );
1055 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1058 DMOFreeMediaType( &dmo_type );
1061 if( i_selected < 0 )
1063 msg_Err( p_enc, "couldn't find codec: %4.4s",
1064 (char *)&p_enc->fmt_out.i_codec );
1065 return VLC_EGENERIC;
1068 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1069 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1070 p_enc->fmt_out.i_bitrate;
1072 /* Get the private data for the codec */
1075 IWMCodecPrivateData *p_privdata;
1076 VIDEOINFOHEADER *p_vih;
1077 uint8_t *p_data = 0;
1078 uint32_t i_data = 0, i_vih;
1080 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1081 &IID_IWMCodecPrivateData,
1082 (void **)&p_privdata );
1085 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1088 msg_Err( p_enc, "SetPartialOutputType() failed" );
1089 p_privdata->vt->Release( (IUnknown *)p_privdata );
1093 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1096 msg_Err( p_enc, "GetPrivateData() failed" );
1097 p_privdata->vt->Release( (IUnknown *)p_privdata );
1101 p_data = malloc( i_data );
1102 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1104 /* Update the media type with the private data */
1105 i_vih = dmo_type.cbFormat + i_data;
1106 p_vih = CoTaskMemAlloc( i_vih );
1107 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1108 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1109 DMOFreeMediaType( &dmo_type );
1110 dmo_type.pbFormat = p_vih;
1111 dmo_type.cbFormat = i_vih;
1113 msg_Dbg( p_enc, "found extra data: %i", i_data );
1114 p_enc->fmt_out.i_extra = i_data;
1115 p_enc->fmt_out.p_extra = p_data;
1119 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1121 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1122 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
1124 DMOFreeMediaType( &dmo_type );
1127 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1128 return VLC_EGENERIC;
1131 msg_Dbg( p_enc, "successfully set output type" );
1135 /*****************************************************************************
1136 * EncoderSetAudioType: configures the input and output types of the dmo
1137 *****************************************************************************/
1138 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1140 int i, i_selected, i_err;
1141 unsigned int i_last_byterate;
1143 DMO_MEDIA_TYPE dmo_type;
1146 /* Setup the format structure */
1147 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1148 if( i_tag == 0 ) return VLC_EGENERIC;
1150 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1151 p_enc->fmt_in.audio.i_bitspersample = 16;
1153 /* We first need to choose an output type from the predefined
1154 * list of choices (we cycle through the list to select the best match) */
1155 i = 0; i_selected = -1; i_last_byterate = 0;
1156 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1158 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1159 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1160 "bits per sample: %i, bitrate: %i, blockalign: %i",
1161 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1162 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1163 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1165 if( p_wf->wFormatTag == i_tag &&
1166 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1167 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1168 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1170 if( (int)p_wf->nAvgBytesPerSec <
1171 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1172 p_wf->nAvgBytesPerSec > i_last_byterate )
1175 i_last_byterate = p_wf->nAvgBytesPerSec;
1176 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1177 i_selected, p_wf->nAvgBytesPerSec * 8 );
1181 DMOFreeMediaType( &dmo_type );
1184 if( i_selected < 0 )
1186 msg_Err( p_enc, "couldn't find a matching ouput" );
1187 return VLC_EGENERIC;
1190 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1191 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1193 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1194 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1195 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1196 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1197 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1199 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1200 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1201 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1202 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1203 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1207 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1208 p_enc->fmt_out.i_extra = p_wf->cbSize;
1209 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1210 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1213 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1214 DMOFreeMediaType( &dmo_type );
1218 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1219 return VLC_EGENERIC;
1222 msg_Dbg( p_enc, "successfully set output type" );
1224 /* Setup the input type */
1225 i = 0; i_selected = -1;
1226 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1228 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1229 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1230 "bits per sample: %i, bitrate: %i, blockalign: %i",
1231 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1232 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1233 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1235 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1236 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1237 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1238 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1243 DMOFreeMediaType( &dmo_type );
1246 if( i_selected < 0 )
1248 msg_Err( p_enc, "couldn't find a matching input" );
1249 return VLC_EGENERIC;
1252 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1253 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1254 DMOFreeMediaType( &dmo_type );
1257 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1258 return VLC_EGENERIC;
1260 msg_Dbg( p_enc, "successfully set input type" );
1265 /*****************************************************************************
1266 * EncOpen: open dmo codec
1267 *****************************************************************************/
1268 static int EncOpen( vlc_object_t *p_this )
1270 encoder_t *p_enc = (encoder_t*)p_this;
1271 encoder_sys_t *p_sys = NULL;
1272 IMediaObject *p_dmo = NULL;
1273 HINSTANCE hmsdmo_dll = NULL;
1276 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1278 /* Initialize OLE/COM */
1282 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, VLC_TRUE )
1290 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1292 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1296 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1299 /* Allocate the memory needed to store the decoder's structure */
1300 if( ( p_enc->p_sys = p_sys =
1301 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1303 msg_Err( p_enc, "out of memory" );
1307 p_sys->hmsdmo_dll = hmsdmo_dll;
1308 p_sys->p_dmo = p_dmo;
1310 p_sys->ldt_fs = ldt_fs;
1313 /* Find out some properties of the inputput */
1315 uint32_t i_size, i_align, dum;
1317 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1318 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1320 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1321 i_size, i_align, dum );
1324 /* Find out some properties of the output */
1326 uint32_t i_size, i_align;
1328 p_sys->i_min_output = 0;
1329 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1331 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1336 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1338 p_sys->i_min_output = i_size;
1342 /* Set output properties */
1343 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1344 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1345 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1347 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1353 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1354 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1357 Restore_LDT_Keeper( ldt_fs );
1359 /* Uninitialize OLE/COM */
1363 if( p_sys ) free( p_sys );
1365 return VLC_EGENERIC;
1368 /****************************************************************************
1369 * Encode: the whole thing
1370 ****************************************************************************/
1371 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1373 encoder_sys_t *p_sys = p_enc->p_sys;
1375 block_t *p_chain = NULL;
1376 block_t *p_block_in;
1383 if( EncOpen( VLC_OBJECT(p_enc) ) )
1385 msg_Err( p_enc, "EncOpen failed" );
1388 p_sys = p_enc->p_sys;
1391 if( !p_data ) return NULL;
1393 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1395 /* Get picture data */
1396 int i_plane, i_line, i_width, i_src_stride;
1397 picture_t *p_pic = (picture_t *)p_data;
1400 int i_buffer = p_enc->fmt_in.video.i_width *
1401 p_enc->fmt_in.video.i_height *
1402 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1404 p_block_in = block_New( p_enc, i_buffer );
1406 /* Copy picture stride by stride */
1407 p_dst = p_block_in->p_buffer;
1408 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1410 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1411 i_width = p_pic->p[i_plane].i_visible_pitch;
1412 i_src_stride = p_pic->p[i_plane].i_pitch;
1414 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1417 p_enc->p_vlc->pf_memcpy( p_dst, p_src, i_width );
1419 p_src += i_src_stride;
1423 i_pts = p_pic->date;
1427 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1428 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1429 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1430 p_block_in->i_buffer );
1432 i_pts = p_aout_buffer->start_date;
1435 /* Feed input to the DMO */
1436 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, VLC_TRUE );
1437 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1438 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1440 p_in->vt->Release( (IUnknown *)p_in );
1441 if( i_result == S_FALSE )
1443 /* No output generated */
1445 msg_Dbg( p_enc, "ProcessInput(): no output generated "I64Fd, i_pts );
1449 else if( i_result == DMO_E_NOTACCEPTING )
1451 /* Need to call ProcessOutput */
1452 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1454 else if( i_result != S_OK )
1456 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1461 msg_Dbg( p_enc, "ProcessInput(): success" );
1464 /* Get output from the DMO */
1467 DMO_OUTPUT_DATA_BUFFER db;
1468 block_t *p_block_out;
1469 CMediaBuffer *p_out;
1471 p_block_out = block_New( p_enc, p_sys->i_min_output );
1472 p_block_out->i_buffer = 0;
1473 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, VLC_FALSE);
1474 memset( &db, 0, sizeof(db) );
1475 db.pBuffer = (IMediaBuffer *)p_out;
1477 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1478 0, 1, &db, &i_status );
1480 if( i_result != S_OK )
1482 if( i_result != S_FALSE )
1483 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1486 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1489 p_out->vt->Release( (IUnknown *)p_out );
1490 block_Release( p_block_out );
1494 if( !p_block_out->i_buffer )
1497 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1499 p_out->vt->Release( (IUnknown *)p_out );
1500 block_Release( p_block_out );
1504 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1507 msg_Dbg( p_enc, "ProcessOutput(): pts: "I64Fd", "I64Fd,
1508 i_pts, db.rtTimestamp / 10 );
1510 i_pts = db.rtTimestamp / 10;
1513 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1515 p_block_out->i_length = db.rtTimelength / 10;
1517 msg_Dbg( p_enc, "ProcessOutput(): length: "I64Fd,
1518 p_block_out->i_length );
1522 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1524 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1525 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1527 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1530 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1531 block_ChainAppend( &p_chain, p_block_out );
1535 /*****************************************************************************
1536 * EncoderClose: close codec
1537 *****************************************************************************/
1538 void EncoderClose( vlc_object_t *p_this )
1540 encoder_t *p_enc = (encoder_t*)p_this;
1541 encoder_sys_t *p_sys = p_enc->p_sys;
1543 if( !p_sys ) return;
1545 FreeLibrary( p_sys->hmsdmo_dll );
1549 Restore_LDT_Keeper( p_sys->ldt_fs );
1552 /* Uninitialize OLE/COM */