2 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavcodec/vdpau.h"
25 #include "libavutil/buffer.h"
26 #include "libavutil/frame.h"
27 #include "libavutil/hwcontext.h"
28 #include "libavutil/hwcontext_vdpau.h"
29 #include "libavutil/pixfmt.h"
31 typedef struct VDPAUContext {
32 AVBufferRef *hw_frames_ctx;
36 static void vdpau_uninit(AVCodecContext *s)
38 InputStream *ist = s->opaque;
39 VDPAUContext *ctx = ist->hwaccel_ctx;
41 ist->hwaccel_uninit = NULL;
42 ist->hwaccel_get_buffer = NULL;
43 ist->hwaccel_retrieve_data = NULL;
45 av_buffer_unref(&ctx->hw_frames_ctx);
46 av_frame_free(&ctx->tmp_frame);
48 av_freep(&ist->hwaccel_ctx);
49 av_freep(&s->hwaccel_context);
52 static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
54 InputStream *ist = s->opaque;
55 VDPAUContext *ctx = ist->hwaccel_ctx;
57 return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
60 static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
62 InputStream *ist = s->opaque;
63 VDPAUContext *ctx = ist->hwaccel_ctx;
66 ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
70 ret = av_frame_copy_props(ctx->tmp_frame, frame);
72 av_frame_unref(ctx->tmp_frame);
76 av_frame_unref(frame);
77 av_frame_move_ref(frame, ctx->tmp_frame);
82 static int vdpau_alloc(AVCodecContext *s)
84 InputStream *ist = s->opaque;
85 int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
89 AVBufferRef *device_ref = NULL;
90 AVHWDeviceContext *device_ctx;
91 AVVDPAUDeviceContext *device_hwctx;
92 AVHWFramesContext *frames_ctx;
94 ctx = av_mallocz(sizeof(*ctx));
96 return AVERROR(ENOMEM);
98 ist->hwaccel_ctx = ctx;
99 ist->hwaccel_uninit = vdpau_uninit;
100 ist->hwaccel_get_buffer = vdpau_get_buffer;
101 ist->hwaccel_retrieve_data = vdpau_retrieve_data;
103 ctx->tmp_frame = av_frame_alloc();
107 ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_VDPAU,
108 ist->hwaccel_device, NULL, 0);
111 device_ctx = (AVHWDeviceContext*)device_ref->data;
112 device_hwctx = device_ctx->hwctx;
114 ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
115 if (!ctx->hw_frames_ctx)
117 av_buffer_unref(&device_ref);
119 frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
120 frames_ctx->format = AV_PIX_FMT_VDPAU;
121 frames_ctx->sw_format = s->sw_pix_fmt;
122 frames_ctx->width = s->coded_width;
123 frames_ctx->height = s->coded_height;
125 ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
129 if (av_vdpau_bind_context(s, device_hwctx->device, device_hwctx->get_proc_address, 0))
132 av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU to decode input stream #%d:%d.\n",
133 ist->file_index, ist->st->index);
138 av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
139 ist->file_index, ist->st->index);
140 av_buffer_unref(&device_ref);
142 return AVERROR(EINVAL);
145 int vdpau_init(AVCodecContext *s)
147 InputStream *ist = s->opaque;
149 if (!ist->hwaccel_ctx) {
150 int ret = vdpau_alloc(s);
155 ist->hwaccel_get_buffer = vdpau_get_buffer;
156 ist->hwaccel_retrieve_data = vdpau_retrieve_data;