FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavdevice/xv.c
Date: 2023-10-02 11:06:47
Exec Total Coverage
Lines: 0 177 0.0%
Functions: 0 9 0.0%
Branches: 0 78 0.0%

Line Branch Exec Source
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 */
28
29 #include <X11/Xlib.h>
30 #include <X11/extensions/Xv.h>
31 #include <X11/extensions/XShm.h>
32 #include <X11/extensions/Xvlib.h>
33 #include <sys/shm.h>
34
35 #include "libavutil/frame.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/imgutils.h"
39 #include "libavformat/mux.h"
40 #include "avdevice.h"
41
42 typedef struct {
43 AVClass *class;
44 GC gc;
45
46 Window window;
47 int64_t window_id;
48 char *window_title;
49 int window_width, window_height;
50 int window_x, window_y;
51 int dest_x, dest_y; /**< display area position */
52 unsigned int dest_w, dest_h; /**< display area dimensions */
53
54 Display* display;
55 char *display_name;
56
57 XvImage* yuv_image;
58 enum AVPixelFormat image_format;
59 int image_width, image_height;
60 XShmSegmentInfo yuv_shminfo;
61 int xv_port;
62 Atom wm_delete_message;
63 } XVContext;
64
65 typedef struct XVTagFormatMap
66 {
67 int tag;
68 enum AVPixelFormat format;
69 } XVTagFormatMap;
70
71 static const XVTagFormatMap tag_codec_map[] = {
72 { MKTAG('I','4','2','0'), AV_PIX_FMT_YUV420P },
73 { MKTAG('U','Y','V','Y'), AV_PIX_FMT_UYVY422 },
74 { MKTAG('Y','U','Y','2'), AV_PIX_FMT_YUYV422 },
75 { 0, AV_PIX_FMT_NONE }
76 };
77
78 static int xv_get_tag_from_format(enum AVPixelFormat format)
79 {
80 const XVTagFormatMap *m = tag_codec_map;
81 int i;
82 for (i = 0; m->tag; m = &tag_codec_map[++i]) {
83 if (m->format == format)
84 return m->tag;
85 }
86 return 0;
87 }
88
89 static int xv_write_trailer(AVFormatContext *s)
90 {
91 XVContext *xv = s->priv_data;
92 if (xv->display) {
93 XShmDetach(xv->display, &xv->yuv_shminfo);
94 if (xv->yuv_image)
95 shmdt(xv->yuv_image->data);
96 XFree(xv->yuv_image);
97 if (xv->gc)
98 XFreeGC(xv->display, xv->gc);
99 XCloseDisplay(xv->display);
100 }
101 return 0;
102 }
103
104 static int xv_write_header(AVFormatContext *s)
105 {
106 XVContext *xv = s->priv_data;
107 unsigned int num_adaptors;
108 XvAdaptorInfo *ai;
109 XvImageFormatValues *fv;
110 XColor fgcolor;
111 XWindowAttributes window_attrs;
112 int num_formats = 0, j, tag, ret;
113 AVCodecParameters *par = s->streams[0]->codecpar;
114
115 if ( s->nb_streams > 1
116 || par->codec_type != AVMEDIA_TYPE_VIDEO
117 || (par->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME && par->codec_id != AV_CODEC_ID_RAWVIDEO)) {
118 av_log(s, AV_LOG_ERROR, "Only a single raw or wrapped avframe video stream is supported.\n");
119 return AVERROR(EINVAL);
120 }
121
122 if (!(tag = xv_get_tag_from_format(par->format))) {
123 av_log(s, AV_LOG_ERROR,
124 "Unsupported pixel format '%s', only yuv420p, uyvy422, yuyv422 are currently supported\n",
125 av_get_pix_fmt_name(par->format));
126 return AVERROR_PATCHWELCOME;
127 }
128 xv->image_format = par->format;
129
130 xv->display = XOpenDisplay(xv->display_name);
131 if (!xv->display) {
132 av_log(s, AV_LOG_ERROR, "Could not open the X11 display '%s'\n", xv->display_name);
133 return AVERROR(EINVAL);
134 }
135
136 xv->image_width = par->width;
137 xv->image_height = par->height;
138 if (!xv->window_width && !xv->window_height) {
139 AVRational sar = par->sample_aspect_ratio;
140 xv->window_width = par->width;
141 xv->window_height = par->height;
142 if (sar.num) {
143 if (sar.num > sar.den)
144 xv->window_width = av_rescale(xv->window_width, sar.num, sar.den);
145 if (sar.num < sar.den)
146 xv->window_height = av_rescale(xv->window_height, sar.den, sar.num);
147 }
148 }
149 if (!xv->window_id) {
150 xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display),
151 xv->window_x, xv->window_y,
152 xv->window_width, xv->window_height,
153 0, 0, 0);
154 if (!xv->window_title) {
155 if (!(xv->window_title = av_strdup(s->url))) {
156 ret = AVERROR(ENOMEM);
157 goto fail;
158 }
159 }
160 XStoreName(xv->display, xv->window, xv->window_title);
161 xv->wm_delete_message = XInternAtom(xv->display, "WM_DELETE_WINDOW", False);
162 XSetWMProtocols(xv->display, xv->window, &xv->wm_delete_message, 1);
163 XMapWindow(xv->display, xv->window);
164 } else
165 xv->window = xv->window_id;
166
167 if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) {
168 ret = AVERROR_EXTERNAL;
169 goto fail;
170 }
171 if (!num_adaptors) {
172 av_log(s, AV_LOG_ERROR, "No X-Video adaptors present\n");
173 return AVERROR(ENODEV);
174 }
175 xv->xv_port = ai[0].base_id;
176 XvFreeAdaptorInfo(ai);
177
178 fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
179 if (!fv) {
180 ret = AVERROR_EXTERNAL;
181 goto fail;
182 }
183 for (j = 0; j < num_formats; j++) {
184 if (fv[j].id == tag) {
185 break;
186 }
187 }
188 XFree(fv);
189
190 if (j >= num_formats) {
191 av_log(s, AV_LOG_ERROR,
192 "Device does not support pixel format %s, aborting\n",
193 av_get_pix_fmt_name(par->format));
194 ret = AVERROR(EINVAL);
195 goto fail;
196 }
197
198 xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
199 xv->image_width = par->width;
200 xv->image_height = par->height;
201 xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port, tag, 0,
202 xv->image_width, xv->image_height, &xv->yuv_shminfo);
203 xv->yuv_shminfo.shmid = shmget(IPC_PRIVATE, xv->yuv_image->data_size,
204 IPC_CREAT | 0777);
205 xv->yuv_shminfo.shmaddr = (char *)shmat(xv->yuv_shminfo.shmid, 0, 0);
206 xv->yuv_image->data = xv->yuv_shminfo.shmaddr;
207 xv->yuv_shminfo.readOnly = False;
208
209 XShmAttach(xv->display, &xv->yuv_shminfo);
210 XSync(xv->display, False);
211 shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0);
212
213 XGetWindowAttributes(xv->display, xv->window, &window_attrs);
214 fgcolor.red = fgcolor.green = fgcolor.blue = 0;
215 fgcolor.flags = DoRed | DoGreen | DoBlue;
216 XAllocColor(xv->display, window_attrs.colormap, &fgcolor);
217 XSetForeground(xv->display, xv->gc, fgcolor.pixel);
218 //force display area recalculation at first frame
219 xv->window_width = xv->window_height = 0;
220
221 return 0;
222 fail:
223 xv_write_trailer(s);
224 return ret;
225 }
226
227 static void compute_display_area(AVFormatContext *s)
228 {
229 XVContext *xv = s->priv_data;
230 AVRational sar, dar; /* sample and display aspect ratios */
231 AVStream *st = s->streams[0];
232 AVCodecParameters *par = st->codecpar;
233
234 /* compute overlay width and height from the codec context information */
235 sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
236 dar = av_mul_q(sar, (AVRational){ par->width, par->height });
237
238 /* we suppose the screen has a 1/1 sample aspect ratio */
239 /* fit in the window */
240 if (av_cmp_q(dar, (AVRational){ xv->dest_w, xv->dest_h }) > 0) {
241 /* fit in width */
242 xv->dest_y = xv->dest_h;
243 xv->dest_x = 0;
244 xv->dest_h = av_rescale(xv->dest_w, dar.den, dar.num);
245 xv->dest_y -= xv->dest_h;
246 xv->dest_y /= 2;
247 } else {
248 /* fit in height */
249 xv->dest_x = xv->dest_w;
250 xv->dest_y = 0;
251 xv->dest_w = av_rescale(xv->dest_h, dar.num, dar.den);
252 xv->dest_x -= xv->dest_w;
253 xv->dest_x /= 2;
254 }
255 }
256
257 static int xv_repaint(AVFormatContext *s)
258 {
259 XVContext *xv = s->priv_data;
260 XWindowAttributes window_attrs;
261
262 XGetWindowAttributes(xv->display, xv->window, &window_attrs);
263 if (window_attrs.width != xv->window_width || window_attrs.height != xv->window_height) {
264 XRectangle rect[2];
265 xv->dest_w = window_attrs.width;
266 xv->dest_h = window_attrs.height;
267 compute_display_area(s);
268 if (xv->dest_x) {
269 rect[0].width = rect[1].width = xv->dest_x;
270 rect[0].height = rect[1].height = window_attrs.height;
271 rect[0].y = rect[1].y = 0;
272 rect[0].x = 0;
273 rect[1].x = xv->dest_w + xv->dest_x;
274 XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
275 }
276 if (xv->dest_y) {
277 rect[0].width = rect[1].width = window_attrs.width;
278 rect[0].height = rect[1].height = xv->dest_y;
279 rect[0].x = rect[1].x = 0;
280 rect[0].y = 0;
281 rect[1].y = xv->dest_h + xv->dest_y;
282 XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
283 }
284 }
285
286 if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
287 xv->yuv_image, 0, 0, xv->image_width, xv->image_height,
288 xv->dest_x, xv->dest_y, xv->dest_w, xv->dest_h, True) != Success) {
289 av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n");
290 return AVERROR_EXTERNAL;
291 }
292 return 0;
293 }
294
295 static int write_picture(AVFormatContext *s, uint8_t *input_data[4],
296 int linesize[4])
297 {
298 XVContext *xv = s->priv_data;
299 XvImage *img = xv->yuv_image;
300 uint8_t *data[4] = {
301 img->data + img->offsets[0],
302 img->data + img->offsets[1],
303 img->data + img->offsets[2]
304 };
305
306 /* Check messages. Window might get closed. */
307 if (!xv->window_id) {
308 XEvent event;
309 while (XPending(xv->display)) {
310 XNextEvent(xv->display, &event);
311 if (event.type == ClientMessage && event.xclient.data.l[0] == xv->wm_delete_message) {
312 av_log(xv, AV_LOG_DEBUG, "Window close event.\n");
313 return AVERROR(EPIPE);
314 }
315 }
316 }
317
318 av_image_copy2(data, img->pitches, input_data, linesize,
319 xv->image_format, img->width, img->height);
320 return xv_repaint(s);
321 }
322
323 static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
324 {
325 AVCodecParameters *par = s->streams[0]->codecpar;
326
327 if (par->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME) {
328 AVFrame *frame = (AVFrame *)pkt->data;
329 return write_picture(s, frame->data, frame->linesize);
330 } else {
331 uint8_t *data[4];
332 int linesize[4];
333
334 av_image_fill_arrays(data, linesize, pkt->data, par->format,
335 par->width, par->height, 1);
336 return write_picture(s, data, linesize);
337 }
338 }
339
340 static int xv_write_frame(AVFormatContext *s, int stream_index, AVFrame **frame,
341 unsigned flags)
342 {
343 /* xv_write_header() should have accepted only supported formats */
344 if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
345 return 0;
346 return write_picture(s, (*frame)->data, (*frame)->linesize);
347 }
348
349 static int xv_control_message(AVFormatContext *s, int type, void *data, size_t data_size)
350 {
351 switch(type) {
352 case AV_APP_TO_DEV_WINDOW_REPAINT:
353 return xv_repaint(s);
354 default:
355 break;
356 }
357 return AVERROR(ENOSYS);
358 }
359
360 #define OFFSET(x) offsetof(XVContext, x)
361 static const AVOption options[] = {
362 { "display_name", "set display name", OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
363 { "window_id", "set existing window id", OFFSET(window_id), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
364 { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
365 { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
366 { "window_x", "set window x offset", OFFSET(window_x), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
367 { "window_y", "set window y offset", OFFSET(window_y), AV_OPT_TYPE_INT, {.i64 = 0 }, -INT_MAX, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
368 { NULL }
369
370 };
371
372 static const AVClass xv_class = {
373 .class_name = "xvideo outdev",
374 .item_name = av_default_item_name,
375 .option = options,
376 .version = LIBAVUTIL_VERSION_INT,
377 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
378 };
379
380 const FFOutputFormat ff_xv_muxer = {
381 .p.name = "xv",
382 .p.long_name = NULL_IF_CONFIG_SMALL("XV (XVideo) output device"),
383 .p.audio_codec = AV_CODEC_ID_NONE,
384 .p.video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
385 .p.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
386 .p.priv_class = &xv_class,
387 .priv_data_size = sizeof(XVContext),
388 .write_header = xv_write_header,
389 .write_packet = xv_write_packet,
390 .write_uncoded_frame = xv_write_frame,
391 .write_trailer = xv_write_trailer,
392 .control_message = xv_control_message,
393 };
394