[FFmpeg-cvslog] vda: support synchronous decoding

Sebastien Zwickert git at videolan.org
Tue Aug 14 15:39:44 CEST 2012


ffmpeg | branch: master | Sebastien Zwickert <dilaroga at gmail.com> | Tue Aug 14 11:45:29 2012 +0200| [3c37970637c0e3f1576cae58feed05497e383fc4] | committer: Diego Biurrun

vda: support synchronous decoding

Note that the symbols used to run the hardware decoder in asynchronous mode
have been marked deprecated and will be dropped at a future version bump.

Signed-off-by: Diego Biurrun <diego at biurrun.de>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=3c37970637c0e3f1576cae58feed05497e383fc4
---

 libavcodec/vda.c          |  105 +++++++++++++++++++++++++++------------------
 libavcodec/vda.h          |   47 ++++++++++++++++++--
 libavcodec/vda_h264.c     |   11 +++--
 libavcodec/vda_internal.h |    4 ++
 libavcodec/version.h      |    3 ++
 5 files changed, 123 insertions(+), 47 deletions(-)

diff --git a/libavcodec/vda.c b/libavcodec/vda.c
index d962c61..3ef8af4 100644
--- a/libavcodec/vda.c
+++ b/libavcodec/vda.c
@@ -20,8 +20,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <pthread.h>
-#include <CoreFoundation/CFDictionary.h>
 #include <CoreFoundation/CFNumber.h>
 #include <CoreFoundation/CFData.h>
 #include <CoreFoundation/CFString.h>
@@ -29,6 +27,9 @@
 #include "libavutil/avutil.h"
 #include "vda_internal.h"
 
+#if FF_API_VDA_ASYNC
+#include <CoreFoundation/CFDictionary.h>
+
 /* helper to create a dictionary according to the given pts */
 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
 {
@@ -77,6 +78,7 @@ static void vda_clear_queue(struct vda_context *vda_ctx)
 
     pthread_mutex_unlock(&vda_ctx->queue_mutex);
 }
