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 #include "third_party/libyuv/include/libyuv.h" | |
| 18 | |
| 19 // This program is run like this: | |
| 20 // DISPLAY=:0 ./vaapi_h264_decoder_unittest --input_file input.h264 | |
| 21 // [--output_file output.i420] [--md5sum expected_md5_hex] | |
| 22 // | |
| 23 // The input is read from input.h264. The output is written to output.i420 if it | |
| 24 // is given. It also verifies the MD5 sum of the decoded I420 data if the | |
| 25 // expected MD5 sum is given. | |
| 26 | |
| 27 namespace content { | |
| 28 namespace { | |
| 29 | |
| 30 // These are the command line parameters | |
| 31 base::FilePath g_input_file; | |
| 32 base::FilePath g_output_file; | |
| 33 std::string g_md5sum; | |
| 34 | |
| 35 // These default values are used if nothing is specified in the command line. | |
| 36 const base::FilePath::CharType* kDefaultInputFile = | |
| 37 FILE_PATH_LITERAL("test-25fps.h264"); | |
| 38 const char* kDefaultMD5Sum = "3af866863225b956001252ebeccdb71d"; | |
| 39 | |
| 40 // This class encapsulates the use of VaapiH264Decoder to a simpler interface. | |
| 41 // It reads an H.264 Annex B bytestream from a file and outputs the decoded | |
| 42 // frames (in I420 format) to another file. | |
|
Ami GONE FROM CHROMIUM
2013/12/09 18:55:56
Will probably be useful to drop a hint here about
chihchung
2013/12/24 07:33:47
Done.
| |
| 43 // | |
| 44 // To use the class, construct an instance, call Initialize() to specify the | |
| 45 // input and output file paths, then call Run() to decode the whole stream and | |
| 46 // output the frames. | |
| 47 // | |
| 48 // This class must be created, called and destroyed on a single thread, and | |
| 49 // does nothing internally on any other thread. | |
| 50 class VaapiH264DecoderLoop { | |
| 51 public: | |
| 52 VaapiH264DecoderLoop(); | |
| 53 ~VaapiH264DecoderLoop(); | |
| 54 | |
| 55 // Initialize the decoder. Return true if successful. | |
| 56 bool Initialize(base::FilePath input_file, base::FilePath output_file); | |
| 57 | |
| 58 // Run the decode loop. The decoded data is written to the file specified by | |
| 59 // output_file in Initialize(). Return true if all decoding is successful. | |
| 60 bool Run(); | |
| 61 | |
| 62 // Get the MD5 sum of the decoded data. | |
| 63 std::string GetMD5Sum(); | |
| 64 | |
| 65 private: | |
| 66 // Callback from the decoder when a picture is decoded. | |
| 67 void OutputPicture(int32 input_id, | |
| 68 const scoped_refptr<VASurface>& va_surface); | |
| 69 | |
| 70 // Recycle one surface and put it on available_surfaces_ list. | |
| 71 void RecycleSurface(VASurfaceID va_surface_id); | |
| 72 | |
| 73 // Give all surfaces in available_surfaces_ to the decoder. | |
| 74 void RefillSurfaces(); | |
| 75 | |
| 76 // Free the current set of surfaces and allocate a new set of | |
| 77 // surfaces. Returns true when successful. | |
| 78 bool AllocateNewSurfaces(); | |
| 79 | |
| 80 // Use the data in the frame: write to file and update MD5 sum. | |
| 81 bool ProcessVideoFrame(const scoped_refptr<media::VideoFrame>& frame); | |
| 82 | |
| 83 scoped_ptr<VaapiWrapper> wrapper_; | |
| 84 scoped_ptr<VaapiH264Decoder> decoder_; | |
| 85 std::string data_; // data read from input_file | |
| 86 base::FilePath output_file_; // output data is written to this file | |
| 87 std::vector<VASurfaceID> available_surfaces_; | |
| 88 | |
| 89 // These members (x_display_, num_outputted_pictures_, num_surfaces_) | |
| 90 // need to be initialized and possibly freed manually. | |
| 91 Display* x_display_; | |
| 92 int num_outputted_pictures_; // number of pictures already outputted | |
| 93 size_t num_surfaces_; // number of surfaces in the current set of surfaces | |
| 94 base::MD5Context md5_context_; | |
| 95 }; | |
| 96 | |
| 97 VaapiH264DecoderLoop::VaapiH264DecoderLoop() | |
| 98 : x_display_(NULL), num_outputted_pictures_(0), num_surfaces_(0) { | |
| 99 base::MD5Init(&md5_context_); | |
| 100 } | |
| 101 | |
| 102 VaapiH264DecoderLoop::~VaapiH264DecoderLoop() { | |
| 103 // We need to destruct decoder and wrapper first because: | |
| 104 // (1) The decoder has a reference to the wrapper. | |
| 105 // (2) The wrapper has a reference to x_display_. | |
| 106 decoder_.reset(); | |
| 107 wrapper_.reset(); | |
| 108 | |
| 109 if (x_display_) { | |
| 110 XCloseDisplay(x_display_); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void LogOnError(VaapiH264Decoder::VAVDAH264DecoderFailure error) { | |
| 115 LOG(ERROR) << "Oh noes! Decoder failed: " << error; | |
|
Ami GONE FROM CHROMIUM
2013/12/09 18:55:56
The choice of ERROR here is effectively equivalent
chihchung
2013/12/24 07:33:47
Done.
| |
| 116 } | |
| 117 | |
| 118 bool VaapiH264DecoderLoop::Initialize(base::FilePath input_file, | |
| 119 base::FilePath output_file) { | |
| 120 x_display_ = XOpenDisplay(NULL); | |
| 121 if (!x_display_) { | |
| 122 LOG(ERROR) << "Can't open X display"; | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 media::VideoCodecProfile profile = media::H264PROFILE_HIGH; | |
| 127 base::Closure report_error_cb = | |
| 128 base::Bind(&LogOnError, VaapiH264Decoder::VAAPI_ERROR); | |
| 129 wrapper_ = VaapiWrapper::Create(profile, x_display_, report_error_cb); | |
| 130 if (!wrapper_.get()) { | |
| 131 LOG(ERROR) << "Can't create vaapi wrapper"; | |
| 132 return false; | |
| 133 } | |
| 134 | |
| 135 decoder_.reset(new VaapiH264Decoder( | |
| 136 wrapper_.get(), | |
| 137 base::Bind(&VaapiH264DecoderLoop::OutputPicture, base::Unretained(this)), | |
| 138 base::Bind(&LogOnError))); | |
| 139 | |
| 140 if (!base::ReadFileToString(input_file, &data_)) { | |
| 141 LOG(ERROR) << "failed to read input data from " << input_file.value(); | |
| 142 return false; | |
| 143 } | |
| 144 | |
| 145 const int input_id = 0; // We don't use input_id in this class. | |
| 146 decoder_->SetStream( | |
| 147 reinterpret_cast<const uint8*>(data_.c_str()), data_.size(), input_id); | |
| 148 | |
| 149 // This creates or truncates output_file. | |
| 150 // Without it, AppendToFile() will not work. | |
| 151 if (!output_file.empty()) { | |
| 152 if (file_util::WriteFile(output_file, NULL, 0) != 0) { | |
| 153 return false; | |
| 154 } | |
| 155 output_file_ = output_file; | |
| 156 } | |
| 157 | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 bool VaapiH264DecoderLoop::Run() { | |
| 162 while (1) { | |
| 163 switch (decoder_->Decode()) { | |
| 164 case VaapiH264Decoder::kDecodeError: | |
| 165 LOG(ERROR) << "Decode Error"; | |
| 166 return false; | |
| 167 case VaapiH264Decoder::kAllocateNewSurfaces: | |
| 168 VLOG(1) << "Allocate new surfaces"; | |
| 169 if (!AllocateNewSurfaces()) { | |
| 170 LOG(ERROR) << "Failed to allocate new surfaces"; | |
| 171 return false; | |
| 172 } | |
| 173 break; | |
| 174 case VaapiH264Decoder::kRanOutOfStreamData: { | |
| 175 bool rc = decoder_->Flush(); | |
| 176 VLOG(1) << "Flush returns " << rc; | |
| 177 return rc; | |
| 178 } | |
| 179 case VaapiH264Decoder::kRanOutOfSurfaces: | |
| 180 VLOG(1) << "Ran out of surfaces"; | |
| 181 RefillSurfaces(); | |
| 182 break; | |
| 183 } | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 std::string VaapiH264DecoderLoop::GetMD5Sum() { | |
| 188 base::MD5Digest digest; | |
| 189 base::MD5Final(&digest, &md5_context_); | |
| 190 return MD5DigestToBase16(digest); | |
| 191 } | |
| 192 | |
| 193 scoped_refptr<media::VideoFrame> CopyNV12ToI420(VAImage* image, void* mem) { | |
| 194 int width = image->width; | |
| 195 int height = image->height; | |
| 196 | |
| 197 DVLOG(1) << "CopyNV12ToI420 width=" << width << ", height=" << height; | |
| 198 | |
| 199 const gfx::Size coded_size(width, height); | |
| 200 const gfx::Rect visible_rect(width, height); | |
| 201 const gfx::Size natural_size(width, height); | |
| 202 | |
| 203 scoped_refptr<media::VideoFrame> frame = | |
| 204 media::VideoFrame::CreateFrame(media::VideoFrame::I420, | |
| 205 coded_size, | |
| 206 visible_rect, | |
| 207 natural_size, | |
| 208 base::TimeDelta()); | |
| 209 | |
| 210 uint8_t* mem_byte_ptr = static_cast<uint8_t*>(mem); | |
| 211 uint8_t* src_y = mem_byte_ptr + image->offsets[0]; | |
| 212 uint8_t* src_uv = mem_byte_ptr + image->offsets[1]; | |
| 213 int src_stride_y = image->pitches[0]; | |
| 214 int src_stride_uv = image->pitches[1]; | |
| 215 | |
| 216 uint8_t* dst_y = frame->data(media::VideoFrame::kYPlane); | |
| 217 uint8_t* dst_u = frame->data(media::VideoFrame::kUPlane); | |
| 218 uint8_t* dst_v = frame->data(media::VideoFrame::kVPlane); | |
| 219 int dst_stride_y = frame->stride(media::VideoFrame::kYPlane); | |
| 220 int dst_stride_u = frame->stride(media::VideoFrame::kUPlane); | |
| 221 int dst_stride_v = frame->stride(media::VideoFrame::kVPlane); | |
| 222 | |
| 223 int rc = libyuv::NV12ToI420(src_y, | |
| 224 src_stride_y, | |
| 225 src_uv, | |
| 226 src_stride_uv, | |
| 227 dst_y, | |
| 228 dst_stride_y, | |
| 229 dst_u, | |
| 230 dst_stride_u, | |
| 231 dst_v, | |
| 232 dst_stride_v, | |
| 233 width, | |
| 234 height); | |
| 235 CHECK_EQ(0, rc); | |
| 236 return frame; | |
| 237 } | |
| 238 | |
| 239 bool VaapiH264DecoderLoop::ProcessVideoFrame( | |
| 240 const scoped_refptr<media::VideoFrame>& frame) { | |
| 241 frame->HashFrameForTesting(&md5_context_); | |
| 242 | |
| 243 if (output_file_.empty()) | |
| 244 return true; | |
| 245 | |
| 246 for (size_t i = 0; i < media::VideoFrame::NumPlanes(frame->format()); i++) { | |
| 247 int to_write = media::VideoFrame::PlaneAllocationSize( | |
| 248 frame->format(), i, frame->coded_size()); | |
| 249 const char* buf = reinterpret_cast<const char*>(frame->data(i)); | |
| 250 int written = file_util::AppendToFile(output_file_, buf, to_write); | |
| 251 if (written != to_write) | |
| 252 return false; | |
| 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_->GetVaImageForTesting(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_->ReturnVaImageForTesting(&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_->ReturnVaImageForTesting(&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 |