2 * This file is part of Libav.
4 * Libav 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.
9 * Libav 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/avassert.h"
22 #include "libavutil/frame.h"
23 #include "libavutil/hwcontext.h"
24 #include "libavutil/log.h"
29 static AVClass vaapi_class = {
30 .class_name = "vaapi",
31 .item_name = av_default_item_name,
32 .version = LIBAVUTIL_VERSION_INT,
35 #define DEFAULT_SURFACES 20
37 typedef struct VAAPIDecoderContext {
40 AVBufferRef *device_ref;
41 AVHWDeviceContext *device;
42 AVBufferRef *frames_ref;
43 AVHWFramesContext *frames;
45 // The output need not have the same format, width and height as the
46 // decoded frames - the copy for non-direct-mapped access is actually
47 // a whole vpp instance which can do arbitrary scaling and format
49 enum AVPixelFormat output_format;
50 } VAAPIDecoderContext;
53 static int vaapi_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
55 InputStream *ist = avctx->opaque;
56 VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
59 err = av_hwframe_get_buffer(ctx->frames_ref, frame, 0);
61 av_log(ctx, AV_LOG_ERROR, "Failed to allocate decoder surface.\n");
63 av_log(ctx, AV_LOG_DEBUG, "Decoder given surface %#x.\n",
64 (unsigned int)(uintptr_t)frame->data[3]);
69 static int vaapi_retrieve_data(AVCodecContext *avctx, AVFrame *input)
71 InputStream *ist = avctx->opaque;
72 VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
76 av_assert0(input->format == AV_PIX_FMT_VAAPI);
78 if (ctx->output_format == AV_PIX_FMT_VAAPI) {
83 av_log(ctx, AV_LOG_DEBUG, "Retrieve data from surface %#x.\n",
84 (unsigned int)(uintptr_t)input->data[3]);
86 output = av_frame_alloc();
88 return AVERROR(ENOMEM);
90 output->format = ctx->output_format;
92 err = av_hwframe_transfer_data(output, input, 0);
94 av_log(ctx, AV_LOG_ERROR, "Failed to transfer data to "
95 "output frame: %d.\n", err);
99 err = av_frame_copy_props(output, input);
101 av_frame_unref(output);
105 av_frame_unref(input);
106 av_frame_move_ref(input, output);
107 av_frame_free(&output);
113 av_frame_free(&output);
117 static void vaapi_decode_uninit(AVCodecContext *avctx)
119 InputStream *ist = avctx->opaque;
120 VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
123 av_buffer_unref(&ctx->frames_ref);
124 av_buffer_unref(&ctx->device_ref);
128 av_buffer_unref(&ist->hw_frames_ctx);
130 ist->hwaccel_ctx = NULL;
131 ist->hwaccel_uninit = NULL;
132 ist->hwaccel_get_buffer = NULL;
133 ist->hwaccel_retrieve_data = NULL;
136 int vaapi_decode_init(AVCodecContext *avctx)
138 InputStream *ist = avctx->opaque;
139 VAAPIDecoderContext *ctx;
141 int loglevel = (ist->hwaccel_id != HWACCEL_VAAPI ? AV_LOG_VERBOSE
144 if (ist->hwaccel_ctx)
145 vaapi_decode_uninit(avctx);
147 // We have -hwaccel without -vaapi_device, so just initialise here with
148 // the device passed as -hwaccel_device (if -vaapi_device was passed, it
149 // will always have been called before now).
150 if (!hw_device_ctx) {
151 err = vaapi_device_init(ist->hwaccel_device);
156 ctx = av_mallocz(sizeof(*ctx));
158 return AVERROR(ENOMEM);
159 ctx->class = &vaapi_class;
161 ctx->device_ref = av_buffer_ref(hw_device_ctx);
162 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
164 ctx->output_format = ist->hwaccel_output_format;
165 avctx->pix_fmt = ctx->output_format;
167 ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
168 if (!ctx->frames_ref) {
169 av_log(ctx, loglevel, "Failed to create VAAPI frame context.\n");
170 err = AVERROR(ENOMEM);
174 ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
176 ctx->frames->format = AV_PIX_FMT_VAAPI;
177 ctx->frames->width = avctx->coded_width;
178 ctx->frames->height = avctx->coded_height;
180 // It would be nice if we could query the available formats here,
181 // but unfortunately we don't have a VAConfigID to do it with.
182 // For now, just assume an NV12 format (or P010 if 10-bit).
183 ctx->frames->sw_format = (avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
184 AV_PIX_FMT_P010 : AV_PIX_FMT_NV12);
186 // For frame-threaded decoding, at least one additional surface
187 // is needed for each thread.
188 ctx->frames->initial_pool_size = DEFAULT_SURFACES;
189 if (avctx->active_thread_type & FF_THREAD_FRAME)
190 ctx->frames->initial_pool_size += avctx->thread_count;
192 err = av_hwframe_ctx_init(ctx->frames_ref);
194 av_log(ctx, loglevel, "Failed to initialise VAAPI frame "
195 "context: %d\n", err);
199 ist->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
200 if (!ist->hw_frames_ctx) {
201 err = AVERROR(ENOMEM);
205 ist->hwaccel_ctx = ctx;
206 ist->hwaccel_uninit = &vaapi_decode_uninit;
207 ist->hwaccel_get_buffer = &vaapi_get_buffer;
208 ist->hwaccel_retrieve_data = &vaapi_retrieve_data;
213 vaapi_decode_uninit(avctx);
217 static AVClass *vaapi_log = &vaapi_class;
219 av_cold int vaapi_device_init(const char *device)
223 err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
226 av_log(&vaapi_log, AV_LOG_ERROR, "Failed to create a VAAPI device\n");