[libav-devel] [PATCH 3/3] omx: Add support for zerocopy input of frames

Martin Storsjö martin at martin.st
Thu Apr 7 11:47:38 CEST 2016


This can only be used if the input data happens to be laid out
exactly correctly.

This might not be supported on all encoders, so only enable it
with an option, but enable it automatically on raspberry pi,
where it is known to be supported.
---
 libavcodec/omx.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 93 insertions(+), 15 deletions(-)

diff --git a/libavcodec/omx.c b/libavcodec/omx.c
index b50376c..12f68d6 100644
--- a/libavcodec/omx.c
+++ b/libavcodec/omx.c
@@ -219,6 +219,8 @@ typedef struct OMXCodecContext {
 
     uint8_t *output_buf;
     int output_buf_size;
+
+    int input_zerocopy;
 } OMXCodecContext;
 
 static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data)
@@ -502,8 +504,14 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
     s->done_out_buffers   = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_out_buffers);
     if (!s->in_buffer_headers || !s->free_in_buffers || !s->out_buffer_headers || !s->done_out_buffers)
         return AVERROR(ENOMEM);
-    for (i = 0; i < s->num_in_buffers && err == OMX_ErrorNone; i++)
-        err = OMX_AllocateBuffer(s->handle, &s->in_buffer_headers[i],  s->in_port,  s, in_port_params.nBufferSize);
+    for (i = 0; i < s->num_in_buffers && err == OMX_ErrorNone; i++) {
+        if (s->input_zerocopy)
+            err = OMX_UseBuffer(s->handle, &s->in_buffer_headers[i], s->in_port, s, in_port_params.nBufferSize, NULL);
+        else
+            err = OMX_AllocateBuffer(s->handle, &s->in_buffer_headers[i],  s->in_port,  s, in_port_params.nBufferSize);
+        if (err == OMX_ErrorNone)
+            s->in_buffer_headers[i]->pAppPrivate = s->in_buffer_headers[i]->pOutputPortPrivate = NULL;
+    }
     CHECK(err);
     s->num_in_buffers = i;
     for (i = 0; i < s->num_out_buffers && err == OMX_ErrorNone; i++)
@@ -550,6 +558,13 @@ static av_cold void cleanup(OMXCodecContext *s)
             s->num_free_in_buffers--;
             memmove(&s->free_in_buffers[0], &s->free_in_buffers[1], s->num_free_in_buffers * sizeof(OMX_BUFFERHEADERTYPE*));
             pthread_mutex_unlock(&s->input_mutex);
