h264_annex_b_to_avc_bitstream_converter_unittest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2020 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 "media/formats/mp4/h264_annex_b_to_avc_bitstream_converter.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "base/files/file.h"
  8. #include "base/path_service.h"
  9. #include "media/formats/mp4/box_definitions.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace {
  12. std::vector<uint8_t> ReadTestFile(std::string name) {
  13. base::FilePath path;
  14. base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
  15. path = path.Append(FILE_PATH_LITERAL(
  16. "media/formats/mp4/h264_annex_b_fuzz_corpus"))
  17. .AppendASCII(name);
  18. base::File f(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
  19. DCHECK(f.IsValid()) << "path: " << path.AsUTF8Unsafe();
  20. auto size = f.GetLength();
  21. std::vector<uint8_t> result(size);
  22. if (size > 0)
  23. f.ReadAtCurrentPosAndCheck(result);
  24. return result;
  25. }
  26. } // namespace
  27. namespace media {
  28. TEST(H264AnnexBToAvcBitstreamConverterTest, Success) {
  29. std::string chunks[] = {
  30. "chunk1-config-idr.bin", "chunk2-non-idr.bin",
  31. "chunk3-non-idr.bin", "chunk4-non-idr.bin",
  32. "chunk5-non-idr.bin", "chunk6-config-idr.bin",
  33. "chunk7-non-idr.bin", "pps_neq_sps_config_idr.bin"};
  34. H264AnnexBToAvcBitstreamConverter converter;
  35. for (std::string& name : chunks) {
  36. SCOPED_TRACE(name);
  37. auto input = ReadTestFile(name);
  38. SCOPED_TRACE(input.size());
  39. std::vector<uint8_t> output;
  40. size_t desired_size = 0;
  41. bool config_changed = false;
  42. auto status =
  43. converter.ConvertChunk(input, output, &config_changed, &desired_size);
  44. ASSERT_EQ(status.code(), MP4Status::Codes::kBufferTooSmall);
  45. output.resize(desired_size);
  46. status = converter.ConvertChunk(input, output, &config_changed, nullptr);
  47. EXPECT_TRUE(status.is_ok()) << status.message();
  48. auto& config = converter.GetCurrentConfig();
  49. if (name.find("config") != std::string::npos) {
  50. // Chunks with configuration
  51. EXPECT_TRUE(config_changed);
  52. EXPECT_EQ(config.version, 1);
  53. EXPECT_EQ(config.profile_indication, 66);
  54. EXPECT_EQ(config.profile_compatibility, 3);
  55. EXPECT_EQ(config.avc_level, 30);
  56. EXPECT_EQ(config.length_size, 4);
  57. EXPECT_EQ(config.sps_list.size(), 1ul);
  58. EXPECT_EQ(config.pps_list.size(), 1ul);
  59. } else {
  60. EXPECT_FALSE(config_changed);
  61. }
  62. std::vector<uint8_t> config_bin;
  63. EXPECT_TRUE(config.Serialize(config_bin)) << " file: " << name;
  64. }
  65. }
  66. // Tests that stream can contain multiple picture parameter sets and switch
  67. // between them without having to reconfigure the decoder.
  68. TEST(H264AnnexBToAvcBitstreamConverterTest, PPS_SwitchWithoutReconfig) {
  69. std::vector<uint8_t> sps{0x00, 0x00, 0x00, 0x01, 0x27, 0x42, 0x00, 0x1E,
  70. 0x89, 0x8A, 0x12, 0x05, 0x01, 0x7F, 0xCA, 0x80};
  71. std::vector<uint8_t> pps1{0x00, 0x00, 0x00, 0x01, 0x28, 0xce, 0x3c, 0x80};
  72. std::vector<uint8_t> pps2{0x00, 0x00, 0x00, 0x01, 0x28, 0x53, 0x8f, 0x20};
  73. std::vector<uint8_t> pps3{0x00, 0x00, 0x00, 0x01, 0x28, 0x73, 0x8F, 0x20};
  74. std::vector<uint8_t> first_frame_idr{
  75. 0x00, 0x00, 0x00, 0x01, 0x25, 0xb4, 0x00, 0x10, 0x00, 0x24, 0xff,
  76. 0xff, 0xf8, 0x7a, 0x28, 0x00, 0x08, 0x0a, 0x7b, 0xdd
  77. // Encoded data omitted here, it's not important for NALU parsing
  78. };
  79. std::vector<uint8_t> first_chunk;
  80. first_chunk.insert(first_chunk.end(), sps.begin(), sps.end());
  81. first_chunk.insert(first_chunk.end(), pps1.begin(), pps1.end());
  82. first_chunk.insert(first_chunk.end(), pps2.begin(), pps2.end());
  83. first_chunk.insert(first_chunk.end(), pps3.begin(), pps3.end());
  84. first_chunk.insert(first_chunk.end(), first_frame_idr.begin(),
  85. first_frame_idr.end());
  86. std::vector<uint8_t> second_non_idr_chunk{
  87. 0x00, 0x00, 0x00, 0x01, 0x21, 0xd8, 0x00, 0x80, 0x04,
  88. 0x95, 0x9d, 0x45, 0x70, 0xd9, 0xbe, 0x21, 0xff, 0x87,
  89. 0x20, 0x03, 0x9c, 0x66, 0x84, 0xe1
  90. // Encoded data omitted here, it's not important for NALU parsing
  91. };
  92. H264AnnexBToAvcBitstreamConverter converter;
  93. std::vector<uint8_t> output(10000);
  94. bool config_changed = false;
  95. auto status =
  96. converter.ConvertChunk(first_chunk, output, &config_changed, nullptr);
  97. EXPECT_TRUE(status.is_ok()) << status.message();
  98. EXPECT_TRUE(config_changed);
  99. status = converter.ConvertChunk(second_non_idr_chunk, output, &config_changed,
  100. nullptr);
  101. EXPECT_TRUE(status.is_ok()) << status.message();
  102. EXPECT_FALSE(config_changed);
  103. }
  104. TEST(H264AnnexBToAvcBitstreamConverterTest, Failure) {
  105. H264AnnexBToAvcBitstreamConverter converter;
  106. std::vector<uint8_t> input{0x0, 0x0, 0x0, 0x1, 0x9, 0x10,
  107. 0x0, 0x0, 0x0, 0x1, 0x67, 0x42};
  108. std::vector<uint8_t> output(input.size());
  109. auto status = converter.ConvertChunk(input, output, nullptr, nullptr);
  110. ASSERT_EQ(status.code(), MP4Status::Codes::kInvalidSPS);
  111. }
  112. } // namespace media