ffmpeg_vdpau.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/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. #if 1 // for ffmpegs older vdpau API, not the oldest though
  37. VdpDecoderCreate *decoder_create;
  38. VdpDecoderDestroy *decoder_destroy;
  39. VdpDecoderRender *decoder_render;
  40. #endif
  41. VdpVideoSurfaceCreate *video_surface_create;
  42. VdpVideoSurfaceDestroy *video_surface_destroy;
  43. VdpVideoSurfaceGetBitsYCbCr *video_surface_get_bits;
  44. VdpVideoSurfaceGetParameters *video_surface_get_parameters;
  45. VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *video_surface_query;
  46. AVFrame *tmp_frame;
  47. enum AVPixelFormat pix_fmt;
  48. VdpYCbCrFormat vdpau_format;
  49. } VDPAUContext;
  50. int vdpau_api_ver = 2;
  51. static void vdpau_uninit(AVCodecContext *s)
  52. {
  53. InputStream *ist = s->opaque;
  54. VDPAUContext *ctx = ist->hwaccel_ctx;
  55. ist->hwaccel_uninit = NULL;
  56. ist->hwaccel_get_buffer = NULL;
  57. ist->hwaccel_retrieve_data = NULL;
  58. if (ctx->decoder_destroy)
  59. ctx->decoder_destroy(ctx->decoder);
  60. if (ctx->device_destroy)
  61. ctx->device_destroy(ctx->device);
  62. if (ctx->dpy)
  63. XCloseDisplay(ctx->dpy);
  64. av_frame_free(&ctx->tmp_frame);
  65. av_freep(&ist->hwaccel_ctx);
  66. av_freep(&s->hwaccel_context);
  67. }
  68. static void vdpau_release_buffer(void *opaque, uint8_t *data)
  69. {
  70. VdpVideoSurface surface = *(VdpVideoSurface*)data;
  71. VDPAUContext *ctx = opaque;
  72. ctx->video_surface_destroy(surface);
  73. av_freep(&data);
  74. }
  75. static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  76. {
  77. InputStream *ist = s->opaque;
  78. VDPAUContext *ctx = ist->hwaccel_ctx;
  79. VdpVideoSurface *surface;
  80. VdpStatus err;
  81. av_assert0(frame->format == AV_PIX_FMT_VDPAU);
  82. surface = av_malloc(sizeof(*surface));
  83. if (!surface)
  84. return AVERROR(ENOMEM);
  85. frame->buf[0] = av_buffer_create((uint8_t*)surface, sizeof(*surface),
  86. vdpau_release_buffer, ctx,
  87. AV_BUFFER_FLAG_READONLY);
  88. if (!frame->buf[0]) {
  89. av_freep(&surface);
  90. return AVERROR(ENOMEM);
  91. }
  92. // properly we should keep a pool of surfaces instead of creating
  93. // them anew for each frame, but since we don't care about speed
  94. // much in this code, we don't bother
  95. err = ctx->video_surface_create(ctx->device, VDP_CHROMA_TYPE_420,
  96. frame->width, frame->height, surface);
  97. if (err != VDP_STATUS_OK) {
  98. av_log(NULL, AV_LOG_ERROR, "Error allocating a VDPAU video surface: %s\n",
  99. ctx->get_error_string(err));
  100. av_buffer_unref(&frame->buf[0]);
  101. return AVERROR_UNKNOWN;
  102. }
  103. frame->data[3] = (uint8_t*)(uintptr_t)*surface;
  104. return 0;
  105. }
  106. static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
  107. {
  108. VdpVideoSurface surface = (VdpVideoSurface)(uintptr_t)frame->data[3];
  109. InputStream *ist = s->opaque;
  110. VDPAUContext *ctx = ist->hwaccel_ctx;
  111. VdpStatus err;
  112. int ret, chroma_type;
  113. err = ctx->video_surface_get_parameters(surface, &chroma_type,
  114. &ctx->tmp_frame->width,
  115. &ctx->tmp_frame->height);
  116. if (err != VDP_STATUS_OK) {
  117. av_log(NULL, AV_LOG_ERROR, "Error getting surface parameters: %s\n",
  118. ctx->get_error_string(err));
  119. return AVERROR_UNKNOWN;
  120. }
  121. ctx->tmp_frame->format = ctx->pix_fmt;
  122. ret = av_frame_get_buffer(ctx->tmp_frame, 32);
  123. if (ret < 0)
  124. return ret;
  125. ctx->tmp_frame->width = frame->width;
  126. ctx->tmp_frame->height = frame->height;
  127. err = ctx->video_surface_get_bits(surface, ctx->vdpau_format,
  128. (void * const *)ctx->tmp_frame->data,
  129. ctx->tmp_frame->linesize);
  130. if (err != VDP_STATUS_OK) {
  131. av_log(NULL, AV_LOG_ERROR, "Error retrieving frame data from VDPAU: %s\n",
  132. ctx->get_error_string(err));
  133. ret = AVERROR_UNKNOWN;
  134. goto fail;
  135. }
  136. if (ctx->vdpau_format == VDP_YCBCR_FORMAT_YV12)
  137. FFSWAP(uint8_t*, ctx->tmp_frame->data[1], ctx->tmp_frame->data[2]);
  138. ret = av_frame_copy_props(ctx->tmp_frame, frame);
  139. if (ret < 0)
  140. goto fail;
  141. av_frame_unref(frame);
  142. av_frame_move_ref(frame, ctx->tmp_frame);
  143. return 0;
  144. fail:
  145. av_frame_unref(ctx->tmp_frame);
  146. return ret;
  147. }
  148. static const int vdpau_formats[][2] = {
  149. { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P },
  150. { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 },
  151. { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 },
  152. { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 },
  153. };
  154. static int vdpau_alloc(AVCodecContext *s)
  155. {
  156. InputStream *ist = s->opaque;
  157. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  158. AVVDPAUContext *vdpau_ctx;
  159. VDPAUContext *ctx;
  160. const char *display, *vendor;
  161. VdpStatus err;
  162. int i;
  163. ctx = av_mallocz(sizeof(*ctx));
  164. if (!ctx)
  165. return AVERROR(ENOMEM);
  166. ist->hwaccel_ctx = ctx;
  167. ist->hwaccel_uninit = vdpau_uninit;
  168. ist->hwaccel_get_buffer = vdpau_get_buffer;
  169. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  170. ctx->tmp_frame = av_frame_alloc();
  171. if (!ctx->tmp_frame)
  172. goto fail;
  173. ctx->dpy = XOpenDisplay(ist->hwaccel_device);
  174. if (!ctx->dpy) {
  175. av_log(NULL, loglevel, "Cannot open the X11 display %s.\n",
  176. XDisplayName(ist->hwaccel_device));
  177. goto fail;
  178. }
  179. display = XDisplayString(ctx->dpy);
  180. err = vdp_device_create_x11(ctx->dpy, XDefaultScreen(ctx->dpy), &ctx->device,
  181. &ctx->get_proc_address);
  182. if (err != VDP_STATUS_OK) {
  183. av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n",
  184. display);
  185. goto fail;
  186. }
  187. #define GET_CALLBACK(id, result) \
  188. do { \
  189. void *tmp; \
  190. err = ctx->get_proc_address(ctx->device, id, &tmp); \
  191. if (err != VDP_STATUS_OK) { \
  192. av_log(NULL, loglevel, "Error getting the " #id " callback.\n"); \
  193. goto fail; \
  194. } \
  195. ctx->result = tmp; \
  196. } while (0)
  197. GET_CALLBACK(VDP_FUNC_ID_GET_ERROR_STRING, get_error_string);
  198. GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
  199. GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, device_destroy);
  200. if (vdpau_api_ver == 1) {
  201. GET_CALLBACK(VDP_FUNC_ID_DECODER_CREATE, decoder_create);
  202. GET_CALLBACK(VDP_FUNC_ID_DECODER_DESTROY, decoder_destroy);
  203. GET_CALLBACK(VDP_FUNC_ID_DECODER_RENDER, decoder_render);
  204. }
  205. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, video_surface_create);
  206. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, video_surface_destroy);
  207. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, video_surface_get_bits);
  208. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_PARAMETERS, video_surface_get_parameters);
  209. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
  210. video_surface_query);
  211. for (i = 0; i < FF_ARRAY_ELEMS(vdpau_formats); i++) {
  212. VdpBool supported;
  213. err = ctx->video_surface_query(ctx->device, VDP_CHROMA_TYPE_420,
  214. vdpau_formats[i][0], &supported);
  215. if (err != VDP_STATUS_OK) {
  216. av_log(NULL, loglevel,
  217. "Error querying VDPAU surface capabilities: %s\n",
  218. ctx->get_error_string(err));
  219. goto fail;
  220. }
  221. if (supported)
  222. break;
  223. }
  224. if (i == FF_ARRAY_ELEMS(vdpau_formats)) {
  225. av_log(NULL, loglevel,
  226. "No supported VDPAU format for retrieving the data.\n");
  227. return AVERROR(EINVAL);
  228. }
  229. ctx->vdpau_format = vdpau_formats[i][0];
  230. ctx->pix_fmt = vdpau_formats[i][1];
  231. if (vdpau_api_ver == 1) {
  232. vdpau_ctx = av_vdpau_alloc_context();
  233. if (!vdpau_ctx)
  234. goto fail;
  235. vdpau_ctx->render = ctx->decoder_render;
  236. s->hwaccel_context = vdpau_ctx;
  237. } else
  238. if (av_vdpau_bind_context(s, ctx->device, ctx->get_proc_address, 0))
  239. goto fail;
  240. ctx->get_information_string(&vendor);
  241. av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, "
  242. "to decode input stream #%d:%d.\n", vendor,
  243. display, ist->file_index, ist->st->index);
  244. return 0;
  245. fail:
  246. av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
  247. ist->file_index, ist->st->index);
  248. vdpau_uninit(s);
  249. return AVERROR(EINVAL);
  250. }
  251. static int vdpau_old_init(AVCodecContext *s)
  252. {
  253. InputStream *ist = s->opaque;
  254. int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  255. AVVDPAUContext *vdpau_ctx;
  256. VDPAUContext *ctx;
  257. VdpStatus err;
  258. int profile, ret;
  259. if (!ist->hwaccel_ctx) {
  260. ret = vdpau_alloc(s);
  261. if (ret < 0)
  262. return ret;
  263. }
  264. ctx = ist->hwaccel_ctx;
  265. vdpau_ctx = s->hwaccel_context;
  266. ret = av_vdpau_get_profile(s, &profile);
  267. if (ret < 0) {
  268. av_log(NULL, loglevel, "No known VDPAU decoder profile for this stream.\n");
  269. return AVERROR(EINVAL);
  270. }
  271. if (ctx->decoder)
  272. ctx->decoder_destroy(ctx->decoder);
  273. err = ctx->decoder_create(ctx->device, profile,
  274. s->coded_width, s->coded_height,
  275. 16, &ctx->decoder);
  276. if (err != VDP_STATUS_OK) {
  277. av_log(NULL, loglevel, "Error creating the VDPAU decoder: %s\n",
  278. ctx->get_error_string(err));
  279. return AVERROR_UNKNOWN;
  280. }
  281. vdpau_ctx->decoder = ctx->decoder;
  282. ist->hwaccel_get_buffer = vdpau_get_buffer;
  283. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  284. return 0;
  285. }
  286. int vdpau_init(AVCodecContext *s)
  287. {
  288. InputStream *ist = s->opaque;
  289. if (vdpau_api_ver == 1)
  290. return vdpau_old_init(s);
  291. if (!ist->hwaccel_ctx) {
  292. int ret = vdpau_alloc(s);
  293. if (ret < 0)
  294. return ret;
  295. }
  296. ist->hwaccel_get_buffer = vdpau_get_buffer;
  297. ist->hwaccel_retrieve_data = vdpau_retrieve_data;
  298. return 0;
  299. }