LCOV - code coverage report
Current view: top level - libavdevice - fbdev_dec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 93 0.0 %
Date: 2017-10-24 00:14:43 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2011 Stefano Sabatini
       3             :  * Copyright (c) 2009 Giliard B. de Freitas <giliarde@gmail.com>
       4             :  * Copyright (C) 2002 Gunnar Monell <gmo@linux.nu>
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * Linux framebuffer input device,
      26             :  * inspired by code from fbgrab.c by Gunnar Monell.
      27             :  * @see http://linux-fbdev.sourceforge.net/
      28             :  */
      29             : 
      30             : #include <unistd.h>
      31             : #include <fcntl.h>
      32             : #include <sys/ioctl.h>
      33             : #include <sys/mman.h>
      34             : #include <time.h>
      35             : #include <linux/fb.h>
      36             : 
      37             : #include "libavutil/internal.h"
      38             : #include "libavutil/log.h"
      39             : #include "libavutil/mem.h"
      40             : #include "libavutil/opt.h"
      41             : #include "libavutil/time.h"
      42             : #include "libavutil/parseutils.h"
      43             : #include "libavutil/pixdesc.h"
      44             : #include "libavformat/internal.h"
      45             : #include "avdevice.h"
      46             : #include "fbdev_common.h"
      47             : 
      48             : typedef struct FBDevContext {
      49             :     AVClass *class;          ///< class for private options
      50             :     int frame_size;          ///< size in bytes of a grabbed frame
      51             :     AVRational framerate_q;  ///< framerate
      52             :     int64_t time_frame;      ///< time for the next frame to output (in 1/1000000 units)
      53             : 
      54             :     int fd;                  ///< framebuffer device file descriptor
      55             :     int width, height;       ///< assumed frame resolution
      56             :     int frame_linesize;      ///< linesize of the output frame, it is assumed to be constant
      57             :     int bytes_per_pixel;
      58             : 
      59             :     struct fb_var_screeninfo varinfo; ///< variable info;
      60             :     struct fb_fix_screeninfo fixinfo; ///< fixed    info;
      61             : 
      62             :     uint8_t *data;           ///< framebuffer data
      63             : } FBDevContext;
      64             : 
      65           0 : static av_cold int fbdev_read_header(AVFormatContext *avctx)
      66             : {
      67           0 :     FBDevContext *fbdev = avctx->priv_data;
      68           0 :     AVStream *st = NULL;
      69             :     enum AVPixelFormat pix_fmt;
      70           0 :     int ret, flags = O_RDONLY;
      71             :     const char* device;
      72             : 
      73           0 :     if (!(st = avformat_new_stream(avctx, NULL)))
      74           0 :         return AVERROR(ENOMEM);
      75           0 :     avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
      76             : 
      77             :     /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
      78           0 :     if (avctx->flags & AVFMT_FLAG_NONBLOCK)
      79           0 :         flags |= O_NONBLOCK;
      80             : 
      81           0 :     if (avctx->filename[0])
      82           0 :         device = avctx->filename;
      83             :     else
      84           0 :         device = ff_fbdev_default_device();
      85             : 
      86           0 :     if ((fbdev->fd = avpriv_open(device, flags)) == -1) {
      87           0 :         ret = AVERROR(errno);
      88           0 :         av_log(avctx, AV_LOG_ERROR,
      89             :                "Could not open framebuffer device '%s': %s\n",
      90           0 :                device, av_err2str(ret));
      91           0 :         return ret;
      92             :     }
      93             : 
      94           0 :     if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
      95           0 :         ret = AVERROR(errno);
      96           0 :         av_log(avctx, AV_LOG_ERROR,
      97           0 :                "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
      98           0 :         goto fail;
      99             :     }
     100             : 
     101           0 :     if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
     102           0 :         ret = AVERROR(errno);
     103           0 :         av_log(avctx, AV_LOG_ERROR,
     104           0 :                "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
     105           0 :         goto fail;
     106             :     }
     107             : 
     108           0 :     pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
     109           0 :     if (pix_fmt == AV_PIX_FMT_NONE) {
     110           0 :         ret = AVERROR(EINVAL);
     111           0 :         av_log(avctx, AV_LOG_ERROR,
     112             :                "Framebuffer pixel format not supported.\n");
     113           0 :         goto fail;
     114             :     }
     115             : 
     116           0 :     fbdev->width           = fbdev->varinfo.xres;
     117           0 :     fbdev->height          = fbdev->varinfo.yres;
     118           0 :     fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
     119           0 :     fbdev->frame_linesize  = fbdev->width * fbdev->bytes_per_pixel;
     120           0 :     fbdev->frame_size      = fbdev->frame_linesize * fbdev->height;
     121           0 :     fbdev->time_frame      = AV_NOPTS_VALUE;
     122           0 :     fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
     123           0 :     if (fbdev->data == MAP_FAILED) {
     124           0 :         ret = AVERROR(errno);
     125           0 :         av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
     126           0 :         goto fail;
     127             :     }
     128             : 
     129           0 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     130           0 :     st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
     131           0 :     st->codecpar->width      = fbdev->width;
     132           0 :     st->codecpar->height     = fbdev->height;
     133           0 :     st->codecpar->format     = pix_fmt;
     134           0 :     st->avg_frame_rate       = fbdev->framerate_q;
     135           0 :     st->codecpar->bit_rate   =
     136           0 :         fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
     137             : 
     138           0 :     av_log(avctx, AV_LOG_INFO,
     139             :            "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%"PRId64"\n",
     140             :            fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
     141             :            av_get_pix_fmt_name(pix_fmt),
     142             :            fbdev->framerate_q.num, fbdev->framerate_q.den,
     143           0 :            st->codecpar->bit_rate);
     144           0 :     return 0;
     145             : 
     146           0 : fail:
     147           0 :     close(fbdev->fd);
     148           0 :     return ret;
     149             : }
     150             : 
     151           0 : static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
     152             : {
     153           0 :     FBDevContext *fbdev = avctx->priv_data;
     154             :     int64_t curtime, delay;
     155             :     struct timespec ts;
     156             :     int i, ret;
     157             :     uint8_t *pin, *pout;
     158             : 
     159           0 :     if (fbdev->time_frame == AV_NOPTS_VALUE)
     160           0 :         fbdev->time_frame = av_gettime();
     161             : 
     162             :     /* wait based on the frame rate */
     163             :     while (1) {
     164           0 :         curtime = av_gettime();
     165           0 :         delay = fbdev->time_frame - curtime;
     166           0 :         av_log(avctx, AV_LOG_TRACE,
     167             :                 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
     168             :                 fbdev->time_frame, curtime, delay);
     169           0 :         if (delay <= 0) {
     170           0 :             fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
     171           0 :             break;
     172             :         }
     173           0 :         if (avctx->flags & AVFMT_FLAG_NONBLOCK)
     174           0 :             return AVERROR(EAGAIN);
     175           0 :         ts.tv_sec  =  delay / 1000000;
     176           0 :         ts.tv_nsec = (delay % 1000000) * 1000;
     177           0 :         while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
     178             :     }
     179             : 
     180           0 :     if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
     181           0 :         return ret;
     182             : 
     183             :     /* refresh fbdev->varinfo, visible data position may change at each call */
     184           0 :     if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
     185           0 :         av_log(avctx, AV_LOG_WARNING,
     186           0 :                "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
     187             :     }
     188             : 
     189           0 :     pkt->pts = curtime;
     190             : 
     191             :     /* compute visible data offset */
     192           0 :     pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
     193           0 :                         fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
     194           0 :     pout = pkt->data;
     195             : 
     196           0 :     for (i = 0; i < fbdev->height; i++) {
     197           0 :         memcpy(pout, pin, fbdev->frame_linesize);
     198           0 :         pin  += fbdev->fixinfo.line_length;
     199           0 :         pout += fbdev->frame_linesize;
     200             :     }
     201             : 
     202           0 :     return fbdev->frame_size;
     203             : }
     204             : 
     205           0 : static av_cold int fbdev_read_close(AVFormatContext *avctx)
     206             : {
     207           0 :     FBDevContext *fbdev = avctx->priv_data;
     208             : 
     209           0 :     munmap(fbdev->data, fbdev->fixinfo.smem_len);
     210           0 :     close(fbdev->fd);
     211             : 
     212           0 :     return 0;
     213             : }
     214             : 
     215           0 : static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
     216             : {
     217           0 :     return ff_fbdev_get_device_list(device_list);
     218             : }
     219             : 
     220             : #define OFFSET(x) offsetof(FBDevContext, x)
     221             : #define DEC AV_OPT_FLAG_DECODING_PARAM
     222             : static const AVOption options[] = {
     223             :     { "framerate","", OFFSET(framerate_q), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
     224             :     { NULL },
     225             : };
     226             : 
     227             : static const AVClass fbdev_class = {
     228             :     .class_name = "fbdev indev",
     229             :     .item_name  = av_default_item_name,
     230             :     .option     = options,
     231             :     .version    = LIBAVUTIL_VERSION_INT,
     232             :     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
     233             : };
     234             : 
     235             : AVInputFormat ff_fbdev_demuxer = {
     236             :     .name           = "fbdev",
     237             :     .long_name      = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
     238             :     .priv_data_size = sizeof(FBDevContext),
     239             :     .read_header    = fbdev_read_header,
     240             :     .read_packet    = fbdev_read_packet,
     241             :     .read_close     = fbdev_read_close,
     242             :     .get_device_list = fbdev_get_device_list,
     243             :     .flags          = AVFMT_NOFILE,
     244             :     .priv_class     = &fbdev_class,
     245             : };

Generated by: LCOV version 1.13