2024年1月3日发(作者:)

byte = datas[byte_index] bit_index = 0 self._bit_count = self._zero_count + 1 if self._bit_count > 8: return False if bit_index + self._bit_count <= 8: mask = 0xff >> bit_index _encoded = (datas[byte_index] & mask) >> (8 - bit_index - self._bit_count) else: mask = 0xff >> bit_index _encoded = datas[byte_index] & mask _encoded = (_encoded << (self._bit_count - (8 - bit_index))) & 0xff byte_index += 1 _encoded += (datas[byte_index] >> (16 - bit_index - self._bit_count)) return Trueclass ExpGolombUE(ExpGolombNumber): def __init__(self, value=0): super(ExpGolombUE, self).__init__(value) def encode(self): _encoded = + 1 self._bit_count = __length() self._zero_count = self._bit_count - 1 def set_data(self, datas, start_bit_index): if super(ExpGolombUE, self).set_data(datas, start_bit_index): = _encoded - 1class ExpGolombSE(ExpGolombNumber): def __init__(self, value=0): super(ExpGolombSE, self).__init__(value) def encode(self): tmp = abs() if 0 == : self._bit_count = 1 self._zero_count = 0 _encoded = 1 return self._bit_count = _length() self._zero_count = self._bit_count tmp = tmp << 1 self._bit_count += 1 if < 0: tmp += 1 _encoded = tmp def set_data(self, datas, start_bit_index): if super(ExpGolombSE, self).set_data(datas, start_bit_index): sign = _encoded & 0x01 = _encoded >> 1

if sign: *= -1class ExpGolombCodec: def __init__(self, buffer): self.__value = 0 self.__prefix = 0 self.__buffer = buffer def __str__(self): return "prefix={}, value={}, data length={}".format(self.__prefix, bin(self.__value), len(self.__buffer)) def __add_coded(self, value): if len(self.__buffer) > 3 and value <= 0x03: if not self.__buffer[-2] and not self.__buffer[-1]: self.__(0x03) self.__(value) def __add_item(self, glbobj): self.__value |= (_encoded << 40 - self.__prefix - _count - _count) self.__prefix += _count + _count def __check_prefix(self, glbobj): if self.__prefix + _count + _count > 40: if self.__prefix >= 32: tmp = self.__value & 0xffffffff00 tmp = tmp >> 8 self.__add_coded(tmp >> 24 & 0xff) self.__add_coded(tmp >> 16 & 0xff) self.__add_coded(tmp >> 8 & 0xff) self.__add_coded(tmp & 0xff) self.__value = (self.__value << 32) & 0xff00000000 self.__prefix -= 32 elif self.__prefix >= 24: tmp = self.__value & 0xffffff0000 tmp = tmp >> 16 self.__add_coded(tmp >> 16) self.__add_coded(tmp >> 8 & 0xff) self.__add_coded(tmp & 0xff) self.__value = (self.__value << 24) & 0xff00000000 self.__prefix -= 24 elif self.__prefix >= 16: tmp = self.__value & 0xffff000000 tmp = tmp >> 24 self.__add_coded(tmp >> 8 & 0xff) self.__add_coded(tmp & 0xff) self.__value = (self.__value << 16) & 0xff00000000 self.__prefix -= 16 elif self.__prefix >= 8: tmp = self.__value & 0xff00000000 tmp = tmp >> 32 self.__add_coded(tmp & 0xff) self.__value = (self.__value << 8) & 0xff00000000

self.__value = (self.__value << 8) & 0xff00000000 self.__prefix -= 8 if 8 == self.__prefix: self.__add_coded(self.__value >> 32 & 0xff) self.__value = 0 self.__prefix = 0 def byte_aligned(self): return 0 == self.__prefix % 8 def add_trail(self): ret = self.__prefix % 8 if ret: self.__buffer[-1] |= (1 << (8 - ret - 1)) else: self.__add_coded(0x80) def reset(self): self.__value = 0 self.__prefix = 0 self.__() def encode_done(self, trail_bit): if self.__prefix > 32: self.__add_coded(self.__value >> 32 & 0xff) self.__add_coded(self.__value >> 24 & 0xff) self.__add_coded(self.__value >> 16 & 0xff) self.__add_coded(self.__value >> 8 & 0xff) self.__add_coded(self.__value & 0xff) elif self.__prefix > 24: self.__add_coded(self.__value >> 32 & 0xff) self.__add_coded(self.__value >> 24 & 0xff) self.__add_coded(self.__value >> 16 & 0xff) self.__add_coded(self.__value >> 8 & 0xff) elif self.__prefix > 16: self.__add_coded(self.__value >> 32 & 0xff) self.__add_coded(self.__value >> 24 & 0xff) self.__add_coded(self.__value >> 16 & 0xff) elif self.__prefix > 8: self.__add_coded(self.__value >> 32 & 0xff) self.__add_coded(self.__value >> 24 & 0xff) else: self.__add_coded(self.__value >> 32 & 0xff) if trail_bit: _trail() print(str(self)) self.__prefix = 0 self.__value = 0 def append(self, glbobj, dbg=False): () self.__check_prefix(glbobj) if dbg: print("before append {} : {}".format(_encoded, self)) self.__add_item(glbobj)

