// Oak Video Editor - Non-Linear Video Editor
// Copyright (C) 2026 Oak Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//! `olive::Decoder` and its supporting types — the media-decoder trait.
//!
//! Mirrors `src/codec/src/decoder.h`. The C++ abstract base plus its
//! FFmpeg/OIIO subclasses become the [`Decoder`] trait (decision 2 in
//! README.md); probe/dispatch lives on the registry functions at the
//! bottom of this module. Audio is handled in raw interleaved-float
//! buffers matching the C ABI, not `oak_core::SampleBuffer` (which the
//! crate does not export).
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
use oak_core::cancelatom::CancelAtom;
use oak_core::{Rational, TimeRange};
use crate::footagedescription::FootageDescription;
use crate::frame::Frame;
/// `OakRenderTexture` — GPU texture token (an oakrender type, opaque to
/// oakcodec). The codec crate cannot depend on oakrender (dependency
/// cycle), so it never constructs a real texture: [`Decoder::retrieve_video`]
/// only reports whether the decode succeeded and returns this unit token
/// (the former empty `CHandle`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OakRenderTexture;
/// `OakNodeBlock` — opaque timeline-block token owned elsewhere; the codec
/// only stores and forwards it (borrowed, never dereferenced). No module
/// ever constructs one (the former `CHandle` was only ever `None`), so the
/// type is an empty marker kept for API compatibility.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OakNodeBlock;
/// `oakcodec_video_stream_info` — POD probe output describing one video
/// stream; see `include/codec/decoder.h`.
#[repr(C)]
pub struct OakCodecVideoStreamInfo {
/// Stream index.
pub stream_index: i32,
/// Width in pixels.
pub width: i32,
/// Height in pixels.
pub height: i32,
/// Frame-rate numerator.
pub frame_rate_num: i32,
/// Frame-rate denominator.
pub frame_rate_den: i32,
/// Stream length in time-base units.
pub duration_ts: i64,
/// Time-base numerator (seconds per time-base unit).
pub time_base_num: i32,
/// Time-base denominator.
pub time_base_den: i32,
/// Native delivery `OakPixelFormat`.
pub format: i32,
/// Plane channel count.
pub channel_count: i32,
/// ISO/IEC 23001-8 color-primaries code point (0 = unknown).
pub color_primaries: i32,
/// ISO/IEC 23001-8 color-transfer code point (0 = unknown).
pub color_trc: i32,
/// 1 when the stream is interlaced.
pub interlaced: i32,
}
/// `oakcodec_audio_stream_info` — POD probe output describing one audio
/// stream; see `include/codec/decoder.h`.
#[repr(C)]
pub struct OakCodecAudioStreamInfo {
/// Stream index.
pub stream_index: i32,
/// Sample rate (Hz).
pub sample_rate: i32,
/// ffmpeg-style channel mask (e.g. 0x3 = stereo).
pub channel_layout: u64,
/// Channel count.
pub channel_count: i32,
/// Stream length in time-base units.
pub duration_ts: i64,
/// Time-base numerator.
pub time_base_num: i32,
/// Time-base denominator.
pub time_base_den: i32,
}
/// Local replacement for `render/rendermodes.h` (oakrender C API has no
/// render-mode counterpart). Values mirror engine/render/rendermodes.h:
/// k_offline = 0, k_online = 1.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i32)]
pub enum RenderMode {
/// Offline / background render.
Offline = 0,
/// Online / real-time render.
Online = 1,
}
/// "Don't force a color range" sentinel for
/// [`RetrieveVideoParams::force_range`] (the actual ranges are the
/// `oak_core_COLOR_RANGE_*` values).
pub const K_COLOR_RANGE_DEFAULT: i32 = -1;
/// `Decoder::RetrieveVideoParams` — what a video retrieve call needs.
pub struct RetrieveVideoParams {
/// Stream to read from.
pub stream: CodecStream,
/// Timestamp, rational seconds.
pub time: Rational,
/// Length of footage before the start (for early-seek semantics).
pub length: TimeRange,
/// Color range override; [`K_COLOR_RANGE_DEFAULT`] means "don't force".
pub force_range: i32,
/// Image sequence: bake the frame number into the filename.
pub is_image_sequence: bool,
/// Image sequence digit count (derived from the filename).
pub image_sequence_digits: i32,
/// Image sequence number to substitute.
pub image_sequence_number: i64,
/// Render mode (drives texture-path choices in the implementations).
pub mode: RenderMode,
/// Frame alpha channel is premultiplied.
pub alpha_is_premultiplied: bool,
/// Target output size for the scaled frame: `Some((w, h))` lets the
/// decoder's swscale pass convert AND resize in one step (native
/// yuv → RGBA/F32 at the target size), skipping the full-resolution
/// float intermediate (a 4K frame is ~132 MB as F32 RGBA — decoding
/// to a 480px preview through it costs ~260 MB of churn per frame).
/// `None` keeps the native size (the old behavior).
pub target_size: Option<(u32, u32)>,
}
/// The colorimetry of a decoded frame, as carried out of the bitstream
/// (raw ISO/IEC 23001-8 / H.273 code points — the same numbering FFmpeg's
/// `AVCodecParameters` uses). The render layer maps these to its input
/// transform (source colorspace → the pipeline working space).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct DecodedColorMeta {
/// Color primaries code point (`AVCOL_PRI_*`; 0/2 = unknown).
pub color_primaries: i32,
/// Transfer characteristic code point (`AVCOL_TRC_*`; 0/2 = unknown).
pub color_trc: i32,
/// True when the decoded RGB is full range (the YUV→RGB used the
/// full-range coefficients).
pub full_range: bool,
}
/// `Decoder::RetrieveAudioStatus` — outcome of an audio retrieve.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RetrieveAudioStatus {
/// Data written to the destination buffer.
Success,
/// The requested range is outside the footage.
InvalidRange,
/// The stream does not support audio.
Unsupported,
/// Media requires a conform that could not be produced.
ConformNeeded,
/// A decoder-level error occurred.
Error,
}
/// `Decoder::RetrieveState`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RetrieveState {
/// Ready to decode.
Ready,
/// Failed to open the stream.
FailedToOpen,
/// The stream index could not be located.
IndexUnavailable,
}
/// `Decoder::CodecStream` — identifies one (filename, stream) pair plus an
/// optional associated timeline block.
///
/// The block is an opaque [`OakNodeBlock`] token that codec only stores and
/// compares, never dereferences or retains (borrowed).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CodecStream {
filename: String,
stream: i32,
block: Option,
}
impl CodecStream {
/// Empty, invalid stream.
pub fn new() -> Self {
CodecStream {
filename: String::new(),
stream: -1,
block: None,
}
}
/// New stream for `(filename, stream)` with an optional block.
pub fn with_block(
filename: String,
stream: i32,
block: Option,
) -> Self {
CodecStream {
filename,
stream,
block,
}
}
/// Non-empty filename and non-negative stream index.
pub fn is_valid(&self) -> bool {
!self.filename.is_empty() && self.stream >= 0
}
/// The file exists on disk.
pub fn exists(&self) -> bool {
Path::new(&self.filename).exists()
}
/// Reset to the empty stream.
pub fn reset(&mut self) {
self.filename.clear();
self.stream = -1;
self.block = None;
}
/// Source filename.
pub fn filename(&self) -> &str {
&self.filename
}
/// Stream index within the source.
pub fn stream(&self) -> i32 {
self.stream
}
/// Associated timeline block (borrowed; only compared, never used).
pub fn block(&self) -> Option {
self.block
}
}
/// `olive::Decoder` — abstraction over external media decoding.
///
/// Implementations are [`crate::ffmpeg::FFmpegDecoder`] and
/// [`crate::oiio::OIIODecoder`]. The trait surface mirrors the C++
/// abstract base; the public API hands out `Arc` values.
pub trait Decoder: Send + Sync {
/// Unique decoder id ("ffmpeg"/"oiio").
fn id(&self) -> String;
/// Whether this decoder supports video streams.
fn supports_video(&self) -> bool {
false
}
/// Whether this decoder supports audio streams.
fn supports_audio(&self) -> bool {
false
}
/// Whether this decoder can read the given file (static probe).
fn probe(
&self,
filename: &str,
cancelled: Option<&CancelAtom>,
) -> Option;
/// Open `stream` for decoding. Thread-safe.
fn open(&self, stream: &CodecStream) -> crate::error::Result<()>;
/// Close the currently open stream (safe when closed).
fn close(&self) -> crate::error::Result<()>;
/// The currently open stream (locked accessor).
fn stream(&self) -> CodecStream;
/// Retrieve a video frame into CPU memory.
fn retrieve_video_frame(&self, p: &RetrieveVideoParams) -> crate::error::Result>;
/// Retrieve a video frame as an imported GPU planar texture (M5 zero
/// copy). `Ok(None)` when the frame is not an importable hardware
/// surface, the import switch is off, no GPU context is available, or
/// the requested output cannot be delivered zero-copy (a size/format
/// that needs the CPU scaler) — the caller then falls back to
/// [`Decoder::retrieve_video_frame`]. Implementations that decode only
/// in software keep the default.
fn retrieve_video_frame_gpu(
&self,
p: &RetrieveVideoParams,
) -> crate::error::Result