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.

243 lines
7.2KB

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