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 const GUID IID_IWMCodecPrivateData = {0x73f0be8e, 0x57f7, 0x4f01, {0xaa, 0x66, 0x9f, 0x57, 0x34, 0xc, 0xfe, 0xe}};
135 const GUID IID_IMediaObject = {0xd8ad0f58, 0x5494, 0x4102, {0x97, 0xc5, 0xec, 0x79, 0x8e, 0x59, 0xbc, 0xf4}};
136 const GUID IID_IMediaBuffer = {0x59eff8b9, 0x938c, 0x4a26, {0x82, 0xf2, 0x95, 0xcb, 0x84, 0xcd, 0xc8, 0x37}};
137 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
138 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
139 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
140 const GUID FORMAT_VideoInfo = {0x05589f80, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
141 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
142 const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
143 const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
144 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
145 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
146 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
149 static const GUID guid_wvc1 = { 0xc9bfbccf, 0xe60e, 0x4588, { 0xa3, 0xdf, 0x5a, 0x03, 0xb1, 0xfd, 0x95, 0x85 } };
150 static const GUID guid_wmv9 = { 0x724bb6a4, 0xe526, 0x450f, { 0xaf, 0xfa, 0xab, 0x9b, 0x45, 0x12, 0x91, 0x11 } };
152 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
153 static const GUID guid_wms = { 0x7bafb3b1, 0xd8f4, 0x4279, { 0x92, 0x53, 0x27, 0xda, 0x42, 0x31, 0x08, 0xde } };
154 static const GUID guid_wmva ={ 0x03be3ac4, 0x84b7, 0x4e0e, { 0xa7, 0x8d, 0xd3, 0x52, 0x4e, 0x60, 0x39, 0x5a } };
156 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
157 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
159 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
160 static const GUID guid_wmv_enc2 = { 0x96b57cdd, 0x8966, 0x410c,{ 0xbb, 0x1f, 0xc9, 0x7e, 0xea, 0x76, 0x5c, 0x04 } };
161 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
165 vlc_fourcc_t i_fourcc;
171 static const codec_dll decoders_table[] =
174 { VLC_FOURCC('W','V','C','1'), "wvc1dmod.dll", &guid_wvc1 },
175 { VLC_FOURCC('w','v','c','1'), "wvc1dmod.dll", &guid_wvc1 },
177 { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
178 { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
179 { VLC_FOURCC('W','M','V','P'), "wmv9dmod.dll", &guid_wmv9 },
180 { VLC_FOURCC('w','m','v','p'), "wmv9dmod.dll", &guid_wmv9 },
182 { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
183 { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
185 { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
186 { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
188 { VLC_FOURCC('M','S','S','2'), "wmsdmod.dll", &guid_wms },
189 { VLC_FOURCC('m','s','s','2'), "wmsdmod.dll", &guid_wms },
190 { VLC_FOURCC('M','S','S','1'), "wmsdmod.dll", &guid_wms },
191 { VLC_FOURCC('m','s','s','1'), "wmsdmod.dll", &guid_wms },
192 /* Windows Media Video Adv */
193 { VLC_FOURCC('W','M','V','A'), "wmvadvd.dll", &guid_wmva },
194 { VLC_FOURCC('w','m','v','a'), "wmvadvd.dll", &guid_wmva },
195 { VLC_FOURCC('W','V','P','2'), "wmvadvd.dll", &guid_wmva },
196 { VLC_FOURCC('w','v','p','2'), "wmvadvd.dll", &guid_wmva },
199 { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
200 { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
201 { VLC_FOURCC('W','M','A','P'), "wma9dmod.dll", &guid_wma9 },
202 { VLC_FOURCC('w','m','a','p'), "wma9dmod.dll", &guid_wma9 },
204 { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
205 { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
208 { VLC_FOURCC('W','M','A','S'), "wmspdmod.dll", &guid_wma },
209 { VLC_FOURCC('w','m','a','s'), "wmspdmod.dll", &guid_wma },
215 static const codec_dll encoders_table[] =
218 { VLC_FOURCC('W','M','V','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
219 { VLC_FOURCC('w','m','v','3'), "wmvdmoe2.dll", &guid_wmv_enc2 },
221 { VLC_FOURCC('W','M','V','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
222 { VLC_FOURCC('w','m','v','2'), "wmvdmoe2.dll", &guid_wmv_enc2 },
224 { VLC_FOURCC('W','M','V','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
225 { VLC_FOURCC('w','m','v','1'), "wmvdmoe2.dll", &guid_wmv_enc2 },
228 { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
229 { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
231 { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
232 { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
238 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
240 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
241 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
247 /*****************************************************************************
248 * DecoderOpen: open dmo codec
249 *****************************************************************************/
250 static int DecoderOpen( vlc_object_t *p_this )
252 decoder_t *p_dec = (decoder_t*)p_this;
255 int i_ret = DecOpen( p_this );
256 if( i_ret != VLC_SUCCESS ) return i_ret;
260 /* We can't open it now, because of ldt_keeper or something
261 * Open/Decode/Close has to be done in the same thread */
266 /* Probe if we support it */
267 for( i = 0; decoders_table[i].i_fourcc != 0; i++ )
269 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
271 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
272 (char*)&p_dec->fmt_in.i_codec,
273 decoders_table[i].psz_dll );
279 if( !decoders_table[i].i_fourcc ) return VLC_EGENERIC;
283 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
285 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
291 /*****************************************************************************
292 * DecOpen: open dmo codec
293 *****************************************************************************/
294 static int DecOpen( vlc_object_t *p_this )
296 decoder_t *p_dec = (decoder_t*)p_this;
297 decoder_sys_t *p_sys = NULL;
299 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
300 IMediaObject *p_dmo = NULL;
301 HINSTANCE hmsdmo_dll = NULL;
303 VIDEOINFOHEADER *p_vih = NULL;
304 WAVEFORMATEX *p_wf = NULL;
307 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
309 /* Initialize OLE/COM */
313 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, VLC_FALSE )
321 /* Setup input format */
322 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
323 dmo_input_type.pUnk = 0;
325 if( p_dec->fmt_in.i_cat == AUDIO_ES )
328 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
329 p_wf = malloc( i_size );
331 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
332 if( p_dec->fmt_in.i_extra )
333 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
335 dmo_input_type.majortype = MEDIATYPE_Audio;
336 dmo_input_type.subtype = dmo_input_type.majortype;
337 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
338 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
339 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
341 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
342 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
343 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
344 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
345 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
346 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
347 p_wf->cbSize = p_dec->fmt_in.i_extra;
349 dmo_input_type.formattype = FORMAT_WaveFormatEx;
350 dmo_input_type.cbFormat = i_size;
351 dmo_input_type.pbFormat = (char *)p_wf;
352 dmo_input_type.bFixedSizeSamples = 1;
353 dmo_input_type.bTemporalCompression = 0;
354 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
358 BITMAPINFOHEADER *p_bih;
360 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
361 p_vih = malloc( i_size );
363 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
364 if( p_dec->fmt_in.i_extra )
365 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
367 p_bih = &p_vih->bmiHeader;
368 p_bih->biCompression = p_dec->fmt_in.i_codec;
369 p_bih->biWidth = p_dec->fmt_in.video.i_width;
370 p_bih->biHeight = p_dec->fmt_in.video.i_height;
371 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
373 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
374 sizeof(BITMAPINFOHEADER);
376 p_vih->rcSource.left = p_vih->rcSource.top = 0;
377 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
378 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
379 p_vih->rcTarget = p_vih->rcSource;
381 dmo_input_type.majortype = MEDIATYPE_Video;
382 dmo_input_type.subtype = dmo_input_type.majortype;
383 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
384 dmo_input_type.formattype = FORMAT_VideoInfo;
385 dmo_input_type.bFixedSizeSamples = 0;
386 dmo_input_type.bTemporalCompression = 1;
387 dmo_input_type.cbFormat = i_size;
388 dmo_input_type.pbFormat = (char *)p_vih;
391 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
393 msg_Err( p_dec, "can't set DMO input type" );
396 msg_Dbg( p_dec, "DMO input type set" );
398 /* Setup output format */
399 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
400 dmo_output_type.pUnk = 0;
402 if( p_dec->fmt_in.i_cat == AUDIO_ES )
404 /* Setup the format */
405 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
406 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
407 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
408 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
409 p_dec->fmt_out.audio.i_physical_channels =
410 p_dec->fmt_out.audio.i_original_channels =
411 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
413 p_wf->wFormatTag = WAVE_FORMAT_PCM;
414 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
415 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
416 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
418 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
419 p_wf->nAvgBytesPerSec =
420 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
423 dmo_output_type.majortype = MEDIATYPE_Audio;
424 dmo_output_type.formattype = FORMAT_WaveFormatEx;
425 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
426 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
427 dmo_output_type.pbFormat = (char *)p_wf;
428 dmo_output_type.bFixedSizeSamples = 1;
429 dmo_output_type.bTemporalCompression = 0;
430 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
434 BITMAPINFOHEADER *p_bih;
436 unsigned i_chroma = VLC_FOURCC('Y','U','Y','2');
437 int i_planes = 1, i_bpp = 16;
440 /* Find out which chroma to use */
441 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
443 if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
445 i_chroma = mt.subtype.Data1;
446 i_planes = 3; i_bpp = 12;
449 DMOFreeMediaType( &mt );
452 p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
453 VLC_FOURCC('I','4','2','0') : i_chroma;
454 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
455 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
456 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
458 /* If an aspect-ratio was specified in the input format then force it */
459 if( p_dec->fmt_in.video.i_aspect )
461 p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
465 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
466 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
469 p_bih = &p_vih->bmiHeader;
470 p_bih->biCompression = i_chroma;
471 p_bih->biHeight *= -1;
472 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
473 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
474 p_dec->fmt_in.video.i_height *
475 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
477 p_bih->biPlanes = i_planes;
478 p_bih->biSize = sizeof(BITMAPINFOHEADER);
480 dmo_output_type.majortype = MEDIATYPE_Video;
481 dmo_output_type.formattype = FORMAT_VideoInfo;
482 dmo_output_type.subtype = dmo_output_type.majortype;
483 dmo_output_type.subtype.Data1 = p_bih->biCompression;
484 dmo_output_type.bFixedSizeSamples = VLC_TRUE;
485 dmo_output_type.bTemporalCompression = 0;
486 dmo_output_type.lSampleSize = p_bih->biSizeImage;
487 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
488 dmo_output_type.pbFormat = (char *)p_vih;
492 /* Enumerate output types */
493 if( p_dec->fmt_in.i_cat == VIDEO_ES )
498 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
500 msg_Dbg( p_dec, "available output chroma: %4.4s",
501 (char *)&mt.subtype.Data1 );
502 DMOFreeMediaType( &mt );
507 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
509 msg_Err( p_dec, "can't set DMO output type" );
512 msg_Dbg( p_dec, "DMO output type set" );
514 /* Allocate the memory needed to store the decoder's structure */
515 if( ( p_dec->p_sys = p_sys =
516 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
518 msg_Err( p_dec, "out of memory" );
522 p_sys->hmsdmo_dll = hmsdmo_dll;
523 p_sys->p_dmo = p_dmo;
525 p_sys->ldt_fs = ldt_fs;
528 /* Find out some properties of the output */
530 uint32_t i_size, i_align;
532 p_sys->i_min_output = 0;
533 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
535 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
540 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
542 p_sys->i_min_output = i_size;
543 p_sys->p_buffer = malloc( i_size );
544 if( !p_sys->p_buffer ) goto error;
548 /* Set output properties */
549 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
550 if( p_dec->fmt_out.i_cat == AUDIO_ES )
551 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
553 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
555 if( p_vih ) free( p_vih );
556 if( p_wf ) free( p_wf );
562 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
563 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
566 Restore_LDT_Keeper( ldt_fs );
568 /* Uninitialize OLE/COM */
572 if( p_vih ) free( p_vih );
573 if( p_wf ) free( p_wf );
574 if( p_sys ) free( p_sys );
579 /*****************************************************************************
580 * LoadDMO: Load the DMO object
581 *****************************************************************************/
582 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
583 IMediaObject **pp_dmo, es_format_t *p_fmt,
586 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
590 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
591 const DMO_PARTIAL_MEDIATYPE *,
592 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
595 IEnumDMO *p_enum_dmo = NULL;
602 IClassFactory *cFactory = NULL;
603 IUnknown *cObject = NULL;
604 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
607 /* Look for a DMO which can handle the requested codec */
608 if( p_fmt->i_cat == AUDIO_ES )
611 dmo_partial_type.type = MEDIATYPE_Audio;
612 dmo_partial_type.subtype = dmo_partial_type.type;
613 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
614 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
615 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
619 dmo_partial_type.type = MEDIATYPE_Video;
620 dmo_partial_type.subtype = dmo_partial_type.type;
621 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
626 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
627 if( *p_hmsdmo_dll == NULL )
629 msg_Dbg( p_this, "failed loading msdmo.dll" );
632 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
633 if( OurDMOEnum == NULL )
635 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
636 FreeLibrary( *p_hmsdmo_dll );
642 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
643 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
647 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
648 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
652 FreeLibrary( *p_hmsdmo_dll );
653 /* return VLC_EGENERIC; */
654 /* Try loading the dll directly */
658 /* Pickup the first available codec */
660 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
661 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
663 char psz_temp[MAX_PATH];
664 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
665 msg_Dbg( p_this, "found DMO: %s", psz_temp );
666 CoTaskMemFree( psz_dmo_name );
669 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
670 &IID_IMediaObject, (void **)pp_dmo ) )
672 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
678 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
682 FreeLibrary( *p_hmsdmo_dll );
683 /* return VLC_EGENERIC; */
684 /* Try loading the dll directly */
693 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
695 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
698 if( codecs_table[i_codec].i_fourcc == 0 )
699 return VLC_EGENERIC; /* Can't happen */
701 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
702 if( *p_hmsdmo_dll == NULL )
704 msg_Dbg( p_this, "failed loading '%s'",
705 codecs_table[i_codec].psz_dll );
709 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
712 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
713 FreeLibrary( *p_hmsdmo_dll );
717 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
719 if( i_err || cFactory == NULL )
721 msg_Dbg( p_this, "no such class object" );
722 FreeLibrary( *p_hmsdmo_dll );
726 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
728 cFactory->vt->Release( (IUnknown*)cFactory );
729 if( i_err || !cObject )
731 msg_Dbg( p_this, "class factory failure" );
732 FreeLibrary( *p_hmsdmo_dll );
735 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
737 cObject->vt->Release( (IUnknown*)cObject );
738 if( i_err || !*pp_dmo )
740 msg_Dbg( p_this, "QueryInterface failure" );
741 FreeLibrary( *p_hmsdmo_dll );
748 /*****************************************************************************
749 * DecoderClose: close codec
750 *****************************************************************************/
751 void DecoderClose( vlc_object_t *p_this )
753 decoder_t *p_dec = (decoder_t*)p_this;
754 decoder_sys_t *p_sys = p_dec->p_sys;
758 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
759 FreeLibrary( p_sys->hmsdmo_dll );
763 Restore_LDT_Keeper( p_sys->ldt_fs );
766 /* Uninitialize OLE/COM */
770 if( p_sys->p_buffer ) free( p_sys->p_buffer );
774 /****************************************************************************
775 * DecodeBlock: the whole thing
776 ****************************************************************************
777 * This function must be fed with ogg packets.
778 ****************************************************************************/
779 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
781 decoder_sys_t *p_sys = p_dec->p_sys;
785 DMO_OUTPUT_DATA_BUFFER db;
792 if( DecOpen( VLC_OBJECT(p_dec) ) )
794 msg_Err( p_dec, "DecOpen failed" );
797 p_sys = p_dec->p_sys;
800 if( !pp_block ) return NULL;
804 /* Won't work with streams with B-frames, but do we have any ? */
805 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
807 /* Date management */
808 if( p_block && p_block->i_pts > 0 &&
809 p_block->i_pts != date_Get( &p_sys->end_date ) )
811 date_Set( &p_sys->end_date, p_block->i_pts );
814 #if 0 /* Breaks the video decoding */
815 if( !date_Get( &p_sys->end_date ) )
817 /* We've just started the stream, wait for the first PTS. */
818 if( p_block ) block_Release( p_block );
823 /* Feed input to the DMO */
824 if( p_block && p_block->i_buffer )
828 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );
830 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
831 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
834 p_in->vt->Release( (IUnknown *)p_in );
836 if( i_result == S_FALSE )
838 /* No output generated */
840 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
844 else if( i_result == (int)DMO_E_NOTACCEPTING )
846 /* Need to call ProcessOutput */
847 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
849 else if( i_result != S_OK )
851 msg_Dbg( p_dec, "ProcessInput(): failed" );
856 //msg_Dbg( p_dec, "ProcessInput(): successful" );
860 else if( p_block && !p_block->i_buffer )
862 block_Release( p_block );
866 /* Get output from the DMO */
867 block_out.p_buffer = p_sys->p_buffer;
868 block_out.i_buffer = 0;
870 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );
871 memset( &db, 0, sizeof(db) );
872 db.pBuffer = (IMediaBuffer *)p_out;
874 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
875 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
878 if( i_result != S_OK )
880 if( i_result != S_FALSE )
881 msg_Dbg( p_dec, "ProcessOutput(): failed" );
884 msg_Dbg( p_dec, "ProcessOutput(): no output" );
887 p_out->vt->Release( (IUnknown *)p_out );
892 msg_Dbg( p_dec, "ProcessOutput(): success" );
895 if( !block_out.i_buffer )
898 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
900 p_out->vt->Release( (IUnknown *)p_out );
904 if( p_dec->fmt_out.i_cat == VIDEO_ES )
906 /* Get a new picture */
907 picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
908 if( !p_pic ) return NULL;
910 CopyPicture( p_dec, p_pic, block_out.p_buffer );
912 /* Date management */
913 p_pic->date = date_Get( &p_sys->end_date );
914 date_Increment( &p_sys->end_date, 1 );
916 p_out->vt->Release( (IUnknown *)p_out );
922 aout_buffer_t *p_aout_buffer;
923 int i_samples = block_out.i_buffer /
924 ( p_dec->fmt_out.audio.i_bitspersample *
925 p_dec->fmt_out.audio.i_channels / 8 );
927 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
928 memcpy( p_aout_buffer->p_buffer,
929 block_out.p_buffer, block_out.i_buffer );
931 /* Date management */
932 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
933 p_aout_buffer->end_date =
934 date_Increment( &p_sys->end_date, i_samples );
936 p_out->vt->Release( (IUnknown *)p_out );
938 return p_aout_buffer;
944 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )
946 int i_plane, i_line, i_width, i_dst_stride;
947 uint8_t *p_dst, *p_src = p_in;
949 p_dst = p_pic->p[1].p_pixels;
950 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
951 p_pic->p[2].p_pixels = p_dst;
953 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
955 p_dst = p_pic->p[i_plane].p_pixels;
956 i_width = p_pic->p[i_plane].i_visible_pitch;
957 i_dst_stride = p_pic->p[i_plane].i_pitch;
959 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
961 p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
963 p_dst += i_dst_stride;
967 p_dst = p_pic->p[1].p_pixels;
968 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
969 p_pic->p[2].p_pixels = p_dst;
972 /****************************************************************************
973 * Encoder descriptor declaration
974 ****************************************************************************/
977 HINSTANCE hmsdmo_dll;
989 /*****************************************************************************
990 * EncoderOpen: open dmo codec
991 *****************************************************************************/
992 static int EncoderOpen( vlc_object_t *p_this )
994 encoder_t *p_enc = (encoder_t*)p_this;
996 int i_ret = EncOpen( p_this );
997 if( i_ret != VLC_SUCCESS ) return i_ret;
1000 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
1002 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
1008 /*****************************************************************************
1009 * EncoderSetVideoType: configures the input and output types of the dmo
1010 *****************************************************************************/
1011 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
1013 int i, i_selected, i_err;
1014 DMO_MEDIA_TYPE dmo_type;
1015 VIDEOINFOHEADER vih, *p_vih;
1016 BITMAPINFOHEADER *p_bih;
1019 p_enc->fmt_in.video.i_bits_per_pixel =
1020 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1022 /* Enumerate input format (for debug output) */
1024 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1026 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1028 msg_Dbg( p_enc, "available input chroma: %4.4s",
1029 (char *)&dmo_type.subtype.Data1 );
1030 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1031 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1032 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1033 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1035 DMOFreeMediaType( &dmo_type );
1038 /* Setup input format */
1039 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;
1417 if( !p_data ) return NULL;
1419 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1421 /* Get picture data */
1422 int i_plane, i_line, i_width, i_src_stride;
1423 picture_t *p_pic = (picture_t *)p_data;
1426 int i_buffer = p_enc->fmt_in.video.i_width *
1427 p_enc->fmt_in.video.i_height *
1428 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1430 p_block_in = block_New( p_enc, i_buffer );
1432 /* Copy picture stride by stride */
1433 p_dst = p_block_in->p_buffer;
1434 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1436 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1437 i_width = p_pic->p[i_plane].i_visible_pitch;
1438 i_src_stride = p_pic->p[i_plane].i_pitch;
1440 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1443 p_enc->p_libvlc->pf_memcpy( p_dst, p_src, i_width );
1445 p_src += i_src_stride;
1449 i_pts = p_pic->date;
1453 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1454 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1455 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1456 p_block_in->i_buffer );
1458 i_pts = p_aout_buffer->start_date;
1461 /* Feed input to the DMO */
1462 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, VLC_TRUE );
1463 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1464 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1466 p_in->vt->Release( (IUnknown *)p_in );
1467 if( i_result == S_FALSE )
1469 /* No output generated */
1471 msg_Dbg( p_enc, "ProcessInput(): no output generated "I64Fd, i_pts );
1475 else if( i_result == (int)DMO_E_NOTACCEPTING )
1477 /* Need to call ProcessOutput */
1478 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1480 else if( i_result != S_OK )
1482 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1487 msg_Dbg( p_enc, "ProcessInput(): success" );
1490 /* Get output from the DMO */
1493 DMO_OUTPUT_DATA_BUFFER db;
1494 block_t *p_block_out;
1495 CMediaBuffer *p_out;
1497 p_block_out = block_New( p_enc, p_sys->i_min_output );
1498 p_block_out->i_buffer = 0;
1499 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, VLC_FALSE);
1500 memset( &db, 0, sizeof(db) );
1501 db.pBuffer = (IMediaBuffer *)p_out;
1503 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1504 0, 1, &db, &i_status );
1506 if( i_result != S_OK )
1508 if( i_result != S_FALSE )
1509 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1512 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1515 p_out->vt->Release( (IUnknown *)p_out );
1516 block_Release( p_block_out );
1520 if( !p_block_out->i_buffer )
1523 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1525 p_out->vt->Release( (IUnknown *)p_out );
1526 block_Release( p_block_out );
1530 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1533 msg_Dbg( p_enc, "ProcessOutput(): pts: "I64Fd", "I64Fd,
1534 i_pts, db.rtTimestamp / 10 );
1536 i_pts = db.rtTimestamp / 10;
1539 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1541 p_block_out->i_length = db.rtTimelength / 10;
1543 msg_Dbg( p_enc, "ProcessOutput(): length: "I64Fd,
1544 p_block_out->i_length );
1548 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1550 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1551 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1553 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1556 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1557 block_ChainAppend( &p_chain, p_block_out );
1561 /*****************************************************************************
1562 * EncoderClose: close codec
1563 *****************************************************************************/
1564 void EncoderClose( vlc_object_t *p_this )
1566 encoder_t *p_enc = (encoder_t*)p_this;
1567 encoder_sys_t *p_sys = p_enc->p_sys;
1569 if( !p_sys ) return;
1571 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1572 FreeLibrary( p_sys->hmsdmo_dll );
1576 Restore_LDT_Keeper( p_sys->ldt_fs );
1579 /* Uninitialize OLE/COM */