]> git.sesse.net Git - vlc/blobdiff - modules/codec/omxil/omxil.c
mediacodec: Support playing back VP8 as well
[vlc] / modules / codec / omxil / omxil.c
index 748da401e26f457ff74fff434ee6731f2e4a7f69..0fafedf068710aa66bf92de30ff546bb0ce2b03a 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * omxil.c: Video decoder module making use of OpenMAX IL components.
  *****************************************************************************
- * Copyright (C) 2010 the VideoLAN team
+ * Copyright (C) 2010 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 # include "config.h"
 #endif
 
-#include <dlfcn.h>
-#define dll_open(name) dlopen( name, RTLD_NOW )
-#define dll_close(handle) dlclose(handle)
+#include <limits.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_codec.h>
-#include <vlc_aout.h>
 #include <vlc_block_helper.h>
 #include <vlc_cpu.h>
+#include "../h264_nal.h"
 
 #include "omxil.h"
+#include "omxil_core.h"
+#include "OMX_Broadcom.h"
 
-//#define OMXIL_EXTRA_DEBUG
+#ifndef NDEBUG
+# define OMXIL_EXTRA_DEBUG
+#endif
 
-/*****************************************************************************
- * List of OpenMAX IL core we will try in order
- *****************************************************************************/
-static const char *ppsz_dll_list[] =
-{
-    "libOMX_Core.so", /* TI OMAP IL core */
-    "libomxil-bellagio.so",  /* Bellagio IL core */
-    0
-};
+#define SENTINEL_FLAG 0x10000
+
+/* Defined in the broadcom version of OMX_Index.h */
+#define OMX_IndexConfigRequestCallback 0x7f000063
+#define OMX_IndexParamBrcmPixelAspectRatio 0x7f00004d
+
+/* Defined in the broadcom version of OMX_Core.h */
+#define OMX_EventParamOrConfigChanged 0x7F000001
 
 /*****************************************************************************
  * Local prototypes
@@ -62,7 +63,7 @@ static int  OpenGeneric( vlc_object_t *, bool b_encode );
 static void CloseGeneric( vlc_object_t * );
 
 static picture_t *DecodeVideo( decoder_t *, block_t ** );
-static aout_buffer_t *DecodeAudio ( decoder_t *, block_t ** );
+static block_t *DecodeAudio ( decoder_t *, block_t ** );
 static block_t *EncodeVideo( encoder_t *, picture_t * );
 
 static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE, OMX_PTR, OMX_EVENTTYPE,
@@ -80,10 +81,13 @@ vlc_module_begin ()
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_VCODEC )
     set_section( N_("Decoding") , NULL )
-#ifdef HAVE_MAEMO
-    set_capability( "decoder", 80 )
-#else
+#if defined(USE_IOMX)
+    /* For IOMX, don't enable it automatically via priorities,
+     * enable it only via the --codec iomx command line parameter when
+     * wanted. */
     set_capability( "decoder", 0 )
+#else
+    set_capability( "decoder", 80 )
 #endif
     set_callbacks( OpenDecoder, CloseGeneric )
 
@@ -94,73 +98,6 @@ vlc_module_begin ()
     set_callbacks( OpenEncoder, CloseGeneric )
 vlc_module_end ()
 
-/*****************************************************************************
- * CreateComponentsList: creates a list of components matching the given role
- *****************************************************************************/
-static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
-{
-    decoder_sys_t *p_sys = p_dec->p_sys;
-    char psz_name[OMX_MAX_STRINGNAME_SIZE];
-    OMX_ERRORTYPE omx_error;
-    OMX_U32 roles = 0;
-    OMX_U8 **ppsz_roles = 0;
-    unsigned int i, j, len;
-
-    if(!psz_role) goto end;
-    len = strlen(psz_role);
-
-    for( i = 0; ; i++ )
-    {
-        bool b_found = false;
-
-        omx_error = p_sys->pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
-        if(omx_error != OMX_ErrorNone) break;
-
-        msg_Dbg(p_dec, "component %s", psz_name);
-
-        omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, 0);
-        if(omx_error != OMX_ErrorNone || !roles) continue;
-
-        ppsz_roles = malloc(roles * (sizeof(OMX_U8*) + OMX_MAX_STRINGNAME_SIZE));
-        if(!ppsz_roles) continue;
-
-        for( j = 0; j < roles; j++ )
-            ppsz_roles[j] = ((OMX_U8 *)(&ppsz_roles[roles])) +
-                j * OMX_MAX_STRINGNAME_SIZE;
-
-        omx_error = p_sys->pf_get_roles_of_component(psz_name, &roles, ppsz_roles);
-        if(omx_error != OMX_ErrorNone) roles = 0;
-
-        for(j = 0; j < roles; j++)
-        {
-            msg_Dbg(p_dec, "  - role: %s", ppsz_roles[j]);
-            if(!strncmp((char *)ppsz_roles[j], psz_role, len)) b_found = true;
-        }
-
-        free(ppsz_roles);
-
-        if(!b_found) continue;
-
-        if(p_sys->components >= MAX_COMPONENTS_LIST_SIZE)
-        {
-            msg_Dbg(p_dec, "too many matching components");
-            continue;
-        }
-
-        strncpy(p_sys->ppsz_components[p_sys->components], psz_name,
-                OMX_MAX_STRINGNAME_SIZE-1);
-        p_sys->components++;
-    }
-
- end:
-    msg_Dbg(p_dec, "found %i matching components for role %s",
-            p_sys->components, psz_role);
-    for( i = 0; i < p_sys->components; i++ )
-        msg_Dbg(p_dec, "- %s", p_sys->ppsz_components[i]);
-
-    return p_sys->components;
-}
-
 /*****************************************************************************
  * ImplementationSpecificWorkarounds: place-holder for implementation
  * specific workarounds
@@ -239,6 +176,27 @@ static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
             def->format.video.xFramerate >>= 16;
         }
     }
+#if 0 /* FIXME: doesn't apply for HP Touchpad */
+    else if (!strncmp(p_sys->psz_component, "OMX.qcom.video.decoder.",
+                      strlen("OMX.qcom.video.decoder")))
+    {
+        /* qdsp6 refuses buffer size larger than 450K on input port */
+        if (def->nBufferSize > 450 * 1024)
+        {
+            def->nBufferSize = 450 * 1024;
+            p_port->i_frame_size = def->nBufferSize;
+        }
+    }
+#endif
+#ifdef RPI_OMX
+    else if (!strcmp(p_sys->psz_component, "OMX.broadcom.video_decode"))
+    {
+        /* Clear these fields before setting parameters, to allow the codec
+         * fill in what it wants (instead of rejecting whatever happened to
+         * be there. */
+        def->format.video.nStride = def->format.video.nSliceHeight = 0;
+    }
+#endif
 
     return OMX_ErrorNone;
 }
