From 9f8937b5ef1301184089f7bc31fe49c439d0ddbc Mon Sep 17 00:00:00 2001 From: Stefano Sabatini Date: Tue, 14 May 2013 13:44:29 +0200 Subject: [PATCH] lavd: add xv output device Based on the work of Jeff Moguillansky . See thread: Subject: [FFmpeg-devel] x11 output device for libavdevice Date: Wed, 10 Apr 2013 23:10:47 +0000 --- Changelog | 1 + configure | 3 + doc/outdevs.texi | 65 ++++++++++++ libavdevice/Makefile | 1 + libavdevice/alldevices.c | 1 + libavdevice/version.h | 4 +- libavdevice/xv.c | 217 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 libavdevice/xv.c diff --git a/Changelog b/Changelog index 84486cedba..67ab0b3bfa 100644 --- a/Changelog +++ b/Changelog @@ -58,6 +58,7 @@ version : - Hald CLUT support (generation and filtering) - VC-1 interlaced B-frame support - support for WavPack muxing (raw and in Matroska) +- XVideo output device version 1.2: diff --git a/configure b/configure index 8c1190f0ae..63d7ebba32 100755 --- a/configure +++ b/configure @@ -2084,6 +2084,8 @@ v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h" v4l2_outdev_deps_any="linux_videodev2_h sys_videoio_h" vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines" vfwcap_indev_extralibs="-lavicap32" +xv_outdev_deps="X11_extensions_Xvlib_h XvGetPortAttribute" +xv_outdev_extralibs="-lXv" x11grab_indev_deps="x11grab" # protocols @@ -3982,6 +3984,7 @@ check_func_headers windows.h SetConsoleTextAttribute check_func_headers windows.h Sleep check_func_headers windows.h VirtualAlloc check_func_headers glob.h glob +check_func_headers "X11/Xlib.h X11/extensions/Xvlib.h" XvGetPortAttribute -lXv check_header cl/cl.h check_header direct.h diff --git a/doc/outdevs.texi b/doc/outdevs.texi index 5896abacba..796797b070 100644 --- a/doc/outdevs.texi +++ b/doc/outdevs.texi @@ -153,4 +153,69 @@ ffmpeg -i INPUT -vcodec rawvideo -pix_fmt yuv420p -window_size qcif -f sdl "SDL sndio audio output device. +@section xv + +XV (XVideo) output device. + +This output device allows to show a video stream in a X Window System +window. + +@subsection Options + +@table @option +@item display_name +Specify the hardware display name, which determines the display and +communications domain to be used. + +The display name or DISPLAY environment variable can be a string in +the format @var{hostname}[:@var{number}[.@var{screen_number}]]. + +@var{hostname} specifies the name of the host machine on which the +display is physically attached. @var{number} specifies the number of +the display server on that host machine. @var{screen_number} specifies +the screen to be used on that server. + +If unspecified, it defaults to the value of the DISPLAY environment +variable. + +For example, @code{dual-headed:0.1} would specify screen 1 of display +0 on the machine named ``dual-headed''. + +Check the X11 specification for more detailed information about the +display name format. + +@item window_size +Set the created window size, can be a string of the form +@var{width}x@var{height} or a video size abbreviation. If not +specified it defaults to the size of the input video. + +@item window_x +@item window_y +Set the X and Y window offsets for the created window. They are both +set to 0 by default. The values may be ignored by the window manager. + +@item window_title +Set the window title, if not specified default to the filename +specified for the output device. +@end table + +For more information about XVideo see @url{http://www.x.org/}. + +@subsection Examples + +@itemize +@item +Decode, display and encode video input with @command{ffmpeg} at the +same time: +@example +ffmpeg -i INPUT OUTPUT -f xv display +@end example + +@item +Decode and display the input video to multiple X11 windows: +@example +ffmpeg -i INPUT -f xv normal -vf negate -f xv negated +@end example +@end itemize + @c man end OUTPUT DEVICES diff --git a/libavdevice/Makefile b/libavdevice/Makefile index f19ccdb014..340e0f1927 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -37,6 +37,7 @@ OBJS-$(CONFIG_V4L2_OUTDEV) += v4l2enc.o v4l2-common.o OBJS-$(CONFIG_V4L_INDEV) += v4l.o OBJS-$(CONFIG_VFWCAP_INDEV) += vfwcap.o OBJS-$(CONFIG_X11GRAB_INDEV) += x11grab.o +OBJS-$(CONFIG_XV_OUTDEV) += xv.o # external libraries OBJS-$(CONFIG_LIBCDIO_INDEV) += libcdio.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index 9672a4700e..fc8d3ce03e 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -64,6 +64,7 @@ void avdevice_register_all(void) // REGISTER_INDEV (V4L, v4l REGISTER_INDEV (VFWCAP, vfwcap); REGISTER_INDEV (X11GRAB, x11grab); + REGISTER_OUTDEV (XV, xv); /* external libraries */ REGISTER_INDEV (LIBCDIO, libcdio); diff --git a/libavdevice/version.h b/libavdevice/version.h index 2aab5208c5..bf170b7cf1 100644 --- a/libavdevice/version.h +++ b/libavdevice/version.h @@ -28,8 +28,8 @@ #include "libavutil/avutil.h" #define LIBAVDEVICE_VERSION_MAJOR 55 -#define LIBAVDEVICE_VERSION_MINOR 1 -#define LIBAVDEVICE_VERSION_MICRO 101 +#define LIBAVDEVICE_VERSION_MINOR 2 +#define LIBAVDEVICE_VERSION_MICRO 100 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \ LIBAVDEVICE_VERSION_MINOR, \ diff --git a/libavdevice/xv.c b/libavdevice/xv.c new file mode 100644 index 0000000000..670c4dee8d --- /dev/null +++ b/libavdevice/xv.c @@ -0,0 +1,217 @@ +/* + * Copyright (c) 2013 Jeff Moguillansky + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * XVideo output device + * + * TODO: + * - add support to more formats + * - add support to window id specification + */ + +#include +#include +#include +#include +#include + +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "avdevice.h" + +typedef struct { + AVClass *class; + GC gc; + + Window window; + char *window_title; + int window_width, window_height; + int window_x, window_y; + + Display* display; + char *display_name; + + XvImage* yuv_image; + int image_width, image_height; + XShmSegmentInfo yuv_shminfo; + int xv_port; +} XVContext; + +static int xv_write_header(AVFormatContext *s) +{ + XVContext *xv = s->priv_data; + unsigned int num_adaptors; + XvAdaptorInfo *ai; + XvImageFormatValues *fv; + int num_formats = 0, j; + AVCodecContext *encctx = s->streams[0]->codec; + + if ( s->nb_streams > 1 + || encctx->codec_type != AVMEDIA_TYPE_VIDEO + || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) { + av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n"); + return AVERROR(EINVAL); + } + + xv->display = XOpenDisplay(xv->display_name); + if (!xv->display) { + av_log(s, AV_LOG_ERROR, "Could not open the X11 display '%s'\n", xv->display_name); + return AVERROR(EINVAL); + } + + xv->image_width = encctx->width; + xv->image_height = encctx->height; + if (!xv->window_width && !xv->window_height) { + xv->window_width = encctx->width; + xv->window_height = encctx->height; + } + xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display), + xv->window_x, xv->window_y, + xv->window_width, xv->window_height, + 0, 0, 0); + if (!xv->window_title) { + if (!(xv->window_title = av_strdup(s->filename))) + return AVERROR(ENOMEM); + } + XStoreName(xv->display, xv->window, xv->window_title); + XMapWindow(xv->display, xv->window); + + if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) + return AVERROR_EXTERNAL; + xv->xv_port = ai[0].base_id; + + if (encctx->pix_fmt != AV_PIX_FMT_YUV420P) { + av_log(s, AV_LOG_ERROR, + "Unsupported pixel format '%s', only yuv420p is currently supported\n", + av_get_pix_fmt_name(encctx->pix_fmt)); + return AVERROR_PATCHWELCOME; + } + + fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats); + if (!fv) + return AVERROR_EXTERNAL; + for (j = 0; j < num_formats; j++) { + if (fv[j].id == MKTAG('I','4','2','0')) { + break; + } + } + XFree(fv); + + if (j >= num_formats) { + av_log(s, AV_LOG_ERROR, + "Device does not support pixel format yuv420p, aborting\n"); + return AVERROR(EINVAL); + } + + xv->gc = XCreateGC(xv->display, xv->window, 0, 0); + xv->image_width = encctx->width; + xv->image_height = encctx->height; + xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port, + MKTAG('I','4','2','0'), 0, + xv->image_width, xv->image_height, &xv->yuv_shminfo); + xv->yuv_shminfo.shmid = shmget(IPC_PRIVATE, xv->yuv_image->data_size, + IPC_CREAT | 0777); + xv->yuv_shminfo.shmaddr = (char *)shmat(xv->yuv_shminfo.shmid, 0, 0); + xv->yuv_image->data = xv->yuv_shminfo.shmaddr; + xv->yuv_shminfo.readOnly = False; + + XShmAttach(xv->display, &xv->yuv_shminfo); + XSync(xv->display, False); + shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0); + + return 0; +} + +static int xv_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + XVContext *xv = s->priv_data; + XvImage *img = xv->yuv_image; + XWindowAttributes window_attrs; + AVPicture pict; + AVCodecContext *ctx = s->streams[0]->codec; + int y, h; + + h = img->height / 2; + + avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height); + for (y = 0; y < img->height; y++) { + memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], + &pict.data[0][y * pict.linesize[0]], img->pitches[0]); + } + + for (y = 0; y < h; ++y) { + memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], + &pict.data[1][y * pict.linesize[1]], img->pitches[1]); + memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])], + &pict.data[2][y * pict.linesize[2]], img->pitches[2]); + } + + XGetWindowAttributes(xv->display, xv->window, &window_attrs); + if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc, + xv->yuv_image, 0, 0, xv->image_width, xv->image_height, 0, 0, + window_attrs.width, window_attrs.height, True) != Success) { + av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n"); + return AVERROR_EXTERNAL; + } + return 0; +} + +static int xv_write_trailer(AVFormatContext *s) +{ + XVContext *xv = s->priv_data; + + XShmDetach(xv->display, &xv->yuv_shminfo); + shmdt(xv->yuv_image->data); + XFree(xv->yuv_image); + XCloseDisplay(xv->display); + return 0; +} + +#define OFFSET(x) offsetof(XVContext, x) +static const AVOption options[] = { + { "display_name", "set display name", OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { "window_x", "set window x offset", OFFSET(window_x), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, + { "window_y", "set window y offset", OFFSET(window_y), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, + { NULL } + +}; + +static const AVClass xv_class = { + .class_name = "xvideo outdev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVOutputFormat ff_xv_muxer = { + .name = "xv", + .long_name = NULL_IF_CONFIG_SMALL("XV (XVideo) output device"), + .priv_data_size = sizeof(XVContext), + .audio_codec = AV_CODEC_ID_NONE, + .video_codec = AV_CODEC_ID_RAWVIDEO, + .write_header = xv_write_header, + .write_packet = xv_write_packet, + .write_trailer = xv_write_trailer, + .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, + .priv_class = &xv_class, +};