| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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 |
| 5 #include <string> |
| 6 |
| 7 // This has to be included first. |
| 8 // See http://code.google.com/p/googletest/issues/detail?id=371 |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" |
| 13 #include "base/file_util.h" |
| 14 #include "base/logging.h" |
| 15 #include "content/common/gpu/media/vaapi_h264_decoder.h" |
| 16 #include "media/base/video_decoder_config.h" |
| 17 |
| 18 // This program is run like this: |
| 19 // DISPLAY=:0 ./vaapi_h264_decoder_unittest --input_file input.264 |
| 20 // [--output_file output.yuv] [--md5sum expected_md5_hex] |
| 21 // |
| 22 // The input is read from input.264. The output is written to output.yuv if it |
| 23 // is given. It also verifies the MD5 sum of the decoded YUV data if the |
| 24 // expected MD5 sum is given. |
| 25 |
| 26 namespace content { |
| 27 namespace { |
| 28 |
| 29 // These are the command line parameters |
| 30 base::FilePath g_input_file; |
| 31 base::FilePath g_output_file; |
| 32 std::string g_md5sum; |
| 33 |
| 34 // These default values are used if nothing is specified in the command line. |
| 35 const base::FilePath::CharType* kDefaultInputFile = |
| 36 FILE_PATH_LITERAL("test-25fps.h264"); |
| 37 const char* kDefaultMD5Sum = "3af866863225b956001252ebeccdb71d"; |
| 38 |
| 39 // This class encapsulates the use of VaapiH264Decoder to a simpler interface. |
| 40 // It reads an H.264 Annex B bytestream from a file and outputs the decoded |
| 41 // frames (in I420 format) to another file. |
| 42 // |
| 43 // To use the class, construct an instance, call Initialize() to specify the |
| 44 // input and output file path, then call Run() to decode the whole stream and |
| 45 // output the frames. |
| 46 // |
| 47 // This class must be created, called and destroyed on a single thread, and |
| 48 // does nothing internally on any other thread. |
| 49 class VaapiH264DecoderLoop { |
| 50 public: |
| 51 VaapiH264DecoderLoop(); |
| 52 ~VaapiH264DecoderLoop(); |
| 53 |
| 54 // Initialize the decoder. Return true if successful. |
| 55 bool Initialize(base::FilePath input_file, base::FilePath output_file); |
| 56 |
| 57 // Run the decode loop. The decoded data is written to the file specified by |
| 58 // output_file in Initialize(). Return true if all decoding is successful. |
| 59 bool Run(); |
| 60 |
| 61 // Get the MD5 sum of the decoded data. |
| 62 std::string GetMD5Sum(); |
| 63 |
| 64 private: |
| 65 void ReportToUMA(VaapiH264Decoder::VAVDAH264DecoderFailure error) {} |
| 66 |
| 67 // Callback from the decoder when a picture is decoded. |
| 68 void OutputPicture(int32 input_id, |
| 69 const scoped_refptr<VASurface>& va_surface); |
| 70 |
| 71 // Recycle one surface and put it on available_surfaces_ list. |
| 72 void RecycleSurface(VASurfaceID va_surface_id); |
| 73 |
| 74 // Give all surfaces in available_surfaces_ to the decoder. |
| 75 void RefillSurfaces(); |
| 76 |
| 77 // Free the current set of surfaces and allocate a new set of |
| 78 // surfaces. Returns true when successful. |
| 79 bool AllocateNewSurfaces(); |
| 80 |
| 81 // Use the data in the frame: write to file and update MD5 sum. |
| 82 bool ProcessVideoFrame(const scoped_refptr<media::VideoFrame>& frame); |
| 83 |
| 84 scoped_ptr<VaapiH264Decoder> decoder_; |
| 85 scoped_ptr<VaapiWrapper> wrapper_; |
| 86 std::string data_; // data read from input_file |
| 87 base::FilePath output_file_; // output data is written to this file |
| 88 std::vector<VASurfaceID> available_surfaces_; |
| 89 |
| 90 // These members (x_display_, num_outputted_pictures_, num_surfaces_) |
| 91 // need to be initialized and possibly freed manually. |
| 92 Display* x_display_; |
| 93 int num_outputted_pictures_; // number of pictures already outputted |
| 94 size_t num_surfaces_; // number of surfaces in the current set of surfaces |
| 95 base::MD5Context md5_context_; |
| 96 }; |
| 97 |
| 98 VaapiH264DecoderLoop::VaapiH264DecoderLoop() |
| 99 : x_display_(NULL), num_outputted_pictures_(0), num_surfaces_(0) { |
| 100 base::MD5Init(&md5_context_); |
| 101 } |
| 102 |
| 103 VaapiH264DecoderLoop::~VaapiH264DecoderLoop() { |
| 104 // We need to destruct decoder and wrapper first because: |
| 105 // (1) The decoder has a reference to the wrapper. |
| 106 // (2) The wrapper has a reference to x_display_. |
| 107 decoder_.reset(); |
| 108 wrapper_.reset(); |
| 109 |
| 110 if (x_display_) { |
| 111 XCloseDisplay(x_display_); |
| 112 } |
| 113 } |
| 114 |
| 115 bool VaapiH264DecoderLoop::Initialize(base::FilePath input_file, |
| 116 base::FilePath output_file) { |
| 117 x_display_ = XOpenDisplay(NULL); |
| 118 if (!x_display_) { |
| 119 LOG(ERROR) << "Can't open X display"; |
| 120 return false; |
| 121 } |
| 122 |
| 123 media::VideoCodecProfile profile = media::H264PROFILE_HIGH; |
| 124 base::Closure report_error_cb = base::Bind(&VaapiH264DecoderLoop::ReportToUMA, |
| 125 base::Unretained(this), |
| 126 VaapiH264Decoder::VAAPI_ERROR); |
| 127 wrapper_ = VaapiWrapper::Create(profile, x_display_, report_error_cb); |
| 128 if (!wrapper_.get()) { |
| 129 LOG(ERROR) << "Can't create vaapi wrapper"; |
| 130 return false; |
| 131 } |
| 132 |
| 133 decoder_.reset(new VaapiH264Decoder( |
| 134 wrapper_.get(), |
| 135 base::Bind(&VaapiH264DecoderLoop::OutputPicture, base::Unretained(this)), |
| 136 base::Bind(&VaapiH264DecoderLoop::ReportToUMA, base::Unretained(this)))); |
| 137 |
| 138 if (!base::ReadFileToString(input_file, &data_)) { |
| 139 LOG(ERROR) << "failed to read input data from " << input_file.value(); |
| 140 return false; |
| 141 } |
| 142 |
| 143 const int input_id = 0; // We don't use input_id in this class. |
| 144 decoder_->SetStream( |
| 145 reinterpret_cast<const uint8*>(data_.c_str()), data_.size(), input_id); |
| 146 |
| 147 // This creates or truncates output_file. |
| 148 // Without it, AppendToFile() will not work. |
| 149 if (!output_file.empty()) { |
| 150 if (file_util::WriteFile(output_file, NULL, 0) != 0) { |
| 151 return false; |
| 152 } |
| 153 output_file_ = output_file; |
| 154 } |
| 155 |
| 156 return true; |
| 157 } |
| 158 |
| 159 bool VaapiH264DecoderLoop::Run() { |
| 160 while (1) { |
| 161 switch (decoder_->Decode()) { |
| 162 case VaapiH264Decoder::kDecodeError: |
| 163 LOG(ERROR) << "Decode Error"; |
| 164 return false; |
| 165 case VaapiH264Decoder::kAllocateNewSurfaces: |
| 166 VLOG(1) << "Allocate new surfaces"; |
| 167 if (!AllocateNewSurfaces()) { |
| 168 LOG(ERROR) << "Failed to allocate new surfaces"; |
| 169 return false; |
| 170 } |
| 171 break; |
| 172 case VaapiH264Decoder::kRanOutOfStreamData: { |
| 173 bool rc = decoder_->Flush(); |
| 174 VLOG(1) << "Flush returns " << rc; |
| 175 return rc; |
| 176 } |
| 177 case VaapiH264Decoder::kRanOutOfSurfaces: |
| 178 VLOG(1) << "Ran out of surfaces"; |
| 179 RefillSurfaces(); |
| 180 break; |
| 181 } |
| 182 } |
| 183 } |
| 184 |
| 185 std::string VaapiH264DecoderLoop::GetMD5Sum() { |
| 186 base::MD5Digest digest; |
| 187 base::MD5Final(&digest, &md5_context_); |
| 188 return MD5DigestToBase16(digest); |
| 189 } |
| 190 |
| 191 bool VaapiH264DecoderLoop::ProcessVideoFrame( |
| 192 const scoped_refptr<media::VideoFrame>& frame) { |
| 193 for (size_t i = 0; i < media::VideoFrame::NumPlanes(frame->format()); i++) { |
| 194 uint8* data = frame->data(i); |
| 195 int bytes = frame->row_bytes(i); |
| 196 for (int j = 0; j < frame->rows(i); j++) { |
| 197 const char* buf = reinterpret_cast<const char*>(data); |
| 198 base::MD5Update(&md5_context_, base::StringPiece(buf, bytes)); |
| 199 if (!output_file_.empty()) { |
| 200 int written = file_util::AppendToFile(output_file_, buf, bytes); |
| 201 if (written != bytes) { |
| 202 return false; |
| 203 } |
| 204 } |
| 205 data += frame->stride(i); |
| 206 } |
| 207 } |
| 208 return true; |
| 209 } |
| 210 |
| 211 void VaapiH264DecoderLoop::OutputPicture( |
| 212 int32 input_id, |
| 213 const scoped_refptr<VASurface>& va_surface) { |
| 214 VLOG(1) << "OutputPicture: picture " << num_outputted_pictures_++; |
| 215 scoped_refptr<media::VideoFrame> frame = |
| 216 wrapper_->VideoFrameFromVASurface(va_surface->id()); |
| 217 if (frame.get()) { |
| 218 if (!ProcessVideoFrame(frame)) { |
| 219 LOG(ERROR) << "write to file failed"; |
| 220 } |
| 221 } else { |
| 222 LOG(ERROR) << "Cannot convert surface to I420."; |
| 223 } |
| 224 } |
| 225 |
| 226 void VaapiH264DecoderLoop::RecycleSurface(VASurfaceID va_surface_id) { |
| 227 available_surfaces_.push_back(va_surface_id); |
| 228 } |
| 229 |
| 230 void VaapiH264DecoderLoop::RefillSurfaces() { |
| 231 for (size_t i = 0; i < available_surfaces_.size(); i++) { |
| 232 VASurface::ReleaseCB release_cb = base::Bind( |
| 233 &VaapiH264DecoderLoop::RecycleSurface, base::Unretained(this)); |
| 234 scoped_refptr<VASurface> surface( |
| 235 new VASurface(available_surfaces_[i], release_cb)); |
| 236 decoder_->ReuseSurface(surface); |
| 237 } |
| 238 available_surfaces_.clear(); |
| 239 } |
| 240 |
| 241 bool VaapiH264DecoderLoop::AllocateNewSurfaces() { |
| 242 CHECK_EQ(num_surfaces_, available_surfaces_.size()) |
| 243 << "not all surfaces are returned"; |
| 244 |
| 245 available_surfaces_.clear(); |
| 246 wrapper_->DestroySurfaces(); |
| 247 |
| 248 gfx::Size size = decoder_->GetPicSize(); |
| 249 num_surfaces_ = decoder_->GetRequiredNumOfPictures(); |
| 250 return wrapper_->CreateSurfaces(size, num_surfaces_, &available_surfaces_); |
| 251 } |
| 252 |
| 253 TEST(VaapiH264DecoderTest, TestDecode) { |
| 254 base::FilePath input_file = g_input_file; |
| 255 base::FilePath output_file = g_output_file; |
| 256 std::string md5sum = g_md5sum; |
| 257 |
| 258 // If nothing specified, use the default file in the source tree. |
| 259 if (input_file.empty() && output_file.empty() && md5sum.empty()) { |
| 260 input_file = base::FilePath(kDefaultInputFile); |
| 261 md5sum = kDefaultMD5Sum; |
| 262 } else { |
| 263 CHECK(!input_file.empty()) << "Need to specify --input_file"; |
| 264 } |
| 265 |
| 266 VLOG(1) << "Input File: " << input_file.value(); |
| 267 VLOG(1) << "Output File: " << output_file.value(); |
| 268 VLOG(1) << "Expected MD5 sum: " << md5sum; |
| 269 |
| 270 content::VaapiH264DecoderLoop loop; |
| 271 CHECK(loop.Initialize(input_file, output_file)) |
| 272 << "initialize decoder loop failed"; |
| 273 CHECK(loop.Run()) << "run decoder loop failed"; |
| 274 |
| 275 if (!md5sum.empty()) { |
| 276 std::string actual = loop.GetMD5Sum(); |
| 277 VLOG(1) << "Actual MD5 sum: " << actual; |
| 278 EXPECT_EQ(md5sum, actual); |
| 279 } |
| 280 } |
| 281 |
| 282 } // namespace |
| 283 } // namespace content |
| 284 |
| 285 int main(int argc, char** argv) { |
| 286 testing::InitGoogleTest(&argc, argv); // Removes gtest-specific args. |
| 287 CommandLine::Init(argc, argv); |
| 288 |
| 289 // Needed to enable DVLOG through --vmodule. |
| 290 logging::LoggingSettings settings; |
| 291 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
| 292 settings.dcheck_state = |
| 293 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS; |
| 294 CHECK(logging::InitLogging(settings)); |
| 295 |
| 296 // Process command line. |
| 297 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 298 CHECK(cmd_line); |
| 299 |
| 300 CommandLine::SwitchMap switches = cmd_line->GetSwitches(); |
| 301 for (CommandLine::SwitchMap::const_iterator it = switches.begin(); |
| 302 it != switches.end(); |
| 303 ++it) { |
| 304 if (it->first == "input_file") { |
| 305 content::g_input_file = base::FilePath(it->second); |
| 306 continue; |
| 307 } |
| 308 if (it->first == "output_file") { |
| 309 content::g_output_file = base::FilePath(it->second); |
| 310 continue; |
| 311 } |
| 312 if (it->first == "md5sum") { |
| 313 content::g_md5sum = it->second; |
| 314 continue; |
| 315 } |
| 316 if (it->first == "v" || it->first == "vmodule") |
| 317 continue; |
| 318 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; |
| 319 } |
| 320 |
| 321 return RUN_ALL_TESTS(); |
| 322 } |
| OLD | NEW |