LCOV - code coverage report
Current view: top level - src/libavdevice - v4l2enc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 44 0.0 %
Date: 2017-01-19 23:52:33 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Clément Bœsch
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include "v4l2-common.h"
      22             : #include "avdevice.h"
      23             : 
      24             : typedef struct {
      25             :     AVClass *class;
      26             :     int fd;
      27             : } V4L2Context;
      28             : 
      29           0 : static av_cold int write_header(AVFormatContext *s1)
      30             : {
      31           0 :     int res = 0, flags = O_RDWR;
      32           0 :     struct v4l2_format fmt = {
      33             :         .type = V4L2_BUF_TYPE_VIDEO_OUTPUT
      34             :     };
      35           0 :     V4L2Context *s = s1->priv_data;
      36             :     AVCodecParameters *par;
      37             :     uint32_t v4l2_pixfmt;
      38             : 
      39           0 :     if (s1->flags & AVFMT_FLAG_NONBLOCK)
      40           0 :         flags |= O_NONBLOCK;
      41             : 
      42           0 :     s->fd = open(s1->filename, flags);
      43           0 :     if (s->fd < 0) {
      44           0 :         res = AVERROR(errno);
      45           0 :         av_log(s1, AV_LOG_ERROR, "Unable to open V4L2 device '%s'\n", s1->filename);
      46           0 :         return res;
      47             :     }
      48             : 
      49           0 :     if (s1->nb_streams != 1 ||
      50           0 :         s1->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
      51           0 :         s1->streams[0]->codecpar->codec_id   != AV_CODEC_ID_RAWVIDEO) {
      52           0 :         av_log(s1, AV_LOG_ERROR,
      53             :                "V4L2 output device supports only a single raw video stream\n");
      54           0 :         return AVERROR(EINVAL);
      55             :     }
      56             : 
      57           0 :     par = s1->streams[0]->codecpar;
      58             : 
      59           0 :     v4l2_pixfmt = ff_fmt_ff2v4l(par->format, AV_CODEC_ID_RAWVIDEO);
      60           0 :     if (!v4l2_pixfmt) { // XXX: try to force them one by one?
      61           0 :         av_log(s1, AV_LOG_ERROR, "Unknown V4L2 pixel format equivalent for %s\n",
      62           0 :                av_get_pix_fmt_name(par->format));
      63           0 :         return AVERROR(EINVAL);
      64             :     }
      65             : 
      66           0 :     if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
      67           0 :         res = AVERROR(errno);
      68           0 :         av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", av_err2str(res));
      69           0 :         return res;
      70             :     }
      71             : 
      72           0 :     fmt.fmt.pix.width       = par->width;
      73           0 :     fmt.fmt.pix.height      = par->height;
      74           0 :     fmt.fmt.pix.pixelformat = v4l2_pixfmt;
      75           0 :     fmt.fmt.pix.sizeimage   = av_image_get_buffer_size(par->format, par->width, par->height, 1);
      76             : 
      77           0 :     if (ioctl(s->fd, VIDIOC_S_FMT, &fmt) < 0) {
      78           0 :         res = AVERROR(errno);
      79           0 :         av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_FMT): %s\n", av_err2str(res));
      80           0 :         return res;
      81             :     }
      82             : 
      83           0 :     return res;
      84             : }
      85             : 
      86           0 : static int write_packet(AVFormatContext *s1, AVPacket *pkt)
      87             : {
      88           0 :     const V4L2Context *s = s1->priv_data;
      89           0 :     if (write(s->fd, pkt->data, pkt->size) == -1)
      90           0 :         return AVERROR(errno);
      91           0 :     return 0;
      92             : }
      93             : 
      94           0 : static int write_trailer(AVFormatContext *s1)
      95             : {
      96           0 :     const V4L2Context *s = s1->priv_data;
      97           0 :     close(s->fd);
      98           0 :     return 0;
      99             : }
     100             : 
     101             : static const AVClass v4l2_class = {
     102             :     .class_name = "V4L2 outdev",
     103             :     .item_name  = av_default_item_name,
     104             :     .version    = LIBAVUTIL_VERSION_INT,
     105             :     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
     106             : };
     107             : 
     108             : AVOutputFormat ff_v4l2_muxer = {
     109             :     .name           = "v4l2",
     110             :     .long_name      = NULL_IF_CONFIG_SMALL("Video4Linux2 output device"),
     111             :     .priv_data_size = sizeof(V4L2Context),
     112             :     .audio_codec    = AV_CODEC_ID_NONE,
     113             :     .video_codec    = AV_CODEC_ID_RAWVIDEO,
     114             :     .write_header   = write_header,
     115             :     .write_packet   = write_packet,
     116             :     .write_trailer  = write_trailer,
     117             :     .flags          = AVFMT_NOFILE,
     118             :     .priv_class     = &v4l2_class,
     119             : };

Generated by: LCOV version 1.12