xref: /OK3568_Linux_fs/kernel/Documentation/userspace-api/media/v4l/dev-stateless-decoder.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun.. _stateless_decoder:
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun**************************************************
6*4882a593SmuzhiyunMemory-to-memory Stateless Video Decoder Interface
7*4882a593Smuzhiyun**************************************************
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunA stateless decoder is a decoder that works without retaining any kind of state
10*4882a593Smuzhiyunbetween processed frames. This means that each frame is decoded independently
11*4882a593Smuzhiyunof any previous and future frames, and that the client is responsible for
12*4882a593Smuzhiyunmaintaining the decoding state and providing it to the decoder with each
13*4882a593Smuzhiyundecoding request. This is in contrast to the stateful video decoder interface,
14*4882a593Smuzhiyunwhere the hardware and driver maintain the decoding state and all the client
15*4882a593Smuzhiyunhas to do is to provide the raw encoded stream and dequeue decoded frames in
16*4882a593Smuzhiyundisplay order.
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunThis section describes how user-space ("the client") is expected to communicate
19*4882a593Smuzhiyunwith stateless decoders in order to successfully decode an encoded stream.
20*4882a593SmuzhiyunCompared to stateful codecs, the decoder/client sequence is simpler, but the
21*4882a593Smuzhiyuncost of this simplicity is extra complexity in the client which is responsible
22*4882a593Smuzhiyunfor maintaining a consistent decoding state.
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunStateless decoders make use of the :ref:`media-request-api`. A stateless
25*4882a593Smuzhiyundecoder must expose the ``V4L2_BUF_CAP_SUPPORTS_REQUESTS`` capability on its
26*4882a593Smuzhiyun``OUTPUT`` queue when :c:func:`VIDIOC_REQBUFS` or :c:func:`VIDIOC_CREATE_BUFS`
27*4882a593Smuzhiyunare invoked.
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunDepending on the encoded formats supported by the decoder, a single decoded
30*4882a593Smuzhiyunframe may be the result of several decode requests (for instance, H.264 streams
31*4882a593Smuzhiyunwith multiple slices per frame). Decoders that support such formats must also
32*4882a593Smuzhiyunexpose the ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF`` capability on their
33*4882a593Smuzhiyun``OUTPUT`` queue.
34*4882a593Smuzhiyun
35*4882a593SmuzhiyunQuerying capabilities
36*4882a593Smuzhiyun=====================
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun1. To enumerate the set of coded formats supported by the decoder, the client
39*4882a593Smuzhiyun   calls :c:func:`VIDIOC_ENUM_FMT` on the ``OUTPUT`` queue.
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun   * The driver must always return the full set of supported ``OUTPUT`` formats,
42*4882a593Smuzhiyun     irrespective of the format currently set on the ``CAPTURE`` queue.
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun   * Simultaneously, the driver must restrain the set of values returned by
45*4882a593Smuzhiyun     codec-specific capability controls (such as H.264 profiles) to the set
46*4882a593Smuzhiyun     actually supported by the hardware.
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun2. To enumerate the set of supported raw formats, the client calls
49*4882a593Smuzhiyun   :c:func:`VIDIOC_ENUM_FMT` on the ``CAPTURE`` queue.
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun   * The driver must return only the formats supported for the format currently
52*4882a593Smuzhiyun     active on the ``OUTPUT`` queue.
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun   * Depending on the currently set ``OUTPUT`` format, the set of supported raw
55*4882a593Smuzhiyun     formats may depend on the value of some codec-dependent controls.
56*4882a593Smuzhiyun     The client is responsible for making sure that these controls are set
57*4882a593Smuzhiyun     before querying the ``CAPTURE`` queue. Failure to do so will result in the
58*4882a593Smuzhiyun     default values for these controls being used, and a returned set of formats
59*4882a593Smuzhiyun     that may not be usable for the media the client is trying to decode.
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun3. The client may use :c:func:`VIDIOC_ENUM_FRAMESIZES` to detect supported
62*4882a593Smuzhiyun   resolutions for a given format, passing desired pixel format in
63*4882a593Smuzhiyun   :c:type:`v4l2_frmsizeenum`'s ``pixel_format``.
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun4. Supported profiles and levels for the current ``OUTPUT`` format, if
66*4882a593Smuzhiyun   applicable, may be queried using their respective controls via
67*4882a593Smuzhiyun   :c:func:`VIDIOC_QUERYCTRL`.
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunInitialization
70*4882a593Smuzhiyun==============
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun1. Set the coded format on the ``OUTPUT`` queue via :c:func:`VIDIOC_S_FMT`.
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun   * **Required fields:**
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun     ``type``
77*4882a593Smuzhiyun         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun     ``pixelformat``
80*4882a593Smuzhiyun         a coded pixel format.
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun     ``width``, ``height``
83*4882a593Smuzhiyun         coded width and height parsed from the stream.
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun     other fields
86*4882a593Smuzhiyun         follow standard semantics.
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun   .. note::
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun      Changing the ``OUTPUT`` format may change the currently set ``CAPTURE``
91*4882a593Smuzhiyun      format. The driver will derive a new ``CAPTURE`` format from the
92*4882a593Smuzhiyun      ``OUTPUT`` format being set, including resolution, colorimetry
93*4882a593Smuzhiyun      parameters, etc. If the client needs a specific ``CAPTURE`` format,
94*4882a593Smuzhiyun      it must adjust it afterwards.
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun2. Call :c:func:`VIDIOC_S_EXT_CTRLS` to set all the controls (parsed headers,
97*4882a593Smuzhiyun   etc.) required by the ``OUTPUT`` format to enumerate the ``CAPTURE`` formats.
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun3. Call :c:func:`VIDIOC_G_FMT` for ``CAPTURE`` queue to get the format for the
100*4882a593Smuzhiyun   destination buffers parsed/decoded from the bytestream.
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun   * **Required fields:**
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun     ``type``
105*4882a593Smuzhiyun         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun   * **Returned fields:**
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun     ``width``, ``height``
110*4882a593Smuzhiyun         frame buffer resolution for the decoded frames.
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun     ``pixelformat``
113*4882a593Smuzhiyun         pixel format for decoded frames.
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun     ``num_planes`` (for _MPLANE ``type`` only)
116*4882a593Smuzhiyun         number of planes for pixelformat.
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun     ``sizeimage``, ``bytesperline``
119*4882a593Smuzhiyun         as per standard semantics; matching frame buffer format.
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun   .. note::
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun      The value of ``pixelformat`` may be any pixel format supported for the
124*4882a593Smuzhiyun      ``OUTPUT`` format, based on the hardware capabilities. It is suggested
125*4882a593Smuzhiyun      that the driver chooses the preferred/optimal format for the current
126*4882a593Smuzhiyun      configuration. For example, a YUV format may be preferred over an RGB
127*4882a593Smuzhiyun      format, if an additional conversion step would be required for RGB.
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun4. *[optional]* Enumerate ``CAPTURE`` formats via :c:func:`VIDIOC_ENUM_FMT` on
130*4882a593Smuzhiyun   the ``CAPTURE`` queue. The client may use this ioctl to discover which
131*4882a593Smuzhiyun   alternative raw formats are supported for the current ``OUTPUT`` format and
132*4882a593Smuzhiyun   select one of them via :c:func:`VIDIOC_S_FMT`.
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun   .. note::
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun      The driver will return only formats supported for the currently selected
137*4882a593Smuzhiyun      ``OUTPUT`` format and currently set controls, even if more formats may be
138*4882a593Smuzhiyun      supported by the decoder in general.
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun      For example, a decoder may support YUV and RGB formats for
141*4882a593Smuzhiyun      resolutions 1920x1088 and lower, but only YUV for higher resolutions (due
142*4882a593Smuzhiyun      to hardware limitations). After setting a resolution of 1920x1088 or lower
143*4882a593Smuzhiyun      as the ``OUTPUT`` format, :c:func:`VIDIOC_ENUM_FMT` may return a set of
144*4882a593Smuzhiyun      YUV and RGB pixel formats, but after setting a resolution higher than
145*4882a593Smuzhiyun      1920x1088, the driver will not return RGB pixel formats, since they are
146*4882a593Smuzhiyun      unsupported for this resolution.
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun5. *[optional]* Choose a different ``CAPTURE`` format than suggested via
149*4882a593Smuzhiyun   :c:func:`VIDIOC_S_FMT` on ``CAPTURE`` queue. It is possible for the client to
150*4882a593Smuzhiyun   choose a different format than selected/suggested by the driver in
151*4882a593Smuzhiyun   :c:func:`VIDIOC_G_FMT`.
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun    * **Required fields:**
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun      ``type``
156*4882a593Smuzhiyun          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun      ``pixelformat``
159*4882a593Smuzhiyun          a raw pixel format.
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun      ``width``, ``height``
162*4882a593Smuzhiyun         frame buffer resolution of the decoded stream; typically unchanged from
163*4882a593Smuzhiyun         what was returned with :c:func:`VIDIOC_G_FMT`, but it may be different
164*4882a593Smuzhiyun         if the hardware supports composition and/or scaling.
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun   After performing this step, the client must perform step 3 again in order
167*4882a593Smuzhiyun   to obtain up-to-date information about the buffers size and layout.
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun6. Allocate source (bytestream) buffers via :c:func:`VIDIOC_REQBUFS` on
170*4882a593Smuzhiyun   ``OUTPUT`` queue.
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun    * **Required fields:**
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun      ``count``
175*4882a593Smuzhiyun          requested number of buffers to allocate; greater than zero.
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun      ``type``
178*4882a593Smuzhiyun          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun      ``memory``
181*4882a593Smuzhiyun          follows standard semantics.
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun    * **Return fields:**
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun      ``count``
186*4882a593Smuzhiyun          actual number of buffers allocated.
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun    * If required, the driver will adjust ``count`` to be equal or bigger to the
189*4882a593Smuzhiyun      minimum of required number of ``OUTPUT`` buffers for the given format and
190*4882a593Smuzhiyun      requested count. The client must check this value after the ioctl returns
191*4882a593Smuzhiyun      to get the actual number of buffers allocated.
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun7. Allocate destination (raw format) buffers via :c:func:`VIDIOC_REQBUFS` on the
194*4882a593Smuzhiyun   ``CAPTURE`` queue.
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun    * **Required fields:**
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun      ``count``
199*4882a593Smuzhiyun          requested number of buffers to allocate; greater than zero. The client
200*4882a593Smuzhiyun          is responsible for deducing the minimum number of buffers required
201*4882a593Smuzhiyun          for the stream to be properly decoded (taking e.g. reference frames
202*4882a593Smuzhiyun          into account) and pass an equal or bigger number.
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun      ``type``
205*4882a593Smuzhiyun          a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun      ``memory``
208*4882a593Smuzhiyun          follows standard semantics. ``V4L2_MEMORY_USERPTR`` is not supported
209*4882a593Smuzhiyun          for ``CAPTURE`` buffers.
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun    * **Return fields:**
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun      ``count``
214*4882a593Smuzhiyun          adjusted to allocated number of buffers, in case the codec requires
215*4882a593Smuzhiyun          more buffers than requested.
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun    * The driver must adjust count to the minimum of required number of
218*4882a593Smuzhiyun      ``CAPTURE`` buffers for the current format, stream configuration and
219*4882a593Smuzhiyun      requested count. The client must check this value after the ioctl
220*4882a593Smuzhiyun      returns to get the number of buffers allocated.
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun8. Allocate requests (likely one per ``OUTPUT`` buffer) via
223*4882a593Smuzhiyun    :c:func:`MEDIA_IOC_REQUEST_ALLOC` on the media device.
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun9. Start streaming on both ``OUTPUT`` and ``CAPTURE`` queues via
226*4882a593Smuzhiyun    :c:func:`VIDIOC_STREAMON`.
227*4882a593Smuzhiyun
228*4882a593SmuzhiyunDecoding
229*4882a593Smuzhiyun========
230*4882a593Smuzhiyun
231*4882a593SmuzhiyunFor each frame, the client is responsible for submitting at least one request to
232*4882a593Smuzhiyunwhich the following is attached:
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun* The amount of encoded data expected by the codec for its current
235*4882a593Smuzhiyun  configuration, as a buffer submitted to the ``OUTPUT`` queue. Typically, this
236*4882a593Smuzhiyun  corresponds to one frame worth of encoded data, but some formats may allow (or
237*4882a593Smuzhiyun  require) different amounts per unit.
238*4882a593Smuzhiyun* All the metadata needed to decode the submitted encoded data, in the form of
239*4882a593Smuzhiyun  controls relevant to the format being decoded.
240*4882a593Smuzhiyun
241*4882a593SmuzhiyunThe amount of data and contents of the source ``OUTPUT`` buffer, as well as the
242*4882a593Smuzhiyuncontrols that must be set on the request, depend on the active coded pixel
243*4882a593Smuzhiyunformat and might be affected by codec-specific extended controls, as stated in
244*4882a593Smuzhiyundocumentation of each format.
245*4882a593Smuzhiyun
246*4882a593SmuzhiyunIf there is a possibility that the decoded frame will require one or more
247*4882a593Smuzhiyundecode requests after the current one in order to be produced, then the client
248*4882a593Smuzhiyunmust set the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag on the ``OUTPUT``
249*4882a593Smuzhiyunbuffer. This will result in the (potentially partially) decoded ``CAPTURE``
250*4882a593Smuzhiyunbuffer not being made available for dequeueing, and reused for the next decode
251*4882a593Smuzhiyunrequest if the timestamp of the next ``OUTPUT`` buffer has not changed.
252*4882a593Smuzhiyun
253*4882a593SmuzhiyunA typical frame would thus be decoded using the following sequence:
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun1. Queue an ``OUTPUT`` buffer containing one unit of encoded bytestream data for
256*4882a593Smuzhiyun   the decoding request, using :c:func:`VIDIOC_QBUF`.
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun    * **Required fields:**
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun      ``index``
261*4882a593Smuzhiyun          index of the buffer being queued.
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun      ``type``
264*4882a593Smuzhiyun          type of the buffer.
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun      ``bytesused``
267*4882a593Smuzhiyun          number of bytes taken by the encoded data frame in the buffer.
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun      ``flags``
270*4882a593Smuzhiyun          the ``V4L2_BUF_FLAG_REQUEST_FD`` flag must be set. Additionally, if
271*4882a593Smuzhiyun          we are not sure that the current decode request is the last one needed
272*4882a593Smuzhiyun          to produce a fully decoded frame, then
273*4882a593Smuzhiyun          ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` must also be set.
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun      ``request_fd``
276*4882a593Smuzhiyun          must be set to the file descriptor of the decoding request.
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun      ``timestamp``
279*4882a593Smuzhiyun          must be set to a unique value per frame. This value will be propagated
280*4882a593Smuzhiyun          into the decoded frame's buffer and can also be used to use this frame
281*4882a593Smuzhiyun          as the reference of another. If using multiple decode requests per
282*4882a593Smuzhiyun          frame, then the timestamps of all the ``OUTPUT`` buffers for a given
283*4882a593Smuzhiyun          frame must be identical. If the timestamp changes, then the currently
284*4882a593Smuzhiyun          held ``CAPTURE`` buffer will be made available for dequeuing and the
285*4882a593Smuzhiyun          current request will work on a new ``CAPTURE`` buffer.
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun2. Set the codec-specific controls for the decoding request, using
288*4882a593Smuzhiyun   :c:func:`VIDIOC_S_EXT_CTRLS`.
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun    * **Required fields:**
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun      ``which``
293*4882a593Smuzhiyun          must be ``V4L2_CTRL_WHICH_REQUEST_VAL``.
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun      ``request_fd``
296*4882a593Smuzhiyun          must be set to the file descriptor of the decoding request.
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun      other fields
299*4882a593Smuzhiyun          other fields are set as usual when setting controls. The ``controls``
300*4882a593Smuzhiyun          array must contain all the codec-specific controls required to decode
301*4882a593Smuzhiyun          a frame.
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun   .. note::
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun      It is possible to specify the controls in different invocations of
306*4882a593Smuzhiyun      :c:func:`VIDIOC_S_EXT_CTRLS`, or to overwrite a previously set control, as
307*4882a593Smuzhiyun      long as ``request_fd`` and ``which`` are properly set. The controls state
308*4882a593Smuzhiyun      at the moment of request submission is the one that will be considered.
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun   .. note::
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun      The order in which steps 1 and 2 take place is interchangeable.
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun3. Submit the request by invoking :c:func:`MEDIA_REQUEST_IOC_QUEUE` on the
315*4882a593Smuzhiyun   request FD.
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun    If the request is submitted without an ``OUTPUT`` buffer, or if some of the
318*4882a593Smuzhiyun    required controls are missing from the request, then
319*4882a593Smuzhiyun    :c:func:`MEDIA_REQUEST_IOC_QUEUE` will return ``-ENOENT``. If more than one
320*4882a593Smuzhiyun    ``OUTPUT`` buffer is queued, then it will return ``-EINVAL``.
321*4882a593Smuzhiyun    :c:func:`MEDIA_REQUEST_IOC_QUEUE` returning non-zero means that no
322*4882a593Smuzhiyun    ``CAPTURE`` buffer will be produced for this request.
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun``CAPTURE`` buffers must not be part of the request, and are queued
325*4882a593Smuzhiyunindependently. They are returned in decode order (i.e. the same order as coded
326*4882a593Smuzhiyunframes were submitted to the ``OUTPUT`` queue).
327*4882a593Smuzhiyun
328*4882a593SmuzhiyunRuntime decoding errors are signaled by the dequeued ``CAPTURE`` buffers
329*4882a593Smuzhiyuncarrying the ``V4L2_BUF_FLAG_ERROR`` flag. If a decoded reference frame has an
330*4882a593Smuzhiyunerror, then all following decoded frames that refer to it also have the
331*4882a593Smuzhiyun``V4L2_BUF_FLAG_ERROR`` flag set, although the decoder will still try to
332*4882a593Smuzhiyunproduce (likely corrupted) frames.
333*4882a593Smuzhiyun
334*4882a593SmuzhiyunBuffer management while decoding
335*4882a593Smuzhiyun================================
336*4882a593SmuzhiyunContrary to stateful decoders, a stateless decoder does not perform any kind of
337*4882a593Smuzhiyunbuffer management: it only guarantees that dequeued ``CAPTURE`` buffers can be
338*4882a593Smuzhiyunused by the client for as long as they are not queued again. "Used" here
339*4882a593Smuzhiyunencompasses using the buffer for compositing or display.
340*4882a593Smuzhiyun
341*4882a593SmuzhiyunA dequeued capture buffer can also be used as the reference frame of another
342*4882a593Smuzhiyunbuffer.
343*4882a593Smuzhiyun
344*4882a593SmuzhiyunA frame is specified as reference by converting its timestamp into nanoseconds,
345*4882a593Smuzhiyunand storing it into the relevant member of a codec-dependent control structure.
346*4882a593SmuzhiyunThe :c:func:`v4l2_timeval_to_ns` function must be used to perform that
347*4882a593Smuzhiyunconversion. The timestamp of a frame can be used to reference it as soon as all
348*4882a593Smuzhiyunits units of encoded data are successfully submitted to the ``OUTPUT`` queue.
349*4882a593Smuzhiyun
350*4882a593SmuzhiyunA decoded buffer containing a reference frame must not be reused as a decoding
351*4882a593Smuzhiyuntarget until all the frames referencing it have been decoded. The safest way to
352*4882a593Smuzhiyunachieve this is to refrain from queueing a reference buffer until all the
353*4882a593Smuzhiyundecoded frames referencing it have been dequeued. However, if the driver can
354*4882a593Smuzhiyunguarantee that buffers queued to the ``CAPTURE`` queue are processed in queued
355*4882a593Smuzhiyunorder, then user-space can take advantage of this guarantee and queue a
356*4882a593Smuzhiyunreference buffer when the following conditions are met:
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun1. All the requests for frames affected by the reference frame have been
359*4882a593Smuzhiyun   queued, and
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun2. A sufficient number of ``CAPTURE`` buffers to cover all the decoded
362*4882a593Smuzhiyun   referencing frames have been queued.
363*4882a593Smuzhiyun
364*4882a593SmuzhiyunWhen queuing a decoding request, the driver will increase the reference count of
365*4882a593Smuzhiyunall the resources associated with reference frames. This means that the client
366*4882a593Smuzhiyuncan e.g. close the DMABUF file descriptors of reference frame buffers if it
367*4882a593Smuzhiyunwon't need them afterwards.
368*4882a593Smuzhiyun
369*4882a593SmuzhiyunSeeking
370*4882a593Smuzhiyun=======
371*4882a593SmuzhiyunIn order to seek, the client just needs to submit requests using input buffers
372*4882a593Smuzhiyuncorresponding to the new stream position. It must however be aware that
373*4882a593Smuzhiyunresolution may have changed and follow the dynamic resolution change sequence in
374*4882a593Smuzhiyunthat case. Also depending on the codec used, picture parameters (e.g. SPS/PPS
375*4882a593Smuzhiyunfor H.264) may have changed and the client is responsible for making sure that a
376*4882a593Smuzhiyunvalid state is sent to the decoder.
377*4882a593Smuzhiyun
378*4882a593SmuzhiyunThe client is then free to ignore any returned ``CAPTURE`` buffer that comes
379*4882a593Smuzhiyunfrom the pre-seek position.
380*4882a593Smuzhiyun
381*4882a593SmuzhiyunPausing
382*4882a593Smuzhiyun=======
383*4882a593Smuzhiyun
384*4882a593SmuzhiyunIn order to pause, the client can just cease queuing buffers onto the ``OUTPUT``
385*4882a593Smuzhiyunqueue. Without source bytestream data, there is no data to process and the codec
386*4882a593Smuzhiyunwill remain idle.
387*4882a593Smuzhiyun
388*4882a593SmuzhiyunDynamic resolution change
389*4882a593Smuzhiyun=========================
390*4882a593Smuzhiyun
391*4882a593SmuzhiyunIf the client detects a resolution change in the stream, it will need to perform
392*4882a593Smuzhiyunthe initialization sequence again with the new resolution:
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun1. If the last submitted request resulted in a ``CAPTURE`` buffer being
395*4882a593Smuzhiyun   held by the use of the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag, then the
396*4882a593Smuzhiyun   last frame is not available on the ``CAPTURE`` queue. In this case, a
397*4882a593Smuzhiyun   ``V4L2_DEC_CMD_FLUSH`` command shall be sent. This will make the driver
398*4882a593Smuzhiyun   dequeue the held ``CAPTURE`` buffer.
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun2. Wait until all submitted requests have completed and dequeue the
401*4882a593Smuzhiyun   corresponding output buffers.
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun3. Call :c:func:`VIDIOC_STREAMOFF` on both the ``OUTPUT`` and ``CAPTURE``
404*4882a593Smuzhiyun   queues.
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun4. Free all ``CAPTURE`` buffers by calling :c:func:`VIDIOC_REQBUFS` on the
407*4882a593Smuzhiyun   ``CAPTURE`` queue with a buffer count of zero.
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun5. Perform the initialization sequence again (minus the allocation of
410*4882a593Smuzhiyun   ``OUTPUT`` buffers), with the new resolution set on the ``OUTPUT`` queue.
411*4882a593Smuzhiyun   Note that due to resolution constraints, a different format may need to be
412*4882a593Smuzhiyun   picked on the ``CAPTURE`` queue.
413*4882a593Smuzhiyun
414*4882a593SmuzhiyunDrain
415*4882a593Smuzhiyun=====
416*4882a593Smuzhiyun
417*4882a593SmuzhiyunIf the last submitted request resulted in a ``CAPTURE`` buffer being
418*4882a593Smuzhiyunheld by the use of the ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag, then the
419*4882a593Smuzhiyunlast frame is not available on the ``CAPTURE`` queue. In this case, a
420*4882a593Smuzhiyun``V4L2_DEC_CMD_FLUSH`` command shall be sent. This will make the driver
421*4882a593Smuzhiyundequeue the held ``CAPTURE`` buffer.
422*4882a593Smuzhiyun
423*4882a593SmuzhiyunAfter that, in order to drain the stream on a stateless decoder, the client
424*4882a593Smuzhiyunjust needs to wait until all the submitted requests are completed.
425