]> 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 da7f7d79cb5de370cb36f599e853bf85ed184d1d..0fafedf068710aa66bf92de30ff546bb0ce2b03a 100644 (file)
 # include "config.h"
 #endif
 
-#include <dlfcn.h>
-#if defined(USE_IOMX)
-/* On dll_open, just check that the OMX_Init symbol already is loaded */
-# define dll_open(name) dlsym(RTLD_DEFAULT, "OMX_Init")
-# define dll_close(handle) do { } while (0)
-# define dlsym(handle, name) dlsym(RTLD_DEFAULT, "I" name)
-#else
-# define dll_open(name) dlopen( name, RTLD_NOW )
-# define dll_close(handle) dlclose(handle)
-#endif
-
 #include <limits.h>
 
 #include <vlc_common.h>
@@ -49,6 +38,8 @@
 #include "../h264_nal.h"
 
 #include "omxil.h"
+#include "omxil_core.h"
+#include "OMX_Broadcom.h"
 
 #ifndef NDEBUG
 # define OMXIL_EXTRA_DEBUG
 
 #define SENTINEL_FLAG 0x10000
 
-/*****************************************************************************
- * List of OpenMAX IL core we will try in order
- *****************************************************************************/
-static const char *ppsz_dll_list[] =
-{
-#if defined(USE_IOMX)
-    "libiomx.so", /* Not used when using IOMX, the lib should already be loaded */
-#else
-    "libOMX_Core.so", /* TI OMAP IL core */
-    "libOmxCore.so", /* Qualcomm IL core */
-    "libomxil-bellagio.so",  /* Bellagio IL core */
-#endif
-    0
-};
+/* Defined in the broadcom version of OMX_Index.h */
+#define OMX_IndexConfigRequestCallback 0x7f000063
+#define OMX_IndexParamBrcmPixelAspectRatio 0x7f00004d
 
-/*****************************************************************************
- * Global OMX Core instance, shared between module instances
- *****************************************************************************/
-static vlc_mutex_t omx_core_mutex = VLC_STATIC_MUTEX;
-static unsigned int omx_refcount = 0;
-static void *dll_handle;
-static OMX_ERRORTYPE (*pf_init) (void);
-static OMX_ERRORTYPE (*pf_deinit) (void);
-static OMX_ERRORTYPE (*pf_get_handle) (OMX_HANDLETYPE *, OMX_STRING,
-                                       OMX_PTR, OMX_CALLBACKTYPE *);
-static OMX_ERRORTYPE (*pf_free_handle) (OMX_HANDLETYPE);
-static OMX_ERRORTYPE (*pf_component_enum)(OMX_STRING, OMX_U32, OMX_U32);
-static OMX_ERRORTYPE (*pf_get_roles_of_component)(OMX_STRING, OMX_U32 *, OMX_U8 **);
+/* Defined in the broadcom version of OMX_Core.h */
+#define OMX_EventParamOrConfigChanged 0x7F000001
 
 /*****************************************************************************
  * Local prototypes
@@ -129,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 = 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 = 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 = 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(!strcmp((char *)ppsz_roles[j], psz_role)) 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
@@ -286,6 +188,15 @@ static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
         }
     }
 #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;
 }
@@ -442,6 +353,34 @@ static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
     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
  *****************************************************************************/
@@ -527,6 +466,7 @@ 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:
@@ -607,7 +547,7 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
         CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
         while (1) {
             OMX_U32 cmd, state;
-            omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, &cmd, &state, 0);
+            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
@@ -658,7 +598,7 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
             }
         }
 
-        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 );
     }
 
@@ -780,6 +720,23 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
         }
     }
 
+    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++)
     {
@@ -810,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 );
         }
@@ -875,45 +832,14 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
     OMX_BUFFERHEADERTYPE *p_header;
     unsigned int i, j;
 
