pixelflow-filters 0.1.0

Official in-repository filters for PixelFlow.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Trim filter validation and executor.

use pixelflow_core::{
    Clip, ClipMedia, ConcurrencyClass, DependencyPattern, DynamicDependencyBounds, ErrorCategory,
    ErrorCode, FilterChangeSet, FilterCompatibility, FilterPlan, FilterPlanRequest, Frame,
    FrameCount, FrameExecutor, FrameRequest, GraphBuilder, PixelFlowError, Result,
};

use crate::{
    OfficialFilterContract, SupportedFormats,
    contracts::{ALL_PLANAR_FAMILIES, ALL_SAMPLE_TYPES},
    options::{optional_i64, single_input_media, single_input_slice},
};

/// Official trim filter script name.
pub const FILTER_TRIM: &str = "trim";

/// Contract for `trim`.
pub const TRIM_CONTRACT: OfficialFilterContract = OfficialFilterContract::new(
    FILTER_TRIM,
    SupportedFormats::new(ALL_PLANAR_FAMILIES, ALL_SAMPLE_TYPES),
    FilterCompatibility::AllowChanges(FilterChangeSet {
        format: false,
        resolution: false,
        frame_count: true,
        frame_rate: false,
    }),
);

/// User-supplied trim range before validation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TrimOptions {
    start: Option<i64>,
    end: Option<i64>,
}

impl TrimOptions {
    /// Creates trim options from script or embedding integer arguments.
    #[must_use]
    pub const fn new(start: Option<i64>, end: Option<i64>) -> Self {
        Self { start, end }
    }
}

/// Trim range validated against finite input media.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ValidatedTrim {
    start: usize,
    end: usize,
}

impl ValidatedTrim {
    /// Returns inclusive source start frame.
    #[must_use]
    pub const fn start(self) -> usize {
        self.start
    }

    /// Returns exclusive source end frame.
    #[must_use]
    pub const fn end(self) -> usize {
        self.end
    }

    /// Returns output frame count.
    #[must_use]
    pub const fn len(self) -> usize {
        self.end - self.start
    }

    /// Returns true when trim selects no frames.
    #[must_use]
    pub const fn is_empty(self) -> bool {
        self.start == self.end
    }

    fn source_frame(self, output_frame: usize) -> Result<usize> {
        if output_frame >= self.len() {
            return Err(trim_error(
                "filter.trim_frame_out_of_range",
                format!("filter '{FILTER_TRIM}' output frame {output_frame} is out of range"),
            ));
        }

        self.start.checked_add(output_frame).ok_or_else(|| {
            trim_error(
                "filter.invalid_trim",
                format!("filter '{FILTER_TRIM}' source frame calculation overflowed"),
            )
        })
    }

    fn output_media(self, input: &ClipMedia) -> ClipMedia {
        ClipMedia::new(
            input.format().clone(),
            input.resolution().clone(),
            FrameCount::Finite(self.len()),
            input.frame_rate(),
        )
    }
}

/// Validates trim options and derives output media.
pub fn trim_output_media(
    input: &ClipMedia,
    options: TrimOptions,
) -> Result<(ValidatedTrim, ClipMedia)> {
    TRIM_CONTRACT.validate_input_media(input)?;

    let input_frames = match input.frame_count() {
        FrameCount::Finite(frames) => frames,
        FrameCount::Unknown => {
            return Err(PixelFlowError::new(
                ErrorCategory::Format,
                ErrorCode::new("filter.variable_frame_count"),
                format!("filter '{FILTER_TRIM}' requires finite input frame count"),
            ));
        }
    };
    let start = match options.start {
        Some(start) => non_negative_frame("start", start)?,
        None => 0,
    };
    let end = match options.end {
        Some(end) => non_negative_frame("end", end)?,
        None => input_frames,
    };

    if start > input_frames {
        return Err(trim_error(
            "filter.trim_out_of_range",
            format!("filter '{FILTER_TRIM}' start must not exceed input frame count"),
        ));
    }
    if end > input_frames {
        return Err(trim_error(
            "filter.trim_out_of_range",
            format!("filter '{FILTER_TRIM}' end must not exceed input frame count"),
        ));
    }
    if start > end {
        return Err(trim_error(
            "filter.invalid_trim",
            format!("filter '{FILTER_TRIM}' start must not exceed end"),
        ));
    }

    let trim = ValidatedTrim { start, end };
    Ok((trim, trim.output_media(input)))
}

