162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci.. _encoder: 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci************************************************* 662306a36Sopenharmony_ciMemory-to-Memory Stateful Video Encoder Interface 762306a36Sopenharmony_ci************************************************* 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciA stateful video encoder takes raw video frames in display order and encodes 1062306a36Sopenharmony_cithem into a bytestream. It generates complete chunks of the bytestream, including 1162306a36Sopenharmony_ciall metadata, headers, etc. The resulting bytestream does not require any 1262306a36Sopenharmony_cifurther post-processing by the client. 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ciPerforming software stream processing, header generation etc. in the driver 1562306a36Sopenharmony_ciin order to support this interface is strongly discouraged. In case such 1662306a36Sopenharmony_cioperations are needed, use of the Stateless Video Encoder Interface (in 1762306a36Sopenharmony_cidevelopment) is strongly advised. 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ciConventions and Notations Used in This Document 2062306a36Sopenharmony_ci=============================================== 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci1. The general V4L2 API rules apply if not specified in this document 2362306a36Sopenharmony_ci otherwise. 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci2. The meaning of words "must", "may", "should", etc. is as per `RFC 2662306a36Sopenharmony_ci 2119 <https://tools.ietf.org/html/rfc2119>`_. 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci3. All steps not marked "optional" are required. 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci4. :c:func:`VIDIOC_G_EXT_CTRLS` and :c:func:`VIDIOC_S_EXT_CTRLS` may be used 3162306a36Sopenharmony_ci interchangeably with :c:func:`VIDIOC_G_CTRL` and :c:func:`VIDIOC_S_CTRL`, 3262306a36Sopenharmony_ci unless specified otherwise. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci5. Single-planar API (see :ref:`planar-apis`) and applicable structures may be 3562306a36Sopenharmony_ci used interchangeably with multi-planar API, unless specified otherwise, 3662306a36Sopenharmony_ci depending on encoder capabilities and following the general V4L2 guidelines. 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci6. i = [a..b]: sequence of integers from a to b, inclusive, i.e. i = 3962306a36Sopenharmony_ci [0..2]: i = 0, 1, 2. 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci7. Given an ``OUTPUT`` buffer A, then A' represents a buffer on the ``CAPTURE`` 4262306a36Sopenharmony_ci queue containing data that resulted from processing buffer A. 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ciGlossary 4562306a36Sopenharmony_ci======== 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciRefer to :ref:`decoder-glossary`. 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ciState Machine 5062306a36Sopenharmony_ci============= 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci.. kernel-render:: DOT 5362306a36Sopenharmony_ci :alt: DOT digraph of encoder state machine 5462306a36Sopenharmony_ci :caption: Encoder State Machine 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci digraph encoder_state_machine { 5762306a36Sopenharmony_ci node [shape = doublecircle, label="Encoding"] Encoding; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci node [shape = circle, label="Initialization"] Initialization; 6062306a36Sopenharmony_ci node [shape = circle, label="Stopped"] Stopped; 6162306a36Sopenharmony_ci node [shape = circle, label="Drain"] Drain; 6262306a36Sopenharmony_ci node [shape = circle, label="Reset"] Reset; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci node [shape = point]; qi 6562306a36Sopenharmony_ci qi -> Initialization [ label = "open()" ]; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci Initialization -> Encoding [ label = "Both queues streaming" ]; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci Encoding -> Drain [ label = "V4L2_ENC_CMD_STOP" ]; 7062306a36Sopenharmony_ci Encoding -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ]; 7162306a36Sopenharmony_ci Encoding -> Stopped [ label = "VIDIOC_STREAMOFF(OUTPUT)" ]; 7262306a36Sopenharmony_ci Encoding -> Encoding; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci Drain -> Stopped [ label = "All CAPTURE\nbuffers dequeued\nor\nVIDIOC_STREAMOFF(OUTPUT)" ]; 7562306a36Sopenharmony_ci Drain -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ]; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci Reset -> Encoding [ label = "VIDIOC_STREAMON(CAPTURE)" ]; 7862306a36Sopenharmony_ci Reset -> Initialization [ label = "VIDIOC_REQBUFS(OUTPUT, 0)" ]; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci Stopped -> Encoding [ label = "V4L2_ENC_CMD_START\nor\nVIDIOC_STREAMON(OUTPUT)" ]; 8162306a36Sopenharmony_ci Stopped -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ]; 8262306a36Sopenharmony_ci } 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ciQuerying Capabilities 8562306a36Sopenharmony_ci===================== 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci1. To enumerate the set of coded formats supported by the encoder, the 8862306a36Sopenharmony_ci client may call :c:func:`VIDIOC_ENUM_FMT` on ``CAPTURE``. 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci * The full set of supported formats will be returned, regardless of the 9162306a36Sopenharmony_ci format set on ``OUTPUT``. 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci2. To enumerate the set of supported raw formats, the client may call 9462306a36Sopenharmony_ci :c:func:`VIDIOC_ENUM_FMT` on ``OUTPUT``. 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci * Only the formats supported for the format currently active on ``CAPTURE`` 9762306a36Sopenharmony_ci will be returned. 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci * In order to enumerate raw formats supported by a given coded format, 10062306a36Sopenharmony_ci the client must first set that coded format on ``CAPTURE`` and then 10162306a36Sopenharmony_ci enumerate the formats on ``OUTPUT``. 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci3. The client may use :c:func:`VIDIOC_ENUM_FRAMESIZES` to detect supported 10462306a36Sopenharmony_ci resolutions for a given format, passing the desired pixel format in 10562306a36Sopenharmony_ci :c:type:`v4l2_frmsizeenum` ``pixel_format``. 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci * Values returned by :c:func:`VIDIOC_ENUM_FRAMESIZES` for a coded pixel 10862306a36Sopenharmony_ci format will include all possible coded resolutions supported by the 10962306a36Sopenharmony_ci encoder for the given coded pixel format. 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci * Values returned by :c:func:`VIDIOC_ENUM_FRAMESIZES` for a raw pixel format 11262306a36Sopenharmony_ci will include all possible frame buffer resolutions supported by the 11362306a36Sopenharmony_ci encoder for the given raw pixel format and coded format currently set on 11462306a36Sopenharmony_ci ``CAPTURE``. 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci4. The client may use :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` to detect supported 11762306a36Sopenharmony_ci frame intervals for a given format and resolution, passing the desired pixel 11862306a36Sopenharmony_ci format in :c:type:`v4l2_frmivalenum` ``pixel_format`` and the resolution 11962306a36Sopenharmony_ci in :c:type:`v4l2_frmivalenum` ``width`` and :c:type:`v4l2_frmivalenum` 12062306a36Sopenharmony_ci ``height``. 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci * Values returned by :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` for a coded pixel 12362306a36Sopenharmony_ci format and coded resolution will include all possible frame intervals 12462306a36Sopenharmony_ci supported by the encoder for the given coded pixel format and resolution. 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci * Values returned by :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` for a raw pixel 12762306a36Sopenharmony_ci format and resolution will include all possible frame intervals supported 12862306a36Sopenharmony_ci by the encoder for the given raw pixel format and resolution and for the 12962306a36Sopenharmony_ci coded format, coded resolution and coded frame interval currently set on 13062306a36Sopenharmony_ci ``CAPTURE``. 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci * Support for :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` is optional. If it is 13362306a36Sopenharmony_ci not implemented, then there are no special restrictions other than the 13462306a36Sopenharmony_ci limits of the codec itself. 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci5. Supported profiles and levels for the coded format currently set on 13762306a36Sopenharmony_ci ``CAPTURE``, if applicable, may be queried using their respective controls 13862306a36Sopenharmony_ci via :c:func:`VIDIOC_QUERYCTRL`. 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci6. Any additional encoder capabilities may be discovered by querying 14162306a36Sopenharmony_ci their respective controls. 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ciInitialization 14462306a36Sopenharmony_ci============== 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci1. Set the coded format on the ``CAPTURE`` queue via :c:func:`VIDIOC_S_FMT`. 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci * **Required fields:** 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci ``type`` 15162306a36Sopenharmony_ci a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``. 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci ``pixelformat`` 15462306a36Sopenharmony_ci the coded format to be produced. 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci ``sizeimage`` 15762306a36Sopenharmony_ci desired size of ``CAPTURE`` buffers; the encoder may adjust it to 15862306a36Sopenharmony_ci match hardware requirements. 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci ``width``, ``height`` 16162306a36Sopenharmony_ci ignored (read-only). 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci other fields 16462306a36Sopenharmony_ci follow standard semantics. 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci * **Returned fields:** 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci ``sizeimage`` 16962306a36Sopenharmony_ci adjusted size of ``CAPTURE`` buffers. 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci ``width``, ``height`` 17262306a36Sopenharmony_ci the coded size selected by the encoder based on current state, e.g. 17362306a36Sopenharmony_ci ``OUTPUT`` format, selection rectangles, etc. (read-only). 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci .. important:: 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci Changing the ``CAPTURE`` format may change the currently set ``OUTPUT`` 17862306a36Sopenharmony_ci format. How the new ``OUTPUT`` format is determined is up to the encoder 17962306a36Sopenharmony_ci and the client must ensure it matches its needs afterwards. 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci2. **Optional.** Enumerate supported ``OUTPUT`` formats (raw formats for 18262306a36Sopenharmony_ci source) for the selected coded format via :c:func:`VIDIOC_ENUM_FMT`. 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci * **Required fields:** 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci ``type`` 18762306a36Sopenharmony_ci a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``. 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci other fields 19062306a36Sopenharmony_ci follow standard semantics. 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci * **Returned fields:** 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci ``pixelformat`` 19562306a36Sopenharmony_ci raw format supported for the coded format currently selected on 19662306a36Sopenharmony_ci the ``CAPTURE`` queue. 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci other fields 19962306a36Sopenharmony_ci follow standard semantics. 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci3. Set the raw source format on the ``OUTPUT`` queue via 20262306a36Sopenharmony_ci :c:func:`VIDIOC_S_FMT`. 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci * **Required fields:** 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci ``type`` 20762306a36Sopenharmony_ci a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``. 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci ``pixelformat`` 21062306a36Sopenharmony_ci raw format of the source. 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci ``width``, ``height`` 21362306a36Sopenharmony_ci source resolution. 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci other fields 21662306a36Sopenharmony_ci follow standard semantics. 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci * **Returned fields:** 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci ``width``, ``height`` 22162306a36Sopenharmony_ci may be adjusted to match encoder minimums, maximums and alignment 22262306a36Sopenharmony_ci requirements, as required by the currently selected formats, as 22362306a36Sopenharmony_ci reported by :c:func:`VIDIOC_ENUM_FRAMESIZES`. 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci other fields 22662306a36Sopenharmony_ci follow standard semantics. 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci * Setting the ``OUTPUT`` format will reset the selection rectangles to their 22962306a36Sopenharmony_ci default values, based on the new resolution, as described in the next 23062306a36Sopenharmony_ci step. 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci4. Set the raw frame interval on the ``OUTPUT`` queue via 23362306a36Sopenharmony_ci :c:func:`VIDIOC_S_PARM`. This also sets the coded frame interval on the 23462306a36Sopenharmony_ci ``CAPTURE`` queue to the same value. 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci * **Required fields:** 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci ``type`` 23962306a36Sopenharmony_ci a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``. 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci ``parm.output`` 24262306a36Sopenharmony_ci set all fields except ``parm.output.timeperframe`` to 0. 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci ``parm.output.timeperframe`` 24562306a36Sopenharmony_ci the desired frame interval; the encoder may adjust it to 24662306a36Sopenharmony_ci match hardware requirements. 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci * **Returned fields:** 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci ``parm.output.timeperframe`` 25162306a36Sopenharmony_ci the adjusted frame interval. 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci .. important:: 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci Changing the ``OUTPUT`` frame interval *also* sets the framerate that 25662306a36Sopenharmony_ci the encoder uses to encode the video. So setting the frame interval 25762306a36Sopenharmony_ci to 1/24 (or 24 frames per second) will produce a coded video stream 25862306a36Sopenharmony_ci that can be played back at that speed. The frame interval for the 25962306a36Sopenharmony_ci ``OUTPUT`` queue is just a hint, the application may provide raw 26062306a36Sopenharmony_ci frames at a different rate. It can be used by the driver to help 26162306a36Sopenharmony_ci schedule multiple encoders running in parallel. 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci In the next step the ``CAPTURE`` frame interval can optionally be 26462306a36Sopenharmony_ci changed to a different value. This is useful for off-line encoding 26562306a36Sopenharmony_ci were the coded frame interval can be different from the rate at 26662306a36Sopenharmony_ci which raw frames are supplied. 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci .. important:: 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci ``timeperframe`` deals with *frames*, not fields. So for interlaced 27162306a36Sopenharmony_ci formats this is the time per two fields, since a frame consists of 27262306a36Sopenharmony_ci a top and a bottom field. 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ci .. note:: 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci It is due to historical reasons that changing the ``OUTPUT`` frame 27762306a36Sopenharmony_ci interval also changes the coded frame interval on the ``CAPTURE`` 27862306a36Sopenharmony_ci queue. Ideally these would be independent settings, but that would 27962306a36Sopenharmony_ci break the existing API. 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci5. **Optional** Set the coded frame interval on the ``CAPTURE`` queue via 28262306a36Sopenharmony_ci :c:func:`VIDIOC_S_PARM`. This is only necessary if the coded frame 28362306a36Sopenharmony_ci interval is different from the raw frame interval, which is typically 28462306a36Sopenharmony_ci the case for off-line encoding. Support for this feature is signalled 28562306a36Sopenharmony_ci by the :ref:`V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL <fmtdesc-flags>` format flag. 28662306a36Sopenharmony_ci 28762306a36Sopenharmony_ci * **Required fields:** 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci ``type`` 29062306a36Sopenharmony_ci a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``. 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci ``parm.capture`` 29362306a36Sopenharmony_ci set all fields except ``parm.capture.timeperframe`` to 0. 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci ``parm.capture.timeperframe`` 29662306a36Sopenharmony_ci the desired coded frame interval; the encoder may adjust it to 29762306a36Sopenharmony_ci match hardware requirements. 29862306a36Sopenharmony_ci 29962306a36Sopenharmony_ci * **Returned fields:** 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ci ``parm.capture.timeperframe`` 30262306a36Sopenharmony_ci the adjusted frame interval. 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ci .. important:: 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci Changing the ``CAPTURE`` frame interval sets the framerate for the 30762306a36Sopenharmony_ci coded video. It does *not* set the rate at which buffers arrive on the 30862306a36Sopenharmony_ci ``CAPTURE`` queue, that depends on how fast the encoder is and how 30962306a36Sopenharmony_ci fast raw frames are queued on the ``OUTPUT`` queue. 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci .. important:: 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci ``timeperframe`` deals with *frames*, not fields. So for interlaced 31462306a36Sopenharmony_ci formats this is the time per two fields, since a frame consists of 31562306a36Sopenharmony_ci a top and a bottom field. 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ci .. note:: 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ci Not all drivers support this functionality, in that case just set 32062306a36Sopenharmony_ci the desired coded frame interval for the ``OUTPUT`` queue. 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ci However, drivers that can schedule multiple encoders based on the 32362306a36Sopenharmony_ci ``OUTPUT`` frame interval must support this optional feature. 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci6. **Optional.** Set the visible resolution for the stream metadata via 32662306a36Sopenharmony_ci :c:func:`VIDIOC_S_SELECTION` on the ``OUTPUT`` queue if it is desired 32762306a36Sopenharmony_ci to be different than the full OUTPUT resolution. 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci * **Required fields:** 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci ``type`` 33262306a36Sopenharmony_ci a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``. 33362306a36Sopenharmony_ci 33462306a36Sopenharmony_ci ``target`` 33562306a36Sopenharmony_ci set to ``V4L2_SEL_TGT_CROP``. 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci ``r.left``, ``r.top``, ``r.width``, ``r.height`` 33862306a36Sopenharmony_ci visible rectangle; this must fit within the `V4L2_SEL_TGT_CROP_BOUNDS` 33962306a36Sopenharmony_ci rectangle and may be subject to adjustment to match codec and 34062306a36Sopenharmony_ci hardware constraints. 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ci * **Returned fields:** 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_ci ``r.left``, ``r.top``, ``r.width``, ``r.height`` 34562306a36Sopenharmony_ci visible rectangle adjusted by the encoder. 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_ci * The following selection targets are supported on ``OUTPUT``: 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci ``V4L2_SEL_TGT_CROP_BOUNDS`` 35062306a36Sopenharmony_ci equal to the full source frame, matching the active ``OUTPUT`` 35162306a36Sopenharmony_ci format. 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci ``V4L2_SEL_TGT_CROP_DEFAULT`` 35462306a36Sopenharmony_ci equal to ``V4L2_SEL_TGT_CROP_BOUNDS``. 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci ``V4L2_SEL_TGT_CROP`` 35762306a36Sopenharmony_ci rectangle within the source buffer to be encoded into the 35862306a36Sopenharmony_ci ``CAPTURE`` stream; defaults to ``V4L2_SEL_TGT_CROP_DEFAULT``. 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ci .. note:: 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci A common use case for this selection target is encoding a source 36362306a36Sopenharmony_ci video with a resolution that is not a multiple of a macroblock, 36462306a36Sopenharmony_ci e.g. the common 1920x1080 resolution may require the source 36562306a36Sopenharmony_ci buffers to be aligned to 1920x1088 for codecs with 16x16 macroblock 36662306a36Sopenharmony_ci size. To avoid encoding the padding, the client needs to explicitly 36762306a36Sopenharmony_ci configure this selection target to 1920x1080. 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_ci .. warning:: 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci The encoder may adjust the crop/compose rectangles to the nearest 37262306a36Sopenharmony_ci supported ones to meet codec and hardware requirements. The client needs 37362306a36Sopenharmony_ci to check the adjusted rectangle returned by :c:func:`VIDIOC_S_SELECTION`. 37462306a36Sopenharmony_ci 37562306a36Sopenharmony_ci7. Allocate buffers for both ``OUTPUT`` and ``CAPTURE`` via 37662306a36Sopenharmony_ci :c:func:`VIDIOC_REQBUFS`. This may be performed in any order. 37762306a36Sopenharmony_ci 37862306a36Sopenharmony_ci * **Required fields:** 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci ``count`` 38162306a36Sopenharmony_ci requested number of buffers to allocate; greater than zero. 38262306a36Sopenharmony_ci 38362306a36Sopenharmony_ci ``type`` 38462306a36Sopenharmony_ci a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT`` or 38562306a36Sopenharmony_ci ``CAPTURE``. 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci other fields 38862306a36Sopenharmony_ci follow standard semantics. 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci * **Returned fields:** 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci ``count`` 39362306a36Sopenharmony_ci actual number of buffers allocated. 39462306a36Sopenharmony_ci 39562306a36Sopenharmony_ci .. warning:: 39662306a36Sopenharmony_ci 39762306a36Sopenharmony_ci The actual number of allocated buffers may differ from the ``count`` 39862306a36Sopenharmony_ci given. The client must check the updated value of ``count`` after the 39962306a36Sopenharmony_ci call returns. 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ci .. note:: 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ci To allocate more than the minimum number of OUTPUT buffers (for pipeline 40462306a36Sopenharmony_ci depth), the client may query the ``V4L2_CID_MIN_BUFFERS_FOR_OUTPUT`` 40562306a36Sopenharmony_ci control to get the minimum number of buffers required, and pass the 40662306a36Sopenharmony_ci obtained value plus the number of additional buffers needed in the 40762306a36Sopenharmony_ci ``count`` field to :c:func:`VIDIOC_REQBUFS`. 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci Alternatively, :c:func:`VIDIOC_CREATE_BUFS` can be used to have more 41062306a36Sopenharmony_ci control over buffer allocation. 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ci * **Required fields:** 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_ci ``count`` 41562306a36Sopenharmony_ci requested number of buffers to allocate; greater than zero. 41662306a36Sopenharmony_ci 41762306a36Sopenharmony_ci ``type`` 41862306a36Sopenharmony_ci a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``. 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci other fields 42162306a36Sopenharmony_ci follow standard semantics. 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ci * **Returned fields:** 42462306a36Sopenharmony_ci 42562306a36Sopenharmony_ci ``count`` 42662306a36Sopenharmony_ci adjusted to the number of allocated buffers. 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ci8. Begin streaming on both ``OUTPUT`` and ``CAPTURE`` queues via 42962306a36Sopenharmony_ci :c:func:`VIDIOC_STREAMON`. This may be performed in any order. The actual 43062306a36Sopenharmony_ci encoding process starts when both queues start streaming. 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci.. note:: 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci If the client stops the ``CAPTURE`` queue during the encode process and then 43562306a36Sopenharmony_ci restarts it again, the encoder will begin generating a stream independent 43662306a36Sopenharmony_ci from the stream generated before the stop. The exact constraints depend 43762306a36Sopenharmony_ci on the coded format, but may include the following implications: 43862306a36Sopenharmony_ci 43962306a36Sopenharmony_ci * encoded frames produced after the restart must not reference any 44062306a36Sopenharmony_ci frames produced before the stop, e.g. no long term references for 44162306a36Sopenharmony_ci H.264/HEVC, 44262306a36Sopenharmony_ci 44362306a36Sopenharmony_ci * any headers that must be included in a standalone stream must be 44462306a36Sopenharmony_ci produced again, e.g. SPS and PPS for H.264/HEVC. 44562306a36Sopenharmony_ci 44662306a36Sopenharmony_ciEncoding 44762306a36Sopenharmony_ci======== 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ciThis state is reached after the `Initialization` sequence finishes 45062306a36Sopenharmony_cisuccessfully. In this state, the client queues and dequeues buffers to both 45162306a36Sopenharmony_ciqueues via :c:func:`VIDIOC_QBUF` and :c:func:`VIDIOC_DQBUF`, following the 45262306a36Sopenharmony_cistandard semantics. 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_ciThe content of encoded ``CAPTURE`` buffers depends on the active coded pixel 45562306a36Sopenharmony_ciformat and may be affected by codec-specific extended controls, as stated 45662306a36Sopenharmony_ciin the documentation of each format. 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ciBoth queues operate independently, following standard behavior of V4L2 buffer 45962306a36Sopenharmony_ciqueues and memory-to-memory devices. In addition, the order of encoded frames 46062306a36Sopenharmony_cidequeued from the ``CAPTURE`` queue may differ from the order of queuing raw 46162306a36Sopenharmony_ciframes to the ``OUTPUT`` queue, due to properties of the selected coded format, 46262306a36Sopenharmony_cie.g. frame reordering. 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_ciThe client must not assume any direct relationship between ``CAPTURE`` and 46562306a36Sopenharmony_ci``OUTPUT`` buffers and any specific timing of buffers becoming 46662306a36Sopenharmony_ciavailable to dequeue. Specifically: 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_ci* a buffer queued to ``OUTPUT`` may result in more than one buffer produced on 46962306a36Sopenharmony_ci ``CAPTURE`` (for example, if returning an encoded frame allowed the encoder 47062306a36Sopenharmony_ci to return a frame that preceded it in display, but succeeded it in the decode 47162306a36Sopenharmony_ci order; however, there may be other reasons for this as well), 47262306a36Sopenharmony_ci 47362306a36Sopenharmony_ci* a buffer queued to ``OUTPUT`` may result in a buffer being produced on 47462306a36Sopenharmony_ci ``CAPTURE`` later into encode process, and/or after processing further 47562306a36Sopenharmony_ci ``OUTPUT`` buffers, or be returned out of order, e.g. if display 47662306a36Sopenharmony_ci reordering is used, 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci* buffers may become available on the ``CAPTURE`` queue without additional 47962306a36Sopenharmony_ci buffers queued to ``OUTPUT`` (e.g. during drain or ``EOS``), because of the 48062306a36Sopenharmony_ci ``OUTPUT`` buffers queued in the past whose encoding results are only 48162306a36Sopenharmony_ci available at later time, due to specifics of the encoding process, 48262306a36Sopenharmony_ci 48362306a36Sopenharmony_ci* buffers queued to ``OUTPUT`` may not become available to dequeue instantly 48462306a36Sopenharmony_ci after being encoded into a corresponding ``CAPTURE`` buffer, e.g. if the 48562306a36Sopenharmony_ci encoder needs to use the frame as a reference for encoding further frames. 48662306a36Sopenharmony_ci 48762306a36Sopenharmony_ci.. note:: 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ci To allow matching encoded ``CAPTURE`` buffers with ``OUTPUT`` buffers they 49062306a36Sopenharmony_ci originated from, the client can set the ``timestamp`` field of the 49162306a36Sopenharmony_ci :c:type:`v4l2_buffer` struct when queuing an ``OUTPUT`` buffer. The 49262306a36Sopenharmony_ci ``CAPTURE`` buffer(s), which resulted from encoding that ``OUTPUT`` buffer 49362306a36Sopenharmony_ci will have their ``timestamp`` field set to the same value when dequeued. 49462306a36Sopenharmony_ci 49562306a36Sopenharmony_ci In addition to the straightforward case of one ``OUTPUT`` buffer producing 49662306a36Sopenharmony_ci one ``CAPTURE`` buffer, the following cases are defined: 49762306a36Sopenharmony_ci 49862306a36Sopenharmony_ci * one ``OUTPUT`` buffer generates multiple ``CAPTURE`` buffers: the same 49962306a36Sopenharmony_ci ``OUTPUT`` timestamp will be copied to multiple ``CAPTURE`` buffers, 50062306a36Sopenharmony_ci 50162306a36Sopenharmony_ci * the encoding order differs from the presentation order (i.e. the 50262306a36Sopenharmony_ci ``CAPTURE`` buffers are out-of-order compared to the ``OUTPUT`` buffers): 50362306a36Sopenharmony_ci ``CAPTURE`` timestamps will not retain the order of ``OUTPUT`` timestamps. 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci.. note:: 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ci To let the client distinguish between frame types (keyframes, intermediate 50862306a36Sopenharmony_ci frames; the exact list of types depends on the coded format), the 50962306a36Sopenharmony_ci ``CAPTURE`` buffers will have corresponding flag bits set in their 51062306a36Sopenharmony_ci :c:type:`v4l2_buffer` struct when dequeued. See the documentation of 51162306a36Sopenharmony_ci :c:type:`v4l2_buffer` and each coded pixel format for exact list of flags 51262306a36Sopenharmony_ci and their meanings. 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ciShould an encoding error occur, it will be reported to the client with the level 51562306a36Sopenharmony_ciof details depending on the encoder capabilities. Specifically: 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_ci* the ``CAPTURE`` buffer (if any) that contains the results of the failed encode 51862306a36Sopenharmony_ci operation will be returned with the ``V4L2_BUF_FLAG_ERROR`` flag set, 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci* if the encoder is able to precisely report the ``OUTPUT`` buffer(s) that triggered 52162306a36Sopenharmony_ci the error, such buffer(s) will be returned with the ``V4L2_BUF_FLAG_ERROR`` flag 52262306a36Sopenharmony_ci set. 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_ci.. note:: 52562306a36Sopenharmony_ci 52662306a36Sopenharmony_ci If a ``CAPTURE`` buffer is too small then it is just returned with the 52762306a36Sopenharmony_ci ``V4L2_BUF_FLAG_ERROR`` flag set. More work is needed to detect that this 52862306a36Sopenharmony_ci error occurred because the buffer was too small, and to provide support to 52962306a36Sopenharmony_ci free existing buffers that were too small. 53062306a36Sopenharmony_ci 53162306a36Sopenharmony_ciIn case of a fatal failure that does not allow the encoding to continue, any 53262306a36Sopenharmony_cifurther operations on corresponding encoder file handle will return the -EIO 53362306a36Sopenharmony_cierror code. The client may close the file handle and open a new one, or 53462306a36Sopenharmony_cialternatively reinitialize the instance by stopping streaming on both queues, 53562306a36Sopenharmony_cireleasing all buffers and performing the Initialization sequence again. 53662306a36Sopenharmony_ci 53762306a36Sopenharmony_ciEncoding Parameter Changes 53862306a36Sopenharmony_ci========================== 53962306a36Sopenharmony_ci 54062306a36Sopenharmony_ciThe client is allowed to use :c:func:`VIDIOC_S_CTRL` to change encoder 54162306a36Sopenharmony_ciparameters at any time. The availability of parameters is encoder-specific 54262306a36Sopenharmony_ciand the client must query the encoder to find the set of available controls. 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ciThe ability to change each parameter during encoding is encoder-specific, as 54562306a36Sopenharmony_ciper the standard semantics of the V4L2 control interface. The client may 54662306a36Sopenharmony_ciattempt to set a control during encoding and if the operation fails with the 54762306a36Sopenharmony_ci-EBUSY error code, the ``CAPTURE`` queue needs to be stopped for the 54862306a36Sopenharmony_ciconfiguration change to be allowed. To do this, it may follow the `Drain` 54962306a36Sopenharmony_cisequence to avoid losing the already queued/encoded frames. 55062306a36Sopenharmony_ci 55162306a36Sopenharmony_ciThe timing of parameter updates is encoder-specific, as per the standard 55262306a36Sopenharmony_cisemantics of the V4L2 control interface. If the client needs to apply the 55362306a36Sopenharmony_ciparameters exactly at specific frame, using the Request API 55462306a36Sopenharmony_ci(:ref:`media-request-api`) should be considered, if supported by the encoder. 55562306a36Sopenharmony_ci 55662306a36Sopenharmony_ciDrain 55762306a36Sopenharmony_ci===== 55862306a36Sopenharmony_ci 55962306a36Sopenharmony_ciTo ensure that all the queued ``OUTPUT`` buffers have been processed and the 56062306a36Sopenharmony_cirelated ``CAPTURE`` buffers are given to the client, the client must follow the 56162306a36Sopenharmony_cidrain sequence described below. After the drain sequence ends, the client has 56262306a36Sopenharmony_cireceived all encoded frames for all ``OUTPUT`` buffers queued before the 56362306a36Sopenharmony_cisequence was started. 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_ci1. Begin the drain sequence by issuing :c:func:`VIDIOC_ENCODER_CMD`. 56662306a36Sopenharmony_ci 56762306a36Sopenharmony_ci * **Required fields:** 56862306a36Sopenharmony_ci 56962306a36Sopenharmony_ci ``cmd`` 57062306a36Sopenharmony_ci set to ``V4L2_ENC_CMD_STOP``. 57162306a36Sopenharmony_ci 57262306a36Sopenharmony_ci ``flags`` 57362306a36Sopenharmony_ci set to 0. 57462306a36Sopenharmony_ci 57562306a36Sopenharmony_ci ``pts`` 57662306a36Sopenharmony_ci set to 0. 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci .. warning:: 57962306a36Sopenharmony_ci 58062306a36Sopenharmony_ci The sequence can be only initiated if both ``OUTPUT`` and ``CAPTURE`` 58162306a36Sopenharmony_ci queues are streaming. For compatibility reasons, the call to 58262306a36Sopenharmony_ci :c:func:`VIDIOC_ENCODER_CMD` will not fail even if any of the queues is 58362306a36Sopenharmony_ci not streaming, but at the same time it will not initiate the `Drain` 58462306a36Sopenharmony_ci sequence and so the steps described below would not be applicable. 58562306a36Sopenharmony_ci 58662306a36Sopenharmony_ci2. Any ``OUTPUT`` buffers queued by the client before the 58762306a36Sopenharmony_ci :c:func:`VIDIOC_ENCODER_CMD` was issued will be processed and encoded as 58862306a36Sopenharmony_ci normal. The client must continue to handle both queues independently, 58962306a36Sopenharmony_ci similarly to normal encode operation. This includes: 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_ci * queuing and dequeuing ``CAPTURE`` buffers, until a buffer marked with the 59262306a36Sopenharmony_ci ``V4L2_BUF_FLAG_LAST`` flag is dequeued, 59362306a36Sopenharmony_ci 59462306a36Sopenharmony_ci .. warning:: 59562306a36Sopenharmony_ci 59662306a36Sopenharmony_ci The last buffer may be empty (with :c:type:`v4l2_buffer` 59762306a36Sopenharmony_ci ``bytesused`` = 0) and in that case it must be ignored by the client, 59862306a36Sopenharmony_ci as it does not contain an encoded frame. 59962306a36Sopenharmony_ci 60062306a36Sopenharmony_ci .. note:: 60162306a36Sopenharmony_ci 60262306a36Sopenharmony_ci Any attempt to dequeue more ``CAPTURE`` buffers beyond the buffer 60362306a36Sopenharmony_ci marked with ``V4L2_BUF_FLAG_LAST`` will result in a -EPIPE error from 60462306a36Sopenharmony_ci :c:func:`VIDIOC_DQBUF`. 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci * dequeuing processed ``OUTPUT`` buffers, until all the buffers queued 60762306a36Sopenharmony_ci before the ``V4L2_ENC_CMD_STOP`` command are dequeued, 60862306a36Sopenharmony_ci 60962306a36Sopenharmony_ci * dequeuing the ``V4L2_EVENT_EOS`` event, if the client subscribes to it. 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci .. note:: 61262306a36Sopenharmony_ci 61362306a36Sopenharmony_ci For backwards compatibility, the encoder will signal a ``V4L2_EVENT_EOS`` 61462306a36Sopenharmony_ci event when the last frame has been encoded and all frames are ready to be 61562306a36Sopenharmony_ci dequeued. It is deprecated behavior and the client must not rely on it. 61662306a36Sopenharmony_ci The ``V4L2_BUF_FLAG_LAST`` buffer flag should be used instead. 61762306a36Sopenharmony_ci 61862306a36Sopenharmony_ci3. Once all ``OUTPUT`` buffers queued before the ``V4L2_ENC_CMD_STOP`` call are 61962306a36Sopenharmony_ci dequeued and the last ``CAPTURE`` buffer is dequeued, the encoder is stopped 62062306a36Sopenharmony_ci and it will accept, but not process any newly queued ``OUTPUT`` buffers 62162306a36Sopenharmony_ci until the client issues any of the following operations: 62262306a36Sopenharmony_ci 62362306a36Sopenharmony_ci * ``V4L2_ENC_CMD_START`` - the encoder will not be reset and will resume 62462306a36Sopenharmony_ci operation normally, with all the state from before the drain, 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ci * a pair of :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the 62762306a36Sopenharmony_ci ``CAPTURE`` queue - the encoder will be reset (see the `Reset` sequence) 62862306a36Sopenharmony_ci and then resume encoding, 62962306a36Sopenharmony_ci 63062306a36Sopenharmony_ci * a pair of :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the 63162306a36Sopenharmony_ci ``OUTPUT`` queue - the encoder will resume operation normally, however any 63262306a36Sopenharmony_ci source frames queued to the ``OUTPUT`` queue between ``V4L2_ENC_CMD_STOP`` 63362306a36Sopenharmony_ci and :c:func:`VIDIOC_STREAMOFF` will be discarded. 63462306a36Sopenharmony_ci 63562306a36Sopenharmony_ci.. note:: 63662306a36Sopenharmony_ci 63762306a36Sopenharmony_ci Once the drain sequence is initiated, the client needs to drive it to 63862306a36Sopenharmony_ci completion, as described by the steps above, unless it aborts the process by 63962306a36Sopenharmony_ci issuing :c:func:`VIDIOC_STREAMOFF` on any of the ``OUTPUT`` or ``CAPTURE`` 64062306a36Sopenharmony_ci queues. The client is not allowed to issue ``V4L2_ENC_CMD_START`` or 64162306a36Sopenharmony_ci ``V4L2_ENC_CMD_STOP`` again while the drain sequence is in progress and they 64262306a36Sopenharmony_ci will fail with -EBUSY error code if attempted. 64362306a36Sopenharmony_ci 64462306a36Sopenharmony_ci For reference, handling of various corner cases is described below: 64562306a36Sopenharmony_ci 64662306a36Sopenharmony_ci * In case of no buffer in the ``OUTPUT`` queue at the time the 64762306a36Sopenharmony_ci ``V4L2_ENC_CMD_STOP`` command was issued, the drain sequence completes 64862306a36Sopenharmony_ci immediately and the encoder returns an empty ``CAPTURE`` buffer with the 64962306a36Sopenharmony_ci ``V4L2_BUF_FLAG_LAST`` flag set. 65062306a36Sopenharmony_ci 65162306a36Sopenharmony_ci * In case of no buffer in the ``CAPTURE`` queue at the time the drain 65262306a36Sopenharmony_ci sequence completes, the next time the client queues a ``CAPTURE`` buffer 65362306a36Sopenharmony_ci it is returned at once as an empty buffer with the ``V4L2_BUF_FLAG_LAST`` 65462306a36Sopenharmony_ci flag set. 65562306a36Sopenharmony_ci 65662306a36Sopenharmony_ci * If :c:func:`VIDIOC_STREAMOFF` is called on the ``CAPTURE`` queue in the 65762306a36Sopenharmony_ci middle of the drain sequence, the drain sequence is canceled and all 65862306a36Sopenharmony_ci ``CAPTURE`` buffers are implicitly returned to the client. 65962306a36Sopenharmony_ci 66062306a36Sopenharmony_ci * If :c:func:`VIDIOC_STREAMOFF` is called on the ``OUTPUT`` queue in the 66162306a36Sopenharmony_ci middle of the drain sequence, the drain sequence completes immediately and 66262306a36Sopenharmony_ci next ``CAPTURE`` buffer will be returned empty with the 66362306a36Sopenharmony_ci ``V4L2_BUF_FLAG_LAST`` flag set. 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_ci Although not mandatory, the availability of encoder commands may be queried 66662306a36Sopenharmony_ci using :c:func:`VIDIOC_TRY_ENCODER_CMD`. 66762306a36Sopenharmony_ci 66862306a36Sopenharmony_ciReset 66962306a36Sopenharmony_ci===== 67062306a36Sopenharmony_ci 67162306a36Sopenharmony_ciThe client may want to request the encoder to reinitialize the encoding, so 67262306a36Sopenharmony_cithat the following stream data becomes independent from the stream data 67362306a36Sopenharmony_cigenerated before. Depending on the coded format, that may imply that: 67462306a36Sopenharmony_ci 67562306a36Sopenharmony_ci* encoded frames produced after the restart must not reference any frames 67662306a36Sopenharmony_ci produced before the stop, e.g. no long term references for H.264/HEVC, 67762306a36Sopenharmony_ci 67862306a36Sopenharmony_ci* any headers that must be included in a standalone stream must be produced 67962306a36Sopenharmony_ci again, e.g. SPS and PPS for H.264/HEVC. 68062306a36Sopenharmony_ci 68162306a36Sopenharmony_ciThis can be achieved by performing the reset sequence. 68262306a36Sopenharmony_ci 68362306a36Sopenharmony_ci1. Perform the `Drain` sequence to ensure all the in-flight encoding finishes 68462306a36Sopenharmony_ci and respective buffers are dequeued. 68562306a36Sopenharmony_ci 68662306a36Sopenharmony_ci2. Stop streaming on the ``CAPTURE`` queue via :c:func:`VIDIOC_STREAMOFF`. This 68762306a36Sopenharmony_ci will return all currently queued ``CAPTURE`` buffers to the client, without 68862306a36Sopenharmony_ci valid frame data. 68962306a36Sopenharmony_ci 69062306a36Sopenharmony_ci3. Start streaming on the ``CAPTURE`` queue via :c:func:`VIDIOC_STREAMON` and 69162306a36Sopenharmony_ci continue with regular encoding sequence. The encoded frames produced into 69262306a36Sopenharmony_ci ``CAPTURE`` buffers from now on will contain a standalone stream that can be 69362306a36Sopenharmony_ci decoded without the need for frames encoded before the reset sequence, 69462306a36Sopenharmony_ci starting at the first ``OUTPUT`` buffer queued after issuing the 69562306a36Sopenharmony_ci `V4L2_ENC_CMD_STOP` of the `Drain` sequence. 69662306a36Sopenharmony_ci 69762306a36Sopenharmony_ciThis sequence may be also used to change encoding parameters for encoders 69862306a36Sopenharmony_ciwithout the ability to change the parameters on the fly. 69962306a36Sopenharmony_ci 70062306a36Sopenharmony_ciCommit Points 70162306a36Sopenharmony_ci============= 70262306a36Sopenharmony_ci 70362306a36Sopenharmony_ciSetting formats and allocating buffers triggers changes in the behavior of the 70462306a36Sopenharmony_ciencoder. 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci1. Setting the format on the ``CAPTURE`` queue may change the set of formats 70762306a36Sopenharmony_ci supported/advertised on the ``OUTPUT`` queue. In particular, it also means 70862306a36Sopenharmony_ci that the ``OUTPUT`` format may be reset and the client must not rely on the 70962306a36Sopenharmony_ci previously set format being preserved. 71062306a36Sopenharmony_ci 71162306a36Sopenharmony_ci2. Enumerating formats on the ``OUTPUT`` queue always returns only formats 71262306a36Sopenharmony_ci supported for the current ``CAPTURE`` format. 71362306a36Sopenharmony_ci 71462306a36Sopenharmony_ci3. Setting the format on the ``OUTPUT`` queue does not change the list of 71562306a36Sopenharmony_ci formats available on the ``CAPTURE`` queue. An attempt to set the ``OUTPUT`` 71662306a36Sopenharmony_ci format that is not supported for the currently selected ``CAPTURE`` format 71762306a36Sopenharmony_ci will result in the encoder adjusting the requested ``OUTPUT`` format to a 71862306a36Sopenharmony_ci supported one. 71962306a36Sopenharmony_ci 72062306a36Sopenharmony_ci4. Enumerating formats on the ``CAPTURE`` queue always returns the full set of 72162306a36Sopenharmony_ci supported coded formats, irrespective of the current ``OUTPUT`` format. 72262306a36Sopenharmony_ci 72362306a36Sopenharmony_ci5. While buffers are allocated on any of the ``OUTPUT`` or ``CAPTURE`` queues, 72462306a36Sopenharmony_ci the client must not change the format on the ``CAPTURE`` queue. Drivers will 72562306a36Sopenharmony_ci return the -EBUSY error code for any such format change attempt. 72662306a36Sopenharmony_ci 72762306a36Sopenharmony_ciTo summarize, setting formats and allocation must always start with the 72862306a36Sopenharmony_ci``CAPTURE`` queue and the ``CAPTURE`` queue is the master that governs the 72962306a36Sopenharmony_ciset of supported formats for the ``OUTPUT`` queue. 730