]> git.sesse.net Git - ffmpeg/blob - libavcodec/vda_h264_dec.c
Merge commit '4fb311c804098d78e5ce5f527f9a9c37536d3a08'
[ffmpeg] / libavcodec / vda_h264_dec.c
1 /*
2  * Copyright (c) 2012, Xidorn Quan
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * H.264 decoder via VDA
24  * @author Xidorn Quan <quanxunzhen@gmail.com>
25  */
26
27 #include <string.h>
28 #include <CoreFoundation/CoreFoundation.h>
29
30 #include "vda.h"
31 #include "h264dec.h"
32 #include "avcodec.h"
33
34 #ifndef kCFCoreFoundationVersionNumber10_7
35 #define kCFCoreFoundationVersionNumber10_7      635.00
36 #endif
37
38 extern AVCodec ff_h264_decoder, ff_h264_vda_decoder;
39
40 static const enum AVPixelFormat vda_pixfmts_prior_10_7[] = {
41     AV_PIX_FMT_UYVY422,
42     AV_PIX_FMT_YUV420P,
43     AV_PIX_FMT_NONE
44 };
45
46 static const enum AVPixelFormat vda_pixfmts[] = {
47     AV_PIX_FMT_UYVY422,
48     AV_PIX_FMT_YUYV422,
49     AV_PIX_FMT_NV12,
50     AV_PIX_FMT_YUV420P,
51     AV_PIX_FMT_NONE
52 };
53
54 typedef struct {
55     H264Context h264ctx;
56     int h264_initialized;
57     struct vda_context vda_ctx;
58     enum AVPixelFormat pix_fmt;
59
60     /* for backing-up fields set by user.
61      * we have to gain full control of such fields here */
62     void *hwaccel_context;
63     enum AVPixelFormat (*get_format)(struct AVCodecContext *s, const enum AVPixelFormat * fmt);
64     int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags);
65 } VDADecoderContext;
66
67 static enum AVPixelFormat get_format(struct AVCodecContext *avctx,
68         const enum AVPixelFormat *fmt)
69 {
70     return AV_PIX_FMT_VDA_VLD;
71 }
72
73 typedef struct {
74     CVPixelBufferRef cv_buffer;
75 } VDABufferContext;
76
77 static void release_buffer(void *opaque, uint8_t *data)
78 {
79     VDABufferContext *context = opaque;
80     CVPixelBufferUnlockBaseAddress(context->cv_buffer, 0);
81     CVPixelBufferRelease(context->cv_buffer);
82     av_free(context);
83 }
84
85 static int get_buffer2(AVCodecContext *avctx, AVFrame *pic, int flag)
86 {
87     VDABufferContext *context = av_mallocz(sizeof(VDABufferContext));
88     AVBufferRef *buffer = av_buffer_create(NULL, 0, release_buffer, context, 0);
89     if (!context || !buffer) {
90         av_free(context);
91         return AVERROR(ENOMEM);
92     }
93
94     pic->buf[0] = buffer;
95     pic->data[0] = (void *)1;
96     return 0;
97 }
98
99 static inline void set_context(AVCodecContext *avctx)
100 {
101     VDADecoderContext *ctx = avctx->priv_data;
102     ctx->hwaccel_context = avctx->hwaccel_context;
103     avctx->hwaccel_context = &ctx->vda_ctx;
104     ctx->get_format = avctx->get_format;
105     avctx->get_format = get_format;
106     ctx->get_buffer2 = avctx->get_buffer2;
107     avctx->get_buffer2 = get_buffer2;
108 }
109
110 static inline void restore_context(AVCodecContext *avctx)
111 {
112     VDADecoderContext *ctx = avctx->priv_data;
113     avctx->hwaccel_context = ctx->hwaccel_context;
114     avctx->get_format = ctx->get_format;
115     avctx->get_buffer2 = ctx->get_buffer2;
116 }
117
118 static int vdadec_decode(AVCodecContext *avctx,
119         void *data, int *got_frame, AVPacket *avpkt)
120 {
121     VDADecoderContext *ctx = avctx->priv_data;
122     AVFrame *pic = data;
123     int ret;
124
125     set_context(avctx);
126     ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
127     restore_context(avctx);
128     if (*got_frame) {
129         AVBufferRef *buffer = pic->buf[0];
130         VDABufferContext *context = av_buffer_get_opaque(buffer);
131         CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
132
133         CVPixelBufferRetain(cv_buffer);
134         CVPixelBufferLockBaseAddress(cv_buffer, 0);
135         context->cv_buffer = cv_buffer;
136         pic->format = ctx->pix_fmt;
137         if (CVPixelBufferIsPlanar(cv_buffer)) {
138             int i, count = CVPixelBufferGetPlaneCount(cv_buffer);
139             av_assert0(count < 4);
140             for (i = 0; i < count; i++) {
141                 pic->data[i] = CVPixelBufferGetBaseAddressOfPlane(cv_buffer, i);
142                 pic->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(cv_buffer, i);
143             }
144         } else {
145             pic->data[0] = CVPixelBufferGetBaseAddress(cv_buffer);
146             pic->linesize[0] = CVPixelBufferGetBytesPerRow(cv_buffer);
147         }
148     }
149     avctx->pix_fmt = ctx->pix_fmt;
150
151     return ret;
152 }
153
154 static av_cold int vdadec_close(AVCodecContext *avctx)
155 {
156     VDADecoderContext *ctx = avctx->priv_data;
157     /* release buffers and decoder */
158     ff_vda_destroy_decoder(&ctx->vda_ctx);
159     /* close H.264 decoder */
160     if (ctx->h264_initialized) {
161         set_context(avctx);
162         ff_h264_decoder.close(avctx);
163         restore_context(avctx);
164     }
165     return 0;
166 }
167
168 static av_cold int vdadec_init(AVCodecContext *avctx)
169 {
170     VDADecoderContext *ctx = avctx->priv_data;
171     struct vda_context *vda_ctx = &ctx->vda_ctx;
172     OSStatus status;
173     int ret, i;
174
175     ctx->h264_initialized = 0;
176
177     /* init pix_fmts of codec */
178     if (!ff_h264_vda_decoder.pix_fmts) {
179         if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber10_7)
180             ff_h264_vda_decoder.pix_fmts = vda_pixfmts_prior_10_7;
181         else
182             ff_h264_vda_decoder.pix_fmts = vda_pixfmts;
183     }
184
185     /* init vda */
186     memset(vda_ctx, 0, sizeof(struct vda_context));
187     vda_ctx->width = avctx->width;
188     vda_ctx->height = avctx->height;
189     vda_ctx->format = 'avc1';
190     vda_ctx->use_sync_decoding = 1;
191     vda_ctx->use_ref_buffer = 1;
192     ctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
193     switch (ctx->pix_fmt) {
194     case AV_PIX_FMT_UYVY422:
195         vda_ctx->cv_pix_fmt_type = '2vuy';
196         break;
197     case AV_PIX_FMT_YUYV422:
198         vda_ctx->cv_pix_fmt_type = 'yuvs';
199         break;
200     case AV_PIX_FMT_NV12:
201         vda_ctx->cv_pix_fmt_type = '420v';
202         break;
203     case AV_PIX_FMT_YUV420P:
204         vda_ctx->cv_pix_fmt_type = 'y420';
205         break;
206     default:
207         av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n", avctx->pix_fmt);
208         goto failed;
209     }
210     status = ff_vda_create_decoder(vda_ctx,
211                                    avctx->extradata, avctx->extradata_size);
212     if (status != kVDADecoderNoErr) {
213         av_log(avctx, AV_LOG_ERROR,
214                 "Failed to init VDA decoder: %d.\n", status);
215         goto failed;
216     }
217
218     /* init H.264 decoder */
219     set_context(avctx);
220     ret = ff_h264_decoder.init(avctx);
221     restore_context(avctx);
222     if (ret < 0) {
223         av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 decoder.\n");
224         goto failed;
225     }
226     ctx->h264_initialized = 1;
227
228     for (i = 0; i < MAX_SPS_COUNT; i++) {
229         const SPS *sps = ctx->h264ctx.ps.sps_list[i] ? (const SPS*)ctx->h264ctx.ps.sps_list[i]->data : NULL;
230         if (sps && (sps->bit_depth_luma != 8 ||
231                 sps->chroma_format_idc == 2 ||
232                 sps->chroma_format_idc == 3)) {
233             av_log(avctx, AV_LOG_ERROR, "Format is not supported.\n");
234             goto failed;
235         }
236     }
237
238     return 0;
239
240 failed:
241     vdadec_close(avctx);
242     return -1;
243 }
244
245 static void vdadec_flush(AVCodecContext *avctx)
246 {
247     set_context(avctx);
248     ff_h264_decoder.flush(avctx);
249     restore_context(avctx);
250 }
251
252 AVCodec ff_h264_vda_decoder = {
253     .name           = "h264_vda",
254     .type           = AVMEDIA_TYPE_VIDEO,
255     .id             = AV_CODEC_ID_H264,
256     .priv_data_size = sizeof(VDADecoderContext),
257     .init           = vdadec_init,
258     .close          = vdadec_close,
259     .decode         = vdadec_decode,
260     .capabilities   = AV_CODEC_CAP_DELAY,
261     .flush          = vdadec_flush,
262     .long_name      = NULL_IF_CONFIG_SMALL("H.264 (VDA acceleration)"),
263 };