]> git.sesse.net Git - ffmpeg/blob - ffmpeg_videotoolbox.c
Merge commit 'c3e5c47bdae2bb8219fea62d91b7455650b22c60'
[ffmpeg] / ffmpeg_videotoolbox.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #if HAVE_UTGETOSTYPEFROMSTRING
20 #include <CoreServices/CoreServices.h>
21 #endif
22
23 #include "config.h"
24 #include "libavcodec/avcodec.h"
25 #if CONFIG_VDA
26 #  include "libavcodec/vda.h"
27 #endif
28 #if CONFIG_VIDEOTOOLBOX
29 #  include "libavcodec/videotoolbox.h"
30 #endif
31 #include "libavutil/imgutils.h"
32 #include "ffmpeg.h"
33
34 typedef struct VTContext {
35     AVFrame *tmp_frame;
36 } VTContext;
37
38 char *videotoolbox_pixfmt;
39
40 static int videotoolbox_retrieve_data(AVCodecContext *s, AVFrame *frame)
41 {
42     InputStream *ist = s->opaque;
43     VTContext  *vt = ist->hwaccel_ctx;
44     CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
45     OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
46     CVReturn err;
47     uint8_t *data[4] = { 0 };
48     int linesize[4] = { 0 };
49     int planes, ret, i;
50     char codec_str[32];
51
52     av_frame_unref(vt->tmp_frame);
53
54     switch (pixel_format) {
55     case kCVPixelFormatType_420YpCbCr8Planar: vt->tmp_frame->format = AV_PIX_FMT_YUV420P; break;
56     case kCVPixelFormatType_422YpCbCr8:       vt->tmp_frame->format = AV_PIX_FMT_UYVY422; break;
57     case kCVPixelFormatType_32BGRA:           vt->tmp_frame->format = AV_PIX_FMT_BGRA; break;
58 #ifdef kCFCoreFoundationVersionNumber10_7
59     case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: vt->tmp_frame->format = AV_PIX_FMT_NV12; break;
60 #endif
61     default:
62         av_get_codec_tag_string(codec_str, sizeof(codec_str), s->codec_tag);
63         av_log(NULL, AV_LOG_ERROR,
64                "%s: Unsupported pixel format: %s\n", codec_str, videotoolbox_pixfmt);
65         return AVERROR(ENOSYS);
66     }
67
68     vt->tmp_frame->width  = frame->width;
69     vt->tmp_frame->height = frame->height;
70     ret = av_frame_get_buffer(vt->tmp_frame, 32);
71     if (ret < 0)
72         return ret;
73
74     err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
75     if (err != kCVReturnSuccess) {
76         av_log(NULL, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
77         return AVERROR_UNKNOWN;
78     }
79
80     if (CVPixelBufferIsPlanar(pixbuf)) {
81
82         planes = CVPixelBufferGetPlaneCount(pixbuf);
83         for (i = 0; i < planes; i++) {
84             data[i]     = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
85             linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
86         }
87     } else {
88         data[0] = CVPixelBufferGetBaseAddress(pixbuf);
89         linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
90     }
91
92     av_image_copy(vt->tmp_frame->data, vt->tmp_frame->linesize,
93                   (const uint8_t **)data, linesize, vt->tmp_frame->format,
94                   frame->width, frame->height);
95
96     ret = av_frame_copy_props(vt->tmp_frame, frame);
97     CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
98     if (ret < 0)
99         return ret;
100
101     av_frame_unref(frame);
102     av_frame_move_ref(frame, vt->tmp_frame);
103
104     return 0;
105 }
106
107 static void videotoolbox_uninit(AVCodecContext *s)
108 {
109     InputStream *ist = s->opaque;
110     VTContext  *vt = ist->hwaccel_ctx;
111
112     ist->hwaccel_uninit        = NULL;
113     ist->hwaccel_retrieve_data = NULL;
114
115     av_frame_free(&vt->tmp_frame);
116
117     if (ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX) {
118 #if CONFIG_VIDEOTOOLBOX
119         av_videotoolbox_default_free(s);
120 #endif
121     } else {
122 #if CONFIG_VDA
123         av_vda_default_free(s);
124 #endif
125     }
126     av_freep(&ist->hwaccel_ctx);
127 }
128
129 int videotoolbox_init(AVCodecContext *s)
130 {
131     InputStream *ist = s->opaque;
132     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
133     int ret = 0;
134     VTContext *vt;
135
136     vt = av_mallocz(sizeof(*vt));
137     if (!vt)
138         return AVERROR(ENOMEM);
139
140     ist->hwaccel_ctx           = vt;
141     ist->hwaccel_uninit        = videotoolbox_uninit;
142     ist->hwaccel_retrieve_data = videotoolbox_retrieve_data;
143
144     vt->tmp_frame = av_frame_alloc();
145     if (!vt->tmp_frame) {
146         ret = AVERROR(ENOMEM);
147         goto fail;
148     }
149
150     if (ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX) {
151 #if CONFIG_VIDEOTOOLBOX
152         if (!videotoolbox_pixfmt) {
153             ret = av_videotoolbox_default_init(s);
154         } else {
155             AVVideotoolboxContext *vtctx = av_videotoolbox_alloc_context();
156             CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
157                                                                videotoolbox_pixfmt,
158                                                                kCFStringEncodingUTF8);
159             vtctx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
160             ret = av_videotoolbox_default_init2(s, vtctx);
161             CFRelease(pixfmt_str);
162         }
163 #endif
164     } else {
165 #if CONFIG_VDA
166         if (!videotoolbox_pixfmt) {
167             ret = av_vda_default_init(s);
168         } else {
169             AVVDAContext *vdactx = av_vda_alloc_context();
170             CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
171                                                                videotoolbox_pixfmt,
172                                                                kCFStringEncodingUTF8);
173 #if HAVE_UTGETOSTYPEFROMSTRING
174             vdactx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
175 #else
176             av_log(s, loglevel, "UTGetOSTypeFromString() is not available "
177                    "on this platform, %s pixel format can not be honored from "
178                    "the command line\n", videotoolbox_pixfmt);
179 #endif
180             ret = av_vda_default_init2(s, vdactx);
181             CFRelease(pixfmt_str);
182         }
183 #endif
184     }
185     if (ret < 0) {
186         av_log(NULL, loglevel,
187                "Error creating %s decoder.\n", ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX ? "Videotoolbox" : "VDA");
188         goto fail;
189     }
190
191     return 0;
192 fail:
193     videotoolbox_uninit(s);
194     return ret;
195 }