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.

153 lines
5.0KB

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