def missing_sequence_header_bitstream_fname(tmpdir):
    fname = str(tmpdir.join("bitstream.vc2"))

    with open(fname, "wb") as f:
        w = bitstream.BitstreamWriter(f)

        state = State(major_version=3)

        context = bitstream.ParseInfo(parse_code=tables.ParseCodes.high_quality_picture)
        with bitstream.Serialiser(w, context, bitstream.vc2_default_values) as ser:
            bitstream.parse_info(ser, state)

        context = bitstream.PictureParse(
            wavelet_transform=bitstream.WaveletTransform(
                transform_parameters=bitstream.TransformParameters(
                    slice_parameters=bitstream.SliceParameters(
                        slices_x=0,
                        slices_y=0,
                    ),
                ),
            ),
        )
        with bitstream.Serialiser(w, context, bitstream.vc2_default_values) as ser:
            bitstream.picture_parse(ser, state)

    return fname
Пример #2
0
    def test_parse_code_version_restriction(self):
        # A sequence with 1 HQ picture fragment but (incorrectly) major_version 2
        seq = bitstream.Sequence(data_units=[
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.sequence_header, ),
                sequence_header=bitstream.SequenceHeader(
                    parse_parameters=bitstream.ParseParameters(major_version=2,
                                                               ),
                    video_parameters=bitstream.SourceParameters(
                        frame_size=bitstream.FrameSize(
                            # Don't waste time on full-sized frames
                            custom_dimensions_flag=True,
                            frame_width=4,
                            frame_height=4,
                        ),
                        clean_area=bitstream.CleanArea(
                            custom_clean_area_flag=True,
                            clean_width=4,
                            clean_height=4,
                        ),
                    ),
                ),
            ),
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture_fragment,
                ),
                fragment_parse=bitstream.FragmentParse(
                    fragment_header=bitstream.FragmentHeader(
                        fragment_slice_count=0, ),
                    transform_parameters=bitstream.TransformParameters(
                        slice_parameters=bitstream.SliceParameters(
                            slices_x=1,
                            slices_y=1,
                        ), ),
                ),
            ),
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture_fragment,
                ),
                fragment_parse=bitstream.FragmentParse(
                    fragment_header=bitstream.FragmentHeader(
                        fragment_slice_count=1, ), ),
            ),
            bitstream.DataUnit(parse_info=bitstream.ParseInfo(
                parse_code=tables.ParseCodes.end_of_sequence, ), ),
        ])
        populate_parse_offsets(seq)

        state = bytes_to_state(serialise_to_bytes(seq))
        with pytest.raises(decoder.ParseCodeNotSupportedByVersion):
            decoder.parse_stream(state)
Пример #3
0
def test_transform_parameters_wavelet_index_must_be_valid():
    state = minimal_transform_parameters_state.copy()
    state.update(
        bytes_to_state(
            serialise_to_bytes(
                bitstream.TransformParameters(
                    wavelet_index=tables.WaveletFilters.haar_no_shift, ),
                state,
            )))
    decoder.transform_parameters(state)

    state = minimal_transform_parameters_state.copy()
    state.update(
        bytes_to_state(
            serialise_to_bytes(
                bitstream.TransformParameters(wavelet_index=9999, ),
                state,
            )))
    with pytest.raises(decoder.BadWaveletIndex) as exc_info:
        decoder.transform_parameters(state)
    assert exc_info.value.wavelet_index == 9999
