> ## Documentation Index
> Fetch the complete documentation index at: https://imsdkdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# v4l2vp9dec

> Hardware-accelerated VP9 video decoder using the V4L2 stateful decoder API on Qualcomm hardware.

# Overview

`v4l2vp9dec` is a hardware-accelerated video decoder that uses the Video4Linux2 (V4L2) stateful decoder API to offload VP9 video decoding to the Qualcomm Video Processing Unit (VPU).

This plugin is provided and maintained by the GStreamer community. This document focuses on its usage in conjunction with Qualcomm-specific QIM SDK GStreamer plugins, along with relevant use cases and internal architectural considerations.

`v4l2vp9dec` is typically used to decode VP9 streams provided by:

* File sources (WebM, Matroska/MKV containers)
* RTSP sources
* HLS/HTTP streaming sources (e.g., YouTube, web video)

The element is responsible **only for decoding** video frames. It does not demux or parse streams on its own — those operations must be handled by peer GStreamer elements upstream (e.g., `matroskademux`).

<Note>
  VP9 frames are self-contained and self-framing. Unlike H.264/H.265, no separate parse element (e.g., `vp9parse`) is required between the demuxer and `v4l2vp9dec`.
</Note>

## Example Pipeline

<img src="https://mintcdn.com/qimsdk/xdnKhBBjxpS5mUYP/plugin-reference/images/v4l2vp9dec_pipeline.png?fit=max&auto=format&n=xdnKhBBjxpS5mUYP&q=85&s=ea62168a1b711e71428e9595fbcd2565" alt="" width="730" height="71" data-path="plugin-reference/images/v4l2vp9dec_pipeline.png" />

<Steps>
  <Step title="Download Required Files">
    vp9\_1080p.webm file is used here as an example.
  </Step>

  <Step title="Copy files to device">
    <CodeGroup>
      ```bash SCP (SSH) theme={null}
      # Replace $HOME to the appropriate device path before running the commands.
      # For QLI:    /root
      # For Ubuntu: /home/ubuntu
      # Modify this based on your platform and ensure files are copied to the correct location on the device.
      # Run from your host machine — replace <user> and <device-ip>

      ssh <user>@<device-ip> "mkdir -p $HOME/{media}"
      scp vp9_1080p.webm <user>@<device-ip>:$HOME/media/
      ```
    </CodeGroup>
  </Step>

  <Step title="Connect to device">
    <CodeGroup>
      ```bash SCP (SSH) theme={null}
      # Run from your host machine — replace <user> and <device-ip>
      ssh <user>@<device-ip>
      ```
    </CodeGroup>
  </Step>

  <Step title="Set environment variables">
    Run below command on your device

    ```bash theme={null}
    export SRC_VIDEO_NAME=vp9_1080p.webm
    ```
  </Step>

  <Step title="Run the pipeline">
    ```bash theme={null}
    gst-launch-1.0 -e \
      filesrc location=$HOME/media/$SRC_VIDEO_NAME ! \
      matroskademux ! queue ! \
      v4l2vp9dec capture-io-mode=4 output-io-mode=4 ! \
      video/x-raw,format=NV12 ! queue ! \
      waylandsink fullscreen=true sync=false
    ```
  </Step>
</Steps>

# Key Responsibilities

`v4l2vp9dec` is responsible for:

* **Hardware acceleration** — offloads VP9 decoding to the dedicated VPU
* **V4L2 state management** — manages the V4L2 stateful decoder state machine (device open, buffer allocation, stream on/off)
* **Buffer I/O management** — handles buffer exchange between GStreamer and the V4L2 driver using DMABuf, MMAP, or UserPtr modes
* **Format negotiation** — negotiates raw output formats supported by the hardware, including UBWC-compressed formats (`NV12_Q08C`, `NV12_Q10LE32C`) for reduced memory bandwidth
* **Multi-stream support** — supports multiple concurrent decoder instances (subject to hardware resource limits)
* **Error handling** — supports decoder error controls such as `max-errors` and corrupted-frame discard behavior

# Hierarchy

