]> git.sesse.net Git - vlc/blobdiff - modules/codec/omxil/omxil.c
omxil: Use a fake library name when loading IOMX
[vlc] / modules / codec / omxil / omxil.c
index 05f490efc34e8553f8f66f1ece61001eabc4d9f1..dde81265259ca63826a97b67a1a94b70cb4f6722 100644 (file)
 # include "config.h"
 #endif
 
-#if defined(HAVE_DL_DLOPEN)
-# include <dlfcn.h>
+#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, name)
+#else
+# define dll_open(name) dlopen( name, RTLD_NOW )
+# define dll_close(handle) dlclose(handle)
 #endif
 
+#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"
 
  *****************************************************************************/
 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
 };
 
 /*****************************************************************************
- * defines
+ * Global OMX Core instance, shared between module instances
  *****************************************************************************/
-#if defined(HAVE_DL_DLOPEN)
-# define dll_open(name) dlopen( name, RTLD_NOW )
-# define dll_close(handle) dlclose(handle)
-#else
-# define dll_open(name) (NULL)
-# define dll_close(handle) (void)0
-#endif
+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 **);
 
 /*****************************************************************************
  * Local prototypes
@@ -89,9 +107,9 @@ static OMX_ERRORTYPE OmxFillBufferDone( OMX_HANDLETYPE, OMX_PTR,
 vlc_module_begin ()
     set_description( N_("Audio/Video decoder (using OpenMAX IL)") )
     set_category( CAT_INPUT )
-    set_subcategory( SUBCAT_INPUT_SCODEC )
+    set_subcategory( SUBCAT_INPUT_VCODEC )
     set_section( N_("Decoding") , NULL )
-    set_capability( "decoder", 0 )
+    set_capability( "decoder", 80 )
     set_callbacks( OpenDecoder, CloseGeneric )
 
     add_submodule ()
@@ -120,12 +138,12 @@ static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
     {
         bool b_found = false;
 
-        omx_error = p_sys->pf_component_enum(psz_name, OMX_MAX_STRINGNAME_SIZE, i);
+        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 = p_sys->pf_get_roles_of_component(psz_name, &roles, 0);
+        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));
@@ -135,7 +153,7 @@ static int CreateComponentsList(decoder_t *p_dec, const char *psz_role)
             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);
+        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++)
@@ -177,9 +195,51 @@ static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
+    int i_profile = 0xFFFF, i_level = 0xFFFF;
+
+    /* Try to find out the profile of the video */
+    while(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
+          p_fmt->i_codec == VLC_CODEC_H264)
+    {
+        uint8_t *p = (uint8_t*)p_dec->fmt_in.p_extra;
+        if(!p || !p_dec->fmt_in.p_extra) break;
+
+        /* Check the profile / level */
+        if(p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('a','v','c','1') &&
+           p[0] == 1)
+        {
+            if(p_dec->fmt_in.i_extra < 12) break;
+            p_sys->i_nal_size_length = 1 + (p[4]&0x03);
+            if( !(p[5]&0x1f) ) break;
+            p += 8;
+        }
+        else
+        {
+            if(p_dec->fmt_in.i_extra < 8) break;
+            if(!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
+            else if(!p[0] && !p[1] && p[2] == 1) p += 3;
+            else break;
+        }
+
+        if( ((*p++)&0x1f) != 7) break;
+
+        /* Get profile/level out of first SPS */
+        i_profile = p[0];
+        i_level = p[2];
+        break;
+    }
 
     if(!strcmp(p_sys->psz_component, "OMX.TI.Video.Decoder"))
     {
+        if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirInput &&
+           p_fmt->i_codec == VLC_CODEC_H264 &&
+           (i_profile != 66 || i_level > 30))
+        {
+            msg_Dbg(p_dec, "h264 profile/level not supported (0x%x, 0x%x)",
+                    i_profile, i_level);
+            return OMX_ErrorNotImplemented;
+        }
+
         if(p_fmt->i_cat == VIDEO_ES && def->eDir == OMX_DirOutput &&
            p_fmt->i_codec == VLC_CODEC_I420)
         {
@@ -204,6 +264,16 @@ static OMX_ERRORTYPE ImplementationSpecificWorkarounds(decoder_t *p_dec,
             def->format.video.xFramerate >>= 16;
         }
     }
+    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;
+        }
+    }
 
     return OMX_ErrorNone;
 }
@@ -292,7 +362,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;
@@ -305,7 +375,9 @@ static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
     default: return OMX_ErrorNotImplemented;
     }
 
-    ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
+    omx_error = ImplementationSpecificWorkarounds(p_dec, p_port, p_fmt);
+    CHECK_ERROR(omx_error, "ImplementationSpecificWorkarounds failed (%x : %s)",
+                omx_error, ErrorToString(omx_error));
 
     omx_error = OMX_SetParameter(p_port->omx_handle,
                                  OMX_IndexParamPortDefinition, def);
