1 /*****************************************************************************
2 * dmo.c : DirectMedia Object decoder module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 the VideoLAN team
7 * Author: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_codec.h>
43 /* Need the w32dll loader from mplayer */
44 # include <wine/winerror.h>
45 # include <ldt_keeper.h>
46 # include <wine/windef.h>
49 #include <vlc_codecs.h>
56 long CoInitialize( void *pvReserved ) { return -1; }
57 void CoUninitialize( void ) { }
59 /* A few prototypes */
60 HMODULE WINAPI LoadLibraryA(LPCSTR);
61 #define LoadLibrary LoadLibraryA
62 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
63 int WINAPI FreeLibrary(HMODULE);
66 typedef long (STDCALL *GETCLASS) ( const GUID*, const GUID*, void** );
68 static int pi_channels_maps[7] =
72 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
73 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
74 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
75 | AOUT_CHAN_REARRIGHT,
76 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
77 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
78 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
79 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
82 /*****************************************************************************
84 *****************************************************************************/
85 static int DecoderOpen ( vlc_object_t * );
86 static int DecOpen ( vlc_object_t * );
87 static void DecoderClose ( vlc_object_t * );
88 static void *DecodeBlock ( decoder_t *, block_t ** );
90 static int EncoderOpen ( vlc_object_t * );
91 static int EncOpen ( vlc_object_t * );
92 static void EncoderClose ( vlc_object_t * );
93 static block_t *EncodeBlock( encoder_t *, void * );
95 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
96 es_format_t *, vlc_bool_t );
97 static void CopyPicture( decoder_t *, picture_t *, uint8_t * );
100 set_description( _("DirectMedia Object decoder") );
101 add_shortcut( "dmo" );
102 set_capability( "decoder", 1 );
103 set_callbacks( DecoderOpen, DecoderClose );
104 set_category( CAT_INPUT );
105 set_subcategory( SUBCAT_INPUT_SCODEC );
107 # define ENC_CFG_PREFIX "sout-dmo-"
109 set_description( _("DirectMedia Object encoder") );
110 set_capability( "encoder", 10 );
111 set_callbacks( EncoderOpen, EncoderClose );
115 /*****************************************************************************
117 *****************************************************************************/
119 /****************************************************************************
120 * Decoder descriptor declaration
121 ****************************************************************************/
124 HINSTANCE hmsdmo_dll;
137 static const GUID guid_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
138 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
140 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
141 static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
142 static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };
144 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
145 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
147 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
148 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
152 vlc_fourcc_t i_fourcc;
158 static const codec_dll decoders_table[] =
161 { VLC_FOURCC('W','V','C','1'), "wvc1dmod.dll", &guid_wvc1 },
162 { VLC_FOURCC('w','v','c','1'), "wvc1dmod.dll", &guid_wvc1 },
164 { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
165 { VLC_FOURCC('W','M','V','P'), "wmv9dmod.dll", &guid_wmv9 },
166 { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
168 { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
169 { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
171 { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
172 { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
174 { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
175 { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
176 /* Windows Media Video Adv */
177 { VLC_FOURCC('W','M','V','A'), "wmvadvd.dll", &guid_wmva },
178 { VLC_FOURCC('W','V','P','2'), "wmvadvd.dll", &guid_wmva },
181 { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
182 { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
183 { VLC_FOURCC('w','m','a','p'), "wma9dmod.dll", &guid_wma9 },
185 { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
186 { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
189 { VLC_FOURCC('w','m','a','s'), "wmspdmod.dll", &guid_wma },
195 static const codec_dll encoders_table[] =
198 { VLC_FOURCC('W','M','V','2'), "wmvdmoe.dll", &guid_wmv_enc },
199 { VLC_FOURCC('w','m','v','2'), "wmvdmoe.dll", &guid_wmv_enc },
201 { VLC_FOURCC('W','M','V','1'), "wmvdmoe.dll", &guid_wmv_enc },
202 { VLC_FOURCC('w','m','v','1'), "wmvdmoe.dll", &guid_wmv_enc },
205 { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
206 { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
208 { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
209 { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
215 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
217 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
218 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
224 /*****************************************************************************
225 * DecoderOpen: open dmo codec
226 *****************************************************************************/
227 static int DecoderOpen( vlc_object_t *p_this )
229 decoder_t *p_dec = (decoder_t*)p_this;
232 int i_ret = DecOpen( p_this );
233 if( i_ret != VLC_SUCCESS ) return i_ret;
237 /* We can't open it now, because of ldt_keeper or something
238 * Open/Decode/Close has to be done in the same thread */
243 /* Probe if we support it */
244 for( i = 0; decoders_table[i].i_fourcc != 0; i++ )
246 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
248 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
249 (char*)&p_dec->fmt_in.i_codec,
250 decoders_table[i].psz_dll );
256 if( !decoders_table[i].i_fourcc ) return VLC_EGENERIC;
260 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
262 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
268 /*****************************************************************************
269 * DecOpen: open dmo codec
270 *****************************************************************************/
271 static int DecOpen( vlc_object_t *p_this )
273 decoder_t *p_dec = (decoder_t*)p_this;
274 decoder_sys_t *p_sys = NULL;
276 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
277 IMediaObject *p_dmo = NULL;
278 HINSTANCE hmsdmo_dll = NULL;
280 VIDEOINFOHEADER *p_vih = NULL;
281 WAVEFORMATEX *p_wf = NULL;
284 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
286 /* Initialize OLE/COM */
290 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, VLC_FALSE )
298 /* Setup input format */
299 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
300 dmo_input_type.pUnk = 0;
302 if( p_dec->fmt_in.i_cat == AUDIO_ES )
305 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
306 p_wf = malloc( i_size );
308 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
309 if( p_dec->fmt_in.i_extra )
310 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
312 dmo_input_type.majortype = MEDIATYPE_Audio;
313 dmo_input_type.subtype = dmo_input_type.majortype;
314 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
315 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
316 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
318 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
319 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
320 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
321 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
322 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
323 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
324 p_wf->cbSize = p_dec->fmt_in.i_extra;
326 dmo_input_type.formattype = FORMAT_WaveFormatEx;
327 dmo_input_type.cbFormat = i_size;
328 dmo_input_type.pbFormat = (char *)p_wf;
329 dmo_input_type.bFixedSizeSamples = 1;
330 dmo_input_type.bTemporalCompression = 0;
331 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
335 BITMAPINFOHEADER *p_bih;
337 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
338 p_vih = malloc( i_size );
340 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
341 if( p_dec->fmt_in.i_extra )
342 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
344 p_bih = &p_vih->bmiHeader;
345 p_bih->biCompression = p_dec->fmt_in.i_codec;
346 p_bih->biWidth = p_dec->fmt_in.video.i_width;
347 p_bih->biHeight = p_dec->fmt_in.video.i_height;
348 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
350 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
351 sizeof(BITMAPINFOHEADER);
353 p_vih->rcSource.left = p_vih->rcSource.top = 0;
354 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
355 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
356 p_vih->rcTarget = p_vih->rcSource;
358 dmo_input_type.majortype = MEDIATYPE_Video;
359 dmo_input_type.subtype = dmo_input_type.majortype;
360 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
361 dmo_input_type.formattype = FORMAT_VideoInfo;
362 dmo_input_type.bFixedSizeSamples = 0;
363 dmo_input_type.bTemporalCompression = 1;
364 dmo_input_type.cbFormat = i_size;
365 dmo_input_type.pbFormat = (char *)p_vih;
368 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
370 msg_Err( p_dec, "can't set DMO input type" );
373 msg_Dbg( p_dec, "DMO input type set" );
375 /* Setup output format */
376 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
377 dmo_output_type.pUnk = 0;
379 if( p_dec->fmt_in.i_cat == AUDIO_ES )
381 /* Setup the format */
382 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
383 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
384 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
385 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
386 p_dec->fmt_out.audio.i_physical_channels =
387 p_dec->fmt_out.audio.i_original_channels =
388 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
390 p_wf->wFormatTag = WAVE_FORMAT_PCM;
391 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
392 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
393 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
395 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
396 p_wf->nAvgBytesPerSec =
397 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
400 dmo_output_type.majortype = MEDIATYPE_Audio;
401 dmo_output_type.formattype = FORMAT_WaveFormatEx;
402 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
403 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
404 dmo_output_type.pbFormat = (char *)p_wf;
405 dmo_output_type.bFixedSizeSamples = 1;
406 dmo_output_type.bTemporalCompression = 0;
407 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
411 BITMAPINFOHEADER *p_bih;
413 unsigned i_chroma = VLC_FOURCC('Y','U','Y','2');
414 int i_planes = 1, i_bpp = 16;
417 /* Find out which chroma to use */
418 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
420 if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
422 i_chroma = mt.subtype.Data1;
423 i_planes = 3; i_bpp = 12;
426 DMOFreeMediaType( &mt );
429 p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
430 VLC_FOURCC('I','4','2','0') : i_chroma;
431 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
432 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
433 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
435 /* If an aspect-ratio was specified in the input format then force it */
436 if( p_dec->fmt_in.video.i_aspect )
438 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
442 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
443 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
446 p_bih = &p_vih->bmiHeader;
447 p_bih->biCompression = i_chroma;
448 p_bih->biHeight *= -1;
449 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
450 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
451 p_dec->fmt_in.video.i_height *
452 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
454 p_bih->biPlanes = i_planes;
455 p_bih->biSize = sizeof(BITMAPINFOHEADER);
457 dmo_output_type.majortype = MEDIATYPE_Video;
458 dmo_output_type.formattype = FORMAT_VideoInfo;
459 dmo_output_type.subtype = dmo_output_type.majortype;
460 dmo_output_type.subtype.Data1 = p_bih->biCompression;
461 dmo_output_type.bFixedSizeSamples = VLC_TRUE;
462 dmo_output_type.bTemporalCompression = 0;
463 dmo_output_type.lSampleSize = p_bih->biSizeImage;
464 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
465 dmo_output_type.pbFormat = (char *)p_vih;
469 /* Enumerate output types */
470 if( p_dec->fmt_in.i_cat == VIDEO_ES )
475 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
477 msg_Dbg( p_dec, "available output chroma: %4.4s",
478 (char *)&mt.subtype.Data1 );
479 DMOFreeMediaType( &mt );
484 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
486 msg_Err( p_dec, "can't set DMO output type" );
489 msg_Dbg( p_dec, "DMO output type set" );
491 /* Allocate the memory needed to store the decoder's structure */
492 if( ( p_dec->p_sys = p_sys =
493 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
495 msg_Err( p_dec, "out of memory" );
499 p_sys->hmsdmo_dll = hmsdmo_dll;
500 p_sys->p_dmo = p_dmo;
502 p_sys->ldt_fs = ldt_fs;
505 /* Find out some properties of the output */
507 uint32_t i_size, i_align;
509 p_sys->i_min_output = 0;
510 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
512 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
517 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
519 p_sys->i_min_output = i_size;
520 p_sys->p_buffer = malloc( i_size );
521 if( !p_sys->p_buffer ) goto error;
525 /* Set output properties */
526 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
527 if( p_dec->fmt_out.i_cat == AUDIO_ES )
528 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
530 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
532 if( p_vih ) free( p_vih );
533 if( p_wf ) free( p_wf );
539 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
540 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
543 Restore_LDT_Keeper( ldt_fs );
545 /* Uninitialize OLE/COM */
549 if( p_vih ) free( p_vih );
550 if( p_wf ) free( p_wf );
551 if( p_sys ) free( p_sys );
556 /*****************************************************************************
557 * LoadDMO: Load the DMO object
558 *****************************************************************************/
559 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
560 IMediaObject **pp_dmo, es_format_t *p_fmt,
563 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
567 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
568 const DMO_PARTIAL_MEDIATYPE *,
569 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
572 IEnumDMO *p_enum_dmo = NULL;
579 IClassFactory *cFactory = NULL;
580 IUnknown *cObject = NULL;
581 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
584 /* Look for a DMO which can handle the requested codec */
585 if( p_fmt->i_cat == AUDIO_ES )
588 dmo_partial_type.type = MEDIATYPE_Audio;
589 dmo_partial_type.subtype = dmo_partial_type.type;
590 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
591 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
592 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
596 dmo_partial_type.type = MEDIATYPE_Video;
597 dmo_partial_type.subtype = dmo_partial_type.type;
598 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
603 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
604 if( *p_hmsdmo_dll == NULL )
606 msg_Dbg( p_this, "failed loading msdmo.dll" );
609 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
610 if( OurDMOEnum == NULL )
612 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
613 FreeLibrary( *p_hmsdmo_dll );
619 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
620 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
624 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
625 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
629 FreeLibrary( *p_hmsdmo_dll );
630 /* return VLC_EGENERIC; */
631 /* Try loading the dll directly */
635 /* Pickup the first available codec */
637 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
638 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
640 char psz_temp[MAX_PATH];
641 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
642 msg_Dbg( p_this, "found DMO: %s", psz_temp );
643 CoTaskMemFree( psz_dmo_name );
646 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
647 &IID_IMediaObject, (void **)pp_dmo ) )
649 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
655 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
659 FreeLibrary( *p_hmsdmo_dll );
660 /* return VLC_EGENERIC; */
661 /* Try loading the dll directly */
670 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
672 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
675 if( codecs_table[i_codec].i_fourcc == 0 )
676 return VLC_EGENERIC; /* Can't happen */
678 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
679 if( *p_hmsdmo_dll == NULL )
681 msg_Dbg( p_this, "failed loading '%s'",
682 codecs_table[i_codec].psz_dll );
686 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
689 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
690 FreeLibrary( *p_hmsdmo_dll );
694 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
696 if( i_err || cFactory == NULL )
698 msg_Dbg( p_this, "no such class object" );
699 FreeLibrary( *p_hmsdmo_dll );
703 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
705 cFactory->vt->Release( (IUnknown*)cFactory );
706 if( i_err || !cObject )
708 msg_Dbg( p_this, "class factory failure" );
709 FreeLibrary( *p_hmsdmo_dll );
712 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
714 cObject->vt->Release( (IUnknown*)cObject );
715 if( i_err || !*pp_dmo )
717 msg_Dbg( p_this, "QueryInterface failure" );
718 FreeLibrary( *p_hmsdmo_dll );
725 /*****************************************************************************
726 * DecoderClose: close codec
727 *****************************************************************************/
728 void DecoderClose( vlc_object_t *p_this )
730 decoder_t *p_dec = (decoder_t*)p_this;
731 decoder_sys_t *p_sys = p_dec->p_sys;
735 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
736 FreeLibrary( p_sys->hmsdmo_dll );
740 Restore_LDT_Keeper( p_sys->ldt_fs );
743 /* Uninitialize OLE/COM */
747 if( p_sys->p_buffer ) free( p_sys->p_buffer );
751 /****************************************************************************
752 * DecodeBlock: the whole thing
753 ****************************************************************************
754 * This function must be fed with ogg packets.
755 ****************************************************************************/
756 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
758 decoder_sys_t *p_sys = p_dec->p_sys;
762 DMO_OUTPUT_DATA_BUFFER db;
769 if( DecOpen( VLC_OBJECT(p_dec) ) )
771 msg_Err( p_dec, "DecOpen failed" );
774 p_sys = p_dec->p_sys;
777 if( !pp_block ) return NULL;
781 /* Won't work with streams with B-frames, but do we have any ? */
782 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
784 /* Date management */
785 if( p_block && p_block->i_pts > 0 &&
786 p_block->i_pts != date_Get( &p_sys->end_date ) )
788 date_Set( &p_sys->end_date, p_block->i_pts );
791 #if 0 /* Breaks the video decoding */
792 if( !date_Get( &p_sys->end_date ) )
794 /* We've just started the stream, wait for the first PTS. */
795 if( p_block ) block_Release( p_block );
800 /* Feed input to the DMO */
801 if( p_block && p_block->i_buffer )
805 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );
807 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
808 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
811 p_in->vt->Release( (IUnknown *)p_in );
813 if( i_result == S_FALSE )
815 /* No output generated */
817 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
821 else if( i_result == (int)DMO_E_NOTACCEPTING )
823 /* Need to call ProcessOutput */
824 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
826 else if( i_result != S_OK )
828 msg_Dbg( p_dec, "ProcessInput(): failed" );
833 //msg_Dbg( p_dec, "ProcessInput(): successful" );
837 else if( p_block && !p_block->i_buffer )
839 block_Release( p_block );
843 /* Get output from the DMO */
844 block_out.p_buffer = p_sys->p_buffer;
845 block_out.i_buffer = 0;
847 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );
848 memset( &db, 0, sizeof(db) );
849 db.pBuffer = (IMediaBuffer *)p_out;
851 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
852 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
855 if( i_result != S_OK )
857 if( i_result != S_FALSE )
858 msg_Dbg( p_dec, "ProcessOutput(): failed" );
861 msg_Dbg( p_dec, "ProcessOutput(): no output" );
864 p_out->vt->Release( (IUnknown *)p_out );
869 msg_Dbg( p_dec, "ProcessOutput(): success" );
872 if( !block_out.i_buffer )
875 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
877 p_out->vt->Release( (IUnknown *)p_out );
881 if( p_dec->fmt_out.i_cat == VIDEO_ES )
883 /* Get a new picture */
884 picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
885 if( !p_pic ) return NULL;
887 CopyPicture( p_dec, p_pic, block_out.p_buffer );
889 /* Date management */
890 p_pic->date = date_Get( &p_sys->end_date );
891 date_Increment( &p_sys->end_date, 1 );
893 p_out->vt->Release( (IUnknown *)p_out );
899 aout_buffer_t *p_aout_buffer;
900 int i_samples = block_out.i_buffer /
901 ( p_dec->fmt_out.audio.i_bitspersample *
902 p_dec->fmt_out.audio.i_channels / 8 );
904 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
905 memcpy( p_aout_buffer->p_buffer,
906 block_out.p_buffer, block_out.i_buffer );
908 /* Date management */
909 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
910 p_aout_buffer->end_date =
911 date_Increment( &p_sys->end_date, i_samples );
913 p_out->vt->Release( (IUnknown *)p_out );
915 return p_aout_buffer;
921 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )
923 int i_plane, i_line, i_width, i_dst_stride;
924 uint8_t *p_dst, *p_src = p_in;
926 p_dst = p_pic->p[1].p_pixels;
927 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
928 p_pic->p[2].p_pixels = p_dst;
930 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
932 p_dst = p_pic->p[i_plane].p_pixels;
933 i_width = p_pic->p[i_plane].i_visible_pitch;
934 i_dst_stride = p_pic->p[i_plane].i_pitch;
936 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
938 p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
940 p_dst += i_dst_stride;
944 p_dst = p_pic->p[1].p_pixels;
945 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
946 p_pic->p[2].p_pixels = p_dst;
949 /****************************************************************************
950 * Encoder descriptor declaration
951 ****************************************************************************/
954 HINSTANCE hmsdmo_dll;
966 /*****************************************************************************
967 * EncoderOpen: open dmo codec
968 *****************************************************************************/
969 static int EncoderOpen( vlc_object_t *p_this )
971 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 /* We can't open it now, because of ldt_keeper or something
979 * Open/Encode/Close has to be done in the same thread */
982 /* Probe if we support it */
983 for( i = 0; encoders_table[i].i_fourcc != 0; i++ )
985 if( encoders_table[i].i_fourcc == p_enc->fmt_out.i_codec )
987 msg_Dbg( p_enc, "DMO codec for %4.4s may work with dll=%s",
988 (char*)&p_enc->fmt_out.i_codec,
989 encoders_table[i].psz_dll );
995 if( !encoders_table[i].i_fourcc ) return VLC_EGENERIC;
999 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1001 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
1007 /*****************************************************************************
1008 * EncoderSetVideoType: configures the input and output types of the dmo
1009 *****************************************************************************/
1010 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1012 int i, i_selected, i_err;
1013 DMO_MEDIA_TYPE dmo_type;
1014 VIDEOINFOHEADER vih, *p_vih;
1015 BITMAPINFOHEADER *p_bih;
1018 p_enc->fmt_in.video.i_bits_per_pixel =
1019 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1021 /* Enumerate input format (for debug output) */
1023 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1025 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1027 msg_Dbg( p_enc, "available input chroma: %4.4s",
1028 (char *)&dmo_type.subtype.Data1 );
1029 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1030 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1031 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1032 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1034 DMOFreeMediaType( &dmo_type );
1037 /* Setup input format */
1038 memset( &dmo_type, 0, sizeof(dmo_type) );
1040 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1042 p_bih = &vih.bmiHeader;
1043 p_bih->biCompression = VLC_FOURCC('I','4','2','0');
1044 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1045 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1046 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1047 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1048 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1049 p_bih->biPlanes = 3;
1050 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1052 vih.rcSource.left = vih.rcSource.top = 0;
1053 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1054 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1055 vih.rcTarget = vih.rcSource;
1057 vih.AvgTimePerFrame = I64C(10000000) / 25; //FIXME
1059 dmo_type.majortype = MEDIATYPE_Video;
1060 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1061 dmo_type.subtype = MEDIASUBTYPE_I420;
1062 //dmo_type.subtype.Data1 = p_bih->biCompression;
1063 dmo_type.formattype = FORMAT_VideoInfo;
1064 dmo_type.bFixedSizeSamples = TRUE;
1065 dmo_type.bTemporalCompression = FALSE;
1066 dmo_type.lSampleSize = p_bih->biSizeImage;
1067 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1068 dmo_type.pbFormat = (char *)&vih;
1070 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1072 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1073 return VLC_EGENERIC;
1076 msg_Dbg( p_enc, "successfully set input type" );
1078 /* Setup output format */
1079 memset( &dmo_type, 0, sizeof(dmo_type) );
1082 /* Enumerate output types */
1083 i = 0, i_selected = -1;
1084 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1086 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1088 msg_Dbg( p_enc, "available output codec: %4.4s",
1089 (char *)&dmo_type.subtype.Data1 );
1091 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1094 DMOFreeMediaType( &dmo_type );
1097 if( i_selected < 0 )
1099 msg_Err( p_enc, "couldn't find codec: %4.4s",
1100 (char *)&p_enc->fmt_out.i_codec );
1101 return VLC_EGENERIC;
1104 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1105 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1106 p_enc->fmt_out.i_bitrate;
1108 /* Get the private data for the codec */
1111 IWMCodecPrivateData *p_privdata;
1112 VIDEOINFOHEADER *p_vih;
1113 uint8_t *p_data = 0;
1114 uint32_t i_data = 0, i_vih;
1116 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1117 &IID_IWMCodecPrivateData,
1118 (void **)&p_privdata );
1121 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1124 msg_Err( p_enc, "SetPartialOutputType() failed" );
1125 p_privdata->vt->Release( (IUnknown *)p_privdata );
1129 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1132 msg_Err( p_enc, "GetPrivateData() failed" );
1133 p_privdata->vt->Release( (IUnknown *)p_privdata );
1137 p_data = malloc( i_data );
1138 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1140 /* Update the media type with the private data */
1141 i_vih = dmo_type.cbFormat + i_data;
1142 p_vih = CoTaskMemAlloc( i_vih );
1143 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1144 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1145 DMOFreeMediaType( &dmo_type );
1146 dmo_type.pbFormat = (char*)p_vih;
1147 dmo_type.cbFormat = i_vih;
1149 msg_Dbg( p_enc, "found extra data: %i", i_data );
1150 p_enc->fmt_out.i_extra = i_data;
1151 p_enc->fmt_out.p_extra = p_data;
1155 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1157 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1158 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
1160 DMOFreeMediaType( &dmo_type );
1163 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1164 return VLC_EGENERIC;
1167 msg_Dbg( p_enc, "successfully set output type" );
1171 /*****************************************************************************
1172 * EncoderSetAudioType: configures the input and output types of the dmo
1173 *****************************************************************************/
1174 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1176 int i, i_selected, i_err;
1177 unsigned int i_last_byterate;
1179 DMO_MEDIA_TYPE dmo_type;
1182 /* Setup the format structure */
1183 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1184 if( i_tag == 0 ) return VLC_EGENERIC;
1186 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1187 p_enc->fmt_in.audio.i_bitspersample = 16;
1189 /* We first need to choose an output type from the predefined
1190 * list of choices (we cycle through the list to select the best match) */
1191 i = 0; i_selected = -1; i_last_byterate = 0;
1192 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1194 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1195 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1196 "bits per sample: %i, bitrate: %i, blockalign: %i",
1197 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1198 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1199 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1201 if( p_wf->wFormatTag == i_tag &&
1202 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1203 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1204 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1206 if( (int)p_wf->nAvgBytesPerSec <
1207 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1208 p_wf->nAvgBytesPerSec > i_last_byterate )
1211 i_last_byterate = p_wf->nAvgBytesPerSec;
1212 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1213 i_selected, p_wf->nAvgBytesPerSec * 8 );
1217 DMOFreeMediaType( &dmo_type );
1220 if( i_selected < 0 )
1222 msg_Err( p_enc, "couldn't find a matching output" );
1223 return VLC_EGENERIC;
1226 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1227 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1229 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1230 "channels: %i, 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 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1236 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1237 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1238 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1239 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1243 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1244 p_enc->fmt_out.i_extra = p_wf->cbSize;
1245 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1246 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1249 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1250 DMOFreeMediaType( &dmo_type );
1254 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1255 return VLC_EGENERIC;
1258 msg_Dbg( p_enc, "successfully set output type" );
1260 /* Setup the input type */
1261 i = 0; i_selected = -1;
1262 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1264 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1265 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1266 "bits per sample: %i, bitrate: %i, blockalign: %i",
1267 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1268 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1269 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1271 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1272 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1273 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1274 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1279 DMOFreeMediaType( &dmo_type );
1282 if( i_selected < 0 )
1284 msg_Err( p_enc, "couldn't find a matching input" );
1285 return VLC_EGENERIC;
1288 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1289 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1290 DMOFreeMediaType( &dmo_type );
1293 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1294 return VLC_EGENERIC;
1296 msg_Dbg( p_enc, "successfully set input type" );
1301 /*****************************************************************************
1302 * EncOpen: open dmo codec
1303 *****************************************************************************/
1304 static int EncOpen( vlc_object_t *p_this )
1306 encoder_t *p_enc = (encoder_t*)p_this;
1307 encoder_sys_t *p_sys = NULL;
1308 IMediaObject *p_dmo = NULL;
1309 HINSTANCE hmsdmo_dll = NULL;
1312 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1314 /* Initialize OLE/COM */
1318 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, VLC_TRUE )
1326 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1328 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1332 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1335 /* Allocate the memory needed to store the decoder's structure */
1336 if( ( p_enc->p_sys = p_sys =
1337 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1339 msg_Err( p_enc, "out of memory" );
1343 p_sys->hmsdmo_dll = hmsdmo_dll;
1344 p_sys->p_dmo = p_dmo;
1346 p_sys->ldt_fs = ldt_fs;
1349 /* Find out some properties of the inputput */
1351 uint32_t i_size, i_align, dum;
1353 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1354 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1356 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1357 i_size, i_align, dum );
1360 /* Find out some properties of the output */
1362 uint32_t i_size, i_align;
1364 p_sys->i_min_output = 0;
1365 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1367 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1372 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1374 p_sys->i_min_output = i_size;
1378 /* Set output properties */
1379 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1380 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1381 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1383 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1389 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1390 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1393 Restore_LDT_Keeper( ldt_fs );
1395 /* Uninitialize OLE/COM */
1399 if( p_sys ) free( p_sys );
1401 return VLC_EGENERIC;
1404 /****************************************************************************
1405 * Encode: the whole thing
1406 ****************************************************************************/
1407 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1409 encoder_sys_t *p_sys = p_enc->p_sys;
1411 block_t *p_chain = NULL;
1412 block_t *p_block_in;
1419 if( EncOpen( VLC_OBJECT(p_enc) ) )
1421 msg_Err( p_enc, "EncOpen failed" );
1424 p_sys = p_enc->p_sys;
1427 if( !p_data ) return NULL;
1429 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1431 /* Get picture data */
1432 int i_plane, i_line, i_width, i_src_stride;
1433 picture_t *p_pic = (picture_t *)p_data;
1436 int i_buffer = p_enc->fmt_in.video.i_width *
1437 p_enc->fmt_in.video.i_height *
1438 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1440 p_block_in = block_New( p_enc, i_buffer );
1442 /* Copy picture stride by stride */
1443 p_dst = p_block_in->p_buffer;
1444 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1446 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1447 i_width = p_pic->p[i_plane].i_visible_pitch;
1448 i_src_stride = p_pic->p[i_plane].i_pitch;
1450 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1453 p_enc->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
1455 p_src += i_src_stride;
1459 i_pts = p_pic->date;
1463 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1464 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1465 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1466 p_block_in->i_buffer );
1468 i_pts = p_aout_buffer->start_date;
1471 /* Feed input to the DMO */
1472 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, VLC_TRUE );
1473 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1474 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1476 p_in->vt->Release( (IUnknown *)p_in );
1477 if( i_result == S_FALSE )
1479 /* No output generated */
1481 msg_Dbg( p_enc, "ProcessInput(): no output generated "I64Fd, i_pts );
1485 else if( i_result == (int)DMO_E_NOTACCEPTING )
1487 /* Need to call ProcessOutput */
1488 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1490 else if( i_result != S_OK )
1492 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1497 msg_Dbg( p_enc, "ProcessInput(): success" );
1500 /* Get output from the DMO */
1503 DMO_OUTPUT_DATA_BUFFER db;
1504 block_t *p_block_out;
1505 CMediaBuffer *p_out;
1507 p_block_out = block_New( p_enc, p_sys->i_min_output );
1508 p_block_out->i_buffer = 0;
1509 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, VLC_FALSE);
1510 memset( &db, 0, sizeof(db) );
1511 db.pBuffer = (IMediaBuffer *)p_out;
1513 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1514 0, 1, &db, &i_status );
1516 if( i_result != S_OK )
1518 if( i_result != S_FALSE )
1519 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1522 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1525 p_out->vt->Release( (IUnknown *)p_out );
1526 block_Release( p_block_out );
1530 if( !p_block_out->i_buffer )
1533 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1535 p_out->vt->Release( (IUnknown *)p_out );
1536 block_Release( p_block_out );
1540 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1543 msg_Dbg( p_enc, "ProcessOutput(): pts: "I64Fd", "I64Fd,
1544 i_pts, db.rtTimestamp / 10 );
1546 i_pts = db.rtTimestamp / 10;
1549 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1551 p_block_out->i_length = db.rtTimelength / 10;
1553 msg_Dbg( p_enc, "ProcessOutput(): length: "I64Fd,
1554 p_block_out->i_length );
1558 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1560 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1561 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1563 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1566 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1567 block_ChainAppend( &p_chain, p_block_out );
1571 /*****************************************************************************
1572 * EncoderClose: close codec
1573 *****************************************************************************/
1574 void EncoderClose( vlc_object_t *p_this )
1576 encoder_t *p_enc = (encoder_t*)p_this;
1577 encoder_sys_t *p_sys = p_enc->p_sys;
1579 if( !p_sys ) return;
1581 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1582 FreeLibrary( p_sys->hmsdmo_dll );
1586 Restore_LDT_Keeper( p_sys->ldt_fs );
1589 /* Uninitialize OLE/COM */