]> git.sesse.net Git - vlc/blob - modules/codec/omxil/omxil.c
omxil: Adjust the slice height according to the crop rect for the TI color format
[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     OMX_CONFIG_RECTTYPE crop_rect;
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         OMX_INIT_STRUCTURE(crop_rect);
448         crop_rect.nPortIndex = def->nPortIndex;
449         omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
450         if (omx_error == OMX_ErrorNone)
451         {
452             p_fmt->video.i_width = crop_rect.nWidth;
453             p_fmt->video.i_visible_width = crop_rect.nWidth;
454             p_fmt->video.i_height = crop_rect.nHeight;
455             p_fmt->video.i_visible_height = crop_rect.nHeight;
456             if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
457                 def->format.video.nSliceHeight -= crop_rect.nTop/2;
458         }
459         else
460         {
461             /* Don't pass the error back to the caller, this isn't mandatory */
462             omx_error = OMX_ErrorNone;
463         }
464
465         /* Hack: Nexus One (stock firmware with binary OMX driver blob)
466          * claims to output 420Planar even though it in in practice is
467          * NV21. */
468         if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
469            !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
470                     strlen("OMX.qcom.video.decoder")))
471             def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
472
473         /* Hack: Galaxy S II (stock firmware) gives a slice height larger
474          * than the video height, but this doesn't imply padding between
475          * the video planes. Nexus S also has a slice height larger than
476          * the video height, but there it actually is real padding, thus
477          * Galaxy S II is the buggy one. The Galaxy S II decoder is
478          * named OMX.SEC.avcdec while the one on Nexus S is
479          * OMX.SEC.AVC.Decoder. Thus do this for any OMX.SEC. that don't
480          * contain the string ".Decoder". */
481         if(!strncmp(p_sys->psz_component, "OMX.SEC.", strlen("OMX.SEC.")) &&
482            !strstr(p_sys->psz_component, ".Decoder"))
483             def->format.video.nSliceHeight = 0;
484
485         if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
486                                &p_fmt->i_codec, 0 ) )
487         {
488             if( !GetVlcChromaFormat( def->format.video.eColorFormat,
489                                      &p_fmt->i_codec, 0 ) )
490             {
491                 omx_error = OMX_ErrorNotImplemented;
492                 CHECK_ERROR(omx_error, "OMX color format %i not supported",
493                             (int)def->format.video.eColorFormat );
494             }
495             GetVlcChromaSizes( p_fmt->i_codec,
496                                def->format.video.nFrameWidth,
497                                def->format.video.nFrameHeight,
498                                &p_port->i_frame_size, &p_port->i_frame_stride,
499                                &p_port->i_frame_stride_chroma_div );
500         }
501         if(p_port->i_frame_size > def->nBufferSize)
502             def->nBufferSize = p_port->i_frame_size;
503         p_port->i_frame_size = def->nBufferSize;
504 #if 0
505         if((int)p_port->i_frame_stride > def->format.video.nStride)
506             def->format.video.nStride = p_port->i_frame_stride;
507 #endif
508         p_port->i_frame_stride = def->format.video.nStride;
509         break;
510
511     case AUDIO_ES:
512         if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
513                                 &p_fmt->i_codec, 0 ) )
514         {
515             omx_error = OMX_ErrorNotImplemented;
516             CHECK_ERROR(omx_error, "OMX audio format %i not supported",
517                         (int)def->format.audio.eEncoding );
518         }
519
520         omx_error = GetAudioParameters(p_port->omx_handle,
521                                        &p_port->format_param, def->nPortIndex,
522                                        def->format.audio.eEncoding,
523                                        &p_fmt->audio.i_channels,
524                                        &p_fmt->audio.i_rate,
525                                        &p_fmt->i_bitrate,
526                                        &p_fmt->audio.i_bitspersample,
527                                        &p_fmt->audio.i_blockalign);
528         CHECK_ERROR(omx_error, "GetAudioParameters failed (%x : %s)",
529                     omx_error, ErrorToString(omx_error));
530
531         if(p_fmt->audio.i_channels < 9)
532         {
533             static const int pi_channels_maps[9] =
534             {
535                 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
536                 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
537                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
538                 | AOUT_CHAN_REARRIGHT,
539                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
540                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
541                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
542                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
543                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
544                 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
545                 | AOUT_CHAN_MIDDLERIGHT,
546                 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
547                 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
548                 | AOUT_CHAN_LFE
549             };
550             p_fmt->audio.i_physical_channels =
551                 p_fmt->audio.i_original_channels =
552                     pi_channels_maps[p_fmt->audio.i_channels];
553         }
554
555         date_Init( &p_dec->p_sys->end_date, p_fmt->audio.i_rate, 1 );
556
557         break;
558
559     default: return OMX_ErrorNotImplemented;
560     }
561
562  error:
563     return omx_error;
564 }
565
566 /*****************************************************************************
567  * DeinitialiseComponent: Deinitialise and unload an OMX component
568  *****************************************************************************/
569 static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
570                                            OMX_HANDLETYPE omx_handle)
571 {
572     decoder_sys_t *p_sys = p_dec->p_sys;
573     OMX_ERRORTYPE omx_error;
574     OMX_STATETYPE state;
575     unsigned int i, j;
576
577     if(!omx_handle) return OMX_ErrorNone;
578
579     omx_error = OMX_GetState(omx_handle, &state);
580     CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
581
582     if(state == OMX_StateExecuting)
583     {
584         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
585                                      OMX_StateIdle, 0 );
586         CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
587         omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
588         CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
589     }
590
591     omx_error = OMX_GetState(omx_handle, &state);
592     CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
593
594     if(state == OMX_StateIdle)
595     {
596         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
597                                      OMX_StateLoaded, 0 );
598         CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
599
600         for(i = 0; i < p_sys->ports; i++)
601         {
602             OmxPort *p_port = &p_sys->p_ports[i];
603             OMX_BUFFERHEADERTYPE *p_buffer;
604
605             for(j = 0; j < p_port->i_buffers; j++)
606             {
607                 OMX_FIFO_GET(&p_port->fifo, p_buffer);
608                 if (p_buffer == &p_sys->sentinel_buffer)
609                     continue;
610                 omx_error = OMX_FreeBuffer( omx_handle,
611                                             p_port->i_port_index, p_buffer );
612
613                 if(omx_error != OMX_ErrorNone) break;
614             }
615             CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
616                         omx_error, (int)p_port->i_port_index, j );
617         }
618
619         omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
620         CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
621     }
622
623  error:
624     for(i = 0; i < p_sys->ports; i++)
625     {
626         OmxPort *p_port = &p_sys->p_ports[i];
627         free(p_port->pp_buffers);
628         p_port->pp_buffers = 0;
629     }
630     omx_error = pf_free_handle( omx_handle );
631     return omx_error;
632 }
633
634 /*****************************************************************************
635  * InitialiseComponent: Load and initialise an OMX component
636  *****************************************************************************/
637 static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
638     OMX_STRING psz_component, OMX_HANDLETYPE *p_handle)
639 {
640     static OMX_CALLBACKTYPE callbacks =
641         { OmxEventHandler, OmxEmptyBufferDone, OmxFillBufferDone };
642     decoder_sys_t *p_sys = p_dec->p_sys;
643     OMX_HANDLETYPE omx_handle;
644     OMX_ERRORTYPE omx_error;
645     unsigned int i;
646     OMX_U8 psz_role[OMX_MAX_STRINGNAME_SIZE];
647     OMX_PARAM_COMPONENTROLETYPE role;
648     OMX_PARAM_PORTDEFINITIONTYPE definition;
649     OMX_PORT_PARAM_TYPE param;
650
651     /* Load component */
652     omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
653     if(omx_error != OMX_ErrorNone)
654     {
655         msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
656                   omx_error, ErrorToString(omx_error) );
657         return omx_error;
658     }
659     strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
660
661     omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
662     if(omx_error == OMX_ErrorNone)
663         msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
664     else
665         msg_Dbg(p_dec, "loaded component %s", psz_component);
666     PrintOmx(p_dec, omx_handle, OMX_ALL);
667
668     /* Set component role */
669     OMX_INIT_STRUCTURE(role);
670     strcpy((char*)role.cRole,
671            GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec : p_dec->fmt_in.i_codec,
672                       p_dec->fmt_in.i_cat, p_sys->b_enc));
673
674     omx_error = OMX_SetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
675                                  &role);
676     omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamStandardComponentRole,
677                                  &role);
678     if(omx_error == OMX_ErrorNone)
679         msg_Dbg(p_dec, "component standard role set to %s", role.cRole);
680
681     /* Find the input / output ports */
682     OMX_INIT_STRUCTURE(param);
683     OMX_INIT_STRUCTURE(definition);
684     omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
685                                  OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, &param);
686     if(omx_error != OMX_ErrorNone) param.nPorts = 0;
687
688     for(i = 0; i < param.nPorts; i++)
689     {
690         OmxPort *p_port;
691
692         /* Get port definition */
693         definition.nPortIndex = param.nStartPortNumber + i;
694         omx_error = OMX_GetParameter(omx_handle, OMX_IndexParamPortDefinition,
695                                      &definition);
696         if(omx_error != OMX_ErrorNone) continue;
697
698         if(definition.eDir == OMX_DirInput) p_port = &p_sys->in;
699         else  p_port = &p_sys->out;
700
701         p_port->b_valid = true;
702         p_port->i_port_index = definition.nPortIndex;
703         p_port->definition = definition;
704         p_port->omx_handle = omx_handle;
705     }
706
707     if(!p_sys->in.b_valid || !p_sys->out.b_valid)
708     {
709         omx_error = OMX_ErrorInvalidComponent;
710         CHECK_ERROR(omx_error, "couldn't find an input and output port");
711     }
712
713     if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8))
714     {
715         OMX_INDEXTYPE index;
716         omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
717         if(omx_error == OMX_ErrorNone)
718         {
719             OMX_BOOL enable = OMX_TRUE;
720             omx_error = OMX_SetConfig(omx_handle, index, &enable);
721             CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
722         } else {
723             OMX_BOOL enable = OMX_TRUE;
724             /* Needed on Samsung Galaxy S II */
725             omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
726             if (omx_error == OMX_ErrorNone)
727                 msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
728             else
729                 msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
730         }
731     }
732
733     /* Set port definitions */
734     for(i = 0; i < p_sys->ports; i++)
735     {
736         omx_error = SetPortDefinition(p_dec, &p_sys->p_ports[i],
737                                       p_sys->p_ports[i].p_fmt);
738         if(omx_error != OMX_ErrorNone) goto error;
739     }
740
741     /* Allocate our array for the omx buffers and enable ports */
742     for(i = 0; i < p_sys->ports; i++)
743     {
744         OmxPort *p_port = &p_sys->p_ports[i];
745
746         p_port->pp_buffers =
747             malloc(p_port->definition.nBufferCountActual *
748                    sizeof(OMX_BUFFERHEADERTYPE*));
749         if(!p_port->pp_buffers)
750         {
751           omx_error = OMX_ErrorInsufficientResources;
752           CHECK_ERROR(omx_error, "memory allocation failed");
753         }
754         p_port->i_buffers = p_port->definition.nBufferCountActual;
755
756         /* Enable port */
757         if(!p_port->definition.bEnabled)
758         {
759             omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
760                                          p_port->i_port_index, NULL);
761             CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
762                         (int)p_port->i_port_index, omx_error );
763             omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
764             CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
765                         (int)p_port->i_port_index, omx_error );
766         }
767     }
768
769     *p_handle = omx_handle;
770     return OMX_ErrorNone;
771
772  error:
773     DeinitialiseComponent(p_dec, omx_handle);
774     *p_handle = 0;
775     return omx_error;
776 }
777
778 /*****************************************************************************
779  * OpenDecoder: Create the decoder instance
780  *****************************************************************************/
781 static int OpenDecoder( vlc_object_t *p_this )
782 {
783     decoder_t *p_dec = (decoder_t*)p_this;
784     int status;
785
786     if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
787         return VLC_EGENERIC;
788
789 #ifdef HAVE_MAEMO
790     if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
791         return VLC_EGENERIC;
792 #endif
793
794     status = OpenGeneric( p_this, false );
795     if(status != VLC_SUCCESS) return status;
796
797     p_dec->pf_decode_video = DecodeVideo;
798     p_dec->pf_decode_audio = DecodeAudio;
799
800     return VLC_SUCCESS;
801 }
802
803 /*****************************************************************************
804  * OpenEncoder: Create the encoder instance
805  *****************************************************************************/
806 static int OpenEncoder( vlc_object_t *p_this )
807 {
808     encoder_t *p_enc = (encoder_t*)p_this;
809     int status;
810
811     if( !GetOmxRole(p_enc->fmt_out.i_codec, p_enc->fmt_in.i_cat, true) )
812         return VLC_EGENERIC;
813
814     status = OpenGeneric( p_this, true );
815     if(status != VLC_SUCCESS) return status;
816
817     p_enc->pf_encode_video = EncodeVideo;
818
819     return VLC_SUCCESS;
820 }
821
822 /*****************************************************************************
823  * OpenGeneric: Create the generic decoder/encoder instance
824  *****************************************************************************/
825 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
826 {
827     decoder_t *p_dec = (decoder_t*)p_this;
828     decoder_sys_t *p_sys;
829     OMX_ERRORTYPE omx_error;
830     OMX_BUFFERHEADERTYPE *p_header;
831     unsigned int i, j;
832
833     vlc_mutex_lock( &omx_core_mutex );
834     if( omx_refcount > 0 )
835         goto loaded;
836
837     /* Load the OMX core */
838     for( i = 0; ppsz_dll_list[i]; i++ )
839     {
840         dll_handle = dll_open( ppsz_dll_list[i] );
841         if( dll_handle ) break;
842     }
843     if( !dll_handle )
844     {
845         vlc_mutex_unlock( &omx_core_mutex );
846         return VLC_EGENERIC;
847     }
848
849     pf_init = dlsym( dll_handle, "OMX_Init" );
850     pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
851     pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
852     pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
853     pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
854     pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
855     if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
856         !pf_component_enum || !pf_get_roles_of_component )
857     {
858         msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
859                   ppsz_dll_list[i], dlerror() );
860         dll_close(dll_handle);
861         vlc_mutex_unlock( &omx_core_mutex );
862         return VLC_EGENERIC;
863     }
864
865 loaded:
866     /* Allocate the memory needed to store the decoder's structure */
867     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
868     {
869         if( omx_refcount == 0 )
870             dll_close(dll_handle);
871         vlc_mutex_unlock( &omx_core_mutex );
872         return VLC_ENOMEM;
873     }
874
875     /* Initialise the thread properties */
876     if(!b_encode)
877     {
878         p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
879         p_dec->fmt_out.video = p_dec->fmt_in.video;
880         p_dec->fmt_out.audio = p_dec->fmt_in.audio;
881         p_dec->fmt_out.i_codec = 0;
882     }
883     p_sys->b_enc = b_encode;
884     p_sys->pp_last_event = &p_sys->p_events;
885     vlc_mutex_init (&p_sys->mutex);
886     vlc_cond_init (&p_sys->cond);
887     vlc_mutex_init (&p_sys->lock);
888     vlc_mutex_init (&p_sys->in.fifo.lock);
889     vlc_cond_init (&p_sys->in.fifo.wait);
890     p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
891     p_sys->in.fifo.pp_last = &p_sys->in.fifo.p_first;
892     p_sys->in.b_direct = false;
893     p_sys->in.b_flushed = true;
894     p_sys->in.p_fmt = &p_dec->fmt_in;
895     vlc_mutex_init (&p_sys->out.fifo.lock);
896     vlc_cond_init (&p_sys->out.fifo.wait);
897     p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
898     p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
899     p_sys->out.b_direct = true;
900     p_sys->out.b_flushed = true;
901     p_sys->out.p_fmt = &p_dec->fmt_out;
902     p_sys->ports = 2;
903     p_sys->p_ports = &p_sys->in;
904
905     msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
906             (char *)&p_dec->fmt_out.i_codec);
907
908     /* Initialise the OMX core */
909     omx_error = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
910     omx_refcount++;
911     if(omx_error != OMX_ErrorNone)
912     {
913         msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
914                   ErrorToString(omx_error) );
915         vlc_mutex_unlock( &omx_core_mutex );
916         CloseGeneric(p_this);
917         return VLC_EGENERIC;
918     }
919     p_sys->b_init = true;
920     vlc_mutex_unlock( &omx_core_mutex );
921
922     /* Enumerate components and build a list of the one we want to try */
923     if( !CreateComponentsList(p_dec,
924              GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
925                         p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
926                         p_sys->b_enc)) )
927     {
928         msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
929                   (char *)&p_dec->fmt_in.i_codec );
930         CloseGeneric(p_this);
931         return VLC_EGENERIC;
932     }
933
934     /* Try to load and initialise a component */
935     omx_error = OMX_ErrorUndefined;
936     for(i = 0; i < p_sys->components; i++)
937     {
938 #ifdef __ANDROID__
939         /* ignore OpenCore software codecs */
940         if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
941             continue;
942         /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
943         if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
944             continue;
945 #endif
946         omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
947                                         &p_sys->omx_handle);
948         if(omx_error == OMX_ErrorNone) break;
949     }
950     CHECK_ERROR(omx_error, "no component could be initialised" );
951
952     /* Move component to Idle then Executing state */
953     OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
954     CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
955
956     /* Allocate omx buffers */
957     for(i = 0; i < p_sys->ports; i++)
958     {
959         OmxPort *p_port = &p_sys->p_ports[i];
960
961         for(j = 0; j < p_port->i_buffers; j++)
962         {
963 #if 0
964 #define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
965             char *p_buf = malloc(p_port->definition.nBufferSize +
966                                  p_port->definition.nBufferAlignment);
967             p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
968 #endif
969
970             if(0 && p_port->b_direct)
971                 omx_error =
972                     OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
973                                    p_port->i_port_index, 0,
974                                    p_port->definition.nBufferSize, (void*)1);
975             else
976                 omx_error =
977                     OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
978                                         p_port->i_port_index, 0,
979                                         p_port->definition.nBufferSize);
980
981             if(omx_error != OMX_ErrorNone) break;
982             OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
983         }
984         p_port->i_buffers = j;
985         CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
986                     omx_error, (int)p_port->i_port_index, j );
987     }
988
989     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
990     CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
991
992     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
993                                  OMX_StateExecuting, 0);
994     CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
995     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
996     CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
997
998     /* Send codec configuration data */
999     if( p_dec->fmt_in.i_extra )
1000     {
1001         OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1002         p_header->nFilledLen = p_dec->fmt_in.i_extra;
1003
1004         /* Convert H.264 NAL format to annex b */
1005         if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
1006         {
1007             p_header->nFilledLen = 0;
1008             convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
1009                              p_header->pBuffer, p_header->nAllocLen,
1010                              (uint32_t*) &p_header->nFilledLen, NULL );
1011         }
1012         else if(p_sys->in.b_direct)
1013         {
1014             p_header->pOutputPortPrivate = p_header->pBuffer;
1015             p_header->pBuffer = p_dec->fmt_in.p_extra;
1016         }
1017         else
1018         {
1019             if(p_header->nFilledLen > p_header->nAllocLen)
1020             {
1021                 msg_Dbg(p_dec, "buffer too small (%i,%i)", (int)p_header->nFilledLen,
1022                         (int)p_header->nAllocLen);
1023                 p_header->nFilledLen = p_header->nAllocLen;
1024             }
1025             memcpy(p_header->pBuffer, p_dec->fmt_in.p_extra, p_header->nFilledLen);
1026         }
1027
1028         p_header->nOffset = 0;
1029         p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1030         msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
1031                 p_header->pBuffer, (int)p_header->nFilledLen);
1032         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1033     }
1034
1035     /* Get back output port definition */
1036     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1037     if(omx_error != OMX_ErrorNone) goto error;
1038
1039     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1040     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1041
1042     if(p_sys->b_error) goto error;
1043
1044     p_dec->b_need_packetized = true;
1045     return VLC_SUCCESS;
1046
1047  error:
1048     CloseGeneric(p_this);
1049     return VLC_EGENERIC;
1050 }
1051
1052 /*****************************************************************************
1053  * PortReconfigure
1054  *****************************************************************************/
1055 static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
1056 {
1057     decoder_sys_t *p_sys = p_dec->p_sys;
1058     OMX_PARAM_PORTDEFINITIONTYPE definition;
1059     OMX_BUFFERHEADERTYPE *p_buffer;
1060     OMX_ERRORTYPE omx_error;
1061     unsigned int i;
1062
1063     /* Sanity checking */
1064     OMX_INIT_STRUCTURE(definition);
1065     definition.nPortIndex = p_port->i_port_index;
1066     omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1067                                  &definition);
1068     if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
1069        (!definition.format.video.nFrameWidth ||
1070        !definition.format.video.nFrameHeight)) )
1071         return OMX_ErrorUndefined;
1072
1073     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
1074                                  p_port->i_port_index, NULL);
1075     CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
1076                 (int)p_port->i_port_index, omx_error );
1077
1078     for(i = 0; i < p_port->i_buffers; i++)
1079     {
1080         OMX_FIFO_GET(&p_port->fifo, p_buffer);
1081         if (p_buffer == &p_sys->sentinel_buffer)
1082             continue;
1083         omx_error = OMX_FreeBuffer( p_sys->omx_handle,
1084                                     p_port->i_port_index, p_buffer );
1085
1086         if(omx_error != OMX_ErrorNone) break;
1087     }
1088     CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
1089                 omx_error, (int)p_port->i_port_index, i );
1090
1091     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1092     CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
1093
1094     /* Get the new port definition */
1095     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
1096     if(omx_error != OMX_ErrorNone) goto error;
1097
1098     if( p_dec->fmt_in.i_cat != AUDIO_ES )
1099     {
1100         /* Don't explicitly set the new parameters that we got with
1101          * OMX_GetParameter above when using audio codecs.
1102          * That struct hasn't been changed since, so there should be
1103          * no need to set it here, unless some codec expects the
1104          * SetParameter call as a trigger event for some part of
1105          * the reconfiguration.
1106          * This fixes using audio decoders on Samsung Galaxy S II,
1107          *
1108          * Only skipping this for audio codecs, to minimize the
1109          * change for current working configurations for video.
1110          */
1111         omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
1112                                      &definition);
1113         CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
1114                     omx_error, ErrorToString(omx_error));
1115     }
1116
1117     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
1118                                  p_port->i_port_index, NULL);
1119     CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
1120                 (int)p_port->i_port_index, omx_error );
1121
1122     if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
1123         free(p_port->pp_buffers);
1124         p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
1125         if(!p_port->pp_buffers)
1126         {
1127             omx_error = OMX_ErrorInsufficientResources;
1128             CHECK_ERROR(omx_error, "memory allocation failed");
1129         }
1130     }
1131     p_port->i_buffers = p_port->definition.nBufferCountActual;
1132     for(i = 0; i < p_port->i_buffers; i++)
1133     {
1134         if(0 && p_port->b_direct)
1135             omx_error =
1136                 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1137                                p_port->i_port_index, 0,
1138                                p_port->definition.nBufferSize, (void*)1);
1139         else
1140             omx_error =
1141                 OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
1142                                     p_port->i_port_index, 0,
1143                                     p_port->definition.nBufferSize);
1144
1145         if(omx_error != OMX_ErrorNone) break;
1146         OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
1147     }
1148     p_port->i_buffers = i;
1149     CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
1150                 omx_error, (int)p_port->i_port_index, i );
1151
1152     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
1153     CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
1154
1155     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
1156     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->out.i_port_index);
1157
1158  error:
1159     return omx_error;
1160 }
1161
1162 /*****************************************************************************
1163  * DecodeVideo: Called to decode one frame
1164  *****************************************************************************/
1165 static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
1166 {
1167     decoder_sys_t *p_sys = p_dec->p_sys;
1168     picture_t *p_pic = NULL, *p_next_pic;
1169     OMX_ERRORTYPE omx_error;
1170     unsigned int i;
1171
1172     OMX_BUFFERHEADERTYPE *p_header;
1173     block_t *p_block;
1174
1175     if( !pp_block || !*pp_block )
1176         return NULL;
1177
1178     p_block = *pp_block;
1179
1180     /* Check for errors from codec */
1181     if(p_sys->b_error)
1182     {
1183         msg_Dbg(p_dec, "error during decoding");
1184         block_Release( p_block );
1185         return 0;
1186     }
1187
1188     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1189     {
1190         block_Release( p_block );
1191         if(!p_sys->in.b_flushed)
1192         {
1193             msg_Dbg(p_dec, "flushing");
1194             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1195                              p_sys->in.definition.nPortIndex, 0 );
1196         }
1197         p_sys->in.b_flushed = true;
1198         return NULL;
1199     }
1200
1201     /* Take care of decoded frames first */
1202     while(!p_pic)
1203     {
1204         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1205         if(!p_header) break; /* No frame available */
1206
1207         if(p_header->nFilledLen)
1208         {
1209             p_pic = p_header->pAppPrivate;
1210             if(!p_pic)
1211             {
1212                 /* We're not in direct rendering mode.
1213                  * Get a new picture and copy the content */
1214                 p_pic = decoder_NewPicture( p_dec );
1215                 if( !p_pic ) break; /* No picture available */
1216
1217                 CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
1218             }
1219
1220             p_pic->date = p_header->nTimeStamp;
1221             p_header->nFilledLen = 0;
1222             p_header->pAppPrivate = 0;
1223         }
1224
1225         /* Get a new picture */
1226         if(p_sys->in.b_direct && !p_header->pAppPrivate)
1227         {
1228             p_next_pic = decoder_NewPicture( p_dec );
1229             if(!p_next_pic) break;
1230
1231             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1232             p_header->pAppPrivate = p_next_pic;
1233             p_header->pInputPortPrivate = p_header->pBuffer;
1234             p_header->pBuffer = p_next_pic->p[0].p_pixels;
1235         }
1236         else
1237         {
1238             OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1239         }
1240
1241 #ifdef OMXIL_EXTRA_DEBUG
1242         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1243 #endif
1244         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1245     }
1246
1247     /* Send the input buffer to the component */
1248     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1249
1250     if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1251         goto reconfig;
1252
1253     if(p_header)
1254     {
1255         p_header->nFilledLen = p_block->i_buffer;
1256         p_header->nOffset = 0;
1257         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1258         p_header->nTimeStamp = p_block->i_dts;
1259
1260         /* In direct mode we pass the input pointer as is.
1261          * Otherwise we memcopy the data */
1262         if(p_sys->in.b_direct)
1263         {
1264             p_header->pOutputPortPrivate = p_header->pBuffer;
1265             p_header->pBuffer = p_block->p_buffer;
1266             p_header->pAppPrivate = p_block;
1267         }
1268         else
1269         {
1270             if(p_header->nFilledLen > p_header->nAllocLen)
1271             {
1272                 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1273                         (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1274                 p_header->nFilledLen = p_header->nAllocLen;
1275             }
1276             memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1277             block_Release(p_block);
1278         }
1279
1280         /* Convert H.264 NAL format to annex b */
1281         if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
1282         {
1283             /* This only works for NAL sizes 3-4 */
1284             int i_len = p_header->nFilledLen, i;
1285             uint8_t* ptr = p_header->pBuffer;
1286             while( i_len >= p_sys->i_nal_size_length )
1287             {
1288                 uint32_t nal_len = 0;
1289                 for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
1290                     nal_len = (nal_len << 8) | ptr[i];
1291                     ptr[i] = 0;
1292                 }
1293                 ptr[p_sys->i_nal_size_length - 1] = 1;
1294                 if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
1295                     break;
1296                 ptr   += nal_len + 4;
1297                 i_len -= nal_len + 4;
1298             }
1299         }
1300 #ifdef OMXIL_EXTRA_DEBUG
1301         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1302                  (int)p_header->nFilledLen );
1303 #endif
1304         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1305         p_sys->in.b_flushed = false;
1306         *pp_block = NULL; /* Avoid being fed the same packet again */
1307     }
1308
1309 reconfig:
1310     /* Handle the PortSettingsChanged events */
1311     for(i = 0; i < p_sys->ports; i++)
1312     {
1313         OmxPort *p_port = &p_sys->p_ports[i];
1314         if(!p_port->b_reconfigure) continue;
1315         p_port->b_reconfigure = 0;
1316         omx_error = PortReconfigure(p_dec, p_port);
1317     }
1318
1319     return p_pic;
1320 }
1321
1322 /*****************************************************************************
1323  * DecodeAudio: Called to decode one frame
1324  *****************************************************************************/
1325 aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
1326 {
1327     decoder_sys_t *p_sys = p_dec->p_sys;
1328     aout_buffer_t *p_buffer = 0;
1329     OMX_BUFFERHEADERTYPE *p_header;
1330     OMX_ERRORTYPE omx_error;
1331     block_t *p_block;
1332     unsigned int i;
1333
1334     if( !pp_block || !*pp_block ) return NULL;
1335
1336     p_block = *pp_block;
1337
1338     /* Check for errors from codec */
1339     if(p_sys->b_error)
1340     {
1341         msg_Dbg(p_dec, "error during decoding");
1342         block_Release( p_block );
1343         return 0;
1344     }
1345
1346     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
1347     {
1348         block_Release( p_block );
1349         date_Set( &p_sys->end_date, 0 );
1350         if(!p_sys->in.b_flushed)
1351         {
1352             msg_Dbg(p_dec, "flushing");
1353             OMX_SendCommand( p_sys->omx_handle, OMX_CommandFlush,
1354                              p_sys->in.definition.nPortIndex, 0 );
1355         }
1356         p_sys->in.b_flushed = true;
1357         return NULL;
1358     }
1359
1360     if( !date_Get( &p_sys->end_date ) )
1361     {
1362         if( !p_block->i_pts )
1363         {
1364             /* We've just started the stream, wait for the first PTS. */
1365             block_Release( p_block );
1366             return NULL;
1367         }
1368         date_Set( &p_sys->end_date, p_block->i_pts );
1369     }
1370
1371     /* Take care of decoded frames first */
1372     while(!p_buffer)
1373     {
1374         unsigned int i_samples;
1375
1376         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
1377         if(!p_header) break; /* No frame available */
1378
1379         i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
1380         if(i_samples)
1381         {
1382             p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
1383             if( !p_buffer ) break; /* No audio buffer available */
1384
1385             memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
1386             p_header->nFilledLen = 0;
1387
1388             if( p_header->nTimeStamp != 0 &&
1389                 p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
1390                 date_Set( &p_sys->end_date, p_header->nTimeStamp );
1391
1392             p_buffer->i_pts = date_Get( &p_sys->end_date );
1393             p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
1394                 p_buffer->i_pts;
1395         }
1396
1397 #ifdef OMXIL_EXTRA_DEBUG
1398         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1399 #endif
1400         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1401         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1402     }
1403
1404
1405     /* Send the input buffer to the component */
1406     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1407
1408     if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
1409         goto reconfig;
1410
1411     if(p_header)
1412     {
1413         p_header->nFilledLen = p_block->i_buffer;
1414         p_header->nOffset = 0;
1415         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1416         p_header->nTimeStamp = p_block->i_dts;
1417
1418         /* In direct mode we pass the input pointer as is.
1419          * Otherwise we memcopy the data */
1420         if(p_sys->in.b_direct)
1421         {
1422             p_header->pOutputPortPrivate = p_header->pBuffer;
1423             p_header->pBuffer = p_block->p_buffer;
1424             p_header->pAppPrivate = p_block;
1425         }
1426         else
1427         {
1428             if(p_header->nFilledLen > p_header->nAllocLen)
1429             {
1430                 msg_Dbg(p_dec, "buffer too small (%i,%i)",
1431                         (int)p_header->nFilledLen, (int)p_header->nAllocLen);
1432                 p_header->nFilledLen = p_header->nAllocLen;
1433             }
1434             memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
1435             block_Release(p_block);
1436         }
1437
1438 #ifdef OMXIL_EXTRA_DEBUG
1439         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1440                  (int)p_header->nFilledLen );
1441 #endif
1442         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1443         p_sys->in.b_flushed = false;
1444         *pp_block = NULL; /* Avoid being fed the same packet again */
1445     }
1446
1447 reconfig:
1448     /* Handle the PortSettingsChanged events */
1449     for(i = 0; i < p_sys->ports; i++)
1450     {
1451         OmxPort *p_port = &p_sys->p_ports[i];
1452         if(!p_port->b_reconfigure) continue;
1453         p_port->b_reconfigure = 0;
1454         omx_error = PortReconfigure(p_dec, p_port);
1455     }
1456
1457     return p_buffer;
1458 }
1459
1460 /*****************************************************************************
1461  * EncodeVideo: Called to encode one frame
1462  *****************************************************************************/
1463 static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
1464 {
1465     decoder_t *p_dec = ( decoder_t *)p_enc;
1466     decoder_sys_t *p_sys = p_dec->p_sys;
1467     OMX_ERRORTYPE omx_error;
1468     unsigned int i;
1469
1470     OMX_BUFFERHEADERTYPE *p_header;
1471     block_t *p_block = 0;
1472
1473     if( !p_pic ) return NULL;
1474
1475     /* Check for errors from codec */
1476     if(p_sys->b_error)
1477     {
1478         msg_Dbg(p_dec, "error during encoding");
1479         return NULL;
1480     }
1481
1482     /* Send the input buffer to the component */
1483     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
1484     if(p_header)
1485     {
1486         /* In direct mode we pass the input pointer as is.
1487          * Otherwise we memcopy the data */
1488         if(p_sys->in.b_direct)
1489         {
1490             p_header->pOutputPortPrivate = p_header->pBuffer;
1491             p_header->pBuffer = p_pic->p[0].p_pixels;
1492         }
1493         else
1494         {
1495             CopyVlcPicture(p_dec, p_header, p_pic);
1496         }
1497
1498         p_header->nFilledLen = p_sys->in.i_frame_size;
1499         p_header->nOffset = 0;
1500         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
1501         p_header->nTimeStamp = p_pic->date;
1502 #ifdef OMXIL_EXTRA_DEBUG
1503         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
1504                  (int)p_header->nFilledLen );
1505 #endif
1506         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
1507         p_sys->in.b_flushed = false;
1508     }
1509
1510     /* Handle the PortSettingsChanged events */
1511     for(i = 0; i < p_sys->ports; i++)
1512     {
1513         OmxPort *p_port = &p_sys->p_ports[i];
1514         if(!p_port->b_reconfigure) continue;
1515         p_port->b_reconfigure = 0;
1516         omx_error = PortReconfigure(p_dec, p_port);
1517     }
1518
1519     /* Wait for the decoded frame */
1520     while(!p_block)
1521     {
1522         OMX_FIFO_GET(&p_sys->out.fifo, p_header);
1523
1524         if(p_header->nFilledLen)
1525         {
1526             if(p_header->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
1527             {
1528                 /* TODO: need to store codec config */
1529                 msg_Dbg(p_dec, "received codec config %i", (int)p_header->nFilledLen);
1530             }
1531
1532             p_block = p_header->pAppPrivate;
1533             if(!p_block)
1534             {
1535                 /* We're not in direct rendering mode.
1536                  * Get a new block and copy the content */
1537                 p_block = block_New( p_dec, p_header->nFilledLen );
1538                 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
1539             }
1540
1541             p_block->i_buffer = p_header->nFilledLen;
1542             p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
1543             p_header->nFilledLen = 0;
1544             p_header->pAppPrivate = 0;
1545         }
1546
1547 #ifdef OMXIL_EXTRA_DEBUG
1548         msg_Dbg( p_dec, "FillThisBuffer %p, %p", p_header, p_header->pBuffer );
1549 #endif
1550         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
1551     }
1552
1553     msg_Dbg(p_dec, "done");
1554     return p_block;
1555 }
1556
1557 /*****************************************************************************
1558  * CloseGeneric: omxil decoder destruction
1559  *****************************************************************************/
1560 static void CloseGeneric( vlc_object_t *p_this )
1561 {
1562     decoder_t *p_dec = (decoder_t *)p_this;
1563     decoder_sys_t *p_sys = p_dec->p_sys;
1564
1565     if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
1566     vlc_mutex_lock( &omx_core_mutex );
1567     omx_refcount--;
1568     if( omx_refcount == 0 )
1569     {
1570         if( p_sys->b_init ) pf_deinit();
1571         dll_close( dll_handle );
1572     }
1573     vlc_mutex_unlock( &omx_core_mutex );
1574
1575     vlc_mutex_destroy (&p_sys->mutex);
1576     vlc_cond_destroy (&p_sys->cond);
1577     vlc_mutex_destroy (&p_sys->in.fifo.lock);
1578     vlc_cond_destroy (&p_sys->in.fifo.wait);
1579     vlc_mutex_destroy (&p_sys->out.fifo.lock);
1580     vlc_cond_destroy (&p_sys->out.fifo.wait);
1581
1582     free( p_sys );
1583 }
1584
1585 /*****************************************************************************
1586  * OmxEventHandler: 
1587  *****************************************************************************/
1588 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
1589     OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data_1,
1590     OMX_U32 data_2, OMX_PTR event_data )
1591 {
1592     decoder_t *p_dec = (decoder_t *)app_data;
1593     decoder_sys_t *p_sys = p_dec->p_sys;
1594     unsigned int i;
1595     (void)omx_handle;
1596
1597     switch (event)
1598     {
1599     case OMX_EventCmdComplete:
1600         switch ((OMX_STATETYPE)data_1)
1601         {
1602         case OMX_CommandStateSet:
1603             msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
1604                      CommandToString(data_1), StateToString(data_2) );
1605             break;
1606
1607         default:
1608             msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
1609                      CommandToString(data_1), (unsigned int)data_2 );
1610             break;
1611         }
1612         break;
1613
1614     case OMX_EventError:
1615         msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
1616                  ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
1617                  (const char *)event_data);
1618         //p_sys->b_error = true;
1619         break;
1620
1621     case OMX_EventPortSettingsChanged:
1622         msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1623                  (unsigned int)data_1, (unsigned int)data_2 );
1624         for(i = 0; i < p_sys->ports; i++)
1625             if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
1626                 p_sys->p_ports[i].b_reconfigure = true;
1627         memset(&p_sys->sentinel_buffer, 0, sizeof(p_sys->sentinel_buffer));
1628         p_sys->sentinel_buffer.nFlags = OMX_BUFFERFLAG_EOS;
1629         OMX_FIFO_PUT(&p_sys->in.fifo, &p_sys->sentinel_buffer);
1630         break;
1631
1632     default:
1633         msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
1634                  (unsigned int)data_1, (unsigned int)data_2 );
1635         break;
1636     }
1637
1638     PostOmxEvent(p_dec, event, data_1, data_2, event_data);
1639     return OMX_ErrorNone;
1640 }
1641
1642 static OMX_ERRORTYPE OmxEmptyBufferDone( OMX_HANDLETYPE omx_handle,
1643     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1644 {
1645     decoder_t *p_dec = (decoder_t *)app_data;
1646     decoder_sys_t *p_sys = p_dec->p_sys;
1647     (void)omx_handle;
1648
1649 #ifdef OMXIL_EXTRA_DEBUG
1650     msg_Dbg( p_dec, "OmxEmptyBufferDone %p, %p", omx_header, omx_header->pBuffer );
1651 #endif
1652
1653     if(omx_header->pAppPrivate || omx_header->pOutputPortPrivate)
1654     {
1655         block_t *p_block = (block_t *)omx_header->pAppPrivate;
1656         omx_header->pBuffer = omx_header->pOutputPortPrivate;
1657         if(p_block) block_Release(p_block);
1658         omx_header->pAppPrivate = 0;
1659     }
1660     OMX_FIFO_PUT(&p_sys->in.fifo, omx_header);
1661
1662     return OMX_ErrorNone;
1663 }
1664
1665 static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
1666     OMX_PTR app_data, OMX_BUFFERHEADERTYPE *omx_header )
1667 {
1668     decoder_t *p_dec = (decoder_t *)app_data;
1669     decoder_sys_t *p_sys = p_dec->p_sys;
1670     (void)omx_handle;
1671
1672 #ifdef OMXIL_EXTRA_DEBUG
1673     msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
1674              (int)omx_header->nFilledLen );
1675 #endif
1676
1677     if(omx_header->pInputPortPrivate)
1678     {
1679         omx_header->pBuffer = omx_header->pInputPortPrivate;
1680     }
1681     OMX_FIFO_PUT(&p_sys->out.fifo, omx_header);
1682
1683     return OMX_ErrorNone;
1684 }