@@ -271,8 +229,9 @@ static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
 
         if(def->eDir == OMX_DirInput || p_dec->p_sys->b_enc)
         {
-            def->nBufferSize = def->format.video.nFrameWidth *
-              def->format.video.nFrameHeight * 2;
+            if (def->eDir == OMX_DirInput && p_dec->p_sys->b_enc)
+                def->nBufferSize = def->format.video.nFrameWidth *
+                  def->format.video.nFrameHeight * 2;
             p_port->i_frame_size = def->nBufferSize;
 
             if(!GetOmxVideoFormat(p_fmt->i_codec,
@@ -309,7 +268,8 @@ static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
                                &p_port->i_frame_size, &p_port->i_frame_stride,
                                &p_port->i_frame_stride_chroma_div );
             def->format.video.nStride = p_port->i_frame_stride;
-            def->nBufferSize = p_port->i_frame_size;
+            if (p_port->i_frame_size > def->nBufferSize)
+                def->nBufferSize = p_port->i_frame_size;
         }
         break;
 
@@ -327,7 +287,7 @@ static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
         }
         else
         {
-            if( !GetVlcAudioFormat(def->format.audio.eEncoding,
+            if( !OmxToVlcAudioFormat(def->format.audio.eEncoding,
                                    &p_fmt->i_codec, 0 ) )
             {
                 omx_error = OMX_ErrorNotImplemented;
@@ -364,27 +324,73 @@ static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
         omx_error = SetAudioParameters(p_port->omx_handle,
                                        &p_port->format_param, def->nPortIndex,
                                        def->format.audio.eEncoding,
+                                       p_fmt->i_codec,
                                        p_fmt->audio.i_channels,
                                        p_fmt->audio.i_rate,
                                        p_fmt->i_bitrate,
                                        p_fmt->audio.i_bitspersample,
                                        p_fmt->audio.i_blockalign);
-        CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
-                    omx_error, ErrorToString(omx_error));
+        if (def->eDir == OMX_DirInput) {
+            CHECK_ERROR(omx_error, "SetAudioParameters failed (%x : %s)",
+                        omx_error, ErrorToString(omx_error));
+        } else if (omx_error != OMX_ErrorNone) {
+            msg_Warn(p_dec, "SetAudioParameters failed (%x : %s) on output port",
+                     omx_error, ErrorToString(omx_error));
+            omx_error = OMX_ErrorNone;
+        }
+    }
+    if (!strcmp(p_dec->p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER") &&
+                def->eDir == OMX_DirOutput)
+    {
+        /* When setting the output buffer size above, the decoder actually
+         * sets the buffer size to a lower value than what was chosen. If
+         * we try to allocate buffers of this size, it fails. Thus, forcibly
+         * use a larger buffer size. */
+        def->nBufferSize *= 2;
     }
 
  error:
     return omx_error;
 }
 
+
+/*****************************************************************************
+ * UpdatePixelAspect: Update vlc pixel aspect based on the aspect reported on
+ * the omx port - NOTE: Broadcom specific
+ *****************************************************************************/
+static OMX_ERRORTYPE UpdatePixelAspect(decoder_t *p_dec)
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    OMX_CONFIG_POINTTYPE pixel_aspect;
+    OMX_INIT_STRUCTURE(pixel_aspect);
+    OMX_ERRORTYPE omx_err;
+
+    if (strncmp(p_sys->psz_component, "OMX.broadcom.", 13))
+        return OMX_ErrorNotImplemented;
+
+    pixel_aspect.nPortIndex = p_sys->out.i_port_index;
+    omx_err = OMX_GetParameter(p_sys->omx_handle,
+            OMX_IndexParamBrcmPixelAspectRatio, &pixel_aspect);
+    if (omx_err != OMX_ErrorNone) {
+        msg_Warn(p_dec, "Failed to retrieve aspect ratio");
+    } else {
+        p_dec->fmt_out.video.i_sar_num = pixel_aspect.nX;
+        p_dec->fmt_out.video.i_sar_den = pixel_aspect.nY;
+    }
+
+    return omx_err;
+}
+
 /*****************************************************************************
  * GetPortDefinition: set vlc format based on the definition of the omx port
  *****************************************************************************/
 static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
                                        es_format_t *p_fmt)
 {
+    decoder_sys_t *p_sys = p_dec->p_sys;
     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
     OMX_ERRORTYPE omx_error;
+    OMX_CONFIG_RECTTYPE crop_rect;
 
     omx_error = OMX_GetParameter(p_port->omx_handle,
                                  OMX_IndexParamPortDefinition, def);
@@ -401,6 +407,41 @@ static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
         p_fmt->video.i_frame_rate = p_dec->fmt_in.video.i_frame_rate;
         p_fmt->video.i_frame_rate_base = p_dec->fmt_in.video.i_frame_rate_base;
 
+        OMX_INIT_STRUCTURE(crop_rect);
+        crop_rect.nPortIndex = def->nPortIndex;
+        omx_error = OMX_GetConfig(p_port->omx_handle, OMX_IndexConfigCommonOutputCrop, &crop_rect);
+        if (omx_error == OMX_ErrorNone)
+        {
+            if (!def->format.video.nSliceHeight)
+                def->format.video.nSliceHeight = def->format.video.nFrameHeight;
+            if (!def->format.video.nStride)
+                def->format.video.nStride = def->format.video.nFrameWidth;
+            p_fmt->video.i_width = crop_rect.nWidth;
+            p_fmt->video.i_visible_width = crop_rect.nWidth;
+            p_fmt->video.i_height = crop_rect.nHeight;
+            p_fmt->video.i_visible_height = crop_rect.nHeight;
+            if (def->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
+                def->format.video.nSliceHeight -= crop_rect.nTop/2;
+        }
+        else
+        {
+            /* Don't pass the error back to the caller, this isn't mandatory */
+            omx_error = OMX_ErrorNone;
+        }
+
+        /* Hack: Nexus One (stock firmware with binary OMX driver blob)
+         * claims to output 420Planar even though it in in practice is
+         * NV21. */
+        if(def->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar &&
+           !strncmp(p_sys->psz_component, "OMX.qcom.video.decoder",
+                    strlen("OMX.qcom.video.decoder")))
+            def->format.video.eColorFormat = OMX_QCOM_COLOR_FormatYVU420SemiPlanar;
+
+        if (IgnoreOmxDecoderPadding(p_sys->psz_component)) {
+            def->format.video.nSliceHeight = 0;
+            def->format.video.nStride = p_fmt->video.i_width;
+        }
+
         if(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
                                &p_fmt->i_codec, 0 ) )
         {
@@ -425,10 +466,11 @@ static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
             def->format.video.nStride = p_port->i_frame_stride;
 #endif
         p_port->i_frame_stride = def->format.video.nStride;
+        UpdatePixelAspect(p_dec);
         break;
 
     case AUDIO_ES:
-        if( !GetVlcAudioFormat( def->format.audio.eEncoding,
+        if( !OmxToVlcAudioFormat( def->format.audio.eEncoding,
                                 &p_fmt->i_codec, 0 ) )
         {
             omx_error = OMX_ErrorNotImplemented;
@@ -503,8 +545,15 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
                                      OMX_StateIdle, 0 );
         CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
-        omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
-        CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
+        while (1) {
+            OMX_U32 cmd, state;
+            omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, &cmd, &state, 0);
+            CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
+            // The event queue can contain other OMX_EventCmdComplete items,
+            // such as for OMX_CommandFlush
+            if (cmd == OMX_CommandStateSet && state == OMX_StateIdle)
+                break;
+        }
     }
 
     omx_error = OMX_GetState(omx_handle, &state);
@@ -524,6 +573,11 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
             for(j = 0; j < p_port->i_buffers; j++)
             {
                 OMX_FIFO_GET(&p_port->fifo, p_buffer);
+                if (p_buffer->nFlags & SENTINEL_FLAG) {
+                    free(p_buffer);
+                    j--;
+                    continue;
+                }
                 omx_error = OMX_FreeBuffer( omx_handle,
                                             p_port->i_port_index, p_buffer );
 
@@ -531,9 +585,20 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
             }
             CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
                         omx_error, (int)p_port->i_port_index, j );
+            while (1) {
+                OMX_FIFO_PEEK(&p_port->fifo, p_buffer);
+                if (!p_buffer) break;
+
+                OMX_FIFO_GET(&p_port->fifo, p_buffer);
+                if (p_buffer->nFlags & SENTINEL_FLAG) {
+                    free(p_buffer);
+                    continue;
+                }
+                msg_Warn( p_dec, "Stray buffer left in fifo, %p", p_buffer );
+            }
         }
 
