def color_spec(state, video_parameters): """(11.4.10.1)""" custom_color_spec_flag = read_bool(state) # (C.3) Check level allows custom color specs ## Begin not in spec assert_level_constraint(state, "custom_color_spec_flag", custom_color_spec_flag) ## End not in spec if custom_color_spec_flag: index = read_uint(state) # (11.4.10.1) Index should be a supported value assert_in_enum(index, PresetColorSpecs, BadPresetColorSpec) ## Not in spec # (C.3) Check level allows the specified preset assert_level_constraint(state, "color_spec_index", index) ## Not in spec preset_color_spec(video_parameters, index) if index == 0: color_primaries(state, video_parameters) color_matrix(state, video_parameters) transfer_function(state, video_parameters) ## Begin not in spec else: # (11.2.2) Color spec must be supported by current version minimum_required_version = preset_color_spec_version_implication(index) if state["major_version"] < minimum_required_version: raise PresetColorSpecNotSupportedByVersion( index, state["major_version"] ) log_version_lower_bound(state, minimum_required_version)
def transfer_function(state, video_parameters): """(11.4.10.4)""" custom_transfer_function_flag = read_bool(state) # (C.3) Check level allows custom transfer functions ## Begin not in spec assert_level_constraint( state, "custom_transfer_function_flag", custom_transfer_function_flag ) ## End not in spec if custom_transfer_function_flag: index = read_uint(state) # (11.4.10.3) Index should be a supported value ## Begin not in spec assert_in_enum(index, PresetTransferFunctions, BadPresetTransferFunction) ## End not in spec # (C.3) Check level allows the specified preset ## Begin not in spec assert_level_constraint(state, "transfer_function_index", index) ## End not in spec # (11.2.2) Preset must be supported by current version ## Begin not in spec minimum_required_version = preset_transfer_function_version_implication(index) if state["major_version"] < minimum_required_version: raise PresetTransferFunctionNotSupportedByVersion( index, state["major_version"] ) log_version_lower_bound(state, minimum_required_version) ## End not in spec preset_transfer_function(video_parameters, index)
def scan_format(state, video_parameters): """(11.4.5)""" custom_scan_format_flag = read_bool(state) # (C.3) Check level allows custom scan formats ## Begin not in spec assert_level_constraint(state, "custom_scan_format_flag", custom_scan_format_flag) ## End not in spec if custom_scan_format_flag: video_parameters["source_sampling"] = read_uint(state) # (11.4.5) Mode be a known value ## Begin not in spec assert_in_enum( video_parameters["source_sampling"], SourceSamplingModes, BadSourceSamplingMode, ) ## End not in spec # (C.3) Check level allows this value ## Begin not in spec assert_level_constraint( state, "source_sampling", video_parameters["source_sampling"], )
def color_diff_sampling_format(state, video_parameters): """(11.4.4)""" custom_color_diff_format_flag = read_bool(state) # (C.3) Check level allows custom color difference sampling ## Begin not in spec assert_level_constraint( state, "custom_color_diff_format_flag", custom_color_diff_format_flag ) ## End not in spec if custom_color_diff_format_flag: video_parameters["color_diff_format_index"] = read_uint(state) # (11.4.4) Index shall be a known value ## Begin not in spec assert_in_enum( video_parameters["color_diff_format_index"], ColorDifferenceSamplingFormats, BadColorDifferenceSamplingFormat, ) ## End not in spec # (C.3) Check level allows this value ## Begin not in spec assert_level_constraint( state, "color_diff_format_index", video_parameters["color_diff_format_index"], )
def frame_size(state, video_parameters): """(11.4.3)""" custom_dimensions_flag = read_bool(state) # (C.3) Check level allows this value ## Begin not in spec assert_level_constraint(state, "custom_dimensions_flag", custom_dimensions_flag) ## End not in spec if custom_dimensions_flag: video_parameters["frame_width"] = read_uint(state) # (C.3) Check level allows this value ## Begin not in spec assert_level_constraint(state, "frame_width", video_parameters["frame_width"]) ## End not in spec video_parameters["frame_height"] = read_uint(state) # (C.3) Check level allows this value ## Begin not in spec assert_level_constraint(state, "frame_height", video_parameters["frame_height"]) ## End not in spec # Errata: spec doesn't prevent zero-pixel pictures # # (11.4.3) Frames must be at least one pixel wide and tall ## Begin not in spec if ( video_parameters["frame_width"] == 0 or video_parameters["frame_height"] == 0 ): raise ZeroPixelFrameSize( video_parameters["frame_width"], video_parameters["frame_height"], )
def extended_transform_parameters(state): """(12.4.4.1)""" asym_transform_index_flag = read_bool(state) # (C.3) Check level allows an asymmetric transform types ## Begin not in spec assert_level_constraint(state, "asym_transform_index_flag", asym_transform_index_flag) ## End not in spec if asym_transform_index_flag: state["wavelet_index_ho"] = read_uint(state) # (12.4.4.2) Check wavelet type is supported ## Begin not in spec assert_in_enum(state["wavelet_index_ho"], WaveletFilters, BadHOWaveletIndex) ## End not in spec # (C.3) Check level allows this wavelet type ## Begin not in spec assert_level_constraint(state, "wavelet_index_ho", state["wavelet_index_ho"]) ## End not in spec asym_transform_flag = read_bool(state) # (C.3) Check level allows an asymmetric transform depths ## Begin not in spec assert_level_constraint(state, "asym_transform_flag", asym_transform_flag) ## End not in spec if asym_transform_flag: state["dwt_depth_ho"] = read_uint(state) # (C.3) Check level allows this wavelet depth ## Begin not in spec assert_level_constraint(state, "dwt_depth_ho", state["dwt_depth_ho"]) ## End not in spec # (11.2.2) Log increased minimum version requirements if an asymmetric # transform was used. ## Begin not in spec minimum_required_version = wavelet_transform_version_implication( state["wavelet_index"], state["wavelet_index_ho"], state["dwt_depth_ho"], ) log_version_lower_bound(state, minimum_required_version)
def pixel_aspect_ratio(state, video_parameters): """(11.4.7)""" custom_pixel_aspect_ratio_flag = read_bool(state) # (C.3) Check level allows custom pixel aspect ratio ## Begin not in spec assert_level_constraint( state, "custom_pixel_aspect_ratio_flag", custom_pixel_aspect_ratio_flag ) ## End not in spec if custom_pixel_aspect_ratio_flag: index = read_uint(state) # (C.3) Check level allows the specified preset ## Begin not in spec assert_level_constraint(state, "pixel_aspect_ratio_index", index) ## End not in spec if index == 0: video_parameters["pixel_aspect_ratio_numer"] = read_uint(state) # (C.3) Check level allows the specified numerator ## Begin not in spec assert_level_constraint( state, "pixel_aspect_ratio_numer", video_parameters["pixel_aspect_ratio_numer"], ) ## End not in spec video_parameters["pixel_aspect_ratio_denom"] = read_uint(state) # (C.3) Check level allows the specified denominator ## Begin not in spec assert_level_constraint( state, "pixel_aspect_ratio_denom", video_parameters["pixel_aspect_ratio_denom"], ) ## End not in spec # Errata: spec fails to require ratio to be non-zero on either side # # (11.4.7) ratio must not contain zeros ## Begin not in spec if ( video_parameters["pixel_aspect_ratio_numer"] == 0 or video_parameters["pixel_aspect_ratio_denom"] == 0 ): raise PixelAspectRatioContainsZeros( video_parameters["pixel_aspect_ratio_numer"], video_parameters["pixel_aspect_ratio_denom"], ) ## End not in spec else: # (11.4.7) Pixel aspect ratio preset must be a known value ## Begin not in spec assert_in_enum(index, PresetPixelAspectRatios, BadPresetPixelAspectRatio) ## End not in spec preset_pixel_aspect_ratio(video_parameters, index)
def clean_area(state, video_parameters): """(11.4.8)""" custom_clean_area_flag = read_bool(state) # (C.3) Check level allows custom clean area ## Begin not in spec assert_level_constraint(state, "custom_clean_area_flag", custom_clean_area_flag) ## End not in spec if custom_clean_area_flag: video_parameters["clean_width"] = read_uint(state) # (C.3) Check level allows this width ## Begin not in spec assert_level_constraint(state, "clean_width", video_parameters["clean_width"]) ## End not in spec video_parameters["clean_height"] = read_uint(state) # (C.3) Check level allows this height ## Begin not in spec assert_level_constraint(state, "clean_height", video_parameters["clean_height"]) ## End not in spec video_parameters["left_offset"] = read_uint(state) # (C.3) Check level allows this offset ## Begin not in spec assert_level_constraint(state, "left_offset", video_parameters["left_offset"]) ## End not in spec video_parameters["top_offset"] = read_uint(state) # (C.3) Check level allows this offset ## Begin not in spec assert_level_constraint(state, "top_offset", video_parameters["top_offset"]) ## End not in spec # (11.4.8) The clean area is restricted to being within the existing # picture area ## Begin not in spec if not ( video_parameters["clean_width"] + video_parameters["left_offset"] <= video_parameters["frame_width"] and video_parameters["clean_height"] + video_parameters["top_offset"] <= video_parameters["frame_height"] ): raise CleanAreaOutOfRange( video_parameters["clean_width"], video_parameters["clean_height"], video_parameters["left_offset"], video_parameters["top_offset"], video_parameters["frame_width"], video_parameters["frame_height"], )
def signal_range(state, video_parameters): """(11.4.9)""" custom_signal_range_flag = read_bool(state) # (C.3) Check level allows custom signal range ## Begin not in spec assert_level_constraint(state, "custom_signal_range_flag", custom_signal_range_flag) ## End not in spec if custom_signal_range_flag: index = read_uint(state) # (C.3) Check level allows the specified preset ## Begin not in spec assert_level_constraint(state, "custom_signal_range_index", index) ## End not in spec if index == 0: video_parameters["luma_offset"] = read_uint(state) # (C.3) Check level allows this offset ## Begin not in spec assert_level_constraint( state, "luma_offset", video_parameters["luma_offset"] ) ## End not in spec video_parameters["luma_excursion"] = read_uint(state) # (C.3) Check level allows this excursion ## Begin not in spec assert_level_constraint( state, "luma_excursion", video_parameters["luma_excursion"] ) ## End not in spec # (11.4.9) Check luma_excursion is valid # # Errata: Spec fails to constrain excursions. Excursions *must* be # >= 1 or the pseudocode behaviour will be undefined. ## Begin not in spec if video_parameters["luma_excursion"] < 1: raise BadCustomSignalExcursion( "luma", video_parameters["luma_excursion"] ) ## End not in spec video_parameters["color_diff_offset"] = read_uint(state) # (C.3) Check level allows this offset ## Begin not in spec assert_level_constraint( state, "color_diff_offset", video_parameters["color_diff_offset"] ) ## End not in spec video_parameters["color_diff_excursion"] = read_uint(state) # (C.3) Check level allows this excursion ## Begin not in spec assert_level_constraint( state, "color_diff_excursion", video_parameters["color_diff_excursion"] ) ## End not in spec # (11.4.9) Check color_diff_excursion is valid # # Errata: Spec fails to constrain excursions. Excursions *must* be # >= 1 or the pseudocode behaviour will be undefined. ## Begin not in spec if video_parameters["color_diff_excursion"] < 1: raise BadCustomSignalExcursion( "color_diff", video_parameters["color_diff_excursion"] ) ## End not in spec else: # (11.4.9) Signal range preset must be a known value ## Begin not in spec assert_in_enum(index, PresetSignalRanges, BadPresetSignalRange) ## End not in spec # (11.2.2) Signal range preset must be supported by current version ## Begin not in spec minimum_required_version = preset_signal_range_version_implication(index) if state["major_version"] < minimum_required_version: raise PresetSignalRangeNotSupportedByVersion( index, state["major_version"] ) log_version_lower_bound(state, minimum_required_version) ## End not in spec preset_signal_range(video_parameters, index)
def frame_rate(state, video_parameters): """(11.4.6)""" custom_frame_rate_flag = read_bool(state) # (C.3) Check level allows custom frame rates ## Begin not in spec assert_level_constraint(state, "custom_frame_rate_flag", custom_frame_rate_flag) ## End not in spec if custom_frame_rate_flag: index = read_uint(state) # (C.3) Check level allows the specified preset assert_level_constraint(state, "frame_rate_index", index) ## Not in spec if index == 0: video_parameters["frame_rate_numer"] = read_uint(state) # (C.3) Check level allows the specified numerator ## Begin not in spec assert_level_constraint( state, "frame_rate_numer", video_parameters["frame_rate_numer"], ) ## End not in spec video_parameters["frame_rate_denom"] = read_uint(state) # (C.3) Check level allows the specified denominator ## Begin not in spec assert_level_constraint( state, "frame_rate_denom", video_parameters["frame_rate_denom"], ) ## End not in spec # Errata: spec doesn't prevent divide-by-zero in custom frame rate # fractions. # # (11.4.6) frame_rate_denom must not be zero (i.e. a divide by zero) ## Begin not in spec if video_parameters["frame_rate_denom"] == 0: raise FrameRateHasZeroDenominator(video_parameters["frame_rate_numer"]) ## End not in spec # Errata: spec doesn't prevent 0 fps # # (11.4.6) frame_rate_numer must not be zero (i.e. 0 fps) ## Begin not in spec if video_parameters["frame_rate_numer"] == 0: raise FrameRateHasZeroNumerator(video_parameters["frame_rate_denom"]) ## End not in spec else: # (11.4.6) Frame rate preset must be a known value ## Begin not in spec assert_in_enum(index, PresetFrameRates, BadPresetFrameRateIndex) ## End not in spec # (11.2.2) Frame rate preset must be supported by current version ## Begin not in spec minimum_required_version = preset_frame_rate_version_implication(index) if state["major_version"] < minimum_required_version: raise PresetFrameRateNotSupportedByVersion( index, state["major_version"] ) log_version_lower_bound(state, minimum_required_version) ## End not in spec preset_frame_rate(video_parameters, index)
def quant_matrix(state): """(12.4.5.3)""" custom_quant_matrix = read_bool(state) # (C.3) Check level allows use of custom quantisation matrices ## Begin not in spec assert_level_constraint(state, "custom_quant_matrix", custom_quant_matrix) ## End not in spec if custom_quant_matrix: # (C.3) Check that quantisation matrix values are in the level-defined # range ## Begin not in spec allowed_values = allowed_values_for( LEVEL_CONSTRAINTS, "quant_matrix_values", state["_level_constrained_values"], ) def check(quant_matrix_value): assert_in( quant_matrix_value, allowed_values, QuantisationMatrixValueNotAllowedInLevel, state["_level_constrained_values"], ) ## End not in spec # NB: For historical reasons, we use a dict not an array in this # implementation. ### state["quant_matrix"] = new_array(state["dwt_depth_ho"] + state["dwt_depth"] + 1) state["quant_matrix"] = {} ## Not in spec if state["dwt_depth_ho"] == 0: state["quant_matrix"][0] = {} state["quant_matrix"][0]["LL"] = read_uint(state) check(state["quant_matrix"][0]["LL"]) ## Not in spec else: state["quant_matrix"][0] = {} state["quant_matrix"][0]["L"] = read_uint(state) check(state["quant_matrix"][0]["L"]) ## Not in spec for level in range(1, state["dwt_depth_ho"] + 1): state["quant_matrix"][level] = {} state["quant_matrix"][level]["H"] = read_uint(state) check(state["quant_matrix"][level]["H"]) ## Not in spec for level in range(state["dwt_depth_ho"] + 1, state["dwt_depth_ho"] + state["dwt_depth"] + 1): state["quant_matrix"][level] = {} state["quant_matrix"][level]["HL"] = read_uint(state) check(state["quant_matrix"][level]["HL"]) ## Not in spec state["quant_matrix"][level]["LH"] = read_uint(state) check(state["quant_matrix"][level]["LH"]) ## Not in spec state["quant_matrix"][level]["HH"] = read_uint(state) check(state["quant_matrix"][level]["HH"]) ## Not in spec else: # (12.4.5.3) If no custom quantisation matrix has been specified, a # default table must be available ## Begin not in spec configuration = ( state["wavelet_index"], state["wavelet_index_ho"], state["dwt_depth"], state["dwt_depth_ho"], ) if configuration not in QUANTISATION_MATRICES: raise NoQuantisationMatrixAvailable(*configuration) ## End not in spec set_quant_matrix(state)