ffmpeg_cuvid.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "libavutil/hwcontext.h"
  19. #include "libavutil/pixdesc.h"
  20. #include "ffmpeg.h"
  21. static void cuvid_uninit(AVCodecContext *avctx)
  22. {
  23. InputStream *ist = avctx->opaque;
  24. av_buffer_unref(&ist->hw_frames_ctx);
  25. }
  26. int cuvid_init(AVCodecContext *avctx)
  27. {
  28. InputStream *ist = avctx->opaque;
  29. AVHWFramesContext *frames_ctx;
  30. int ret;
  31. av_log(avctx, AV_LOG_VERBOSE, "Initializing cuvid hwaccel\n");
  32. if (!hw_device_ctx) {
  33. ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_CUDA,
  34. ist->hwaccel_device, NULL, 0);
  35. if (ret < 0) {
  36. av_log(avctx, AV_LOG_ERROR, "Error creating a CUDA device\n");
  37. return ret;
  38. }
  39. }
  40. av_buffer_unref(&ist->hw_frames_ctx);
  41. ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
  42. if (!ist->hw_frames_ctx) {
  43. av_log(avctx, AV_LOG_ERROR, "Error creating a CUDA frames context\n");
  44. return AVERROR(ENOMEM);
  45. }
  46. frames_ctx = (AVHWFramesContext*)ist->hw_frames_ctx->data;
  47. frames_ctx->format = AV_PIX_FMT_CUDA;
  48. frames_ctx->sw_format = avctx->sw_pix_fmt;
  49. frames_ctx->width = avctx->width;
  50. frames_ctx->height = avctx->height;
  51. av_log(avctx, AV_LOG_DEBUG, "Initializing CUDA frames context: sw_format = %s, width = %d, height = %d\n",
  52. av_get_pix_fmt_name(frames_ctx->sw_format), frames_ctx->width, frames_ctx->height);
  53. ret = av_hwframe_ctx_init(ist->hw_frames_ctx);
  54. if (ret < 0) {
  55. av_log(avctx, AV_LOG_ERROR, "Error initializing a CUDA frame pool\n");
  56. return ret;
  57. }
  58. ist->hwaccel_uninit = cuvid_uninit;
  59. return 0;
  60. }