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