Skip to main content

viser_contextaware/
profile.rs

1use serde::{Deserialize, Serialize};
2use viser_ffmpeg::{
3    Codec, RES_360P, RES_480P, RES_720P, RES_1080P, RES_1440P, RES_2160P, Resolution,
4};
5use viser_ladder::Opts as LadderOpts;
6
7/// Target device class that determines resolution caps and codec preferences.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum DeviceClass {
11    /// Smartphones and small tablets.
12    Mobile,
13    /// Laptops and desktop monitors.
14    Desktop,
15    /// 1080p TVs and large displays.
16    Tv,
17    /// 4K (2160p) TVs.
18    Tv4k,
19}
20
21/// Encoding constraints and quality targets for a device class.
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct Profile {
24    /// Human-readable profile name (e.g. `"Mobile"`).
25    pub name: String,
26    /// Device class this profile targets.
27    pub device: DeviceClass,
28    /// Description of the intended device and viewing context.
29    pub description: String,
30    /// VMAF model name used for quality scoring (e.g. `"vmaf_v0.6.1"`).
31    pub vmaf_model: String,
32    /// Maximum resolution allowed for this device.
33    pub max_res: Resolution,
34    /// Candidate resolutions to evaluate.
35    pub resolutions: Vec<Resolution>,
36    /// Codecs to encode with, in preference order.
37    pub codecs: Vec<Codec>,
38    /// Ladder selection options (rung count, bitrate and VMAF bounds).
39    pub ladder_opts: LadderOpts,
40}
41
42/// Returns the standard Mobile profile (capped at 720p, AV1/x264).
43pub fn mobile_profile() -> Profile {
44    Profile {
45        name: "Mobile".into(),
46        device: DeviceClass::Mobile,
47        description: "Smartphones and small tablets. Screen <7 inches.".into(),
48        vmaf_model: "vmaf_v0.6.1".into(),
49        max_res: RES_720P,
50        resolutions: vec![RES_360P, RES_480P, RES_720P],
51        codecs: vec![Codec::SvtAv1, Codec::X264],
52        ladder_opts: LadderOpts {
53            num_rungs: 4,
54            min_bitrate: 150.0,
55            max_bitrate: 3000.0,
56            min_vmaf: 50.0,
57            max_vmaf: 95.0,
58            audio_bitrate_kbps: 0.0,
59        },
60    }
61}
62
63/// Returns the standard Desktop profile (capped at 1080p, AV1/x265/x264).
64pub fn desktop_profile() -> Profile {
65    Profile {
66        name: "Desktop".into(),
67        device: DeviceClass::Desktop,
68        description: "Laptops and desktop monitors. 13-27 inch screens.".into(),
69        vmaf_model: "vmaf_v0.6.1".into(),
70        max_res: RES_1080P,
71        resolutions: vec![RES_480P, RES_720P, RES_1080P],
72        codecs: vec![Codec::SvtAv1, Codec::X265, Codec::X264],
73        ladder_opts: LadderOpts::default(),
74    }
75}
76
77/// Returns the standard 1080p TV profile (8 rungs, AV1/x265/x264).
78pub fn tv_profile() -> Profile {
79    Profile {
80        name: "TV (1080p)".into(),
81        device: DeviceClass::Tv,
82        description: "TVs and large displays. 40-65 inch screens.".into(),
83        vmaf_model: "vmaf_v0.6.1".into(),
84        max_res: RES_1080P,
85        resolutions: vec![RES_480P, RES_720P, RES_1080P],
86        codecs: vec![Codec::SvtAv1, Codec::X265, Codec::X264],
87        ladder_opts: LadderOpts {
88            num_rungs: 8,
89            min_bitrate: 200.0,
90            max_bitrate: 12000.0,
91            min_vmaf: 40.0,
92            max_vmaf: 0.0,
93            audio_bitrate_kbps: 0.0,
94        },
95    }
96}
97
98/// Returns the standard 4K TV profile (up to 2160p, 4K VMAF model, AV1/x265).
99pub fn tv_4k_profile() -> Profile {
100    Profile {
101        name: "TV (4K)".into(),
102        device: DeviceClass::Tv4k,
103        description: "4K TVs. 55-85 inch screens.".into(),
104        vmaf_model: "vmaf_4k_v0.6.1".into(),
105        max_res: RES_2160P,
106        resolutions: vec![RES_720P, RES_1080P, RES_1440P, RES_2160P],
107        codecs: vec![Codec::SvtAv1, Codec::X265],
108        ladder_opts: LadderOpts {
109            num_rungs: 8,
110            min_bitrate: 1000.0,
111            max_bitrate: 25000.0,
112            min_vmaf: 40.0,
113            max_vmaf: 97.0,
114            audio_bitrate_kbps: 0.0,
115        },
116    }
117}
118
119/// Returns all standard profiles: Mobile, Desktop, TV (1080p), and TV (4K).
120pub fn all_profiles() -> Vec<Profile> {
121    vec![mobile_profile(), desktop_profile(), tv_profile(), tv_4k_profile()]
122}