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.

241 lines
7.2KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 "avconv.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. goto fail;
  104. ist->hwaccel_ctx = ctx;
  105. ist->hwaccel_uninit = vdpau_uninit;
  106. ist->hwaccel_get_buffer = vdpau_get_buffer;
  107. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  108. ctx->tmp_frame = av_frame_alloc();
  109. if (!ctx->tmp_frame)
  110. goto fail;
  111. device_priv->dpy = XOpenDisplay(ist->hwaccel_device);
  112. if (!device_priv->dpy) {
  113. av_log(NULL, loglevel, "Cannot open the X11 display %s.\n",
  114. XDisplayName(ist->hwaccel_device));
  115. goto fail;
  116. }
  117. display = XDisplayString(device_priv->dpy);
  118. err = vdp_device_create_x11(device_priv->dpy, XDefaultScreen(device_priv->dpy),
  119. &device, &get_proc_address);
  120. if (err != VDP_STATUS_OK) {
  121. av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n",
  122. display);
  123. goto fail;
  124. }
  125. #define GET_CALLBACK(id, result) \
  126. do { \
  127. void *tmp; \
  128. err = get_proc_address(device, id, &tmp); \
  129. if (err != VDP_STATUS_OK) { \
  130. av_log(NULL, loglevel, "Error getting the " #id " callback.\n"); \
  131. goto fail; \
  132. } \
  133. result = tmp; \
  134. } while (0)
  135. GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
  136. GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, device_priv->device_destroy);
  137. device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VDPAU);
  138. if (!device_ref)
  139. goto fail;
  140. device_ctx = (AVHWDeviceContext*)device_ref->data;
  141. device_hwctx = device_ctx->hwctx;
  142. device_ctx->user_opaque = device_priv;
  143. device_ctx->free = device_free;
  144. device_hwctx->device = device;
  145. device_hwctx->get_proc_address = get_proc_address;
  146. device_priv = NULL;
  147. ret = av_hwdevice_ctx_init(device_ref);
  148. if (ret < 0)
  149. goto fail;
  150. ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
  151. if (!ctx->hw_frames_ctx)
  152. goto fail;
  153. av_buffer_unref(&device_ref);
  154. frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
  155. frames_ctx->format = AV_PIX_FMT_VDPAU;
  156. frames_ctx->sw_format = s->sw_pix_fmt;
  157. frames_ctx->width = s->coded_width;
  158. frames_ctx->height = s->coded_height;
  159. ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
  160. if (ret < 0)
  161. goto fail;
  162. if (av_vdpau_bind_context(s, device, get_proc_address, 0))
  163. goto fail;
  164. get_information_string(&vendor);
  165. av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, "
  166. "to decode input stream #%d:%d.\n", vendor,
  167. display, ist->file_index, ist->st->index);
  168. return 0;
  169. fail:
  170. av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
  171. ist->file_index, ist->st->index);
  172. if (device_priv) {
  173. if (device_priv->device_destroy)
  174. device_priv->device_destroy(device);
  175. if (device_priv->dpy)
  176. XCloseDisplay(device_priv->dpy);
  177. }
  178. av_freep(&device_priv);
  179. av_buffer_unref(&device_ref);
  180. vdpau_uninit(s);
  181. return AVERROR(EINVAL);
  182. }
  183. int vdpau_init(AVCodecContext *s)
  184. {
  185. InputStream *ist = s->opaque;
  186. if (!ist->hwaccel_ctx) {
  187. int ret = vdpau_alloc(s);
  188. if (ret < 0)
  189. return ret;
  190. }
  191. ist->hwaccel_get_buffer = vdpau_get_buffer;
  192. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  193. return 0;
  194. }