]> git.sesse.net Git - ffmpeg/blob - libavcodec/vda_h264_dec.c
j2kdec: merge JPEG2000_PGOD_CPRL code from jpeg2000
[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 "h264.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 } VDADecoderContext;
60
61 static enum AVPixelFormat get_format(struct AVCodecContext *avctx,
62         const enum AVPixelFormat *fmt)
63 {
64     return AV_PIX_FMT_VDA_VLD;
65 }
66
67 typedef struct {
68     CVPixelBufferRef cv_buffer;
69 } VDABufferContext;
70
71 static void release_buffer(void *opaque, uint8_t *data)
72 {
73     VDABufferContext *context = opaque;
74     CVPixelBufferUnlockBaseAddress(context->cv_buffer, 0);
75     CVPixelBufferRelease(context->cv_buffer);
76     av_free(context);
77 }
78
79 static int get_buffer2(AVCodecContext *avctx, AVFrame *pic, int flag)
80 {
81     VDABufferContext *context = av_mallocz(sizeof(VDABufferContext));
82     AVBufferRef *buffer = av_buffer_create(NULL, 0, release_buffer, context, 0);
83     if (!context || !buffer) {
84         av_free(context);
85         return AVERROR(ENOMEM);
86     }
87
88     pic->buf[0] = buffer;
89     pic->data[0] = (void *)1;
90     return 0;
91 }
92
93 static int vdadec_decode(AVCodecContext *avctx,
94         void *data, int *got_frame, AVPacket *avpkt)
95 {
96     VDADecoderContext *ctx = avctx->priv_data;
97     AVFrame *pic = data;
98     int ret;
99
100     ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
101     if (*got_frame) {
102         AVBufferRef *buffer = pic->buf[0];
103         VDABufferContext *context = av_buffer_get_opaque(buffer);
104         CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
105
106         CVPixelBufferRetain(cv_buffer);
107         CVPixelBufferLockBaseAddress(cv_buffer, 0);
108         context->cv_buffer = cv_buffer;
109         pic->format = ctx->pix_fmt;
110         if (CVPixelBufferIsPlanar(cv_buffer)) {
111             int i, count = CVPixelBufferGetPlaneCount(cv_buffer);
112             av_assert0(count < 4);
113             for (i = 0; i < count; i++) {
114                 pic->data[i] = CVPixelBufferGetBaseAddressOfPlane(cv_buffer, i);
115                 pic->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(cv_buffer, i);
116             }
117         } else {
118             pic->data[0] = CVPixelBufferGetBaseAddress(cv_buffer);
119             pic->linesize[0] = CVPixelBufferGetBytesPerRow(cv_buffer);
120         }
121     }
122     avctx->pix_fmt = ctx->pix_fmt;
123
124     return ret;
125 }
126
127 static av_cold int vdadec_close(AVCodecContext *avctx)
128 {
129     VDADecoderContext *ctx = avctx->priv_data;
130     /* release buffers and decoder */
131     ff_vda_destroy_decoder(&ctx->vda_ctx);
132     /* close H.264 decoder */
133     if (ctx->h264_initialized)
134         ff_h264_decoder.close(avctx);
135     return 0;
136 }
137
138 static av_cold int check_format(AVCodecContext *avctx)
139 {
140     AVCodecParserContext *parser;
141     uint8_t *pout;
142     int psize;
143     int index;
144     H264Context *h;
145     int ret = -1;
146
147     /* init parser & parse file */
148     parser = av_parser_init(avctx->codec->id);
149     if (!parser) {
150         av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 parser.\n");
151         goto final;
152     }
153     parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
154     index = av_parser_parse2(parser, avctx, &pout, &psize, NULL, 0, 0, 0, 0);
155     if (index < 0) {
156         av_log(avctx, AV_LOG_ERROR, "Failed to parse this file.\n");
157         goto release_parser;
158     }
159
160     /* check if support */
161     h = parser->priv_data;
162     switch (h->sps.bit_depth_luma) {
163     case 8:
164         if (!CHROMA444(h) && !CHROMA422(h)) {
165             // only this will H.264 decoder switch to hwaccel
166             ret = 0;
167             break;
168         }
169     default:
170         av_log(avctx, AV_LOG_ERROR, "Unsupported file.\n");
171     }
172
173 release_parser:
174     av_parser_close(parser);
175
176 final:
177     return ret;
178 }
179
180 static av_cold int vdadec_init(AVCodecContext *avctx)
181 {
182     VDADecoderContext *ctx = avctx->priv_data;
183     struct vda_context *vda_ctx = &ctx->vda_ctx;
184     OSStatus status;
185     int ret;
186
187     ctx->h264_initialized = 0;
188
189     /* init pix_fmts of codec */
190     if (!ff_h264_vda_decoder.pix_fmts) {
191         if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber10_7)
192             ff_h264_vda_decoder.pix_fmts = vda_pixfmts_prior_10_7;
193         else
194             ff_h264_vda_decoder.pix_fmts = vda_pixfmts;
195     }
196
197     /* check if VDA supports this file */
198     if (check_format(avctx) < 0)
199         goto failed;
200
201     /* init vda */
202     memset(vda_ctx, 0, sizeof(struct vda_context));
203     vda_ctx->width = avctx->width;
204     vda_ctx->height = avctx->height;
205     vda_ctx->format = 'avc1';
206     vda_ctx->use_sync_decoding = 1;
207     vda_ctx->use_ref_buffer = 1;
208     ctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
209     switch (ctx->pix_fmt) {
210     case AV_PIX_FMT_UYVY422:
211         vda_ctx->cv_pix_fmt_type = '2vuy';
212         break;
213     case AV_PIX_FMT_YUYV422:
214         vda_ctx->cv_pix_fmt_type = 'yuvs';
215         break;
216     case AV_PIX_FMT_NV12:
217         vda_ctx->cv_pix_fmt_type = '420v';
218         break;
219     case AV_PIX_FMT_YUV420P:
220         vda_ctx->cv_pix_fmt_type = 'y420';
221         break;
222     default:
223         av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n", avctx->pix_fmt);
224         goto failed;
225     }
226     status = ff_vda_create_decoder(vda_ctx,
227                                    avctx->extradata, avctx->extradata_size);
228     if (status != kVDADecoderNoErr) {
229         av_log(avctx, AV_LOG_ERROR,
230                 "Failed to init VDA decoder: %d.\n", status);
231         goto failed;
232     }
233     avctx->hwaccel_context = vda_ctx;
234
235     /* changes callback functions */
236     avctx->get_format = get_format;
237     avctx->get_buffer2 = get_buffer2;
238 #if FF_API_GET_BUFFER
239     // force the old get_buffer to be empty
240     avctx->get_buffer = NULL;
241 #endif
242
243     /* init H.264 decoder */
244     ret = ff_h264_decoder.init(avctx);
245     if (ret < 0) {
246         av_log(avctx, AV_LOG_ERROR, "Failed to open H.264 decoder.\n");
247         goto failed;
248     }
249     ctx->h264_initialized = 1;
250
251     return 0;
252
253 failed:
254     vdadec_close(avctx);
255     return -1;
256 }
257
258 static void vdadec_flush(AVCodecContext *avctx)
259 {
260     return ff_h264_decoder.flush(avctx);
261 }
262
263 AVCodec ff_h264_vda_decoder = {
264     .name           = "h264_vda",
265     .type           = AVMEDIA_TYPE_VIDEO,
266     .id             = AV_CODEC_ID_H264,
267     .priv_data_size = sizeof(VDADecoderContext),
268     .init           = vdadec_init,
269     .close          = vdadec_close,
270     .decode         = vdadec_decode,
271     .capabilities   = CODEC_CAP_DELAY,
272     .flush          = vdadec_flush,
273     .long_name      = NULL_IF_CONFIG_SMALL("H.264 (VDA acceleration)"),
274 };