[GObject](https://docs.gtk.org/gobject/)<br />
   <Icon icon="arrow-turn-down-right" iconType="solid" />[GstObject](https://gstreamer.freedesktop.org/documentation/gstreamer/gstobject.html)<br />
      <Icon icon="arrow-turn-down-right" iconType="solid" />[GstElement](https://gstreamer.freedesktop.org/documentation/gstreamer/gstelement.html)<br />
         <Icon icon="arrow-turn-down-right" iconType="solid" />GstVideoDecoder<br />
            <Icon icon="arrow-turn-down-right" iconType="solid" />GstV4l2VideoDec<br />
               <Icon icon="arrow-turn-down-right" iconType="solid" />v4l2vp9dec

# Pad Templates

### sink

| Capabilities           |              |
| ---------------------- | ------------ |
| `video/x-vp9`          | `format: NA` |
| Availability: *Always* |              |
| Direction: *sink*      |              |

### src

| Capabilities           |                                                                                                           |
| ---------------------- | --------------------------------------------------------------------------------------------------------- |
| `video/x-raw`          | `format: { NV12_Q08C, NV12_Q10LE32C, NV12, NV21 }` <br /> `width: [1, 32768]` <br /> `height: [1, 32768]` |
| Availability: *Always* |                                                                                                           |
| Direction: *source*    |                                                                                                           |

<Note>
  The exact set of profiles reported depends on what the underlying V4L2 driver enumerates via `V4L2_CID_MPEG_VIDEO_VP9_PROFILE` (or `V4L2_CID_MPEG_VIDEO_VPX_PROFILE`). VP9 does not define decoder levels in the V4L2 API; therefore, no level probing is performed.
</Note>

# Element Properties

| Property                             | Description                                                                                                                                                                                                     |
| ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `automatic-request-sync-point-flags` | Flags used when automatically requesting sync points.<br /><br />`Type: Flags`<br />`Default: 0x3 (corrupt-output+discard-input)`<br />`Flags: readable/writable`                                               |
| `automatic-request-sync-points`      | Automatically requests sync points (keyframes) when useful, such as after decoding errors.<br /><br />`Type: Boolean`<br />`Default: false`<br />`Flags: readable/writable`                                     |
| `capture-io-mode`                    | I/O mode for the capture queue (src pad). Controls how decoded frame buffers are allocated and transferred.<br /><br />`Type: Enum `<br />`Default: 0, "auto"`<br />`Flags: readable/writable`                  |
| `device`                             | The V4L2 device node path. Set automatically at registration time. Read-only after element creation.<br /><br />`Type: String`<br />`Default: "/dev/video32"`<br />`Flags: readable`                            |
| `device-fd`                          | File descriptor of the opened V4L2 device. A value of `-1` indicates that the device is not open.<br /><br />`Type: Integer`<br />`Default: -1`<br />`Flags: readable`                                          |
| `device-name`                        | Human-readable name of the V4L2 device as reported by the driver via VIDIOC\_QUERYCAP.<br /><br />`Type: String`<br />`Default: NULL`<br />`Flags: readable`                                                    |
| `discard-corrupted-frames`           | When enabled, frames marked as corrupted by the driver are dropped instead of being passed downstream.<br /><br />`Type: Boolean`<br />`Default: false`<br />`Flags: readable/writable`                         |
| `extra-controls`                     | Extra V4L2 controls (CIDs) to set on the device, specified as a GstStructure. Applied via VIDIOC\_S\_EXT\_CTRLS.<br /><br />`Type: GstStructure`<br />`Default: NULL`<br />`Flags: readable/writable`           |
| `max-errors`                         | Maximum number of consecutive decoder errors before the element returns a flow error. A value of `-1` means unlimited.<br /><br />`Type: Integer`<br />`Default: -1`<br />`Flags: readable/writable`            |
| `min-force-key-unit-interval`        | Minimum interval in nanoseconds between force-keyunit requests sent upstream. A value of `0` means no minimum interval.<br /><br />`Type: Unsigned Integer64`<br />`Default: 0`<br />`Flags: readable/writable` |
| `output-io-mode`                     | I/O mode for the output queue (sink pad). Controls how compressed input buffers are submitted to the driver.<br /><br />`Type: Enum `<br />`Default: 0, "auto"`<br />`Flags: readable/writable`                 |
| `qos`                                | Handles Quality-of-Service events from downstream. When enabled, the decoder may drop frames to maintain real-time playback.<br /><br />`Type: Boolean`<br />`Default: true`<br />`Flags: readable/writable`    |

## I/O Mode Values

Both `capture-io-mode` and `output-io-mode` accept the same `GstV4l2IOMode` enumeration:

| Value           | Integer | Description                                                                                                                       |
| --------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `auto`          | 0       | Automatically select the best I/O mode. Prefers DMABuf export for capture, MMAP for output.                                       |
| `rw`            | 1       | Use read/write system calls. Rarely used for M2M devices.                                                                         |
| `mmap`          | 2       | Use kernel memory-mapped buffers (`V4L2_MEMORY_MMAP`).                                                                            |
| `userptr`       | 3       | Use user-space pointer buffers (`V4L2_MEMORY_USERPTR`).                                                                           |
| `dmabuf`        | 4       | Export buffers as DMABuf file descriptors (`V4L2_MEMORY_DMABUF`). Enables zero-copy sharing with downstream GPU/display elements. |
| `dmabuf-import` | 5       | Import DMABuf file descriptors from an external allocator into the V4L2 queue.                                                    |

# Internal Architecture

`v4l2vp9dec` operates using two V4L2 queue objects internally:

* **Output queue** (`V4L2_BUF_TYPE_VIDEO_OUTPUT`) — receives compressed VP9 frame buffers from upstream
* **Capture queue** (`V4L2_BUF_TYPE_VIDEO_CAPTURE`) — produces decoded raw video frames for downstream

<img src="https://mintcdn.com/qimsdk/xdnKhBBjxpS5mUYP/plugin-reference/images/v4l2vp9dec_internal_architecture.png?fit=max&auto=format&n=xdnKhBBjxpS5mUYP&q=85&s=e79f63f240e07440e4d46fb84248f13a" alt="" width="941" height="1083" data-path="plugin-reference/images/v4l2vp9dec_internal_architecture.png" />

# State Transitions

| Transition               | Decoder Behavior                                                           | Notes                                                                                                                                                              |
| ------------------------ | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `NULL → READY`           | Element becomes ready. Static configuration available.                     | Configure `capture-io-mode` and `output-io-mode` before active decoding.                                                                                           |
| `READY → PAUSED`         | Decoder activates. V4L2 session setup begins.                              | Caps negotiation: sink is `video/x-vp9`, src is `video/x-raw` (NV12/NV12\_Q08C). VP9 frames from WebM/MKV are self-framing; no separate parse element is required. |
| `PAUSED → PLAYING`       | Continuous decode starts.                                                  | With `capture-io-mode=4` and `output-io-mode=4`, pipeline is configured for DMABuf zero-copy.                                                                      |
| `PLAYING → PAUSED`       | Decoding paused at GStreamer scheduling level.                             | No full decoder teardown. Resume continues from current position.                                                                                                  |
| `PLAYING/PAUSED → READY` | Decoder stops. V4L2 streaming stopped, buffers released.                   | Key cleanup transition.                                                                                                                                            |
| `READY → NULL`           | Element fully shut down. All resources released.                           |                                                                                                                                                                    |
| EOS while PLAYING        | Decoder drains pending input, outputs remaining frames, then forwards EOS. |                                                                                                                                                                    |
| FLUSH                    | Current decode interrupted. Queued buffers discarded.                      | Common during seek or pipeline reset. Decoder waits for new VP9 keyframe after flush.                                                                              |

# Dynamic Resolution Change

`v4l2vp9dec` supports mid-stream dynamic resolution changes without requiring a pipeline restart, handled through the V4L2 source change event mechanism:

1. At initialization, the decoder subscribes to `V4L2_EVENT_SOURCE_CHANGE` events on the capture queue.
2. When the driver detects a resolution change in the bitstream, it signals the event.
3. The decoder stops the capture queue, discards the existing buffer pool, re-negotiates format via `VIDIOC_G_FMT`, sets a new output state with updated dimensions, and reallocates the capture buffer pool.

This allows seamless decoding of adaptive bitrate VP9 streams and content with embedded resolution changes.

# Memory and Buffer Management

## DMABuf Usage

Setting `capture-io-mode=4` exports decoded frame buffers as DMABuf file descriptors. These can be imported directly by `waylandsink` or `qtivcomposer` without CPU memory copies, enabling zero-copy pipelines.

## Alignment Requirements

Decoded buffers follow Qualcomm hardware alignment requirements (e.g., 128-byte stride alignment).

## Format Support

Standard NV12 is common. UBWC (Universal Bandwidth Compression) is supported through formats like `NV12_Q08C` to reduce memory bandwidth.

## Codec Header Initialization

VP9 is a frame-based codec — each VP9 frame is self-contained and does not require a separate out-of-band codec header. The first input buffer is used directly as the initialization data. If the input caps contain `codec_data` (e.g., VP9 codec private data from a WebM container), that data is sent first.

## Latency

The decoder computes and reports pipeline latency based on the minimum number of capture buffers required by the driver and the frame duration:

```
latency = min_buffers × frame_duration
```

## Drain and Flush

* **Drain** — sends `V4L2_DEC_CMD_STOP` to signal end-of-stream; waits for all remaining frames to be produced
* **Flush** — stops both queues, resets buffer pools, and restarts streaming; used during seek operations

# Usage

<Note>
  Ensure you have followed the [prerequisites](v4l2vp9dec#example-pipeline) before continuing
</Note>

### Decode VP9 from a WebM File and Display

Demonstrates hardware-accelerated VP9 decoding from a WebM file with DMABuf zero-copy for Wayland preview.

<img src="https://mintcdn.com/qimsdk/xdnKhBBjxpS5mUYP/plugin-reference/images/v4l2vp9dec_display.png?fit=max&auto=format&n=xdnKhBBjxpS5mUYP&q=85&s=f4becabc59e8a19f41bba5d8cc9d38ab" alt="" width="730" height="71" data-path="plugin-reference/images/v4l2vp9dec_display.png" />

```bash theme={null}
gst-launch-1.0 -e \
  filesrc location=$HOME/media/$SRC_VIDEO_NAME ! \
  matroskademux ! queue ! \
  v4l2vp9dec output-io-mode=dmabuf capture-io-mode=dmabuf ! \
  video/x-raw,format=NV12 ! queue ! \
  waylandsink fullscreen=true
```

### Four-Stream Side-by-Side Composition

Demonstrates decoding four VP9 input streams using four hardware decoder instances, arranging them in a 2×2 grid using `qtivcomposer`, and displaying the composed frame.

<img src="https://mintcdn.com/qimsdk/xdnKhBBjxpS5mUYP/plugin-reference/images/v4l2vp9dec_side_by_side_composition.png?fit=max&auto=format&n=xdnKhBBjxpS5mUYP&q=85&s=a7e02c33fd650a7e40c5519cb5093af1" alt="" width="1042" height="427" data-path="plugin-reference/images/v4l2vp9dec_side_by_side_composition.png" />

```bash theme={null}
gst-launch-1.0 -e --gst-debug=2 \
  qtivcomposer name=mixer \
    sink_0::position="<0, 0>"     sink_0::dimensions="<960, 540>" \
    sink_1::position="<960, 0>"   sink_1::dimensions="<960, 540>" \
    sink_2::position="<0, 540>"   sink_2::dimensions="<960, 540>" \
    sink_3::position="<960, 540>" sink_3::dimensions="<960, 540>" \
  ! queue ! waylandsink fullscreen=true sync=false enable-last-sample=false \
  filesrc location=$HOME/media/$SRC_VIDEO_NAME ! \
    matroskademux ! queue ! v4l2vp9dec output-io-mode=4 capture-io-mode=4 ! queue ! mixer. \
  filesrc location=$HOME/media/$SRC_VIDEO_NAME ! \
    matroskademux ! queue ! v4l2vp9dec output-io-mode=4 capture-io-mode=4 ! queue ! mixer. \
  filesrc location=$HOME/media/$SRC_VIDEO_NAME ! \
    matroskademux ! queue ! v4l2vp9dec output-io-mode=4 capture-io-mode=4 ! queue ! mixer. \
  filesrc location=$HOME/media/$SRC_VIDEO_NAME ! \
    matroskademux ! queue ! v4l2vp9dec output-io-mode=4 capture-io-mode=4 ! queue ! mixer.
```
