Skip to content

Commit 75ef372

Browse files
committed
finally fixed chroma prediction!!!
1 parent a5141af commit 75ef372

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

src/h264/decoder.rs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -686,71 +686,79 @@ pub fn render_chroma_intra_prediction(
686686
}
687687
Intra_Chroma_Pred_Mode::DC => {
688688
// Section 8.3.4.1 Specification of Intra_Chroma_DC prediction mode
689-
let mut ctx = Surroundings4x4::default();
690-
for blk_idx in 0..4 {
691-
let mut blk_loc = get_4x4chroma_block_location(blk_idx);
692-
blk_loc.x += loc.x;
693-
blk_loc.y += loc.y;
694-
ctx.load(target, blk_loc, false);
689+
let offset = point_to_plain_offset(loc);
695690

696-
// Calculate the sum of all the values at the left of the current block
697-
let same_mb = get_4x4chroma_block_neighbor(blk_idx, MbNeighborName::A).1.is_none();
698-
let left_sum = if same_mb || slice.has_mb_neighbor(mb_addr, MbNeighborName::A) {
699-
Some(sum(ctx.left4()))
700-
} else {
701-
None
702-
};
691+
// Calculate the sum of all the values at the top of the current block
692+
let mut top_left = None;
693+
let mut top_right = None;
694+
if slice.has_mb_neighbor(mb_addr, MbNeighborName::B) {
695+
let target_slice = target.slice(PlaneOffset { y: offset.y - 1, ..offset });
696+
top_left = Some(sum(&target_slice[0][0..4]));
697+
top_right = Some(sum(&target_slice[0][4..8]));
698+
}
703699

704-
// Calculate the sum of all the values at the top of the current block
705-
let same_mb = get_4x4chroma_block_neighbor(blk_idx, MbNeighborName::B).1.is_none();
706-
let top_sum = if same_mb || slice.has_mb_neighbor(mb_addr, MbNeighborName::B) {
707-
Some(sum(ctx.top4()))
708-
} else {
709-
None
710-
};
700+
// Calculate the sum of all the values at the left of the current block
701+
let mut left_top = None;
702+
let mut left_bottom = None;
703+
if slice.has_mb_neighbor(mb_addr, MbNeighborName::A) {
704+
let target_slice = target.slice(PlaneOffset { x: offset.x - 1, ..offset });
705+
let mut left_column = [0u8; 8];
706+
for (idx, row) in target_slice.rows_iter().take(8).enumerate() {
707+
left_column[idx] = row[0];
708+
}
709+
left_top = Some(sum(&left_column[0..4]));
710+
left_bottom = Some(sum(&left_column[4..8]));
711+
}
711712

713+
for blk_idx in 0..4 {
712714
const DEFAULT_VALUE : u32 = 1 << 7; // = 1 << ( BitDepthC − 1 )
713715
let result = match blk_idx {
714716
0 => { // If ( xO, yO ) is equal to ( 0, 0 ) or xO and yO are greater than 0
715-
if let (Some(left), Some(top)) = (left_sum, top_sum) {
717+
if let (Some(left), Some(top)) = (left_top, top_left) {
716718
(left + top + 4) >> 3
717-
} else if let Some(s) = top_sum {
719+
} else if let Some(s) = top_left {
718720
(s + 2) >> 2
719-
} else if let Some(s) = left_sum {
721+
} else if let Some(s) = left_top {
720722
(s + 2) >> 2
721723
} else {
722724
DEFAULT_VALUE
723725
}
724726
}
725727
1 => { // If xO is greater than 0 and yO is equal to 0
726-
if let Some(s) = top_sum {
728+
if let Some(s) = top_right {
727729
(s + 2) >> 2
728-
} else if let Some(s) = left_sum {
730+
} else if let Some(s) = left_top {
729731
(s + 2) >> 2
730732
} else {
731733
DEFAULT_VALUE
732734
}
733735
}
734736
2 => { // If xO is equal to 0 and yO is greater than 0
735-
if let Some(s) = left_sum {
737+
if let Some(s) = left_bottom {
736738
(s + 2) >> 2
737-
} else if let Some(s) = top_sum {
739+
} else if let Some(s) = top_left {
738740
(s + 2) >> 2
739741
} else {
740742
DEFAULT_VALUE
741743
}
742744
}
743-
3 => {
744-
if let (Some(left), Some(top)) = (left_sum, top_sum) {
745+
3 => { // If xO is equal to 0 and yO is greater than 0
746+
if let (Some(left), Some(top)) = (left_bottom, top_right) {
745747
(left + top + 4) >> 3
748+
} else if let Some(s) = top_right {
749+
(s + 2) >> 2
750+
} else if let Some(s) = left_bottom {
751+
(s + 2) >> 2
746752
} else {
747753
DEFAULT_VALUE
748754
}
749755
}
750756
_ => unreachable!()
751757
};
752758

753-
info!(" >chroma DC blk: {blk_idx:?} left: {left_sum:?} top: {top_sum:?} sum: {result}");
759+
let mut blk_loc = get_4x4chroma_block_location(blk_idx);
760+
blk_loc.x += loc.x;
761+
blk_loc.y += loc.y;
754762
let mut target_slice = target.mut_slice(point_to_plain_offset(blk_loc));
755763
for row in target_slice.rows_iter_mut().take(4) {
756764
row[0..4].fill(result as u8);
@@ -786,7 +794,6 @@ pub fn render_chroma_intra_prediction(
786794
let c = (34 * v + 32) >> 6;
787795

788796
let offset = point_to_plain_offset(loc);
789-
info!(" >chroma Plane blk: {loc:?} A: {a:?} B: {b:?} c: {c}");
790797
let mut target_slice = target.mut_slice(offset);
791798
for (y, row) in target_slice.rows_iter_mut().take(mb_height).enumerate() {
792799
for (x, pixel) in row.iter_mut().take(mb_width).enumerate() {

0 commit comments

Comments
 (0)