-        omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
+        omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
         CHECK_ERROR(omx_error, "Wait for Loaded failed (%x)", omx_error );
     }
 
@@ -544,7 +609,7 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
         free(p_port->pp_buffers);
         p_port->pp_buffers = 0;
     }
-    omx_error = p_sys->pf_free_handle( omx_handle );
+    omx_error = pf_free_handle( omx_handle );
     return omx_error;
 }
 
@@ -566,8 +631,7 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
     OMX_PORT_PARAM_TYPE param;
 
     /* Load component */
-    omx_error = p_sys->pf_get_handle( &omx_handle, psz_component,
-                                      p_dec, &callbacks );
+    omx_error = pf_get_handle( &omx_handle, psz_component, p_dec, &callbacks );
     if(omx_error != OMX_ErrorNone)
     {
         msg_Warn( p_dec, "OMX_GetHandle(%s) failed (%x: %s)", psz_component,
@@ -576,8 +640,11 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
     }
     strncpy(p_sys->psz_component, psz_component, OMX_MAX_STRINGNAME_SIZE-1);
 
-    OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
-    msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
+    omx_error = OMX_ComponentRoleEnum(omx_handle, psz_role, 0);
+    if(omx_error == OMX_ErrorNone)
+        msg_Dbg(p_dec, "loaded component %s of role %s", psz_component, psz_role);
+    else
+        msg_Dbg(p_dec, "loaded component %s", psz_component);
     PrintOmx(p_dec, omx_handle, OMX_ALL);
 
     /* Set component role */
@@ -598,7 +665,14 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
     OMX_INIT_STRUCTURE(definition);
     omx_error = OMX_GetParameter(omx_handle, p_dec->fmt_in.i_cat == VIDEO_ES ?
                                  OMX_IndexParamVideoInit : OMX_IndexParamAudioInit, &param);
-    if(omx_error != OMX_ErrorNone) param.nPorts = 0;
+    if(omx_error != OMX_ErrorNone) {
+#ifdef __ANDROID__
+        param.nPorts = 2;
+        param.nStartPortNumber = 0;
+#else
+        param.nPorts = 0;
+#endif
+    }
 
     for(i = 0; i < param.nPorts; i++)
     {
@@ -625,6 +699,44 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
         CHECK_ERROR(omx_error, "couldn't find an input and output port");
     }
 
+    if(!strncmp(p_sys->psz_component, "OMX.SEC.", 8) &&
+       p_dec->fmt_in.i_cat == VIDEO_ES)
+    {
+        OMX_INDEXTYPE index;
+        omx_error = OMX_GetExtensionIndex(omx_handle, (OMX_STRING) "OMX.SEC.index.ThumbnailMode", &index);
+        if(omx_error == OMX_ErrorNone)
+        {
+            OMX_BOOL enable = OMX_TRUE;
+            omx_error = OMX_SetConfig(omx_handle, index, &enable);
+            CHECK_ERROR(omx_error, "Unable to set ThumbnailMode");
+        } else {
+            OMX_BOOL enable = OMX_TRUE;
+            /* Needed on Samsung Galaxy S II */
+            omx_error = OMX_SetConfig(omx_handle, OMX_IndexVendorSetYUV420pMode, &enable);
+            if (omx_error == OMX_ErrorNone)
+                msg_Dbg(p_dec, "Set OMX_IndexVendorSetYUV420pMode successfully");
+            else
+                msg_Dbg(p_dec, "Unable to set OMX_IndexVendorSetYUV420pMode: %x", omx_error);
+        }
+    }
+
+    if(!strncmp(p_sys->psz_component, "OMX.broadcom.", 13))
+    {
+        OMX_CONFIG_REQUESTCALLBACKTYPE notifications;
+        OMX_INIT_STRUCTURE(notifications);
+
+        notifications.nPortIndex = p_sys->out.i_port_index;
+        notifications.nIndex = OMX_IndexParamBrcmPixelAspectRatio;
+        notifications.bEnable = OMX_TRUE;
+
+        omx_error = OMX_SetParameter(omx_handle,
+                OMX_IndexConfigRequestCallback, &notifications);
+        if (omx_error == OMX_ErrorNone)
+            msg_Dbg(p_dec, "Enabled aspect ratio notifications");
+        else
+            msg_Dbg(p_dec, "Could not enable aspect ratio notifications");
+    }
+
     /* Set port definitions */
     for(i = 0; i < p_sys->ports; i++)
     {
@@ -655,7 +767,7 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
                                          p_port->i_port_index, NULL);
             CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
                         (int)p_port->i_port_index, omx_error );