if dbg: print("after append {} : {}".format(_encoded, self)) def encode_ue(self, value_list): self.__value = 0 self.__prefix = 0 self.__buffer = [] for x in value_list: y = x if type(x) is not int: y = ord(x) if y > 0xff: raise Exception("must be byte: {}".format(y)) e = ExpGolombUE(y) (e)是按照H264标准封装的部分H264语法结构(暂未实现预测等关键参数),源码如下:# -*- coding: cp936 -*-# author: yangyiyin# 2022/2/14 chengduimport abcimport mathfrom golomb import *NALU_TYPE_RESERVED = 0NALU_TYPE_CODED_SLICE_NON_IDR_PIC = 1NALU_TYPE_CODED_SLICE_DATA_PARTION_A = 2NALU_TYPE_CODED_SLICE_DATA_PARTION_B = 3NALU_TYPE_CODED_SLICE_DATA_PARTION_C = 4NALU_TYPE_CODED_SLICE_IDR_PIC = 5NALU_TYPE_SUPPLEMENTAL_ENHANCEMENT_INFO = 6NALU_TYPE_SEQUENCE_PARAMETER_SET = 7NALU_TYPE_PICTURE_PARAMETER_SET = 8NALU_TYPE_ACCESS_UNIT_DELIMITER = 9NALU_TYPE_END_OF_SEQUENCE = 10NALU_TYPE_END_OF_STREAM = 11NALU_TYPE_FILLER_DATA = 12NALU_TYPE_SEQUENCE_PARAMETER_SET_EXT = 13NALU_TYPE_PREFIX_NALU = 14NALU_TYPE_SUBSET_SEQUENCE_PARAMETER_SET = 15NALU_TYPE_DEPTH_PARAMETER_SET = 16NALU_TYPE_RESERVED_1 = 17NALU_TYPE_RESERVED_2 = 18NALU_TYPE_CODED_SLICE_AUXILIARY_PIC = 19NALU_TYPE_CODED_SLICE_EXT = 20NALU_TYPE_CODED_SLICE_EXT_DEPTH_3D_AVC = 21NALU_TYPE_SINGLE_TIME_AGG_PACKET_A = 24NALU_TYPE_SINGLE_TIME_AGG_PACKET_B = 25NALU_TYPE_MULTI_TIME_AGG_PACKET_16 = 26NALU_TYPE_MULTI_TIME_AGG_PACKET_24 = 27NALU_TYPE_FRAGMENTATION_UINT_A = 28NALU_TYPE_FRAGMENTATION_UNIT_B = 29# _group_map_typeSLICE_GROUP_MAP_TYPE_INTERLEAVED = 0SLICE_GROUP_MAP_TYPE_DISPERSED_MAPPING = 1

