You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
8.3KB

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