1 /*****************************************************************************
2 * dmo.c : DirectMedia Object decoder module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 the VideoLAN team
7 * Author: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_codec.h>
40 /* Need the w32dll loader from mplayer */
41 # include <wine/winerror.h>
42 # include <ldt_keeper.h>
43 # include <wine/windef.h>
46 #include <vlc_codecs.h>
53 long CoInitialize( void *pvReserved ) { return -1; }
54 void CoUninitialize( void ) { }
56 /* A few prototypes */
57 HMODULE WINAPI LoadLibraryA(LPCSTR);
58 #define LoadLibrary LoadLibraryA
59 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
60 int WINAPI FreeLibrary(HMODULE);
63 typedef long (STDCALL *GETCLASS) ( const GUID*, const GUID*, void** );
65 static int pi_channels_maps[7] =
69 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
70 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
71 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
72 | AOUT_CHAN_REARRIGHT,
73 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
74 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
75 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
76 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
79 /*****************************************************************************
81 *****************************************************************************/
82 static int DecoderOpen ( vlc_object_t * );
83 static int DecOpen ( vlc_object_t * );
84 static void DecoderClose ( vlc_object_t * );
85 static void *DecodeBlock ( decoder_t *, block_t ** );
87 static int EncoderOpen ( vlc_object_t * );
88 static int EncOpen ( vlc_object_t * );
89 static void EncoderClose ( vlc_object_t * );
90 static block_t *EncodeBlock( encoder_t *, void * );
92 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
93 es_format_t *, vlc_bool_t );
94 static void CopyPicture( decoder_t *, picture_t *, uint8_t * );
97 set_description( _("DirectMedia Object decoder") );
98 add_shortcut( "dmo" );
99 set_capability( "decoder", 1 );
100 set_callbacks( DecoderOpen, DecoderClose );
101 set_category( CAT_INPUT );
102 set_subcategory( SUBCAT_INPUT_SCODEC );
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_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
135 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
137 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
138 static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
139 static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };
141 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
142 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
144 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
145 static const GUID guid_wmv_enc2 = { 0x96b57cdd, 0x8966, 0x410c,{ 0xbb, 0x1f, 0xc9, 0x7e, 0xea, 0x76, 0x5c, 0x04 } };
146 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
150 vlc_fourcc_t i_fourcc;
156 static const codec_dll decoders_table[] =
159 { VLC_FOURCC('W','V','C','1'), "wvc1dmod.dll", &guid_wvc1 },
160 { VLC_FOURCC('w','v','c','1'), "wvc1dmod.dll", &guid_wvc1 },
162 { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
163 { VLC_FOURCC('W','M','V','P'), "wmv9dmod.dll", &guid_wmv9 },
164 { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
166 { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
167 { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
169 { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
170 { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
172 { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
173 { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
174 /* Windows Media Video Adv */
175 { VLC_FOURCC('W','M','V','A'), "wmvadvd.dll", &guid_wmva },
176 { VLC_FOURCC('W','V','P','2'), "wmvadvd.dll", &guid_wmva },
179 { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
180 { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
181 { VLC_FOURCC('w','m','a','p'), "wma9dmod.dll", &guid_wma9 },
183 { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
184 { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
187 { VLC_FOURCC('w','m','a','s'), "wmspdmod.dll", &guid_wma },
193 static const codec_dll encoders_table[] =
196 { VLC_FOURCC('W','M','V','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
197 { VLC_FOURCC('w','m','v','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
199 { VLC_FOURCC('W','M','V','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
200 { VLC_FOURCC('w','m','v','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
202 { VLC_FOURCC('W','M','V','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
203 { VLC_FOURCC('w','m','v','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
206 { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
207 { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
209 { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
210 { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
216 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
218 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
219 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
225 /*****************************************************************************
226 * DecoderOpen: open dmo codec
227 *****************************************************************************/
228 static int DecoderOpen( vlc_object_t *p_this )
230 decoder_t *p_dec = (decoder_t*)p_this;
233 int i_ret = DecOpen( p_this );
234 if( i_ret != VLC_SUCCESS ) return i_ret;
238 /* We can't open it now, because of ldt_keeper or something
239 * Open/Decode/Close has to be done in the same thread */
244 /* Probe if we support it */
245 for( i = 0; decoders_table[i].i_fourcc != 0; i++ )
247 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
249 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
250 (char*)&p_dec->fmt_in.i_codec,
251 decoders_table[i].psz_dll );
257 if( !decoders_table[i].i_fourcc ) return VLC_EGENERIC;
261 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
263 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
269 /*****************************************************************************
270 * DecOpen: open dmo codec
271 *****************************************************************************/
272 static int DecOpen( vlc_object_t *p_this )
274 decoder_t *p_dec = (decoder_t*)p_this;
275 decoder_sys_t *p_sys = NULL;
277 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
278 IMediaObject *p_dmo = NULL;
279 HINSTANCE hmsdmo_dll = NULL;
281 VIDEOINFOHEADER *p_vih = NULL;
282 WAVEFORMATEX *p_wf = NULL;
285 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
287 /* Initialize OLE/COM */
291 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, VLC_FALSE )
299 /* Setup input format */
300 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
301 dmo_input_type.pUnk = 0;
303 if( p_dec->fmt_in.i_cat == AUDIO_ES )
306 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
307 p_wf = malloc( i_size );
309 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
310 if( p_dec->fmt_in.i_extra )
311 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
313 dmo_input_type.majortype = MEDIATYPE_Audio;
314 dmo_input_type.subtype = dmo_input_type.majortype;
315 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
316 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
317 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
319 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
320 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
321 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
322 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
323 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
324 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
325 p_wf->cbSize = p_dec->fmt_in.i_extra;
327 dmo_input_type.formattype = FORMAT_WaveFormatEx;
328 dmo_input_type.cbFormat = i_size;
329 dmo_input_type.pbFormat = (char *)p_wf;
330 dmo_input_type.bFixedSizeSamples = 1;
331 dmo_input_type.bTemporalCompression = 0;
332 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
336 BITMAPINFOHEADER *p_bih;
338 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
339 p_vih = malloc( i_size );
341 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
342 if( p_dec->fmt_in.i_extra )
343 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
345 p_bih = &p_vih->bmiHeader;
346 p_bih->biCompression = p_dec->fmt_in.i_codec;
347 p_bih->biWidth = p_dec->fmt_in.video.i_width;
348 p_bih->biHeight = p_dec->fmt_in.video.i_height;
349 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
351 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
352 sizeof(BITMAPINFOHEADER);
354 p_vih->rcSource.left = p_vih->rcSource.top = 0;
355 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
356 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
357 p_vih->rcTarget = p_vih->rcSource;
359 dmo_input_type.majortype = MEDIATYPE_Video;
360 dmo_input_type.subtype = dmo_input_type.majortype;
361 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
362 dmo_input_type.formattype = FORMAT_VideoInfo;
363 dmo_input_type.bFixedSizeSamples = 0;
364 dmo_input_type.bTemporalCompression = 1;
365 dmo_input_type.cbFormat = i_size;
366 dmo_input_type.pbFormat = (char *)p_vih;
369 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
371 msg_Err( p_dec, "can't set DMO input type" );
374 msg_Dbg( p_dec, "DMO input type set" );
376 /* Setup output format */
377 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
378 dmo_output_type.pUnk = 0;
380 if( p_dec->fmt_in.i_cat == AUDIO_ES )
382 /* Setup the format */
383 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
384 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
385 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
386 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
387 p_dec->fmt_out.audio.i_physical_channels =
388 p_dec->fmt_out.audio.i_original_channels =
389 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
391 p_wf->wFormatTag = WAVE_FORMAT_PCM;
392 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
393 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
394 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
396 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
397 p_wf->nAvgBytesPerSec =
398 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
401 dmo_output_type.majortype = MEDIATYPE_Audio;
402 dmo_output_type.formattype = FORMAT_WaveFormatEx;
403 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
404 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
405 dmo_output_type.pbFormat = (char *)p_wf;
406 dmo_output_type.bFixedSizeSamples = 1;
407 dmo_output_type.bTemporalCompression = 0;
408 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
412 BITMAPINFOHEADER *p_bih;
414 unsigned i_chroma = VLC_FOURCC('Y','U','Y','2');
415 int i_planes = 1, i_bpp = 16;
418 /* Find out which chroma to use */
419 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
421 if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
423 i_chroma = mt.subtype.Data1;
424 i_planes = 3; i_bpp = 12;
427 DMOFreeMediaType( &mt );
430 p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
431 VLC_FOURCC('I','4','2','0') : i_chroma;
432 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
433 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
434 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
436 /* If an aspect-ratio was specified in the input format then force it */
437 if( p_dec->fmt_in.video.i_aspect )
439 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
443 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
444 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
447 p_bih = &p_vih->bmiHeader;
448 p_bih->biCompression = i_chroma;
449 p_bih->biHeight *= -1;
450 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
451 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
452 p_dec->fmt_in.video.i_height *
453 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
455 p_bih->biPlanes = i_planes;
456 p_bih->biSize = sizeof(BITMAPINFOHEADER);
458 dmo_output_type.majortype = MEDIATYPE_Video;
459 dmo_output_type.formattype = FORMAT_VideoInfo;
460 dmo_output_type.subtype = dmo_output_type.majortype;
461 dmo_output_type.subtype.Data1 = p_bih->biCompression;
462 dmo_output_type.bFixedSizeSamples = VLC_TRUE;
463 dmo_output_type.bTemporalCompression = 0;
464 dmo_output_type.lSampleSize = p_bih->biSizeImage;
465 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
466 dmo_output_type.pbFormat = (char *)p_vih;
470 /* Enumerate output types */
471 if( p_dec->fmt_in.i_cat == VIDEO_ES )
476 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
478 msg_Dbg( p_dec, "available output chroma: %4.4s",
479 (char *)&mt.subtype.Data1 );
480 DMOFreeMediaType( &mt );
485 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
487 msg_Err( p_dec, "can't set DMO output type" );
490 msg_Dbg( p_dec, "DMO output type set" );
492 /* Allocate the memory needed to store the decoder's structure */
493 if( ( p_dec->p_sys = p_sys =
494 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
496 msg_Err( p_dec, "out of memory" );
500 p_sys->hmsdmo_dll = hmsdmo_dll;
501 p_sys->p_dmo = p_dmo;
503 p_sys->ldt_fs = ldt_fs;
506 /* Find out some properties of the output */
508 uint32_t i_size, i_align;
510 p_sys->i_min_output = 0;
511 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
513 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
518 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
520 p_sys->i_min_output = i_size;
521 p_sys->p_buffer = malloc( i_size );
522 if( !p_sys->p_buffer ) goto error;
526 /* Set output properties */
527 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
528 if( p_dec->fmt_out.i_cat == AUDIO_ES )
529 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
531 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
533 if( p_vih ) free( p_vih );
534 if( p_wf ) free( p_wf );
540 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
541 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
544 Restore_LDT_Keeper( ldt_fs );
546 /* Uninitialize OLE/COM */
550 if( p_vih ) free( p_vih );
551 if( p_wf ) free( p_wf );
552 if( p_sys ) free( p_sys );
557 /*****************************************************************************
558 * LoadDMO: Load the DMO object
559 *****************************************************************************/
560 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
561 IMediaObject **pp_dmo, es_format_t *p_fmt,
564 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
568 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
569 const DMO_PARTIAL_MEDIATYPE *,
570 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
573 IEnumDMO *p_enum_dmo = NULL;
580 IClassFactory *cFactory = NULL;
581 IUnknown *cObject = NULL;
582 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
585 /* Look for a DMO which can handle the requested codec */
586 if( p_fmt->i_cat == AUDIO_ES )
589 dmo_partial_type.type = MEDIATYPE_Audio;
590 dmo_partial_type.subtype = dmo_partial_type.type;
591 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
592 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
593 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
597 dmo_partial_type.type = MEDIATYPE_Video;
598 dmo_partial_type.subtype = dmo_partial_type.type;
599 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
604 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
605 if( *p_hmsdmo_dll == NULL )
607 msg_Dbg( p_this, "failed loading msdmo.dll" );
610 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
611 if( OurDMOEnum == NULL )
613 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
614 FreeLibrary( *p_hmsdmo_dll );
620 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
621 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
625 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
626 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
630 FreeLibrary( *p_hmsdmo_dll );
631 /* return VLC_EGENERIC; */
632 /* Try loading the dll directly */
636 /* Pickup the first available codec */
638 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
639 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
641 char psz_temp[MAX_PATH];
642 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
643 msg_Dbg( p_this, "found DMO: %s", psz_temp );
644 CoTaskMemFree( psz_dmo_name );
647 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
648 &IID_IMediaObject, (void **)pp_dmo ) )
650 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
656 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
660 FreeLibrary( *p_hmsdmo_dll );
661 /* return VLC_EGENERIC; */
662 /* Try loading the dll directly */
671 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
673 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
676 if( codecs_table[i_codec].i_fourcc == 0 )
677 return VLC_EGENERIC; /* Can't happen */
679 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
680 if( *p_hmsdmo_dll == NULL )
682 msg_Dbg( p_this, "failed loading '%s'",
683 codecs_table[i_codec].psz_dll );
687 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
690 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
691 FreeLibrary( *p_hmsdmo_dll );
695 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
697 if( i_err || cFactory == NULL )
699 msg_Dbg( p_this, "no such class object" );
700 FreeLibrary( *p_hmsdmo_dll );
704 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
706 cFactory->vt->Release( (IUnknown*)cFactory );
707 if( i_err || !cObject )
709 msg_Dbg( p_this, "class factory failure" );
710 FreeLibrary( *p_hmsdmo_dll );
713 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
715 cObject->vt->Release( (IUnknown*)cObject );
716 if( i_err || !*pp_dmo )
718 msg_Dbg( p_this, "QueryInterface failure" );
719 FreeLibrary( *p_hmsdmo_dll );
726 /*****************************************************************************
727 * DecoderClose: close codec
728 *****************************************************************************/
729 void DecoderClose( vlc_object_t *p_this )
731 decoder_t *p_dec = (decoder_t*)p_this;
732 decoder_sys_t *p_sys = p_dec->p_sys;
736 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
737 FreeLibrary( p_sys->hmsdmo_dll );
741 Restore_LDT_Keeper( p_sys->ldt_fs );
744 /* Uninitialize OLE/COM */
748 if( p_sys->p_buffer ) free( p_sys->p_buffer );
752 /****************************************************************************
753 * DecodeBlock: the whole thing
754 ****************************************************************************
755 * This function must be fed with ogg packets.
756 ****************************************************************************/
757 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
759 decoder_sys_t *p_sys = p_dec->p_sys;
763 DMO_OUTPUT_DATA_BUFFER db;
770 if( DecOpen( VLC_OBJECT(p_dec) ) )
772 msg_Err( p_dec, "DecOpen failed" );
775 p_sys = p_dec->p_sys;
778 if( !pp_block ) return NULL;
782 /* Won't work with streams with B-frames, but do we have any ? */
783 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
785 /* Date management */
786 if( p_block && p_block->i_pts > 0 &&
787 p_block->i_pts != date_Get( &p_sys->end_date ) )
789 date_Set( &p_sys->end_date, p_block->i_pts );
792 #if 0 /* Breaks the video decoding */
793 if( !date_Get( &p_sys->end_date ) )
795 /* We've just started the stream, wait for the first PTS. */
796 if( p_block ) block_Release( p_block );
801 /* Feed input to the DMO */
802 if( p_block && p_block->i_buffer )
806 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );
808 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
809 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
812 p_in->vt->Release( (IUnknown *)p_in );
814 if( i_result == S_FALSE )
816 /* No output generated */
818 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
822 else if( i_result == (int)DMO_E_NOTACCEPTING )
824 /* Need to call ProcessOutput */
825 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
827 else if( i_result != S_OK )
829 msg_Dbg( p_dec, "ProcessInput(): failed" );
834 //msg_Dbg( p_dec, "ProcessInput(): successful" );
838 else if( p_block && !p_block->i_buffer )
840 block_Release( p_block );
844 /* Get output from the DMO */
845 block_out.p_buffer = p_sys->p_buffer;
846 block_out.i_buffer = 0;
848 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );
849 memset( &db, 0, sizeof(db) );
850 db.pBuffer = (IMediaBuffer *)p_out;
852 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
853 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
856 if( i_result != S_OK )
858 if( i_result != S_FALSE )
859 msg_Dbg( p_dec, "ProcessOutput(): failed" );
862 msg_Dbg( p_dec, "ProcessOutput(): no output" );
865 p_out->vt->Release( (IUnknown *)p_out );
870 msg_Dbg( p_dec, "ProcessOutput(): success" );
873 if( !block_out.i_buffer )
876 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
878 p_out->vt->Release( (IUnknown *)p_out );
882 if( p_dec->fmt_out.i_cat == VIDEO_ES )
884 /* Get a new picture */
885 picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
886 if( !p_pic ) return NULL;
888 CopyPicture( p_dec, p_pic, block_out.p_buffer );
890 /* Date management */
891 p_pic->date = date_Get( &p_sys->end_date );
892 date_Increment( &p_sys->end_date, 1 );
894 p_out->vt->Release( (IUnknown *)p_out );
900 aout_buffer_t *p_aout_buffer;
901 int i_samples = block_out.i_buffer /
902 ( p_dec->fmt_out.audio.i_bitspersample *
903 p_dec->fmt_out.audio.i_channels / 8 );
905 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
906 memcpy( p_aout_buffer->p_buffer,
907 block_out.p_buffer, block_out.i_buffer );
909 /* Date management */
910 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
911 p_aout_buffer->end_date =
912 date_Increment( &p_sys->end_date, i_samples );
914 p_out->vt->Release( (IUnknown *)p_out );
916 return p_aout_buffer;
922 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )
924 int i_plane, i_line, i_width, i_dst_stride;
925 uint8_t *p_dst, *p_src = p_in;
927 p_dst = p_pic->p[1].p_pixels;
928 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
929 p_pic->p[2].p_pixels = p_dst;
931 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
933 p_dst = p_pic->p[i_plane].p_pixels;
934 i_width = p_pic->p[i_plane].i_visible_pitch;
935 i_dst_stride = p_pic->p[i_plane].i_pitch;
937 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
939 p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
941 p_dst += i_dst_stride;
945 p_dst = p_pic->p[1].p_pixels;
946 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
947 p_pic->p[2].p_pixels = p_dst;
950 /****************************************************************************
951 * Encoder descriptor declaration
952 ****************************************************************************/
955 HINSTANCE hmsdmo_dll;
967 /*****************************************************************************
968 * EncoderOpen: open dmo codec
969 *****************************************************************************/
970 static int EncoderOpen( vlc_object_t *p_this )
972 encoder_t *p_enc = (encoder_t*)p_this;
974 int i_ret = EncOpen( p_this );
975 if( i_ret != VLC_SUCCESS ) return i_ret;
978 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
980 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
986 /*****************************************************************************
987 * EncoderSetVideoType: configures the input and output types of the dmo
988 *****************************************************************************/
989 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
991 int i, i_selected, i_err;
992 DMO_MEDIA_TYPE dmo_type;
993 VIDEOINFOHEADER vih, *p_vih;
994 BITMAPINFOHEADER *p_bih;
997 p_enc->fmt_in.video.i_bits_per_pixel =
998 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1000 /* Enumerate input format (for debug output) */
1002 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1004 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1006 msg_Dbg( p_enc, "available input chroma: %4.4s",
1007 (char *)&dmo_type.subtype.Data1 );
1008 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1009 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1010 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1011 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1013 DMOFreeMediaType( &dmo_type );
1016 /* Setup input format */
1017 memset( &dmo_type, 0, sizeof(dmo_type) );
1018 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1020 p_bih = &vih.bmiHeader;
1021 p_bih->biCompression = VLC_FOURCC('I','4','2','0');
1022 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1023 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1024 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1025 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1026 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1027 p_bih->biPlanes = 3;
1028 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1030 vih.rcSource.left = vih.rcSource.top = 0;
1031 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1032 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1033 vih.rcTarget = vih.rcSource;
1035 vih.AvgTimePerFrame = I64C(10000000) / 25; //FIXME
1037 dmo_type.majortype = MEDIATYPE_Video;
1038 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1039 dmo_type.subtype = MEDIASUBTYPE_I420;
1040 //dmo_type.subtype.Data1 = p_bih->biCompression;
1041 dmo_type.formattype = FORMAT_VideoInfo;
1042 dmo_type.bFixedSizeSamples = TRUE;
1043 dmo_type.bTemporalCompression = FALSE;
1044 dmo_type.lSampleSize = p_bih->biSizeImage;
1045 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1046 dmo_type.pbFormat = (char *)&vih;
1048 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1050 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1051 return VLC_EGENERIC;
1054 msg_Dbg( p_enc, "successfully set input type" );
1056 /* Setup output format */
1057 memset( &dmo_type, 0, sizeof(dmo_type) );
1060 /* Enumerate output types */
1061 i = 0, i_selected = -1;
1062 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1064 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1066 msg_Dbg( p_enc, "available output codec: %4.4s",
1067 (char *)&dmo_type.subtype.Data1 );
1069 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1072 DMOFreeMediaType( &dmo_type );
1075 if( i_selected < 0 )
1077 msg_Err( p_enc, "couldn't find codec: %4.4s",
1078 (char *)&p_enc->fmt_out.i_codec );
1079 return VLC_EGENERIC;
1082 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1083 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1084 p_enc->fmt_out.i_bitrate;
1086 /* Get the private data for the codec */
1089 IWMCodecPrivateData *p_privdata;
1090 VIDEOINFOHEADER *p_vih;
1091 uint8_t *p_data = 0;
1092 uint32_t i_data = 0, i_vih;
1094 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1095 &IID_IWMCodecPrivateData,
1096 (void **)&p_privdata );
1099 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1102 msg_Err( p_enc, "SetPartialOutputType() failed" );
1103 p_privdata->vt->Release( (IUnknown *)p_privdata );
1107 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1110 msg_Err( p_enc, "GetPrivateData() failed" );
1111 p_privdata->vt->Release( (IUnknown *)p_privdata );
1115 p_data = malloc( i_data );
1116 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1118 /* Update the media type with the private data */
1119 i_vih = dmo_type.cbFormat + i_data;
1120 p_vih = CoTaskMemAlloc( i_vih );
1121 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1122 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1123 DMOFreeMediaType( &dmo_type );
1124 dmo_type.pbFormat = (char*)p_vih;
1125 dmo_type.cbFormat = i_vih;
1127 msg_Dbg( p_enc, "found extra data: %i", i_data );
1128 p_enc->fmt_out.i_extra = i_data;
1129 p_enc->fmt_out.p_extra = p_data;
1133 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1135 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1136 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
1138 DMOFreeMediaType( &dmo_type );
1141 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1142 return VLC_EGENERIC;
1145 msg_Dbg( p_enc, "successfully set output type" );
1149 /*****************************************************************************
1150 * EncoderSetAudioType: configures the input and output types of the dmo
1151 *****************************************************************************/
1152 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1154 int i, i_selected, i_err;
1155 unsigned int i_last_byterate;
1157 DMO_MEDIA_TYPE dmo_type;
1160 /* Setup the format structure */
1161 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1162 if( i_tag == 0 ) return VLC_EGENERIC;
1164 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1165 p_enc->fmt_in.audio.i_bitspersample = 16;
1167 /* We first need to choose an output type from the predefined
1168 * list of choices (we cycle through the list to select the best match) */
1169 i = 0; i_selected = -1; i_last_byterate = 0;
1170 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1172 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1173 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1174 "bits per sample: %i, bitrate: %i, blockalign: %i",
1175 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1176 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1177 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1179 if( p_wf->wFormatTag == i_tag &&
1180 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1181 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1182 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1184 if( (int)p_wf->nAvgBytesPerSec <
1185 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1186 p_wf->nAvgBytesPerSec > i_last_byterate )
1189 i_last_byterate = p_wf->nAvgBytesPerSec;
1190 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1191 i_selected, p_wf->nAvgBytesPerSec * 8 );
1195 DMOFreeMediaType( &dmo_type );
1198 if( i_selected < 0 )
1200 msg_Err( p_enc, "couldn't find a matching output" );
1201 return VLC_EGENERIC;
1204 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1205 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1207 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1208 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1209 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1210 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1211 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1213 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1214 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1215 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1216 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1217 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1221 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1222 p_enc->fmt_out.i_extra = p_wf->cbSize;
1223 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1224 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1227 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1228 DMOFreeMediaType( &dmo_type );
1232 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1233 return VLC_EGENERIC;
1236 msg_Dbg( p_enc, "successfully set output type" );
1238 /* Setup the input type */
1239 i = 0; i_selected = -1;
1240 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1242 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1243 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1244 "bits per sample: %i, bitrate: %i, blockalign: %i",
1245 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1246 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1247 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1249 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1250 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1251 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1252 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1257 DMOFreeMediaType( &dmo_type );
1260 if( i_selected < 0 )
1262 msg_Err( p_enc, "couldn't find a matching input" );
1263 return VLC_EGENERIC;
1266 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1267 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1268 DMOFreeMediaType( &dmo_type );
1271 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1272 return VLC_EGENERIC;
1274 msg_Dbg( p_enc, "successfully set input type" );
1279 /*****************************************************************************
1280 * EncOpen: open dmo codec
1281 *****************************************************************************/
1282 static int EncOpen( vlc_object_t *p_this )
1284 encoder_t *p_enc = (encoder_t*)p_this;
1285 encoder_sys_t *p_sys = NULL;
1286 IMediaObject *p_dmo = NULL;
1287 HINSTANCE hmsdmo_dll = NULL;
1290 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1292 /* Initialize OLE/COM */
1296 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, VLC_TRUE )
1304 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1306 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1310 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1313 /* Allocate the memory needed to store the decoder's structure */
1314 if( ( p_enc->p_sys = p_sys =
1315 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1317 msg_Err( p_enc, "out of memory" );
1321 p_sys->hmsdmo_dll = hmsdmo_dll;
1322 p_sys->p_dmo = p_dmo;
1324 p_sys->ldt_fs = ldt_fs;
1327 /* Find out some properties of the inputput */
1329 uint32_t i_size, i_align, dum;
1331 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1332 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1334 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1335 i_size, i_align, dum );
1338 /* Find out some properties of the output */
1340 uint32_t i_size, i_align;
1342 p_sys->i_min_output = 0;
1343 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1345 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1350 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1352 p_sys->i_min_output = i_size;
1356 /* Set output properties */
1357 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1358 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1359 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1361 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1367 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1368 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1371 Restore_LDT_Keeper( ldt_fs );
1373 /* Uninitialize OLE/COM */
1377 if( p_sys ) free( p_sys );
1379 return VLC_EGENERIC;
1382 /****************************************************************************
1383 * Encode: the whole thing
1384 ****************************************************************************/
1385 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1387 encoder_sys_t *p_sys = p_enc->p_sys;
1389 block_t *p_chain = NULL;
1390 block_t *p_block_in;
1395 if( !p_data ) return NULL;
1397 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1399 /* Get picture data */
1400 int i_plane, i_line, i_width, i_src_stride;
1401 picture_t *p_pic = (picture_t *)p_data;
1404 int i_buffer = p_enc->fmt_in.video.i_width *
1405 p_enc->fmt_in.video.i_height *
1406 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1408 p_block_in = block_New( p_enc, i_buffer );
1410 /* Copy picture stride by stride */
1411 p_dst = p_block_in->p_buffer;
1412 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1414 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1415 i_width = p_pic->p[i_plane].i_visible_pitch;
1416 i_src_stride = p_pic->p[i_plane].i_pitch;
1418 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1421 p_enc->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
1423 p_src += i_src_stride;
1427 i_pts = p_pic->date;
1431 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1432 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1433 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1434 p_block_in->i_buffer );
1436 i_pts = p_aout_buffer->start_date;
1439 /* Feed input to the DMO */
1440 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, VLC_TRUE );
1441 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1442 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1444 p_in->vt->Release( (IUnknown *)p_in );
1445 if( i_result == S_FALSE )
1447 /* No output generated */
1449 msg_Dbg( p_enc, "ProcessInput(): no output generated "I64Fd, i_pts );
1453 else if( i_result == (int)DMO_E_NOTACCEPTING )
1455 /* Need to call ProcessOutput */
1456 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1458 else if( i_result != S_OK )
1460 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1465 msg_Dbg( p_enc, "ProcessInput(): success" );
1468 /* Get output from the DMO */
1471 DMO_OUTPUT_DATA_BUFFER db;
1472 block_t *p_block_out;
1473 CMediaBuffer *p_out;
1475 p_block_out = block_New( p_enc, p_sys->i_min_output );
1476 p_block_out->i_buffer = 0;
1477 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, VLC_FALSE);
1478 memset( &db, 0, sizeof(db) );
1479 db.pBuffer = (IMediaBuffer *)p_out;
1481 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1482 0, 1, &db, &i_status );
1484 if( i_result != S_OK )
1486 if( i_result != S_FALSE )
1487 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1490 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1493 p_out->vt->Release( (IUnknown *)p_out );
1494 block_Release( p_block_out );
1498 if( !p_block_out->i_buffer )
1501 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1503 p_out->vt->Release( (IUnknown *)p_out );
1504 block_Release( p_block_out );
1508 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1511 msg_Dbg( p_enc, "ProcessOutput(): pts: "I64Fd", "I64Fd,
1512 i_pts, db.rtTimestamp / 10 );
1514 i_pts = db.rtTimestamp / 10;
1517 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1519 p_block_out->i_length = db.rtTimelength / 10;
1521 msg_Dbg( p_enc, "ProcessOutput(): length: "I64Fd,
1522 p_block_out->i_length );
1526 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1528 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1529 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1531 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1534 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1535 block_ChainAppend( &p_chain, p_block_out );
1539 /*****************************************************************************
1540 * EncoderClose: close codec
1541 *****************************************************************************/
1542 void EncoderClose( vlc_object_t *p_this )
1544 encoder_t *p_enc = (encoder_t*)p_this;
1545 encoder_sys_t *p_sys = p_enc->p_sys;
1547 if( !p_sys ) return;
1549 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1550 FreeLibrary( p_sys->hmsdmo_dll );
1554 Restore_LDT_Keeper( p_sys->ldt_fs );
1557 /* Uninitialize OLE/COM */