-            omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
+            omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
             CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
                         (int)p_port->i_port_index, omx_error );
         }
@@ -681,11 +793,6 @@ static int OpenDecoder( vlc_object_t *p_this )
     if( 0 || !GetOmxRole(p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat, false) )
         return VLC_EGENERIC;
 
-#ifdef HAVE_MAEMO
-    if( p_dec->fmt_in.i_cat != VIDEO_ES && !p_dec->b_force)
-        return VLC_EGENERIC;
-#endif
-
     status = OpenGeneric( p_this, false );
     if(status != VLC_SUCCESS) return status;
 
@@ -719,47 +826,20 @@ static int OpenEncoder( vlc_object_t *p_this )
  *****************************************************************************/
 static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
 {
-    void *dll_handle = 0, *pf_init = 0, *pf_deinit = 0;
-    void *pf_get_handle = 0, *pf_free_handle = 0, *pf_component_enum = 0;
-    void *pf_get_roles_of_component = 0;
     decoder_t *p_dec = (decoder_t*)p_this;
     decoder_sys_t *p_sys;
     OMX_ERRORTYPE omx_error;
     OMX_BUFFERHEADERTYPE *p_header;
     unsigned int i, j;
 
-    /* Load the OMX core */
-    for( i = 0; ppsz_dll_list[i]; i++ )
-    {
-        dll_handle = dll_open( ppsz_dll_list[i] );
-        if( dll_handle ) break;
-    }
-    if( !dll_handle ) return VLC_EGENERIC;
-
-    pf_init = dlsym( dll_handle, "OMX_Init" );
-    pf_deinit = dlsym( dll_handle, "OMX_Deinit" );
-    pf_get_handle = dlsym( dll_handle, "OMX_GetHandle" );
-    pf_free_handle = dlsym( dll_handle, "OMX_FreeHandle" );
-    pf_component_enum = dlsym( dll_handle, "OMX_ComponentNameEnum" );
-    pf_get_roles_of_component = dlsym( dll_handle, "OMX_GetRolesOfComponent" );
-    if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle ||
-        !pf_component_enum || !pf_get_roles_of_component )
-    {
-        msg_Warn( p_this, "cannot find OMX_* symbols in `%s' (%s)",
-                  ppsz_dll_list[i], dlerror() );
-        dll_close(dll_handle);
-    }
-
-    if( !pf_init || !pf_deinit || !pf_get_handle || !pf_free_handle || !pf_component_enum )
-    {
-        dll_close(dll_handle);
+    if (InitOmxCore(p_this) != VLC_SUCCESS) {
         return VLC_EGENERIC;
     }
 
     /* Allocate the memory needed to store the decoder's structure */
     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
     {
-        dll_close(dll_handle);
+        DeinitOmxCore();
         return VLC_ENOMEM;
     }
 
@@ -770,19 +850,15 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
         p_dec->fmt_out.video = p_dec->fmt_in.video;
         p_dec->fmt_out.audio = p_dec->fmt_in.audio;
         p_dec->fmt_out.i_codec = 0;
+
+        /* set default aspect of 1, if parser did not set it */
+        if (p_dec->fmt_out.video.i_sar_num == 0)
+            p_dec->fmt_out.video.i_sar_num = 1;
+        if (p_dec->fmt_out.video.i_sar_den == 0)
+            p_dec->fmt_out.video.i_sar_den = 1;
     }
     p_sys->b_enc = b_encode;
-    p_sys->dll_handle = dll_handle;
-    p_sys->pf_init = pf_init;
-    p_sys->pf_deinit = pf_deinit;
-    p_sys->pf_get_handle = pf_get_handle;
-    p_sys->pf_free_handle = pf_free_handle;
-    p_sys->pf_component_enum = pf_component_enum;
-    p_sys->pf_get_roles_of_component = pf_get_roles_of_component;
-    p_sys->pp_last_event = &p_sys->p_events;
-    vlc_mutex_init (&p_sys->mutex);
-    vlc_cond_init (&p_sys->cond);
-    vlc_mutex_init (&p_sys->lock);
+    InitOmxEventQueue(&p_sys->event_queue);
     vlc_mutex_init (&p_sys->in.fifo.lock);
     vlc_cond_init (&p_sys->in.fifo.wait);
     p_sys->in.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pOutputPortPrivate) / sizeof(void *);
@@ -794,31 +870,23 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
     vlc_cond_init (&p_sys->out.fifo.wait);
     p_sys->out.fifo.offset = offsetof(OMX_BUFFERHEADERTYPE, pInputPortPrivate) / sizeof(void *);
     p_sys->out.fifo.pp_last = &p_sys->out.fifo.p_first;
-    p_sys->out.b_direct = true;
+    p_sys->out.b_direct = false;
     p_sys->out.b_flushed = true;
     p_sys->out.p_fmt = &p_dec->fmt_out;
     p_sys->ports = 2;
     p_sys->p_ports = &p_sys->in;
+    p_sys->b_use_pts = 1;
 
     msg_Dbg(p_dec, "fmt in:%4.4s, out: %4.4s", (char *)&p_dec->fmt_in.i_codec,
             (char *)&p_dec->fmt_out.i_codec);
 
