]> git.sesse.net Git - vlc/blob - modules/codec/dmo/dmo.c
* modules/codec/dmo: "DirectX Media Object" decoder plugin (win32 only).
[vlc] / modules / codec / dmo / dmo.c
1 /*****************************************************************************\r
2  * dmo.c : DirectMedia Object decoder module for vlc\r
3  *****************************************************************************\r
4  * Copyright (C) 2002, 2003 VideoLAN\r
5  * $Id$\r
6  *\r
7  * Author: Gildas Bazin <gbazin@videolan.org>\r
8  *\r
9  * This program is free software; you can redistribute it and/or modify\r
10  * it under the terms of the GNU General Public License as published by\r
11  * the Free Software Foundation; either version 2 of the License, or\r
12  * (at your option) any later version.\r
13  *\r
14  * This program is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU General Public License for more details.\r
18  *\r
19  * You should have received a copy of the GNU General Public License\r
20  * along with this program; if not, write to the Free Software\r
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.\r
22  *****************************************************************************/\r
23 \r
24 /*****************************************************************************\r
25  * Preamble\r
26  *****************************************************************************/\r
27 #include <stdlib.h>\r
28 #include <stdio.h>\r
29 #include <string.h>\r
30 \r
31 #include <vlc/vlc.h>\r
32 #include <vlc/decoder.h>\r
33 #include <vlc/vout.h>\r
34 \r
35 #include <objbase.h>\r
36 #include "codecs.h"\r
37 #include "dmo.h"\r
38 \r
39 static int pi_channels_maps[7] =\r
40 {\r
41     0,\r
42     AOUT_CHAN_CENTER,\r
43     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,\r
44     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,\r
45     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT\r
46      | AOUT_CHAN_REARRIGHT,\r
47     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER\r
48      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,\r
49     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER\r
50      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE\r
51 };\r
52 \r
53 /*****************************************************************************\r
54  * Module descriptor\r
55  *****************************************************************************/\r
56 static int  DecoderOpen  ( vlc_object_t * );\r
57 static void DecoderClose ( vlc_object_t * );\r
58 static void *DecodeBlock ( decoder_t *, block_t ** );\r
59 \r
60 static void CopyPicture( decoder_t *, picture_t *, uint8_t * );\r
61 \r
62 vlc_module_begin();\r
63     set_description( _("DirectMedia Object decoder") );\r
64     add_shortcut( "dmo" );\r
65     set_capability( "decoder", 1 );\r
66     set_callbacks( DecoderOpen, DecoderClose );\r
67 vlc_module_end();\r
68 \r
69 /*****************************************************************************\r
70  * Local prototypes\r
71  *****************************************************************************/\r
72 \r
73 /****************************************************************************\r
74  * Decoder descriptor declaration\r
75  ****************************************************************************/\r
76 struct decoder_sys_t\r
77 {\r
78     HINSTANCE hmsdmo_dll;\r
79     IMediaObject *p_dmo;\r
80 \r
81     int i_min_output;\r
82 \r
83     audio_date_t end_date;\r
84 };\r
85 \r
86 /*****************************************************************************\r
87  * DecoderOpen: open dmo codec\r
88  *****************************************************************************/\r
89 static int DecoderOpen( vlc_object_t *p_this )\r
90 {\r
91     decoder_t *p_dec = (decoder_t*)p_this;\r
92     decoder_sys_t *p_sys = NULL;\r
93 \r
94     DMO_PARTIAL_MEDIATYPE dmo_partial_type;\r
95     DMO_MEDIA_TYPE dmo_input_type, dmo_output_type;\r
96     IEnumDMO *p_enum_dmo = NULL; \r
97     IMediaObject *p_dmo = NULL;\r
98     WCHAR *psz_dmo_name;\r
99     GUID clsid_dmo;\r
100 \r
101     VIDEOINFOHEADER *p_vih = NULL;\r
102     WAVEFORMATEX *p_wf = NULL;\r
103 \r
104     HRESULT (STDCALL *OurDMOEnum)( const GUID *, uint32_t, uint32_t,\r
105                            const DMO_PARTIAL_MEDIATYPE *,\r
106                            uint32_t, const DMO_PARTIAL_MEDIATYPE *,\r
107                            IEnumDMO ** );\r
108 \r
109     /* Load msdmo DLL */\r
110     HINSTANCE hmsdmo_dll = LoadLibrary( "msdmo.dll" );\r
111     if( hmsdmo_dll == NULL )\r
112     {\r
113         msg_Dbg( p_dec, "failed loading msdmo.dll" );\r
114         return VLC_EGENERIC;\r
115     }\r
116     OurDMOEnum = (void *)GetProcAddress( hmsdmo_dll, "DMOEnum" );\r
117     if( OurDMOEnum == NULL )\r
118     {\r
119         msg_Dbg( p_dec, "GetProcAddress failed to find DMOEnum()" );\r
120         FreeLibrary( hmsdmo_dll );\r
121         return VLC_EGENERIC;\r
122     }\r
123 \r
124     /* Look for a DMO which can handle the requested codec */\r
125     if( p_dec->fmt_in.i_cat == AUDIO_ES )\r
126     {\r
127         uint16_t i_tag;\r
128         dmo_partial_type.type = MEDIATYPE_Audio;\r
129         dmo_partial_type.subtype = dmo_partial_type.type;\r
130         dmo_partial_type.subtype.Data1 = p_dec->fmt_in.i_codec;\r
131         fourcc_to_wf_tag( p_dec->fmt_in.i_codec, &i_tag );\r
132         if( i_tag ) dmo_partial_type.subtype.Data1 = i_tag;\r
133     }\r
134     else\r
135     {\r
136         dmo_partial_type.type = MEDIATYPE_Video;\r
137         dmo_partial_type.subtype = dmo_partial_type.type;\r
138         dmo_partial_type.subtype.Data1 = p_dec->fmt_in.i_codec;\r
139     }\r
140 \r
141     /* Initialize OLE/COM */\r
142     CoInitialize( 0 );\r
143 \r
144     if( OurDMOEnum( &GUID_NULL, 1 /*DMO_ENUMF_INCLUDE_KEYED*/,\r
145                     1, &dmo_partial_type, 0, NULL, &p_enum_dmo ) )\r
146     {\r
147         goto error;\r
148     }\r
149 \r
150     /* Pickup the first available codec */\r
151     if( p_enum_dmo->vt->Next( p_enum_dmo, 1, &clsid_dmo,\r
152                               &psz_dmo_name, NULL ) )\r
153     {\r
154         goto error;\r
155     }\r
156     p_enum_dmo->vt->Release( (IUnknown *)p_enum_dmo );\r
157 \r
158 #if 1\r
159     {\r
160         char psz_temp[MAX_PATH];\r
161         wcstombs( psz_temp, psz_dmo_name, MAX_PATH );\r
162         msg_Dbg( p_dec, "found DMO: %s", psz_temp );\r
163     }\r
164 #endif\r
165 \r
166     CoTaskMemFree( psz_dmo_name );\r
167 \r
168     /* Create DMO */\r
169     if( CoCreateInstance( &clsid_dmo, NULL, CLSCTX_INPROC,\r
170                           &IID_IMediaObject, (void **)&p_dmo ) )\r
171     {\r
172         msg_Err( p_dec, "can't create DMO" );\r
173         goto error;\r
174     }\r
175 \r
176     /* Setup input format */\r
177     memset( &dmo_input_type, 0, sizeof(dmo_input_type) );\r
178     dmo_input_type.majortype = dmo_partial_type.type;\r
179     dmo_input_type.subtype = dmo_partial_type.subtype;\r
180     dmo_input_type.pUnk = 0;\r
181 \r
182     if( p_dec->fmt_in.i_cat == AUDIO_ES )\r
183     {\r
184         int i_size = sizeof(WAVEFORMATEX) + p_dec->fmt_in.i_extra;\r
185         p_wf = malloc( i_size );\r
186 \r
187         memset( p_wf, 0, sizeof(WAVEFORMATEX) );\r
188         if( p_dec->fmt_in.i_extra )\r
189             memcpy( &p_wf[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );\r
190 \r
191         p_wf->wFormatTag = dmo_partial_type.subtype.Data1;\r
192         p_wf->nSamplesPerSec = p_dec->fmt_in.audio.i_rate;\r
193         p_wf->nChannels = p_dec->fmt_in.audio.i_channels;\r
194         p_wf->wBitsPerSample = p_dec->fmt_in.audio.i_bitspersample;\r
195         p_wf->nBlockAlign = p_dec->fmt_in.audio.i_blockalign;\r
196         p_wf->nAvgBytesPerSec = p_dec->fmt_in.i_bitrate / 8;\r
197         p_wf->cbSize = i_size;\r
198 \r
199         dmo_input_type.formattype = FORMAT_WaveFormatEx;\r
200         dmo_input_type.cbFormat   = i_size;\r
201         dmo_input_type.pbFormat   = (char *)p_wf;\r
202         dmo_input_type.pUnk       = NULL;\r
203         dmo_input_type.bFixedSizeSamples = 1;\r
204         dmo_input_type.bTemporalCompression = 0;\r
205         dmo_input_type.lSampleSize = p_wf->nBlockAlign;\r
206     }\r
207     else\r
208     {\r
209         BITMAPINFOHEADER *p_bih;\r
210 \r
211         int i_size = sizeof(VIDEOINFOHEADER) + p_dec->fmt_in.i_extra;\r
212         p_vih = malloc( i_size );\r
213 \r
214         memset( p_vih, 0, sizeof(VIDEOINFOHEADER) );\r
215         if( p_dec->fmt_in.i_extra )\r
216             memcpy( &p_vih[1], p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );\r
217 \r
218         p_bih = &p_vih->bmiHeader;\r
219         p_bih->biCompression = dmo_partial_type.subtype.Data1;\r
220         p_bih->biWidth = p_dec->fmt_in.video.i_width;\r
221         p_bih->biHeight = p_dec->fmt_in.video.i_height;\r
222         p_bih->biBitCount = p_dec->fmt_in.video.i_bits_per_pixel;\r
223         p_bih->biPlanes = 1;\r
224         p_bih->biSize = i_size - sizeof(VIDEOINFOHEADER) +\r
225             sizeof(BITMAPINFOHEADER);\r
226 \r
227         p_vih->rcSource.left = p_vih->rcSource.top = 0;\r
228         p_vih->rcSource.right = p_dec->fmt_in.video.i_width;\r
229         p_vih->rcSource.bottom = p_dec->fmt_in.video.i_height;\r
230         p_vih->rcTarget = p_vih->rcSource;\r
231 \r
232         dmo_input_type.formattype = MEDIASUBTYPE_VideoInfo;\r
233         dmo_input_type.bFixedSizeSamples = 0;\r
234         dmo_input_type.bTemporalCompression = 1;\r
235         dmo_input_type.cbFormat = i_size;\r
236         dmo_input_type.pbFormat = (char *)p_vih;\r
237     }\r
238 \r
239     if( p_dmo->vt->SetInputType( p_dmo, 0, &dmo_input_type, 0 ) )\r
240     {\r
241         msg_Err( p_dec, "can't set DMO input type" );\r
242         goto error;\r
243     }\r
244     msg_Dbg( p_dec, "DMO input type set" );\r
245 \r
246     /* Setup output format */\r
247     memset( &dmo_output_type, 0, sizeof(dmo_output_type) );\r
248     dmo_output_type.majortype = dmo_partial_type.type;\r
249     dmo_output_type.pUnk = 0;\r
250 \r
251     if( p_dec->fmt_in.i_cat == AUDIO_ES )\r
252     {\r
253         /* Setup the format */\r
254         p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;\r
255         p_dec->fmt_out.audio.i_rate     = p_dec->fmt_in.audio.i_rate;\r
256         p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;\r
257         p_dec->fmt_out.audio.i_bitspersample =\r
258             p_dec->fmt_in.audio.i_bitspersample;\r
259         p_dec->fmt_out.audio.i_physical_channels =\r
260             p_dec->fmt_out.audio.i_original_channels =\r
261                 pi_channels_maps[p_dec->fmt_out.audio.i_channels];\r
262 \r
263         p_wf->wFormatTag = WAVE_FORMAT_PCM;\r
264         p_wf->nSamplesPerSec = p_dec->fmt_out.audio.i_rate;\r
265         p_wf->nChannels = p_dec->fmt_out.audio.i_channels;\r
266         p_wf->wBitsPerSample = p_dec->fmt_out.audio.i_bitspersample;\r
267         p_wf->nBlockAlign =\r
268             p_wf->wBitsPerSample / 8 * p_wf->nChannels;\r
269         p_wf->nAvgBytesPerSec =\r
270             p_wf->nSamplesPerSec * p_wf->nBlockAlign;\r
271         p_wf->cbSize = sizeof(WAVEFORMATEX);\r
272 \r
273         dmo_output_type.formattype = FORMAT_WaveFormatEx;\r
274         dmo_output_type.subtype    = MEDIASUBTYPE_PCM;\r
275         dmo_output_type.cbFormat   = sizeof(WAVEFORMATEX);\r
276         dmo_output_type.pbFormat   = (char *)p_wf;\r
277         dmo_output_type.bFixedSizeSamples = 1;\r
278         dmo_output_type.bTemporalCompression = 0;\r
279         dmo_output_type.lSampleSize = p_wf->nBlockAlign;\r
280         dmo_output_type.pUnk = NULL;\r
281     }\r
282     else\r
283     {\r
284         BITMAPINFOHEADER *p_bih;\r
285 \r
286         p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');\r
287         p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;\r
288         p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;\r
289         p_dec->fmt_out.video.i_bits_per_pixel = 12;\r
290         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *\r
291             p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;\r
292 \r
293         dmo_output_type.formattype = MEDIASUBTYPE_VideoInfo;\r
294         dmo_output_type.subtype = MEDIASUBTYPE_YV12;\r
295 \r
296         p_bih = &p_vih->bmiHeader;\r
297         p_bih->biCompression = dmo_partial_type.subtype.Data1;\r
298         p_bih->biHeight *= -1;\r
299         p_bih->biBitCount = p_dec->fmt_out.video.i_bits_per_pixel;\r
300         p_bih->biSizeImage = p_dec->fmt_in.video.i_width *\r
301             p_dec->fmt_in.video.i_height *\r
302             (p_dec->fmt_in.video.i_bits_per_pixel + 7) / 8;\r
303 \r
304         //p_bih->biPlanes = 1;\r
305         p_bih->biSize = sizeof(BITMAPINFOHEADER);\r
306 \r
307         dmo_output_type.bFixedSizeSamples = VLC_TRUE;\r
308         dmo_output_type.bTemporalCompression = 0;\r
309         dmo_output_type.lSampleSize = p_bih->biSizeImage;\r
310         dmo_output_type.cbFormat = sizeof(VIDEOINFOHEADER);\r
311         dmo_output_type.pbFormat = (char *)p_vih;\r
312     }\r
313 \r
314     /* Enumerate output types */\r
315     if( p_dec->fmt_in.i_cat == VIDEO_ES )\r
316     {\r
317         int i = 0;\r
318         DMO_MEDIA_TYPE mt;\r
319 \r
320         while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )\r
321         {\r
322             msg_Dbg( p_dec, "available output chroma: %4.4s",\r
323                      (char *)&mt.subtype.Data1 );\r
324         }\r
325     }\r
326 \r
327     /* Choose an output type.\r
328      * FIXME, get rid of the dmo_output_type code above. */\r
329     if( p_dec->fmt_in.i_cat == VIDEO_ES )\r
330     {\r
331         int i = 0;\r
332         DMO_MEDIA_TYPE mt;\r
333 \r
334         while( !p_dmo->vt->GetOutputType( p_dmo, 0, i++, &mt ) )\r
335         {\r
336             if( dmo_output_type.subtype.Data1 == mt.subtype.Data1 )\r
337             {\r
338                 *p_vih = *(VIDEOINFOHEADER *)mt.pbFormat;\r
339                 break;\r
340             }\r
341         }\r
342     }\r
343 \r
344     if( p_dmo->vt->SetOutputType( p_dmo, 0, &dmo_output_type, 0 ) )\r
345     {\r
346         msg_Err( p_dec, "can't set DMO output type" );\r
347         goto error;\r
348     }\r
349     msg_Dbg( p_dec, "DMO output type set" );\r
350 \r
351     /* Allocate the memory needed to store the decoder's structure */\r
352     if( ( p_dec->p_sys = p_sys =\r
353           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )\r
354     {\r
355         msg_Err( p_dec, "out of memory" );\r
356         goto error;\r
357     }\r
358 \r
359     p_sys->hmsdmo_dll = hmsdmo_dll;\r
360     p_sys->p_dmo = p_dmo;\r
361 \r
362     /* Find out some properties of the output */\r
363     {\r
364         uint32_t i_size, i_align;\r
365 \r
366         p_sys->i_min_output = 0;\r
367         if( p_dmo->vt->GetOutputSizeInfo( p_dmo, 0, &i_size, &i_align ) )\r
368         {\r
369             msg_Err( p_dec, "GetOutputSizeInfo() failed" );\r
370             goto error;\r
371         }\r
372         else\r
373         {\r
374             msg_Dbg( p_dec, "GetOutputSizeInfo(): bytes %i, align %i",\r
375                      i_size, i_align );\r
376             p_sys->i_min_output = i_size;\r
377         }\r
378     }\r
379 \r
380     /* Set output properties */\r
381     p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;\r
382     if( p_dec->fmt_out.i_cat == AUDIO_ES )\r
383         aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );\r
384     else\r
385         aout_DateInit( &p_sys->end_date, 25 /* FIXME */ );\r
386     aout_DateSet( &p_sys->end_date, 0 );\r
387 \r
388     /* Set callbacks */\r
389     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))\r
390         DecodeBlock;\r
391     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))\r
392         DecodeBlock;\r
393 \r
394     if( p_vih ) free( p_vih );\r
395     if( p_wf ) free( p_wf );\r
396 \r
397     return VLC_SUCCESS;\r
398 \r
399  error:\r
400     /* Uninitialize OLE/COM */\r
401     CoUninitialize();\r
402     FreeLibrary( hmsdmo_dll );\r
403 \r
404     if( p_vih ) free( p_vih );\r
405     if( p_wf )  free( p_wf );\r
406     if( p_sys ) free( p_sys );\r
407 \r
408     return VLC_EGENERIC;\r
409 }\r
410 \r
411 /*****************************************************************************\r
412  * DecoderClose: close codec\r
413  *****************************************************************************/\r
414 void DecoderClose( vlc_object_t *p_this )\r
415 {\r
416     decoder_t *p_dec = (decoder_t*)p_this;\r
417     decoder_sys_t *p_sys = p_dec->p_sys;\r
418 \r
419     /* Uninitialize OLE/COM */\r
420     CoUninitialize();\r
421 \r
422     FreeLibrary( p_sys->hmsdmo_dll );\r
423 \r
424     free( p_sys );\r
425 }\r
426 \r
427 /****************************************************************************\r
428  * DecodeBlock: the whole thing\r
429  ****************************************************************************\r
430  * This function must be fed with ogg packets.\r
431  ****************************************************************************/\r
432 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )\r
433 {\r
434     decoder_sys_t *p_sys = p_dec->p_sys;\r
435     block_t *p_block;\r
436     int i_result;\r
437 \r
438     DMO_OUTPUT_DATA_BUFFER db;\r
439     CMediaBuffer *p_out;\r
440     block_t block_out;\r
441     uint32_t i_status, i_buffer_out;\r
442     uint8_t *p_buffer_out;\r
443 \r
444     if( !pp_block ) return NULL;\r
445 \r
446     p_block = *pp_block;\r
447 \r
448     /* Won't work with streams with B-frames, but do we have any ? */\r
449     if( p_block && p_block->i_pts <= 0 ) p_block->i_pts = p_block->i_dts;\r
450 \r
451     /* Date management */\r
452     if( p_block && p_block->i_pts > 0 &&\r
453         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )\r
454     {\r
455         aout_DateSet( &p_sys->end_date, p_block->i_pts );\r
456     }\r
457 \r
458 #if 0 /* Breaks the video decoding */\r
459     if( !aout_DateGet( &p_sys->end_date ) )\r
460     {\r
461         /* We've just started the stream, wait for the first PTS. */\r
462         if( p_block ) block_Release( p_block );\r
463         return NULL;\r
464     }\r
465 #endif\r
466 \r
467     /* Feed input to the DMO */\r
468     if( p_block && p_block->i_buffer )\r
469     {\r
470         CMediaBuffer *p_in;\r
471 \r
472         p_in = CMediaBufferCreate( p_block, p_block->i_buffer, VLC_TRUE );\r
473 \r
474         i_result = p_sys->p_dmo->vt->ProcessInput( p_sys->p_dmo, 0,\r
475                        (IMediaBuffer *)p_in, DMO_INPUT_DATA_BUFFER_SYNCPOINT,\r
476                        0, 0 );\r
477 \r
478         p_in->vt->Release( (IUnknown *)p_in );\r
479 \r
480         if( i_result == S_FALSE )\r
481         {\r
482             /* No output generated */\r
483 #ifdef DMO_DEBUG\r
484             msg_Dbg( p_dec, "ProcessInput(): no output generated" );\r
485 #endif\r
486             return NULL;\r
487         }\r
488         else if( i_result == DMO_E_NOTACCEPTING )\r
489         {\r
490             /* Need to call ProcessOutput */\r
491             msg_Dbg( p_dec, "ProcessInput(): not accepting" );\r
492         }\r
493         else if( i_result != S_OK )\r
494         {\r
495             msg_Dbg( p_dec, "ProcessInput(): failed" );\r
496             return NULL;\r
497         }\r
498         else\r
499         {\r
500             //msg_Dbg( p_dec, "ProcessInput(): successful" );\r
501             *pp_block = 0;\r
502         }\r
503     }\r
504     else if( p_block && !p_block->i_buffer )\r
505     {\r
506         block_Release( p_block );\r
507         *pp_block = 0;\r
508     }\r
509 \r
510     /* Get output from the DMO */\r
511     block_out.p_buffer = malloc( p_sys->i_min_output );\r
512     block_out.i_buffer = 0;\r
513 \r
514     p_out = CMediaBufferCreate( &block_out, p_sys->i_min_output, VLC_FALSE );\r
515     db.rtTimestamp = 0;\r
516     db.rtTimelength = 0;\r
517     db.dwStatus = 0;\r
518     db.pBuffer = (IMediaBuffer *)p_out;\r
519 \r
520     i_result = p_sys->p_dmo->vt->ProcessOutput( p_sys->p_dmo,\r
521                    DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER,\r
522                    1, &db, &i_status );\r
523 \r
524     if( i_result != S_OK )\r
525     {\r
526         if( i_result != S_FALSE )\r
527             msg_Dbg( p_dec, "ProcessOutput(): failed" );\r
528 #if DMO_DEBUG\r
529         else\r
530             msg_Dbg( p_dec, "ProcessOutput(): no output" );\r
531 #endif\r
532 \r
533         p_out->vt->Release( (IUnknown *)p_out );\r
534         free( block_out.p_buffer );\r
535         return NULL;\r
536     }\r
537 \r
538 #if DMO_DEBUG\r
539     msg_Dbg( p_dec, "ProcessOutput(): success" );\r
540 #endif\r
541 \r
542     i_result = p_out->vt->GetBufferAndLength( (IMediaBuffer *)p_out,\r
543                                               &p_buffer_out, &i_buffer_out );\r
544     if( i_result != S_OK )\r
545     {\r
546         msg_Dbg( p_dec, "GetBufferAndLength(): failed" );\r
547         p_out->vt->Release( (IUnknown *)p_out );\r
548         free( block_out.p_buffer );\r
549         return NULL;\r
550     }\r
551 \r
552     if( !i_buffer_out )\r
553     {\r
554 #if DMO_DEBUG\r
555         msg_Dbg( p_dec, "ProcessOutput(): no output (i_buffer_out == 0)" );\r
556 #endif\r
557         p_out->vt->Release( (IUnknown *)p_out );\r
558         free( block_out.p_buffer );\r
559         return NULL;\r
560     }\r
561 \r
562     if( p_dec->fmt_out.i_cat == VIDEO_ES )\r
563     {\r
564         /* Get a new picture */\r
565         picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );\r
566         if( !p_pic ) return NULL;\r
567 \r
568         CopyPicture( p_dec, p_pic, block_out.p_buffer );\r
569 \r
570         /* Date management */\r
571         p_pic->date = aout_DateGet( &p_sys->end_date );\r
572         aout_DateIncrement( &p_sys->end_date, 1 );\r
573 \r
574         p_out->vt->Release( (IUnknown *)p_out );\r
575         free( block_out.p_buffer );\r
576 \r
577         return p_pic;\r
578     }\r
579     else\r
580     {\r
581         aout_buffer_t *p_aout_buffer;\r
582         int i_samples = i_buffer_out /\r
583             ( p_dec->fmt_in.audio.i_bitspersample *\r
584               p_dec->fmt_in.audio.i_channels / 8 );\r
585 \r
586         p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );\r
587         memcpy( p_aout_buffer->p_buffer, p_buffer_out, i_buffer_out );\r
588 \r
589         /* Date management */\r
590         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );\r
591         p_aout_buffer->end_date =\r
592             aout_DateIncrement( &p_sys->end_date, i_samples );\r
593 \r
594         p_out->vt->Release( (IUnknown *)p_out );\r
595         free( block_out.p_buffer );\r
596 \r
597         return p_aout_buffer;\r
598     }\r
599 \r
600     return NULL;\r
601 }\r
602 \r
603 static void CopyPicture( decoder_t *p_dec, picture_t *p_pic, uint8_t *p_in )\r
604 {\r
605     int i_plane, i_line, i_width, i_dst_stride;\r
606     uint8_t *p_dst, *p_src = p_in;\r
607 \r
608     p_dst = p_pic->p[1].p_pixels;\r
609     p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;\r
610     p_pic->p[2].p_pixels = p_dst;\r
611 \r
612     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )\r
613     {\r
614         p_dst = p_pic->p[i_plane].p_pixels;\r
615         i_width = p_pic->p[i_plane].i_visible_pitch;\r
616         i_dst_stride  = p_pic->p[i_plane].i_pitch;\r
617 \r
618         p_src += i_width;\r
619 \r
620         for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )\r
621         {\r
622             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );\r
623             p_src += i_width;\r
624             p_dst += i_dst_stride;\r
625         }\r
626     }\r
627 \r
628     p_dst = p_pic->p[1].p_pixels;\r
629     p_pic->p[1].p_pixels = p_pic->p[2].p_pixels;\r
630     p_pic->p[2].p_pixels = p_dst;\r
631 }\r