xv.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * - add support to window id specification
  27. */
  28. #include <X11/Xlib.h>
  29. #include <X11/extensions/Xv.h>
  30. #include <X11/extensions/XShm.h>
  31. #include <X11/extensions/Xvlib.h>
  32. #include <sys/shm.h>
  33. #include "libavutil/opt.h"
  34. #include "libavutil/pixdesc.h"
  35. #include "libavutil/imgutils.h"
  36. #include "libavformat/internal.h"
  37. #include "avdevice.h"
  38. typedef struct {
  39. AVClass *class;
  40. GC gc;
  41. Window window;
  42. char *window_title;
  43. int window_width, window_height;
  44. int window_x, window_y;
  45. Display* display;
  46. char *display_name;
  47. XvImage* yuv_image;
  48. enum AVPixelFormat image_format;
  49. int image_width, image_height;
  50. XShmSegmentInfo yuv_shminfo;
  51. int xv_port;
  52. } XVContext;
  53. typedef struct XVTagFormatMap
  54. {
  55. int tag;
  56. enum AVPixelFormat format;
  57. } XVTagFormatMap;
  58. static XVTagFormatMap tag_codec_map[] = {
  59. { MKTAG('I','4','2','0'), AV_PIX_FMT_YUV420P },
  60. { MKTAG('U','Y','V','Y'), AV_PIX_FMT_UYVY422 },
  61. { MKTAG('Y','U','Y','2'), AV_PIX_FMT_YUYV422 },
  62. { 0, AV_PIX_FMT_NONE }
  63. };
  64. static int xv_get_tag_from_format(enum AVPixelFormat format)
  65. {
  66. XVTagFormatMap *m = tag_codec_map;
  67. int i;
  68. for (i = 0; m->tag; m = &tag_codec_map[++i]) {
  69. if (m->format == format)
  70. return m->tag;
  71. }
  72. return 0;
  73. }
  74. static int xv_write_trailer(AVFormatContext *s)
  75. {
  76. XVContext *xv = s->priv_data;
  77. if (xv->display) {
  78. XShmDetach(xv->display, &xv->yuv_shminfo);
  79. if (xv->yuv_image)
  80. shmdt(xv->yuv_image->data);
  81. XFree(xv->yuv_image);
  82. if (xv->gc)
  83. XFreeGC(xv->display, xv->gc);
  84. XCloseDisplay(xv->display);
  85. }
  86. return 0;
  87. }
  88. static int xv_write_header(AVFormatContext *s)
  89. {
  90. XVContext *xv = s->priv_data;
  91. unsigned int num_adaptors;
  92. XvAdaptorInfo *ai;
  93. XvImageFormatValues *fv;
  94. int num_formats = 0, j, tag, ret;
  95. AVCodecContext *encctx = s->streams[0]->codec;
  96. if ( s->nb_streams > 1
  97. || encctx->codec_type != AVMEDIA_TYPE_VIDEO
  98. || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
  99. av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
  100. return AVERROR(EINVAL);
  101. }
  102. if (!(tag = xv_get_tag_from_format(encctx->pix_fmt))) {
  103. av_log(s, AV_LOG_ERROR,
  104. "Unsupported pixel format '%s', only yuv420p, uyvy422, yuyv422 are currently supported\n",
  105. av_get_pix_fmt_name(encctx->pix_fmt));
  106. return AVERROR_PATCHWELCOME;
  107. }
  108. xv->image_format = encctx->pix_fmt;
  109. xv->display = XOpenDisplay(xv->display_name);
  110. if (!xv->display) {
  111. av_log(s, AV_LOG_ERROR, "Could not open the X11 display '%s'\n", xv->display_name);
  112. return AVERROR(EINVAL);
  113. }
  114. xv->image_width = encctx->width;
  115. xv->image_height = encctx->height;
  116. if (!xv->window_width && !xv->window_height) {
  117. xv->window_width = encctx->width;
  118. xv->window_height = encctx->height;
  119. }
  120. xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display),
  121. xv->window_x, xv->window_y,
  122. xv->window_width, xv->window_height,
  123. 0, 0, 0);
  124. if (!xv->window_title) {
  125. if (!(xv->window_title = av_strdup(s->filename))) {
  126. ret = AVERROR(ENOMEM);
  127. goto fail;
  128. }
  129. }
  130. XStoreName(xv->display, xv->window, xv->window_title);
  131. XMapWindow(xv->display, xv->window);
  132. if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) {
  133. ret = AVERROR_EXTERNAL;
  134. goto fail;
  135. }
  136. if (!num_adaptors) {
  137. av_log(s, AV_LOG_ERROR, "No X-Video adaptors present\n");
  138. return AVERROR(ENODEV);
  139. }
  140. xv->xv_port = ai[0].base_id;
  141. XvFreeAdaptorInfo(ai);
  142. fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
  143. if (!fv) {
  144. ret = AVERROR_EXTERNAL;
  145. goto fail;
  146. }
  147. for (j = 0; j < num_formats; j++) {
  148. if (fv[j].id == tag) {
  149. break;
  150. }
  151. }
  152. XFree(fv);
  153. if (j >= num_formats) {
  154. av_log(s, AV_LOG_ERROR,
  155. "Device does not support pixel format %s, aborting\n",
  156. av_get_pix_fmt_name(encctx->pix_fmt));
  157. ret = AVERROR(EINVAL);
  158. goto fail;
  159. }
  160. xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
  161. xv->image_width = encctx->width;
  162. xv->image_height = encctx->height;
  163. xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port, tag, 0,
  164. xv->image_width, xv->image_height, &xv->yuv_shminfo);
  165. xv->yuv_shminfo.shmid = shmget(IPC_PRIVATE, xv->yuv_image->data_size,
  166. IPC_CREAT | 0777);
  167. xv->yuv_shminfo.shmaddr = (char *)shmat(xv->yuv_shminfo.shmid, 0, 0);
  168. xv->yuv_image->data = xv->yuv_shminfo.shmaddr;
  169. xv->yuv_shminfo.readOnly = False;
  170. XShmAttach(xv->display, &xv->yuv_shminfo);
  171. XSync(xv->display, False);
  172. shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0);
  173. return 0;
  174. fail:
  175. xv_write_trailer(s);
  176. return ret;
  177. }
  178. static int write_picture(AVFormatContext *s, AVPicture *pict)
  179. {
  180. XVContext *xv = s->priv_data;
  181. XvImage *img = xv->yuv_image;
  182. XWindowAttributes window_attrs;
  183. uint8_t *data[3] = {
  184. img->data + img->offsets[0],
  185. img->data + img->offsets[1],
  186. img->data + img->offsets[2]
  187. };
  188. av_image_copy(data, img->pitches, (const uint8_t **)pict->data, pict->linesize,
  189. xv->image_format, img->width, img->height);
  190. XGetWindowAttributes(xv->display, xv->window, &window_attrs);
  191. if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
  192. xv->yuv_image, 0, 0, xv->image_width, xv->image_height, 0, 0,
  193. window_attrs.width, window_attrs.height, True) != Success) {
  194. av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n");
  195. return AVERROR_EXTERNAL;
  196. }
  197. return 0;
  198. }
  199. static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
  200. {
  201. AVPicture pict;
  202. AVCodecContext *ctx = s->streams[0]->codec;
  203. avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height);
  204. return write_picture(s, &pict);
  205. }
  206. static int xv_write_frame(AVFormatContext *s, int stream_index, AVFrame **frame,
  207. unsigned flags)
  208. {
  209. /* xv_write_header() should have accepted only supported formats */
  210. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  211. return 0;
  212. return write_picture(s, (AVPicture *)*frame);
  213. }
  214. #define OFFSET(x) offsetof(XVContext, x)
  215. static const AVOption options[] = {
  216. { "display_name", "set display name", OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  217. { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  218. { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  219. { "window_x", "set window x offset", OFFSET(window_x), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  220. { "window_y", "set window y offset", OFFSET(window_y), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  221. { NULL }
  222. };
  223. static const AVClass xv_class = {
  224. .class_name = "xvideo outdev",
  225. .item_name = av_default_item_name,
  226. .option = options,
  227. .version = LIBAVUTIL_VERSION_INT,
  228. };
  229. AVOutputFormat ff_xv_muxer = {
  230. .name = "xv",
  231. .long_name = NULL_IF_CONFIG_SMALL("XV (XVideo) output device"),
  232. .priv_data_size = sizeof(XVContext),
  233. .audio_codec = AV_CODEC_ID_NONE,
  234. .video_codec = AV_CODEC_ID_RAWVIDEO,
  235. .write_header = xv_write_header,
  236. .write_packet = xv_write_packet,
  237. .write_uncoded_frame = xv_write_frame,
  238. .write_trailer = xv_write_trailer,
  239. .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  240. .priv_class = &xv_class,
  241. };