Пример #4
0
def test_transform_parameters_et_al_level_constraints(
        parse_code, extra_slice_parameters, extra_constrained_values):
    # Checks level constraints for:
    #
    # * transform_parameters
    #   * extended_transform_parameters
    #   * slice_parameters
    #   * quant_matrix
    #
    # Simply check that all constrainted level values get asserted. We check
    # this by making sure assert_level_constraint has added the relevant values
    # to # state["_level_constrained_values"].
    state = minimal_transform_parameters_state.copy()
    state["parse_code"] = parse_code
    state.update(
        bytes_to_state(
            serialise_to_bytes(
                bitstream.TransformParameters(
                    wavelet_index=tables.WaveletFilters.haar_no_shift,
                    dwt_depth=1,
                    extended_transform_parameters=bitstream.
                    ExtendedTransformParameters(
                        asym_transform_index_flag=True,
                        wavelet_index_ho=tables.WaveletFilters.le_gall_5_3,
                        asym_transform_flag=True,
                        dwt_depth_ho=2,
                    ),
                    slice_parameters=bitstream.SliceParameters(
                        slices_x=1, slices_y=2, **extra_slice_parameters),
                    quant_matrix=bitstream.QuantMatrix(
                        custom_quant_matrix=False, ),
                ),
                state,
            )))
    decoder.transform_parameters(state)
    expected_constrained_values = {
        "wavelet_index": tables.WaveletFilters.haar_no_shift,
        "dwt_depth": 1,
        "asym_transform_index_flag": True,
        "wavelet_index_ho": tables.WaveletFilters.le_gall_5_3,
        "asym_transform_flag": True,
        "dwt_depth_ho": 2,
        "slices_x": 1,
        "slices_y": 2,
        "slices_have_same_dimensions": True,
        "custom_quant_matrix": False,
    }
    expected_constrained_values.update(extra_constrained_values)
    assert state["_level_constrained_values"] == expected_constrained_values
Пример #5
0
def test_whole_picture(parse_code, fragment_slice_counts):
    # A sanity check which runs fragmented picture decoding for whole pictures
    # and makes sure nothing crashes

    # Serialise a sample stream
    sh = bitstream.SequenceHeader(
        video_parameters=bitstream.SourceParameters(
            frame_size=bitstream.FrameSize(
                # Don't waste time on full-sized frames
                custom_dimensions_flag=True,
                frame_width=8,
                frame_height=8,
            ),
            clean_area=bitstream.CleanArea(
                custom_clean_area_flag=True,
                clean_width=8,
                clean_height=8,
            ),
        ), )
    serialisation_state = State()
    sh_bytes = serialise_to_bytes(sh, serialisation_state)
    serialisation_state["parse_code"] = parse_code
    frag_bytes = b""

    # Add the first (header) fragment in the picture
    frag_bytes += serialise_to_bytes(
        bitstream.FragmentParse(
            fragment_header=bitstream.FragmentHeader(fragment_slice_count=0, ),
            transform_parameters=bitstream.TransformParameters(
                slice_parameters=bitstream.SliceParameters(
                    slices_x=3,
                    slices_y=2,
                )),
        ),
        serialisation_state,
    )

    # Add the slice-containing fragments
    num_slices = 0
    for fragment_slice_count in fragment_slice_counts:
        x = num_slices % 3
        y = num_slices // 3
        num_slices += fragment_slice_count

        frag_bytes += serialise_to_bytes(
            bitstream.FragmentParse(fragment_header=bitstream.FragmentHeader(
                fragment_slice_count=fragment_slice_count,
                fragment_x_offset=x,
                fragment_y_offset=y,
            ), ),
            serialisation_state,
        )

    # Check it is parsed without failiures
    state = bytes_to_state(sh_bytes + frag_bytes)
    state["_num_pictures_in_sequence"] = 0
    state["_fragment_slices_remaining"] = 0
    decoder.sequence_header(state)

    # Parse header fragment
    decoder.byte_align(state)
    state["parse_code"] = parse_code
    decoder.fragment_parse(state)

    # Parse slice-containing fragments
    num_slices = 0
    for fragment_slice_count in fragment_slice_counts:
        assert state["fragmented_picture_done"] is False

        decoder.byte_align(state)
        state["parse_code"] = parse_code
        decoder.fragment_parse(state)

        num_slices += fragment_slice_count
        assert state["fragment_slices_received"] == num_slices
        assert state["_fragment_slices_remaining"] == 6 - num_slices

    assert state["fragmented_picture_done"] is True