-    /* Initialise the OMX core */
-    omx_error = p_sys->pf_init();
-    if(omx_error != OMX_ErrorNone)
-    {
-        msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
-                  ErrorToString(omx_error) );
-        CloseGeneric(p_this);
-        return VLC_EGENERIC;
-    }
-    p_sys->b_init = true;
-
     /* Enumerate components and build a list of the one we want to try */
-    if( !CreateComponentsList(p_dec,
+    p_sys->components =
+        CreateComponentsList(p_this,
              GetOmxRole(p_sys->b_enc ? p_dec->fmt_out.i_codec :
                         p_dec->fmt_in.i_codec, p_dec->fmt_in.i_cat,
-                        p_sys->b_enc)) )
+                        p_sys->b_enc), p_sys->ppsz_components);
+    if( !p_sys->components )
     {
         msg_Warn( p_this, "couldn't find an omx component for codec %4.4s",
                   (char *)&p_dec->fmt_in.i_codec );
@@ -827,8 +895,43 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
     }
 
     /* Try to load and initialise a component */
+    omx_error = OMX_ErrorUndefined;
     for(i = 0; i < p_sys->components; i++)
     {
+#ifdef __ANDROID__
+        /* ignore OpenCore software codecs */
+        if (!strncmp(p_sys->ppsz_components[i], "OMX.PV.", 7))
+            continue;
+        /* The same sw codecs, renamed in ICS (perhaps also in honeycomb) */
+        if (!strncmp(p_sys->ppsz_components[i], "OMX.google.", 11))
+            continue;
+        /* This one has been seen on HTC One V - it behaves like it works,
+         * but FillBufferDone returns buffers filled with 0 bytes. The One V
+         * has got a working OMX.qcom.video.decoder.avc instead though. */
+        if (!strncmp(p_sys->ppsz_components[i], "OMX.ARICENT.", 12))
+            continue;
+        /* Codecs with DRM, that don't output plain YUV data but only
+         * support direct rendering where the output can't be intercepted. */
+        if (strstr(p_sys->ppsz_components[i], ".secure"))
+            continue;
+        /* Use VC1 decoder for WMV3 for now */
+        if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.WMV.Decoder"))
+            continue;
+        /* This decoder does work, but has an insane latency (leading to errors
+         * about "main audio output playback way too late" and dropped frames).
+         * At least Samsung Galaxy S III (where this decoder is present) has
+         * got another one, OMX.SEC.mp3.dec, that works well and has a
+         * sensible latency. (Also, even if that one isn't found, in general,
+         * using SW codecs is usually more than fast enough for MP3.) */
+        if (!strcmp(p_sys->ppsz_components[i], "OMX.SEC.MP3.Decoder"))
+            continue;
+        /* This codec should be able to handle both VC1 and WMV3, but
+         * for VC1 it doesn't output any buffers at all (in the way we use
+         * it) and for WMV3 it outputs plain black buffers. Thus ignore
+         * it until we can make it work properly. */
+        if (!strcmp(p_sys->ppsz_components[i], "OMX.Nvidia.vc1.decode"))
+            continue;
+#endif
         omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
                                         &p_sys->omx_handle);
         if(omx_error == OMX_ErrorNone) break;
@@ -853,7 +956,7 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
             p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
 #endif
 
-            if(0 && p_port->b_direct)
+            if(p_port->b_direct)
                 omx_error =
                     OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
                                    p_port->i_port_index, 0,
@@ -872,13 +975,13 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
                     omx_error, (int)p_port->i_port_index, j );
     }
 
-    omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
+    omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
     CHECK_ERROR(omx_error, "Wait for Idle failed (%x)", omx_error );
 
     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
                                  OMX_StateExecuting, 0);
     CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
-    omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
+    omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
     CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
 
     /* Send codec configuration data */
@@ -887,11 +990,50 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
         OMX_FIFO_GET(&p_sys->in.fifo, p_header);
         p_header->nFilledLen = p_dec->fmt_in.i_extra;
 
-        if(p_sys->in.b_direct)
+        /* Convert H.264 NAL format to annex b */
+        if( p_sys->i_nal_size_length && !p_sys->in.b_direct )
+        {
+            p_header->nFilledLen = 0;
+            convert_sps_pps( p_dec, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra,
+                             p_header->pBuffer, p_header->nAllocLen,
+                             (uint32_t*) &p_header->nFilledLen, NULL );
+        }
+        else if(p_sys->in.b_direct)
         {
             p_header->pOutputPortPrivate = p_header->pBuffer;
             p_header->pBuffer = p_dec->fmt_in.p_extra;
         }
+        else if (p_dec->fmt_in.i_codec == VLC_CODEC_WMV3 &&
+                 p_dec->fmt_in.i_extra >= 4 &&
+                 p_header->nAllocLen >= 36)
+        {
+            int profile;
+            // According to OMX IL 1.2.0 spec (4.3.33.2), the codec config
+            // data for VC-1 Main/Simple (aka WMV3) is according to table 265
+            // in the VC-1 spec. Most of the fields are just set with placeholders
+            // (like framerate, hrd_buffer/rate).
+            static const uint8_t wmv3seq[] = {
+                0xff, 0xff, 0xff, 0xc5, // numframes=ffffff, marker byte
+                0x04, 0x00, 0x00, 0x00, // marker byte
+                0x00, 0x00, 0x00, 0x00, // struct C, almost equal to p_extra
+                0x00, 0x00, 0x00, 0x00, // struct A, vert size
+                0x00, 0x00, 0x00, 0x00, // struct A, horiz size
+                0x0c, 0x00, 0x00, 0x00, // marker byte
+                0xff, 0xff, 0x00, 0x80, // struct B, level=4, cbr=0, hrd_buffer=ffff
+                0xff, 0xff, 0x00, 0x00, // struct B, hrd_rate=ffff
+                0xff, 0xff, 0xff, 0xff, // struct B, framerate=ffffffff
+            };
+            p_header->nFilledLen = sizeof(wmv3seq);
+            memcpy(p_header->pBuffer, wmv3seq, p_header->nFilledLen);
+            // Struct C - almost equal to the extradata
+            memcpy(&p_header->pBuffer[8], p_dec->fmt_in.p_extra, 4);
+            // Expand profile from the highest 2 bits to the highest 4 bits
+            profile = p_header->pBuffer[8] >> 6;
+            p_header->pBuffer[8] = (p_header->pBuffer[8] & 0x0f) | (profile << 4);
+            // Fill in the height/width for struct A
+            SetDWLE(&p_header->pBuffer[12], p_dec->fmt_in.video.i_height);
+            SetDWLE(&p_header->pBuffer[16], p_dec->fmt_in.video.i_width);
+        }
         else
         {
             if(p_header->nFilledLen > p_header->nAllocLen)
@@ -904,7 +1046,7 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
         }
 
         p_header->nOffset = 0;