-    vlc_mutex_lock( &omx_core_mutex );
-    if( omx_refcount > 0 )
-        goto loaded;
-
-    /* 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 )
-    {
-        vlc_mutex_unlock( &omx_core_mutex );
-        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);
-        vlc_mutex_unlock( &omx_core_mutex );
+    if (InitOmxCore(p_this) != VLC_SUCCESS) {
         return VLC_EGENERIC;
     }
 
-loaded:
     /* Allocate the memory needed to store the decoder's structure */
     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
     {
-        if( omx_refcount == 0 )
-            dll_close(dll_handle);
-        vlc_mutex_unlock( &omx_core_mutex );
+        DeinitOmxCore();
         return VLC_ENOMEM;
     }
 
@@ -924,12 +850,15 @@ loaded:
         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->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 *);
@@ -946,30 +875,18 @@ loaded:
     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 = 0;
+    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 = omx_refcount > 0 ? OMX_ErrorNone : pf_init();
-    omx_refcount++;
-    if(omx_error != OMX_ErrorNone)
-    {
-        msg_Warn( p_this, "OMX_Init failed (%x: %s)", omx_error,
-                  ErrorToString(omx_error) );
-        vlc_mutex_unlock( &omx_core_mutex );
-        CloseGeneric(p_this);
-        return VLC_EGENERIC;
-    }
-    p_sys->b_init = true;
-    vlc_mutex_unlock( &omx_core_mutex );
-
     /* 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 );
@@ -1058,13 +975,13 @@ loaded:
                     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 */
@@ -1145,14 +1062,9 @@ loaded:
     if(p_sys->b_error) goto error;
 
     p_dec->b_need_packetized = true;
-    if (!strcmp(p_sys->psz_component, "OMX.TI.DUCATI1.VIDEO.DECODER"))
-        p_sys->b_use_pts = 1;
-
-    if (!strcmp(p_sys->psz_component, "OMX.STM.Video.Decoder"))
-        p_sys->b_use_pts = 1;
 
-    if (p_sys->b_use_pts)
-        msg_Dbg( p_dec, "using pts timestamp mode for %s", p_sys->psz_component);
+    if (!p_sys->b_use_pts)
+        msg_Dbg( p_dec, "using dts timestamp mode for %s", p_sys->psz_component);
 
     return VLC_SUCCESS;
 
@@ -1190,6 +1102,8 @@ 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--;
@@ -1203,7 +1117,7 @@ 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 */
@@ -1264,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);
@@ -1286,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;
@@ -1343,7 +1259,7 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
             }
 
             if (p_pic)
-                p_pic->date = p_header->nTimeStamp;
+                p_pic->date = FromOmxTicks(p_header->nTimeStamp);
             p_header->nFilledLen = 0;
             p_header->pAppPrivate = 0;
         }
@@ -1370,6 +1286,7 @@ 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_TIMEOUT(&p_sys->in.fifo, p_header, 200000);
 
@@ -1380,13 +1297,14 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
 
     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;
         if (p_sys->b_use_pts && p_block->i_pts)
-            p_header->nTimeStamp = p_block->i_pts;
+            p_header->nTimeStamp = ToOmxTicks(p_block->i_pts);
         else
-            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 */
@@ -1395,31 +1313,42 @@ 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 );
+                                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:
@@ -1509,9 +1438,10 @@ block_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 ) -
@@ -1539,7 +1469,7 @@ block_t *DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
         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 */
@@ -1624,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 );
@@ -1665,7 +1595,7 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
             }
 
             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;
         }
@@ -1689,17 +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);
-    vlc_mutex_lock( &omx_core_mutex );
-    omx_refcount--;
-    if( omx_refcount == 0 )
-    {
-        if( p_sys->b_init ) pf_deinit();
-        dll_close( dll_handle );
-    }
-    vlc_mutex_unlock( &omx_core_mutex );
 
-    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);
@@ -1720,34 +1643,16 @@ 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 );
-        if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition )
+        if( data_2 == 0 || data_2 == OMX_IndexParamPortDefinition ||
+            data_2 == OMX_IndexParamAudioPcm )
         {
             OMX_BUFFERHEADERTYPE *sentinel;
             for(i = 0; i < p_sys->ports; i++)
@@ -1770,14 +1675,15 @@ static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
             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;
 }
 
@@ -1812,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)