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 );
103 set_category( CAT_INPUT );
104 set_subcategory( SUBCAT_INPUT_SCODEC );
106 # define ENC_CFG_PREFIX "sout-dmo-"
108 set_description( _("DirectMedia Object encoder") );
109 set_capability( "encoder", 10 );
110 set_callbacks( EncoderOpen, EncoderClose );
114 /*****************************************************************************
116 *****************************************************************************/
118 /****************************************************************************
119 * Decoder descriptor declaration
120 ****************************************************************************/
123 HINSTANCE hmsdmo_dll;
136 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
137 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
139 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
140 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
142 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
143 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
147 vlc_fourcc_t i_fourcc;
153 static const codec_dll decoders_table[] =
156 { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
157 { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
159 { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
160 { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
162 { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
163 { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
166 { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
167 { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
169 { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
170 { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
176 static const codec_dll encoders_table[] =
179 { VLC_FOURCC('W','M','V','2'), "wmvdmoe.dll", &guid_wmv_enc },
180 { VLC_FOURCC('w','m','v','1'), "wmvdmoe.dll", &guid_wmv_enc },
182 { VLC_FOURCC('W','M','V','1'), "wmvdmoe.dll", &guid_wmv_enc },
183 { VLC_FOURCC('w','m','v','1'), "wmvdmoe.dll", &guid_wmv_enc },
186 { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
187 { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
189 { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
190 { 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 int i_chroma = VLC_FOURCC('Y','U','Y','2'), i_planes = 1, i_bpp = 16;
397 /* Find out which chroma to use */
398 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
400 if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
402 i_chroma = mt.subtype.Data1;
403 i_planes = 3; i_bpp = 12;
406 DMOFreeMediaType( &mt );
409 p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
410 VLC_FOURCC('I','4','2','0') : i_chroma;
411 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
412 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
413 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
414 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
415 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
417 p_bih = &p_vih->bmiHeader;
418 p_bih->biCompression = i_chroma;
419 p_bih->biHeight *= -1;
420 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
421 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
422 p_dec->fmt_in.video.i_height *
423 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
425 p_bih->biPlanes = i_planes;
426 p_bih->biSize = sizeof(BITMAPINFOHEADER);
428 dmo_output_type.majortype = MEDIATYPE_Video;
429 dmo_output_type.formattype = FORMAT_VideoInfo;
430 dmo_output_type.subtype = dmo_output_type.majortype;
431 dmo_output_type.subtype.Data1 = p_bih->biCompression;
432 dmo_output_type.bFixedSizeSamples = VLC_TRUE;
433 dmo_output_type.bTemporalCompression = 0;
434 dmo_output_type.lSampleSize = p_bih->biSizeImage;
435 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
436 dmo_output_type.pbFormat = (char *)p_vih;
440 /* Enumerate output types */
441 if( p_dec->fmt_in.i_cat == VIDEO_ES )
446 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
448 msg_Dbg( p_dec, "available output chroma: %4.4s",
449 (char *)&mt.subtype.Data1 );
450 DMOFreeMediaType( &mt );
455 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
457 msg_Err( p_dec, "can't set DMO output type" );
460 msg_Dbg( p_dec, "DMO output type set" );
462 /* Allocate the memory needed to store the decoder's structure */
463 if( ( p_dec->p_sys = p_sys =
464 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
466 msg_Err( p_dec, "out of memory" );
470 p_sys->hmsdmo_dll = hmsdmo_dll;
471 p_sys->p_dmo = p_dmo;
473 p_sys->ldt_fs = ldt_fs;
476 /* Find out some properties of the output */
478 uint32_t i_size, i_align;
480 p_sys->i_min_output = 0;
481 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
483 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
488 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
490 p_sys->i_min_output = i_size;
491 p_sys->p_buffer = malloc( i_size );
492 if( !p_sys->p_buffer ) goto error;
496 /* Set output properties */
497 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
498 if( p_dec->fmt_out.i_cat == AUDIO_ES )
499 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
501 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
503 if( p_vih ) free( p_vih );
504 if( p_wf ) free( p_wf );
510 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
511 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
514 Restore_LDT_Keeper( ldt_fs );
516 /* Uninitialize OLE/COM */
520 if( p_vih ) free( p_vih );
521 if( p_wf ) free( p_wf );
522 if( p_sys ) free( p_sys );
527 /*****************************************************************************
528 * LoadDMO: Load the DMO object
529 *****************************************************************************/
530 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
531 IMediaObject **pp_dmo, es_format_t *p_fmt,
534 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
538 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
539 const DMO_PARTIAL_MEDIATYPE *,
540 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
543 IEnumDMO *p_enum_dmo = NULL;
550 IClassFactory *cFactory = NULL;
551 IUnknown *cObject = NULL;
552 codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
555 /* Look for a DMO which can handle the requested codec */
556 if( p_fmt->i_cat == AUDIO_ES )
559 dmo_partial_type.type = MEDIATYPE_Audio;
560 dmo_partial_type.subtype = dmo_partial_type.type;
561 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
562 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
563 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
567 dmo_partial_type.type = MEDIATYPE_Video;
568 dmo_partial_type.subtype = dmo_partial_type.type;
569 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
574 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
575 if( *p_hmsdmo_dll == NULL )
577 msg_Dbg( p_this, "failed loading msdmo.dll" );
580 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
581 if( OurDMOEnum == NULL )
583 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
584 FreeLibrary( *p_hmsdmo_dll );
590 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
591 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
595 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
596 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
600 FreeLibrary( *p_hmsdmo_dll );
601 /* return VLC_EGENERIC; */
602 /* Try loading the dll directly */
606 /* Pickup the first available codec */
608 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
609 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
611 char psz_temp[MAX_PATH];
612 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
613 msg_Dbg( p_this, "found DMO: %s", psz_temp );
614 CoTaskMemFree( psz_dmo_name );
617 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
618 &IID_IMediaObject, (void **)pp_dmo ) )
620 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
626 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
630 FreeLibrary( *p_hmsdmo_dll );
631 /* return VLC_EGENERIC; */
632 /* Try loading the dll directly */
640 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
642 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
645 if( codecs_table[i_codec].i_fourcc == 0 )
646 return VLC_EGENERIC; /* Can't happen */
648 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
649 if( *p_hmsdmo_dll == NULL )
651 msg_Dbg( p_this, "failed loading '%s'",
652 codecs_table[i_codec].psz_dll );
656 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
659 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
660 FreeLibrary( *p_hmsdmo_dll );
664 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
666 if( i_err || cFactory == NULL )
668 msg_Dbg( p_this, "no such class object" );
669 FreeLibrary( *p_hmsdmo_dll );
673 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
675 cFactory->vt->Release( (IUnknown*)cFactory );
676 if( i_err || !cObject )
678 msg_Dbg( p_this, "class factory failure" );
679 FreeLibrary( *p_hmsdmo_dll );
682 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
684 cObject->vt->Release( (IUnknown*)cObject );
685 if( i_err || !*pp_dmo )
687 msg_Dbg( p_this, "QueryInterface failure" );
688 FreeLibrary( *p_hmsdmo_dll );
695 /*****************************************************************************
696 * DecoderClose: close codec
697 *****************************************************************************/
698 void DecoderClose( vlc_object_t *p_this )
700 decoder_t *p_dec = (decoder_t*)p_this;
701 decoder_sys_t *p_sys = p_dec->p_sys;
705 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
706 FreeLibrary( p_sys->hmsdmo_dll );
710 Restore_LDT_Keeper( p_sys->ldt_fs );
713 /* Uninitialize OLE/COM */
717 if( p_sys->p_buffer ) free( p_sys->p_buffer );
721 /****************************************************************************
722 * DecodeBlock: the whole thing
723 ****************************************************************************
724 * This function must be fed with ogg packets.
725 ****************************************************************************/
726 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
728 decoder_sys_t *p_sys = p_dec->p_sys;
732 DMO_OUTPUT_DATA_BUFFER db;
739 if( DecOpen( VLC_OBJECT(p_dec) ) )
741 msg_Err( p_dec, "DecOpen failed" );
744 p_sys = p_dec->p_sys;
747 if( !pp_block ) return NULL;
751 /* Won't work with streams with B-frames, but do we have any ? */
752 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
754 /* Date management */
755 if( p_block && p_block->i_pts > 0 &&
756 p_block->i_pts != date_Get( &p_sys->end_date ) )
758 date_Set( &p_sys->end_date, p_block->i_pts );
761 #if 0 /* Breaks the video decoding */
762 if( !date_Get( &p_sys->end_date ) )
764 /* We've just started the stream, wait for the first PTS. */
765 if( p_block ) block_Release( p_block );
770 /* Feed input to the DMO */
771 if( p_block && p_block->i_buffer )
775 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );
777 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
778 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
781 p_in->vt->Release( (IUnknown *)p_in );
783 if( i_result == S_FALSE )
785 /* No output generated */
787 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
791 else if( i_result == DMO_E_NOTACCEPTING )
793 /* Need to call ProcessOutput */
794 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
796 else if( i_result != S_OK )
798 msg_Dbg( p_dec, "ProcessInput(): failed" );
803 //msg_Dbg( p_dec, "ProcessInput(): successful" );
807 else if( p_block && !p_block->i_buffer )
809 block_Release( p_block );
813 /* Get output from the DMO */
814 block_out.p_buffer = p_sys->p_buffer;
815 block_out.i_buffer = 0;
817 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );
818 memset( &db, 0, sizeof(db) );
819 db.pBuffer = (IMediaBuffer *)p_out;
821 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
822 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
825 if( i_result != S_OK )
827 if( i_result != S_FALSE )
828 msg_Dbg( p_dec, "ProcessOutput(): failed" );
831 msg_Dbg( p_dec, "ProcessOutput(): no output" );
834 p_out->vt->Release( (IUnknown *)p_out );
839 msg_Dbg( p_dec, "ProcessOutput(): success" );
842 if( !block_out.i_buffer )
845 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
847 p_out->vt->Release( (IUnknown *)p_out );
851 if( p_dec->fmt_out.i_cat == VIDEO_ES )
853 /* Get a new picture */
854 picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
855 if( !p_pic ) return NULL;
857 CopyPicture( p_dec, p_pic, block_out.p_buffer );
859 /* Date management */
860 p_pic->date = date_Get( &p_sys->end_date );
861 date_Increment( &p_sys->end_date, 1 );
863 p_out->vt->Release( (IUnknown *)p_out );
869 aout_buffer_t *p_aout_buffer;
870 int i_samples = block_out.i_buffer /
871 ( p_dec->fmt_out.audio.i_bitspersample *
872 p_dec->fmt_out.audio.i_channels / 8 );
874 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
875 memcpy( p_aout_buffer->p_buffer,
876 block_out.p_buffer, block_out.i_buffer );
878 /* Date management */
879 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
880 p_aout_buffer->end_date =
881 date_Increment( &p_sys->end_date, i_samples );
883 p_out->vt->Release( (IUnknown *)p_out );
885 return p_aout_buffer;
891 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )
893 int i_plane, i_line, i_width, i_dst_stride;
894 uint8_t *p_dst, *p_src = p_in;
896 p_dst = p_pic->p[1].p_pixels;
897 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
898 p_pic->p[2].p_pixels = p_dst;
900 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
902 p_dst = p_pic->p[i_plane].p_pixels;
903 i_width = p_pic->p[i_plane].i_visible_pitch;
904 i_dst_stride = p_pic->p[i_plane].i_pitch;
906 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
908 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
910 p_dst += i_dst_stride;
914 p_dst = p_pic->p[1].p_pixels;
915 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
916 p_pic->p[2].p_pixels = p_dst;
919 /****************************************************************************
920 * Encoder descriptor declaration
921 ****************************************************************************/
924 HINSTANCE hmsdmo_dll;
936 /*****************************************************************************
937 * EncoderOpen: open dmo codec
938 *****************************************************************************/
939 static int EncoderOpen( vlc_object_t *p_this )
941 encoder_t *p_enc = (encoder_t*)p_this;
944 int i_ret = EncOpen( p_this );
945 if( i_ret != VLC_SUCCESS ) return i_ret;
948 /* We can't open it now, because of ldt_keeper or something
949 * Open/Encode/Close has to be done in the same thread */
952 /* Probe if we support it */
953 for( i = 0; encoders_table[i].i_fourcc != 0; i++ )
955 if( encoders_table[i].i_fourcc == p_enc->fmt_out.i_codec )
957 msg_Dbg( p_enc, "DMO codec for %4.4s may work with dll=%s",
958 (char*)&p_enc->fmt_out.i_codec,
959 encoders_table[i].psz_dll );
965 if( !encoders_table[i].i_fourcc ) return VLC_EGENERIC;
969 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
971 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
977 /*****************************************************************************
978 * EncoderSetVideoType: configures the input and output types of the dmo
979 *****************************************************************************/
980 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
982 int i, i_selected, i_err;
983 DMO_MEDIA_TYPE dmo_type;
984 VIDEOINFOHEADER vih, *p_vih;
985 BITMAPINFOHEADER *p_bih;
988 p_enc->fmt_in.video.i_bits_per_pixel =
989 p_enc->fmt_out.video.i_bits_per_pixel = 12;
991 /* Enumerate input format (for debug output) */
993 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
995 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
997 msg_Dbg( p_enc, "available input chroma: %4.4s",
998 (char *)&dmo_type.subtype.Data1 );
999 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1000 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1001 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1002 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1004 DMOFreeMediaType( &dmo_type );
1007 /* Setup input format */
1008 memset( &dmo_type, 0, sizeof(dmo_type) );
1010 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1012 p_bih = &vih.bmiHeader;
1013 p_bih->biCompression = VLC_FOURCC('I','4','2','0');
1014 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1015 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1016 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1017 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1018 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1019 p_bih->biPlanes = 3;
1020 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1022 vih.rcSource.left = vih.rcSource.top = 0;
1023 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1024 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1025 vih.rcTarget = vih.rcSource;
1027 vih.AvgTimePerFrame = I64C(10000000) / 25; //FIXME
1029 dmo_type.majortype = MEDIATYPE_Video;
1030 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1031 dmo_type.subtype = MEDIASUBTYPE_I420;
1032 //dmo_type.subtype.Data1 = p_bih->biCompression;
1033 dmo_type.formattype = FORMAT_VideoInfo;
1034 dmo_type.bFixedSizeSamples = TRUE;
1035 dmo_type.bTemporalCompression = FALSE;
1036 dmo_type.lSampleSize = p_bih->biSizeImage;
1037 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1038 dmo_type.pbFormat = (char *)&vih;
1040 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1042 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1043 return VLC_EGENERIC;
1046 msg_Dbg( p_enc, "successfully set input type" );
1048 /* Setup output format */
1049 memset( &dmo_type, 0, sizeof(dmo_type) );
1052 /* Enumerate output types */
1053 i = 0, i_selected = -1;
1054 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1056 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1058 msg_Dbg( p_enc, "available output codec: %4.4s",
1059 (char *)&dmo_type.subtype.Data1 );
1061 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1064 DMOFreeMediaType( &dmo_type );
1067 if( i_selected < 0 )
1069 msg_Err( p_enc, "couldn't find codec: %4.4s",
1070 (char *)&p_enc->fmt_out.i_codec );
1071 return VLC_EGENERIC;
1074 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1075 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1076 p_enc->fmt_out.i_bitrate;
1078 /* Get the private data for the codec */
1081 IWMCodecPrivateData *p_privdata;
1082 VIDEOINFOHEADER *p_vih;
1083 uint8_t *p_data = 0;
1084 uint32_t i_data = 0, i_vih;
1086 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1087 &IID_IWMCodecPrivateData,
1088 (void **)&p_privdata );
1091 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1094 msg_Err( p_enc, "SetPartialOutputType() failed" );
1095 p_privdata->vt->Release( (IUnknown *)p_privdata );
1099 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1102 msg_Err( p_enc, "GetPrivateData() failed" );
1103 p_privdata->vt->Release( (IUnknown *)p_privdata );
1107 p_data = malloc( i_data );
1108 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1110 /* Update the media type with the private data */
1111 i_vih = dmo_type.cbFormat + i_data;
1112 p_vih = CoTaskMemAlloc( i_vih );
1113 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1114 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1115 DMOFreeMediaType( &dmo_type );
1116 dmo_type.pbFormat = p_vih;
1117 dmo_type.cbFormat = i_vih;
1119 msg_Dbg( p_enc, "found extra data: %i", i_data );
1120 p_enc->fmt_out.i_extra = i_data;
1121 p_enc->fmt_out.p_extra = p_data;
1125 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1127 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1128 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
1130 DMOFreeMediaType( &dmo_type );
1133 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1134 return VLC_EGENERIC;
1137 msg_Dbg( p_enc, "successfully set output type" );
1141 /*****************************************************************************
1142 * EncoderSetAudioType: configures the input and output types of the dmo
1143 *****************************************************************************/
1144 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1146 int i, i_selected, i_err;
1147 unsigned int i_last_byterate;
1149 DMO_MEDIA_TYPE dmo_type;
1152 /* Setup the format structure */
1153 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1154 if( i_tag == 0 ) return VLC_EGENERIC;
1156 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1157 p_enc->fmt_in.audio.i_bitspersample = 16;
1159 /* We first need to choose an output type from the predefined
1160 * list of choices (we cycle through the list to select the best match) */
1161 i = 0; i_selected = -1; i_last_byterate = 0;
1162 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1164 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1165 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1166 "bits per sample: %i, bitrate: %i, blockalign: %i",
1167 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1168 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1169 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1171 if( p_wf->wFormatTag == i_tag &&
1172 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1173 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1174 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1176 if( (int)p_wf->nAvgBytesPerSec <
1177 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1178 p_wf->nAvgBytesPerSec > i_last_byterate )
1181 i_last_byterate = p_wf->nAvgBytesPerSec;
1182 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1183 i_selected, p_wf->nAvgBytesPerSec * 8 );
1187 DMOFreeMediaType( &dmo_type );
1190 if( i_selected < 0 )
1192 msg_Err( p_enc, "couldn't find a matching ouput" );
1193 return VLC_EGENERIC;
1196 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1197 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1199 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1200 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1201 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1202 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1203 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1205 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1206 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1207 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1208 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1209 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1213 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1214 p_enc->fmt_out.i_extra = p_wf->cbSize;
1215 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1216 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1219 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1220 DMOFreeMediaType( &dmo_type );
1224 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1225 return VLC_EGENERIC;
1228 msg_Dbg( p_enc, "successfully set output type" );
1230 /* Setup the input type */
1231 i = 0; i_selected = -1;
1232 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1234 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1235 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1236 "bits per sample: %i, bitrate: %i, blockalign: %i",
1237 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1238 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1239 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1241 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1242 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1243 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1244 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1249 DMOFreeMediaType( &dmo_type );
1252 if( i_selected < 0 )
1254 msg_Err( p_enc, "couldn't find a matching input" );
1255 return VLC_EGENERIC;
1258 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1259 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1260 DMOFreeMediaType( &dmo_type );
1263 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1264 return VLC_EGENERIC;
1266 msg_Dbg( p_enc, "successfully set input type" );
1271 /*****************************************************************************
1272 * EncOpen: open dmo codec
1273 *****************************************************************************/
1274 static int EncOpen( vlc_object_t *p_this )
1276 encoder_t *p_enc = (encoder_t*)p_this;
1277 encoder_sys_t *p_sys = NULL;
1278 IMediaObject *p_dmo = NULL;
1279 HINSTANCE hmsdmo_dll = NULL;
1282 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1284 /* Initialize OLE/COM */
1288 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, VLC_TRUE )
1296 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1298 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1302 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1305 /* Allocate the memory needed to store the decoder's structure */
1306 if( ( p_enc->p_sys = p_sys =
1307 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1309 msg_Err( p_enc, "out of memory" );
1313 p_sys->hmsdmo_dll = hmsdmo_dll;
1314 p_sys->p_dmo = p_dmo;
1316 p_sys->ldt_fs = ldt_fs;
1319 /* Find out some properties of the inputput */
1321 uint32_t i_size, i_align, dum;
1323 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1324 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1326 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1327 i_size, i_align, dum );
1330 /* Find out some properties of the output */
1332 uint32_t i_size, i_align;
1334 p_sys->i_min_output = 0;
1335 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1337 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1342 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1344 p_sys->i_min_output = i_size;
1348 /* Set output properties */
1349 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1350 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1351 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1353 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1359 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1360 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1363 Restore_LDT_Keeper( ldt_fs );
1365 /* Uninitialize OLE/COM */
1369 if( p_sys ) free( p_sys );
1371 return VLC_EGENERIC;
1374 /****************************************************************************
1375 * Encode: the whole thing
1376 ****************************************************************************/
1377 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1379 encoder_sys_t *p_sys = p_enc->p_sys;
1381 block_t *p_chain = NULL;
1382 block_t *p_block_in;
1389 if( EncOpen( VLC_OBJECT(p_enc) ) )
1391 msg_Err( p_enc, "EncOpen failed" );
1394 p_sys = p_enc->p_sys;
1397 if( !p_data ) return NULL;
1399 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1401 /* Get picture data */
1402 int i_plane, i_line, i_width, i_src_stride;
1403 picture_t *p_pic = (picture_t *)p_data;
1406 int i_buffer = p_enc->fmt_in.video.i_width *
1407 p_enc->fmt_in.video.i_height *
1408 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1410 p_block_in = block_New( p_enc, i_buffer );
1412 /* Copy picture stride by stride */
1413 p_dst = p_block_in->p_buffer;
1414 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1416 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1417 i_width = p_pic->p[i_plane].i_visible_pitch;
1418 i_src_stride = p_pic->p[i_plane].i_pitch;
1420 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1423 p_enc->p_vlc->pf_memcpy( p_dst, p_src, i_width );
1425 p_src += i_src_stride;
1429 i_pts = p_pic->date;
1433 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1434 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1435 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1436 p_block_in->i_buffer );
1438 i_pts = p_aout_buffer->start_date;
1441 /* Feed input to the DMO */
1442 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, VLC_TRUE );
1443 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1444 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1446 p_in->vt->Release( (IUnknown *)p_in );
1447 if( i_result == S_FALSE )
1449 /* No output generated */
1451 msg_Dbg( p_enc, "ProcessInput(): no output generated "I64Fd, i_pts );
1455 else if( i_result == DMO_E_NOTACCEPTING )
1457 /* Need to call ProcessOutput */
1458 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1460 else if( i_result != S_OK )
1462 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1467 msg_Dbg( p_enc, "ProcessInput(): success" );
1470 /* Get output from the DMO */
1473 DMO_OUTPUT_DATA_BUFFER db;
1474 block_t *p_block_out;
1475 CMediaBuffer *p_out;
1477 p_block_out = block_New( p_enc, p_sys->i_min_output );
1478 p_block_out->i_buffer = 0;
1479 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, VLC_FALSE);
1480 memset( &db, 0, sizeof(db) );
1481 db.pBuffer = (IMediaBuffer *)p_out;
1483 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1484 0, 1, &db, &i_status );
1486 if( i_result != S_OK )
1488 if( i_result != S_FALSE )
1489 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1492 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1495 p_out->vt->Release( (IUnknown *)p_out );
1496 block_Release( p_block_out );
1500 if( !p_block_out->i_buffer )
1503 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1505 p_out->vt->Release( (IUnknown *)p_out );
1506 block_Release( p_block_out );
1510 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1513 msg_Dbg( p_enc, "ProcessOutput(): pts: "I64Fd", "I64Fd,
1514 i_pts, db.rtTimestamp / 10 );
1516 i_pts = db.rtTimestamp / 10;
1519 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1521 p_block_out->i_length = db.rtTimelength / 10;
1523 msg_Dbg( p_enc, "ProcessOutput(): length: "I64Fd,
1524 p_block_out->i_length );
1528 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1530 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1531 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1533 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1536 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1537 block_ChainAppend( &p_chain, p_block_out );
1541 /*****************************************************************************
1542 * EncoderClose: close codec
1543 *****************************************************************************/
1544 void EncoderClose( vlc_object_t *p_this )
1546 encoder_t *p_enc = (encoder_t*)p_this;
1547 encoder_sys_t *p_sys = p_enc->p_sys;
1549 if( !p_sys ) return;
1551 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1552 FreeLibrary( p_sys->hmsdmo_dll );
1556 Restore_LDT_Keeper( p_sys->ldt_fs );
1559 /* Uninitialize OLE/COM */