SLICE_GROUP_MAP_TYPE_DISPERSED_MAPPING = 1SLICE_GROUP_MAP_TYPE_FOREGROUND_LEFTOVER = 2SLICE_GROUP_MAP_TYPE_CANGING = 3SLICE_GROUP_MAP_TYPE_CANGING1 = 4SLICE_GROUP_MAP_TYPE_CANGING2 = 5SLICE_GROUP_MAP_TYPE_EXPLICIT_ASSIGNMENT = 6# _format_idcCHROMA_FORMAT_MONOCHROME = 0CHROMA_FORMAT_420 = 1CHROMA_FORMAT_422 = 2CHROMA_FORMAT_444 = 3# slice typeSLICE_TYPE_P = [0, 5]SLICE_TYPE_B = [1, 6]SLICE_TYPE_I = [2, 7]SLICE_TYPE_SP = [3, 8]SLICE_TYPE_SI = [4, 9]SEI_PT_BUFFERING_PERIOD = 0SEI_PT_PIC_TIMING = 1SEI_PT_PCAN_SCAN_RECT = 2SEI_PT_FILLER_PAYLOAD = 3SEI_PT_USER_DATA_REGISTERED_ITU_T_T35 = 4SEI_PT_USER_DATA_UNREGISTERED = 5SEI_PT_RECOVERY_POINT= 6SEI_PT_DEC_REF_PIC_MARKING_REPETITION = 7SEI_PT_SPARE_PIC = 8SEI_PT_SCENE_INFO = 9SEI_PT_SUB_SEQ_IFO = 10SEI_PT_SUB_SEQ_LAYER_CHARACTERISTICS = 11SEI_PT_SUB_SEQ_CHARACTERISTICS = 12SEI_PT_FULL_FRAME_FREEZE = 13SEI_PT_FULL_FRAME_FREEZE_RELEASE = 14SEI_PT_FULL_FRAME_SNAPSHOT = 15SEI_PT_PROGRESSIVE_REFINEMENT_SEGMENT_START = 16SEI_PT_PROGRESSIVE_REFINEMENT_SEGMENT_END = 17SEI_PT_MOTION_CONSTRAINED_SLICE_GROUP_SET = 18SEI_PT_FILM_GRAIN_CHARACTERISTICS = 19SEI_PT_DEBLOCKING_FILTER_DISPLAY_PREFERENCE = 20SEI_PT_STEREO_VIDEO_INFO = 21SEI_PT_POST_FILTER_HINT = 22SEI_PT_TONE_MAPPING_INFO = 23SEI_PT_SCALABILITY_INFO = 24SEI_PT_SUB_PIC_SCALABLE_LAYER = 25SEI_PT_NON_REQUIRED_LAYER_REP = 26SEI_PT_PRIORITY_LAYER_INFO = 27SEI_PT_LAYERS_NOT_PRESENT = 28SEI_PT_LAYER_DEPENDENCY_CHANGE = 29SEI_PT_SCALABLE_NESTING = 30SEI_PT_BASE_LAYER_TEMPORAL_HRD = 31SEI_PT_QUALITY_LAYER_INTEGRITY_CHECK = 32SEI_PT_REDUNDANT_PIC_PROPERTY = 33SEI_PT_TL0_DEP_REP_INDEX = 34SEI_PT_TL_SWITCHING_POINT = 35SEI_PT_PARALLEL_DECODING_INFO = 36class RbspTrailingBits(ExpGolombObject): def __init__(self, bit_count, value): super(RbspTrailingBits, self).__init__(bit_count, value)class NALUHeader(object): def __init__(self, nalu_type):

def __init__(self, nalu_type): den_zero_bit = FixedLenUint(1) _ref_idc = FixedLenUint(2) _type = FixedLenUint(5, nalu_type)class NALU(metaclass=a): def __init__(self, nalu_type): self.__data = bytearray() = NALUHeader(nalu_type) = 0 = ExpGolombCodec(self.__data) def encode(self, trail_bit=True): () (den_zero_bit) (_ref_idc) (_type) self._encode() _done(trail_bit) = len(self.__data) @property def data(self): return self.__data @ctmethod def _encode(self): ...# HRD parameters syntaxclass HRDParameters: def __init__(self): _cnt_minus1 = ExpGolombUE() _rate_scale = FixedLenUint(4) _size_scale = FixedLenUint(4) _rate_value_minus1 = [ExpGolombUE() for x in range(_cnt_)] _size_value_minus1 = [ExpGolombUE() for x in range(_cnt_)] _flag = [FixedLenUint(1) for x in range(_cnt_)] l_cpb_removal_delay_length_minus1 = FixedLenUint(4) _removal_delay_length_minus1 = FixedLenUint(4) _output_delay_length_minus1 = FixedLenUint(4) _offset_length = FixedLenUint(4)# VUI parameters syntaxclass VUIParameters(): def __init__(self): _ratio_info_present_flag = FixedLenUint(1); _ratio_idc = FixedLenUint(8); _width = FixedLenUint(16) _height = FixedLenUint(16) an_info_present_flag = FixedLenUint(1) an_appropriate_flag = FixedLenUint(1) _signal_type_present_flag = FixedLenUint(1) _format = FixedLenUint(3) _full_range_flag = FixedLenUint(1) _description_present_flag = FixedLenUint(1) _primaries = FixedLenUint(8) er_characteristics = FixedLenUint(8) _coefficients = FixedLenUint(8) _loc_info_present_flag = FixedLenUint(1)

