xv.c 13 KB

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