18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci.. _encoder:
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci*************************************************
68c2ecf20Sopenharmony_ciMemory-to-Memory Stateful Video Encoder Interface
78c2ecf20Sopenharmony_ci*************************************************
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciA stateful video encoder takes raw video frames in display order and encodes
108c2ecf20Sopenharmony_cithem into a bytestream. It generates complete chunks of the bytestream, including
118c2ecf20Sopenharmony_ciall metadata, headers, etc. The resulting bytestream does not require any
128c2ecf20Sopenharmony_cifurther post-processing by the client.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciPerforming software stream processing, header generation etc. in the driver
158c2ecf20Sopenharmony_ciin order to support this interface is strongly discouraged. In case such
168c2ecf20Sopenharmony_cioperations are needed, use of the Stateless Video Encoder Interface (in
178c2ecf20Sopenharmony_cidevelopment) is strongly advised.
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciConventions and Notations Used in This Document
208c2ecf20Sopenharmony_ci===============================================
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci1. The general V4L2 API rules apply if not specified in this document
238c2ecf20Sopenharmony_ci   otherwise.
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci2. The meaning of words "must", "may", "should", etc. is as per `RFC
268c2ecf20Sopenharmony_ci   2119 <https://tools.ietf.org/html/rfc2119>`_.
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci3. All steps not marked "optional" are required.
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci4. :c:func:`VIDIOC_G_EXT_CTRLS` and :c:func:`VIDIOC_S_EXT_CTRLS` may be used
318c2ecf20Sopenharmony_ci   interchangeably with :c:func:`VIDIOC_G_CTRL` and :c:func:`VIDIOC_S_CTRL`,
328c2ecf20Sopenharmony_ci   unless specified otherwise.
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci5. Single-planar API (see :ref:`planar-apis`) and applicable structures may be
358c2ecf20Sopenharmony_ci   used interchangeably with multi-planar API, unless specified otherwise,
368c2ecf20Sopenharmony_ci   depending on encoder capabilities and following the general V4L2 guidelines.
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci6. i = [a..b]: sequence of integers from a to b, inclusive, i.e. i =
398c2ecf20Sopenharmony_ci   [0..2]: i = 0, 1, 2.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci7. Given an ``OUTPUT`` buffer A, then A' represents a buffer on the ``CAPTURE``
428c2ecf20Sopenharmony_ci   queue containing data that resulted from processing buffer A.
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciGlossary
458c2ecf20Sopenharmony_ci========
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciRefer to :ref:`decoder-glossary`.
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciState Machine
508c2ecf20Sopenharmony_ci=============
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci.. kernel-render:: DOT
538c2ecf20Sopenharmony_ci   :alt: DOT digraph of encoder state machine
548c2ecf20Sopenharmony_ci   :caption: Encoder State Machine
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci   digraph encoder_state_machine {
578c2ecf20Sopenharmony_ci       node [shape = doublecircle, label="Encoding"] Encoding;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci       node [shape = circle, label="Initialization"] Initialization;
608c2ecf20Sopenharmony_ci       node [shape = circle, label="Stopped"] Stopped;
618c2ecf20Sopenharmony_ci       node [shape = circle, label="Drain"] Drain;
628c2ecf20Sopenharmony_ci       node [shape = circle, label="Reset"] Reset;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci       node [shape = point]; qi
658c2ecf20Sopenharmony_ci       qi -> Initialization [ label = "open()" ];
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci       Initialization -> Encoding [ label = "Both queues streaming" ];
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci       Encoding -> Drain [ label = "V4L2_ENC_CMD_STOP" ];
708c2ecf20Sopenharmony_ci       Encoding -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ];
718c2ecf20Sopenharmony_ci       Encoding -> Stopped [ label = "VIDIOC_STREAMOFF(OUTPUT)" ];
728c2ecf20Sopenharmony_ci       Encoding -> Encoding;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci       Drain -> Stopped [ label = "All CAPTURE\nbuffers dequeued\nor\nVIDIOC_STREAMOFF(OUTPUT)" ];
758c2ecf20Sopenharmony_ci       Drain -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ];
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci       Reset -> Encoding [ label = "VIDIOC_STREAMON(CAPTURE)" ];
788c2ecf20Sopenharmony_ci       Reset -> Initialization [ label = "VIDIOC_REQBUFS(OUTPUT, 0)" ];
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci       Stopped -> Encoding [ label = "V4L2_ENC_CMD_START\nor\nVIDIOC_STREAMON(OUTPUT)" ];
818c2ecf20Sopenharmony_ci       Stopped -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ];
828c2ecf20Sopenharmony_ci   }
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciQuerying Capabilities
858c2ecf20Sopenharmony_ci=====================
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci1. To enumerate the set of coded formats supported by the encoder, the
888c2ecf20Sopenharmony_ci   client may call :c:func:`VIDIOC_ENUM_FMT` on ``CAPTURE``.
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci   * The full set of supported formats will be returned, regardless of the
918c2ecf20Sopenharmony_ci     format set on ``OUTPUT``.
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci2. To enumerate the set of supported raw formats, the client may call
948c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_ENUM_FMT` on ``OUTPUT``.
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci   * Only the formats supported for the format currently active on ``CAPTURE``
978c2ecf20Sopenharmony_ci     will be returned.
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci   * In order to enumerate raw formats supported by a given coded format,
1008c2ecf20Sopenharmony_ci     the client must first set that coded format on ``CAPTURE`` and then
1018c2ecf20Sopenharmony_ci     enumerate the formats on ``OUTPUT``.
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci3. The client may use :c:func:`VIDIOC_ENUM_FRAMESIZES` to detect supported
1048c2ecf20Sopenharmony_ci   resolutions for a given format, passing the desired pixel format in
1058c2ecf20Sopenharmony_ci   :c:type:`v4l2_frmsizeenum` ``pixel_format``.
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci   * Values returned by :c:func:`VIDIOC_ENUM_FRAMESIZES` for a coded pixel
1088c2ecf20Sopenharmony_ci     format will include all possible coded resolutions supported by the
1098c2ecf20Sopenharmony_ci     encoder for the given coded pixel format.
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci   * Values returned by :c:func:`VIDIOC_ENUM_FRAMESIZES` for a raw pixel format
1128c2ecf20Sopenharmony_ci     will include all possible frame buffer resolutions supported by the
1138c2ecf20Sopenharmony_ci     encoder for the given raw pixel format and coded format currently set on
1148c2ecf20Sopenharmony_ci     ``CAPTURE``.
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci4. The client may use :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` to detect supported
1178c2ecf20Sopenharmony_ci   frame intervals for a given format and resolution, passing the desired pixel
1188c2ecf20Sopenharmony_ci   format in :c:type:`v4l2_frmsizeenum` ``pixel_format`` and the resolution
1198c2ecf20Sopenharmony_ci   in :c:type:`v4l2_frmsizeenum` ``width`` and :c:type:`v4l2_frmsizeenum`
1208c2ecf20Sopenharmony_ci   ``height``.
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci   * Values returned by :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` for a coded pixel
1238c2ecf20Sopenharmony_ci     format and coded resolution will include all possible frame intervals
1248c2ecf20Sopenharmony_ci     supported by the encoder for the given coded pixel format and resolution.
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci   * Values returned by :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` for a raw pixel
1278c2ecf20Sopenharmony_ci     format and resolution will include all possible frame intervals supported
1288c2ecf20Sopenharmony_ci     by the encoder for the given raw pixel format and resolution and for the
1298c2ecf20Sopenharmony_ci     coded format, coded resolution and coded frame interval currently set on
1308c2ecf20Sopenharmony_ci     ``CAPTURE``.
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci   * Support for :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` is optional. If it is
1338c2ecf20Sopenharmony_ci     not implemented, then there are no special restrictions other than the
1348c2ecf20Sopenharmony_ci     limits of the codec itself.
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci5. Supported profiles and levels for the coded format currently set on
1378c2ecf20Sopenharmony_ci   ``CAPTURE``, if applicable, may be queried using their respective controls
1388c2ecf20Sopenharmony_ci   via :c:func:`VIDIOC_QUERYCTRL`.
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci6. Any additional encoder capabilities may be discovered by querying
1418c2ecf20Sopenharmony_ci   their respective controls.
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciInitialization
1448c2ecf20Sopenharmony_ci==============
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci1. Set the coded format on the ``CAPTURE`` queue via :c:func:`VIDIOC_S_FMT`.
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci   * **Required fields:**
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci     ``type``
1518c2ecf20Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci     ``pixelformat``
1548c2ecf20Sopenharmony_ci         the coded format to be produced.
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci     ``sizeimage``
1578c2ecf20Sopenharmony_ci         desired size of ``CAPTURE`` buffers; the encoder may adjust it to
1588c2ecf20Sopenharmony_ci         match hardware requirements.
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci     ``width``, ``height``
1618c2ecf20Sopenharmony_ci         ignored (read-only).
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci     other fields
1648c2ecf20Sopenharmony_ci         follow standard semantics.
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci   * **Return fields:**
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci     ``sizeimage``
1698c2ecf20Sopenharmony_ci         adjusted size of ``CAPTURE`` buffers.
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci     ``width``, ``height``
1728c2ecf20Sopenharmony_ci         the coded size selected by the encoder based on current state, e.g.
1738c2ecf20Sopenharmony_ci         ``OUTPUT`` format, selection rectangles, etc. (read-only).
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci   .. important::
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci      Changing the ``CAPTURE`` format may change the currently set ``OUTPUT``
1788c2ecf20Sopenharmony_ci      format. How the new ``OUTPUT`` format is determined is up to the encoder
1798c2ecf20Sopenharmony_ci      and the client must ensure it matches its needs afterwards.
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci2. **Optional.** Enumerate supported ``OUTPUT`` formats (raw formats for
1828c2ecf20Sopenharmony_ci   source) for the selected coded format via :c:func:`VIDIOC_ENUM_FMT`.
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci   * **Required fields:**
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci     ``type``
1878c2ecf20Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci     other fields
1908c2ecf20Sopenharmony_ci         follow standard semantics.
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci   * **Return fields:**
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci     ``pixelformat``
1958c2ecf20Sopenharmony_ci         raw format supported for the coded format currently selected on
1968c2ecf20Sopenharmony_ci         the ``CAPTURE`` queue.
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci     other fields
1998c2ecf20Sopenharmony_ci         follow standard semantics.
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci3. Set the raw source format on the ``OUTPUT`` queue via
2028c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_S_FMT`.
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci   * **Required fields:**
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci     ``type``
2078c2ecf20Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci     ``pixelformat``
2108c2ecf20Sopenharmony_ci         raw format of the source.
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci     ``width``, ``height``
2138c2ecf20Sopenharmony_ci         source resolution.
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci     other fields
2168c2ecf20Sopenharmony_ci         follow standard semantics.
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci   * **Return fields:**
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci     ``width``, ``height``
2218c2ecf20Sopenharmony_ci         may be adjusted to match encoder minimums, maximums and alignment
2228c2ecf20Sopenharmony_ci         requirements, as required by the currently selected formats, as
2238c2ecf20Sopenharmony_ci         reported by :c:func:`VIDIOC_ENUM_FRAMESIZES`.
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci     other fields
2268c2ecf20Sopenharmony_ci         follow standard semantics.
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci   * Setting the ``OUTPUT`` format will reset the selection rectangles to their
2298c2ecf20Sopenharmony_ci     default values, based on the new resolution, as described in the next
2308c2ecf20Sopenharmony_ci     step.
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci4. Set the raw frame interval on the ``OUTPUT`` queue via
2338c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_S_PARM`. This also sets the coded frame interval on the
2348c2ecf20Sopenharmony_ci   ``CAPTURE`` queue to the same value.
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci   * ** Required fields:**
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci     ``type``
2398c2ecf20Sopenharmony_ci	 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci     ``parm.output``
2428c2ecf20Sopenharmony_ci	 set all fields except ``parm.output.timeperframe`` to 0.
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci     ``parm.output.timeperframe``
2458c2ecf20Sopenharmony_ci	 the desired frame interval; the encoder may adjust it to
2468c2ecf20Sopenharmony_ci	 match hardware requirements.
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci   * **Return fields:**
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci     ``parm.output.timeperframe``
2518c2ecf20Sopenharmony_ci	 the adjusted frame interval.
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci   .. important::
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci      Changing the ``OUTPUT`` frame interval *also* sets the framerate that
2568c2ecf20Sopenharmony_ci      the encoder uses to encode the video. So setting the frame interval
2578c2ecf20Sopenharmony_ci      to 1/24 (or 24 frames per second) will produce a coded video stream
2588c2ecf20Sopenharmony_ci      that can be played back at that speed. The frame interval for the
2598c2ecf20Sopenharmony_ci      ``OUTPUT`` queue is just a hint, the application may provide raw
2608c2ecf20Sopenharmony_ci      frames at a different rate. It can be used by the driver to help
2618c2ecf20Sopenharmony_ci      schedule multiple encoders running in parallel.
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci      In the next step the ``CAPTURE`` frame interval can optionally be
2648c2ecf20Sopenharmony_ci      changed to a different value. This is useful for off-line encoding
2658c2ecf20Sopenharmony_ci      were the coded frame interval can be different from the rate at
2668c2ecf20Sopenharmony_ci      which raw frames are supplied.
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci   .. important::
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci      ``timeperframe`` deals with *frames*, not fields. So for interlaced
2718c2ecf20Sopenharmony_ci      formats this is the time per two fields, since a frame consists of
2728c2ecf20Sopenharmony_ci      a top and a bottom field.
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci   .. note::
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci      It is due to historical reasons that changing the ``OUTPUT`` frame
2778c2ecf20Sopenharmony_ci      interval also changes the coded frame interval on the ``CAPTURE``
2788c2ecf20Sopenharmony_ci      queue. Ideally these would be independent settings, but that would
2798c2ecf20Sopenharmony_ci      break the existing API.
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci5. **Optional** Set the coded frame interval on the ``CAPTURE`` queue via
2828c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_S_PARM`. This is only necessary if the coded frame
2838c2ecf20Sopenharmony_ci   interval is different from the raw frame interval, which is typically
2848c2ecf20Sopenharmony_ci   the case for off-line encoding. Support for this feature is signalled
2858c2ecf20Sopenharmony_ci   by the :ref:`V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL <fmtdesc-flags>` format flag.
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci   * ** Required fields:**
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci     ``type``
2908c2ecf20Sopenharmony_ci	 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci     ``parm.capture``
2938c2ecf20Sopenharmony_ci	 set all fields except ``parm.capture.timeperframe`` to 0.
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci     ``parm.capture.timeperframe``
2968c2ecf20Sopenharmony_ci	 the desired coded frame interval; the encoder may adjust it to
2978c2ecf20Sopenharmony_ci	 match hardware requirements.
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci   * **Return fields:**
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci     ``parm.capture.timeperframe``
3028c2ecf20Sopenharmony_ci	 the adjusted frame interval.
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci   .. important::
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci      Changing the ``CAPTURE`` frame interval sets the framerate for the
3078c2ecf20Sopenharmony_ci      coded video. It does *not* set the rate at which buffers arrive on the
3088c2ecf20Sopenharmony_ci      ``CAPTURE`` queue, that depends on how fast the encoder is and how
3098c2ecf20Sopenharmony_ci      fast raw frames are queued on the ``OUTPUT`` queue.
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci   .. important::
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci      ``timeperframe`` deals with *frames*, not fields. So for interlaced
3148c2ecf20Sopenharmony_ci      formats this is the time per two fields, since a frame consists of
3158c2ecf20Sopenharmony_ci      a top and a bottom field.
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci   .. note::
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci      Not all drivers support this functionality, in that case just set
3208c2ecf20Sopenharmony_ci      the desired coded frame interval for the ``OUTPUT`` queue.
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci      However, drivers that can schedule multiple encoders based on the
3238c2ecf20Sopenharmony_ci      ``OUTPUT`` frame interval must support this optional feature.
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci6. **Optional.** Set the visible resolution for the stream metadata via
3268c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_S_SELECTION` on the ``OUTPUT`` queue if it is desired
3278c2ecf20Sopenharmony_ci   to be different than the full OUTPUT resolution.
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci   * **Required fields:**
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci     ``type``
3328c2ecf20Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci     ``target``
3358c2ecf20Sopenharmony_ci         set to ``V4L2_SEL_TGT_CROP``.
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci     ``r.left``, ``r.top``, ``r.width``, ``r.height``
3388c2ecf20Sopenharmony_ci         visible rectangle; this must fit within the `V4L2_SEL_TGT_CROP_BOUNDS`
3398c2ecf20Sopenharmony_ci         rectangle and may be subject to adjustment to match codec and
3408c2ecf20Sopenharmony_ci         hardware constraints.
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci   * **Return fields:**
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci     ``r.left``, ``r.top``, ``r.width``, ``r.height``
3458c2ecf20Sopenharmony_ci         visible rectangle adjusted by the encoder.
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci   * The following selection targets are supported on ``OUTPUT``:
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci     ``V4L2_SEL_TGT_CROP_BOUNDS``
3508c2ecf20Sopenharmony_ci         equal to the full source frame, matching the active ``OUTPUT``
3518c2ecf20Sopenharmony_ci         format.
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci     ``V4L2_SEL_TGT_CROP_DEFAULT``
3548c2ecf20Sopenharmony_ci         equal to ``V4L2_SEL_TGT_CROP_BOUNDS``.
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci     ``V4L2_SEL_TGT_CROP``
3578c2ecf20Sopenharmony_ci         rectangle within the source buffer to be encoded into the
3588c2ecf20Sopenharmony_ci         ``CAPTURE`` stream; defaults to ``V4L2_SEL_TGT_CROP_DEFAULT``.
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci         .. note::
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci            A common use case for this selection target is encoding a source
3638c2ecf20Sopenharmony_ci            video with a resolution that is not a multiple of a macroblock,
3648c2ecf20Sopenharmony_ci            e.g.  the common 1920x1080 resolution may require the source
3658c2ecf20Sopenharmony_ci            buffers to be aligned to 1920x1088 for codecs with 16x16 macroblock
3668c2ecf20Sopenharmony_ci            size. To avoid encoding the padding, the client needs to explicitly
3678c2ecf20Sopenharmony_ci            configure this selection target to 1920x1080.
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci   .. warning::
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci      The encoder may adjust the crop/compose rectangles to the nearest
3728c2ecf20Sopenharmony_ci      supported ones to meet codec and hardware requirements. The client needs
3738c2ecf20Sopenharmony_ci      to check the adjusted rectangle returned by :c:func:`VIDIOC_S_SELECTION`.
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci7. Allocate buffers for both ``OUTPUT`` and ``CAPTURE`` via
3768c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_REQBUFS`. This may be performed in any order.
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci   * **Required fields:**
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci     ``count``
3818c2ecf20Sopenharmony_ci         requested number of buffers to allocate; greater than zero.
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci     ``type``
3848c2ecf20Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT`` or
3858c2ecf20Sopenharmony_ci         ``CAPTURE``.
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci     other fields
3888c2ecf20Sopenharmony_ci         follow standard semantics.
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci   * **Return fields:**
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci     ``count``
3938c2ecf20Sopenharmony_ci          actual number of buffers allocated.
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci   .. warning::
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci      The actual number of allocated buffers may differ from the ``count``
3988c2ecf20Sopenharmony_ci      given. The client must check the updated value of ``count`` after the
3998c2ecf20Sopenharmony_ci      call returns.
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci   .. note::
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci      To allocate more than the minimum number of OUTPUT buffers (for pipeline
4048c2ecf20Sopenharmony_ci      depth), the client may query the ``V4L2_CID_MIN_BUFFERS_FOR_OUTPUT``
4058c2ecf20Sopenharmony_ci      control to get the minimum number of buffers required, and pass the
4068c2ecf20Sopenharmony_ci      obtained value plus the number of additional buffers needed in the
4078c2ecf20Sopenharmony_ci      ``count`` field to :c:func:`VIDIOC_REQBUFS`.
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci   Alternatively, :c:func:`VIDIOC_CREATE_BUFS` can be used to have more
4108c2ecf20Sopenharmony_ci   control over buffer allocation.
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci   * **Required fields:**
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci     ``count``
4158c2ecf20Sopenharmony_ci         requested number of buffers to allocate; greater than zero.
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci     ``type``
4188c2ecf20Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci     other fields
4218c2ecf20Sopenharmony_ci         follow standard semantics.
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci   * **Return fields:**
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci     ``count``
4268c2ecf20Sopenharmony_ci         adjusted to the number of allocated buffers.
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci8. Begin streaming on both ``OUTPUT`` and ``CAPTURE`` queues via
4298c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_STREAMON`. This may be performed in any order. The actual
4308c2ecf20Sopenharmony_ci   encoding process starts when both queues start streaming.
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci.. note::
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci   If the client stops the ``CAPTURE`` queue during the encode process and then
4358c2ecf20Sopenharmony_ci   restarts it again, the encoder will begin generating a stream independent
4368c2ecf20Sopenharmony_ci   from the stream generated before the stop. The exact constraints depend
4378c2ecf20Sopenharmony_ci   on the coded format, but may include the following implications:
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci   * encoded frames produced after the restart must not reference any
4408c2ecf20Sopenharmony_ci     frames produced before the stop, e.g. no long term references for
4418c2ecf20Sopenharmony_ci     H.264/HEVC,
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci   * any headers that must be included in a standalone stream must be
4448c2ecf20Sopenharmony_ci     produced again, e.g. SPS and PPS for H.264/HEVC.
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ciEncoding
4478c2ecf20Sopenharmony_ci========
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ciThis state is reached after the `Initialization` sequence finishes
4508c2ecf20Sopenharmony_cisuccessfully.  In this state, the client queues and dequeues buffers to both
4518c2ecf20Sopenharmony_ciqueues via :c:func:`VIDIOC_QBUF` and :c:func:`VIDIOC_DQBUF`, following the
4528c2ecf20Sopenharmony_cistandard semantics.
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ciThe content of encoded ``CAPTURE`` buffers depends on the active coded pixel
4558c2ecf20Sopenharmony_ciformat and may be affected by codec-specific extended controls, as stated
4568c2ecf20Sopenharmony_ciin the documentation of each format.
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ciBoth queues operate independently, following standard behavior of V4L2 buffer
4598c2ecf20Sopenharmony_ciqueues and memory-to-memory devices. In addition, the order of encoded frames
4608c2ecf20Sopenharmony_cidequeued from the ``CAPTURE`` queue may differ from the order of queuing raw
4618c2ecf20Sopenharmony_ciframes to the ``OUTPUT`` queue, due to properties of the selected coded format,
4628c2ecf20Sopenharmony_cie.g. frame reordering.
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ciThe client must not assume any direct relationship between ``CAPTURE`` and
4658c2ecf20Sopenharmony_ci``OUTPUT`` buffers and any specific timing of buffers becoming
4668c2ecf20Sopenharmony_ciavailable to dequeue. Specifically:
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci* a buffer queued to ``OUTPUT`` may result in more than one buffer produced on
4698c2ecf20Sopenharmony_ci  ``CAPTURE`` (for example, if returning an encoded frame allowed the encoder
4708c2ecf20Sopenharmony_ci  to return a frame that preceded it in display, but succeeded it in the decode
4718c2ecf20Sopenharmony_ci  order; however, there may be other reasons for this as well),
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci* a buffer queued to ``OUTPUT`` may result in a buffer being produced on
4748c2ecf20Sopenharmony_ci  ``CAPTURE`` later into encode process, and/or after processing further
4758c2ecf20Sopenharmony_ci  ``OUTPUT`` buffers, or be returned out of order, e.g. if display
4768c2ecf20Sopenharmony_ci  reordering is used,
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci* buffers may become available on the ``CAPTURE`` queue without additional
4798c2ecf20Sopenharmony_ci  buffers queued to ``OUTPUT`` (e.g. during drain or ``EOS``), because of the
4808c2ecf20Sopenharmony_ci  ``OUTPUT`` buffers queued in the past whose encoding results are only
4818c2ecf20Sopenharmony_ci  available at later time, due to specifics of the encoding process,
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci* buffers queued to ``OUTPUT`` may not become available to dequeue instantly
4848c2ecf20Sopenharmony_ci  after being encoded into a corresponding ``CAPTURE`` buffer, e.g. if the
4858c2ecf20Sopenharmony_ci  encoder needs to use the frame as a reference for encoding further frames.
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci.. note::
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci   To allow matching encoded ``CAPTURE`` buffers with ``OUTPUT`` buffers they
4908c2ecf20Sopenharmony_ci   originated from, the client can set the ``timestamp`` field of the
4918c2ecf20Sopenharmony_ci   :c:type:`v4l2_buffer` struct when queuing an ``OUTPUT`` buffer. The
4928c2ecf20Sopenharmony_ci   ``CAPTURE`` buffer(s), which resulted from encoding that ``OUTPUT`` buffer
4938c2ecf20Sopenharmony_ci   will have their ``timestamp`` field set to the same value when dequeued.
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci   In addition to the straightforward case of one ``OUTPUT`` buffer producing
4968c2ecf20Sopenharmony_ci   one ``CAPTURE`` buffer, the following cases are defined:
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci   * one ``OUTPUT`` buffer generates multiple ``CAPTURE`` buffers: the same
4998c2ecf20Sopenharmony_ci     ``OUTPUT`` timestamp will be copied to multiple ``CAPTURE`` buffers,
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci   * the encoding order differs from the presentation order (i.e. the
5028c2ecf20Sopenharmony_ci     ``CAPTURE`` buffers are out-of-order compared to the ``OUTPUT`` buffers):
5038c2ecf20Sopenharmony_ci     ``CAPTURE`` timestamps will not retain the order of ``OUTPUT`` timestamps.
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci.. note::
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci   To let the client distinguish between frame types (keyframes, intermediate
5088c2ecf20Sopenharmony_ci   frames; the exact list of types depends on the coded format), the
5098c2ecf20Sopenharmony_ci   ``CAPTURE`` buffers will have corresponding flag bits set in their
5108c2ecf20Sopenharmony_ci   :c:type:`v4l2_buffer` struct when dequeued. See the documentation of
5118c2ecf20Sopenharmony_ci   :c:type:`v4l2_buffer` and each coded pixel format for exact list of flags
5128c2ecf20Sopenharmony_ci   and their meanings.
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ciShould an encoding error occur, it will be reported to the client with the level
5158c2ecf20Sopenharmony_ciof details depending on the encoder capabilities. Specifically:
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci* the ``CAPTURE`` buffer (if any) that contains the results of the failed encode
5188c2ecf20Sopenharmony_ci  operation will be returned with the ``V4L2_BUF_FLAG_ERROR`` flag set,
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci* if the encoder is able to precisely report the ``OUTPUT`` buffer(s) that triggered
5218c2ecf20Sopenharmony_ci  the error, such buffer(s) will be returned with the ``V4L2_BUF_FLAG_ERROR`` flag
5228c2ecf20Sopenharmony_ci  set.
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci.. note::
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci   If a ``CAPTURE`` buffer is too small then it is just returned with the
5278c2ecf20Sopenharmony_ci   ``V4L2_BUF_FLAG_ERROR`` flag set. More work is needed to detect that this
5288c2ecf20Sopenharmony_ci   error occurred because the buffer was too small, and to provide support to
5298c2ecf20Sopenharmony_ci   free existing buffers that were too small.
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ciIn case of a fatal failure that does not allow the encoding to continue, any
5328c2ecf20Sopenharmony_cifurther operations on corresponding encoder file handle will return the -EIO
5338c2ecf20Sopenharmony_cierror code. The client may close the file handle and open a new one, or
5348c2ecf20Sopenharmony_cialternatively reinitialize the instance by stopping streaming on both queues,
5358c2ecf20Sopenharmony_cireleasing all buffers and performing the Initialization sequence again.
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ciEncoding Parameter Changes
5388c2ecf20Sopenharmony_ci==========================
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ciThe client is allowed to use :c:func:`VIDIOC_S_CTRL` to change encoder
5418c2ecf20Sopenharmony_ciparameters at any time. The availability of parameters is encoder-specific
5428c2ecf20Sopenharmony_ciand the client must query the encoder to find the set of available controls.
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ciThe ability to change each parameter during encoding is encoder-specific, as
5458c2ecf20Sopenharmony_ciper the standard semantics of the V4L2 control interface. The client may
5468c2ecf20Sopenharmony_ciattempt to set a control during encoding and if the operation fails with the
5478c2ecf20Sopenharmony_ci-EBUSY error code, the ``CAPTURE`` queue needs to be stopped for the
5488c2ecf20Sopenharmony_ciconfiguration change to be allowed. To do this, it may follow the `Drain`
5498c2ecf20Sopenharmony_cisequence to avoid losing the already queued/encoded frames.
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ciThe timing of parameter updates is encoder-specific, as per the standard
5528c2ecf20Sopenharmony_cisemantics of the V4L2 control interface. If the client needs to apply the
5538c2ecf20Sopenharmony_ciparameters exactly at specific frame, using the Request API
5548c2ecf20Sopenharmony_ci(:ref:`media-request-api`) should be considered, if supported by the encoder.
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ciDrain
5578c2ecf20Sopenharmony_ci=====
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ciTo ensure that all the queued ``OUTPUT`` buffers have been processed and the
5608c2ecf20Sopenharmony_cirelated ``CAPTURE`` buffers are given to the client, the client must follow the
5618c2ecf20Sopenharmony_cidrain sequence described below. After the drain sequence ends, the client has
5628c2ecf20Sopenharmony_cireceived all encoded frames for all ``OUTPUT`` buffers queued before the
5638c2ecf20Sopenharmony_cisequence was started.
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci1. Begin the drain sequence by issuing :c:func:`VIDIOC_ENCODER_CMD`.
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci   * **Required fields:**
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci     ``cmd``
5708c2ecf20Sopenharmony_ci         set to ``V4L2_ENC_CMD_STOP``.
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci     ``flags``
5738c2ecf20Sopenharmony_ci         set to 0.
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci     ``pts``
5768c2ecf20Sopenharmony_ci         set to 0.
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci   .. warning::
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci      The sequence can be only initiated if both ``OUTPUT`` and ``CAPTURE``
5818c2ecf20Sopenharmony_ci      queues are streaming. For compatibility reasons, the call to
5828c2ecf20Sopenharmony_ci      :c:func:`VIDIOC_ENCODER_CMD` will not fail even if any of the queues is
5838c2ecf20Sopenharmony_ci      not streaming, but at the same time it will not initiate the `Drain`
5848c2ecf20Sopenharmony_ci      sequence and so the steps described below would not be applicable.
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci2. Any ``OUTPUT`` buffers queued by the client before the
5878c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_ENCODER_CMD` was issued will be processed and encoded as
5888c2ecf20Sopenharmony_ci   normal. The client must continue to handle both queues independently,
5898c2ecf20Sopenharmony_ci   similarly to normal encode operation. This includes:
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci   * queuing and dequeuing ``CAPTURE`` buffers, until a buffer marked with the
5928c2ecf20Sopenharmony_ci     ``V4L2_BUF_FLAG_LAST`` flag is dequeued,
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci     .. warning::
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci        The last buffer may be empty (with :c:type:`v4l2_buffer`
5978c2ecf20Sopenharmony_ci        ``bytesused`` = 0) and in that case it must be ignored by the client,
5988c2ecf20Sopenharmony_ci        as it does not contain an encoded frame.
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci     .. note::
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci        Any attempt to dequeue more ``CAPTURE`` buffers beyond the buffer
6038c2ecf20Sopenharmony_ci        marked with ``V4L2_BUF_FLAG_LAST`` will result in a -EPIPE error from
6048c2ecf20Sopenharmony_ci        :c:func:`VIDIOC_DQBUF`.
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci   * dequeuing processed ``OUTPUT`` buffers, until all the buffers queued
6078c2ecf20Sopenharmony_ci     before the ``V4L2_ENC_CMD_STOP`` command are dequeued,
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci   * dequeuing the ``V4L2_EVENT_EOS`` event, if the client subscribes to it.
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci   .. note::
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci      For backwards compatibility, the encoder will signal a ``V4L2_EVENT_EOS``
6148c2ecf20Sopenharmony_ci      event when the last frame has been encoded and all frames are ready to be
6158c2ecf20Sopenharmony_ci      dequeued. It is deprecated behavior and the client must not rely on it.
6168c2ecf20Sopenharmony_ci      The ``V4L2_BUF_FLAG_LAST`` buffer flag should be used instead.
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci3. Once all ``OUTPUT`` buffers queued before the ``V4L2_ENC_CMD_STOP`` call are
6198c2ecf20Sopenharmony_ci   dequeued and the last ``CAPTURE`` buffer is dequeued, the encoder is stopped
6208c2ecf20Sopenharmony_ci   and it will accept, but not process any newly queued ``OUTPUT`` buffers
6218c2ecf20Sopenharmony_ci   until the client issues any of the following operations:
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci   * ``V4L2_ENC_CMD_START`` - the encoder will not be reset and will resume
6248c2ecf20Sopenharmony_ci     operation normally, with all the state from before the drain,
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci   * a pair of :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the
6278c2ecf20Sopenharmony_ci     ``CAPTURE`` queue - the encoder will be reset (see the `Reset` sequence)
6288c2ecf20Sopenharmony_ci     and then resume encoding,
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci   * a pair of :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the
6318c2ecf20Sopenharmony_ci     ``OUTPUT`` queue - the encoder will resume operation normally, however any
6328c2ecf20Sopenharmony_ci     source frames queued to the ``OUTPUT`` queue between ``V4L2_ENC_CMD_STOP``
6338c2ecf20Sopenharmony_ci     and :c:func:`VIDIOC_STREAMOFF` will be discarded.
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci.. note::
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci   Once the drain sequence is initiated, the client needs to drive it to
6388c2ecf20Sopenharmony_ci   completion, as described by the steps above, unless it aborts the process by
6398c2ecf20Sopenharmony_ci   issuing :c:func:`VIDIOC_STREAMOFF` on any of the ``OUTPUT`` or ``CAPTURE``
6408c2ecf20Sopenharmony_ci   queues.  The client is not allowed to issue ``V4L2_ENC_CMD_START`` or
6418c2ecf20Sopenharmony_ci   ``V4L2_ENC_CMD_STOP`` again while the drain sequence is in progress and they
6428c2ecf20Sopenharmony_ci   will fail with -EBUSY error code if attempted.
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci   For reference, handling of various corner cases is described below:
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci   * In case of no buffer in the ``OUTPUT`` queue at the time the
6478c2ecf20Sopenharmony_ci     ``V4L2_ENC_CMD_STOP`` command was issued, the drain sequence completes
6488c2ecf20Sopenharmony_ci     immediately and the encoder returns an empty ``CAPTURE`` buffer with the
6498c2ecf20Sopenharmony_ci     ``V4L2_BUF_FLAG_LAST`` flag set.
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci   * In case of no buffer in the ``CAPTURE`` queue at the time the drain
6528c2ecf20Sopenharmony_ci     sequence completes, the next time the client queues a ``CAPTURE`` buffer
6538c2ecf20Sopenharmony_ci     it is returned at once as an empty buffer with the ``V4L2_BUF_FLAG_LAST``
6548c2ecf20Sopenharmony_ci     flag set.
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci   * If :c:func:`VIDIOC_STREAMOFF` is called on the ``CAPTURE`` queue in the
6578c2ecf20Sopenharmony_ci     middle of the drain sequence, the drain sequence is canceled and all
6588c2ecf20Sopenharmony_ci     ``CAPTURE`` buffers are implicitly returned to the client.
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci   * If :c:func:`VIDIOC_STREAMOFF` is called on the ``OUTPUT`` queue in the
6618c2ecf20Sopenharmony_ci     middle of the drain sequence, the drain sequence completes immediately and
6628c2ecf20Sopenharmony_ci     next ``CAPTURE`` buffer will be returned empty with the
6638c2ecf20Sopenharmony_ci     ``V4L2_BUF_FLAG_LAST`` flag set.
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci   Although not mandatory, the availability of encoder commands may be queried
6668c2ecf20Sopenharmony_ci   using :c:func:`VIDIOC_TRY_ENCODER_CMD`.
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ciReset
6698c2ecf20Sopenharmony_ci=====
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ciThe client may want to request the encoder to reinitialize the encoding, so
6728c2ecf20Sopenharmony_cithat the following stream data becomes independent from the stream data
6738c2ecf20Sopenharmony_cigenerated before. Depending on the coded format, that may imply that:
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci* encoded frames produced after the restart must not reference any frames
6768c2ecf20Sopenharmony_ci  produced before the stop, e.g. no long term references for H.264/HEVC,
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci* any headers that must be included in a standalone stream must be produced
6798c2ecf20Sopenharmony_ci  again, e.g. SPS and PPS for H.264/HEVC.
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ciThis can be achieved by performing the reset sequence.
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci1. Perform the `Drain` sequence to ensure all the in-flight encoding finishes
6848c2ecf20Sopenharmony_ci   and respective buffers are dequeued.
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci2. Stop streaming on the ``CAPTURE`` queue via :c:func:`VIDIOC_STREAMOFF`. This
6878c2ecf20Sopenharmony_ci   will return all currently queued ``CAPTURE`` buffers to the client, without
6888c2ecf20Sopenharmony_ci   valid frame data.
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci3. Start streaming on the ``CAPTURE`` queue via :c:func:`VIDIOC_STREAMON` and
6918c2ecf20Sopenharmony_ci   continue with regular encoding sequence. The encoded frames produced into
6928c2ecf20Sopenharmony_ci   ``CAPTURE`` buffers from now on will contain a standalone stream that can be
6938c2ecf20Sopenharmony_ci   decoded without the need for frames encoded before the reset sequence,
6948c2ecf20Sopenharmony_ci   starting at the first ``OUTPUT`` buffer queued after issuing the
6958c2ecf20Sopenharmony_ci   `V4L2_ENC_CMD_STOP` of the `Drain` sequence.
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ciThis sequence may be also used to change encoding parameters for encoders
6988c2ecf20Sopenharmony_ciwithout the ability to change the parameters on the fly.
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ciCommit Points
7018c2ecf20Sopenharmony_ci=============
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ciSetting formats and allocating buffers triggers changes in the behavior of the
7048c2ecf20Sopenharmony_ciencoder.
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci1. Setting the format on the ``CAPTURE`` queue may change the set of formats
7078c2ecf20Sopenharmony_ci   supported/advertised on the ``OUTPUT`` queue. In particular, it also means
7088c2ecf20Sopenharmony_ci   that the ``OUTPUT`` format may be reset and the client must not rely on the
7098c2ecf20Sopenharmony_ci   previously set format being preserved.
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci2. Enumerating formats on the ``OUTPUT`` queue always returns only formats
7128c2ecf20Sopenharmony_ci   supported for the current ``CAPTURE`` format.
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci3. Setting the format on the ``OUTPUT`` queue does not change the list of
7158c2ecf20Sopenharmony_ci   formats available on the ``CAPTURE`` queue. An attempt to set the ``OUTPUT``
7168c2ecf20Sopenharmony_ci   format that is not supported for the currently selected ``CAPTURE`` format
7178c2ecf20Sopenharmony_ci   will result in the encoder adjusting the requested ``OUTPUT`` format to a
7188c2ecf20Sopenharmony_ci   supported one.
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci4. Enumerating formats on the ``CAPTURE`` queue always returns the full set of
7218c2ecf20Sopenharmony_ci   supported coded formats, irrespective of the current ``OUTPUT`` format.
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci5. While buffers are allocated on any of the ``OUTPUT`` or ``CAPTURE`` queues,
7248c2ecf20Sopenharmony_ci   the client must not change the format on the ``CAPTURE`` queue. Drivers will
7258c2ecf20Sopenharmony_ci   return the -EBUSY error code for any such format change attempt.
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ciTo summarize, setting formats and allocation must always start with the
7288c2ecf20Sopenharmony_ci``CAPTURE`` queue and the ``CAPTURE`` queue is the master that governs the
7298c2ecf20Sopenharmony_ciset of supported formats for the ``OUTPUT`` queue.
730