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