forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBb02f499Tahinli1 month ago1000 commits

raw.rs110 lines, 5.3 KiBHistoryRawDownload
1//! The raw video software seat: uncompressed pictures in an AVI (`BI_RGB` 8 bit palette, RGB555,
2//! 24 and 32 bit, YUY2, UYVY, YVYU, I420, YV12, NV12, Y800, v210) or a QuickTime mov (`raw `,
3//! `2vuy`, `yuvs`, `yuv2`, `v210`, `v410`, `b16g`), unpacked and flipped upright by `ec-riff`'s [`RawVideoDecoder`] and handed
4//! to the shared intra-only stream and span loop ([`super::intra`]). Every picture is a sync
5//! sample, so a seek is one sample read.
6//!
7//! YUV layouts keep their own chroma (4:2:2 averaged down to 4:2:0 as every 4:2:2 source is) and
8//! are untagged like the rest of an AVI's pictures. RGB is turned into 4:4:4 YCbCr with the
9//! BT.709 matrix over the full range at 8 bit, and tagged as exactly that, so the one conversion
10//! back to RGB every source takes undoes it (to within a code or two, the chroma being averaged
11//! down to 4:2:0 on the way, as for any 4:4:4 source). The alpha of a 32-bit DIB is dropped: the
12//! engine composites a clip as opaque.
13
14use ec_core::frame::{PixelFormat, Plane, VideoFrame};
15use ec_core::registry::{CodecId, CodecParameters};
16use ec_riff::RawVideoDecoder;
17
18use super::intra::{IntraFacts, Seat};
19
20pub(crate) struct RawSeat(pub(super) RawVideoDecoder);
21
22impl Seat for RawSeat {
23 const NAME: &'static str = "raw video";
24 fn open(_: IntraFacts, header: &[u8]) -> Result<Self, String> {
25 let mut params = CodecParameters::new(CodecId::RawVideo);
26 params.extradata = Some(header.to_vec().into());
27 RawVideoDecoder::new(params).map(Self).map_err(|e| e.to_string())
28 }
29 fn decode(&mut self, sample: &[u8]) -> Result<VideoFrame, String> {
30 self.0.decode(sample).map(yuv).map_err(|e| e.to_string())
31 }
32}
33
34pub(crate) type Stream = super::intra::Stream<RawSeat>;
35
36/// An upright picture as the planar YCbCr the engine takes: RGB (any of `Bgr8`, `Rgb8`, `Bgra8`)
37/// becomes full-range BT.709 4:4:4, NV12 is split into I420, 16-bit gray is narrowed to 10-bit
38/// (`I010`, neutral chroma), and every other layout is left as it is (`Gray8` is given neutral
39/// chroma by the shared fold, 4:2:2 / 4:4:4 at 10 bit are carried as they are).
40pub(crate) fn yuv(pic: VideoFrame) -> VideoFrame {
41 let (bpp, swap) = match pic.format {
42 PixelFormat::Bgr8 => (3, false),
43 PixelFormat::Rgb8 => (3, true),
44 PixelFormat::Bgra8 => (4, false),
45 PixelFormat::Nv12 => return nv12(&pic),
46 PixelFormat::Gray16 => return gray16(&pic),
47 _ => return pic,
48 };
49 let (w, h) = (pic.width as usize, pic.height as usize);
50 let src = &pic.planes[0];
51 let (mut y, mut u, mut v) = (vec![0u8; w * h], vec![0u8; w * h], vec![0u8; w * h]);
52 for row in 0..h {
53 let line = &src.data[row * src.stride..row * src.stride + w * bpp];
54 for (col, px) in line.chunks_exact(bpp).enumerate() {
55 let (b, g, r) = if swap {
56 (i32::from(px[2]), i32::from(px[1]), i32::from(px[0]))
57 } else {
58 (i32::from(px[0]), i32::from(px[1]), i32::from(px[2]))
59 };
60 let at = row * w + col;
61 let luma = (13933 * r + 46871 * g + 4732 * b + 32768) >> 16;
62 let cb = ((-7509 * r - 25259 * g + 32768 * b + 32768) >> 16) + 128;
63 let cr = ((32768 * r - 29764 * g - 3004 * b + 32768) >> 16) + 128;
64 y[at] = luma.clamp(0, 255) as u8;
65 u[at] = cb.clamp(0, 255) as u8;
66 v[at] = cr.clamp(0, 255) as u8;
67 }
68 }
69 let planes = vec![Plane::new(y, w), Plane::new(u, w), Plane::new(v, w)];
70 let mut out = VideoFrame::try_new(PixelFormat::I444, pic.width, pic.height, planes).expect("three planes of the picture's size");
71 out.pts = pic.pts;
72 out
73}
74
75/// NV12 (luma, then interleaved CbCr) as I420.
76fn nv12(pic: &VideoFrame) -> VideoFrame {
77 let (w, h) = (pic.width as usize, pic.height as usize);
78 let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
79 let uv = &pic.planes[1];
80 let (mut u, mut v) = (Vec::with_capacity(cw * ch), Vec::with_capacity(cw * ch));
81 for row in 0..ch {
82 for pair in uv.data[row * uv.stride..row * uv.stride + 2 * cw].as_chunks::<2>().0 {
83 u.push(pair[0]);
84 v.push(pair[1]);
85 }
86 }
87 let planes = vec![pic.planes[0].clone(), Plane::new(u, cw), Plane::new(v, cw)];
88 let mut out = VideoFrame::try_new(PixelFormat::I420, pic.width, pic.height, planes).expect("three planes of the picture's size");
89 out.pts = pic.pts;
90 out
91}
92
93/// 16-bit little-endian gray as `I010`: luma rounded down to 10 bit, chroma neutral (512).
94fn gray16(pic: &VideoFrame) -> VideoFrame {
95 let (w, h) = (pic.width as usize, pic.height as usize);
96 let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
97 let src = &pic.planes[0];
98 let mut luma = Vec::with_capacity(w * h * 2);
99 for row in 0..h {
100 for px in src.data[row * src.stride..row * src.stride + 2 * w].as_chunks::<2>().0 {
101 let v = (u32::from(u16::from_le_bytes([px[0], px[1]])) + 32) >> 6;
102 luma.extend_from_slice(&(v.min(1023) as u16).to_le_bytes());
103 }
104 }
105 let neutral: Vec<u8> = std::iter::repeat_n(512u16.to_le_bytes(), cw * ch).flatten().collect();
106 let planes = vec![Plane::new(luma, 2 * w), Plane::new(neutral.clone(), 2 * cw), Plane::new(neutral, 2 * cw)];
107 let mut out = VideoFrame::try_new(PixelFormat::I010, pic.width, pic.height, planes).expect("three planes of the picture's size");
108 out.pts = pic.pts;
109 out
110}