mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2024-12-23 12:43:46 +02:00
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@biurrun.de>
This commit is contained in:
parent
cfc680ab39
commit
3c37970637
111
libavcodec/vda.c
111
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;
|
||||
}
|
||||
queue_walker = next_frame;
|
||||
}
|
||||
}
|
||||
vda_frame *new_frame;
|
||||
vda_frame *queue_walker;
|
||||
|
||||
pthread_mutex_unlock(&vda_ctx->queue_mutex);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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 0;
|
||||
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 status;
|
||||
}
|
||||
|
@ -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
|
||||
@ -91,9 +100,28 @@ 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
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
||||
/* @} */
|
||||
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user