18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci.. _stateless_decoder:
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci**************************************************
68c2ecf20Sopenharmony_ciMemory-to-memory Stateless Video Decoder Interface
78c2ecf20Sopenharmony_ci**************************************************
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciA stateless decoder is a decoder that works without retaining any kind of state
108c2ecf20Sopenharmony_cibetween processed frames. This means that each frame is decoded independently
118c2ecf20Sopenharmony_ciof any previous and future frames, and that the client is responsible for
128c2ecf20Sopenharmony_cimaintaining the decoding state and providing it to the decoder with each
138c2ecf20Sopenharmony_cidecoding request. This is in contrast to the stateful video decoder interface,
148c2ecf20Sopenharmony_ciwhere the hardware and driver maintain the decoding state and all the client
158c2ecf20Sopenharmony_cihas to do is to provide the raw encoded stream and dequeue decoded frames in
168c2ecf20Sopenharmony_cidisplay order.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciThis section describes how user-space ("the client") is expected to communicate
198c2ecf20Sopenharmony_ciwith stateless decoders in order to successfully decode an encoded stream.
208c2ecf20Sopenharmony_ciCompared to stateful codecs, the decoder/client sequence is simpler, but the
218c2ecf20Sopenharmony_cicost of this simplicity is extra complexity in the client which is responsible
228c2ecf20Sopenharmony_cifor maintaining a consistent decoding state.
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciStateless decoders make use of the :ref:`media-request-api`. A stateless
258c2ecf20Sopenharmony_cidecoder must expose the ``V4L2_BUF_CAP_SUPPORTS_REQUESTS`` capability on its
268c2ecf20Sopenharmony_ci``OUTPUT`` queue when :c:func:`VIDIOC_REQBUFS` or :c:func:`VIDIOC_CREATE_BUFS`
278c2ecf20Sopenharmony_ciare invoked.
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciDepending on the encoded formats supported by the decoder, a single decoded
308c2ecf20Sopenharmony_ciframe may be the result of several decode requests (for instance, H.264 streams
318c2ecf20Sopenharmony_ciwith multiple slices per frame). Decoders that support such formats must also
328c2ecf20Sopenharmony_ciexpose the ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF`` capability on their
338c2ecf20Sopenharmony_ci``OUTPUT`` queue.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciQuerying capabilities
368c2ecf20Sopenharmony_ci=====================
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci1. To enumerate the set of coded formats supported by the decoder, the client
398c2ecf20Sopenharmony_ci   calls :c:func:`VIDIOC_ENUM_FMT` on the ``OUTPUT`` queue.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci   * The driver must always return the full set of supported ``OUTPUT`` formats,
428c2ecf20Sopenharmony_ci     irrespective of the format currently set on the ``CAPTURE`` queue.
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci   * Simultaneously, the driver must restrain the set of values returned by
458c2ecf20Sopenharmony_ci     codec-specific capability controls (such as H.264 profiles) to the set
468c2ecf20Sopenharmony_ci     actually supported by the hardware.
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci2. To enumerate the set of supported raw formats, the client calls
498c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_ENUM_FMT` on the ``CAPTURE`` queue.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci   * The driver must return only the formats supported for the format currently
528c2ecf20Sopenharmony_ci     active on the ``OUTPUT`` queue.
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci   * Depending on the currently set ``OUTPUT`` format, the set of supported raw
558c2ecf20Sopenharmony_ci     formats may depend on the value of some codec-dependent controls.
568c2ecf20Sopenharmony_ci     The client is responsible for making sure that these controls are set
578c2ecf20Sopenharmony_ci     before querying the ``CAPTURE`` queue. Failure to do so will result in the
588c2ecf20Sopenharmony_ci     default values for these controls being used, and a returned set of formats
598c2ecf20Sopenharmony_ci     that may not be usable for the media the client is trying to decode.
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci3. The client may use :c:func:`VIDIOC_ENUM_FRAMESIZES` to detect supported
628c2ecf20Sopenharmony_ci   resolutions for a given format, passing desired pixel format in
638c2ecf20Sopenharmony_ci   :c:type:`v4l2_frmsizeenum`'s ``pixel_format``.
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci4. Supported profiles and levels for the current ``OUTPUT`` format, if
668c2ecf20Sopenharmony_ci   applicable, may be queried using their respective controls via
678c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_QUERYCTRL`.
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciInitialization
708c2ecf20Sopenharmony_ci==============
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci1. Set the coded format on the ``OUTPUT`` queue via :c:func:`VIDIOC_S_FMT`.
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci   * **Required fields:**
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci     ``type``
778c2ecf20Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci     ``pixelformat``
808c2ecf20Sopenharmony_ci         a coded pixel format.
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci     ``width``, ``height``
838c2ecf20Sopenharmony_ci         coded width and height parsed from the stream.
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci     other fields
868c2ecf20Sopenharmony_ci         follow standard semantics.
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci   .. note::
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci      Changing the ``OUTPUT`` format may change the currently set ``CAPTURE``
918c2ecf20Sopenharmony_ci      format. The driver will derive a new ``CAPTURE`` format from the
928c2ecf20Sopenharmony_ci      ``OUTPUT`` format being set, including resolution, colorimetry
938c2ecf20Sopenharmony_ci      parameters, etc. If the client needs a specific ``CAPTURE`` format,
948c2ecf20Sopenharmony_ci      it must adjust it afterwards.
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci2. Call :c:func:`VIDIOC_S_EXT_CTRLS` to set all the controls (parsed headers,
978c2ecf20Sopenharmony_ci   etc.) required by the ``OUTPUT`` format to enumerate the ``CAPTURE`` formats.
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci3. Call :c:func:`VIDIOC_G_FMT` for ``CAPTURE`` queue to get the format for the
1008c2ecf20Sopenharmony_ci   destination buffers parsed/decoded from the bytestream.
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci   * **Required fields:**
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci     ``type``
1058c2ecf20Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci   * **Returned fields:**
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci     ``width``, ``height``
1108c2ecf20Sopenharmony_ci         frame buffer resolution for the decoded frames.
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci     ``pixelformat``
1138c2ecf20Sopenharmony_ci         pixel format for decoded frames.
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci     ``num_planes`` (for _MPLANE ``type`` only)
1168c2ecf20Sopenharmony_ci         number of planes for pixelformat.
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci     ``sizeimage``, ``bytesperline``
1198c2ecf20Sopenharmony_ci         as per standard semantics; matching frame buffer format.
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci   .. note::
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci      The value of ``pixelformat`` may be any pixel format supported for the
1248c2ecf20Sopenharmony_ci      ``OUTPUT`` format, based on the hardware capabilities. It is suggested
1258c2ecf20Sopenharmony_ci      that the driver chooses the preferred/optimal format for the current
1268c2ecf20Sopenharmony_ci      configuration. For example, a YUV format may be preferred over an RGB
1278c2ecf20Sopenharmony_ci      format, if an additional conversion step would be required for RGB.
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci4. *[optional]* Enumerate ``CAPTURE`` formats via :c:func:`VIDIOC_ENUM_FMT` on
1308c2ecf20Sopenharmony_ci   the ``CAPTURE`` queue. The client may use this ioctl to discover which
1318c2ecf20Sopenharmony_ci   alternative raw formats are supported for the current ``OUTPUT`` format and
1328c2ecf20Sopenharmony_ci   select one of them via :c:func:`VIDIOC_S_FMT`.
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci   .. note::
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci      The driver will return only formats supported for the currently selected
1378c2ecf20Sopenharmony_ci      ``OUTPUT`` format and currently set controls, even if more formats may be
1388c2ecf20Sopenharmony_ci      supported by the decoder in general.
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci      For example, a decoder may support YUV and RGB formats for
1418c2ecf20Sopenharmony_ci      resolutions 1920x1088 and lower, but only YUV for higher resolutions (due
1428c2ecf20Sopenharmony_ci      to hardware limitations). After setting a resolution of 1920x1088 or lower
1438c2ecf20Sopenharmony_ci      as the ``OUTPUT`` format, :c:func:`VIDIOC_ENUM_FMT` may return a set of
1448c2ecf20Sopenharmony_ci      YUV and RGB pixel formats, but after setting a resolution higher than
1458c2ecf20Sopenharmony_ci      1920x1088, the driver will not return RGB pixel formats, since they are
1468c2ecf20Sopenharmony_ci      unsupported for this resolution.
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci5. *[optional]* Choose a different ``CAPTURE`` format than suggested via
1498c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_S_FMT` on ``CAPTURE`` queue. It is possible for the client to
1508c2ecf20Sopenharmony_ci   choose a different format than selected/suggested by the driver in
1518c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_G_FMT`.
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci    * **Required fields:**
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci      ``type``
1568c2ecf20Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci      ``pixelformat``
1598c2ecf20Sopenharmony_ci          a raw pixel format.
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci      ``width``, ``height``
1628c2ecf20Sopenharmony_ci         frame buffer resolution of the decoded stream; typically unchanged from
1638c2ecf20Sopenharmony_ci         what was returned with :c:func:`VIDIOC_G_FMT`, but it may be different
1648c2ecf20Sopenharmony_ci         if the hardware supports composition and/or scaling.
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci   After performing this step, the client must perform step 3 again in order
1678c2ecf20Sopenharmony_ci   to obtain up-to-date information about the buffers size and layout.
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci6. Allocate source (bytestream) buffers via :c:func:`VIDIOC_REQBUFS` on
1708c2ecf20Sopenharmony_ci   ``OUTPUT`` queue.
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci    * **Required fields:**
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci      ``count``
1758c2ecf20Sopenharmony_ci          requested number of buffers to allocate; greater than zero.
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci      ``type``
1788c2ecf20Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci      ``memory``
1818c2ecf20Sopenharmony_ci          follows standard semantics.
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci    * **Return fields:**
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci      ``count``
1868c2ecf20Sopenharmony_ci          actual number of buffers allocated.
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci    * If required, the driver will adjust ``count`` to be equal or bigger to the
1898c2ecf20Sopenharmony_ci      minimum of required number of ``OUTPUT`` buffers for the given format and
1908c2ecf20Sopenharmony_ci      requested count. The client must check this value after the ioctl returns
1918c2ecf20Sopenharmony_ci      to get the actual number of buffers allocated.
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci7. Allocate destination (raw format) buffers via :c:func:`VIDIOC_REQBUFS` on the
1948c2ecf20Sopenharmony_ci   ``CAPTURE`` queue.
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci    * **Required fields:**
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci      ``count``
1998c2ecf20Sopenharmony_ci          requested number of buffers to allocate; greater than zero. The client
2008c2ecf20Sopenharmony_ci          is responsible for deducing the minimum number of buffers required
2018c2ecf20Sopenharmony_ci          for the stream to be properly decoded (taking e.g. reference frames
2028c2ecf20Sopenharmony_ci          into account) and pass an equal or bigger number.
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci      ``type``
2058c2ecf20Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci      ``memory``
2088c2ecf20Sopenharmony_ci          follows standard semantics. ``V4L2_MEMORY_USERPTR`` is not supported
2098c2ecf20Sopenharmony_ci          for ``CAPTURE`` buffers.
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci    * **Return fields:**
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci      ``count``
2148c2ecf20Sopenharmony_ci          adjusted to allocated number of buffers, in case the codec requires
2158c2ecf20Sopenharmony_ci          more buffers than requested.
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci    * The driver must adjust count to the minimum of required number of
2188c2ecf20Sopenharmony_ci      ``CAPTURE`` buffers for the current format, stream configuration and
2198c2ecf20Sopenharmony_ci      requested count. The client must check this value after the ioctl
2208c2ecf20Sopenharmony_ci      returns to get the number of buffers allocated.
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci8. Allocate requests (likely one per ``OUTPUT`` buffer) via
2238c2ecf20Sopenharmony_ci    :c:func:`MEDIA_IOC_REQUEST_ALLOC` on the media device.
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci9. Start streaming on both ``OUTPUT`` and ``CAPTURE`` queues via
2268c2ecf20Sopenharmony_ci    :c:func:`VIDIOC_STREAMON`.
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciDecoding
2298c2ecf20Sopenharmony_ci========
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ciFor each frame, the client is responsible for submitting at least one request to
2328c2ecf20Sopenharmony_ciwhich the following is attached:
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci* The amount of encoded data expected by the codec for its current
2358c2ecf20Sopenharmony_ci  configuration, as a buffer submitted to the ``OUTPUT`` queue. Typically, this
2368c2ecf20Sopenharmony_ci  corresponds to one frame worth of encoded data, but some formats may allow (or
2378c2ecf20Sopenharmony_ci  require) different amounts per unit.
2388c2ecf20Sopenharmony_ci* All the metadata needed to decode the submitted encoded data, in the form of
2398c2ecf20Sopenharmony_ci  controls relevant to the format being decoded.
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciThe amount of data and contents of the source ``OUTPUT`` buffer, as well as the
2428c2ecf20Sopenharmony_cicontrols that must be set on the request, depend on the active coded pixel
2438c2ecf20Sopenharmony_ciformat and might be affected by codec-specific extended controls, as stated in
2448c2ecf20Sopenharmony_cidocumentation of each format.
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ciIf there is a possibility that the decoded frame will require one or more
2478c2ecf20Sopenharmony_cidecode requests after the current one in order to be produced, then the client
2488c2ecf20Sopenharmony_cimust set the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag on the ``OUTPUT``
2498c2ecf20Sopenharmony_cibuffer. This will result in the (potentially partially) decoded ``CAPTURE``
2508c2ecf20Sopenharmony_cibuffer not being made available for dequeueing, and reused for the next decode
2518c2ecf20Sopenharmony_cirequest if the timestamp of the next ``OUTPUT`` buffer has not changed.
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ciA typical frame would thus be decoded using the following sequence:
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci1. Queue an ``OUTPUT`` buffer containing one unit of encoded bytestream data for
2568c2ecf20Sopenharmony_ci   the decoding request, using :c:func:`VIDIOC_QBUF`.
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci    * **Required fields:**
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci      ``index``
2618c2ecf20Sopenharmony_ci          index of the buffer being queued.
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci      ``type``
2648c2ecf20Sopenharmony_ci          type of the buffer.
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci      ``bytesused``
2678c2ecf20Sopenharmony_ci          number of bytes taken by the encoded data frame in the buffer.
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci      ``flags``
2708c2ecf20Sopenharmony_ci          the ``V4L2_BUF_FLAG_REQUEST_FD`` flag must be set. Additionally, if
2718c2ecf20Sopenharmony_ci          we are not sure that the current decode request is the last one needed
2728c2ecf20Sopenharmony_ci          to produce a fully decoded frame, then
2738c2ecf20Sopenharmony_ci          ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` must also be set.
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci      ``request_fd``
2768c2ecf20Sopenharmony_ci          must be set to the file descriptor of the decoding request.
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci      ``timestamp``
2798c2ecf20Sopenharmony_ci          must be set to a unique value per frame. This value will be propagated
2808c2ecf20Sopenharmony_ci          into the decoded frame's buffer and can also be used to use this frame
2818c2ecf20Sopenharmony_ci          as the reference of another. If using multiple decode requests per
2828c2ecf20Sopenharmony_ci          frame, then the timestamps of all the ``OUTPUT`` buffers for a given
2838c2ecf20Sopenharmony_ci          frame must be identical. If the timestamp changes, then the currently
2848c2ecf20Sopenharmony_ci          held ``CAPTURE`` buffer will be made available for dequeuing and the
2858c2ecf20Sopenharmony_ci          current request will work on a new ``CAPTURE`` buffer.
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci2. Set the codec-specific controls for the decoding request, using
2888c2ecf20Sopenharmony_ci   :c:func:`VIDIOC_S_EXT_CTRLS`.
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci    * **Required fields:**
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci      ``which``
2938c2ecf20Sopenharmony_ci          must be ``V4L2_CTRL_WHICH_REQUEST_VAL``.
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci      ``request_fd``
2968c2ecf20Sopenharmony_ci          must be set to the file descriptor of the decoding request.
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci      other fields
2998c2ecf20Sopenharmony_ci          other fields are set as usual when setting controls. The ``controls``
3008c2ecf20Sopenharmony_ci          array must contain all the codec-specific controls required to decode
3018c2ecf20Sopenharmony_ci          a frame.
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci   .. note::
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci      It is possible to specify the controls in different invocations of
3068c2ecf20Sopenharmony_ci      :c:func:`VIDIOC_S_EXT_CTRLS`, or to overwrite a previously set control, as
3078c2ecf20Sopenharmony_ci      long as ``request_fd`` and ``which`` are properly set. The controls state
3088c2ecf20Sopenharmony_ci      at the moment of request submission is the one that will be considered.
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci   .. note::
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci      The order in which steps 1 and 2 take place is interchangeable.
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci3. Submit the request by invoking :c:func:`MEDIA_REQUEST_IOC_QUEUE` on the
3158c2ecf20Sopenharmony_ci   request FD.
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci    If the request is submitted without an ``OUTPUT`` buffer, or if some of the
3188c2ecf20Sopenharmony_ci    required controls are missing from the request, then
3198c2ecf20Sopenharmony_ci    :c:func:`MEDIA_REQUEST_IOC_QUEUE` will return ``-ENOENT``. If more than one
3208c2ecf20Sopenharmony_ci    ``OUTPUT`` buffer is queued, then it will return ``-EINVAL``.
3218c2ecf20Sopenharmony_ci    :c:func:`MEDIA_REQUEST_IOC_QUEUE` returning non-zero means that no
3228c2ecf20Sopenharmony_ci    ``CAPTURE`` buffer will be produced for this request.
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci``CAPTURE`` buffers must not be part of the request, and are queued
3258c2ecf20Sopenharmony_ciindependently. They are returned in decode order (i.e. the same order as coded
3268c2ecf20Sopenharmony_ciframes were submitted to the ``OUTPUT`` queue).
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ciRuntime decoding errors are signaled by the dequeued ``CAPTURE`` buffers
3298c2ecf20Sopenharmony_cicarrying the ``V4L2_BUF_FLAG_ERROR`` flag. If a decoded reference frame has an
3308c2ecf20Sopenharmony_cierror, then all following decoded frames that refer to it also have the
3318c2ecf20Sopenharmony_ci``V4L2_BUF_FLAG_ERROR`` flag set, although the decoder will still try to
3328c2ecf20Sopenharmony_ciproduce (likely corrupted) frames.
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ciBuffer management while decoding
3358c2ecf20Sopenharmony_ci================================
3368c2ecf20Sopenharmony_ciContrary to stateful decoders, a stateless decoder does not perform any kind of
3378c2ecf20Sopenharmony_cibuffer management: it only guarantees that dequeued ``CAPTURE`` buffers can be
3388c2ecf20Sopenharmony_ciused by the client for as long as they are not queued again. "Used" here
3398c2ecf20Sopenharmony_ciencompasses using the buffer for compositing or display.
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ciA dequeued capture buffer can also be used as the reference frame of another
3428c2ecf20Sopenharmony_cibuffer.
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ciA frame is specified as reference by converting its timestamp into nanoseconds,
3458c2ecf20Sopenharmony_ciand storing it into the relevant member of a codec-dependent control structure.
3468c2ecf20Sopenharmony_ciThe :c:func:`v4l2_timeval_to_ns` function must be used to perform that
3478c2ecf20Sopenharmony_ciconversion. The timestamp of a frame can be used to reference it as soon as all
3488c2ecf20Sopenharmony_ciits units of encoded data are successfully submitted to the ``OUTPUT`` queue.
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ciA decoded buffer containing a reference frame must not be reused as a decoding
3518c2ecf20Sopenharmony_citarget until all the frames referencing it have been decoded. The safest way to
3528c2ecf20Sopenharmony_ciachieve this is to refrain from queueing a reference buffer until all the
3538c2ecf20Sopenharmony_cidecoded frames referencing it have been dequeued. However, if the driver can
3548c2ecf20Sopenharmony_ciguarantee that buffers queued to the ``CAPTURE`` queue are processed in queued
3558c2ecf20Sopenharmony_ciorder, then user-space can take advantage of this guarantee and queue a
3568c2ecf20Sopenharmony_cireference buffer when the following conditions are met:
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci1. All the requests for frames affected by the reference frame have been
3598c2ecf20Sopenharmony_ci   queued, and
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci2. A sufficient number of ``CAPTURE`` buffers to cover all the decoded
3628c2ecf20Sopenharmony_ci   referencing frames have been queued.
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ciWhen queuing a decoding request, the driver will increase the reference count of
3658c2ecf20Sopenharmony_ciall the resources associated with reference frames. This means that the client
3668c2ecf20Sopenharmony_cican e.g. close the DMABUF file descriptors of reference frame buffers if it
3678c2ecf20Sopenharmony_ciwon't need them afterwards.
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ciSeeking
3708c2ecf20Sopenharmony_ci=======
3718c2ecf20Sopenharmony_ciIn order to seek, the client just needs to submit requests using input buffers
3728c2ecf20Sopenharmony_cicorresponding to the new stream position. It must however be aware that
3738c2ecf20Sopenharmony_ciresolution may have changed and follow the dynamic resolution change sequence in
3748c2ecf20Sopenharmony_cithat case. Also depending on the codec used, picture parameters (e.g. SPS/PPS
3758c2ecf20Sopenharmony_cifor H.264) may have changed and the client is responsible for making sure that a
3768c2ecf20Sopenharmony_civalid state is sent to the decoder.
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ciThe client is then free to ignore any returned ``CAPTURE`` buffer that comes
3798c2ecf20Sopenharmony_cifrom the pre-seek position.
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ciPausing
3828c2ecf20Sopenharmony_ci=======
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ciIn order to pause, the client can just cease queuing buffers onto the ``OUTPUT``
3858c2ecf20Sopenharmony_ciqueue. Without source bytestream data, there is no data to process and the codec
3868c2ecf20Sopenharmony_ciwill remain idle.
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ciDynamic resolution change
3898c2ecf20Sopenharmony_ci=========================
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ciIf the client detects a resolution change in the stream, it will need to perform
3928c2ecf20Sopenharmony_cithe initialization sequence again with the new resolution:
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci1. If the last submitted request resulted in a ``CAPTURE`` buffer being
3958c2ecf20Sopenharmony_ci   held by the use of the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag, then the
3968c2ecf20Sopenharmony_ci   last frame is not available on the ``CAPTURE`` queue. In this case, a
3978c2ecf20Sopenharmony_ci   ``V4L2_DEC_CMD_FLUSH`` command shall be sent. This will make the driver
3988c2ecf20Sopenharmony_ci   dequeue the held ``CAPTURE`` buffer.
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci2. Wait until all submitted requests have completed and dequeue the
4018c2ecf20Sopenharmony_ci   corresponding output buffers.
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci3. Call :c:func:`VIDIOC_STREAMOFF` on both the ``OUTPUT`` and ``CAPTURE``
4048c2ecf20Sopenharmony_ci   queues.
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci4. Free all ``CAPTURE`` buffers by calling :c:func:`VIDIOC_REQBUFS` on the
4078c2ecf20Sopenharmony_ci   ``CAPTURE`` queue with a buffer count of zero.
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci5. Perform the initialization sequence again (minus the allocation of
4108c2ecf20Sopenharmony_ci   ``OUTPUT`` buffers), with the new resolution set on the ``OUTPUT`` queue.
4118c2ecf20Sopenharmony_ci   Note that due to resolution constraints, a different format may need to be
4128c2ecf20Sopenharmony_ci   picked on the ``CAPTURE`` queue.
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ciDrain
4158c2ecf20Sopenharmony_ci=====
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ciIf the last submitted request resulted in a ``CAPTURE`` buffer being
4188c2ecf20Sopenharmony_ciheld by the use of the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag, then the
4198c2ecf20Sopenharmony_cilast frame is not available on the ``CAPTURE`` queue. In this case, a
4208c2ecf20Sopenharmony_ci``V4L2_DEC_CMD_FLUSH`` command shall be sent. This will make the driver
4218c2ecf20Sopenharmony_cidequeue the held ``CAPTURE`` buffer.
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ciAfter that, in order to drain the stream on a stateless decoder, the client
4248c2ecf20Sopenharmony_cijust needs to wait until all the submitted requests are completed.
425