0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 .. _stateless_decoder:
0004
0005 **************************************************
0006 Memory-to-memory Stateless Video Decoder Interface
0007 **************************************************
0008
0009 A stateless decoder is a decoder that works without retaining any kind of state
0010 between processed frames. This means that each frame is decoded independently
0011 of any previous and future frames, and that the client is responsible for
0012 maintaining the decoding state and providing it to the decoder with each
0013 decoding request. This is in contrast to the stateful video decoder interface,
0014 where the hardware and driver maintain the decoding state and all the client
0015 has to do is to provide the raw encoded stream and dequeue decoded frames in
0016 display order.
0017
0018 This section describes how user-space ("the client") is expected to communicate
0019 with stateless decoders in order to successfully decode an encoded stream.
0020 Compared to stateful codecs, the decoder/client sequence is simpler, but the
0021 cost of this simplicity is extra complexity in the client which is responsible
0022 for maintaining a consistent decoding state.
0023
0024 Stateless decoders make use of the :ref:`media-request-api`. A stateless
0025 decoder must expose the ``V4L2_BUF_CAP_SUPPORTS_REQUESTS`` capability on its
0026 ``OUTPUT`` queue when :c:func:`VIDIOC_REQBUFS` or :c:func:`VIDIOC_CREATE_BUFS`
0027 are invoked.
0028
0029 Depending on the encoded formats supported by the decoder, a single decoded
0030 frame may be the result of several decode requests (for instance, H.264 streams
0031 with multiple slices per frame). Decoders that support such formats must also
0032 expose the ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF`` capability on their
0033 ``OUTPUT`` queue.
0034
0035 Querying capabilities
0036 =====================
0037
0038 1. To enumerate the set of coded formats supported by the decoder, the client
0039 calls :c:func:`VIDIOC_ENUM_FMT` on the ``OUTPUT`` queue.
0040
0041 * The driver must always return the full set of supported ``OUTPUT`` formats,
0042 irrespective of the format currently set on the ``CAPTURE`` queue.
0043
0044 * Simultaneously, the driver must restrain the set of values returned by
0045 codec-specific capability controls (such as H.264 profiles) to the set
0046 actually supported by the hardware.
0047
0048 2. To enumerate the set of supported raw formats, the client calls
0049 :c:func:`VIDIOC_ENUM_FMT` on the ``CAPTURE`` queue.
0050
0051 * The driver must return only the formats supported for the format currently
0052 active on the ``OUTPUT`` queue.
0053
0054 * Depending on the currently set ``OUTPUT`` format, the set of supported raw
0055 formats may depend on the value of some codec-dependent controls.
0056 The client is responsible for making sure that these controls are set
0057 before querying the ``CAPTURE`` queue. Failure to do so will result in the
0058 default values for these controls being used, and a returned set of formats
0059 that may not be usable for the media the client is trying to decode.
0060
0061 3. The client may use :c:func:`VIDIOC_ENUM_FRAMESIZES` to detect supported
0062 resolutions for a given format, passing desired pixel format in
0063 :c:type:`v4l2_frmsizeenum`'s ``pixel_format``.
0064
0065 4. Supported profiles and levels for the current ``OUTPUT`` format, if
0066 applicable, may be queried using their respective controls via
0067 :c:func:`VIDIOC_QUERYCTRL`.
0068
0069 Initialization
0070 ==============
0071
0072 1. Set the coded format on the ``OUTPUT`` queue via :c:func:`VIDIOC_S_FMT`.
0073
0074 * **Required fields:**
0075
0076 ``type``
0077 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
0078
0079 ``pixelformat``
0080 a coded pixel format.
0081
0082 ``width``, ``height``
0083 coded width and height parsed from the stream.
0084
0085 other fields
0086 follow standard semantics.
0087
0088 .. note::
0089
0090 Changing the ``OUTPUT`` format may change the currently set ``CAPTURE``
0091 format. The driver will derive a new ``CAPTURE`` format from the
0092 ``OUTPUT`` format being set, including resolution, colorimetry
0093 parameters, etc. If the client needs a specific ``CAPTURE`` format,
0094 it must adjust it afterwards.
0095
0096 2. Call :c:func:`VIDIOC_S_EXT_CTRLS` to set all the controls (parsed headers,
0097 etc.) required by the ``OUTPUT`` format to enumerate the ``CAPTURE`` formats.
0098
0099 3. Call :c:func:`VIDIOC_G_FMT` for ``CAPTURE`` queue to get the format for the
0100 destination buffers parsed/decoded from the bytestream.
0101
0102 * **Required fields:**
0103
0104 ``type``
0105 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
0106
0107 * **Returned fields:**
0108
0109 ``width``, ``height``
0110 frame buffer resolution for the decoded frames.
0111
0112 ``pixelformat``
0113 pixel format for decoded frames.
0114
0115 ``num_planes`` (for _MPLANE ``type`` only)
0116 number of planes for pixelformat.
0117
0118 ``sizeimage``, ``bytesperline``
0119 as per standard semantics; matching frame buffer format.
0120
0121 .. note::
0122
0123 The value of ``pixelformat`` may be any pixel format supported for the
0124 ``OUTPUT`` format, based on the hardware capabilities. It is suggested
0125 that the driver chooses the preferred/optimal format for the current
0126 configuration. For example, a YUV format may be preferred over an RGB
0127 format, if an additional conversion step would be required for RGB.
0128
0129 4. *[optional]* Enumerate ``CAPTURE`` formats via :c:func:`VIDIOC_ENUM_FMT` on
0130 the ``CAPTURE`` queue. The client may use this ioctl to discover which
0131 alternative raw formats are supported for the current ``OUTPUT`` format and
0132 select one of them via :c:func:`VIDIOC_S_FMT`.
0133
0134 .. note::
0135
0136 The driver will return only formats supported for the currently selected
0137 ``OUTPUT`` format and currently set controls, even if more formats may be
0138 supported by the decoder in general.
0139
0140 For example, a decoder may support YUV and RGB formats for
0141 resolutions 1920x1088 and lower, but only YUV for higher resolutions (due
0142 to hardware limitations). After setting a resolution of 1920x1088 or lower
0143 as the ``OUTPUT`` format, :c:func:`VIDIOC_ENUM_FMT` may return a set of
0144 YUV and RGB pixel formats, but after setting a resolution higher than
0145 1920x1088, the driver will not return RGB pixel formats, since they are
0146 unsupported for this resolution.
0147
0148 5. *[optional]* Choose a different ``CAPTURE`` format than suggested via
0149 :c:func:`VIDIOC_S_FMT` on ``CAPTURE`` queue. It is possible for the client to
0150 choose a different format than selected/suggested by the driver in
0151 :c:func:`VIDIOC_G_FMT`.
0152
0153 * **Required fields:**
0154
0155 ``type``
0156 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
0157
0158 ``pixelformat``
0159 a raw pixel format.
0160
0161 ``width``, ``height``
0162 frame buffer resolution of the decoded stream; typically unchanged from
0163 what was returned with :c:func:`VIDIOC_G_FMT`, but it may be different
0164 if the hardware supports composition and/or scaling.
0165
0166 After performing this step, the client must perform step 3 again in order
0167 to obtain up-to-date information about the buffers size and layout.
0168
0169 6. Allocate source (bytestream) buffers via :c:func:`VIDIOC_REQBUFS` on
0170 ``OUTPUT`` queue.
0171
0172 * **Required fields:**
0173
0174 ``count``
0175 requested number of buffers to allocate; greater than zero.
0176
0177 ``type``
0178 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
0179
0180 ``memory``
0181 follows standard semantics.
0182
0183 * **Return fields:**
0184
0185 ``count``
0186 actual number of buffers allocated.
0187
0188 * If required, the driver will adjust ``count`` to be equal or bigger to the
0189 minimum of required number of ``OUTPUT`` buffers for the given format and
0190 requested count. The client must check this value after the ioctl returns
0191 to get the actual number of buffers allocated.
0192
0193 7. Allocate destination (raw format) buffers via :c:func:`VIDIOC_REQBUFS` on the
0194 ``CAPTURE`` queue.
0195
0196 * **Required fields:**
0197
0198 ``count``
0199 requested number of buffers to allocate; greater than zero. The client
0200 is responsible for deducing the minimum number of buffers required
0201 for the stream to be properly decoded (taking e.g. reference frames
0202 into account) and pass an equal or bigger number.
0203
0204 ``type``
0205 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
0206
0207 ``memory``
0208 follows standard semantics. ``V4L2_MEMORY_USERPTR`` is not supported
0209 for ``CAPTURE`` buffers.
0210
0211 * **Return fields:**
0212
0213 ``count``
0214 adjusted to allocated number of buffers, in case the codec requires
0215 more buffers than requested.
0216
0217 * The driver must adjust count to the minimum of required number of
0218 ``CAPTURE`` buffers for the current format, stream configuration and
0219 requested count. The client must check this value after the ioctl
0220 returns to get the number of buffers allocated.
0221
0222 8. Allocate requests (likely one per ``OUTPUT`` buffer) via
0223 :c:func:`MEDIA_IOC_REQUEST_ALLOC` on the media device.
0224
0225 9. Start streaming on both ``OUTPUT`` and ``CAPTURE`` queues via
0226 :c:func:`VIDIOC_STREAMON`.
0227
0228 Decoding
0229 ========
0230
0231 For each frame, the client is responsible for submitting at least one request to
0232 which the following is attached:
0233
0234 * The amount of encoded data expected by the codec for its current
0235 configuration, as a buffer submitted to the ``OUTPUT`` queue. Typically, this
0236 corresponds to one frame worth of encoded data, but some formats may allow (or
0237 require) different amounts per unit.
0238 * All the metadata needed to decode the submitted encoded data, in the form of
0239 controls relevant to the format being decoded.
0240
0241 The amount of data and contents of the source ``OUTPUT`` buffer, as well as the
0242 controls that must be set on the request, depend on the active coded pixel
0243 format and might be affected by codec-specific extended controls, as stated in
0244 documentation of each format.
0245
0246 If there is a possibility that the decoded frame will require one or more
0247 decode requests after the current one in order to be produced, then the client
0248 must set the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag on the ``OUTPUT``
0249 buffer. This will result in the (potentially partially) decoded ``CAPTURE``
0250 buffer not being made available for dequeueing, and reused for the next decode
0251 request if the timestamp of the next ``OUTPUT`` buffer has not changed.
0252
0253 A typical frame would thus be decoded using the following sequence:
0254
0255 1. Queue an ``OUTPUT`` buffer containing one unit of encoded bytestream data for
0256 the decoding request, using :c:func:`VIDIOC_QBUF`.
0257
0258 * **Required fields:**
0259
0260 ``index``
0261 index of the buffer being queued.
0262
0263 ``type``
0264 type of the buffer.
0265
0266 ``bytesused``
0267 number of bytes taken by the encoded data frame in the buffer.
0268
0269 ``flags``
0270 the ``V4L2_BUF_FLAG_REQUEST_FD`` flag must be set. Additionally, if
0271 we are not sure that the current decode request is the last one needed
0272 to produce a fully decoded frame, then
0273 ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` must also be set.
0274
0275 ``request_fd``
0276 must be set to the file descriptor of the decoding request.
0277
0278 ``timestamp``
0279 must be set to a unique value per frame. This value will be propagated
0280 into the decoded frame's buffer and can also be used to use this frame
0281 as the reference of another. If using multiple decode requests per
0282 frame, then the timestamps of all the ``OUTPUT`` buffers for a given
0283 frame must be identical. If the timestamp changes, then the currently
0284 held ``CAPTURE`` buffer will be made available for dequeuing and the
0285 current request will work on a new ``CAPTURE`` buffer.
0286
0287 2. Set the codec-specific controls for the decoding request, using
0288 :c:func:`VIDIOC_S_EXT_CTRLS`.
0289
0290 * **Required fields:**
0291
0292 ``which``
0293 must be ``V4L2_CTRL_WHICH_REQUEST_VAL``.
0294
0295 ``request_fd``
0296 must be set to the file descriptor of the decoding request.
0297
0298 other fields
0299 other fields are set as usual when setting controls. The ``controls``
0300 array must contain all the codec-specific controls required to decode
0301 a frame.
0302
0303 .. note::
0304
0305 It is possible to specify the controls in different invocations of
0306 :c:func:`VIDIOC_S_EXT_CTRLS`, or to overwrite a previously set control, as
0307 long as ``request_fd`` and ``which`` are properly set. The controls state
0308 at the moment of request submission is the one that will be considered.
0309
0310 .. note::
0311
0312 The order in which steps 1 and 2 take place is interchangeable.
0313
0314 3. Submit the request by invoking :c:func:`MEDIA_REQUEST_IOC_QUEUE` on the
0315 request FD.
0316
0317 If the request is submitted without an ``OUTPUT`` buffer, or if some of the
0318 required controls are missing from the request, then
0319 :c:func:`MEDIA_REQUEST_IOC_QUEUE` will return ``-ENOENT``. If more than one
0320 ``OUTPUT`` buffer is queued, then it will return ``-EINVAL``.
0321 :c:func:`MEDIA_REQUEST_IOC_QUEUE` returning non-zero means that no
0322 ``CAPTURE`` buffer will be produced for this request.
0323
0324 ``CAPTURE`` buffers must not be part of the request, and are queued
0325 independently. They are returned in decode order (i.e. the same order as coded
0326 frames were submitted to the ``OUTPUT`` queue).
0327
0328 Runtime decoding errors are signaled by the dequeued ``CAPTURE`` buffers
0329 carrying the ``V4L2_BUF_FLAG_ERROR`` flag. If a decoded reference frame has an
0330 error, then all following decoded frames that refer to it also have the
0331 ``V4L2_BUF_FLAG_ERROR`` flag set, although the decoder will still try to
0332 produce (likely corrupted) frames.
0333
0334 Buffer management while decoding
0335 ================================
0336 Contrary to stateful decoders, a stateless decoder does not perform any kind of
0337 buffer management: it only guarantees that dequeued ``CAPTURE`` buffers can be
0338 used by the client for as long as they are not queued again. "Used" here
0339 encompasses using the buffer for compositing or display.
0340
0341 A dequeued capture buffer can also be used as the reference frame of another
0342 buffer.
0343
0344 A frame is specified as reference by converting its timestamp into nanoseconds,
0345 and storing it into the relevant member of a codec-dependent control structure.
0346 The :c:func:`v4l2_timeval_to_ns` function must be used to perform that
0347 conversion. The timestamp of a frame can be used to reference it as soon as all
0348 its units of encoded data are successfully submitted to the ``OUTPUT`` queue.
0349
0350 A decoded buffer containing a reference frame must not be reused as a decoding
0351 target until all the frames referencing it have been decoded. The safest way to
0352 achieve this is to refrain from queueing a reference buffer until all the
0353 decoded frames referencing it have been dequeued. However, if the driver can
0354 guarantee that buffers queued to the ``CAPTURE`` queue are processed in queued
0355 order, then user-space can take advantage of this guarantee and queue a
0356 reference buffer when the following conditions are met:
0357
0358 1. All the requests for frames affected by the reference frame have been
0359 queued, and
0360
0361 2. A sufficient number of ``CAPTURE`` buffers to cover all the decoded
0362 referencing frames have been queued.
0363
0364 When queuing a decoding request, the driver will increase the reference count of
0365 all the resources associated with reference frames. This means that the client
0366 can e.g. close the DMABUF file descriptors of reference frame buffers if it
0367 won't need them afterwards.
0368
0369 Seeking
0370 =======
0371 In order to seek, the client just needs to submit requests using input buffers
0372 corresponding to the new stream position. It must however be aware that
0373 resolution may have changed and follow the dynamic resolution change sequence in
0374 that case. Also depending on the codec used, picture parameters (e.g. SPS/PPS
0375 for H.264) may have changed and the client is responsible for making sure that a
0376 valid state is sent to the decoder.
0377
0378 The client is then free to ignore any returned ``CAPTURE`` buffer that comes
0379 from the pre-seek position.
0380
0381 Pausing
0382 =======
0383
0384 In order to pause, the client can just cease queuing buffers onto the ``OUTPUT``
0385 queue. Without source bytestream data, there is no data to process and the codec
0386 will remain idle.
0387
0388 Dynamic resolution change
0389 =========================
0390
0391 If the client detects a resolution change in the stream, it will need to perform
0392 the initialization sequence again with the new resolution:
0393
0394 1. If the last submitted request resulted in a ``CAPTURE`` buffer being
0395 held by the use of the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag, then the
0396 last frame is not available on the ``CAPTURE`` queue. In this case, a
0397 ``V4L2_DEC_CMD_FLUSH`` command shall be sent. This will make the driver
0398 dequeue the held ``CAPTURE`` buffer.
0399
0400 2. Wait until all submitted requests have completed and dequeue the
0401 corresponding output buffers.
0402
0403 3. Call :c:func:`VIDIOC_STREAMOFF` on both the ``OUTPUT`` and ``CAPTURE``
0404 queues.
0405
0406 4. Free all ``CAPTURE`` buffers by calling :c:func:`VIDIOC_REQBUFS` on the
0407 ``CAPTURE`` queue with a buffer count of zero.
0408
0409 5. Perform the initialization sequence again (minus the allocation of
0410 ``OUTPUT`` buffers), with the new resolution set on the ``OUTPUT`` queue.
0411 Note that due to resolution constraints, a different format may need to be
0412 picked on the ``CAPTURE`` queue.
0413
0414 Drain
0415 =====
0416
0417 If the last submitted request resulted in a ``CAPTURE`` buffer being
0418 held by the use of the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag, then the
0419 last frame is not available on the ``CAPTURE`` queue. In this case, a
0420 ``V4L2_DEC_CMD_FLUSH`` command shall be sent. This will make the driver
0421 dequeue the held ``CAPTURE`` buffer.
0422
0423 After that, in order to drain the stream on a stateless decoder, the client
0424 just needs to wait until all the submitted requests are completed.