_sample_loc_type_top_field = ExpGolombUE() _sample_loc_type_bottom_field = ExpGolombUE() _info_present_flag = FixedLenUint(1) _units_in_tick = FixedLenUint(32) _scale = FixedLenUint(32) _frame_rate_flag = FixedLenUint(1) _hrd_parameters_present_flag = FixedLenUint(1) _hrd_parameters = HRDParameters() _hrd_parameters_present_flag = FixedLenUint(1) _hrd_parameters = HRDParameters() _delay_hrd_flag = FixedLenUint(1) _struct_present_flag = FixedLenUint(1) eam_restriction_flag = FixedLenUint(1) _vectors_over_pic_boundaries_flag = FixedLenUint(1) _bytes_per_pic_denom = ExpGolombUE() _bits_per_mb_denom = ExpGolombUE() 2_max_mv_length_horizontal = ExpGolombUE() 2_max_mv_length_vertical = ExpGolombUE() _num_reorder_frames = ExpGolombUE() _dec_frame_buffering = ExpGolombUE()class SequenceParameterSet(NALU): def __init__(self): super(SequenceParameterSet, self).__init__(NALU_TYPE_SEQUENCE_PARAMETER_SET) e_idc = FixedLenUint(8) aint_set0_flag = FixedLenUint(1) aint_set1_flag = FixedLenUint(1) aint_set2_flag = FixedLenUint(1) aint_set3_flag = FixedLenUint(1) aint_set4_flag = FixedLenUint(1) aint_set5_flag = FixedLenUint(1) ed_zero_2bits = FixedLenUint(2) _idc = FixedLenUint(8) _parameter_set_id = ExpGolombUE() _format_idc = ExpGolombUE() te_colour_plane_flag = FixedLenUint(1) _depth_luma_minus8 = ExpGolombUE() _depth_chroma_minus8 = ExpGolombUE() e_y_zero_transform_bypass_flag = FixedLenUint(1) _scaling_matrix_present_flag = FixedLenUint(1) _scaling_list_present_flag = [FixedLenUint(1) for x in range(12)] _scale = [[ExpGolombSE() for x in range(16)] for y in range(6)] + [[ExpGolombSE() for x in range(64)] for y in range(6)] 2_max_frame_num_minus4 = ExpGolombUE() _order_cnt_type = ExpGolombUE() 2_max_pic_order_cnt_lsb_minus4 = ExpGolombUE() _pic_order_always_zero_flag = FixedLenUint(1) _for_non_ref_pic = ExpGolombSE() _for_top_to_bottom_field = ExpGolombSE() _ref_frames_in_pic_order_cnt_cycle = ExpGolombUE() _for_ref_frame = [ExpGolombSE(0) for x in range(_ref_frames_in_pic_order_cnt_)] _num_ref_frames = ExpGolombUE() _in_frame_num_value_allowed_flag = FixedLenUint(1)

_width_in_mbs_minus1 = ExpGolombUE() _height_in_map_units_minus1 = ExpGolombUE() _mbs_only_flag = FixedLenUint(1) _adaptive_frame_field_flag = FixedLenUint(1) _8x8_inference_flag = FixedLenUint(1) _cropping_flag = FixedLenUint(1) _crop_left_offset = ExpGolombUE() _crop_right_offset = ExpGolombUE() _crop_top_offset = ExpGolombUE() _crop_bottom_offset = ExpGolombUE() _parameters_present_flag = FixedLenUint(1) _parameters = VUIParameters() # Table 6-1 – SubWidthC, and SubHeightC values derived from chroma_format_idc and separate_colour_plane_flag def get_sub_width_height_c(self): if CHROMA_FORMAT_MONOCHROME == _format_ or te_colour_plane_: return 0, 0 if CHROMA_FORMAT_420 == _format_: return 2, 2 elif CHROMA_FORMAT_422 == _format_: return 2, 1 elif CHROMA_FORMAT_444 == _format_: return 1, 1 return None, None def _encode(self): (e_idc) (aint_set0_flag) (aint_set1_flag) (aint_set2_flag) (aint_set3_flag) (aint_set4_flag) (aint_set5_flag) (ed_zero_2bits) (_idc) (_parameter_set_id) if e_ in [100, 110, 122, 244, 44, 83, 86, 118, 128, 138, 139, 134, 135]: (_format_idc) if _format_ == CHROMA_FORMAT_444: (te_colour_plane_flag) (_depth_luma_minus8) (_depth_chroma_minus8) (e_y_zero_transform_bypass_flag) (_scaling_matrix_present_flag) count = 8 if _format_ != CHROMA_FORMAT_444 else 12 if _scaling_matrix_present_: for index, flag in enumerate(_scaling_list_present_flag[0:count]): (flag) if not : continue last_scale = 8 next_scale = 8

