ffmpeg_vaapi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "config.h"
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/frame.h"
  21. #include "libavutil/hwcontext.h"
  22. #include "libavutil/log.h"
  23. #include "ffmpeg.h"
  24. static AVClass vaapi_class = {
  25. .class_name = "vaapi",
  26. .item_name = av_default_item_name,
  27. .version = LIBAVUTIL_VERSION_INT,
  28. };
  29. #define DEFAULT_SURFACES 20
  30. typedef struct VAAPIDecoderContext {
  31. const AVClass *class;
  32. AVBufferRef *device_ref;
  33. AVHWDeviceContext *device;
  34. AVBufferRef *frames_ref;
  35. AVHWFramesContext *frames;
  36. // The output need not have the same format, width and height as the
  37. // decoded frames - the copy for non-direct-mapped access is actually
  38. // a whole vpp instance which can do arbitrary scaling and format
  39. // conversion.
  40. enum AVPixelFormat output_format;
  41. } VAAPIDecoderContext;
  42. static int vaapi_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  43. {
  44. InputStream *ist = avctx->opaque;
  45. VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
  46. int err;
  47. err = av_hwframe_get_buffer(ctx->frames_ref, frame, 0);
  48. if (err < 0) {
  49. av_log(ctx, AV_LOG_ERROR, "Failed to allocate decoder surface.\n");
  50. } else {
  51. av_log(ctx, AV_LOG_DEBUG, "Decoder given surface %#x.\n",
  52. (unsigned int)(uintptr_t)frame->data[3]);
  53. }
  54. return err;
  55. }
  56. static int vaapi_retrieve_data(AVCodecContext *avctx, AVFrame *input)
  57. {
  58. InputStream *ist = avctx->opaque;
  59. VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
  60. AVFrame *output = 0;
  61. int err;
  62. av_assert0(input->format == AV_PIX_FMT_VAAPI);
  63. if (ctx->output_format == AV_PIX_FMT_VAAPI) {
  64. // Nothing to do.
  65. return 0;
  66. }
  67. av_log(ctx, AV_LOG_DEBUG, "Retrieve data from surface %#x.\n",
  68. (unsigned int)(uintptr_t)input->data[3]);
  69. output = av_frame_alloc();
  70. if (!output)
  71. return AVERROR(ENOMEM);
  72. output->format = ctx->output_format;
  73. err = av_hwframe_transfer_data(output, input, 0);
  74. if (err < 0) {
  75. av_log(ctx, AV_LOG_ERROR, "Failed to transfer data to "
  76. "output frame: %d.\n", err);
  77. goto fail;
  78. }
  79. err = av_frame_copy_props(output, input);
  80. if (err < 0) {
  81. av_frame_unref(output);
  82. goto fail;
  83. }
  84. av_frame_unref(input);
  85. av_frame_move_ref(input, output);
  86. av_frame_free(&output);
  87. return 0;
  88. fail:
  89. if (output)
  90. av_frame_free(&output);
  91. return err;
  92. }
  93. static void vaapi_decode_uninit(AVCodecContext *avctx)
  94. {
  95. InputStream *ist = avctx->opaque;
  96. VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
  97. if (ctx) {
  98. av_buffer_unref(&ctx->frames_ref);
  99. av_buffer_unref(&ctx->device_ref);
  100. av_free(ctx);
  101. }
  102. av_buffer_unref(&ist->hw_frames_ctx);
  103. ist->hwaccel_ctx = NULL;
  104. ist->hwaccel_uninit = NULL;
  105. ist->hwaccel_get_buffer = NULL;
  106. ist->hwaccel_retrieve_data = NULL;
  107. }
  108. int vaapi_decode_init(AVCodecContext *avctx)
  109. {
  110. InputStream *ist = avctx->opaque;
  111. VAAPIDecoderContext *ctx;
  112. int err;
  113. int loglevel = (ist->hwaccel_id != HWACCEL_VAAPI ? AV_LOG_VERBOSE
  114. : AV_LOG_ERROR);
  115. if (ist->hwaccel_ctx)
  116. vaapi_decode_uninit(avctx);
  117. // We have -hwaccel without -vaapi_device, so just initialise here with
  118. // the device passed as -hwaccel_device (if -vaapi_device was passed, it
  119. // will always have been called before now).
  120. if (!hw_device_ctx) {
  121. err = vaapi_device_init(ist->hwaccel_device);
  122. if (err < 0)
  123. return err;
  124. }
  125. ctx = av_mallocz(sizeof(*ctx));
  126. if (!ctx)
  127. return AVERROR(ENOMEM);
  128. ctx->class = &vaapi_class;
  129. ist->hwaccel_ctx = ctx;
  130. ctx->device_ref = av_buffer_ref(hw_device_ctx);
  131. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  132. ctx->output_format = ist->hwaccel_output_format;
  133. avctx->pix_fmt = ctx->output_format;
  134. ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  135. if (!ctx->frames_ref) {
  136. av_log(ctx, loglevel, "Failed to create VAAPI frame context.\n");
  137. err = AVERROR(ENOMEM);
  138. goto fail;
  139. }
  140. ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
  141. ctx->frames->format = AV_PIX_FMT_VAAPI;
  142. ctx->frames->width = avctx->coded_width;
  143. ctx->frames->height = avctx->coded_height;
  144. // It would be nice if we could query the available formats here,
  145. // but unfortunately we don't have a VAConfigID to do it with.
  146. // For now, just assume an NV12 format (or P010 if 10-bit).
  147. ctx->frames->sw_format = (avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
  148. AV_PIX_FMT_P010 : AV_PIX_FMT_NV12);
  149. // For frame-threaded decoding, at least one additional surface
  150. // is needed for each thread.
  151. ctx->frames->initial_pool_size = DEFAULT_SURFACES;
  152. if (avctx->active_thread_type & FF_THREAD_FRAME)
  153. ctx->frames->initial_pool_size += avctx->thread_count;
  154. err = av_hwframe_ctx_init(ctx->frames_ref);
  155. if (err < 0) {
  156. av_log(ctx, loglevel, "Failed to initialise VAAPI frame "
  157. "context: %d\n", err);
  158. goto fail;
  159. }
  160. ist->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
  161. if (!ist->hw_frames_ctx) {
  162. err = AVERROR(ENOMEM);
  163. goto fail;
  164. }
  165. ist->hwaccel_uninit = &vaapi_decode_uninit;
  166. ist->hwaccel_get_buffer = &vaapi_get_buffer;
  167. ist->hwaccel_retrieve_data = &vaapi_retrieve_data;
  168. return 0;
  169. fail:
  170. vaapi_decode_uninit(avctx);
  171. return err;
  172. }
  173. static AVClass *vaapi_log = &vaapi_class;
  174. av_cold int vaapi_device_init(const char *device)
  175. {
  176. int err;
  177. av_buffer_unref(&hw_device_ctx);
  178. err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
  179. device, NULL, 0);
  180. if (err < 0) {
  181. av_log(&vaapi_log, AV_LOG_ERROR, "Failed to create a VAAPI device\n");
  182. return err;
  183. }
  184. return 0;
  185. }