| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | use ec_core::BitWriter; |
| 14 | |
| 15 | use super::Colour; |
| 16 | use crate::params::enc::{PACKED_HEADER_PICTURE, PACKED_HEADER_SEQUENCE, PACKED_HEADER_SLICE}; |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | pub(crate) struct Packed { |
| 23 | pub(crate) kind: u32, |
| 24 | pub(crate) bytes: Vec<u8>, |
| 25 | pub(crate) bits: u32, |
| 26 | } |
| 27 | |
| 28 | |
| 29 | fn nal(kind: u32, header: &[u8], rbsp: &[u8], padding_bits: u32) -> Packed { |
| 30 | let mut bytes = vec![0, 0, 0, 1]; |
| 31 | bytes.extend_from_slice(header); |
| 32 | let mut escaped = Vec::with_capacity(rbsp.len() + 8); |
| 33 | ec_h264_syntax::escape_rbsp(rbsp, &mut escaped); |
| 34 | bytes.extend_from_slice(&escaped); |
| 35 | let bits = bytes.len() as u32 * 8 - padding_bits; |
| 36 | Packed { kind, bytes, bits } |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | pub(super) struct H264Params { |
| 45 | pub(super) level_idc: u8, |
| 46 | pub(super) mb_width: u32, |
| 47 | pub(super) mb_height: u32, |
| 48 | pub(super) crop_right: u32, |
| 49 | pub(super) crop_bottom: u32, |
| 50 | pub(super) num_units_in_tick: u32, |
| 51 | pub(super) time_scale: u32, |
| 52 | pub(super) pic_init_qp: u8, |
| 53 | pub(super) colour: Option<Colour>, |
| 54 | } |
| 55 | |
| 56 | |
| 57 | pub(super) const H264_LOG2_MAX_FRAME_NUM: u32 = 4; |
| 58 | |
| 59 | |
| 60 | pub(super) fn h264_sps(p: &H264Params) -> Packed { |
| 61 | let mut w = BitWriter::with_capacity(64); |
| 62 | w.write_bits(100, 8); |
| 63 | w.write_bits(0, 8); |
| 64 | w.write_bits(u32::from(p.level_idc), 8); |
| 65 | w.write_ue(0); |
| 66 | |
| 67 | w.write_ue(1); |
| 68 | w.write_ue(0); |
| 69 | w.write_ue(0); |
| 70 | w.write_bit(false); |
| 71 | w.write_bit(false); |
| 72 | w.write_ue(H264_LOG2_MAX_FRAME_NUM - 4); |
| 73 | w.write_ue(2); |
| 74 | w.write_ue(1); |
| 75 | w.write_bit(false); |
| 76 | w.write_ue(p.mb_width - 1); |
| 77 | w.write_ue(p.mb_height - 1); |
| 78 | w.write_bit(true); |
| 79 | w.write_bit(true); |
| 80 | let cropping = p.crop_right != 0 || p.crop_bottom != 0; |
| 81 | w.write_bit(cropping); |
| 82 | if cropping { |
| 83 | |
| 84 | w.write_ue(0); |
| 85 | w.write_ue(p.crop_right); |
| 86 | w.write_ue(0); |
| 87 | w.write_ue(p.crop_bottom); |
| 88 | } |
| 89 | w.write_bit(true); |
| 90 | w.write_bit(false); |
| 91 | w.write_bit(false); |
| 92 | w.write_bit(p.colour.is_some()); |
| 93 | if let Some(colour) = p.colour { |
| 94 | w.write_bits(5, 3); |
| 95 | w.write_bit(colour.full_range); |
| 96 | w.write_bit(true); |
| 97 | w.write_bits(u32::from(colour.primaries), 8); |
| 98 | w.write_bits(u32::from(colour.transfer), 8); |
| 99 | w.write_bits(u32::from(colour.matrix), 8); |
| 100 | } |
| 101 | w.write_bit(false); |
| 102 | w.write_bit(true); |
| 103 | w.write_bits(p.num_units_in_tick, 32); |
| 104 | w.write_bits(p.time_scale, 32); |
| 105 | w.write_bit(true); |
| 106 | w.write_bit(false); |
| 107 | w.write_bit(false); |
| 108 | w.write_bit(false); |
| 109 | w.write_bit(false); |
| 110 | trailing(&mut w); |
| 111 | nal(PACKED_HEADER_SEQUENCE, &[0x67], &w.into_bytes(), 0) |
| 112 | } |
| 113 | |
| 114 | |
| 115 | pub(super) fn h264_pps(p: &H264Params) -> Packed { |
| 116 | let mut w = BitWriter::with_capacity(16); |
| 117 | w.write_ue(0); |
| 118 | w.write_ue(0); |
| 119 | w.write_bit(true); |
| 120 | w.write_bit(false); |
| 121 | w.write_ue(0); |
| 122 | w.write_ue(0); |
| 123 | w.write_ue(0); |
| 124 | w.write_bit(false); |
| 125 | w.write_bits(0, 2); |
| 126 | w.write_se(i32::from(p.pic_init_qp) - 26); |
| 127 | w.write_se(0); |
| 128 | w.write_se(0); |
| 129 | w.write_bit(true); |
| 130 | w.write_bit(false); |
| 131 | w.write_bit(false); |
| 132 | trailing(&mut w); |
| 133 | nal(PACKED_HEADER_PICTURE, &[0x68], &w.into_bytes(), 0) |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | pub(super) fn h264_slice_header(idr: bool, frame_num: u32, idr_pic_id: u32) -> Packed { |
| 142 | let mut w = BitWriter::with_capacity(16); |
| 143 | w.write_ue(0); |
| 144 | |
| 145 | |
| 146 | w.write_ue(if idr { 7 } else { 5 }); |
| 147 | w.write_ue(0); |
| 148 | w.write_bits(frame_num, H264_LOG2_MAX_FRAME_NUM); |
| 149 | if idr { |
| 150 | w.write_ue(idr_pic_id); |
| 151 | } |
| 152 | |
| 153 | if !idr { |
| 154 | w.write_bit(false); |
| 155 | w.write_bit(false); |
| 156 | } |
| 157 | |
| 158 | if idr { |
| 159 | w.write_bit(false); |
| 160 | w.write_bit(false); |
| 161 | } else { |
| 162 | w.write_bit(false); |
| 163 | } |
| 164 | if !idr { |
| 165 | w.write_ue(0); |
| 166 | } |
| 167 | w.write_se(0); |
| 168 | w.write_ue(0); |
| 169 | w.write_se(0); |
| 170 | w.write_se(0); |
| 171 | let bits = w.bit_len() as u32; |
| 172 | let padding = (8 - bits % 8) % 8; |
| 173 | w.align_to_byte(); |
| 174 | let header = if idr { 0x65 } else { 0x61 }; |
| 175 | nal(PACKED_HEADER_SLICE, &[header], &w.into_bytes(), padding) |
| 176 | } |
| 177 | |
| 178 | |
| 179 | fn trailing(w: &mut BitWriter) { |
| 180 | w.write_bit(true); |
| 181 | w.align_to_byte(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | pub(super) mod hevc { |
| 193 | use ec_h265_syntax::{ |
| 194 | ConformanceWindow, NalHeader, NalUnitType, Pps, ProfileTierLevel, ShortTermRefPicSet, |
| 195 | SliceHeader, SliceType, Sps, Vps, |
| 196 | }; |
| 197 | |
| 198 | use super::{Colour, Packed, nal}; |
| 199 | use crate::params::enc::{PACKED_HEADER_PICTURE, PACKED_HEADER_SEQUENCE, PACKED_HEADER_SLICE}; |
| 200 | |
| 201 | |
| 202 | pub(in crate::enc) struct Params { |
| 203 | pub(in crate::enc) width: u32, |
| 204 | pub(in crate::enc) height: u32, |
| 205 | pub(in crate::enc) coded_width: u32, |
| 206 | pub(in crate::enc) coded_height: u32, |
| 207 | pub(in crate::enc) level_idc: u8, |
| 208 | pub(in crate::enc) num_units_in_tick: u32, |
| 209 | pub(in crate::enc) time_scale: u32, |
| 210 | pub(in crate::enc) init_qp: u8, |
| 211 | pub(in crate::enc) colour: Option<Colour>, |
| 212 | } |
| 213 | |
| 214 | |
| 215 | pub(in crate::enc) fn parameter_sets(p: &Params) -> (Vps, Sps, Pps) { |
| 216 | let ptl = ProfileTierLevel::main(p.level_idc); |
| 217 | let vps = Vps { |
| 218 | id: 0, |
| 219 | ptl, |
| 220 | max_dec_pic_buffering_minus1: 1, |
| 221 | max_num_reorder_pics: 0, |
| 222 | }; |
| 223 | let sps = Sps { |
| 224 | vps_id: 0, |
| 225 | id: 0, |
| 226 | chroma_format_idc: 1, |
| 227 | separate_colour_plane: false, |
| 228 | pic_width_in_luma_samples: p.coded_width, |
| 229 | pic_height_in_luma_samples: p.coded_height, |
| 230 | |
| 231 | |
| 232 | conf_win: ConformanceWindow { |
| 233 | left: 0, |
| 234 | right: (p.coded_width - p.width) / 2, |
| 235 | top: 0, |
| 236 | bottom: (p.coded_height - p.height) / 2, |
| 237 | }, |
| 238 | bit_depth_luma_minus8: 0, |
| 239 | bit_depth_chroma_minus8: 0, |
| 240 | log2_max_poc_lsb_minus4: 8, |
| 241 | max_dec_pic_buffering_minus1: 1, |
| 242 | max_num_reorder_pics: 0, |
| 243 | log2_min_cb_size_minus3: 0, |
| 244 | log2_diff_max_min_cb_size: 3, |
| 245 | log2_min_tb_size_minus2: 0, |
| 246 | log2_diff_max_min_tb_size: 3, |
| 247 | max_transform_hierarchy_depth_inter: 1, |
| 248 | max_transform_hierarchy_depth_intra: 1, |
| 249 | scaling_list_enabled: false, |
| 250 | amp_enabled: true, |
| 251 | sao_enabled: true, |
| 252 | pcm_enabled: false, |
| 253 | pcm: None, |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | num_short_term_ref_pic_sets: 0, |
| 259 | short_term_ref_pic_sets: Vec::new(), |
| 260 | long_term_ref_pics_present: false, |
| 261 | num_long_term_ref_pics_sps: 0, |
| 262 | long_term_ref_pics_sps: Vec::new(), |
| 263 | temporal_mvp_enabled: false, |
| 264 | strong_intra_smoothing: true, |
| 265 | ptl, |
| 266 | vui: Some(ec_h265_syntax::vui::VuiParameters { |
| 267 | timing: Some((p.num_units_in_tick, p.time_scale)), |
| 268 | video_signal_type: p.colour.map(|colour| { |
| 269 | ec_h265_syntax::vui::VideoSignalType { |
| 270 | video_format: 5, |
| 271 | video_full_range_flag: colour.full_range, |
| 272 | colour_description: Some(ec_h265_syntax::vui::ColourDescription { |
| 273 | colour_primaries: colour.primaries, |
| 274 | transfer_characteristics: colour.transfer, |
| 275 | matrix_coeffs: colour.matrix, |
| 276 | }), |
| 277 | } |
| 278 | }), |
| 279 | ..ec_h265_syntax::vui::VuiParameters::default() |
| 280 | }), |
| 281 | }; |
| 282 | let pps = Pps { |
| 283 | id: 0, |
| 284 | sps_id: 0, |
| 285 | init_qp_minus26: i32::from(p.init_qp) - 26, |
| 286 | num_ref_idx_l0_default_active_minus1: 0, |
| 287 | num_ref_idx_l1_default_active_minus1: 0, |
| 288 | transform_skip_enabled: true, |
| 289 | loop_filter_across_tiles_enabled: false, |
| 290 | loop_filter_across_slices_enabled: false, |
| 291 | ..Pps::default() |
| 292 | }; |
| 293 | (vps, sps, pps) |
| 294 | } |
| 295 | |
| 296 | |
| 297 | |
| 298 | fn previous_picture_set() -> ShortTermRefPicSet { |
| 299 | let mut set = ShortTermRefPicSet { |
| 300 | num_negative: 1, |
| 301 | num_positive: 0, |
| 302 | num_used_by_curr: 1, |
| 303 | ..ShortTermRefPicSet::default() |
| 304 | }; |
| 305 | set.delta_poc_s0[0] = -1; |
| 306 | set.used_s0[0] = true; |
| 307 | set |
| 308 | } |
| 309 | |
| 310 | pub(in crate::enc) fn packed_vps(vps: &Vps) -> Packed { |
| 311 | nal( |
| 312 | PACKED_HEADER_SEQUENCE, |
| 313 | &NalHeader::new(NalUnitType::Vps).to_bytes(), |
| 314 | &vps.to_rbsp(), |
| 315 | 0, |
| 316 | ) |
| 317 | } |
| 318 | |
| 319 | pub(in crate::enc) fn packed_sps(sps: &Sps) -> Packed { |
| 320 | nal( |
| 321 | PACKED_HEADER_SEQUENCE, |
| 322 | &NalHeader::new(NalUnitType::Sps).to_bytes(), |
| 323 | &sps.to_rbsp(), |
| 324 | 0, |
| 325 | ) |
| 326 | } |
| 327 | |
| 328 | pub(in crate::enc) fn packed_pps(pps: &Pps) -> Packed { |
| 329 | nal( |
| 330 | PACKED_HEADER_PICTURE, |
| 331 | &NalHeader::new(NalUnitType::Pps).to_bytes(), |
| 332 | &pps.to_rbsp(), |
| 333 | 0, |
| 334 | ) |
| 335 | } |
| 336 | |
| 337 | |
| 338 | pub(in crate::enc) fn packed_slice(sps: &Sps, pps: &Pps, keyframe: bool, poc: i32) -> Packed { |
| 339 | let nal_type = if keyframe { |
| 340 | NalUnitType::IdrWRadl |
| 341 | } else { |
| 342 | NalUnitType::TrailR |
| 343 | }; |
| 344 | let mut header = SliceHeader::intra(pps, 0); |
| 345 | header.slice_type = if keyframe { SliceType::I } else { SliceType::P }; |
| 346 | header.poc_lsb = (poc as u32) & ((1 << (sps.log2_max_poc_lsb_minus4 + 4)) - 1); |
| 347 | header.sao_luma = sps.sao_enabled; |
| 348 | header.sao_chroma = sps.sao_enabled; |
| 349 | header.five_minus_max_num_merge_cand = 0; |
| 350 | |
| 351 | |
| 352 | header.short_term_ref_pic_set_sps_flag = false; |
| 353 | header.short_term_ref_pic_set = previous_picture_set(); |
| 354 | header.loop_filter_across_slices_enabled = false; |
| 355 | let mut w = ec_core::BitWriter::with_capacity(16); |
| 356 | |
| 357 | |
| 358 | |
| 359 | header.write(&mut w, sps, pps, nal_type); |
| 360 | nal( |
| 361 | PACKED_HEADER_SLICE, |
| 362 | &NalHeader::new(nal_type).to_bytes(), |
| 363 | &w.into_bytes(), |
| 364 | 0, |
| 365 | ) |
| 366 | } |
| 367 | } |