ffmpeg_vdpau.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include "ffmpeg.h"
  20. #include "libavcodec/vdpau.h"
  21. #include "libavutil/buffer.h"
  22. #include "libavutil/frame.h"
  23. #include "libavutil/hwcontext.h"
  24. #include "libavutil/hwcontext_vdpau.h"
  25. #include "libavutil/pixfmt.h"
  26. typedef struct VDPAUContext {
  27. AVBufferRef *hw_frames_ctx;
  28. AVFrame *tmp_frame;
  29. } VDPAUContext;
  30. static void vdpau_uninit(AVCodecContext *s)
  31. {
  32. InputStream *ist = s->opaque;
  33. VDPAUContext *ctx = ist->hwaccel_ctx;
  34. ist->hwaccel_uninit = NULL;
  35. ist->hwaccel_get_buffer = NULL;
  36. ist->hwaccel_retrieve_data = NULL;
  37. av_buffer_unref(&ctx->hw_frames_ctx);
  38. av_frame_free(&ctx->tmp_frame);
  39. av_freep(&ist->hwaccel_ctx);
  40. av_freep(&s->hwaccel_context);
  41. }
  42. static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  43. {
  44. InputStream *ist = s->opaque;
  45. VDPAUContext *ctx = ist->hwaccel_ctx;
  46. return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
  47. }
  48. static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
  49. {
  50. InputStream *ist = s->opaque;
  51. VDPAUContext *ctx = ist->hwaccel_ctx;
  52. int ret;
  53. ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
  54. if (ret < 0)
  55. return ret;
  56. ret = av_frame_copy_props(ctx->tmp_frame, frame);
  57. if (ret < 0) {
  58. av_frame_unref(ctx->tmp_frame);
  59. return ret;
  60. }
  61. av_frame_unref(frame);
  62. av_frame_move_ref(frame, ctx->tmp_frame);
  63. return 0;
  64. }
  65. static int vdpau_alloc(AVCodecContext *s)
  66. {
  67. InputStream *ist = s->opaque;
  68. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  69. VDPAUContext *ctx;
  70. int ret;
  71. AVBufferRef *device_ref = NULL;
  72. AVHWDeviceContext *device_ctx;
  73. AVVDPAUDeviceContext *device_hwctx;
  74. AVHWFramesContext *frames_ctx;
  75. ctx = av_mallocz(sizeof(*ctx));
  76. if (!ctx)
  77. return AVERROR(ENOMEM);
  78. ist->hwaccel_ctx = ctx;
  79. ist->hwaccel_uninit = vdpau_uninit;
  80. ist->hwaccel_get_buffer = vdpau_get_buffer;
  81. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  82. ctx->tmp_frame = av_frame_alloc();
  83. if (!ctx->tmp_frame)
  84. goto fail;
  85. ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_VDPAU,
  86. ist->hwaccel_device, NULL, 0);
  87. if (ret < 0)
  88. goto fail;
  89. device_ctx = (AVHWDeviceContext*)device_ref->data;
  90. device_hwctx = device_ctx->hwctx;
  91. ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
  92. if (!ctx->hw_frames_ctx)
  93. goto fail;
  94. av_buffer_unref(&device_ref);
  95. frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  96. frames_ctx->format = AV_PIX_FMT_VDPAU;
  97. frames_ctx->sw_format = s->sw_pix_fmt;
  98. frames_ctx->width = s->coded_width;
  99. frames_ctx->height = s->coded_height;
  100. ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
  101. if (ret < 0)
  102. goto fail;
  103. if (av_vdpau_bind_context(s, device_hwctx->device, device_hwctx->get_proc_address, 0))
  104. goto fail;
  105. av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU to decode input stream #%d:%d.\n",
  106. ist->file_index, ist->st->index);
  107. return 0;
  108. fail:
  109. av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
  110. ist->file_index, ist->st->index);
  111. av_buffer_unref(&device_ref);
  112. vdpau_uninit(s);
  113. return AVERROR(EINVAL);
  114. }
  115. int vdpau_init(AVCodecContext *s)
  116. {
  117. InputStream *ist = s->opaque;
  118. if (!ist->hwaccel_ctx) {
  119. int ret = vdpau_alloc(s);
  120. if (ret < 0)
  121. return ret;
  122. }
  123. ist->hwaccel_get_buffer = vdpau_get_buffer;
  124. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  125. return 0;
  126. }