avconv: refactor selecting an encoder
[ffmpeg.git] / avconv_dxva2.c
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 <windows.h>
20
21 #ifdef _WIN32_WINNT
22 #undef _WIN32_WINNT
23 #endif
24 #define _WIN32_WINNT 0x0600
25 #define DXVA2API_USE_BITFIELDS
26 #define COBJMACROS
27
28 #include <stdint.h>
29
30 #include <d3d9.h>
31 #include <dxva2api.h>
32
33 #include "avconv.h"
34
35 #include "libavcodec/dxva2.h"
36
37 #include "libavutil/avassert.h"
38 #include "libavutil/buffer.h"
39 #include "libavutil/frame.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/pixfmt.h"
42
43 #include "libavutil/hwcontext.h"
44 #include "libavutil/hwcontext_dxva2.h"
45
46 /* define all the GUIDs used directly here,
47    to avoid problems with inconsistent dxva2api.h versions in mingw-w64 and different MSVC version */
48 #include <initguid.h>
49 DEFINE_GUID(IID_IDirectXVideoDecoderService, 0xfc51a551,0xd5e7,0x11d9,0xaf,0x55,0x00,0x05,0x4e,0x43,0xff,0x02);
50
51 DEFINE_GUID(DXVA2_ModeMPEG2_VLD,      0xee27417f, 0x5e28,0x4e65,0xbe,0xea,0x1d,0x26,0xb5,0x08,0xad,0xc9);
52 DEFINE_GUID(DXVA2_ModeMPEG2and1_VLD,  0x86695f12, 0x340e,0x4f04,0x9f,0xd3,0x92,0x53,0xdd,0x32,0x74,0x60);
53 DEFINE_GUID(DXVA2_ModeH264_E,         0x1b81be68, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
54 DEFINE_GUID(DXVA2_ModeH264_F,         0x1b81be69, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
55 DEFINE_GUID(DXVADDI_Intel_ModeH264_E, 0x604F8E68, 0x4951,0x4C54,0x88,0xFE,0xAB,0xD2,0x5C,0x15,0xB3,0xD6);
56 DEFINE_GUID(DXVA2_ModeVC1_D,          0x1b81beA3, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
57 DEFINE_GUID(DXVA2_ModeVC1_D2010,      0x1b81beA4, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
58 DEFINE_GUID(DXVA2_ModeHEVC_VLD_Main,  0x5b11d51b, 0x2f4c,0x4452,0xbc,0xc3,0x09,0xf2,0xa1,0x16,0x0c,0xc0);
59 DEFINE_GUID(DXVA2_NoEncrypt,          0x1b81beD0, 0xa0c7,0x11d3,0xb9,0x84,0x00,0xc0,0x4f,0x2e,0x73,0xc5);
60 DEFINE_GUID(GUID_NULL,                0x00000000, 0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
61
62 typedef struct dxva2_mode {
63   const GUID     *guid;
64   enum AVCodecID codec;
65 } dxva2_mode;
66
67 static const dxva2_mode dxva2_modes[] = {
68     /* MPEG-2 */
69     { &DXVA2_ModeMPEG2_VLD,      AV_CODEC_ID_MPEG2VIDEO },
70     { &DXVA2_ModeMPEG2and1_VLD,  AV_CODEC_ID_MPEG2VIDEO },
71
72     /* H.264 */
73     { &DXVA2_ModeH264_F,         AV_CODEC_ID_H264 },
74     { &DXVA2_ModeH264_E,         AV_CODEC_ID_H264 },
75     /* Intel specific H.264 mode */
76     { &DXVADDI_Intel_ModeH264_E, AV_CODEC_ID_H264 },
77
78     /* VC-1 / WMV3 */
79     { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_VC1  },
80     { &DXVA2_ModeVC1_D2010,      AV_CODEC_ID_WMV3 },
81     { &DXVA2_ModeVC1_D,          AV_CODEC_ID_VC1  },
82     { &DXVA2_ModeVC1_D,          AV_CODEC_ID_WMV3 },
83
84     /* HEVC/H.265 */
85     { &DXVA2_ModeHEVC_VLD_Main,  AV_CODEC_ID_HEVC },
86
87     { NULL,                      0 },
88 };
89
90 typedef struct DXVA2Context {
91     IDirectXVideoDecoder        *decoder;
92
93     GUID                        decoder_guid;
94     DXVA2_ConfigPictureDecode   decoder_config;
95     IDirectXVideoDecoderService *decoder_service;
96
97     AVFrame                     *tmp_frame;
98
99     AVBufferRef                 *hw_device_ctx;
100     AVBufferRef                 *hw_frames_ctx;
101 } DXVA2Context;
102
103 static void dxva2_uninit(AVCodecContext *s)
104 {
105     InputStream  *ist = s->opaque;
106     DXVA2Context *ctx = ist->hwaccel_ctx;
107
108     ist->hwaccel_uninit        = NULL;
109     ist->hwaccel_get_buffer    = NULL;
110     ist->hwaccel_retrieve_data = NULL;
111
112     if (ctx->decoder_service)
113         IDirectXVideoDecoderService_Release(ctx->decoder_service);
114
115     av_buffer_unref(&ctx->hw_frames_ctx);
116     av_buffer_unref(&ctx->hw_device_ctx);
117
118     av_frame_free(&ctx->tmp_frame);
119
120     av_freep(&ist->hwaccel_ctx);
121     av_freep(&s->hwaccel_context);
122 }
123
124 static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
125 {
126     InputStream  *ist = s->opaque;
127     DXVA2Context *ctx = ist->hwaccel_ctx;
128
129     return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
130 }
131
132 static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
133 {
134     InputStream        *ist = s->opaque;
135     DXVA2Context       *ctx = ist->hwaccel_ctx;
136     int                ret;
137
138     ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
139     if (ret < 0)
140         return ret;
141
142     ret = av_frame_copy_props(ctx->tmp_frame, frame);
143     if (ret < 0) {
144         av_frame_unref(ctx->tmp_frame);
145         return ret;
146     }
147
148     av_frame_unref(frame);
149     av_frame_move_ref(frame, ctx->tmp_frame);
150
151     return 0;
152 }
153
154 static int dxva2_alloc(AVCodecContext *s)
155 {
156     InputStream  *ist = s->opaque;
157     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
158     DXVA2Context *ctx;
159     HANDLE device_handle;
160     HRESULT hr;
161
162     AVHWDeviceContext    *device_ctx;
163     AVDXVA2DeviceContext *device_hwctx;
164     int ret;
165
166     ctx = av_mallocz(sizeof(*ctx));
167     if (!ctx)
168         return AVERROR(ENOMEM);
169
170     ist->hwaccel_ctx           = ctx;
171     ist->hwaccel_uninit        = dxva2_uninit;
172     ist->hwaccel_get_buffer    = dxva2_get_buffer;
173     ist->hwaccel_retrieve_data = dxva2_retrieve_data;
174
175     ret = av_hwdevice_ctx_create(&ctx->hw_device_ctx, AV_HWDEVICE_TYPE_DXVA2,
176                                  ist->hwaccel_device, NULL, 0);
177     if (ret < 0)
178         goto fail;
179     device_ctx   = (AVHWDeviceContext*)ctx->hw_device_ctx->data;
180     device_hwctx = device_ctx->hwctx;
181
182     hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr,
183                                                   &device_handle);
184     if (FAILED(hr)) {
185         av_log(NULL, loglevel, "Failed to open a device handle\n");
186         goto fail;
187     }
188
189     hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr, device_handle,
190                                                  &IID_IDirectXVideoDecoderService,
191                                                  (void **)&ctx->decoder_service);
192     IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, device_handle);
193     if (FAILED(hr)) {
194         av_log(NULL, loglevel, "Failed to create IDirectXVideoDecoderService\n");
195         goto fail;
196     }
197
198     ctx->tmp_frame = av_frame_alloc();
199     if (!ctx->tmp_frame)
200         goto fail;
201
202     s->hwaccel_context = av_mallocz(sizeof(struct dxva_context));
203     if (!s->hwaccel_context)
204         goto fail;
205
206     return 0;
207 fail:
208     dxva2_uninit(s);
209     return AVERROR(EINVAL);
210 }
211
212 static int dxva2_get_decoder_configuration(AVCodecContext *s, const GUID *device_guid,
213                                            const DXVA2_VideoDesc *desc,
214                                            DXVA2_ConfigPictureDecode *config)
215 {
216     InputStream  *ist = s->opaque;
217     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
218     DXVA2Context *ctx = ist->hwaccel_ctx;
219     unsigned cfg_count = 0, best_score = 0;
220     DXVA2_ConfigPictureDecode *cfg_list = NULL;
221     DXVA2_ConfigPictureDecode best_cfg = {{0}};
222     HRESULT hr;
223     int i;
224
225     hr = IDirectXVideoDecoderService_GetDecoderConfigurations(ctx->decoder_service, device_guid, desc, NULL, &cfg_count, &cfg_list);
226     if (FAILED(hr)) {
227         av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n");
228         return AVERROR(EINVAL);
229     }
230
231     for (i = 0; i < cfg_count; i++) {
232         DXVA2_ConfigPictureDecode *cfg = &cfg_list[i];
233
234         unsigned score;
235         if (cfg->ConfigBitstreamRaw == 1)
236             score = 1;
237         else if (s->codec_id == AV_CODEC_ID_H264 && cfg->ConfigBitstreamRaw == 2)
238             score = 2;
239         else
240             continue;
241         if (IsEqualGUID(&cfg->guidConfigBitstreamEncryption, &DXVA2_NoEncrypt))
242             score += 16;
243         if (score > best_score) {
244             best_score = score;
245             best_cfg   = *cfg;
246         }
247     }
248     CoTaskMemFree(cfg_list);
249
250     if (!best_score) {
251         av_log(NULL, loglevel, "No valid decoder configuration available\n");
252         return AVERROR(EINVAL);
253     }
254
255     *config = best_cfg;
256     return 0;
257 }
258
259 static int dxva2_create_decoder(AVCodecContext *s)
260 {
261     InputStream  *ist = s->opaque;
262     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
263     DXVA2Context *ctx = ist->hwaccel_ctx;
264     struct dxva_context *dxva_ctx = s->hwaccel_context;
265     GUID *guid_list = NULL;
266     unsigned guid_count = 0, i, j;
267     GUID device_guid = GUID_NULL;
268     D3DFORMAT target_format = 0;
269     DXVA2_VideoDesc desc = { 0 };
270     DXVA2_ConfigPictureDecode config;
271     HRESULT hr;
272     int surface_alignment, num_surfaces;
273     int ret;
274
275     AVDXVA2FramesContext *frames_hwctx;
276     AVHWFramesContext *frames_ctx;
277
278     hr = IDirectXVideoDecoderService_GetDecoderDeviceGuids(ctx->decoder_service, &guid_count, &guid_list);
279     if (FAILED(hr)) {
280         av_log(NULL, loglevel, "Failed to retrieve decoder device GUIDs\n");
281         goto fail;
282     }
283
284     for (i = 0; dxva2_modes[i].guid; i++) {
285         D3DFORMAT *target_list = NULL;
286         unsigned target_count = 0;
287         const dxva2_mode *mode = &dxva2_modes[i];
288         if (mode->codec != s->codec_id)
289             continue;
290
291         for (j = 0; j < guid_count; j++) {
292             if (IsEqualGUID(mode->guid, &guid_list[j]))
293                 break;
294         }
295         if (j == guid_count)
296             continue;
297
298         hr = IDirectXVideoDecoderService_GetDecoderRenderTargets(ctx->decoder_service, mode->guid, &target_count, &target_list);
299         if (FAILED(hr)) {
300             continue;
301         }
302         for (j = 0; j < target_count; j++) {
303             const D3DFORMAT format = target_list[j];
304             if (format == MKTAG('N','V','1','2')) {
305                 target_format = format;
306                 break;
307             }
308         }
309         CoTaskMemFree(target_list);
310         if (target_format) {
311             device_guid = *mode->guid;
312             break;
313         }
314     }
315     CoTaskMemFree(guid_list);
316
317     if (IsEqualGUID(&device_guid, &GUID_NULL)) {
318         av_log(NULL, loglevel, "No decoder device for codec found\n");
319         goto fail;
320     }
321
322     desc.SampleWidth  = s->coded_width;
323     desc.SampleHeight = s->coded_height;
324     desc.Format       = target_format;
325
326     ret = dxva2_get_decoder_configuration(s, &device_guid, &desc, &config);
327     if (ret < 0) {
328         goto fail;
329     }
330
331     /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
332        but it causes issues for H.264 on certain AMD GPUs..... */
333     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
334         surface_alignment = 32;
335     /* the HEVC DXVA2 spec asks for 128 pixel aligned surfaces to ensure
336        all coding features have enough room to work with */
337     else if  (s->codec_id == AV_CODEC_ID_HEVC)
338         surface_alignment = 128;
339     else
340         surface_alignment = 16;
341
342     /* 4 base work surfaces */
343     num_surfaces = 4;
344
345     /* add surfaces based on number of possible refs */
346     if (s->codec_id == AV_CODEC_ID_H264 || s->codec_id == AV_CODEC_ID_HEVC)
347         num_surfaces += 16;
348     else
349         num_surfaces += 2;
350
351     /* add extra surfaces for frame threading */
352     if (s->active_thread_type & FF_THREAD_FRAME)
353         num_surfaces += s->thread_count;
354
355     ctx->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->hw_device_ctx);
356     if (!ctx->hw_frames_ctx)
357         goto fail;
358     frames_ctx   = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
359     frames_hwctx = frames_ctx->hwctx;
360
361     frames_ctx->format            = AV_PIX_FMT_DXVA2_VLD;
362     frames_ctx->sw_format         = AV_PIX_FMT_NV12;
363     frames_ctx->width             = FFALIGN(s->coded_width, surface_alignment);
364     frames_ctx->height            = FFALIGN(s->coded_height, surface_alignment);
365     frames_ctx->initial_pool_size = num_surfaces;
366
367     frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
368
369     ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
370     if (ret < 0) {
371         av_log(NULL, loglevel, "Failed to initialize the HW frames context\n");
372         goto fail;
373     }
374
375     hr = IDirectXVideoDecoderService_CreateVideoDecoder(ctx->decoder_service, &device_guid,
376                                                         &desc, &config, frames_hwctx->surfaces,
377                                                         frames_hwctx->nb_surfaces, &frames_hwctx->decoder_to_release);
378     if (FAILED(hr)) {
379         av_log(NULL, loglevel, "Failed to create DXVA2 video decoder\n");
380         goto fail;
381     }
382
383     ctx->decoder_guid   = device_guid;
384     ctx->decoder_config = config;
385
386     dxva_ctx->cfg           = &ctx->decoder_config;
387     dxva_ctx->decoder       = frames_hwctx->decoder_to_release;
388     dxva_ctx->surface       = frames_hwctx->surfaces;
389     dxva_ctx->surface_count = frames_hwctx->nb_surfaces;
390
391     if (IsEqualGUID(&ctx->decoder_guid, &DXVADDI_Intel_ModeH264_E))
392         dxva_ctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO;
393
394     return 0;
395 fail:
396     av_buffer_unref(&ctx->hw_frames_ctx);
397     return AVERROR(EINVAL);
398 }
399
400 int dxva2_init(AVCodecContext *s)
401 {
402     InputStream *ist = s->opaque;
403     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
404     DXVA2Context *ctx;
405     int ret;
406
407     if (!ist->hwaccel_ctx) {
408         ret = dxva2_alloc(s);
409         if (ret < 0)
410             return ret;
411     }
412     ctx = ist->hwaccel_ctx;
413
414     if (s->codec_id == AV_CODEC_ID_H264 &&
415         (s->profile & ~FF_PROFILE_H264_CONSTRAINED) > FF_PROFILE_H264_HIGH) {
416         av_log(NULL, loglevel, "Unsupported H.264 profile for DXVA2 HWAccel: %d\n", s->profile);
417         return AVERROR(EINVAL);
418     }
419
420     av_buffer_unref(&ctx->hw_frames_ctx);
421
422     ret = dxva2_create_decoder(s);
423     if (ret < 0) {
424         av_log(NULL, loglevel, "Error creating the DXVA2 decoder\n");
425         return ret;
426     }
427
428     return 0;
429 }