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