Bug 1772053 - Enable dynamic code disable mitigations only on Windows 10 1703+ r...
[gecko.git] / dom / media / gtest / TestVPXDecoding.cpp
blobd58ca24cc7127f017ddc513e6d2efd405d9ee186
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "gtest/gtest.h"
6 #include "mozilla/ArrayUtils.h"
7 #include "nsTArray.h"
8 #include "VPXDecoder.h"
10 #include <stdio.h>
12 using namespace mozilla;
14 static void ReadVPXFile(const char* aPath, nsTArray<uint8_t>& aBuffer) {
15 FILE* f = fopen(aPath, "rb");
16 ASSERT_NE(f, (FILE*)nullptr);
18 int r = fseek(f, 0, SEEK_END);
19 ASSERT_EQ(r, 0);
21 long size = ftell(f);
22 ASSERT_NE(size, -1);
23 aBuffer.SetLength(size);
25 r = fseek(f, 0, SEEK_SET);
26 ASSERT_EQ(r, 0);
28 size_t got = fread(aBuffer.Elements(), 1, size, f);
29 ASSERT_EQ(got, size_t(size));
31 r = fclose(f);
32 ASSERT_EQ(r, 0);
35 static vpx_codec_iface_t* ParseIVFConfig(nsTArray<uint8_t>& data,
36 vpx_codec_dec_cfg_t& config) {
37 if (data.Length() < 32 + 12) {
38 // Not enough data for file & first frame headers.
39 return nullptr;
41 if (data[0] != 'D' || data[1] != 'K' || data[2] != 'I' || data[3] != 'F') {
42 // Expect 'DKIP'
43 return nullptr;
45 if (data[4] != 0 || data[5] != 0) {
46 // Expect version==0.
47 return nullptr;
49 if (data[8] != 'V' || data[9] != 'P' ||
50 (data[10] != '8' && data[10] != '9') || data[11] != '0') {
51 // Expect 'VP80' or 'VP90'.
52 return nullptr;
54 config.w = uint32_t(data[12]) || (uint32_t(data[13]) << 8);
55 config.h = uint32_t(data[14]) || (uint32_t(data[15]) << 8);
56 vpx_codec_iface_t* codec =
57 (data[10] == '8') ? vpx_codec_vp8_dx() : vpx_codec_vp9_dx();
58 // Remove headers, to just leave raw VPx data to be decoded.
59 data.RemoveElementsAt(0, 32 + 12);
60 return codec;
63 struct TestFileData {
64 const char* mFilename;
65 vpx_codec_err_t mDecodeResult;
67 static const TestFileData testFiles[] = {
68 {"test_case_1224361.vp8.ivf", VPX_CODEC_OK},
69 {"test_case_1224363.vp8.ivf", VPX_CODEC_CORRUPT_FRAME},
70 {"test_case_1224369.vp8.ivf", VPX_CODEC_CORRUPT_FRAME}};
72 TEST(libvpx, test_cases)
74 for (size_t test = 0; test < ArrayLength(testFiles); ++test) {
75 nsTArray<uint8_t> data;
76 ReadVPXFile(testFiles[test].mFilename, data);
77 ASSERT_GT(data.Length(), 0u);
79 vpx_codec_dec_cfg_t config;
80 vpx_codec_iface_t* dx = ParseIVFConfig(data, config);
81 ASSERT_TRUE(dx);
82 config.threads = 2;
84 vpx_codec_ctx_t ctx;
85 PodZero(&ctx);
86 vpx_codec_err_t r = vpx_codec_dec_init(&ctx, dx, &config, 0);
87 ASSERT_EQ(VPX_CODEC_OK, r);
89 r = vpx_codec_decode(&ctx, data.Elements(), data.Length(), nullptr, 0);
90 // This test case is known to be corrupt.
91 EXPECT_EQ(testFiles[test].mDecodeResult, r);
93 r = vpx_codec_destroy(&ctx);
94 EXPECT_EQ(VPX_CODEC_OK, r);