@@ -346,6 +418,7 @@ static OMX_ERRORTYPE SetPortDefinition(decoder_t *p_dec, OmxPort *p_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;
 
@@ -364,6 +437,14 @@ 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;
 
+        /* 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(!GetVlcVideoFormat( def->format.video.eCompressionFormat,
                                &p_fmt->i_codec, 0 ) )
         {
@@ -391,7 +472,7 @@ static OMX_ERRORTYPE GetPortDefinition(decoder_t *p_dec, OmxPort *p_port,
         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;
@@ -459,25 +540,25 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
     if(!omx_handle) return OMX_ErrorNone;
 
     omx_error = OMX_GetState(omx_handle, &state);
-    CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
+    CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
 
     if(state == OMX_StateExecuting)
     {
         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
                                      OMX_StateIdle, 0 );
-        CHECK_ERROR(omx_error, "ONX_CommandStateSet Idle failed (%x)", omx_error );
+        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 );
     }
 
     omx_error = OMX_GetState(omx_handle, &state);
-    CHECK_ERROR(omx_error, "ONX_GetState failed (%x)", omx_error );
+    CHECK_ERROR(omx_error, "OMX_GetState failed (%x)", omx_error );
 
     if(state == OMX_StateIdle)
     {
         omx_error = OMX_SendCommand( omx_handle, OMX_CommandStateSet,
                                      OMX_StateLoaded, 0 );
-        CHECK_ERROR(omx_error, "ONX_CommandStateSet Loaded failed (%x)", omx_error );
+        CHECK_ERROR(omx_error, "OMX_CommandStateSet Loaded failed (%x)", omx_error );
 
         for(i = 0; i < p_sys->ports; i++)
         {
@@ -492,7 +573,7 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
 
                 if(omx_error != OMX_ErrorNone) break;
             }
-            CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
+            CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
                         omx_error, (int)p_port->i_port_index, j );
         }
 
@@ -507,7 +588,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;
 }
 
@@ -529,8 +610,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,
@@ -539,8 +619,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 */
@@ -588,6 +671,18 @@ 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))
+    {
+        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");
+        }
+    }
+
     /* Set port definitions */
     for(i = 0; i < p_sys->ports; i++)
     {
@@ -616,7 +711,7 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
         {
             omx_error = OMX_SendCommand( omx_handle, OMX_CommandPortEnable,
                                          p_port->i_port_index, NULL);
-            CHECK_ERROR(omx_error, "ONX_CommandPortEnable on %i failed (%x)",
+            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);
             CHECK_ERROR(omx_error, "Wait for PortEnable on %i failed (%x)",
@@ -644,6 +739,11 @@ 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;
 
@@ -677,22 +777,27 @@ 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;
 
+    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 ) return VLC_EGENERIC;
+    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" );
@@ -706,18 +811,17 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
         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);
+        vlc_mutex_unlock( &omx_core_mutex );
         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 )
     {
-        dll_close(dll_handle);
+        if( omx_refcount == 0 )
+            dll_close(dll_handle);
+        vlc_mutex_unlock( &omx_core_mutex );
         return VLC_ENOMEM;
     }
 
@@ -730,13 +834,6 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
         p_dec->fmt_out.i_codec = 0;
     }
     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);
@@ -762,15 +859,18 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
             (char *)&p_dec->fmt_out.i_codec);
 
     /* Initialise the OMX core */
-    omx_error = p_sys->pf_init();
+    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,
@@ -785,8 +885,14 @@ 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;
+#endif
         omx_error = InitialiseComponent(p_dec, p_sys->ppsz_components[i],
                                         &p_sys->omx_handle);
         if(omx_error == OMX_ErrorNone) break;
@@ -795,7 +901,7 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
 
     /* Move component to Idle then Executing state */
     OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet, OMX_StateIdle, 0 );
-    CHECK_ERROR(omx_error, "ONX_CommandStateSet Idle failed (%x)", omx_error );
+    CHECK_ERROR(omx_error, "OMX_CommandStateSet Idle failed (%x)", omx_error );
 
     /* Allocate omx buffers */
     for(i = 0; i < p_sys->ports; i++)
@@ -826,7 +932,7 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
             OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
         }
         p_port->i_buffers = j;
-        CHECK_ERROR(omx_error, "ONX_UseBuffer failed (%x, %i, %i)",
+        CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
                     omx_error, (int)p_port->i_port_index, j );
     }
 
@@ -835,7 +941,7 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
 
     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandStateSet,
                                  OMX_StateExecuting, 0);
