]> git.sesse.net Git - ffmpeg/blob - ffmpeg_vda.c
Merge commit '650060dfb665552442ec11b456660e3e9a9d9016'
[ffmpeg] / ffmpeg_vda.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 "libavcodec/avcodec.h"
20 #include "libavcodec/vda.h"
21 #include "libavutil/imgutils.h"
22
23 #include "ffmpeg.h"
24
25 typedef struct VDAContext {
26     AVFrame *tmp_frame;
27 } VDAContext;
28
29 static int vda_retrieve_data(AVCodecContext *s, AVFrame *frame)
30 {
31     InputStream *ist = s->opaque;
32     VDAContext  *vda = ist->hwaccel_ctx;
33     CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
34     OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
35     CVReturn err;
36     uint8_t *data[4] = { 0 };
37     int linesize[4] = { 0 };
38     int planes, ret, i;
39
40     av_frame_unref(vda->tmp_frame);
41
42     switch (pixel_format) {
43     case kCVPixelFormatType_420YpCbCr8Planar: vda->tmp_frame->format = AV_PIX_FMT_YUV420P; break;
44     case kCVPixelFormatType_422YpCbCr8:       vda->tmp_frame->format = AV_PIX_FMT_UYVY422; break;
45     default:
46         av_log(NULL, AV_LOG_ERROR,
47                "Unsupported pixel format: %u\n", pixel_format);
48         return AVERROR(ENOSYS);
49     }
50
51     vda->tmp_frame->width  = frame->width;
52     vda->tmp_frame->height = frame->height;
53     ret = av_frame_get_buffer(vda->tmp_frame, 32);
54     if (ret < 0)
55         return ret;
56
57     err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
58     if (err != kCVReturnSuccess) {
59         av_log(NULL, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
60         return AVERROR_UNKNOWN;
61     }
62
63     if (CVPixelBufferIsPlanar(pixbuf)) {
64
65         planes = CVPixelBufferGetPlaneCount(pixbuf);
66         for (i = 0; i < planes; i++) {
67             data[i]     = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
68             linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
69         }
70     } else {
71         data[0] = CVPixelBufferGetBaseAddress(pixbuf);
72         linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
73     }
74
75     av_image_copy(vda->tmp_frame->data, vda->tmp_frame->linesize,
76                   (const uint8_t **)data, linesize, vda->tmp_frame->format,
77                   frame->width, frame->height);
78
79     CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
80
81     ret = av_frame_copy_props(vda->tmp_frame, frame);
82
83     if (ret < 0)
84         return ret;
85
86     av_frame_unref(frame);
87     av_frame_move_ref(frame, vda->tmp_frame);
88
89     return 0;
90 }
91
92 static void vda_uninit(AVCodecContext *s)
93 {
94     InputStream *ist = s->opaque;
95     VDAContext  *vda = ist->hwaccel_ctx;
96
97     ist->hwaccel_uninit        = NULL;
98     ist->hwaccel_retrieve_data = NULL;
99
100     av_frame_free(&vda->tmp_frame);
101
102     av_vda_default_free(s);
103     av_freep(&ist->hwaccel_ctx);
104 }
105
106 int vda_init(AVCodecContext *s)
107 {
108     InputStream *ist = s->opaque;
109     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
110     VDAContext *vda;
111     int ret;
112
113     vda = av_mallocz(sizeof(*vda));
114     if (!vda)
115         return AVERROR(ENOMEM);
116
117     ist->hwaccel_ctx           = vda;
118     ist->hwaccel_uninit        = vda_uninit;
119     ist->hwaccel_retrieve_data = vda_retrieve_data;
120
121     vda->tmp_frame = av_frame_alloc();
122     if (!vda->tmp_frame) {
123         ret = AVERROR(ENOMEM);
124         goto fail;
125     }
126
127     ret = av_vda_default_init(s);
128     if (ret < 0) {
129         av_log(NULL, loglevel, "Error creating VDA decoder.\n");
130         goto fail;
131     }
132
133     return 0;
134 fail:
135     vda_uninit(s);
136     return ret;
137 }