LCOV - code coverage report
Current view: top level - libavcodec - v4l2_m2m.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 197 0.0 %
Date: 2017-12-14 01:15:32 Functions: 0 9 0.0 %

          Line data    Source code
       1             : /*
       2             :  * V4L mem2mem
       3             :  *
       4             :  * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
       5             :  * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
       6             :  *
       7             :  * This file is part of FFmpeg.
       8             :  *
       9             :  * FFmpeg is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free Software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  *
      14             :  * FFmpeg is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  *
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with FFmpeg; if not, write to the Free Software
      21             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      22             :  */
      23             : 
      24             : #include <linux/videodev2.h>
      25             : #include <sys/ioctl.h>
      26             : #include <sys/mman.h>
      27             : #include <unistd.h>
      28             : #include <dirent.h>
      29             : #include <fcntl.h>
      30             : #include "libavcodec/avcodec.h"
      31             : #include "libavcodec/internal.h"
      32             : #include "libavutil/pixdesc.h"
      33             : #include "libavutil/imgutils.h"
      34             : #include "libavutil/pixfmt.h"
      35             : #include "v4l2_context.h"
      36             : #include "v4l2_fmt.h"
      37             : #include "v4l2_m2m.h"
      38             : 
      39           0 : static inline int v4l2_splane_video(struct v4l2_capability *cap)
      40             : {
      41           0 :     if (cap->capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT) &&
      42           0 :         cap->capabilities & V4L2_CAP_STREAMING)
      43           0 :         return 1;
      44             : 
      45           0 :     if (cap->capabilities & V4L2_CAP_VIDEO_M2M)
      46           0 :         return 1;
      47             : 
      48           0 :     return 0;
      49             : }
      50             : 
      51           0 : static inline int v4l2_mplane_video(struct v4l2_capability *cap)
      52             : {
      53           0 :     if (cap->capabilities & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE) &&
      54           0 :         cap->capabilities & V4L2_CAP_STREAMING)
      55           0 :         return 1;
      56             : 
      57           0 :     if (cap->capabilities & V4L2_CAP_VIDEO_M2M_MPLANE)
      58           0 :         return 1;
      59             : 
      60           0 :     return 0;
      61             : }
      62             : 
      63           0 : static int v4l2_prepare_contexts(V4L2m2mContext* s)
      64             : {
      65             :     struct v4l2_capability cap;
      66             :     int ret;
      67             : 
      68           0 :     s->capture.done = s->output.done = 0;
      69           0 :     s->capture.name = "capture";
      70           0 :     s->output.name = "output ";
      71           0 :     atomic_init(&s->refcount, 0);
      72           0 :     sem_init(&s->refsync, 0, 0);
      73             : 
      74           0 :     memset(&cap, 0, sizeof(cap));
      75           0 :     ret = ioctl(s->fd, VIDIOC_QUERYCAP, &cap);
      76           0 :     if (ret < 0)
      77           0 :         return ret;
      78             : 
      79           0 :     av_log(s->avctx, AV_LOG_INFO, "driver '%s' on card '%s'\n", cap.driver, cap.card);
      80             : 
      81           0 :     if (v4l2_mplane_video(&cap)) {
      82           0 :         s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
      83           0 :         s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
      84           0 :         return 0;
      85             :     }
      86             : 
      87           0 :     if (v4l2_splane_video(&cap)) {
      88           0 :         s->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
      89           0 :         s->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
      90           0 :         return 0;
      91             :     }
      92             : 
      93           0 :     return AVERROR(EINVAL);
      94             : }
      95             : 
      96           0 : static int v4l2_probe_driver(V4L2m2mContext* s)
      97             : {
      98             :     int ret;
      99             : 
     100           0 :     s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0);
     101           0 :     if (s->fd < 0)
     102           0 :         return AVERROR(errno);
     103             : 
     104           0 :     ret = v4l2_prepare_contexts(s);
     105           0 :     if (ret < 0)
     106           0 :         goto done;
     107             : 
     108           0 :     ret = ff_v4l2_context_get_format(&s->output);
     109           0 :     if (ret) {
     110           0 :         av_log(s->avctx, AV_LOG_DEBUG, "v4l2 output format not supported\n");
     111           0 :         goto done;
     112             :     }
     113             : 
     114           0 :     ret = ff_v4l2_context_get_format(&s->capture);
     115           0 :     if (ret) {
     116           0 :         av_log(s->avctx, AV_LOG_DEBUG, "v4l2 capture format not supported\n");
     117           0 :         goto done;
     118             :     }
     119             : 
     120           0 : done:
     121           0 :     if (close(s->fd) < 0) {
     122           0 :         ret = AVERROR(errno);
     123           0 :         av_log(s->avctx, AV_LOG_ERROR, "failure closing %s (%s)\n", s->devname, av_err2str(AVERROR(errno)));
     124             :     }
     125             : 
     126           0 :     s->fd = -1;
     127             : 
     128           0 :     return ret;
     129             : }
     130             : 
     131           0 : static int v4l2_configure_contexts(V4L2m2mContext* s)
     132             : {
     133           0 :     void *log_ctx = s->avctx;
     134             :     int ret;
     135             : 
     136           0 :     s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0);
     137           0 :     if (s->fd < 0)
     138           0 :         return AVERROR(errno);
     139             : 
     140           0 :     ret = v4l2_prepare_contexts(s);
     141           0 :     if (ret < 0)
     142           0 :         goto error;
     143             : 
     144           0 :     ret = ff_v4l2_context_set_format(&s->output);
     145           0 :     if (ret) {
     146           0 :         av_log(log_ctx, AV_LOG_ERROR, "can't set v4l2 output format\n");
     147           0 :         goto error;
     148             :     }
     149             : 
     150           0 :     ret = ff_v4l2_context_set_format(&s->capture);
     151           0 :     if (ret) {
     152           0 :         av_log(log_ctx, AV_LOG_ERROR, "can't to set v4l2 capture format\n");
     153           0 :         goto error;
     154             :     }
     155             : 
     156           0 :     ret = ff_v4l2_context_init(&s->output);
     157           0 :     if (ret) {
     158           0 :         av_log(log_ctx, AV_LOG_ERROR, "no v4l2 output context's buffers\n");
     159           0 :         goto error;
     160             :     }
     161             : 
     162             :     /* decoder's buffers need to be updated at a later stage */
     163           0 :     if (!av_codec_is_decoder(s->avctx->codec)) {
     164           0 :         ret = ff_v4l2_context_init(&s->capture);
     165           0 :         if (ret) {
     166           0 :             av_log(log_ctx, AV_LOG_ERROR, "no v4l2 capture context's buffers\n");
     167           0 :             goto error;
     168             :         }
     169             :     }
     170             : 
     171           0 :     return 0;
     172             : 
     173           0 : error:
     174           0 :     if (close(s->fd) < 0) {
     175           0 :         ret = AVERROR(errno);
     176           0 :         av_log(log_ctx, AV_LOG_ERROR, "error closing %s (%s)\n",
     177           0 :             s->devname, av_err2str(AVERROR(errno)));
     178             :     }
     179           0 :     s->fd = -1;
     180             : 
     181           0 :     return ret;
     182             : }
     183             : 
     184             : /******************************************************************************
     185             :  *
     186             :  *                  V4L2 M2M Interface
     187             :  *
     188             :  ******************************************************************************/
     189           0 : int ff_v4l2_m2m_codec_reinit(V4L2m2mContext* s)
     190             : {
     191             :     int ret;
     192             : 
     193           0 :     av_log(s->avctx, AV_LOG_DEBUG, "reinit context\n");
     194             : 
     195             :     /* 1. streamoff */
     196           0 :     ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF);
     197           0 :     if (ret)
     198           0 :         av_log(s->avctx, AV_LOG_ERROR, "capture VIDIOC_STREAMOFF\n");
     199             : 
     200             :     /* 2. unmap the capture buffers (v4l2 and ffmpeg):
     201             :      *    we must wait for all references to be released before being allowed
     202             :      *    to queue new buffers.
     203             :      */
     204           0 :     av_log(s->avctx, AV_LOG_DEBUG, "waiting for user to release AVBufferRefs\n");
     205           0 :     if (atomic_load(&s->refcount))
     206           0 :         while(sem_wait(&s->refsync) == -1 && errno == EINTR);
     207             : 
     208           0 :     ff_v4l2_context_release(&s->capture);
     209             : 
     210             :     /* 3. get the new capture format */
     211           0 :     ret = ff_v4l2_context_get_format(&s->capture);
     212           0 :     if (ret) {
     213           0 :         av_log(s->avctx, AV_LOG_ERROR, "query the new capture format\n");
     214           0 :         return ret;
     215             :     }
     216             : 
     217             :     /* 4. set the capture format */
     218           0 :     ret = ff_v4l2_context_set_format(&s->capture);
     219           0 :     if (ret) {
     220           0 :         av_log(s->avctx, AV_LOG_ERROR, "setting capture format\n");
     221           0 :         return ret;
     222             :     }
     223             : 
     224             :     /* 5. complete reinit */
     225           0 :     sem_destroy(&s->refsync);
     226           0 :     sem_init(&s->refsync, 0, 0);
     227           0 :     s->draining = 0;
     228           0 :     s->reinit = 0;
     229             : 
     230           0 :     return 0;
     231             : }
     232             : 
     233           0 : int ff_v4l2_m2m_codec_full_reinit(V4L2m2mContext *s)
     234             : {
     235           0 :     void *log_ctx = s->avctx;
     236             :     int ret;
     237             : 
     238           0 :     av_log(log_ctx, AV_LOG_DEBUG, "%s full reinit\n", s->devname);
     239             : 
     240             :     /* wait for pending buffer references */
     241           0 :     if (atomic_load(&s->refcount))
     242           0 :         while(sem_wait(&s->refsync) == -1 && errno == EINTR);
     243             : 
     244             :     /* close the driver */
     245           0 :     ff_v4l2_m2m_codec_end(s->avctx);
     246             : 
     247             :     /* start again now that we know the stream dimensions */
     248           0 :     s->draining = 0;
     249           0 :     s->reinit = 0;
     250             : 
     251           0 :     s->fd = open(s->devname, O_RDWR | O_NONBLOCK, 0);
     252           0 :     if (s->fd < 0)
     253           0 :         return AVERROR(errno);
     254             : 
     255           0 :     ret = v4l2_prepare_contexts(s);
     256           0 :     if (ret < 0)
     257           0 :         goto error;
     258             : 
     259             :     /* if a full re-init was requested - probe didn't run - we need to populate
     260             :      * the format for each context
     261             :      */
     262           0 :     ret = ff_v4l2_context_get_format(&s->output);
     263           0 :     if (ret) {
     264           0 :         av_log(log_ctx, AV_LOG_DEBUG, "v4l2 output format not supported\n");
     265           0 :         goto error;
     266             :     }
     267             : 
     268           0 :     ret = ff_v4l2_context_get_format(&s->capture);
     269           0 :     if (ret) {
     270           0 :         av_log(log_ctx, AV_LOG_DEBUG, "v4l2 capture format not supported\n");
     271           0 :         goto error;
     272             :     }
     273             : 
     274           0 :     ret = ff_v4l2_context_set_format(&s->output);
     275           0 :     if (ret) {
     276           0 :         av_log(log_ctx, AV_LOG_ERROR, "can't set v4l2 output format\n");
     277           0 :         goto error;
     278             :     }
     279             : 
     280           0 :     ret = ff_v4l2_context_set_format(&s->capture);
     281           0 :     if (ret) {
     282           0 :         av_log(log_ctx, AV_LOG_ERROR, "can't to set v4l2 capture format\n");
     283           0 :         goto error;
     284             :     }
     285             : 
     286           0 :     ret = ff_v4l2_context_init(&s->output);
     287           0 :     if (ret) {
     288           0 :         av_log(log_ctx, AV_LOG_ERROR, "no v4l2 output context's buffers\n");
     289           0 :         goto error;
     290             :     }
     291             : 
     292             :     /* decoder's buffers need to be updated at a later stage */
     293           0 :     if (!av_codec_is_decoder(s->avctx->codec)) {
     294           0 :         ret = ff_v4l2_context_init(&s->capture);
     295           0 :         if (ret) {
     296           0 :             av_log(log_ctx, AV_LOG_ERROR, "no v4l2 capture context's buffers\n");
     297           0 :             goto error;
     298             :         }
     299             :     }
     300             : 
     301           0 :     return 0;
     302             : 
     303           0 : error:
     304           0 :     if (close(s->fd) < 0) {
     305           0 :         ret = AVERROR(errno);
     306           0 :         av_log(log_ctx, AV_LOG_ERROR, "error closing %s (%s)\n",
     307           0 :             s->devname, av_err2str(AVERROR(errno)));
     308             :     }
     309           0 :     s->fd = -1;
     310             : 
     311           0 :     return ret;
     312             : }
     313             : 
     314           0 : int ff_v4l2_m2m_codec_end(AVCodecContext *avctx)
     315             : {
     316           0 :     V4L2m2mContext* s = avctx->priv_data;
     317             :     int ret;
     318             : 
     319           0 :     ret = ff_v4l2_context_set_status(&s->output, VIDIOC_STREAMOFF);
     320           0 :     if (ret)
     321           0 :             av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->output.name);
     322             : 
     323           0 :     ret = ff_v4l2_context_set_status(&s->capture, VIDIOC_STREAMOFF);
     324           0 :     if (ret)
     325           0 :         av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s\n", s->capture.name);
     326             : 
     327           0 :     ff_v4l2_context_release(&s->output);
     328             : 
     329           0 :     if (atomic_load(&s->refcount))
     330           0 :         av_log(avctx, AV_LOG_ERROR, "ff_v4l2m2m_codec_end leaving pending buffers\n");
     331             : 
     332           0 :     ff_v4l2_context_release(&s->capture);
     333           0 :     sem_destroy(&s->refsync);
     334             : 
     335             :     /* release the hardware */
     336           0 :     if (close(s->fd) < 0 )
     337           0 :         av_log(avctx, AV_LOG_ERROR, "failure closing %s (%s)\n", s->devname, av_err2str(AVERROR(errno)));
     338             : 
     339           0 :     s->fd = -1;
     340             : 
     341           0 :     return 0;
     342             : }
     343             : 
     344           0 : int ff_v4l2_m2m_codec_init(AVCodecContext *avctx)
     345             : {
     346           0 :     int ret = AVERROR(EINVAL);
     347             :     struct dirent *entry;
     348             :     char node[PATH_MAX];
     349             :     DIR *dirp;
     350             : 
     351           0 :     V4L2m2mContext *s = avctx->priv_data;
     352           0 :     s->avctx = avctx;
     353             : 
     354           0 :     dirp = opendir("/dev");
     355           0 :     if (!dirp)
     356           0 :         return AVERROR(errno);
     357             : 
     358           0 :     for (entry = readdir(dirp); entry; entry = readdir(dirp)) {
     359             : 
     360           0 :         if (strncmp(entry->d_name, "video", 5))
     361           0 :             continue;
     362             : 
     363           0 :         snprintf(node, sizeof(node), "/dev/%s", entry->d_name);
     364           0 :         av_log(s->avctx, AV_LOG_DEBUG, "probing device %s\n", node);
     365           0 :         strncpy(s->devname, node, strlen(node) + 1);
     366           0 :         ret = v4l2_probe_driver(s);
     367           0 :         if (!ret)
     368           0 :                 break;
     369             :     }
     370             : 
     371           0 :     closedir(dirp);
     372             : 
     373           0 :     if (ret) {
     374           0 :         av_log(s->avctx, AV_LOG_ERROR, "Could not find a valid device\n");
     375           0 :         memset(s->devname, 0, sizeof(s->devname));
     376             : 
     377           0 :         return ret;
     378             :     }
     379             : 
     380           0 :     av_log(s->avctx, AV_LOG_INFO, "Using device %s\n", node);
     381             : 
     382           0 :     return v4l2_configure_contexts(s);
     383             : }

Generated by: LCOV version 1.13