]> git.sesse.net Git - vlc/blob - modules/codec/omxil/omxil.c
omxil: Ignore OMX.google.* codecs
[vlc] / modules / codec / omxil / omxil.c
1 /*****************************************************************************
2  * omxil.c: Video decoder module making use of OpenMAX IL components.
3  *****************************************************************************
4  * Copyright (C) 2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <dlfcn.h>
32 #if defined(USE_IOMX)
33 /* On dll_open, just check that the OMX_Init symbol already is loaded */
34 # define dll_open(name) dlsym(RTLD_DEFAULT, "OMX_Init")
35 # define dll_close(handle) do { } while (0)
36 # define dlsym(handle, name) dlsym(RTLD_DEFAULT, "I" name)
37 #else
38 # define dll_open(name) dlopen( name, RTLD_NOW )
39 # define dll_close(handle) dlclose(handle)
40 #endif
41
42 #include <limits.h>
43
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
46 #include <vlc_codec.h>
47 #include <vlc_aout.h>
48 #include <vlc_block_helper.h>
49 #include <vlc_cpu.h>
50 #include "../h264_nal.h"
51
52 #include "omxil.h"
53
54 //#define OMXIL_EXTRA_DEBUG
55
56 /*****************************************************************************
57  * List of OpenMAX IL core we will try in order
58  *****************************************************************************/
59 static const char *ppsz_dll_list[] =
60 {
61 #if defined(USE_IOMX)
62     "libiomx.so", /* Not used when using IOMX, the lib should already be loaded */
63 #else
64     "libOMX_Core.so", /* TI OMAP IL core */
65     "libOmxCore.so", /* Qualcomm IL core */
66     "libomxil-bellagio.so",  /* Bellagio IL core */
67 #endif
68     0
69 };
70
71 /*****************************************************************************
72  * Global OMX Core instance, shared between module instances
73  *****************************************************************************/
74 static vlc_mutex_t omx_core_mutex = VLC_STATIC_MUTEX;
75 static unsigned int omx_refcount = 0;
76 static void *dll_handle;
77 static OMX_ERRORTYPE (*pf_init) (void);
78 static OMX_ERRORTYPE (*pf_deinit) (void);
79 static OMX_ERRORTYPE (*pf_get_handle) (OMX_HANDLETYPE *, OMX_STRING,
80                                        OMX_PTR, OMX_CALLBACKTYPE *);
81 static OMX_ERRORTYPE (*pf_free_handle) (OMX_HANDLETYPE);
82 static OMX_ERRORTYPE (*pf_component_enum)(OMX_STRING, OMX_U32, OMX_U32);
83 static OMX_ERRORTYPE (*pf_get_roles_of_component)(OMX_STRING, OMX_U32 *, OMX_U8 **);
84
85 /*****************************************************************************
86  * Local prototypes
87  *****************************************************************************/
88 static int  OpenDecoder( vlc_object_t * );
89 static int  OpenEncoder( vlc_object_t * );
90 static int  OpenGeneric( vlc_object_t *, bool b_encode );
91 static void CloseGeneric( vlc_object_t * );
92
93 static picture_t *DecodeVideo( decoder_t *, block_t ** );
94 static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
95 static block_t *EncodeVideo( encoder_t *, picture_t * );
96
97 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
98                                       OMX_U32, OMX_U32, OMX_PTR );
99 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE, OMX_PTR,
100                                          OMX_BUFFERHEADERTYPE * );
101 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
102                                         OMX_BUFFERHEADERTYPE * );
103
104 /*****************************************************************************
105  * Module descriptor
106  *****************************************************************************/
107 vlc_module_begin ()
108     set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
109     set_category( CAT_INPUT )
110     set_subcategory( SUBCAT_INPUT_VCODEC )
111     set_section( N_("Decoding") , NULL )
112 #if defined(USE_IOMX)
113     /* For IOMX, don't enable it automatically via priorities,
114      * enable it only via the --codec iomx command line parameter when
115      * wanted. */
116     set_capability( "decoder", 0 )
117 #else
118     set_capability( "decoder", 80 )
119 #endif
120     set_callbacks( OpenDecoder, CloseGeneric )
121
122     add_submodule ()
123     set_section( N_("Encoding") , NULL )
124     set_description( N_("Video encoder (using OpenMAX IL)") )
125     set_capability( "encoder", 0 )
126     set_callbacks( OpenEncoder, CloseGeneric )
127 vlc_module_end ()
128
129 /*****************************************************************************
130  * CreateComponentsList: creates a list of components matching the given role
131  *****************************************************************************/
132 static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
133 {
134     decoder_sys_t *p_sys = p_dec->p_sys;
135     char psz_name[OMX_MAX_STRINGNAME_SIZE];
136     OMX_ERRORTYPE omx_error;
137     OMX_U32 roles = 0;
138     OMX_U8 **ppsz_roles = 0;
139     unsigned int i, j, len;
140
141     if(!psz_role) goto end;
142     len = strlen(psz_role);
143
144     for( i = 0; ; i++ )
145     {
146         bool b_found = false;
147
148         omx_error = pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
149         if(omx_error != OMX_ErrorNone) break;
150
151         msg_Dbg(p_dec, "component %s", psz_name);
152
153         omx_error = pf_get_roles_of_component(psz_name, &roles, 0);
154         if(omx_error != OMX_ErrorNone || !roles) continue;
155
156         ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
157         if(!ppsz_roles) continue;
158
159         for( j = 0; j < roles; j++ )
160             ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
161                 j * OMX_MAX_STRINGNAME_SIZE;
162
163         omx_error = pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
164         if(omx_error != OMX_ErrorNone) roles = 0;
165
166         for(j = 0; j < roles; j++)
167         {
168             msg_Dbg(p_dec, "  - role: %s", ppsz_roles[j]);
169             if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
170         }
171
172         free(ppsz_roles);
173
174         if(!b_found) continue;
175
176         if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
177         {
178             msg_Dbg(p_dec, "too many matching components");
179             continue;
180         }
181
182         strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
183                 OMX_MAX_STRINGNAME_SIZE-1);
184         p_sys->components++;
185     }
186
187  end:
188     msg_Dbg(p_dec, "found %i matching components for role %s",
189             p_sys->components, psz_role);
190     for( i = 0; i < p_sys->components; i++ )
191         msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
192
193     return p_sys->components;
194 }
195
196 /*****************************************************************************
197  * ImplementationSpecificWorkarounds: place-holder for implementation
198  * specific workarounds
199  *****************************************************************************/
200 static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
201     OmxPort *p_port, es_format_t *p_fmt)
202 {
203     decoder_sys_t *p_sys = p_dec->p_sys;
204     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
205     int i_profile = 0xFFFF, i_level = 0xFFFF;
206
207     /* Try to find out the profile of the video */
208     while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
209           p_fmt->i_codec == VLC_CODEC_H264)
210     {
211         uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
212         if(!p || !p_dec->fmt_in.p_extra) break;
213
214         /* Check the profile / level */
215         if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
216            p[0] == 1)
217         {
218             if(p_dec->fmt_in.i_extra < 12) break;
219             p_sys->i_nal_size_length = 1 + (p[4]&0x03);
220             if( !(p[5]&0x1f) ) break;
221             p += 8;
222         }
223         else
224         {
225             if(p_dec->fmt_in.i_extra < 8) break;
226             if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
227             else if(!p[0] && !p[1] && p[2] == 1) p += 3;
228             else break;
229         }
230
231         if( ((*p++)&0x1f) != 7) break;
232
233         /* Get profile/level out of first SPS */
234         i_profile = p[0];
235         i_level = p[2];
236         break;
237     }
238
239     if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
240     {
241         if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
242            p_fmt->i_codec == VLC_CODEC_H264 &&
243            (i_profile != 66 || i_level > 30))
244         {
245             msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
246                     i_profile, i_level);
247             return OMX_ErrorNotImplemented;
248         }
249
250         if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
251            p_fmt->i_codec == VLC_CODEC_I420)
252         {
253             /* I420 xvideo is slow on OMAP */
254             def->format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
255             GetVlcChromaFormat( def->format.video.eColorFormat,
256                                 &p_fmt->i_codec, 0 );
257             GetVlcChromaSizes( p_fmt->i_codec,
258                                def->format.video.nFrameWidth,
259                                def->format.video.nFrameHeight,
260                                &p_port->i_frame_size, &p_port->i_frame_stride,
261                                &p_port->i_frame_stride_chroma_div );
262             def->format.video.nStride = p_port->i_frame_stride;
263             def->nBufferSize = p_port->i_frame_size;
264         }
265     }
266     else if(!strcmp(p_sys->psz_component, "OMX.st.video_encoder"))
267     {
268         if(p_fmt->i_cat == VIDEO_ES)
269         {
270             /* Bellagio's encoder doesn't encode the framerate in Q16 */
271             def->format.video.xFramerate >>= 16;
272         }
273     }
274     else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
275                       strlen("OMX.qcom.video.decoder")))
276     {
277         /* qdsp6 refuses buffer size larger than 450K on input port */
278         if (def->nBufferSize > 450 * 1024)
279         {
280             def->nBufferSize = 450 * 1024;
281             p_port->i_frame_size = def->nBufferSize;
282         }
283     }
284
285     return OMX_ErrorNone;
286 }
287
288 /*****************************************************************************
289  * SetPortDefinition: set definition of the omx port based on the vlc format
290  *****************************************************************************/
291 static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
292                                        es_format_t *p_fmt)
293 {
294     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
295     OMX_ERRORTYPE omx_error;
296
297     omx_error = OMX_GetParameter(p_port->omx_handle,
298                                  OMX_IndexParamPortDefinition, def);
299     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
300                 omx_error, ErrorToString(omx_error));
301
302     switch(p_fmt->i_cat)
303     {
304     case VIDEO_ES:
305         def->format.video.nFrameWidth = p_fmt->video.i_width;
306         def->format.video.nFrameHeight = p_fmt->video.i_height;
307         if(def->format.video.eCompressionFormat == OMX_VIDEO_CodingUnused)
308             def->format.video.nStride = def->format.video.nFrameWidth;
309         if( p_fmt->video.i_frame_rate > 0 &&
310             p_fmt->video.i_frame_rate_base > 0 )
311             def->format.video.xFramerate = (p_fmt->video.i_frame_rate << 16) /
312                 p_fmt->video.i_frame_rate_base;
313
314         if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
315         {
316             def->nBufferSize = def->format.video.nFrameWidth *
317               def->format.video.nFrameHeight * 2;
318             p_port->i_frame_size = def->nBufferSize;
319
320             if(!GetOmxVideoFormat(p_fmt->i_codec,
321                                   &def->format.video.eCompressionFormat, 0) )
322             {
323                 if(!GetOmxChromaFormat(p_fmt->i_codec,
324                                        &def->format.video.eColorFormat, 0) )
325                 {
326                     omx_error = OMX_ErrorNotImplemented;
327                     CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
328                                 (char *)&p_fmt->i_codec );
329                 }
330                 GetVlcChromaSizes( p_fmt->i_codec,
331                                    def->format.video.nFrameWidth,
332                                    def->format.video.nFrameHeight,
333                                    &p_port->i_frame_size, &p_port->i_frame_stride,
334                                    &p_port->i_frame_stride_chroma_div );
335                 def->format.video.nStride = p_port->i_frame_stride;
336                 def->nBufferSize = p_port->i_frame_size;
337             }
338         }
339         else
340         {
341             if( !GetVlcChromaFormat( def->format.video.eColorFormat,
342                                      &p_fmt->i_codec, 0 ) )
343             {
344                 omx_error = OMX_ErrorNotImplemented;
345                 CHECK_ERROR(omx_error, "OMX color format %i not supported",
346                             (int)def->format.video.eColorFormat );
347             }
348             GetVlcChromaSizes( p_fmt->i_codec,
349                                def->format.video.nFrameWidth,
350                                def->format.video.nFrameHeight,
351                                &p_port->i_frame_size, &p_port->i_frame_stride,
352                                &p_port->i_frame_stride_chroma_div );
353             def->format.video.nStride = p_port->i_frame_stride;
354             def->nBufferSize = p_port->i_frame_size;
355         }
356         break;
357
358     case AUDIO_ES:
359         p_port->i_frame_size = def->nBufferSize;
360         if(def->eDir == OMX_DirInput)
361         {
362             if(!GetOmxAudioFormat(p_fmt->i_codec,
363                                   &def->format.audio.eEncoding, 0) )
364             {
365                 omx_error = OMX_ErrorNotImplemented;
366                 CHECK_ERROR(omx_error, "codec %4.4s doesn't match any OMX format",
367                             (char *)&p_fmt->i_codec );
368             }
369         }
370         else
371         {
372             if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
373                                    &p_fmt->i_codec, 0 ) )
374             {
375                 omx_error = OMX_ErrorNotImplemented;
376                 CHECK_ERROR(omx_error, "OMX audio encoding %i not supported",
377                             (int)def->format.audio.eEncoding );
378             }
379         }
380         break;
381
382     default: return OMX_ErrorNotImplemented;
383     }
384
385     omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
386     CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
387                 omx_error, ErrorToString(omx_error));
388
389     omx_error = OMX_SetParameter(p_port->omx_handle,
390                                  OMX_IndexParamPortDefinition, def);
391     CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
392                 omx_error, ErrorToString(omx_error));
393
394     omx_error = OMX_GetParameter(p_port->omx_handle,
395                                  OMX_IndexParamPortDefinition, def);
396     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
397                 omx_error, ErrorToString(omx_error));
398
399     if(p_port->i_frame_size > def->nBufferSize)
400         def->nBufferSize = p_port->i_frame_size;
401     p_port->i_frame_size = def->nBufferSize;
402
403     /* Deal with audio params */
404     if(p_fmt->i_cat == AUDIO_ES)
405     {
406         omx_error = SetAudioParameters(p_port->omx_handle,
407                                        &p_port->format_param, def->nPortIndex,
408                                        def->format.audio.eEncoding,
409                                        p_fmt->audio.i_channels,
410                                        p_fmt->audio.i_rate,
411                                        p_fmt->i_bitrate,
412                                        p_fmt->audio.i_bitspersample,
413                                        p_fmt->audio.i_blockalign);
414         CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
415                     omx_error, ErrorToString(omx_error));
416     }
417
418  error:
419     return omx_error;
420 }
421
422 /*****************************************************************************
423  * GetPortDefinition: set vlc format based on the definition of the omx port
424  *****************************************************************************/
425 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
426                                        es_format_t *p_fmt)
427 {
428     decoder_sys_t *p_sys = p_dec->p_sys;
429     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
430     OMX_ERRORTYPE omx_error;
431
432     omx_error = OMX_GetParameter(p_port->omx_handle,
433                                  OMX_IndexParamPortDefinition, def);
434     CHECK_ERROR(omx_error, "OMX_GetParameter failed (%x : %s)",
435                 omx_error, ErrorToString(omx_error));
436
437     switch(p_fmt->i_cat)
438     {
439     case VIDEO_ES:
440         p_fmt->video.i_width = def->format.video.nFrameWidth;
441         p_fmt->video.i_visible_width = def->format.video.nFrameWidth;
442         p_fmt->video.i_height = def->format.video.nFrameHeight;
443         p_fmt->video.i_visible_height = def->format.video.nFrameHeight;
444         p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
445         p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
446
447         /* Hack: Nexus One (stock firmware with binary OMX driver blob)
448          * claims to output 420Planar even though it in in practice is
449          * NV21. */
450         if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
451            !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
452                     strlen("OMX.qcom.video.decoder")))
453             def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
454
455         /* Hack: Galaxy S II (stock firmware) gives a slice height larger
456          * than the video height, but this doesn't imply padding between
457          * the video planes. Nexus S also has a slice height larger than
458          * the video height, but there it actually is real padding, thus
459          * Galaxy S II is the buggy one. The Galaxy S II decoder is
460          * named OMX.SEC.avcdec while the one on Nexus S is
461          * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
462          * contain the string ".Decoder". */
463         if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
464            !strstr(p_sys->psz_component, ".Decoder"))
465             def->format.video.nSliceHeight = 0;
466
467         if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
468                                &p_fmt->i_codec, 0 ) )
469         {
470             if( !GetVlcChromaFormat( def->format.video.eColorFormat,
471                                      &p_fmt->i_codec, 0 ) )
472             {
473                 omx_error = OMX_ErrorNotImplemented;
474                 CHECK_ERROR(omx_error, "OMX color format %i not supported",
475                             (int)def->format.video.eColorFormat );
476             }
477             GetVlcChromaSizes( p_fmt->i_codec,
478                                def->format.video.nFrameWidth,
479                                def->format.video.nFrameHeight,
480                                &p_port->i_frame_size, &p_port->i_frame_stride,
481                                &p_port->i_frame_stride_chroma_div );
482         }
483         if(p_port->i_frame_size > def->nBufferSize)
484             def->nBufferSize = p_port->i_frame_size;
485         p_port->i_frame_size = def->nBufferSize;
486 #if 0
487         if((int)p_port->i_frame_stride > def->format.video.nStride)
488             def->format.video.nStride = p_port->i_frame_stride;
489 #endif
490         p_port->i_frame_stride = def->format.video.nStride;
491         break;
492
493     case AUDIO_ES:
494         if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
495                                 &p_fmt->i_codec, 0 ) )
496         {
497             omx_error = OMX_ErrorNotImplemented;
498             CHECK_ERROR(omx_error, "OMX audio format %i not supported",
499                         (int)def->format.audio.eEncoding );
500         }
501
502         omx_error = GetAudioParameters(p_port->omx_handle,
503                                        &p_port->format_param, def->nPortIndex,
504                                        def->format.audio.eEncoding,
505                                        &p_fmt->audio.i_channels,
506                                        &p_fmt->audio.i_rate,
507                                        &p_fmt->i_bitrate,
508                                        &p_fmt->audio.i_bitspersample,
509                                        &p_fmt->audio.i_blockalign);
510         CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
511                     omx_error, ErrorToString(omx_error));
512
513         if(p_fmt->audio.i_channels < 9)
514         {
515             static const int pi_channels_maps[9] =
516             {
517                 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
518                 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
519                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
520                 | AOUT_CHAN_REARRIGHT,
521                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
522                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
523                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
524                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
525                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
526                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
527                 | AOUT_CHAN_MIDDLERIGHT,
528                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
529                 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
530                 | AOUT_CHAN_LFE
531             };
532             p_fmt->audio.i_physical_channels =
533                 p_fmt->audio.i_original_channels =
534                     pi_channels_maps[p_fmt->audio.i_channels];
535         }
536
537         date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
538
539         break;
540
541     default: return OMX_ErrorNotImplemented;
542     }
543
544  error:
545     return omx_error;
546 }
547
548 /*****************************************************************************
549  * DeinitialiseComponent: Deinitialise and unload an OMX component
550  *****************************************************************************/
551 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
552                                            OMX_HANDLETYPE omx_handle)
553 {
554     decoder_sys_t *p_sys = p_dec->p_sys;
555     OMX_ERRORTYPE omx_error;
556     OMX_STATETYPE state;
557     unsigned int i, j;
558
559     if(!omx_handle) return OMX_ErrorNone;
560
561     omx_error = OMX_GetState(omx_handle, &state);
562     CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
563
564     if(state == OMX_StateExecuting)
565     {
566         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
567                                      OMX_StateIdle, 0 );
568         CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
569         omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
570         CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
571     }
572
573     omx_error = OMX_GetState(omx_handle, &state);
574     CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
575
576     if(state == OMX_StateIdle)
577     {
578         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
579                                      OMX_StateLoaded, 0 );
580         CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
581
582         for(i = 0; i < p_sys->ports; i++)
583         {
584             OmxPort *p_port = &p_sys->p_ports[i];
585             OMX_BUFFERHEADERTYPE *p_buffer;
586
587             for(j = 0; j < p_port->i_buffers; j++)
588             {
589                 OMX_FIFO_GET(&p_port->fifo, p_buffer);
590                 if (p_buffer == &p_sys->sentinel_buffer)
591                     continue;
592                 omx_error = OMX_FreeBuffer( omx_handle,
593                                             p_port->i_port_index, p_buffer );
594
595                 if(omx_error != OMX_ErrorNone) break;
596             }
597             CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
598                         omx_error, (int)p_port->i_port_index, j );
599         }
600
601         omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
602         CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
603     }
604
605  error:
606     for(i = 0; i < p_sys->ports; i++)
607     {
608         OmxPort *p_port = &p_sys->p_ports[i];
609         free(p_port->pp_buffers);
610         p_port->pp_buffers = 0;
611     }
612     omx_error = pf_free_handle( omx_handle );
613     return omx_error;
614 }
615
616 /*****************************************************************************
617  * InitialiseComponent: Load and initialise an OMX component
618  *****************************************************************************/
619 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
620     OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
621 {
622     static OMX_CALLBACKTYPE callbacks =
623         { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
624     decoder_sys_t *p_sys = p_dec->p_sys;
625     OMX_HANDLETYPE omx_handle;
626     OMX_ERRORTYPE omx_error;
627     unsigned int i;
628     OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
629     OMX_PARAM_COMPONENTROLETYPE role;
630     OMX_PARAM_PORTDEFINITIONTYPE definition;
631     OMX_PORT_PARAM_TYPE param;
632
633     /* Load component */
634     omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
635     if(omx_error != OMX_ErrorNone)
636     {
637         msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
638                   omx_error, ErrorToString(omx_error) );
639         return omx_error;
640     }
641     strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
642
643     omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
644     if(omx_error == OMX_ErrorNone)
645         msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
646     else
647         msg_Dbg(p_dec, "loaded component %s", psz_component);
648     PrintOmx(p_dec, omx_handle, OMX_ALL);
649
650     /* Set component role */
651     OMX_INIT_STRUCTURE(role);
652     strcpy((char*)role.cRole,
653            GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
654                       p_dec->fmt_in.i_cat, p_sys->b_enc));
655
656     omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
657                                  &role);
658     omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
659                                  &role);
660     if(omx_error == OMX_ErrorNone)
661         msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
662
663     /* Find the input / output ports */
664     OMX_INIT_STRUCTURE(param);
665     OMX_INIT_STRUCTURE(definition);
666     omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
667                                  OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, &param);
668     if(omx_error != OMX_ErrorNone) param.nPorts = 0;
669
670     for(i = 0; i < param.nPorts; i++)
671     {
672         OmxPort *p_port;
673
674         /* Get port definition */
675         definition.nPortIndex = param.nStartPortNumber + i;
676         omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
677                                      &definition);
678         if(omx_error != OMX_ErrorNone) continue;
679
680         if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
681         else  p_port = &p_sys->out;
682
683         p_port->b_valid = true;
684         p_port->i_port_index = definition.nPortIndex;
685         p_port->definition = definition;
686         p_port->omx_handle = omx_handle;
687     }
688
689     if(!p_sys->in.b_valid || !p_sys->out.b_valid)
690     {
691         omx_error = OMX_ErrorInvalidComponent;
692         CHECK_ERROR(omx_error, "couldn't find an input and output port");
693     }
694
695     if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
696     {
697         OMX_INDEXTYPE index;
698         omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
699         if(omx_error == OMX_ErrorNone)
700         {
701             OMX_BOOL enable = OMX_TRUE;
702             omx_error = OMX_SetConfig(omx_handle, index, &enable);
703             CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
704         } else {
705             OMX_BOOL enable = OMX_TRUE;
706             /* Needed on Samsung Galaxy S II */
707             omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
708             if (omx_error == OMX_ErrorNone)
709                 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
710             else
711                 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
712         }
713     }
714
715     /* Set port definitions */
716     for(i = 0; i < p_sys->ports; i++)
717     {
718         omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
719                                       p_sys->p_ports[i].p_fmt);
720         if(omx_error != OMX_ErrorNone) goto error;
721     }
722
723     /* Allocate our array for the omx buffers and enable ports */
724     for(i = 0; i < p_sys->ports; i++)
725     {
726         OmxPort *p_port = &p_sys->p_ports[i];
727
728         p_port->pp_buffers =
729             malloc(p_port->definition.nBufferCountActual *
730                    sizeof(OMX_BUFFERHEADERTYPE*));
731         if(!p_port->pp_buffers)
732         {
733           omx_error = OMX_ErrorInsufficientResources;
734           CHECK_ERROR(omx_error, "memory allocation failed");
735         }
736         p_port->i_buffers = p_port->definition.nBufferCountActual;
737
738         /* Enable port */
739         if(!p_port->definition.bEnabled)
740         {
741             omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
742                                          p_port->i_port_index, NULL);
743             CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
744                         (int)p_port->i_port_index, omx_error );
745             omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
746             CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
747                         (int)p_port->i_port_index, omx_error );
748         }
749     }
750
751     *p_handle = omx_handle;
752     return OMX_ErrorNone;
753
754  error:
755     DeinitialiseComponent(p_dec, omx_handle);
756     *p_handle = 0;
757     return omx_error;
758 }
759
760 /*****************************************************************************
761  * OpenDecoder: Create the decoder instance
762  *****************************************************************************/
763 static int OpenDecoder( vlc_object_t *p_this )
764 {
765     decoder_t *p_dec = (decoder_t*)p_this;
766     int status;
767
768     if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
769         return VLC_EGENERIC;
770
771 #ifdef HAVE_MAEMO
772     if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
773         return VLC_EGENERIC;
774 #endif
775
776     status = OpenGeneric( p_this, false );
777     if(status != VLC_SUCCESS) return status;
778
779     p_dec->pf_decode_video = DecodeVideo;
780     p_dec->pf_decode_audio = DecodeAudio;
781
782     return VLC_SUCCESS;
783 }
784
785 /*****************************************************************************
786  * OpenEncoder: Create the encoder instance
787  *****************************************************************************/
788 static int OpenEncoder( vlc_object_t *p_this )
789 {
790     encoder_t *p_enc = (encoder_t*)p_this;
791     int status;
792
793     if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
794         return VLC_EGENERIC;
795
796     status = OpenGeneric( p_this, true );
797     if(status != VLC_SUCCESS) return status;
798
799     p_enc->pf_encode_video = EncodeVideo;
800
801     return VLC_SUCCESS;
802 }
803
804 /*****************************************************************************
805  * OpenGeneric: Create the generic decoder/encoder instance
806  *****************************************************************************/
807 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
808 {
809     decoder_t *p_dec = (decoder_t*)p_this;
810     decoder_sys_t *p_sys;
811     OMX_ERRORTYPE omx_error;
812     OMX_BUFFERHEADERTYPE *p_header;
813     unsigned int i, j;
814
815     vlc_mutex_lock( &omx_core_mutex );
816     if( omx_refcount > 0 )
817         goto loaded;
818
819     /* Load the OMX core */
820     for( i = 0; ppsz_dll_list[i]; i++ )
821     {
822         dll_handle = dll_open( ppsz_dll_list[i] );
823         if( dll_handle ) break;
824     }
825     if( !dll_handle )
826     {
827         vlc_mutex_unlock( &omx_core_mutex );
828         return VLC_EGENERIC;
829     }
830
831     pf_init = dlsym( dll_handle, "OMX_Init" );
832     pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
833     pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
834     pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
835     pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
836     pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
837     if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
838         !pf_component_enum || !pf_get_roles_of_component )
839     {
840         msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
841                   ppsz_dll_list[i], dlerror() );
842         dll_close(dll_handle);
843         vlc_mutex_unlock( &omx_core_mutex );
844         return VLC_EGENERIC;
845     }
846
847 loaded:
848     /* Allocate the memory needed to store the decoder's structure */
849     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
850     {
851         if( omx_refcount == 0 )
852             dll_close(dll_handle);
853         vlc_mutex_unlock( &omx_core_mutex );
854         return VLC_ENOMEM;
855     }
856
857     /* Initialise the thread properties */
858     if(!b_encode)
859     {
860         p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
861         p_dec->fmt_out.video = p_dec->fmt_in.video;
862         p_dec->fmt_out.audio = p_dec->fmt_in.audio;
863         p_dec->fmt_out.i_codec = 0;
864     }
865     p_sys->b_enc = b_encode;
866     p_sys->pp_last_event = &p_sys->p_events;
867     vlc_mutex_init (&p_sys->mutex);
868     vlc_cond_init (&p_sys->cond);
869     vlc_mutex_init (&p_sys->lock);
870     vlc_mutex_init (&p_sys->in.fifo.lock);
871     vlc_cond_init (&p_sys->in.fifo.wait);
872     p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
873     p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
874     p_sys->in.b_direct = false;
875     p_sys->in.b_flushed = true;
876     p_sys->in.p_fmt = &p_dec->fmt_in;
877     vlc_mutex_init (&p_sys->out.fifo.lock);
878     vlc_cond_init (&p_sys->out.fifo.wait);
879     p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
880     p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
881     p_sys->out.b_direct = true;
882     p_sys->out.b_flushed = true;
883     p_sys->out.p_fmt = &p_dec->fmt_out;
884     p_sys->ports = 2;
885     p_sys->p_ports = &p_sys->in;
886
887     msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
888             (char *)&p_dec->fmt_out.i_codec);
889
890     /* Initialise the OMX core */
891     omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
892     omx_refcount++;
893     if(omx_error != OMX_ErrorNone)
894     {
895         msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
896                   ErrorToString(omx_error) );
897         vlc_mutex_unlock( &omx_core_mutex );
898         CloseGeneric(p_this);
899         return VLC_EGENERIC;
900     }
901     p_sys->b_init = true;
902     vlc_mutex_unlock( &omx_core_mutex );
903
904     /* Enumerate components and build a list of the one we want to try */
905     if( !CreateComponentsList(p_dec,
906              GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
907                         p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
908                         p_sys->b_enc)) )
909     {
910         msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
911                   (char *)&p_dec->fmt_in.i_codec );
912         CloseGeneric(p_this);
913         return VLC_EGENERIC;
914     }
915
916     /* Try to load and initialise a component */
917     omx_error = OMX_ErrorUndefined;
918     for(i = 0; i < p_sys->components; i++)
919     {
920 #ifdef __ANDROID__
921         /* ignore OpenCore software codecs */
922         if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
923             continue;
924         /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
925         if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
926             continue;
927 #endif
928         omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
929                                         &p_sys->omx_handle);
930         if(omx_error == OMX_ErrorNone) break;
931     }
932     CHECK_ERROR(omx_error, "no component could be initialised" );
933
934     /* Move component to Idle then Executing state */
935     OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
936     CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
937
938     /* Allocate omx buffers */
939     for(i = 0; i < p_sys->ports; i++)
940     {
941         OmxPort *p_port = &p_sys->p_ports[i];
942
943         for(j = 0; j < p_port->i_buffers; j++)
944         {
945 #if 0
946 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
947             char *p_buf = malloc(p_port->definition.nBufferSize +
948                                  p_port->definition.nBufferAlignment);
949             p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
950 #endif
951
952             if(0 && p_port->b_direct)
953                 omx_error =
954                     OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
955                                    p_port->i_port_index, 0,
956                                    p_port->definition.nBufferSize, (void*)1);
957             else
958                 omx_error =
959                     OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
960                                         p_port->i_port_index, 0,
961                                         p_port->definition.nBufferSize);
962
963             if(omx_error != OMX_ErrorNone) break;
964             OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
965         }
966         p_port->i_buffers = j;
967         CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
968                     omx_error, (int)p_port->i_port_index, j );
969     }
970
971     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
972     CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
973
974     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
975                                  OMX_StateExecuting, 0);
976     CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
977     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
978     CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
979
980     /* Send codec configuration data */
981     if( p_dec->fmt_in.i_extra )
982     {
983         OMX_FIFO_GET(&p_sys->in.fifo, p_header);
984         p_header->nFilledLen = p_dec->fmt_in.i_extra;
985
986         /* Convert H.264 NAL format to annex b */
987         if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
988         {
989             p_header->nFilledLen = 0;
990             convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
991                              p_header->pBuffer, p_header->nAllocLen,
992                              (uint32_t*) &p_header->nFilledLen, NULL );
993         }
994         else if(p_sys->in.b_direct)
995         {
996             p_header->pOutputPortPrivate = p_header->pBuffer;
997             p_header->pBuffer = p_dec->fmt_in.p_extra;
998         }
999         else
1000         {
1001             if(p_header->nFilledLen > p_header->nAllocLen)
1002             {
1003                 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1004                         (int)p_header->nAllocLen);
1005                 p_header->nFilledLen = p_header->nAllocLen;
1006             }
1007             memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1008         }
1009
1010         p_header->nOffset = 0;
1011         p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1012         msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1013                 p_header->pBuffer, (int)p_header->nFilledLen);
1014         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1015     }
1016
1017     /* Get back output port definition */
1018     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1019     if(omx_error != OMX_ErrorNone) goto error;
1020
1021     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1022     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1023
1024     if(p_sys->b_error) goto error;
1025
1026     p_dec->b_need_packetized = true;
1027     return VLC_SUCCESS;
1028
1029  error:
1030     CloseGeneric(p_this);
1031     return VLC_EGENERIC;
1032 }
1033
1034 /*****************************************************************************
1035  * PortReconfigure
1036  *****************************************************************************/
1037 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1038 {
1039     decoder_sys_t *p_sys = p_dec->p_sys;
1040     OMX_PARAM_PORTDEFINITIONTYPE definition;
1041     OMX_BUFFERHEADERTYPE *p_buffer;
1042     OMX_ERRORTYPE omx_error;
1043     unsigned int i;
1044
1045     /* Sanity checking */
1046     OMX_INIT_STRUCTURE(definition);
1047     definition.nPortIndex = p_port->i_port_index;
1048     omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1049                                  &definition);
1050     if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1051        (!definition.format.video.nFrameWidth ||
1052        !definition.format.video.nFrameHeight)) )
1053         return OMX_ErrorUndefined;
1054
1055     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1056                                  p_port->i_port_index, NULL);
1057     CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1058                 (int)p_port->i_port_index, omx_error );
1059
1060     for(i = 0; i < p_port->i_buffers; i++)
1061     {
1062         OMX_FIFO_GET(&p_port->fifo, p_buffer);
1063         if (p_buffer == &p_sys->sentinel_buffer)
1064             continue;
1065         omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1066                                     p_port->i_port_index, p_buffer );
1067
1068         if(omx_error != OMX_ErrorNone) break;
1069     }
1070     CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1071                 omx_error, (int)p_port->i_port_index, i );
1072
1073     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1074     CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1075
1076     /* Get the new port definition */
1077     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1078     if(omx_error != OMX_ErrorNone) goto error;
1079
1080     if( p_dec->fmt_in.i_cat != AUDIO_ES )
1081     {
1082         /* Don't explicitly set the new parameters that we got with
1083          * OMX_GetParameter above when using audio codecs.
1084          * That struct hasn't been changed since, so there should be
1085          * no need to set it here, unless some codec expects the
1086          * SetParameter call as a trigger event for some part of
1087          * the reconfiguration.
1088          * This fixes using audio decoders on Samsung Galaxy S II,
1089          *
1090          * Only skipping this for audio codecs, to minimize the
1091          * change for current working configurations for video.
1092          */
1093         omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1094                                      &definition);
1095         CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1096                     omx_error, ErrorToString(omx_error));
1097     }
1098
1099     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1100                                  p_port->i_port_index, NULL);
1101     CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1102                 (int)p_port->i_port_index, omx_error );
1103
1104     if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1105         free(p_port->pp_buffers);
1106         p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1107         if(!p_port->pp_buffers)
1108         {
1109             omx_error = OMX_ErrorInsufficientResources;
1110             CHECK_ERROR(omx_error, "memory allocation failed");
1111         }
1112     }
1113     p_port->i_buffers = p_port->definition.nBufferCountActual;
1114     for(i = 0; i < p_port->i_buffers; i++)
1115     {
1116         if(0 && p_port->b_direct)
1117             omx_error =
1118                 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1119                                p_port->i_port_index, 0,
1120                                p_port->definition.nBufferSize, (void*)1);
1121         else
1122             omx_error =
1123                 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1124                                     p_port->i_port_index, 0,
1125                                     p_port->definition.nBufferSize);
1126
1127         if(omx_error != OMX_ErrorNone) break;
1128         OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1129     }
1130     p_port->i_buffers = i;
1131     CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1132                 omx_error, (int)p_port->i_port_index, i );
1133
1134     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1135     CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1136
1137     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1138     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1139
1140  error:
1141     return omx_error;
1142 }
1143
1144 /*****************************************************************************
1145  * DecodeVideo: Called to decode one frame
1146  *****************************************************************************/
1147 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1148 {
1149     decoder_sys_t *p_sys = p_dec->p_sys;
1150     picture_t *p_pic = NULL, *p_next_pic;
1151     OMX_ERRORTYPE omx_error;
1152     unsigned int i;
1153
1154     OMX_BUFFERHEADERTYPE *p_header;
1155     block_t *p_block;
1156
1157     if( !pp_block || !*pp_block )
1158         return NULL;
1159
1160     p_block = *pp_block;
1161
1162     /* Check for errors from codec */
1163     if(p_sys->b_error)
1164     {
1165         msg_Dbg(p_dec, "error during decoding");
1166         block_Release( p_block );
1167         return 0;
1168     }
1169
1170     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1171     {
1172         block_Release( p_block );
1173         if(!p_sys->in.b_flushed)
1174         {
1175             msg_Dbg(p_dec, "flushing");
1176             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1177                              p_sys->in.definition.nPortIndex, 0 );
1178         }
1179         p_sys->in.b_flushed = true;
1180         return NULL;
1181     }
1182
1183     /* Take care of decoded frames first */
1184     while(!p_pic)
1185     {
1186         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1187         if(!p_header) break; /* No frame available */
1188
1189         if(p_header->nFilledLen)
1190         {
1191             p_pic = p_header->pAppPrivate;
1192             if(!p_pic)
1193             {
1194                 /* We're not in direct rendering mode.
1195                  * Get a new picture and copy the content */
1196                 p_pic = decoder_NewPicture( p_dec );
1197                 if( !p_pic ) break; /* No picture available */
1198
1199                 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1200             }
1201
1202             p_pic->date = p_header->nTimeStamp;
1203             p_header->nFilledLen = 0;
1204             p_header->pAppPrivate = 0;
1205         }
1206
1207         /* Get a new picture */
1208         if(p_sys->in.b_direct && !p_header->pAppPrivate)
1209         {
1210             p_next_pic = decoder_NewPicture( p_dec );
1211             if(!p_next_pic) break;
1212
1213             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1214             p_header->pAppPrivate = p_next_pic;
1215             p_header->pInputPortPrivate = p_header->pBuffer;
1216             p_header->pBuffer = p_next_pic->p[0].p_pixels;
1217         }
1218         else
1219         {
1220             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1221         }
1222
1223 #ifdef OMXIL_EXTRA_DEBUG
1224         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1225 #endif
1226         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1227     }
1228
1229     /* Send the input buffer to the component */
1230     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1231
1232     if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1233         goto reconfig;
1234
1235     if(p_header)
1236     {
1237         p_header->nFilledLen = p_block->i_buffer;
1238         p_header->nOffset = 0;
1239         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1240         p_header->nTimeStamp = p_block->i_dts;
1241
1242         /* In direct mode we pass the input pointer as is.
1243          * Otherwise we memcopy the data */
1244         if(p_sys->in.b_direct)
1245         {
1246             p_header->pOutputPortPrivate = p_header->pBuffer;
1247             p_header->pBuffer = p_block->p_buffer;
1248             p_header->pAppPrivate = p_block;
1249         }
1250         else
1251         {
1252             if(p_header->nFilledLen > p_header->nAllocLen)
1253             {
1254                 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1255                         (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1256                 p_header->nFilledLen = p_header->nAllocLen;
1257             }
1258             memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1259             block_Release(p_block);
1260         }
1261
1262         /* Convert H.264 NAL format to annex b */
1263         if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1264         {
1265             /* This only works for NAL sizes 3-4 */
1266             int i_len = p_header->nFilledLen, i;
1267             uint8_t* ptr = p_header->pBuffer;
1268             while( i_len >= p_sys->i_nal_size_length )
1269             {
1270                 uint32_t nal_len = 0;
1271                 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1272                     nal_len = (nal_len << 8) | ptr[i];
1273                     ptr[i] = 0;
1274                 }
1275                 ptr[p_sys->i_nal_size_length - 1] = 1;
1276                 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1277                     break;
1278                 ptr   += nal_len + 4;
1279                 i_len -= nal_len + 4;
1280             }
1281         }
1282 #ifdef OMXIL_EXTRA_DEBUG
1283         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1284                  (int)p_header->nFilledLen );
1285 #endif
1286         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1287         p_sys->in.b_flushed = false;
1288         *pp_block = NULL; /* Avoid being fed the same packet again */
1289     }
1290
1291 reconfig:
1292     /* Handle the PortSettingsChanged events */
1293     for(i = 0; i < p_sys->ports; i++)
1294     {
1295         OmxPort *p_port = &p_sys->p_ports[i];
1296         if(!p_port->b_reconfigure) continue;
1297         p_port->b_reconfigure = 0;
1298         omx_error = PortReconfigure(p_dec, p_port);
1299     }
1300
1301     return p_pic;
1302 }
1303
1304 /*****************************************************************************
1305  * DecodeAudio: Called to decode one frame
1306  *****************************************************************************/
1307 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1308 {
1309     decoder_sys_t *p_sys = p_dec->p_sys;
1310     aout_buffer_t *p_buffer = 0;
1311     OMX_BUFFERHEADERTYPE *p_header;
1312     OMX_ERRORTYPE omx_error;
1313     block_t *p_block;
1314     unsigned int i;
1315
1316     if( !pp_block || !*pp_block ) return NULL;
1317
1318     p_block = *pp_block;
1319
1320     /* Check for errors from codec */
1321     if(p_sys->b_error)
1322     {
1323         msg_Dbg(p_dec, "error during decoding");
1324         block_Release( p_block );
1325         return 0;
1326     }
1327
1328     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1329     {
1330         block_Release( p_block );
1331         date_Set( &p_sys->end_date, 0 );
1332         if(!p_sys->in.b_flushed)
1333         {
1334             msg_Dbg(p_dec, "flushing");
1335             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1336                              p_sys->in.definition.nPortIndex, 0 );
1337         }
1338         p_sys->in.b_flushed = true;
1339         return NULL;
1340     }
1341
1342     if( !date_Get( &p_sys->end_date ) )
1343     {
1344         if( !p_block->i_pts )
1345         {
1346             /* We've just started the stream, wait for the first PTS. */
1347             block_Release( p_block );
1348             return NULL;
1349         }
1350         date_Set( &p_sys->end_date, p_block->i_pts );
1351     }
1352
1353     /* Take care of decoded frames first */
1354     while(!p_buffer)
1355     {
1356         unsigned int i_samples;
1357
1358         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1359         if(!p_header) break; /* No frame available */
1360
1361         i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1362         if(i_samples)
1363         {
1364             p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1365             if( !p_buffer ) break; /* No audio buffer available */
1366
1367             memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1368             p_header->nFilledLen = 0;
1369
1370             if( p_header->nTimeStamp != 0 &&
1371                 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1372                 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1373
1374             p_buffer->i_pts = date_Get( &p_sys->end_date );
1375             p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1376                 p_buffer->i_pts;
1377         }
1378
1379 #ifdef OMXIL_EXTRA_DEBUG
1380         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1381 #endif
1382         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1383         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1384     }
1385
1386
1387     /* Send the input buffer to the component */
1388     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1389
1390     if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1391         goto reconfig;
1392
1393     if(p_header)
1394     {
1395         p_header->nFilledLen = p_block->i_buffer;
1396         p_header->nOffset = 0;
1397         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1398         p_header->nTimeStamp = p_block->i_dts;
1399
1400         /* In direct mode we pass the input pointer as is.
1401          * Otherwise we memcopy the data */
1402         if(p_sys->in.b_direct)
1403         {
1404             p_header->pOutputPortPrivate = p_header->pBuffer;
1405             p_header->pBuffer = p_block->p_buffer;
1406             p_header->pAppPrivate = p_block;
1407         }
1408         else
1409         {
1410             if(p_header->nFilledLen > p_header->nAllocLen)
1411             {
1412                 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1413                         (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1414                 p_header->nFilledLen = p_header->nAllocLen;
1415             }
1416             memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1417             block_Release(p_block);
1418         }
1419
1420 #ifdef OMXIL_EXTRA_DEBUG
1421         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1422                  (int)p_header->nFilledLen );
1423 #endif
1424         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1425         p_sys->in.b_flushed = false;
1426         *pp_block = NULL; /* Avoid being fed the same packet again */
1427     }
1428
1429 reconfig:
1430     /* Handle the PortSettingsChanged events */
1431     for(i = 0; i < p_sys->ports; i++)
1432     {
1433         OmxPort *p_port = &p_sys->p_ports[i];
1434         if(!p_port->b_reconfigure) continue;
1435         p_port->b_reconfigure = 0;
1436         omx_error = PortReconfigure(p_dec, p_port);
1437     }
1438
1439     return p_buffer;
1440 }
1441
1442 /*****************************************************************************
1443  * EncodeVideo: Called to encode one frame
1444  *****************************************************************************/
1445 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1446 {
1447     decoder_t *p_dec = ( decoder_t *)p_enc;
1448     decoder_sys_t *p_sys = p_dec->p_sys;
1449     OMX_ERRORTYPE omx_error;
1450     unsigned int i;
1451
1452     OMX_BUFFERHEADERTYPE *p_header;
1453     block_t *p_block = 0;
1454
1455     if( !p_pic ) return NULL;
1456
1457     /* Check for errors from codec */
1458     if(p_sys->b_error)
1459     {
1460         msg_Dbg(p_dec, "error during encoding");
1461         return NULL;
1462     }
1463
1464     /* Send the input buffer to the component */
1465     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1466     if(p_header)
1467     {
1468         /* In direct mode we pass the input pointer as is.
1469          * Otherwise we memcopy the data */
1470         if(p_sys->in.b_direct)
1471         {
1472             p_header->pOutputPortPrivate = p_header->pBuffer;
1473             p_header->pBuffer = p_pic->p[0].p_pixels;
1474         }
1475         else
1476         {
1477             CopyVlcPicture(p_dec, p_header, p_pic);
1478         }
1479
1480         p_header->nFilledLen = p_sys->in.i_frame_size;
1481         p_header->nOffset = 0;
1482         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1483         p_header->nTimeStamp = p_pic->date;
1484 #ifdef OMXIL_EXTRA_DEBUG
1485         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1486                  (int)p_header->nFilledLen );
1487 #endif
1488         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1489         p_sys->in.b_flushed = false;
1490     }
1491
1492     /* Handle the PortSettingsChanged events */
1493     for(i = 0; i < p_sys->ports; i++)
1494     {
1495         OmxPort *p_port = &p_sys->p_ports[i];
1496         if(!p_port->b_reconfigure) continue;
1497         p_port->b_reconfigure = 0;
1498         omx_error = PortReconfigure(p_dec, p_port);
1499     }
1500
1501     /* Wait for the decoded frame */
1502     while(!p_block)
1503     {
1504         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1505
1506         if(p_header->nFilledLen)
1507         {
1508             if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1509             {
1510                 /* TODO: need to store codec config */
1511                 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1512             }
1513
1514             p_block = p_header->pAppPrivate;
1515             if(!p_block)
1516             {
1517                 /* We're not in direct rendering mode.
1518                  * Get a new block and copy the content */
1519                 p_block = block_New( p_dec, p_header->nFilledLen );
1520                 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1521             }
1522
1523             p_block->i_buffer = p_header->nFilledLen;
1524             p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1525             p_header->nFilledLen = 0;
1526             p_header->pAppPrivate = 0;
1527         }
1528
1529 #ifdef OMXIL_EXTRA_DEBUG
1530         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1531 #endif
1532         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1533     }
1534
1535     msg_Dbg(p_dec, "done");
1536     return p_block;
1537 }
1538
1539 /*****************************************************************************
1540  * CloseGeneric: omxil decoder destruction
1541  *****************************************************************************/
1542 static void CloseGeneric( vlc_object_t *p_this )
1543 {
1544     decoder_t *p_dec = (decoder_t *)p_this;
1545     decoder_sys_t *p_sys = p_dec->p_sys;
1546
1547     if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1548     vlc_mutex_lock( &omx_core_mutex );
1549     omx_refcount--;
1550     if( omx_refcount == 0 )
1551     {
1552         if( p_sys->b_init ) pf_deinit();
1553         dll_close( dll_handle );
1554     }
1555     vlc_mutex_unlock( &omx_core_mutex );
1556
1557     vlc_mutex_destroy (&p_sys->mutex);
1558     vlc_cond_destroy (&p_sys->cond);
1559     vlc_mutex_destroy (&p_sys->in.fifo.lock);
1560     vlc_cond_destroy (&p_sys->in.fifo.wait);
1561     vlc_mutex_destroy (&p_sys->out.fifo.lock);
1562     vlc_cond_destroy (&p_sys->out.fifo.wait);
1563
1564     free( p_sys );
1565 }
1566
1567 /*****************************************************************************
1568  * OmxEventHandler: 
1569  *****************************************************************************/
1570 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1571     OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1572     OMX_U32 data_2, OMX_PTR event_data )
1573 {
1574     decoder_t *p_dec = (decoder_t *)app_data;
1575     decoder_sys_t *p_sys = p_dec->p_sys;
1576     unsigned int i;
1577     (void)omx_handle;
1578
1579     switch (event)
1580     {
1581     case OMX_EventCmdComplete:
1582         switch ((OMX_STATETYPE)data_1)
1583         {
1584         case OMX_CommandStateSet:
1585             msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1586                      CommandToString(data_1), StateToString(data_2) );
1587             break;
1588
1589         default:
1590             msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1591                      CommandToString(data_1), (unsigned int)data_2 );
1592             break;
1593         }
1594         break;
1595
1596     case OMX_EventError:
1597         msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1598                  ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1599                  (const char *)event_data);
1600         //p_sys->b_error = true;
1601         break;
1602
1603     case OMX_EventPortSettingsChanged:
1604         msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1605                  (unsigned int)data_1, (unsigned int)data_2 );
1606         for(i = 0; i < p_sys->ports; i++)
1607             if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1608                 p_sys->p_ports[i].b_reconfigure = true;
1609         memset(&p_sys->sentinel_buffer, 0, sizeof(p_sys->sentinel_buffer));
1610         p_sys->sentinel_buffer.nFlags = OMX_BUFFERFLAG_EOS;
1611         OMX_FIFO_PUT(&p_sys->in.fifo, &p_sys->sentinel_buffer);
1612         break;
1613
1614     default:
1615         msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1616                  (unsigned int)data_1, (unsigned int)data_2 );
1617         break;
1618     }
1619
1620     PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1621     return OMX_ErrorNone;
1622 }
1623
1624 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1625     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1626 {
1627     decoder_t *p_dec = (decoder_t *)app_data;
1628     decoder_sys_t *p_sys = p_dec->p_sys;
1629     (void)omx_handle;
1630
1631 #ifdef OMXIL_EXTRA_DEBUG
1632     msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1633 #endif
1634
1635     if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1636     {
1637         block_t *p_block = (block_t *)omx_header->pAppPrivate;
1638         omx_header->pBuffer = omx_header->pOutputPortPrivate;
1639         if(p_block) block_Release(p_block);
1640         omx_header->pAppPrivate = 0;
1641     }
1642     OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1643
1644     return OMX_ErrorNone;
1645 }
1646
1647 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1648     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1649 {
1650     decoder_t *p_dec = (decoder_t *)app_data;
1651     decoder_sys_t *p_sys = p_dec->p_sys;
1652     (void)omx_handle;
1653
1654 #ifdef OMXIL_EXTRA_DEBUG
1655     msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1656              (int)omx_header->nFilledLen );
1657 #endif
1658
1659     if(omx_header->pInputPortPrivate)
1660     {
1661         omx_header->pBuffer = omx_header->pInputPortPrivate;
1662     }
1663     OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1664
1665     return OMX_ErrorNone;
1666 }