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