for delta_scale in _scale[index]: (delta_scale) next_scale = divmod(last_scale + delta_ + 256, 256)[1] last_scale = last_scale if 0 == next_scale else next_scale if not next_scale: print("next_scal is zero, skip append glb item.") break (2_max_frame_num_minus4) (_order_cnt_type) if _order_cnt_ == 0: (2_max_pic_order_cnt_lsb_minus4) else: (_pic_order_always_zero_flag) (_for_non_ref_pic) (_for_top_to_bottom_field) (_ref_frames_in_pic_order_cnt_cycle) for x in _for_ref_frame: (x) (_num_ref_frames) (_in_frame_num_value_allowed_flag) (_width_in_mbs_minus1) (_height_in_map_units_minus1) (_mbs_only_flag) if not _mbs_only_: (_adaptive_frame_field_flag) (_8x8_inference_flag) (_cropping_flag) if _cropping_: (_crop_left_offset) (_crop_right_offset) (_crop_top_offset) (_crop_bottom_offset) (_parameters_present_flag) if _parameters_present_: self.__add_vui_params() def __add_vui_params(self): (__ratio_info_present_flag) if __ratio_info_present_: (__ratio_idc) (__width) (__height) (_an_info_present_flag) if _an_info_present_: (_an_appropriate_flag) (__signal_type_present_flag) if __signal_type_present_: (__format) (__full_range_flag)

(__description_present_flag) if __description_present_: (__primaries) (_er_characteristics) (__coefficients) (__loc_info_present_flag) if __loc_info_present_: (__sample_loc_type_top_field) (__sample_loc_type_bottom_field) (__info_present_flag) if __info_present_: (__units_in_tick) (__scale) (__frame_rate_flag) (__hrd_parameters_present_flag) if __hrd_parameters_present_: self.__add_hrd_params(__hrd_parameters) (__hrd_parameters_present_flag) if __hrd_parameters_present_: self.__add_hrd_params(__hrd_parameters) if __hrd_parameters_present_ or __hrd_parameters_present_: (__delay_hrd_flag) (__struct_present_flag) (_eam_restriction_flag) if _eam_restriction_: (__vectors_over_pic_boundaries_flag) (__bytes_per_pic_denom) (__bits_per_mb_denom) (_2_max_mv_length_horizontal) (_2_max_mv_length_vertical) (__num_reorder_frames) (__dec_frame_buffering) def __add_hrd_params(self, hrd): (_cnt_minus1) (_rate_scale) (_size_scale) for x in _cnt_minus1: (_rate_value_minus1[x]) (_size_value_minus1[x]) ((_flag[x])) (l_cpb_removal_delay_length_minus1) (_removal_delay_length_minus1) (_output_delay_length_minus1) (_offset_length)class PictureParameterSet(NALU): def __init__(self): super(PictureParameterSet, self).__init__(NALU_TYPE_PICTURE_PARAMETER_SET) _parameter_set_id = ExpGolombUE() _parameter_set_id = ExpGolombUE()

