oximedia-cli 0.1.8

Command-line interface for OxiMedia
Documentation
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! Video frame extraction utilities for the OxiMedia CLI.
//!
//! Provides helpers for extracting raw RGB24 frames from video files.
//! Y4M (YUV4MPEG2) is supported natively via [`oximedia_container`].
//! Other container formats (MP4, MKV, WebM, MPEG-TS) are supported via
//! the async demuxer + codec pipeline.

use anyhow::{bail, Context, Result};
use oximedia_codec::{Av1Decoder, DecoderConfig, VideoDecoder, VideoFrame, Vp8Decoder, Vp9Decoder};
use oximedia_container::demux::y4m::{Y4mChroma, Y4mDemuxer};
use oximedia_container::demux::{MatroskaDemuxer, Mp4Demuxer, MpegTsDemuxer};
use oximedia_container::Demuxer;
use oximedia_core::{CodecId, PixelFormat};
use oximedia_io::FileSource;
use std::io::Cursor;
use std::path::Path;

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Extract a single video frame from `input` as packed RGB24 bytes.
///
/// Returns `(rgb_data, width, height)`.
///
/// # Format support
///
/// - **Y4M (YUV4MPEG2)**: supported for all standard chroma modes (fast path).
/// - **MP4 / MOV / M4V**: supported via Mp4Demuxer + AV1/VP9 decoder.
/// - **MKV / WebM**: supported via MatroskaDemuxer + AV1/VP9/VP8 decoder.
/// - **MPEG-TS / M2TS / MTS**: supported via MpegTsDemuxer + AV1/VP9/VP8 decoder.
/// - **All other formats**: returns a descriptive error.
///
/// # Errors
///
/// Returns an error if the file does not exist, is not a recognised format,
/// does not contain enough frames, or is corrupted.
pub async fn extract_video_frame_rgb(input: &Path, frame_num: u64) -> Result<(Vec<u8>, u32, u32)> {
    if !input.exists() {
        bail!("Input file not found: {}", input.display());
    }

    // Peek at the first 9 bytes to detect Y4M magic.
    let magic = read_magic(input)?;
    if magic.starts_with(b"YUV4MPEG2") {
        return extract_y4m_frame_rgb(input, frame_num);
    }

    // Non-Y4M: use the async container/codec pipeline.
    decode_nth_frame_rgb(input, frame_num).await
}

/// Extract multiple frames from `input` in a single pass.
///
/// `frame_indices` must be **sorted ascending**. Frames not present in the
/// file are silently omitted from the result.
///
/// Returns a `Vec` of `(rgb_data, width, height)` tuples, one per requested
/// frame index that was successfully read.
///
/// # Errors
///
/// See [`extract_video_frame_rgb`].
pub async fn extract_video_frames_rgb(
    input: &Path,
    frame_indices: &[u64],
) -> Result<Vec<(Vec<u8>, u32, u32)>> {
    if frame_indices.is_empty() {
        return Ok(Vec::new());
    }

    if !input.exists() {
        bail!("Input file not found: {}", input.display());
    }

    let magic = read_magic(input)?;
    if magic.starts_with(b"YUV4MPEG2") {
        return extract_y4m_frames_rgb(input, frame_indices);
    }

    // Non-Y4M: use the async container/codec pipeline.
    decode_frames_rgb(input, frame_indices).await
}

// ---------------------------------------------------------------------------
// Internal: Y4M single-frame extraction
// ---------------------------------------------------------------------------

