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.

368 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. xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display),
  133. xv->window_x, xv->window_y,
  134. xv->window_width, xv->window_height,
  135. 0, 0, 0);
  136. if (!xv->window_title) {
  137. if (!(xv->window_title = av_strdup(s->filename))) {
  138. ret = AVERROR(ENOMEM);
  139. goto fail;
  140. }
  141. }
  142. XStoreName(xv->display, xv->window, xv->window_title);
  143. XMapWindow(xv->display, xv->window);
  144. } else
  145. xv->window = xv->window_id;
  146. if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) {
  147. ret = AVERROR_EXTERNAL;
  148. goto fail;
  149. }
  150. if (!num_adaptors) {
  151. av_log(s, AV_LOG_ERROR, "No X-Video adaptors present\n");
  152. return AVERROR(ENODEV);
  153. }
  154. xv->xv_port = ai[0].base_id;
  155. XvFreeAdaptorInfo(ai);
  156. fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
  157. if (!fv) {
  158. ret = AVERROR_EXTERNAL;
  159. goto fail;
  160. }
  161. for (j = 0; j < num_formats; j++) {
  162. if (fv[j].id == tag) {
  163. break;
  164. }
  165. }
  166. XFree(fv);
  167. if (j >= num_formats) {
  168. av_log(s, AV_LOG_ERROR,
  169. "Device does not support pixel format %s, aborting\n",
  170. av_get_pix_fmt_name(encctx->pix_fmt));
  171. ret = AVERROR(EINVAL);
  172. goto fail;
  173. }
  174. xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
  175. xv->image_width = encctx->width;
  176. xv->image_height = encctx->height;
  177. xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port, tag, 0,
  178. xv->image_width, xv->image_height, &xv->yuv_shminfo);
  179. xv->yuv_shminfo.shmid = shmget(IPC_PRIVATE, xv->yuv_image->data_size,
  180. IPC_CREAT | 0777);
  181. xv->yuv_shminfo.shmaddr = (char *)shmat(xv->yuv_shminfo.shmid, 0, 0);
  182. xv->yuv_image->data = xv->yuv_shminfo.shmaddr;
  183. xv->yuv_shminfo.readOnly = False;
  184. XShmAttach(xv->display, &xv->yuv_shminfo);
  185. XSync(xv->display, False);
  186. shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0);
  187. XGetWindowAttributes(xv->display, xv->window, &window_attrs);
  188. fgcolor.red = fgcolor.green = fgcolor.blue = 0;
  189. fgcolor.flags = DoRed | DoGreen | DoBlue;
  190. XAllocColor(xv->display, window_attrs.colormap, &fgcolor);
  191. XSetForeground(xv->display, xv->gc, fgcolor.pixel);
  192. //force display area recalculation at first frame
  193. xv->window_width = xv->window_height = 0;
  194. return 0;
  195. fail:
  196. xv_write_trailer(s);
  197. return ret;
  198. }
  199. static void compute_display_area(AVFormatContext *s)
  200. {
  201. XVContext *xv = s->priv_data;
  202. AVRational sar, dar; /* sample and display aspect ratios */
  203. AVStream *st = s->streams[0];
  204. AVCodecContext *encctx = st->codec;
  205. /* compute overlay width and height from the codec context information */
  206. sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
  207. dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
  208. /* we suppose the screen has a 1/1 sample aspect ratio */
  209. /* fit in the window */
  210. if (av_cmp_q(dar, (AVRational){ xv->dest_w, xv->dest_h }) > 0) {
  211. /* fit in width */
  212. xv->dest_y = xv->dest_h;
  213. xv->dest_x = 0;
  214. xv->dest_h = av_rescale(xv->dest_w, dar.den, dar.num);
  215. xv->dest_y -= xv->dest_h;
  216. xv->dest_y /= 2;
  217. } else {
  218. /* fit in height */
  219. xv->dest_x = xv->dest_w;
  220. xv->dest_y = 0;
  221. xv->dest_w = av_rescale(xv->dest_h, dar.num, dar.den);
  222. xv->dest_x -= xv->dest_w;
  223. xv->dest_x /= 2;
  224. }
  225. }
  226. static int xv_repaint(AVFormatContext *s)
  227. {
  228. XVContext *xv = s->priv_data;
  229. XWindowAttributes window_attrs;
  230. XGetWindowAttributes(xv->display, xv->window, &window_attrs);
  231. if (window_attrs.width != xv->window_width || window_attrs.height != xv->window_height) {
  232. XRectangle rect[2];
  233. xv->dest_w = window_attrs.width;
  234. xv->dest_h = window_attrs.height;
  235. compute_display_area(s);
  236. if (xv->dest_x) {
  237. rect[0].width = rect[1].width = xv->dest_x;
  238. rect[0].height = rect[1].height = window_attrs.height;
  239. rect[0].y = rect[1].y = 0;
  240. rect[0].x = 0;
  241. rect[1].x = xv->dest_w + xv->dest_x;
  242. XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
  243. }
  244. if (xv->dest_y) {
  245. rect[0].width = rect[1].width = window_attrs.width;
  246. rect[0].height = rect[1].height = xv->dest_y;
  247. rect[0].x = rect[1].x = 0;
  248. rect[0].y = 0;
  249. rect[1].y = xv->dest_h + xv->dest_y;
  250. XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
  251. }
  252. }
  253. if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
  254. xv->yuv_image, 0, 0, xv->image_width, xv->image_height,
  255. xv->dest_x, xv->dest_y, xv->dest_w, xv->dest_h, True) != Success) {
  256. av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n");
  257. return AVERROR_EXTERNAL;
  258. }
  259. return 0;
  260. }
  261. static int write_picture(AVFormatContext *s, AVPicture *pict)
  262. {
  263. XVContext *xv = s->priv_data;
  264. XvImage *img = xv->yuv_image;
  265. uint8_t *data[3] = {
  266. img->data + img->offsets[0],
  267. img->data + img->offsets[1],
  268. img->data + img->offsets[2]
  269. };
  270. av_image_copy(data, img->pitches, (const uint8_t **)pict->data, pict->linesize,
  271. xv->image_format, img->width, img->height);
  272. return xv_repaint(s);
  273. }
  274. static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
  275. {
  276. AVPicture pict;
  277. AVCodecContext *ctx = s->streams[0]->codec;
  278. avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height);
  279. return write_picture(s, &pict);
  280. }
  281. static int xv_write_frame(AVFormatContext *s, int stream_index, AVFrame **frame,
  282. unsigned flags)
  283. {
  284. /* xv_write_header() should have accepted only supported formats */
  285. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  286. return 0;
  287. return write_picture(s, (AVPicture *)*frame);
  288. }
  289. static int xv_control_message(AVFormatContext *s, int type, void *data, size_t data_size)
  290. {
  291. switch(type) {
  292. case AV_APP_TO_DEV_WINDOW_REPAINT:
  293. return xv_repaint(s);
  294. default:
  295. break;
  296. }
  297. return AVERROR(ENOSYS);
  298. }
  299. #define OFFSET(x) offsetof(XVContext, x)
  300. static const AVOption options[] = {
  301. { "display_name", "set display name", OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  302. { "window_id", "set existing window id", OFFSET(window_id), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  303. { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  304. { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  305. { "window_x", "set window x offset", OFFSET(window_x), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  306. { "window_y", "set window y offset", OFFSET(window_y), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  307. { NULL }
  308. };
  309. static const AVClass xv_class = {
  310. .class_name = "xvideo outdev",
  311. .item_name = av_default_item_name,
  312. .option = options,
  313. .version = LIBAVUTIL_VERSION_INT,
  314. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  315. };
  316. AVOutputFormat ff_xv_muxer = {
  317. .name = "xv",
  318. .long_name = NULL_IF_CONFIG_SMALL("XV (XVideo) output device"),
  319. .priv_data_size = sizeof(XVContext),
  320. .audio_codec = AV_CODEC_ID_NONE,
  321. .video_codec = AV_CODEC_ID_RAWVIDEO,
  322. .write_header = xv_write_header,
  323. .write_packet = xv_write_packet,
  324. .write_uncoded_frame = xv_write_frame,
  325. .write_trailer = xv_write_trailer,
  326. .control_message = xv_control_message,
  327. .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  328. .priv_class = &xv_class,
  329. };