blob: 7d4fbf8a379dd06185595a8c498cffdc82d1b65f
1 | /* |
2 | * This file is part of FFmpeg. |
3 | * |
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. |
8 | * |
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. |
13 | * |
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 |
17 | */ |
18 | |
19 | #include <stdint.h> |
20 | |
21 | #include "ffmpeg.h" |
22 | |
23 | #include "libavcodec/vdpau.h" |
24 | |
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" |
30 | |
31 | typedef struct VDPAUContext { |
32 | AVBufferRef *hw_frames_ctx; |
33 | AVFrame *tmp_frame; |
34 | } VDPAUContext; |
35 | |
36 | static 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 | |
45 | av_buffer_unref(&ctx->hw_frames_ctx); |
46 | av_frame_free(&ctx->tmp_frame); |
47 | |
48 | av_freep(&ist->hwaccel_ctx); |
49 | av_freep(&s->hwaccel_context); |
50 | } |
51 | |
52 | static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags) |
53 | { |
54 | InputStream *ist = s->opaque; |
55 | VDPAUContext *ctx = ist->hwaccel_ctx; |
56 | |
57 | return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0); |
58 | } |
59 | |
60 | static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame) |
61 | { |
62 | InputStream *ist = s->opaque; |
63 | VDPAUContext *ctx = ist->hwaccel_ctx; |
64 | int ret; |
65 | |
66 | ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0); |
67 | if (ret < 0) |
68 | return ret; |
69 | |
70 | ret = av_frame_copy_props(ctx->tmp_frame, frame); |
71 | if (ret < 0) { |
72 | av_frame_unref(ctx->tmp_frame); |
73 | return ret; |
74 | } |
75 | |
76 | av_frame_unref(frame); |
77 | av_frame_move_ref(frame, ctx->tmp_frame); |
78 | |
79 | return 0; |
80 | } |
81 | |
82 | static 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; |
86 | VDPAUContext *ctx; |
87 | int ret; |
88 | |
89 | AVBufferRef *device_ref = NULL; |
90 | AVHWDeviceContext *device_ctx; |
91 | AVVDPAUDeviceContext *device_hwctx; |
92 | AVHWFramesContext *frames_ctx; |
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 | |
107 | ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_VDPAU, |
108 | ist->hwaccel_device, NULL, 0); |
109 | if (ret < 0) |
110 | goto fail; |
111 | device_ctx = (AVHWDeviceContext*)device_ref->data; |
112 | device_hwctx = device_ctx->hwctx; |
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; |
124 | |
125 | ret = av_hwframe_ctx_init(ctx->hw_frames_ctx); |
126 | if (ret < 0) |
127 | goto fail; |
128 | |
129 | if (av_vdpau_bind_context(s, device_hwctx->device, device_hwctx->get_proc_address, 0)) |
130 | goto fail; |
131 | |
132 | av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU to decode input stream #%d:%d.\n", |
133 | ist->file_index, ist->st->index); |
134 | |
135 | return 0; |
136 | |
137 | fail: |
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); |
141 | vdpau_uninit(s); |
142 | return AVERROR(EINVAL); |
143 | } |
144 | |
145 | int vdpau_init(AVCodecContext *s) |
146 | { |
147 | InputStream *ist = s->opaque; |
148 | |
149 | if (!ist->hwaccel_ctx) { |
150 | int ret = vdpau_alloc(s); |
151 | if (ret < 0) |
152 | return ret; |
153 | } |
154 | |
155 | ist->hwaccel_get_buffer = vdpau_get_buffer; |
156 | ist->hwaccel_retrieve_data = vdpau_retrieve_data; |
157 | |
158 | return 0; |
159 | } |
160 |