-    CHECK_ERROR(omx_error, "ONX_CommandStateSet Executing failed (%x)", omx_error );
+    CHECK_ERROR(omx_error, "OMX_CommandStateSet Executing failed (%x)", omx_error );
     omx_error = WaitForSpecificOmxEvent(p_dec, OMX_EventCmdComplete, 0, 0, 0);
     CHECK_ERROR(omx_error, "Wait for Executing failed (%x)", omx_error );
 
@@ -845,7 +951,15 @@ 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;
@@ -908,7 +1022,7 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
 
     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortDisable,
                                  p_port->i_port_index, NULL);
-    CHECK_ERROR(omx_error, "ONX_CommandPortDisable on %i failed (%x)",
+    CHECK_ERROR(omx_error, "OMX_CommandPortDisable on %i failed (%x)",
                 (int)p_port->i_port_index, omx_error );
 
     for(i = 0; i < p_port->i_buffers; i++)
@@ -919,7 +1033,7 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
 
         if(omx_error != OMX_ErrorNone) break;
     }
-    CHECK_ERROR(omx_error, "ONX_FreeBuffer failed (%x, %i, %i)",
+    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);
@@ -935,9 +1049,19 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
 
     omx_error = OMX_SendCommand( p_sys->omx_handle, OMX_CommandPortEnable,
                                  p_port->i_port_index, NULL);
-    CHECK_ERROR(omx_error, "ONX_CommandPortEnable on %i failed (%x)",
+    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)
@@ -955,7 +1079,7 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
         OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
     }
     p_port->i_buffers = i;
-    CHECK_ERROR(omx_error, "ONX_UseBuffer failed (%x, %i, %i)",
+    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);
@@ -1023,7 +1147,7 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
                 p_pic = decoder_NewPicture( p_dec );
                 if( !p_pic ) break; /* No picture available */
 
-                CopyOmxPicture(p_dec, p_pic, p_header);
+                CopyOmxPicture(p_dec, p_pic, p_header, p_sys->out.definition.format.video.nSliceHeight);
             }
 
             p_pic->date = p_header->nTimeStamp;
@@ -1055,6 +1179,10 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
 
     /* Send the input buffer to the component */
     OMX_FIFO_GET(&p_sys->in.fifo, p_header);
+
+    if (p_header && p_header->nFlags & OMX_BUFFERFLAG_EOS)
+        goto reconfig;
+
     if(p_header)
     {
         p_header->nFilledLen = p_block->i_buffer;
@@ -1082,6 +1210,26 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
             block_Release(p_block);
         }
 
+        /* Convert H.264 NAL format to annex b */
+        if( p_sys->i_nal_size_length >= 3 && p_sys->i_nal_size_length <= 4 )
+        {
+            /* This only works for NAL sizes 3-4 */
+            int i_len = p_header->nFilledLen, i;
+            uint8_t* ptr = p_header->pBuffer;
+            while( i_len >= p_sys->i_nal_size_length )
+            {
+                uint32_t nal_len = 0;
+                for( i = 0; i < p_sys->i_nal_size_length; i++ ) {
+                    nal_len = (nal_len << 8) | ptr[i];
+                    ptr[i] = 0;
+                }
+                ptr[p_sys->i_nal_size_length - 1] = 1;
+                if( nal_len > INT_MAX || nal_len > (unsigned int) i_len )
+                    break;
+                ptr   += nal_len + 4;
+                i_len -= nal_len + 4;
+            }
+        }
 #ifdef OMXIL_EXTRA_DEBUG
         msg_Dbg( p_dec, "EmptyThisBuffer %p, %p, %i", p_header, p_header->pBuffer,
                  (int)p_header->nFilledLen );
@@ -1091,6 +1239,7 @@ static picture_t *DecodeVideo( 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++)
     {
@@ -1249,11 +1398,13 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pic )
     OMX_BUFFERHEADERTYPE *p_header;
     block_t *p_block = 0;
 
+    if( !p_pic ) return NULL;
+
     /* Check for errors from codec */
     if(p_sys->b_error)
     {
         msg_Dbg(p_dec, "error during encoding");
-        return 0;
+        return NULL;
     }
 
     /* Send the input buffer to the component */
@@ -1340,8 +1491,14 @@ 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_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);
@@ -1395,6 +1552,9 @@ static OMX_ERRORTYPE OmxEventHandler( OMX_HANDLETYPE omx_handle,
         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;
+        memset(&p_sys->sentinel_buffer, 0, sizeof(p_sys->sentinel_buffer));
+        p_sys->sentinel_buffer.nFlags = OMX_BUFFERFLAG_EOS;
+        OMX_FIFO_PUT(&p_sys->in.fifo, &p_sys->sentinel_buffer);
         break;
 
     default: