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