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
26 #include "libavutil/log.h"
27 #include "libavutil/time.h"
30 #include "mpegvideo.h"
31 #include "dxva2_internal.h"
33 void *ff_dxva2_get_surface(const AVFrame *frame)
35 return frame->data[3];
38 unsigned ff_dxva2_get_surface_index(const struct dxva_context *ctx,
41 void *surface = ff_dxva2_get_surface(frame);
44 for (i = 0; i < ctx->surface_count; i++)
45 if (ctx->surface[i] == surface)
52 int ff_dxva2_commit_buffer(AVCodecContext *avctx,
53 struct dxva_context *ctx,
54 DXVA2_DecodeBufferDesc *dsc,
55 unsigned type, const void *data, unsigned size,
63 hr = IDirectXVideoDecoder_GetBuffer(ctx->decoder, type,
64 &dxva_data, &dxva_size);
66 av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %u: 0x%lx\n",
70 if (size <= dxva_size) {
71 memcpy(dxva_data, data, size);
73 memset(dsc, 0, sizeof(*dsc));
74 dsc->CompressedBufferType = type;
76 dsc->NumMBsInBuffer = mb_count;
80 av_log(avctx, AV_LOG_ERROR, "Buffer for type %u was too small\n", type);
84 hr = IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder, type);
86 av_log(avctx, AV_LOG_ERROR,
87 "Failed to release buffer type %u: 0x%lx\n",
94 int ff_dxva2_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
95 const void *pp, unsigned pp_size,
96 const void *qm, unsigned qm_size,
97 int (*commit_bs_si)(AVCodecContext *,
98 DXVA2_DecodeBufferDesc *bs,
99 DXVA2_DecodeBufferDesc *slice))
101 struct dxva_context *ctx = avctx->hwaccel_context;
102 unsigned buffer_count = 0;
103 DXVA2_DecodeBufferDesc buffer[4];
104 DXVA2_DecodeExecuteParams exec = { 0 };
105 int result, runs = 0;
109 hr = IDirectXVideoDecoder_BeginFrame(ctx->decoder,
110 ff_dxva2_get_surface(frame),
114 } while (hr == E_PENDING && ++runs < 50);
117 av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%lx\n", hr);
121 result = ff_dxva2_commit_buffer(avctx, ctx, &buffer[buffer_count],
122 DXVA2_PictureParametersBufferType,
125 av_log(avctx, AV_LOG_ERROR,
126 "Failed to add picture parameter buffer\n");
132 result = ff_dxva2_commit_buffer(avctx, ctx, &buffer[buffer_count],
133 DXVA2_InverseQuantizationMatrixBufferType,
136 av_log(avctx, AV_LOG_ERROR,
137 "Failed to add inverse quantization matrix buffer\n");
143 result = commit_bs_si(avctx,
144 &buffer[buffer_count + 0],
145 &buffer[buffer_count + 1]);
147 av_log(avctx, AV_LOG_ERROR,
148 "Failed to add bitstream or slice control buffer\n");
153 /* TODO Film Grain when possible */
155 assert(buffer_count == 1 + (qm_size > 0) + 2);
157 exec.NumCompBuffers = buffer_count;
158 exec.pCompressedBuffers = buffer;
159 exec.pExtensionData = NULL;
160 hr = IDirectXVideoDecoder_Execute(ctx->decoder, &exec);
162 av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%lx\n", hr);
167 hr = IDirectXVideoDecoder_EndFrame(ctx->decoder, NULL);
169 av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%lx\n", hr);