ac3enc: Reshuffle some float/fixed-mode ifdefs to avoid a dummy function
[libav.git] / avconv_vdpau.c
CommitLineData
7671dd7c
AK
1/*
2 * This file is part of Libav.
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include <stdint.h>
20
7671dd7c
AK
21#include "avconv.h"
22
23#include "libavcodec/vdpau.h"
24
7671dd7c
AK
25#include "libavutil/buffer.h"
26#include "libavutil/frame.h"
bd49be88
AK
27#include "libavutil/hwcontext.h"
28#include "libavutil/hwcontext_vdpau.h"
7671dd7c
AK
29#include "libavutil/pixfmt.h"
30
31typedef struct VDPAUContext {
bd49be88 32 AVBufferRef *hw_frames_ctx;
7671dd7c 33 AVFrame *tmp_frame;
7671dd7c
AK
34} VDPAUContext;
35
36static void vdpau_uninit(AVCodecContext *s)
37{
38 InputStream *ist = s->opaque;
39 VDPAUContext *ctx = ist->hwaccel_ctx;
40
41 ist->hwaccel_uninit = NULL;
42 ist->hwaccel_get_buffer = NULL;
43 ist->hwaccel_retrieve_data = NULL;
44
bd49be88 45 av_buffer_unref(&ctx->hw_frames_ctx);
7671dd7c
AK
46 av_frame_free(&ctx->tmp_frame);
47
48 av_freep(&ist->hwaccel_ctx);
49 av_freep(&s->hwaccel_context);
50}
51
7671dd7c
AK
52static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
53{
54 InputStream *ist = s->opaque;
55 VDPAUContext *ctx = ist->hwaccel_ctx;
7671dd7c 56
bd49be88 57 return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
7671dd7c
AK
58}
59
60static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
61{
7671dd7c
AK
62 InputStream *ist = s->opaque;
63 VDPAUContext *ctx = ist->hwaccel_ctx;
bd49be88 64 int ret;
7671dd7c 65
bd49be88 66 ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
7671dd7c
AK
67 if (ret < 0)
68 return ret;
69
7671dd7c 70 ret = av_frame_copy_props(ctx->tmp_frame, frame);
bd49be88
AK
71 if (ret < 0) {
72 av_frame_unref(ctx->tmp_frame);
73 return ret;
74 }
7671dd7c
AK
75
76 av_frame_unref(frame);
77 av_frame_move_ref(frame, ctx->tmp_frame);
7671dd7c 78
bd49be88 79 return 0;
7671dd7c
AK
80}
81
7671dd7c
AK
82static int vdpau_alloc(AVCodecContext *s)
83{
84 InputStream *ist = s->opaque;
85 int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
7671dd7c 86 VDPAUContext *ctx;
bd49be88
AK
87 int ret;
88
bd49be88
AK
89 AVBufferRef *device_ref = NULL;
90 AVHWDeviceContext *device_ctx;
91 AVVDPAUDeviceContext *device_hwctx;
92 AVHWFramesContext *frames_ctx;
7671dd7c
AK
93
94 ctx = av_mallocz(sizeof(*ctx));
95 if (!ctx)
96 return AVERROR(ENOMEM);
97
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;
102
103 ctx->tmp_frame = av_frame_alloc();
104 if (!ctx->tmp_frame)
105 goto fail;
106
f72db3f2
AK
107 ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_VDPAU,
108 ist->hwaccel_device, NULL, 0);
bd49be88
AK
109 if (ret < 0)
110 goto fail;
f72db3f2
AK
111 device_ctx = (AVHWDeviceContext*)device_ref->data;
112 device_hwctx = device_ctx->hwctx;
bd49be88
AK
113
114 ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
115 if (!ctx->hw_frames_ctx)
116 goto fail;
117 av_buffer_unref(&device_ref);
118
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;
7671dd7c 124
bd49be88
AK
125 ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
126 if (ret < 0)
127 goto fail;
128
f72db3f2 129 if (av_vdpau_bind_context(s, device_hwctx->device, device_hwctx->get_proc_address, 0))
7671dd7c 130 goto fail;
7671dd7c 131
f72db3f2
AK
132 av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU to decode input stream #%d:%d.\n",
133 ist->file_index, ist->st->index);
7671dd7c
AK
134
135 return 0;
136
137fail:
138 av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
139 ist->file_index, ist->st->index);
bd49be88 140 av_buffer_unref(&device_ref);
7671dd7c
AK
141 vdpau_uninit(s);
142 return AVERROR(EINVAL);
143}
144
145int vdpau_init(AVCodecContext *s)
146{
147 InputStream *ist = s->opaque;
7671dd7c
AK
148
149 if (!ist->hwaccel_ctx) {
8de1d679 150 int ret = vdpau_alloc(s);
7671dd7c
AK
151 if (ret < 0)
152 return ret;
153 }
7671dd7c
AK
154
155 ist->hwaccel_get_buffer = vdpau_get_buffer;
156 ist->hwaccel_retrieve_data = vdpau_retrieve_data;
157
158 return 0;
159}