ffmpeg_vdpau.c 12 KB

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