xv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/frame.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/pixdesc.h"
  35. #include "libavutil/imgutils.h"
  36. #include "libavformat/mux.h"
  37. #include "avdevice.h"
  38. typedef struct {
  39. AVClass *class;
  40. GC gc;
  41. Window window;
  42. int64_t window_id;
  43. char *window_title;
  44. int window_width, window_height;
  45. int window_x, window_y;
  46. int dest_x, dest_y; /**< display area position */
  47. unsigned int dest_w, dest_h; /**< display area dimensions */
  48. Display* display;
  49. char *display_name;
  50. XvImage* yuv_image;
  51. enum AVPixelFormat image_format;
  52. int image_width, image_height;
  53. XShmSegmentInfo yuv_shminfo;
  54. int xv_port;
  55. Atom wm_delete_message;
  56. } XVContext;
  57. typedef struct XVTagFormatMap
  58. {
  59. int tag;
  60. enum AVPixelFormat format;
  61. } XVTagFormatMap;
  62. static const XVTagFormatMap tag_codec_map[] = {
  63. { MKTAG('I','4','2','0'), AV_PIX_FMT_YUV420P },
  64. { MKTAG('U','Y','V','Y'), AV_PIX_FMT_UYVY422 },
  65. { MKTAG('Y','U','Y','2'), AV_PIX_FMT_YUYV422 },
  66. { 0, AV_PIX_FMT_NONE }
  67. };
  68. static int xv_get_tag_from_format(enum AVPixelFormat format)
  69. {
  70. const XVTagFormatMap *m = tag_codec_map;
  71. int i;
  72. for (i = 0; m->tag; m = &tag_codec_map[++i]) {
  73. if (m->format == format)
  74. return m->tag;
  75. }
  76. return 0;
  77. }
  78. static int xv_write_trailer(AVFormatContext *s)
  79. {
  80. XVContext *xv = s->priv_data;
  81. if (xv->display) {
  82. XShmDetach(xv->display, &xv->yuv_shminfo);
  83. if (xv->yuv_image)
  84. shmdt(xv->yuv_image->data);
  85. XFree(xv->yuv_image);
  86. if (xv->gc)
  87. XFreeGC(xv->display, xv->gc);
  88. XCloseDisplay(xv->display);
  89. }
  90. return 0;
  91. }
  92. static int xv_write_header(AVFormatContext *s)
  93. {
  94. XVContext *xv = s->priv_data;
  95. unsigned int num_adaptors;
  96. XvAdaptorInfo *ai;
  97. XvImageFormatValues *fv;
  98. XColor fgcolor;
  99. XWindowAttributes window_attrs;
  100. int num_formats = 0, j, tag, ret;
  101. AVCodecParameters *par = s->streams[0]->codecpar;
  102. if ( s->nb_streams > 1
  103. || par->codec_type != AVMEDIA_TYPE_VIDEO
  104. || (par->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME && par->codec_id != AV_CODEC_ID_RAWVIDEO)) {
  105. av_log(s, AV_LOG_ERROR, "Only a single raw or wrapped avframe video stream is supported.\n");
  106. return AVERROR(EINVAL);
  107. }
  108. if (!(tag = xv_get_tag_from_format(par->format))) {
  109. av_log(s, AV_LOG_ERROR,
  110. "Unsupported pixel format '%s', only yuv420p, uyvy422, yuyv422 are currently supported\n",
  111. av_get_pix_fmt_name(par->format));
  112. return AVERROR_PATCHWELCOME;
  113. }
  114. xv->image_format = par->format;
  115. xv->display = XOpenDisplay(xv->display_name);
  116. if (!xv->display) {
  117. av_log(s, AV_LOG_ERROR, "Could not open the X11 display '%s'\n", xv->display_name);
  118. return AVERROR(EINVAL);
  119. }
  120. xv->image_width = par->width;
  121. xv->image_height = par->height;
  122. if (!xv->window_width && !xv->window_height) {
  123. AVRational sar = par->sample_aspect_ratio;
  124. xv->window_width = par->width;
  125. xv->window_height = par->height;
  126. if (sar.num) {
  127. if (sar.num > sar.den)
  128. xv->window_width = av_rescale(xv->window_width, sar.num, sar.den);
  129. if (sar.num < sar.den)
  130. xv->window_height = av_rescale(xv->window_height, sar.den, sar.num);
  131. }
  132. }
  133. if (!xv->window_id) {
  134. xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display),
  135. xv->window_x, xv->window_y,
  136. xv->window_width, xv->window_height,
  137. 0, 0, 0);
  138. if (!xv->window_title) {
  139. if (!(xv->window_title = av_strdup(s->url))) {
  140. ret = AVERROR(ENOMEM);
  141. goto fail;
  142. }
  143. }
  144. XStoreName(xv->display, xv->window, xv->window_title);
  145. xv->wm_delete_message = XInternAtom(xv->display, "WM_DELETE_WINDOW", False);
  146. XSetWMProtocols(xv->display, xv->window, &xv->wm_delete_message, 1);
  147. XMapWindow(xv->display, xv->window);
  148. } else
  149. xv->window = xv->window_id;
  150. if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) {
  151. ret = AVERROR_EXTERNAL;
  152. goto fail;
  153. }
  154. if (!num_adaptors) {
  155. av_log(s, AV_LOG_ERROR, "No X-Video adaptors present\n");
  156. return AVERROR(ENODEV);
  157. }
  158. xv->xv_port = ai[0].base_id;
  159. XvFreeAdaptorInfo(ai);
  160. fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
  161. if (!fv) {
  162. ret = AVERROR_EXTERNAL;
  163. goto fail;
  164. }
  165. for (j = 0; j < num_formats; j++) {
  166. if (fv[j].id == tag) {
  167. break;
  168. }
  169. }
  170. XFree(fv);
  171. if (j >= num_formats) {
  172. av_log(s, AV_LOG_ERROR,
  173. "Device does not support pixel format %s, aborting\n",
  174. av_get_pix_fmt_name(par->format));
  175. ret = AVERROR(EINVAL);
  176. goto fail;
  177. }
  178. xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
  179. xv->image_width = par->width;
  180. xv->image_height = par->height;
  181. xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port, tag, 0,
  182. xv->image_width, xv->image_height, &xv->yuv_shminfo);
  183. xv->yuv_shminfo.shmid = shmget(IPC_PRIVATE, xv->yuv_image->data_size,
  184. IPC_CREAT | 0777);
  185. xv->yuv_shminfo.shmaddr = (char *)shmat(xv->yuv_shminfo.shmid, 0, 0);
  186. xv->yuv_image->data = xv->yuv_shminfo.shmaddr;
  187. xv->yuv_shminfo.readOnly = False;
  188. XShmAttach(xv->display, &xv->yuv_shminfo);
  189. XSync(xv->display, False);
  190. shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0);
  191. XGetWindowAttributes(xv->display, xv->window, &window_attrs);
  192. fgcolor.red = fgcolor.green = fgcolor.blue = 0;
  193. fgcolor.flags = DoRed | DoGreen | DoBlue;
  194. XAllocColor(xv->display, window_attrs.colormap, &fgcolor);
  195. XSetForeground(xv->display, xv->gc, fgcolor.pixel);
  196. //force display area recalculation at first frame
  197. xv->window_width = xv->window_height = 0;
  198. return 0;
  199. fail:
  200. xv_write_trailer(s);
  201. return ret;
  202. }
  203. static void compute_display_area(AVFormatContext *s)
  204. {
  205. XVContext *xv = s->priv_data;
  206. AVRational sar, dar; /* sample and display aspect ratios */
  207. AVStream *st = s->streams[0];
  208. AVCodecParameters *par = st->codecpar;
  209. /* compute overlay width and height from the codec context information */
  210. sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
  211. dar = av_mul_q(sar, (AVRational){ par->width, par->height });
  212. /* we suppose the screen has a 1/1 sample aspect ratio */
  213. /* fit in the window */
  214. if (av_cmp_q(dar, (AVRational){ xv->dest_w, xv->dest_h }) > 0) {
  215. /* fit in width */
  216. xv->dest_y = xv->dest_h;
  217. xv->dest_x = 0;
  218. xv->dest_h = av_rescale(xv->dest_w, dar.den, dar.num);
  219. xv->dest_y -= xv->dest_h;
  220. xv->dest_y /= 2;
  221. } else {
  222. /* fit in height */
  223. xv->dest_x = xv->dest_w;
  224. xv->dest_y = 0;
  225. xv->dest_w = av_rescale(xv->dest_h, dar.num, dar.den);
  226. xv->dest_x -= xv->dest_w;
  227. xv->dest_x /= 2;
  228. }
  229. }
  230. static int xv_repaint(AVFormatContext *s)
  231. {
  232. XVContext *xv = s->priv_data;
  233. XWindowAttributes window_attrs;
  234. XGetWindowAttributes(xv->display, xv->window, &window_attrs);
  235. if (window_attrs.width != xv->window_width || window_attrs.height != xv->window_height) {
  236. XRectangle rect[2];
  237. xv->dest_w = window_attrs.width;
  238. xv->dest_h = window_attrs.height;
  239. compute_display_area(s);
  240. if (xv->dest_x) {
  241. rect[0].width = rect[1].width = xv->dest_x;
  242. rect[0].height = rect[1].height = window_attrs.height;
  243. rect[0].y = rect[1].y = 0;
  244. rect[0].x = 0;
  245. rect[1].x = xv->dest_w + xv->dest_x;
  246. XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
  247. }
  248. if (xv->dest_y) {
  249. rect[0].width = rect[1].width = window_attrs.width;
  250. rect[0].height = rect[1].height = xv->dest_y;
  251. rect[0].x = rect[1].x = 0;
  252. rect[0].y = 0;
  253. rect[1].y = xv->dest_h + xv->dest_y;
  254. XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
  255. }
  256. }
  257. if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
  258. xv->yuv_image, 0, 0, xv->image_width, xv->image_height,
  259. xv->dest_x, xv->dest_y, xv->dest_w, xv->dest_h, True) != Success) {
  260. av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n");
  261. return AVERROR_EXTERNAL;
  262. }
  263. return 0;
  264. }
  265. static int write_picture(AVFormatContext *s, uint8_t *input_data[4],
  266. int linesize[4])
  267. {
  268. XVContext *xv = s->priv_data;
  269. XvImage *img = xv->yuv_image;
  270. uint8_t *data[4] = {
  271. img->data + img->offsets[0],
  272. img->data + img->offsets[1],
  273. img->data + img->offsets[2]
  274. };
  275. /* Check messages. Window might get closed. */
  276. if (!xv->window_id) {
  277. XEvent event;
  278. while (XPending(xv->display)) {
  279. XNextEvent(xv->display, &event);
  280. if (event.type == ClientMessage && event.xclient.data.l[0] == xv->wm_delete_message) {
  281. av_log(xv, AV_LOG_DEBUG, "Window close event.\n");
  282. return AVERROR(EPIPE);
  283. }
  284. }
  285. }
  286. av_image_copy2(data, img->pitches, input_data, linesize,
  287. xv->image_format, img->width, img->height);
  288. return xv_repaint(s);
  289. }
  290. static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
  291. {
  292. AVCodecParameters *par = s->streams[0]->codecpar;
  293. if (par->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
  294. AVFrame *frame = (AVFrame *)pkt->data;
  295. return write_picture(s, frame->data, frame->linesize);
  296. } else {
  297. uint8_t *data[4];
  298. int linesize[4];
  299. av_image_fill_arrays(data, linesize, pkt->data, par->format,
  300. par->width, par->height, 1);
  301. return write_picture(s, data, linesize);
  302. }
  303. }
  304. static int xv_write_frame(AVFormatContext *s, int stream_index, AVFrame **frame,
  305. unsigned flags)
  306. {
  307. /* xv_write_header() should have accepted only supported formats */
  308. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  309. return 0;
  310. return write_picture(s, (*frame)->data, (*frame)->linesize);
  311. }
  312. static int xv_control_message(AVFormatContext *s, int type, void *data, size_t data_size)
  313. {
  314. switch(type) {
  315. case AV_APP_TO_DEV_WINDOW_REPAINT:
  316. return xv_repaint(s);
  317. default:
  318. break;
  319. }
  320. return AVERROR(ENOSYS);
  321. }
  322. #define OFFSET(x) offsetof(XVContext, x)
  323. static const AVOption options[] = {
  324. { "display_name", "set display name", OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  325. { "window_id", "set existing window id", OFFSET(window_id), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  326. { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  327. { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  328. { "window_x", "set window x offset", OFFSET(window_x), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  329. { "window_y", "set window y offset", OFFSET(window_y), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  330. { NULL }
  331. };
  332. static const AVClass xv_class = {
  333. .class_name = "xvideo outdev",
  334. .item_name = av_default_item_name,
  335. .option = options,
  336. .version = LIBAVUTIL_VERSION_INT,
  337. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  338. };
  339. const FFOutputFormat ff_xv_muxer = {
  340. .p.name = "xv",
  341. .p.long_name = NULL_IF_CONFIG_SMALL("XV (XVideo) output device"),
  342. .p.audio_codec = AV_CODEC_ID_NONE,
  343. .p.video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
  344. .p.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  345. .p.priv_class = &xv_class,
  346. .priv_data_size = sizeof(XVContext),
  347. .write_header = xv_write_header,
  348. .write_packet = xv_write_packet,
  349. .write_uncoded_frame = xv_write_frame,
  350. .write_trailer = xv_write_trailer,
  351. .control_message = xv_control_message,
  352. };