Пример #6
0
    def test_output_picture(self):
        # This test adds a callback for output_picture and makes sure that both
        # fragments and pictures call it correctly (and that sanity-checks very
        # loosely that decoding etc. is happening). Finally, it also checks
        # that two concatenated sequences are read one after another.

        # A sequence with a HQ picture followed by a HQ fragment (both all
        # zeros)
        seq1 = bitstream.Sequence(data_units=[
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.sequence_header, ),
                sequence_header=bitstream.SequenceHeader(
                    video_parameters=bitstream.SourceParameters(
                        frame_size=bitstream.FrameSize(
                            # Don't waste time on full-sized frames
                            custom_dimensions_flag=True,
                            frame_width=4,
                            frame_height=2,
                        ),
                        clean_area=bitstream.CleanArea(
                            custom_clean_area_flag=True,
                            clean_width=4,
                            clean_height=2,
                        ),
                        color_diff_sampling_format=bitstream.
                        ColorDiffSamplingFormat(
                            custom_color_diff_format_flag=True,
                            color_diff_format_index=tables.
                            ColorDifferenceSamplingFormats.
                            color_4_2_2,  # noqa: E501
                        ),
                        # Output values will be treated as 8-bit (and thus all
                        # decode to 128)
                        signal_range=bitstream.SignalRange(
                            custom_signal_range_flag=True,
                            index=tables.PresetSignalRanges.
                            video_8bit_full_range,
                        ),
                    ),
                    picture_coding_mode=tables.PictureCodingModes.
                    pictures_are_frames,
                ),
            ),
            # A HQ picture
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture, ),
                picture_parse=bitstream.PictureParse(
                    picture_header=bitstream.PictureHeader(picture_number=10,
                                                           ), ),
            ),
            # A fragmented HQ picture (sent over two fragments to ensure the
            # callback only fires after a whole picture arrives)
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture_fragment,
                ),
                fragment_parse=bitstream.FragmentParse(
                    fragment_header=bitstream.FragmentHeader(
                        picture_number=11,
                        fragment_slice_count=0,
                    ),
                    transform_parameters=bitstream.TransformParameters(
                        slice_parameters=bitstream.SliceParameters(
                            slices_x=2,
                            slices_y=1,
                        ), ),
                ),
            ),
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture_fragment,
                ),
                fragment_parse=bitstream.FragmentParse(
                    fragment_header=bitstream.FragmentHeader(
                        picture_number=11,
                        fragment_slice_count=1,
                        fragment_x_offset=0,
                        fragment_y_offset=0,
                    ), ),
            ),
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture_fragment,
                ),
                fragment_parse=bitstream.FragmentParse(
                    fragment_header=bitstream.FragmentHeader(
                        picture_number=11,
                        fragment_slice_count=1,
                        fragment_x_offset=1,
                        fragment_y_offset=0,
                    ), ),
            ),
            bitstream.DataUnit(parse_info=bitstream.ParseInfo(
                parse_code=tables.ParseCodes.end_of_sequence, ), ),
        ])

        # Another (HQ) picture in a separate sequence
        seq2 = bitstream.Sequence(data_units=[
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.sequence_header, ),
                sequence_header=bitstream.SequenceHeader(
                    parse_parameters=bitstream.ParseParameters(
                        major_version=2),
                    video_parameters=bitstream.SourceParameters(
                        frame_size=bitstream.FrameSize(
                            # Don't waste time on full-sized frames
                            custom_dimensions_flag=True,
                            frame_width=4,
                            frame_height=2,
                        ),
                        clean_area=bitstream.CleanArea(
                            custom_clean_area_flag=True,
                            clean_width=4,
                            clean_height=2,
                        ),
                        color_diff_sampling_format=bitstream.
                        ColorDiffSamplingFormat(
                            custom_color_diff_format_flag=True,
                            color_diff_format_index=tables.
                            ColorDifferenceSamplingFormats.
                            color_4_2_2,  # noqa: E501
                        ),
                        # Output values will be treated as 8-bit (and thus all
                        # decode to 128)
                        signal_range=bitstream.SignalRange(
                            custom_signal_range_flag=True,
                            index=tables.PresetSignalRanges.
                            video_8bit_full_range,
                        ),
                    ),
                    picture_coding_mode=tables.PictureCodingModes.
                    pictures_are_frames,
                ),
            ),
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture, ),
                picture_parse=bitstream.PictureParse(
                    picture_header=bitstream.PictureHeader(
                        picture_number=12), ),
            ),
            bitstream.DataUnit(parse_info=bitstream.ParseInfo(
                parse_code=tables.ParseCodes.end_of_sequence, ), ),
        ])
        populate_parse_offsets(seq1)
        populate_parse_offsets(seq2)

        state = bytes_to_state(
            serialise_to_bytes(seq1) + serialise_to_bytes(seq2))
        state["_output_picture_callback"] = Mock()
        decoder.parse_stream(state)

        assert state["_output_picture_callback"].call_count == 3

        for i, (args, kwargs) in enumerate(
                state["_output_picture_callback"].call_args_list):
            assert kwargs == {}
            # Should get a 4x2 mid-gray frame with 4:2:2 color difference sampling
            assert args[0] == {
                "pic_num": 10 + i,
                "Y": [[128, 128, 128, 128], [128, 128, 128, 128]],
                "C1": [[128, 128], [128, 128]],
                "C2": [[128, 128], [128, 128]],
            }
            # Just sanity check the second argument looks like a set of video parameters
            assert args[1]["frame_width"] == 4
            assert args[1]["frame_height"] == 2
            assert args[1]["luma_offset"] == 0
            assert args[1]["luma_offset"] == 0
            assert args[1]["luma_excursion"] == 255
            assert args[1]["color_diff_offset"] == 128
            assert args[1]["color_diff_excursion"] == 255
            # And the picture coding mode too...
            assert args[2] == tables.PictureCodingModes.pictures_are_frames
