21#if HAVE_UTGETOSTYPEFROMSTRING
22#include <CoreServices/CoreServices.h>
25#include "libavcodec/avcodec.h"
26#include "libavcodec/videotoolbox.h"
27#include "libavutil/imgutils.h"
41 CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
42 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
44 uint8_t *data[4] = { 0 };
45 int linesize[4] = { 0 };
48 if (frame->format ==
ist->hwaccel_output_format) {
49 av_log_once(s, AV_LOG_INFO, AV_LOG_TRACE, &vt->
log_once,
50 "There is no video filter for videotoolbox pix_fmt now, remove the "
51 "-hwaccel_output_format option if video filter doesn't work\n");
57 switch (pixel_format) {
58 case kCVPixelFormatType_420YpCbCr8Planar: vt->
tmp_frame->format = AV_PIX_FMT_YUV420P;
break;
59 case kCVPixelFormatType_422YpCbCr8: vt->
tmp_frame->format = AV_PIX_FMT_UYVY422;
break;
60 case kCVPixelFormatType_32BGRA: vt->
tmp_frame->format = AV_PIX_FMT_BGRA;
break;
61#ifdef kCFCoreFoundationVersionNumber10_7
62 case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
63 case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange: vt->
tmp_frame->format = AV_PIX_FMT_NV12;
break;
65#if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
66 case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
67 case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange: vt->
tmp_frame->format = AV_PIX_FMT_P010;
break;
70 av_log(NULL, AV_LOG_ERROR,
71 "%s: Unsupported pixel format: %s\n",
73 return AVERROR(ENOSYS);
78 ret = av_frame_get_buffer(vt->
tmp_frame, 0);
82 err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
83 if (err != kCVReturnSuccess) {
84 av_log(NULL, AV_LOG_ERROR,
"Error locking the pixel buffer.\n");
85 return AVERROR_UNKNOWN;
88 if (CVPixelBufferIsPlanar(pixbuf)) {
90 planes = CVPixelBufferGetPlaneCount(pixbuf);
91 for (i = 0; i < planes; i++) {
92 data[i] = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
93 linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
96 data[0] = CVPixelBufferGetBaseAddress(pixbuf);
97 linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
101 (
const uint8_t **)data, linesize, vt->
tmp_frame->format,
102 frame->width, frame->height);
104 ret = av_frame_copy_props(vt->
tmp_frame, frame);
105 CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
109 av_frame_unref(frame);
120 ist->hwaccel_uninit = NULL;
121 ist->hwaccel_retrieve_data = NULL;
125 av_videotoolbox_default_free(s);
126 av_freep(&
ist->hwaccel_ctx);
132 int loglevel = (
ist->hwaccel_id ==
HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
136 vt = av_mallocz(
sizeof(*vt));
138 return AVERROR(ENOMEM);
140 ist->hwaccel_ctx = vt;
146 ret = AVERROR(ENOMEM);
152 ret = av_videotoolbox_default_init(s);
154 AVVideotoolboxContext *vtctx = av_videotoolbox_alloc_context();
155 CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
157 kCFStringEncodingUTF8);
158#if HAVE_UTGETOSTYPEFROMSTRING
159 vtctx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
161 av_log(s, loglevel,
"UTGetOSTypeFromString() is not available "
162 "on this platform, %s pixel format can not be honored from "
165 ret = av_videotoolbox_default_init2(s, vtctx);
166 CFRelease(pixfmt_str);
169 av_log(NULL, loglevel,
"Error creating Videotoolbox decoder.\n");