fn extract_y4m_frame_rgb(input: &Path, frame_num: u64) -> Result<(Vec<u8>, u32, u32)> {
    let data = std::fs::read(input).context("Failed to read Y4M file")?;
    let mut demuxer = Y4mDemuxer::new(Cursor::new(data)).context("Failed to parse Y4M header")?;

    let width = demuxer.width();
    let height = demuxer.height();
    let chroma = demuxer.chroma();

    // Walk forward to the requested frame.
    let mut current: u64 = 0;
    loop {
        let raw = demuxer
            .read_frame()
            .with_context(|| format!("I/O error reading frame {current}"))?;

        match raw {
            None => {
                bail!(
                    "Y4M file has fewer than {} frames (only {} found)",
                    frame_num + 1,
                    current
                );
            }
            Some(yuv) => {
                if current == frame_num {
                    let rgb = yuv_to_rgb24(&yuv, width, height, chroma);
                    return Ok((rgb, width, height));
                }
                current += 1;
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Internal: Y4M multi-frame extraction (single pass)
// ---------------------------------------------------------------------------

fn extract_y4m_frames_rgb(input: &Path, frame_indices: &[u64]) -> Result<Vec<(Vec<u8>, u32, u32)>> {
    let data = std::fs::read(input).context("Failed to read Y4M file")?;
    let mut demuxer = Y4mDemuxer::new(Cursor::new(data)).context("Failed to parse Y4M header")?;

    let width = demuxer.width();
    let height = demuxer.height();
    let chroma = demuxer.chroma();

    // We'll collect matching frames in order.
    let mut results: Vec<(Vec<u8>, u32, u32)> = Vec::with_capacity(frame_indices.len());
    let mut idx_iter = frame_indices.iter().peekable();
    let mut current: u64 = 0;

    loop {
        // If all requested indices have been satisfied, stop early.
        let Some(&next_wanted) = idx_iter.peek() else {
            break;
        };

        let raw = demuxer
            .read_frame()
            .with_context(|| format!("I/O error reading frame {current}"))?;

        match raw {
            None => break, // EOF — remaining indices are past the end of file.
            Some(yuv) => {
                if current == *next_wanted {
                    let rgb = yuv_to_rgb24(&yuv, width, height, chroma);
                    results.push((rgb, width, height));
                    idx_iter.next(); // consume this index
                }
                current += 1;
            }
        }
    }

    Ok(results)
}

// ---------------------------------------------------------------------------
// Internal: container dispatch — detect extension and open demuxer
// ---------------------------------------------------------------------------

async fn decode_nth_frame_rgb(input: &Path, frame_num: u64) -> Result<(Vec<u8>, u32, u32)> {
    let ext = input
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_lowercase();

    match ext.as_str() {
        "mp4" | "m4v" | "mov" => {
            let source = FileSource::open(input)
                .await
                .with_context(|| format!("Cannot open {}", input.display()))?;
            let demuxer = Mp4Demuxer::new(source);
            let frames = decode_via_demuxer(demuxer, &[frame_num]).await?;
            frames.into_iter().next().ok_or_else(|| {
                anyhow::anyhow!("Frame {} not found in {}", frame_num, input.display())
            })
        }
        "mkv" | "webm" => {
            let source = FileSource::open(input)
                .await
                .with_context(|| format!("Cannot open {}", input.display()))?;
            let demuxer = MatroskaDemuxer::new(source);
            let frames = decode_via_demuxer(demuxer, &[frame_num]).await?;
            frames.into_iter().next().ok_or_else(|| {
                anyhow::anyhow!("Frame {} not found in {}", frame_num, input.display())
            })
        }
        "ts" | "mts" | "m2ts" => {
            let source = FileSource::open(input)
                .await
                .with_context(|| format!("Cannot open {}", input.display()))?;
            let demuxer = MpegTsDemuxer::new(source);
            let frames = decode_via_demuxer(demuxer, &[frame_num]).await?;
            frames.into_iter().next().ok_or_else(|| {
                anyhow::anyhow!("Frame {} not found in {}", frame_num, input.display())
            })
        }
        _ => bail!(
            "Unsupported container extension '{}'; convert to Y4M or use MP4/MKV/WebM/TS. \
             Example: oximedia convert --input {} --output {}.y4m",
            ext,
            input.display(),
            input.file_stem().and_then(|s| s.to_str()).unwrap_or("out")
        ),
    }
}

async fn decode_frames_rgb(
    input: &Path,
    frame_indices: &[u64],
) -> Result<Vec<(Vec<u8>, u32, u32)>> {
    if frame_indices.is_empty() {
        return Ok(Vec::new());
    }

    let ext = input
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_lowercase();

    match ext.as_str() {
        "mp4" | "m4v" | "mov" => {
            let source = FileSource::open(input)
                .await
                .with_context(|| format!("Cannot open {}", input.display()))?;
            let demuxer = Mp4Demuxer::new(source);
            decode_via_demuxer(demuxer, frame_indices).await
        }
        "mkv" | "webm" => {
            let source = FileSource::open(input)
                .await
                .with_context(|| format!("Cannot open {}", input.display()))?;
            let demuxer = MatroskaDemuxer::new(source);
            decode_via_demuxer(demuxer, frame_indices).await
        }
        "ts" | "mts" | "m2ts" => {
            let source = FileSource::open(input)
                .await
                .with_context(|| format!("Cannot open {}", input.display()))?;
            let demuxer = MpegTsDemuxer::new(source);
            decode_via_demuxer(demuxer, frame_indices).await
        }
        _ => bail!(
            "Unsupported container extension '{}'; convert to Y4M or use MP4/MKV/WebM/TS. \
             Example: oximedia convert --input {} --output {}.y4m",
            ext,
            input.display(),
            input.file_stem().and_then(|s| s.to_str()).unwrap_or("out")
        ),
    }
}

// ---------------------------------------------------------------------------
// Internal: generic async demux+decode loop
// ---------------------------------------------------------------------------

/// Drive a concrete demuxer to extract the requested `frame_indices` as RGB24.
///
/// Frame indices must be sorted ascending. Frames past the end of file are
/// silently omitted from the result (same contract as the Y4M multi-frame path).
async fn decode_via_demuxer<D: Demuxer>(
    mut demuxer: D,
    frame_indices: &[u64],
) -> Result<Vec<(Vec<u8>, u32, u32)>> {
    demuxer.probe().await.context("Container probe failed")?;

    // Find first video stream.
    let (video_stream_idx, codec_id) = {
        let streams = demuxer.streams();
        streams
            .iter()
            .find(|s| s.is_video())
            .map(|s| (s.index, s.codec))
            .ok_or_else(|| anyhow::anyhow!("No video stream found in container"))?
    };

    // Create the appropriate decoder for the codec.
    let mut decoder: Box<dyn VideoDecoder + Send> = match codec_id {
        CodecId::Av1 => {
            let cfg = DecoderConfig {
                codec: CodecId::Av1,
                ..DecoderConfig::default()
            };
            Box::new(Av1Decoder::new(cfg).context("Failed to create AV1 decoder")?)
        }
        CodecId::Vp9 => {
            let cfg = DecoderConfig {
                codec: CodecId::Vp9,
                ..DecoderConfig::default()
            };
            Box::new(Vp9Decoder::new(cfg).context("Failed to create VP9 decoder")?)
        }
        CodecId::Vp8 => {
            let cfg = DecoderConfig {
                codec: CodecId::Vp8,
                ..DecoderConfig::default()
            };
            Box::new(Vp8Decoder::new(cfg).context("Failed to create VP8 decoder")?)
        }
        other => bail!(
            "Video codec {:?} is not yet wired for scopes/thumbnail extraction. \
             Convert to AV1/VP9/VP8 or use Y4M. \
             Example: oximedia convert --input <file> --output out.y4m",
            other
        ),
    };

    let mut results: Vec<(Vec<u8>, u32, u32)> = Vec::with_capacity(frame_indices.len());
    let mut idx_iter = frame_indices.iter().peekable();
    let mut frame_counter: u64 = 0;

    // Packet-read loop.
    loop {
        // If all wanted frames are collected, stop early.
        if idx_iter.peek().is_none() {
            break;
        }

        let packet = match demuxer.read_packet().await {
            Ok(p) => p,
            Err(e) if e.is_eof() => break,
            Err(e) => return Err(anyhow::anyhow!("Demuxer read error: {e}")),
        };

        // Skip packets from other streams.
        if packet.stream_index != video_stream_idx {
            continue;
        }

        let pts = packet.timestamp.pts;
        decoder
            .send_packet(&packet.data, pts)
            .context("Decoder send_packet failed")?;

        // Drain all frames the decoder is ready to yield.
        loop {
            match decoder.receive_frame() {
                Ok(Some(frame)) => {
                    if let Some(&&next_wanted) = idx_iter.peek() {
                        if frame_counter == next_wanted {
                            let rgb = video_frame_to_rgb24(&frame)?;
                            results.push((rgb, frame.width, frame.height));
                            idx_iter.next();
                        }
                    }
                    frame_counter += 1;
                }
                Ok(None) => break,
                Err(e) => {
                    return Err(anyhow::anyhow!(
                        "Decode error on frame {frame_counter}: {e}"
                    ))
                }
            }
        }
    }

    // Flush decoder for any trailing frames.
    decoder.flush().context("Decoder flush failed")?;
    loop {
        if idx_iter.peek().is_none() {
            break;
        }
        match decoder.receive_frame() {
            Ok(Some(frame)) => {
                if let Some(&&next_wanted) = idx_iter.peek() {
                    if frame_counter == next_wanted {
                        let rgb = video_frame_to_rgb24(&frame)?;
                        results.push((rgb, frame.width, frame.height));
                        idx_iter.next();
                    }
                }
                frame_counter += 1;
            }
            Ok(None) => break,
            Err(e) => {
                return Err(anyhow::anyhow!(
                    "Decode flush error on frame {frame_counter}: {e}"
                ))
            }
        }
    }

    Ok(results)
}

// ---------------------------------------------------------------------------
// Internal: VideoFrame → packed RGB24
// ---------------------------------------------------------------------------

/// Convert a decoded `VideoFrame` to packed RGB24.
///
/// Handles stride-aware plane repacking before conversion. Only 8-bit
/// YUV420p, YUV422p, and YUV444p are supported; other formats return
/// a descriptive error directing the user to re-encode to 8-bit first.
fn video_frame_to_rgb24(frame: &VideoFrame) -> Result<Vec<u8>> {
    let w = frame.width;
    let h = frame.height;

    match frame.format {
        PixelFormat::Yuv420p => {
            if frame.planes.len() < 3 {
                bail!("YUV420p frame has fewer than 3 planes");
            }
            let y = repack_plane(&frame.planes[0]);
            let u = repack_plane(&frame.planes[1]);
            let v = repack_plane(&frame.planes[2]);
            let mut packed = Vec::with_capacity(y.len() + u.len() + v.len());
            packed.extend_from_slice(&y);
            packed.extend_from_slice(&u);
            packed.extend_from_slice(&v);
            Ok(yuv420_to_rgb24(&packed, w, h))
        }
        PixelFormat::Yuv422p => {
            if frame.planes.len() < 3 {
                bail!("YUV422p frame has fewer than 3 planes");
            }
            let y = repack_plane(&frame.planes[0]);
            let u = repack_plane(&frame.planes[1]);
            let v = repack_plane(&frame.planes[2]);
            let mut packed = Vec::with_capacity(y.len() + u.len() + v.len());
            packed.extend_from_slice(&y);
            packed.extend_from_slice(&u);
            packed.extend_from_slice(&v);
            Ok(yuv422_to_rgb24(&packed, w, h))
        }
        PixelFormat::Yuv444p => {
            if frame.planes.len() < 3 {
                bail!("YUV444p frame has fewer than 3 planes");
            }
            let y = repack_plane(&frame.planes[0]);
            let u = repack_plane(&frame.planes[1]);
            let v = repack_plane(&frame.planes[2]);
            let mut packed = Vec::with_capacity(y.len() + u.len() + v.len());
            packed.extend_from_slice(&y);
            packed.extend_from_slice(&u);
            packed.extend_from_slice(&v);
            Ok(yuv444_to_rgb24(&packed, w, h))
        }
        other => bail!(
            "Pixel format {:?} is not supported for RGB extraction. \
             Only 8-bit YUV (420p/422p/444p) is supported. \
             Re-encode to 8-bit AV1/VP9 or use Y4M.",
            other
        ),
    }
}

/// Repack a plane that may have row-stride padding into a tightly-packed buffer.
///
/// If `stride == width`, this is a simple slice copy; otherwise only `width`
/// bytes per row are copied, discarding any row-end padding.
fn repack_plane(plane: &oximedia_codec::Plane) -> Vec<u8> {
    let width = plane.width as usize;
    let height = plane.height as usize;
    let stride = plane.stride;

    if stride == width {
        plane.data[..width * height].to_vec()
    } else {
        let mut out = Vec::with_capacity(width * height);
        for row in 0..height {
            let start = row * stride;
            let end = start + width;
            if end <= plane.data.len() {
                out.extend_from_slice(&plane.data[start..end]);
            } else {
                // Partial / corrupt row — copy what we have and pad with zeros.
                let available = plane.data.len().saturating_sub(start);
                out.extend_from_slice(&plane.data[start..start + available]);
                out.extend(std::iter::repeat(0).take(width - available));
            }
        }
        out
    }
}

// ---------------------------------------------------------------------------
// Internal: magic detection
// ---------------------------------------------------------------------------

fn read_magic(input: &Path) -> Result<Vec<u8>> {
    use std::io::Read;
    let mut f = std::fs::File::open(input).context("Failed to open input file")?;
    let mut buf = [0u8; 9];
    let n = f.read(&mut buf).context("Failed to read file header")?;
    Ok(buf[..n].to_vec())
}

// ---------------------------------------------------------------------------
// YUV → RGB conversion
// ---------------------------------------------------------------------------

/// Convert raw planar YUV data to packed RGB24, dispatching on chroma format.
///
/// BT.601 studio-swing coefficients are used (Y: 16-235, UV: 16-240).
/// For mono frames a grey ramp is produced.
pub fn yuv_to_rgb24(yuv: &[u8], width: u32, height: u32, chroma: Y4mChroma) -> Vec<u8> {
    match chroma {
        Y4mChroma::Mono => yuv_mono_to_rgb24(yuv, width, height),
        Y4mChroma::C420jpeg | Y4mChroma::C420mpeg2 | Y4mChroma::C420paldv => {
            yuv420_to_rgb24(yuv, width, height)
        }
        Y4mChroma::C422 => yuv422_to_rgb24(yuv, width, height),
        Y4mChroma::C444 => yuv444_to_rgb24(yuv, width, height),
        Y4mChroma::C444alpha => {
            // Strip the alpha plane (last w*h bytes) and treat remainder as 444.
            let pixel_count = (width as usize) * (height as usize);
            yuv444_to_rgb24(&yuv[..pixel_count * 3], width, height)
        }
    }
}

/// BT.601 full-range YUV → RGB clamped to [0, 255].
///
/// `y`, `u`, `v` are in the 8-bit range [0, 255] with U/V centred at 128.
#[inline(always)]
fn yuv_pixel_to_rgb(y: u8, u: u8, v: u8) -> (u8, u8, u8) {
    let yf = y as f32;
    let uf = u as f32 - 128.0;
    let vf = v as f32 - 128.0;

    let r = (yf + 1.402 * vf).clamp(0.0, 255.0) as u8;
    let g = (yf - 0.344_136 * uf - 0.714_136 * vf).clamp(0.0, 255.0) as u8;
    let b = (yf + 1.772 * uf).clamp(0.0, 255.0) as u8;
    (r, g, b)
}

fn yuv420_to_rgb24(yuv: &[u8], width: u32, height: u32) -> Vec<u8> {
    let w = width as usize;
    let h = height as usize;
    // Y4mChroma::bytes_per_frame uses (w+1)/2 for odd dimensions — match here.
    let chroma_w = (w + 1) / 2;
    let chroma_h = (h + 1) / 2;

    let y_size = w * h;
    let u_size = chroma_w * chroma_h;

    let y_plane = &yuv[..y_size];
    let u_plane = &yuv[y_size..y_size + u_size];
    let v_plane = &yuv[y_size + u_size..];

    let mut rgb = vec![0u8; w * h * 3];
    for row in 0..h {
        for col in 0..w {
            let y = y_plane[row * w + col];
            let u = u_plane[(row / 2) * chroma_w + (col / 2)];
            let v = v_plane[(row / 2) * chroma_w + (col / 2)];
            let (r, g, b) = yuv_pixel_to_rgb(y, u, v);
            let idx = (row * w + col) * 3;
            rgb[idx] = r;
            rgb[idx + 1] = g;
            rgb[idx + 2] = b;
        }
    }
    rgb
}

fn yuv422_to_rgb24(yuv: &[u8], width: u32, height: u32) -> Vec<u8> {
    let w = width as usize;
    let h = height as usize;
    let chroma_w = (w + 1) / 2;

    let y_size = w * h;
    let u_size = chroma_w * h;

    let y_plane = &yuv[..y_size];
    let u_plane = &yuv[y_size..y_size + u_size];
    let v_plane = &yuv[y_size + u_size..];

    let mut rgb = vec![0u8; w * h * 3];
    for row in 0..h {
        for col in 0..w {
            let y = y_plane[row * w + col];
            let u = u_plane[row * chroma_w + (col / 2)];
            let v = v_plane[row * chroma_w + (col / 2)];
            let (r, g, b) = yuv_pixel_to_rgb(y, u, v);
            let idx = (row * w + col) * 3;
            rgb[idx] = r;
            rgb[idx + 1] = g;
            rgb[idx + 2] = b;
        }
    }
    rgb
}

fn yuv444_to_rgb24(yuv: &[u8], width: u32, height: u32) -> Vec<u8> {
    let w = width as usize;
    let h = height as usize;
    let pixel_count = w * h;

    let y_plane = &yuv[..pixel_count];
    let u_plane = &yuv[pixel_count..pixel_count * 2];
    let v_plane = &yuv[pixel_count * 2..];

    let mut rgb = vec![0u8; pixel_count * 3];
    for i in 0..pixel_count {
        let (r, g, b) = yuv_pixel_to_rgb(y_plane[i], u_plane[i], v_plane[i]);
        rgb[i * 3] = r;
        rgb[i * 3 + 1] = g;
        rgb[i * 3 + 2] = b;
    }
    rgb
}

fn yuv_mono_to_rgb24(yuv: &[u8], width: u32, height: u32) -> Vec<u8> {
    let pixel_count = (width as usize) * (height as usize);
    let mut rgb = vec![0u8; pixel_count * 3];
    for (i, &luma) in yuv[..pixel_count].iter().enumerate() {
        rgb[i * 3] = luma;
        rgb[i * 3 + 1] = luma;
        rgb[i * 3 + 2] = luma;
    }
    rgb
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn make_y4m(width: u32, height: u32, frame_count: usize) -> Vec<u8> {
        let mut data: Vec<u8> = Vec::new();
        let header = format!("YUV4MPEG2 W{width} H{height} F25:1 Ip C420jpeg\n");
        data.extend_from_slice(header.as_bytes());
        let frame_size = Y4mChroma::C420jpeg.bytes_per_frame(width, height);
        for i in 0..frame_count {
            data.extend_from_slice(b"FRAME\n");
            let fill = (i & 0xFF) as u8;
            data.extend(std::iter::repeat(fill).take(frame_size));
        }
        data
    }

    #[test]
    fn test_yuv_pixel_to_rgb_grey() {
        // Y=128, U=128, V=128 should give near-grey (128, 128, 128)
        let (r, g, b) = yuv_pixel_to_rgb(128, 128, 128);
        assert!((r as i32 - 128).abs() <= 2);
        assert!((g as i32 - 128).abs() <= 2);
        assert!((b as i32 - 128).abs() <= 2);
    }

    #[tokio::test]
    async fn test_extract_nonexistent_file() {
        let p = std::env::temp_dir().join("oximedia_no_such_file_9999.y4m");
        let result = extract_video_frame_rgb(&p, 0).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_extract_frame_from_y4m_file() {
        let tmp = std::env::temp_dir().join("oximedia_fe_test_single.y4m");
        let data = make_y4m(4, 4, 3);
        std::fs::write(&tmp, &data).expect("write test Y4M");

        let result = extract_video_frame_rgb(&tmp, 0).await;
        assert!(result.is_ok(), "{:?}", result);
        let (rgb, w, h) = result.expect("frame extraction");
        assert_eq!(w, 4);
        assert_eq!(h, 4);
        assert_eq!(rgb.len(), 4 * 4 * 3);

        std::fs::remove_file(&tmp).ok();
    }

    #[tokio::test]
    async fn test_extract_frame_out_of_range() {
        let tmp = std::env::temp_dir().join("oximedia_fe_test_oor.y4m");
        let data = make_y4m(4, 4, 2);
        std::fs::write(&tmp, &data).expect("write test Y4M");

        let result = extract_video_frame_rgb(&tmp, 5).await;
        assert!(result.is_err());

        std::fs::remove_file(&tmp).ok();
    }

    #[tokio::test]
    async fn test_extract_multiple_frames_single_pass() {
        let tmp = std::env::temp_dir().join("oximedia_fe_test_multi.y4m");
        let data = make_y4m(4, 4, 10);
        std::fs::write(&tmp, &data).expect("write test Y4M");

        let indices = vec![0u64, 2, 5, 9];
        let result = extract_video_frames_rgb(&tmp, &indices).await;
        assert!(result.is_ok(), "{:?}", result);
        let frames = result.expect("frame extraction");
        assert_eq!(frames.len(), 4);
        for (rgb, w, h) in &frames {
            assert_eq!(*w, 4);
            assert_eq!(*h, 4);
            assert_eq!(rgb.len(), 4 * 4 * 3);
        }

        std::fs::remove_file(&tmp).ok();
    }

    #[test]
    fn test_yuv420_odd_dimensions() {
        // 7x7 — chroma planes use (7+1)/2=4 per axis, total frame = 49 + 16 + 16 = 81
        let frame_size = Y4mChroma::C420jpeg.bytes_per_frame(7, 7);
        assert_eq!(frame_size, 81);
        let yuv = vec![128u8; frame_size];
        let rgb = yuv420_to_rgb24(&yuv, 7, 7);
        assert_eq!(rgb.len(), 7 * 7 * 3);
    }

    #[tokio::test]
    async fn test_unsupported_extension_returns_clean_error() {
        let tmp = std::env::temp_dir().join("oximedia_fe_test_unsupported.avi");
        // Write a fake AVI magic
        std::fs::write(&tmp, b"RIFF\x00\x00\x00\x00AVI fake").expect("write test file");

        let result = extract_video_frame_rgb(&tmp, 0).await;
        assert!(result.is_err());
        let msg = result
            .expect_err("expected error")
            .to_string()
            .to_lowercase();
        assert!(
            msg.contains("unsupported") || msg.contains("container") || msg.contains("extension"),
            "unexpected error message: {msg}"
        );

        std::fs::remove_file(&tmp).ok();
    }

    #[tokio::test]
    async fn test_unsupported_extension_multi_returns_clean_error() {
        let tmp = std::env::temp_dir().join("oximedia_fe_test_unsup_multi.avi");
        std::fs::write(&tmp, b"RIFF\x00\x00\x00\x00AVI fake").expect("write test file");

        let result = extract_video_frames_rgb(&tmp, &[0, 1]).await;
        assert!(result.is_err());
        let msg = result
            .expect_err("expected error")
            .to_string()
            .to_lowercase();
        assert!(
            msg.contains("unsupported") || msg.contains("container") || msg.contains("extension"),
            "unexpected error message: {msg}"
        );

        std::fs::remove_file(&tmp).ok();
    }

    #[tokio::test]
    async fn y4m_fast_path_still_works() {
        // Verify that a non-existent Y4M file returns Err cleanly (not a panic).
        let result = extract_video_frame_rgb(Path::new("/nonexistent.y4m"), 0).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn y4m_returns_err_not_panic_on_missing_file() {
        let result = extract_video_frames_rgb(Path::new("/nonexistent.y4m"), &[0, 1, 2]).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn unsupported_container_returns_clean_err() {
        // .avi is not supported — either file-not-found or unsupported-extension, both are Err.
        let path_buf = std::env::temp_dir().join("test_oximedia_unsup_check.avi");
        let result = extract_video_frame_rgb(&path_buf, 0).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_repack_plane_no_stride_padding() {
        use oximedia_codec::Plane;
        let plane = Plane {
            data: vec![1, 2, 3, 4, 5, 6],
            stride: 3,
            width: 3,
            height: 2,
        };
        let repacked = repack_plane(&plane);
        assert_eq!(repacked, vec![1, 2, 3, 4, 5, 6]);
    }

    #[test]
    fn test_repack_plane_with_stride_padding() {
        use oximedia_codec::Plane;
        // 3-wide, 2-tall, stride=4 (1 padding byte per row)
        let plane = Plane {
            data: vec![1, 2, 3, 0, 4, 5, 6, 0],
            stride: 4,
            width: 3,
            height: 2,
        };
        let repacked = repack_plane(&plane);
        assert_eq!(repacked, vec![1, 2, 3, 4, 5, 6]);
    }
}