forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

profile.rs287 lines, 11.0 KiBHistoryRawDownload
1//! DV system profiles (`dv_profile.c`) and the macroblock placement tables (`dv.c`).
2
3use crate::tables::{AUDIO_SHUFFLE525, AUDIO_SHUFFLE625};
4
5/// Native chroma layout of a DV system.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum Chroma {
8 /// 4:1:1 (NTSC DV25 / DVCPRO25, and the PAL DVCPRO25 variant).
9 Yuv411,
10 /// 4:2:0 (PAL DV25).
11 Yuv420,
12 /// 4:2:2 (DVCPRO50 and DVCPRO HD).
13 Yuv422,
14}
15
16/// One DV flavour: geometry, frame size and the tables its audio uses.
17#[derive(Debug)]
18pub struct Profile {
19 /// `dsf` bit of the DIF header: 0 = 525/60, 1 = 625/50.
20 pub dsf: u8,
21 /// Video source `stype` (0 = 25 Mbps, 4 = 50 Mbps, 0x14 = 1080i HD, 0x18 = 720p HD, 1 = IEC 61883-5 PAL).
22 pub video_stype: u8,
23 /// Bytes per frame.
24 pub frame_size: usize,
25 /// DIF sequences per DIF channel (10 for 525, 12 for 625).
26 pub difseg_size: usize,
27 /// DIF channels per frame.
28 pub n_difchan: usize,
29 /// Frame duration as `(num, den)` seconds.
30 pub time_base: (u32, u32),
31 /// Picture width.
32 pub width: u32,
33 /// Picture height.
34 pub height: u32,
35 /// Sample aspect ratio for 4:3 and 16:9 pictures.
36 pub sar: [(u32, u32); 2],
37 /// Native chroma layout.
38 pub chroma: Chroma,
39 /// Blocks per macroblock.
40 pub bpm: usize,
41 /// AC block sizes in bits.
42 pub block_sizes: &'static [u8; 8],
43 /// Stride of the audio shuffle table.
44 pub audio_stride: usize,
45 /// Minimum audio samples per frame at 48, 44.1 and 32 kHz.
46 pub audio_min_samples: [usize; 3],
47 /// The audio shuffle table.
48 pub audio_shuffle: &'static [[u8; 9]],
49}
50
51impl Profile {
52 /// DVCPRO HD (SMPTE 370M).
53 pub fn is_hd(&self) -> bool {
54 self.video_stype & 0x10 != 0
55 }
56 pub(crate) fn is_1080i50(&self) -> bool {
57 self.video_stype == 0x14 && self.dsf == 1
58 }
59 pub(crate) fn is_720p50(&self) -> bool {
60 self.video_stype == 0x18 && self.dsf == 1
61 }
62 /// Frames per second as `(num, den)`.
63 pub fn frame_rate(&self) -> (u32, u32) {
64 (self.time_base.1, self.time_base.0)
65 }
66 /// Number of 5-macroblock video segments in a frame.
67 pub fn work_size(&self) -> usize {
68 let mut size = self.n_difchan * self.difseg_size * 27;
69 if self.is_1080i50() {
70 size -= 3 * 27;
71 }
72 if self.is_720p50() {
73 size -= 4 * 27;
74 }
75 size
76 }
77}
78
79static BLOCK_SIZES_DV2550: [u8; 8] = [112, 112, 112, 112, 80, 80, 0, 0];
80static BLOCK_SIZES_DV100: [u8; 8] = [80, 80, 80, 80, 80, 80, 64, 64];
81
82#[allow(clippy::too_many_arguments)]
83const fn p(
84 dsf: u8,
85 video_stype: u8,
86 frame_size: usize,
87 difseg_size: usize,
88 n_difchan: usize,
89 time_base: (u32, u32),
90 height: u32,
91 width: u32,
92 sar: [(u32, u32); 2],
93 chroma: Chroma,
94 bpm: usize,
95 block_sizes: &'static [u8; 8],
96 audio_stride: usize,
97 audio_min_samples: [usize; 3],
98 audio_shuffle: &'static [[u8; 9]],
99) -> Profile {
100 Profile {
101 dsf,
102 video_stype,
103 frame_size,
104 difseg_size,
105 n_difchan,
106 time_base,
107 width,
108 height,
109 sar,
110 chroma,
111 bpm,
112 block_sizes,
113 audio_stride,
114 audio_min_samples,
115 audio_shuffle,
116 }
117}
118
119const NTSC_MIN: [usize; 3] = [1580, 1452, 1053];
120const PAL_MIN: [usize; 3] = [1896, 1742, 1264];
121
122/// The profile list, in FFmpeg's order (the index matters for [`frame_profile`]).
123pub static PROFILES: [Profile; 10] = [
124 p(0, 0x0, 120000, 10, 1, (1001, 30000), 480, 720, [(8, 9), (32, 27)], Chroma::Yuv411, 6, &BLOCK_SIZES_DV2550, 90, NTSC_MIN, &AUDIO_SHUFFLE525),
125 p(1, 0x0, 144000, 12, 1, (1, 25), 576, 720, [(16, 15), (64, 45)], Chroma::Yuv420, 6, &BLOCK_SIZES_DV2550, 108, PAL_MIN, &AUDIO_SHUFFLE625),
126 p(1, 0x0, 144000, 12, 1, (1, 25), 576, 720, [(16, 15), (64, 45)], Chroma::Yuv411, 6, &BLOCK_SIZES_DV2550, 108, PAL_MIN, &AUDIO_SHUFFLE625),
127 p(0, 0x4, 240000, 10, 2, (1001, 30000), 480, 720, [(8, 9), (32, 27)], Chroma::Yuv422, 6, &BLOCK_SIZES_DV2550, 90, NTSC_MIN, &AUDIO_SHUFFLE525),
128 p(1, 0x4, 288000, 12, 2, (1, 25), 576, 720, [(16, 15), (64, 45)], Chroma::Yuv422, 6, &BLOCK_SIZES_DV2550, 108, PAL_MIN, &AUDIO_SHUFFLE625),
129 p(0, 0x14, 480000, 10, 4, (1001, 30000), 1080, 1280, [(1, 1), (3, 2)], Chroma::Yuv422, 8, &BLOCK_SIZES_DV100, 90, NTSC_MIN, &AUDIO_SHUFFLE525),
130 p(1, 0x14, 576000, 12, 4, (1, 25), 1080, 1440, [(1, 1), (4, 3)], Chroma::Yuv422, 8, &BLOCK_SIZES_DV100, 108, PAL_MIN, &AUDIO_SHUFFLE625),
131 p(0, 0x18, 240000, 10, 2, (1001, 60000), 720, 960, [(1, 1), (4, 3)], Chroma::Yuv422, 8, &BLOCK_SIZES_DV100, 90, NTSC_MIN, &AUDIO_SHUFFLE525),
132 p(1, 0x18, 288000, 12, 2, (1, 50), 720, 960, [(1, 1), (4, 3)], Chroma::Yuv422, 8, &BLOCK_SIZES_DV100, 90, PAL_MIN, &AUDIO_SHUFFLE625),
133 p(1, 0x1, 144000, 12, 1, (1, 25), 576, 720, [(16, 15), (64, 45)], Chroma::Yuv420, 6, &BLOCK_SIZES_DV2550, 108, PAL_MIN, &AUDIO_SHUFFLE625),
134];
135
136/// Bytes needed to identify a profile (six DIF blocks).
137pub const PROFILE_BYTES: usize = 6 * 80;
138
139/// Pick the profile of a DV frame (`ff_dv_frame_profile`, minus the QuickTime `SL25` codec-tag
140/// hack). `prev` is the profile of the previous frame, used for damaged headers.
141pub fn frame_profile(prev: Option<&'static Profile>, frame: &[u8]) -> Option<&'static Profile> {
142 if frame.len() < PROFILE_BYTES {
143 return None;
144 }
145 let dsf = (frame[3] & 0x80) >> 7;
146 let stype = frame[80 * 5 + 48 + 3] & 0x1f;
147 let pal = frame[80 * 5 + 48 + 3] & 0x20 != 0;
148 if dsf == 1 && stype == 0 && frame[4] & 0x07 != 0 {
149 return Some(&PROFILES[2]);
150 }
151 if dsf == 0 && pal && stype == PROFILES[1].video_stype && frame.len() == PROFILES[1].frame_size {
152 return Some(&PROFILES[1]);
153 }
154 if let Some(p) = PROFILES.iter().find(|p| p.dsf == dsf && p.video_stype == stype) {
155 return Some(p);
156 }
157 if let Some(s) = prev
158 && frame.len() == s.frame_size
159 {
160 return Some(s);
161 }
162 if frame[3] & 0x7f == 0x3f && frame[80 * 5 + 48 + 3] == 0xff {
163 return Some(&PROFILES[usize::from(dsf)]);
164 }
165 None
166}
167
168/// One 5-macroblock video segment: where it starts (in 80-byte DIF blocks) and where its
169/// macroblocks go (`x | y << 8`, units of 8 pixels).
170#[derive(Clone, Copy, Debug, Default)]
171pub(crate) struct WorkChunk {
172 pub buf_offset: u16,
173 pub mb: [u16; 5],
174}
175
176fn mb_coordinates(d: &Profile, chan: usize, seq: usize, slot: usize, tbl: &mut [u16; 5]) {
177 const OFF: [usize; 5] = [2, 6, 8, 0, 4];
178 const SHUF1: [usize; 5] = [36, 18, 54, 0, 72];
179 const SHUF2: [usize; 5] = [24, 12, 36, 0, 48];
180 const SHUF3: [usize; 5] = [18, 9, 27, 0, 36];
181 const L_START: [usize; 10] = [0, 4, 9, 13, 18, 22, 27, 31, 36, 40];
182 const L_START_SHUFFLED: [usize; 5] = [9, 4, 13, 0, 18];
183 const SERPENT1: [usize; 27] = [
184 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2,
185 ];
186 const SERPENT2: [usize; 30] = [
187 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5,
188 ];
189 const REMAP: [[usize; 2]; 64] = [
190 [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 2], [0, 3], [10, 0],
191 [10, 1], [10, 2], [10, 3], [20, 0], [20, 1], [20, 2], [20, 3], [30, 0], [30, 1], [30, 2],
192 [30, 3], [40, 0], [40, 1], [40, 2], [40, 3], [50, 0], [50, 1], [50, 2], [50, 3], [60, 0],
193 [60, 1], [60, 2], [60, 3], [70, 0], [70, 1], [70, 2], [70, 3], [0, 64], [0, 65], [0, 66],
194 [10, 64], [10, 65], [10, 66], [20, 64], [20, 65], [20, 66], [30, 64], [30, 65], [30, 66],
195 [40, 64], [40, 65], [40, 66], [50, 64], [50, 65], [50, 66], [60, 64], [60, 65], [60, 66],
196 [70, 64], [70, 65], [70, 66], [0, 67], [20, 67], [40, 67], [60, 67],
197 ];
198 for m in 0..5 {
199 match d.width {
200 1440 => {
201 let blk = (chan * 11 + seq) * 27 + slot;
202 let (x, y);
203 if chan == 0 && seq == 11 {
204 let xx = m * 27 + slot;
205 if xx < 90 {
206 x = xx;
207 y = 0;
208 } else {
209 x = (xx - 90) * 2;
210 y = 67;
211 }
212 } else {
213 let i = (4 * chan + blk + OFF[m]) % 11;
214 let k = (blk / 11) % 27;
215 x = SHUF1[m] + (chan & 1) * 9 + k % 9;
216 y = (i * 3 + k / 9) * 2 + (chan >> 1) + 1;
217 }
218 tbl[m] = ((x << 1) | (y << 9)) as u16;
219 }
220 1280 => {
221 let blk = (chan * 10 + seq) * 27 + slot;
222 let i = (4 * chan + (seq / 5) + 2 * blk + OFF[m]) % 10;
223 let k = (blk / 5) % 27;
224 let mut x = SHUF1[m] + (chan & 1) * 9 + k % 9;
225 let mut y = (i * 3 + k / 9) * 2 + (chan >> 1) + 4;
226 if x >= 80 {
227 let r = REMAP[y.min(63)];
228 x = r[0] + ((x - 80) << usize::from(y > 59));
229 y = r[1];
230 }
231 tbl[m] = ((x << 1) | (y << 9)) as u16;
232 }
233 960 => {
234 let blk = (chan * 10 + seq) * 27 + slot;
235 let i = (4 * chan + (seq / 5) + 2 * blk + OFF[m]) % 10;
236 let k = (blk / 5) % 27 + (i & 1) * 3;
237 let x = SHUF2[m] + k % 6 + 6 * (chan & 1);
238 let y = L_START[i] + k / 6 + 45 * (chan >> 1);
239 tbl[m] = ((x << 1) | (y << 9)) as u16;
240 }
241 _ => match d.chroma {
242 Chroma::Yuv422 => {
243 let x = SHUF3[m] + slot / 3;
244 let y = SERPENT1[slot] + ((((seq + OFF[m]) % d.difseg_size) << 1) + chan) * 3;
245 tbl[m] = ((x << 1) | (y << 8)) as u16;
246 }
247 Chroma::Yuv420 => {
248 let x = SHUF3[m] + slot / 3;
249 let y = SERPENT1[slot] + ((seq + OFF[m]) % d.difseg_size) * 3;
250 tbl[m] = ((x << 1) | (y << 9)) as u16;
251 }
252 Chroma::Yuv411 => {
253 let i = (seq + OFF[m]) % d.difseg_size;
254 let k = slot + if m == 1 || m == 2 { 3 } else { 0 };
255 let x = L_START_SHUFFLED[m] + k / 6;
256 let mut y = SERPENT2[k] + i * 6;
257 if x > 21 {
258 y = y * 2 - i * 6;
259 }
260 tbl[m] = ((x << 2) | (y << 8)) as u16;
261 }
262 },
263 }
264 }
265}
266
267pub(crate) fn work_chunks(d: &Profile) -> Vec<WorkChunk> {
268 let mut v = Vec::with_capacity(d.work_size());
269 let mut p = 0usize;
270 for c in 0..d.n_difchan {
271 for s in 0..d.difseg_size {
272 p += 6;
273 for j in 0..27 {
274 if j % 3 == 0 {
275 p += 1;
276 }
277 if !(d.is_1080i50() && c != 0 && s == 11) && !(d.is_720p50() && s > 9) {
278 let mut w = WorkChunk { buf_offset: p as u16, mb: [0; 5] };
279 mb_coordinates(d, c, s, j, &mut w.mb);
280 v.push(w);
281 }
282 p += 5;
283 }
284 }
285 }
286 v
287}