def test_two_files_with_metadata(self, tmpdir):
        fa = str(tmpdir.join("a.raw"))
        fb = str(tmpdir.join("b.raw"))

        generate_picture(fa, 100, width=16, height=8)
        generate_picture(fb, 200, width=32, height=16)

        (
            picture_a,
            picture_b,
            byte_for_byte_identical,
        ) = read_pictures_with_only_one_metadata_file_required(fa, fb)

        assert picture_a == read(fa)
        assert picture_b == read(fb)
    def test_valid_multi_sequence_stream(
        self,
        tmpdir,
        valid_bitstream,
        output_name,
        capsys,
    ):
        filename = str(tmpdir.join("multi_sequence.vc2"))

        with open(filename, "wb") as f:
            f.write(open(valid_bitstream, "rb").read() * 2)

        v = BitstreamValidator(filename, True, 0, output_name)
        assert v.run() == 0

        # Check no error reported and that status line is shown
        stdout, stderr = capsys.readouterr()
        assert "%] Starting bitstream validation" in stderr
        assert "%] Decoded picture written to {}".format(output_name % 0) in stderr
        assert "%] Decoded picture written to {}".format(output_name % 1) in stderr
        assert "%] Decoded picture written to {}".format(output_name % 2) in stderr
        assert "%] Decoded picture written to {}".format(output_name % 3) in stderr
        assert stdout == (
            "No errors found in bitstream. "
            "Verify decoded pictures to confirm conformance.\n"
        )

        # Check picture files are as expected
        for i, expected_picture_number in enumerate([100, 101, 100, 101]):
            picture, video_parameters, picture_coding_mode = read(output_name % i)
            assert picture["pic_num"] == expected_picture_number
    def test_difference_mask(self, tmpdir):
        fa = str(tmpdir.join("a.raw"))
        fb = str(tmpdir.join("b.raw"))
        mask_filename = str(tmpdir.join("mask.raw"))

        generate_picture(
            fa,
            (
                np.array([[0, 1, 2, 3], [4, 5, 6, 7]]),
                np.array([[8, 9]]),
                np.array([[10, 11]]),
            ),
            picture_number=123,
        )
        generate_picture(
            fb,
            (
                np.array([[0, 1, 2, 4], [4, 5, 7, 7]]),
                np.array([[9, 9]]),
                np.array([[10, 11]]),
            ),
            picture_number=123,
        )

        out, exitcode = compare_pictures(fa, fb, mask_filename)
        assert exitcode == 4
        assert "Pictures are different" in out

        picture, video_parameters, picture_coding_mode = read(mask_filename)

        # Mask should be as expected
        assert picture["Y"] == [[1023, 1023, 0, 1023], [1023, 1023, 1023, 0]]
        assert picture["C1"] == [[512, 512]]
        assert picture["C2"] == [[512, 512]]

        # Should match input picture numbers
        assert picture["pic_num"] == 123

        # Should match format of input pictures
        _, video_parameters_a, picture_coding_mode_a = read(fa)
        assert video_parameters == video_parameters_a
        assert picture_coding_mode == picture_coding_mode_a
def test_read_and_write_convenience_functions(picture, video_parameters,
                                              picture_coding_mode, tmp_path):
    filename = os.path.join(str(tmp_path), "test_file.xxx")

    write(picture, video_parameters, picture_coding_mode, filename)

    # Correct files created
    assert set(os.listdir(str(tmp_path))) == set(
        ["test_file.raw", "test_file.json"])

    # Round-trip works
    assert read(filename) == (
        picture,
        video_parameters,
        picture_coding_mode,
    )
    def test_valid_bitstream(self, valid_bitstream, output_name, capsys):
        v = BitstreamValidator(valid_bitstream, True, 0, output_name)
        assert v.run() == 0

        # Check no error reported and that status line is shown
        stdout, stderr = capsys.readouterr()
        assert "%] Starting bitstream validation" in stderr
        assert "%] Decoded picture written to {}".format(output_name % 0) in stderr
        assert "%] Decoded picture written to {}".format(output_name % 1) in stderr
        assert stdout == (
            "No errors found in bitstream. "
            "Verify decoded pictures to confirm conformance.\n"
        )

        # Check picture files are as expected
        for i, expected_picture_number in enumerate([100, 101]):
            picture, video_parameters, picture_coding_mode = read(output_name % i)
            assert picture["pic_num"] == expected_picture_number
def test_real_pictures(codec_features):
    picture, video_parameters, picture_coding_mode = file_format.read(
        LOVELL_IMAGE_PATH, )

    codec_features["wavelet_index"] = WaveletFilters.le_gall_5_3
    codec_features["wavelet_index_ho"] = WaveletFilters.le_gall_5_3
    codec_features["dwt_depth"] = 2
    codec_features["dwt_depth_ho"] = 0

    codec_features["slices_x"] = 8
    codec_features["slices_y"] = 16
    codec_features["lossless"] = False
    codec_features["picture_bytes"] = (64 * 64 * 2) // 2  # ~2:1 compression

    codec_features["video_parameters"] = video_parameters

    with alternative_real_pictures([LOVELL_IMAGE_PATH]):
        stream = real_pictures(codec_features)

    coded_pictures = encode_and_decode(stream)

    assert len(coded_pictures) == 1
    coded_picture = coded_pictures[0]

    for component in ["Y", "C1", "C2"]:
        before = np.array(picture[component])
        after = np.array(coded_picture[component])

        # Coding should be lossy
        assert not np.array_equal(before, after)

        # PSNR should be reasonable
        error = after - before
        square_error = error * error
        mean_square_error = np.mean(square_error)
        max_value = 255.0
        peak_signal_to_noise_ratio = (20 * np.log(max_value) / np.log(10)) - (
            10 * np.log(mean_square_error) / np.log(10))

        # NB: This is a pretty low PSNR threshold but appropriate here since
        # we're encoding a very small (and, for its size, very detailed) image.
        assert peak_signal_to_noise_ratio > 45.0
Exemple #7
0
def read_as_xyz(filename):
    """
    Read a VC-2 raw image (see :py:mod:`vc2_conformance.file_format`) into a
    floating point CIE XYZ color 3D array.

    Returns
    =======
    xyz : :py:class:`numpy.array`
        A (height, width, 3) array containing the loaded picture, upsampled to
        4:4:4 color subsampling and converted to CIE XYZ color values.
    video_parameters : :py:class:`~vc2_conformance.pseudocode.video_parameters.VideoParameters`
    picture_coding_mode : :py:class:`~vc2_data_tables.PictureCodingModes`
    """
    picture, video_parameters, picture_coding_mode = read(filename)

    xyz = to_xyz(
        np.array(picture["Y"]),
        np.array(picture["C1"]),
        np.array(picture["C2"]),
        video_parameters,
    )

    return xyz, video_parameters, picture_coding_mode
Exemple #8
0
def test_pictures(filename):
    # Should be able to successfully read the file without crashing...
    read(filename)