Пример #7
0
    def test_picture_and_incomplete_fragment_interleaving_disallowed(
            self, num_slices_to_send, exp_fail):
        # A sequence with a 3x2 slice picture fragment with num_slices_to_send slices in
        # it followed by an HQ picture
        seq = bitstream.Sequence(data_units=[
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.sequence_header, ),
                sequence_header=bitstream.SequenceHeader(
                    video_parameters=bitstream.SourceParameters(
                        frame_size=bitstream.FrameSize(
                            # Don't waste time on full-sized pictures
                            custom_dimensions_flag=True,
                            frame_width=8,
                            frame_height=8,
                        ),
                        clean_area=bitstream.CleanArea(
                            custom_clean_area_flag=True,
                            clean_width=8,
                            clean_height=8,
                        ),
                    ), ),
            ),
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture_fragment,
                ),
                fragment_parse=bitstream.FragmentParse(
                    fragment_header=bitstream.FragmentHeader(
                        picture_number=0,
                        fragment_slice_count=0,
                    ),
                    transform_parameters=bitstream.TransformParameters(
                        slice_parameters=bitstream.SliceParameters(
                            slices_x=3,
                            slices_y=2,
                        ), ),
                ),
            ),
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture_fragment,
                ),
                fragment_parse=bitstream.FragmentParse(
                    fragment_header=bitstream.FragmentHeader(
                        picture_number=0,
                        fragment_slice_count=num_slices_to_send,
                    ), ),
            ),
            bitstream.DataUnit(
                parse_info=bitstream.ParseInfo(
                    parse_code=tables.ParseCodes.high_quality_picture, ),
                picture_parse=bitstream.PictureParse(
                    picture_header=bitstream.PictureHeader(picture_number=1,
                                                           ), ),
            ),
            bitstream.DataUnit(parse_info=bitstream.ParseInfo(
                parse_code=tables.ParseCodes.end_of_sequence, ), ),
        ])
        # Don't include second (non-header) fragment if sending no slices
        if num_slices_to_send == 0:
            del seq["data_units"][2]

        populate_parse_offsets(seq)

        state = bytes_to_state(serialise_to_bytes(seq))
        if exp_fail:
            with pytest.raises(decoder.PictureInterleavedWithFragmentedPicture
                               ) as exc_info:
                decoder.parse_stream(state)
            first_fragment_offset = (
                seq["data_units"][0]["parse_info"]["next_parse_offset"] +
                tables.PARSE_INFO_HEADER_BYTES)
            assert exc_info.value.initial_fragment_offset == (
                first_fragment_offset, 7)
            picture_offset = (
                sum(seq["data_units"][i]["parse_info"]["next_parse_offset"]
                    for i in range(len(seq["data_units"]) - 2)) +
                tables.PARSE_INFO_HEADER_BYTES)
            assert exc_info.value.this_offset == (picture_offset, 7)
            assert exc_info.value.fragment_slices_received == num_slices_to_send
            assert exc_info.value.fragment_slices_remaining == 6 - num_slices_to_send
        else:
            decoder.parse_stream(state)