| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <sstream> |
| 6 #include <string> |
| 6 #include <vector> | 7 #include <vector> |
| 7 | 8 |
| 8 #include "base/bind.h" | 9 #include "base/bind.h" |
| 9 #include "base/command_line.h" | |
| 10 #include "base/files/memory_mapped_file.h" | |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/path_service.h" | 11 #include "base/strings/string_util.h" |
| 13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 14 #include "media/base/stream_parser_buffer.h" | 13 #include "media/base/stream_parser_buffer.h" |
| 15 #include "media/base/test_data_util.h" | |
| 16 #include "media/filters/h264_parser.h" | 14 #include "media/filters/h264_parser.h" |
| 17 #include "media/formats/mp2t/es_parser_h264.h" | 15 #include "media/formats/mp2t/es_parser_h264.h" |
| 16 #include "media/formats/mp2t/es_parser_test_base.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| 20 namespace media { | 19 namespace media { |
| 21 class VideoDecoderConfig; | 20 class VideoDecoderConfig; |
| 22 | 21 |
| 23 namespace mp2t { | 22 namespace mp2t { |
| 24 | 23 |
| 25 namespace { | 24 class EsParserH264Test : public EsParserTestBase, |
| 25 public testing::Test { |
| 26 public: |
| 27 EsParserH264Test() {} |
| 28 virtual ~EsParserH264Test() {} |
| 26 | 29 |
| 27 struct Packet { | 30 protected: |
| 28 // Offset in the stream. | 31 void LoadH264Stream(const char* filename); |
| 29 size_t offset; | 32 void GetPesTimestamps(std::vector<Packet>* pes_packets); |
| 33 bool Process(const std::vector<Packet>& pes_packets, bool force_timing); |
| 34 void CheckAccessUnits(); |
| 30 | 35 |
| 31 // Size of the packet. | 36 // Access units of the stream with AUD NALUs. |
| 32 size_t size; | 37 std::vector<Packet> access_units_; |
| 33 | 38 |
| 34 // Timestamp of the packet. | 39 private: |
| 35 base::TimeDelta pts; | 40 // Get the offset of the start of each access unit of |stream_|. |
| 41 // This function assumes there is only one slice per access unit. |
| 42 // This is a very simplified access unit segmenter that is good |
| 43 // enough for unit tests. |
| 44 void GetAccessUnits(); |
| 45 |
| 46 // Insert an AUD before each access unit. |
| 47 // Update |stream_| and |access_units_| accordingly. |
| 48 void InsertAUD(); |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(EsParserH264Test); |
| 36 }; | 51 }; |
| 37 | 52 |
| 38 // Compute the size of each packet assuming packets are given in stream order | 53 void EsParserH264Test::LoadH264Stream(const char* filename) { |
| 39 // and the last packet covers the end of the stream. | 54 // Load the input H264 file and segment it into access units. |
| 40 void ComputePacketSize(std::vector<Packet>& packets, size_t stream_size) { | 55 LoadStream(filename); |
| 41 for (size_t k = 0; k < packets.size() - 1; k++) { | 56 GetAccessUnits(); |
| 42 DCHECK_GE(packets[k + 1].offset, packets[k].offset); | 57 ASSERT_GT(access_units_.size(), 0u); |
| 43 packets[k].size = packets[k + 1].offset - packets[k].offset; | 58 |
| 44 } | 59 // Insert AUDs into the stream. |
| 45 packets[packets.size() - 1].size = | 60 InsertAUD(); |
| 46 stream_size - packets[packets.size() - 1].offset; | 61 |
| 62 // Generate some timestamps based on a 25fps stream. |
| 63 for (size_t k = 0; k < access_units_.size(); k++) |
| 64 access_units_[k].pts = base::TimeDelta::FromMilliseconds(k * 40u); |
| 47 } | 65 } |
| 48 | 66 |
| 49 // Get the offset of the start of each access unit. | 67 void EsParserH264Test::GetAccessUnits() { |
| 50 // This function assumes there is only one slice per access unit. | 68 access_units_.resize(0); |
| 51 // This is a very simplified access unit segmenter that is good | |
| 52 // enough for unit tests. | |
| 53 std::vector<Packet> GetAccessUnits(const uint8* stream, size_t stream_size) { | |
| 54 std::vector<Packet> access_units; | |
| 55 bool start_access_unit = true; | 69 bool start_access_unit = true; |
| 56 | 70 |
| 57 // In a first pass, retrieve the offsets of all access units. | 71 // In a first pass, retrieve the offsets of all access units. |
| 58 size_t offset = 0; | 72 size_t offset = 0; |
| 59 while (true) { | 73 while (true) { |
| 60 // Find the next start code. | 74 // Find the next start code. |
| 61 off_t relative_offset = 0; | 75 off_t relative_offset = 0; |
| 62 off_t start_code_size = 0; | 76 off_t start_code_size = 0; |
| 63 bool success = H264Parser::FindStartCode( | 77 bool success = H264Parser::FindStartCode( |
| 64 &stream[offset], stream_size - offset, | 78 &stream_[offset], stream_.size() - offset, |
| 65 &relative_offset, &start_code_size); | 79 &relative_offset, &start_code_size); |
| 66 if (!success) | 80 if (!success) |
| 67 break; | 81 break; |
| 68 offset += relative_offset; | 82 offset += relative_offset; |
| 69 | 83 |
| 70 if (start_access_unit) { | 84 if (start_access_unit) { |
| 71 Packet cur_access_unit; | 85 Packet cur_access_unit; |
| 72 cur_access_unit.offset = offset; | 86 cur_access_unit.offset = offset; |
| 73 access_units.push_back(cur_access_unit); | 87 access_units_.push_back(cur_access_unit); |
| 74 start_access_unit = false; | 88 start_access_unit = false; |
| 75 } | 89 } |
| 76 | 90 |
| 77 // Get the NALU type. | 91 // Get the NALU type. |
| 78 offset += start_code_size; | 92 offset += start_code_size; |
| 79 if (offset >= stream_size) | 93 if (offset >= stream_.size()) |
| 80 break; | 94 break; |
| 81 int nal_unit_type = stream[offset] & 0x1f; | 95 int nal_unit_type = stream_[offset] & 0x1f; |
| 82 | 96 |
| 83 // We assume there is only one slice per access unit. | 97 // We assume there is only one slice per access unit. |
| 84 if (nal_unit_type == H264NALU::kIDRSlice || | 98 if (nal_unit_type == H264NALU::kIDRSlice || |
| 85 nal_unit_type == H264NALU::kNonIDRSlice) { | 99 nal_unit_type == H264NALU::kNonIDRSlice) { |
| 86 start_access_unit = true; | 100 start_access_unit = true; |
| 87 } | 101 } |
| 88 } | 102 } |
| 89 | 103 |
| 90 ComputePacketSize(access_units, stream_size); | 104 ComputePacketSize(&access_units_); |
| 91 return access_units; | |
| 92 } | 105 } |
| 93 | 106 |
| 94 // Append an AUD NALU at the beginning of each access unit | 107 void EsParserH264Test::InsertAUD() { |
| 95 // needed for streams which do not already have AUD NALUs. | |
| 96 void AppendAUD( | |
| 97 const uint8* stream, size_t stream_size, | |
| 98 const std::vector<Packet>& access_units, | |
| 99 std::vector<uint8>& stream_with_aud, | |
| 100 std::vector<Packet>& access_units_with_aud) { | |
| 101 uint8 aud[] = { 0x00, 0x00, 0x01, 0x09 }; | 108 uint8 aud[] = { 0x00, 0x00, 0x01, 0x09 }; |
| 102 stream_with_aud.resize(stream_size + access_units.size() * sizeof(aud)); | 109 |
| 103 access_units_with_aud.resize(access_units.size()); | 110 std::vector<uint8> stream_with_aud( |
| 111 stream_.size() + access_units_.size() * sizeof(aud)); |
| 112 std::vector<EsParserTestBase::Packet> access_units_with_aud( |
| 113 access_units_.size()); |
| 104 | 114 |
| 105 size_t offset = 0; | 115 size_t offset = 0; |
| 106 for (size_t k = 0; k < access_units.size(); k++) { | 116 for (size_t k = 0; k < access_units_.size(); k++) { |
| 107 access_units_with_aud[k].offset = offset; | 117 access_units_with_aud[k].offset = offset; |
| 108 access_units_with_aud[k].size = access_units[k].size + sizeof(aud); | 118 access_units_with_aud[k].size = access_units_[k].size + sizeof(aud); |
| 109 | 119 |
| 110 memcpy(&stream_with_aud[offset], aud, sizeof(aud)); | 120 memcpy(&stream_with_aud[offset], aud, sizeof(aud)); |
| 111 offset += sizeof(aud); | 121 offset += sizeof(aud); |
| 112 | 122 |
| 113 memcpy(&stream_with_aud[offset], | 123 memcpy(&stream_with_aud[offset], |
| 114 &stream[access_units[k].offset], access_units[k].size); | 124 &stream_[access_units_[k].offset], access_units_[k].size); |
| 115 offset += access_units[k].size; | 125 offset += access_units_[k].size; |
| 116 } | 126 } |
| 127 |
| 128 // Update the stream and access units used for the test. |
| 129 stream_ = stream_with_aud; |
| 130 access_units_ = access_units_with_aud; |
| 117 } | 131 } |
| 118 | 132 |
| 119 } // namespace | 133 void EsParserH264Test::GetPesTimestamps(std::vector<Packet>* pes_packets_ptr) { |
| 134 DCHECK(pes_packets_ptr); |
| 135 const std::vector<Packet>& pes_packets = *pes_packets_ptr; |
| 120 | 136 |
| 121 class EsParserH264Test : public testing::Test { | |
| 122 public: | |
| 123 EsParserH264Test() : buffer_count_(0) { | |
| 124 } | |
| 125 virtual ~EsParserH264Test() {} | |
| 126 | |
| 127 protected: | |
| 128 void LoadStream(const char* filename); | |
| 129 void GetPesTimestamps(std::vector<Packet>& pes_packets); | |
| 130 void ProcessPesPackets(const std::vector<Packet>& pes_packets, | |
| 131 bool force_timing); | |
| 132 | |
| 133 // Stream with AUD NALUs. | |
| 134 std::vector<uint8> stream_; | |
| 135 | |
| 136 // Access units of the stream with AUD NALUs. | |
| 137 std::vector<Packet> access_units_; | |
| 138 | |
| 139 // Number of buffers generated while parsing the H264 stream. | |
| 140 size_t buffer_count_; | |
| 141 | |
| 142 private: | |
| 143 void EmitBuffer(scoped_refptr<StreamParserBuffer> buffer); | |
| 144 | |
| 145 void NewVideoConfig(const VideoDecoderConfig& config) { | |
| 146 } | |
| 147 | |
| 148 DISALLOW_COPY_AND_ASSIGN(EsParserH264Test); | |
| 149 }; | |
| 150 | |
| 151 void EsParserH264Test::LoadStream(const char* filename) { | |
| 152 base::FilePath file_path = GetTestDataFilePath(filename); | |
| 153 | |
| 154 base::MemoryMappedFile stream_without_aud; | |
| 155 ASSERT_TRUE(stream_without_aud.Initialize(file_path)) | |
| 156 << "Couldn't open stream file: " << file_path.MaybeAsASCII(); | |
| 157 | |
| 158 // The input file does not have AUDs. | |
| 159 std::vector<Packet> access_units_without_aud = GetAccessUnits( | |
| 160 stream_without_aud.data(), stream_without_aud.length()); | |
| 161 ASSERT_GT(access_units_without_aud.size(), 0u); | |
| 162 AppendAUD(stream_without_aud.data(), stream_without_aud.length(), | |
| 163 access_units_without_aud, | |
| 164 stream_, access_units_); | |
| 165 | |
| 166 // Generate some timestamps based on a 25fps stream. | |
| 167 for (size_t k = 0; k < access_units_.size(); k++) | |
| 168 access_units_[k].pts = base::TimeDelta::FromMilliseconds(k * 40u); | |
| 169 } | |
| 170 | |
| 171 void EsParserH264Test::GetPesTimestamps(std::vector<Packet>& pes_packets) { | |
| 172 // Default: set to a negative timestamp to be able to differentiate from | 137 // Default: set to a negative timestamp to be able to differentiate from |
| 173 // real timestamps. | 138 // real timestamps. |
| 174 // Note: we don't use kNoTimestamp() here since this one has already | 139 // Note: we don't use kNoTimestamp() here since this one has already |
| 175 // a special meaning in EsParserH264. The negative timestamps should be | 140 // a special meaning in EsParserH264. The negative timestamps should be |
| 176 // ultimately discarded by the H264 parser since not relevant. | 141 // ultimately discarded by the H264 parser since not relevant. |
| 177 for (size_t k = 0; k < pes_packets.size(); k++) { | 142 for (size_t k = 0; k < pes_packets.size(); k++) { |
| 178 pes_packets[k].pts = base::TimeDelta::FromMilliseconds(-1); | 143 (*pes_packets_ptr)[k].pts = base::TimeDelta::FromMilliseconds(-1); |
| 179 } | 144 } |
| 180 | 145 |
| 181 // Set a valid timestamp for PES packets which include the start | 146 // Set a valid timestamp for PES packets which include the start |
| 182 // of an H264 access unit. | 147 // of an H264 access unit. |
| 183 size_t pes_idx = 0; | 148 size_t pes_idx = 0; |
| 184 for (size_t k = 0; k < access_units_.size(); k++) { | 149 for (size_t k = 0; k < access_units_.size(); k++) { |
| 185 for (; pes_idx < pes_packets.size(); pes_idx++) { | 150 for (; pes_idx < pes_packets.size(); pes_idx++) { |
| 186 size_t pes_start = pes_packets[pes_idx].offset; | 151 size_t pes_start = pes_packets[pes_idx].offset; |
| 187 size_t pes_end = pes_packets[pes_idx].offset + pes_packets[pes_idx].size; | 152 size_t pes_end = pes_packets[pes_idx].offset + pes_packets[pes_idx].size; |
| 188 if (pes_start <= access_units_[k].offset && | 153 if (pes_start <= access_units_[k].offset && |
| 189 pes_end > access_units_[k].offset) { | 154 pes_end > access_units_[k].offset) { |
| 190 pes_packets[pes_idx].pts = access_units_[k].pts; | 155 (*pes_packets_ptr)[pes_idx].pts = access_units_[k].pts; |
| 191 break; | 156 break; |
| 192 } | 157 } |
| 193 } | 158 } |
| 194 } | 159 } |
| 195 } | 160 } |
| 196 | 161 |
| 197 void EsParserH264Test::ProcessPesPackets( | 162 bool EsParserH264Test::Process( |
| 198 const std::vector<Packet>& pes_packets, | 163 const std::vector<Packet>& pes_packets, |
| 199 bool force_timing) { | 164 bool force_timing) { |
| 200 EsParserH264 es_parser( | 165 EsParserH264 es_parser( |
| 201 base::Bind(&EsParserH264Test::NewVideoConfig, base::Unretained(this)), | 166 base::Bind(&EsParserH264Test::NewVideoConfig, base::Unretained(this)), |
| 202 base::Bind(&EsParserH264Test::EmitBuffer, base::Unretained(this))); | 167 base::Bind(&EsParserH264Test::EmitBuffer, base::Unretained(this))); |
| 203 | 168 return ProcessPesPackets(&es_parser, pes_packets, force_timing); |
| 204 for (size_t k = 0; k < pes_packets.size(); k++) { | |
| 205 size_t cur_pes_offset = pes_packets[k].offset; | |
| 206 size_t cur_pes_size = pes_packets[k].size; | |
| 207 | |
| 208 base::TimeDelta pts = kNoTimestamp(); | |
| 209 DecodeTimestamp dts = kNoDecodeTimestamp(); | |
| 210 if (pes_packets[k].pts >= base::TimeDelta() || force_timing) | |
| 211 pts = pes_packets[k].pts; | |
| 212 | |
| 213 ASSERT_TRUE( | |
| 214 es_parser.Parse(&stream_[cur_pes_offset], cur_pes_size, pts, dts)); | |
| 215 } | |
| 216 es_parser.Flush(); | |
| 217 } | 169 } |
| 218 | 170 |
| 219 void EsParserH264Test::EmitBuffer(scoped_refptr<StreamParserBuffer> buffer) { | 171 void EsParserH264Test::CheckAccessUnits() { |
| 220 ASSERT_LT(buffer_count_, access_units_.size()); | 172 EXPECT_EQ(buffer_count_, access_units_.size()); |
| 221 EXPECT_EQ(buffer->timestamp(), access_units_[buffer_count_].pts); | 173 |
| 222 buffer_count_++; | 174 std::stringstream buffer_timestamps_stream; |
| 175 for (size_t k = 0; k < access_units_.size(); k++) { |
| 176 buffer_timestamps_stream << "(" |
| 177 << access_units_[k].pts.InMilliseconds() |
| 178 << ") "; |
| 179 } |
| 180 std::string buffer_timestamps = buffer_timestamps_stream.str(); |
| 181 base::TrimWhitespaceASCII( |
| 182 buffer_timestamps, base::TRIM_ALL, &buffer_timestamps); |
| 183 EXPECT_EQ(buffer_timestamps_, buffer_timestamps); |
| 223 } | 184 } |
| 224 | 185 |
| 225 TEST_F(EsParserH264Test, OneAccessUnitPerPes) { | 186 TEST_F(EsParserH264Test, OneAccessUnitPerPes) { |
| 226 LoadStream("bear.h264"); | 187 LoadH264Stream("bear.h264"); |
| 227 | 188 |
| 228 // One to one equivalence between PES packets and access units. | 189 // One to one equivalence between PES packets and access units. |
| 229 std::vector<Packet> pes_packets(access_units_); | 190 std::vector<Packet> pes_packets(access_units_); |
| 230 GetPesTimestamps(pes_packets); | 191 GetPesTimestamps(&pes_packets); |
| 231 | 192 |
| 232 // Process each PES packet. | 193 // Process each PES packet. |
| 233 ProcessPesPackets(pes_packets, false); | 194 EXPECT_TRUE(Process(pes_packets, false)); |
| 234 EXPECT_EQ(buffer_count_, access_units_.size()); | 195 CheckAccessUnits(); |
| 235 } | 196 } |
| 236 | 197 |
| 237 TEST_F(EsParserH264Test, NonAlignedPesPacket) { | 198 TEST_F(EsParserH264Test, NonAlignedPesPacket) { |
| 238 LoadStream("bear.h264"); | 199 LoadH264Stream("bear.h264"); |
| 239 | 200 |
| 240 // Generate the PES packets. | 201 // Generate the PES packets. |
| 241 std::vector<Packet> pes_packets; | 202 std::vector<Packet> pes_packets; |
| 242 Packet cur_pes_packet; | 203 Packet cur_pes_packet; |
| 243 cur_pes_packet.offset = 0; | 204 cur_pes_packet.offset = 0; |
| 244 for (size_t k = 0; k < access_units_.size(); k++) { | 205 for (size_t k = 0; k < access_units_.size(); k++) { |
| 245 pes_packets.push_back(cur_pes_packet); | 206 pes_packets.push_back(cur_pes_packet); |
| 246 | 207 |
| 247 // The current PES packet includes the remaining bytes of the previous | 208 // The current PES packet includes the remaining bytes of the previous |
| 248 // access unit and some bytes of the current access unit | 209 // access unit and some bytes of the current access unit |
| 249 // (487 bytes in this unit test but no more than the current access unit | 210 // (487 bytes in this unit test but no more than the current access unit |
| 250 // size). | 211 // size). |
| 251 cur_pes_packet.offset = access_units_[k].offset + | 212 cur_pes_packet.offset = access_units_[k].offset + |
| 252 std::min<size_t>(487u, access_units_[k].size); | 213 std::min<size_t>(487u, access_units_[k].size); |
| 253 } | 214 } |
| 254 ComputePacketSize(pes_packets, stream_.size()); | 215 ComputePacketSize(&pes_packets); |
| 255 GetPesTimestamps(pes_packets); | 216 GetPesTimestamps(&pes_packets); |
| 256 | 217 |
| 257 // Process each PES packet. | 218 // Process each PES packet. |
| 258 ProcessPesPackets(pes_packets, false); | 219 EXPECT_TRUE(Process(pes_packets, false)); |
| 259 EXPECT_EQ(buffer_count_, access_units_.size()); | 220 CheckAccessUnits(); |
| 260 } | 221 } |
| 261 | 222 |
| 262 TEST_F(EsParserH264Test, SeveralPesPerAccessUnit) { | 223 TEST_F(EsParserH264Test, SeveralPesPerAccessUnit) { |
| 263 LoadStream("bear.h264"); | 224 LoadH264Stream("bear.h264"); |
| 264 | 225 |
| 265 // Get the minimum size of an access unit. | 226 // Get the minimum size of an access unit. |
| 266 size_t min_access_unit_size = stream_.size(); | 227 size_t min_access_unit_size = stream_.size(); |
| 267 for (size_t k = 0; k < access_units_.size(); k++) { | 228 for (size_t k = 0; k < access_units_.size(); k++) { |
| 268 if (min_access_unit_size >= access_units_[k].size) | 229 if (min_access_unit_size >= access_units_[k].size) |
| 269 min_access_unit_size = access_units_[k].size; | 230 min_access_unit_size = access_units_[k].size; |
| 270 } | 231 } |
| 271 | 232 |
| 272 // Use a small PES packet size or the minimum access unit size | 233 // Use a small PES packet size or the minimum access unit size |
| 273 // if it is even smaller. | 234 // if it is even smaller. |
| 274 size_t pes_size = 512; | 235 size_t pes_size = 512; |
| 275 if (min_access_unit_size < pes_size) | 236 if (min_access_unit_size < pes_size) |
| 276 pes_size = min_access_unit_size; | 237 pes_size = min_access_unit_size; |
| 277 | 238 |
| 278 std::vector<Packet> pes_packets; | 239 std::vector<Packet> pes_packets; |
| 279 Packet cur_pes_packet; | 240 Packet cur_pes_packet; |
| 280 cur_pes_packet.offset = 0; | 241 cur_pes_packet.offset = 0; |
| 281 while (cur_pes_packet.offset < stream_.size()) { | 242 while (cur_pes_packet.offset < stream_.size()) { |
| 282 pes_packets.push_back(cur_pes_packet); | 243 pes_packets.push_back(cur_pes_packet); |
| 283 cur_pes_packet.offset += pes_size; | 244 cur_pes_packet.offset += pes_size; |
| 284 } | 245 } |
| 285 ComputePacketSize(pes_packets, stream_.size()); | 246 ComputePacketSize(&pes_packets); |
| 286 GetPesTimestamps(pes_packets); | 247 GetPesTimestamps(&pes_packets); |
| 287 | 248 |
| 288 // Process each PES packet. | 249 // Process each PES packet. |
| 289 ProcessPesPackets(pes_packets, false); | 250 EXPECT_TRUE(Process(pes_packets, false)); |
| 290 EXPECT_EQ(buffer_count_, access_units_.size()); | 251 CheckAccessUnits(); |
| 291 | 252 |
| 292 // Process PES packets forcing timings for each PES packet. | 253 // Process PES packets forcing timings for each PES packet. |
| 293 buffer_count_ = 0; | 254 EXPECT_TRUE(Process(pes_packets, true)); |
| 294 ProcessPesPackets(pes_packets, true); | 255 CheckAccessUnits(); |
| 295 EXPECT_EQ(buffer_count_, access_units_.size()); | |
| 296 } | 256 } |
| 297 | 257 |
| 298 } // namespace mp2t | 258 } // namespace mp2t |
| 299 } // namespace media | 259 } // namespace media |
| OLD | NEW |