-        p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
+        p_header->nFlags = OMX_BUFFERFLAG_CODECCONFIG | OMX_BUFFERFLAG_ENDOFFRAME;
         msg_Dbg(p_dec, "sending codec config data %p, %p, %i", p_header,
                 p_header->pBuffer, (int)p_header->nFilledLen);
         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
@@ -920,6 +1062,10 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
     if(p_sys->b_error) goto error;
 
     p_dec->b_need_packetized = true;
+
+    if (!p_sys->b_use_pts)
+        msg_Dbg( p_dec, "using dts timestamp mode for %s", p_sys->psz_component);
+
     return VLC_SUCCESS;
 
  error:
@@ -943,9 +1089,9 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
     definition.nPortIndex = p_port->i_port_index;
     omx_error = OMX_GetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
                                  &definition);
-    if(omx_error != OMX_ErrorNone ||
-       !definition.format.video.nFrameWidth ||
-       !definition.format.video.nFrameHeight )
+    if(omx_error != OMX_ErrorNone || (p_dec->fmt_in.i_cat == VIDEO_ES &&
+       (!definition.format.video.nFrameWidth ||
+       !definition.format.video.nFrameHeight)) )
         return OMX_ErrorUndefined;
 
     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
@@ -956,6 +1102,13 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
     for(i = 0; i < p_port->i_buffers; i++)
     {
         OMX_FIFO_GET(&p_port->fifo, p_buffer);
+        if (p_buffer->pAppPrivate != NULL)
+            decoder_DeletePicture( p_dec, p_buffer->pAppPrivate );
+        if (p_buffer->nFlags & SENTINEL_FLAG) {
+            free(p_buffer);
+            i--;
+            continue;
+        }
         omx_error = OMX_FreeBuffer( p_sys->omx_handle,
                                     p_port->i_port_index, p_buffer );
 
@@ -964,25 +1117,50 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
     CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
                 omx_error, (int)p_port->i_port_index, i );
 
-    omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
+    omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
     CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
 
     /* Get the new port definition */
     omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
     if(omx_error != OMX_ErrorNone) goto error;
-    omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
-                                 &definition);
-    CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
-                omx_error, ErrorToString(omx_error));
+
+    if( p_dec->fmt_in.i_cat != AUDIO_ES )
+    {
+        /* Don't explicitly set the new parameters that we got with
+         * OMX_GetParameter above when using audio codecs.
+         * That struct hasn't been changed since, so there should be
+         * no need to set it here, unless some codec expects the
+         * SetParameter call as a trigger event for some part of
+         * the reconfiguration.
+         * This fixes using audio decoders on Samsung Galaxy S II,
+         *
+         * Only skipping this for audio codecs, to minimize the
+         * change for current working configurations for video.
+         */
+        omx_error = OMX_SetParameter(p_dec->p_sys->omx_handle, OMX_IndexParamPortDefinition,
+                                     &definition);
+        CHECK_ERROR(omx_error, "OMX_SetParameter failed (%x : %s)",
+                    omx_error, ErrorToString(omx_error));
+    }
 
     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
                                  p_port->i_port_index, NULL);
     CHECK_ERROR(omx_error, "OMX_CommandPortEnable on %i failed (%x)",
                 (int)p_port->i_port_index, omx_error );
 
+    if (p_port->definition.nBufferCountActual > p_port->i_buffers) {
+        free(p_port->pp_buffers);
+        p_port->pp_buffers = malloc(p_port->definition.nBufferCountActual * sizeof(OMX_BUFFERHEADERTYPE*));
+        if(!p_port->pp_buffers)
+        {
+            omx_error = OMX_ErrorInsufficientResources;
+            CHECK_ERROR(omx_error, "memory allocation failed");
+        }
+    }
+    p_port->i_buffers = p_port->definition.nBufferCountActual;
     for(i = 0; i < p_port->i_buffers; i++)
     {
-        if(0 && p_port->b_direct)
+        if(p_port->b_direct)
             omx_error =
                 OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
                                p_port->i_port_index, 0,
@@ -1000,7 +1178,7 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
     CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
                 omx_error, (int)p_port->i_port_index, i );
 
-    omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
+    omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
     CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
 
     PrintOmx(p_dec, p_sys->omx_handle, p_dec->p_sys->in.i_port_index);
@@ -1022,6 +1200,8 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
 
     OMX_BUFFERHEADERTYPE *p_header;
     block_t *p_block;
+    int i_input_used = 0;
+    struct H264ConvertState convert_state = { 0, 0 };
 
     if( !pp_block || !*pp_block )
         return NULL;
@@ -1055,6 +1235,12 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
         if(!p_header) break; /* No frame available */
 
+        if(p_sys->out.b_update_def)
+        {
+            omx_error = GetPortDefinition(p_dec, &p_sys->out, p_sys->out.p_fmt);
+            p_sys->out.b_update_def = 0;
+        }
+
         if(p_header->nFilledLen)
         {
             p_pic = p_header->pAppPrivate;
@@ -1063,18 +1249,23 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
                 /* We're not in direct rendering mode.
                  * Get a new picture and copy the content */
                 p_pic = decoder_NewPicture( p_dec );
-                if( !p_pic ) break; /* No picture available */
 
-                CopyOmxPicture(p_dec, p_pic, p_header);
+                if (p_pic)
+                    CopyOmxPicture(p_sys->out.definition.format.video.eColorFormat,
+                                   p_pic, p_sys->out.definition.format.video.nSliceHeight,
+                                   p_sys->out.i_frame_stride,
+                                   p_header->pBuffer + p_header->nOffset,
+                                   p_sys->out.i_frame_stride_chroma_div);
             }
 