+            if (s->input_zerocopy && buffer->pAppPrivate) {
+                if (buffer->pOutputPortPrivate)
+                    av_free(buffer->pAppPrivate);
+                else
+                    av_frame_free((AVFrame**)&buffer->pAppPrivate);
+                buffer->pBuffer = NULL;
+            }
             OMX_FreeBuffer(s->handle, s->in_port, buffer);
         }
         for (i = 0; i < s->num_out_buffers; i++) {
@@ -591,6 +606,10 @@ static av_cold int omx_encode_init(AVCodecContext *avctx)
     const char *role;
     OMX_BUFFERHEADERTYPE *buffer;
 
+#if CONFIG_RPI_OMX
+    s->input_zerocopy = 1;
+#endif
+
     if ((ret = omx_init(avctx, s->libname, s->libprefix)) < 0)
         return ret;
 
@@ -680,19 +699,13 @@ static int omx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     uint8_t *ptr;
 
     if (frame) {
-        int chroma_linesize, chroma_stride;
+        int chroma_linesize, chroma_stride, need_copy;
         pthread_mutex_lock(&s->input_mutex);
         while (!s->num_free_in_buffers)
             pthread_cond_wait(&s->input_cond, &s->input_mutex);
         buffer = s->free_in_buffers[--s->num_free_in_buffers];
         pthread_mutex_unlock(&s->input_mutex);
 
-        ptr = buffer->pBuffer;
-        for (i = 0; i < avctx->height; i++) {
-            memcpy(ptr, frame->data[0] + i*frame->linesize[0], avctx->width);
-            ptr += s->stride;
-        }
-        ptr += s->stride * (s->plane_size - avctx->height);
         if (avctx->pix_fmt == AV_PIX_FMT_NV12 || avctx->pix_fmt == AV_PIX_FMT_NV21) {
             chroma_linesize = avctx->width;
             chroma_stride = s->stride;
@@ -700,16 +713,79 @@ static int omx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
             chroma_linesize = avctx->width/2;
             chroma_stride = s->stride/2;
         }
-        for (i = 0; i < avctx->height/2; i++) {
-            memcpy(ptr, frame->data[1] + i*frame->linesize[1], chroma_linesize);
-            ptr += chroma_stride;
+
+        if (s->input_zerocopy) {
+            int error = 0;
+            // We release the previous buffer attached to the OMX buffer header
+            // only here when we want to reuse it; we could also release it
+            // in the emptyBufferDone callback.
+            if (buffer->pAppPrivate) {
+                if (buffer->pOutputPortPrivate)
+                    av_free(buffer->pAppPrivate);
+                else
+                    av_frame_free((AVFrame**)&buffer->pAppPrivate);
+                buffer->pAppPrivate = NULL;
+            }
+            // YUV420P only, for now
+            if (frame->buf[0] && !frame->buf[1] && !frame->buf[2] &&
+                frame->linesize[0] == s->stride &&
+                frame->linesize[1] == chroma_stride &&
+                frame->data[0] + s->plane_size*s->stride == frame->data[1] &&
+                frame->data[1] + s->plane_size/2*chroma_stride == frame->data[2]) {
+                // If the input frame happens to have all planes stored contiguously,
+                // with the right strides, just clone the frame and set the OMX
+                // buffer header to point to it
+                AVFrame *local = av_frame_clone(frame);
+                if (!local) {
+                    error = 1;
+                } else {
+                    buffer->pAppPrivate = local;
+                    buffer->pOutputPortPrivate = NULL;
+                    buffer->pBuffer = local->data[0];
+                    need_copy = 0;
+                }
+            } else {
+                // If not, we need to allocate a new buffer with the right
+                // size and copy the input frame into it.
+                uint8_t *buf = av_malloc(s->stride*s->plane_size*3/2);
+                if (!buf) {
+                    error = 1;
+                } else {
+                    buffer->pAppPrivate = buf;
+                    buffer->pOutputPortPrivate = (void*) 1;
+                    buffer->pBuffer = buf;
+                    need_copy = 1;
+                }
+            }
+            if (error) {
+                // If we failed, return the buffer to the queue so it's not
+                // lost
+                pthread_mutex_lock(&s->input_mutex);
+                s->free_in_buffers[s->num_free_in_buffers++] = buffer;
+                pthread_mutex_unlock(&s->input_mutex);
+                return AVERROR(ENOMEM);
+            }
+        } else {
+            need_copy = 1;
         }
-        if (avctx->pix_fmt != AV_PIX_FMT_NV12 && avctx->pix_fmt != AV_PIX_FMT_NV21) {
-            ptr += chroma_stride * (s->plane_size/2 - avctx->height/2);
+        if (need_copy) {
+            ptr = buffer->pBuffer;
+            for (i = 0; i < avctx->height; i++) {
+                memcpy(ptr, frame->data[0] + i*frame->linesize[0], avctx->width);
+                ptr += s->stride;
+            }
+            ptr += s->stride * (s->plane_size - avctx->height);
             for (i = 0; i < avctx->height/2; i++) {
-                memcpy(ptr, frame->data[2] + i*frame->linesize[2], chroma_linesize);
+                memcpy(ptr, frame->data[1] + i*frame->linesize[1], chroma_linesize);
                 ptr += chroma_stride;
             }
+            if (avctx->pix_fmt != AV_PIX_FMT_NV12 && avctx->pix_fmt != AV_PIX_FMT_NV21) {
+                ptr += chroma_stride * (s->plane_size/2 - avctx->height/2);
+                for (i = 0; i < avctx->height/2; i++) {
+                    memcpy(ptr, frame->data[2] + i*frame->linesize[2], chroma_linesize);
+                    ptr += chroma_stride;
+                }
+            }
         }
         buffer->nFilledLen = s->stride*s->plane_size + 2*s->stride/2*s->plane_size/2;
         buffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
@@ -795,9 +871,11 @@ static av_cold int omx_encode_end(AVCodecContext *avctx)
 
 #define OFFSET(x) offsetof(OMXCodecContext, x)
 #define VDE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+#define VE  AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
     { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
+    { "zerocopy", "Try to avoid copying input frames if possible", OFFSET(input_zerocopy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
     { NULL }
 };
 
-- 
2.1.4



More information about the libav-devel mailing list