[vlc-devel] [RFC PATCH 07/11] omxil: factorize FreeBuffers and AllocateBuffers

Thomas Guillem guillem at archos.com
Tue Jun 24 16:15:09 CEST 2014


---
 modules/codec/omxil/omxil.c |  236 ++++++++++++++++++++++---------------------
 1 file changed, 123 insertions(+), 113 deletions(-)

diff --git a/modules/codec/omxil/omxil.c b/modules/codec/omxil/omxil.c
index df75c93..c1ed20d 100644
--- a/modules/codec/omxil/omxil.c
+++ b/modules/codec/omxil/omxil.c
@@ -355,6 +355,113 @@ static OMX_ERRORTYPE UpdatePixelAspect(decoder_t *p_dec)
     return omx_err;
 }
 
+static OMX_ERRORTYPE AllocateBuffers(decoder_t *p_dec, OmxPort *p_port)
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    OMX_ERRORTYPE omx_error = OMX_ErrorUndefined;
+    OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
+    unsigned int i;
+
+#ifdef OMXIL_EXTRA_DEBUG
+    msg_Dbg( p_dec, "AllocateBuffers(%d)", def->eDir );
+#endif
+
+    p_port->i_buffers = p_port->definition.nBufferCountActual;
+
+    p_port->pp_buffers = calloc(p_port->i_buffers, sizeof(OMX_BUFFERHEADERTYPE*));
+    if( !p_port->pp_buffers )
+    {
+        return OMX_ErrorInsufficientResources;
+    }
+
+    for(i = 0; i < p_port->i_buffers; i++)
+    {
+#if 0
+#define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
+        char *p_buf = malloc(p_port->definition.nBufferSize +
+                             p_port->definition.nBufferAlignment);
+        p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
+#endif
+
+        if(p_port->b_direct)
+            omx_error =
+                OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
+                               p_port->i_port_index, 0,
+                               p_port->definition.nBufferSize, (void*)1);
+        else
+            omx_error =
+                OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
+                                    p_port->i_port_index, 0,
+                                    p_port->definition.nBufferSize);
+
+        if(omx_error != OMX_ErrorNone) break;
+        OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
+    }
+    CHECK_ERROR(omx_error, "AllocateBuffers failed (%x, %i)",
+                omx_error, (int)p_port->i_port_index );
+
+
+#ifdef OMXIL_EXTRA_DEBUG
+    msg_Dbg( p_dec, "AllocateBuffers(%d)::done", def->eDir );
+#endif
+error:
+    return omx_error;
+}
+
+static OMX_ERRORTYPE FreeBuffers(decoder_t *p_dec, OmxPort *p_port)
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    OMX_PARAM_PORTDEFINITIONTYPE *def = &p_port->definition;
+    OMX_ERRORTYPE omx_error = OMX_ErrorNone;
+    OMX_BUFFERHEADERTYPE *p_buffer;
+    unsigned int i;
+
+#ifdef OMXIL_EXTRA_DEBUG
+    msg_Dbg( p_dec, "FreeBuffers(%d)", def->eDir );
+#endif
+
+    /* wait for omx to release all buffers */
+    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_Dbg( p_dec, "Stray buffer left in fifo, %p", p_buffer );
+    }
+
+    for(i = 0; i < p_port->i_buffers; i++)
+    {
+        p_buffer =  p_port->pp_buffers[i];
+
+        if( p_buffer )
+        {
+            if( p_buffer->pAppPrivate != NULL )
+                decoder_DeletePicture( p_dec, p_buffer->pAppPrivate );
+            omx_error = OMX_FreeBuffer( p_sys->omx_handle,
+                                        p_port->i_port_index, p_buffer );
+            if(omx_error != OMX_ErrorNone) break;
+        }
+    }
+
+    p_port->i_buffers = 0;
+
+    if( p_port->pp_buffers )
+    {
+        free( p_port->pp_buffers );
+        p_port->pp_buffers = NULL;
+    }
+
+#ifdef OMXIL_EXTRA_DEBUG
+    msg_Dbg( p_dec, "FreeBuffers(%d)::done", def->eDir );
+#endif
+
+    return omx_error;
+}
+
 /*****************************************************************************
  * GetPortDefinition: set vlc format based on the definition of the omx port
  *****************************************************************************/
@@ -507,7 +614,7 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
     decoder_sys_t *p_sys = p_dec->p_sys;
     OMX_ERRORTYPE omx_error;
     OMX_STATETYPE state;
-    unsigned int i, j;
+    unsigned int i;
 
     if(!omx_handle) return OMX_ErrorNone;
 
@@ -542,34 +649,10 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
         for(i = 0; i < p_sys->ports; i++)
         {
             OmxPort *p_port = &p_sys->p_ports[i];
-            OMX_BUFFERHEADERTYPE *p_buffer;
-
-            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 );
 
-                if(omx_error != OMX_ErrorNone) break;
-            }
-            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 = FreeBuffers( p_dec, p_port );
+            CHECK_ERROR(omx_error, "FreeBuffers failed (%x, %i)",
+                        omx_error, (int)p_port->i_port_index );
         }
 
         omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
