- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# samples/common/classes/NvVideoEncoder.cpp

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 #include "NvVideoEncoder.h"
    00030 #include "NvLogging.h"
    00031
    00032 #include <cstring>
    00033 #include <errno.h>
    00034
    00035 #define ENCODER_DEV "/dev/nvhost-msenc"
    00036 #define ENCODER_COMP_NAME "NVENC"
    00037
    00038 #define CHECK_V4L2_RETURN(ret, str)              \
    00039     if (ret < 0) {                               \
    00040         COMP_SYS_ERROR_MSG(str << ": failed");   \
    00041         return -1;                               \
    00042     } else {                                     \
    00043         COMP_DEBUG_MSG(str << ": success");      \
    00044         return 0;                                \
    00045     }
    00046
    00047 #define RETURN_ERROR_IF_FORMATS_SET() \
    00048     if (output_plane_pixfmt != 0 || capture_plane_pixfmt != 0) { \
    00049         COMP_ERROR_MSG("Should be called before setting plane formats") \
    00050         return -1; \
    00051     }
    00052
    00053 #define RETURN_ERROR_IF_BUFFERS_REQUESTED() \
    00054     if (output_plane.getNumBuffers() != 0 && capture_plane.getNumBuffers() != 0) { \
    00055         COMP_ERROR_MSG("Should be called before requesting buffers on either plane") \
    00056         return -1; \
    00057     }
    00058
    00059 #define RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED() \
    00060     if (output_plane.getNumBuffers() == 0 || capture_plane.getNumBuffers() == 0) { \
    00061         COMP_ERROR_MSG("Should be called before requesting buffers on either plane") \
    00062         return -1; \
    00063     }
    00064
    00065 #define RETURN_ERROR_IF_FORMATS_NOT_SET() \
    00066     if (output_plane_pixfmt == 0 || capture_plane_pixfmt == 0) { \
    00067         COMP_ERROR_MSG("Should be called after setting plane formats") \
    00068         return -1; \
    00069     }
    00070
    00071 using namespace std;
    00072
    00073 NvVideoEncoder::NvVideoEncoder(const char *name, int flags)
    00074     :NvV4l2Element(name, ENCODER_DEV, flags, valid_fields)
    00075 {
    00076 }
    00077
    00078 NvVideoEncoder *
    00079 NvVideoEncoder::createVideoEncoder(const char *name, int flags)
    00080 {
    00081     NvVideoEncoder *enc = new NvVideoEncoder(name, flags);
    00082     if (enc->isInError())
    00083     {
    00084         delete enc;
    00085         return NULL;
    00086     }
    00087     return enc;
    00088 }
    00089
    00090 NvVideoEncoder::~NvVideoEncoder()
    00091 {
    00092 }
    00093
    00094 int
    00095 NvVideoEncoder::setOutputPlaneFormat(uint32_t pixfmt, uint32_t width,
    00096         uint32_t height)
    00097 {
    00098     struct v4l2_format format;
    00099     uint32_t num_bufferplanes;
    00100     NvBuffer::NvBufferPlaneFormat planefmts[MAX_PLANES];
    00101
    00102     if (pixfmt != V4L2_PIX_FMT_YUV420M)
    00103     {
    00104         COMP_ERROR_MSG("Only V4L2_PIX_FMT_YUV420M is supported");
    00105         return -1;
    00106     }
    00107
    00108     output_plane_pixfmt = pixfmt;
    00109     NvBuffer::fill_buffer_plane_format(&num_bufferplanes, planefmts, width,
    00110             height, pixfmt);
    00111     output_plane.setBufferPlaneFormat(num_bufferplanes, planefmts);
    00112
    00113     memset(&format, 0, sizeof(struct v4l2_format));
    00114     format.type = output_plane.getBufType();
    00115     format.fmt.pix_mp.width = width;
    00116     format.fmt.pix_mp.height = height;
    00117     format.fmt.pix_mp.pixelformat = pixfmt;
    00118     format.fmt.pix_mp.num_planes = num_bufferplanes;
    00119
    00120     return output_plane.setFormat(format);
    00121 }
    00122
    00123 int
    00124 NvVideoEncoder::setCapturePlaneFormat(uint32_t pixfmt, uint32_t width,
    00125         uint32_t height, uint32_t sizeimage)
    00126 {
    00127     struct v4l2_format format;
    00128
    00129     memset(&format, 0, sizeof(struct v4l2_format));
    00130     format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
    00131     switch (pixfmt)
    00132     {
    00133         case V4L2_PIX_FMT_H264:
    00134         case V4L2_PIX_FMT_H265:
    00135             capture_plane_pixfmt = pixfmt;
    00136             break;
    00137         default:
    00138             ERROR_MSG("Unknown supported pixel format for encoder " << pixfmt);
    00139             return -1;
    00140     }
    00141
    00142     format.fmt.pix_mp.pixelformat = pixfmt;
    00143     format.fmt.pix_mp.width = width;
    00144     format.fmt.pix_mp.height = height;
    00145     format.fmt.pix_mp.num_planes = 1;
    00146     format.fmt.pix_mp.plane_fmt[0].sizeimage = sizeimage;
    00147
    00148     return capture_plane.setFormat(format);
    00149 }
    00150
    00151 int
    00152 NvVideoEncoder::setFrameRate(uint32_t framerate_num, uint32_t framerate_den)
    00153 {
    00154     struct v4l2_streamparm parms;
    00155
    00156     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00157
    00158     memset(&parms, 0, sizeof(parms));
    00159     parms.parm.output.timeperframe.numerator = framerate_den;
    00160     parms.parm.output.timeperframe.denominator = framerate_num;
    00161
    00162     CHECK_V4L2_RETURN(output_plane.setStreamParms(parms),
    00163             "Setting framerate to " << framerate_num << "/" << framerate_den);
    00164 }
    00165
    00166 int
    00167 NvVideoEncoder::setBitrate(uint32_t bitrate)
    00168 {
    00169     struct v4l2_ext_control control;
    00170     struct v4l2_ext_controls ctrls;
    00171
    00172     cout << capture_plane_pixfmt <<endl;
    00173     cout << output_plane_pixfmt <<endl;
    00174     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00175
    00176     memset(&control, 0, sizeof(control));
    00177     memset(&ctrls, 0, sizeof(ctrls));
    00178
    00179     ctrls.count = 1;
    00180     ctrls.controls = &control;
    00181     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00182
    00183     control.id = V4L2_CID_MPEG_VIDEO_BITRATE;
    00184     control.value = bitrate;
    00185
    00186     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00187             "Setting encoder bitrate to " << bitrate);
    00188 }
    00189
    00190 int
    00191 NvVideoEncoder::setProfile(uint32_t profile)
    00192 {
    00193     struct v4l2_ext_control control;
    00194     struct v4l2_ext_controls ctrls;
    00195
    00196     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00197     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00198
    00199     memset(&control, 0, sizeof(control));
    00200     memset(&ctrls, 0, sizeof(ctrls));
    00201
    00202     ctrls.count = 1;
    00203     ctrls.controls = &control;
    00204     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00205
    00206     switch (capture_plane_pixfmt)
    00207     {
    00208         case V4L2_PIX_FMT_H264:
    00209             control.id = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
    00210             break;
    00211         case V4L2_PIX_FMT_H265:
    00212             control.id = V4L2_CID_MPEG_VIDEO_H265_PROFILE;
    00213             break;
    00214         default:
    00215             COMP_ERROR_MSG("Unsupported encoder type");
    00216             return -1;
    00217     }
    00218     control.value = profile;
    00219
    00220     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00221             "Setting encoder profile to " << profile);
    00222 }
    00223
    00224 int
    00225 NvVideoEncoder::setLevel(enum v4l2_mpeg_video_h264_level level)
    00226 {
    00227     struct v4l2_ext_control control;
    00228     struct v4l2_ext_controls ctrls;
    00229
    00230     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00231     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00232
    00233     if (capture_plane_pixfmt != V4L2_PIX_FMT_H264)
    00234     {
    00235         COMP_WARN_MSG("Currently only supported for H.264");
    00236         return 0;
    00237     }
    00238
    00239     memset(&control, 0, sizeof(control));
    00240     memset(&ctrls, 0, sizeof(ctrls));
    00241
    00242     ctrls.count = 1;
    00243     ctrls.controls = &control;
    00244     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00245
    00246     control.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
    00247     control.value = level;
    00248
    00249     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00250             "Setting encoder level to " << level);
    00251 }
    00252
    00253 int
    00254 NvVideoEncoder::setRateControlMode(enum v4l2_mpeg_video_bitrate_mode mode)
    00255 {
    00256     struct v4l2_ext_control control;
    00257     struct v4l2_ext_controls ctrls;
    00258
    00259     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00260     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00261
    00262     memset(&control, 0, sizeof(control));
    00263     memset(&ctrls, 0, sizeof(ctrls));
    00264
    00265     ctrls.count = 1;
    00266     ctrls.controls = &control;
    00267     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00268
    00269     control.id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE;
    00270     control.value = mode;
    00271
    00272     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00273             "Setting encoder rate control mode to " << mode);
    00274 }
    00275
    00276 int
    00277 NvVideoEncoder::setIFrameInterval(uint32_t interval)
    00278 {
    00279     struct v4l2_ext_control control;
    00280     struct v4l2_ext_controls ctrls;
    00281
    00282     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00283     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00284
    00285     memset(&control, 0, sizeof(control));
    00286     memset(&ctrls, 0, sizeof(ctrls));
    00287
    00288     ctrls.count = 1;
    00289     ctrls.controls = &control;
    00290     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00291
    00292     control.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
    00293     control.value = interval;
    00294
    00295     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00296             "Setting encoder I-frame interval to " << interval);
    00297 }
    00298
    00299 int
    00300 NvVideoEncoder::setIDRInterval(uint32_t interval)
    00301 {
    00302     struct v4l2_ext_control control;
    00303     struct v4l2_ext_controls ctrls;
    00304
    00305     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00306     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00307
    00308     memset(&control, 0, sizeof(control));
    00309     memset(&ctrls, 0, sizeof(ctrls));
    00310
    00311     ctrls.count = 1;
    00312     ctrls.controls = &control;
    00313     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00314
    00315     control.id = V4L2_CID_MPEG_VIDEO_IDR_INTERVAL;
    00316     control.value = interval;
    00317
    00318     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00319             "Setting encoder IDR interval to " << interval);
    00320 }
    00321
    00322 int
    00323 NvVideoEncoder::forceIDR()
    00324 {
    00325     struct v4l2_ext_control control;
    00326     struct v4l2_ext_controls ctrls;
    00327
    00328     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00329
    00330     memset(&control, 0, sizeof(control));
    00331     memset(&ctrls, 0, sizeof(ctrls));
    00332
    00333     ctrls.count = 1;
    00334     ctrls.controls = &control;
    00335     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00336
    00337     control.id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE;
    00338
    00339     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00340             "Forcing IDR");
    00341 }
    00342
    00343 int
    00344 NvVideoEncoder::setTemporalTradeoff(v4l2_enc_temporal_tradeoff_level_type level)
    00345 {
    00346     struct v4l2_ext_control control;
    00347     struct v4l2_ext_controls ctrls;
    00348
    00349     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00350     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00351
    00352     memset(&control, 0, sizeof(control));
    00353     memset(&ctrls, 0, sizeof(ctrls));
    00354
    00355     ctrls.count = 1;
    00356     ctrls.controls = &control;
    00357     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00358
    00359     control.id = V4L2_CID_MPEG_VIDEOENC_TEMPORAL_TRADEOFF_LEVEL;
    00360     control.value = level;
    00361
    00362     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00363             "Setting encoder temporal tradeoff level to " << level);
    00364 }
    00365
    00366 int
    00367 NvVideoEncoder::setSliceLength(v4l2_enc_slice_length_type type, uint32_t length)
    00368 {
    00369     struct v4l2_ext_control control;
    00370     struct v4l2_ext_controls ctrls;
    00371     v4l2_enc_slice_length_param param = {type, length};
    00372
    00373     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00374     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00375
    00376     memset(&control, 0, sizeof(control));
    00377     memset(&ctrls, 0, sizeof(ctrls));
    00378
    00379     ctrls.count = 1;
    00380     ctrls.controls = &control;
    00381     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00382
    00383     control.id = V4L2_CID_MPEG_VIDEOENC_SLICE_LENGTH_PARAM;
    00384     control.string = (char *) &param;
    00385
    00386     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00387             "Setting encoder packet type to " << type << ", length to " <<
    00388             length);
    00389 }
    00390
    00391 int
    00392 NvVideoEncoder::setROIParams(uint32_t buffer_index,
    00393         v4l2_enc_frame_ROI_params &params)
    00394 {
    00395     struct v4l2_ext_control control;
    00396     struct v4l2_ext_controls ctrls;
    00397
    00398     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00399     RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED();
    00400
    00401     memset(&control, 0, sizeof(control));
    00402     memset(&ctrls, 0, sizeof(ctrls));
    00403
    00404     ctrls.count = 1;
    00405     ctrls.controls = &control;
    00406     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00407
    00408     control.id = V4L2_CID_MPEG_VIDEOENC_ROI_PARAMS;
    00409     control.string = (char *) &params;
    00410     params.config_store = buffer_index;
    00411
    00412     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00413             "Setting encoder ROI params for buffer " << buffer_index);
    00414 }
    00415
    00416 int
    00417 NvVideoEncoder::setVirtualBufferSize(uint32_t size)
    00418 {
    00419     struct v4l2_ext_control control;
    00420     struct v4l2_ext_controls ctrls;
    00421     v4l2_enc_virtual_buffer_size buffer_size = {size};
    00422
    00423     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00424     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00425
    00426     memset(&control, 0, sizeof(control));
    00427     memset(&ctrls, 0, sizeof(ctrls));
    00428
    00429     ctrls.count = 1;
    00430     ctrls.controls = &control;
    00431     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00432
    00433     control.id = V4L2_CID_MPEG_VIDEOENC_VIRTUALBUFFER_SIZE;
    00434     control.string = (char *) &buffer_size;
    00435
    00436     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00437             "Setting encoder virtual buffer size to " << size);
    00438 }
    00439 int
    00440 NvVideoEncoder::setNumReferenceFrames(uint32_t num_frames)
    00441 {
    00442     struct v4l2_ext_control control;
    00443     struct v4l2_ext_controls ctrls;
    00444     v4l2_enc_num_ref_frames frames = { num_frames };
    00445
    00446     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00447     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00448
    00449     memset(&control, 0, sizeof(control));
    00450     memset(&ctrls, 0, sizeof(ctrls));
    00451
    00452     ctrls.count = 1;
    00453     ctrls.controls = &control;
    00454     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00455
    00456     control.id = V4L2_CID_MPEG_VIDEOENC_NUM_REFERENCE_FRAMES;
    00457     control.string = (char *) &frames;
    00458
    00459     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00460             "Setting encoder number of reference frames to " << num_frames);
    00461 }
    00462
    00463 int
    00464 NvVideoEncoder::setSliceIntrarefresh(uint32_t interval)
    00465 {
    00466     struct v4l2_ext_control control;
    00467     struct v4l2_ext_controls ctrls;
    00468     v4l2_enc_slice_intrarefresh_param param = {interval};
    00469
    00470     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00471     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00472
    00473     memset(&control, 0, sizeof(control));
    00474     memset(&ctrls, 0, sizeof(ctrls));
    00475
    00476     ctrls.count = 1;
    00477     ctrls.controls = &control;
    00478     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00479
    00480     control.id = V4L2_CID_MPEG_VIDEOENC_SLICE_INTRAREFRESH_PARAM;
    00481     control.string = (char *) &param;
    00482
    00483     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00484             "Setting encoder slice intrarefresh interval to " << interval);
    00485 }
    00486
    00487 int
    00488 NvVideoEncoder::setNumBFrames(uint32_t num)
    00489 {
    00490     struct v4l2_ext_control control;
    00491     struct v4l2_ext_controls ctrls;
    00492
    00493     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00494     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00495
    00496     memset(&control, 0, sizeof(control));
    00497     memset(&ctrls, 0, sizeof(ctrls));
    00498
    00499     ctrls.count = 1;
    00500     ctrls.controls = &control;
    00501     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00502
    00503     control.id = V4L2_CID_MPEG_VIDEOENC_NUM_BFRAMES;
    00504     control.value = num;
    00505
    00506     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00507             "Setting encoder number of B frames to " << num);
    00508 }
    00509
    00510 int
    00511 NvVideoEncoder::setInsertSpsPpsAtIdrEnabled(bool enabled)
    00512 {
    00513     struct v4l2_ext_control control;
    00514     struct v4l2_ext_controls ctrls;
    00515
    00516     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00517     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00518
    00519     memset(&control, 0, sizeof(control));
    00520     memset(&ctrls, 0, sizeof(ctrls));
    00521
    00522     ctrls.count = 1;
    00523     ctrls.controls = &control;
    00524     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00525
    00526     control.id = V4L2_CID_MPEG_VIDEOENC_INSERT_SPS_PPS_AT_IDR;
    00527     control.value = enabled;
    00528
    00529     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00530             "Setting encoder SPSPPSatIDR to " << enabled);
    00531 }
    00532
    00533 int
    00534 NvVideoEncoder::enableMotionVectorReporting()
    00535 {
    00536     struct v4l2_ext_control control;
    00537     struct v4l2_ext_controls ctrls;
    00538
    00539     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00540     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00541
    00542     memset(&control, 0, sizeof(control));
    00543     memset(&ctrls, 0, sizeof(ctrls));
    00544
    00545     ctrls.count = 1;
    00546     ctrls.controls = &control;
    00547     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00548
    00549     control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_METADATA_MV;
    00550     control.value = 1;
    00551
    00552     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00553             "Enabling encoder motion vector reporting");
    00554 }
    00555
    00556 int
    00557 NvVideoEncoder::getMetadata(uint32_t buffer_index,
    00558         v4l2_ctrl_videoenc_outputbuf_metadata &enc_metadata)
    00559 {
    00560     v4l2_ctrl_video_metadata metadata;
    00561     struct v4l2_ext_control control;
    00562     struct v4l2_ext_controls ctrls;
    00563
    00564     RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED();
    00565
    00566     ctrls.count = 1;
    00567     ctrls.controls = &control;
    00568     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00569
    00570     metadata.buffer_index = buffer_index;
    00571     metadata.VideoEncMetadata = &enc_metadata;
    00572
    00573     control.id = V4L2_CID_MPEG_VIDEOENC_METADATA;
    00574     control.string = (char *)&metadata;
    00575
    00576     CHECK_V4L2_RETURN(getExtControls(ctrls),
    00577             "Getting encoder output metadata for buffer " << buffer_index);
    00578 }
    00579
    00580 int
    00581 NvVideoEncoder::getMotionVectors(uint32_t buffer_index,
    00582         v4l2_ctrl_videoenc_outputbuf_metadata_MV &enc_mv_metadata)
    00583 {
    00584     v4l2_ctrl_video_metadata metadata;
    00585     struct v4l2_ext_control control;
    00586     struct v4l2_ext_controls ctrls;
    00587
    00588     RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED();
    00589
    00590     ctrls.count = 1;
    00591     ctrls.controls = &control;
    00592     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00593
    00594     metadata.buffer_index = buffer_index;
    00595     metadata.VideoEncMetadataMV = &enc_mv_metadata;
    00596
    00597     control.id = V4L2_CID_MPEG_VIDEOENC_METADATA_MV;
    00598     control.string = (char *)&metadata;
    00599
    00600     CHECK_V4L2_RETURN(getExtControls(ctrls),
    00601             "Getting encoder output MV metadata for buffer " << buffer_index);
    00602 }
    00603
    00604 int
    00605 NvVideoEncoder::setQpRange(uint32_t MinQpI, uint32_t MaxQpI, uint32_t MinQpP,
    00606             uint32_t MaxQpP, uint32_t MinQpB, uint32_t MaxQpB)
    00607 {
    00608     v4l2_ctrl_video_qp_range qprange;
    00609     struct v4l2_ext_control control;
    00610     struct v4l2_ext_controls ctrls;
    00611
    00612     RETURN_ERROR_IF_FORMATS_NOT_SET();
    00613     RETURN_ERROR_IF_BUFFERS_REQUESTED();
    00614
    00615     memset(&control, 0, sizeof(control));
    00616     memset(&ctrls, 0, sizeof(ctrls));
    00617
    00618     qprange.MinQpI = MinQpI;
    00619     qprange.MaxQpI = MaxQpI;
    00620     qprange.MinQpP = MinQpP;
    00621     qprange.MaxQpP = MaxQpP;
    00622     qprange.MinQpB = MinQpB;
    00623     qprange.MaxQpB = MaxQpB;
    00624
    00625     ctrls.count = 1;
    00626     ctrls.controls = &control;
    00627     ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    00628
    00629     control.id = V4L2_CID_MPEG_VIDEOENC_QP_RANGE;
    00630     control.string = (char *)&qprange;
    00631
    00632     CHECK_V4L2_RETURN(setExtControls(ctrls),
    00633             "Setting encoder Qp range " << ctrls.count);
    00634 }