vp9_parser_unittest.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // For some sample vp9 test videos, $filename, there is a file of golden value
  5. // of frame entropy, named $filename.context. These values are dumped from
  6. // libvpx.
  7. //
  8. // The syntax of these context dump is described as follows. For every
  9. // frame, there are corresponding data in context file,
  10. // 1. [initial] [current] [should_update=0], or
  11. // 2. [initial] [current] [should_update=1] [update]
  12. // The first two are expected frame entropy, fhdr->initial_frame_context and
  13. // fhdr->frame_context.
  14. // If |should_update| is true, it follows by the frame context to update.
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <memory>
  18. #include <utility>
  19. #include <vector>
  20. #include "base/files/memory_mapped_file.h"
  21. #include "base/logging.h"
  22. #include "media/base/test_data_util.h"
  23. #include "media/filters/ivf_parser.h"
  24. #include "media/filters/vp9_parser.h"
  25. #include "testing/gtest/include/gtest/gtest.h"
  26. using ::testing::TestWithParam;
  27. namespace media {
  28. namespace {
  29. struct TestParams {
  30. const char* file_name;
  31. int profile;
  32. int bit_depth;
  33. size_t width;
  34. size_t height;
  35. bool frame_parallel_decoding_mode;
  36. int loop_filter_level;
  37. int quantization_base_index;
  38. size_t first_frame_header_size_bytes;
  39. enum Vp9InterpolationFilter filter;
  40. size_t second_frame_header_size_bytes;
  41. size_t second_frame_uncompressed_header_size_bytes;
  42. };
  43. const struct TestParams kTestParams[] = {
  44. {"test-25fps.vp9", 0, 8, 320, 240, true, 9, 65, 120,
  45. Vp9InterpolationFilter::EIGHTTAP, 48, 11},
  46. {"test-25fps.vp9_2", 2, 10, 320, 240, true, 8, 79, 115,
  47. Vp9InterpolationFilter::SWITCHABLE, 46, 10}};
  48. const char kInitialIV[] = "aaaaaaaaaaaaaaaa";
  49. const char kIVIncrementOne[] = "aaaaaaaaaaaaaaab";
  50. const char kIVIncrementTwo[] = "aaaaaaaaaaaaaaac";
  51. const char kKeyID[] = "key-id";
  52. } // anonymous namespace
  53. class Vp9ParserTest : public TestWithParam<TestParams> {
  54. protected:
  55. Vp9ParserTest() = default;
  56. void TearDown() override {
  57. stream_.reset();
  58. vp9_parser_.reset();
  59. context_file_.Close();
  60. }
  61. void Initialize(const std::string& filename,
  62. bool parsing_compressed_header,
  63. bool needs_external_context_update) {
  64. base::FilePath file_path = GetTestDataFilePath(filename);
  65. stream_ = std::make_unique<base::MemoryMappedFile>();
  66. ASSERT_TRUE(stream_->Initialize(file_path)) << "Couldn't open stream file: "
  67. << file_path.MaybeAsASCII();
  68. IvfFileHeader ivf_file_header;
  69. ASSERT_TRUE(ivf_parser_.Initialize(stream_->data(), stream_->length(),
  70. &ivf_file_header));
  71. ASSERT_EQ(ivf_file_header.fourcc, 0x30395056u); // VP90
  72. vp9_parser_ = std::make_unique<Vp9Parser>(parsing_compressed_header,
  73. needs_external_context_update);
  74. if (parsing_compressed_header) {
  75. base::FilePath context_path = GetTestDataFilePath(filename + ".context");
  76. context_file_.Initialize(context_path,
  77. base::File::FLAG_OPEN | base::File::FLAG_READ);
  78. ASSERT_TRUE(context_file_.IsValid());
  79. }
  80. }
  81. bool ReadShouldContextUpdate() {
  82. char should_update;
  83. int read_num = context_file_.ReadAtCurrentPos(&should_update, 1);
  84. EXPECT_EQ(1, read_num);
  85. return should_update != 0;
  86. }
  87. void ReadContext(Vp9FrameContext* frame_context) {
  88. ASSERT_EQ(
  89. static_cast<int>(sizeof(*frame_context)),
  90. context_file_.ReadAtCurrentPos(reinterpret_cast<char*>(frame_context),
  91. sizeof(*frame_context)));
  92. }
  93. Vp9Parser::Result ParseNextFrame(struct Vp9FrameHeader* frame_hdr);
  94. void CheckSubsampleValues(
  95. const uint8_t* superframe,
  96. size_t framesize,
  97. std::unique_ptr<DecryptConfig> config,
  98. std::vector<std::unique_ptr<DecryptConfig>>& expected_split);
  99. const Vp9SegmentationParams& GetSegmentation() const {
  100. return vp9_parser_->context().segmentation();
  101. }
  102. const Vp9LoopFilterParams& GetLoopFilter() const {
  103. return vp9_parser_->context().loop_filter();
  104. }
  105. Vp9Parser::ContextRefreshCallback GetContextRefreshCb(
  106. const Vp9FrameHeader& frame_hdr) const {
  107. return vp9_parser_->GetContextRefreshCb(frame_hdr.frame_context_idx);
  108. }
  109. void VerifyNoContextManagersNeedUpdate() {
  110. for (const auto& manager : vp9_parser_->context().frame_context_managers_)
  111. ASSERT_FALSE(manager.needs_client_update());
  112. }
  113. IvfParser ivf_parser_;
  114. std::unique_ptr<base::MemoryMappedFile> stream_;
  115. std::unique_ptr<Vp9Parser> vp9_parser_;
  116. base::File context_file_;
  117. };
  118. Vp9Parser::Result Vp9ParserTest::ParseNextFrame(Vp9FrameHeader* fhdr) {
  119. while (true) {
  120. std::unique_ptr<DecryptConfig> null_config;
  121. gfx::Size allocate_size;
  122. Vp9Parser::Result res =
  123. vp9_parser_->ParseNextFrame(fhdr, &allocate_size, &null_config);
  124. if (res == Vp9Parser::kEOStream) {
  125. IvfFrameHeader ivf_frame_header;
  126. const uint8_t* ivf_payload;
  127. if (!ivf_parser_.ParseNextFrame(&ivf_frame_header, &ivf_payload))
  128. return Vp9Parser::kEOStream;
  129. vp9_parser_->SetStream(ivf_payload, ivf_frame_header.frame_size,
  130. nullptr);
  131. continue;
  132. }
  133. return res;
  134. }
  135. }
  136. void Vp9ParserTest::CheckSubsampleValues(
  137. const uint8_t* superframe,
  138. size_t framesize,
  139. std::unique_ptr<DecryptConfig> config,
  140. std::vector<std::unique_ptr<DecryptConfig>>& expected_split) {
  141. vp9_parser_->SetStream(superframe, framesize, std::move(config));
  142. for (auto& expected : expected_split) {
  143. std::unique_ptr<DecryptConfig> actual =
  144. vp9_parser_->NextFrameDecryptContextForTesting();
  145. EXPECT_EQ(actual->iv(), expected->iv());
  146. EXPECT_EQ(actual->subsamples().size(), expected->subsamples().size());
  147. }
  148. }
  149. uint8_t make_marker_byte(bool is_superframe, const uint8_t frame_count) {
  150. DCHECK_LE(frame_count, 8);
  151. const uint8_t superframe_marker_byte =
  152. // superframe marker byte
  153. // marker (0b110) encoded at bits 6, 7, 8
  154. // or non-superframe marker (0b111)
  155. (0xE0 & ((is_superframe ? 0x06 : 0x07) << 5)) |
  156. // magnitude - 1 encoded at bits 4, 5
  157. (0x18 & (0x00 << 3)) |
  158. // frame count - 2 encoded at bits 1, 2, 3
  159. (0x07 & (frame_count - 1));
  160. return superframe_marker_byte;
  161. }
  162. // ┌───────────────────┬────────────────────┐
  163. // │ frame 1 │ frame 2 │
  164. // ┝━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━┥
  165. // │ clear1 | cipher 1 │ clear 2 | cipher 2 │
  166. // └───────────────────┴────────────────────┘
  167. TEST_F(Vp9ParserTest, AlignedFrameSubsampleParsing) {
  168. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  169. const uint8_t superframe_marker_byte = make_marker_byte(true, 2);
  170. const uint8_t kSuperframe[] = {
  171. // First frame; 32 bytes.
  172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  173. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  175. // Second frame; 32 bytes.
  176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. // Superframe marker goes before and after frame index.
  180. superframe_marker_byte,
  181. // First frame length (magnitude 1)
  182. 0x20,
  183. // Second frame length (magnigude 1)
  184. 0x20,
  185. // marker again.
  186. superframe_marker_byte};
  187. std::vector<std::unique_ptr<DecryptConfig>> expected;
  188. expected.push_back(DecryptConfig::CreateCbcsConfig(
  189. kKeyID, kInitialIV, {SubsampleEntry(16, 16)}, absl::nullopt));
  190. expected.push_back(DecryptConfig::CreateCbcsConfig(
  191. kKeyID, kIVIncrementOne, {SubsampleEntry(16, 16)}, absl::nullopt));
  192. CheckSubsampleValues(
  193. kSuperframe, sizeof(kSuperframe),
  194. DecryptConfig::CreateCencConfig(
  195. kKeyID, kInitialIV, {SubsampleEntry(16, 16), SubsampleEntry(16, 16)}),
  196. expected);
  197. }
  198. // ┌───────────────────┬────────────────────┐
  199. // │ frame 1 │ frame 2 │
  200. // ┝━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━┥
  201. // │ clear1 | cipher 1 │
  202. // └────────────────────────────────────────┘
  203. TEST_F(Vp9ParserTest, UnalignedFrameSubsampleParsing) {
  204. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  205. const uint8_t superframe_marker_byte = make_marker_byte(true, 2);
  206. const uint8_t kSuperframe[] = {
  207. // First frame; 32 bytes.
  208. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  209. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  210. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  211. // Second frame; 32 bytes.
  212. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  215. // Superframe marker goes before and after frame index.
  216. superframe_marker_byte,
  217. // First frame length (magnitude 1)
  218. 0x20,
  219. // Second frame length (magnigude 1)
  220. 0x20,
  221. // marker again.
  222. superframe_marker_byte};
  223. std::vector<std::unique_ptr<DecryptConfig>> expected;
  224. expected.push_back(DecryptConfig::CreateCbcsConfig(
  225. kKeyID, kInitialIV, {SubsampleEntry(32, 0)}, absl::nullopt));
  226. expected.push_back(DecryptConfig::CreateCbcsConfig(
  227. kKeyID, kInitialIV, {SubsampleEntry(16, 16)}, absl::nullopt));
  228. CheckSubsampleValues(kSuperframe, sizeof(kSuperframe),
  229. DecryptConfig::CreateCencConfig(
  230. kKeyID, kInitialIV, {SubsampleEntry(48, 16)}),
  231. expected);
  232. }
  233. // ┌─────────────────────────┬────────────────────┐
  234. // │ frame 1 │ frame 2 │
  235. // ┝━━━━━━━━━━━━━━━━━━━┯━━━━━┷━━━━━━━━━━━━━━━━━━━━┥
  236. // │ clear1 | cipher 1 │ clear 2 | cipher 2 │
  237. // └───────────────────┴──────────────────────────┘
  238. TEST_F(Vp9ParserTest, ClearSectionRollsOverSubsampleParsing) {
  239. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  240. const uint8_t superframe_marker_byte = make_marker_byte(true, 2);
  241. const uint8_t kSuperframe[] = {
  242. // First frame; 48 bytes.
  243. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  244. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  245. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  246. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  247. // Second frame; 32 bytes.
  248. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  249. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  250. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  251. // Superframe marker goes before and after frame index.
  252. superframe_marker_byte,
  253. // First frame length (magnitude 1)
  254. 0x30,
  255. // Second frame length (magnigude 1)
  256. 0x20,
  257. // marker again.
  258. superframe_marker_byte};
  259. std::vector<std::unique_ptr<DecryptConfig>> expected;
  260. expected.push_back(DecryptConfig::CreateCbcsConfig(
  261. kKeyID, kInitialIV, {SubsampleEntry(16, 16), SubsampleEntry(16, 0)},
  262. absl::nullopt));
  263. expected.push_back(DecryptConfig::CreateCbcsConfig(
  264. kKeyID, kIVIncrementOne, {SubsampleEntry(16, 16)}, absl::nullopt));
  265. CheckSubsampleValues(
  266. kSuperframe, sizeof(kSuperframe),
  267. DecryptConfig::CreateCencConfig(
  268. kKeyID, kInitialIV, {SubsampleEntry(16, 16), SubsampleEntry(32, 16)}),
  269. expected);
  270. }
  271. // ┌────────────────────────────────────────┬────────────────────┐
  272. // │ frame 1 │ frame 2 │
  273. // ┝━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━┥
  274. // │ clear1 | cipher 1 │ clear 2 | cipher 2 │ clear 3 | cipher 3 │
  275. // └───────────────────┴────────────────────┴────────────────────┘
  276. TEST_F(Vp9ParserTest, FirstFrame2xSubsampleParsing) {
  277. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  278. const uint8_t superframe_marker_byte = make_marker_byte(true, 2);
  279. const uint8_t kSuperframe[] = {
  280. // First frame; 64 bytes.
  281. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  283. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  284. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  285. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  286. 0x00, 0x00, 0x00, 0x00,
  287. // Second frame; 32 bytes.
  288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  289. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  290. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  291. // Superframe marker goes before and after frame index.
  292. superframe_marker_byte,
  293. // First frame length (magnitude 1)
  294. 0x40,
  295. // Second frame length (magnigude 1)
  296. 0x20,
  297. // marker again.
  298. superframe_marker_byte};
  299. std::vector<std::unique_ptr<DecryptConfig>> expected;
  300. expected.push_back(DecryptConfig::CreateCbcsConfig(
  301. kKeyID, kInitialIV, {SubsampleEntry(16, 16), SubsampleEntry(16, 16)},
  302. absl::nullopt));
  303. expected.push_back(DecryptConfig::CreateCbcsConfig(
  304. kKeyID, kIVIncrementTwo, {SubsampleEntry(16, 16)}, absl::nullopt));
  305. CheckSubsampleValues(kSuperframe, sizeof(kSuperframe),
  306. DecryptConfig::CreateCencConfig(
  307. kKeyID, kInitialIV,
  308. {SubsampleEntry(16, 16), SubsampleEntry(16, 16),
  309. SubsampleEntry(16, 16)}),
  310. expected);
  311. }
  312. // ┌─────────────────────────────────────────────┬───────────────┐
  313. // │ frame 1 │ frame 2 │
  314. // ┝━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━┯━━━━┷━━━━━━━━━━━━━━━┥
  315. // │ clear1 | cipher 1 │ clear 2 | cipher 2 │ clear 3 | cipher 3 │
  316. // └───────────────────┴────────────────────┴────────────────────┘
  317. TEST_F(Vp9ParserTest, UnalignedBigFrameSubsampleParsing) {
  318. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  319. const uint8_t superframe_marker_byte = make_marker_byte(true, 2);
  320. const uint8_t kSuperframe[] = {
  321. // First frame; 72 bytes.
  322. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  323. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  324. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  325. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  326. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  327. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  328. // Second frame; 32 bytes.
  329. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  330. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  331. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  332. // Superframe marker goes before and after frame index.
  333. superframe_marker_byte,
  334. // First frame length (magnitude 1)
  335. 0x48,
  336. // Second frame length (magnigude 1)
  337. 0x20,
  338. // marker again.
  339. superframe_marker_byte};
  340. std::vector<std::unique_ptr<DecryptConfig>> expected;
  341. expected.push_back(DecryptConfig::CreateCbcsConfig(
  342. kKeyID, kInitialIV,
  343. {SubsampleEntry(16, 16), SubsampleEntry(16, 16), SubsampleEntry(8, 0)},
  344. absl::nullopt));
  345. expected.push_back(DecryptConfig::CreateCbcsConfig(
  346. kKeyID, kIVIncrementTwo, {SubsampleEntry(16, 16)}, absl::nullopt));
  347. CheckSubsampleValues(
  348. kSuperframe, sizeof(kSuperframe),
  349. DecryptConfig::CreateCencConfig(kKeyID, kInitialIV,
  350. {
  351. SubsampleEntry(16, 16),
  352. SubsampleEntry(16, 16),
  353. SubsampleEntry(24, 16),
  354. }),
  355. expected);
  356. }
  357. // ┌───────────────────┬────────────────────┐
  358. // │ frame 1 │ frame 2 │
  359. // ┝━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━┥
  360. // │ clear1 | cipher 1 │
  361. // └────────────────────────────────────────┘
  362. TEST_F(Vp9ParserTest, UnalignedInvalidSubsampleParsing) {
  363. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  364. const uint8_t superframe_marker_byte = make_marker_byte(true, 2);
  365. const uint8_t kSuperframe[] = {
  366. // First frame; 32 bytes.
  367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  368. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  369. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  370. // Second frame; 32 bytes.
  371. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  372. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  373. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  374. // Superframe marker goes before and after frame index.
  375. superframe_marker_byte,
  376. // First frame length (magnitude 1)
  377. 0x20,
  378. // Second frame length (magnigude 1)
  379. 0x20,
  380. // marker again.
  381. superframe_marker_byte};
  382. vp9_parser_->SetStream(kSuperframe, sizeof(kSuperframe),
  383. DecryptConfig::CreateCencConfig(
  384. kKeyID, kInitialIV, {SubsampleEntry(16, 32)}));
  385. ASSERT_EQ(vp9_parser_->NextFrameDecryptContextForTesting().get(), nullptr);
  386. }
  387. // ┌─────────────────────────────────────┬─────────┐
  388. // │ single frame in superframe │ marker │
  389. // ┝━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━┥
  390. // │ clear1 = 0 | cipher 1 │
  391. // └───────────────────────────────────────────────┘
  392. TEST_F(Vp9ParserTest, CipherBytesCoverSuperframeMarkerSubsampleParsing) {
  393. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  394. const uint8_t superframe_marker_byte = make_marker_byte(false, 1);
  395. const uint8_t kSuperframe[] = {
  396. // First frame; 44 bytes.
  397. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  398. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  399. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  400. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  401. // Superframe marker goes before and after frame index.
  402. superframe_marker_byte,
  403. // First frame length (magnitude 1)
  404. 0x20,
  405. // Second frame length (magnigude 1)
  406. 0x20,
  407. // marker again.
  408. superframe_marker_byte};
  409. vp9_parser_->SetStream(kSuperframe, sizeof(kSuperframe),
  410. DecryptConfig::CreateCencConfig(
  411. kKeyID, kInitialIV, {SubsampleEntry(0, 48)}));
  412. std::unique_ptr<DecryptConfig> actual =
  413. vp9_parser_->NextFrameDecryptContextForTesting();
  414. EXPECT_EQ(actual->iv(), kInitialIV);
  415. EXPECT_EQ(actual->subsamples().size(), 1lu);
  416. }
  417. // ┌─────────────────────────────────────┬─────────┐
  418. // │ single frame in superframe │ marker │
  419. // ┝━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━┥
  420. // │ clear1 │
  421. // └───────────────────────────────────────────────┘
  422. TEST_F(Vp9ParserTest, ClearBytesCoverSuperframeMarkerSubsampleParsing) {
  423. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  424. const uint8_t superframe_marker_byte = make_marker_byte(false, 1);
  425. const uint8_t kSuperframe[] = {
  426. // First frame; 44 bytes.
  427. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  428. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  429. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  430. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  431. // Superframe marker goes before and after frame index.
  432. superframe_marker_byte,
  433. // First frame length (magnitude 1)
  434. 0x20,
  435. // Second frame length (magnigude 1)
  436. 0x20,
  437. // marker again.
  438. superframe_marker_byte};
  439. vp9_parser_->SetStream(kSuperframe, sizeof(kSuperframe),
  440. DecryptConfig::CreateCencConfig(
  441. kKeyID, kInitialIV, {SubsampleEntry(48, 0)}));
  442. std::unique_ptr<DecryptConfig> actual =
  443. vp9_parser_->NextFrameDecryptContextForTesting();
  444. EXPECT_EQ(actual->iv(), kInitialIV);
  445. EXPECT_EQ(actual->subsamples().size(), 1lu);
  446. }
  447. // ┌─────────────────────────────────────┬─────────┐
  448. // │ single frame in superframe │ marker │
  449. // ┝━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┷━━━━━━━━━┥
  450. // │ clear 1 | cipher 1 │ clear 2 │
  451. // └────────────────────┴──────────────────────────┘
  452. TEST_F(Vp9ParserTest, SecondClearSubsampleSuperframeMarkerSubsampleParsing) {
  453. vp9_parser_ = std::make_unique<Vp9Parser>(false);
  454. const uint8_t superframe_marker_byte = make_marker_byte(false, 1);
  455. const uint8_t kSuperframe[] = {
  456. // First frame; 44 bytes.
  457. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  458. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  459. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  460. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  461. // Superframe marker goes before and after frame index.
  462. superframe_marker_byte,
  463. // First frame length (magnitude 1)
  464. 0x20,
  465. // Second frame length (magnigude 1)
  466. 0x20,
  467. // marker again.
  468. superframe_marker_byte};
  469. vp9_parser_->SetStream(
  470. kSuperframe, sizeof(kSuperframe),
  471. DecryptConfig::CreateCencConfig(kKeyID, kInitialIV,
  472. {
  473. SubsampleEntry(16, 16),
  474. SubsampleEntry(16, 0),
  475. }));
  476. std::unique_ptr<DecryptConfig> actual =
  477. vp9_parser_->NextFrameDecryptContextForTesting();
  478. EXPECT_EQ(actual->iv(), kInitialIV);
  479. EXPECT_EQ(actual->subsamples().size(), 2lu);
  480. }
  481. TEST_F(Vp9ParserTest, TestIncrementIV) {
  482. std::vector<std::tuple<char const*, uint32_t, char const*>> input_output = {
  483. {"--------aaaaaaaa", 1, "--------aaaaaaab"},
  484. {"--------aaaaaaa\377", 1, "--------aaaaaab\0"},
  485. {"--------aaaaaaa\377", 2, "--------aaaaaab\1"},
  486. {"--------\377\377\377\377\377\377\377\377", 2,
  487. "--------\0\0\0\0\0\0\0\1"}};
  488. for (auto& testcase : input_output) {
  489. EXPECT_EQ(
  490. vp9_parser_->IncrementIVForTesting(
  491. std::string(std::get<0>(testcase), 16), std::get<1>(testcase)),
  492. std::string(std::get<2>(testcase), 16));
  493. }
  494. }
  495. TEST_F(Vp9ParserTest, StreamFileParsingWithoutCompressedHeader) {
  496. Initialize("test-25fps.vp9", /*parsing_compressed_header=*/false,
  497. /*needs_external_context_update=*/false);
  498. // Number of frames in the test stream to be parsed.
  499. const int num_expected_frames = 269;
  500. int num_parsed_frames = 0;
  501. // Allow to parse twice as many frames in order to detect any extra frames
  502. // parsed.
  503. while (num_parsed_frames < num_expected_frames * 2) {
  504. Vp9FrameHeader fhdr;
  505. if (ParseNextFrame(&fhdr) != Vp9Parser::kOk)
  506. break;
  507. ++num_parsed_frames;
  508. }
  509. DVLOG(1) << "Number of successfully parsed frames before EOS: "
  510. << num_parsed_frames;
  511. EXPECT_EQ(num_expected_frames, num_parsed_frames);
  512. }
  513. TEST_F(Vp9ParserTest, StreamFileParsingWithCompressedHeader) {
  514. Initialize("test-25fps.vp9", /*parsing_compressed_header=*/true,
  515. /*needs_external_context_update=*/true);
  516. // Number of frames in the test stream to be parsed.
  517. const int num_expected_frames = 269;
  518. int num_parsed_frames = 0;
  519. // Allow to parse twice as many frames in order to detect any extra frames
  520. // parsed.
  521. while (num_parsed_frames < num_expected_frames * 2) {
  522. Vp9FrameHeader fhdr;
  523. if (ParseNextFrame(&fhdr) != Vp9Parser::kOk)
  524. break;
  525. Vp9FrameContext frame_context;
  526. ReadContext(&frame_context);
  527. EXPECT_TRUE(memcmp(&frame_context, &fhdr.initial_frame_context,
  528. sizeof(frame_context)) == 0);
  529. ReadContext(&frame_context);
  530. EXPECT_TRUE(memcmp(&frame_context, &fhdr.frame_context,
  531. sizeof(frame_context)) == 0);
  532. // test-25fps.vp9 doesn't need frame update from driver.
  533. auto context_refresh_cb = GetContextRefreshCb(fhdr);
  534. EXPECT_TRUE(!context_refresh_cb);
  535. ASSERT_FALSE(ReadShouldContextUpdate());
  536. ++num_parsed_frames;
  537. }
  538. DVLOG(1) << "Number of successfully parsed frames before EOS: "
  539. << num_parsed_frames;
  540. EXPECT_EQ(num_expected_frames, num_parsed_frames);
  541. }
  542. TEST_F(Vp9ParserTest, StreamFileParsingWithCompressedHeaderAndContextUpdate) {
  543. Initialize("bear-vp9.ivf", /*parsing_compressed_header=*/true,
  544. /*needs_external_context_update=*/true);
  545. // Number of frames in the test stream to be parsed.
  546. const int num_expected_frames = 82;
  547. int num_parsed_frames = 0;
  548. // Allow to parse twice as many frames in order to detect any extra frames
  549. // parsed.
  550. while (num_parsed_frames < num_expected_frames * 2) {
  551. Vp9FrameHeader fhdr;
  552. if (ParseNextFrame(&fhdr) != Vp9Parser::kOk)
  553. break;
  554. Vp9FrameContext frame_context;
  555. ReadContext(&frame_context);
  556. EXPECT_TRUE(memcmp(&frame_context, &fhdr.initial_frame_context,
  557. sizeof(frame_context)) == 0);
  558. ReadContext(&frame_context);
  559. EXPECT_TRUE(memcmp(&frame_context, &fhdr.frame_context,
  560. sizeof(frame_context)) == 0);
  561. bool should_update = ReadShouldContextUpdate();
  562. auto context_refresh_cb = GetContextRefreshCb(fhdr);
  563. if (!context_refresh_cb) {
  564. EXPECT_FALSE(should_update);
  565. } else {
  566. EXPECT_TRUE(should_update);
  567. ReadContext(&frame_context);
  568. std::move(context_refresh_cb).Run(frame_context);
  569. }
  570. ++num_parsed_frames;
  571. }
  572. DVLOG(1) << "Number of successfully parsed frames before EOS: "
  573. << num_parsed_frames;
  574. EXPECT_EQ(num_expected_frames, num_parsed_frames);
  575. }
  576. TEST_F(Vp9ParserTest, StreamFileParsingWithCompressedHeaderButNoContextUpdate) {
  577. Initialize("bear-vp9.ivf", /*parsing_compressed_header=*/true,
  578. /*needs_external_context_update=*/false);
  579. // Number of frames in the test stream to be parsed.
  580. constexpr int num_expected_frames = 82;
  581. int num_parsed_frames = 0;
  582. // Allow to parse twice as many frames in order to detect any extra frames
  583. // parsed.
  584. while (num_parsed_frames < num_expected_frames * 2) {
  585. Vp9FrameHeader fhdr;
  586. const auto parse_result = ParseNextFrame(&fhdr);
  587. // We shouldn't be waiting for a refresh.
  588. EXPECT_NE(Vp9Parser::kAwaitingRefresh, parse_result);
  589. if (parse_result != Vp9Parser::kOk)
  590. break;
  591. VerifyNoContextManagersNeedUpdate();
  592. ++num_parsed_frames;
  593. }
  594. DVLOG(1) << "Number of successfully parsed frames before EOS: "
  595. << num_parsed_frames;
  596. EXPECT_EQ(num_expected_frames, num_parsed_frames);
  597. }
  598. TEST_F(Vp9ParserTest, AwaitingContextUpdate) {
  599. Initialize("bear-vp9.ivf", /*parsing_compressed_header=*/true,
  600. /*needs_external_context_update=*/true);
  601. Vp9FrameHeader fhdr;
  602. ASSERT_EQ(Vp9Parser::kOk, ParseNextFrame(&fhdr));
  603. Vp9FrameContext frame_context;
  604. ReadContext(&frame_context);
  605. ReadContext(&frame_context);
  606. bool should_update = ReadShouldContextUpdate();
  607. ASSERT_TRUE(should_update);
  608. ReadContext(&frame_context);
  609. // Not update yet. Should return kAwaitingRefresh.
  610. EXPECT_EQ(Vp9Parser::kAwaitingRefresh, ParseNextFrame(&fhdr));
  611. EXPECT_EQ(Vp9Parser::kAwaitingRefresh, ParseNextFrame(&fhdr));
  612. // After update, parse should be ok.
  613. auto context_refresh_cb = GetContextRefreshCb(fhdr);
  614. EXPECT_FALSE(!context_refresh_cb);
  615. std::move(context_refresh_cb).Run(frame_context);
  616. EXPECT_EQ(Vp9Parser::kOk, ParseNextFrame(&fhdr));
  617. // Make sure it parsed the 2nd frame.
  618. EXPECT_EQ(9u, fhdr.header_size_in_bytes);
  619. }
  620. TEST_P(Vp9ParserTest, VerifyFirstFrame) {
  621. Initialize(GetParam().file_name, /*parsing_compressed_header=*/false,
  622. /*needs_external_context_update=*/false);
  623. Vp9FrameHeader fhdr;
  624. ASSERT_EQ(Vp9Parser::kOk, ParseNextFrame(&fhdr));
  625. EXPECT_EQ(GetParam().profile, fhdr.profile);
  626. EXPECT_FALSE(fhdr.show_existing_frame);
  627. EXPECT_EQ(Vp9FrameHeader::KEYFRAME, fhdr.frame_type);
  628. EXPECT_TRUE(fhdr.show_frame);
  629. EXPECT_FALSE(fhdr.error_resilient_mode);
  630. EXPECT_EQ(GetParam().bit_depth, fhdr.bit_depth);
  631. EXPECT_EQ(Vp9ColorSpace::UNKNOWN, fhdr.color_space);
  632. EXPECT_FALSE(fhdr.color_range);
  633. EXPECT_EQ(1, fhdr.subsampling_x);
  634. EXPECT_EQ(1, fhdr.subsampling_y);
  635. EXPECT_EQ(GetParam().width, fhdr.frame_width);
  636. EXPECT_EQ(GetParam().height, fhdr.frame_height);
  637. EXPECT_EQ(GetParam().width, fhdr.render_width);
  638. EXPECT_EQ(GetParam().height, fhdr.render_height);
  639. EXPECT_TRUE(fhdr.refresh_frame_context);
  640. EXPECT_EQ(GetParam().frame_parallel_decoding_mode,
  641. fhdr.frame_parallel_decoding_mode);
  642. EXPECT_EQ(0, fhdr.frame_context_idx_to_save_probs);
  643. const Vp9LoopFilterParams& lf = GetLoopFilter();
  644. EXPECT_EQ(GetParam().loop_filter_level, lf.level);
  645. EXPECT_EQ(0, lf.sharpness);
  646. EXPECT_TRUE(lf.delta_enabled);
  647. EXPECT_TRUE(lf.delta_update);
  648. EXPECT_TRUE(lf.update_ref_deltas[0]);
  649. EXPECT_EQ(1, lf.ref_deltas[0]);
  650. EXPECT_EQ(-1, lf.ref_deltas[2]);
  651. EXPECT_EQ(-1, lf.ref_deltas[3]);
  652. const Vp9QuantizationParams& qp = fhdr.quant_params;
  653. EXPECT_EQ(GetParam().quantization_base_index, qp.base_q_idx);
  654. EXPECT_FALSE(qp.delta_q_y_dc);
  655. EXPECT_FALSE(qp.delta_q_uv_dc);
  656. EXPECT_FALSE(qp.delta_q_uv_ac);
  657. EXPECT_FALSE(qp.IsLossless());
  658. const Vp9SegmentationParams& seg = GetSegmentation();
  659. EXPECT_FALSE(seg.enabled);
  660. EXPECT_EQ(0, fhdr.tile_cols_log2);
  661. EXPECT_EQ(0, fhdr.tile_rows_log2);
  662. EXPECT_EQ(GetParam().first_frame_header_size_bytes,
  663. fhdr.header_size_in_bytes);
  664. EXPECT_EQ(18u, fhdr.uncompressed_header_size);
  665. // Now verify the second frame in the file which should be INTERFRAME.
  666. ASSERT_EQ(Vp9Parser::kOk, ParseNextFrame(&fhdr));
  667. EXPECT_EQ(GetParam().bit_depth, fhdr.bit_depth);
  668. EXPECT_EQ(Vp9ColorSpace::UNKNOWN, fhdr.color_space);
  669. EXPECT_EQ(Vp9FrameHeader::INTERFRAME, fhdr.frame_type);
  670. EXPECT_FALSE(fhdr.show_frame);
  671. EXPECT_FALSE(fhdr.intra_only);
  672. EXPECT_FALSE(fhdr.reset_frame_context);
  673. EXPECT_TRUE(fhdr.RefreshFlag(2));
  674. EXPECT_EQ(0, fhdr.ref_frame_idx[0]);
  675. EXPECT_EQ(1, fhdr.ref_frame_idx[1]);
  676. EXPECT_EQ(2, fhdr.ref_frame_idx[2]);
  677. EXPECT_TRUE(fhdr.allow_high_precision_mv);
  678. EXPECT_EQ(GetParam().filter, fhdr.interpolation_filter);
  679. EXPECT_EQ(GetParam().second_frame_header_size_bytes,
  680. fhdr.header_size_in_bytes);
  681. EXPECT_EQ(GetParam().second_frame_uncompressed_header_size_bytes,
  682. fhdr.uncompressed_header_size);
  683. }
  684. INSTANTIATE_TEST_SUITE_P(All, Vp9ParserTest, ::testing::ValuesIn(kTestParams));
  685. TEST_F(Vp9ParserTest, CheckColorSpace) {
  686. Vp9FrameHeader fhdr{};
  687. EXPECT_FALSE(fhdr.GetColorSpace().IsSpecified());
  688. fhdr.color_space = Vp9ColorSpace::BT_709;
  689. EXPECT_EQ(VideoColorSpace::REC709(), fhdr.GetColorSpace());
  690. fhdr.color_space = Vp9ColorSpace::BT_601;
  691. EXPECT_EQ(VideoColorSpace::REC601(), fhdr.GetColorSpace());
  692. }
  693. } // namespace media