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/decoder.h>
42 /* Need the w32dll loader from mplayer */
43 # include <wine/winerror.h>
44 # include <ldt_keeper.h>
45 # include <wine/windef.h>
55 long CoInitialize( void *pvReserved ) { return -1; }
56 void CoUninitialize( void ) { }
58 /* A few prototypes */
59 HMODULE WINAPI LoadLibraryA(LPCSTR);
60 #define LoadLibrary LoadLibraryA
61 FARPROC WINAPI GetProcAddress(HMODULE,LPCSTR);
62 int WINAPI FreeLibrary(HMODULE);
65 typedef long (STDCALL *GETCLASS) ( const GUID*, const GUID*, void** );
67 static int pi_channels_maps[7] =
71 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
72 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
73 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
74 | AOUT_CHAN_REARRIGHT,
75 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
76 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
77 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
78 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
81 /*****************************************************************************
83 *****************************************************************************/
84 static int DecoderOpen ( vlc_object_t * );
85 static int DecOpen ( vlc_object_t * );
86 static void DecoderClose ( vlc_object_t * );
87 static void *DecodeBlock ( decoder_t *, block_t ** );
89 static int EncoderOpen ( vlc_object_t * );
90 static int EncOpen ( vlc_object_t * );
91 static void EncoderClose ( vlc_object_t * );
92 static block_t *EncodeBlock( encoder_t *, void * );
94 static int LoadDMO( vlc_object_t *, HINSTANCE *, IMediaObject **,
95 es_format_t *, vlc_bool_t );
96 static void CopyPicture( decoder_t *, picture_t *, uint8_t * );
99 set_description( _("DirectMedia Object decoder") );
100 add_shortcut( "dmo" );
101 set_capability( "decoder", 1 );
102 set_callbacks( DecoderOpen, DecoderClose );
103 set_category( CAT_INPUT );
104 set_subcategory( SUBCAT_INPUT_SCODEC );
106 # define ENC_CFG_PREFIX "sout-dmo-"
108 set_description( _("DirectMedia Object encoder") );
109 set_capability( "encoder", 10 );
110 set_callbacks( EncoderOpen, EncoderClose );
114 /*****************************************************************************
116 *****************************************************************************/
118 /****************************************************************************
119 * Decoder descriptor declaration
120 ****************************************************************************/
123 HINSTANCE hmsdmo_dll;
136 static const GUID guid_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 } };
139 static const GUID guid_wma9 = { 0x27ca0808, 0x01f5, 0x4e7a, { 0x8b, 0x05, 0x87, 0xf8, 0x07, 0xa2, 0x33, 0xd1 } };
141 static const GUID guid_wmv = { 0x82d353df, 0x90bd, 0x4382, { 0x8b, 0xc2, 0x3f, 0x61, 0x92, 0xb7, 0x6e, 0x34 } };
142 static const GUID guid_wma = { 0x874131cb, 0x4ecc, 0x443b, { 0x89, 0x48, 0x74, 0x6b, 0x89, 0x59, 0x5d, 0x20 } };
144 static const GUID guid_wmv_enc = { 0x3181343b, 0x94a2, 0x4feb, { 0xad, 0xef, 0x30, 0xa1, 0xdd, 0xe6, 0x17, 0xb4 } };
145 static const GUID guid_wma_enc = { 0x70f598e9, 0xf4ab, 0x495a, { 0x99, 0xe2, 0xa7, 0xc4, 0xd3, 0xd8, 0x9a, 0xbf } };
149 vlc_fourcc_t i_fourcc;
155 static const codec_dll decoders_table[] =
158 { VLC_FOURCC('W','V','C','1'), "wvc1dmod.dll", &guid_wvc1 },
159 { VLC_FOURCC('w','v','c','1'), "wvc1dmod.dll", &guid_wvc1 },
161 { VLC_FOURCC('W','M','V','3'), "wmv9dmod.dll", &guid_wmv9 },
162 { VLC_FOURCC('w','m','v','3'), "wmv9dmod.dll", &guid_wmv9 },
164 { VLC_FOURCC('W','M','V','2'), "wmvdmod.dll", &guid_wmv },
165 { VLC_FOURCC('w','m','v','2'), "wmvdmod.dll", &guid_wmv },
167 { VLC_FOURCC('W','M','V','1'), "wmvdmod.dll", &guid_wmv },
168 { VLC_FOURCC('w','m','v','1'), "wmvdmod.dll", &guid_wmv },
171 { VLC_FOURCC('W','M','A','3'), "wma9dmod.dll", &guid_wma9 },
172 { VLC_FOURCC('w','m','a','3'), "wma9dmod.dll", &guid_wma9 },
173 { VLC_FOURCC('w','m','a','p'), "wma9dmod.dll", &guid_wma9 },
175 { VLC_FOURCC('W','M','A','2'), "wma9dmod.dll", &guid_wma9 },
176 { VLC_FOURCC('w','m','a','2'), "wma9dmod.dll", &guid_wma9 },
179 { VLC_FOURCC('w','m','a','s'), "wmspdmod.dll", &guid_wma },
185 static const codec_dll encoders_table[] =
188 { VLC_FOURCC('W','M','V','2'), "wmvdmoe.dll", &guid_wmv_enc },
189 { VLC_FOURCC('w','m','v','1'), "wmvdmoe.dll", &guid_wmv_enc },
191 { VLC_FOURCC('W','M','V','1'), "wmvdmoe.dll", &guid_wmv_enc },
192 { VLC_FOURCC('w','m','v','1'), "wmvdmoe.dll", &guid_wmv_enc },
195 { VLC_FOURCC('W','M','A','3'), "wmadmoe.dll", &guid_wma_enc },
196 { VLC_FOURCC('w','m','a','3'), "wmadmoe.dll", &guid_wma_enc },
198 { VLC_FOURCC('W','M','A','2'), "wmadmoe.dll", &guid_wma_enc },
199 { VLC_FOURCC('w','m','a','2'), "wmadmoe.dll", &guid_wma_enc },
205 static void WINAPI DMOFreeMediaType( DMO_MEDIA_TYPE *mt )
207 if( mt->cbFormat != 0 ) CoTaskMemFree( (PVOID)mt->pbFormat );
208 if( mt->pUnk != NULL ) mt->pUnk->vt->Release( (IUnknown *)mt->pUnk );
214 /*****************************************************************************
215 * DecoderOpen: open dmo codec
216 *****************************************************************************/
217 static int DecoderOpen( vlc_object_t *p_this )
219 decoder_t *p_dec = (decoder_t*)p_this;
222 int i_ret = DecOpen( p_this );
223 if( i_ret != VLC_SUCCESS ) return i_ret;
227 /* We can't open it now, because of ldt_keeper or something
228 * Open/Decode/Close has to be done in the same thread */
233 /* Probe if we support it */
234 for( i = 0; decoders_table[i].i_fourcc != 0; i++ )
236 if( decoders_table[i].i_fourcc == p_dec->fmt_in.i_codec )
238 msg_Dbg( p_dec, "DMO codec for %4.4s may work with dll=%s",
239 (char*)&p_dec->fmt_in.i_codec,
240 decoders_table[i].psz_dll );
246 if( !decoders_table[i].i_fourcc ) return VLC_EGENERIC;
250 p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
252 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
258 /*****************************************************************************
259 * DecOpen: open dmo codec
260 *****************************************************************************/
261 static int DecOpen( vlc_object_t *p_this )
263 decoder_t *p_dec = (decoder_t*)p_this;
264 decoder_sys_t *p_sys = NULL;
266 DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;
267 IMediaObject *p_dmo = NULL;
268 HINSTANCE hmsdmo_dll = NULL;
270 VIDEOINFOHEADER *p_vih = NULL;
271 WAVEFORMATEX *p_wf = NULL;
274 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
276 /* Initialize OLE/COM */
280 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_dec->fmt_in, VLC_FALSE )
288 /* Setup input format */
289 memset( &dmo_input_type, 0, sizeof(dmo_input_type) );
290 dmo_input_type.pUnk = 0;
292 if( p_dec->fmt_in.i_cat == AUDIO_ES )
295 int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;
296 p_wf = malloc( i_size );
298 memset( p_wf, 0, sizeof(WAVEFORMATEX) );
299 if( p_dec->fmt_in.i_extra )
300 memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
302 dmo_input_type.majortype = MEDIATYPE_Audio;
303 dmo_input_type.subtype = dmo_input_type.majortype;
304 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
305 fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );
306 if( i_tag ) dmo_input_type.subtype.Data1 = i_tag;
308 p_wf->wFormatTag = dmo_input_type.subtype.Data1;
309 p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;
310 p_wf->nChannels = p_dec->fmt_in.audio.i_channels;
311 p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;
312 p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;
313 p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;
314 p_wf->cbSize = p_dec->fmt_in.i_extra;
316 dmo_input_type.formattype = FORMAT_WaveFormatEx;
317 dmo_input_type.cbFormat = i_size;
318 dmo_input_type.pbFormat = (char *)p_wf;
319 dmo_input_type.bFixedSizeSamples = 1;
320 dmo_input_type.bTemporalCompression = 0;
321 dmo_input_type.lSampleSize = p_wf->nBlockAlign;
325 BITMAPINFOHEADER *p_bih;
327 int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;
328 p_vih = malloc( i_size );
330 memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );
331 if( p_dec->fmt_in.i_extra )
332 memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
334 p_bih = &p_vih->bmiHeader;
335 p_bih->biCompression = p_dec->fmt_in.i_codec;
336 p_bih->biWidth = p_dec->fmt_in.video.i_width;
337 p_bih->biHeight = p_dec->fmt_in.video.i_height;
338 p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;
340 p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +
341 sizeof(BITMAPINFOHEADER);
343 p_vih->rcSource.left = p_vih->rcSource.top = 0;
344 p_vih->rcSource.right = p_dec->fmt_in.video.i_width;
345 p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;
346 p_vih->rcTarget = p_vih->rcSource;
348 dmo_input_type.majortype = MEDIATYPE_Video;
349 dmo_input_type.subtype = dmo_input_type.majortype;
350 dmo_input_type.subtype.Data1 = p_dec->fmt_in.i_codec;
351 dmo_input_type.formattype = FORMAT_VideoInfo;
352 dmo_input_type.bFixedSizeSamples = 0;
353 dmo_input_type.bTemporalCompression = 1;
354 dmo_input_type.cbFormat = i_size;
355 dmo_input_type.pbFormat = (char *)p_vih;
358 if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )
360 msg_Err( p_dec, "can't set DMO input type" );
363 msg_Dbg( p_dec, "DMO input type set" );
365 /* Setup output format */
366 memset( &dmo_output_type, 0, sizeof(dmo_output_type) );
367 dmo_output_type.pUnk = 0;
369 if( p_dec->fmt_in.i_cat == AUDIO_ES )
371 /* Setup the format */
372 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
373 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
374 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
375 p_dec->fmt_out.audio.i_bitspersample = 16;//p_dec->fmt_in.audio.i_bitspersample; We request 16
376 p_dec->fmt_out.audio.i_physical_channels =
377 p_dec->fmt_out.audio.i_original_channels =
378 pi_channels_maps[p_dec->fmt_out.audio.i_channels];
380 p_wf->wFormatTag = WAVE_FORMAT_PCM;
381 p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;
382 p_wf->nChannels = p_dec->fmt_out.audio.i_channels;
383 p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;
385 p_wf->wBitsPerSample / 8 * p_wf->nChannels;
386 p_wf->nAvgBytesPerSec =
387 p_wf->nSamplesPerSec * p_wf->nBlockAlign;
390 dmo_output_type.majortype = MEDIATYPE_Audio;
391 dmo_output_type.formattype = FORMAT_WaveFormatEx;
392 dmo_output_type.subtype = MEDIASUBTYPE_PCM;
393 dmo_output_type.cbFormat = sizeof(WAVEFORMATEX);
394 dmo_output_type.pbFormat = (char *)p_wf;
395 dmo_output_type.bFixedSizeSamples = 1;
396 dmo_output_type.bTemporalCompression = 0;
397 dmo_output_type.lSampleSize = p_wf->nBlockAlign;
401 BITMAPINFOHEADER *p_bih;
403 unsigned i_chroma = VLC_FOURCC('Y','U','Y','2');
404 int i_planes = 1, i_bpp = 16;
407 /* Find out which chroma to use */
408 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
410 if( mt.subtype.Data1 == VLC_FOURCC('Y','V','1','2') )
412 i_chroma = mt.subtype.Data1;
413 i_planes = 3; i_bpp = 12;
416 DMOFreeMediaType( &mt );
419 p_dec->fmt_out.i_codec = i_chroma == VLC_FOURCC('Y','V','1','2') ?
420 VLC_FOURCC('I','4','2','0') : i_chroma;
421 p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
422 p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
423 p_dec->fmt_out.video.i_bits_per_pixel = i_bpp;
424 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
425 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
427 p_bih = &p_vih->bmiHeader;
428 p_bih->biCompression = i_chroma;
429 p_bih->biHeight *= -1;
430 p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;
431 p_bih->biSizeImage = p_dec->fmt_in.video.i_width *
432 p_dec->fmt_in.video.i_height *
433 (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;
435 p_bih->biPlanes = i_planes;
436 p_bih->biSize = sizeof(BITMAPINFOHEADER);
438 dmo_output_type.majortype = MEDIATYPE_Video;
439 dmo_output_type.formattype = FORMAT_VideoInfo;
440 dmo_output_type.subtype = dmo_output_type.majortype;
441 dmo_output_type.subtype.Data1 = p_bih->biCompression;
442 dmo_output_type.bFixedSizeSamples = VLC_TRUE;
443 dmo_output_type.bTemporalCompression = 0;
444 dmo_output_type.lSampleSize = p_bih->biSizeImage;
445 dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);
446 dmo_output_type.pbFormat = (char *)p_vih;
450 /* Enumerate output types */
451 if( p_dec->fmt_in.i_cat == VIDEO_ES )
456 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )
458 msg_Dbg( p_dec, "available output chroma: %4.4s",
459 (char *)&mt.subtype.Data1 );
460 DMOFreeMediaType( &mt );
465 if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )
467 msg_Err( p_dec, "can't set DMO output type" );
470 msg_Dbg( p_dec, "DMO output type set" );
472 /* Allocate the memory needed to store the decoder's structure */
473 if( ( p_dec->p_sys = p_sys =
474 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
476 msg_Err( p_dec, "out of memory" );
480 p_sys->hmsdmo_dll = hmsdmo_dll;
481 p_sys->p_dmo = p_dmo;
483 p_sys->ldt_fs = ldt_fs;
486 /* Find out some properties of the output */
488 uint32_t i_size, i_align;
490 p_sys->i_min_output = 0;
491 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
493 msg_Err( p_dec, "GetOutputSizeInfo() failed" );
498 msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",
500 p_sys->i_min_output = i_size;
501 p_sys->p_buffer = malloc( i_size );
502 if( !p_sys->p_buffer ) goto error;
506 /* Set output properties */
507 p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
508 if( p_dec->fmt_out.i_cat == AUDIO_ES )
509 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
511 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
513 if( p_vih ) free( p_vih );
514 if( p_wf ) free( p_wf );
520 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
521 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
524 Restore_LDT_Keeper( ldt_fs );
526 /* Uninitialize OLE/COM */
530 if( p_vih ) free( p_vih );
531 if( p_wf ) free( p_wf );
532 if( p_sys ) free( p_sys );
537 /*****************************************************************************
538 * LoadDMO: Load the DMO object
539 *****************************************************************************/
540 static int LoadDMO( vlc_object_t *p_this, HINSTANCE *p_hmsdmo_dll,
541 IMediaObject **pp_dmo, es_format_t *p_fmt,
544 DMO_PARTIAL_MEDIATYPE dmo_partial_type;
548 long (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,
549 const DMO_PARTIAL_MEDIATYPE *,
550 uint32_t, const DMO_PARTIAL_MEDIATYPE *,
553 IEnumDMO *p_enum_dmo = NULL;
560 IClassFactory *cFactory = NULL;
561 IUnknown *cObject = NULL;
562 const codec_dll *codecs_table = b_out ? encoders_table : decoders_table;
565 /* Look for a DMO which can handle the requested codec */
566 if( p_fmt->i_cat == AUDIO_ES )
569 dmo_partial_type.type = MEDIATYPE_Audio;
570 dmo_partial_type.subtype = dmo_partial_type.type;
571 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
572 fourcc_to_wf_tag( p_fmt->i_codec, &i_tag );
573 if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;
577 dmo_partial_type.type = MEDIATYPE_Video;
578 dmo_partial_type.subtype = dmo_partial_type.type;
579 dmo_partial_type.subtype.Data1 = p_fmt->i_codec;
584 *p_hmsdmo_dll = LoadLibrary( "msdmo.dll" );
585 if( *p_hmsdmo_dll == NULL )
587 msg_Dbg( p_this, "failed loading msdmo.dll" );
590 OurDMOEnum = (void *)GetProcAddress( *p_hmsdmo_dll, "DMOEnum" );
591 if( OurDMOEnum == NULL )
593 msg_Dbg( p_this, "GetProcAddress failed to find DMOEnum()" );
594 FreeLibrary( *p_hmsdmo_dll );
600 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
601 1, &dmo_partial_type, 0, NULL, &p_enum_dmo );
605 i_err = OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,
606 0, NULL, 1, &dmo_partial_type, &p_enum_dmo );
610 FreeLibrary( *p_hmsdmo_dll );
611 /* return VLC_EGENERIC; */
612 /* Try loading the dll directly */
616 /* Pickup the first available codec */
618 while( ( S_OK == p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,
619 &psz_dmo_name, &i_dummy /* NULL doesn't work */ ) ) )
621 char psz_temp[MAX_PATH];
622 wcstombs( psz_temp, psz_dmo_name, MAX_PATH );
623 msg_Dbg( p_this, "found DMO: %s", psz_temp );
624 CoTaskMemFree( psz_dmo_name );
627 if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,
628 &IID_IMediaObject, (void **)pp_dmo ) )
630 msg_Warn( p_this, "can't create DMO: %s", psz_temp );
636 p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );
640 FreeLibrary( *p_hmsdmo_dll );
641 /* return VLC_EGENERIC; */
642 /* Try loading the dll directly */
651 for( i_codec = 0; codecs_table[i_codec].i_fourcc != 0; i_codec++ )
653 if( codecs_table[i_codec].i_fourcc == p_fmt->i_codec )
656 if( codecs_table[i_codec].i_fourcc == 0 )
657 return VLC_EGENERIC; /* Can't happen */
659 *p_hmsdmo_dll = LoadLibrary( codecs_table[i_codec].psz_dll );
660 if( *p_hmsdmo_dll == NULL )
662 msg_Dbg( p_this, "failed loading '%s'",
663 codecs_table[i_codec].psz_dll );
667 GetClass = (GETCLASS)GetProcAddress( *p_hmsdmo_dll, "DllGetClassObject" );
670 msg_Dbg( p_this, "GetProcAddress failed to find DllGetClassObject()" );
671 FreeLibrary( *p_hmsdmo_dll );
675 i_err = GetClass( codecs_table[i_codec].p_guid, &IID_IClassFactory,
677 if( i_err || cFactory == NULL )
679 msg_Dbg( p_this, "no such class object" );
680 FreeLibrary( *p_hmsdmo_dll );
684 i_err = cFactory->vt->CreateInstance( cFactory, 0, &IID_IUnknown,
686 cFactory->vt->Release( (IUnknown*)cFactory );
687 if( i_err || !cObject )
689 msg_Dbg( p_this, "class factory failure" );
690 FreeLibrary( *p_hmsdmo_dll );
693 i_err = cObject->vt->QueryInterface( cObject, &IID_IMediaObject,
695 cObject->vt->Release( (IUnknown*)cObject );
696 if( i_err || !*pp_dmo )
698 msg_Dbg( p_this, "QueryInterface failure" );
699 FreeLibrary( *p_hmsdmo_dll );
706 /*****************************************************************************
707 * DecoderClose: close codec
708 *****************************************************************************/
709 void DecoderClose( vlc_object_t *p_this )
711 decoder_t *p_dec = (decoder_t*)p_this;
712 decoder_sys_t *p_sys = p_dec->p_sys;
716 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
717 FreeLibrary( p_sys->hmsdmo_dll );
721 Restore_LDT_Keeper( p_sys->ldt_fs );
724 /* Uninitialize OLE/COM */
728 if( p_sys->p_buffer ) free( p_sys->p_buffer );
732 /****************************************************************************
733 * DecodeBlock: the whole thing
734 ****************************************************************************
735 * This function must be fed with ogg packets.
736 ****************************************************************************/
737 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
739 decoder_sys_t *p_sys = p_dec->p_sys;
743 DMO_OUTPUT_DATA_BUFFER db;
750 if( DecOpen( VLC_OBJECT(p_dec) ) )
752 msg_Err( p_dec, "DecOpen failed" );
755 p_sys = p_dec->p_sys;
758 if( !pp_block ) return NULL;
762 /* Won't work with streams with B-frames, but do we have any ? */
763 if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;
765 /* Date management */
766 if( p_block && p_block->i_pts > 0 &&
767 p_block->i_pts != date_Get( &p_sys->end_date ) )
769 date_Set( &p_sys->end_date, p_block->i_pts );
772 #if 0 /* Breaks the video decoding */
773 if( !date_Get( &p_sys->end_date ) )
775 /* We've just started the stream, wait for the first PTS. */
776 if( p_block ) block_Release( p_block );
781 /* Feed input to the DMO */
782 if( p_block && p_block->i_buffer )
786 p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );
788 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
789 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_SYNCPOINT,
792 p_in->vt->Release( (IUnknown *)p_in );
794 if( i_result == S_FALSE )
796 /* No output generated */
798 msg_Dbg( p_dec, "ProcessInput(): no output generated" );
802 else if( i_result == (int)DMO_E_NOTACCEPTING )
804 /* Need to call ProcessOutput */
805 msg_Dbg( p_dec, "ProcessInput(): not accepting" );
807 else if( i_result != S_OK )
809 msg_Dbg( p_dec, "ProcessInput(): failed" );
814 //msg_Dbg( p_dec, "ProcessInput(): successful" );
818 else if( p_block && !p_block->i_buffer )
820 block_Release( p_block );
824 /* Get output from the DMO */
825 block_out.p_buffer = p_sys->p_buffer;
826 block_out.i_buffer = 0;
828 p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );
829 memset( &db, 0, sizeof(db) );
830 db.pBuffer = (IMediaBuffer *)p_out;
832 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
833 DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,
836 if( i_result != S_OK )
838 if( i_result != S_FALSE )
839 msg_Dbg( p_dec, "ProcessOutput(): failed" );
842 msg_Dbg( p_dec, "ProcessOutput(): no output" );
845 p_out->vt->Release( (IUnknown *)p_out );
850 msg_Dbg( p_dec, "ProcessOutput(): success" );
853 if( !block_out.i_buffer )
856 msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );
858 p_out->vt->Release( (IUnknown *)p_out );
862 if( p_dec->fmt_out.i_cat == VIDEO_ES )
864 /* Get a new picture */
865 picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
866 if( !p_pic ) return NULL;
868 CopyPicture( p_dec, p_pic, block_out.p_buffer );
870 /* Date management */
871 p_pic->date = date_Get( &p_sys->end_date );
872 date_Increment( &p_sys->end_date, 1 );
874 p_out->vt->Release( (IUnknown *)p_out );
880 aout_buffer_t *p_aout_buffer;
881 int i_samples = block_out.i_buffer /
882 ( p_dec->fmt_out.audio.i_bitspersample *
883 p_dec->fmt_out.audio.i_channels / 8 );
885 p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
886 memcpy( p_aout_buffer->p_buffer,
887 block_out.p_buffer, block_out.i_buffer );
889 /* Date management */
890 p_aout_buffer->start_date = date_Get( &p_sys->end_date );
891 p_aout_buffer->end_date =
892 date_Increment( &p_sys->end_date, i_samples );
894 p_out->vt->Release( (IUnknown *)p_out );
896 return p_aout_buffer;
902 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )
904 int i_plane, i_line, i_width, i_dst_stride;
905 uint8_t *p_dst, *p_src = p_in;
907 p_dst = p_pic->p[1].p_pixels;
908 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
909 p_pic->p[2].p_pixels = p_dst;
911 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
913 p_dst = p_pic->p[i_plane].p_pixels;
914 i_width = p_pic->p[i_plane].i_visible_pitch;
915 i_dst_stride = p_pic->p[i_plane].i_pitch;
917 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
919 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
921 p_dst += i_dst_stride;
925 p_dst = p_pic->p[1].p_pixels;
926 p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;
927 p_pic->p[2].p_pixels = p_dst;
930 /****************************************************************************
931 * Encoder descriptor declaration
932 ****************************************************************************/
935 HINSTANCE hmsdmo_dll;
947 /*****************************************************************************
948 * EncoderOpen: open dmo codec
949 *****************************************************************************/
950 static int EncoderOpen( vlc_object_t *p_this )
952 encoder_t *p_enc = (encoder_t*)p_this;
955 int i_ret = EncOpen( p_this );
956 if( i_ret != VLC_SUCCESS ) return i_ret;
959 /* We can't open it now, because of ldt_keeper or something
960 * Open/Encode/Close has to be done in the same thread */
963 /* Probe if we support it */
964 for( i = 0; encoders_table[i].i_fourcc != 0; i++ )
966 if( encoders_table[i].i_fourcc == p_enc->fmt_out.i_codec )
968 msg_Dbg( p_enc, "DMO codec for %4.4s may work with dll=%s",
969 (char*)&p_enc->fmt_out.i_codec,
970 encoders_table[i].psz_dll );
976 if( !encoders_table[i].i_fourcc ) return VLC_EGENERIC;
980 p_enc->pf_encode_video = (block_t *(*)(encoder_t *, picture_t *))
982 p_enc->pf_encode_audio = (block_t *(*)(encoder_t *, aout_buffer_t *))
988 /*****************************************************************************
989 * EncoderSetVideoType: configures the input and output types of the dmo
990 *****************************************************************************/
991 static int EncoderSetVideoType( encoder_t *p_enc, IMediaObject *p_dmo )
993 int i, i_selected, i_err;
994 DMO_MEDIA_TYPE dmo_type;
995 VIDEOINFOHEADER vih, *p_vih;
996 BITMAPINFOHEADER *p_bih;
999 p_enc->fmt_in.video.i_bits_per_pixel =
1000 p_enc->fmt_out.video.i_bits_per_pixel = 12;
1002 /* Enumerate input format (for debug output) */
1004 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1006 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1008 msg_Dbg( p_enc, "available input chroma: %4.4s",
1009 (char *)&dmo_type.subtype.Data1 );
1010 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB565, 16 ) )
1011 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB565" );
1012 if( !memcmp( &dmo_type.subtype, &MEDIASUBTYPE_RGB24, 16 ) )
1013 msg_Dbg( p_enc, "-> MEDIASUBTYPE_RGB24" );
1015 DMOFreeMediaType( &dmo_type );
1018 /* Setup input format */
1019 memset( &dmo_type, 0, sizeof(dmo_type) );
1021 memset( &vih, 0, sizeof(VIDEOINFOHEADER) );
1023 p_bih = &vih.bmiHeader;
1024 p_bih->biCompression = VLC_FOURCC('I','4','2','0');
1025 p_bih->biWidth = p_enc->fmt_in.video.i_width;
1026 p_bih->biHeight = p_enc->fmt_in.video.i_height;
1027 p_bih->biBitCount = p_enc->fmt_in.video.i_bits_per_pixel;
1028 p_bih->biSizeImage = p_enc->fmt_in.video.i_width *
1029 p_enc->fmt_in.video.i_height * p_enc->fmt_in.video.i_bits_per_pixel /8;
1030 p_bih->biPlanes = 3;
1031 p_bih->biSize = sizeof(BITMAPINFOHEADER);
1033 vih.rcSource.left = vih.rcSource.top = 0;
1034 vih.rcSource.right = p_enc->fmt_in.video.i_width;
1035 vih.rcSource.bottom = p_enc->fmt_in.video.i_height;
1036 vih.rcTarget = vih.rcSource;
1038 vih.AvgTimePerFrame = I64C(10000000) / 25; //FIXME
1040 dmo_type.majortype = MEDIATYPE_Video;
1041 //dmo_type.subtype = MEDIASUBTYPE_RGB24;
1042 dmo_type.subtype = MEDIASUBTYPE_I420;
1043 //dmo_type.subtype.Data1 = p_bih->biCompression;
1044 dmo_type.formattype = FORMAT_VideoInfo;
1045 dmo_type.bFixedSizeSamples = TRUE;
1046 dmo_type.bTemporalCompression = FALSE;
1047 dmo_type.lSampleSize = p_bih->biSizeImage;
1048 dmo_type.cbFormat = sizeof(VIDEOINFOHEADER);
1049 dmo_type.pbFormat = (char *)&vih;
1051 if( ( i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 ) ) )
1053 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1054 return VLC_EGENERIC;
1057 msg_Dbg( p_enc, "successfully set input type" );
1059 /* Setup output format */
1060 memset( &dmo_type, 0, sizeof(dmo_type) );
1063 /* Enumerate output types */
1064 i = 0, i_selected = -1;
1065 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1067 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1069 msg_Dbg( p_enc, "available output codec: %4.4s",
1070 (char *)&dmo_type.subtype.Data1 );
1072 if( p_vih->bmiHeader.biCompression == p_enc->fmt_out.i_codec )
1075 DMOFreeMediaType( &dmo_type );
1078 if( i_selected < 0 )
1080 msg_Err( p_enc, "couldn't find codec: %4.4s",
1081 (char *)&p_enc->fmt_out.i_codec );
1082 return VLC_EGENERIC;
1085 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1086 ((VIDEOINFOHEADER *)dmo_type.pbFormat)->dwBitRate =
1087 p_enc->fmt_out.i_bitrate;
1089 /* Get the private data for the codec */
1092 IWMCodecPrivateData *p_privdata;
1093 VIDEOINFOHEADER *p_vih;
1094 uint8_t *p_data = 0;
1095 uint32_t i_data = 0, i_vih;
1097 i_err = p_dmo->vt->QueryInterface( (IUnknown *)p_dmo,
1098 &IID_IWMCodecPrivateData,
1099 (void **)&p_privdata );
1102 i_err = p_privdata->vt->SetPartialOutputType( p_privdata, &dmo_type );
1105 msg_Err( p_enc, "SetPartialOutputType() failed" );
1106 p_privdata->vt->Release( (IUnknown *)p_privdata );
1110 i_err = p_privdata->vt->GetPrivateData( p_privdata, NULL, &i_data );
1113 msg_Err( p_enc, "GetPrivateData() failed" );
1114 p_privdata->vt->Release( (IUnknown *)p_privdata );
1118 p_data = malloc( i_data );
1119 i_err = p_privdata->vt->GetPrivateData( p_privdata, p_data, &i_data );
1121 /* Update the media type with the private data */
1122 i_vih = dmo_type.cbFormat + i_data;
1123 p_vih = CoTaskMemAlloc( i_vih );
1124 memcpy( p_vih, dmo_type.pbFormat, dmo_type.cbFormat );
1125 memcpy( ((uint8_t *)p_vih) + dmo_type.cbFormat, p_data, i_data );
1126 DMOFreeMediaType( &dmo_type );
1127 dmo_type.pbFormat = (char*)p_vih;
1128 dmo_type.cbFormat = i_vih;
1130 msg_Dbg( p_enc, "found extra data: %i", i_data );
1131 p_enc->fmt_out.i_extra = i_data;
1132 p_enc->fmt_out.p_extra = p_data;
1136 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1138 p_vih = (VIDEOINFOHEADER *)dmo_type.pbFormat;
1139 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
1141 DMOFreeMediaType( &dmo_type );
1144 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1145 return VLC_EGENERIC;
1148 msg_Dbg( p_enc, "successfully set output type" );
1152 /*****************************************************************************
1153 * EncoderSetAudioType: configures the input and output types of the dmo
1154 *****************************************************************************/
1155 static int EncoderSetAudioType( encoder_t *p_enc, IMediaObject *p_dmo )
1157 int i, i_selected, i_err;
1158 unsigned int i_last_byterate;
1160 DMO_MEDIA_TYPE dmo_type;
1163 /* Setup the format structure */
1164 fourcc_to_wf_tag( p_enc->fmt_out.i_codec, &i_tag );
1165 if( i_tag == 0 ) return VLC_EGENERIC;
1167 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
1168 p_enc->fmt_in.audio.i_bitspersample = 16;
1170 /* We first need to choose an output type from the predefined
1171 * list of choices (we cycle through the list to select the best match) */
1172 i = 0; i_selected = -1; i_last_byterate = 0;
1173 while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &dmo_type ) )
1175 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1176 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1177 "bits per sample: %i, bitrate: %i, blockalign: %i",
1178 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1179 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1180 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1182 if( p_wf->wFormatTag == i_tag &&
1183 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1184 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1185 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1187 if( (int)p_wf->nAvgBytesPerSec <
1188 p_enc->fmt_out.i_bitrate * 110 / 800 /* + 10% */ &&
1189 p_wf->nAvgBytesPerSec > i_last_byterate )
1192 i_last_byterate = p_wf->nAvgBytesPerSec;
1193 msg_Dbg( p_enc, "selected entry %i (bitrate: %i)",
1194 i_selected, p_wf->nAvgBytesPerSec * 8 );
1198 DMOFreeMediaType( &dmo_type );
1201 if( i_selected < 0 )
1203 msg_Err( p_enc, "couldn't find a matching ouput" );
1204 return VLC_EGENERIC;
1207 p_dmo->vt->GetOutputType( p_dmo, 0, i_selected, &dmo_type );
1208 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1210 msg_Dbg( p_enc, "selected format: %i, sample rate:%i, "
1211 "channels: %i, bits per sample: %i, bitrate: %i, blockalign: %i",
1212 (int)p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1213 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1214 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1216 p_enc->fmt_out.audio.i_rate = p_wf->nSamplesPerSec;
1217 p_enc->fmt_out.audio.i_channels = p_wf->nChannels;
1218 p_enc->fmt_out.audio.i_bitspersample = p_wf->wBitsPerSample;
1219 p_enc->fmt_out.audio.i_blockalign = p_wf->nBlockAlign;
1220 p_enc->fmt_out.i_bitrate = p_wf->nAvgBytesPerSec * 8;
1224 msg_Dbg( p_enc, "found cbSize: %i", p_wf->cbSize );
1225 p_enc->fmt_out.i_extra = p_wf->cbSize;
1226 p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
1227 memcpy( p_enc->fmt_out.p_extra, &p_wf[1], p_enc->fmt_out.i_extra );
1230 i_err = p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_type, 0 );
1231 DMOFreeMediaType( &dmo_type );
1235 msg_Err( p_enc, "can't set DMO output type: %i", i_err );
1236 return VLC_EGENERIC;
1239 msg_Dbg( p_enc, "successfully set output type" );
1241 /* Setup the input type */
1242 i = 0; i_selected = -1;
1243 while( !p_dmo->vt->GetInputType( p_dmo, 0, i++, &dmo_type ) )
1245 p_wf = (WAVEFORMATEX *)dmo_type.pbFormat;
1246 msg_Dbg( p_enc, "available format :%i, sample rate: %i, channels: %i, "
1247 "bits per sample: %i, bitrate: %i, blockalign: %i",
1248 (int) p_wf->wFormatTag, (int)p_wf->nSamplesPerSec,
1249 (int)p_wf->nChannels, (int)p_wf->wBitsPerSample,
1250 (int)p_wf->nAvgBytesPerSec * 8, (int)p_wf->nBlockAlign );
1252 if( p_wf->wFormatTag == WAVE_FORMAT_PCM &&
1253 p_wf->nSamplesPerSec == p_enc->fmt_in.audio.i_rate &&
1254 p_wf->nChannels == p_enc->fmt_in.audio.i_channels &&
1255 p_wf->wBitsPerSample == p_enc->fmt_in.audio.i_bitspersample )
1260 DMOFreeMediaType( &dmo_type );
1263 if( i_selected < 0 )
1265 msg_Err( p_enc, "couldn't find a matching input" );
1266 return VLC_EGENERIC;
1269 p_dmo->vt->GetInputType( p_dmo, 0, i_selected, &dmo_type );
1270 i_err = p_dmo->vt->SetInputType( p_dmo, 0, &dmo_type, 0 );
1271 DMOFreeMediaType( &dmo_type );
1274 msg_Err( p_enc, "can't set DMO input type: %x", i_err );
1275 return VLC_EGENERIC;
1277 msg_Dbg( p_enc, "successfully set input type" );
1282 /*****************************************************************************
1283 * EncOpen: open dmo codec
1284 *****************************************************************************/
1285 static int EncOpen( vlc_object_t *p_this )
1287 encoder_t *p_enc = (encoder_t*)p_this;
1288 encoder_sys_t *p_sys = NULL;
1289 IMediaObject *p_dmo = NULL;
1290 HINSTANCE hmsdmo_dll = NULL;
1293 ldt_fs_t *ldt_fs = Setup_LDT_Keeper();
1295 /* Initialize OLE/COM */
1299 if( LoadDMO( p_this, &hmsdmo_dll, &p_dmo, &p_enc->fmt_out, VLC_TRUE )
1307 if( p_enc->fmt_in.i_cat == VIDEO_ES )
1309 if( EncoderSetVideoType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1313 if( EncoderSetAudioType( p_enc, p_dmo ) != VLC_SUCCESS ) goto error;
1316 /* Allocate the memory needed to store the decoder's structure */
1317 if( ( p_enc->p_sys = p_sys =
1318 (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1320 msg_Err( p_enc, "out of memory" );
1324 p_sys->hmsdmo_dll = hmsdmo_dll;
1325 p_sys->p_dmo = p_dmo;
1327 p_sys->ldt_fs = ldt_fs;
1330 /* Find out some properties of the inputput */
1332 uint32_t i_size, i_align, dum;
1334 if( p_dmo->vt->GetInputSizeInfo( p_dmo, 0, &i_size, &i_align, &dum ) )
1335 msg_Err( p_enc, "GetInputSizeInfo() failed" );
1337 msg_Dbg( p_enc, "GetInputSizeInfo(): bytes %i, align %i, %i",
1338 i_size, i_align, dum );
1341 /* Find out some properties of the output */
1343 uint32_t i_size, i_align;
1345 p_sys->i_min_output = 0;
1346 if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )
1348 msg_Err( p_enc, "GetOutputSizeInfo() failed" );
1353 msg_Dbg( p_enc, "GetOutputSizeInfo(): bytes %i, align %i",
1355 p_sys->i_min_output = i_size;
1359 /* Set output properties */
1360 p_enc->fmt_out.i_cat = p_enc->fmt_out.i_cat;
1361 if( p_enc->fmt_out.i_cat == AUDIO_ES )
1362 date_Init( &p_sys->end_date, p_enc->fmt_out.audio.i_rate, 1 );
1364 date_Init( &p_sys->end_date, 25 /* FIXME */, 1 );
1370 if( p_dmo ) p_dmo->vt->Release( (IUnknown *)p_dmo );
1371 if( hmsdmo_dll ) FreeLibrary( hmsdmo_dll );
1374 Restore_LDT_Keeper( ldt_fs );
1376 /* Uninitialize OLE/COM */
1380 if( p_sys ) free( p_sys );
1382 return VLC_EGENERIC;
1385 /****************************************************************************
1386 * Encode: the whole thing
1387 ****************************************************************************/
1388 static block_t *EncodeBlock( encoder_t *p_enc, void *p_data )
1390 encoder_sys_t *p_sys = p_enc->p_sys;
1392 block_t *p_chain = NULL;
1393 block_t *p_block_in;
1400 if( EncOpen( VLC_OBJECT(p_enc) ) )
1402 msg_Err( p_enc, "EncOpen failed" );
1405 p_sys = p_enc->p_sys;
1408 if( !p_data ) return NULL;
1410 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1412 /* Get picture data */
1413 int i_plane, i_line, i_width, i_src_stride;
1414 picture_t *p_pic = (picture_t *)p_data;
1417 int i_buffer = p_enc->fmt_in.video.i_width *
1418 p_enc->fmt_in.video.i_height *
1419 p_enc->fmt_in.video.i_bits_per_pixel / 8;
1421 p_block_in = block_New( p_enc, i_buffer );
1423 /* Copy picture stride by stride */
1424 p_dst = p_block_in->p_buffer;
1425 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1427 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
1428 i_width = p_pic->p[i_plane].i_visible_pitch;
1429 i_src_stride = p_pic->p[i_plane].i_pitch;
1431 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
1434 p_enc->p_vlc->pf_memcpy( p_dst, p_src, i_width );
1436 p_src += i_src_stride;
1440 i_pts = p_pic->date;
1444 aout_buffer_t *p_aout_buffer = (aout_buffer_t *)p_data;
1445 p_block_in = block_New( p_enc, p_aout_buffer->i_nb_bytes );
1446 memcpy( p_block_in->p_buffer, p_aout_buffer->p_buffer,
1447 p_block_in->i_buffer );
1449 i_pts = p_aout_buffer->start_date;
1452 /* Feed input to the DMO */
1453 p_in = CMediaBufferCreate( p_block_in, p_block_in->i_buffer, VLC_TRUE );
1454 i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,
1455 (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFERF_TIME, i_pts * 10, 0 );
1457 p_in->vt->Release( (IUnknown *)p_in );
1458 if( i_result == S_FALSE )
1460 /* No output generated */
1462 msg_Dbg( p_enc, "ProcessInput(): no output generated "I64Fd, i_pts );
1466 else if( i_result == (int)DMO_E_NOTACCEPTING )
1468 /* Need to call ProcessOutput */
1469 msg_Dbg( p_enc, "ProcessInput(): not accepting" );
1471 else if( i_result != S_OK )
1473 msg_Dbg( p_enc, "ProcessInput(): failed: %x", i_result );
1478 msg_Dbg( p_enc, "ProcessInput(): success" );
1481 /* Get output from the DMO */
1484 DMO_OUTPUT_DATA_BUFFER db;
1485 block_t *p_block_out;
1486 CMediaBuffer *p_out;
1488 p_block_out = block_New( p_enc, p_sys->i_min_output );
1489 p_block_out->i_buffer = 0;
1490 p_out = CMediaBufferCreate(p_block_out, p_sys->i_min_output, VLC_FALSE);
1491 memset( &db, 0, sizeof(db) );
1492 db.pBuffer = (IMediaBuffer *)p_out;
1494 i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,
1495 0, 1, &db, &i_status );
1497 if( i_result != S_OK )
1499 if( i_result != S_FALSE )
1500 msg_Dbg( p_enc, "ProcessOutput(): failed: %x", i_result );
1503 msg_Dbg( p_enc, "ProcessOutput(): no output" );
1506 p_out->vt->Release( (IUnknown *)p_out );
1507 block_Release( p_block_out );
1511 if( !p_block_out->i_buffer )
1514 msg_Dbg( p_enc, "ProcessOutput(): no output (i_buffer_out == 0)" );
1516 p_out->vt->Release( (IUnknown *)p_out );
1517 block_Release( p_block_out );
1521 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIME )
1524 msg_Dbg( p_enc, "ProcessOutput(): pts: "I64Fd", "I64Fd,
1525 i_pts, db.rtTimestamp / 10 );
1527 i_pts = db.rtTimestamp / 10;
1530 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_TIMELENGTH )
1532 p_block_out->i_length = db.rtTimelength / 10;
1534 msg_Dbg( p_enc, "ProcessOutput(): length: "I64Fd,
1535 p_block_out->i_length );
1539 if( p_enc->fmt_out.i_cat == VIDEO_ES )
1541 if( db.dwStatus & DMO_OUTPUT_DATA_BUFFERF_SYNCPOINT )
1542 p_block_out->i_flags |= BLOCK_FLAG_TYPE_I;
1544 p_block_out->i_flags |= BLOCK_FLAG_TYPE_P;
1547 p_block_out->i_dts = p_block_out->i_pts = i_pts;
1548 block_ChainAppend( &p_chain, p_block_out );
1552 /*****************************************************************************
1553 * EncoderClose: close codec
1554 *****************************************************************************/
1555 void EncoderClose( vlc_object_t *p_this )
1557 encoder_t *p_enc = (encoder_t*)p_this;
1558 encoder_sys_t *p_sys = p_enc->p_sys;
1560 if( !p_sys ) return;
1562 if( p_sys->p_dmo ) p_sys->p_dmo->vt->Release( (IUnknown *)p_sys->p_dmo );
1563 FreeLibrary( p_sys->hmsdmo_dll );
1567 Restore_LDT_Keeper( p_sys->ldt_fs );
1570 /* Uninitialize OLE/COM */