wip(av1): write the true display size as frame_width/height, not the padded one
Browse files at this commit6b1cdccc4dee5d3c2e9b390e80871c38124aac44Parent: c47074a3 changed files +56 -44
- crates/ec-av1/src/encode.rs +34 -28
- crates/ec-av1/src/encoder.rs +2 -2
- crates/ec-av1/src/tile.rs +20 -14
crates/ec-av1/src/encode.rsmodified+34 -28
| @@ -379,7 +379,11 @@ pub(crate) fn key_frame_headers_colour( | |||
| 379 | enable_ref_frame_mvs: false, | 379 | enable_ref_frame_mvs: false, |
| 380 | film_grain_params_present: false, | 380 | film_grain_params_present: false, |
| 381 | }; | 381 | }; |
| 382 | - let (mi_cols, mi_rows) = (w / 4, h / 4); | 382 | + // spec `compute_image_size` (5.9.15): MiCols/MiRows come from the frame's |
| 383 | + // own (true, unpadded) width and height, not from any block-grid | ||
| 384 | + // alignment — a decoder derives these straight from `frame_width`/ | ||
| 385 | + // `frame_height` below, so this must match it exactly. | ||
| 386 | + let (mi_cols, mi_rows) = (2 * ((w + 7) >> 3), 2 * ((h + 7) >> 3)); | ||
| 383 | let header = FrameHeader { | 387 | let header = FrameHeader { |
| 384 | frame_type: FrameType::Key, | 388 | frame_type: FrameType::Key, |
| 385 | frame_is_intra: true, | 389 | frame_is_intra: true, |
| @@ -1063,8 +1067,14 @@ pub(crate) fn encode_key_frame_inner( | |||
| 1063 | format!("intra mode {bad} is not one the encoder predicts"), | 1067 | format!("intra mode {bad} is not one the encoder predicts"), |
| 1064 | )); | 1068 | )); |
| 1065 | } | 1069 | } |
| 1066 | - let (seq, mut header) = | 1070 | + // The header carries the frame's true (unpadded) size as `frame_width`/ |
| 1067 | - key_frame_headers_colour(picture.width, picture.height, base_q_idx, color_config)?; | 1071 | + // `frame_height` -- what a decoder actually crops to -- with `mi_cols`/ |
| 1072 | + // `mi_rows` derived from that same true size (spec `compute_image_size`). | ||
| 1073 | + // `render_width`/`render_height` come out equal to it too, so | ||
| 1074 | + // `render_and_frame_size_different` is false and no render_size bits are | ||
| 1075 | + // written, mirroring libaom/rav1e: the padded `picture` below is only the | ||
| 1076 | + // internal coding surface, never what the header names. | ||
| 1077 | + let (seq, mut header) = key_frame_headers_colour(render.0, render.1, base_q_idx, color_config)?; | ||
| 1068 | header.render_width = render.0 as u32; | 1078 | header.render_width = render.0 as u32; |
| 1069 | header.render_height = render.1 as u32; | 1079 | header.render_height = render.1 as u32; |
| 1070 | 1080 | ||
| @@ -1099,7 +1109,14 @@ pub(crate) fn encode_key_frame_inner( | |||
| 1099 | modes, | 1109 | modes, |
| 1100 | }; | 1110 | }; |
| 1101 | 1111 | ||
| 1102 | - let (cols, rows) = (picture.width / BLOCK, picture.height / BLOCK); | 1112 | + // The block grid this frame is coded over: [`crate::tile::block_grid`]'s |
| 1113 | + // ceiling of the header's own (true-size-derived) `mi_cols`/`mi_rows`, | ||
| 1114 | + // which may be fewer columns/rows than the padded picture has room for | ||
| 1115 | + // -- a block whose origin sits past the true bound is not coded at all, | ||
| 1116 | + // matching what a decoder derives from `frame_width`/`frame_height` and | ||
| 1117 | + // so never visits either. | ||
| 1118 | + let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows); | ||
| 1119 | + let (cols, rows) = (cols as usize, rows as usize); | ||
| 1103 | // The luma mode of the block above and of the block to the left, which is | 1120 | // The luma mode of the block above and of the block to the left, which is |
| 1104 | // what picks the CDF the next block's mode is coded against -- the same | 1121 | // what picks the CDF the next block's mode is coded against -- the same |
| 1105 | // bookkeeping the tile writer keeps, so that the search is costing the | 1122 | // bookkeeping the tile writer keeps, so that the search is costing the |
| @@ -2186,23 +2203,18 @@ mod tests { | |||
| 2186 | (width, height), | 2203 | (width, height), |
| 2187 | "{width}x{height}: reconstruction is cropped to the picture's own size" | 2204 | "{width}x{height}: reconstruction is cropped to the picture's own size" |
| 2188 | ); | 2205 | ); |
| 2189 | - let (padded_width, padded_height) = ( | ||
| 2190 | - width.next_multiple_of(BLOCK), | ||
| 2191 | - height.next_multiple_of(BLOCK), | ||
| 2192 | - ); | ||
| 2193 | assert_eq!( | 2206 | assert_eq!( |
| 2194 | ffprobe_size(&encoded.stream), | 2207 | ffprobe_size(&encoded.stream), |
| 2195 | - (padded_width as u32, padded_height as u32), | 2208 | + (width as u32, height as u32), |
| 2196 | - "{width}x{height}: ffprobe reports the coded (padded) size" | 2209 | + "{width}x{height}: ffprobe reports the true (display) size, not the padded one" |
| 2197 | ); | 2210 | ); |
| 2198 | - let decoded = ffmpeg_decode(&encoded.stream, padded_width, padded_height); | 2211 | + let decoded = ffmpeg_decode(&encoded.stream, width, height); |
| 2199 | - let cropped = crop_picture(&decoded, width, height); | ||
| 2200 | assert_eq!( | 2212 | assert_eq!( |
| 2201 | - cropped.y, encoded.reconstruction.y, | 2213 | + decoded.y, encoded.reconstruction.y, |
| 2202 | "{width}x{height}: luma" | 2214 | "{width}x{height}: luma" |
| 2203 | ); | 2215 | ); |
| 2204 | - assert_eq!(cropped.u, encoded.reconstruction.u, "{width}x{height}: U"); | 2216 | + assert_eq!(decoded.u, encoded.reconstruction.u, "{width}x{height}: U"); |
| 2205 | - assert_eq!(cropped.v, encoded.reconstruction.v, "{width}x{height}: V"); | 2217 | + assert_eq!(decoded.v, encoded.reconstruction.v, "{width}x{height}: V"); |
| 2206 | } | 2218 | } |
| 2207 | } | 2219 | } |
| 2208 | 2220 | ||
| @@ -2362,24 +2374,18 @@ mod tests { | |||
| 2362 | ); | 2374 | ); |
| 2363 | } | 2375 | } |
| 2364 | // See `a_frame_round_trips_at_its_own_size`: `ffprobe` reports the | 2376 | // See `a_frame_round_trips_at_its_own_size`: `ffprobe` reports the |
| 2365 | - // coded (superblock-padded) size a sequence's frames share, not the | 2377 | + // true (display) size a sequence's frames share, not the padded one. |
| 2366 | - // render size. | ||
| 2367 | - let (padded_width, padded_height) = ( | ||
| 2368 | - width.next_multiple_of(SUPERBLOCK), | ||
| 2369 | - height.next_multiple_of(SUPERBLOCK), | ||
| 2370 | - ); | ||
| 2371 | assert_eq!( | 2378 | assert_eq!( |
| 2372 | ffprobe_size(&encoded.stream), | 2379 | ffprobe_size(&encoded.stream), |
| 2373 | - (padded_width as u32, padded_height as u32), | 2380 | + (width as u32, height as u32), |
| 2374 | - "sequence: ffprobe reports the coded (padded) size" | 2381 | + "sequence: ffprobe reports the true (display) size" |
| 2375 | ); | 2382 | ); |
| 2376 | - let decoded = ffmpeg_decode_sequence(&encoded.stream, padded_width, padded_height, 3); | 2383 | + let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, 3); |
| 2377 | assert_eq!(decoded.len(), 3, "decoded frame count"); | 2384 | assert_eq!(decoded.len(), 3, "decoded frame count"); |
| 2378 | for (i, (frame, decoded)) in encoded.frames.iter().zip(&decoded).enumerate() { | 2385 | for (i, (frame, decoded)) in encoded.frames.iter().zip(&decoded).enumerate() { |
| 2379 | - let cropped = crop_picture(decoded, width, height); | 2386 | + assert_eq!(decoded.y, frame.reconstruction.y, "frame {i}: luma"); |
| 2380 | - assert_eq!(cropped.y, frame.reconstruction.y, "frame {i}: luma"); | 2387 | + assert_eq!(decoded.u, frame.reconstruction.u, "frame {i}: U"); |
| 2381 | - assert_eq!(cropped.u, frame.reconstruction.u, "frame {i}: U"); | 2388 | + assert_eq!(decoded.v, frame.reconstruction.v, "frame {i}: V"); |
| 2382 | - assert_eq!(cropped.v, frame.reconstruction.v, "frame {i}: V"); | ||
| 2383 | } | 2389 | } |
| 2384 | } | 2390 | } |
| 2385 | 2391 | ||
crates/ec-av1/src/encoder.rsmodified+2 -2
| @@ -427,8 +427,8 @@ mod tests { | |||
| 427 | assert_eq!(*key, expect_key[t], "picture {t}: ffprobe key_frame flag"); | 427 | assert_eq!(*key, expect_key[t], "picture {t}: ffprobe key_frame flag"); |
| 428 | assert_eq!( | 428 | assert_eq!( |
| 429 | (*w, *h), | 429 | (*w, *h), |
| 430 | - (coded_w as u32, coded_h as u32), | 430 | + (width as u32, height as u32), |
| 431 | - "picture {t}: coded size" | 431 | + "picture {t}: ffprobe reports the true (display) size, not the padded coded one" |
| 432 | ); | 432 | ); |
| 433 | } | 433 | } |
| 434 | } | 434 | } |
crates/ec-av1/src/tile.rsmodified+20 -14
| @@ -35,6 +35,16 @@ const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; | |||
| 35 | 35 | ||
| 36 | /// Mode-info units across a 32x32 block. | 36 | /// Mode-info units across a 32x32 block. |
| 37 | const BLOCK_MI: u32 = SB_MI / 2; | 37 | const BLOCK_MI: u32 = SB_MI / 2; |
| 38 | + | ||
| 39 | +/// The number of 32x32 block columns/rows a frame whose *true* (unpadded) | ||
| 40 | +/// size gives `mi_cols`/`mi_rows` is coded over: every block whose mi origin | ||
| 41 | +/// (`col * BLOCK_MI`) is inside the true bound is coded, so this is a | ||
| 42 | +/// ceiling, not the exact division a block-aligned frame would give. Shared | ||
| 43 | +/// by the tile writer's own iteration and by [`crate::encode`]'s block/ | ||
| 44 | +/// superblock generation, which must agree with it exactly. | ||
| 45 | +pub(crate) fn block_grid(mi_cols: u32, mi_rows: u32) -> (u32, u32) { | ||
| 46 | + (mi_cols.div_ceil(BLOCK_MI), mi_rows.div_ceil(BLOCK_MI)) | ||
| 47 | +} | ||
| 38 | /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode. | 48 | /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode. |
| 39 | const DC_PRED: usize = 0; | 49 | const DC_PRED: usize = 0; |
| 40 | 50 | ||
| @@ -479,22 +489,18 @@ struct Neighbour { | |||
| 479 | dc: Option<bool>, | 489 | dc: Option<bool>, |
| 480 | } | 490 | } |
| 481 | 491 | ||
| 482 | -/// Rejects a frame the 32x32 block grid cannot tile. | 492 | +/// Rejects a frame with no mode-info grid at all. |
| 483 | /// | 493 | /// |
| 484 | -/// The frame need not be a whole number of superblocks — a superblock at the | 494 | +/// `mi_cols`/`mi_rows` are the frame's *true* (unpadded) size in 4x4 units |
| 485 | -/// right-hand or bottom edge may be half outside it — but every 32x32 block | 495 | +/// (spec `compute_image_size`), not necessarily a multiple of the 32x32 block |
| 486 | -/// that is coded has to be wholly inside, because a block that hangs over the | 496 | +/// grid: a block whose origin sits at or past this bound is not coded (spec's |
| 487 | -/// edge has to be coded as a rectangle this writer has no transform for. | 497 | +/// `decode_partition` never visits it), and one that straddles the bound is |
| 498 | +/// coded whole, its samples coming from the padded planes. | ||
| 488 | fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> { | 499 | fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> { |
| 489 | - if mi_cols == 0 | 500 | + if mi_cols == 0 || mi_rows == 0 { |
| 490 | - || mi_rows == 0 | ||
| 491 | - || !mi_cols.is_multiple_of(BLOCK_MI) | ||
| 492 | - || !mi_rows.is_multiple_of(BLOCK_MI) | ||
| 493 | - { | ||
| 494 | return Err(Error::unsupported( | 501 | return Err(Error::unsupported( |
| 495 | "AV1 tile", | 502 | "AV1 tile", |
| 496 | - "a coefficient key frame is written only for frames that are a \ | 503 | + "a coefficient key frame needs a nonzero mode-info grid", |
| 497 | - whole number of 32x32 blocks", | ||
| 498 | )); | 504 | )); |
| 499 | } | 505 | } |
| 500 | Ok(()) | 506 | Ok(()) |
| @@ -685,7 +691,7 @@ pub fn sb_coeff_key_frame_tile( | |||
| 685 | superblocks: &[Superblock], | 691 | superblocks: &[Superblock], |
| 686 | ) -> Result<Vec<u8>> { | 692 | ) -> Result<Vec<u8>> { |
| 687 | check_blocks(mi_cols, mi_rows)?; | 693 | check_blocks(mi_cols, mi_rows)?; |
| 688 | - let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); | 694 | + let (cols, rows) = block_grid(mi_cols, mi_rows); |
| 689 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | 695 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 690 | if superblocks.len() != (sb_cols * sb_rows) as usize { | 696 | if superblocks.len() != (sb_cols * sb_rows) as usize { |
| 691 | return Err(Error::unsupported( | 697 | return Err(Error::unsupported( |
| @@ -988,7 +994,7 @@ pub fn split_coeff_key_frame_tile( | |||
| 988 | blocks: &[BlockCoeffs], | 994 | blocks: &[BlockCoeffs], |
| 989 | ) -> Result<Vec<u8>> { | 995 | ) -> Result<Vec<u8>> { |
| 990 | check_blocks(mi_cols, mi_rows)?; | 996 | check_blocks(mi_cols, mi_rows)?; |
| 991 | - let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); | 997 | + let (cols, rows) = block_grid(mi_cols, mi_rows); |
| 992 | if blocks.len() != (cols * rows) as usize { | 998 | if blocks.len() != (cols * rows) as usize { |
| 993 | return Err(Error::unsupported( | 999 | return Err(Error::unsupported( |
| 994 | "AV1 tile", | 1000 | "AV1 tile", |