[FFmpeg-devel] [PATCH] avdevice: add fbdev output device

Stefano Sabatini stefasab at gmail.com
Tue Oct 22 20:04:19 CEST 2013


On date Tuesday 2013-10-22 11:52:04 +0200, Lukasz M encoded:
[...]
> > > BTW, Wouldn't it be good idea to extend AVOutputFormat with information
> > > about supported pixel/sample formats?
> > > That would allow to guess correct formats without user interaction.
> >
> > Pixel and sample formats are specified (statically) in the codec
> > description.
> 
> 

> It is offtopic, I will probably come back with it later as RFC or something.
> In general I mean that codec may support all formats but output device only
> few of them.
> If device provide list of supported formats aside the codec formats list,
> then proper format
> may be chosen without user intaraction and without "guessing" basing on
> both list.
> I haven't analyse code so these are just simple thoughts, not sure it is
> applicable.

Some devices support some combinations of formats/codecs (typically
rawvideo + a few supported formats), but I can think of output devices
accepting accepting encoded packets, thus you should probably consider
codec/format couples.

I'm not sure if automatic choice can be fully automated (probably yes).

> > > +static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt)
> > > +{
> > > +    FBDevContext *fbdev = h->priv_data;
> > > +    uint8_t *pin, *pout;
> > > +    enum AVPixelFormat pix_fmt;
> > > +    int disp_height;
> > > +    int bytes_to_copy;
> > > +    int src_line_size = fbdev->width * fbdev->bytes_per_pixel;
> > > +
> > > +    if (fbdev->index != pkt->stream_index)
> > > +        return 0;
> > > +
> >
> > > +    if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
> > > +        av_log(h, AV_LOG_WARNING,
> > > +               "Error refreshing variable info: %s\n", strerror(errno));
> > > +
> > > +    pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
> >
> > Is this supposed to change midstream? Otherwise you could just use
> > fbdev->pix_fmt.
> >
> 
> Yes, format may change at any time. I triggered that change with fbset
> -depth 16 (from 32)
> 
> Rest of remarks fixed.

> From ddce8e28c1f62531f54d494f51c718cf4234a389 Mon Sep 17 00:00:00 2001
> From: Lukasz Marek <lukasz.m.luki at gmail.com>
> Date: Wed, 16 Oct 2013 17:18:55 +0200
> Subject: [PATCH] avdevice: add fbdev output device
> 
> Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
> ---
>  Changelog                |    1 +
>  configure                |    1 +
>  doc/outdevs.texi         |   29 ++++++
>  libavdevice/Makefile     |    1 +
>  libavdevice/alldevices.c |    2 +-
>  libavdevice/fbdev_enc.c  |  257 ++++++++++++++++++++++++++++++++++++++++++++++
>  libavdevice/version.h    |    2 +-
>  7 files changed, 291 insertions(+), 2 deletions(-)
>  create mode 100644 libavdevice/fbdev_enc.c
> 
> diff --git a/Changelog b/Changelog
> index 178476d..f44fcc8 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -41,6 +41,7 @@ version <next>
>  - max_error_rate parameter in ffmpeg
>  - PulseAudio output device
>  - ReplayGain scanner
> +- Linux framebuffer output device
>  
>  
>  version 2.0:
> diff --git a/configure b/configure
> index 75d523d..bc111f4 100755
> --- a/configure
> +++ b/configure
> @@ -2125,6 +2125,7 @@ dshow_indev_extralibs="-lpsapi -lole32 -lstrmiids -luuid"
>  dv1394_indev_deps="dv1394"
>  dv1394_indev_select="dv_demuxer"
>  fbdev_indev_deps="linux_fb_h"
> +fbdev_outdev_deps="linux_fb_h"
>  iec61883_indev_deps="libiec61883"
>  jack_indev_deps="jack_jack_h sem_timedwait"
>  lavfi_indev_deps="avfilter"
> diff --git a/doc/outdevs.texi b/doc/outdevs.texi
> index 55c9110..c737225 100644
> --- a/doc/outdevs.texi
> +++ b/doc/outdevs.texi
> @@ -104,6 +104,35 @@ ffmpeg -i INPUT -pix_fmt rgb24 -f caca -list_dither colors -
>  @end example
>  @end itemize
>  
> + at section fbdev
> +
> +Linux framebuffer output device.
> +
> +The Linux framebuffer is a graphic hardware-independent abstraction
> +layer to show graphics on a computer monitor, typically on the
> +console. It is accessed through a file device node, usually
> + at file{/dev/fb0}.
> +
> +For more detailed information read the file
> + at file{Documentation/fb/framebuffer.txt} included in the Linux source tree.
> +
> + at subsection Options
> + at table @option
> +
> + at item xoffset
> + at item yoffset
> +Set x/y coordinate of top left corner. Default is 0.
> + at end table
> +
> + at subsection Examples
> +Play a file on framebuffer device @file{/dev/fb0}.
> +Required pixel format depends on current framebuffer settings.
> + at example
> +ffmpeg -re -i INPUT -vcodec rawvideo -pix_fmt bgra -f fbdev /dev/fb0
> + at end example
> +
> +See also @url{http://linux-fbdev.sourceforge.net/}, and fbset(1).
> +
>  @section oss
>  
>  OSS (Open Sound System) output device.
> diff --git a/libavdevice/Makefile b/libavdevice/Makefile
> index ad959ee..f6f05e5 100644
> --- a/libavdevice/Makefile
> +++ b/libavdevice/Makefile
> @@ -24,6 +24,7 @@ OBJS-$(CONFIG_DSHOW_INDEV)               += dshow.o dshow_enummediatypes.o \
>                                              dshow_pin.o dshow_common.o
>  OBJS-$(CONFIG_DV1394_INDEV)              += dv1394.o
>  OBJS-$(CONFIG_FBDEV_INDEV)               += fbdev.o
> +OBJS-$(CONFIG_FBDEV_OUTDEV)              += fbdev_enc.o
>  OBJS-$(CONFIG_IEC61883_INDEV)            += iec61883.o
>  OBJS-$(CONFIG_JACK_INDEV)                += jack_audio.o timefilter.o
>  OBJS-$(CONFIG_LAVFI_INDEV)               += lavfi.o
> diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c
> index 33ce155..5178f30 100644
> --- a/libavdevice/alldevices.c
> +++ b/libavdevice/alldevices.c
> @@ -51,7 +51,7 @@ void avdevice_register_all(void)
>      REGISTER_OUTDEV  (CACA,             caca);
>      REGISTER_INDEV   (DSHOW,            dshow);
>      REGISTER_INDEV   (DV1394,           dv1394);
> -    REGISTER_INDEV   (FBDEV,            fbdev);
> +    REGISTER_INOUTDEV(FBDEV,            fbdev);
>      REGISTER_INDEV   (IEC61883,         iec61883);
>      REGISTER_INDEV   (JACK,             jack);
>      REGISTER_INDEV   (LAVFI,            lavfi);
> diff --git a/libavdevice/fbdev_enc.c b/libavdevice/fbdev_enc.c
> new file mode 100644
> index 0000000..d65e2b5
> --- /dev/null
> +++ b/libavdevice/fbdev_enc.c
> @@ -0,0 +1,257 @@
> +/*
> + * Copyright (c) 2013 Lukasz Marek
> + *
> + * 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 <unistd.h>
> +#include <fcntl.h>
> +#include <sys/ioctl.h>
> +#include <sys/mman.h>
> +#include <linux/fb.h>
> +#include "libavutil/pixdesc.h"
> +#include "libavutil/log.h"
> +#include "libavutil/mem.h"
> +#include "libavutil/opt.h"
> +#include "libavformat/avformat.h"
> +
> +typedef struct {
> +    AVClass *class;                   ///< class for private options
> +    int xoffset;                      ///< x coordinate of top left corner
> +    int yoffset;                      ///< y coordinate of top left corner
> +    struct fb_var_screeninfo varinfo; ///< framebuffer variable info
> +    struct fb_fix_screeninfo fixinfo; ///< framebuffer fixed info
> +    int fd;                           ///< framebuffer device file descriptor
> +    int index;                        ///< index of a video stream
> +    uint8_t *data;                    ///< framebuffer data
> +    int width;                        ///< width of source video
> +    int height;                       ///< height of source video
> +    int bytes_per_pixel;              ///< bytes per pixel of source video
> +    enum AVPixelFormat pix_fmt;       ///< pixel format of source video
> +} FBDevContext;
> +
> +struct rgb_pixfmt_map_entry {
> +    int bits_per_pixel;
> +    int red_offset, green_offset, blue_offset, alpha_offset;
> +    enum AVPixelFormat pixfmt;
> +};
> +
> +static const struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
> +    // bpp, red_offset,  green_offset, blue_offset, alpha_offset, pixfmt
> +    {  32,       0,           8,          16,           24,   AV_PIX_FMT_RGBA  },
> +    {  32,      16,           8,           0,           24,   AV_PIX_FMT_BGRA  },
> +    {  32,       8,          16,          24,            0,   AV_PIX_FMT_ARGB  },
> +    {  32,       3,           2,           8,            0,   AV_PIX_FMT_ABGR  },
> +    {  24,       0,           8,          16,            0,   AV_PIX_FMT_RGB24 },
> +    {  24,      16,           8,           0,            0,   AV_PIX_FMT_BGR24 },
> +    {  16,      11,           5,           0,           16,   AV_PIX_FMT_RGB565 },
> +};
> +
> +static enum AVPixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
> +{
> +    int i;
> +
> +    for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
> +        const struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
> +        if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
> +            entry->red_offset     == varinfo->red.offset     &&
> +            entry->green_offset   == varinfo->green.offset   &&
> +            entry->blue_offset    == varinfo->blue.offset)
> +            return entry->pixfmt;
> +    }
> +
> +    return AV_PIX_FMT_NONE;
> +}
> +
> +static av_cold int fbdev_write_header(AVFormatContext *h)
> +{
> +    FBDevContext *fbdev = h->priv_data;
> +    enum AVPixelFormat pix_fmt;
> +    AVStream *st = NULL;
> +    int ret, flags = O_RDWR;
> +
> +    for (int i = 0; i < h->nb_streams; i++) {
> +        if (h->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
> +            if (!st) {
> +                fbdev->index = i;
> +                st = h->streams[i];
> +            } else {
> +                av_log(h, AV_LOG_WARNING, "More than one video stream found. First one is used.\n");
> +                break;
> +            }
> +        }
> +    }
> +    if (!st) {
> +        av_log(h, AV_LOG_ERROR, "No video stream found.\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if ((fbdev->fd = avpriv_open(h->filename, flags)) == -1) {
> +        ret = AVERROR(errno);
> +        av_log(h, AV_LOG_ERROR,
> +               "Could not open framebuffer device '%s': %s\n",
> +               h->filename, av_err2str(ret));
> +        return ret;
> +    }
> +
> +    if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
> +        ret = AVERROR(errno);
> +        av_log(h, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
> +        goto fail;
> +    }
> +
> +    if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
> +        ret = AVERROR(errno);
> +        av_log(h, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
> +        goto fail;
> +    }
> +
> +    pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
> +    if (pix_fmt == AV_PIX_FMT_NONE) {
> +        ret = AVERROR(EINVAL);
> +        av_log(h, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n");
> +        goto fail;
> +    }
> +
> +    fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0);
> +    if (fbdev->data == MAP_FAILED) {
> +        ret = AVERROR(errno);
> +        av_log(h, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
> +        goto fail;
> +    }
> +
> +    fbdev->pix_fmt = st->codec->pix_fmt;
> +    fbdev->width = st->codec->width;
> +    fbdev->height = st->codec->height;
> +    fbdev->bytes_per_pixel = ((st->codec->bits_per_coded_sample + 7) >> 3);
> +
> +    return 0;
> +  fail:
> +    close(fbdev->fd);
> +    return ret;
> +}
> +
> +static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt)
> +{
> +    FBDevContext *fbdev = h->priv_data;
> +    uint8_t *pin, *pout;
> +    enum AVPixelFormat pix_fmt;
> +    int disp_height;
> +    int bytes_to_copy;
> +    int src_line_size = fbdev->width * fbdev->bytes_per_pixel;
> +
> +    if (fbdev->index != pkt->stream_index)
> +        return 0;
> +

> +    if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
> +        av_log(h, AV_LOG_WARNING,
> +               "Error refreshing variable info: %s\n", strerror(errno));

Note: strerror() is not thread-safe, please use av_str2err(AVERROR(errno))

> +
> +    pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
> +
> +    if (pix_fmt != fbdev->pix_fmt) {
> +        av_log(h, AV_LOG_ERROR, "Pixel format %s is not supported, use %s\n",
> +               av_get_pix_fmt_name(fbdev->pix_fmt), av_get_pix_fmt_name(pix_fmt));
> +        return AVERROR(EINVAL);
> +    }

The logic and error message is a bit misleading. In case pixel format
changes dynamically it could be != fbdev->pix_fmt and still be a valid
pixel format. So you should probably first check in case you get
pix_fmt == NONE.

Alternatively you fail if you notice that the pixel format changes.

[...]

LGTM otherwise.
-- 
FFmpeg = Formidable Faithless Minimal Peaceful Erroneous Gnome


More information about the ffmpeg-devel mailing list