use crate::bitstream::{rbsp_trailing_bits, BitWriter};
use crate::h264::params::{H264Config, MB_SIZE};
pub struct PaddedYuv<'a> {
pub y: &'a [u8],
pub u: &'a [u8],
pub v: &'a [u8],
pub coded_width: u32,
pub coded_height: u32,
}
const MB_TYPE_I_PCM: u32 = 25;
pub fn idr_slice_rbsp(cfg: &H264Config, yuv: &PaddedYuv) -> Vec<u8> {
let mut w = BitWriter::new();
w.write_ue(0); w.write_ue(7); w.write_ue(0); w.write_bits(0, 4); w.write_ue(0); w.flag(false); w.flag(false); w.write_se(0);
let cw = yuv.coded_width;
let ch = yuv.coded_height;
let cwc = cw / 2;
let half = MB_SIZE / 2;
let (nx, ny) = (cw / MB_SIZE, ch / MB_SIZE);
for mb_y in 0..ny {
for mb_x in 0..nx {
let cx = mb_x * MB_SIZE;
let cy = mb_y * MB_SIZE;
w.write_ue(MB_TYPE_I_PCM);
w.align_zero(); emit_block(&mut w, yuv.y, cw, cx, cy, MB_SIZE);
let (ccx, ccy) = (cx / 2, cy / 2);
emit_block(&mut w, yuv.u, cwc, ccx, ccy, half);
emit_block(&mut w, yuv.v, cwc, ccx, ccy, half);
}
}
let _ = cfg;
rbsp_trailing_bits(&mut w);
w.finish()
}
#[inline]
fn emit_block(w: &mut BitWriter, plane: &[u8], stride: u32, x0: u32, y0: u32, size: u32) {
for yy in 0..size {
let row = ((y0 + yy) * stride + x0) as usize;
for xx in 0..size as usize {
w.write_byte(plane[row + xx]);
}
}
}