private static unsafe void patch_slice_header(h264_sps_id_patcher_t h, bit_reader_t bs, bs_t bd) { uint first_mb_in_slice = (uint)ue_bits(bs); uint slice_type = (uint)ue_bits(bs); uint pps_id = (uint)ue_bits(bs); pps_id = (uint)h.map_pps[pps_id]; Debug.Assert(pps_id <= 255); h264e_bs_put_golomb(bd, first_mb_in_slice); h264e_bs_put_golomb(bd, slice_type); h264e_bs_put_golomb(bd, pps_id); copy_bits(bs, bd); }
private static unsafe int patch_pps(h264_sps_id_patcher_t h, bit_reader_t bs, bs_t bd, int new_pps_id, int *old_id) { int bytes; uint pps_id = (uint)ue_bits(bs); // max = 255 uint sps_id = (uint)ue_bits(bs); // max = 31 *old_id = (int)pps_id; sps_id = (uint)h.map_sps[sps_id]; pps_id = (uint)new_pps_id; Debug.Assert(sps_id <= 31); Debug.Assert(pps_id <= 255); h264e_bs_put_golomb(bd, pps_id); h264e_bs_put_golomb(bd, sps_id); copy_bits(bs, bd); bytes = (int)h264e_bs_byte_align(bd) / 8; h264e_bs_flush(bd); return(bytes); }
private static unsafe int transcode_nalu(h264_sps_id_patcher_t h, byte *src, int nalu_bytes, byte *dst) { int old_id; bit_reader_t bst = new bit_reader_t(); bs_t bdt = new bs_t(); bit_reader_t bs = new bit_reader_t(); bs_t bd = new bs_t(); int payload_type = src[0] & 31; *dst = *src; h264e_bs_init_bits(bd, dst + 1); init_bits(bs, src + 1, (uint)nalu_bytes - 1); h264e_bs_init_bits(bdt, dst + 1); init_bits(bst, src + 1, (uint)nalu_bytes - 1); switch (payload_type) { case 7: { int cb = change_sps_id(bst, bdt, 0, &old_id); int id = find_mem_cache(h.sps_cache, h.sps_bytes, MINIMP4_MAX_SPS, dst + 1, cb); if (id == -1) { return(0); } h.map_sps[old_id] = id; change_sps_id(bs, bd, id, &old_id); } break; case 8: { int cb = patch_pps(h, bst, bdt, 0, &old_id); int id = find_mem_cache(h.pps_cache, h.pps_bytes, MINIMP4_MAX_PPS, dst + 1, cb); if (id == -1) { return(0); } h.map_pps[old_id] = id; patch_pps(h, bs, bd, id, &old_id); } break; case 1: case 2: case 5: patch_slice_header(h, bs, bd); break; default: memcpy(dst, src, nalu_bytes); return(nalu_bytes); } nalu_bytes = (int)(1 + h264e_bs_byte_align(bd) / 8); h264e_bs_flush(bd); return(nalu_bytes); }