/// Adds validated trim filter node and returns matching render executor.
pub fn add_trim_filter(
    builder: &mut GraphBuilder,
    input: Clip,
    input_media: &ClipMedia,
    options: TrimOptions,
) -> Result<(Clip, TrimExecutor)> {
    let (trim, output_media) = trim_output_media(input_media, options)?;
    let output = builder.filter_with_schedule(
        FILTER_TRIM,
        &[input],
        output_media,
        TRIM_CONTRACT.compatibility(),
        DependencyPattern::frame_map(DynamicDependencyBounds::any()),
        ConcurrencyClass::Stateless,
    )?;

    Ok((output, TrimExecutor::new(trim)))
}

fn parse_trim_options(options: &pixelflow_core::FilterOptions) -> Result<TrimOptions> {
    let start = optional_i64(options, FILTER_TRIM, "start", "filter.invalid_trim")?;
    let end = optional_i64(options, FILTER_TRIM, "end", "filter.invalid_trim")?;

    Ok(TrimOptions::new(start, end))
}

pub(crate) fn plan_trim(request: FilterPlanRequest<'_>) -> Result<FilterPlan> {
    let input = single_input_media(request, FILTER_TRIM)?;
    let output_media = trim_output_media(input, parse_trim_options(request.options())?)?.1;

    Ok(
        FilterPlan::new(output_media, TRIM_CONTRACT.compatibility()).with_schedule(
            DependencyPattern::frame_map(DynamicDependencyBounds::any()),
            ConcurrencyClass::Stateless,
        ),
    )
}

pub(crate) fn trim_executor_from_options(
    input_media: &[ClipMedia],
    options: &pixelflow_core::FilterOptions,
) -> Result<TrimExecutor> {
    let input = single_input_slice(input_media, FILTER_TRIM)?;
    let (trim, _media) = trim_output_media(input, parse_trim_options(options)?)?;

    Ok(TrimExecutor::new(trim))
}

/// Render executor for validated contiguous trim operations.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TrimExecutor {
    trim: ValidatedTrim,
}

impl TrimExecutor {
    /// Creates trim executor from validated trim range.
    #[must_use]
    pub const fn new(trim: ValidatedTrim) -> Self {
        Self { trim }
    }

    /// Returns validated trim range used by this executor.
    #[must_use]
    pub const fn trim(&self) -> ValidatedTrim {
        self.trim
    }
}

impl FrameExecutor for TrimExecutor {
    fn prepare(&self, request: FrameRequest<'_>) -> Result<Frame> {
        let source_frame = self.trim.source_frame(request.frame_number())?;
        request.input_frame(0, source_frame)
    }
}

fn non_negative_frame(name: &str, value: i64) -> Result<usize> {
    usize::try_from(value).map_err(|_| {
        trim_error(
            "filter.invalid_trim",
            format!("filter '{FILTER_TRIM}' option '{name}' must be non-negative"),
        )
    })
}

