xref: /linux/Documentation/userspace-api/media/v4l/dev-encoder.rst (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
184d33341SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later
29ec656cfSTomasz Figa
39ec656cfSTomasz Figa.. _encoder:
49ec656cfSTomasz Figa
59ec656cfSTomasz Figa*************************************************
69ec656cfSTomasz FigaMemory-to-Memory Stateful Video Encoder Interface
79ec656cfSTomasz Figa*************************************************
89ec656cfSTomasz Figa
99ec656cfSTomasz FigaA stateful video encoder takes raw video frames in display order and encodes
109ec656cfSTomasz Figathem into a bytestream. It generates complete chunks of the bytestream, including
119ec656cfSTomasz Figaall metadata, headers, etc. The resulting bytestream does not require any
129ec656cfSTomasz Figafurther post-processing by the client.
139ec656cfSTomasz Figa
149ec656cfSTomasz FigaPerforming software stream processing, header generation etc. in the driver
159ec656cfSTomasz Figain order to support this interface is strongly discouraged. In case such
169ec656cfSTomasz Figaoperations are needed, use of the Stateless Video Encoder Interface (in
179ec656cfSTomasz Figadevelopment) is strongly advised.
189ec656cfSTomasz Figa
199ec656cfSTomasz FigaConventions and Notations Used in This Document
209ec656cfSTomasz Figa===============================================
219ec656cfSTomasz Figa
229ec656cfSTomasz Figa1. The general V4L2 API rules apply if not specified in this document
239ec656cfSTomasz Figa   otherwise.
249ec656cfSTomasz Figa
259ec656cfSTomasz Figa2. The meaning of words "must", "may", "should", etc. is as per `RFC
269ec656cfSTomasz Figa   2119 <https://tools.ietf.org/html/rfc2119>`_.
279ec656cfSTomasz Figa
289ec656cfSTomasz Figa3. All steps not marked "optional" are required.
299ec656cfSTomasz Figa
309ec656cfSTomasz Figa4. :c:func:`VIDIOC_G_EXT_CTRLS` and :c:func:`VIDIOC_S_EXT_CTRLS` may be used
319ec656cfSTomasz Figa   interchangeably with :c:func:`VIDIOC_G_CTRL` and :c:func:`VIDIOC_S_CTRL`,
329ec656cfSTomasz Figa   unless specified otherwise.
339ec656cfSTomasz Figa
349ec656cfSTomasz Figa5. Single-planar API (see :ref:`planar-apis`) and applicable structures may be
359ec656cfSTomasz Figa   used interchangeably with multi-planar API, unless specified otherwise,
369ec656cfSTomasz Figa   depending on encoder capabilities and following the general V4L2 guidelines.
379ec656cfSTomasz Figa
389ec656cfSTomasz Figa6. i = [a..b]: sequence of integers from a to b, inclusive, i.e. i =
399ec656cfSTomasz Figa   [0..2]: i = 0, 1, 2.
409ec656cfSTomasz Figa
419ec656cfSTomasz Figa7. Given an ``OUTPUT`` buffer A, then A' represents a buffer on the ``CAPTURE``
429ec656cfSTomasz Figa   queue containing data that resulted from processing buffer A.
439ec656cfSTomasz Figa
449ec656cfSTomasz FigaGlossary
459ec656cfSTomasz Figa========
469ec656cfSTomasz Figa
479ec656cfSTomasz FigaRefer to :ref:`decoder-glossary`.
489ec656cfSTomasz Figa
499ec656cfSTomasz FigaState Machine
509ec656cfSTomasz Figa=============
519ec656cfSTomasz Figa
529ec656cfSTomasz Figa.. kernel-render:: DOT
539ec656cfSTomasz Figa   :alt: DOT digraph of encoder state machine
549ec656cfSTomasz Figa   :caption: Encoder State Machine
559ec656cfSTomasz Figa
569ec656cfSTomasz Figa   digraph encoder_state_machine {
579ec656cfSTomasz Figa       node [shape = doublecircle, label="Encoding"] Encoding;
589ec656cfSTomasz Figa
599ec656cfSTomasz Figa       node [shape = circle, label="Initialization"] Initialization;
609ec656cfSTomasz Figa       node [shape = circle, label="Stopped"] Stopped;
619ec656cfSTomasz Figa       node [shape = circle, label="Drain"] Drain;
629ec656cfSTomasz Figa       node [shape = circle, label="Reset"] Reset;
639ec656cfSTomasz Figa
649ec656cfSTomasz Figa       node [shape = point]; qi
659ec656cfSTomasz Figa       qi -> Initialization [ label = "open()" ];
669ec656cfSTomasz Figa
679ec656cfSTomasz Figa       Initialization -> Encoding [ label = "Both queues streaming" ];
689ec656cfSTomasz Figa
699ec656cfSTomasz Figa       Encoding -> Drain [ label = "V4L2_ENC_CMD_STOP" ];
709ec656cfSTomasz Figa       Encoding -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ];
719ec656cfSTomasz Figa       Encoding -> Stopped [ label = "VIDIOC_STREAMOFF(OUTPUT)" ];
729ec656cfSTomasz Figa       Encoding -> Encoding;
739ec656cfSTomasz Figa
749ec656cfSTomasz Figa       Drain -> Stopped [ label = "All CAPTURE\nbuffers dequeued\nor\nVIDIOC_STREAMOFF(OUTPUT)" ];
759ec656cfSTomasz Figa       Drain -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ];
769ec656cfSTomasz Figa
779ec656cfSTomasz Figa       Reset -> Encoding [ label = "VIDIOC_STREAMON(CAPTURE)" ];
789ec656cfSTomasz Figa       Reset -> Initialization [ label = "VIDIOC_REQBUFS(OUTPUT, 0)" ];
799ec656cfSTomasz Figa
809ec656cfSTomasz Figa       Stopped -> Encoding [ label = "V4L2_ENC_CMD_START\nor\nVIDIOC_STREAMON(OUTPUT)" ];
819ec656cfSTomasz Figa       Stopped -> Reset [ label = "VIDIOC_STREAMOFF(CAPTURE)" ];
829ec656cfSTomasz Figa   }
839ec656cfSTomasz Figa
849ec656cfSTomasz FigaQuerying Capabilities
859ec656cfSTomasz Figa=====================
869ec656cfSTomasz Figa
879ec656cfSTomasz Figa1. To enumerate the set of coded formats supported by the encoder, the
889ec656cfSTomasz Figa   client may call :c:func:`VIDIOC_ENUM_FMT` on ``CAPTURE``.
899ec656cfSTomasz Figa
909ec656cfSTomasz Figa   * The full set of supported formats will be returned, regardless of the
919ec656cfSTomasz Figa     format set on ``OUTPUT``.
929ec656cfSTomasz Figa
939ec656cfSTomasz Figa2. To enumerate the set of supported raw formats, the client may call
949ec656cfSTomasz Figa   :c:func:`VIDIOC_ENUM_FMT` on ``OUTPUT``.
959ec656cfSTomasz Figa
969ec656cfSTomasz Figa   * Only the formats supported for the format currently active on ``CAPTURE``
979ec656cfSTomasz Figa     will be returned.
989ec656cfSTomasz Figa
999ec656cfSTomasz Figa   * In order to enumerate raw formats supported by a given coded format,
1009ec656cfSTomasz Figa     the client must first set that coded format on ``CAPTURE`` and then
1019ec656cfSTomasz Figa     enumerate the formats on ``OUTPUT``.
1029ec656cfSTomasz Figa
1039ec656cfSTomasz Figa3. The client may use :c:func:`VIDIOC_ENUM_FRAMESIZES` to detect supported
1049ec656cfSTomasz Figa   resolutions for a given format, passing the desired pixel format in
1059ec656cfSTomasz Figa   :c:type:`v4l2_frmsizeenum` ``pixel_format``.
1069ec656cfSTomasz Figa
1079ec656cfSTomasz Figa   * Values returned by :c:func:`VIDIOC_ENUM_FRAMESIZES` for a coded pixel
1089ec656cfSTomasz Figa     format will include all possible coded resolutions supported by the
1099ec656cfSTomasz Figa     encoder for the given coded pixel format.
1109ec656cfSTomasz Figa
1119ec656cfSTomasz Figa   * Values returned by :c:func:`VIDIOC_ENUM_FRAMESIZES` for a raw pixel format
1129ec656cfSTomasz Figa     will include all possible frame buffer resolutions supported by the
1139ec656cfSTomasz Figa     encoder for the given raw pixel format and coded format currently set on
1149ec656cfSTomasz Figa     ``CAPTURE``.
1159ec656cfSTomasz Figa
1169ec656cfSTomasz Figa4. The client may use :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` to detect supported
1179ec656cfSTomasz Figa   frame intervals for a given format and resolution, passing the desired pixel
118*992ba89dSPaul Kocialkowski   format in :c:type:`v4l2_frmivalenum` ``pixel_format`` and the resolution
119*992ba89dSPaul Kocialkowski   in :c:type:`v4l2_frmivalenum` ``width`` and :c:type:`v4l2_frmivalenum`
1209ec656cfSTomasz Figa   ``height``.
1219ec656cfSTomasz Figa
1229ec656cfSTomasz Figa   * Values returned by :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` for a coded pixel
1239ec656cfSTomasz Figa     format and coded resolution will include all possible frame intervals
1249ec656cfSTomasz Figa     supported by the encoder for the given coded pixel format and resolution.
1259ec656cfSTomasz Figa
1269ec656cfSTomasz Figa   * Values returned by :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` for a raw pixel
1279ec656cfSTomasz Figa     format and resolution will include all possible frame intervals supported
1289ec656cfSTomasz Figa     by the encoder for the given raw pixel format and resolution and for the
1299ec656cfSTomasz Figa     coded format, coded resolution and coded frame interval currently set on
1309ec656cfSTomasz Figa     ``CAPTURE``.
1319ec656cfSTomasz Figa
1329ec656cfSTomasz Figa   * Support for :c:func:`VIDIOC_ENUM_FRAMEINTERVALS` is optional. If it is
1339ec656cfSTomasz Figa     not implemented, then there are no special restrictions other than the
1349ec656cfSTomasz Figa     limits of the codec itself.
1359ec656cfSTomasz Figa
1369ec656cfSTomasz Figa5. Supported profiles and levels for the coded format currently set on
1379ec656cfSTomasz Figa   ``CAPTURE``, if applicable, may be queried using their respective controls
1389ec656cfSTomasz Figa   via :c:func:`VIDIOC_QUERYCTRL`.
1399ec656cfSTomasz Figa
1409ec656cfSTomasz Figa6. Any additional encoder capabilities may be discovered by querying
1419ec656cfSTomasz Figa   their respective controls.
1429ec656cfSTomasz Figa
1439ec656cfSTomasz FigaInitialization
1449ec656cfSTomasz Figa==============
1459ec656cfSTomasz Figa
1469ec656cfSTomasz Figa1. Set the coded format on the ``CAPTURE`` queue via :c:func:`VIDIOC_S_FMT`.
1479ec656cfSTomasz Figa
1489ec656cfSTomasz Figa   * **Required fields:**
1499ec656cfSTomasz Figa
1509ec656cfSTomasz Figa     ``type``
1519ec656cfSTomasz Figa         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
1529ec656cfSTomasz Figa
1539ec656cfSTomasz Figa     ``pixelformat``
1549ec656cfSTomasz Figa         the coded format to be produced.
1559ec656cfSTomasz Figa
1569ec656cfSTomasz Figa     ``sizeimage``
1579ec656cfSTomasz Figa         desired size of ``CAPTURE`` buffers; the encoder may adjust it to
1589ec656cfSTomasz Figa         match hardware requirements.
1599ec656cfSTomasz Figa
1609ec656cfSTomasz Figa     ``width``, ``height``
1619ec656cfSTomasz Figa         ignored (read-only).
1629ec656cfSTomasz Figa
1639ec656cfSTomasz Figa     other fields
1649ec656cfSTomasz Figa         follow standard semantics.
1659ec656cfSTomasz Figa
1661073f441SPaul Kocialkowski   * **Returned fields:**
1679ec656cfSTomasz Figa
1689ec656cfSTomasz Figa     ``sizeimage``
1699ec656cfSTomasz Figa         adjusted size of ``CAPTURE`` buffers.
1709ec656cfSTomasz Figa
1719ec656cfSTomasz Figa     ``width``, ``height``
1729ec656cfSTomasz Figa         the coded size selected by the encoder based on current state, e.g.
1739ec656cfSTomasz Figa         ``OUTPUT`` format, selection rectangles, etc. (read-only).
1749ec656cfSTomasz Figa
1759ec656cfSTomasz Figa   .. important::
1769ec656cfSTomasz Figa
1779ec656cfSTomasz Figa      Changing the ``CAPTURE`` format may change the currently set ``OUTPUT``
1789ec656cfSTomasz Figa      format. How the new ``OUTPUT`` format is determined is up to the encoder
1799ec656cfSTomasz Figa      and the client must ensure it matches its needs afterwards.
1809ec656cfSTomasz Figa
1819ec656cfSTomasz Figa2. **Optional.** Enumerate supported ``OUTPUT`` formats (raw formats for
1829ec656cfSTomasz Figa   source) for the selected coded format via :c:func:`VIDIOC_ENUM_FMT`.
1839ec656cfSTomasz Figa
1849ec656cfSTomasz Figa   * **Required fields:**
1859ec656cfSTomasz Figa
1869ec656cfSTomasz Figa     ``type``
1879ec656cfSTomasz Figa         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
1889ec656cfSTomasz Figa
1899ec656cfSTomasz Figa     other fields
1909ec656cfSTomasz Figa         follow standard semantics.
1919ec656cfSTomasz Figa
1921073f441SPaul Kocialkowski   * **Returned fields:**
1939ec656cfSTomasz Figa
1949ec656cfSTomasz Figa     ``pixelformat``
1959ec656cfSTomasz Figa         raw format supported for the coded format currently selected on
1969ec656cfSTomasz Figa         the ``CAPTURE`` queue.
1979ec656cfSTomasz Figa
1989ec656cfSTomasz Figa     other fields
1999ec656cfSTomasz Figa         follow standard semantics.
2009ec656cfSTomasz Figa
2019ec656cfSTomasz Figa3. Set the raw source format on the ``OUTPUT`` queue via
2029ec656cfSTomasz Figa   :c:func:`VIDIOC_S_FMT`.
2039ec656cfSTomasz Figa
2049ec656cfSTomasz Figa   * **Required fields:**
2059ec656cfSTomasz Figa
2069ec656cfSTomasz Figa     ``type``
2079ec656cfSTomasz Figa         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
2089ec656cfSTomasz Figa
2099ec656cfSTomasz Figa     ``pixelformat``
2109ec656cfSTomasz Figa         raw format of the source.
2119ec656cfSTomasz Figa
2129ec656cfSTomasz Figa     ``width``, ``height``
2139ec656cfSTomasz Figa         source resolution.
2149ec656cfSTomasz Figa
2159ec656cfSTomasz Figa     other fields
2169ec656cfSTomasz Figa         follow standard semantics.
2179ec656cfSTomasz Figa
2181073f441SPaul Kocialkowski   * **Returned fields:**
2199ec656cfSTomasz Figa
2209ec656cfSTomasz Figa     ``width``, ``height``
2219ec656cfSTomasz Figa         may be adjusted to match encoder minimums, maximums and alignment
2229ec656cfSTomasz Figa         requirements, as required by the currently selected formats, as
2239ec656cfSTomasz Figa         reported by :c:func:`VIDIOC_ENUM_FRAMESIZES`.
2249ec656cfSTomasz Figa
2259ec656cfSTomasz Figa     other fields
2269ec656cfSTomasz Figa         follow standard semantics.
2279ec656cfSTomasz Figa
2289ec656cfSTomasz Figa   * Setting the ``OUTPUT`` format will reset the selection rectangles to their
2299ec656cfSTomasz Figa     default values, based on the new resolution, as described in the next
2309ec656cfSTomasz Figa     step.
2319ec656cfSTomasz Figa
2329ec656cfSTomasz Figa4. Set the raw frame interval on the ``OUTPUT`` queue via
2339ec656cfSTomasz Figa   :c:func:`VIDIOC_S_PARM`. This also sets the coded frame interval on the
2349ec656cfSTomasz Figa   ``CAPTURE`` queue to the same value.
2359ec656cfSTomasz Figa
2369ec656cfSTomasz Figa   * **Required fields:**
2379ec656cfSTomasz Figa
2389ec656cfSTomasz Figa     ``type``
2399ec656cfSTomasz Figa	 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
2409ec656cfSTomasz Figa
2419ec656cfSTomasz Figa     ``parm.output``
2429ec656cfSTomasz Figa	 set all fields except ``parm.output.timeperframe`` to 0.
2439ec656cfSTomasz Figa
2449ec656cfSTomasz Figa     ``parm.output.timeperframe``
2459ec656cfSTomasz Figa	 the desired frame interval; the encoder may adjust it to
2469ec656cfSTomasz Figa	 match hardware requirements.
2479ec656cfSTomasz Figa
2481073f441SPaul Kocialkowski   * **Returned fields:**
2499ec656cfSTomasz Figa
2509ec656cfSTomasz Figa     ``parm.output.timeperframe``
2519ec656cfSTomasz Figa	 the adjusted frame interval.
2529ec656cfSTomasz Figa
2539ec656cfSTomasz Figa   .. important::
2549ec656cfSTomasz Figa
2559ec656cfSTomasz Figa      Changing the ``OUTPUT`` frame interval *also* sets the framerate that
2569ec656cfSTomasz Figa      the encoder uses to encode the video. So setting the frame interval
2579ec656cfSTomasz Figa      to 1/24 (or 24 frames per second) will produce a coded video stream
2589ec656cfSTomasz Figa      that can be played back at that speed. The frame interval for the
2599ec656cfSTomasz Figa      ``OUTPUT`` queue is just a hint, the application may provide raw
2609ec656cfSTomasz Figa      frames at a different rate. It can be used by the driver to help
2619ec656cfSTomasz Figa      schedule multiple encoders running in parallel.
2629ec656cfSTomasz Figa
2639ec656cfSTomasz Figa      In the next step the ``CAPTURE`` frame interval can optionally be
2649ec656cfSTomasz Figa      changed to a different value. This is useful for off-line encoding
2659ec656cfSTomasz Figa      were the coded frame interval can be different from the rate at
2669ec656cfSTomasz Figa      which raw frames are supplied.
2679ec656cfSTomasz Figa
2689ec656cfSTomasz Figa   .. important::
2699ec656cfSTomasz Figa
2709ec656cfSTomasz Figa      ``timeperframe`` deals with *frames*, not fields. So for interlaced
2719ec656cfSTomasz Figa      formats this is the time per two fields, since a frame consists of
2729ec656cfSTomasz Figa      a top and a bottom field.
2739ec656cfSTomasz Figa
2749ec656cfSTomasz Figa   .. note::
2759ec656cfSTomasz Figa
2769ec656cfSTomasz Figa      It is due to historical reasons that changing the ``OUTPUT`` frame
2779ec656cfSTomasz Figa      interval also changes the coded frame interval on the ``CAPTURE``
2789ec656cfSTomasz Figa      queue. Ideally these would be independent settings, but that would
2799ec656cfSTomasz Figa      break the existing API.
2809ec656cfSTomasz Figa
2819ec656cfSTomasz Figa5. **Optional** Set the coded frame interval on the ``CAPTURE`` queue via
2829ec656cfSTomasz Figa   :c:func:`VIDIOC_S_PARM`. This is only necessary if the coded frame
2839ec656cfSTomasz Figa   interval is different from the raw frame interval, which is typically
284d0938277SHans Verkuil   the case for off-line encoding. Support for this feature is signalled
285d0938277SHans Verkuil   by the :ref:`V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL <fmtdesc-flags>` format flag.
2869ec656cfSTomasz Figa
2879ec656cfSTomasz Figa   * **Required fields:**
2889ec656cfSTomasz Figa
2899ec656cfSTomasz Figa     ``type``
2909ec656cfSTomasz Figa	 a ``V4L2_BUF_TYPE_*`` enum appropriate for ``CAPTURE``.
2919ec656cfSTomasz Figa
2929ec656cfSTomasz Figa     ``parm.capture``
2939ec656cfSTomasz Figa	 set all fields except ``parm.capture.timeperframe`` to 0.
2949ec656cfSTomasz Figa
2959ec656cfSTomasz Figa     ``parm.capture.timeperframe``
2969ec656cfSTomasz Figa	 the desired coded frame interval; the encoder may adjust it to
2979ec656cfSTomasz Figa	 match hardware requirements.
2989ec656cfSTomasz Figa
2991073f441SPaul Kocialkowski   * **Returned fields:**
3009ec656cfSTomasz Figa
3019ec656cfSTomasz Figa     ``parm.capture.timeperframe``
3029ec656cfSTomasz Figa	 the adjusted frame interval.
3039ec656cfSTomasz Figa
3049ec656cfSTomasz Figa   .. important::
3059ec656cfSTomasz Figa
3069ec656cfSTomasz Figa      Changing the ``CAPTURE`` frame interval sets the framerate for the
3079ec656cfSTomasz Figa      coded video. It does *not* set the rate at which buffers arrive on the
3089ec656cfSTomasz Figa      ``CAPTURE`` queue, that depends on how fast the encoder is and how
3099ec656cfSTomasz Figa      fast raw frames are queued on the ``OUTPUT`` queue.
3109ec656cfSTomasz Figa
3119ec656cfSTomasz Figa   .. important::
3129ec656cfSTomasz Figa
3139ec656cfSTomasz Figa      ``timeperframe`` deals with *frames*, not fields. So for interlaced
3149ec656cfSTomasz Figa      formats this is the time per two fields, since a frame consists of
3159ec656cfSTomasz Figa      a top and a bottom field.
3169ec656cfSTomasz Figa
3179ec656cfSTomasz Figa   .. note::
3189ec656cfSTomasz Figa
3199ec656cfSTomasz Figa      Not all drivers support this functionality, in that case just set
3209ec656cfSTomasz Figa      the desired coded frame interval for the ``OUTPUT`` queue.
3219ec656cfSTomasz Figa
3229ec656cfSTomasz Figa      However, drivers that can schedule multiple encoders based on the
3239ec656cfSTomasz Figa      ``OUTPUT`` frame interval must support this optional feature.
3249ec656cfSTomasz Figa
3259ec656cfSTomasz Figa6. **Optional.** Set the visible resolution for the stream metadata via
3269ec656cfSTomasz Figa   :c:func:`VIDIOC_S_SELECTION` on the ``OUTPUT`` queue if it is desired
3279ec656cfSTomasz Figa   to be different than the full OUTPUT resolution.
3289ec656cfSTomasz Figa
3299ec656cfSTomasz Figa   * **Required fields:**
3309ec656cfSTomasz Figa
3319ec656cfSTomasz Figa     ``type``
3329ec656cfSTomasz Figa         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
3339ec656cfSTomasz Figa
3349ec656cfSTomasz Figa     ``target``
3359ec656cfSTomasz Figa         set to ``V4L2_SEL_TGT_CROP``.
3369ec656cfSTomasz Figa
3379ec656cfSTomasz Figa     ``r.left``, ``r.top``, ``r.width``, ``r.height``
3389ec656cfSTomasz Figa         visible rectangle; this must fit within the `V4L2_SEL_TGT_CROP_BOUNDS`
3399ec656cfSTomasz Figa         rectangle and may be subject to adjustment to match codec and
3409ec656cfSTomasz Figa         hardware constraints.
3419ec656cfSTomasz Figa
3421073f441SPaul Kocialkowski   * **Returned fields:**
3439ec656cfSTomasz Figa
3449ec656cfSTomasz Figa     ``r.left``, ``r.top``, ``r.width``, ``r.height``
3459ec656cfSTomasz Figa         visible rectangle adjusted by the encoder.
3469ec656cfSTomasz Figa
3479ec656cfSTomasz Figa   * The following selection targets are supported on ``OUTPUT``:
3489ec656cfSTomasz Figa
3499ec656cfSTomasz Figa     ``V4L2_SEL_TGT_CROP_BOUNDS``
3509ec656cfSTomasz Figa         equal to the full source frame, matching the active ``OUTPUT``
3519ec656cfSTomasz Figa         format.
3529ec656cfSTomasz Figa
3539ec656cfSTomasz Figa     ``V4L2_SEL_TGT_CROP_DEFAULT``
3549ec656cfSTomasz Figa         equal to ``V4L2_SEL_TGT_CROP_BOUNDS``.
3559ec656cfSTomasz Figa
3569ec656cfSTomasz Figa     ``V4L2_SEL_TGT_CROP``
3579ec656cfSTomasz Figa         rectangle within the source buffer to be encoded into the
3589ec656cfSTomasz Figa         ``CAPTURE`` stream; defaults to ``V4L2_SEL_TGT_CROP_DEFAULT``.
3599ec656cfSTomasz Figa
3609ec656cfSTomasz Figa         .. note::
3619ec656cfSTomasz Figa
3629ec656cfSTomasz Figa            A common use case for this selection target is encoding a source
3639ec656cfSTomasz Figa            video with a resolution that is not a multiple of a macroblock,
3649ec656cfSTomasz Figa            e.g.  the common 1920x1080 resolution may require the source
3659ec656cfSTomasz Figa            buffers to be aligned to 1920x1088 for codecs with 16x16 macroblock
3669ec656cfSTomasz Figa            size. To avoid encoding the padding, the client needs to explicitly
3679ec656cfSTomasz Figa            configure this selection target to 1920x1080.
3689ec656cfSTomasz Figa
3699ec656cfSTomasz Figa   .. warning::
3709ec656cfSTomasz Figa
3719ec656cfSTomasz Figa      The encoder may adjust the crop/compose rectangles to the nearest
3729ec656cfSTomasz Figa      supported ones to meet codec and hardware requirements. The client needs
3739ec656cfSTomasz Figa      to check the adjusted rectangle returned by :c:func:`VIDIOC_S_SELECTION`.
3749ec656cfSTomasz Figa
3759ec656cfSTomasz Figa7. Allocate buffers for both ``OUTPUT`` and ``CAPTURE`` via
3769ec656cfSTomasz Figa   :c:func:`VIDIOC_REQBUFS`. This may be performed in any order.
3779ec656cfSTomasz Figa
3789ec656cfSTomasz Figa   * **Required fields:**
3799ec656cfSTomasz Figa
3809ec656cfSTomasz Figa     ``count``
3819ec656cfSTomasz Figa         requested number of buffers to allocate; greater than zero.
3829ec656cfSTomasz Figa
3839ec656cfSTomasz Figa     ``type``
3849ec656cfSTomasz Figa         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT`` or
3859ec656cfSTomasz Figa         ``CAPTURE``.
3869ec656cfSTomasz Figa
3879ec656cfSTomasz Figa     other fields
3889ec656cfSTomasz Figa         follow standard semantics.
3899ec656cfSTomasz Figa
3901073f441SPaul Kocialkowski   * **Returned fields:**
3919ec656cfSTomasz Figa
3929ec656cfSTomasz Figa     ``count``
3939ec656cfSTomasz Figa          actual number of buffers allocated.
3949ec656cfSTomasz Figa
3959ec656cfSTomasz Figa   .. warning::
3969ec656cfSTomasz Figa
3979ec656cfSTomasz Figa      The actual number of allocated buffers may differ from the ``count``
3989ec656cfSTomasz Figa      given. The client must check the updated value of ``count`` after the
3999ec656cfSTomasz Figa      call returns.
4009ec656cfSTomasz Figa
4019ec656cfSTomasz Figa   .. note::
4029ec656cfSTomasz Figa
4039ec656cfSTomasz Figa      To allocate more than the minimum number of OUTPUT buffers (for pipeline
4049ec656cfSTomasz Figa      depth), the client may query the ``V4L2_CID_MIN_BUFFERS_FOR_OUTPUT``
4059ec656cfSTomasz Figa      control to get the minimum number of buffers required, and pass the
4069ec656cfSTomasz Figa      obtained value plus the number of additional buffers needed in the
4079ec656cfSTomasz Figa      ``count`` field to :c:func:`VIDIOC_REQBUFS`.
4089ec656cfSTomasz Figa
4099ec656cfSTomasz Figa   Alternatively, :c:func:`VIDIOC_CREATE_BUFS` can be used to have more
4109ec656cfSTomasz Figa   control over buffer allocation.
4119ec656cfSTomasz Figa
4129ec656cfSTomasz Figa   * **Required fields:**
4139ec656cfSTomasz Figa
4149ec656cfSTomasz Figa     ``count``
4159ec656cfSTomasz Figa         requested number of buffers to allocate; greater than zero.
4169ec656cfSTomasz Figa
4179ec656cfSTomasz Figa     ``type``
4189ec656cfSTomasz Figa         a ``V4L2_BUF_TYPE_*`` enum appropriate for ``OUTPUT``.
4199ec656cfSTomasz Figa
4209ec656cfSTomasz Figa     other fields
4219ec656cfSTomasz Figa         follow standard semantics.
4229ec656cfSTomasz Figa
4231073f441SPaul Kocialkowski   * **Returned fields:**
4249ec656cfSTomasz Figa
4259ec656cfSTomasz Figa     ``count``
4269ec656cfSTomasz Figa         adjusted to the number of allocated buffers.
4279ec656cfSTomasz Figa
4289ec656cfSTomasz Figa8. Begin streaming on both ``OUTPUT`` and ``CAPTURE`` queues via
4299ec656cfSTomasz Figa   :c:func:`VIDIOC_STREAMON`. This may be performed in any order. The actual
4309ec656cfSTomasz Figa   encoding process starts when both queues start streaming.
4319ec656cfSTomasz Figa
4329ec656cfSTomasz Figa.. note::
4339ec656cfSTomasz Figa
4349ec656cfSTomasz Figa   If the client stops the ``CAPTURE`` queue during the encode process and then
4359ec656cfSTomasz Figa   restarts it again, the encoder will begin generating a stream independent
4369ec656cfSTomasz Figa   from the stream generated before the stop. The exact constraints depend
4379ec656cfSTomasz Figa   on the coded format, but may include the following implications:
4389ec656cfSTomasz Figa
4399ec656cfSTomasz Figa   * encoded frames produced after the restart must not reference any
4409ec656cfSTomasz Figa     frames produced before the stop, e.g. no long term references for
4419ec656cfSTomasz Figa     H.264/HEVC,
4429ec656cfSTomasz Figa
4439ec656cfSTomasz Figa   * any headers that must be included in a standalone stream must be
4449ec656cfSTomasz Figa     produced again, e.g. SPS and PPS for H.264/HEVC.
4459ec656cfSTomasz Figa
4469ec656cfSTomasz FigaEncoding
4479ec656cfSTomasz Figa========
4489ec656cfSTomasz Figa
4499ec656cfSTomasz FigaThis state is reached after the `Initialization` sequence finishes
4509ec656cfSTomasz Figasuccessfully.  In this state, the client queues and dequeues buffers to both
4519ec656cfSTomasz Figaqueues via :c:func:`VIDIOC_QBUF` and :c:func:`VIDIOC_DQBUF`, following the
4529ec656cfSTomasz Figastandard semantics.
4539ec656cfSTomasz Figa
4549ec656cfSTomasz FigaThe content of encoded ``CAPTURE`` buffers depends on the active coded pixel
4559ec656cfSTomasz Figaformat and may be affected by codec-specific extended controls, as stated
4569ec656cfSTomasz Figain the documentation of each format.
4579ec656cfSTomasz Figa
4589ec656cfSTomasz FigaBoth queues operate independently, following standard behavior of V4L2 buffer
4599ec656cfSTomasz Figaqueues and memory-to-memory devices. In addition, the order of encoded frames
4609ec656cfSTomasz Figadequeued from the ``CAPTURE`` queue may differ from the order of queuing raw
4619ec656cfSTomasz Figaframes to the ``OUTPUT`` queue, due to properties of the selected coded format,
4629ec656cfSTomasz Figae.g. frame reordering.
4639ec656cfSTomasz Figa
4649ec656cfSTomasz FigaThe client must not assume any direct relationship between ``CAPTURE`` and
4659ec656cfSTomasz Figa``OUTPUT`` buffers and any specific timing of buffers becoming
4669ec656cfSTomasz Figaavailable to dequeue. Specifically:
4679ec656cfSTomasz Figa
4689ec656cfSTomasz Figa* a buffer queued to ``OUTPUT`` may result in more than one buffer produced on
4699ec656cfSTomasz Figa  ``CAPTURE`` (for example, if returning an encoded frame allowed the encoder
4709ec656cfSTomasz Figa  to return a frame that preceded it in display, but succeeded it in the decode
4719ec656cfSTomasz Figa  order; however, there may be other reasons for this as well),
4729ec656cfSTomasz Figa
4739ec656cfSTomasz Figa* a buffer queued to ``OUTPUT`` may result in a buffer being produced on
4749ec656cfSTomasz Figa  ``CAPTURE`` later into encode process, and/or after processing further
4759ec656cfSTomasz Figa  ``OUTPUT`` buffers, or be returned out of order, e.g. if display
4769ec656cfSTomasz Figa  reordering is used,
4779ec656cfSTomasz Figa
4789ec656cfSTomasz Figa* buffers may become available on the ``CAPTURE`` queue without additional
4799ec656cfSTomasz Figa  buffers queued to ``OUTPUT`` (e.g. during drain or ``EOS``), because of the
4809ec656cfSTomasz Figa  ``OUTPUT`` buffers queued in the past whose encoding results are only
4819ec656cfSTomasz Figa  available at later time, due to specifics of the encoding process,
4829ec656cfSTomasz Figa
4839ec656cfSTomasz Figa* buffers queued to ``OUTPUT`` may not become available to dequeue instantly
4849ec656cfSTomasz Figa  after being encoded into a corresponding ``CAPTURE`` buffer, e.g. if the
4859ec656cfSTomasz Figa  encoder needs to use the frame as a reference for encoding further frames.
4869ec656cfSTomasz Figa
4879ec656cfSTomasz Figa.. note::
4889ec656cfSTomasz Figa
4899ec656cfSTomasz Figa   To allow matching encoded ``CAPTURE`` buffers with ``OUTPUT`` buffers they
4909ec656cfSTomasz Figa   originated from, the client can set the ``timestamp`` field of the
4919ec656cfSTomasz Figa   :c:type:`v4l2_buffer` struct when queuing an ``OUTPUT`` buffer. The
4929ec656cfSTomasz Figa   ``CAPTURE`` buffer(s), which resulted from encoding that ``OUTPUT`` buffer
4939ec656cfSTomasz Figa   will have their ``timestamp`` field set to the same value when dequeued.
4949ec656cfSTomasz Figa
4959ec656cfSTomasz Figa   In addition to the straightforward case of one ``OUTPUT`` buffer producing
4969ec656cfSTomasz Figa   one ``CAPTURE`` buffer, the following cases are defined:
4979ec656cfSTomasz Figa
4989ec656cfSTomasz Figa   * one ``OUTPUT`` buffer generates multiple ``CAPTURE`` buffers: the same
4999ec656cfSTomasz Figa     ``OUTPUT`` timestamp will be copied to multiple ``CAPTURE`` buffers,
5009ec656cfSTomasz Figa
5019ec656cfSTomasz Figa   * the encoding order differs from the presentation order (i.e. the
5029ec656cfSTomasz Figa     ``CAPTURE`` buffers are out-of-order compared to the ``OUTPUT`` buffers):
5039ec656cfSTomasz Figa     ``CAPTURE`` timestamps will not retain the order of ``OUTPUT`` timestamps.
5049ec656cfSTomasz Figa
5059ec656cfSTomasz Figa.. note::
5069ec656cfSTomasz Figa
5079ec656cfSTomasz Figa   To let the client distinguish between frame types (keyframes, intermediate
5089ec656cfSTomasz Figa   frames; the exact list of types depends on the coded format), the
5099ec656cfSTomasz Figa   ``CAPTURE`` buffers will have corresponding flag bits set in their
5109ec656cfSTomasz Figa   :c:type:`v4l2_buffer` struct when dequeued. See the documentation of
5119ec656cfSTomasz Figa   :c:type:`v4l2_buffer` and each coded pixel format for exact list of flags
5129ec656cfSTomasz Figa   and their meanings.
5139ec656cfSTomasz Figa
5149ec656cfSTomasz FigaShould an encoding error occur, it will be reported to the client with the level
5159ec656cfSTomasz Figaof details depending on the encoder capabilities. Specifically:
5169ec656cfSTomasz Figa
5179ec656cfSTomasz Figa* the ``CAPTURE`` buffer (if any) that contains the results of the failed encode
5189ec656cfSTomasz Figa  operation will be returned with the ``V4L2_BUF_FLAG_ERROR`` flag set,
5199ec656cfSTomasz Figa
5209ec656cfSTomasz Figa* if the encoder is able to precisely report the ``OUTPUT`` buffer(s) that triggered
5219ec656cfSTomasz Figa  the error, such buffer(s) will be returned with the ``V4L2_BUF_FLAG_ERROR`` flag
5229ec656cfSTomasz Figa  set.
5239ec656cfSTomasz Figa
5249ec656cfSTomasz Figa.. note::
5259ec656cfSTomasz Figa
5269ec656cfSTomasz Figa   If a ``CAPTURE`` buffer is too small then it is just returned with the
5279ec656cfSTomasz Figa   ``V4L2_BUF_FLAG_ERROR`` flag set. More work is needed to detect that this
5289ec656cfSTomasz Figa   error occurred because the buffer was too small, and to provide support to
5299ec656cfSTomasz Figa   free existing buffers that were too small.
5309ec656cfSTomasz Figa
5319ec656cfSTomasz FigaIn case of a fatal failure that does not allow the encoding to continue, any
5329ec656cfSTomasz Figafurther operations on corresponding encoder file handle will return the -EIO
5339ec656cfSTomasz Figaerror code. The client may close the file handle and open a new one, or
5349ec656cfSTomasz Figaalternatively reinitialize the instance by stopping streaming on both queues,
5359ec656cfSTomasz Figareleasing all buffers and performing the Initialization sequence again.
5369ec656cfSTomasz Figa
5379ec656cfSTomasz FigaEncoding Parameter Changes
5389ec656cfSTomasz Figa==========================
5399ec656cfSTomasz Figa
5409ec656cfSTomasz FigaThe client is allowed to use :c:func:`VIDIOC_S_CTRL` to change encoder
5419ec656cfSTomasz Figaparameters at any time. The availability of parameters is encoder-specific
5429ec656cfSTomasz Figaand the client must query the encoder to find the set of available controls.
5439ec656cfSTomasz Figa
5449ec656cfSTomasz FigaThe ability to change each parameter during encoding is encoder-specific, as
5459ec656cfSTomasz Figaper the standard semantics of the V4L2 control interface. The client may
5469ec656cfSTomasz Figaattempt to set a control during encoding and if the operation fails with the
5479ec656cfSTomasz Figa-EBUSY error code, the ``CAPTURE`` queue needs to be stopped for the
5489ec656cfSTomasz Figaconfiguration change to be allowed. To do this, it may follow the `Drain`
5499ec656cfSTomasz Figasequence to avoid losing the already queued/encoded frames.
5509ec656cfSTomasz Figa
5519ec656cfSTomasz FigaThe timing of parameter updates is encoder-specific, as per the standard
5529ec656cfSTomasz Figasemantics of the V4L2 control interface. If the client needs to apply the
5539ec656cfSTomasz Figaparameters exactly at specific frame, using the Request API
5549ec656cfSTomasz Figa(:ref:`media-request-api`) should be considered, if supported by the encoder.
5559ec656cfSTomasz Figa
5569ec656cfSTomasz FigaDrain
5579ec656cfSTomasz Figa=====
5589ec656cfSTomasz Figa
5599ec656cfSTomasz FigaTo ensure that all the queued ``OUTPUT`` buffers have been processed and the
5609ec656cfSTomasz Figarelated ``CAPTURE`` buffers are given to the client, the client must follow the
5619ec656cfSTomasz Figadrain sequence described below. After the drain sequence ends, the client has
5629ec656cfSTomasz Figareceived all encoded frames for all ``OUTPUT`` buffers queued before the
5639ec656cfSTomasz Figasequence was started.
5649ec656cfSTomasz Figa
5659ec656cfSTomasz Figa1. Begin the drain sequence by issuing :c:func:`VIDIOC_ENCODER_CMD`.
5669ec656cfSTomasz Figa
5679ec656cfSTomasz Figa   * **Required fields:**
5689ec656cfSTomasz Figa
5699ec656cfSTomasz Figa     ``cmd``
5709ec656cfSTomasz Figa         set to ``V4L2_ENC_CMD_STOP``.
5719ec656cfSTomasz Figa
5729ec656cfSTomasz Figa     ``flags``
5739ec656cfSTomasz Figa         set to 0.
5749ec656cfSTomasz Figa
5759ec656cfSTomasz Figa     ``pts``
5769ec656cfSTomasz Figa         set to 0.
5779ec656cfSTomasz Figa
5789ec656cfSTomasz Figa   .. warning::
5799ec656cfSTomasz Figa
5809ec656cfSTomasz Figa      The sequence can be only initiated if both ``OUTPUT`` and ``CAPTURE``
5819ec656cfSTomasz Figa      queues are streaming. For compatibility reasons, the call to
5829ec656cfSTomasz Figa      :c:func:`VIDIOC_ENCODER_CMD` will not fail even if any of the queues is
5839ec656cfSTomasz Figa      not streaming, but at the same time it will not initiate the `Drain`
5849ec656cfSTomasz Figa      sequence and so the steps described below would not be applicable.
5859ec656cfSTomasz Figa
5869ec656cfSTomasz Figa2. Any ``OUTPUT`` buffers queued by the client before the
5879ec656cfSTomasz Figa   :c:func:`VIDIOC_ENCODER_CMD` was issued will be processed and encoded as
5889ec656cfSTomasz Figa   normal. The client must continue to handle both queues independently,
5899ec656cfSTomasz Figa   similarly to normal encode operation. This includes:
5909ec656cfSTomasz Figa
5919ec656cfSTomasz Figa   * queuing and dequeuing ``CAPTURE`` buffers, until a buffer marked with the
5929ec656cfSTomasz Figa     ``V4L2_BUF_FLAG_LAST`` flag is dequeued,
5939ec656cfSTomasz Figa
5949ec656cfSTomasz Figa     .. warning::
5959ec656cfSTomasz Figa
5969ec656cfSTomasz Figa        The last buffer may be empty (with :c:type:`v4l2_buffer`
5979ec656cfSTomasz Figa        ``bytesused`` = 0) and in that case it must be ignored by the client,
5989ec656cfSTomasz Figa        as it does not contain an encoded frame.
5999ec656cfSTomasz Figa
6009ec656cfSTomasz Figa     .. note::
6019ec656cfSTomasz Figa
6029ec656cfSTomasz Figa        Any attempt to dequeue more ``CAPTURE`` buffers beyond the buffer
6039ec656cfSTomasz Figa        marked with ``V4L2_BUF_FLAG_LAST`` will result in a -EPIPE error from
6049ec656cfSTomasz Figa        :c:func:`VIDIOC_DQBUF`.
6059ec656cfSTomasz Figa
6069ec656cfSTomasz Figa   * dequeuing processed ``OUTPUT`` buffers, until all the buffers queued
6079ec656cfSTomasz Figa     before the ``V4L2_ENC_CMD_STOP`` command are dequeued,
6089ec656cfSTomasz Figa
6099ec656cfSTomasz Figa   * dequeuing the ``V4L2_EVENT_EOS`` event, if the client subscribes to it.
6109ec656cfSTomasz Figa
6119ec656cfSTomasz Figa   .. note::
6129ec656cfSTomasz Figa
6139ec656cfSTomasz Figa      For backwards compatibility, the encoder will signal a ``V4L2_EVENT_EOS``
6149ec656cfSTomasz Figa      event when the last frame has been encoded and all frames are ready to be
6159ec656cfSTomasz Figa      dequeued. It is deprecated behavior and the client must not rely on it.
6169ec656cfSTomasz Figa      The ``V4L2_BUF_FLAG_LAST`` buffer flag should be used instead.
6179ec656cfSTomasz Figa
6189ec656cfSTomasz Figa3. Once all ``OUTPUT`` buffers queued before the ``V4L2_ENC_CMD_STOP`` call are
6199ec656cfSTomasz Figa   dequeued and the last ``CAPTURE`` buffer is dequeued, the encoder is stopped
6209ec656cfSTomasz Figa   and it will accept, but not process any newly queued ``OUTPUT`` buffers
6219ec656cfSTomasz Figa   until the client issues any of the following operations:
6229ec656cfSTomasz Figa
6239ec656cfSTomasz Figa   * ``V4L2_ENC_CMD_START`` - the encoder will not be reset and will resume
6249ec656cfSTomasz Figa     operation normally, with all the state from before the drain,
6259ec656cfSTomasz Figa
6269ec656cfSTomasz Figa   * a pair of :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the
6279ec656cfSTomasz Figa     ``CAPTURE`` queue - the encoder will be reset (see the `Reset` sequence)
6289ec656cfSTomasz Figa     and then resume encoding,
6299ec656cfSTomasz Figa
6309ec656cfSTomasz Figa   * a pair of :c:func:`VIDIOC_STREAMOFF` and :c:func:`VIDIOC_STREAMON` on the
6319ec656cfSTomasz Figa     ``OUTPUT`` queue - the encoder will resume operation normally, however any
6329ec656cfSTomasz Figa     source frames queued to the ``OUTPUT`` queue between ``V4L2_ENC_CMD_STOP``
6339ec656cfSTomasz Figa     and :c:func:`VIDIOC_STREAMOFF` will be discarded.
6349ec656cfSTomasz Figa
6359ec656cfSTomasz Figa.. note::
6369ec656cfSTomasz Figa
6379ec656cfSTomasz Figa   Once the drain sequence is initiated, the client needs to drive it to
6389ec656cfSTomasz Figa   completion, as described by the steps above, unless it aborts the process by
6399ec656cfSTomasz Figa   issuing :c:func:`VIDIOC_STREAMOFF` on any of the ``OUTPUT`` or ``CAPTURE``
6409ec656cfSTomasz Figa   queues.  The client is not allowed to issue ``V4L2_ENC_CMD_START`` or
6419ec656cfSTomasz Figa   ``V4L2_ENC_CMD_STOP`` again while the drain sequence is in progress and they
6429ec656cfSTomasz Figa   will fail with -EBUSY error code if attempted.
6439ec656cfSTomasz Figa
6449ec656cfSTomasz Figa   For reference, handling of various corner cases is described below:
6459ec656cfSTomasz Figa
6469ec656cfSTomasz Figa   * In case of no buffer in the ``OUTPUT`` queue at the time the
6479ec656cfSTomasz Figa     ``V4L2_ENC_CMD_STOP`` command was issued, the drain sequence completes
6489ec656cfSTomasz Figa     immediately and the encoder returns an empty ``CAPTURE`` buffer with the
6499ec656cfSTomasz Figa     ``V4L2_BUF_FLAG_LAST`` flag set.
6509ec656cfSTomasz Figa
6519ec656cfSTomasz Figa   * In case of no buffer in the ``CAPTURE`` queue at the time the drain
6529ec656cfSTomasz Figa     sequence completes, the next time the client queues a ``CAPTURE`` buffer
6539ec656cfSTomasz Figa     it is returned at once as an empty buffer with the ``V4L2_BUF_FLAG_LAST``
6549ec656cfSTomasz Figa     flag set.
6559ec656cfSTomasz Figa
6569ec656cfSTomasz Figa   * If :c:func:`VIDIOC_STREAMOFF` is called on the ``CAPTURE`` queue in the
6579ec656cfSTomasz Figa     middle of the drain sequence, the drain sequence is canceled and all
6589ec656cfSTomasz Figa     ``CAPTURE`` buffers are implicitly returned to the client.
6599ec656cfSTomasz Figa
6609ec656cfSTomasz Figa   * If :c:func:`VIDIOC_STREAMOFF` is called on the ``OUTPUT`` queue in the
6619ec656cfSTomasz Figa     middle of the drain sequence, the drain sequence completes immediately and
6629ec656cfSTomasz Figa     next ``CAPTURE`` buffer will be returned empty with the
6639ec656cfSTomasz Figa     ``V4L2_BUF_FLAG_LAST`` flag set.
6649ec656cfSTomasz Figa
6659ec656cfSTomasz Figa   Although not mandatory, the availability of encoder commands may be queried
6669ec656cfSTomasz Figa   using :c:func:`VIDIOC_TRY_ENCODER_CMD`.
6679ec656cfSTomasz Figa
6689ec656cfSTomasz FigaReset
6699ec656cfSTomasz Figa=====
6709ec656cfSTomasz Figa
6719ec656cfSTomasz FigaThe client may want to request the encoder to reinitialize the encoding, so
6729ec656cfSTomasz Figathat the following stream data becomes independent from the stream data
6739ec656cfSTomasz Figagenerated before. Depending on the coded format, that may imply that:
6749ec656cfSTomasz Figa
6759ec656cfSTomasz Figa* encoded frames produced after the restart must not reference any frames
6769ec656cfSTomasz Figa  produced before the stop, e.g. no long term references for H.264/HEVC,
6779ec656cfSTomasz Figa
6789ec656cfSTomasz Figa* any headers that must be included in a standalone stream must be produced
6799ec656cfSTomasz Figa  again, e.g. SPS and PPS for H.264/HEVC.
6809ec656cfSTomasz Figa
6819ec656cfSTomasz FigaThis can be achieved by performing the reset sequence.
6829ec656cfSTomasz Figa
6839ec656cfSTomasz Figa1. Perform the `Drain` sequence to ensure all the in-flight encoding finishes
6849ec656cfSTomasz Figa   and respective buffers are dequeued.
6859ec656cfSTomasz Figa
6869ec656cfSTomasz Figa2. Stop streaming on the ``CAPTURE`` queue via :c:func:`VIDIOC_STREAMOFF`. This
6879ec656cfSTomasz Figa   will return all currently queued ``CAPTURE`` buffers to the client, without
6889ec656cfSTomasz Figa   valid frame data.
6899ec656cfSTomasz Figa
6909ec656cfSTomasz Figa3. Start streaming on the ``CAPTURE`` queue via :c:func:`VIDIOC_STREAMON` and
6919ec656cfSTomasz Figa   continue with regular encoding sequence. The encoded frames produced into
6929ec656cfSTomasz Figa   ``CAPTURE`` buffers from now on will contain a standalone stream that can be
6939ec656cfSTomasz Figa   decoded without the need for frames encoded before the reset sequence,
6949ec656cfSTomasz Figa   starting at the first ``OUTPUT`` buffer queued after issuing the
6959ec656cfSTomasz Figa   `V4L2_ENC_CMD_STOP` of the `Drain` sequence.
6969ec656cfSTomasz Figa
6979ec656cfSTomasz FigaThis sequence may be also used to change encoding parameters for encoders
6989ec656cfSTomasz Figawithout the ability to change the parameters on the fly.
6999ec656cfSTomasz Figa
7009ec656cfSTomasz FigaCommit Points
7019ec656cfSTomasz Figa=============
7029ec656cfSTomasz Figa
7039ec656cfSTomasz FigaSetting formats and allocating buffers triggers changes in the behavior of the
7049ec656cfSTomasz Figaencoder.
7059ec656cfSTomasz Figa
7069ec656cfSTomasz Figa1. Setting the format on the ``CAPTURE`` queue may change the set of formats
7079ec656cfSTomasz Figa   supported/advertised on the ``OUTPUT`` queue. In particular, it also means
7089ec656cfSTomasz Figa   that the ``OUTPUT`` format may be reset and the client must not rely on the
7099ec656cfSTomasz Figa   previously set format being preserved.
7109ec656cfSTomasz Figa
7119ec656cfSTomasz Figa2. Enumerating formats on the ``OUTPUT`` queue always returns only formats
7129ec656cfSTomasz Figa   supported for the current ``CAPTURE`` format.
7139ec656cfSTomasz Figa
7149ec656cfSTomasz Figa3. Setting the format on the ``OUTPUT`` queue does not change the list of
7159ec656cfSTomasz Figa   formats available on the ``CAPTURE`` queue. An attempt to set the ``OUTPUT``
7169ec656cfSTomasz Figa   format that is not supported for the currently selected ``CAPTURE`` format
7179ec656cfSTomasz Figa   will result in the encoder adjusting the requested ``OUTPUT`` format to a
7189ec656cfSTomasz Figa   supported one.
7199ec656cfSTomasz Figa
7209ec656cfSTomasz Figa4. Enumerating formats on the ``CAPTURE`` queue always returns the full set of
7219ec656cfSTomasz Figa   supported coded formats, irrespective of the current ``OUTPUT`` format.
7229ec656cfSTomasz Figa
7239ec656cfSTomasz Figa5. While buffers are allocated on any of the ``OUTPUT`` or ``CAPTURE`` queues,
7249ec656cfSTomasz Figa   the client must not change the format on the ``CAPTURE`` queue. Drivers will
7259ec656cfSTomasz Figa   return the -EBUSY error code for any such format change attempt.
7269ec656cfSTomasz Figa
7279ec656cfSTomasz FigaTo summarize, setting formats and allocation must always start with the
7289ec656cfSTomasz Figa``CAPTURE`` queue and the ``CAPTURE`` queue is the master that governs the
7299ec656cfSTomasz Figaset of supported formats for the ``OUTPUT`` queue.
730