* commit '35177ba77ff60a8b8839783f57e44bcc4214507a':
avconv: add support for DXVA2 decoding
Conflicts:
Makefile
configure
Merged-by: Michael Niedermayer <michaelni@gmx.at>
... | ... |
@@ -30,6 +30,8 @@ $(foreach prog,$(AVBASENAMES),$(eval OBJS-$(prog)-$(CONFIG_OPENCL) += cmdutils_o |
30 | 30 |
|
31 | 31 |
OBJS-ffmpeg += ffmpeg_opt.o ffmpeg_filter.o |
32 | 32 |
OBJS-ffmpeg-$(HAVE_VDPAU_X11) += ffmpeg_vdpau.o |
33 |
+OBJS-ffmpeg-$(CONFIG_DXVA2) += ffmpeg_dxva2.o |
|
34 |
+ |
|
33 | 35 |
TESTTOOLS = audiogen videogen rotozoom tiny_psnr tiny_ssim base64 |
34 | 36 |
HOSTPROGS := $(TESTTOOLS:%=tests/%) doc/print_options |
35 | 37 |
TOOLS = qt-faststart trasher uncoded_frame |
... | ... |
@@ -4800,6 +4800,10 @@ enabled vdpau && enabled xlib && |
4800 | 4800 |
prepend ffmpeg_libs $($ldflags_filter "-lvdpau") && |
4801 | 4801 |
enable vdpau_x11 |
4802 | 4802 |
|
4803 |
+enabled dxva2 && |
|
4804 |
+ check_header dxva2api.h -D_WIN32_WINNT=0x0600 && |
|
4805 |
+ prepend ffmpeg_libs $($ldflags_filter "-lole32") |
|
4806 |
+ |
|
4803 | 4807 |
# Funny iconv installations are not unusual, so check it after all flags have been set |
4804 | 4808 |
disabled iconv || check_func_headers iconv.h iconv || check_lib2 iconv.h iconv -liconv || disable iconv |
4805 | 4809 |
|
... | ... |
@@ -645,6 +645,9 @@ Automatically select the hardware acceleration method. |
645 | 645 |
|
646 | 646 |
@item vdpau |
647 | 647 |
Use VDPAU (Video Decode and Presentation API for Unix) hardware acceleration. |
648 |
+ |
|
649 |
+@item dxva2 |
|
650 |
+Use DXVA2 (DirectX Video Acceleration) hardware acceleration. |
|
648 | 651 |
@end table |
649 | 652 |
|
650 | 653 |
This option has no effect if the selected hwaccel is not available or not |
... | ... |
@@ -667,6 +670,10 @@ method chosen. |
667 | 667 |
@item vdpau |
668 | 668 |
For VDPAU, this option specifies the X11 display/screen to use. If this option |
669 | 669 |
is not specified, the value of the @var{DISPLAY} environment variable is used |
670 |
+ |
|
671 |
+@item dxva2 |
|
672 |
+For DXVA2, this option should contain the number of the display adapter to use. |
|
673 |
+If this option is not specified, the default adapter is used. |
|
670 | 674 |
@end table |
671 | 675 |
@end table |
672 | 676 |
|
... | ... |
@@ -60,6 +60,7 @@ enum HWAccelID { |
60 | 60 |
HWACCEL_NONE = 0, |
61 | 61 |
HWACCEL_AUTO, |
62 | 62 |
HWACCEL_VDPAU, |
63 |
+ HWACCEL_DXVA2, |
|
63 | 64 |
}; |
64 | 65 |
|
65 | 66 |
typedef struct HWAccel { |
... | ... |
@@ -512,5 +513,6 @@ FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost); |
512 | 512 |
int ffmpeg_parse_options(int argc, char **argv); |
513 | 513 |
|
514 | 514 |
int vdpau_init(AVCodecContext *s); |
515 |
+int dxva2_init(AVCodecContext *s); |
|
515 | 516 |
|
516 | 517 |
#endif /* FFMPEG_H */ |
517 | 518 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,618 @@ |
0 |
+/* |
|
1 |
+ * This file is part of FFmpeg. |
|
2 |
+ * |
|
3 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
4 |
+ * modify it under the terms of the GNU Lesser General Public |
|
5 |
+ * License as published by the Free Software Foundation; either |
|
6 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
7 |
+ * |
|
8 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
9 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
11 |
+ * Lesser General Public License for more details. |
|
12 |
+ * |
|
13 |
+ * You should have received a copy of the GNU Lesser General Public |
|
14 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
15 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 |
+ */ |
|
17 |
+ |
|
18 |
+#include <windows.h> |
|
19 |
+ |
|
20 |
+#ifdef _WIN32_WINNT |
|
21 |
+#undef _WIN32_WINNT |
|
22 |
+#endif |
|
23 |
+#define _WIN32_WINNT 0x0600 |
|
24 |
+#define DXVA2API_USE_BITFIELDS |
|
25 |
+#define COBJMACROS |
|
26 |
+ |
|
27 |
+#include <stdint.h> |
|
28 |
+ |
|
29 |
+/* initguid.h needs to be above d3d/dxva to ensure |
|
30 |
+ the GUIDs are initialized properly */ |
|
31 |
+#include <initguid.h> |
|
32 |
+ |
|
33 |
+#include <d3d9.h> |
|
34 |
+#include <dxva2api.h> |
|
35 |
+ |
|
36 |
+#include "ffmpeg.h" |
|
37 |
+ |
|
38 |
+#include "libavcodec/dxva2.h" |
|
39 |
+ |
|
40 |
+#include "libavutil/avassert.h" |
|
41 |
+#include "libavutil/buffer.h" |
|
42 |
+#include "libavutil/frame.h" |
|
43 |
+#include "libavutil/imgutils.h" |
|
44 |
+#include "libavutil/pixfmt.h" |
|
45 |
+ |
|
46 |
+typedef IDirect3D9* WINAPI pDirect3DCreate9(UINT); |
|
47 |
+typedef HRESULT WINAPI pCreateDeviceManager9(UINT *, IDirect3DDeviceManager9 **); |
|
48 |
+ |
|
49 |
+/* GUIDs not defined in the common dxva2api.h in mingw-w64 */ |
|
50 |
+#ifndef _MSC_VER |
|
51 |
+DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD, 0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60); |
|
52 |
+DEFINE_GUID(DXVA2_ModeVC1_D2010, 0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5); |
|
53 |
+DEFINE_GUID(DXVA2_NoEncrypt, 0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5); |
|
54 |
+#endif |
|
55 |
+ |
|
56 |
+DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6); |
|
57 |
+DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00); |
|
58 |
+ |
|
59 |
+typedef struct dxva2_mode { |
|
60 |
+ const GUID *guid; |
|
61 |
+ enum AVCodecID codec; |
|
62 |
+} dxva2_mode; |
|
63 |
+ |
|
64 |
+static const dxva2_mode dxva2_modes[] = { |
|
65 |
+ /* MPEG-2 */ |
|
66 |
+ { &DXVA2_ModeMPEG2_VLD, AV_CODEC_ID_MPEG2VIDEO }, |
|
67 |
+ { &DXVA2_ModeMPEG2and1_VLD, AV_CODEC_ID_MPEG2VIDEO }, |
|
68 |
+ |
|
69 |
+ /* H.264 */ |
|
70 |
+ { &DXVA2_ModeH264_F, AV_CODEC_ID_H264 }, |
|
71 |
+ { &DXVA2_ModeH264_E, AV_CODEC_ID_H264 }, |
|
72 |
+ /* Intel specific H.264 mode */ |
|
73 |
+ { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 }, |
|
74 |
+ |
|
75 |
+ /* VC-1 / WMV3 */ |
|
76 |
+ { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_VC1 }, |
|
77 |
+ { &DXVA2_ModeVC1_D2010, AV_CODEC_ID_WMV3 }, |
|
78 |
+ { &DXVA2_ModeVC1_D, AV_CODEC_ID_VC1 }, |
|
79 |
+ { &DXVA2_ModeVC1_D, AV_CODEC_ID_WMV3 }, |
|
80 |
+ |
|
81 |
+ { NULL, 0 }, |
|
82 |
+}; |
|
83 |
+ |
|
84 |
+typedef struct surface_info { |
|
85 |
+ int used; |
|
86 |
+ uint64_t age; |
|
87 |
+} surface_info; |
|
88 |
+ |
|
89 |
+typedef struct DXVA2Context { |
|
90 |
+ HMODULE d3dlib; |
|
91 |
+ HMODULE dxva2lib; |
|
92 |
+ |
|
93 |
+ HANDLE deviceHandle; |
|
94 |
+ |
|
95 |
+ IDirect3D9 *d3d9; |
|
96 |
+ IDirect3DDevice9 *d3d9device; |
|
97 |
+ IDirect3DDeviceManager9 *d3d9devmgr; |
|
98 |
+ IDirectXVideoDecoderService *decoder_service; |
|
99 |
+ IDirectXVideoDecoder *decoder; |
|
100 |
+ |
|
101 |
+ GUID decoder_guid; |
|
102 |
+ DXVA2_ConfigPictureDecode decoder_config; |
|
103 |
+ |
|
104 |
+ LPDIRECT3DSURFACE9 *surfaces; |
|
105 |
+ surface_info *surface_infos; |
|
106 |
+ uint32_t num_surfaces; |
|
107 |
+ uint64_t surface_age; |
|
108 |
+ |
|
109 |
+ AVFrame *tmp_frame; |
|
110 |
+} DXVA2Context; |
|
111 |
+ |
|
112 |
+typedef struct DXVA2SurfaceWrapper { |
|
113 |
+ DXVA2Context *ctx; |
|
114 |
+ LPDIRECT3DSURFACE9 surface; |
|
115 |
+ IDirectXVideoDecoder *decoder; |
|
116 |
+} DXVA2SurfaceWrapper; |
|
117 |
+ |
|
118 |
+static void dxva2_destroy_decoder(AVCodecContext *s) |
|
119 |
+{ |
|
120 |
+ InputStream *ist = s->opaque; |
|
121 |
+ DXVA2Context *ctx = ist->hwaccel_ctx; |
|
122 |
+ |
|
123 |
+ if (ctx->surfaces) { |
|
124 |
+ for (int i = 0; i < ctx->num_surfaces; i++) { |
|
125 |
+ if (ctx->surfaces[i]) |
|
126 |
+ IDirect3DSurface9_Release(ctx->surfaces[i]); |
|
127 |
+ } |
|
128 |
+ } |
|
129 |
+ av_freep(&ctx->surfaces); |
|
130 |
+ av_freep(&ctx->surface_infos); |
|
131 |
+ ctx->num_surfaces = 0; |
|
132 |
+ ctx->surface_age = 0; |
|
133 |
+ |
|
134 |
+ if (ctx->decoder) { |
|
135 |
+ IDirectXVideoDecoder_Release(ctx->decoder); |
|
136 |
+ ctx->decoder = NULL; |
|
137 |
+ } |
|
138 |
+} |
|
139 |
+ |
|
140 |
+static void dxva2_uninit(AVCodecContext *s) |
|
141 |
+{ |
|
142 |
+ InputStream *ist = s->opaque; |
|
143 |
+ DXVA2Context *ctx = ist->hwaccel_ctx; |
|
144 |
+ |
|
145 |
+ ist->hwaccel_uninit = NULL; |
|
146 |
+ ist->hwaccel_get_buffer = NULL; |
|
147 |
+ ist->hwaccel_retrieve_data = NULL; |
|
148 |
+ |
|
149 |
+ if (ctx->decoder) |
|
150 |
+ dxva2_destroy_decoder(s); |
|
151 |
+ |
|
152 |
+ if (ctx->decoder_service) |
|
153 |
+ IDirectXVideoDecoderService_Release(ctx->decoder_service); |
|
154 |
+ |
|
155 |
+ if (ctx->d3d9devmgr && ctx->deviceHandle != INVALID_HANDLE_VALUE) |
|
156 |
+ IDirect3DDeviceManager9_CloseDeviceHandle(ctx->d3d9devmgr, ctx->deviceHandle); |
|
157 |
+ |
|
158 |
+ if (ctx->d3d9devmgr) |
|
159 |
+ IDirect3DDeviceManager9_Release(ctx->d3d9devmgr); |
|
160 |
+ |
|
161 |
+ if (ctx->d3d9device) |
|
162 |
+ IDirect3DDevice9_Release(ctx->d3d9device); |
|
163 |
+ |
|
164 |
+ if (ctx->d3d9) |
|
165 |
+ IDirect3D9_Release(ctx->d3d9); |
|
166 |
+ |
|
167 |
+ if (ctx->d3dlib) |
|
168 |
+ FreeLibrary(ctx->d3dlib); |
|
169 |
+ |
|
170 |
+ if (ctx->dxva2lib) |
|
171 |
+ FreeLibrary(ctx->dxva2lib); |
|
172 |
+ |
|
173 |
+ av_frame_free(&ctx->tmp_frame); |
|
174 |
+ |
|
175 |
+ av_freep(&ist->hwaccel_ctx); |
|
176 |
+ av_freep(&s->hwaccel_context); |
|
177 |
+} |
|
178 |
+ |
|
179 |
+static void dxva2_release_buffer(void *opaque, uint8_t *data) |
|
180 |
+{ |
|
181 |
+ DXVA2SurfaceWrapper *w = opaque; |
|
182 |
+ DXVA2Context *ctx = w->ctx; |
|
183 |
+ int i; |
|
184 |
+ |
|
185 |
+ for (i = 0; i < ctx->num_surfaces; i++) { |
|
186 |
+ if (ctx->surfaces[i] == w->surface) { |
|
187 |
+ ctx->surface_infos[i].used = 0; |
|
188 |
+ break; |
|
189 |
+ } |
|
190 |
+ } |
|
191 |
+ IDirect3DSurface9_Release(w->surface); |
|
192 |
+ IDirectXVideoDecoder_Release(w->decoder); |
|
193 |
+ av_free(w); |
|
194 |
+} |
|
195 |
+ |
|
196 |
+static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags) |
|
197 |
+{ |
|
198 |
+ InputStream *ist = s->opaque; |
|
199 |
+ DXVA2Context *ctx = ist->hwaccel_ctx; |
|
200 |
+ int i, old_unused = -1; |
|
201 |
+ LPDIRECT3DSURFACE9 surface; |
|
202 |
+ DXVA2SurfaceWrapper *w = NULL; |
|
203 |
+ |
|
204 |
+ av_assert0(frame->format == AV_PIX_FMT_DXVA2_VLD); |
|
205 |
+ |
|
206 |
+ for (i = 0; i < ctx->num_surfaces; i++) { |
|
207 |
+ surface_info *info = &ctx->surface_infos[i]; |
|
208 |
+ if (!info->used && (old_unused == -1 || info->age < ctx->surface_infos[old_unused].age)) |
|
209 |
+ old_unused = i; |
|
210 |
+ } |
|
211 |
+ if (old_unused == -1) { |
|
212 |
+ av_log(NULL, AV_LOG_ERROR, "No free DXVA2 surface!\n"); |
|
213 |
+ return AVERROR(ENOMEM); |
|
214 |
+ } |
|
215 |
+ i = old_unused; |
|
216 |
+ |
|
217 |
+ surface = ctx->surfaces[i]; |
|
218 |
+ |
|
219 |
+ w = av_mallocz(sizeof(*w)); |
|
220 |
+ if (!w) |
|
221 |
+ return AVERROR(ENOMEM); |
|
222 |
+ |
|
223 |
+ frame->buf[0] = av_buffer_create((uint8_t*)surface, 0, |
|
224 |
+ dxva2_release_buffer, w, |
|
225 |
+ AV_BUFFER_FLAG_READONLY); |
|
226 |
+ if (!frame->buf[0]) { |
|
227 |
+ av_free(w); |
|
228 |
+ return AVERROR(ENOMEM); |
|
229 |
+ } |
|
230 |
+ |
|
231 |
+ w->ctx = ctx; |
|
232 |
+ w->surface = surface; |
|
233 |
+ IDirect3DSurface9_AddRef(w->surface); |
|
234 |
+ w->decoder = ctx->decoder; |
|
235 |
+ IDirectXVideoDecoder_AddRef(w->decoder); |
|
236 |
+ |
|
237 |
+ ctx->surface_infos[i].used = 1; |
|
238 |
+ ctx->surface_infos[i].age = ctx->surface_age++; |
|
239 |
+ |
|
240 |
+ frame->data[3] = (uint8_t *)surface; |
|
241 |
+ |
|
242 |
+ return 0; |
|
243 |
+} |
|
244 |
+ |
|
245 |
+static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame) |
|
246 |
+{ |
|
247 |
+ LPDIRECT3DSURFACE9 surface = (LPDIRECT3DSURFACE9)frame->data[3]; |
|
248 |
+ InputStream *ist = s->opaque; |
|
249 |
+ DXVA2Context *ctx = ist->hwaccel_ctx; |
|
250 |
+ D3DSURFACE_DESC surfaceDesc; |
|
251 |
+ D3DLOCKED_RECT LockedRect; |
|
252 |
+ HRESULT hr; |
|
253 |
+ int ret; |
|
254 |
+ |
|
255 |
+ IDirect3DSurface9_GetDesc(surface, &surfaceDesc); |
|
256 |
+ |
|
257 |
+ ctx->tmp_frame->width = frame->width; |
|
258 |
+ ctx->tmp_frame->height = frame->height; |
|
259 |
+ ctx->tmp_frame->format = AV_PIX_FMT_NV12; |
|
260 |
+ |
|
261 |
+ ret = av_frame_get_buffer(ctx->tmp_frame, 32); |
|
262 |
+ if (ret < 0) |
|
263 |
+ return ret; |
|
264 |
+ |
|
265 |
+ hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, D3DLOCK_READONLY); |
|
266 |
+ if (FAILED(hr)) { |
|
267 |
+ av_log(NULL, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n"); |
|
268 |
+ return AVERROR_UNKNOWN; |
|
269 |
+ } |
|
270 |
+ |
|
271 |
+ av_image_copy_plane(ctx->tmp_frame->data[0], ctx->tmp_frame->linesize[0], |
|
272 |
+ (uint8_t*)LockedRect.pBits, |
|
273 |
+ LockedRect.Pitch, frame->width, frame->height); |
|
274 |
+ |
|
275 |
+ av_image_copy_plane(ctx->tmp_frame->data[1], ctx->tmp_frame->linesize[1], |
|
276 |
+ (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height, |
|
277 |
+ LockedRect.Pitch, frame->width, frame->height / 2); |
|
278 |
+ |
|
279 |
+ IDirect3DSurface9_UnlockRect(surface); |
|
280 |
+ |
|
281 |
+ ret = av_frame_copy_props(ctx->tmp_frame, frame); |
|
282 |
+ if (ret < 0) |
|
283 |
+ goto fail; |
|
284 |
+ |
|
285 |
+ av_frame_unref(frame); |
|
286 |
+ av_frame_move_ref(frame, ctx->tmp_frame); |
|
287 |
+ |
|
288 |
+ return 0; |
|
289 |
+fail: |
|
290 |
+ av_frame_unref(ctx->tmp_frame); |
|
291 |
+ return ret; |
|
292 |
+} |
|
293 |
+ |
|
294 |
+static int dxva2_alloc(AVCodecContext *s) |
|
295 |
+{ |
|
296 |
+ InputStream *ist = s->opaque; |
|
297 |
+ int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; |
|
298 |
+ DXVA2Context *ctx; |
|
299 |
+ pDirect3DCreate9 *createD3D = NULL; |
|
300 |
+ pCreateDeviceManager9 *createDeviceManager = NULL; |
|
301 |
+ HRESULT hr; |
|
302 |
+ D3DPRESENT_PARAMETERS d3dpp = {0}; |
|
303 |
+ D3DDISPLAYMODE d3ddm; |
|
304 |
+ unsigned resetToken = 0; |
|
305 |
+ UINT adapter = D3DADAPTER_DEFAULT; |
|
306 |
+ |
|
307 |
+ ctx = av_mallocz(sizeof(*ctx)); |
|
308 |
+ if (!ctx) |
|
309 |
+ return AVERROR(ENOMEM); |
|
310 |
+ |
|
311 |
+ ctx->deviceHandle = INVALID_HANDLE_VALUE; |
|
312 |
+ |
|
313 |
+ ist->hwaccel_ctx = ctx; |
|
314 |
+ ist->hwaccel_uninit = dxva2_uninit; |
|
315 |
+ ist->hwaccel_get_buffer = dxva2_get_buffer; |
|
316 |
+ ist->hwaccel_retrieve_data = dxva2_retrieve_data; |
|
317 |
+ |
|
318 |
+ ctx->d3dlib = LoadLibrary("d3d9.dll"); |
|
319 |
+ if (!ctx->d3dlib) { |
|
320 |
+ av_log(NULL, loglevel, "Failed to load D3D9 library\n"); |
|
321 |
+ goto fail; |
|
322 |
+ } |
|
323 |
+ ctx->dxva2lib = LoadLibrary("dxva2.dll"); |
|
324 |
+ if (!ctx->dxva2lib) { |
|
325 |
+ av_log(NULL, loglevel, "Failed to load DXVA2 library\n"); |
|
326 |
+ goto fail; |
|
327 |
+ } |
|
328 |
+ |
|
329 |
+ createD3D = (pDirect3DCreate9 *)GetProcAddress(ctx->d3dlib, "Direct3DCreate9"); |
|
330 |
+ if (!createD3D) { |
|
331 |
+ av_log(NULL, loglevel, "Failed to locate Direct3DCreate9\n"); |
|
332 |
+ goto fail; |
|
333 |
+ } |
|
334 |
+ createDeviceManager = (pCreateDeviceManager9 *)GetProcAddress(ctx->dxva2lib, "DXVA2CreateDirect3DDeviceManager9"); |
|
335 |
+ if (!createDeviceManager) { |
|
336 |
+ av_log(NULL, loglevel, "Failed to locate DXVA2CreateDirect3DDeviceManager9\n"); |
|
337 |
+ goto fail; |
|
338 |
+ } |
|
339 |
+ |
|
340 |
+ ctx->d3d9 = createD3D(D3D_SDK_VERSION); |
|
341 |
+ if (!ctx->d3d9) { |
|
342 |
+ av_log(NULL, loglevel, "Failed to create IDirect3D object\n"); |
|
343 |
+ goto fail; |
|
344 |
+ } |
|
345 |
+ |
|
346 |
+ if (ist->hwaccel_device) { |
|
347 |
+ adapter = atoi(ist->hwaccel_device); |
|
348 |
+ av_log(NULL, AV_LOG_INFO, "Using HWAccel device %d\n", adapter); |
|
349 |
+ } |
|
350 |
+ |
|
351 |
+ IDirect3D9_GetAdapterDisplayMode(ctx->d3d9, adapter, &d3ddm); |
|
352 |
+ d3dpp.Windowed = TRUE; |
|
353 |
+ d3dpp.BackBufferWidth = 640; |
|
354 |
+ d3dpp.BackBufferHeight = 480; |
|
355 |
+ d3dpp.BackBufferCount = 0; |
|
356 |
+ d3dpp.BackBufferFormat = d3ddm.Format; |
|
357 |
+ d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; |
|
358 |
+ d3dpp.Flags = D3DPRESENTFLAG_VIDEO; |
|
359 |
+ |
|
360 |
+ hr = IDirect3D9_CreateDevice(ctx->d3d9, adapter, D3DDEVTYPE_HAL, GetShellWindow(), |
|
361 |
+ D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE, |
|
362 |
+ &d3dpp, &ctx->d3d9device); |
|
363 |
+ if (FAILED(hr)) { |
|
364 |
+ av_log(NULL, loglevel, "Failed to create Direct3D device\n"); |
|
365 |
+ goto fail; |
|
366 |
+ } |
|
367 |
+ |
|
368 |
+ hr = createDeviceManager(&resetToken, &ctx->d3d9devmgr); |
|
369 |
+ if (FAILED(hr)) { |
|
370 |
+ av_log(NULL, loglevel, "Failed to create Direct3D device manager\n"); |
|
371 |
+ goto fail; |
|
372 |
+ } |
|
373 |
+ |
|
374 |
+ hr = IDirect3DDeviceManager9_ResetDevice(ctx->d3d9devmgr, ctx->d3d9device, resetToken); |
|
375 |
+ if (FAILED(hr)) { |
|
376 |
+ av_log(NULL, loglevel, "Failed to bind Direct3D device to device manager\n"); |
|
377 |
+ goto fail; |
|
378 |
+ } |
|
379 |
+ |
|
380 |
+ hr = IDirect3DDeviceManager9_OpenDeviceHandle(ctx->d3d9devmgr, &ctx->deviceHandle); |
|
381 |
+ if (FAILED(hr)) { |
|
382 |
+ av_log(NULL, loglevel, "Failed to open device handle\n"); |
|
383 |
+ goto fail; |
|
384 |
+ } |
|
385 |
+ |
|
386 |
+ hr = IDirect3DDeviceManager9_GetVideoService(ctx->d3d9devmgr, ctx->deviceHandle, &IID_IDirectXVideoDecoderService, (void **)&ctx->decoder_service); |
|
387 |
+ if (FAILED(hr)) { |
|
388 |
+ av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n"); |
|
389 |
+ goto fail; |
|
390 |
+ } |
|
391 |
+ |
|
392 |
+ ctx->tmp_frame = av_frame_alloc(); |
|
393 |
+ if (!ctx->tmp_frame) |
|
394 |
+ goto fail; |
|
395 |
+ |
|
396 |
+ s->hwaccel_context = av_mallocz(sizeof(struct dxva_context)); |
|
397 |
+ if (!s->hwaccel_context) |
|
398 |
+ goto fail; |
|
399 |
+ |
|
400 |
+ return 0; |
|
401 |
+fail: |
|
402 |
+ dxva2_uninit(s); |
|
403 |
+ return AVERROR(EINVAL); |
|
404 |
+} |
|
405 |
+ |
|
406 |
+static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid, |
|
407 |
+ const DXVA2_VideoDesc *desc, |
|
408 |
+ DXVA2_ConfigPictureDecode *config) |
|
409 |
+{ |
|
410 |
+ InputStream *ist = s->opaque; |
|
411 |
+ int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; |
|
412 |
+ DXVA2Context *ctx = ist->hwaccel_ctx; |
|
413 |
+ unsigned cfg_count = 0, best_score = 0; |
|
414 |
+ DXVA2_ConfigPictureDecode *cfg_list = NULL; |
|
415 |
+ DXVA2_ConfigPictureDecode best_cfg = {{0}}; |
|
416 |
+ HRESULT hr; |
|
417 |
+ int i; |
|
418 |
+ |
|
419 |
+ hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list); |
|
420 |
+ if (FAILED(hr)) { |
|
421 |
+ av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n"); |
|
422 |
+ return AVERROR(EINVAL); |
|
423 |
+ } |
|
424 |
+ |
|
425 |
+ for (i = 0; i < cfg_count; i++) { |
|
426 |
+ DXVA2_ConfigPictureDecode *cfg = &cfg_list[i]; |
|
427 |
+ |
|
428 |
+ unsigned score; |
|
429 |
+ if (cfg->ConfigBitstreamRaw == 1) |
|
430 |
+ score = 1; |
|
431 |
+ else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2) |
|
432 |
+ score = 2; |
|
433 |
+ else |
|
434 |
+ continue; |
|
435 |
+ if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt)) |
|
436 |
+ score += 16; |
|
437 |
+ if (score > best_score) { |
|
438 |
+ best_score = score; |
|
439 |
+ best_cfg = *cfg; |
|
440 |
+ } |
|
441 |
+ } |
|
442 |
+ CoTaskMemFree(cfg_list); |
|
443 |
+ |
|
444 |
+ if (!best_score) { |
|
445 |
+ av_log(NULL, loglevel, "No valid decoder configuration available\n"); |
|
446 |
+ return AVERROR(EINVAL); |
|
447 |
+ } |
|
448 |
+ |
|
449 |
+ *config = best_cfg; |
|
450 |
+ return 0; |
|
451 |
+} |
|
452 |
+ |
|
453 |
+static int dxva2_create_decoder(AVCodecContext *s) |
|
454 |
+{ |
|
455 |
+ InputStream *ist = s->opaque; |
|
456 |
+ int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; |
|
457 |
+ DXVA2Context *ctx = ist->hwaccel_ctx; |
|
458 |
+ struct dxva_context *dxva_ctx = s->hwaccel_context; |
|
459 |
+ GUID *guid_list = NULL; |
|
460 |
+ unsigned guid_count = 0, i, j; |
|
461 |
+ GUID device_guid = GUID_NULL; |
|
462 |
+ D3DFORMAT target_format = 0; |
|
463 |
+ DXVA2_VideoDesc desc = { 0 }; |
|
464 |
+ DXVA2_ConfigPictureDecode config; |
|
465 |
+ HRESULT hr; |
|
466 |
+ int surface_alignment; |
|
467 |
+ int ret; |
|
468 |
+ |
|
469 |
+ hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list); |
|
470 |
+ if (FAILED(hr)) { |
|
471 |
+ av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n"); |
|
472 |
+ goto fail; |
|
473 |
+ } |
|
474 |
+ |
|
475 |
+ for (i = 0; dxva2_modes[i].guid; i++) { |
|
476 |
+ D3DFORMAT *target_list = NULL; |
|
477 |
+ unsigned target_count = 0; |
|
478 |
+ const dxva2_mode *mode = &dxva2_modes[i]; |
|
479 |
+ if (mode->codec != s->codec_id) |
|
480 |
+ continue; |
|
481 |
+ |
|
482 |
+ for (j = 0; j < guid_count; j++) { |
|
483 |
+ if (IsEqualGUID(mode->guid, &guid_list[j])) |
|
484 |
+ break; |
|
485 |
+ } |
|
486 |
+ if (j == guid_count) |
|
487 |
+ continue; |
|
488 |
+ |
|
489 |
+ hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list); |
|
490 |
+ if (FAILED(hr)) { |
|
491 |
+ continue; |
|
492 |
+ } |
|
493 |
+ for (j = 0; j < target_count; j++) { |
|
494 |
+ const D3DFORMAT format = target_list[j]; |
|
495 |
+ if (format == MKTAG('N','V','1','2')) { |
|
496 |
+ target_format = format; |
|
497 |
+ break; |
|
498 |
+ } |
|
499 |
+ } |
|
500 |
+ CoTaskMemFree(target_list); |
|
501 |
+ if (target_format) { |
|
502 |
+ device_guid = *mode->guid; |
|
503 |
+ break; |
|
504 |
+ } |
|
505 |
+ } |
|
506 |
+ CoTaskMemFree(guid_list); |
|
507 |
+ |
|
508 |
+ if (IsEqualGUID(&device_guid, &GUID_NULL)) { |
|
509 |
+ av_log(NULL, loglevel, "No decoder device for codec found\n"); |
|
510 |
+ goto fail; |
|
511 |
+ } |
|
512 |
+ |
|
513 |
+ desc.SampleWidth = s->coded_width; |
|
514 |
+ desc.SampleHeight = s->coded_height; |
|
515 |
+ desc.Format = target_format; |
|
516 |
+ |
|
517 |
+ ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config); |
|
518 |
+ if (ret < 0) { |
|
519 |
+ goto fail; |
|
520 |
+ } |
|
521 |
+ |
|
522 |
+ /* decoding MPEG-2 requires additional alignment on some Intel GPUs, |
|
523 |
+ but it causes issues for H.264 on certain AMD GPUs..... */ |
|
524 |
+ if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) |
|
525 |
+ surface_alignment = 32; |
|
526 |
+ else |
|
527 |
+ surface_alignment = 16; |
|
528 |
+ |
|
529 |
+ /* 4 base work surfaces */ |
|
530 |
+ ctx->num_surfaces = 4; |
|
531 |
+ |
|
532 |
+ /* add surfaces based on number of possible refs */ |
|
533 |
+ if (s->codec_id == AV_CODEC_ID_H264) |
|
534 |
+ ctx->num_surfaces += 16; |
|
535 |
+ else |
|
536 |
+ ctx->num_surfaces += 2; |
|
537 |
+ |
|
538 |
+ /* add extra surfaces for frame threading */ |
|
539 |
+ if (s->active_thread_type & FF_THREAD_FRAME) |
|
540 |
+ ctx->num_surfaces += s->thread_count; |
|
541 |
+ |
|
542 |
+ ctx->surfaces = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surfaces)); |
|
543 |
+ ctx->surface_infos = av_mallocz(ctx->num_surfaces * sizeof(*ctx->surface_infos)); |
|
544 |
+ |
|
545 |
+ if (!ctx->surfaces || !ctx->surface_infos) { |
|
546 |
+ av_log(NULL, loglevel, "Unable to allocate surface arrays\n"); |
|
547 |
+ goto fail; |
|
548 |
+ } |
|
549 |
+ |
|
550 |
+ hr = IDirectXVideoDecoderService_CreateSurface(ctx->decoder_service, |
|
551 |
+ FFALIGN(s->coded_width, surface_alignment), |
|
552 |
+ FFALIGN(s->coded_height, surface_alignment), |
|
553 |
+ ctx->num_surfaces - 1, |
|
554 |
+ target_format, D3DPOOL_DEFAULT, 0, |
|
555 |
+ DXVA2_VideoDecoderRenderTarget, |
|
556 |
+ ctx->surfaces, NULL); |
|
557 |
+ if (FAILED(hr)) { |
|
558 |
+ av_log(NULL, loglevel, "Failed to create %d video surfaces\n", ctx->num_surfaces); |
|
559 |
+ goto fail; |
|
560 |
+ } |
|
561 |
+ |
|
562 |
+ hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid, |
|
563 |
+ &desc, &config, ctx->surfaces, |
|
564 |
+ ctx->num_surfaces, &ctx->decoder); |
|
565 |
+ if (FAILED(hr)) { |
|
566 |
+ av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n"); |
|
567 |
+ goto fail; |
|
568 |
+ } |
|
569 |
+ |
|
570 |
+ ctx->decoder_guid = device_guid; |
|
571 |
+ ctx->decoder_config = config; |
|
572 |
+ |
|
573 |
+ dxva_ctx->cfg = &ctx->decoder_config; |
|
574 |
+ dxva_ctx->decoder = ctx->decoder; |
|
575 |
+ dxva_ctx->surface = ctx->surfaces; |
|
576 |
+ dxva_ctx->surface_count = ctx->num_surfaces; |
|
577 |
+ |
|
578 |
+ if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E)) |
|
579 |
+ dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO; |
|
580 |
+ |
|
581 |
+ return 0; |
|
582 |
+fail: |
|
583 |
+ dxva2_destroy_decoder(s); |
|
584 |
+ return AVERROR(EINVAL); |
|
585 |
+} |
|
586 |
+ |
|
587 |
+int dxva2_init(AVCodecContext *s) |
|
588 |
+{ |
|
589 |
+ InputStream *ist = s->opaque; |
|
590 |
+ int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; |
|
591 |
+ DXVA2Context *ctx; |
|
592 |
+ int ret; |
|
593 |
+ |
|
594 |
+ if (!ist->hwaccel_ctx) { |
|
595 |
+ ret = dxva2_alloc(s); |
|
596 |
+ if (ret < 0) |
|
597 |
+ return ret; |
|
598 |
+ } |
|
599 |
+ ctx = ist->hwaccel_ctx; |
|
600 |
+ |
|
601 |
+ if (s->codec_id == AV_CODEC_ID_H264 && |
|
602 |
+ (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) { |
|
603 |
+ av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile); |
|
604 |
+ return AVERROR(EINVAL); |
|
605 |
+ } |
|
606 |
+ |
|
607 |
+ if (ctx->decoder) |
|
608 |
+ dxva2_destroy_decoder(s); |
|
609 |
+ |
|
610 |
+ ret = dxva2_create_decoder(s); |
|
611 |
+ if (ret < 0) { |
|
612 |
+ av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n"); |
|
613 |
+ return ret; |
|
614 |
+ } |
|
615 |
+ |
|
616 |
+ return 0; |
|
617 |
+} |