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