@@ -580,7 +663,8 @@ static OMX_ERRORTYPE DeinitialiseComponent(decoder_t *p_dec,
     for(i = 0; i < p_sys->ports; i++)
     {
         OmxPort *p_port = &p_sys->p_ports[i];
-        free(p_port->pp_buffers);
+        if( p_port->pp_buffers )
+            free(p_port->pp_buffers);
         p_port->pp_buffers = 0;
     }
     omx_error = pf_free_handle( omx_handle );
@@ -740,16 +824,6 @@ static OMX_ERRORTYPE InitialiseComponent(decoder_t *p_dec,
     {
         OmxPort *p_port = &p_sys->p_ports[i];
 
-        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;
-
         /* Enable port */
         if(!p_port->definition.bEnabled)
         {
@@ -825,7 +899,7 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
     decoder_sys_t *p_sys;
     OMX_ERRORTYPE omx_error;
     OMX_BUFFERHEADERTYPE *p_header;
-    unsigned int i, j;
+    unsigned int i;
 
     if (InitOmxCore(p_this) != VLC_SUCCESS) {
         return VLC_EGENERIC;
@@ -941,33 +1015,9 @@ static int OpenGeneric( vlc_object_t *p_this, bool b_encode )
     for(i = 0; i < p_sys->ports; i++)
     {
         OmxPort *p_port = &p_sys->p_ports[i];
-
-        for(j = 0; j < p_port->i_buffers; j++)
-        {
-#if 0
-#define ALIGN(x,BLOCKLIGN) (((x) + BLOCKLIGN - 1) & ~(BLOCKLIGN - 1))
-            char *p_buf = malloc(p_port->definition.nBufferSize +
-                                 p_port->definition.nBufferAlignment);
-            p_port->pp_buffers[i] = (void *)ALIGN((uintptr_t)p_buf, p_port->definition.nBufferAlignment);
-#endif
-
-            if(p_port->b_direct)
-                omx_error =
-                    OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
-                                   p_port->i_port_index, 0,
-                                   p_port->definition.nBufferSize, (void*)1);
-            else
-                omx_error =
-                    OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[j],
-                                        p_port->i_port_index, 0,
-                                        p_port->definition.nBufferSize);
-
-            if(omx_error != OMX_ErrorNone) break;
-            OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[j]);
-        }
-        p_port->i_buffers = j;
-        CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
-                    omx_error, (int)p_port->i_port_index, j );
+        omx_error = AllocateBuffers( p_dec, p_port );
+        CHECK_ERROR(omx_error, "AllocateBuffers failed (%x, %i)",
+                    omx_error, (int)p_port->i_port_index );
     }
 
     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
@@ -1094,23 +1144,9 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
     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++)
-    {
-        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 );
-
-        if(omx_error != OMX_ErrorNone) break;
-    }
-    CHECK_ERROR(omx_error, "OMX_FreeBuffer failed (%x, %i, %i)",
-                omx_error, (int)p_port->i_port_index, i );
+    omx_error = FreeBuffers( p_dec, p_port );
+    CHECK_ERROR(omx_error, "FreeBuffers failed (%x, %i)",
+                omx_error, (int)p_port->i_port_index );
 
     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
     CHECK_ERROR(omx_error, "Wait for PortDisable failed (%x)", omx_error );
@@ -1143,35 +1179,9 @@ static OMX_ERRORTYPE PortReconfigure(decoder_t *p_dec, OmxPort *p_port)
     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(p_port->b_direct)
-            omx_error =
-                OMX_UseBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
-                               p_port->i_port_index, 0,
-                               p_port->definition.nBufferSize, (void*)1);
-        else
-            omx_error =
-                OMX_AllocateBuffer( p_sys->omx_handle, &p_port->pp_buffers[i],
-                                    p_port->i_port_index, 0,
-                                    p_port->definition.nBufferSize);
-
-        if(omx_error != OMX_ErrorNone) break;
-        OMX_FIFO_PUT(&p_port->fifo, p_port->pp_buffers[i]);
-    }
-    p_port->i_buffers = i;
-    CHECK_ERROR(omx_error, "OMX_UseBuffer failed (%x, %i, %i)",
-                omx_error, (int)p_port->i_port_index, i );
+    omx_error = AllocateBuffers( p_dec, p_port );
+    CHECK_ERROR(omx_error, "OMX_AllocateBuffers failed (%x, %i)",
+                omx_error, (int)p_port->i_port_index );
 
     omx_error = WaitForSpecificOmxEvent(&p_sys->event_queue, OMX_EventCmdComplete, 0, 0, 0);
     CHECK_ERROR(omx_error, "Wait for PortEnable failed (%x)", omx_error );
-- 
1.7.10.4


-- 


This email and any files transmitted with it are confidential and are 
intended solely for the use of the individual or entity to which they are 
addressed. Access to this e-mail by anyone else is unauthorised. If you are 
not the intended recipient, any disclosure, copying, distribution or any 
action taken or omitted to be taken in reliance on it, is prohibited. 
E-mail messages are not necessarily secure. Archos does not accept 
responsibility for any changes made to this message after it was sent.



More information about the vlc-devel mailing list