fn trim_error(code: &'static str, message: impl Into<String>) -> PixelFlowError {
    PixelFlowError::new(ErrorCategory::Format, ErrorCode::new(code), message)
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use pixelflow_core::{
        ClipMedia, ClipResolution, DependencyPattern, DynamicDependencyBounds, ErrorCategory,
        ErrorCode, Frame, FrameCount, FrameExecutor, FrameRate, FrameRequest, GraphBuilder,
        MetadataValue, NodeKind, Rational, RenderEngine, RenderExecutorMap, RenderOptions,
        WorkerPoolConfig,
    };

    use crate::testkit::{fixed_media, synthetic_u8_frame, with_frame_number_metadata};

    use super::{TrimOptions, trim_output_media};

    #[test]
    fn trim_options_validate_ranges_and_preserve_media_timing() {
        let input = fixed_media("yuv420p10", 1920, 1080);

        let cases = [
            (TrimOptions::new(None, None), 0, 24, 24),
            (TrimOptions::new(None, Some(5)), 0, 5, 5),
            (TrimOptions::new(Some(23), None), 23, 24, 1),
            (TrimOptions::new(Some(4), Some(4)), 4, 4, 0),
        ];

        for (options, expected_start, expected_end, expected_len) in cases {
            let (trim, output) = trim_output_media(&input, options)
                .expect("valid trim range should derive output media");

            assert_eq!(trim.start(), expected_start);
            assert_eq!(trim.end(), expected_end);
            assert_eq!(trim.len(), expected_len);
            assert_eq!(output.format(), input.format());
            assert_eq!(
                output.resolution(),
                &ClipResolution::Fixed {
                    width: 1920,
                    height: 1080,
                }
            );
            assert_eq!(output.frame_count(), FrameCount::Finite(expected_len));
            assert_eq!(
                output.frame_rate(),
                FrameRate::Cfr(Rational {
                    numerator: 24_000,
                    denominator: 1_001,
                })
            );
        }
    }

    #[test]
    fn trim_options_reject_invalid_empty_reversed_and_out_of_range_cases() {
        let input = fixed_media("gray8", 8, 6);

        for options in [
            TrimOptions::new(Some(-1), None),
            TrimOptions::new(None, Some(-1)),
            TrimOptions::new(Some(8), Some(4)),
        ] {
            let error = trim_output_media(&input, options).expect_err("invalid trim should fail");
            assert_eq!(error.category(), ErrorCategory::Format);
            assert_eq!(error.code(), ErrorCode::new("filter.invalid_trim"));
        }

        for options in [
            TrimOptions::new(Some(25), None),
            TrimOptions::new(None, Some(25)),
        ] {
            let error =
                trim_output_media(&input, options).expect_err("out-of-range trim should fail");
            assert_eq!(error.category(), ErrorCategory::Format);
            assert_eq!(error.code(), ErrorCode::new("filter.trim_out_of_range"));
        }

        let (empty, output) = trim_output_media(&input, TrimOptions::new(Some(4), Some(4)))
            .expect("empty trim is valid and produces zero output frames");
        assert_eq!(empty.len(), 0);
        assert_eq!(output.frame_count(), FrameCount::Finite(0));
    }

    #[test]
    fn trim_rejects_unknown_input_frame_count_during_planning_with_format_diagnostic() {
        let input = fixed_media("gray8", 8, 6);
        let unknown_count = ClipMedia::new(
            input.format().clone(),
            input.resolution().clone(),
            FrameCount::Unknown,
            input.frame_rate(),
        );

        let error = trim_output_media(&unknown_count, TrimOptions::new(None, None))
            .expect_err("unknown frame count should fail before trim planning");

        assert_eq!(error.category(), ErrorCategory::Format);
        assert_eq!(error.code(), ErrorCode::new("filter.variable_frame_count"));
    }

    #[test]
    fn add_trim_filter_creates_filter_node_with_frame_map_schedule() {
        let input_media = fixed_media("gray8", 8, 6);
        let mut builder = GraphBuilder::new();
        let source = builder.source("source", input_media.clone());

        let (trimmed, executor) = super::add_trim_filter(
            &mut builder,
            source,
            &input_media,
            TrimOptions::new(Some(2), Some(6)),
        )
        .expect("trim node should build");
        let graph = builder.build();
        let node = graph
            .node(trimmed.node_id())
            .expect("trim node should exist");

        assert_eq!(executor.trim().start(), 2);
        assert_eq!(executor.trim().end(), 6);
        assert_eq!(node.media().frame_count(), FrameCount::Finite(4));
        assert_eq!(node.media().frame_rate(), input_media.frame_rate());
        assert!(matches!(
            node.kind(),
            NodeKind::Filter { dependencies, .. }
                if dependencies == &DependencyPattern::frame_map(DynamicDependencyBounds::any())
        ));
    }

    #[test]
    fn trim_executor_maps_output_frames_to_contiguous_source_frames() {
        struct NumberedSource;

        impl FrameExecutor for NumberedSource {
            fn prepare(&self, request: FrameRequest<'_>) -> pixelflow_core::Result<Frame> {
                let frame = synthetic_u8_frame("gray8", 2, 2, |_plane, x, y| {
                    u8::try_from(request.frame_number() + x + y).expect("fixture sample fits u8")
                })?;
                with_frame_number_metadata(&frame, request.frame_number())
            }
        }

        let input_media = fixed_media("gray8", 2, 2);
        let mut builder = GraphBuilder::new();
        let source = builder.source("source", input_media.clone());
        let (trimmed, executor) = super::add_trim_filter(
            &mut builder,
            source,
            &input_media,
            TrimOptions::new(Some(5), Some(8)),
        )
        .expect("trim node should build");
        builder.set_output(trimmed);
        let graph = builder.build();

        let mut executors = RenderExecutorMap::new();
        executors.insert(source.node_id(), Arc::new(NumberedSource));
        executors.insert(trimmed.node_id(), Arc::new(executor));

        let frames: Vec<_> = RenderEngine::new(WorkerPoolConfig::new(1))
            .render_ordered(graph, executors, RenderOptions::default())
            .expect("render should start")
            .map(|frame| frame.expect("trimmed frame should render"))
            .collect();

        let selected_numbers: Vec<_> = frames
            .iter()
            .map(|frame| match frame.metadata().get("core:frame_number") {
                Some(MetadataValue::Int(value)) => *value,
                other => panic!("expected frame_number metadata, got {other:?}"),
            })
            .collect();
        assert_eq!(selected_numbers, [5, 6, 7]);
    }
}