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