y_coding_mode_flag = FixedLenUint(1) _field_pic_order_in_frame_present_flag = FixedLenUint(1) _slice_groups_minus1 = ExpGolombUE() _group_map_type = ExpGolombUE() _length_minus1 = [ExpGolombUE()] * (_slice_groups_ + 1) _left = [ExpGolombUE()] * _slice_groups_ _right = [ExpGolombUE()] * _slice_groups_ _group_change_direction_flag = FixedLenUint(1) _group_change_rate_minus1 = ExpGolombUE() _size_in_map_units_minus1 = ExpGolombUE() bit_count = ( 2(_slice_groups_ + 1)) _group_id = [FixedLenUint(bit_count)] * (_size_in_map_units_ + 1) _ref_idx_l0_default_active_minus1 = ExpGolombUE() _ref_idx_l1_default_active_minus1 = ExpGolombUE() ed_pred_flag = FixedLenUint(1) ed_bipred_idc = FixedLenUint(2) _init_qp_minus26 = ExpGolombSE() _init_qs_minus26 = ExpGolombSE() _qp_index_offset = ExpGolombSE() king_filter_control_present_flag = FixedLenUint(1) ained_intra_pred_flag = FixedLenUint(1) ant_pic_cnt_present_flag = FixedLenUint(1) orm_8x8_mode_flag = FixedLenUint(1) _scaling_matrix_present_flag = FixedLenUint(1) _chroma_qp_index_offset = ExpGolombSE() def _encode(self): (_parameter_set_id) (_parameter_set_id) (y_coding_mode_flag) (_field_pic_order_in_frame_present_flag) (_slice_groups_minus1) if _slice_groups_ > 0: (_group_map_type) if SLICE_GROUP_MAP_TYPE_INTERLEAVED == _group_map_: for x in _length_minus1: (x) elif SLICE_GROUP_MAP_TYPE_FOREGROUND_LEFTOVER == _group_map_: for x in range(_slice_groups_): (_left[x]) (_right[x]) elif _group_map_ in [SLICE_GROUP_MAP_TYPE_CANGING, SLICE_GROUP_MAP_TYPE_CANGING1, SLICE_GROUP_MAP_TYPE_ (_group_change_direction_flag) (_group_change_rate_minus1) elif SLICE_GROUP_MAP_TYPE_EXPLICIT_ASSIGNMENT == _group_map_: (_size_in_map_units_minus1) for x in _group_id: (x) (_ref_idx_l0_default_active_minus1) (_ref_idx_l1_default_active_minus1) (ed_pred_flag) (ed_bipred_idc) (_init_qp_minus26) (_init_qs_minus26)

(_init_qs_minus26) (_qp_index_offset) (king_filter_control_present_flag) (ained_intra_pred_flag) (ant_pic_cnt_present_flag) (orm_8x8_mode_flag) (_scaling_matrix_present_flag) (_chroma_qp_index_offset) # print("NALU:", repr())class RefPicListModification: def __init__(self, slice_type): _type = slice_type _pic_list_modification_flag_10 = FixedLenUint(1) def encode(self, glb): if _type % 5 not in [2, 4]: (_pic_list_modification_flag_10)class DecRefPicMarking: def __init__(self, idr_pic_flag=False): _pic_flag = idr_pic_flag _output_of_prior_pics_flag = FixedLenUint(1) _term_refreence_flag = FixedLenUint(1) ve_ref_pic_marking_mode_flag = FixedLenUint(1) def encode(self, codec): if _pic_flag: (_output_of_prior_pics_flag) (_term_refreence_flag) else: (ve_ref_pic_marking_mode_flag)class SliceHeader: def __init__(self, nalu_header: NALUHeader, sps: SequenceParameterSet, pps: PictureParameterSet): _header = nalu_header = sps = pps _flag = (NALU_TYPE_CODED_SLICE_IDR_PIC == nalu__) _mb_in_slice = ExpGolombUE() _type = ExpGolombUE() _parameter_set_id = ExpGolombUE() _plane_id = FixedLenUint(2) # used as an identifier for pictures and shall be represented by log2_max_frame_num_minus4 + 4 bits in the bitstream _num = FixedLenUint(2_max_frame_num_ + 4) _pic_flag = FixedLenUint(1) _field_flag = FixedLenUint(1) _pic_id = ExpGolombUE() # The length of the pic_order_cnt_lsb syntax element is log2_max_pic_order_cnt_lsb_minus4 + 4 bits _order_cnt_lsb = FixedLenUint(2_max_pic_order_cnt_lsb_ + 4) _pic_order_cnt_bottom = ExpGolombSE() _pic_order_cnt = [ExpGolombSE(), ExpGolombSE()] ant_pic_cnt = ExpGolombUE() _spatial_mv_pred_flag = FixedLenUint(1) _ref_idx_active_override_flag = FixedLenUint(1)

_ref_idx_active_override_flag = FixedLenUint(1) _ref_idx_l0_active_minus1 = ExpGolombUE() _ref_idx_l1_active_minus1 = ExpGolombUE() _init_idc = ExpGolombUE() _qp_delta = ExpGolombSE() _for_switch_flag = FixedLenUint(1) _qs_delta = ExpGolombSE() e_deblocking_filter_idc = ExpGolombUE() _alpha_c0_offset_div2 = ExpGolombSE() _beta_offset_div2 = ExpGolombSE() _group_change_cycle = ExpGolombUE() def encode(self, codec: ExpGolombCodec): (_mb_in_slice) (_type) (_parameter_set_id) if 1 == te_colour_plane_: (_plane_id) # If the current picture is an IDR picture, frame_num shall be equal to 0 (_num) if not _mbs_only_: (_pic_flag) if _pic_: (_field_flag) if _flag: (_pic_id) if 0 == _order_cnt_: (_order_cnt_lsb) if _field_pic_order_in_frame_present_ and not _pic_: (_pic_order_cnt_bottom) if 1 == _order_cnt_ and not _pic_order_always_zero_: (_pic_order_cnt[0]) if _field_pic_order_in_frame_present_ and not _pic_: (_pic_order_cnt[1]) if ant_pic_cnt_present_: (ant_pic_cnt) st = _ if st in SLICE_TYPE_P or st in SLICE_TYPE_SP or st in SLICE_TYPE_B: (_ref_idx_active_override_flag) if _ref_idx_active_override_: (_ref_idx_l0_active_minus1) if st in SLICE_TYPE_B: (_ref_idx_l1_active_minus1) if 0: ... else: ref = RefPicListModification(_) (codec) if __ref_: drpm = DecRefPicMarking(_flag) (codec)

if y_coding_mode_ and st not in SLICE_TYPE_I and st not in SLICE_TYPE_SI: (_init_idc) (_qp_delta) if st in SLICE_TYPE_SP or st in SLICE_TYPE_SI: if st in SLICE_TYPE_SP: (_for_switch_flag) (_qs_delta) if king_filter_control_present_: (e_deblocking_filter_idc) if e_deblocking_filter_ > 2: raise Exception("disable_deblocking_filter_idc should be in range of 0 to 2.") if e_deblocking_filter_ != 1: (_alpha_c0_offset_div2) (_beta_offset_div2) if _slice_groups_ > 0 and 3 <= _group_map_ <= 5: (_group_change_cycle)class SliceData: def __init__(self, mb_type, sps: SequenceParameterSet, pps: PictureParameterSet, sh: SliceHeader): = sps = pps y_coding_mode_flag = y_coding_mode_ _mb_in_slice = _mb_in_ _type = _ # MbaffFrameFlag = ( mb_adaptive_frame_field_flag && !field_pic_flag ) _frame_flag = (_adaptive_frame_field_ and not _pic_) # PicWidthInMbs = pic_width_in_mbs_minus1 + 1 _width_in_mbs = _width_in_mbs_ + 1 # PicHeightInMapUnits = pic_height_in_map_units_minus1 + 1 _height_in_map_units = _height_in_map_units_ + 1 # FrameHeightInMbs = ( 2 - frame_mbs_only_flag ) * PicHeightInMapUnits _height_in_mbs = (2 - _mbs_only_) * _height_in_map_units # PicHeightInMbs = FrameHeightInMbs / ( 1 + field_pic_flag ) _height_in_mbs = _height_in_mbs / (1 + _pic_) # PicSizeInMbs = PicWidthInMbs * PicHeightInMbs _size_in_mbs = _width_in_mbs * _height_in_mbs # PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits _size_in_map_units = _width_in_mbs * _height_in_map_units _skip_run = ExpGolombUE() _unit_to_slice_group_map = [] lock_layer = [MacroBlockLayer(mb_type, , ) for x in range(2560)] def encode(self, codec): if y_coding_mode_: # cabac_alignment_one_bit while not _aligned(): (FixedLenUint(1, 1)) for x in lock_layer:

for x in lock_layer: (codec) def next_mb_address(self, curr_mb_address): i = curr_mb_address + 1 while i < _size_in_mbs: # and MbToSliceGroupMap[i] != MbToSliceGroupMap[curr_mb_address] i = + 1 return i def get_map_unit_to_slice_group_map(self): if SLICE_GROUP_MAP_TYPE_INTERLEAVED == _group_map_type: group = 0 i = 0 for group in range(_slice_groups_ + 1): if i < _size_in_map_units: for j in range(_length_minus1[group].value + 1): if i + j < _size_in_map_units: _unit_to_slice_group_map[i + j] = group j += 1 i += _length_minus1[group].value + 1class MacroBlockLayer: def __init__(self, mb_type, sps, pps: PictureParameterSet): = pps _type = ExpGolombUE(mb_type) # bit_depth_luma_minus8 shall be in the range of 0 to 6 _depth_luma_minus8 = _depth_luma_ assert (0 <= _depth_luma_minus8 <= 6) # bit_depth_chroma_minus8 shall be in the range of 0 to 6 _depth_chroma_minus8 = _depth_luma_ assert (0 <= _depth_luma_minus8 <= 6) _width_c, _height_c = _sub_width_height_c() if _width_c and _height_c: _width_c = int(16 / _width_c) _height_c = int(16 / _height_c) else: _width_c = 0 _height_c = 0 # pcm_sample_luma bit count _depth_y = 8 + _depth_luma_minus8 _bd_offset_y = 6 + _depth_luma_minus8 #if 25 == _: # each macroblock is comprised of one 16x16 luma array _sample_luma = [FixedLenUint(_depth_y) for x in range(256)] if 25 == _ else [] # pcm_sample_chroma bit count _depth_c = 8 + _depth_chroma_minus8 _sample_chroma = [FixedLenUint(_depth_c) for x in range(2 * _width_c * _height_c)] if 25 == _ else [] orm_size_8x8_flag = FixedLenUint(1) _block_pattern = ExpGolombUE(0) def encode(self, codec: ExpGolombCodec): (_type) if 25 == _: # pcm_alignment_zero_bit while not _aligned(): (FixedLenUint(1, 0))

(FixedLenUint(1, 0)) for x in _sample_luma: (x) for x in _sample_chroma: (x) else: if orm_8x8_mode_ and 0 == _: (orm_size_8x8_flag) mb_pred = MacroblockPrediction() mb_(codec) (_block_pattern)class MacroblockPrediction: def __init__(self, chroma_array_type=0): _blk_count = 4 iter = range(_blk_count) _pred_mode_flag = [FixedLenUint(1) for x in iter] _pred_mode = [FixedLenUint(3) for x in iter] _array_type = chroma_array_type _chroma_pred_mode = ExpGolombUE() def encode(self, codec): #for x in range(_blk_count): # (_pred_mode_flag[x]) # if _pred_mode_flag[x].value: # (_pred_mode[x]) #(_chroma_pred_mode) ...class SliceLayerWithoutPartitioningRBSP(NALU): def __init__(self, nalu_type, mb_type, sps, pps): super(SliceLayerWithoutPartitioningRBSP, self).__init__(nalu_type) = SliceHeader(, sps, pps) self.d = SliceData(mb_type, sps, pps, ) def _encode(self): () ()class UserDataUnregistered: def __init__(self,): _iso_iec_11587 = [FixedLenUint(32) for x in range(4)] _data_payload_byte = ""class SupplementalEnhancementInformation(NALU): def __init__(self,): super(SupplementalEnhancementInformation, self).__init__(NALU_TYPE_SUPPLEMENTAL_ENHANCEMENT_INFO) _payload_type_byte = FixedLenUint(8) d_size = 0 _payload_size_byte = FixedLenUint(8) _data_unregistered = UserDataUnregistered() def _encode(self): (_payload_type_byte) if SEI_PT_USER_DATA_UNREGISTERED == _payload_type_:

if SEI_PT_USER_DATA_UNREGISTERED == _payload_type_: d_size = 16 + len(_data__data_payload_byte) d, _payload_size_ = divmod(d_size, 255) for x in range(d): (FixedLenUint(8, 255)) (_payload_size_byte) if SEI_PT_USER_DATA_UNREGISTERED == _payload_type_: for x in _data__iso_iec_11587: (x) for x in _data__data_payload_byte: (FixedLenUint(8, ord(x)))源码如下:# author: yangyiyin# 2022/2/14 chengduimport randomfrom avc import *f = open("test", "wb")sps = SequenceParameterSet()pps = PictureParameterSet()sei = SupplementalEnhancementInformation()_ref_ = e_ = _ = _format_ = CHROMA_FORMAT_2_max_pic_order_cnt_lsb_ = _ref_frames_in_pic_order_cnt_ = _width_in_mbs_ = _height_in_map_units_ = _mbs_only_ = _8x8_inference_ = _parameters_present_ = __signal_type_present_ = __ = __full_range_ = __info_present_ = __units_in_ = __ = _eam_restriction_ = _2_max_mv_length_ = _2_max_mv_length_ = __num_reorder_ = __dec_frame_ = ()print()_ref_ = y_coding_mode_ = _ref_idx_l0_default_active_ = ed_pred_ = ed_bipred_ = _init_qp_ = -_qp_index_ = -king_filter_control_present_ = orm_8x8_mode_ = _chroma_qp_index_ = -()print()#_ref_ = 3