decoders.rs 1.81 KB
Newer Older
1
2
3
4
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
5
use serde::{Deserialize, Serialize};
6
use utoipa::ToSchema;
7
8

use super::common::EncodedMediaData;
9
10
use super::rdma::DecodedMediaData;
pub mod image;
11
12
#[cfg(feature = "media-ffmpeg")]
pub mod video;
13
14

pub use image::{ImageDecoder, ImageMetadata};
15
16
#[cfg(feature = "media-ffmpeg")]
pub use video::{VideoDecoder, VideoMetadata};
17
18
19
20
21

#[async_trait::async_trait]
pub trait Decoder: Clone + Send + 'static {
    fn decode(&self, data: EncodedMediaData) -> Result<DecodedMediaData>;

22
23
24
25
    // Merges this decoder with an optional runtime override.
    // Limits should always be enforced from the MDC config
    fn with_runtime(&self, runtime: Option<&Self>) -> Self;

26
27
28
29
30
31
32
33
34
    async fn decode_async(&self, data: EncodedMediaData) -> Result<DecodedMediaData> {
        // light clone (only config params)
        let decoder = self.clone();
        // compute heavy -> rayon
        let result = tokio_rayon::spawn(move || decoder.decode(data)).await?;
        Ok(result)
    }
}

35
36
37
/// Media decoder configuration.
/// Used both for MDC server config and runtime `media_io_kwargs`.
/// When used at runtime, limits are enforced from MDC and cannot be overridden.
38
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize, ToSchema)]
39
pub struct MediaDecoder {
40
41
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub image: Option<ImageDecoder>,
42
    #[cfg(feature = "media-ffmpeg")]
43
44
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub video: Option<VideoDecoder>,
45
    // TODO: audio decoder
46
}
47

48
#[derive(Serialize, Deserialize, Clone, Debug)]
49
50
pub enum DecodedMediaMetadata {
    Image(ImageMetadata),
51
52
    #[cfg(feature = "media-ffmpeg")]
    Video(VideoMetadata),
53
}