xv.c 13 KB

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