2 * DXVA2 HW acceleration.
4 * copyright (c) 2010 Laurent Aimar
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "dxva2_internal.h"
24 #include "libavutil/time.h"
26 void *ff_dxva2_get_surface(const Picture *picture)
28 return picture->f.data[3];
31 unsigned ff_dxva2_get_surface_index(const struct dxva_context *ctx,
32 const Picture *picture)
34 void *surface = ff_dxva2_get_surface(picture);
37 for (i = 0; i < ctx->surface_count; i++)
38 if (ctx->surface[i] == surface)
45 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
46 struct dxva_context *ctx,
47 DXVA2_DecodeBufferDesc *dsc,
48 unsigned type, const void *data, unsigned size,
56 hr = IDirectXVideoDecoder_GetBuffer(ctx->decoder, type,
57 &dxva_data, &dxva_size);
59 av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %d: 0x%x\n",
63 if (size <= dxva_size) {
64 memcpy(dxva_data, data, size);
66 memset(dsc, 0, sizeof(*dsc));
67 dsc->CompressedBufferType = type;
69 dsc->NumMBsInBuffer = mb_count;
73 av_log(avctx, AV_LOG_ERROR, "Buffer for type %d was too small\n", type);
77 hr = IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder, type);
79 av_log(avctx, AV_LOG_ERROR,
80 "Failed to release buffer type %d: 0x%x\n",
87 int ff_dxva2_common_end_frame(AVCodecContext *avctx, Picture *pic,
88 const void *pp, unsigned pp_size,
89 const void *qm, unsigned qm_size,
90 int (*commit_bs_si)(AVCodecContext *,
91 DXVA2_DecodeBufferDesc *bs,
92 DXVA2_DecodeBufferDesc *slice))
94 struct dxva_context *ctx = avctx->hwaccel_context;
95 unsigned buffer_count = 0;
96 DXVA2_DecodeBufferDesc buffer[4];
97 DXVA2_DecodeExecuteParams exec = { 0 };
102 hr = IDirectXVideoDecoder_BeginFrame(ctx->decoder,
103 ff_dxva2_get_surface(pic),
107 } while (hr == E_PENDING && ++runs < 50);
110 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", hr);
114 result = ff_dxva2_commit_buffer(avctx, ctx, &buffer[buffer_count],
115 DXVA2_PictureParametersBufferType,
118 av_log(avctx, AV_LOG_ERROR,
119 "Failed to add picture parameter buffer\n");
125 result = ff_dxva2_commit_buffer(avctx, ctx, &buffer[buffer_count],
126 DXVA2_InverseQuantizationMatrixBufferType,
129 av_log(avctx, AV_LOG_ERROR,
130 "Failed to add inverse quantization matrix buffer\n");
136 result = commit_bs_si(avctx,
137 &buffer[buffer_count + 0],
138 &buffer[buffer_count + 1]);
140 av_log(avctx, AV_LOG_ERROR,
141 "Failed to add bitstream or slice control buffer\n");
146 /* TODO Film Grain when possible */
148 assert(buffer_count == 1 + (qm_size > 0) + 2);
150 exec.NumCompBuffers = buffer_count;
151 exec.pCompressedBuffers = buffer;
152 exec.pExtensionData = NULL;
153 hr = IDirectXVideoDecoder_Execute(ctx->decoder, &exec);
155 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", hr);
160 hr = IDirectXVideoDecoder_EndFrame(ctx->decoder, NULL);
162 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", hr);