Hello, I really like the libavdevice code. I implemented an X11 output device for libavdevice. I would like to request if it's possible to add this to the main branch? It uses XVideo extension for hardware acceleration and it supports window resizing. To use (with video and audio output): ffmpeg -i <input file> -f x11 "Output" -f alsa "default" Here's my code: libavdevice/x11.c: /* * Copyright (c) 2011 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 */ #include "libavutil/avstring.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "avdevice.h" #include <X11/Xlib.h> #include <X11/extensions/Xv.h> #include <X11/extensions/Xvlib.h> #include <sys/shm.h> typedef struct { GC gc; Window window; Display* dpy; XvImage* yuv_image[1]; XShmSegmentInfo yuv_shminfo[1]; int xv_port, width, height; } X11Data; typedef struct { AVClass *class; char *window_title; X11Data *data; } X11Context; #define OFFSET(x) offsetof(X11Context,x) static int x11_write_header(AVFormatContext *s) { X11Context *x11 = s->priv_data; X11Data *data = (X11Data*)calloc(1,sizeof(X11Data)); unsigned int p_num_adaptors; int j; XvAdaptorInfo *ai; AVCodecContext *pCodecCtx = s->streams[0]->codec; x11->data = data; if (!x11->window_title) x11->window_title = av_strdup(""); data->width = pCodecCtx->width; data->height = pCodecCtx->height; data->dpy = XOpenDisplay(NULL); data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); XStoreName(data->dpy, data->window, x11->window_title); XMapWindow(data->dpy, data->window); XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai); data->xv_port = ai[0].base_id; data->gc = XCreateGC(data->dpy, data->window, 0, 0); for (j = 0; j<1; j++) { data->yuv_image[j] = XvShmCreateImage(data->dpy, data->xv_port+j, ('I'|('4'<<8)|('2'<<16)|'0'<<24) , 0, data->width, data->height, &data->yuv_shminfo[j]); data->yuv_shminfo[j].shmid = shmget(IPC_PRIVATE, data->yuv_image[j]->data_size, IPC_CREAT | 0777); data->yuv_image[j]->data = (char*)shmat(data->yuv_shminfo[j].shmid, 0, 0); data->yuv_shminfo[j].shmaddr = data->yuv_image[j]->data; data->yuv_shminfo[j].readOnly = False; } XShmAttach(data->dpy, &data->yuv_shminfo[0]); return 0; } static int x11_write_packet(AVFormatContext *s, AVPacket *pkt) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; XvImage *img = data->yuv_image[0]; int y, h = img->height / 2; XWindowAttributes attr; AVPicture pict; AVCodecContext *pCodecCtx = s->streams[0]->codec; avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); for(y = 0; y < img->height; y++) { memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); } for(y = 0; y < h; ++y) { memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); } XGetWindowAttributes(data->dpy, data->window,&attr); XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image[0],0, 0, data->width, data->height,0, 0, attr.width,attr.height, True); XSync(data->dpy,True); return 0; } static int x11_write_trailer(AVFormatContext *s) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; int j; av_freep(&x11->window_title); XShmDetach(data->dpy,&data->yuv_shminfo[0]); for (j = 0; j<1; j++) { shmdt(data->yuv_image[j]->data); XFree(data->yuv_image[j]); } XCloseDisplay(data->dpy); return 0; } static const AVOption options[] = { { "window_title", "set X11 window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, { NULL }, }; static const AVClass x11_class = { .class_name = "x11 outdev", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT, }; AVOutputFormat ff_x11_muxer = { .name = "x11", .long_name = NULL_IF_CONFIG_SMALL("x11 output device"), .priv_data_size = sizeof(X11Context), .audio_codec = AV_CODEC_ID_NONE, .video_codec = AV_CODEC_ID_RAWVIDEO, .write_header = x11_write_header, .write_packet = x11_write_packet, .write_trailer = x11_write_trailer, .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, .priv_class = &x11_class, }; .... here's the other modifications: libavdevice/alldevices.c: void avdevice_register_all(void) { ... REGISTER_OUTDEV (X11, x11); ... } libavdevice/Makefile: ... OBJS-$(CONFIG_X11_OUTDEV) += x11.o ffmpeg/config.mak: EXTRALIBS=... -lX11 -lXv ... ffmpeg/config.h: ... #define CONFIG_X11_OUTDEV 1 Thanks, Jeff
Hi, one more thing I forgot in my code, freeing the pointer: static int x11_write_trailer(AVFormatContext *s) { ... if (x11->data) free(x11->data); return 0; } ________________________________________ From: Moguillansky, Jeff Sent: Wednesday, April 10, 2013 4:10 PM To: ffmpeg-devel@ffmpeg.org Subject: x11 output device for libavdevice Hello, I really like the libavdevice code. I implemented an X11 output device for libavdevice. I would like to request if it's possible to add this to the main branch? It uses XVideo extension for hardware acceleration and it supports window resizing. To use (with video and audio output): ffmpeg -i <input file> -f x11 "Output" -f alsa "default" Here's my code: libavdevice/x11.c: /* * Copyright (c) 2011 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 */ #include "libavutil/avstring.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "avdevice.h" #include <X11/Xlib.h> #include <X11/extensions/Xv.h> #include <X11/extensions/Xvlib.h> #include <sys/shm.h> typedef struct { GC gc; Window window; Display* dpy; XvImage* yuv_image[1]; XShmSegmentInfo yuv_shminfo[1]; int xv_port, width, height; } X11Data; typedef struct { AVClass *class; char *window_title; X11Data *data; } X11Context; #define OFFSET(x) offsetof(X11Context,x) static int x11_write_header(AVFormatContext *s) { X11Context *x11 = s->priv_data; X11Data *data = (X11Data*)calloc(1,sizeof(X11Data)); unsigned int p_num_adaptors; int j; XvAdaptorInfo *ai; AVCodecContext *pCodecCtx = s->streams[0]->codec; x11->data = data; if (!x11->window_title) x11->window_title = av_strdup(""); data->width = pCodecCtx->width; data->height = pCodecCtx->height; data->dpy = XOpenDisplay(NULL); data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); XStoreName(data->dpy, data->window, x11->window_title); XMapWindow(data->dpy, data->window); XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai); data->xv_port = ai[0].base_id; data->gc = XCreateGC(data->dpy, data->window, 0, 0); for (j = 0; j<1; j++) { data->yuv_image[j] = XvShmCreateImage(data->dpy, data->xv_port+j, ('I'|('4'<<8)|('2'<<16)|'0'<<24) , 0, data->width, data->height, &data->yuv_shminfo[j]); data->yuv_shminfo[j].shmid = shmget(IPC_PRIVATE, data->yuv_image[j]->data_size, IPC_CREAT | 0777); data->yuv_image[j]->data = (char*)shmat(data->yuv_shminfo[j].shmid, 0, 0); data->yuv_shminfo[j].shmaddr = data->yuv_image[j]->data; data->yuv_shminfo[j].readOnly = False; } XShmAttach(data->dpy, &data->yuv_shminfo[0]); return 0; } static int x11_write_packet(AVFormatContext *s, AVPacket *pkt) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; XvImage *img = data->yuv_image[0]; int y, h = img->height / 2; XWindowAttributes attr; AVPicture pict; AVCodecContext *pCodecCtx = s->streams[0]->codec; avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); for(y = 0; y < img->height; y++) { memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); } for(y = 0; y < h; ++y) { memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); } XGetWindowAttributes(data->dpy, data->window,&attr); XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image[0],0, 0, data->width, data->height,0, 0, attr.width,attr.height, True); XSync(data->dpy,True); return 0; } static int x11_write_trailer(AVFormatContext *s) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; int j; av_freep(&x11->window_title); XShmDetach(data->dpy,&data->yuv_shminfo[0]); for (j = 0; j<1; j++) { shmdt(data->yuv_image[j]->data); XFree(data->yuv_image[j]); } XCloseDisplay(data->dpy); return 0; } static const AVOption options[] = { { "window_title", "set X11 window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, { NULL }, }; static const AVClass x11_class = { .class_name = "x11 outdev", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT, }; AVOutputFormat ff_x11_muxer = { .name = "x11", .long_name = NULL_IF_CONFIG_SMALL("x11 output device"), .priv_data_size = sizeof(X11Context), .audio_codec = AV_CODEC_ID_NONE, .video_codec = AV_CODEC_ID_RAWVIDEO, .write_header = x11_write_header, .write_packet = x11_write_packet, .write_trailer = x11_write_trailer, .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, .priv_class = &x11_class, }; .... here's the other modifications: libavdevice/alldevices.c: void avdevice_register_all(void) { ... REGISTER_OUTDEV (X11, x11); ... } libavdevice/Makefile: ... OBJS-$(CONFIG_X11_OUTDEV) += x11.o ffmpeg/config.mak: EXTRALIBS=... -lX11 -lXv ... ffmpeg/config.h: ... #define CONFIG_X11_OUTDEV 1 Thanks, Jeff
On 4/10/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Hello, I really like the libavdevice code. I implemented an X11 output device for libavdevice. I would like to request if it's possible to add this to the main branch? It uses XVideo extension for hardware acceleration and it supports window resizing.
To use (with video and audio output): ffmpeg -i <input file> -f x11 "Output" -f alsa "default"
Here's my code:
First normal way to send code is via patch.
libavdevice/x11.c:
/* * Copyright (c) 2011 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 */
#include "libavutil/avstring.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "avdevice.h" #include <X11/Xlib.h> #include <X11/extensions/Xv.h> #include <X11/extensions/Xvlib.h> #include <sys/shm.h>
Wrong order of headers. <> ones goes first.
typedef struct { GC gc; Window window; Display* dpy; XvImage* yuv_image[1]; XShmSegmentInfo yuv_shminfo[1]; int xv_port, width, height; } X11Data;
typedef struct { AVClass *class; char *window_title; X11Data *data; } X11Context;
#define OFFSET(x) offsetof(X11Context,x)
static int x11_write_header(AVFormatContext *s) {
If you looked at other files in libavdevice, you would notice they do not use such coding style.
X11Context *x11 = s->priv_data; X11Data *data = (X11Data*)calloc(1,sizeof(X11Data)); unsigned int p_num_adaptors; int j; XvAdaptorInfo *ai; AVCodecContext *pCodecCtx = s->streams[0]->codec; x11->data = data; if (!x11->window_title) x11->window_title = av_strdup(""); data->width = pCodecCtx->width; data->height = pCodecCtx->height; data->dpy = XOpenDisplay(NULL); data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); XStoreName(data->dpy, data->window, x11->window_title); XMapWindow(data->dpy, data->window); XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai); data->xv_port = ai[0].base_id; data->gc = XCreateGC(data->dpy, data->window, 0, 0);
Can any of those fail? If yes, it will segv on next call which is far from being robust.
for (j = 0; j<1; j++) {
What is point of this loop?
data->yuv_image[j] = XvShmCreateImage(data->dpy, data->xv_port+j, ('I'|('4'<<8)|('2'<<16)|'0'<<24) , 0, data->width, data->height,
Probably other format can be queried and supported to. Beside there is nicer way to write I420.
&data->yuv_shminfo[j]); data->yuv_shminfo[j].shmid = shmget(IPC_PRIVATE, data->yuv_image[j]->data_size, IPC_CREAT | 0777); data->yuv_image[j]->data = (char*)shmat(data->yuv_shminfo[j].shmid, 0, 0); data->yuv_shminfo[j].shmaddr = data->yuv_image[j]->data; data->yuv_shminfo[j].readOnly = False; } XShmAttach(data->dpy, &data->yuv_shminfo[0]); return 0; }
static int x11_write_packet(AVFormatContext *s, AVPacket *pkt) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; XvImage *img = data->yuv_image[0]; int y, h = img->height / 2; XWindowAttributes attr; AVPicture pict; AVCodecContext *pCodecCtx = s->streams[0]->codec; avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); for(y = 0; y < img->height; y++) { memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); } for(y = 0; y < h; ++y) { memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); } XGetWindowAttributes(data->dpy, data->window,&attr); XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image[0],0, 0, data->width, data->height,0, 0, attr.width,attr.height, True); XSync(data->dpy,True); return 0; }
static int x11_write_trailer(AVFormatContext *s) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; int j; av_freep(&x11->window_title); XShmDetach(data->dpy,&data->yuv_shminfo[0]); for (j = 0; j<1; j++) { shmdt(data->yuv_image[j]->data); XFree(data->yuv_image[j]); } XCloseDisplay(data->dpy);
return 0; }
static const AVOption options[] = { { "window_title", "set X11 window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, { NULL }, };
static const AVClass x11_class = { .class_name = "x11 outdev", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT, };
AVOutputFormat ff_x11_muxer = { .name = "x11", .long_name = NULL_IF_CONFIG_SMALL("x11 output device"), .priv_data_size = sizeof(X11Context), .audio_codec = AV_CODEC_ID_NONE, .video_codec = AV_CODEC_ID_RAWVIDEO, .write_header = x11_write_header, .write_packet = x11_write_packet, .write_trailer = x11_write_trailer, .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, .priv_class = &x11_class, };
.... here's the other modifications: libavdevice/alldevices.c: void avdevice_register_all(void) { ... REGISTER_OUTDEV (X11, x11); ... }
libavdevice/Makefile: ... OBJS-$(CONFIG_X11_OUTDEV) += x11.o
ffmpeg/config.mak: EXTRALIBS=... -lX11 -lXv ...
ffmpeg/config.h: ... #define CONFIG_X11_OUTDEV 1
Thanks, Jeff
Please tell us if you do NOT plan to work on above mentioned code issues.
Thanks I'll learn how to send a patch, and make modifications you mentioned below. I'll add error handling, and get rid of the for loop. -Jeff ________________________________________ From: ffmpeg-devel-bounces@ffmpeg.org [ffmpeg-devel-bounces@ffmpeg.org] on behalf of Paul B Mahol [onemda@gmail.com] Sent: Wednesday, April 10, 2013 4:30 PM To: FFmpeg development discussions and patches Subject: Re: [FFmpeg-devel] x11 output device for libavdevice On 4/10/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Hello, I really like the libavdevice code. I implemented an X11 output device for libavdevice. I would like to request if it's possible to add this to the main branch? It uses XVideo extension for hardware acceleration and it supports window resizing.
To use (with video and audio output): ffmpeg -i <input file> -f x11 "Output" -f alsa "default"
Here's my code:
First normal way to send code is via patch.
libavdevice/x11.c:
/* * Copyright (c) 2011 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 */
#include "libavutil/avstring.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "avdevice.h" #include <X11/Xlib.h> #include <X11/extensions/Xv.h> #include <X11/extensions/Xvlib.h> #include <sys/shm.h>
Wrong order of headers. <> ones goes first.
typedef struct { GC gc; Window window; Display* dpy; XvImage* yuv_image[1]; XShmSegmentInfo yuv_shminfo[1]; int xv_port, width, height; } X11Data;
typedef struct { AVClass *class; char *window_title; X11Data *data; } X11Context;
#define OFFSET(x) offsetof(X11Context,x)
static int x11_write_header(AVFormatContext *s) {
If you looked at other files in libavdevice, you would notice they do not use such coding style.
X11Context *x11 = s->priv_data; X11Data *data = (X11Data*)calloc(1,sizeof(X11Data)); unsigned int p_num_adaptors; int j; XvAdaptorInfo *ai; AVCodecContext *pCodecCtx = s->streams[0]->codec; x11->data = data; if (!x11->window_title) x11->window_title = av_strdup(""); data->width = pCodecCtx->width; data->height = pCodecCtx->height; data->dpy = XOpenDisplay(NULL); data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); XStoreName(data->dpy, data->window, x11->window_title); XMapWindow(data->dpy, data->window); XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai); data->xv_port = ai[0].base_id; data->gc = XCreateGC(data->dpy, data->window, 0, 0);
Can any of those fail? If yes, it will segv on next call which is far from being robust.
for (j = 0; j<1; j++) {
What is point of this loop?
data->yuv_image[j] = XvShmCreateImage(data->dpy, data->xv_port+j, ('I'|('4'<<8)|('2'<<16)|'0'<<24) , 0, data->width, data->height,
Probably other format can be queried and supported to. Beside there is nicer way to write I420.
&data->yuv_shminfo[j]); data->yuv_shminfo[j].shmid = shmget(IPC_PRIVATE, data->yuv_image[j]->data_size, IPC_CREAT | 0777); data->yuv_image[j]->data = (char*)shmat(data->yuv_shminfo[j].shmid, 0, 0); data->yuv_shminfo[j].shmaddr = data->yuv_image[j]->data; data->yuv_shminfo[j].readOnly = False; } XShmAttach(data->dpy, &data->yuv_shminfo[0]); return 0; }
static int x11_write_packet(AVFormatContext *s, AVPacket *pkt) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; XvImage *img = data->yuv_image[0]; int y, h = img->height / 2; XWindowAttributes attr; AVPicture pict; AVCodecContext *pCodecCtx = s->streams[0]->codec; avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); for(y = 0; y < img->height; y++) { memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); } for(y = 0; y < h; ++y) { memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); } XGetWindowAttributes(data->dpy, data->window,&attr); XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image[0],0, 0, data->width, data->height,0, 0, attr.width,attr.height, True); XSync(data->dpy,True); return 0; }
static int x11_write_trailer(AVFormatContext *s) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; int j; av_freep(&x11->window_title); XShmDetach(data->dpy,&data->yuv_shminfo[0]); for (j = 0; j<1; j++) { shmdt(data->yuv_image[j]->data); XFree(data->yuv_image[j]); } XCloseDisplay(data->dpy);
return 0; }
static const AVOption options[] = { { "window_title", "set X11 window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, { NULL }, };
static const AVClass x11_class = { .class_name = "x11 outdev", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT, };
AVOutputFormat ff_x11_muxer = { .name = "x11", .long_name = NULL_IF_CONFIG_SMALL("x11 output device"), .priv_data_size = sizeof(X11Context), .audio_codec = AV_CODEC_ID_NONE, .video_codec = AV_CODEC_ID_RAWVIDEO, .write_header = x11_write_header, .write_packet = x11_write_packet, .write_trailer = x11_write_trailer, .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, .priv_class = &x11_class, };
.... here's the other modifications: libavdevice/alldevices.c: void avdevice_register_all(void) { ... REGISTER_OUTDEV (X11, x11); ... }
libavdevice/Makefile: ... OBJS-$(CONFIG_X11_OUTDEV) += x11.o
ffmpeg/config.mak: EXTRALIBS=... -lX11 -lXv ...
ffmpeg/config.h: ... #define CONFIG_X11_OUTDEV 1
Thanks, Jeff
Please tell us if you do NOT plan to work on above mentioned code issues. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Hi, Let me know if this is better. Here's a patch file. I added error handling, fixed the includes, and got rid of the for loop. I'm not sure how to modify configure script (I just modified the generated makefiles)? Thanks, Jeff ________________________________________ From: ffmpeg-devel-bounces@ffmpeg.org [ffmpeg-devel-bounces@ffmpeg.org] on behalf of Paul B Mahol [onemda@gmail.com] Sent: Wednesday, April 10, 2013 4:30 PM To: FFmpeg development discussions and patches Subject: Re: [FFmpeg-devel] x11 output device for libavdevice On 4/10/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Hello, I really like the libavdevice code. I implemented an X11 output device for libavdevice. I would like to request if it's possible to add this to the main branch? It uses XVideo extension for hardware acceleration and it supports window resizing.
To use (with video and audio output): ffmpeg -i <input file> -f x11 "Output" -f alsa "default"
Here's my code:
First normal way to send code is via patch.
libavdevice/x11.c:
/* * Copyright (c) 2011 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 */
#include "libavutil/avstring.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "avdevice.h" #include <X11/Xlib.h> #include <X11/extensions/Xv.h> #include <X11/extensions/Xvlib.h> #include <sys/shm.h>
Wrong order of headers. <> ones goes first.
typedef struct { GC gc; Window window; Display* dpy; XvImage* yuv_image[1]; XShmSegmentInfo yuv_shminfo[1]; int xv_port, width, height; } X11Data;
typedef struct { AVClass *class; char *window_title; X11Data *data; } X11Context;
#define OFFSET(x) offsetof(X11Context,x)
static int x11_write_header(AVFormatContext *s) {
If you looked at other files in libavdevice, you would notice they do not use such coding style.
X11Context *x11 = s->priv_data; X11Data *data = (X11Data*)calloc(1,sizeof(X11Data)); unsigned int p_num_adaptors; int j; XvAdaptorInfo *ai; AVCodecContext *pCodecCtx = s->streams[0]->codec; x11->data = data; if (!x11->window_title) x11->window_title = av_strdup(""); data->width = pCodecCtx->width; data->height = pCodecCtx->height; data->dpy = XOpenDisplay(NULL); data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); XStoreName(data->dpy, data->window, x11->window_title); XMapWindow(data->dpy, data->window); XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai); data->xv_port = ai[0].base_id; data->gc = XCreateGC(data->dpy, data->window, 0, 0);
Can any of those fail? If yes, it will segv on next call which is far from being robust.
for (j = 0; j<1; j++) {
What is point of this loop?
data->yuv_image[j] = XvShmCreateImage(data->dpy, data->xv_port+j, ('I'|('4'<<8)|('2'<<16)|'0'<<24) , 0, data->width, data->height,
Probably other format can be queried and supported to. Beside there is nicer way to write I420.
&data->yuv_shminfo[j]); data->yuv_shminfo[j].shmid = shmget(IPC_PRIVATE, data->yuv_image[j]->data_size, IPC_CREAT | 0777); data->yuv_image[j]->data = (char*)shmat(data->yuv_shminfo[j].shmid, 0, 0); data->yuv_shminfo[j].shmaddr = data->yuv_image[j]->data; data->yuv_shminfo[j].readOnly = False; } XShmAttach(data->dpy, &data->yuv_shminfo[0]); return 0; }
static int x11_write_packet(AVFormatContext *s, AVPacket *pkt) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; XvImage *img = data->yuv_image[0]; int y, h = img->height / 2; XWindowAttributes attr; AVPicture pict; AVCodecContext *pCodecCtx = s->streams[0]->codec; avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); for(y = 0; y < img->height; y++) { memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); } for(y = 0; y < h; ++y) { memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); } XGetWindowAttributes(data->dpy, data->window,&attr); XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image[0],0, 0, data->width, data->height,0, 0, attr.width,attr.height, True); XSync(data->dpy,True); return 0; }
static int x11_write_trailer(AVFormatContext *s) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; int j; av_freep(&x11->window_title); XShmDetach(data->dpy,&data->yuv_shminfo[0]); for (j = 0; j<1; j++) { shmdt(data->yuv_image[j]->data); XFree(data->yuv_image[j]); } XCloseDisplay(data->dpy);
return 0; }
static const AVOption options[] = { { "window_title", "set X11 window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, { NULL }, };
static const AVClass x11_class = { .class_name = "x11 outdev", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT, };
AVOutputFormat ff_x11_muxer = { .name = "x11", .long_name = NULL_IF_CONFIG_SMALL("x11 output device"), .priv_data_size = sizeof(X11Context), .audio_codec = AV_CODEC_ID_NONE, .video_codec = AV_CODEC_ID_RAWVIDEO, .write_header = x11_write_header, .write_packet = x11_write_packet, .write_trailer = x11_write_trailer, .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, .priv_class = &x11_class, };
.... here's the other modifications: libavdevice/alldevices.c: void avdevice_register_all(void) { ... REGISTER_OUTDEV (X11, x11); ... }
libavdevice/Makefile: ... OBJS-$(CONFIG_X11_OUTDEV) += x11.o
ffmpeg/config.mak: EXTRALIBS=... -lX11 -lXv ...
ffmpeg/config.h: ... #define CONFIG_X11_OUTDEV 1
Thanks, Jeff
Please tell us if you do NOT plan to work on above mentioned code issues. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On 4/11/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Hi, Let me know if this is better. Here's a patch file. I added error handling, fixed the includes, and got rid of the for loop. I'm not sure how to modify configure script (I just modified the generated makefiles)?
Thanks, Jeff
Please stop top posting. Also read http://ffmpeg.org/contact.html
From f6e30e8180c01e655f87c033b518cdf009e2ebe2 Mon Sep 17 00:00:00 2001 From: JeffMoguillansky <Jeff.Moguillansky@am.sony.com> Date: Wed, 10 Apr 2013 17:49:48 -0700 Subject: [PATCH 1/2] Implementing x11 output device
Sqush both patches into one, no point in separation.
--- libavdevice/x11.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 libavdevice/x11.c
diff --git a/libavdevice/x11.c b/libavdevice/x11.c new file mode 100644 index 0000000..8d6bd37 --- /dev/null +++ b/libavdevice/x11.c @@ -0,0 +1,135 @@ +/* + * 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 + */ + +#include <X11/Xlib.h> +#include <X11/extensions/Xv.h> +#include <X11/extensions/Xvlib.h> +#include <sys/shm.h> +#include <xorg/fourcc.h>
I dont have this header installed. When I said there is better way to write I420 I expected you to reuse ffmpeg code.
+#include "libavutil/avstring.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "avdevice.h" + +typedef struct { + GC gc; Window window; Display* dpy; XvImage* yuv_image; XShmSegmentInfo yuv_shminfo; + int xv_port, width, height;
Tabs are not allowed at all in code.
+} X11Data; + +typedef struct { + AVClass *class; + char *window_title; + X11Data *data; +} X11Context; + +#define OFFSET(x) offsetof(X11Context,x) +#define V(s) if (!(s)) { av_log(NULL, AV_LOG_ERROR, "[%s][%d]: ERROR: %s failed\n", __FUNCTION__,__LINE__,#s); return AVERROR_UNKNOWN; } + +static int x11_write_header(AVFormatContext *s) {
Again style issue.
+ X11Context *x11 = s->priv_data;
Weird identation.
+ X11Data *data = (X11Data*)calloc(1,sizeof(X11Data)); + unsigned int p_num_adaptors; + XvAdaptorInfo *ai = NULL; + AVCodecContext *pCodecCtx = s->streams[0]->codec; + x11->data = data; + if (!x11->window_title) x11->window_title = av_strdup(""); + data->width = pCodecCtx->width; data->height = pCodecCtx->height; + V((data->dpy = XOpenDisplay(NULL)) != NULL); + data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); + XStoreName(data->dpy, data->window, x11->window_title); + XMapWindow(data->dpy, data->window); + V(XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai) == Success); + data->xv_port = ai[0].base_id; + V((data->gc = XCreateGC(data->dpy, data->window, 0, 0)) != NULL); + V((data->yuv_image = XvShmCreateImage(data->dpy, data->xv_port, FOURCC_I420 , 0, data->width, data->height, &data->yuv_shminfo)) != NULL); + V((data->yuv_shminfo.shmid = shmget(IPC_PRIVATE, data->yuv_image->data_size, IPC_CREAT | 0777)) != -1); + V((data->yuv_image->data = (char*)shmat(data->yuv_shminfo.shmid, 0, 0)) != NULL); + data->yuv_shminfo.shmaddr = data->yuv_image->data; + data->yuv_shminfo.readOnly = False; + V(XShmAttach(data->dpy, &data->yuv_shminfo) != 0); + return 0; +} + +static int x11_write_packet(AVFormatContext *s, AVPacket *pkt) { + X11Context *x11 = NULL; + X11Data *data = NULL; + XvImage *img = NULL; + int y, h; + XWindowAttributes attr; + AVPicture pict; + AVCodecContext *pCodecCtx = s->streams[0]->codec; + V((x11 = s->priv_data) != NULL); + V((data = x11->data) != NULL); + V((img = data->yuv_image) != NULL); + h = img->height / 2; + avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); + for(y = 0; y < img->height; y++) { + memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); + } + for(y = 0; y < h; ++y) { + memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); + memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); + } + XGetWindowAttributes(data->dpy, data->window,&attr); + V(XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image,0, 0, data->width, data->height,0, 0, attr.width,attr.height, True) == Success); + XSync(data->dpy,True); + return 0; +} + +static int x11_write_trailer(AVFormatContext *s) { + X11Context *x11 = NULL; + X11Data *data = NULL; + V((x11 = s->priv_data) != NULL); + V((data = x11->data) != NULL); + av_freep(&x11->window_title); + XShmDetach(data->dpy,&data->yuv_shminfo); + shmdt(data->yuv_image->data); + XFree(data->yuv_image); + XCloseDisplay(data->dpy); + if (x11->data) { free(x11->data); x11->data = NULL; } + + return 0; +} + +static const AVOption options[] = { + { "window_title", "set X11 window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { NULL }, +}; + +static const AVClass x11_class = { + .class_name = "x11 outdev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVOutputFormat ff_x11_muxer = { + .name = "x11",
but you are using XV extension, and not using x11 at all. So device,file, etc should be named xv instead.
+ .long_name = NULL_IF_CONFIG_SMALL("x11 output device"), + .priv_data_size = sizeof(X11Context), + .audio_codec = AV_CODEC_ID_NONE, + .video_codec = AV_CODEC_ID_RAWVIDEO, + .write_header = x11_write_header, + .write_packet = x11_write_packet, + .write_trailer = x11_write_trailer, + .flags = AVFMT_NOFILE, + .priv_class = &x11_class, +}; -- 1.7.10.4
From 2506039ecc826d05e5bbe05a96313cf3257c3d0d Mon Sep 17 00:00:00 2001 From: JeffMoguillansky <Jeff.Moguillansky@am.sony.com> Date: Wed, 10 Apr 2013 17:56:21 -0700 Subject: [PATCH 2/2] Implementing x11 output
As already mentioned this one should be squashed in previous commit.
--- libavdevice/Makefile | 1 + libavdevice/alldevices.c | 1 + 2 files changed, 2 insertions(+)
diff --git a/libavdevice/Makefile b/libavdevice/Makefile index efffa8b..8af4079 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile OBJS-$(CONFIG_OSS_OUTDEV) += oss_audio.o OBJS-$(CONFIG_PULSE_INDEV) += pulse.o OBJS-$(CONFIG_SDL_OUTDEV) += sdl.o +OBJS-$(CONFIG_X11_OUTDEV) += x11.o
Broken alphabetical order.
OBJS-$(CONFIG_SNDIO_INDEV) += sndio_common.o sndio_dec.o OBJS-$(CONFIG_SNDIO_OUTDEV) += sndio_common.o sndio_enc.o OBJS-$(CONFIG_V4L2_INDEV) += v4l2.o timefilter.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index daa6638..e367423 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -59,6 +59,7 @@ void avdevice_register_all(void) REGISTER_INOUTDEV(OSS, oss); REGISTER_INDEV (PULSE, pulse); REGISTER_OUTDEV (SDL, sdl); + REGISTER_OUTDEV (x11, x11);
Again tabs, and broken vertical alignment
REGISTER_INOUTDEV(SNDIO, sndio); REGISTER_INDEV (V4L2, v4l2); // REGISTER_INDEV (V4L, v4l -- 1.7.10.4
________________________________________ From: ffmpeg-devel-bounces@ffmpeg.org [ffmpeg-devel-bounces@ffmpeg.org] on behalf of Paul B Mahol [onemda@gmail.com] Sent: Wednesday, April 10, 2013 4:30 PM To: FFmpeg development discussions and patches Subject: Re: [FFmpeg-devel] x11 output device for libavdevice
On 4/10/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Hello, I really like the libavdevice code. I implemented an X11 output device for libavdevice. I would like to request if it's possible to add this to the main branch? It uses XVideo extension for hardware acceleration and it supports window resizing.
To use (with video and audio output): ffmpeg -i <input file> -f x11 "Output" -f alsa "default"
Here's my code:
First normal way to send code is via patch.
libavdevice/x11.c:
/* * Copyright (c) 2011 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 */
#include "libavutil/avstring.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "avdevice.h" #include <X11/Xlib.h> #include <X11/extensions/Xv.h> #include <X11/extensions/Xvlib.h> #include <sys/shm.h>
Wrong order of headers. <> ones goes first.
typedef struct { GC gc; Window window; Display* dpy; XvImage* yuv_image[1]; XShmSegmentInfo yuv_shminfo[1]; int xv_port, width, height; } X11Data;
typedef struct { AVClass *class; char *window_title; X11Data *data; } X11Context;
#define OFFSET(x) offsetof(X11Context,x)
static int x11_write_header(AVFormatContext *s) {
If you looked at other files in libavdevice, you would notice they do not use such coding style.
X11Context *x11 = s->priv_data; X11Data *data = (X11Data*)calloc(1,sizeof(X11Data)); unsigned int p_num_adaptors; int j; XvAdaptorInfo *ai; AVCodecContext *pCodecCtx = s->streams[0]->codec; x11->data = data; if (!x11->window_title) x11->window_title = av_strdup(""); data->width = pCodecCtx->width; data->height = pCodecCtx->height; data->dpy = XOpenDisplay(NULL); data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); XStoreName(data->dpy, data->window, x11->window_title); XMapWindow(data->dpy, data->window); XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai); data->xv_port = ai[0].base_id; data->gc = XCreateGC(data->dpy, data->window, 0, 0);
Can any of those fail? If yes, it will segv on next call which is far from being robust.
for (j = 0; j<1; j++) {
What is point of this loop?
data->yuv_image[j] = XvShmCreateImage(data->dpy, data->xv_port+j, ('I'|('4'<<8)|('2'<<16)|'0'<<24) , 0, data->width, data->height,
Probably other format can be queried and supported to. Beside there is nicer way to write I420.
&data->yuv_shminfo[j]); data->yuv_shminfo[j].shmid = shmget(IPC_PRIVATE, data->yuv_image[j]->data_size, IPC_CREAT | 0777); data->yuv_image[j]->data = (char*)shmat(data->yuv_shminfo[j].shmid, 0, 0); data->yuv_shminfo[j].shmaddr = data->yuv_image[j]->data; data->yuv_shminfo[j].readOnly = False; } XShmAttach(data->dpy, &data->yuv_shminfo[0]); return 0; }
static int x11_write_packet(AVFormatContext *s, AVPacket *pkt) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; XvImage *img = data->yuv_image[0]; int y, h = img->height / 2; XWindowAttributes attr; AVPicture pict; AVCodecContext *pCodecCtx = s->streams[0]->codec; avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); for(y = 0; y < img->height; y++) { memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); } for(y = 0; y < h; ++y) { memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); } XGetWindowAttributes(data->dpy, data->window,&attr); XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image[0],0, 0, data->width, data->height,0, 0, attr.width,attr.height, True); XSync(data->dpy,True); return 0; }
static int x11_write_trailer(AVFormatContext *s) { X11Context *x11 = s->priv_data; X11Data *data = x11->data; int j; av_freep(&x11->window_title); XShmDetach(data->dpy,&data->yuv_shminfo[0]); for (j = 0; j<1; j++) { shmdt(data->yuv_image[j]->data); XFree(data->yuv_image[j]); } XCloseDisplay(data->dpy);
return 0; }
static const AVOption options[] = { { "window_title", "set X11 window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, { NULL }, };
static const AVClass x11_class = { .class_name = "x11 outdev", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT, };
AVOutputFormat ff_x11_muxer = { .name = "x11", .long_name = NULL_IF_CONFIG_SMALL("x11 output device"), .priv_data_size = sizeof(X11Context), .audio_codec = AV_CODEC_ID_NONE, .video_codec = AV_CODEC_ID_RAWVIDEO, .write_header = x11_write_header, .write_packet = x11_write_packet, .write_trailer = x11_write_trailer, .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, .priv_class = &x11_class, };
.... here's the other modifications: libavdevice/alldevices.c: void avdevice_register_all(void) { ... REGISTER_OUTDEV (X11, x11); ... }
libavdevice/Makefile: ... OBJS-$(CONFIG_X11_OUTDEV) += x11.o
ffmpeg/config.mak: EXTRALIBS=... -lX11 -lXv ...
ffmpeg/config.h: ... #define CONFIG_X11_OUTDEV 1
Thanks, Jeff
Please tell us if you do NOT plan to work on above mentioned code issues. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Thanks Paul, here's a new patch version. -Jeff
On 4/11/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Thanks Paul, here's a new patch version. -Jeff
Why you ignored my comments about style issue, and obvious wrong name for avdevice, it uses Xv extensions and it does not use any slower X11 code. Also why you compress code lines too much, it is almost unreadeable.
Hi Paul, I didn't ignore I'm just new to the process. I renamed the device to "xv", and tried to fix the style issues by rewriting the struct, please let me know what are the style issues. If you can tell me what else needs to be done I'd be happy to make changes. Thanks, Jeff
On 4/12/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Hi Paul, I didn't ignore I'm just new to the process. I renamed the device to "xv", and tried to fix the style issues by rewriting the struct, please let me know what are the style issues. If you can tell me what else needs to be done I'd be happy to make changes.
Style issues are not code rewritting, it is just adding newlines and spaces here and there.
Thanks, Jeff
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This one's more readable, let me know if it looks ok. Thanks, Jeff
Sorry that was the wrong file. Please look at this file instead. Thanks, Jeff ________________________________________ From: Moguillansky, Jeff Sent: Friday, April 12, 2013 1:32 PM To: FFmpeg development discussions and patches Subject: RE: [FFmpeg-devel] x11 output device for libavdevice This one's more readable, let me know if it looks ok. Thanks, Jeff
On Fri, Apr 12, 2013 at 08:33:48PM +0000, Moguillansky, Jeff wrote:
Sorry that was the wrong file. Please look at this file instead.
Thanks, Jeff ________________________________________ From: Moguillansky, Jeff Sent: Friday, April 12, 2013 1:32 PM To: FFmpeg development discussions and patches Subject: RE: [FFmpeg-devel] x11 output device for libavdevice
This one's more readable, let me know if it looks ok. Thanks, Jeff
Makefile | 1 alldevices.c | 1 xvideo.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) 1b5cc31d4229439b65e346b386a0fa9b4800fe5a xvideo_output.patch diff --git a/libavdevice/Makefile b/libavdevice/Makefile index efffa8b..83728cc 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -36,6 +36,7 @@ OBJS-$(CONFIG_V4L2_INDEV) += v4l2.o timefilter.o OBJS-$(CONFIG_V4L_INDEV) += v4l.o OBJS-$(CONFIG_VFWCAP_INDEV) += vfwcap.o OBJS-$(CONFIG_X11GRAB_INDEV) += x11grab.o +OBJS-$(CONFIG_XVIDEO_OUTDEV) += xvideo.o
# external libraries OBJS-$(CONFIG_LIBCDIO_INDEV) += libcdio.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index daa6638..5680796 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -64,6 +64,7 @@ void avdevice_register_all(void) // REGISTER_INDEV (V4L, v4l REGISTER_INDEV (VFWCAP, vfwcap); REGISTER_INDEV (X11GRAB, x11grab); + REGISTER_OUTDEV (XVIDEO, xvideo);
/* external libraries */ REGISTER_INDEV (LIBCDIO, libcdio); diff --git a/libavdevice/xvideo.c b/libavdevice/xvideo.c new file mode 100644 index 0000000..cca7567 --- /dev/null +++ b/libavdevice/xvideo.c @@ -0,0 +1,150 @@ +/* + * 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 + */ + +#include <X11/Xlib.h> +#include <X11/extensions/Xv.h> +#include <X11/extensions/Xvlib.h> +#include <sys/shm.h> +#include "libavutil/avstring.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "avdevice.h" +#include "libavfilter/libmpcodecs/img_format.h" + +typedef struct { + GC gc; + Window window; + Display* dpy; + XvImage* yuv_image; + XShmSegmentInfo yuv_shminfo; + int xv_port, width, height; +} XVideoData; + +typedef struct { + AVClass *class; + char *window_title; + XVideoData *data; +} XVideoContext; + +#define OFFSET(x) offsetof(XVideoContext,x) +
+#define ASSERT(s) if (!(s)) { av_log(NULL, AV_LOG_ERROR, "[%s][%d]: ERROR: %s failed\n", __FUNCTION__,__LINE__,#s); return AVERROR_UNKNOWN; }
This is misnamed an assert() aborts execution on failure, thats rather an error check
+ +static int xvideo_write_header(AVFormatContext *s) { + XVideoContext *xvideo = s->priv_data;
+ XVideoData *data = (XVideoData*)calloc(1,sizeof(XVideoData));
unneeded cast, and can av_mallocz() be used ?
+ unsigned int p_num_adaptors; + XvAdaptorInfo *ai = NULL; + AVCodecContext *pCodecCtx = s->streams[0]->codec; + xvideo->data = data;
+ if (!xvideo->window_title) xvideo->window_title = av_strdup("");
+ data->width = pCodecCtx->width; data->height = pCodecCtx->height;
I think some peoplke find it more readable to not put 2 statements on the same line, not that i mind it much personally ... checks on codec type & nb_streams are missing
+ data->dpy = XOpenDisplay(NULL); + ASSERT(data->dpy != NULL); + data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); + XStoreName(data->dpy, data->window, xvideo->window_title); + XMapWindow(data->dpy, data->window); + ASSERT(XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai) == Success); + data->xv_port = ai[0].base_id; + data->gc = XCreateGC(data->dpy, data->window, 0, 0); + ASSERT(data->gc != NULL); + data->yuv_image = XvShmCreateImage(data->dpy, data->xv_port, IMGFMT_I420 , 0, data->width, data->height, &data->yuv_shminfo); + ASSERT(data->yuv_image != NULL); + data->yuv_shminfo.shmid = shmget(IPC_PRIVATE, data->yuv_image->data_size, IPC_CREAT | 0777); + ASSERT(data->yuv_shminfo.shmid != -1); + data->yuv_image->data = (char*)shmat(data->yuv_shminfo.shmid, 0, 0); + ASSERT(data->yuv_image->data != NULL); + data->yuv_shminfo.shmaddr = data->yuv_image->data; + data->yuv_shminfo.readOnly = False; + ASSERT(XShmAttach(data->dpy, &data->yuv_shminfo) != 0); + return 0; +} + +static int xvideo_write_packet(AVFormatContext *s, AVPacket *pkt) { + XVideoContext *xvideo = NULL; + XVideoData *data = NULL; + XvImage *img = NULL; + int y, h; + XWindowAttributes attr; + AVPicture pict; + AVCodecContext *pCodecCtx = s->streams[0]->codec; + xvideo = s->priv_data; + ASSERT(xvideo != NULL); + data = xvideo->data; + ASSERT(data != NULL); + img = data->yuv_image; + ASSERT(img != NULL); + h = img->height / 2; + avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); + for(y = 0; y < img->height; y++) { + memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); + }
a check for pkt->size being large enough is missing here somewhere
+ for(y = 0; y < h; ++y) { + memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); + memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); + } + XGetWindowAttributes(data->dpy, data->window,&attr); + ASSERT(XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image,0, 0, data->width, data->height,0, 0, attr.width,attr.height, True) == Success); + XSync(data->dpy,True); + return 0; +} + +static int xvideo_write_trailer(AVFormatContext *s) { + XVideoContext *xvideo = NULL; + XVideoData *data = NULL; + xvideo = s->priv_data; + ASSERT(xvideo != NULL); + data = xvideo->data; + ASSERT(data != NULL); + av_freep(&xvideo->window_title); + XShmDetach(data->dpy,&data->yuv_shminfo); + shmdt(data->yuv_image->data); + XFree(data->yuv_image); + XCloseDisplay(data->dpy); + if (xvideo->data) { free(xvideo->data); xvideo->data = NULL; } + + return 0; +} + +static const AVOption options[] = { + { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { NULL }, +}; + +static const AVClass xvideo_class = { + .class_name = "xvideo outdev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVOutputFormat ff_xvideo_muxer = { + .name = "xv", + .long_name = NULL_IF_CONFIG_SMALL("XVideo output device"), + .priv_data_size = sizeof(XVideoContext), + .audio_codec = AV_CODEC_ID_NONE, + .video_codec = AV_CODEC_ID_RAWVIDEO, + .write_header = xvideo_write_header, + .write_packet = xvideo_write_packet, + .write_trailer = xvideo_write_trailer, + .flags = AVFMT_NOFILE, + .priv_class = &xvideo_class, +};
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
-- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I have never wished to cater to the crowd; for what I know they do not approve, and what they approve I do not know. -- Epicurus
Thanks, how about this one? Jeff
On Fri, Apr 12, 2013 at 11:31:37PM +0000, Moguillansky, Jeff wrote:
Thanks, how about this one? Jeff
diff --git a/libavdevice/Makefile b/libavdevice/Makefile index efffa8b..83728cc 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -36,6 +36,7 @@ OBJS-$(CONFIG_V4L2_INDEV) += v4l2.o timefilter.o OBJS-$(CONFIG_V4L_INDEV) += v4l.o OBJS-$(CONFIG_VFWCAP_INDEV) += vfwcap.o OBJS-$(CONFIG_X11GRAB_INDEV) += x11grab.o +OBJS-$(CONFIG_XVIDEO_OUTDEV) += xvideo.o
Missing configure x11 dependencies: it will likely break compilation with system without X11.
# external libraries OBJS-$(CONFIG_LIBCDIO_INDEV) += libcdio.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index daa6638..5680796 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -64,6 +64,7 @@ void avdevice_register_all(void) // REGISTER_INDEV (V4L, v4l REGISTER_INDEV (VFWCAP, vfwcap); REGISTER_INDEV (X11GRAB, x11grab); + REGISTER_OUTDEV (XVIDEO, xvideo);
/* external libraries */ REGISTER_INDEV (LIBCDIO, libcdio); diff --git a/libavdevice/xvideo.c b/libavdevice/xvideo.c new file mode 100644 index 0000000..d7a1131 --- /dev/null +++ b/libavdevice/xvideo.c @@ -0,0 +1,155 @@ +/* + * 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 + */ + +#include <X11/Xlib.h> +#include <X11/extensions/Xv.h> +#include <X11/extensions/Xvlib.h> +#include <sys/shm.h> +#include "libavutil/avstring.h" +#include "libavutil/mem.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "avdevice.h"
+#include "libavfilter/libmpcodecs/img_format.h" +
libavdevice must not depend on libavfilter. And especially not in the libmpcodecs wrapper.
+typedef struct { + GC gc; + Window window; + Display* dpy; + XvImage* yuv_image;
+ XShmSegmentInfo yuv_shminfo;
Trailing whitespace here and in various other parts of the file.
+ int xv_port, width, height; +} XVideoData; + +typedef struct { + AVClass *class; + char *window_title; + XVideoData *data; +} XVideoContext; + +#define OFFSET(x) offsetof(XVideoContext,x) + +#define VERIFY(s) if (!(s)) { av_log(NULL, AV_LOG_ERROR, "[%s][%d]: ERROR: %s failed\n", __FUNCTION__,__LINE__,#s); return AVERROR_UNKNOWN; } +
Some meaningful error management instead of this would be greatly appreciated. Also, av_log should be using the context for logging.
+static int xvideo_write_header(AVFormatContext *s) {
style: \n between ) and { for functions.
+ XVideoContext *xvideo = s->priv_data; + XVideoData *data = av_mallocz(sizeof(XVideoData)); + unsigned int p_num_adaptors; + XvAdaptorInfo *ai = NULL; + AVCodecContext *pCodecCtx = s->streams[0]->codec;
pleaseAvoidCamelCaseForVariables Also adding some respiration spacing between declarations and code (here) would be appreciated.
+ VERIFY(pCodecCtx->codec_tag == IMGFMT_I420); + VERIFY(s->nb_streams == 1); + xvideo->data = data;
return AVERROR(ENOMEM) here if !data
+ if (!xvideo->window_title) xvideo->window_title = av_strdup("");
style: please break that line Also return AVERROR(ENOMEM) if xvideo->window_title is NULL after this step.
+ data->width = pCodecCtx->width;
+ data->height = pCodecCtx->height; + data->dpy = XOpenDisplay(NULL); + VERIFY(data->dpy != NULL);
+ data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0);
style: inconsistent packed spacing
+ XStoreName(data->dpy, data->window, xvideo->window_title); + XMapWindow(data->dpy, data->window); + VERIFY(XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai) == Success); + data->xv_port = ai[0].base_id; + data->gc = XCreateGC(data->dpy, data->window, 0, 0); + VERIFY(data->gc != NULL); + data->yuv_image = XvShmCreateImage(data->dpy, data->xv_port, IMGFMT_I420 , 0, data->width, data->height, &data->yuv_shminfo); + VERIFY(data->yuv_image != NULL); + data->yuv_shminfo.shmid = shmget(IPC_PRIVATE, data->yuv_image->data_size, IPC_CREAT | 0777); + VERIFY(data->yuv_shminfo.shmid != -1); + data->yuv_image->data = (char*)shmat(data->yuv_shminfo.shmid, 0, 0); + VERIFY(data->yuv_image->data != NULL); + data->yuv_shminfo.shmaddr = data->yuv_image->data; + data->yuv_shminfo.readOnly = False; + VERIFY(XShmAttach(data->dpy, &data->yuv_shminfo) != 0); + return 0; +} + +static int xvideo_write_packet(AVFormatContext *s, AVPacket *pkt) { + XVideoContext *xvideo = NULL; + XVideoData *data = NULL; + XvImage *img = NULL; + int y, h; + XWindowAttributes attr; + AVPicture pict; + AVCodecContext *pCodecCtx = s->streams[0]->codec;
+ xvideo = s->priv_data; + VERIFY(xvideo != NULL);
This check is not needed
+ data = xvideo->data;
+ VERIFY(data != NULL);
Assuming the check I mentioned in the previous version, you can av_assert0() here or just drop that check completely.
+ img = data->yuv_image; + VERIFY(img != NULL);
You already checked for this in the previous function. av_assert0() if you're paranoid, or just drop that check
+ h = img->height / 2; + avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); + VERIFY(pict.linesize[0] == img->pitches[0] && pict.linesize[1] == img->pitches[1] && pict.linesize[2] == img->pitches[2]); + for(y = 0; y < img->height; y++) { + memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); + } + for(y = 0; y < h; ++y) { + memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); + memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); ^ inconsistent spacing breaking alignment
+ }
+ XGetWindowAttributes(data->dpy, data->window,&attr); + VERIFY(XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image,0, 0, data->width, data->height,0, 0, attr.width,attr.height, True) == Success); + XSync(data->dpy,True);
style: some weird inconsistent spacing around commas here.
+ return 0; +} + +static int xvideo_write_trailer(AVFormatContext *s) {
+ XVideoContext *xvideo = NULL; + XVideoData *data = NULL;
Unnecessary NULL assignment; assign them directly to s->priv_data and xvideo->data.
+ xvideo = s->priv_data; + VERIFY(xvideo != NULL); + data = xvideo->data; + VERIFY(data != NULL);
Unnecessary checks.
+ av_freep(&xvideo->window_title); + XShmDetach(data->dpy,&data->yuv_shminfo); + shmdt(data->yuv_image->data); + XFree(data->yuv_image); + XCloseDisplay(data->dpy);
+ if (xvideo->data) { free(xvideo->data); xvideo->data = NULL; } +
Unnecessary if also, trailing whitespace in the line below
+ return 0; +} + +static const AVOption options[] = { + { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { NULL }, +}; + +static const AVClass xvideo_class = { + .class_name = "xvideo outdev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVOutputFormat ff_xvideo_muxer = { + .name = "xv", + .long_name = NULL_IF_CONFIG_SMALL("XVideo output device"), + .priv_data_size = sizeof(XVideoContext), + .audio_codec = AV_CODEC_ID_NONE, + .video_codec = AV_CODEC_ID_RAWVIDEO, + .write_header = xvideo_write_header, + .write_packet = xvideo_write_packet, + .write_trailer = xvideo_write_trailer, + .flags = AVFMT_NOFILE, + .priv_class = &xvideo_class, +};
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
-- Clément B.
On 4/12/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Thanks, how about this one? Jeff
diff --git a/libavdevice/Makefile b/libavdevice/Makefile index efffa8b..83728cc 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -36,6 +36,7 @@ OBJS-$(CONFIG_V4L2_INDEV) += v4l2.o timefilter.o OBJS-$(CONFIG_V4L_INDEV) += v4l.o OBJS-$(CONFIG_VFWCAP_INDEV) += vfwcap.o OBJS-$(CONFIG_X11GRAB_INDEV) += x11grab.o +OBJS-$(CONFIG_XVIDEO_OUTDEV) += xvideo.o
# external libraries OBJS-$(CONFIG_LIBCDIO_INDEV) += libcdio.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index daa6638..5680796 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -64,6 +64,7 @@ void avdevice_register_all(void) // REGISTER_INDEV (V4L, v4l REGISTER_INDEV (VFWCAP, vfwcap); REGISTER_INDEV (X11GRAB, x11grab); + REGISTER_OUTDEV (XVIDEO, xvideo);
/* external libraries */ REGISTER_INDEV (LIBCDIO, libcdio); diff --git a/libavdevice/xvideo.c b/libavdevice/xvideo.c new file mode 100644 index 0000000..d7a1131 --- /dev/null +++ b/libavdevice/xvideo.c @@ -0,0 +1,155 @@ +/* + * 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 + */ + +#include <X11/Xlib.h> +#include <X11/extensions/Xv.h> +#include <X11/extensions/Xvlib.h> +#include <sys/shm.h> +#include "libavutil/avstring.h" +#include "libavutil/mem.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "avdevice.h" +#include "libavfilter/libmpcodecs/img_format.h"
remove that^
+ +typedef struct { + GC gc; + Window window; + Display* dpy; + XvImage* yuv_image; + XShmSegmentInfo yuv_shminfo; + int xv_port, width, height; +} XVideoData;
Another struct is not really needed.
+ +typedef struct { + AVClass *class; + char *window_title; + XVideoData *data; +} XVideoContext; + +#define OFFSET(x) offsetof(XVideoContext,x) + +#define VERIFY(s) if (!(s)) { av_log(NULL, AV_LOG_ERROR, "[%s][%d]: ERROR: %s failed\n", __FUNCTION__,__LINE__,#s); return AVERROR_UNKNOWN; } + +static int xvideo_write_header(AVFormatContext *s) { + XVideoContext *xvideo = s->priv_data; + XVideoData *data = av_mallocz(sizeof(XVideoData));
Again this is pointless, use same struct. and then this call is not needed. If size of X11/Xv*/GC/Window struct can change (we should safely assume they do not), malloc them instead.
+ unsigned int p_num_adaptors; + XvAdaptorInfo *ai = NULL; + AVCodecContext *pCodecCtx = s->streams[0]->codec;
Replace pCodecCtx with cctx, or codec_ctx, if you insist on long names.
+ VERIFY(pCodecCtx->codec_tag == IMGFMT_I420);
Replace IMGFMT_I420 with MKTAG('I','4','2','0'); Also it is not goal to write code with smallest possible number of lines, so just remove this VERIFY macro.
+ VERIFY(s->nb_streams == 1); + xvideo->data = data; + if (!xvideo->window_title) xvideo->window_title = av_strdup(""); + data->width = pCodecCtx->width; + data->height = pCodecCtx->height; + data->dpy = XOpenDisplay(NULL); + VERIFY(data->dpy != NULL); + data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy),0, 0,data->width,data->height,0,0,0); + XStoreName(data->dpy, data->window, xvideo->window_title); + XMapWindow(data->dpy, data->window); + VERIFY(XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy),&p_num_adaptors, &ai) == Success); + data->xv_port = ai[0].base_id; + data->gc = XCreateGC(data->dpy, data->window, 0, 0); + VERIFY(data->gc != NULL); + data->yuv_image = XvShmCreateImage(data->dpy, data->xv_port, IMGFMT_I420 , 0, data->width, data->height, &data->yuv_shminfo);
Replace IMGFMT_I420 with MKTAG('I','4','2','0'); What about quering what formats driver actually supports instead of this hardcoded case. See sdl.c for some hints how to do it.
+ VERIFY(data->yuv_image != NULL); + data->yuv_shminfo.shmid = shmget(IPC_PRIVATE, data->yuv_image->data_size, IPC_CREAT | 0777); + VERIFY(data->yuv_shminfo.shmid != -1); + data->yuv_image->data = (char*)shmat(data->yuv_shminfo.shmid, 0, 0); + VERIFY(data->yuv_image->data != NULL); + data->yuv_shminfo.shmaddr = data->yuv_image->data; + data->yuv_shminfo.readOnly = False; + VERIFY(XShmAttach(data->dpy, &data->yuv_shminfo) != 0); + return 0; +} + +static int xvideo_write_packet(AVFormatContext *s, AVPacket *pkt) { + XVideoContext *xvideo = NULL; + XVideoData *data = NULL; + XvImage *img = NULL; + int y, h; + XWindowAttributes attr; + AVPicture pict; + AVCodecContext *pCodecCtx = s->streams[0]->codec; + xvideo = s->priv_data; + VERIFY(xvideo != NULL); + data = xvideo->data; + VERIFY(data != NULL); + img = data->yuv_image; + VERIFY(img != NULL); + h = img->height / 2; + avpicture_fill(&pict, pkt->data, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); + VERIFY(pict.linesize[0] == img->pitches[0] && pict.linesize[1] == img->pitches[1] && pict.linesize[2] == img->pitches[2]); + for(y = 0; y < img->height; y++) { + memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); + } + for(y = 0; y < h; ++y) { + memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); + memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])],&pict.data[2][y * pict.linesize[2]], img->pitches[2]); + } + XGetWindowAttributes(data->dpy, data->window,&attr); + VERIFY(XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image,0, 0, data->width, data->height,0, 0, attr.width,attr.height, True) == Success); + XSync(data->dpy,True); + return 0; +} + +static int xvideo_write_trailer(AVFormatContext *s) { + XVideoContext *xvideo = NULL; + XVideoData *data = NULL; + xvideo = s->priv_data; + VERIFY(xvideo != NULL); + data = xvideo->data; + VERIFY(data != NULL); + av_freep(&xvideo->window_title); + XShmDetach(data->dpy,&data->yuv_shminfo); + shmdt(data->yuv_image->data); + XFree(data->yuv_image); + XCloseDisplay(data->dpy); + if (xvideo->data) { free(xvideo->data); xvideo->data = NULL; } + + return 0; +} + +static const AVOption options[] = { + { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { NULL }, +}; + +static const AVClass xvideo_class = { + .class_name = "xvideo outdev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVOutputFormat ff_xvideo_muxer = { + .name = "xv", + .long_name = NULL_IF_CONFIG_SMALL("XVideo output device"), + .priv_data_size = sizeof(XVideoContext), + .audio_codec = AV_CODEC_ID_NONE, + .video_codec = AV_CODEC_ID_RAWVIDEO, + .write_header = xvideo_write_header, + .write_packet = xvideo_write_packet, + .write_trailer = xvideo_write_trailer, + .flags = AVFMT_NOFILE, + .priv_class = &xvideo_class, +};
Here is WIP what I did using your patch: --- configure | 4 ++ libavdevice/Makefile | 1 + libavdevice/alldevices.c | 1 + libavdevice/xvideo.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 libavdevice/xvideo.c diff --git a/configure b/configure index d6b8dd3..caf1f82 100755 --- a/configure +++ b/configure @@ -239,6 +239,7 @@ External library support: --enable-opencl enable OpenCL code --enable-openssl enable openssl [no] --enable-x11grab enable X11 grabbing [no] + --enable-xv enable Xv video display --enable-zlib enable zlib [autodetect] Advanced options (experts only): @@ -1192,6 +1193,7 @@ EXTERNAL_LIBRARY_LIST=" opencl openssl x11grab + xv zlib " @@ -2063,6 +2065,7 @@ openal_indev_deps="openal" oss_indev_deps_any="soundcard_h sys_soundcard_h" oss_outdev_deps_any="soundcard_h sys_soundcard_h" pulse_indev_deps="libpulse" +xvideo_outdev_deps="xv" sdl_outdev_deps="sdl" sndio_indev_deps="sndio_h" sndio_outdev_deps="sndio_h" @@ -4004,6 +4007,7 @@ enabled libquvi && require_pkg_config libquvi quvi/quvi.h quvi_init enabled librtmp && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init enabled libshine && require_pkg_config shine shine/layer3.h shine_encode_frame +enabled xv && require2 xv "X11/Xlib.h X11/extensions/Xvlib.h" XvGetPortAttribute -lXv enabled libsoxr && require libsoxr soxr.h soxr_create -lsoxr enabled libspeex && require libspeex speex/speex.h speex_decoder_init -lspeex enabled libstagefright_h264 && require_cpp libstagefright_h264 "binder/ProcessState.h media/stagefright/MetaData.h diff --git a/libavdevice/Makefile b/libavdevice/Makefile index efffa8b..83728cc 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -36,6 +36,7 @@ OBJS-$(CONFIG_V4L2_INDEV) += v4l2.o timefilter.o OBJS-$(CONFIG_V4L_INDEV) += v4l.o OBJS-$(CONFIG_VFWCAP_INDEV) += vfwcap.o OBJS-$(CONFIG_X11GRAB_INDEV) += x11grab.o +OBJS-$(CONFIG_XVIDEO_OUTDEV) += xvideo.o # external libraries OBJS-$(CONFIG_LIBCDIO_INDEV) += libcdio.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index daa6638..bd4e341 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -64,6 +64,7 @@ void avdevice_register_all(void) // REGISTER_INDEV (V4L, v4l REGISTER_INDEV (VFWCAP, vfwcap); REGISTER_INDEV (X11GRAB, x11grab); + REGISTER_OUTDEV (XVIDEO, xvideo); /* external libraries */ REGISTER_INDEV (LIBCDIO, libcdio); diff --git a/libavdevice/xvideo.c b/libavdevice/xvideo.c new file mode 100644 index 0000000..626d9ec --- /dev/null +++ b/libavdevice/xvideo.c @@ -0,0 +1,165 @@ +/* + * 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 + */ + +#include <X11/Xlib.h> +#include <X11/extensions/Xv.h> +#include <X11/extensions/Xvlib.h> +#include <X11/extensions/XShm.h> +#include <sys/shm.h> + +#include "libavutil/avstring.h" +#include "libavutil/common.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "avdevice.h" + +typedef struct { + GC gc; + Window window; + Display* dpy; + XvImage* yuv_image; + XShmSegmentInfo yuv_shminfo; + int xv_port, width, height; +} XVideoData; + +typedef struct { + AVClass *class; + char *window_title; + XVideoData *data; +} XVideoContext; + +#define OFFSET(x) offsetof(XVideoContext,x) + +static int xvideo_write_header(AVFormatContext *s) +{ + XVideoContext *xvideo = s->priv_data; + XVideoData *data = (XVideoData*)calloc(1,sizeof(XVideoData)); + unsigned int p_num_adaptors; + XvAdaptorInfo *ai = NULL; + AVCodecContext *ctx = s->streams[0]->codec; + xvideo->data = data; + + data->width = ctx->width; + data->height = ctx->height; + data->dpy = XOpenDisplay(NULL); + + data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy), 0, 0, data->width, data->height, 0, 0, 0); + + if (!xvideo->window_title) + xvideo->window_title = av_strdup(s->filename); + + XStoreName(data->dpy, data->window, xvideo->window_title); + XMapWindow(data->dpy, data->window); + + if (XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy), &p_num_adaptors, &ai) != Success) + return AVERROR_UNKNOWN; + + data->xv_port = ai[0].base_id; + data->gc = XCreateGC(data->dpy, data->window, 0, 0); + data->yuv_image = XvShmCreateImage(data->dpy, data->xv_port, MKTAG('I','4','2','0'), 0, data->width, data->height, &data->yuv_shminfo); + data->yuv_shminfo.shmid = shmget(IPC_PRIVATE, data->yuv_image->data_size, IPC_CREAT | 0777); + data->yuv_shminfo.shmaddr = (char *) shmat(data->yuv_shminfo.shmid, 0,0); + data->yuv_image->data = data->yuv_shminfo.shmaddr; + data->yuv_shminfo.readOnly = False; + + XShmAttach(data->dpy, &data->yuv_shminfo); + XSync(data->dpy, False); + shmctl(data->yuv_shminfo.shmid, IPC_RMID, 0); + + return 0; +} + +static int xvideo_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + XVideoContext *xvideo = s->priv_data; + XVideoData *data = xvideo->data; + XvImage *img = NULL; + XWindowAttributes attr; + AVPicture pict; + AVCodecContext *ctx = s->streams[0]->codec; + int y, h; + + img = data->yuv_image; + h = img->height / 2; + avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height); + + for (y = 0; y < img->height; y++) { + memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], &pict.data[0][y * pict.linesize[0]], img->pitches[0]); + } + + for (y = 0; y < h; ++y) { + memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], &pict.data[1][y * pict.linesize[1]], img->pitches[1]); + memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])], &pict.data[2][y * pict.linesize[2]], img->pitches[2]); + } + + XGetWindowAttributes(data->dpy, data->window, &attr); + + if (XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, data->yuv_image, 0, 0, data->width, data->height, 0, 0, attr.width,attr.height, True) != Success) + return AVERROR_UNKNOWN; + + XSync(data->dpy, True); + + return 0; +} + +static int xvideo_write_trailer(AVFormatContext *s) +{ + XVideoContext *xvideo = s->priv_data; + XVideoData *data = xvideo->data; + + av_freep(&xvideo->window_title); + XShmDetach(data->dpy, &data->yuv_shminfo); + shmdt(data->yuv_image->data); + XFree(data->yuv_image); + XCloseDisplay(data->dpy); + + if (xvideo->data) { + free(xvideo->data); + xvideo->data = NULL; + } + + return 0; +} + +static const AVOption options[] = { + { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, + { NULL }, +}; + +static const AVClass xvideo_class = { + .class_name = "xvideo outdev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVOutputFormat ff_xvideo_muxer = { + .name = "xv", + .long_name = NULL_IF_CONFIG_SMALL("XVideo output device"), + .priv_data_size = sizeof(XVideoContext), + .audio_codec = AV_CODEC_ID_NONE, + .video_codec = AV_CODEC_ID_RAWVIDEO, + .write_header = xvideo_write_header, + .write_packet = xvideo_write_packet, + .write_trailer = xvideo_write_trailer, + .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, + .priv_class = &xvideo_class, +}; -- 1.7.11.2
Thanks I'll have a new version ready soon. -Jeff
Hi Paul, are you referring to this code in sdl.c for querying the formats: } sdl_overlay_pix_fmt_map[] = { { AV_PIX_FMT_YUV420P, SDL_IYUV_OVERLAY }, { AV_PIX_FMT_YUYV422, SDL_YUY2_OVERLAY }, { AV_PIX_FMT_UYVY422, SDL_UYVY_OVERLAY }, { AV_PIX_FMT_NONE, 0 }, }; ... for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) { if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) { sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt; break; } } Are there sample videos that I can use for testing with these formats? Thanks, Jeff
On 4/15/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Hi Paul, are you referring to this code in sdl.c for querying the formats:
} sdl_overlay_pix_fmt_map[] = { { AV_PIX_FMT_YUV420P, SDL_IYUV_OVERLAY }, { AV_PIX_FMT_YUYV422, SDL_YUY2_OVERLAY }, { AV_PIX_FMT_UYVY422, SDL_UYVY_OVERLAY }, { AV_PIX_FMT_NONE, 0 }, };
...
for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) { if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) { sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt; break; } }
Are there sample videos that I can use for testing with these formats?
There is Xv query function which returns supported formats by Xv. (it could support more than yuv420p/I420 like BGRA) See xv code that is in mpv/mplayer... (This is second time I mention this) For testing you just set -pix_fmt that is equivalent of Xv one and swscale/ffmpeg do conversion for you - no need for special files.
Thanks, Jeff _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Thanks Paul, I looked at the mpv/mplayer XVideo source code (vo_xv.c) as a reference. Here's a new xvideo.c file that checks that the driver supports I420 (using XvListImageFormats). Todo: add support for other formats, and conversion, etc. Btw, SDL internally can use XVideo extension. Maybe there's no need for xvideo device. It would be nice to enable hardware acceleration in sdl.c. ffplay source code uses sdl with hardware acceleration. Is that ok if I send the xvideo.c file instead of a patch file? Thanks, Jeff
On 4/15/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Thanks Paul, I looked at the mpv/mplayer XVideo source code (vo_xv.c) as a reference. Here's a new xvideo.c file that checks that the driver supports I420 (using XvListImageFormats). Todo: add support for other formats, and conversion, etc. Btw, SDL internally can use XVideo extension. Maybe there's no need for xvideo device. It would be nice to enable hardware acceleration in sdl.c. ffplay source code uses sdl with hardware acceleration.
SDL does not support rgb and only subsamples yuv, which sucks.
Is that ok if I send the xvideo.c file instead of a patch file?
Send full patch, that is only way to test your code.
Thanks, Jeff
Paul B Mahol <onemda <at> gmail.com> writes:
Btw, SDL internally can use XVideo extension. Maybe there's no need for xvideo device. It would be nice to enable hardware acceleration in sdl.c. ffplay source code uses sdl with hardware acceleration.
SDL does not support rgb and only subsamples yuv, which sucks.
Sorry if I misunderstand: Are you sure it makes any sense if a FFmpeg xv output device supports anything else but yuv420p? Carl Eugen
Hi Paul, here's a new patch, please let me know what's next. Thanks, Jeff
On date Tuesday 2013-04-16 22:24:34 +0000, Moguillansky, Jeff encoded:
Hi Paul, here's a new patch,
please use git format and send in attachment the generated patch, it's usually easier to handle than copy&paste from the mail text.
please let me know what's next. Thanks, Jeff
diff --git a/configure b/configure index a0e6b47..90dc430 100755 --- a/configure +++ b/configure @@ -238,6 +238,7 @@ External library support: --enable-opencl enable OpenCL code --enable-openssl enable openssl [no] --enable-x11grab enable X11 grabbing [no] + --enable-xv enable Xv video display --enable-zlib enable zlib [autodetect]
Advanced options (experts only): @@ -1190,6 +1191,7 @@ EXTERNAL_LIBRARY_LIST=" opencl openssl x11grab + xv zlib "
@@ -2046,6 +2048,7 @@ openal_indev_deps="openal" oss_indev_deps_any="soundcard_h sys_soundcard_h" oss_outdev_deps_any="soundcard_h sys_soundcard_h" pulse_indev_deps="libpulse" +xvideo_outdev_deps="xv"
alphabetical order
sdl_outdev_deps="sdl" sndio_indev_deps="sndio_h" sndio_outdev_deps="sndio_h" @@ -4051,6 +4054,7 @@ else fi fi enabled sdl && add_cflags $sdl_cflags && add_extralibs $sdl_libs +enabled xv && require2 xv "X11/Xlib.h X11/extensions/Xvlib.h" XvGetPortAttribute -lXv
texi2html --help 2> /dev/null | grep -q 'init-file' && enable texi2html || disable texi2html makeinfo --version > /dev/null 2>&1 && enable makeinfo || disable makeinfo diff --git a/libavdevice/Makefile b/libavdevice/Makefile index efffa8b..83728cc 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -36,6 +36,7 @@ OBJS-$(CONFIG_V4L2_INDEV) += v4l2.o timefilter.o OBJS-$(CONFIG_V4L_INDEV) += v4l.o OBJS-$(CONFIG_VFWCAP_INDEV) += vfwcap.o OBJS-$(CONFIG_X11GRAB_INDEV) += x11grab.o +OBJS-$(CONFIG_XVIDEO_OUTDEV) += xvideo.o
# external libraries OBJS-$(CONFIG_LIBCDIO_INDEV) += libcdio.o diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c index daa6638..bd4e341 100644 --- a/libavdevice/alldevices.c +++ b/libavdevice/alldevices.c @@ -64,6 +64,7 @@ void avdevice_register_all(void) // REGISTER_INDEV (V4L, v4l REGISTER_INDEV (VFWCAP, vfwcap); REGISTER_INDEV (X11GRAB, x11grab); + REGISTER_OUTDEV (XVIDEO, xvideo);
/* external libraries */ REGISTER_INDEV (LIBCDIO, libcdio); diff --git a/libavdevice/xvideo.c b/libavdevice/xvideo.c new file mode 100644 index 0000000..f6ec205 --- /dev/null +++ b/libavdevice/xvideo.c @@ -0,0 +1,188 @@ +/*
+ * XVideo output device
nit: put file description to a separate doxy: /** @file * XVideo output device */ see sdl.c for example
+ * 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 + */ + +#include <X11/Xlib.h> +#include <X11/extensions/Xv.h> +#include <X11/extensions/Xvlib.h> +#include <X11/extensions/XShm.h> +#include <sys/shm.h> + +#include "libavutil/avstring.h" +#include "libavutil/common.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/pixdesc.h" +#include "avdevice.h" +
+typedef struct { + GC gc; + Window window; + Display* dpy; + XvImage* yuv_image; + XShmSegmentInfo yuv_shminfo; + int xv_port, width, height; +} XVideoData; + +typedef struct { + AVClass *class; + char *window_title; + XVideoData *data; +} XVideoContext;
any reason not to merge the two structures
+ +#define OFFSET(x) offsetof(XVideoContext,x)
Nit: this could be defined close to the option array.
+ +static int xvideo_write_header(AVFormatContext *s) +{ + XVideoContext *xvideo = s->priv_data; + XVideoData *data = (XVideoData*)calloc(1,sizeof(XVideoData));
missing NULL check, and it could be avoided by merging XVideoData in XVideoContext. Also unneded cast.
+ unsigned int p_num_adaptors; + XvAdaptorInfo *ai = NULL; + XvImageFormatValues *fv = NULL; + int num_formats = 0, j, format_I420_supported = 0; + AVCodecContext *ctx = s->streams[0]->codec; + xvideo->data = data; + + data->width = ctx->width; + data->height = ctx->height; + data->dpy = XOpenDisplay(NULL); +
+ data->window = XCreateSimpleWindow(data->dpy, DefaultRootWindow(data->dpy), + 0, 0, data->width, data->height, 0, 0, 0 + );
nit: weird indent
+ + if (!xvideo->window_title) + xvideo->window_title = av_strdup(s->filename);
missing NULL check
+ + XStoreName(data->dpy, data->window, xvideo->window_title); + XMapWindow(data->dpy, data->window); + + if (XvQueryAdaptors(data->dpy, DefaultRootWindow(data->dpy), + &p_num_adaptors, &ai) != Success) + return AVERROR_UNKNOWN;
AVERROR_EXTERNAL also there is some way to return an error string from xv? (not blocking)
+ data->xv_port = ai[0].base_id; + + fv = XvListImageFormats(data->dpy, data->xv_port, &num_formats);
+ if (fv == NULL) + return AVERROR_UNKNOWN;
nit: if (!fv) AVERROR_EXTERNAL, here and below
+ for (j = 0; j<num_formats; j++) { + if (fv[j].id == MKTAG('I','4','2','0')) { format_I420_supported = 1; break; } + }
weird indent
+ XFree(fv); + if (!format_I420_supported) + return AVERROR_UNKNOWN; + + data->gc = XCreateGC(data->dpy, data->window, 0, 0); + data->yuv_image = XvShmCreateImage(data->dpy, data->xv_port, + MKTAG('I','4','2','0'), 0, data->width, data->height, &data->yuv_shminfo + ); + data->yuv_shminfo.shmid = shmget(IPC_PRIVATE, data->yuv_image->data_size, IPC_CREAT | 0777); + data->yuv_shminfo.shmaddr = (char *) shmat(data->yuv_shminfo.shmid, 0,0); + data->yuv_image->data = data->yuv_shminfo.shmaddr; + data->yuv_shminfo.readOnly = False; + + XShmAttach(data->dpy, &data->yuv_shminfo); + XSync(data->dpy, False); + shmctl(data->yuv_shminfo.shmid, IPC_RMID, 0); + + return 0; +} + +static int xvideo_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + XVideoContext *xvideo = s->priv_data; + XVideoData *data = xvideo->data; + XvImage *img = NULL; + XWindowAttributes attr; + AVPicture pict; + AVCodecContext *ctx = s->streams[0]->codec; + int y, h; + + img = data->yuv_image; + h = img->height / 2; + avpicture_fill(&pict, pkt->data, ctx->pix_fmt, ctx->width, ctx->height);
av_image_fill_arrays()?
+
+ for (y = 0; y < img->height; y++) { + memcpy(&img->data[img->offsets[0] + (y * img->pitches[0])], + &pict.data[0][y * pict.linesize[0]], img->pitches[0]); + } + + for (y = 0; y < h; ++y) { + memcpy(&img->data[img->offsets[1] + (y * img->pitches[1])], + &pict.data[1][y * pict.linesize[1]], img->pitches[1]); + memcpy(&img->data[img->offsets[2] + (y * img->pitches[2])], + &pict.data[2][y * pict.linesize[2]], img->pitches[2]); + }
av_image_copy()?
+ + XGetWindowAttributes(data->dpy, data->window, &attr); + + if (XvShmPutImage(data->dpy, data->xv_port, data->window, data->gc, + data->yuv_image, 0, 0, data->width, data->height, 0, 0, + attr.width,attr.height, True) != Success) + return AVERROR_UNKNOWN; + return 0; +} + +static int xvideo_write_trailer(AVFormatContext *s) +{ + XVideoContext *xvideo = s->priv_data; + XVideoData *data = xvideo->data; +
+ av_freep(&xvideo->window_title);
unneded (will be freed automatically when the context is closed)
+ XShmDetach(data->dpy, &data->yuv_shminfo); + shmdt(data->yuv_image->data); + XFree(data->yuv_image); + XCloseDisplay(data->dpy); + + if (xvideo->data) { + free(xvideo->data); + xvideo->data = NULL;
av_freep, also you can avoid this altogether if you merge the structure in the context
+ } + + return 0; +} + +static const AVOption options[] = { + { "window_title", "set window title", OFFSET(window_title), + AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM + }, + { NULL }, +}; + +static const AVClass xvideo_class = { + .class_name = "xvideo outdev", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVOutputFormat ff_xvideo_muxer = { + .name = "xv", + .long_name = NULL_IF_CONFIG_SMALL("XVideo output device"), + .priv_data_size = sizeof(XVideoContext), + .audio_codec = AV_CODEC_ID_NONE, + .video_codec = AV_CODEC_ID_RAWVIDEO, + .write_header = xvideo_write_header, + .write_packet = xvideo_write_packet, + .write_trailer = xvideo_write_trailer, + .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS, + .priv_class = &xvideo_class,
Missing docs in doc/outdevs.texi. -- FFmpeg = Furious and Faithful Muttering Puritan Emblematic Gospel
Thanks, Unfortunately I won't be able to continue working on xvideo output device but if anyone wants to take over, feel free. -Jeff
On date Wednesday 2013-04-17 19:35:03 +0000, Moguillansky, Jeff encoded:
Thanks,
Unfortunately I won't be able to continue working on xvideo output device but if anyone wants to take over, feel free. -Jeff
Here. Todo: support more pixel formats and options (for example w/h/x/y), but I don't plan to work on them soon, so I suggest to push as is and make easier for other contributors to extend it. BTW does someone know how to draw on the root window (allowing to implement live desktop background with FFmpeg)? -- FFmpeg = Fundamentalist and Friendly Meaningless Puristic Emblematic Gladiator
Stefano Sabatini <stefasab@gmail.com> wrote:
On date Wednesday 2013-04-17 19:35:03 +0000, Moguillansky, Jeff encoded:
Thanks,
Unfortunately I won't be able to continue working on xvideo output device but if anyone wants to take over, feel free. -Jeff
Here.
Todo: support more pixel formats and options (for example w/h/x/y), but I don't plan to work on them soon, so I suggest to push as is and make easier for other contributors to extend it.
BTW does someone know how to draw on the root window (allowing to implement live desktop background with FFmpeg)?
Just call DefaultRootWindow instead of creating a window.
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ --enable-xv enable Xv video display [no]
Imo, this should be autodetected. (Why should it be handled differently from alsa?) Carl Eugen
On date Tuesday 2013-05-14 17:16:34 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ --enable-xv enable Xv video display [no]
Imo, this should be autodetected. (Why should it be handled differently from alsa?)
Because right now the build system is schizofrenic, and it is not clear when autodetection logic should be used or not. In most cases the user is *required* to explicitly specify all the libraries she wants to use (and the configure script will complain&fail in case the library is not detected). To my eyes libxv is no exception. Ideally we should be able to set the autodetection policy, for example to automatically enable all the available libraries/features. -- FFmpeg = Fast and Free Maxi Pure Elected God
Stefano Sabatini <stefasab <at> gmail.com> writes:
On date Tuesday 2013-05-14 17:16:34 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ --enable-xv enable Xv video display [no]
Imo, this should be autodetected. (Why should it be handled differently from alsa?)
Because right now the build system is schizofrenic, and it is not clear when autodetection logic should be used or not.
(Imo, it is crystal-clear, but that is not the quesion.)
In most cases the user is *required* to explicitly specify all the libraries she wants to use (and the configure script will complain&fail in case the library is not detected). To my eyes libxv is no exception.
So allow me to repeat my question: How is xv different from alsa? And imo, they are both *very* different from libx264, libm3lame, libdirac and libtheora (etc.).
Ideally we should be able to set the autodetection policy, for example to automatically enable all the available libraries/features.
That may have been an interesting idea some years ago, now it sounds like a behaviour change that will produce many surprised users (which is why I am against such a change). Carl Eugen
On date Sunday 2013-05-26 10:41:36 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
On date Tuesday 2013-05-14 17:16:34 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ --enable-xv enable Xv video display [no]
Imo, this should be autodetected. (Why should it be handled differently from alsa?)
Because right now the build system is schizofrenic, and it is not clear when autodetection logic should be used or not.
(Imo, it is crystal-clear, but that is not the quesion.)
It is not clear at all to me. What is the principle? Where is it documented?
In most cases the user is *required* to explicitly specify all the libraries she wants to use (and the configure script will complain&fail in case the library is not detected). To my eyes libxv is no exception.
So allow me to repeat my question:
How is xv different from alsa?
The ALSA wrapper depends on the libasound library, which in turn can be installed only when some features are available on the system. The XV wrapper depends on the libXv library, which in turn can be installed only when some features are available on the system. So from this point of view I see no differences. We sometimes apply the principle: enable a feature if all its required components have been explicitly enabled and are available and in other cases this other one: enable a feature if all its required components are available on the system
And imo, they are both *very* different from libx264, libm3lame, libdirac and libtheora (etc.).
Why and how?
Ideally we should be able to set the autodetection policy, for example to automatically enable all the available libraries/features.
That may have been an interesting idea some years ago, now it sounds like a behaviour change that will produce many surprised users (which is why I am against such a change).
On the other hand the current system as is is inconsistent, confusing and undocumented, and thus surprising even for a veteran developer. -- FFmpeg = Fierce and Free Mythic Political Epic Geek
Stefano Sabatini <stefasab <at> gmail.com> writes:
Because right now the build system is schizofrenic, and it is not clear when autodetection logic should be used or not.
(Imo, it is crystal-clear, but that is not the quesion.)
It is not clear at all to me. What is the principle?
"System libraries" are auto-detected. (vda is currently wrong, I forgot to work on this in the last days...) Obviously you can discuss what is a system library and what is not a system library, we so far argued that not only libz and libbz2 (and libva) but alsa and sdl are also system libs. I find this argumentation not unreasonable and I do not remember a user or a packager complaining. (But when we broke libva, complaints were made immediately.) (Please understand that this is *our* definition of "system library", it may of course diverge - significantly - from other people's definition.) [...]
How is xv different from alsa?
The ALSA wrapper depends on the libasound library, which in turn can be installed only when some features are available on the system.
(Which features?)
The XV wrapper depends on the libXv library, which in turn can be installed only when some features are available on the system.
So from this point of view I see no differences.
Then please make xv autodetected. Carl Eugen
On date Sunday 2013-05-26 13:18:57 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
Because right now the build system is schizofrenic, and it is not clear when autodetection logic should be used or not.
(Imo, it is crystal-clear, but that is not the quesion.)
It is not clear at all to me. What is the principle?
"System libraries" are auto-detected. (vda is currently wrong, I forgot to work on this in the last days...)
Obviously you can discuss what is a system library and what is not a system library, we so far argued that not only libz and libbz2 (and libva) but alsa and sdl are also system libs.
I find this argumentation not unreasonable and I do not remember a user or a packager complaining. (But when we broke libva, complaints were made immediately.)
(Please understand that this is *our* definition of "system library", it may of course diverge - significantly - from other people's definition.)
Now I have no idea what a "system library" is, and why for example libz or libsdl are considered "system libraries", and libx264 is not. -- FFmpeg = Fancy Fierce Multipurpose Pitiless Elastic Gladiator
On date Tuesday 2013-05-14 17:58:35 +0200, Stefano Sabatini encoded:
On date Wednesday 2013-04-17 19:35:03 +0000, Moguillansky, Jeff encoded:
Thanks,
Unfortunately I won't be able to continue working on xvideo output device but if anyone wants to take over, feel free. -Jeff
Here.
Todo: support more pixel formats and options (for example w/h/x/y), but I don't plan to work on them soon, so I suggest to push as is and make easier for other contributors to extend it.
BTW does someone know how to draw on the root window (allowing to implement live desktop background with FFmpeg)? -- FFmpeg = Fundamentalist and Friendly Meaningless Puristic Emblematic Gladiator
From d01b0791f02936033c335166fbd2c650e90994fa Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Tue, 14 May 2013 13:44:29 +0200 Subject: [PATCH] lavd: add xv 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 --- configure | 4 + doc/outdevs.texi | 27 +++++++ libavdevice/Makefile | 1 + libavdevice/alldevices.c | 1 + libavdevice/xv.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 216 insertions(+) create mode 100644 libavdevice/xv.c
Updated with a few more options, and a TODO. As suggested by Reimar, the live desktop thing could be done by implementing a -window_id option to set the already existing window to use, as an alternative to creating a new one. -- FFmpeg = Faithless and Foolish Merciful Political Exciting Game
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ --enable-xv enable Xv video display [no]
I still believe that this should be auto-detected / still don't understand why it should be handled differently from alsa (and sdl). Carl Eugen
On 5/26/13, Carl Eugen Hoyos <cehoyos@ag.or.at> wrote:
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ --enable-xv enable Xv video display [no]
I still believe that this should be auto-detected / still don't understand why it should be handled differently from alsa (and sdl).
what about caca, v4l2?
Carl Eugen
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Paul B Mahol <onemda <at> gmail.com> writes:
+ --enable-xv enable Xv video display [no]
I still believe that this should be auto-detected / still don't understand why it should be handled differently from alsa (and sdl).
what about caca,
I am not sure, it could be seen as different from the others, but I won't disagree with changing caca detection behaviour.
v4l2?
I am not sure how v4l2 is related to above, but it is auto-detected afaik. Carl Eugen
On date Sunday 2013-05-26 12:48:17 +0200, Stefano Sabatini encoded: [...]
Updated with a few more options, and a TODO. As suggested by Reimar, the live desktop thing could be done by implementing a -window_id option to set the already existing window to use, as an alternative to creating a new one. -- FFmpeg = Faithless and Foolish Merciful Political Exciting Game
From c82fa8dec930cb4e098390bff4a6c218e8267aeb Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Tue, 14 May 2013 13:44:29 +0200 Subject: [PATCH] lavd: add xv 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 --- configure | 4 + doc/outdevs.texi | 60 +++++++++++++ libavdevice/Makefile | 1 + libavdevice/alldevices.c | 1 + libavdevice/xv.c | 217 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 283 insertions(+) create mode 100644 libavdevice/xv.c
Updated, now with auto-detection. I'll push the patch in a few days if no one will review it. -- FFmpeg = Fantastic and Fabulous Meaningless Plastic Elfic Gymnast
On date Tuesday 2013-05-28 18:33:09 +0200, Stefano Sabatini encoded:
On date Sunday 2013-05-26 12:48:17 +0200, Stefano Sabatini encoded: [...]
Updated with a few more options, and a TODO. As suggested by Reimar, the live desktop thing could be done by implementing a -window_id option to set the already existing window to use, as an alternative to creating a new one. -- FFmpeg = Faithless and Foolish Merciful Political Exciting Game
From c82fa8dec930cb4e098390bff4a6c218e8267aeb Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Tue, 14 May 2013 13:44:29 +0200 Subject: [PATCH] lavd: add xv 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 --- configure | 4 + doc/outdevs.texi | 60 +++++++++++++ libavdevice/Makefile | 1 + libavdevice/alldevices.c | 1 + libavdevice/xv.c | 217 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 283 insertions(+) create mode 100644 libavdevice/xv.c
Updated, now with auto-detection.
I'll push the patch in a few days if no one will review it.
Pushed. -- FFmpeg = Faithless and Fundamental Mind-dumbing Political Ermetic God
On date Thursday 2013-05-30 09:57:17 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
I'll push the patch in a few days if no one will review it.
Pushed.
On my system, gcc needs -lX11 -lXext to succeed linking.
Fix it or send a patch, since I can't reproduce the issue here. -- FFmpeg = Fantastic and Friendly Most Pitiless Experimenting Gorilla
On Thursday 30 May 2013 12:12:12 pm Stefano Sabatini wrote:
On date Thursday 2013-05-30 09:57:17 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
I'll push the patch in a few days if no one will review it.
Pushed.
On my system, gcc needs -lX11 -lXext to succeed linking.
Fix it or send a patch, since I can't reproduce the issue here.
Patch attached, this is approximately what MPlayer does. Carl Eugen
On date Thursday 2013-05-30 12:40:59 +0200, Carl Eugen Hoyos encoded:
On Thursday 30 May 2013 12:12:12 pm Stefano Sabatini wrote:
On date Thursday 2013-05-30 09:57:17 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
I'll push the patch in a few days if no one will review it.
Pushed.
On my system, gcc needs -lX11 -lXext to succeed linking.
Fix it or send a patch, since I can't reproduce the issue here.
Patch attached, this is approximately what MPlayer does.
Carl Eugen
diff --git a/configure b/configure index 63d7ebb..be20646 100755 --- a/configure +++ b/configure @@ -2085,7 +2085,7 @@ v4l2_outdev_deps_any="linux_videodev2_h sys_videoio_h" vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines" vfwcap_indev_extralibs="-lavicap32" xv_outdev_deps="X11_extensions_Xvlib_h XvGetPortAttribute" -xv_outdev_extralibs="-lXv" +xv_outdev_extralibs="-lXv -lX11 -lXext" x11grab_indev_deps="x11grab"
# protocols @@ -3984,7 +3984,7 @@ check_func_headers windows.h SetConsoleTextAttribute check_func_headers windows.h Sleep check_func_headers windows.h VirtualAlloc check_func_headers glob.h glob -check_func_headers "X11/Xlib.h X11/extensions/Xvlib.h" XvGetPortAttribute -lXv +check_func_headers "X11/Xlib.h X11/extensions/Xvlib.h" XvGetPortAttribute -lXv -lX11 -lXext
LGTM, thanks. -- FFmpeg = Foolish Funny Mastering Pacific Experimenting Guide
On 4/12/13, Moguillansky, Jeff <Jeff.Moguillansky@am.sony.com> wrote:
Sorry that was the wrong file. Please look at this file instead.
Please do not top post. Your code have issues with XShm, eg. it worked some time but than it stoped and i have only black window. Btw: you can see what xv code in mpv/mplayer2/mplayer do. That code is also far more readable. Also some ASSERT macros are purely redudant. Also ASSERT is wrong name because it does not assert, but just returns. There is no need to use macros for error checking, see mpv/mpla... code again...
Moguillansky, Jeff <Jeff.Moguillansky <at> am.sony.com> writes:
Here's a patch file.
tools/patcheck should tell you about a few issues. (Our git repository rejects tabs outside Makefiles.) Carl Eugen
participants (7)
-
Carl Eugen Hoyos -
Clément Bœsch -
Michael Niedermayer -
Moguillansky, Jeff -
Paul B Mahol -
Reimar Döffinger -
Stefano Sabatini