[FFmpeg-devel] [PATCH 2/2] lavd: add v4l2 outdev.

Stefano Sabatini stefasab at gmail.com
Mon May 20 00:37:20 CEST 2013


On date Sunday 2013-05-19 23:39:47 +0200, Clément Bœsch encoded:
> TODO: lavd minor bump
> ---
>  Changelog                |   1 +
>  configure                |   1 +
>  libavdevice/Makefile     |   1 +
>  libavdevice/alldevices.c |   2 +-
>  libavdevice/v4l2enc.c    | 108 +++++++++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 112 insertions(+), 1 deletion(-)
>  create mode 100644 libavdevice/v4l2enc.c
> 
> diff --git a/Changelog b/Changelog
> index 249e8c3..2ed6c87 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -51,6 +51,7 @@ version <next>:
>  - Apple Intermediate Codec decoder
>  - Escape 130 video decoder
>  - FTP protocol support
> +- V4L2 output device
>  
>  
>  version 1.2:
> diff --git a/configure b/configure
> index 6fe7945..01f14fe 100755
> --- a/configure
> +++ b/configure
> @@ -2079,6 +2079,7 @@ sndio_indev_deps="sndio_h"
>  sndio_outdev_deps="sndio_h"
>  v4l_indev_deps="linux_videodev_h"
>  v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h"
> +v4l2_outdev_deps_any="linux_videodev2_h sys_videoio_h"
>  vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines"
>  vfwcap_indev_extralibs="-lavicap32"
>  x11grab_indev_deps="x11grab"
> diff --git a/libavdevice/Makefile b/libavdevice/Makefile
> index dfde768..f19ccdb 100644
> --- a/libavdevice/Makefile
> +++ b/libavdevice/Makefile
> @@ -33,6 +33,7 @@ OBJS-$(CONFIG_SDL_OUTDEV)                += sdl.o
>  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 v4l2-common.o timefilter.o
> +OBJS-$(CONFIG_V4L2_OUTDEV)               += v4l2enc.o v4l2-common.o
>  OBJS-$(CONFIG_V4L_INDEV)                 += v4l.o
>  OBJS-$(CONFIG_VFWCAP_INDEV)              += vfwcap.o
>  OBJS-$(CONFIG_X11GRAB_INDEV)             += x11grab.o
> diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c
> index daa6638..9672a47 100644
> --- a/libavdevice/alldevices.c
> +++ b/libavdevice/alldevices.c
> @@ -60,7 +60,7 @@ void avdevice_register_all(void)
>      REGISTER_INDEV   (PULSE,            pulse);
>      REGISTER_OUTDEV  (SDL,              sdl);
>      REGISTER_INOUTDEV(SNDIO,            sndio);
> -    REGISTER_INDEV   (V4L2,             v4l2);
> +    REGISTER_INOUTDEV(V4L2,             v4l2);
>  //    REGISTER_INDEV   (V4L,              v4l
>      REGISTER_INDEV   (VFWCAP,           vfwcap);
>      REGISTER_INDEV   (X11GRAB,          x11grab);
> diff --git a/libavdevice/v4l2enc.c b/libavdevice/v4l2enc.c
> new file mode 100644
> index 0000000..eaa77ea
> --- /dev/null
> +++ b/libavdevice/v4l2enc.c
> @@ -0,0 +1,108 @@
> +/*
> + * Copyright (c) 2013 Clément Bœsch
> + *
> + * 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 "v4l2-common.h"
> +#include "avdevice.h"
> +

> +struct video_data {
> +    int fd;
> +};

Silly name? V4L2Context seems more sensible.

> +
> +static av_cold int write_header(AVFormatContext *s1)
> +{
> +    int res = 0, flags = O_RDWR;
> +    struct v4l2_format fmt = {
> +        .type = V4L2_BUF_TYPE_VIDEO_OUTPUT
> +    };
> +    struct video_data *s = s1->priv_data;

> +    AVCodecContext *enc;

nit++: enc_ctx?

> +    uint32_t v4l2_pixfmt;
> +
> +    if (s1->flags & AVFMT_FLAG_NONBLOCK)
> +        flags |= O_NONBLOCK;
> +
> +    s->fd = open(s1->filename, flags);
> +    if (s->fd < 0) {

> +        av_log(s1, AV_LOG_ERROR, "Unable to open V4L2 device '%s'.\n", s1->filename);

Nit: no trailing "."

Also:

    if (s->fd < 0) {
       ret = AVERROR(errno); 
       av_log(s1, AV_LOG_ERROR, "Unable to open V4L2 device '%s');
       return ret;
    }

since av_log might change errno.

> +    }
> +
> +    if (s1->nb_streams != 1 ||
> +        s1->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
> +        s1->streams[0]->codec->codec_id   != AV_CODEC_ID_RAWVIDEO) {
> +        av_log(s1, AV_LOG_ERROR,
> +               "V4L2 output device supports only a single raw video stream.\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    enc = s1->streams[0]->codec;
> +
> +    v4l2_pixfmt = avpriv_fmt_ff2v4l(enc->pix_fmt, AV_CODEC_ID_RAWVIDEO);
> +    if (!v4l2_pixfmt) { // XXX: try to force them one by one?
> +        av_log(s1, AV_LOG_ERROR, "Unknow V4L2 pixel format equivalent for %s\n",
> +               av_get_pix_fmt_name(enc->pix_fmt));
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
> +        res = AVERROR(errno);
> +        av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", av_err2str(res));
> +        return res;
> +    }
> +
> +    fmt.fmt.pix.width       = enc->width;
> +    fmt.fmt.pix.height      = enc->height;
> +    fmt.fmt.pix.pixelformat = v4l2_pixfmt;
> +    fmt.fmt.pix.sizeimage   = av_image_get_buffer_size(enc->pix_fmt, enc->width, enc->height, 1);
> +
> +    if (ioctl(s->fd, VIDIOC_S_FMT, &fmt) < 0) {
> +        res = AVERROR(errno);
> +        av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_FMT): %s\n", av_err2str(res));
> +        return res;
> +    }
> +
> +    return res;
> +}
> +
> +static int write_packet(AVFormatContext *s1, AVPacket *pkt)
> +{
> +    const struct video_data *s = s1->priv_data;
> +    write(s->fd, pkt->data, pkt->size);
> +    return 0;
> +}
> +
> +static int write_trailer(AVFormatContext *s1)
> +{
> +    const struct video_data *s = s1->priv_data;
> +    close(s->fd);
> +    return 0;
> +}
> +
> +AVOutputFormat ff_v4l2_muxer = {
> +    .name           = "v4l2",

> +    .long_name      = NULL_IF_CONFIG_SMALL("Video4Linux2 device push"),

"push"?

Some docs in outdevs.texi with an example may be usefull as well.

LGTM otherwise, thanks.
-- 
FFmpeg = Fancy and Fantastic Mournful Powered Exxagerate Guru


More information about the ffmpeg-devel mailing list