Browse code

lavd: add xv output device

Based on the work of Jeff Moguillansky <Jeff.Moguillansky@am.sony.com>.

See thread:
Subject: [FFmpeg-devel] x11 output device for libavdevice
Date: Wed, 10 Apr 2013 23:10:47 +0000

Stefano Sabatini authored on 2013/05/14 20:44:29
Showing 7 changed files
... ...
@@ -58,6 +58,7 @@ version <next>:
58 58
 - Hald CLUT support (generation and filtering)
59 59
 - VC-1 interlaced B-frame support
60 60
 - support for WavPack muxing (raw and in Matroska)
61
+- XVideo output device
61 62
 
62 63
 
63 64
 version 1.2:
... ...
@@ -2084,6 +2084,8 @@ v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h"
2084 2084
 v4l2_outdev_deps_any="linux_videodev2_h sys_videoio_h"
2085 2085
 vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines"
2086 2086
 vfwcap_indev_extralibs="-lavicap32"
2087
+xv_outdev_deps="X11_extensions_Xvlib_h XvGetPortAttribute"
2088
+xv_outdev_extralibs="-lXv"
2087 2089
 x11grab_indev_deps="x11grab"
2088 2090
 
2089 2091
 # protocols
... ...
@@ -3982,6 +3984,7 @@ check_func_headers windows.h SetConsoleTextAttribute
3982 3982
 check_func_headers windows.h Sleep
3983 3983
 check_func_headers windows.h VirtualAlloc
3984 3984
 check_func_headers glob.h glob
3985
+check_func_headers "X11/Xlib.h X11/extensions/Xvlib.h" XvGetPortAttribute -lXv
3985 3986
 
3986 3987
 check_header cl/cl.h
3987 3988
 check_header direct.h
... ...
@@ -153,4 +153,69 @@ ffmpeg -i INPUT -vcodec rawvideo -pix_fmt yuv420p -window_size qcif -f sdl "SDL
153 153
 
154 154
 sndio audio output device.
155 155
 
