162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci.. _stateless_decoder:
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci**************************************************
662306a36Sopenharmony_ciMemory-to-memory Stateless Video Decoder Interface
762306a36Sopenharmony_ci**************************************************
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciA stateless decoder is a decoder that works without retaining any kind of state
1062306a36Sopenharmony_cibetween processed frames. This means that each frame is decoded independently
1162306a36Sopenharmony_ciof any previous and future frames, and that the client is responsible for
1262306a36Sopenharmony_cimaintaining the decoding state and providing it to the decoder with each
1362306a36Sopenharmony_cidecoding request. This is in contrast to the stateful video decoder interface,
1462306a36Sopenharmony_ciwhere the hardware and driver maintain the decoding state and all the client
1562306a36Sopenharmony_cihas to do is to provide the raw encoded stream and dequeue decoded frames in
1662306a36Sopenharmony_cidisplay order.
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ciThis section describes how user-space ("the client") is expected to communicate
1962306a36Sopenharmony_ciwith stateless decoders in order to successfully decode an encoded stream.
2062306a36Sopenharmony_ciCompared to stateful codecs, the decoder/client sequence is simpler, but the
2162306a36Sopenharmony_cicost of this simplicity is extra complexity in the client which is responsible
2262306a36Sopenharmony_cifor maintaining a consistent decoding state.
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciStateless decoders make use of the :ref:`media-request-api`. A stateless
2562306a36Sopenharmony_cidecoder must expose the ``V4L2_BUF_CAP_SUPPORTS_REQUESTS`` capability on its
2662306a36Sopenharmony_ci``OUTPUT`` queue when :c:func:`VIDIOC_REQBUFS` or :c:func:`VIDIOC_CREATE_BUFS`
2762306a36Sopenharmony_ciare invoked.
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciDepending on the encoded formats supported by the decoder, a single decoded
3062306a36Sopenharmony_ciframe may be the result of several decode requests (for instance, H.264 streams
3162306a36Sopenharmony_ciwith multiple slices per frame). Decoders that support such formats must also
3262306a36Sopenharmony_ciexpose the ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF`` capability on their
3362306a36Sopenharmony_ci``OUTPUT`` queue.
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciQuerying capabilities
3662306a36Sopenharmony_ci=====================
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci1. To enumerate the set of coded formats supported by the decoder, the client
3962306a36Sopenharmony_ci   calls :c:func:`VIDIOC_ENUM_FMT` on the ``OUTPUT`` queue.
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci   * The driver must always return the full set of supported ``OUTPUT`` formats,
4262306a36Sopenharmony_ci     irrespective of the format currently set on the ``CAPTURE`` queue.
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci   * Simultaneously, the driver must restrain the set of values returned by
4562306a36Sopenharmony_ci     codec-specific capability controls (such as H.264 profiles) to the set
4662306a36Sopenharmony_ci     actually supported by the hardware.
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci2. To enumerate the set of supported raw formats, the client calls
4962306a36Sopenharmony_ci   :c:func:`VIDIOC_ENUM_FMT` on the ``CAPTURE`` queue.
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci   * The driver must return only the formats supported for the format currently
5262306a36Sopenharmony_ci     active on the ``OUTPUT`` queue.
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci   * Depending on the currently set ``OUTPUT`` format, the set of supported raw
5562306a36Sopenharmony_ci     formats may depend on the value of some codec-dependent controls.
5662306a36Sopenharmony_ci     The client is responsible for making sure that these controls are set
5762306a36Sopenharmony_ci     before querying the ``CAPTURE`` queue. Failure to do so will result in the
5862306a36Sopenharmony_ci     default values for these controls being used, and a returned set of formats
5962306a36Sopenharmony_ci     that may not be usable for the media the client is trying to decode.
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci3. The client may use :c:func:`VIDIOC_ENUM_FRAMESIZES` to detect supported
6262306a36Sopenharmony_ci   resolutions for a given format, passing desired pixel format in
6362306a36Sopenharmony_ci   :c:type:`v4l2_frmsizeenum`'s ``pixel_format``.
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci4. Supported profiles and levels for the current ``OUTPUT`` format, if
6662306a36Sopenharmony_ci   applicable, may be queried using their respective controls via
6762306a36Sopenharmony_ci   :c:func:`VIDIOC_QUERYCTRL`.
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciInitialization
7062306a36Sopenharmony_ci==============
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci1. Set the coded format on the ``OUTPUT`` queue via :c:func:`VIDIOC_S_FMT`.
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci   * **Required fields:**
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci     ``type``
7762306a36Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci     ``pixelformat``
8062306a36Sopenharmony_ci         a coded pixel format.
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci     ``width``, ``height``
8362306a36Sopenharmony_ci         coded width and height parsed from the stream.
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci     other fields
8662306a36Sopenharmony_ci         follow standard semantics.
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci   .. note::
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci      Changing the ``OUTPUT`` format may change the currently set ``CAPTURE``
9162306a36Sopenharmony_ci      format. The driver will derive a new ``CAPTURE`` format from the
9262306a36Sopenharmony_ci      ``OUTPUT`` format being set, including resolution, colorimetry
9362306a36Sopenharmony_ci      parameters, etc. If the client needs a specific ``CAPTURE`` format,
9462306a36Sopenharmony_ci      it must adjust it afterwards.
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci2. Call :c:func:`VIDIOC_S_EXT_CTRLS` to set all the controls (parsed headers,
9762306a36Sopenharmony_ci   etc.) required by the ``OUTPUT`` format to enumerate the ``CAPTURE`` formats.
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci3. Call :c:func:`VIDIOC_G_FMT` for ``CAPTURE`` queue to get the format for the
10062306a36Sopenharmony_ci   destination buffers parsed/decoded from the bytestream.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci   * **Required fields:**
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci     ``type``
10562306a36Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci   * **Returned fields:**
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci     ``width``, ``height``
11062306a36Sopenharmony_ci         frame buffer resolution for the decoded frames.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci     ``pixelformat``
11362306a36Sopenharmony_ci         pixel format for decoded frames.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci     ``num_planes`` (for _MPLANE ``type`` only)
11662306a36Sopenharmony_ci         number of planes for pixelformat.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci     ``sizeimage``, ``bytesperline``
11962306a36Sopenharmony_ci         as per standard semantics; matching frame buffer format.
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci   .. note::
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci      The value of ``pixelformat`` may be any pixel format supported for the
12462306a36Sopenharmony_ci      ``OUTPUT`` format, based on the hardware capabilities. It is suggested
12562306a36Sopenharmony_ci      that the driver chooses the preferred/optimal format for the current
12662306a36Sopenharmony_ci      configuration. For example, a YUV format may be preferred over an RGB
12762306a36Sopenharmony_ci      format, if an additional conversion step would be required for RGB.
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci4. *[optional]* Enumerate ``CAPTURE`` formats via :c:func:`VIDIOC_ENUM_FMT` on
13062306a36Sopenharmony_ci   the ``CAPTURE`` queue. The client may use this ioctl to discover which
13162306a36Sopenharmony_ci   alternative raw formats are supported for the current ``OUTPUT`` format and
13262306a36Sopenharmony_ci   select one of them via :c:func:`VIDIOC_S_FMT`.
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci   .. note::
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci      The driver will return only formats supported for the currently selected
13762306a36Sopenharmony_ci      ``OUTPUT`` format and currently set controls, even if more formats may be
13862306a36Sopenharmony_ci      supported by the decoder in general.
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci      For example, a decoder may support YUV and RGB formats for
14162306a36Sopenharmony_ci      resolutions 1920x1088 and lower, but only YUV for higher resolutions (due
14262306a36Sopenharmony_ci      to hardware limitations). After setting a resolution of 1920x1088 or lower
14362306a36Sopenharmony_ci      as the ``OUTPUT`` format, :c:func:`VIDIOC_ENUM_FMT` may return a set of
14462306a36Sopenharmony_ci      YUV and RGB pixel formats, but after setting a resolution higher than
14562306a36Sopenharmony_ci      1920x1088, the driver will not return RGB pixel formats, since they are
14662306a36Sopenharmony_ci      unsupported for this resolution.
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci5. *[optional]* Choose a different ``CAPTURE`` format than suggested via
14962306a36Sopenharmony_ci   :c:func:`VIDIOC_S_FMT` on ``CAPTURE`` queue. It is possible for the client to
15062306a36Sopenharmony_ci   choose a different format than selected/suggested by the driver in
15162306a36Sopenharmony_ci   :c:func:`VIDIOC_G_FMT`.
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci    * **Required fields:**
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci      ``type``
15662306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci      ``pixelformat``
15962306a36Sopenharmony_ci          a raw pixel format.
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci      ``width``, ``height``
16262306a36Sopenharmony_ci         frame buffer resolution of the decoded stream; typically unchanged from
16362306a36Sopenharmony_ci         what was returned with :c:func:`VIDIOC_G_FMT`, but it may be different
16462306a36Sopenharmony_ci         if the hardware supports composition and/or scaling.
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci   After performing this step, the client must perform step 3 again in order
16762306a36Sopenharmony_ci   to obtain up-to-date information about the buffers size and layout.
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci6. Allocate source (bytestream) buffers via :c:func:`VIDIOC_REQBUFS` on
17062306a36Sopenharmony_ci   ``OUTPUT`` queue.
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci    * **Required fields:**
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci      ``count``
17562306a36Sopenharmony_ci          requested number of buffers to allocate; greater than zero.
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci      ``type``
17862306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci      ``memory``
18162306a36Sopenharmony_ci          follows standard semantics.
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci    * **Returned fields:**
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci      ``count``
18662306a36Sopenharmony_ci          actual number of buffers allocated.
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci    * If required, the driver will adjust ``count`` to be equal or bigger to the
18962306a36Sopenharmony_ci      minimum of required number of ``OUTPUT`` buffers for the given format and
19062306a36Sopenharmony_ci      requested count. The client must check this value after the ioctl returns
19162306a36Sopenharmony_ci      to get the actual number of buffers allocated.
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci7. Allocate destination (raw format) buffers via :c:func:`VIDIOC_REQBUFS` on the
19462306a36Sopenharmony_ci   ``CAPTURE`` queue.
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci    * **Required fields:**
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci      ``count``
19962306a36Sopenharmony_ci          requested number of buffers to allocate; greater than zero. The client
20062306a36Sopenharmony_ci          is responsible for deducing the minimum number of buffers required
20162306a36Sopenharmony_ci          for the stream to be properly decoded (taking e.g. reference frames
20262306a36Sopenharmony_ci          into account) and pass an equal or bigger number.
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci      ``type``
20562306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci      ``memory``
20862306a36Sopenharmony_ci          follows standard semantics. ``V4L2_MEMORY_USERPTR`` is not supported
20962306a36Sopenharmony_ci          for ``CAPTURE`` buffers.
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci    * **Returned fields:**
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci      ``count``
21462306a36Sopenharmony_ci          adjusted to allocated number of buffers, in case the codec requires
21562306a36Sopenharmony_ci          more buffers than requested.
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci    * The driver must adjust count to the minimum of required number of
21862306a36Sopenharmony_ci      ``CAPTURE`` buffers for the current format, stream configuration and
21962306a36Sopenharmony_ci      requested count. The client must check this value after the ioctl
22062306a36Sopenharmony_ci      returns to get the number of buffers allocated.
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci8. Allocate requests (likely one per ``OUTPUT`` buffer) via
22362306a36Sopenharmony_ci    :c:func:`MEDIA_IOC_REQUEST_ALLOC` on the media device.
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci9. Start streaming on both ``OUTPUT`` and ``CAPTURE`` queues via
22662306a36Sopenharmony_ci    :c:func:`VIDIOC_STREAMON`.
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ciDecoding
22962306a36Sopenharmony_ci========
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ciFor each frame, the client is responsible for submitting at least one request to
23262306a36Sopenharmony_ciwhich the following is attached:
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci* The amount of encoded data expected by the codec for its current
23562306a36Sopenharmony_ci  configuration, as a buffer submitted to the ``OUTPUT`` queue. Typically, this
23662306a36Sopenharmony_ci  corresponds to one frame worth of encoded data, but some formats may allow (or
23762306a36Sopenharmony_ci  require) different amounts per unit.
23862306a36Sopenharmony_ci* All the metadata needed to decode the submitted encoded data, in the form of
23962306a36Sopenharmony_ci  controls relevant to the format being decoded.
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ciThe amount of data and contents of the source ``OUTPUT`` buffer, as well as the
24262306a36Sopenharmony_cicontrols that must be set on the request, depend on the active coded pixel
24362306a36Sopenharmony_ciformat and might be affected by codec-specific extended controls, as stated in
24462306a36Sopenharmony_cidocumentation of each format.
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ciIf there is a possibility that the decoded frame will require one or more
24762306a36Sopenharmony_cidecode requests after the current one in order to be produced, then the client
24862306a36Sopenharmony_cimust set the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag on the ``OUTPUT``
24962306a36Sopenharmony_cibuffer. This will result in the (potentially partially) decoded ``CAPTURE``
25062306a36Sopenharmony_cibuffer not being made available for dequeueing, and reused for the next decode
25162306a36Sopenharmony_cirequest if the timestamp of the next ``OUTPUT`` buffer has not changed.
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ciA typical frame would thus be decoded using the following sequence:
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci1. Queue an ``OUTPUT`` buffer containing one unit of encoded bytestream data for
25662306a36Sopenharmony_ci   the decoding request, using :c:func:`VIDIOC_QBUF`.
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci    * **Required fields:**
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci      ``index``
26162306a36Sopenharmony_ci          index of the buffer being queued.
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci      ``type``
26462306a36Sopenharmony_ci          type of the buffer.
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci      ``bytesused``
26762306a36Sopenharmony_ci          number of bytes taken by the encoded data frame in the buffer.
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci      ``flags``
27062306a36Sopenharmony_ci          the ``V4L2_BUF_FLAG_REQUEST_FD`` flag must be set. Additionally, if
27162306a36Sopenharmony_ci          we are not sure that the current decode request is the last one needed
27262306a36Sopenharmony_ci          to produce a fully decoded frame, then
27362306a36Sopenharmony_ci          ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` must also be set.
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci      ``request_fd``
27662306a36Sopenharmony_ci          must be set to the file descriptor of the decoding request.
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci      ``timestamp``
27962306a36Sopenharmony_ci          must be set to a unique value per frame. This value will be propagated
28062306a36Sopenharmony_ci          into the decoded frame's buffer and can also be used to use this frame
28162306a36Sopenharmony_ci          as the reference of another. If using multiple decode requests per
28262306a36Sopenharmony_ci          frame, then the timestamps of all the ``OUTPUT`` buffers for a given
28362306a36Sopenharmony_ci          frame must be identical. If the timestamp changes, then the currently
28462306a36Sopenharmony_ci          held ``CAPTURE`` buffer will be made available for dequeuing and the
28562306a36Sopenharmony_ci          current request will work on a new ``CAPTURE`` buffer.
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci2. Set the codec-specific controls for the decoding request, using
28862306a36Sopenharmony_ci   :c:func:`VIDIOC_S_EXT_CTRLS`.
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci    * **Required fields:**
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci      ``which``
29362306a36Sopenharmony_ci          must be ``V4L2_CTRL_WHICH_REQUEST_VAL``.
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci      ``request_fd``
29662306a36Sopenharmony_ci          must be set to the file descriptor of the decoding request.
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci      other fields
29962306a36Sopenharmony_ci          other fields are set as usual when setting controls. The ``controls``
30062306a36Sopenharmony_ci          array must contain all the codec-specific controls required to decode
30162306a36Sopenharmony_ci          a frame.
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci   .. note::
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci      It is possible to specify the controls in different invocations of
30662306a36Sopenharmony_ci      :c:func:`VIDIOC_S_EXT_CTRLS`, or to overwrite a previously set control, as
30762306a36Sopenharmony_ci      long as ``request_fd`` and ``which`` are properly set. The controls state
30862306a36Sopenharmony_ci      at the moment of request submission is the one that will be considered.
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci   .. note::
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci      The order in which steps 1 and 2 take place is interchangeable.
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci3. Submit the request by invoking :c:func:`MEDIA_REQUEST_IOC_QUEUE` on the
31562306a36Sopenharmony_ci   request FD.
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci    If the request is submitted without an ``OUTPUT`` buffer, or if some of the
31862306a36Sopenharmony_ci    required controls are missing from the request, then
31962306a36Sopenharmony_ci    :c:func:`MEDIA_REQUEST_IOC_QUEUE` will return ``-ENOENT``. If more than one
32062306a36Sopenharmony_ci    ``OUTPUT`` buffer is queued, then it will return ``-EINVAL``.
32162306a36Sopenharmony_ci    :c:func:`MEDIA_REQUEST_IOC_QUEUE` returning non-zero means that no
32262306a36Sopenharmony_ci    ``CAPTURE`` buffer will be produced for this request.
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci``CAPTURE`` buffers must not be part of the request, and are queued
32562306a36Sopenharmony_ciindependently. They are returned in decode order (i.e. the same order as coded
32662306a36Sopenharmony_ciframes were submitted to the ``OUTPUT`` queue).
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ciRuntime decoding errors are signaled by the dequeued ``CAPTURE`` buffers
32962306a36Sopenharmony_cicarrying the ``V4L2_BUF_FLAG_ERROR`` flag. If a decoded reference frame has an
33062306a36Sopenharmony_cierror, then all following decoded frames that refer to it also have the
33162306a36Sopenharmony_ci``V4L2_BUF_FLAG_ERROR`` flag set, although the decoder will still try to
33262306a36Sopenharmony_ciproduce (likely corrupted) frames.
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ciBuffer management while decoding
33562306a36Sopenharmony_ci================================
33662306a36Sopenharmony_ciContrary to stateful decoders, a stateless decoder does not perform any kind of
33762306a36Sopenharmony_cibuffer management: it only guarantees that dequeued ``CAPTURE`` buffers can be
33862306a36Sopenharmony_ciused by the client for as long as they are not queued again. "Used" here
33962306a36Sopenharmony_ciencompasses using the buffer for compositing or display.
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ciA dequeued capture buffer can also be used as the reference frame of another
34262306a36Sopenharmony_cibuffer.
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ciA frame is specified as reference by converting its timestamp into nanoseconds,
34562306a36Sopenharmony_ciand storing it into the relevant member of a codec-dependent control structure.
34662306a36Sopenharmony_ciThe :c:func:`v4l2_timeval_to_ns` function must be used to perform that
34762306a36Sopenharmony_ciconversion. The timestamp of a frame can be used to reference it as soon as all
34862306a36Sopenharmony_ciits units of encoded data are successfully submitted to the ``OUTPUT`` queue.
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ciA decoded buffer containing a reference frame must not be reused as a decoding
35162306a36Sopenharmony_citarget until all the frames referencing it have been decoded. The safest way to
35262306a36Sopenharmony_ciachieve this is to refrain from queueing a reference buffer until all the
35362306a36Sopenharmony_cidecoded frames referencing it have been dequeued. However, if the driver can
35462306a36Sopenharmony_ciguarantee that buffers queued to the ``CAPTURE`` queue are processed in queued
35562306a36Sopenharmony_ciorder, then user-space can take advantage of this guarantee and queue a
35662306a36Sopenharmony_cireference buffer when the following conditions are met:
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci1. All the requests for frames affected by the reference frame have been
35962306a36Sopenharmony_ci   queued, and
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci2. A sufficient number of ``CAPTURE`` buffers to cover all the decoded
36262306a36Sopenharmony_ci   referencing frames have been queued.
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ciWhen queuing a decoding request, the driver will increase the reference count of
36562306a36Sopenharmony_ciall the resources associated with reference frames. This means that the client
36662306a36Sopenharmony_cican e.g. close the DMABUF file descriptors of reference frame buffers if it
36762306a36Sopenharmony_ciwon't need them afterwards.
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ciSeeking
37062306a36Sopenharmony_ci=======
37162306a36Sopenharmony_ciIn order to seek, the client just needs to submit requests using input buffers
37262306a36Sopenharmony_cicorresponding to the new stream position. It must however be aware that
37362306a36Sopenharmony_ciresolution may have changed and follow the dynamic resolution change sequence in
37462306a36Sopenharmony_cithat case. Also depending on the codec used, picture parameters (e.g. SPS/PPS
37562306a36Sopenharmony_cifor H.264) may have changed and the client is responsible for making sure that a
37662306a36Sopenharmony_civalid state is sent to the decoder.
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ciThe client is then free to ignore any returned ``CAPTURE`` buffer that comes
37962306a36Sopenharmony_cifrom the pre-seek position.
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ciPausing
38262306a36Sopenharmony_ci=======
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ciIn order to pause, the client can just cease queuing buffers onto the ``OUTPUT``
38562306a36Sopenharmony_ciqueue. Without source bytestream data, there is no data to process and the codec
38662306a36Sopenharmony_ciwill remain idle.
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ciDynamic resolution change
38962306a36Sopenharmony_ci=========================
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ciIf the client detects a resolution change in the stream, it will need to perform
39262306a36Sopenharmony_cithe initialization sequence again with the new resolution:
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci1. If the last submitted request resulted in a ``CAPTURE`` buffer being
39562306a36Sopenharmony_ci   held by the use of the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag, then the
39662306a36Sopenharmony_ci   last frame is not available on the ``CAPTURE`` queue. In this case, a
39762306a36Sopenharmony_ci   ``V4L2_DEC_CMD_FLUSH`` command shall be sent. This will make the driver
39862306a36Sopenharmony_ci   dequeue the held ``CAPTURE`` buffer.
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci2. Wait until all submitted requests have completed and dequeue the
40162306a36Sopenharmony_ci   corresponding output buffers.
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci3. Call :c:func:`VIDIOC_STREAMOFF` on both the ``OUTPUT`` and ``CAPTURE``
40462306a36Sopenharmony_ci   queues.
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci4. Free all ``CAPTURE`` buffers by calling :c:func:`VIDIOC_REQBUFS` on the
40762306a36Sopenharmony_ci   ``CAPTURE`` queue with a buffer count of zero.
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci5. Perform the initialization sequence again (minus the allocation of
41062306a36Sopenharmony_ci   ``OUTPUT`` buffers), with the new resolution set on the ``OUTPUT`` queue.
41162306a36Sopenharmony_ci   Note that due to resolution constraints, a different format may need to be
41262306a36Sopenharmony_ci   picked on the ``CAPTURE`` queue.
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ciDrain
41562306a36Sopenharmony_ci=====
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ciIf the last submitted request resulted in a ``CAPTURE`` buffer being
41862306a36Sopenharmony_ciheld by the use of the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag, then the
41962306a36Sopenharmony_cilast frame is not available on the ``CAPTURE`` queue. In this case, a
42062306a36Sopenharmony_ci``V4L2_DEC_CMD_FLUSH`` command shall be sent. This will make the driver
42162306a36Sopenharmony_cidequeue the held ``CAPTURE`` buffer.
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ciAfter that, in order to drain the stream on a stateless decoder, the client
42462306a36Sopenharmony_cijust needs to wait until all the submitted requests are completed.
425