LCOV - code coverage report
Current view: top level - libavcodec - v4l2_m2m_enc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 149 0.0 %
Date: 2017-12-12 03:56:30 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /*
       2             :  * V4L2 mem2mem encoders
       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 <search.h>
      27             : #include "libavcodec/avcodec.h"
      28             : #include "libavutil/pixdesc.h"
      29             : #include "libavutil/pixfmt.h"
      30             : #include "libavutil/opt.h"
      31             : #include "v4l2_context.h"
      32             : #include "v4l2_m2m.h"
      33             : 
      34             : #define MPEG_CID(x) V4L2_CID_MPEG_VIDEO_##x
      35             : #define MPEG_VIDEO(x) V4L2_MPEG_VIDEO_##x
      36             : 
      37           0 : static inline void v4l2_set_timeperframe(V4L2m2mContext *s, unsigned int num, unsigned int den)
      38             : {
      39           0 :     struct v4l2_streamparm parm = { 0 };
      40             : 
      41           0 :     parm.type = V4L2_TYPE_IS_MULTIPLANAR(s->output.type) ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT;
      42           0 :     parm.parm.output.timeperframe.denominator = den;
      43           0 :     parm.parm.output.timeperframe.numerator = num;
      44             : 
      45           0 :     if (ioctl(s->fd, VIDIOC_S_PARM, &parm) < 0)
      46           0 :         av_log(s->avctx, AV_LOG_WARNING, "Failed to set timeperframe");
      47           0 : }
      48             : 
      49           0 : static inline void v4l2_set_ext_ctrl(V4L2m2mContext *s, unsigned int id, signed int value, const char *name)
      50             : {
      51           0 :     struct v4l2_ext_controls ctrls = { 0 };
      52           0 :     struct v4l2_ext_control ctrl = { 0 };
      53             : 
      54             :     /* set ctrls */
      55           0 :     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
      56           0 :     ctrls.controls = &ctrl;
      57           0 :     ctrls.count = 1;
      58             : 
      59             :     /* set ctrl*/
      60           0 :     ctrl.value = value;
      61           0 :     ctrl.id = id ;
      62             : 
      63           0 :     if (ioctl(s->fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
      64           0 :         av_log(s->avctx, AV_LOG_WARNING, "Failed to set %s\n", name);
      65             :     else
      66           0 :         av_log(s->avctx, AV_LOG_DEBUG, "Encoder: %s = %d\n", name, value);
      67           0 : }
      68             : 
      69           0 : static inline int v4l2_get_ext_ctrl(V4L2m2mContext *s, unsigned int id, signed int *value, const char *name)
      70             : {
      71           0 :     struct v4l2_ext_controls ctrls = { 0 };
      72           0 :     struct v4l2_ext_control ctrl = { 0 };
      73             :     int ret;
      74             : 
      75             :     /* set ctrls */
      76           0 :     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
      77           0 :     ctrls.controls = &ctrl;
      78           0 :     ctrls.count = 1;
      79             : 
      80             :     /* set ctrl*/
      81           0 :     ctrl.id = id ;
      82             : 
      83           0 :     ret = ioctl(s->fd, VIDIOC_G_EXT_CTRLS, &ctrls);
      84           0 :     if (ret < 0) {
      85           0 :         av_log(s->avctx, AV_LOG_WARNING, "Failed to set %s\n", name);
      86           0 :         return ret;
      87             :     }
      88             : 
      89           0 :     *value = ctrl.value;
      90             : 
      91           0 :     return 0;
      92             : }
      93             : 
      94           0 : static inline unsigned int v4l2_h264_profile_from_ff(int p)
      95             : {
      96             :     static const struct h264_profile  {
      97             :         unsigned int ffmpeg_val;
      98             :         unsigned int v4l2_val;
      99             :     } profile[] = {
     100             :         { FF_PROFILE_H264_CONSTRAINED_BASELINE, MPEG_VIDEO(H264_PROFILE_CONSTRAINED_BASELINE) },
     101             :         { FF_PROFILE_H264_HIGH_444_PREDICTIVE, MPEG_VIDEO(H264_PROFILE_HIGH_444_PREDICTIVE) },
     102             :         { FF_PROFILE_H264_HIGH_422_INTRA, MPEG_VIDEO(H264_PROFILE_HIGH_422_INTRA) },
     103             :         { FF_PROFILE_H264_HIGH_444_INTRA, MPEG_VIDEO(H264_PROFILE_HIGH_444_INTRA) },
     104             :         { FF_PROFILE_H264_HIGH_10_INTRA, MPEG_VIDEO(H264_PROFILE_HIGH_10_INTRA) },
     105             :         { FF_PROFILE_H264_HIGH_422, MPEG_VIDEO(H264_PROFILE_HIGH_422) },
     106             :         { FF_PROFILE_H264_BASELINE, MPEG_VIDEO(H264_PROFILE_BASELINE) },
     107             :         { FF_PROFILE_H264_EXTENDED, MPEG_VIDEO(H264_PROFILE_EXTENDED) },
     108             :         { FF_PROFILE_H264_HIGH_10, MPEG_VIDEO(H264_PROFILE_HIGH_10) },
     109             :         { FF_PROFILE_H264_MAIN, MPEG_VIDEO(H264_PROFILE_MAIN) },
     110             :         { FF_PROFILE_H264_HIGH, MPEG_VIDEO(H264_PROFILE_HIGH) },
     111             :     };
     112             :     int i;
     113             : 
     114           0 :     for (i = 0; i < FF_ARRAY_ELEMS(profile); i++) {
     115           0 :         if (profile[i].ffmpeg_val == p)
     116           0 :             return profile[i].v4l2_val;
     117             :     }
     118           0 :     return AVERROR(ENOENT);
     119             : }
     120             : 
     121           0 : static inline int v4l2_mpeg4_profile_from_ff(int p)
     122             : {
     123             :     static const struct mpeg4_profile {
     124             :         unsigned int ffmpeg_val;
     125             :         unsigned int v4l2_val;
     126             :     } profile[] = {
     127             :         { FF_PROFILE_MPEG4_ADVANCED_CODING, MPEG_VIDEO(MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY) },
     128             :         { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, MPEG_VIDEO(MPEG4_PROFILE_ADVANCED_SIMPLE) },
     129             :         { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, MPEG_VIDEO(MPEG4_PROFILE_SIMPLE_SCALABLE) },
     130             :         { FF_PROFILE_MPEG4_SIMPLE, MPEG_VIDEO(MPEG4_PROFILE_SIMPLE) },
     131             :         { FF_PROFILE_MPEG4_CORE, MPEG_VIDEO(MPEG4_PROFILE_CORE) },
     132             :     };
     133             :     int i;
     134             : 
     135           0 :     for (i = 0; i < FF_ARRAY_ELEMS(profile); i++) {
     136           0 :         if (profile[i].ffmpeg_val == p)
     137           0 :             return profile[i].v4l2_val;
     138             :     }
     139           0 :     return AVERROR(ENOENT);
     140             : }
     141             : 
     142           0 : static int v4l2_check_b_frame_support(V4L2m2mContext *s)
     143             : {
     144           0 :     if (s->avctx->max_b_frames)
     145           0 :         av_log(s->avctx, AV_LOG_WARNING, "Encoder does not support b-frames yet\n");
     146             : 
     147           0 :     v4l2_set_ext_ctrl(s, MPEG_CID(B_FRAMES), 0, "number of B-frames");
     148           0 :     v4l2_get_ext_ctrl(s, MPEG_CID(B_FRAMES), &s->avctx->max_b_frames, "number of B-frames");
     149           0 :     if (s->avctx->max_b_frames == 0)
     150           0 :         return 0;
     151             : 
     152           0 :     avpriv_report_missing_feature(s->avctx, "DTS/PTS calculation for V4L2 encoding");
     153             : 
     154           0 :     return AVERROR_PATCHWELCOME;
     155             : }
     156             : 
     157           0 : static int v4l2_prepare_encoder(V4L2m2mContext *s)
     158             : {
     159           0 :     AVCodecContext *avctx = s->avctx;
     160             :     int qmin_cid, qmax_cid, qmin, qmax;
     161             :     int ret, val;
     162             : 
     163             :     /**
     164             :      * requirements
     165             :      */
     166           0 :     ret = v4l2_check_b_frame_support(s);
     167           0 :     if (ret)
     168           0 :         return ret;
     169             : 
     170             :     /**
     171             :      * settingss
     172             :      */
     173           0 :     if (avctx->framerate.num || avctx->framerate.den)
     174           0 :         v4l2_set_timeperframe(s, avctx->framerate.num, avctx->framerate.den);
     175             : 
     176             :     /* set ext ctrls */
     177           0 :     v4l2_set_ext_ctrl(s, MPEG_CID(HEADER_MODE), MPEG_VIDEO(HEADER_MODE_SEPARATE), "header mode");
     178           0 :     v4l2_set_ext_ctrl(s, MPEG_CID(BITRATE) , avctx->bit_rate, "bit rate");
     179           0 :     v4l2_set_ext_ctrl(s, MPEG_CID(GOP_SIZE), avctx->gop_size,"gop size");
     180             : 
     181           0 :     av_log(avctx, AV_LOG_DEBUG,
     182             :         "Encoder Context: id (%d), profile (%d), frame rate(%d/%d), number b-frames (%d), "
     183             :         "gop size (%d), bit rate (%"PRId64"), qmin (%d), qmax (%d)\n",
     184           0 :         avctx->codec_id, avctx->profile, avctx->framerate.num, avctx->framerate.den,
     185             :         avctx->max_b_frames, avctx->gop_size, avctx->bit_rate, avctx->qmin, avctx->qmax);
     186             : 
     187           0 :     switch (avctx->codec_id) {
     188           0 :     case AV_CODEC_ID_H264:
     189           0 :         val = v4l2_h264_profile_from_ff(avctx->profile);
     190           0 :         if (val < 0)
     191           0 :             av_log(avctx, AV_LOG_WARNING, "h264 profile not found\n");
     192             :         else
     193           0 :             v4l2_set_ext_ctrl(s, MPEG_CID(H264_PROFILE), val, "h264 profile");
     194           0 :         qmin_cid = MPEG_CID(H264_MIN_QP);
     195           0 :         qmax_cid = MPEG_CID(H264_MAX_QP);
     196           0 :         qmin = 0;
     197           0 :         qmax = 51;
     198           0 :         break;
     199           0 :     case AV_CODEC_ID_MPEG4:
     200           0 :         val = v4l2_mpeg4_profile_from_ff(avctx->profile);
     201           0 :         if (val < 0)
     202           0 :             av_log(avctx, AV_LOG_WARNING, "mpeg4 profile not found\n");
     203             :         else
     204           0 :             v4l2_set_ext_ctrl(s, MPEG_CID(MPEG4_PROFILE), val, "mpeg4 profile");
     205           0 :         qmin_cid = MPEG_CID(MPEG4_MIN_QP);
     206           0 :         qmax_cid = MPEG_CID(MPEG4_MAX_QP);
     207           0 :         if (avctx->flags & AV_CODEC_FLAG_QPEL)
     208           0 :             v4l2_set_ext_ctrl(s, MPEG_CID(MPEG4_QPEL), 1, "qpel");
     209           0 :         qmin = 1;
     210           0 :         qmax = 31;
     211           0 :         break;
     212           0 :     case AV_CODEC_ID_H263:
     213           0 :         qmin_cid = MPEG_CID(H263_MIN_QP);
     214           0 :         qmax_cid = MPEG_CID(H263_MAX_QP);
     215           0 :         qmin = 1;
     216           0 :         qmax = 31;
     217           0 :         break;
     218           0 :     case AV_CODEC_ID_VP8:
     219           0 :         qmin_cid = MPEG_CID(VPX_MIN_QP);
     220           0 :         qmax_cid = MPEG_CID(VPX_MAX_QP);
     221           0 :         qmin = 0;
     222           0 :         qmax = 127;
     223           0 :         break;
     224           0 :     case AV_CODEC_ID_VP9:
     225           0 :         qmin_cid = MPEG_CID(VPX_MIN_QP);
     226           0 :         qmax_cid = MPEG_CID(VPX_MAX_QP);
     227           0 :         qmin = 0;
     228           0 :         qmax = 255;
     229           0 :         break;
     230           0 :     default:
     231           0 :         return 0;
     232             :     }
     233             : 
     234           0 :     if (qmin != avctx->qmin || qmax != avctx->qmax)
     235           0 :         av_log(avctx, AV_LOG_WARNING, "Encoder adjusted: qmin (%d), qmax (%d)\n", qmin, qmax);
     236             : 
     237           0 :     v4l2_set_ext_ctrl(s, qmin_cid, qmin, "minimum video quantizer scale");
     238           0 :     v4l2_set_ext_ctrl(s, qmax_cid, qmax, "maximum video quantizer scale");
     239             : 
     240           0 :     return 0;
     241             : }
     242             : 
     243           0 : static int v4l2_send_frame(AVCodecContext *avctx, const AVFrame *frame)
     244             : {
     245           0 :     V4L2m2mContext *s = avctx->priv_data;
     246           0 :     V4L2Context *const output = &s->output;
     247             : 
     248           0 :     return ff_v4l2_context_enqueue_frame(output, frame);
     249             : }
     250             : 
     251           0 : static int v4l2_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
     252             : {
     253           0 :     V4L2m2mContext *s = avctx->priv_data;
     254           0 :     V4L2Context *const capture = &s->capture;
     255           0 :     V4L2Context *const output = &s->output;
     256             :     int ret;
     257             : 
     258           0 :     if (s->draining)
     259           0 :         goto dequeue;
     260             : 
     261           0 :     if (!output->streamon) {
     262           0 :         ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON);
     263           0 :         if (ret) {
     264           0 :             av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF failed on output context\n");
     265           0 :             return ret;
     266             :         }
     267             :     }
     268             : 
     269           0 :     if (!capture->streamon) {
     270           0 :         ret = ff_v4l2_context_set_status(capture, VIDIOC_STREAMON);
     271           0 :         if (ret) {
     272           0 :             av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMON failed on capture context\n");
     273           0 :             return ret;
     274             :         }
     275             :     }
     276             : 
     277           0 : dequeue:
     278           0 :     return ff_v4l2_context_dequeue_packet(capture, avpkt);
     279             : }
     280             : 
     281           0 : static av_cold int v4l2_encode_init(AVCodecContext *avctx)
     282             : {
     283           0 :     V4L2m2mContext *s = avctx->priv_data;
     284           0 :     V4L2Context *capture = &s->capture;
     285           0 :     V4L2Context *output = &s->output;
     286             :     int ret;
     287             : 
     288             :     /* common settings output/capture */
     289           0 :     output->height = capture->height = avctx->height;
     290           0 :     output->width = capture->width = avctx->width;
     291             : 
     292             :     /* output context */
     293           0 :     output->av_codec_id = AV_CODEC_ID_RAWVIDEO;
     294           0 :     output->av_pix_fmt = avctx->pix_fmt;
     295             : 
     296             :     /* capture context */
     297           0 :     capture->av_codec_id = avctx->codec_id;
     298           0 :     capture->av_pix_fmt = AV_PIX_FMT_NONE;
     299             : 
     300           0 :     ret = ff_v4l2_m2m_codec_init(avctx);
     301           0 :     if (ret) {
     302           0 :         av_log(avctx, AV_LOG_ERROR, "can't configure encoder\n");
     303           0 :         return ret;
     304             :     }
     305             : 
     306           0 :     return v4l2_prepare_encoder(s);
     307             : }
     308             : 
     309             : #define OFFSET(x) offsetof(V4L2m2mContext, x)
     310             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
     311             : 
     312             : static const AVOption options[] = {
     313             :     V4L_M2M_DEFAULT_OPTS,
     314             :     { "num_capture_buffers", "Number of buffers in the capture context",
     315             :         OFFSET(capture.num_buffers), AV_OPT_TYPE_INT, {.i64 = 4 }, 4, INT_MAX, FLAGS },
     316             :     { NULL },
     317             : };
     318             : 
     319             : #define M2MENC(NAME, LONGNAME, CODEC) \
     320             : static const AVClass v4l2_m2m_ ## NAME ## _enc_class = {\
     321             :     .class_name = #NAME "_v4l2_m2m_encoder",\
     322             :     .item_name  = av_default_item_name,\
     323             :     .option     = options,\
     324             :     .version    = LIBAVUTIL_VERSION_INT,\
     325             : };\
     326             : \
     327             : AVCodec ff_ ## NAME ## _v4l2m2m_encoder = { \
     328             :     .name           = #NAME "_v4l2m2m" ,\
     329             :     .long_name      = NULL_IF_CONFIG_SMALL("V4L2 mem2mem " LONGNAME " encoder wrapper"),\
     330             :     .type           = AVMEDIA_TYPE_VIDEO,\
     331             :     .id             = CODEC ,\
     332             :     .priv_data_size = sizeof(V4L2m2mContext),\
     333             :     .priv_class     = &v4l2_m2m_ ## NAME ##_enc_class,\
     334             :     .init           = v4l2_encode_init,\
     335             :     .send_frame     = v4l2_send_frame,\
     336             :     .receive_packet = v4l2_receive_packet,\
     337             :     .close          = ff_v4l2_m2m_codec_end,\
     338             : };
     339             : 
     340             : M2MENC(mpeg4,"MPEG4", AV_CODEC_ID_MPEG4);
     341             : M2MENC(h263, "H.263", AV_CODEC_ID_H263);
     342             : M2MENC(h264, "H.264", AV_CODEC_ID_H264);
     343             : M2MENC(hevc, "HEVC",  AV_CODEC_ID_HEVC);
     344             : M2MENC(vp8,  "VP8",   AV_CODEC_ID_VP8);

Generated by: LCOV version 1.13