2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #if HAVE_UTGETOSTYPEFROMSTRING
22 #include <CoreServices/CoreServices.h>
25 #include "libavcodec/avcodec.h"
26 #include "libavcodec/videotoolbox.h"
27 #include "libavutil/imgutils.h"
30 typedef struct VTContext {
34 char *videotoolbox_pixfmt;
36 static int videotoolbox_retrieve_data(AVCodecContext *s, AVFrame *frame)
38 InputStream *ist = s->opaque;
39 VTContext *vt = ist->hwaccel_ctx;
40 CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
41 OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
43 uint8_t *data[4] = { 0 };
44 int linesize[4] = { 0 };
47 av_frame_unref(vt->tmp_frame);
49 switch (pixel_format) {
50 case kCVPixelFormatType_420YpCbCr8Planar: vt->tmp_frame->format = AV_PIX_FMT_YUV420P; break;
51 case kCVPixelFormatType_422YpCbCr8: vt->tmp_frame->format = AV_PIX_FMT_UYVY422; break;
52 case kCVPixelFormatType_32BGRA: vt->tmp_frame->format = AV_PIX_FMT_BGRA; break;
53 #ifdef kCFCoreFoundationVersionNumber10_7
54 case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: vt->tmp_frame->format = AV_PIX_FMT_NV12; break;
56 #if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
57 case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange: vt->tmp_frame->format = AV_PIX_FMT_P010; break;
60 av_log(NULL, AV_LOG_ERROR,
61 "%s: Unsupported pixel format: %s\n",
62 av_fourcc2str(s->codec_tag), videotoolbox_pixfmt);
63 return AVERROR(ENOSYS);
66 vt->tmp_frame->width = frame->width;
67 vt->tmp_frame->height = frame->height;
68 ret = av_frame_get_buffer(vt->tmp_frame, 32);
72 err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
73 if (err != kCVReturnSuccess) {
74 av_log(NULL, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
75 return AVERROR_UNKNOWN;
78 if (CVPixelBufferIsPlanar(pixbuf)) {
80 planes = CVPixelBufferGetPlaneCount(pixbuf);
81 for (i = 0; i < planes; i++) {
82 data[i] = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
83 linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
86 data[0] = CVPixelBufferGetBaseAddress(pixbuf);
87 linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
90 av_image_copy(vt->tmp_frame->data, vt->tmp_frame->linesize,
91 (const uint8_t **)data, linesize, vt->tmp_frame->format,
92 frame->width, frame->height);
94 ret = av_frame_copy_props(vt->tmp_frame, frame);
95 CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
99 av_frame_unref(frame);
100 av_frame_move_ref(frame, vt->tmp_frame);
105 static void videotoolbox_uninit(AVCodecContext *s)
107 InputStream *ist = s->opaque;
108 VTContext *vt = ist->hwaccel_ctx;
110 ist->hwaccel_uninit = NULL;
111 ist->hwaccel_retrieve_data = NULL;
113 av_frame_free(&vt->tmp_frame);
115 av_videotoolbox_default_free(s);
116 av_freep(&ist->hwaccel_ctx);
119 int videotoolbox_init(AVCodecContext *s)
121 InputStream *ist = s->opaque;
122 int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
126 vt = av_mallocz(sizeof(*vt));
128 return AVERROR(ENOMEM);
130 ist->hwaccel_ctx = vt;
131 ist->hwaccel_uninit = videotoolbox_uninit;
132 ist->hwaccel_retrieve_data = videotoolbox_retrieve_data;
134 vt->tmp_frame = av_frame_alloc();
135 if (!vt->tmp_frame) {
136 ret = AVERROR(ENOMEM);
141 if (!videotoolbox_pixfmt) {
142 ret = av_videotoolbox_default_init(s);
144 AVVideotoolboxContext *vtctx = av_videotoolbox_alloc_context();
145 CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
147 kCFStringEncodingUTF8);
148 #if HAVE_UTGETOSTYPEFROMSTRING
149 vtctx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
151 av_log(s, loglevel, "UTGetOSTypeFromString() is not available "
152 "on this platform, %s pixel format can not be honored from "
153 "the command line\n", videotoolbox_pixfmt);
155 ret = av_videotoolbox_default_init2(s, vtctx);
156 CFRelease(pixfmt_str);
159 av_log(NULL, loglevel, "Error creating Videotoolbox decoder.\n");
165 videotoolbox_uninit(s);