Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: content/common/gpu/media/vaapi_h264_decoder_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698