-            p_pic->date = p_header->nTimeStamp;
+            if (p_pic)
+                p_pic->date = FromOmxTicks(p_header->nTimeStamp);
             p_header->nFilledLen = 0;
             p_header->pAppPrivate = 0;
         }
 
         /* Get a new picture */
-        if(p_sys->in.b_direct && !p_header->pAppPrivate)
+        if(p_sys->out.b_direct && !p_header->pAppPrivate)
         {
             p_next_pic = decoder_NewPicture( p_dec );
             if(!p_next_pic) break;
@@ -1095,14 +1286,25 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
         OMX_FillThisBuffer(p_sys->omx_handle, p_header);
     }
 
+more_input:
     /* Send the input buffer to the component */
-    OMX_FIFO_GET(&p_sys->in.fifo, p_header);
+    OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
+
+    if (p_header && p_header->nFlags & SENTINEL_FLAG) {
+        free(p_header);
+        goto reconfig;
+    }
+
     if(p_header)
     {
-        p_header->nFilledLen = p_block->i_buffer;
+        bool decode_more = false;
+        p_header->nFilledLen = p_block->i_buffer - i_input_used;
         p_header->nOffset = 0;
         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
-        p_header->nTimeStamp = p_block->i_dts;
+        if (p_sys->b_use_pts && p_block->i_pts)
+            p_header->nTimeStamp = ToOmxTicks(p_block->i_pts);
+        else
+            p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
 
         /* In direct mode we pass the input pointer as is.
          * Otherwise we memcopy the data */
@@ -1111,35 +1313,59 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
             p_header->pOutputPortPrivate = p_header->pBuffer;
             p_header->pBuffer = p_block->p_buffer;
             p_header->pAppPrivate = p_block;
+            i_input_used = p_header->nFilledLen;
         }
         else
         {
             if(p_header->nFilledLen > p_header->nAllocLen)
             {
-                msg_Dbg(p_dec, "buffer too small (%i,%i)",
-                        (int)p_header->nFilledLen, (int)p_header->nAllocLen);
                 p_header->nFilledLen = p_header->nAllocLen;
             }
-            memcpy(p_header->pBuffer, p_block->p_buffer, p_header->nFilledLen );
-            block_Release(p_block);
+            memcpy(p_header->pBuffer, p_block->p_buffer + i_input_used, p_header->nFilledLen);
+            i_input_used += p_header->nFilledLen;
+            if (i_input_used == p_block->i_buffer)
+            {
+                block_Release(p_block);
+            }
+            else
+            {
+                decode_more = true;
+                p_header->nFlags &= ~OMX_BUFFERFLAG_ENDOFFRAME;
+            }
         }
 
+        /* Convert H.264 NAL format to annex b. Doesn't do anything if
+         * i_nal_size_length == 0, which is the case for codecs other
+         * than H.264 */
+        convert_h264_to_annexb( p_header->pBuffer, p_header->nFilledLen,
+                                p_sys->i_nal_size_length, &convert_state );
 #ifdef OMXIL_EXTRA_DEBUG
-        msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
-                 (int)p_header->nFilledLen );
+        msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i, %"PRId64, p_header, p_header->pBuffer,
+                 (int)p_header->nFilledLen, FromOmxTicks(p_header->nTimeStamp) );
 #endif
         OMX_EmptyThisBuffer(p_sys->omx_handle, p_header);
         p_sys->in.b_flushed = false;
-        *pp_block = NULL; /* Avoid being fed the same packet again */
+        if (decode_more)
+            goto more_input;
+        else
+            *pp_block = NULL; /* Avoid being fed the same packet again */
     }
 
+reconfig:
     /* Handle the PortSettingsChanged events */
     for(i = 0; i < p_sys->ports; i++)
     {
         OmxPort *p_port = &p_sys->p_ports[i];
-        if(!p_port->b_reconfigure) continue;
-        p_port->b_reconfigure = 0;
-        omx_error = PortReconfigure(p_dec, p_port);
+        if(p_port->b_reconfigure)
+        {
+            omx_error = PortReconfigure(p_dec, p_port);
+            p_port->b_reconfigure = 0;
+        }
+        if(p_port->b_update_def)
+        {
+            omx_error = GetPortDefinition(p_dec, p_port, p_port->p_fmt);
+            p_port->b_update_def = 0;
+        }
     }
 
     return p_pic;
@@ -1148,10 +1374,10 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
 /*****************************************************************************
  * DecodeAudio: Called to decode one frame
  *****************************************************************************/
-aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
+block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
-    aout_buffer_t *p_buffer = 0;
+    block_t *p_buffer = NULL;
     OMX_BUFFERHEADERTYPE *p_header;
     OMX_ERRORTYPE omx_error;
     block_t *p_block;
