162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci.. _decoder:
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci*************************************************
662306a36Sopenharmony_ciMemory-to-Memory Stateful Video Decoder Interface
762306a36Sopenharmony_ci*************************************************
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciA stateful video decoder takes complete chunks of the bytestream (e.g. Annex-B
1062306a36Sopenharmony_ciH.264/HEVC stream, raw VP8/9 stream) and decodes them into raw video frames in
1162306a36Sopenharmony_cidisplay order. The decoder is expected not to require any additional information
1262306a36Sopenharmony_cifrom the client to process these buffers.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciPerforming software parsing, processing etc. of the stream in the driver in
1562306a36Sopenharmony_ciorder to support this interface is strongly discouraged. In case such
1662306a36Sopenharmony_cioperations are needed, use of the Stateless Video Decoder Interface (in
1762306a36Sopenharmony_cidevelopment) is strongly advised.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciConventions and Notations Used in This Document
2062306a36Sopenharmony_ci===============================================
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci1. The general V4L2 API rules apply if not specified in this document
2362306a36Sopenharmony_ci   otherwise.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci2. The meaning of words "must", "may", "should", etc. is as per `RFC
2662306a36Sopenharmony_ci   2119 <https://tools.ietf.org/html/rfc2119>`_.
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci3. All steps not marked "optional" are required.
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci4. :c:func:`VIDIOC_G_EXT_CTRLS` and :c:func:`VIDIOC_S_EXT_CTRLS` may be used
3162306a36Sopenharmony_ci   interchangeably with :c:func:`VIDIOC_G_CTRL` and :c:func:`VIDIOC_S_CTRL`,
3262306a36Sopenharmony_ci   unless specified otherwise.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci5. Single-planar API (see :ref:`planar-apis`) and applicable structures may be
3562306a36Sopenharmony_ci   used interchangeably with multi-planar API, unless specified otherwise,
3662306a36Sopenharmony_ci   depending on decoder capabilities and following the general V4L2 guidelines.
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci6. i = [a..b]: sequence of integers from a to b, inclusive, i.e. i =
3962306a36Sopenharmony_ci   [0..2]: i = 0, 1, 2.
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci7. Given an ``OUTPUT`` buffer A, then A' represents a buffer on the ``CAPTURE``
4262306a36Sopenharmony_ci   queue containing data that resulted from processing buffer A.
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci.. _decoder-glossary:
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ciGlossary
4762306a36Sopenharmony_ci========
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciCAPTURE
5062306a36Sopenharmony_ci   the destination buffer queue; for decoders, the queue of buffers containing
5162306a36Sopenharmony_ci   decoded frames; for encoders, the queue of buffers containing an encoded
5262306a36Sopenharmony_ci   bytestream; ``V4L2_BUF_TYPE_VIDEO_CAPTURE`` or
5362306a36Sopenharmony_ci   ``V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE``; data is captured from the hardware
5462306a36Sopenharmony_ci   into ``CAPTURE`` buffers.
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciclient
5762306a36Sopenharmony_ci   the application communicating with the decoder or encoder implementing
5862306a36Sopenharmony_ci   this interface.
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cicoded format
6162306a36Sopenharmony_ci   encoded/compressed video bytestream format (e.g. H.264, VP8, etc.); see
6262306a36Sopenharmony_ci   also: raw format.
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cicoded height
6562306a36Sopenharmony_ci   height for given coded resolution.
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cicoded resolution
6862306a36Sopenharmony_ci   stream resolution in pixels aligned to codec and hardware requirements;
6962306a36Sopenharmony_ci   typically visible resolution rounded up to full macroblocks;
7062306a36Sopenharmony_ci   see also: visible resolution.
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cicoded width
7362306a36Sopenharmony_ci   width for given coded resolution.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cicoding tree unit
7662306a36Sopenharmony_ci   processing unit of the HEVC codec (corresponds to macroblock units in
7762306a36Sopenharmony_ci   H.264, VP8, VP9),
7862306a36Sopenharmony_ci   can use block structures of up to 64×64 pixels.
7962306a36Sopenharmony_ci   Good at sub-partitioning the picture into variable sized structures.
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_cidecode order
8262306a36Sopenharmony_ci   the order in which frames are decoded; may differ from display order if the
8362306a36Sopenharmony_ci   coded format includes a feature of frame reordering; for decoders,
8462306a36Sopenharmony_ci   ``OUTPUT`` buffers must be queued by the client in decode order; for
8562306a36Sopenharmony_ci   encoders ``CAPTURE`` buffers must be returned by the encoder in decode order.
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_cidestination
8862306a36Sopenharmony_ci   data resulting from the decode process; see ``CAPTURE``.
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cidisplay order
9162306a36Sopenharmony_ci   the order in which frames must be displayed; for encoders, ``OUTPUT``
9262306a36Sopenharmony_ci   buffers must be queued by the client in display order; for decoders,
9362306a36Sopenharmony_ci   ``CAPTURE`` buffers must be returned by the decoder in display order.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciDPB
9662306a36Sopenharmony_ci   Decoded Picture Buffer; an H.264/HEVC term for a buffer that stores a decoded
9762306a36Sopenharmony_ci   raw frame available for reference in further decoding steps.
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciEOS
10062306a36Sopenharmony_ci   end of stream.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciIDR
10362306a36Sopenharmony_ci   Instantaneous Decoder Refresh; a type of a keyframe in an H.264/HEVC-encoded
10462306a36Sopenharmony_ci   stream, which clears the list of earlier reference frames (DPBs).
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cikeyframe
10762306a36Sopenharmony_ci   an encoded frame that does not reference frames decoded earlier, i.e.
10862306a36Sopenharmony_ci   can be decoded fully on its own.
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cimacroblock
11162306a36Sopenharmony_ci   a processing unit in image and video compression formats based on linear
11262306a36Sopenharmony_ci   block transforms (e.g. H.264, VP8, VP9); codec-specific, but for most of
11362306a36Sopenharmony_ci   popular codecs the size is 16x16 samples (pixels). The HEVC codec uses a
11462306a36Sopenharmony_ci   slightly more flexible processing unit called coding tree unit (CTU).
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ciOUTPUT
11762306a36Sopenharmony_ci   the source buffer queue; for decoders, the queue of buffers containing
11862306a36Sopenharmony_ci   an encoded bytestream; for encoders, the queue of buffers containing raw
11962306a36Sopenharmony_ci   frames; ``V4L2_BUF_TYPE_VIDEO_OUTPUT`` or
12062306a36Sopenharmony_ci   ``V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE``; the hardware is fed with data
12162306a36Sopenharmony_ci   from ``OUTPUT`` buffers.
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ciPPS
12462306a36Sopenharmony_ci   Picture Parameter Set; a type of metadata entity in an H.264/HEVC bytestream.
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ciraw format
12762306a36Sopenharmony_ci   uncompressed format containing raw pixel data (e.g. YUV, RGB formats).
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ciresume point
13062306a36Sopenharmony_ci   a point in the bytestream from which decoding may start/continue, without
13162306a36Sopenharmony_ci   any previous state/data present, e.g.: a keyframe (VP8/VP9) or
13262306a36Sopenharmony_ci   SPS/PPS/IDR sequence (H.264/HEVC); a resume point is required to start decode
13362306a36Sopenharmony_ci   of a new stream, or to resume decoding after a seek.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cisource
13662306a36Sopenharmony_ci   data fed to the decoder or encoder; see ``OUTPUT``.
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cisource height
13962306a36Sopenharmony_ci   height in pixels for given source resolution; relevant to encoders only.
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_cisource resolution
14262306a36Sopenharmony_ci   resolution in pixels of source frames being source to the encoder and
14362306a36Sopenharmony_ci   subject to further cropping to the bounds of visible resolution; relevant to
14462306a36Sopenharmony_ci   encoders only.
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_cisource width
14762306a36Sopenharmony_ci   width in pixels for given source resolution; relevant to encoders only.
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ciSPS
15062306a36Sopenharmony_ci   Sequence Parameter Set; a type of metadata entity in an H.264/HEVC bytestream.
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistream metadata
15362306a36Sopenharmony_ci   additional (non-visual) information contained inside encoded bytestream;
15462306a36Sopenharmony_ci   for example: coded resolution, visible resolution, codec profile.
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_civisible height
15762306a36Sopenharmony_ci   height for given visible resolution; display height.
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_civisible resolution
16062306a36Sopenharmony_ci   stream resolution of the visible picture, in pixels, to be used for
16162306a36Sopenharmony_ci   display purposes; must be smaller or equal to coded resolution;
16262306a36Sopenharmony_ci   display resolution.
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_civisible width
16562306a36Sopenharmony_ci   width for given visible resolution; display width.
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciState Machine
16862306a36Sopenharmony_ci=============
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci.. kernel-render:: DOT
17162306a36Sopenharmony_ci   :alt: DOT digraph of decoder state machine
17262306a36Sopenharmony_ci   :caption: Decoder State Machine
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci   digraph decoder_state_machine {
17562306a36Sopenharmony_ci       node [shape = doublecircle, label="Decoding"] Decoding;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci       node [shape = circle, label="Initialization"] Initialization;
17862306a36Sopenharmony_ci       node [shape = circle, label="Capture\nsetup"] CaptureSetup;
17962306a36Sopenharmony_ci       node [shape = circle, label="Dynamic\nResolution\nChange"] ResChange;
18062306a36Sopenharmony_ci       node [shape = circle, label="Stopped"] Stopped;
18162306a36Sopenharmony_ci       node [shape = circle, label="Drain"] Drain;
18262306a36Sopenharmony_ci       node [shape = circle, label="Seek"] Seek;
18362306a36Sopenharmony_ci       node [shape = circle, label="End of Stream"] EoS;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci       node [shape = point]; qi
18662306a36Sopenharmony_ci       qi -> Initialization [ label = "open()" ];
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci       Initialization -> CaptureSetup [ label = "CAPTURE\nformat\nestablished" ];
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci       CaptureSetup -> Stopped [ label = "CAPTURE\nbuffers\nready" ];
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci       Decoding -> ResChange [ label = "Stream\nresolution\nchange" ];
19362306a36Sopenharmony_ci       Decoding -> Drain [ label = "V4L2_DEC_CMD_STOP" ];
19462306a36Sopenharmony_ci       Decoding -> EoS [ label = "EoS mark\nin the stream" ];
19562306a36Sopenharmony_ci       Decoding -> Seek [ label = "VIDIOC_STREAMOFF(OUTPUT)" ];
19662306a36Sopenharmony_ci       Decoding -> Stopped [ label = "VIDIOC_STREAMOFF(CAPTURE)" ];
19762306a36Sopenharmony_ci       Decoding -> Decoding;
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci       ResChange -> CaptureSetup [ label = "CAPTURE\nformat\nestablished" ];
20062306a36Sopenharmony_ci       ResChange -> Seek [ label = "VIDIOC_STREAMOFF(OUTPUT)" ];
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci       EoS -> Drain [ label = "Implicit\ndrain" ];
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci       Drain -> Stopped [ label = "All CAPTURE\nbuffers dequeued\nor\nVIDIOC_STREAMOFF(CAPTURE)" ];
20562306a36Sopenharmony_ci       Drain -> Seek [ label = "VIDIOC_STREAMOFF(OUTPUT)" ];
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci       Seek -> Decoding [ label = "VIDIOC_STREAMON(OUTPUT)" ];
20862306a36Sopenharmony_ci       Seek -> Initialization [ label = "VIDIOC_REQBUFS(OUTPUT, 0)" ];
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci       Stopped -> Decoding [ label = "V4L2_DEC_CMD_START\nor\nVIDIOC_STREAMON(CAPTURE)" ];
21162306a36Sopenharmony_ci       Stopped -> Seek [ label = "VIDIOC_STREAMOFF(OUTPUT)" ];
21262306a36Sopenharmony_ci   }
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ciQuerying Capabilities
21562306a36Sopenharmony_ci=====================
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci1. To enumerate the set of coded formats supported by the decoder, the
21862306a36Sopenharmony_ci   client may call :c:func:`VIDIOC_ENUM_FMT` on ``OUTPUT``.
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci   * The full set of supported formats will be returned, regardless of the
22162306a36Sopenharmony_ci     format set on ``CAPTURE``.
22262306a36Sopenharmony_ci   * Check the flags field of :c:type:`v4l2_fmtdesc` for more information
22362306a36Sopenharmony_ci     about the decoder's capabilities with respect to each coded format.
22462306a36Sopenharmony_ci     In particular whether or not the decoder has a full-fledged bytestream
22562306a36Sopenharmony_ci     parser and if the decoder supports dynamic resolution changes.
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci2. To enumerate the set of supported raw formats, the client may call
22862306a36Sopenharmony_ci   :c:func:`VIDIOC_ENUM_FMT` on ``CAPTURE``.
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci   * Only the formats supported for the format currently active on ``OUTPUT``
23162306a36Sopenharmony_ci     will be returned.
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci   * In order to enumerate raw formats supported by a given coded format,
23462306a36Sopenharmony_ci     the client must first set that coded format on ``OUTPUT`` and then
23562306a36Sopenharmony_ci     enumerate formats on ``CAPTURE``.
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci3. The client may use :c:func:`VIDIOC_ENUM_FRAMESIZES` to detect supported
23862306a36Sopenharmony_ci   resolutions for a given format, passing desired pixel format in
23962306a36Sopenharmony_ci   :c:type:`v4l2_frmsizeenum` ``pixel_format``.
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci   * Values returned by :c:func:`VIDIOC_ENUM_FRAMESIZES` for a coded pixel
24262306a36Sopenharmony_ci     format will include all possible coded resolutions supported by the
24362306a36Sopenharmony_ci     decoder for given coded pixel format.
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci   * Values returned by :c:func:`VIDIOC_ENUM_FRAMESIZES` for a raw pixel format
24662306a36Sopenharmony_ci     will include all possible frame buffer resolutions supported by the
24762306a36Sopenharmony_ci     decoder for given raw pixel format and the coded format currently set on
24862306a36Sopenharmony_ci     ``OUTPUT``.
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci4. Supported profiles and levels for the coded format currently set on
25162306a36Sopenharmony_ci   ``OUTPUT``, if applicable, may be queried using their respective controls
25262306a36Sopenharmony_ci   via :c:func:`VIDIOC_QUERYCTRL`.
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ciInitialization
25562306a36Sopenharmony_ci==============
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci1. Set the coded format on ``OUTPUT`` via :c:func:`VIDIOC_S_FMT`.
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci   * **Required fields:**
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci     ``type``
26262306a36Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci     ``pixelformat``
26562306a36Sopenharmony_ci         a coded pixel format.
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci     ``width``, ``height``
26862306a36Sopenharmony_ci         coded resolution of the stream; required only if it cannot be parsed
26962306a36Sopenharmony_ci         from the stream for the given coded format; otherwise the decoder will
27062306a36Sopenharmony_ci         use this resolution as a placeholder resolution that will likely change
27162306a36Sopenharmony_ci         as soon as it can parse the actual coded resolution from the stream.
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci     ``sizeimage``
27462306a36Sopenharmony_ci         desired size of ``OUTPUT`` buffers; the decoder may adjust it to
27562306a36Sopenharmony_ci         match hardware requirements.
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci     other fields
27862306a36Sopenharmony_ci         follow standard semantics.
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci   * **Returned fields:**
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci     ``sizeimage``
28362306a36Sopenharmony_ci         adjusted size of ``OUTPUT`` buffers.
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci   * The ``CAPTURE`` format will be updated with an appropriate frame buffer
28662306a36Sopenharmony_ci     resolution instantly based on the width and height returned by
28762306a36Sopenharmony_ci     :c:func:`VIDIOC_S_FMT`.
28862306a36Sopenharmony_ci     However, for coded formats that include stream resolution information,
28962306a36Sopenharmony_ci     after the decoder is done parsing the information from the stream, it will
29062306a36Sopenharmony_ci     update the ``CAPTURE`` format with new values and signal a source change
29162306a36Sopenharmony_ci     event, regardless of whether they match the values set by the client or
29262306a36Sopenharmony_ci     not.
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci   .. important::
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci      Changing the ``OUTPUT`` format may change the currently set ``CAPTURE``
29762306a36Sopenharmony_ci      format. How the new ``CAPTURE`` format is determined is up to the decoder
29862306a36Sopenharmony_ci      and the client must ensure it matches its needs afterwards.
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci2.  Allocate source (bytestream) buffers via :c:func:`VIDIOC_REQBUFS` on
30162306a36Sopenharmony_ci    ``OUTPUT``.
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci    * **Required fields:**
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci      ``count``
30662306a36Sopenharmony_ci          requested number of buffers to allocate; greater than zero.
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci      ``type``
30962306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci      ``memory``
31262306a36Sopenharmony_ci          follows standard semantics.
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci    * **Returned fields:**
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci      ``count``
31762306a36Sopenharmony_ci          the actual number of buffers allocated.
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci    .. warning::
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci       The actual number of allocated buffers may differ from the ``count``
32262306a36Sopenharmony_ci       given. The client must check the updated value of ``count`` after the
32362306a36Sopenharmony_ci       call returns.
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci    Alternatively, :c:func:`VIDIOC_CREATE_BUFS` on the ``OUTPUT`` queue can be
32662306a36Sopenharmony_ci    used to have more control over buffer allocation.
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci    * **Required fields:**
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci      ``count``
33162306a36Sopenharmony_ci          requested number of buffers to allocate; greater than zero.
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci      ``type``
33462306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci      ``memory``
33762306a36Sopenharmony_ci          follows standard semantics.
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci      ``format``
34062306a36Sopenharmony_ci          follows standard semantics.
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci    * **Returned fields:**
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci      ``count``
34562306a36Sopenharmony_ci          adjusted to the number of allocated buffers.
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci    .. warning::
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci       The actual number of allocated buffers may differ from the ``count``
35062306a36Sopenharmony_ci       given. The client must check the updated value of ``count`` after the
35162306a36Sopenharmony_ci       call returns.
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci3.  Start streaming on the ``OUTPUT`` queue via :c:func:`VIDIOC_STREAMON`.
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci4.  **This step only applies to coded formats that contain resolution information
35662306a36Sopenharmony_ci    in the stream.** Continue queuing/dequeuing bytestream buffers to/from the
35762306a36Sopenharmony_ci    ``OUTPUT`` queue via :c:func:`VIDIOC_QBUF` and :c:func:`VIDIOC_DQBUF`. The
35862306a36Sopenharmony_ci    buffers will be processed and returned to the client in order, until
35962306a36Sopenharmony_ci    required metadata to configure the ``CAPTURE`` queue are found. This is
36062306a36Sopenharmony_ci    indicated by the decoder sending a ``V4L2_EVENT_SOURCE_CHANGE`` event with
36162306a36Sopenharmony_ci    ``changes`` set to ``V4L2_EVENT_SRC_CH_RESOLUTION``.
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci    * It is not an error if the first buffer does not contain enough data for
36462306a36Sopenharmony_ci      this to occur. Processing of the buffers will continue as long as more
36562306a36Sopenharmony_ci      data is needed.
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci    * If data in a buffer that triggers the event is required to decode the
36862306a36Sopenharmony_ci      first frame, it will not be returned to the client, until the
36962306a36Sopenharmony_ci      initialization sequence completes and the frame is decoded.
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci    * If the client has not set the coded resolution of the stream on its own,
37262306a36Sopenharmony_ci      calling :c:func:`VIDIOC_G_FMT`, :c:func:`VIDIOC_S_FMT`,
37362306a36Sopenharmony_ci      :c:func:`VIDIOC_TRY_FMT` or :c:func:`VIDIOC_REQBUFS` on the ``CAPTURE``
37462306a36Sopenharmony_ci      queue will not return the real values for the stream until a
37562306a36Sopenharmony_ci      ``V4L2_EVENT_SOURCE_CHANGE`` event with ``changes`` set to
37662306a36Sopenharmony_ci      ``V4L2_EVENT_SRC_CH_RESOLUTION`` is signaled.
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci    .. important::
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci       Any client query issued after the decoder queues the event will return
38162306a36Sopenharmony_ci       values applying to the just parsed stream, including queue formats,
38262306a36Sopenharmony_ci       selection rectangles and controls.
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci    .. note::
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci       A client capable of acquiring stream parameters from the bytestream on
38762306a36Sopenharmony_ci       its own may attempt to set the width and height of the ``OUTPUT`` format
38862306a36Sopenharmony_ci       to non-zero values matching the coded size of the stream, skip this step
38962306a36Sopenharmony_ci       and continue with the `Capture Setup` sequence. However, it must not
39062306a36Sopenharmony_ci       rely on any driver queries regarding stream parameters, such as
39162306a36Sopenharmony_ci       selection rectangles and controls, since the decoder has not parsed them
39262306a36Sopenharmony_ci       from the stream yet. If the values configured by the client do not match
39362306a36Sopenharmony_ci       those parsed by the decoder, a `Dynamic Resolution Change` will be
39462306a36Sopenharmony_ci       triggered to reconfigure them.
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci    .. note::
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci       No decoded frames are produced during this phase.
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci5.  Continue with the `Capture Setup` sequence.
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ciCapture Setup
40362306a36Sopenharmony_ci=============
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci1.  Call :c:func:`VIDIOC_G_FMT` on the ``CAPTURE`` queue to get format for the
40662306a36Sopenharmony_ci    destination buffers parsed/decoded from the bytestream.
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci    * **Required fields:**
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci      ``type``
41162306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci    * **Returned fields:**
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci      ``width``, ``height``
41662306a36Sopenharmony_ci          frame buffer resolution for the decoded frames.
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci      ``pixelformat``
41962306a36Sopenharmony_ci          pixel format for decoded frames.
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci      ``num_planes`` (for _MPLANE ``type`` only)
42262306a36Sopenharmony_ci          number of planes for pixelformat.
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci      ``sizeimage``, ``bytesperline``
42562306a36Sopenharmony_ci          as per standard semantics; matching frame buffer format.
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci    .. note::
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci       The value of ``pixelformat`` may be any pixel format supported by the
43062306a36Sopenharmony_ci       decoder for the current stream. The decoder should choose a
43162306a36Sopenharmony_ci       preferred/optimal format for the default configuration. For example, a
43262306a36Sopenharmony_ci       YUV format may be preferred over an RGB format if an additional
43362306a36Sopenharmony_ci       conversion step would be required for the latter.
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci2.  **Optional.** Acquire the visible resolution via
43662306a36Sopenharmony_ci    :c:func:`VIDIOC_G_SELECTION`.
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci    * **Required fields:**
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci      ``type``
44162306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci      ``target``
44462306a36Sopenharmony_ci          set to ``V4L2_SEL_TGT_COMPOSE``.
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci    * **Returned fields:**
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci      ``r.left``, ``r.top``, ``r.width``, ``r.height``
44962306a36Sopenharmony_ci          the visible rectangle; it must fit within the frame buffer resolution
45062306a36Sopenharmony_ci          returned by :c:func:`VIDIOC_G_FMT` on ``CAPTURE``.
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci    * The following selection targets are supported on ``CAPTURE``:
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci      ``V4L2_SEL_TGT_CROP_BOUNDS``
45562306a36Sopenharmony_ci          corresponds to the coded resolution of the stream.
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci      ``V4L2_SEL_TGT_CROP_DEFAULT``
45862306a36Sopenharmony_ci          the rectangle covering the part of the ``CAPTURE`` buffer that
45962306a36Sopenharmony_ci          contains meaningful picture data (visible area); width and height
46062306a36Sopenharmony_ci          will be equal to the visible resolution of the stream.
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci      ``V4L2_SEL_TGT_CROP``
46362306a36Sopenharmony_ci          the rectangle within the coded resolution to be output to
46462306a36Sopenharmony_ci          ``CAPTURE``; defaults to ``V4L2_SEL_TGT_CROP_DEFAULT``; read-only on
46562306a36Sopenharmony_ci          hardware without additional compose/scaling capabilities.
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci      ``V4L2_SEL_TGT_COMPOSE_BOUNDS``
46862306a36Sopenharmony_ci          the maximum rectangle within a ``CAPTURE`` buffer, which the cropped
46962306a36Sopenharmony_ci          frame can be composed into; equal to ``V4L2_SEL_TGT_CROP`` if the
47062306a36Sopenharmony_ci          hardware does not support compose/scaling.
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci      ``V4L2_SEL_TGT_COMPOSE_DEFAULT``
47362306a36Sopenharmony_ci          equal to ``V4L2_SEL_TGT_CROP``.
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci      ``V4L2_SEL_TGT_COMPOSE``
47662306a36Sopenharmony_ci          the rectangle inside a ``CAPTURE`` buffer into which the cropped
47762306a36Sopenharmony_ci          frame is written; defaults to ``V4L2_SEL_TGT_COMPOSE_DEFAULT``;
47862306a36Sopenharmony_ci          read-only on hardware without additional compose/scaling capabilities.
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci      ``V4L2_SEL_TGT_COMPOSE_PADDED``
48162306a36Sopenharmony_ci          the rectangle inside a ``CAPTURE`` buffer which is overwritten by the
48262306a36Sopenharmony_ci          hardware; equal to ``V4L2_SEL_TGT_COMPOSE`` if the hardware does not
48362306a36Sopenharmony_ci          write padding pixels.
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci    .. warning::
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci       The values are guaranteed to be meaningful only after the decoder
48862306a36Sopenharmony_ci       successfully parses the stream metadata. The client must not rely on the
48962306a36Sopenharmony_ci       query before that happens.
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci3.  **Optional.** Enumerate ``CAPTURE`` formats via :c:func:`VIDIOC_ENUM_FMT` on
49262306a36Sopenharmony_ci    the ``CAPTURE`` queue. Once the stream information is parsed and known, the
49362306a36Sopenharmony_ci    client may use this ioctl to discover which raw formats are supported for
49462306a36Sopenharmony_ci    given stream and select one of them via :c:func:`VIDIOC_S_FMT`.
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci    .. important::
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci       The decoder will return only formats supported for the currently
49962306a36Sopenharmony_ci       established coded format, as per the ``OUTPUT`` format and/or stream
50062306a36Sopenharmony_ci       metadata parsed in this initialization sequence, even if more formats
50162306a36Sopenharmony_ci       may be supported by the decoder in general. In other words, the set
50262306a36Sopenharmony_ci       returned will be a subset of the initial query mentioned in the
50362306a36Sopenharmony_ci       `Querying Capabilities` section.
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci       For example, a decoder may support YUV and RGB formats for resolutions
50662306a36Sopenharmony_ci       1920x1088 and lower, but only YUV for higher resolutions (due to
50762306a36Sopenharmony_ci       hardware limitations). After parsing a resolution of 1920x1088 or lower,
50862306a36Sopenharmony_ci       :c:func:`VIDIOC_ENUM_FMT` may return a set of YUV and RGB pixel formats,
50962306a36Sopenharmony_ci       but after parsing resolution higher than 1920x1088, the decoder will not
51062306a36Sopenharmony_ci       return RGB, unsupported for this resolution.
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci       However, subsequent resolution change event triggered after
51362306a36Sopenharmony_ci       discovering a resolution change within the same stream may switch
51462306a36Sopenharmony_ci       the stream into a lower resolution and :c:func:`VIDIOC_ENUM_FMT`
51562306a36Sopenharmony_ci       would return RGB formats again in that case.
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci4.  **Optional.** Set the ``CAPTURE`` format via :c:func:`VIDIOC_S_FMT` on the
51862306a36Sopenharmony_ci    ``CAPTURE`` queue. The client may choose a different format than
51962306a36Sopenharmony_ci    selected/suggested by the decoder in :c:func:`VIDIOC_G_FMT`.
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci    * **Required fields:**
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci      ``type``
52462306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci      ``pixelformat``
52762306a36Sopenharmony_ci          a raw pixel format.
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci      ``width``, ``height``
53062306a36Sopenharmony_ci         frame buffer resolution of the decoded stream; typically unchanged from
53162306a36Sopenharmony_ci	 what was returned with :c:func:`VIDIOC_G_FMT`, but it may be different
53262306a36Sopenharmony_ci	 if the hardware supports composition and/or scaling.
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci   * Setting the ``CAPTURE`` format will reset the compose selection rectangles
53562306a36Sopenharmony_ci     to their default values, based on the new resolution, as described in the
53662306a36Sopenharmony_ci     previous step.
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci5. **Optional.** Set the compose rectangle via :c:func:`VIDIOC_S_SELECTION` on
53962306a36Sopenharmony_ci   the ``CAPTURE`` queue if it is desired and if the decoder has compose and/or
54062306a36Sopenharmony_ci   scaling capabilities.
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci   * **Required fields:**
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci     ``type``
54562306a36Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci     ``target``
54862306a36Sopenharmony_ci         set to ``V4L2_SEL_TGT_COMPOSE``.
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci     ``r.left``, ``r.top``, ``r.width``, ``r.height``
55162306a36Sopenharmony_ci         the rectangle inside a ``CAPTURE`` buffer into which the cropped
55262306a36Sopenharmony_ci         frame is written; defaults to ``V4L2_SEL_TGT_COMPOSE_DEFAULT``;
55362306a36Sopenharmony_ci         read-only on hardware without additional compose/scaling capabilities.
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ci   * **Returned fields:**
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci     ``r.left``, ``r.top``, ``r.width``, ``r.height``
55862306a36Sopenharmony_ci         the visible rectangle; it must fit within the frame buffer resolution
55962306a36Sopenharmony_ci         returned by :c:func:`VIDIOC_G_FMT` on ``CAPTURE``.
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci   .. warning::
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci      The decoder may adjust the compose rectangle to the nearest
56462306a36Sopenharmony_ci      supported one to meet codec and hardware requirements. The client needs
56562306a36Sopenharmony_ci      to check the adjusted rectangle returned by :c:func:`VIDIOC_S_SELECTION`.
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci6.  If all the following conditions are met, the client may resume the decoding
56862306a36Sopenharmony_ci    instantly:
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci    * ``sizeimage`` of the new format (determined in previous steps) is less
57162306a36Sopenharmony_ci      than or equal to the size of currently allocated buffers,
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci    * the number of buffers currently allocated is greater than or equal to the
57462306a36Sopenharmony_ci      minimum number of buffers acquired in previous steps. To fulfill this
57562306a36Sopenharmony_ci      requirement, the client may use :c:func:`VIDIOC_CREATE_BUFS` to add new
57662306a36Sopenharmony_ci      buffers.
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci    In that case, the remaining steps do not apply and the client may resume
57962306a36Sopenharmony_ci    the decoding by one of the following actions:
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci    * if the ``CAPTURE`` queue is streaming, call :c:func:`VIDIOC_DECODER_CMD`
58262306a36Sopenharmony_ci      with the ``V4L2_DEC_CMD_START`` command,
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci    * if the ``CAPTURE`` queue is not streaming, call :c:func:`VIDIOC_STREAMON`
58562306a36Sopenharmony_ci      on the ``CAPTURE`` queue.
58662306a36Sopenharmony_ci
58762306a36Sopenharmony_ci    However, if the client intends to change the buffer set, to lower
58862306a36Sopenharmony_ci    memory usage or for any other reasons, it may be achieved by following
58962306a36Sopenharmony_ci    the steps below.
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci7.  **If the** ``CAPTURE`` **queue is streaming,** keep queuing and dequeuing
59262306a36Sopenharmony_ci    buffers on the ``CAPTURE`` queue until a buffer marked with the
59362306a36Sopenharmony_ci    ``V4L2_BUF_FLAG_LAST`` flag is dequeued.
59462306a36Sopenharmony_ci
59562306a36Sopenharmony_ci8.  **If the** ``CAPTURE`` **queue is streaming,** call :c:func:`VIDIOC_STREAMOFF`
59662306a36Sopenharmony_ci    on the ``CAPTURE`` queue to stop streaming.
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci    .. warning::
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci       The ``OUTPUT`` queue must remain streaming. Calling
60162306a36Sopenharmony_ci       :c:func:`VIDIOC_STREAMOFF` on it would abort the sequence and trigger a
60262306a36Sopenharmony_ci       seek.
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ci9.  **If the** ``CAPTURE`` **queue has buffers allocated,** free the ``CAPTURE``
60562306a36Sopenharmony_ci    buffers using :c:func:`VIDIOC_REQBUFS`.
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci    * **Required fields:**
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci      ``count``
61062306a36Sopenharmony_ci          set to 0.
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci      ``type``
61362306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ci      ``memory``
61662306a36Sopenharmony_ci          follows standard semantics.
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci10. Allocate ``CAPTURE`` buffers via :c:func:`VIDIOC_REQBUFS` on the
61962306a36Sopenharmony_ci    ``CAPTURE`` queue.
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ci    * **Required fields:**
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci      ``count``
62462306a36Sopenharmony_ci          requested number of buffers to allocate; greater than zero.
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ci      ``type``
62762306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci      ``memory``
63062306a36Sopenharmony_ci          follows standard semantics.
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ci    * **Returned fields:**
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci      ``count``
63562306a36Sopenharmony_ci          actual number of buffers allocated.
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci    .. warning::
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci       The actual number of allocated buffers may differ from the ``count``
64062306a36Sopenharmony_ci       given. The client must check the updated value of ``count`` after the
64162306a36Sopenharmony_ci       call returns.
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci    .. note::
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci       To allocate more than the minimum number of buffers (for pipeline
64662306a36Sopenharmony_ci       depth), the client may query the ``V4L2_CID_MIN_BUFFERS_FOR_CAPTURE``
64762306a36Sopenharmony_ci       control to get the minimum number of buffers required, and pass the
64862306a36Sopenharmony_ci       obtained value plus the number of additional buffers needed in the
64962306a36Sopenharmony_ci       ``count`` field to :c:func:`VIDIOC_REQBUFS`.
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci    Alternatively, :c:func:`VIDIOC_CREATE_BUFS` on the ``CAPTURE`` queue can be
65262306a36Sopenharmony_ci    used to have more control over buffer allocation. For example, by
65362306a36Sopenharmony_ci    allocating buffers larger than the current ``CAPTURE`` format, future
65462306a36Sopenharmony_ci    resolution changes can be accommodated.
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ci    * **Required fields:**
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci      ``count``
65962306a36Sopenharmony_ci          requested number of buffers to allocate; greater than zero.
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ci      ``type``
66262306a36Sopenharmony_ci          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ci      ``memory``
66562306a36Sopenharmony_ci          follows standard semantics.
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_ci      ``format``
66862306a36Sopenharmony_ci          a format representing the maximum framebuffer resolution to be
66962306a36Sopenharmony_ci          accommodated by newly allocated buffers.
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci    * **Returned fields:**
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci      ``count``
67462306a36Sopenharmony_ci          adjusted to the number of allocated buffers.
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci    .. warning::
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci        The actual number of allocated buffers may differ from the ``count``
67962306a36Sopenharmony_ci        given. The client must check the updated value of ``count`` after the
68062306a36Sopenharmony_ci        call returns.
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_ci    .. note::
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci       To allocate buffers for a format different than parsed from the stream
68562306a36Sopenharmony_ci       metadata, the client must proceed as follows, before the metadata
68662306a36Sopenharmony_ci       parsing is initiated:
68762306a36Sopenharmony_ci
68862306a36Sopenharmony_ci       * set width and height of the ``OUTPUT`` format to desired coded resolution to
68962306a36Sopenharmony_ci         let the decoder configure the ``CAPTURE`` format appropriately,
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci       * query the ``CAPTURE`` format using :c:func:`VIDIOC_G_FMT` and save it
69262306a36Sopenharmony_ci         until this step.
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_ci       The format obtained in the query may be then used with
69562306a36Sopenharmony_ci       :c:func:`VIDIOC_CREATE_BUFS` in this step to allocate the buffers.
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ci11. Call :c:func:`VIDIOC_STREAMON` on the ``CAPTURE`` queue to start decoding
69862306a36Sopenharmony_ci    frames.
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ciDecoding
70162306a36Sopenharmony_ci========
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ciThis state is reached after the `Capture Setup` sequence finishes successfully.
70462306a36Sopenharmony_ciIn this state, the client queues and dequeues buffers to both queues via
70562306a36Sopenharmony_ci:c:func:`VIDIOC_QBUF` and :c:func:`VIDIOC_DQBUF`, following the standard
70662306a36Sopenharmony_cisemantics.
70762306a36Sopenharmony_ci
70862306a36Sopenharmony_ciThe content of the source ``OUTPUT`` buffers depends on the active coded pixel
70962306a36Sopenharmony_ciformat and may be affected by codec-specific extended controls, as stated in
71062306a36Sopenharmony_cithe documentation of each format.
71162306a36Sopenharmony_ci
71262306a36Sopenharmony_ciBoth queues operate independently, following the standard behavior of V4L2
71362306a36Sopenharmony_cibuffer queues and memory-to-memory devices. In addition, the order of decoded
71462306a36Sopenharmony_ciframes dequeued from the ``CAPTURE`` queue may differ from the order of queuing
71562306a36Sopenharmony_cicoded frames to the ``OUTPUT`` queue, due to properties of the selected coded
71662306a36Sopenharmony_ciformat, e.g. frame reordering.
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ciThe client must not assume any direct relationship between ``CAPTURE``
71962306a36Sopenharmony_ciand ``OUTPUT`` buffers and any specific timing of buffers becoming
72062306a36Sopenharmony_ciavailable to dequeue. Specifically:
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci* a buffer queued to ``OUTPUT`` may result in no buffers being produced
72362306a36Sopenharmony_ci  on ``CAPTURE`` (e.g. if it does not contain encoded data, or if only
72462306a36Sopenharmony_ci  metadata syntax structures are present in it),
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci* a buffer queued to ``OUTPUT`` may result in more than one buffer produced
72762306a36Sopenharmony_ci  on ``CAPTURE`` (if the encoded data contained more than one frame, or if
72862306a36Sopenharmony_ci  returning a decoded frame allowed the decoder to return a frame that
72962306a36Sopenharmony_ci  preceded it in decode, but succeeded it in the display order),
73062306a36Sopenharmony_ci
73162306a36Sopenharmony_ci* a buffer queued to ``OUTPUT`` may result in a buffer being produced on
73262306a36Sopenharmony_ci  ``CAPTURE`` later into decode process, and/or after processing further
73362306a36Sopenharmony_ci  ``OUTPUT`` buffers, or be returned out of order, e.g. if display
73462306a36Sopenharmony_ci  reordering is used,
73562306a36Sopenharmony_ci
73662306a36Sopenharmony_ci* buffers may become available on the ``CAPTURE`` queue without additional
73762306a36Sopenharmony_ci  buffers queued to ``OUTPUT`` (e.g. during drain or ``EOS``), because of the
73862306a36Sopenharmony_ci  ``OUTPUT`` buffers queued in the past whose decoding results are only
73962306a36Sopenharmony_ci  available at later time, due to specifics of the decoding process.
74062306a36Sopenharmony_ci
74162306a36Sopenharmony_ci.. note::
74262306a36Sopenharmony_ci
74362306a36Sopenharmony_ci   To allow matching decoded ``CAPTURE`` buffers with ``OUTPUT`` buffers they
74462306a36Sopenharmony_ci   originated from, the client can set the ``timestamp`` field of the
74562306a36Sopenharmony_ci   :c:type:`v4l2_buffer` struct when queuing an ``OUTPUT`` buffer. The
74662306a36Sopenharmony_ci   ``CAPTURE`` buffer(s), which resulted from decoding that ``OUTPUT`` buffer
74762306a36Sopenharmony_ci   will have their ``timestamp`` field set to the same value when dequeued.
74862306a36Sopenharmony_ci
74962306a36Sopenharmony_ci   In addition to the straightforward case of one ``OUTPUT`` buffer producing
75062306a36Sopenharmony_ci   one ``CAPTURE`` buffer, the following cases are defined:
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_ci   * one ``OUTPUT`` buffer generates multiple ``CAPTURE`` buffers: the same
75362306a36Sopenharmony_ci     ``OUTPUT`` timestamp will be copied to multiple ``CAPTURE`` buffers.
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci   * multiple ``OUTPUT`` buffers generate one ``CAPTURE`` buffer: timestamp of
75662306a36Sopenharmony_ci     the ``OUTPUT`` buffer queued first will be copied.
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ci   * the decoding order differs from the display order (i.e. the ``CAPTURE``
75962306a36Sopenharmony_ci     buffers are out-of-order compared to the ``OUTPUT`` buffers): ``CAPTURE``
76062306a36Sopenharmony_ci     timestamps will not retain the order of ``OUTPUT`` timestamps.
76162306a36Sopenharmony_ci
76262306a36Sopenharmony_ci.. note::
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci   The backing memory of ``CAPTURE`` buffers that are used as reference frames
76562306a36Sopenharmony_ci   by the stream may be read by the hardware even after they are dequeued.
76662306a36Sopenharmony_ci   Consequently, the client should avoid writing into this memory while the
76762306a36Sopenharmony_ci   ``CAPTURE`` queue is streaming. Failure to observe this may result in
76862306a36Sopenharmony_ci   corruption of decoded frames.
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci   Similarly, when using a memory type other than ``V4L2_MEMORY_MMAP``, the
77162306a36Sopenharmony_ci   client should make sure that each ``CAPTURE`` buffer is always queued with
77262306a36Sopenharmony_ci   the same backing memory for as long as the ``CAPTURE`` queue is streaming.
77362306a36Sopenharmony_ci   The reason for this is that V4L2 buffer indices can be used by drivers to
77462306a36Sopenharmony_ci   identify frames. Thus, if the backing memory of a reference frame is
77562306a36Sopenharmony_ci   submitted under a different buffer ID, the driver may misidentify it and
77662306a36Sopenharmony_ci   decode a new frame into it while it is still in use, resulting in corruption
77762306a36Sopenharmony_ci   of the following frames.
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ciDuring the decoding, the decoder may initiate one of the special sequences, as
78062306a36Sopenharmony_cilisted below. The sequences will result in the decoder returning all the
78162306a36Sopenharmony_ci``CAPTURE`` buffers that originated from all the ``OUTPUT`` buffers processed
78262306a36Sopenharmony_cibefore the sequence started. Last of the buffers will have the
78362306a36Sopenharmony_ci``V4L2_BUF_FLAG_LAST`` flag set. To determine the sequence to follow, the client
78462306a36Sopenharmony_cimust check if there is any pending event and:
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci* if a ``V4L2_EVENT_SOURCE_CHANGE`` event with ``changes`` set to
78762306a36Sopenharmony_ci  ``V4L2_EVENT_SRC_CH_RESOLUTION`` is pending, the `Dynamic Resolution
78862306a36Sopenharmony_ci  Change` sequence needs to be followed,
78962306a36Sopenharmony_ci
79062306a36Sopenharmony_ci* if a ``V4L2_EVENT_EOS`` event is pending, the `End of Stream` sequence needs
79162306a36Sopenharmony_ci  to be followed.
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ciSome of the sequences can be intermixed with each other and need to be handled
79462306a36Sopenharmony_cias they happen. The exact operation is documented for each sequence.
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_ciShould a decoding error occur, it will be reported to the client with the level
79762306a36Sopenharmony_ciof details depending on the decoder capabilities. Specifically:
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci* the CAPTURE buffer that contains the results of the failed decode operation
80062306a36Sopenharmony_ci  will be returned with the V4L2_BUF_FLAG_ERROR flag set,
80162306a36Sopenharmony_ci
80262306a36Sopenharmony_ci* if the decoder is able to precisely report the OUTPUT buffer that triggered
80362306a36Sopenharmony_ci  the error, such buffer will be returned with the V4L2_BUF_FLAG_ERROR flag
80462306a36Sopenharmony_ci  set.
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ciIn case of a fatal failure that does not allow the decoding to continue, any
80762306a36Sopenharmony_cifurther operations on corresponding decoder file handle will return the -EIO
80862306a36Sopenharmony_cierror code. The client may close the file handle and open a new one, or
80962306a36Sopenharmony_cialternatively reinitialize the instance by stopping streaming on both queues,
81062306a36Sopenharmony_cireleasing all buffers and performing the Initialization sequence again.
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ciSeek
81362306a36Sopenharmony_ci====
81462306a36Sopenharmony_ci
81562306a36Sopenharmony_ciSeek is controlled by the ``OUTPUT`` queue, as it is the source of coded data.
81662306a36Sopenharmony_ciThe seek does not require any specific operation on the ``CAPTURE`` queue, but
81762306a36Sopenharmony_ciit may be affected as per normal decoder operation.
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci1. Stop the ``OUTPUT`` queue to begin the seek sequence via
82062306a36Sopenharmony_ci   :c:func:`VIDIOC_STREAMOFF`.
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_ci   * **Required fields:**
82362306a36Sopenharmony_ci
82462306a36Sopenharmony_ci     ``type``
82562306a36Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci   * The decoder will drop all the pending ``OUTPUT`` buffers and they must be
82862306a36Sopenharmony_ci     treated as returned to the client (following standard semantics).
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci2. Restart the ``OUTPUT`` queue via :c:func:`VIDIOC_STREAMON`.
83162306a36Sopenharmony_ci
83262306a36Sopenharmony_ci   * **Required fields:**
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_ci     ``type``
83562306a36Sopenharmony_ci         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_ci   * The decoder will start accepting new source bytestream buffers after the
83862306a36Sopenharmony_ci     call returns.
83962306a36Sopenharmony_ci
84062306a36Sopenharmony_ci3. Start queuing buffers containing coded data after the seek to the ``OUTPUT``
84162306a36Sopenharmony_ci   queue until a suitable resume point is found.
84262306a36Sopenharmony_ci
84362306a36Sopenharmony_ci   .. note::
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ci      There is no requirement to begin queuing coded data starting exactly
84662306a36Sopenharmony_ci      from a resume point (e.g. SPS or a keyframe). Any queued ``OUTPUT``
84762306a36Sopenharmony_ci      buffers will be processed and returned to the client until a suitable
84862306a36Sopenharmony_ci      resume point is found.  While looking for a resume point, the decoder
84962306a36Sopenharmony_ci      should not produce any decoded frames into ``CAPTURE`` buffers.
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_ci      Some hardware is known to mishandle seeks to a non-resume point. Such an
85262306a36Sopenharmony_ci      operation may result in an unspecified number of corrupted decoded frames
85362306a36Sopenharmony_ci      being made available on the ``CAPTURE`` queue. Drivers must ensure that
85462306a36Sopenharmony_ci      no fatal decoding errors or crashes occur, and implement any necessary
85562306a36Sopenharmony_ci      handling and workarounds for hardware issues related to seek operations.
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_ci   .. warning::
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ci      In case of the H.264/HEVC codec, the client must take care not to seek
86062306a36Sopenharmony_ci      over a change of SPS/PPS. Even though the target frame could be a
86162306a36Sopenharmony_ci      keyframe, the stale SPS/PPS inside decoder state would lead to undefined
86262306a36Sopenharmony_ci      results when decoding. Although the decoder must handle that case without
86362306a36Sopenharmony_ci      a crash or a fatal decode error, the client must not expect a sensible
86462306a36Sopenharmony_ci      decode output.
86562306a36Sopenharmony_ci
86662306a36Sopenharmony_ci      If the hardware can detect such corrupted decoded frames, then
86762306a36Sopenharmony_ci      corresponding buffers will be returned to the client with the
86862306a36Sopenharmony_ci      V4L2_BUF_FLAG_ERROR set. See the `Decoding` section for further
86962306a36Sopenharmony_ci      description of decode error reporting.
87062306a36Sopenharmony_ci
87162306a36Sopenharmony_ci4. After a resume point is found, the decoder will start returning ``CAPTURE``
87262306a36Sopenharmony_ci   buffers containing decoded frames.
87362306a36Sopenharmony_ci
87462306a36Sopenharmony_ci.. important::
87562306a36Sopenharmony_ci
87662306a36Sopenharmony_ci   A seek may result in the `Dynamic Resolution Change` sequence being
87762306a36Sopenharmony_ci   initiated, due to the seek target having decoding parameters different from
87862306a36Sopenharmony_ci   the part of the stream decoded before the seek. The sequence must be handled
87962306a36Sopenharmony_ci   as per normal decoder operation.
88062306a36Sopenharmony_ci
88162306a36Sopenharmony_ci.. warning::
88262306a36Sopenharmony_ci
88362306a36Sopenharmony_ci   It is not specified when the ``CAPTURE`` queue starts producing buffers
88462306a36Sopenharmony_ci   containing decoded data from the ``OUTPUT`` buffers queued after the seek,
88562306a36Sopenharmony_ci   as it operates independently from the ``OUTPUT`` queue.
88662306a36Sopenharmony_ci
88762306a36Sopenharmony_ci   The decoder may return a number of remaining ``CAPTURE`` buffers containing
88862306a36Sopenharmony_ci   decoded frames originating from the ``OUTPUT`` buffers queued before the
88962306a36Sopenharmony_ci   seek sequence is performed.
89062306a36Sopenharmony_ci
89162306a36Sopenharmony_ci   The ``VIDIOC_STREAMOFF`` operation discards any remaining queued
89262306a36Sopenharmony_ci   ``OUTPUT`` buffers, which means that not all of the ``OUTPUT`` buffers
89362306a36Sopenharmony_ci   queued before the seek sequence may have matching ``CAPTURE`` buffers
89462306a36Sopenharmony_ci   produced.  For example, given the sequence of operations on the
89562306a36Sopenharmony_ci   ``OUTPUT`` queue:
89662306a36Sopenharmony_ci
89762306a36Sopenharmony_ci     QBUF(A), QBUF(B), STREAMOFF(), STREAMON(), QBUF(G), QBUF(H),
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci   any of the following results on the ``CAPTURE`` queue is allowed:
90062306a36Sopenharmony_ci
90162306a36Sopenharmony_ci     {A', B', G', H'}, {A', G', H'}, {G', H'}.
90262306a36Sopenharmony_ci
90362306a36Sopenharmony_ci   To determine the CAPTURE buffer containing the first decoded frame after the
90462306a36Sopenharmony_ci   seek, the client may observe the timestamps to match the CAPTURE and OUTPUT
90562306a36Sopenharmony_ci   buffers or use V4L2_DEC_CMD_STOP and V4L2_DEC_CMD_START to drain the
90662306a36Sopenharmony_ci   decoder.
90762306a36Sopenharmony_ci
90862306a36Sopenharmony_ci.. note::
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_ci   To achieve instantaneous seek, the client may restart streaming on the
91162306a36Sopenharmony_ci   ``CAPTURE`` queue too to discard decoded, but not yet dequeued buffers.
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ciDynamic Resolution Change
91462306a36Sopenharmony_ci=========================
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ciStreams that include resolution metadata in the bytestream may require switching
91762306a36Sopenharmony_cito a different resolution during the decoding.
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_ci.. note::
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci   Not all decoders can detect resolution changes. Those that do set the
92262306a36Sopenharmony_ci   ``V4L2_FMT_FLAG_DYN_RESOLUTION`` flag for the coded format when
92362306a36Sopenharmony_ci   :c:func:`VIDIOC_ENUM_FMT` is called.
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_ciThe sequence starts when the decoder detects a coded frame with one or more of
92662306a36Sopenharmony_cithe following parameters different from those previously established (and
92762306a36Sopenharmony_cireflected by corresponding queries):
92862306a36Sopenharmony_ci
92962306a36Sopenharmony_ci* coded resolution (``OUTPUT`` width and height),
93062306a36Sopenharmony_ci
93162306a36Sopenharmony_ci* visible resolution (selection rectangles),
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ci* the minimum number of buffers needed for decoding,
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_ci* bit-depth of the bitstream has been changed.
93662306a36Sopenharmony_ci
93762306a36Sopenharmony_ciWhenever that happens, the decoder must proceed as follows:
93862306a36Sopenharmony_ci
93962306a36Sopenharmony_ci1.  After encountering a resolution change in the stream, the decoder sends a
94062306a36Sopenharmony_ci    ``V4L2_EVENT_SOURCE_CHANGE`` event with ``changes`` set to
94162306a36Sopenharmony_ci    ``V4L2_EVENT_SRC_CH_RESOLUTION``.
94262306a36Sopenharmony_ci
94362306a36Sopenharmony_ci    .. important::
94462306a36Sopenharmony_ci
94562306a36Sopenharmony_ci       Any client query issued after the decoder queues the event will return
94662306a36Sopenharmony_ci       values applying to the stream after the resolution change, including
94762306a36Sopenharmony_ci       queue formats, selection rectangles and controls.
94862306a36Sopenharmony_ci
94962306a36Sopenharmony_ci2.  The decoder will then process and decode all remaining buffers from before
95062306a36Sopenharmony_ci    the resolution change point.
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_ci    * The last buffer from before the change must be marked with the
95362306a36Sopenharmony_ci      ``V4L2_BUF_FLAG_LAST`` flag, similarly to the `Drain` sequence above.
95462306a36Sopenharmony_ci
95562306a36Sopenharmony_ci    .. warning::
95662306a36Sopenharmony_ci
95762306a36Sopenharmony_ci       The last buffer may be empty (with :c:type:`v4l2_buffer` ``bytesused``
95862306a36Sopenharmony_ci       = 0) and in that case it must be ignored by the client, as it does not
95962306a36Sopenharmony_ci       contain a decoded frame.
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_ci    .. note::
96262306a36Sopenharmony_ci
96362306a36Sopenharmony_ci       Any attempt to dequeue more ``CAPTURE`` buffers beyond the buffer marked
96462306a36Sopenharmony_ci       with ``V4L2_BUF_FLAG_LAST`` will result in a -EPIPE error from
96562306a36Sopenharmony_ci       :c:func:`VIDIOC_DQBUF`.
96662306a36Sopenharmony_ci
96762306a36Sopenharmony_ciThe client must continue the sequence as described below to continue the
96862306a36Sopenharmony_cidecoding process.
96962306a36Sopenharmony_ci
97062306a36Sopenharmony_ci1.  Dequeue the source change event.
97162306a36Sopenharmony_ci
97262306a36Sopenharmony_ci    .. important::
97362306a36Sopenharmony_ci
97462306a36Sopenharmony_ci       A source change triggers an implicit decoder drain, similar to the
97562306a36Sopenharmony_ci       explicit `Drain` sequence. The decoder is stopped after it completes.
97662306a36Sopenharmony_ci       The decoding process must be resumed with either a pair of calls to
97762306a36Sopenharmony_ci       :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the
97862306a36Sopenharmony_ci       ``CAPTURE`` queue, or a call to :c:func:`VIDIOC_DECODER_CMD` with the
97962306a36Sopenharmony_ci       ``V4L2_DEC_CMD_START`` command.
98062306a36Sopenharmony_ci
98162306a36Sopenharmony_ci2.  Continue with the `Capture Setup` sequence.
98262306a36Sopenharmony_ci
98362306a36Sopenharmony_ci.. note::
98462306a36Sopenharmony_ci
98562306a36Sopenharmony_ci   During the resolution change sequence, the ``OUTPUT`` queue must remain
98662306a36Sopenharmony_ci   streaming. Calling :c:func:`VIDIOC_STREAMOFF` on the ``OUTPUT`` queue would
98762306a36Sopenharmony_ci   abort the sequence and initiate a seek.
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci   In principle, the ``OUTPUT`` queue operates separately from the ``CAPTURE``
99062306a36Sopenharmony_ci   queue and this remains true for the duration of the entire resolution change
99162306a36Sopenharmony_ci   sequence as well.
99262306a36Sopenharmony_ci
99362306a36Sopenharmony_ci   The client should, for best performance and simplicity, keep queuing/dequeuing
99462306a36Sopenharmony_ci   buffers to/from the ``OUTPUT`` queue even while processing this sequence.
99562306a36Sopenharmony_ci
99662306a36Sopenharmony_ciDrain
99762306a36Sopenharmony_ci=====
99862306a36Sopenharmony_ci
99962306a36Sopenharmony_ciTo ensure that all queued ``OUTPUT`` buffers have been processed and related
100062306a36Sopenharmony_ci``CAPTURE`` buffers are given to the client, the client must follow the drain
100162306a36Sopenharmony_cisequence described below. After the drain sequence ends, the client has
100262306a36Sopenharmony_cireceived all decoded frames for all ``OUTPUT`` buffers queued before the
100362306a36Sopenharmony_cisequence was started.
100462306a36Sopenharmony_ci
100562306a36Sopenharmony_ci1. Begin drain by issuing :c:func:`VIDIOC_DECODER_CMD`.
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_ci   * **Required fields:**
100862306a36Sopenharmony_ci
100962306a36Sopenharmony_ci     ``cmd``
101062306a36Sopenharmony_ci         set to ``V4L2_DEC_CMD_STOP``.
101162306a36Sopenharmony_ci
101262306a36Sopenharmony_ci     ``flags``
101362306a36Sopenharmony_ci         set to 0.
101462306a36Sopenharmony_ci
101562306a36Sopenharmony_ci     ``pts``
101662306a36Sopenharmony_ci         set to 0.
101762306a36Sopenharmony_ci
101862306a36Sopenharmony_ci   .. warning::
101962306a36Sopenharmony_ci
102062306a36Sopenharmony_ci      The sequence can be only initiated if both ``OUTPUT`` and ``CAPTURE``
102162306a36Sopenharmony_ci      queues are streaming. For compatibility reasons, the call to
102262306a36Sopenharmony_ci      :c:func:`VIDIOC_DECODER_CMD` will not fail even if any of the queues is
102362306a36Sopenharmony_ci      not streaming, but at the same time it will not initiate the `Drain`
102462306a36Sopenharmony_ci      sequence and so the steps described below would not be applicable.
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_ci2. Any ``OUTPUT`` buffers queued by the client before the
102762306a36Sopenharmony_ci   :c:func:`VIDIOC_DECODER_CMD` was issued will be processed and decoded as
102862306a36Sopenharmony_ci   normal. The client must continue to handle both queues independently,
102962306a36Sopenharmony_ci   similarly to normal decode operation. This includes:
103062306a36Sopenharmony_ci
103162306a36Sopenharmony_ci   * handling any operations triggered as a result of processing those buffers,
103262306a36Sopenharmony_ci     such as the `Dynamic Resolution Change` sequence, before continuing with
103362306a36Sopenharmony_ci     the drain sequence,
103462306a36Sopenharmony_ci
103562306a36Sopenharmony_ci   * queuing and dequeuing ``CAPTURE`` buffers, until a buffer marked with the
103662306a36Sopenharmony_ci     ``V4L2_BUF_FLAG_LAST`` flag is dequeued,
103762306a36Sopenharmony_ci
103862306a36Sopenharmony_ci     .. warning::
103962306a36Sopenharmony_ci
104062306a36Sopenharmony_ci        The last buffer may be empty (with :c:type:`v4l2_buffer`
104162306a36Sopenharmony_ci        ``bytesused`` = 0) and in that case it must be ignored by the client,
104262306a36Sopenharmony_ci        as it does not contain a decoded frame.
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci     .. note::
104562306a36Sopenharmony_ci
104662306a36Sopenharmony_ci        Any attempt to dequeue more ``CAPTURE`` buffers beyond the buffer
104762306a36Sopenharmony_ci        marked with ``V4L2_BUF_FLAG_LAST`` will result in a -EPIPE error from
104862306a36Sopenharmony_ci        :c:func:`VIDIOC_DQBUF`.
104962306a36Sopenharmony_ci
105062306a36Sopenharmony_ci   * dequeuing processed ``OUTPUT`` buffers, until all the buffers queued
105162306a36Sopenharmony_ci     before the ``V4L2_DEC_CMD_STOP`` command are dequeued,
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci   * dequeuing the ``V4L2_EVENT_EOS`` event, if the client subscribed to it.
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ci   .. note::
105662306a36Sopenharmony_ci
105762306a36Sopenharmony_ci      For backwards compatibility, the decoder will signal a ``V4L2_EVENT_EOS``
105862306a36Sopenharmony_ci      event when the last frame has been decoded and all frames are ready to be
105962306a36Sopenharmony_ci      dequeued. It is a deprecated behavior and the client must not rely on it.
106062306a36Sopenharmony_ci      The ``V4L2_BUF_FLAG_LAST`` buffer flag should be used instead.
106162306a36Sopenharmony_ci
106262306a36Sopenharmony_ci3. Once all the ``OUTPUT`` buffers queued before the ``V4L2_DEC_CMD_STOP`` call
106362306a36Sopenharmony_ci   are dequeued and the last ``CAPTURE`` buffer is dequeued, the decoder is
106462306a36Sopenharmony_ci   stopped and it will accept, but not process, any newly queued ``OUTPUT``
106562306a36Sopenharmony_ci   buffers until the client issues any of the following operations:
106662306a36Sopenharmony_ci
106762306a36Sopenharmony_ci   * ``V4L2_DEC_CMD_START`` - the decoder will not be reset and will resume
106862306a36Sopenharmony_ci     operation normally, with all the state from before the drain,
106962306a36Sopenharmony_ci
107062306a36Sopenharmony_ci   * a pair of :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the
107162306a36Sopenharmony_ci     ``CAPTURE`` queue - the decoder will resume the operation normally,
107262306a36Sopenharmony_ci     however any ``CAPTURE`` buffers still in the queue will be returned to the
107362306a36Sopenharmony_ci     client,
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_ci   * a pair of :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the
107662306a36Sopenharmony_ci     ``OUTPUT`` queue - any pending source buffers will be returned to the
107762306a36Sopenharmony_ci     client and the `Seek` sequence will be triggered.
107862306a36Sopenharmony_ci
107962306a36Sopenharmony_ci.. note::
108062306a36Sopenharmony_ci
108162306a36Sopenharmony_ci   Once the drain sequence is initiated, the client needs to drive it to
108262306a36Sopenharmony_ci   completion, as described by the steps above, unless it aborts the process by
108362306a36Sopenharmony_ci   issuing :c:func:`VIDIOC_STREAMOFF` on any of the ``OUTPUT`` or ``CAPTURE``
108462306a36Sopenharmony_ci   queues.  The client is not allowed to issue ``V4L2_DEC_CMD_START`` or
108562306a36Sopenharmony_ci   ``V4L2_DEC_CMD_STOP`` again while the drain sequence is in progress and they
108662306a36Sopenharmony_ci   will fail with -EBUSY error code if attempted.
108762306a36Sopenharmony_ci
108862306a36Sopenharmony_ci   Although not mandatory, the availability of decoder commands may be queried
108962306a36Sopenharmony_ci   using :c:func:`VIDIOC_TRY_DECODER_CMD`.
109062306a36Sopenharmony_ci
109162306a36Sopenharmony_ciEnd of Stream
109262306a36Sopenharmony_ci=============
109362306a36Sopenharmony_ci
109462306a36Sopenharmony_ciIf the decoder encounters an end of stream marking in the stream, the decoder
109562306a36Sopenharmony_ciwill initiate the `Drain` sequence, which the client must handle as described
109662306a36Sopenharmony_ciabove, skipping the initial :c:func:`VIDIOC_DECODER_CMD`.
109762306a36Sopenharmony_ci
109862306a36Sopenharmony_ciCommit Points
109962306a36Sopenharmony_ci=============
110062306a36Sopenharmony_ci
110162306a36Sopenharmony_ciSetting formats and allocating buffers trigger changes in the behavior of the
110262306a36Sopenharmony_cidecoder.
110362306a36Sopenharmony_ci
110462306a36Sopenharmony_ci1. Setting the format on the ``OUTPUT`` queue may change the set of formats
110562306a36Sopenharmony_ci   supported/advertised on the ``CAPTURE`` queue. In particular, it also means
110662306a36Sopenharmony_ci   that the ``CAPTURE`` format may be reset and the client must not rely on the
110762306a36Sopenharmony_ci   previously set format being preserved.
110862306a36Sopenharmony_ci
110962306a36Sopenharmony_ci2. Enumerating formats on the ``CAPTURE`` queue always returns only formats
111062306a36Sopenharmony_ci   supported for the current ``OUTPUT`` format.
111162306a36Sopenharmony_ci
111262306a36Sopenharmony_ci3. Setting the format on the ``CAPTURE`` queue does not change the list of
111362306a36Sopenharmony_ci   formats available on the ``OUTPUT`` queue. An attempt to set a ``CAPTURE``
111462306a36Sopenharmony_ci   format that is not supported for the currently selected ``OUTPUT`` format
111562306a36Sopenharmony_ci   will result in the decoder adjusting the requested ``CAPTURE`` format to a
111662306a36Sopenharmony_ci   supported one.
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci4. Enumerating formats on the ``OUTPUT`` queue always returns the full set of
111962306a36Sopenharmony_ci   supported coded formats, irrespectively of the current ``CAPTURE`` format.
112062306a36Sopenharmony_ci
112162306a36Sopenharmony_ci5. While buffers are allocated on any of the ``OUTPUT`` or ``CAPTURE`` queues,
112262306a36Sopenharmony_ci   the client must not change the format on the ``OUTPUT`` queue. Drivers will
112362306a36Sopenharmony_ci   return the -EBUSY error code for any such format change attempt.
112462306a36Sopenharmony_ci
112562306a36Sopenharmony_ciTo summarize, setting formats and allocation must always start with the
112662306a36Sopenharmony_ci``OUTPUT`` queue and the ``OUTPUT`` queue is the master that governs the
112762306a36Sopenharmony_ciset of supported formats for the ``CAPTURE`` queue.
1128