You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.2KB

  1. /*
  2. * DXVA2 HW acceleration.
  3. *
  4. * copyright (c) 2010 Laurent Aimar
  5. *
  6. * This file is part of FFmpeg.
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. */
  22. #include "dxva2_internal.h"
  23. #include "libavutil/time.h"
  24. void *ff_dxva2_get_surface(const Picture *picture)
  25. {
  26. return picture->f.data[3];
  27. }
  28. unsigned ff_dxva2_get_surface_index(const struct dxva_context *ctx,
  29. const Picture *picture)
  30. {
  31. void *surface = ff_dxva2_get_surface(picture);
  32. unsigned i;
  33. for (i = 0; i < ctx->surface_count; i++)
  34. if (ctx->surface[i] == surface)
  35. return i;
  36. assert(0);
  37. return 0;
  38. }
  39. int ff_dxva2_commit_buffer(AVCodecContext *avctx,
  40. struct dxva_context *ctx,
  41. DXVA2_DecodeBufferDesc *dsc,
  42. unsigned type, const void *data, unsigned size,
  43. unsigned mb_count)
  44. {
  45. void *dxva_data;
  46. unsigned dxva_size;
  47. int result;
  48. HRESULT hr;
  49. hr = IDirectXVideoDecoder_GetBuffer(ctx->decoder, type,
  50. &dxva_data, &dxva_size);
  51. if (FAILED(hr)) {
  52. av_log(avctx, AV_LOG_ERROR, "Failed to get a buffer for %d: 0x%x\n",
  53. type, hr);
  54. return -1;
  55. }
  56. if (size <= dxva_size) {
  57. memcpy(dxva_data, data, size);
  58. memset(dsc, 0, sizeof(*dsc));
  59. dsc->CompressedBufferType = type;
  60. dsc->DataSize = size;
  61. dsc->NumMBsInBuffer = mb_count;
  62. result = 0;
  63. } else {
  64. av_log(avctx, AV_LOG_ERROR, "Buffer for type %d was too small\n", type);
  65. result = -1;
  66. }
  67. hr = IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder, type);
  68. if (FAILED(hr)) {
  69. av_log(avctx, AV_LOG_ERROR,
  70. "Failed to release buffer type %d: 0x%x\n",
  71. type, hr);
  72. result = -1;
  73. }
  74. return result;
  75. }
  76. int ff_dxva2_common_end_frame(AVCodecContext *avctx, Picture *pic,
  77. const void *pp, unsigned pp_size,
  78. const void *qm, unsigned qm_size,
  79. int (*commit_bs_si)(AVCodecContext *,
  80. DXVA2_DecodeBufferDesc *bs,
  81. DXVA2_DecodeBufferDesc *slice))
  82. {
  83. struct dxva_context *ctx = avctx->hwaccel_context;
  84. unsigned buffer_count = 0;
  85. DXVA2_DecodeBufferDesc buffer[4];
  86. DXVA2_DecodeExecuteParams exec = { 0 };
  87. int result, runs = 0;
  88. HRESULT hr;
  89. do {
  90. hr = IDirectXVideoDecoder_BeginFrame(ctx->decoder,
  91. ff_dxva2_get_surface(pic),
  92. NULL);
  93. if (hr == E_PENDING)
  94. av_usleep(2000);
  95. } while (hr == E_PENDING && ++runs < 50);
  96. if (FAILED(hr)) {
  97. av_log(avctx, AV_LOG_ERROR, "Failed to begin frame: 0x%x\n", hr);
  98. return -1;
  99. }
  100. result = ff_dxva2_commit_buffer(avctx, ctx, &buffer[buffer_count],
  101. DXVA2_PictureParametersBufferType,
  102. pp, pp_size, 0);
  103. if (result) {
  104. av_log(avctx, AV_LOG_ERROR,
  105. "Failed to add picture parameter buffer\n");
  106. goto end;
  107. }
  108. buffer_count++;
  109. if (qm_size > 0) {
  110. result = ff_dxva2_commit_buffer(avctx, ctx, &buffer[buffer_count],
  111. DXVA2_InverseQuantizationMatrixBufferType,
  112. qm, qm_size, 0);
  113. if (result) {
  114. av_log(avctx, AV_LOG_ERROR,
  115. "Failed to add inverse quantization matrix buffer\n");
  116. goto end;
  117. }
  118. buffer_count++;
  119. }
  120. result = commit_bs_si(avctx,
  121. &buffer[buffer_count + 0],
  122. &buffer[buffer_count + 1]);
  123. if (result) {
  124. av_log(avctx, AV_LOG_ERROR,
  125. "Failed to add bitstream or slice control buffer\n");
  126. goto end;
  127. }
  128. buffer_count += 2;
  129. /* TODO Film Grain when possible */
  130. assert(buffer_count == 1 + (qm_size > 0) + 2);
  131. exec.NumCompBuffers = buffer_count;
  132. exec.pCompressedBuffers = buffer;
  133. exec.pExtensionData = NULL;
  134. hr = IDirectXVideoDecoder_Execute(ctx->decoder, &exec);
  135. if (FAILED(hr)) {
  136. av_log(avctx, AV_LOG_ERROR, "Failed to execute: 0x%x\n", hr);
  137. result = -1;
  138. }
  139. end:
  140. hr = IDirectXVideoDecoder_EndFrame(ctx->decoder, NULL);
  141. if (FAILED(hr)) {
  142. av_log(avctx, AV_LOG_ERROR, "Failed to end frame: 0x%x\n", hr);
  143. result = -1;
  144. }
  145. return result;
  146. }