h264_decoder_unittest.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. // Copyright 2017 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. #include <stdint.h>
  5. #include <string.h>
  6. #include <cstring>
  7. #include <memory>
  8. #include <string>
  9. #include "base/check.h"
  10. #include "base/command_line.h"
  11. #include "base/containers/queue.h"
  12. #include "base/containers/span.h"
  13. #include "base/files/file_util.h"
  14. #include "base/memory/raw_ptr.h"
  15. #include "media/base/test_data_util.h"
  16. #include "media/gpu/h264_decoder.h"
  17. #include "testing/gmock/include/gmock/gmock.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. using ::testing::_;
  20. using ::testing::Args;
  21. using ::testing::Expectation;
  22. using ::testing::InSequence;
  23. using ::testing::MakeMatcher;
  24. using ::testing::Matcher;
  25. using ::testing::MatcherInterface;
  26. using ::testing::MatchResultListener;
  27. using ::testing::Mock;
  28. using ::testing::Return;
  29. namespace media {
  30. namespace {
  31. const std::string kBaselineFrame0 = "bear-320x192-baseline-frame-0.h264";
  32. const std::string kBaselineFrame1 = "bear-320x192-baseline-frame-1.h264";
  33. const std::string kBaselineFrame2 = "bear-320x192-baseline-frame-2.h264";
  34. const std::string kBaselineFrame3 = "bear-320x192-baseline-frame-3.h264";
  35. const std::string kHighFrame0 = "bear-320x192-high-frame-0.h264";
  36. const std::string kHighFrame1 = "bear-320x192-high-frame-1.h264";
  37. const std::string kHighFrame2 = "bear-320x192-high-frame-2.h264";
  38. const std::string kHighFrame3 = "bear-320x192-high-frame-3.h264";
  39. const std::string k10BitFrame0 = "bear-320x180-10bit-frame-0.h264";
  40. const std::string k10BitFrame1 = "bear-320x180-10bit-frame-1.h264";
  41. const std::string k10BitFrame2 = "bear-320x180-10bit-frame-2.h264";
  42. const std::string k10BitFrame3 = "bear-320x180-10bit-frame-3.h264";
  43. const std::string kYUV444Frame = "blackwhite_yuv444p-frame.h264";
  44. // Checks whether the decrypt config in the picture matches the decrypt config
  45. // passed to this matcher.
  46. MATCHER_P(DecryptConfigMatches, decrypt_config, "") {
  47. return arg->decrypt_config()->Matches(*decrypt_config);
  48. }
  49. MATCHER(SubsampleSizeMatches, "Verify subsample sizes match buffer size") {
  50. const size_t buffer_size = ::testing::get<0>(arg);
  51. const std::vector<SubsampleEntry>& subsamples = ::testing::get<1>(arg);
  52. size_t subsample_total_size = 0;
  53. for (const auto& sample : subsamples) {
  54. subsample_total_size += sample.cypher_bytes;
  55. subsample_total_size += sample.clear_bytes;
  56. }
  57. return subsample_total_size == buffer_size;
  58. }
  59. // Emulates encrypted slice header parsing. We don't actually encrypt the data
  60. // so we can easily do this by just parsing it.
  61. H264Decoder::H264Accelerator::Status ParseSliceHeader(
  62. const std::vector<base::span<const uint8_t>>& data,
  63. const std::vector<SubsampleEntry>& subsamples,
  64. const std::vector<uint8_t>& sps_nalu_data,
  65. const std::vector<uint8_t>& pps_nalu_data,
  66. H264SliceHeader* slice_hdr_out) {
  67. // Construct the bitstream for parsing.
  68. std::vector<uint8_t> full_data;
  69. const std::vector<uint8_t> start_code = {0u, 0u, 1u};
  70. full_data.insert(full_data.end(), start_code.begin(), start_code.end());
  71. full_data.insert(full_data.end(), sps_nalu_data.begin(), sps_nalu_data.end());
  72. full_data.insert(full_data.end(), start_code.begin(), start_code.end());
  73. full_data.insert(full_data.end(), pps_nalu_data.begin(), pps_nalu_data.end());
  74. for (const auto& span : data) {
  75. full_data.insert(full_data.end(), start_code.begin(), start_code.end());
  76. full_data.insert(full_data.end(), span.begin(), span.end());
  77. }
  78. H264Parser parser;
  79. parser.SetStream(full_data.data(), full_data.size());
  80. while (true) {
  81. H264NALU nalu;
  82. H264Parser::Result res = parser.AdvanceToNextNALU(&nalu);
  83. if (res == H264Parser::kEOStream)
  84. break;
  85. EXPECT_EQ(H264Parser::kOk, res);
  86. switch (nalu.nal_unit_type) {
  87. case H264NALU::kSPS:
  88. int sps_id;
  89. EXPECT_EQ(H264Parser::kOk, parser.ParseSPS(&sps_id));
  90. break;
  91. case H264NALU::kPPS:
  92. int pps_id;
  93. EXPECT_EQ(H264Parser::kOk, parser.ParsePPS(&pps_id));
  94. break;
  95. case H264NALU::kIDRSlice: // fallthrough
  96. case H264NALU::kNonIDRSlice:
  97. EXPECT_EQ(H264Parser::kOk,
  98. parser.ParseSliceHeader(nalu, slice_hdr_out));
  99. slice_hdr_out->full_sample_encryption = true;
  100. break;
  101. }
  102. }
  103. return H264Decoder::H264Accelerator::Status::kOk;
  104. }
  105. class MockH264Accelerator : public H264Decoder::H264Accelerator {
  106. public:
  107. MockH264Accelerator() = default;
  108. MOCK_METHOD0(CreateH264Picture, scoped_refptr<H264Picture>());
  109. MOCK_METHOD1(SubmitDecode, Status(scoped_refptr<H264Picture> pic));
  110. MOCK_METHOD3(ParseEncryptedSliceHeader,
  111. Status(const std::vector<base::span<const uint8_t>>& data,
  112. const std::vector<SubsampleEntry>& subsamples,
  113. H264SliceHeader* slice_hdr_out));
  114. MOCK_METHOD7(SubmitFrameMetadata,
  115. Status(const H264SPS* sps,
  116. const H264PPS* pps,
  117. const H264DPB& dpb,
  118. const H264Picture::Vector& ref_pic_listp0,
  119. const H264Picture::Vector& ref_pic_listb0,
  120. const H264Picture::Vector& ref_pic_listb1,
  121. scoped_refptr<H264Picture> pic));
  122. MOCK_METHOD8(SubmitSlice,
  123. Status(const H264PPS* pps,
  124. const H264SliceHeader* slice_hdr,
  125. const H264Picture::Vector& ref_pic_list0,
  126. const H264Picture::Vector& ref_pic_list1,
  127. scoped_refptr<H264Picture> pic,
  128. const uint8_t* data,
  129. size_t size,
  130. const std::vector<SubsampleEntry>& subsamples));
  131. MOCK_METHOD1(OutputPicture, bool(scoped_refptr<H264Picture> pic));
  132. MOCK_METHOD2(SetStream,
  133. Status(base::span<const uint8_t> stream,
  134. const DecryptConfig* decrypt_config));
  135. void Reset() override {}
  136. void ProcessSPS(const H264SPS* sps,
  137. base::span<const uint8_t> sps_nalu_data) override {
  138. last_sps_nalu_data.assign(sps_nalu_data.begin(), sps_nalu_data.end());
  139. }
  140. void ProcessPPS(const H264PPS* pps,
  141. base::span<const uint8_t> pps_nalu_data) override {
  142. last_pps_nalu_data.assign(pps_nalu_data.begin(), pps_nalu_data.end());
  143. }
  144. std::vector<uint8_t> last_sps_nalu_data;
  145. std::vector<uint8_t> last_pps_nalu_data;
  146. };
  147. // Test H264Decoder by feeding different of h264 frame sequences and make
  148. // sure it behaves as expected.
  149. class H264DecoderTest : public ::testing::Test {
  150. public:
  151. H264DecoderTest() = default;
  152. void SetUp() override;
  153. // Sets the bitstreams to be decoded, frame by frame. The content of each
  154. // file is the encoded bitstream of a single video frame.
  155. void SetInputFrameFiles(const std::vector<std::string>& frame_files);
  156. // Keeps decoding the input bitstream set at |SetInputFrameFiles| until the
  157. // decoder has consumed all bitstreams or returned from
  158. // |H264Decoder::Decode|. If |full_sample_encryption| is true, then it sets
  159. // a DecryptConfig for the the DecoderBuffer that indicates all but the first
  160. // byte are encrypted. Returns the same result as |H264Decoder::Decode|.
  161. AcceleratedVideoDecoder::DecodeResult Decode(
  162. bool full_sample_encryption = false);
  163. protected:
  164. std::unique_ptr<H264Decoder> decoder_;
  165. raw_ptr<MockH264Accelerator> accelerator_;
  166. private:
  167. base::queue<std::string> input_frame_files_;
  168. std::string bitstream_;
  169. scoped_refptr<DecoderBuffer> decoder_buffer_;
  170. };
  171. void H264DecoderTest::SetUp() {
  172. auto mock_accelerator = std::make_unique<MockH264Accelerator>();
  173. accelerator_ = mock_accelerator.get();
  174. decoder_ = std::make_unique<H264Decoder>(std::move(mock_accelerator),
  175. VIDEO_CODEC_PROFILE_UNKNOWN);
  176. // Sets default behaviors for mock methods for convenience.
  177. ON_CALL(*accelerator_, CreateH264Picture()).WillByDefault([]() {
  178. return new H264Picture();
  179. });
  180. ON_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _))
  181. .WillByDefault(Return(H264Decoder::H264Accelerator::Status::kOk));
  182. ON_CALL(*accelerator_, SubmitDecode(_))
  183. .WillByDefault(Return(H264Decoder::H264Accelerator::Status::kOk));
  184. ON_CALL(*accelerator_, OutputPicture(_)).WillByDefault(Return(true));
  185. ON_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _))
  186. .With(Args<6, 7>(SubsampleSizeMatches()))
  187. .WillByDefault(Return(H264Decoder::H264Accelerator::Status::kOk));
  188. ON_CALL(*accelerator_, SetStream(_, _))
  189. .WillByDefault(
  190. Return(H264Decoder::H264Accelerator::Status::kNotSupported));
  191. }
  192. void H264DecoderTest::SetInputFrameFiles(
  193. const std::vector<std::string>& input_frame_files) {
  194. for (auto f : input_frame_files)
  195. input_frame_files_.push(f);
  196. }
  197. AcceleratedVideoDecoder::DecodeResult H264DecoderTest::Decode(
  198. bool full_sample_encryption) {
  199. while (true) {
  200. auto result = decoder_->Decode();
  201. int32_t bitstream_id = 0;
  202. if (result != AcceleratedVideoDecoder::kRanOutOfStreamData ||
  203. input_frame_files_.empty())
  204. return result;
  205. auto input_file = GetTestDataFilePath(input_frame_files_.front());
  206. input_frame_files_.pop();
  207. CHECK(base::ReadFileToString(input_file, &bitstream_));
  208. decoder_buffer_ = DecoderBuffer::CopyFrom(
  209. reinterpret_cast<const uint8_t*>(bitstream_.data()), bitstream_.size());
  210. if (full_sample_encryption) {
  211. // We only use this in 2 tests, each use the same data where the offset to
  212. // the byte after the NALU type for the slice header is 669.
  213. constexpr int kOffsetToSliceHeader = 669;
  214. decoder_buffer_->set_decrypt_config(DecryptConfig::CreateCencConfig(
  215. "kFakeKeyId", std::string(DecryptConfig::kDecryptionKeySize, 'x'),
  216. {SubsampleEntry(kOffsetToSliceHeader,
  217. bitstream_.size() - kOffsetToSliceHeader)}));
  218. }
  219. EXPECT_NE(decoder_buffer_.get(), nullptr);
  220. decoder_->SetStream(bitstream_id++, *decoder_buffer_);
  221. }
  222. }
  223. // To have better description on mismatch.
  224. class WithPocMatcher : public MatcherInterface<scoped_refptr<H264Picture>> {
  225. public:
  226. explicit WithPocMatcher(int expected_poc) : expected_poc_(expected_poc) {}
  227. bool MatchAndExplain(scoped_refptr<H264Picture> p,
  228. MatchResultListener* listener) const override {
  229. if (p->pic_order_cnt == expected_poc_)
  230. return true;
  231. *listener << "with poc: " << p->pic_order_cnt;
  232. return false;
  233. }
  234. void DescribeTo(std::ostream* os) const override {
  235. *os << "with poc " << expected_poc_;
  236. }
  237. private:
  238. int expected_poc_;
  239. };
  240. inline Matcher<scoped_refptr<H264Picture>> WithPoc(int expected_poc) {
  241. return MakeMatcher(new WithPocMatcher(expected_poc));
  242. }
  243. // Test Cases
  244. TEST_F(H264DecoderTest, DecodeSingleFrame) {
  245. SetInputFrameFiles({kBaselineFrame0});
  246. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  247. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  248. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  249. EXPECT_EQ(8u, decoder_->GetBitDepth());
  250. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  251. EXPECT_CALL(*accelerator_, CreateH264Picture()).WillOnce(Return(nullptr));
  252. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfSurfaces, Decode());
  253. ASSERT_TRUE(Mock::VerifyAndClearExpectations(&*accelerator_));
  254. {
  255. InSequence sequence;
  256. EXPECT_CALL(*accelerator_, CreateH264Picture());
  257. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  258. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  259. EXPECT_CALL(*accelerator_, SubmitDecode(_));
  260. EXPECT_CALL(*accelerator_, OutputPicture(_));
  261. }
  262. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  263. ASSERT_TRUE(decoder_->Flush());
  264. }
  265. // This is for CENCv1 full sample encryption.
  266. TEST_F(H264DecoderTest, DecodeSingleEncryptedFrame) {
  267. SetInputFrameFiles({kBaselineFrame0});
  268. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode(true));
  269. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  270. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  271. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  272. {
  273. InSequence sequence;
  274. EXPECT_CALL(*accelerator_, ParseEncryptedSliceHeader(_, _, _))
  275. .WillOnce([this](const std::vector<base::span<const uint8_t>>& data,
  276. const std::vector<SubsampleEntry>& subsamples,
  277. H264SliceHeader* slice_hdr_out) {
  278. return ParseSliceHeader(
  279. data, subsamples, accelerator_->last_sps_nalu_data,
  280. accelerator_->last_pps_nalu_data, slice_hdr_out);
  281. });
  282. EXPECT_CALL(*accelerator_, CreateH264Picture());
  283. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  284. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  285. EXPECT_CALL(*accelerator_, SubmitDecode(_));
  286. EXPECT_CALL(*accelerator_, OutputPicture(_));
  287. }
  288. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  289. ASSERT_TRUE(decoder_->Flush());
  290. }
  291. TEST_F(H264DecoderTest, SkipNonIDRFrames) {
  292. SetInputFrameFiles({kBaselineFrame1, kBaselineFrame2, kBaselineFrame0});
  293. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  294. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  295. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  296. EXPECT_EQ(8u, decoder_->GetBitDepth());
  297. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  298. {
  299. InSequence sequence;
  300. EXPECT_CALL(*accelerator_, CreateH264Picture());
  301. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  302. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  303. EXPECT_CALL(*accelerator_, SubmitDecode(_));
  304. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  305. }
  306. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  307. ASSERT_TRUE(decoder_->Flush());
  308. }
  309. TEST_F(H264DecoderTest, DecodeProfileBaseline) {
  310. SetInputFrameFiles({
  311. kBaselineFrame0, kBaselineFrame1, kBaselineFrame2, kBaselineFrame3,
  312. });
  313. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  314. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  315. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  316. EXPECT_EQ(8u, decoder_->GetBitDepth());
  317. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  318. EXPECT_CALL(*accelerator_, CreateH264Picture()).Times(4);
  319. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _)).Times(4);
  320. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _)).Times(4);
  321. Expectation decode_poc0, decode_poc2, decode_poc4, decode_poc6;
  322. {
  323. InSequence decode_order;
  324. decode_poc0 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  325. decode_poc2 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(2)));
  326. decode_poc4 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(4)));
  327. decode_poc6 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(6)));
  328. }
  329. {
  330. InSequence display_order;
  331. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0))).After(decode_poc0);
  332. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(2))).After(decode_poc2);
  333. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(4))).After(decode_poc4);
  334. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(6))).After(decode_poc6);
  335. }
  336. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  337. ASSERT_TRUE(decoder_->Flush());
  338. }
  339. TEST_F(H264DecoderTest, Decode10BitStream) {
  340. SetInputFrameFiles({k10BitFrame0, k10BitFrame1, k10BitFrame2, k10BitFrame3});
  341. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  342. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  343. EXPECT_EQ(gfx::Rect(320, 180), decoder_->GetVisibleRect());
  344. EXPECT_EQ(H264PROFILE_HIGH10PROFILE, decoder_->GetProfile());
  345. EXPECT_EQ(10u, decoder_->GetBitDepth());
  346. EXPECT_LE(14u, decoder_->GetRequiredNumOfPictures());
  347. // One picture will be kept in the DPB for reordering. The second picture
  348. // should be outputted after feeding the third and fourth frames.
  349. EXPECT_CALL(*accelerator_, CreateH264Picture()).Times(4);
  350. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _)).Times(4);
  351. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _)).Times(4);
  352. Expectation decode_poc0, decode_poc2, decode_poc4, decode_poc6;
  353. {
  354. InSequence decode_order;
  355. decode_poc0 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  356. decode_poc6 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(6)));
  357. decode_poc2 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(2)));
  358. decode_poc4 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(4)));
  359. }
  360. {
  361. InSequence display_order;
  362. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0))).After(decode_poc0);
  363. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(2))).After(decode_poc2);
  364. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(4))).After(decode_poc4);
  365. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(6))).After(decode_poc6);
  366. }
  367. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  368. ASSERT_TRUE(decoder_->Flush());
  369. }
  370. TEST_F(H264DecoderTest, OutputPictureFailureCausesDecodeToFail) {
  371. // Provide enough data that Decode() will try to output a frame.
  372. SetInputFrameFiles({
  373. kBaselineFrame0,
  374. kBaselineFrame1,
  375. });
  376. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  377. EXPECT_CALL(*accelerator_, OutputPicture(_)).WillRepeatedly(Return(false));
  378. ASSERT_EQ(AcceleratedVideoDecoder::kDecodeError, Decode());
  379. }
  380. TEST_F(H264DecoderTest, DecodeProfileHigh) {
  381. SetInputFrameFiles({kHighFrame0, kHighFrame1, kHighFrame2, kHighFrame3});
  382. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  383. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  384. EXPECT_EQ(H264PROFILE_HIGH, decoder_->GetProfile());
  385. EXPECT_EQ(8u, decoder_->GetBitDepth());
  386. EXPECT_LE(16u, decoder_->GetRequiredNumOfPictures());
  387. // Two pictures will be kept in the DPB for reordering. The first picture
  388. // should be outputted after feeding the third frame.
  389. EXPECT_CALL(*accelerator_, CreateH264Picture()).Times(4);
  390. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _)).Times(4);
  391. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _)).Times(4);
  392. Expectation decode_poc0, decode_poc2, decode_poc4, decode_poc6;
  393. {
  394. InSequence decode_order;
  395. decode_poc0 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  396. decode_poc4 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(4)));
  397. decode_poc2 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(2)));
  398. decode_poc6 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(6)));
  399. }
  400. {
  401. InSequence display_order;
  402. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0))).After(decode_poc0);
  403. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(2))).After(decode_poc2);
  404. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(4))).After(decode_poc4);
  405. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(6))).After(decode_poc6);
  406. }
  407. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  408. ASSERT_TRUE(decoder_->Flush());
  409. }
  410. TEST_F(H264DecoderTest, DenyDecodeNonYUV420) {
  411. // YUV444 frame causes kDecodeError.
  412. SetInputFrameFiles({kYUV444Frame});
  413. ASSERT_EQ(AcceleratedVideoDecoder::kDecodeError, Decode());
  414. }
  415. TEST_F(H264DecoderTest, SwitchBaselineToHigh) {
  416. SetInputFrameFiles({
  417. kBaselineFrame0, kHighFrame0, kHighFrame1, kHighFrame2, kHighFrame3,
  418. });
  419. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  420. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  421. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  422. EXPECT_EQ(8u, decoder_->GetBitDepth());
  423. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  424. {
  425. InSequence sequence;
  426. EXPECT_CALL(*accelerator_, CreateH264Picture());
  427. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  428. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  429. EXPECT_CALL(*accelerator_, SubmitDecode(_));
  430. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  431. }
  432. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  433. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  434. EXPECT_EQ(H264PROFILE_HIGH, decoder_->GetProfile());
  435. EXPECT_EQ(8u, decoder_->GetBitDepth());
  436. EXPECT_LE(16u, decoder_->GetRequiredNumOfPictures());
  437. ASSERT_TRUE(Mock::VerifyAndClearExpectations(&*accelerator_));
  438. EXPECT_CALL(*accelerator_, CreateH264Picture()).Times(4);
  439. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _)).Times(4);
  440. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _)).Times(4);
  441. Expectation decode_poc0, decode_poc2, decode_poc4, decode_poc6;
  442. {
  443. InSequence decode_order;
  444. decode_poc0 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  445. decode_poc4 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(4)));
  446. decode_poc2 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(2)));
  447. decode_poc6 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(6)));
  448. }
  449. {
  450. InSequence display_order;
  451. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0))).After(decode_poc0);
  452. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(2))).After(decode_poc2);
  453. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(4))).After(decode_poc4);
  454. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(6))).After(decode_poc6);
  455. }
  456. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  457. ASSERT_TRUE(decoder_->Flush());
  458. }
  459. TEST_F(H264DecoderTest, SwitchHighToBaseline) {
  460. SetInputFrameFiles({
  461. kHighFrame0, kBaselineFrame0, kBaselineFrame1, kBaselineFrame2,
  462. kBaselineFrame3,
  463. });
  464. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  465. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  466. EXPECT_EQ(H264PROFILE_HIGH, decoder_->GetProfile());
  467. EXPECT_EQ(8u, decoder_->GetBitDepth());
  468. EXPECT_LE(16u, decoder_->GetRequiredNumOfPictures());
  469. {
  470. InSequence sequence;
  471. EXPECT_CALL(*accelerator_, CreateH264Picture());
  472. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  473. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  474. EXPECT_CALL(*accelerator_, SubmitDecode(_));
  475. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  476. }
  477. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  478. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  479. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  480. EXPECT_EQ(8u, decoder_->GetBitDepth());
  481. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  482. ASSERT_TRUE(Mock::VerifyAndClearExpectations(&*accelerator_));
  483. EXPECT_CALL(*accelerator_, CreateH264Picture()).Times(4);
  484. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _)).Times(4);
  485. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _)).Times(4);
  486. Expectation decode_poc0, decode_poc2, decode_poc4, decode_poc6;
  487. {
  488. InSequence decode_order;
  489. decode_poc0 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  490. decode_poc2 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(2)));
  491. decode_poc4 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(4)));
  492. decode_poc6 = EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(6)));
  493. }
  494. {
  495. InSequence display_order;
  496. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0))).After(decode_poc0);
  497. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(2))).After(decode_poc2);
  498. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(4))).After(decode_poc4);
  499. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(6))).After(decode_poc6);
  500. }
  501. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  502. ASSERT_TRUE(decoder_->Flush());
  503. }
  504. TEST_F(H264DecoderTest, SwitchYUV420ToNonYUV420) {
  505. SetInputFrameFiles({kBaselineFrame0, kYUV444Frame});
  506. // The first frame, YUV420, is decoded with no error.
  507. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  508. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  509. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  510. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  511. {
  512. InSequence sequence;
  513. EXPECT_CALL(*accelerator_, CreateH264Picture());
  514. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  515. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  516. EXPECT_CALL(*accelerator_, SubmitDecode(_));
  517. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  518. }
  519. // The second frame, YUV444, causes kDecodeError.
  520. ASSERT_EQ(AcceleratedVideoDecoder::kDecodeError, Decode());
  521. }
  522. // Verify that the decryption config is passed to the accelerator.
  523. TEST_F(H264DecoderTest, SetEncryptedStream) {
  524. std::string bitstream;
  525. auto input_file = GetTestDataFilePath(kBaselineFrame0);
  526. CHECK(base::ReadFileToString(input_file, &bitstream));
  527. const char kAnyKeyId[] = "any_16byte_keyid";
  528. const char kAnyIv[] = "any_16byte_iv___";
  529. const std::vector<SubsampleEntry> subsamples = {
  530. // No encrypted bytes. This test only checks whether the data is passed
  531. // thru to the acclerator so making this completely clear.
  532. {static_cast<uint32_t>(bitstream.size()), 0},
  533. };
  534. std::unique_ptr<DecryptConfig> decrypt_config =
  535. DecryptConfig::CreateCencConfig(kAnyKeyId, kAnyIv, subsamples);
  536. EXPECT_CALL(*accelerator_,
  537. SubmitFrameMetadata(_, _, _, _, _, _,
  538. DecryptConfigMatches(decrypt_config.get())))
  539. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kOk));
  540. EXPECT_CALL(*accelerator_,
  541. SubmitDecode(DecryptConfigMatches(decrypt_config.get())))
  542. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kOk));
  543. auto buffer = DecoderBuffer::CopyFrom(
  544. reinterpret_cast<const uint8_t*>(bitstream.data()), bitstream.size());
  545. ASSERT_NE(buffer.get(), nullptr);
  546. buffer->set_decrypt_config(std::move(decrypt_config));
  547. decoder_->SetStream(0, *buffer);
  548. EXPECT_EQ(AcceleratedVideoDecoder::kConfigChange, decoder_->Decode());
  549. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  550. EXPECT_EQ(8u, decoder_->GetBitDepth());
  551. EXPECT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, decoder_->Decode());
  552. EXPECT_TRUE(decoder_->Flush());
  553. }
  554. TEST_F(H264DecoderTest, ParseEncryptedSliceHeaderRetry) {
  555. SetInputFrameFiles({kBaselineFrame0});
  556. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode(true));
  557. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  558. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  559. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  560. EXPECT_CALL(*accelerator_, ParseEncryptedSliceHeader(_, _, _))
  561. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain));
  562. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode(true));
  563. // Try again, assuming key still not set. Only ParseEncryptedSliceHeader()
  564. // should be called again.
  565. EXPECT_CALL(*accelerator_, ParseEncryptedSliceHeader(_, _, _))
  566. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain));
  567. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode(true));
  568. // Assume key has been provided now, next call to Decode() should proceed.
  569. {
  570. InSequence sequence;
  571. EXPECT_CALL(*accelerator_, ParseEncryptedSliceHeader(_, _, _))
  572. .WillOnce([this](const std::vector<base::span<const uint8_t>>& data,
  573. const std::vector<SubsampleEntry>& subsamples,
  574. H264SliceHeader* slice_hdr_out) {
  575. return ParseSliceHeader(
  576. data, subsamples, accelerator_->last_sps_nalu_data,
  577. accelerator_->last_pps_nalu_data, slice_hdr_out);
  578. });
  579. EXPECT_CALL(*accelerator_, CreateH264Picture());
  580. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  581. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  582. EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  583. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  584. }
  585. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode(true));
  586. ASSERT_TRUE(decoder_->Flush());
  587. }
  588. TEST_F(H264DecoderTest, SubmitFrameMetadataRetry) {
  589. SetInputFrameFiles({kBaselineFrame0});
  590. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  591. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  592. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  593. EXPECT_EQ(8u, decoder_->GetBitDepth());
  594. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  595. {
  596. InSequence sequence;
  597. EXPECT_CALL(*accelerator_, CreateH264Picture());
  598. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _))
  599. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain));
  600. }
  601. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode());
  602. // Try again, assuming key still not set. Only SubmitFrameMetadata()
  603. // should be called again.
  604. EXPECT_CALL(*accelerator_, CreateH264Picture()).Times(0);
  605. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _))
  606. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain));
  607. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode());
  608. // Assume key has been provided now, next call to Decode() should proceed.
  609. {
  610. InSequence sequence;
  611. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  612. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  613. EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  614. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  615. }
  616. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  617. ASSERT_TRUE(decoder_->Flush());
  618. }
  619. TEST_F(H264DecoderTest, SubmitSliceRetry) {
  620. SetInputFrameFiles({kBaselineFrame0});
  621. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  622. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  623. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  624. EXPECT_EQ(8u, decoder_->GetBitDepth());
  625. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  626. {
  627. InSequence sequence;
  628. EXPECT_CALL(*accelerator_, CreateH264Picture());
  629. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  630. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _))
  631. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain));
  632. }
  633. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode());
  634. // Try again, assuming key still not set. Only SubmitSlice() should be
  635. // called again.
  636. EXPECT_CALL(*accelerator_, CreateH264Picture()).Times(0);
  637. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _)).Times(0);
  638. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _))
  639. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain));
  640. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode());
  641. // Assume key has been provided now, next call to Decode() should proceed.
  642. {
  643. InSequence sequence;
  644. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  645. EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  646. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  647. }
  648. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  649. ASSERT_TRUE(decoder_->Flush());
  650. }
  651. TEST_F(H264DecoderTest, SubmitDecodeRetry) {
  652. SetInputFrameFiles({kBaselineFrame0, kBaselineFrame1});
  653. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  654. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  655. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  656. EXPECT_EQ(8u, decoder_->GetBitDepth());
  657. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  658. {
  659. InSequence sequence;
  660. EXPECT_CALL(*accelerator_, CreateH264Picture());
  661. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  662. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  663. EXPECT_CALL(*accelerator_, SubmitDecode(_))
  664. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain));
  665. }
  666. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode());
  667. // Try again, assuming key still not set. Only SubmitDecode() should be
  668. // called again.
  669. EXPECT_CALL(*accelerator_, CreateH264Picture()).Times(0);
  670. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _)).Times(0);
  671. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _)).Times(0);
  672. EXPECT_CALL(*accelerator_, SubmitDecode(_))
  673. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain));
  674. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode());
  675. // Assume key has been provided now, next call to Decode() should output
  676. // the first frame.
  677. {
  678. InSequence sequence;
  679. EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  680. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  681. EXPECT_CALL(*accelerator_, CreateH264Picture());
  682. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  683. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  684. EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(2)));
  685. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(2)));
  686. }
  687. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  688. ASSERT_TRUE(decoder_->Flush());
  689. }
  690. TEST_F(H264DecoderTest, SetStreamRetry) {
  691. SetInputFrameFiles({kBaselineFrame0});
  692. EXPECT_CALL(*accelerator_, SetStream(_, _))
  693. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kTryAgain))
  694. .WillOnce(Return(H264Decoder::H264Accelerator::Status::kOk));
  695. ASSERT_EQ(AcceleratedVideoDecoder::kTryAgain, Decode());
  696. ASSERT_EQ(AcceleratedVideoDecoder::kConfigChange, Decode());
  697. EXPECT_EQ(gfx::Size(320, 192), decoder_->GetPicSize());
  698. EXPECT_EQ(H264PROFILE_BASELINE, decoder_->GetProfile());
  699. EXPECT_EQ(8u, decoder_->GetBitDepth());
  700. EXPECT_LE(9u, decoder_->GetRequiredNumOfPictures());
  701. {
  702. InSequence sequence;
  703. EXPECT_CALL(*accelerator_, CreateH264Picture());
  704. EXPECT_CALL(*accelerator_, SubmitFrameMetadata(_, _, _, _, _, _, _));
  705. EXPECT_CALL(*accelerator_, SubmitSlice(_, _, _, _, _, _, _, _));
  706. EXPECT_CALL(*accelerator_, SubmitDecode(WithPoc(0)));
  707. EXPECT_CALL(*accelerator_, OutputPicture(WithPoc(0)));
  708. }
  709. ASSERT_EQ(AcceleratedVideoDecoder::kRanOutOfStreamData, Decode());
  710. ASSERT_TRUE(decoder_->Flush());
  711. }
  712. } // namespace
  713. } // namespace media