forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

leaf.rs575 lines, 19.4 KiBHistoryRawDownload
1use super::*;
2
3/// Writes one 16x16 leaf a straddling 32x32 quadrant's [`Quadrant::Split`]
4/// splits into: intra (spec `inter_frame_mode_info`'s intra branch, the only
5/// one this writer used to code here) or real inter (`is_inter` coded
6/// `true`), `NEARESTMV` only -- `NEWMV` at this size is this function's
7/// caller's to never build (`Quadrant::Split`'s `BlockCoeffs.inter`) -- same
8/// `single_ref`/mv-stack/DRL symbol chain as the whole-32x32 branch above,
9/// just at this leaf's own 4x4-mi window (`bw4`/`bh4` of 4, not 8) and coded
10/// through [`TxbSet::Luma16Inter`] rather than [`TxbSet::Luma32Inter`]. Its
11/// own `PARTITION_NONE` symbol is already written, same contract as
12/// [`write_block`].
13#[allow(clippy::too_many_arguments)]
14pub(super) fn write_inter_frame_leaf(
15 enc: &mut SymbolEncoder,
16 cdfs: &mut Cdfs,
17 neighbours: &mut Neighbours,
18 grid: &mut MiGrid,
19 mi_cols: u32,
20 mi_rows: u32,
21 block: &BlockCoeffs,
22 at: (usize, usize),
23 scan16: &Vec<u16>,
24 scan8: &Vec<u16>,
25 tx_select: bool,
26 all_scans: &[Vec<u16>; 4],
27) -> Result<()> {
28 if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES {
29 return Err(Error::unsupported(
30 "AV1 tile",
31 format!(
32 "intra mode {} is not one of the thirteen this writer codes",
33 block.mode
34 ),
35 ));
36 }
37 /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s
38 /// `size_group_lookup[BLOCK_16X16]`) for this leaf's own size.
39 const SIZE_GROUP_16: usize = 2;
40
41 let (r, c) = at;
42 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
43 let skip_ctx =
44 usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]);
45 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
46 write_cdef_idx(enc, (mi_r, mi_c), block.skip);
47 write_delta_q(enc, cdfs, (mi_r, mi_c), false, block.skip);
48
49 let (has_above, has_left) = (neighbours.has_above(mi_r), neighbours.has_left(mi_c));
50 let (above_inter, left_inter) = (neighbours.above_inter[mi_c], neighbours.left_inter[mi_r]);
51 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
52 let is_inter = block.inter.is_some();
53 enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]);
54
55 let mode_for_tx;
56 if let Some(info) = block.inter {
57 write_comp_mode(
58 enc,
59 cdfs,
60 neighbours,
61 (mi_r, mi_c),
62 (has_above, has_left),
63 info.ref1.is_some(),
64 );
65 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
66 if info.ref1.is_some() {
67 let n = SUB_MI as usize;
68 let (mv, mv1, is_new_mv) = write_compound_block(
69 enc,
70 cdfs,
71 neighbours,
72 grid,
73 (mi_row, mi_col),
74 (n, n),
75 (has_above, has_left),
76 info,
77 (mi_cols as usize, mi_rows as usize),
78 )?;
79 for dr in 0..n {
80 for dc in 0..n {
81 grid.set(
82 mi_row + dr,
83 mi_col + dc,
84 MiInfo {
85 is_inter: true,
86 ref_frame: info.ref_frame,
87 ref_frame1: info.ref1.unwrap_or(NO_REF1),
88 mv1: mv16(mv1),
89 mv: mv16(mv),
90 is_new_mv,
91 size: n as u8,
92 size_h: n as u8,
93 is_global_mv0: false,
94 is_global_mv1: false,
95 },
96 );
97 }
98 }
99 mode_for_tx = 0;
100 } else {
101 write_single_ref(
102 enc,
103 cdfs,
104 info.ref_frame,
105 neighbours.above_ref[mi_c],
106 neighbours.above_ref1[mi_c],
107 neighbours.left_ref[mi_r],
108 neighbours.left_ref1[mi_r],
109 );
110
111 let stack = find_mv_stack(
112 grid,
113 mi_row,
114 mi_col,
115 SUB_MI as usize,
116 SUB_MI as usize,
117 info.ref_frame,
118 mi_cols as usize,
119 mi_rows as usize,
120 );
121
122 let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?;
123 for dr in 0..SUB_MI as usize {
124 for dc in 0..SUB_MI as usize {
125 grid.set(
126 mi_row + dr,
127 mi_col + dc,
128 MiInfo {
129 is_inter: true,
130 ref_frame: info.ref_frame,
131 ref_frame1: NO_REF1,
132 mv1: (0, 0),
133 mv: mv16(mv),
134 is_new_mv,
135 size: SUB_MI as usize as u8,
136 size_h: SUB_MI as usize as u8,
137 is_global_mv0: false,
138 is_global_mv1: false,
139 },
140 );
141 }
142 }
143 write_motion_mode(
144 enc,
145 cdfs,
146 grid,
147 (mi_row, mi_col),
148 (SUB_MI as usize, SUB_MI as usize),
149 (SUB, SUB),
150 (mi_cols as usize, mi_rows as usize),
151 info.ref_frame,
152 block.motion_mode,
153 )?;
154 mode_for_tx = 0;
155 }
156 } else {
157 let mode = usize::from(block.mode);
158 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]);
159 if (V_PRED..=D67_PRED).contains(&mode) {
160 enc.symbol(
161 (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
162 &mut cdfs.angle_delta[mode - V_PRED],
163 );
164 }
165 let uv_mode = usize::from(block.uv_mode);
166 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);
167 if uv_mode == UV_CFL_PRED {
168 write_cfl_alphas(enc, cdfs, block.cfl_alphas.expect("cfl"));
169 }
170 if (V_PRED..=D67_PRED).contains(&uv_mode) {
171 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
172 }
173 mode_for_tx = tx_row(mode, block.filter_intra);
174 // Intra: no vote, but still a coded cell -- mvstack's extended-scan
175 // coverage must see it (module doc).
176 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
177 write_palette_syntax(
178 enc,
179 cdfs,
180 neighbours,
181 block.palette.as_ref(),
182 block.palette_uv.as_ref(),
183 block.filter_intra,
184 mode,
185 uv_mode,
186 (mi_row, mi_col),
187 SUB,
188 );
189 for dr in 0..SUB_MI as usize {
190 for dc in 0..SUB_MI as usize {
191 grid.set(
192 mi_row + dr,
193 mi_col + dc,
194 MiInfo {
195 is_inter: false,
196 ref_frame: -1,
197 ref_frame1: NO_REF1,
198 mv1: (0, 0),
199 mv: (0, 0),
200 is_new_mv: false,
201 size: SUB_MI as usize as u8,
202 size_h: SUB_MI as usize as u8,
203 is_global_mv0: false,
204 is_global_mv1: false,
205 },
206 );
207 }
208 }
209 }
210
211 let at_mi = (at.0 * (SUB / MI), at.1 * (SUB / MI));
212 let tx = if tx_select {
213 write_tx_syntax_inter(
214 enc,
215 cdfs,
216 neighbours,
217 at_mi,
218 SUB,
219 is_inter,
220 block.skip,
221 usize::from(block.tx_depth),
222 )
223 } else {
224 SUB
225 };
226 let split = tx < SUB;
227 if block.skip {
228 let zero_grids = [
229 vec![0i32; TX16 * TX16],
230 vec![0i32; TX8 * TX8],
231 vec![0i32; TX8 * TX8],
232 ];
233 neighbours.record(at, SUB, mode_for_tx, &zero_grids);
234 } else {
235 let grids = [
236 level_grid(&block.luma, TX16)?,
237 level_grid(&block.u, TX8)?,
238 level_grid(&block.v, TX8)?,
239 ];
240 if split {
241 write_luma_tus(
242 enc,
243 cdfs,
244 neighbours,
245 at_mi,
246 SUB,
247 tx,
248 &grids[0],
249 mode_for_tx,
250 all_scans,
251 is_inter,
252 &block.luma_tx_types,
253 )?;
254 }
255 write_block_planes(
256 enc,
257 cdfs,
258 if is_inter {
259 &[TxbSet::Luma16Inter, TxbSet::Chroma8, TxbSet::Chroma8]
260 } else {
261 &[TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8]
262 },
263 &grids,
264 &[scan16, scan8, scan8],
265 &neighbours.around(at, SUB),
266 mode_for_tx,
267 split,
268 block_tx_type(block),
269 is_inter,
270 );
271 neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split);
272 }
273 neighbours.record_inter(at, SUB, block.skip, is_inter, block_ref(block));
274 record_block_compound(neighbours, (mi_r, mi_c), SUB, block);
275 Ok(())
276}
277
278/// Writes one 8x8 leaf of a straddling 16x16 inter-frame block
279/// (lane-av1inter8): its own skip flag, intra/inter choice and (when inter)
280/// `NEARESTMV`/`NEWMV` chain, or (when intra) `Y_MODE`, then its own luma and
281/// two chroma transform blocks -- coded exactly like
282/// [`write_inter_frame_leaf`] but through [`TxbSet::Luma8Inter`]/
283/// [`TxbSet::Chroma4`] and at this leaf's own 2x2-mi mv-stack window, reading
284/// its skip/intra-inter context from the *enclosing* 16x16 slot's
285/// [`Neighbours`] arrays (`outer_at`, in [`SUB`]-grid units) unless
286/// `prev_leaf` names this straddling block's own first leaf as the true
287/// mi-adjacent neighbour -- the same override [`write_leaf8`] applies to its
288/// mode context, needed for the same reason: `Neighbours`' above/left arrays
289/// only resolve to [`SUB`] granularity, too coarse for the second leaf of a
290/// straddling 16x16 to see the first. `Y_MODE`'s own context (`Size_Group`)
291/// is not neighbour-dependent, unlike the key frame path's `kf_y_mode`, so no
292/// override is needed there. Hands back this leaf's own skip flag and
293/// intra/inter choice, which is what the next leaf (or the caller's final
294/// write-back, mirroring `write_leaf8`'s caller) reads.
295#[allow(clippy::too_many_arguments)]
296pub(super) fn write_inter_frame_leaf8(
297 enc: &mut SymbolEncoder,
298 cdfs: &mut Cdfs,
299 neighbours: &mut Neighbours,
300 grid: &mut MiGrid,
301 mi_cols: u32,
302 mi_rows: u32,
303 block: &BlockCoeffs,
304 leaf_mi: (usize, usize),
305 scan8: &Vec<u16>,
306 scan4: &Vec<u16>,
307 tx_select: bool,
308 all_scans: &[Vec<u16>; 4],
309) -> Result<(bool, bool)> {
310 if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES {
311 return Err(Error::unsupported(
312 "AV1 tile",
313 format!(
314 "intra mode {} is not one of the thirteen this writer codes",
315 block.mode
316 ),
317 ));
318 }
319 /// `Y_MODE`'s size group (spec `Size_Group`, `common_data.h`'s
320 /// `size_group_lookup[BLOCK_8X8]`) for this leaf's own size.
321 const SIZE_GROUP_8: usize = 1;
322
323 // This leaf's own 4x4 mode-info cells, not the enclosing 16x16 slot's:
324 // under a real PARTITION_SPLIT all four leaves are coded, so the
325 // bottom-left leaf's true above neighbour is the FIRST leaf and the
326 // bottom-right's above and left are two DIFFERENT ones (lane-av1rd2 --
327 // reading the coarse slot desynced the stream, and so did an override
328 // that remembered only the previous leaf).
329 let above_skip = neighbours.above_skip[leaf_mi.1];
330 let left_skip = neighbours.left_skip[leaf_mi.0];
331 let above_inter = neighbours.above_inter[leaf_mi.1];
332 let left_inter = neighbours.left_inter[leaf_mi.0];
333 let skip_ctx = usize::from(above_skip) + usize::from(left_skip);
334 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
335 write_cdef_idx(enc, leaf_mi, block.skip);
336 write_delta_q(enc, cdfs, leaf_mi, false, block.skip);
337
338 let (has_above, has_left) = (
339 neighbours.has_above(leaf_mi.0),
340 neighbours.has_left(leaf_mi.1),
341 );
342 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
343 let is_inter = block.inter.is_some();
344 enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]);
345
346 let mode_for_tx;
347 if let Some(info) = block.inter {
348 write_comp_mode(
349 enc,
350 cdfs,
351 neighbours,
352 leaf_mi,
353 (has_above, has_left),
354 info.ref1.is_some(),
355 );
356 let (mi_row, mi_col) = leaf_mi;
357 if info.ref1.is_some() {
358 let (mv, mv1, is_new_mv) = write_compound_block(
359 enc,
360 cdfs,
361 neighbours,
362 grid,
363 (mi_row, mi_col),
364 (2, 2),
365 (has_above, has_left),
366 info,
367 (mi_cols as usize, mi_rows as usize),
368 )?;
369 for dr in 0..2 {
370 for dc in 0..2 {
371 grid.set(
372 mi_row + dr,
373 mi_col + dc,
374 MiInfo {
375 is_inter: true,
376 ref_frame: info.ref_frame,
377 ref_frame1: info.ref1.unwrap_or(NO_REF1),
378 mv1: mv16(mv1),
379 mv: mv16(mv),
380 is_new_mv,
381 size: 2,
382 size_h: 2,
383 is_global_mv0: false,
384 is_global_mv1: false,
385 },
386 );
387 }
388 }
389 mode_for_tx = 0;
390 } else {
391 write_single_ref(
392 enc,
393 cdfs,
394 info.ref_frame,
395 neighbours.above_ref[leaf_mi.1],
396 neighbours.above_ref1[leaf_mi.1],
397 neighbours.left_ref[leaf_mi.0],
398 neighbours.left_ref1[leaf_mi.0],
399 );
400
401 let stack = find_mv_stack(
402 grid,
403 mi_row,
404 mi_col,
405 2,
406 2,
407 info.ref_frame,
408 mi_cols as usize,
409 mi_rows as usize,
410 );
411
412 let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?;
413 for dr in 0..2 {
414 for dc in 0..2 {
415 grid.set(
416 mi_row + dr,
417 mi_col + dc,
418 MiInfo {
419 is_inter: true,
420 ref_frame: info.ref_frame,
421 ref_frame1: NO_REF1,
422 mv1: (0, 0),
423 mv: mv16(mv),
424 is_new_mv,
425 size: 2,
426 size_h: 2,
427 is_global_mv0: false,
428 is_global_mv1: false,
429 },
430 );
431 }
432 }
433 write_motion_mode(
434 enc,
435 cdfs,
436 grid,
437 (mi_row, mi_col),
438 (2, 2),
439 (8, 8),
440 (mi_cols as usize, mi_rows as usize),
441 info.ref_frame,
442 block.motion_mode,
443 )?;
444 mode_for_tx = 0;
445 }
446 } else {
447 let mode = usize::from(block.mode);
448 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]);
449 if (V_PRED..=D67_PRED).contains(&mode) {
450 enc.symbol(
451 (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
452 &mut cdfs.angle_delta[mode - V_PRED],
453 );
454 }
455 let uv_mode = usize::from(block.uv_mode);
456 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);
457 if uv_mode == UV_CFL_PRED {
458 write_cfl_alphas(enc, cdfs, block.cfl_alphas.expect("cfl"));
459 }
460 if (V_PRED..=D67_PRED).contains(&uv_mode) {
461 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[uv_mode - V_PRED]);
462 }
463 mode_for_tx = tx_row(mode, block.filter_intra);
464 // Intra: no vote, but still a coded cell -- mvstack's extended-scan
465 // coverage must see it (module doc).
466 let (mi_row, mi_col) = leaf_mi;
467 write_palette_syntax(
468 enc,
469 cdfs,
470 neighbours,
471 block.palette.as_ref(),
472 block.palette_uv.as_ref(),
473 block.filter_intra,
474 mode,
475 uv_mode,
476 leaf_mi,
477 8,
478 );
479 for dr in 0..2 {
480 for dc in 0..2 {
481 grid.set(
482 mi_row + dr,
483 mi_col + dc,
484 MiInfo {
485 is_inter: false,
486 ref_frame: -1,
487 ref_frame1: NO_REF1,
488 mv1: (0, 0),
489 mv: (0, 0),
490 is_new_mv: false,
491 size: 2,
492 size_h: 2,
493 is_global_mv0: false,
494 is_global_mv1: false,
495 },
496 );
497 }
498 }
499 }
500
501 let planes = if is_inter {
502 [TxbSet::Luma8Inter, TxbSet::Chroma4, TxbSet::Chroma4]
503 } else {
504 [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]
505 };
506 let tx = if tx_select {
507 write_tx_syntax_inter(
508 enc,
509 cdfs,
510 neighbours,
511 leaf_mi,
512 8,
513 is_inter,
514 block.skip,
515 usize::from(block.tx_depth),
516 )
517 } else {
518 8
519 };
520 let split = tx < 8;
521 if block.skip {
522 let zero_grids = [
523 vec![0i32; TX8 * TX8],
524 vec![0i32; TX4 * TX4],
525 vec![0i32; TX4 * TX4],
526 ];
527 neighbours.record_mi(leaf_mi, 8, &zero_grids);
528 neighbours.record_inter_mi(
529 leaf_mi,
530 8,
531 block.skip,
532 block.inter.is_some(),
533 block_ref(block),
534 );
535 record_block_compound(neighbours, leaf_mi, 8, block);
536 } else {
537 let grids = [
538 level_grid(&block.luma, TX8)?,
539 level_grid(&block.u, TX4)?,
540 level_grid(&block.v, TX4)?,
541 ];
542 if split {
543 write_luma_tus(
544 enc,
545 cdfs,
546 neighbours,
547 leaf_mi,
548 8,
549 tx,
550 &grids[0],
551 mode_for_tx,
552 all_scans,
553 is_inter,
554 &block.luma_tx_types,
555 )?;
556 }
557 write_block_planes(
558 enc,
559 cdfs,
560 &planes,
561 &grids,
562 &[scan8, scan4, scan4],
563 &neighbours.around_mi(leaf_mi, 8),
564 mode_for_tx,
565 split,
566 block_tx_type(block),
567 is_inter,
568 );
569 neighbours.record_mi_planes(leaf_mi, 8, &grids, !split);
570 neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block));
571 record_block_compound(neighbours, leaf_mi, 8, block);
572 }
573 Ok((block.skip, is_inter))
574}
575