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.

300 lines
9.9KB

  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/avassert.h"
  25. #include "libavutil/buffer.h"
  26. #include "libavutil/frame.h"
  27. #include "libavutil/pixfmt.h"
  28. typedef struct VDPAUContext {
  29. Display *dpy;
  30. VdpDevice device;
  31. VdpDecoder decoder;
  32. VdpGetProcAddress *get_proc_address;
  33. VdpGetErrorString *get_error_string;
  34. VdpGetInformationString *get_information_string;
  35. VdpDeviceDestroy *device_destroy;
  36. VdpVideoSurfaceCreate *video_surface_create;
  37. VdpVideoSurfaceDestroy *video_surface_destroy;
  38. VdpVideoSurfaceGetBitsYCbCr *video_surface_get_bits;
  39. VdpVideoSurfaceGetParameters *video_surface_get_parameters;
  40. VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *video_surface_query;
  41. AVFrame *tmp_frame;
  42. enum AVPixelFormat pix_fmt;
  43. VdpYCbCrFormat vdpau_format;
  44. } VDPAUContext;
  45. static void vdpau_uninit(AVCodecContext *s)
  46. {
  47. InputStream *ist = s->opaque;
  48. VDPAUContext *ctx = ist->hwaccel_ctx;
  49. ist->hwaccel_uninit = NULL;
  50. ist->hwaccel_get_buffer = NULL;
  51. ist->hwaccel_retrieve_data = NULL;
  52. if (ctx->device_destroy)
  53. ctx->device_destroy(ctx->device);
  54. if (ctx->dpy)
  55. XCloseDisplay(ctx->dpy);
  56. av_frame_free(&ctx->tmp_frame);
  57. av_freep(&ist->hwaccel_ctx);
  58. av_freep(&s->hwaccel_context);
  59. }
  60. static void vdpau_release_buffer(void *opaque, uint8_t *data)
  61. {
  62. VdpVideoSurface surface = *(VdpVideoSurface*)data;
  63. VDPAUContext *ctx = opaque;
  64. ctx->video_surface_destroy(surface);
  65. av_freep(&data);
  66. }
  67. static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  68. {
  69. InputStream *ist = s->opaque;
  70. VDPAUContext *ctx = ist->hwaccel_ctx;
  71. VdpVideoSurface *surface;
  72. VdpStatus err;
  73. VdpChromaType chroma;
  74. uint32_t width, height;
  75. av_assert0(frame->format == AV_PIX_FMT_VDPAU);
  76. if (av_vdpau_get_surface_parameters(s, &chroma, &width, &height))
  77. return AVERROR(ENOSYS);
  78. surface = av_malloc(sizeof(*surface));
  79. if (!surface)
  80. return AVERROR(ENOMEM);
  81. frame->buf[0] = av_buffer_create((uint8_t*)surface, sizeof(*surface),
  82. vdpau_release_buffer, ctx,
  83. AV_BUFFER_FLAG_READONLY);
  84. if (!frame->buf[0]) {
  85. av_freep(&surface);
  86. return AVERROR(ENOMEM);
  87. }
  88. // properly we should keep a pool of surfaces instead of creating
  89. // them anew for each frame, but since we don't care about speed
  90. // much in this code, we don't bother
  91. err = ctx->video_surface_create(ctx->device, chroma, width, height,
  92. surface);
  93. if (err != VDP_STATUS_OK) {
  94. av_log(NULL, AV_LOG_ERROR, "Error allocating a VDPAU video surface: %s\n",
  95. ctx->get_error_string(err));
  96. av_buffer_unref(&frame->buf[0]);
  97. return AVERROR_UNKNOWN;
  98. }
  99. frame->data[3] = (uint8_t*)(uintptr_t)*surface;
  100. return 0;
  101. }
  102. static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
  103. {
  104. VdpVideoSurface surface = (VdpVideoSurface)(uintptr_t)frame->data[3];
  105. InputStream *ist = s->opaque;
  106. VDPAUContext *ctx = ist->hwaccel_ctx;
  107. VdpStatus err;
  108. int ret, chroma_type;
  109. err = ctx->video_surface_get_parameters(surface, &chroma_type,
  110. &ctx->tmp_frame->width,
  111. &ctx->tmp_frame->height);
  112. if (err != VDP_STATUS_OK) {
  113. av_log(NULL, AV_LOG_ERROR, "Error getting surface parameters: %s\n",
  114. ctx->get_error_string(err));
  115. return AVERROR_UNKNOWN;
  116. }
  117. ctx->tmp_frame->format = ctx->pix_fmt;
  118. ret = av_frame_get_buffer(ctx->tmp_frame, 32);
  119. if (ret < 0)
  120. return ret;
  121. ctx->tmp_frame->width = frame->width;
  122. ctx->tmp_frame->height = frame->height;
  123. err = ctx->video_surface_get_bits(surface, ctx->vdpau_format,
  124. (void * const *)ctx->tmp_frame->data,
  125. ctx->tmp_frame->linesize);
  126. if (err != VDP_STATUS_OK) {
  127. av_log(NULL, AV_LOG_ERROR, "Error retrieving frame data from VDPAU: %s\n",
  128. ctx->get_error_string(err));
  129. ret = AVERROR_UNKNOWN;
  130. goto fail;
  131. }
  132. if (ctx->vdpau_format == VDP_YCBCR_FORMAT_YV12)
  133. FFSWAP(uint8_t*, ctx->tmp_frame->data[1], ctx->tmp_frame->data[2]);
  134. ret = av_frame_copy_props(ctx->tmp_frame, frame);
  135. if (ret < 0)
  136. goto fail;
  137. av_frame_unref(frame);
  138. av_frame_move_ref(frame, ctx->tmp_frame);
  139. return 0;
  140. fail:
  141. av_frame_unref(ctx->tmp_frame);
  142. return ret;
  143. }
  144. static const int vdpau_formats[][2] = {
  145. { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P },
  146. { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 },
  147. { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 },
  148. { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 },
  149. };
  150. static int vdpau_alloc(AVCodecContext *s)
  151. {
  152. InputStream *ist = s->opaque;
  153. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  154. VDPAUContext *ctx;
  155. const char *display, *vendor;
  156. VdpStatus err;
  157. int i;
  158. ctx = av_mallocz(sizeof(*ctx));
  159. if (!ctx)
  160. return AVERROR(ENOMEM);
  161. ist->hwaccel_ctx = ctx;
  162. ist->hwaccel_uninit = vdpau_uninit;
  163. ist->hwaccel_get_buffer = vdpau_get_buffer;
  164. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  165. ctx->tmp_frame = av_frame_alloc();
  166. if (!ctx->tmp_frame)
  167. goto fail;
  168. ctx->dpy = XOpenDisplay(ist->hwaccel_device);
  169. if (!ctx->dpy) {
  170. av_log(NULL, loglevel, "Cannot open the X11 display %s.\n",
  171. XDisplayName(ist->hwaccel_device));
  172. goto fail;
  173. }
  174. display = XDisplayString(ctx->dpy);
  175. err = vdp_device_create_x11(ctx->dpy, XDefaultScreen(ctx->dpy), &ctx->device,
  176. &ctx->get_proc_address);
  177. if (err != VDP_STATUS_OK) {
  178. av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n",
  179. display);
  180. goto fail;
  181. }
  182. #define GET_CALLBACK(id, result) \
  183. do { \
  184. void *tmp; \
  185. err = ctx->get_proc_address(ctx->device, id, &tmp); \
  186. if (err != VDP_STATUS_OK) { \
  187. av_log(NULL, loglevel, "Error getting the " #id " callback.\n"); \
  188. goto fail; \
  189. } \
  190. ctx->result = tmp; \
  191. } while (0)
  192. GET_CALLBACK(VDP_FUNC_ID_GET_ERROR_STRING, get_error_string);
  193. GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
  194. GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, device_destroy);
  195. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, video_surface_create);
  196. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, video_surface_destroy);
  197. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, video_surface_get_bits);
  198. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_PARAMETERS, video_surface_get_parameters);
  199. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
  200. video_surface_query);
  201. for (i = 0; i < FF_ARRAY_ELEMS(vdpau_formats); i++) {
  202. VdpBool supported;
  203. err = ctx->video_surface_query(ctx->device, VDP_CHROMA_TYPE_420,
  204. vdpau_formats[i][0], &supported);
  205. if (err != VDP_STATUS_OK) {
  206. av_log(NULL, loglevel,
  207. "Error querying VDPAU surface capabilities: %s\n",
  208. ctx->get_error_string(err));
  209. goto fail;
  210. }
  211. if (supported)
  212. break;
  213. }
  214. if (i == FF_ARRAY_ELEMS(vdpau_formats)) {
  215. av_log(NULL, loglevel,
  216. "No supported VDPAU format for retrieving the data.\n");
  217. return AVERROR(EINVAL);
  218. }
  219. ctx->vdpau_format = vdpau_formats[i][0];
  220. ctx->pix_fmt = vdpau_formats[i][1];
  221. if (av_vdpau_bind_context(s, ctx->device, ctx->get_proc_address, 0))
  222. goto fail;
  223. ctx->get_information_string(&vendor);
  224. av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, "
  225. "to decode input stream #%d:%d.\n", vendor,
  226. display, ist->file_index, ist->st->index);
  227. return 0;
  228. fail:
  229. av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
  230. ist->file_index, ist->st->index);
  231. vdpau_uninit(s);
  232. return AVERROR(EINVAL);
  233. }
  234. int vdpau_init(AVCodecContext *s)
  235. {
  236. InputStream *ist = s->opaque;
  237. if (!ist->hwaccel_ctx) {
  238. int ret = vdpau_alloc(s);
  239. if (ret < 0)
  240. return ret;
  241. }
  242. ist->hwaccel_get_buffer = vdpau_get_buffer;
  243. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  244. return 0;
  245. }