Chromium Code Reviews| 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. | |
|
Ami GONE FROM CHROMIUM
2013/12/03 21:17:16
Is it easy to see why this should be a separate bi
chihchung
2013/12/04 05:38:10
Hi,
Sorry I should have made the context more cle
| |
| 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 scoped_refptr<media::VideoFrame> CopyNV12ToI420(VAImage* image, void* mem) { | |
| 192 DVLOG(1) << "CopyNV12ToI420 width=" << image->width | |
| 193 << ", height=" << image->height; | |
| 194 | |
| 195 const gfx::Size coded_size(image->width, image->height); | |
| 196 const gfx::Rect visible_rect(image->width, image->height); | |
| 197 const gfx::Size natural_size(image->width, image->height); | |
| 198 | |
| 199 scoped_refptr<media::VideoFrame> frame = | |
| 200 media::VideoFrame::CreateFrame(media::VideoFrame::I420, | |
| 201 coded_size, | |
| 202 visible_rect, | |
| 203 natural_size, | |
| 204 base::TimeDelta()); | |
| 205 | |
| 206 uint8_t* mem_byte_ptr = static_cast<uint8_t*>(mem); | |
| 207 uint8_t* srcY = mem_byte_ptr + image->offsets[0]; | |
| 208 uint8_t* srcU = mem_byte_ptr + image->offsets[1]; | |
| 209 uint8_t* srcV = srcU + 1; | |
| 210 | |
| 211 uint8_t* dstY = frame->data(media::VideoFrame::kYPlane); | |
| 212 uint8_t* dstU = frame->data(media::VideoFrame::kUPlane); | |
| 213 uint8_t* dstV = frame->data(media::VideoFrame::kVPlane); | |
| 214 | |
| 215 for (int i = 0; i < image->height; i++) { | |
| 216 memcpy(dstY, srcY, image->width); | |
| 217 srcY += image->pitches[0]; | |
| 218 dstY += frame->stride(media::VideoFrame::kYPlane); | |
| 219 | |
| 220 // U/V components are subsampled, so there is only one U/V row for every two | |
| 221 // Y rows. | |
| 222 if ((i & 1) == 1) | |
| 223 continue; | |
| 224 for (int j = 0, k = 0; j < image->width; j += 2, k++) { | |
| 225 dstU[k] = srcU[j]; | |
| 226 dstV[k] = srcV[j]; | |
| 227 } | |
| 228 srcU += image->pitches[1]; | |
| 229 srcV += image->pitches[1]; | |
| 230 dstU += frame->stride(media::VideoFrame::kUPlane); | |
| 231 dstV += frame->stride(media::VideoFrame::kVPlane); | |
| 232 } | |
| 233 | |
| 234 return frame; | |
| 235 } | |
| 236 | |
| 237 bool VaapiH264DecoderLoop::ProcessVideoFrame( | |
| 238 const scoped_refptr<media::VideoFrame>& frame) { | |
| 239 for (size_t i = 0; i < media::VideoFrame::NumPlanes(frame->format()); i++) { | |
| 240 uint8* data = frame->data(i); | |
| 241 int bytes = frame->row_bytes(i); | |
| 242 for (int j = 0; j < frame->rows(i); j++) { | |
| 243 const char* buf = reinterpret_cast<const char*>(data); | |
| 244 base::MD5Update(&md5_context_, base::StringPiece(buf, bytes)); | |
| 245 if (!output_file_.empty()) { | |
| 246 int written = file_util::AppendToFile(output_file_, buf, bytes); | |
| 247 if (written != bytes) { | |
| 248 return false; | |
| 249 } | |
| 250 } | |
| 251 data += frame->stride(i); | |
| 252 } | |
| 253 } | |
| 254 return true; | |
| 255 } | |
| 256 | |
| 257 void VaapiH264DecoderLoop::OutputPicture( | |
| 258 int32 input_id, | |
| 259 const scoped_refptr<VASurface>& va_surface) { | |
| 260 VLOG(1) << "OutputPicture: picture " << num_outputted_pictures_++; | |
| 261 | |
| 262 VAImage image; | |
| 263 void* mem; | |
| 264 | |
| 265 if (!wrapper_->GetVaImage(va_surface->id(), &image, &mem)) { | |
| 266 LOG(ERROR) << "Cannot get VAImage."; | |
| 267 return; | |
| 268 } | |
| 269 | |
| 270 if (image.format.fourcc != VA_FOURCC_NV12) { | |
| 271 LOG(ERROR) << "Unexpected image format: " << image.format.fourcc; | |
| 272 wrapper_->ReturnVaImage(&image); | |
| 273 return; | |
| 274 } | |
| 275 | |
| 276 // Convert NV12 to I420 format. | |
| 277 scoped_refptr<media::VideoFrame> frame = CopyNV12ToI420(&image, mem); | |
| 278 | |
| 279 if (frame.get()) { | |
| 280 if (!ProcessVideoFrame(frame)) { | |
| 281 LOG(ERROR) << "Write to file failed"; | |
| 282 } | |
| 283 } else { | |
| 284 LOG(ERROR) << "Cannot convert image to I420."; | |
| 285 } | |
| 286 | |
| 287 wrapper_->ReturnVaImage(&image); | |
| 288 } | |
| 289 | |
| 290 void VaapiH264DecoderLoop::RecycleSurface(VASurfaceID va_surface_id) { | |
| 291 available_surfaces_.push_back(va_surface_id); | |
| 292 } | |
| 293 | |
| 294 void VaapiH264DecoderLoop::RefillSurfaces() { | |
| 295 for (size_t i = 0; i < available_surfaces_.size(); i++) { | |
| 296 VASurface::ReleaseCB release_cb = base::Bind( | |
| 297 &VaapiH264DecoderLoop::RecycleSurface, base::Unretained(this)); | |
| 298 scoped_refptr<VASurface> surface( | |
| 299 new VASurface(available_surfaces_[i], release_cb)); | |
| 300 decoder_->ReuseSurface(surface); | |
| 301 } | |
| 302 available_surfaces_.clear(); | |
| 303 } | |
| 304 | |
| 305 bool VaapiH264DecoderLoop::AllocateNewSurfaces() { | |
| 306 CHECK_EQ(num_surfaces_, available_surfaces_.size()) | |
| 307 << "not all surfaces are returned"; | |
| 308 | |
| 309 available_surfaces_.clear(); | |
| 310 wrapper_->DestroySurfaces(); | |
| 311 | |
| 312 gfx::Size size = decoder_->GetPicSize(); | |
| 313 num_surfaces_ = decoder_->GetRequiredNumOfPictures(); | |
| 314 return wrapper_->CreateSurfaces(size, num_surfaces_, &available_surfaces_); | |
| 315 } | |
| 316 | |
| 317 TEST(VaapiH264DecoderTest, TestDecode) { | |
| 318 base::FilePath input_file = g_input_file; | |
| 319 base::FilePath output_file = g_output_file; | |
| 320 std::string md5sum = g_md5sum; | |
| 321 | |
| 322 // If nothing specified, use the default file in the source tree. | |
| 323 if (input_file.empty() && output_file.empty() && md5sum.empty()) { | |
| 324 input_file = base::FilePath(kDefaultInputFile); | |
| 325 md5sum = kDefaultMD5Sum; | |
| 326 } else { | |
| 327 ASSERT_FALSE(input_file.empty()) << "Need to specify --input_file"; | |
| 328 } | |
| 329 | |
| 330 VLOG(1) << "Input File: " << input_file.value(); | |
| 331 VLOG(1) << "Output File: " << output_file.value(); | |
| 332 VLOG(1) << "Expected MD5 sum: " << md5sum; | |
| 333 | |
| 334 content::VaapiH264DecoderLoop loop; | |
| 335 ASSERT_TRUE(loop.Initialize(input_file, output_file)) | |
| 336 << "initialize decoder loop failed"; | |
| 337 ASSERT_TRUE(loop.Run()) << "run decoder loop failed"; | |
| 338 | |
| 339 if (!md5sum.empty()) { | |
| 340 std::string actual = loop.GetMD5Sum(); | |
| 341 VLOG(1) << "Actual MD5 sum: " << actual; | |
| 342 EXPECT_EQ(md5sum, actual); | |
| 343 } | |
| 344 } | |
| 345 | |
| 346 } // namespace | |
| 347 } // namespace content | |
| 348 | |
| 349 int main(int argc, char** argv) { | |
| 350 testing::InitGoogleTest(&argc, argv); // Removes gtest-specific args. | |
| 351 CommandLine::Init(argc, argv); | |
| 352 | |
| 353 // Needed to enable DVLOG through --vmodule. | |
| 354 logging::LoggingSettings settings; | |
| 355 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
| 356 settings.dcheck_state = | |
| 357 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS; | |
| 358 CHECK(logging::InitLogging(settings)); | |
| 359 | |
| 360 // Process command line. | |
| 361 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
| 362 CHECK(cmd_line); | |
| 363 | |
| 364 CommandLine::SwitchMap switches = cmd_line->GetSwitches(); | |
| 365 for (CommandLine::SwitchMap::const_iterator it = switches.begin(); | |
| 366 it != switches.end(); | |
| 367 ++it) { | |
| 368 if (it->first == "input_file") { | |
| 369 content::g_input_file = base::FilePath(it->second); | |
| 370 continue; | |
| 371 } | |
| 372 if (it->first == "output_file") { | |
| 373 content::g_output_file = base::FilePath(it->second); | |
| 374 continue; | |
| 375 } | |
| 376 if (it->first == "md5sum") { | |
| 377 content::g_md5sum = it->second; | |
| 378 continue; | |
| 379 } | |
| 380 if (it->first == "v" || it->first == "vmodule") | |
| 381 continue; | |
| 382 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; | |
| 383 } | |
| 384 | |
| 385 return RUN_ALL_TESTS(); | |
| 386 } | |
| OLD | NEW |