xv.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. AVRational sar = encctx->sample_aspect_ratio;
  118. xv->window_width = encctx->width;
  119. xv->window_height = encctx->height;
  120. if (sar.num) {
  121. if (sar.num > sar.den)
  122. xv->window_width = av_rescale(xv->window_width, sar.num, sar.den);
  123. if (sar.num < sar.den)
  124. xv->window_height = av_rescale(xv->window_height, sar.den, sar.num);
  125. }
  126. }
  127. xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display),
  128. xv->window_x, xv->window_y,
  129. xv->window_width, xv->window_height,
  130. 0, 0, 0);
  131. if (!xv->window_title) {
  132. if (!(xv->window_title = av_strdup(s->filename))) {
  133. ret = AVERROR(ENOMEM);
  134. goto fail;
  135. }
  136. }
  137. XStoreName(xv->display, xv->window, xv->window_title);
  138. XMapWindow(xv->display, xv->window);
  139. if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) {
  140. ret = AVERROR_EXTERNAL;
  141. goto fail;
  142. }
  143. if (!num_adaptors) {
  144. av_log(s, AV_LOG_ERROR, "No X-Video adaptors present\n");
  145. return AVERROR(ENODEV);
  146. }
  147. xv->xv_port = ai[0].base_id;
  148. XvFreeAdaptorInfo(ai);
  149. fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
  150. if (!fv) {
  151. ret = AVERROR_EXTERNAL;
  152. goto fail;
  153. }
  154. for (j = 0; j < num_formats; j++) {
  155. if (fv[j].id == tag) {
  156. break;
  157. }
  158. }
  159. XFree(fv);
  160. if (j >= num_formats) {
  161. av_log(s, AV_LOG_ERROR,
  162. "Device does not support pixel format %s, aborting\n",
  163. av_get_pix_fmt_name(encctx->pix_fmt));
  164. ret = AVERROR(EINVAL);
  165. goto fail;
  166. }
  167. xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
  168. xv->image_width = encctx->width;
  169. xv->image_height = encctx->height;
  170. xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port, tag, 0,
  171. xv->image_width, xv->image_height, &xv->yuv_shminfo);
  172. xv->yuv_shminfo.shmid = shmget(IPC_PRIVATE, xv->yuv_image->data_size,
  173. IPC_CREAT | 0777);
  174. xv->yuv_shminfo.shmaddr = (char *)shmat(xv->yuv_shminfo.shmid, 0, 0);
  175. xv->yuv_image->data = xv->yuv_shminfo.shmaddr;
  176. xv->yuv_shminfo.readOnly = False;
  177. XShmAttach(xv->display, &xv->yuv_shminfo);
  178. XSync(xv->display, False);
  179. shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0);
  180. return 0;
  181. fail:
  182. xv_write_trailer(s);
  183. return ret;
  184. }
  185. static int write_picture(AVFormatContext *s, AVPicture *pict)
  186. {
  187. XVContext *xv = s->priv_data;
  188. XvImage *img = xv->yuv_image;
  189. XWindowAttributes window_attrs;
  190. uint8_t *data[3] = {
  191. img->data + img->offsets[0],
  192. img->data + img->offsets[1],
  193. img->data + img->offsets[2]
  194. };
  195. av_image_copy(data, img->pitches, (const uint8_t **)pict->data, pict->linesize,
  196. xv->image_format, img->width, img->height);
  197. XGetWindowAttributes(xv->display, xv->window, &window_attrs);
  198. if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
  199. xv->yuv_image, 0, 0, xv->image_width, xv->image_height, 0, 0,
  200. window_attrs.width, window_attrs.height, True) != Success) {
  201. av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n");
  202. return AVERROR_EXTERNAL;
  203. }
  204. return 0;
  205. }
  206. static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
  207. {
  208. AVPicture pict;
  209. AVCodecContext *ctx = s->streams[0]->codec;
  210. avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height);
  211. return write_picture(s, &pict);
  212. }
  213. static int xv_write_frame(AVFormatContext *s, int stream_index, AVFrame **frame,
  214. unsigned flags)
  215. {
  216. /* xv_write_header() should have accepted only supported formats */
  217. if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
  218. return 0;
  219. return write_picture(s, (AVPicture *)*frame);
  220. }
  221. #define OFFSET(x) offsetof(XVContext, x)
  222. static const AVOption options[] = {
  223. { "display_name", "set display name", OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  224. { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  225. { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  226. { "window_x", "set window x offset", OFFSET(window_x), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  227. { "window_y", "set window y offset", OFFSET(window_y), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  228. { NULL }
  229. };
  230. static const AVClass xv_class = {
  231. .class_name = "xvideo outdev",
  232. .item_name = av_default_item_name,
  233. .option = options,
  234. .version = LIBAVUTIL_VERSION_INT,
  235. };
  236. AVOutputFormat ff_xv_muxer = {
  237. .name = "xv",
  238. .long_name = NULL_IF_CONFIG_SMALL("XV (XVideo) output device"),
  239. .priv_data_size = sizeof(XVContext),
  240. .audio_codec = AV_CODEC_ID_NONE,
  241. .video_codec = AV_CODEC_ID_RAWVIDEO,
  242. .write_header = xv_write_header,
  243. .write_packet = xv_write_packet,
  244. .write_uncoded_frame = xv_write_frame,
  245. .write_trailer = xv_write_trailer,
  246. .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  247. .priv_class = &xv_class,
  248. };