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