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.

369 lines
12KB

  1. /*
  2. * Copyright (c) 2013 Jeff Moguillansky
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * XVideo output device
  23. *
  24. * TODO:
  25. * - add support to more formats
  26. */
  27. #include <X11/Xlib.h>
  28. #include <X11/extensions/Xv.h>
  29. #include <X11/extensions/XShm.h>
  30. #include <X11/extensions/Xvlib.h>
  31. #include <sys/shm.h>
  32. #include "libavutil/opt.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavformat/internal.h"
  36. #include "avdevice.h"
  37. typedef struct {
  38. AVClass *class;
  39. GC gc;
  40. Window window;
  41. int64_t window_id;
  42. char *window_title;
  43. int window_width, window_height;
  44. int window_x, window_y;
  45. int dest_x, dest_y; /**< display area position */
  46. unsigned int dest_w, dest_h; /**< display area dimensions */
  47. Display* display;
  48. char *display_name;
  49. XvImage* yuv_image;
  50. enum AVPixelFormat image_format;
  51. int image_width, image_height;
  52. XShmSegmentInfo yuv_shminfo;
  53. int xv_port;
  54. } XVContext;
  55. typedef struct XVTagFormatMap
  56. {
  57. int tag;
  58. enum AVPixelFormat format;
  59. } XVTagFormatMap;
  60. static XVTagFormatMap tag_codec_map[] = {
  61. { MKTAG('I','4','2','0'), AV_PIX_FMT_YUV420P },
  62. { MKTAG('U','Y','V','Y'), AV_PIX_FMT_UYVY422 },
  63. { MKTAG('Y','U','Y','2'), AV_PIX_FMT_YUYV422 },
  64. { 0, AV_PIX_FMT_NONE }
  65. };
  66. static int xv_get_tag_from_format(enum AVPixelFormat format)
  67. {
  68. XVTagFormatMap *m = tag_codec_map;
  69. int i;
  70. for (i = 0; m->tag; m = &tag_codec_map[++i]) {
  71. if (m->format == format)
  72. return m->tag;
  73. }
  74. return 0;
  75. }
  76. static int xv_write_trailer(AVFormatContext *s)
  77. {
  78. XVContext *xv = s->priv_data;
  79. if (xv->display) {
  80. XShmDetach(xv->display, &xv->yuv_shminfo);
  81. if (xv->yuv_image)
  82. shmdt(xv->yuv_image->data);
  83. XFree(xv->yuv_image);
  84. if (xv->gc)
  85. XFreeGC(xv->display, xv->gc);
  86. XCloseDisplay(xv->display);
  87. }
  88. return 0;
  89. }
  90. static int xv_write_header(AVFormatContext *s)
  91. {
  92. XVContext *xv = s->priv_data;
  93. unsigned int num_adaptors;
  94. XvAdaptorInfo *ai;
  95. XvImageFormatValues *fv;
  96. XColor fgcolor;
  97. XWindowAttributes window_attrs;
  98. int num_formats = 0, j, tag, ret;
  99. AVCodecContext *encctx = s->streams[0]->codec;
  100. if ( s->nb_streams > 1
  101. || encctx->codec_type != AVMEDIA_TYPE_VIDEO
  102. || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
  103. av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
  104. return AVERROR(EINVAL);
  105. }
  106. if (!(tag = xv_get_tag_from_format(encctx->pix_fmt))) {
  107. av_log(s, AV_LOG_ERROR,
  108. "Unsupported pixel format '%s', only yuv420p, uyvy422, yuyv422 are currently supported\n",
  109. av_get_pix_fmt_name(encctx->pix_fmt));
  110. return AVERROR_PATCHWELCOME;
  111. }
  112. xv->image_format = encctx->pix_fmt;
  113. xv->display = XOpenDisplay(xv->display_name);
  114. if (!xv->display) {
  115. av_log(s, AV_LOG_ERROR, "Could not open the X11 display '%s'\n", xv->display_name);
  116. return AVERROR(EINVAL);
  117. }
  118. xv->image_width = encctx->width;
  119. xv->image_height = encctx->height;
  120. if (!xv->window_width && !xv->window_height) {
  121. AVRational sar = encctx->sample_aspect_ratio;
  122. xv->window_width = encctx->width;
  123. xv->window_height = encctx->height;
  124. if (sar.num) {
  125. if (sar.num > sar.den)
  126. xv->window_width = av_rescale(xv->window_width, sar.num, sar.den);
  127. if (sar.num < sar.den)
  128. xv->window_height = av_rescale(xv->window_height, sar.den, sar.num);
  129. }
  130. }
  131. if (!xv->window_id) {
  132. //TODO: reident
  133. xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display),
  134. xv->window_x, xv->window_y,
  135. xv->window_width, xv->window_height,
  136. 0, 0, 0);
  137. if (!xv->window_title) {
  138. if (!(xv->window_title = av_strdup(s->filename))) {
  139. ret = AVERROR(ENOMEM);
  140. goto fail;
  141. }
  142. }
  143. XStoreName(xv->display, xv->window, xv->window_title);
  144. XMapWindow(xv->display, xv->window);
  145. } else
  146. xv->window = xv->window_id;
  147. if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) {
  148. ret = AVERROR_EXTERNAL;
  149. goto fail;
  150. }
  151. if (!num_adaptors) {
  152. av_log(s, AV_LOG_ERROR, "No X-Video adaptors present\n");
  153. return AVERROR(ENODEV);
  154. }
  155. xv->xv_port = ai[0].base_id;
  156. XvFreeAdaptorInfo(ai);
  157. fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
  158. if (!fv) {
  159. ret = AVERROR_EXTERNAL;
  160. goto fail;
  161. }
  162. for (j = 0; j < num_formats; j++) {
  163. if (fv[j].id == tag) {
  164. break;
  165. }
  166. }
  167. XFree(fv);
  168. if (j >= num_formats) {
  169. av_log(s, AV_LOG_ERROR,
  170. "Device does not support pixel format %s, aborting\n",
  171. av_get_pix_fmt_name(encctx->pix_fmt));
  172. ret = AVERROR(EINVAL);
  173. goto fail;
  174. }
  175. xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
  176. xv->image_width = encctx->width;
  177. xv->image_height = encctx->height;
  178. xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port, tag, 0,
  179. xv->image_width, xv->image_height, &xv->yuv_shminfo);
  180. xv->yuv_shminfo.shmid = shmget(IPC_PRIVATE, xv->yuv_image->data_size,
  181. IPC_CREAT | 0777);
  182. xv->yuv_shminfo.shmaddr = (char *)shmat(xv->yuv_shminfo.shmid, 0, 0);
  183. xv->yuv_image->data = xv->yuv_shminfo.shmaddr;
  184. xv->yuv_shminfo.readOnly = False;
  185. XShmAttach(xv->display, &xv->yuv_shminfo);
  186. XSync(xv->display, False);
  187. shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0);
  188. XGetWindowAttributes(xv->display, xv->window, &window_attrs);
  189. fgcolor.red = fgcolor.green = fgcolor.blue = 0;
  190. fgcolor.flags = DoRed | DoGreen | DoBlue;
  191. XAllocColor(xv->display, window_attrs.colormap, &fgcolor);
  192. XSetForeground(xv->display, xv->gc, fgcolor.pixel);
  193. //force display area recalculation at first frame
  194. xv->window_width = xv->window_height = 0;
  195. return 0;
  196. fail:
  197. xv_write_trailer(s);
  198. return ret;
  199. }
  200. static void compute_display_area(AVFormatContext *s)
  201. {
  202. XVContext *xv = s->priv_data;
  203. AVRational sar, dar; /* sample and display aspect ratios */
  204. AVStream *st = s->streams[0];
  205. AVCodecContext *encctx = st->codec;
  206. /* compute overlay width and height from the codec context information */
  207. sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
  208. dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
  209. /* we suppose the screen has a 1/1 sample aspect ratio */
  210. /* fit in the window */
  211. if (av_cmp_q(dar, (AVRational){ xv->dest_w, xv->dest_h }) > 0) {
  212. /* fit in width */
  213. xv->dest_y = xv->dest_h;
  214. xv->dest_x = 0;
  215. xv->dest_h = av_rescale(xv->dest_w, dar.den, dar.num);
  216. xv->dest_y -= xv->dest_h;
  217. xv->dest_y /= 2;
  218. } else {
  219. /* fit in height */
  220. xv->dest_x = xv->dest_w;
  221. xv->dest_y = 0;
  222. xv->dest_w = av_rescale(xv->dest_h, dar.num, dar.den);
  223. xv->dest_x -= xv->dest_w;
  224. xv->dest_x /= 2;
  225. }
  226. }
  227. static int xv_repaint(AVFormatContext *s)
  228. {
  229. XVContext *xv = s->priv_data;
  230. XWindowAttributes window_attrs;
  231. XGetWindowAttributes(xv->display, xv->window, &window_attrs);
  232. if (window_attrs.width != xv->window_width || window_attrs.height != xv->window_height) {
  233. XRectangle rect[2];
  234. xv->dest_w = window_attrs.width;
  235. xv->dest_h = window_attrs.height;
  236. compute_display_area(s);
  237. if (xv->dest_x) {
  238. rect[0].width = rect[1].width = xv->dest_x;
  239. rect[0].height = rect[1].height = window_attrs.height;
  240. rect[0].y = rect[1].y = 0;
  241. rect[0].x = 0;
  242. rect[1].x = xv->dest_w + xv->dest_x;
  243. XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
  244. }
  245. if (xv->dest_y) {
  246. rect[0].width = rect[1].width = window_attrs.width;
  247. rect[0].height = rect[1].height = xv->dest_y;
  248. rect[0].x = rect[1].x = 0;
  249. rect[0].y = 0;
  250. rect[1].y = xv->dest_h + xv->dest_y;
  251. XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
  252. }
  253. }
  254. if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
  255. xv->yuv_image, 0, 0, xv->image_width, xv->image_height,
  256. xv->dest_x, xv->dest_y, xv->dest_w, xv->dest_h, True) != Success) {
  257. av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n");
  258. return AVERROR_EXTERNAL;
  259. }
  260. return 0;
  261. }
  262. static int write_picture(AVFormatContext *s, AVPicture *pict)
  263. {
  264. XVContext *xv = s->priv_data;
  265. XvImage *img = xv->yuv_image;
  266. uint8_t *data[3] = {
  267. img->data + img->offsets[0],
  268. img->data + img->offsets[1],
  269. img->data + img->offsets[2]
  270. };
  271. av_image_copy(data, img->pitches, (const uint8_t **)pict->data, pict->linesize,
  272. xv->image_format, img->width, img->height);
  273. return xv_repaint(s);
  274. }
  275. static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
  276. {
  277. AVPicture pict;
  278. AVCodecContext *ctx = s->streams[0]->codec;
  279. avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height);
  280. return write_picture(s, &pict);
  281. }
  282. static int xv_write_frame(AVFormatContext *s, int stream_index, AVFrame **frame,
  283. unsigned flags)
  284. {
  285. /* xv_write_header() should have accepted only supported formats */
  286. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  287. return 0;
  288. return write_picture(s, (AVPicture *)*frame);
  289. }
  290. static int xv_control_message(AVFormatContext *s, int type, void *data, size_t data_size)
  291. {
  292. switch(type) {
  293. case AV_APP_TO_DEV_WINDOW_REPAINT:
  294. return xv_repaint(s);
  295. default:
  296. break;
  297. }
  298. return AVERROR(ENOSYS);
  299. }
  300. #define OFFSET(x) offsetof(XVContext, x)
  301. static const AVOption options[] = {
  302. { "display_name", "set display name", OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  303. { "window_id", "set existing window id", OFFSET(window_id), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  304. { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  305. { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  306. { "window_x", "set window x offset", OFFSET(window_x), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  307. { "window_y", "set window y offset", OFFSET(window_y), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  308. { NULL }
  309. };
  310. static const AVClass xv_class = {
  311. .class_name = "xvideo outdev",
  312. .item_name = av_default_item_name,
  313. .option = options,
  314. .version = LIBAVUTIL_VERSION_INT,
  315. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  316. };
  317. AVOutputFormat ff_xv_muxer = {
  318. .name = "xv",
  319. .long_name = NULL_IF_CONFIG_SMALL("XV (XVideo) output device"),
  320. .priv_data_size = sizeof(XVContext),
  321. .audio_codec = AV_CODEC_ID_NONE,
  322. .video_codec = AV_CODEC_ID_RAWVIDEO,
  323. .write_header = xv_write_header,
  324. .write_packet = xv_write_packet,
  325. .write_uncoded_frame = xv_write_frame,
  326. .write_trailer = xv_write_trailer,
  327. .control_message = xv_control_message,
  328. .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  329. .priv_class = &xv_class,
  330. };