def rbsp_data(bs: BitStream): from_byte = bs.pos().byte n = find_start_code(bs) to_byte = bs.pos().byte num = to_byte - from_byte - n bs.moveto(from_byte, 0) rbsp = bytearray() i = 0 while i < num: if i <= num - 3: next_3_byte = bs.read_bytes_to_array(3, forward=False) if next_3_byte[0] == 0 and next_3_byte[1] == 0 and next_3_byte[ 2] == 3: rbsp.extend([0, 0]) bs.skip(3, 0) i += 3 continue rbsp.extend(bs.read_bytes_to_array(1)) i += 1 return rbsp
def find_start_code(bs: BitStream): start_code_len = 0 try: while True: head = bs.read_bytes_to_array(4) pos = bs.pos() if head[0] == 0 and head[1] == 0 and head[2] == 1: bs.moveto(pos.byte - 1, pos.bit) start_code_len = 3 break elif head[0] == 0 and head[1] == 0 and head[2] == 0 and head[ 3] == 1: start_code_len = 4 break else: bs.moveto(pos.byte - 3, pos.bit) except EOFError as e: return 0 return start_code_len