]> git.sesse.net Git - ffmpeg/blob - fftools/ffmpeg_videotoolbox.c
Merge commit 'c513fcd7d235aa4cef45a6c3125bd4dcc03bf276'
[ffmpeg] / fftools / 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 #include "config.h"
20
21 #if HAVE_UTGETOSTYPEFROMSTRING
22 #include <CoreServices/CoreServices.h>
23 #endif
24
25 #include "libavcodec/avcodec.h"
26 #include "libavcodec/videotoolbox.h"
27 #include "libavutil/imgutils.h"
28 #include "ffmpeg.h"
29
30 typedef struct VTContext {
31     AVFrame *tmp_frame;
32 } VTContext;
33
34 char *videotoolbox_pixfmt;
35
36 static int videotoolbox_retrieve_data(AVCodecContext *s, AVFrame *frame)
37 {
38     InputStream *ist = s->opaque;
39     VTContext  *vt = ist->hwaccel_ctx;
40     CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
41     OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
42     CVReturn err;
43     uint8_t *data[4] = { 0 };
44     int linesize[4] = { 0 };
45     int planes, ret, i;
46
47     av_frame_unref(vt->tmp_frame);
48
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;
55 #endif
56     default:
57         av_log(NULL, AV_LOG_ERROR,
58                "%s: Unsupported pixel format: %s\n",
59                av_fourcc2str(s->codec_tag), videotoolbox_pixfmt);
60         return AVERROR(ENOSYS);
61     }
62
63     vt->tmp_frame->width  = frame->width;
64     vt->tmp_frame->height = frame->height;
65     ret = av_frame_get_buffer(vt->tmp_frame, 32);
66     if (ret < 0)
67         return ret;
68
69     err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
70     if (err != kCVReturnSuccess) {
71         av_log(NULL, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
72         return AVERROR_UNKNOWN;
73     }
74
75     if (CVPixelBufferIsPlanar(pixbuf)) {
76
77         planes = CVPixelBufferGetPlaneCount(pixbuf);
78         for (i = 0; i < planes; i++) {
79             data[i]     = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
80             linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
81         }
82     } else {
83         data[0] = CVPixelBufferGetBaseAddress(pixbuf);
84         linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
85     }
86
87     av_image_copy(vt->tmp_frame->data, vt->tmp_frame->linesize,
88                   (const uint8_t **)data, linesize, vt->tmp_frame->format,
89                   frame->width, frame->height);
90
91     ret = av_frame_copy_props(vt->tmp_frame, frame);
92     CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
93     if (ret < 0)
94         return ret;
95
96     av_frame_unref(frame);
97     av_frame_move_ref(frame, vt->tmp_frame);
98
99     return 0;
100 }
101
102 static void videotoolbox_uninit(AVCodecContext *s)
103 {
104     InputStream *ist = s->opaque;
105     VTContext  *vt = ist->hwaccel_ctx;
106
107     ist->hwaccel_uninit        = NULL;
108     ist->hwaccel_retrieve_data = NULL;
109
110     av_frame_free(&vt->tmp_frame);
111
112     av_videotoolbox_default_free(s);
113     av_freep(&ist->hwaccel_ctx);
114 }
115
116 int videotoolbox_init(AVCodecContext *s)
117 {
118     InputStream *ist = s->opaque;
119     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
120     int ret = 0;
121     VTContext *vt;
122
123     vt = av_mallocz(sizeof(*vt));
124     if (!vt)
125         return AVERROR(ENOMEM);
126
127     ist->hwaccel_ctx           = vt;
128     ist->hwaccel_uninit        = videotoolbox_uninit;
129     ist->hwaccel_retrieve_data = videotoolbox_retrieve_data;
130
131     vt->tmp_frame = av_frame_alloc();
132     if (!vt->tmp_frame) {
133         ret = AVERROR(ENOMEM);
134         goto fail;
135     }
136
137     // TODO: reindent
138         if (!videotoolbox_pixfmt) {
139             ret = av_videotoolbox_default_init(s);
140         } else {
141             AVVideotoolboxContext *vtctx = av_videotoolbox_alloc_context();
142             CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
143                                                                videotoolbox_pixfmt,
144                                                                kCFStringEncodingUTF8);
145 #if HAVE_UTGETOSTYPEFROMSTRING
146             vtctx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
147 #else
148             av_log(s, loglevel, "UTGetOSTypeFromString() is not available "
149                    "on this platform, %s pixel format can not be honored from "
150                    "the command line\n", videotoolbox_pixfmt);
151 #endif
152             ret = av_videotoolbox_default_init2(s, vtctx);
153             CFRelease(pixfmt_str);
154         }
155     if (ret < 0) {
156         av_log(NULL, loglevel, "Error creating Videotoolbox decoder.\n");
157         goto fail;
158     }
159
160     return 0;
161 fail:
162     videotoolbox_uninit(s);
163     return ret;
164 }