+#endif
 
 /* Decoder callback that adds the VDA frame to the queue in display order. */
 static void vda_decoder_callback(void *vda_hw_ctx,
@@ -86,8 +88,6 @@ static void vda_decoder_callback(void *vda_hw_ctx,
                                  CVImageBufferRef image_buffer)
 {
     struct vda_context *vda_ctx = vda_hw_ctx;
-    vda_frame *new_frame;
-    vda_frame *queue_walker;
 
     if (!image_buffer)
         return;
@@ -95,35 +95,42 @@ static void vda_decoder_callback(void *vda_hw_ctx,
     if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
         return;
 
-    if (!(new_frame = av_mallocz(sizeof(vda_frame))))
-        return;
-    new_frame->next_frame = NULL;
-    new_frame->cv_buffer  = CVPixelBufferRetain(image_buffer);
-    new_frame->pts        = vda_pts_from_dictionary(user_info);
-
-    pthread_mutex_lock(&vda_ctx->queue_mutex);
-
-    queue_walker = vda_ctx->queue;
-
-    if (!queue_walker || new_frame->pts < queue_walker->pts) {
-        /* we have an empty queue, or this frame earlier than the current queue head */
-        new_frame->next_frame = queue_walker;
-        vda_ctx->queue        = new_frame;
+    if (vda_ctx->use_sync_decoding) {
+        vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
     } else {
-        /* walk the queue and insert this frame where it belongs in display order */
-        vda_frame *next_frame;
-        while (1) {
-            next_frame = queue_walker->next_frame;
-            if (!next_frame || new_frame->pts < next_frame->pts) {
-                new_frame->next_frame    = next_frame;
-                queue_walker->next_frame = new_frame;
-                break;
+        vda_frame *new_frame;
+        vda_frame *queue_walker;
+
+        if (!(new_frame = av_mallocz(sizeof(vda_frame))))
+            return;
+        new_frame->next_frame = NULL;
+        new_frame->cv_buffer  = CVPixelBufferRetain(image_buffer);
+        new_frame->pts        = vda_pts_from_dictionary(user_info);
+
+        pthread_mutex_lock(&vda_ctx->queue_mutex);
+
+        queue_walker = vda_ctx->queue;
+
+        if (!queue_walker || new_frame->pts < queue_walker->pts) {
+            /* we have an empty queue, or this frame earlier than the current queue head */
+            new_frame->next_frame = queue_walker;
+            vda_ctx->queue        = new_frame;
+        } else {
+            /* walk the queue and insert this frame where it belongs in display order */
+            vda_frame *next_frame;
+            while (1) {
+                next_frame = queue_walker->next_frame;
+                if (!next_frame || new_frame->pts < next_frame->pts) {
+                    new_frame->next_frame    = next_frame;
+                    queue_walker->next_frame = new_frame;
+                    break;
+                }
+                queue_walker = next_frame;
             }
-            queue_walker = next_frame;
         }
-    }
 
-    pthread_mutex_unlock(&vda_ctx->queue_mutex);
+        pthread_mutex_unlock(&vda_ctx->queue_mutex);
+    }
 }
 
 int ff_vda_create_decoder(struct vda_context *vda_ctx,
@@ -140,7 +147,9 @@ int ff_vda_create_decoder(struct vda_context *vda_ctx,
     CFMutableDictionaryRef io_surface_properties;
     CFNumberRef cv_pix_fmt;
 
+#if FF_API_VDA_ASYNC
     pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
+#endif
 
     /* Each VCL NAL in the bistream sent to the decoder
      * is preceeded by a 4 bytes length header.
@@ -209,10 +218,7 @@ int ff_vda_create_decoder(struct vda_context *vda_ctx,
     CFRelease(cv_pix_fmt);
     CFRelease(buffer_attributes);
 
-    if (kVDADecoderNoErr != status)
-        return status;
-
-    return 0;
+    return status;
 }
 
 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
@@ -222,18 +228,17 @@ int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
     if (vda_ctx->decoder)
         status = VDADecoderDestroy(vda_ctx->decoder);
 
+#if FF_API_VDA_ASYNC
     vda_clear_queue(vda_ctx);
-
     pthread_mutex_destroy(&vda_ctx->queue_mutex);
+#endif
 
     av_freep(&vda_ctx->priv_bitstream);
 
-    if (kVDADecoderNoErr != status)
-        return status;
-
-    return 0;
+    return status;
 }
 
+#if FF_API_VDA_ASYNC
 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
 {
     vda_frame *top_frame;
@@ -273,8 +278,26 @@ int ff_vda_decoder_decode(struct vda_context *vda_ctx,
     CFRelease(user_info);
     CFRelease(coded_frame);
 
-    if (kVDADecoderNoErr != status)
-        return status;
+    return status;
+}
+#endif
+
+int ff_vda_sync_decode(struct vda_context *vda_ctx)
+{
+    OSStatus status;
+    CFDataRef coded_frame;
+    uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
+
+    coded_frame = CFDataCreate(kCFAllocatorDefault,
+                               vda_ctx->priv_bitstream,
+                               vda_ctx->priv_bitstream_size);
+
+    status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
+
+    if (kVDADecoderNoErr == status)
+        status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
+
+    CFRelease(coded_frame);
 
-    return 0;
+    return status;
 }
diff --git a/libavcodec/vda.h b/libavcodec/vda.h
index ec7d2f7..4daaa14 100644
--- a/libavcodec/vda.h
+++ b/libavcodec/vda.h
@@ -29,7 +29,12 @@
  * Public libavcodec VDA header.
  */
 
+#include "libavcodec/version.h"
+
+#if FF_API_VDA_ASYNC
 #include <pthread.h>
+#endif
+
 #include <stdint.h>
 
 // emmintrin.h is unable to compile with -std=c99 -Werror=missing-prototypes
@@ -47,8 +52,11 @@
  * @{
  */
 
+#if FF_API_VDA_ASYNC
 /**
- *  This structure is used to store a decoded frame information and data.
+ * This structure is used to store decoded frame information and data.
+ *
+ * @deprecated Use synchronous decoding mode.
  */
 typedef struct vda_frame {
     /**
@@ -75,6 +83,7 @@ typedef struct vda_frame {
     */
     struct vda_frame    *next_frame;
 } vda_frame;
+#endif
 
 /**
  * This structure is used to provide the necessary configurations and data
@@ -92,8 +101,27 @@ struct vda_context {
     VDADecoder          decoder;
 
     /**
+    * The Core Video pixel buffer that contains the current image data.
+    *
+    * encoding: unused
+    * decoding: Set by libavcodec. Unset by user.
+    */
+    CVPixelBufferRef    cv_buffer;
+
+    /**
+    * Use the hardware decoder in synchronous mode.
+    *
+    * encoding: unused
+    * decoding: Set by user.
+    */
+    int                 use_sync_decoding;
+
+#if FF_API_VDA_ASYNC
+    /**
     * VDA frames queue ordered by presentation timestamp.
     *
+    * @deprecated Use synchronous decoding mode.
+    *
     * - encoding: unused
     * - decoding: Set/Unset by libavcodec.
     */
@@ -102,10 +130,13 @@ struct vda_context {
     /**
     * Mutex for locking queue operations.
     *
+    * @deprecated Use synchronous decoding mode.
+    *
     * - encoding: unused
     * - decoding: Set/Unset by libavcodec.
     */
     pthread_mutex_t     queue_mutex;
+#endif
 
     /**
     * The frame width.
@@ -163,11 +194,21 @@ int ff_vda_create_decoder(struct vda_context *vda_ctx,
 /** Destroy the video decoder. */
 int ff_vda_destroy_decoder(struct vda_context *vda_ctx);
 
-/** Return the top frame of the queue. */
+#if FF_API_VDA_ASYNC
+/**
+* Return the top frame of the queue.
+*
+* @deprecated Use synchronous decoding mode.
+*/
 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx);
 
-/** Release the given frame. */
+/**
+* Release the given frame.
+*
+* @deprecated Use synchronous decoding mode.
+*/
 void ff_vda_release_vda_frame(vda_frame *frame);
+#endif
 
 /**
  * @}
diff --git a/libavcodec/vda_h264.c b/libavcodec/vda_h264.c
index a3f8c11..e621973 100644
--- a/libavcodec/vda_h264.c
+++ b/libavcodec/vda_h264.c
@@ -73,9 +73,14 @@ static int end_frame(AVCodecContext *avctx)
     if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
         return -1;
 
-    status = ff_vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
-                                   vda_ctx->priv_bitstream_size,
-                                   frame->reordered_opaque);
+    if (vda_ctx->use_sync_decoding) {
+        status = ff_vda_sync_decode(vda_ctx);
+        frame->data[3] = (void*)vda_ctx->cv_buffer;
+    } else {
+        status = ff_vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
+                                       vda_ctx->priv_bitstream_size,
+                                       frame->reordered_opaque);
+    }
 
     if (status)
         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
diff --git a/libavcodec/vda_internal.h b/libavcodec/vda_internal.h
index 364ebfb..f5d24c2 100644
--- a/libavcodec/vda_internal.h
+++ b/libavcodec/vda_internal.h
@@ -31,11 +31,15 @@
  * @{
  */
 
+#if FF_API_VDA_ASYNC
 /** Send frame data to the hardware decoder. */
 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
                           uint8_t *bitstream,
                           int bitstream_size,
                           int64_t frame_pts);
+#endif
+
+int ff_vda_sync_decode(struct vda_context *vda_ctx);
 
 /* @} */
 
diff --git a/libavcodec/version.h b/libavcodec/version.h
index a57369a..a5ff119 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -79,5 +79,8 @@
 #ifndef FF_API_CODEC_ID
 #define FF_API_CODEC_ID          (LIBAVCODEC_VERSION_MAJOR < 55)
 #endif
+#ifndef FF_API_VDA_ASYNC
+#define FF_API_VDA_ASYNC         (LIBAVCODEC_VERSION_MAJOR < 55)
+#endif
 
 #endif /* AVCODEC_VERSION_H */



More information about the ffmpeg-cvslog mailing list