2013-05-14 11:44:29 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/extensions/Xv.h>
|
|
|
|
#include <X11/extensions/XShm.h>
|
2013-10-09 13:32:21 +00:00
|
|
|
#include <X11/extensions/Xvlib.h>
|
2013-05-14 11:44:29 +00:00
|
|
|
#include <sys/shm.h>
|
|
|
|
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
2013-11-14 20:17:00 +00:00
|
|
|
#include "libavutil/imgutils.h"
|
2014-01-01 11:05:48 +00:00
|
|
|
#include "libavformat/internal.h"
|
2013-05-14 11:44:29 +00:00
|
|
|
#include "avdevice.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
AVClass *class;
|
|
|
|
GC gc;
|
|
|
|
|
|
|
|
Window window;
|
2014-04-18 23:53:20 +00:00
|
|
|
int64_t window_id;
|
2013-05-14 11:44:29 +00:00
|
|
|
char *window_title;
|
|
|
|
int window_width, window_height;
|
|
|
|
int window_x, window_y;
|
2014-04-18 17:31:52 +00:00
|
|
|
int dest_x, dest_y; /**< display area position */
|
|
|
|
unsigned int dest_w, dest_h; /**< display area dimensions */
|
2013-05-14 11:44:29 +00:00
|
|
|
|
|
|
|
Display* display;
|
|
|
|
char *display_name;
|
|
|
|
|
|
|
|
XvImage* yuv_image;
|
2013-11-13 22:40:46 +00:00
|
|
|
enum AVPixelFormat image_format;
|
2013-05-14 11:44:29 +00:00
|
|
|
int image_width, image_height;
|
|
|
|
XShmSegmentInfo yuv_shminfo;
|
|
|
|
int xv_port;
|
|
|
|
} XVContext;
|
|
|
|
|
2013-11-13 22:40:46 +00:00
|
|
|
typedef struct XVTagFormatMap
|
|
|
|
{
|
|
|
|
int tag;
|
|
|
|
enum AVPixelFormat format;
|
|
|
|
} XVTagFormatMap;
|
|
|
|
|
|
|
|
static XVTagFormatMap tag_codec_map[] = {
|
|
|
|
{ MKTAG('I','4','2','0'), AV_PIX_FMT_YUV420P },
|
|
|
|
{ MKTAG('U','Y','V','Y'), AV_PIX_FMT_UYVY422 },
|
|
|
|
{ MKTAG('Y','U','Y','2'), AV_PIX_FMT_YUYV422 },
|
|
|
|
{ 0, AV_PIX_FMT_NONE }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int xv_get_tag_from_format(enum AVPixelFormat format)
|
|
|
|
{
|
|
|
|
XVTagFormatMap *m = tag_codec_map;
|
|
|
|
int i;
|
|
|
|
for (i = 0; m->tag; m = &tag_codec_map[++i]) {
|
|
|
|
if (m->format == format)
|
|
|
|
return m->tag;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:40:47 +00:00
|
|
|
static int xv_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
XVContext *xv = s->priv_data;
|
|
|
|
if (xv->display) {
|
|
|
|
XShmDetach(xv->display, &xv->yuv_shminfo);
|
|
|
|
if (xv->yuv_image)
|
|
|
|
shmdt(xv->yuv_image->data);
|
|
|
|
XFree(xv->yuv_image);
|
|
|
|
if (xv->gc)
|
|
|
|
XFreeGC(xv->display, xv->gc);
|
|
|
|
XCloseDisplay(xv->display);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-14 11:44:29 +00:00
|
|
|
static int xv_write_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
XVContext *xv = s->priv_data;
|
|
|
|
unsigned int num_adaptors;
|
|
|
|
XvAdaptorInfo *ai;
|
|
|
|
XvImageFormatValues *fv;
|
2014-04-18 17:31:52 +00:00
|
|
|
XColor fgcolor;
|
|
|
|
XWindowAttributes window_attrs;
|
2013-11-13 22:40:47 +00:00
|
|
|
int num_formats = 0, j, tag, ret;
|
2013-05-14 11:44:29 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:40:46 +00:00
|
|
|
if (!(tag = xv_get_tag_from_format(encctx->pix_fmt))) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Unsupported pixel format '%s', only yuv420p, uyvy422, yuyv422 are currently supported\n",
|
|
|
|
av_get_pix_fmt_name(encctx->pix_fmt));
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
xv->image_format = encctx->pix_fmt;
|
|
|
|
|
2013-05-14 11:44:29 +00:00
|
|
|
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) {
|
2013-11-25 16:26:07 +00:00
|
|
|
AVRational sar = encctx->sample_aspect_ratio;
|
2013-05-14 11:44:29 +00:00
|
|
|
xv->window_width = encctx->width;
|
|
|
|
xv->window_height = encctx->height;
|
2013-11-25 16:26:07 +00:00
|
|
|
if (sar.num) {
|
|
|
|
if (sar.num > sar.den)
|
|
|
|
xv->window_width = av_rescale(xv->window_width, sar.num, sar.den);
|
|
|
|
if (sar.num < sar.den)
|
|
|
|
xv->window_height = av_rescale(xv->window_height, sar.den, sar.num);
|
|
|
|
}
|
2013-05-14 11:44:29 +00:00
|
|
|
}
|
2014-04-18 23:53:20 +00:00
|
|
|
if (!xv->window_id) {
|
2014-05-02 15:52:57 +00:00
|
|
|
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))) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-11-13 22:40:47 +00:00
|
|
|
}
|
2014-05-02 15:52:57 +00:00
|
|
|
XStoreName(xv->display, xv->window, xv->window_title);
|
|
|
|
XMapWindow(xv->display, xv->window);
|
2014-04-18 23:53:20 +00:00
|
|
|
} else
|
|
|
|
xv->window = xv->window_id;
|
2013-05-14 11:44:29 +00:00
|
|
|
|
2013-11-13 22:40:47 +00:00
|
|
|
if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) {
|
|
|
|
ret = AVERROR_EXTERNAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-12-29 10:29:18 +00:00
|
|
|
if (!num_adaptors) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "No X-Video adaptors present\n");
|
|
|
|
return AVERROR(ENODEV);
|
|
|
|
}
|
2013-05-14 11:44:29 +00:00
|
|
|
xv->xv_port = ai[0].base_id;
|
2013-11-12 21:38:14 +00:00
|
|
|
XvFreeAdaptorInfo(ai);
|
2013-05-14 11:44:29 +00:00
|
|
|
|
|
|
|
fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
|
2013-11-13 22:40:47 +00:00
|
|
|
if (!fv) {
|
|
|
|
ret = AVERROR_EXTERNAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-05-14 11:44:29 +00:00
|
|
|
for (j = 0; j < num_formats; j++) {
|
2013-11-13 22:40:46 +00:00
|
|
|
if (fv[j].id == tag) {
|
2013-05-14 11:44:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XFree(fv);
|
|
|
|
|
|
|
|
if (j >= num_formats) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
2013-11-13 22:40:46 +00:00
|
|
|
"Device does not support pixel format %s, aborting\n",
|
|
|
|
av_get_pix_fmt_name(encctx->pix_fmt));
|
2013-11-13 22:40:47 +00:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2013-05-14 11:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
|
|
|
|
xv->image_width = encctx->width;
|
|
|
|
xv->image_height = encctx->height;
|
2013-11-13 22:40:46 +00:00
|
|
|
xv->yuv_image = XvShmCreateImage(xv->display, xv->xv_port, tag, 0,
|
2013-05-14 11:44:29 +00:00
|
|
|
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);
|
|
|
|
|
2014-04-18 17:31:52 +00:00
|
|
|
XGetWindowAttributes(xv->display, xv->window, &window_attrs);
|
|
|
|
fgcolor.red = fgcolor.green = fgcolor.blue = 0;
|
|
|
|
fgcolor.flags = DoRed | DoGreen | DoBlue;
|
|
|
|
XAllocColor(xv->display, window_attrs.colormap, &fgcolor);
|
|
|
|
XSetForeground(xv->display, xv->gc, fgcolor.pixel);
|
|
|
|
//force display area recalculation at first frame
|
|
|
|
xv->window_width = xv->window_height = 0;
|
|
|
|
|
2013-05-14 11:44:29 +00:00
|
|
|
return 0;
|
2013-11-13 22:40:47 +00:00
|
|
|
fail:
|
|
|
|
xv_write_trailer(s);
|
|
|
|
return ret;
|
2013-05-14 11:44:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 17:31:52 +00:00
|
|
|
static void compute_display_area(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
XVContext *xv = s->priv_data;
|
|
|
|
AVRational sar, dar; /* sample and display aspect ratios */
|
|
|
|
AVStream *st = s->streams[0];
|
|
|
|
AVCodecContext *encctx = st->codec;
|
|
|
|
|
|
|
|
/* compute overlay width and height from the codec context information */
|
|
|
|
sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
|
|
|
|
dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
|
|
|
|
|
|
|
|
/* we suppose the screen has a 1/1 sample aspect ratio */
|
|
|
|
/* fit in the window */
|
|
|
|
if (av_cmp_q(dar, (AVRational){ xv->dest_w, xv->dest_h }) > 0) {
|
|
|
|
/* fit in width */
|
|
|
|
xv->dest_y = xv->dest_h;
|
|
|
|
xv->dest_x = 0;
|
|
|
|
xv->dest_h = av_rescale(xv->dest_w, dar.den, dar.num);
|
|
|
|
xv->dest_y -= xv->dest_h;
|
|
|
|
xv->dest_y /= 2;
|
|
|
|
} else {
|
|
|
|
/* fit in height */
|
|
|
|
xv->dest_x = xv->dest_w;
|
|
|
|
xv->dest_y = 0;
|
|
|
|
xv->dest_w = av_rescale(xv->dest_h, dar.num, dar.den);
|
|
|
|
xv->dest_x -= xv->dest_w;
|
|
|
|
xv->dest_x /= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 23:54:21 +00:00
|
|
|
static int xv_repaint(AVFormatContext *s)
|
2013-05-14 11:44:29 +00:00
|
|
|
{
|
|
|
|
XVContext *xv = s->priv_data;
|
|
|
|
XWindowAttributes window_attrs;
|
|
|
|
|
|
|
|
XGetWindowAttributes(xv->display, xv->window, &window_attrs);
|
2014-04-18 17:31:52 +00:00
|
|
|
if (window_attrs.width != xv->window_width || window_attrs.height != xv->window_height) {
|
|
|
|
XRectangle rect[2];
|
|
|
|
xv->dest_w = window_attrs.width;
|
|
|
|
xv->dest_h = window_attrs.height;
|
|
|
|
compute_display_area(s);
|
|
|
|
if (xv->dest_x) {
|
|
|
|
rect[0].width = rect[1].width = xv->dest_x;
|
|
|
|
rect[0].height = rect[1].height = window_attrs.height;
|
|
|
|
rect[0].y = rect[1].y = 0;
|
|
|
|
rect[0].x = 0;
|
|
|
|
rect[1].x = xv->dest_w + xv->dest_x;
|
|
|
|
XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
|
|
|
|
}
|
|
|
|
if (xv->dest_y) {
|
|
|
|
rect[0].width = rect[1].width = window_attrs.width;
|
|
|
|
rect[0].height = rect[1].height = xv->dest_y;
|
|
|
|
rect[0].x = rect[1].x = 0;
|
|
|
|
rect[0].y = 0;
|
|
|
|
rect[1].y = xv->dest_h + xv->dest_y;
|
|
|
|
XFillRectangles(xv->display, xv->window, xv->gc, rect, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-14 11:44:29 +00:00
|
|
|
if (XvShmPutImage(xv->display, xv->xv_port, xv->window, xv->gc,
|
2014-04-18 17:31:52 +00:00
|
|
|
xv->yuv_image, 0, 0, xv->image_width, xv->image_height,
|
|
|
|
xv->dest_x, xv->dest_y, xv->dest_w, xv->dest_h, True) != Success) {
|
2013-05-14 11:44:29 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Could not copy image to XV shared memory buffer\n");
|
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-18 23:54:21 +00:00
|
|
|
static int write_picture(AVFormatContext *s, AVPicture *pict)
|
|
|
|
{
|
|
|
|
XVContext *xv = s->priv_data;
|
|
|
|
XvImage *img = xv->yuv_image;
|
|
|
|
uint8_t *data[3] = {
|
|
|
|
img->data + img->offsets[0],
|
|
|
|
img->data + img->offsets[1],
|
|
|
|
img->data + img->offsets[2]
|
|
|
|
};
|
|
|
|
av_image_copy(data, img->pitches, (const uint8_t **)pict->data, pict->linesize,
|
|
|
|
xv->image_format, img->width, img->height);
|
|
|
|
return xv_repaint(s);
|
|
|
|
}
|
|
|
|
|
2014-01-01 11:05:48 +00:00
|
|
|
static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AVPicture pict;
|
|
|
|
AVCodecContext *ctx = s->streams[0]->codec;
|
|
|
|
|
|
|
|
avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height);
|
|
|
|
return write_picture(s, &pict);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xv_write_frame(AVFormatContext *s, int stream_index, AVFrame **frame,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
/* xv_write_header() should have accepted only supported formats */
|
|
|
|
if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
|
|
|
|
return 0;
|
|
|
|
return write_picture(s, (AVPicture *)*frame);
|
|
|
|
}
|
|
|
|
|
2014-04-18 23:54:21 +00:00
|
|
|
static int xv_control_message(AVFormatContext *s, int type, void *data, size_t data_size)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case AV_APP_TO_DEV_WINDOW_REPAINT:
|
|
|
|
return xv_repaint(s);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
2013-05-14 11:44:29 +00:00
|
|
|
#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 },
|
2014-04-18 23:53:20 +00:00
|
|
|
{ "window_id", "set existing window id", OFFSET(window_id), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
|
2013-05-14 11:44:29 +00:00
|
|
|
{ "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,
|
2014-02-22 22:32:51 +00:00
|
|
|
.category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
|
2013-05-14 11:44:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2014-01-01 11:05:48 +00:00
|
|
|
.write_uncoded_frame = xv_write_frame,
|
2013-05-14 11:44:29 +00:00
|
|
|
.write_trailer = xv_write_trailer,
|
2014-04-18 23:54:21 +00:00
|
|
|
.control_message = xv_control_message,
|
2013-05-14 11:44:29 +00:00
|
|
|
.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
|
|
|
|
.priv_class = &xv_class,
|
|
|
|
};
|