@@ -1197,12 +1423,13 @@ aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
     /* Take care of decoded frames first */
     while(!p_buffer)
     {
-        unsigned int i_samples;
+        unsigned int i_samples = 0;
 
         OMX_FIFO_PEEK(&p_sys->out.fifo, p_header);
         if(!p_header) break; /* No frame available */
 
-        i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
+        if (p_sys->out.p_fmt->audio.i_channels)
+            i_samples = p_header->nFilledLen / p_sys->out.p_fmt->audio.i_channels / 2;
         if(i_samples)
         {
             p_buffer = decoder_NewAudioBuffer( p_dec, i_samples );
@@ -1211,9 +1438,10 @@ aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
             memcpy( p_buffer->p_buffer, p_header->pBuffer, p_buffer->i_buffer );
             p_header->nFilledLen = 0;
 
-            if( p_header->nTimeStamp != 0 &&
-                p_header->nTimeStamp != date_Get( &p_sys->end_date ) )
-                date_Set( &p_sys->end_date, p_header->nTimeStamp );
+            int64_t timestamp = FromOmxTicks(p_header->nTimeStamp);
+            if( timestamp != 0 &&
+                timestamp != date_Get( &p_sys->end_date ) )
+                date_Set( &p_sys->end_date, timestamp );
 
             p_buffer->i_pts = date_Get( &p_sys->end_date );
             p_buffer->i_length = date_Increment( &p_sys->end_date, i_samples ) -
@@ -1229,13 +1457,19 @@ aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
 
 
     /* Send the input buffer to the component */
-    OMX_FIFO_GET(&p_sys->in.fifo, p_header);
+    OMX_FIFO_GET_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
+
+    if (p_header && p_header->nFlags & SENTINEL_FLAG) {
+        free(p_header);
+        goto reconfig;
+    }
+
     if(p_header)
     {
         p_header->nFilledLen = p_block->i_buffer;
         p_header->nOffset = 0;
         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
-        p_header->nTimeStamp = p_block->i_dts;
+        p_header->nTimeStamp = ToOmxTicks(p_block->i_dts);
 
         /* In direct mode we pass the input pointer as is.
          * Otherwise we memcopy the data */
@@ -1266,6 +1500,7 @@ aout_buffer_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
         *pp_block = NULL; /* Avoid being fed the same packet again */
     }
 
+reconfig:
     /* Handle the PortSettingsChanged events */
     for(i = 0; i < p_sys->ports; i++)
     {
@@ -1319,7 +1554,7 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
         p_header->nFilledLen = p_sys->in.i_frame_size;
         p_header->nOffset = 0;
         p_header->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
-        p_header->nTimeStamp = p_pic->date;
+        p_header->nTimeStamp = ToOmxTicks(p_pic->date);
 #ifdef OMXIL_EXTRA_DEBUG
         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
                  (int)p_header->nFilledLen );
@@ -1355,12 +1590,12 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
             {
                 /* We're not in direct rendering mode.
                  * Get a new block and copy the content */
-                p_block = block_New( p_dec, p_header->nFilledLen );
+                p_block = block_Alloc( p_header->nFilledLen );
                 memcpy(p_block->p_buffer, p_header->pBuffer, p_header->nFilledLen );
             }
 
             p_block->i_buffer = p_header->nFilledLen;
-            p_block->i_pts = p_block->i_dts = p_header->nTimeStamp;
+            p_block->i_pts = p_block->i_dts = FromOmxTicks(p_header->nTimeStamp);
             p_header->nFilledLen = 0;
             p_header->pAppPrivate = 0;
         }
@@ -1384,11 +1619,10 @@ static void CloseGeneric( vlc_object_t *p_this )
     decoder_sys_t *p_sys = p_dec->p_sys;
 
     if(p_sys->omx_handle) DeinitialiseComponent(p_dec, p_sys->omx_handle);
-    if(p_sys->b_init) p_sys->pf_deinit();
-    dll_close( p_sys->dll_handle );
 
-    vlc_mutex_destroy (&p_sys->mutex);
-    vlc_cond_destroy (&p_sys->cond);
+    DeinitOmxCore();
+
+    DeinitOmxEventQueue(&p_sys->event_queue);
     vlc_mutex_destroy (&p_sys->in.fifo.lock);
     vlc_cond_destroy (&p_sys->in.fifo.wait);
     vlc_mutex_destroy (&p_sys->out.fifo.lock);
@@ -1409,45 +1643,47 @@ static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
     unsigned int i;
     (void)omx_handle;
 
+    PrintOmxEvent((vlc_object_t *) p_dec, event, data_1, data_2, event_data);
     switch (event)
     {
-    case OMX_EventCmdComplete:
-        switch ((OMX_STATETYPE)data_1)
-        {
-        case OMX_CommandStateSet:
-            msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %s)", EventToString(event),
-                     CommandToString(data_1), StateToString(data_2) );
-            break;
-
-        default:
-            msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u)", EventToString(event),
-                     CommandToString(data_1), (unsigned int)data_2 );
-            break;
-        }
-        break;
-
     case OMX_EventError:
-        msg_Dbg( p_dec, "OmxEventHandler (%s, %s, %u, %s)", EventToString(event),
-                 ErrorToString((OMX_ERRORTYPE)data_1), (unsigned int)data_2,
-                 (const char *)event_data);
         //p_sys->b_error = true;
         break;
 
     case OMX_EventPortSettingsChanged:
-        msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
-                 (unsigned int)data_1, (unsigned int)data_2 );
-        for(i = 0; i < p_sys->ports; i++)
-            if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
-                p_sys->p_ports[i].b_reconfigure = true;
+        if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition ||
+            data_2 == OMX_IndexParamAudioPcm )
+        {
+            OMX_BUFFERHEADERTYPE *sentinel;
+            for(i = 0; i < p_sys->ports; i++)
+                if(p_sys->p_ports[i].definition.eDir == OMX_DirOutput)
+                    p_sys->p_ports[i].b_reconfigure = true;
+            sentinel = calloc(1, sizeof(*sentinel));
+            if (sentinel) {
+                sentinel->nFlags = SENTINEL_FLAG;
+                OMX_FIFO_PUT(&p_sys->in.fifo, sentinel);
+            }
+        }
+        else if( data_2 == OMX_IndexConfigCommonOutputCrop )
+        {
+            for(i = 0; i < p_sys->ports; i++)
+                if(p_sys->p_ports[i].definition.nPortIndex == data_1)
+                    p_sys->p_ports[i].b_update_def = true;
+        }
+        else
+        {
+            msg_Dbg( p_dec, "Unhandled setting change %x", (unsigned int)data_2 );
+        }
+        break;
+    case OMX_EventParamOrConfigChanged:
+        UpdatePixelAspect(p_dec);
         break;
 
     default:
-        msg_Dbg( p_dec, "OmxEventHandler (%s, %u, %u)", EventToString(event),
-                 (unsigned int)data_1, (unsigned int)data_2 );
         break;
     }
 
-    PostOmxEvent(p_dec, event, data_1, data_2, event_data);
+    PostOmxEvent(&p_sys->event_queue, event, data_1, data_2, event_data);
     return OMX_ErrorNone;
 }
 
@@ -1482,8 +1718,8 @@ static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE omx_handle,
     (void)omx_handle;
 
 #ifdef OMXIL_EXTRA_DEBUG
-    msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i", omx_header, omx_header->pBuffer,
-             (int)omx_header->nFilledLen );
+    msg_Dbg( p_dec, "OmxFillBufferDone %p, %p, %i, %"PRId64, omx_header, omx_header->pBuffer,
+             (int)omx_header->nFilledLen, FromOmxTicks(omx_header->nTimeStamp) );
 #endif
 
     if(omx_header->pInputPortPrivate)