ffmpeg_vdpau.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <vdpau/vdpau.h>
  20. #include <vdpau/vdpau_x11.h>
  21. #include <X11/Xlib.h>
  22. #include "ffmpeg.h"
  23. #include "libavcodec/vdpau.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/buffer.h"
  26. #include "libavutil/frame.h"
  27. #include "libavutil/hwcontext.h"
  28. #include "libavutil/hwcontext_vdpau.h"
  29. #include "libavutil/pixfmt.h"
  30. typedef struct VDPAUContext {
  31. AVBufferRef *hw_frames_ctx;
  32. AVFrame *tmp_frame;
  33. } VDPAUContext;
  34. typedef struct VDPAUHWDevicePriv {
  35. VdpDeviceDestroy *device_destroy;
  36. Display *dpy;
  37. } VDPAUHWDevicePriv;
  38. static void device_free(AVHWDeviceContext *ctx)
  39. {
  40. AVVDPAUDeviceContext *hwctx = ctx->hwctx;
  41. VDPAUHWDevicePriv *priv = ctx->user_opaque;
  42. if (priv->device_destroy)
  43. priv->device_destroy(hwctx->device);
  44. if (priv->dpy)
  45. XCloseDisplay(priv->dpy);
  46. av_freep(&priv);
  47. }
  48. static void vdpau_uninit(AVCodecContext *s)
  49. {
  50. InputStream *ist = s->opaque;
  51. VDPAUContext *ctx = ist->hwaccel_ctx;
  52. ist->hwaccel_uninit = NULL;
  53. ist->hwaccel_get_buffer = NULL;
  54. ist->hwaccel_retrieve_data = NULL;
  55. av_buffer_unref(&ctx->hw_frames_ctx);
  56. av_frame_free(&ctx->tmp_frame);
  57. av_freep(&ist->hwaccel_ctx);
  58. av_freep(&s->hwaccel_context);
  59. }
  60. static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  61. {
  62. InputStream *ist = s->opaque;
  63. VDPAUContext *ctx = ist->hwaccel_ctx;
  64. return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
  65. }
  66. static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
  67. {
  68. InputStream *ist = s->opaque;
  69. VDPAUContext *ctx = ist->hwaccel_ctx;
  70. int ret;
  71. ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
  72. if (ret < 0)
  73. return ret;
  74. ret = av_frame_copy_props(ctx->tmp_frame, frame);
  75. if (ret < 0) {
  76. av_frame_unref(ctx->tmp_frame);
  77. return ret;
  78. }
  79. av_frame_unref(frame);
  80. av_frame_move_ref(frame, ctx->tmp_frame);
  81. return 0;
  82. }
  83. static int vdpau_alloc(AVCodecContext *s)
  84. {
  85. InputStream *ist = s->opaque;
  86. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  87. VDPAUContext *ctx;
  88. const char *display, *vendor;
  89. VdpStatus err;
  90. int ret;
  91. VdpDevice device;
  92. VdpGetProcAddress *get_proc_address;
  93. VdpGetInformationString *get_information_string;
  94. VDPAUHWDevicePriv *device_priv = NULL;
  95. AVBufferRef *device_ref = NULL;
  96. AVHWDeviceContext *device_ctx;
  97. AVVDPAUDeviceContext *device_hwctx;
  98. AVHWFramesContext *frames_ctx;
  99. ctx = av_mallocz(sizeof(*ctx));
  100. if (!ctx)
  101. return AVERROR(ENOMEM);
  102. device_priv = av_mallocz(sizeof(*device_priv));
  103. if (!device_priv) {
  104. av_freep(&ctx);
  105. goto fail;
  106. }
  107. ist->hwaccel_ctx = ctx;
  108. ist->hwaccel_uninit = vdpau_uninit;
  109. ist->hwaccel_get_buffer = vdpau_get_buffer;
  110. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  111. ctx->tmp_frame = av_frame_alloc();
  112. if (!ctx->tmp_frame)
  113. goto fail;
  114. device_priv->dpy = XOpenDisplay(ist->hwaccel_device);
  115. if (!device_priv->dpy) {
  116. av_log(NULL, loglevel, "Cannot open the X11 display %s.\n",
  117. XDisplayName(ist->hwaccel_device));
  118. goto fail;
  119. }
  120. display = XDisplayString(device_priv->dpy);
  121. err = vdp_device_create_x11(device_priv->dpy, XDefaultScreen(device_priv->dpy),
  122. &device, &get_proc_address);
  123. if (err != VDP_STATUS_OK) {
  124. av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n",
  125. display);
  126. goto fail;
  127. }
  128. #define GET_CALLBACK(id, result) \
  129. do { \
  130. void *tmp; \
  131. err = get_proc_address(device, id, &tmp); \
  132. if (err != VDP_STATUS_OK) { \
  133. av_log(NULL, loglevel, "Error getting the " #id " callback.\n"); \
  134. goto fail; \
  135. } \
  136. result = tmp; \
  137. } while (0)
  138. GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
  139. GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, device_priv->device_destroy);
  140. device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VDPAU);
  141. if (!device_ref)
  142. goto fail;
  143. device_ctx = (AVHWDeviceContext*)device_ref->data;
  144. device_hwctx = device_ctx->hwctx;
  145. device_ctx->user_opaque = device_priv;
  146. device_ctx->free = device_free;
  147. device_hwctx->device = device;
  148. device_hwctx->get_proc_address = get_proc_address;
  149. device_priv = NULL;
  150. ret = av_hwdevice_ctx_init(device_ref);
  151. if (ret < 0)
  152. goto fail;
  153. ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
  154. if (!ctx->hw_frames_ctx)
  155. goto fail;
  156. av_buffer_unref(&device_ref);
  157. frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  158. frames_ctx->format = AV_PIX_FMT_VDPAU;
  159. frames_ctx->sw_format = s->sw_pix_fmt;
  160. frames_ctx->width = s->coded_width;
  161. frames_ctx->height = s->coded_height;
  162. ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
  163. if (ret < 0)
  164. goto fail;
  165. if (av_vdpau_bind_context(s, device, get_proc_address, 0))
  166. goto fail;
  167. get_information_string(&vendor);
  168. av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, "
  169. "to decode input stream #%d:%d.\n", vendor,
  170. display, ist->file_index, ist->st->index);
  171. return 0;
  172. fail:
  173. av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
  174. ist->file_index, ist->st->index);
  175. if (device_priv) {
  176. if (device_priv->device_destroy)
  177. device_priv->device_destroy(device);
  178. if (device_priv->dpy)
  179. XCloseDisplay(device_priv->dpy);
  180. }
  181. av_freep(&device_priv);
  182. av_buffer_unref(&device_ref);
  183. vdpau_uninit(s);
  184. return AVERROR(EINVAL);
  185. }
  186. int vdpau_init(AVCodecContext *s)
  187. {
  188. InputStream *ist = s->opaque;
  189. if (!ist->hwaccel_ctx) {
  190. int ret = vdpau_alloc(s);
  191. if (ret < 0)
  192. return ret;
  193. }
  194. ist->hwaccel_get_buffer = vdpau_get_buffer;
  195. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  196. return 0;
  197. }