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