156
+@section xv
157
+
158
+XV (XVideo) output device.
159
+
160
+This output device allows to show a video stream in a X Window System
161
+window.
162
+
163
+@subsection Options
164
+
165
+@table @option
166
+@item display_name
167
+Specify the hardware display name, which determines the display and
168
+communications domain to be used.
169
+
170
+The display name or DISPLAY environment variable can be a string in
171
+the format @var{hostname}[:@var{number}[.@var{screen_number}]].
172
+
173
+@var{hostname} specifies the name of the host machine on which the
174
+display is physically attached. @var{number} specifies the number of
175
+the display server on that host machine. @var{screen_number} specifies
176
+the screen to be used on that server.
177
+
178
+If unspecified, it defaults to the value of the DISPLAY environment
179
+variable.
180
+
181
+For example, @code{dual-headed:0.1} would specify screen 1 of display
182
+0 on the machine named ``dual-headed''.
183
+
184
+Check the X11 specification for more detailed information about the
185
+display name format.
186
+
187
+@item window_size
188
+Set the created window size, can be a string of the form
189
+@var{width}x@var{height} or a video size abbreviation. If not
190
+specified it defaults to the size of the input video.
191
+
192
+@item window_x
193
+@item window_y
194
+Set the X and Y window offsets for the created window. They are both
195
+set to 0 by default. The values may be ignored by the window manager.
196
+
197
+@item window_title
198
+Set the window title, if not specified default to the filename
199
+specified for the output device.
200
+@end table
201
+
202
+For more information about XVideo see @url{http://www.x.org/}.
203
+
204
+@subsection Examples
205
+
206
+@itemize
207
+@item
208
+Decode, display and encode video input with @command{ffmpeg} at the
209
+same time:
210
+@example
211
+ffmpeg -i INPUT OUTPUT -f xv display
212
+@end example
213
+
214
+@item
215
+Decode and display the input video to multiple X11 windows:
216
+@example
217
+ffmpeg -i INPUT -f xv normal -vf negate -f xv negated
218
+@end example
219
+@end itemize
220
+
156 221
 @c man end OUTPUT DEVICES
... ...
@@ -37,6 +37,7 @@ OBJS-$(CONFIG_V4L2_OUTDEV)               += v4l2enc.o v4l2-common.o
37 37
 OBJS-$(CONFIG_V4L_INDEV)                 += v4l.o
38 38
 OBJS-$(CONFIG_VFWCAP_INDEV)              += vfwcap.o
39 39
 OBJS-$(CONFIG_X11GRAB_INDEV)             += x11grab.o
40
+OBJS-$(CONFIG_XV_OUTDEV)                 += xv.o
40 41
 
41 42
 # external libraries
42 43
 OBJS-$(CONFIG_LIBCDIO_INDEV)             += libcdio.o
... ...
@@ -64,6 +64,7 @@ void avdevice_register_all(void)
64 64
 //    REGISTER_INDEV   (V4L,              v4l
65 65
     REGISTER_INDEV   (VFWCAP,           vfwcap);
66 66
     REGISTER_INDEV   (X11GRAB,          x11grab);
67
+    REGISTER_OUTDEV  (XV,               xv);
67 68
 
68 69
     /* external libraries */
69 70
     REGISTER_INDEV   (LIBCDIO,          libcdio);
... ...
@@ -28,8 +28,8 @@
28 28
 #include "libavutil/avutil.h"
29 29
 
30 30
 #define LIBAVDEVICE_VERSION_MAJOR  55
31
-#define LIBAVDEVICE_VERSION_MINOR   1
32
-#define LIBAVDEVICE_VERSION_MICRO 101
31
+#define LIBAVDEVICE_VERSION_MINOR   2
32
+#define LIBAVDEVICE_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
35 35
                                                LIBAVDEVICE_VERSION_MINOR, \
36 36
new file mode 100644
... ...
@@ -0,0 +1,217 @@
0
+/*
1
+ * Copyright (c) 2013 Jeff Moguillansky
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
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
+
29
+#include <X11/Xlib.h>
30
+#include <X11/extensions/Xv.h>
31
+#include <X11/extensions/Xvlib.h>
32
+#include <X11/extensions/XShm.h>
33
+#include <sys/shm.h>
34
+
35
+#include "libavutil/opt.h"
36
+#include "libavutil/pixdesc.h"
37
+#include "avdevice.h"
38
+
39
+typedef struct {
40
+    AVClass *class;
41
+    GC gc;
42
+
43
+    Window window;
44
+    char *window_title;
45
+    int window_width, window_height;
46
+    int window_x, window_y;
47
+
48
+    Display* display;
49
+    char *display_name;
50
+
51
+    XvImage* yuv_image;
52
+    int image_width, image_height;
53
+    XShmSegmentInfo yuv_shminfo;
54
+    int xv_port;
55
+} XVContext;
56
+
57
+static int xv_write_header(AVFormatContext *s)
58
+{
59
+    XVContext *xv = s->priv_data;
60
+    unsigned int num_adaptors;
61
+    XvAdaptorInfo *ai;
62
+    XvImageFormatValues *fv;
63
+    int num_formats = 0, j;
64
+    AVCodecContext *encctx = s->streams[0]->codec;
65
+
66
+    if (   s->nb_streams > 1
67
+        || encctx->codec_type != AVMEDIA_TYPE_VIDEO
68
+        || encctx->codec_id   != AV_CODEC_ID_RAWVIDEO) {
69
+        av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
70
+        return AVERROR(EINVAL);
71
+    }
72
+
73
+    xv->display = XOpenDisplay(xv->display_name);
74
+    if (!xv->display) {
75
+        av_log(s, AV_LOG_ERROR, "Could not open the X11 display '%s'\n", xv->display_name);
76
+        return AVERROR(EINVAL);
77
+    }
78
+
79
+    xv->image_width  = encctx->width;
80
+    xv->image_height = encctx->height;
81
+    if (!xv->window_width && !xv->window_height) {
82
+        xv->window_width  = encctx->width;
83
+        xv->window_height = encctx->height;
84
+    }
85
+    xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display),
86
+                                     xv->window_x, xv->window_y,
87
+                                     xv->window_width, xv->window_height,
88
+                                     0, 0, 0);
89
+    if (!xv->window_title) {
90
+        if (!(xv->window_title = av_strdup(s->filename)))
91
+            return AVERROR(ENOMEM);
92
+    }
93
+    XStoreName(xv->display, xv->window, xv->window_title);
94
+    XMapWindow(xv->display, xv->window);
95
+
96
+    if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success)
97
+        return AVERROR_EXTERNAL;
98
+    xv->xv_port = ai[0].base_id;
99
+
100
+    if (encctx->pix_fmt != AV_PIX_FMT_YUV420P) {
101
+        av_log(s, AV_LOG_ERROR,
102
+               "Unsupported pixel format '%s', only yuv420p is currently supported\n",
103
+               av_get_pix_fmt_name(encctx->pix_fmt));
104
+        return AVERROR_PATCHWELCOME;
105
+    }
106
+
107
+    fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
108
+    if (!fv)
109
+        return AVERROR_EXTERNAL;
110
+    for (j = 0; j < num_formats; j++) {
111
+        if (fv[j].id == MKTAG('I','4','2','0')) {
112
+            break;
113
+        }
114
+    }
115
+    XFree(fv);
116
+
117
+    if (j >= num_formats) {
118
+        av_log(s, AV_LOG_ERROR,
119
+               "Device does not support pixel format yuv420p, aborting\n");
120
+        return AVERROR(EINVAL);
121
+    }
122
+
123
+    xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
124
+    xv->image_width  = encctx->width;
125
+    xv->image_height = encctx->height;
126
+    xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port,
127
+                                     MKTAG('I','4','2','0'), 0,
128
+                                     xv->image_width, xv->image_height, &xv->yuv_shminfo);
129
+    xv->yuv_shminfo.shmid = shmget(IPC_PRIVATE, xv->yuv_image->data_size,
130
+                                   IPC_CREAT | 0777);
131
+    xv->yuv_shminfo.shmaddr = (char *)shmat(xv->yuv_shminfo.shmid, 0, 0);
132
+    xv->yuv_image->data = xv->yuv_shminfo.shmaddr;
133
+    xv->yuv_shminfo.readOnly = False;
134
+
135
+    XShmAttach(xv->display, &xv->yuv_shminfo);
136
+    XSync(xv->display, False);
137
+    shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0);
138
+
139
+    return 0;
140
+}
141
+
142
+static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
143
+{
144
+    XVContext *xv = s->priv_data;
145
+    XvImage *img = xv->yuv_image;
146
+    XWindowAttributes window_attrs;
147
+    AVPicture pict;
148
+    AVCodecContext *ctx = s->streams[0]->codec;
149
+    int y, h;
150
+
151
+    h = img->height / 2;
152
+
153
+    avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height);
154
+    for (y = 0; y < img->height; y++) {
155
+        memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])],
156
+               &pict.data[0][y * pict.linesize[0]], img->pitches[0]);
157
+    }
158
+
159
+    for (y = 0; y < h; ++y) {
160
+        memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])],
161
+               &pict.data[1][y * pict.linesize[1]], img->pitches[1]);
162
+        memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],
163
+               &pict.data[2][y * pict.linesize[2]], img->pitches[2]);
164
+    }
165
+
166
+    XGetWindowAttributes(xv->display, xv->window, &window_attrs);
167
+    if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
168
+                      xv->yuv_image, 0, 0, xv->image_width, xv->image_height, 0, 0,
169
+                      window_attrs.width, window_attrs.height, True) != Success) {
170
+        av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n");
171
+        return AVERROR_EXTERNAL;
172
+    }
173
+    return 0;
174
+}
175
+
176
+static int xv_write_trailer(AVFormatContext *s)
177
+{
178
+    XVContext *xv = s->priv_data;
179
+
180
+    XShmDetach(xv->display, &xv->yuv_shminfo);
181
+    shmdt(xv->yuv_image->data);
182
+    XFree(xv->yuv_image);
183
+    XCloseDisplay(xv->display);
184
+    return 0;
185
+}
186
+
187
+#define OFFSET(x) offsetof(XVContext, x)
188
+static const AVOption options[] = {
189
+    { "display_name", "set display name",       OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
190
+    { "window_size",  "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
191
+    { "window_title", "set window title",       OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
192
+    { "window_x",     "set window x offset",    OFFSET(window_x),     AV_OPT_TYPE_INT,    {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
193
+    { "window_y",     "set window y offset",    OFFSET(window_y),     AV_OPT_TYPE_INT,    {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
194
+    { NULL }
195
+
196
+};
197
+
198
+static const AVClass xv_class = {
199
+    .class_name = "xvideo outdev",
200
+    .item_name  = av_default_item_name,
201
+    .option     = options,
202
+    .version    = LIBAVUTIL_VERSION_INT,
203
+};
204
+
205
+AVOutputFormat ff_xv_muxer = {
206
+    .name           = "xv",
207
+    .long_name      = NULL_IF_CONFIG_SMALL("XV (XVideo) output device"),
208
+    .priv_data_size = sizeof(XVContext),
209
+    .audio_codec    = AV_CODEC_ID_NONE,
210
+    .video_codec    = AV_CODEC_ID_RAWVIDEO,
211
+    .write_header   = xv_write_header,
212
+    .write_packet   = xv_write_packet,
213
+    .write_trailer  = xv_write_trailer,
214
+    .flags          = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
215
+    .priv_class     = &xv_class,
216
+};