- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# include/NvVideoEncoder.h

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
    00003  *
    00004  * Redistribution and use in source and binary forms, with or without
    00005  * modification, are permitted provided that the following conditions
    00006  * are met:
    00007  *  * Redistributions of source code must retain the above copyright
    00008  *    notice, this list of conditions and the following disclaimer.
    00009  *  * Redistributions in binary form must reproduce the above copyright
    00010  *    notice, this list of conditions and the following disclaimer in the
    00011  *    documentation and/or other materials provided with the distribution.
    00012  *  * Neither the name of NVIDIA CORPORATION nor the names of its
    00013  *    contributors may be used to endorse or promote products derived
    00014  *    from this software without specific prior written permission.
    00015  *
    00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
    00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    00019  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    00020  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    00021  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    00022  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    00023  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    00024  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00026  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00027  */
    00028
    00029 /**
    00030  * @file NvVideoEncoder.h
    00031  *
    00032  * @brief Helper class for V4L2 Video Encoder.
    00033  */
    00034
    00035 #ifndef __NV_VIDEO_ENCODER_H__
    00036 #define __NV_VIDEO_ENCODER_H__
    00037
    00038 #include "NvV4l2Element.h"
    00039
    00040 /**
    00041  * @brief Helper class for V4L2 Video Encoder.
    00042  *
    00043  * The video encoder device node is \b "/dev/nvhost-msenc". The category name
    00044  * for encoder is \b "NVENC".
    00045  *
    00046  * Refer to @ref V4L2Enc for more information on the converter.
    00047  */
    00048
    00049 class NvVideoEncoder:public NvV4l2Element
    00050 {
    00051 public:
    00052     /**
    00053      * Create a new V4L2 Video Encoder named \a name
    00054      *
    00055      * This function internally calls v4l2_open on the encoder dev node
    00056      * \b "/dev/nvhost-msenc" and checks for \b V4L2_CAP_VIDEO_M2M_MPLANE
    00057      * capability on the device. This method allows the caller to specify
    00058      * additional flags with which the device should be opened.
    00059      *
    00060      * The device is opened in blocking mode which can be modified by passing
    00061      * the @a O_NONBLOCK flag to this method.
    00062      *
    00063      * @returns Reference to the newly created encoder object else NULL in
    00064      *          case of failure during initialization.
    00065      */
    00066     static NvVideoEncoder *createVideoEncoder(const char *name, int flags = 0);
    00067
    00068      ~NvVideoEncoder();
    00069
    00070     /**
    00071      * Sets the format on the encoder output plane.
    00072      *
    00073      * Calls VIDIOC_S_FMT ioctl internally on the output plane.
    00074      *
    00075      * @pre Capture plane format (#setCapturePlaneFormat) must be set before calling this.
    00076      *
    00077      * @param[in] pixfmt One of the raw V4L2 pixel formats
    00078      * @param[in] width Width of the input buffers in pixels
    00079      * @param[in] height Height of the input buffers in pixels
    00080      * @returns 0 for success, -1 for failure
    00081      */
    00082     int setOutputPlaneFormat(uint32_t pixfmt, uint32_t width, uint32_t height);
    00083     /**
    00084      * Sets the format on the encoder capture plane.
    00085      *
    00086      * Calls VIDIOC_S_FMT ioctl internally on the capture plane.
    00087      *
    00088      * @param[in] pixfmt One of the coded V4L2 pixel formats
    00089      * @param[in] width Width of the input buffers in pixels
    00090      * @param[in] height Height of the input buffers in pixels
    00091      * @param[in] sizeimage Maximum size of the encoded buffers on the capture
    00092      *                      plane in bytes
    00093      * @returns 0 for success, -1 for failure
    00094      */
    00095     int setCapturePlaneFormat(uint32_t pixfmt, uint32_t width,
    00096                               uint32_t height, uint32_t sizeimage);
    00097
    00098     /**
    00099      * Set the encode framerate
    00100      *
    00101      * Calls the VIDIOC_S_PARM ioctl on the encoder capture plane. Can b
    00102      * called anytime after setFormat on both the planes.
    00103      *
    00104      * @param[in] framerate_num Numerator part of the framerate fraction
    00105      * @param[in] framerate_den Denominator part of the framerate fraction
    00106      *
    00107      * @returns 0 for success, -1 for failure
    00108      */
    00109     int setFrameRate(uint32_t framerate_num, uint32_t framerate_den);
    00110
    00111     /**
    00112      * Set the encoder bitrate
    00113      *
    00114      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00115      * V4L2_CID_MPEG_VIDEO_BITRATE. Can be called anytime after setFormat on
    00116      * both the planes.
    00117      *
    00118      * @param[in] bitrate Bitrate of the encoded stream, in bits per second
    00119      *
    00120      * @returns 0 for success, -1 for failure
    00121      */
    00122     int setBitrate(uint32_t bitrate);
    00123
    00124     /**
    00125      * Set the encoder profile.
    00126      *
    00127      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00128      * V4L2_CID_MPEG_VIDEO_H264_PROFILE or V4L2_CID_MPEG_VIDEO_H265_PROFILE
    00129      * depending on the encoder type. Must be called after setFormat on both
    00130      * the planes and before #requestBuffers on any of the planes.
    00131      *
    00132      * @param[in] profile Profile to be used for encoding
    00133      *
    00134      * @returns 0 for success, -1 for failure
    00135      */
    00136     int setProfile(uint32_t profile);
    00137
    00138     /**
    00139      * Set the encoder level.
    00140      *
    00141      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00142      * V4L2_CID_MPEG_VIDEO_H264_LEVEL. Must be called after setFormat on both
    00143      * the planes and before #requestBuffers on any of the planes.
    00144      *
    00145      * @param[in] level Level to be used for encoding, one of enum
    00146      *                  v4l2_mpeg_video_h264_level
    00147      *
    00148      * @returns 0 for success, -1 for failure
    00149      */
    00150     int setLevel(enum v4l2_mpeg_video_h264_level level);
    00151
    00152     /**
    00153      * Set the encoder rate control mode
    00154      *
    00155      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00156      * #V4L2_CID_MPEG_VIDEO_BITRATE_MODE. Must be called after setFormat on both
    00157      * the planes and before #requestBuffers on any of the planes.
    00158      *
    00159      * @param[in] mode Type of rate control, one of enum
    00160      *              v4l2_mpeg_video_bitrate_mode
    00161      *
    00162      * @returns 0 for success, -1 for failure
    00163      */
    00164     int setRateControlMode(enum v4l2_mpeg_video_bitrate_mode mode);
    00165
    00166     /**
    00167      * Set the encoder I-frame interval.
    00168      *
    00169      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00170      * V4L2_CID_MPEG_VIDEO_GOP_SIZE. Must be called after setFormat on both
    00171      * the planes and before #requestBuffers on any of the planes.
    00172      *
    00173      * @param[in] interval Interval between two I frames, in number of frames
    00174      *
    00175      * @returns 0 for success, -1 for failure
    00176      */
    00177     int setIFrameInterval(uint32_t interval);
    00178
    00179     /**
    00180      * Set the encoder IDR interval.
    00181      *
    00182      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00183      * #V4L2_CID_MPEG_VIDEO_IDR_INTERVAL. Must be called after setFormat on both
    00184      * the planes and before #requestBuffers on any of the planes.
    00185      *
    00186      * @param[in] interval Interval between two IDR frames, in number of frames
    00187      *
    00188      * @returns 0 for success, -1 for failure
    00189      */
    00190     int setIDRInterval(uint32_t interval);
    00191
    00192     /**
    00193      * Force an IDR frame
    00194      *
    00195      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00196      * V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE. Must be called after
    00197      * setFormat on both the planes.
    00198      *
    00199      * @param[in] interval Interval between two IDR frames, in number of frames
    00200      *
    00201      * @returns 0 for success, -1 for failure
    00202      */
    00203     int forceIDR();
    00204
    00205     /**
    00206      * Set the encoder Temporal Tradeoff.
    00207      *
    00208      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00209      * #V4L2_CID_MPEG_VIDEOENC_TEMPORAL_TRADEOFF_LEVEL. Must be called after
    00210      * setFormat on both the planes and before #requestBuffers on any of the
    00211      * planes.
    00212      *
    00213      * @param[in] level Temporal tradeoff level, one of
    00214      *                  v4l2_enc_temporal_tradeoff_level_type
    00215      * @returns 0 for success, -1 for failure
    00216      */
    00217     int setTemporalTradeoff(v4l2_enc_temporal_tradeoff_level_type level);
    00218
    00219     /**
    00220      * Set the encoder output slice length.
    00221      *
    00222      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00223      * #V4L2_CID_MPEG_VIDEOENC_SLICE_LENGTH_PARAM. Must be called after setFormat on
    00224      * both the planes and before #requestBuffers on any of the planes.
    00225      *
    00226      * @param[in] type Slice length type, one of enum v4l2_enc_slice_length_type
    00227      * @param[in] length Length of the slice in bytes if type is
    00228      *      V4L2_ENC_SLICE_LENGTH_TYPE_BITS, else in number of MBs if type is
    00229      *      V4L2_ENC_SLICE_LENGTH_TYPE_MBLK
    00230      * @returns 0 for success, -1 for failure
    00231      */
    00232     int setSliceLength(v4l2_enc_slice_length_type type, uint32_t length);
    00233
    00234     /**
    00235      * Set the Region of Interest parameters for the next buffer which will
    00236      * be queued on output plane with index \a buffer_index
    00237      *
    00238      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00239      * #V4L2_CID_MPEG_VIDEOENC_ROI_PARAMS. Must be called after
    00240      * requesting buffer on both the planes.
    00241      *
    00242      * @param[in] buffer_index Index of output plane buffer on which the ROI
    00243      *                         params should be applied.
    00244      * @param[in] params Parameters to be applied on the frame, structure of
    00245      *                   type #v4l2_enc_frame_ROI_params
    00246      * @returns 0 for success, -1 for failure
    00247      */
    00248     int setROIParams(uint32_t buffer_index, v4l2_enc_frame_ROI_params & params);
    00249
    00250     /**
    00251      * Set the virtual buffer size of the encoder.
    00252      *
    00253      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00254      * #V4L2_CID_MPEG_VIDEOENC_VIRTUALBUFFER_SIZE. Must be called after
    00255      * setFormat on both the planes.
    00256      *
    00257      * @param[in] size Virtual buffer size, in bytes.
    00258      * @returns 0 for success, -1 for failure
    00259      */
    00260     int setVirtualBufferSize(uint32_t size);
    00261
    00262     /**
    00263      * Set the number of reference frames of the encoder.
    00264      *
    00265      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00266      * #V4L2_CID_MPEG_VIDEOENC_NUM_REFERENCE_FRAMES. Must be called after
    00267      * setFormat on both the planes.
    00268      *
    00269      * @param[in] num_frames Number of reference frames.
    00270      * @returns 0 for success, -1 for failure
    00271      */
    00272     int setNumReferenceFrames(uint32_t num_frames);
    00273
    00274     /**
    00275      * Set slice intrareferesh interval params.
    00276      *
    00277      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00278      * #V4L2_CID_MPEG_VIDEOENC_SLICE_INTRAREFRESH_PARAM. Must be called after
    00279      * setFormat on both the planes.
    00280      *
    00281      * @param[in] interval Slice intrarefresh interval, in number of slices.
    00282      * @returns 0 for success, -1 for failure
    00283      */
    00284     int setSliceIntrarefresh(uint32_t interval);
    00285
    00286     /**
    00287      * Set the number of B frames two P frames.
    00288      *
    00289      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00290      * #V4L2_CID_MPEG_VIDEOENC_NUM_BFRAMES. Must be called after
    00291      * setFormat on both the planes.
    00292      *
    00293      * @param[in] num Number of B frames.
    00294      * @returns 0 for success, -1 for failure
    00295      */
    00296     int setNumBFrames(uint32_t num);
    00297
    00298     /**
    00299      * Enabled/disable insert SPS PPS at every IDR.
    00300      *
    00301      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00302      * #V4L2_CID_MPEG_VIDEOENC_INSERT_SPS_PPS_AT_IDR. Must be called after
    00303      * setFormat on both the planes.
    00304      *
    00305      * @param[in] enabled Boolean value indicating wether to enable/disable
    00306      *                    the control.
    00307      * @returns 0 for success, -1 for failure
    00308      */
    00309     int setInsertSpsPpsAtIdrEnabled(bool enabled);
    00310
    00311     /**
    00312      * Enable video encoder output motion vector metadata reporting
    00313      *
    00314      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00315      * #V4L2_CID_MPEG_VIDEOENC_METADATA_MV. Must be called after setFormat on
    00316      * both the planes and before #requestBuffers on any of the planes.
    00317      *
    00318      * @returns 0 for success, -1 for failure
    00319      */
    00320     int enableMotionVectorReporting();
    00321
    00322     /**
    00323      * Get metadata for encoded capture plane buffer
    00324      *
    00325      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00326      * #V4L2_CID_MPEG_VIDEOENC_METADATA. Must be called for a buffer which has
    00327      * been dequeued from the capture plane. The returned metadata corresponds
    00328      * to the last dequeued buffer with index @a buffer_index.
    00329      *
    00330      * @param[in] buffer_index Index of the capture plane buffer whose metadata
    00331      *              is required
    00332      * @param[in,out] Reference to the metadata structure
    00333      *              v4l2_ctrl_videoenc_outputbuf_metadata to be filled
    00334      *
    00335      * @returns 0 for success, -1 for failure
    00336      */
    00337     int getMetadata(uint32_t buffer_index,
    00338             v4l2_ctrl_videoenc_outputbuf_metadata &enc_metadata);
    00339
    00340     /**
    00341      * Get motion vector metadata for encoded capture plane buffer
    00342      *
    00343      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00344      * #V4L2_CID_MPEG_VIDEOENC_METADATA_MV. Must be called for a buffer which has
    00345      * been dequeued from the capture plane. The returned metadata corresponds
    00346      * to the last dequeued buffer with index @a buffer_index.
    00347      *
    00348      * @param[in] buffer_index Index of the capture plane buffer whose metadata
    00349      *              is required
    00350      * @param[in,out] Reference to the metadata structure
    00351      *              v4l2_ctrl_videoenc_outputbuf_metadata_MV to be filled
    00352      *
    00353      * @returns 0 for success, -1 for failure
    00354      */
    00355     int getMotionVectors(uint32_t buffer_index,
    00356             v4l2_ctrl_videoenc_outputbuf_metadata_MV &enc_mv_metadata);
    00357
    00358     /**
    00359      * Set QP values for I/P/B frames
    00360      *
    00361      * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control id
    00362      * #V4L2_CID_MPEG_VIDEOENC_QP_RANGE. Must be called after
    00363      * setFormat on both the planes.
    00364      *
    00365      * @param[in] MinQpI Minimum Qp Value for I frame
    00366      * @param[in] MaxQpI Minimum Qp Value for I frame
    00367      * @param[in] MinQpP Minimum Qp Value for P frame
    00368      * @param[in] MaxQpP Minimum Qp Value for P frame
    00369      * @param[in] MinQpB Minimum Qp Value for B frame
    00370      * @param[in] MaxQpB Minimum Qp Value for B frame
    00371      * @returns 0 for success, -1 for failure
    00372      */
    00373     int setQpRange(uint32_t MinQpI, uint32_t MaxQpI, uint32_t MinQpP,
    00374             uint32_t MaxQpP, uint32_t MinQpB, uint32_t MaxQpB);
    00375
    00376
    00377 private:
    00378     /**
    00379      * Contructor used by #createVideoEncoder
    00380      */
    00381     NvVideoEncoder(const char *name, int flags);
    00382
    00383     static const NvElementProfiler::ProfilerField valid_fields =
    00384             NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS |
    00385             NvElementProfiler::PROFILER_FIELD_LATENCIES |
    00386             NvElementProfiler::PROFILER_FIELD_FPS;
    00387 };
    00388
    00389 #endif