Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/webrtc/modules/video_coding/codecs/vp8/test/vp8_impl_unittest.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 9.7 KB
Line 
1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "testing/gtest/include/gtest/gtest.h"
12#include "webrtc/modules/video_coding/codecs/test_framework/unit_test.h"
13#include "webrtc/modules/video_coding/codecs/test_framework/video_source.h"
14#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
15#include "webrtc/system_wrappers/interface/scoped_ptr.h"
16#include "webrtc/system_wrappers/interface/tick_util.h"
17#include "webrtc/test/testsupport/fileutils.h"
18#include "webrtc/test/testsupport/gtest_disable.h"
19
20namespace webrtc {
21
22enum { kMaxWaitEncTimeMs = 100 };
23enum { kMaxWaitDecTimeMs = 25 };
24
25
26// TODO(mikhal): Replace these with mocks.
27class Vp8UnitTestEncodeCompleteCallback : public webrtc::EncodedImageCallback {
28 public:
29  Vp8UnitTestEncodeCompleteCallback(VideoFrame* frame,
30                                    unsigned int decoderSpecificSize,
31                                    void* decoderSpecificInfo)
32     : encoded_video_frame_(frame),
33       encode_complete_(false) {}
34  int Encoded(EncodedImage& encodedImage,
35              const CodecSpecificInfo* codecSpecificInfo,
36              const RTPFragmentationHeader*);
37  bool EncodeComplete();
38  // Note that this only makes sense if an encode has been completed
39  VideoFrameType EncodedFrameType() const {return  encoded_frame_type_;}
40
41 private:
42  VideoFrame* encoded_video_frame_;
43  bool encode_complete_;
44  VideoFrameType encoded_frame_type_;
45};
46
47int Vp8UnitTestEncodeCompleteCallback::Encoded(EncodedImage& encodedImage,
48    const CodecSpecificInfo* codecSpecificInfo,
49    const RTPFragmentationHeader* fragmentation) {
50  encoded_video_frame_->VerifyAndAllocate(encodedImage._size);
51  encoded_video_frame_->CopyFrame(encodedImage._size, encodedImage._buffer);
52  encoded_video_frame_->SetLength(encodedImage._length);
53  // TODO(mikhal): Update frame type API.
54  // encoded_video_frame_->SetFrameType(encodedImage._frameType);
55  encoded_video_frame_->SetWidth(encodedImage._encodedWidth);
56  encoded_video_frame_->SetHeight(encodedImage._encodedHeight);
57  encoded_video_frame_->SetTimeStamp(encodedImage._timeStamp);
58  encode_complete_ = true;
59  encoded_frame_type_ = encodedImage._frameType;
60  return 0;
61}
62
63bool Vp8UnitTestEncodeCompleteCallback::EncodeComplete() {
64  if (encode_complete_) {
65    encode_complete_ = false;
66    return true;
67  }
68  return false;
69}
70
71class Vp8UnitTestDecodeCompleteCallback : public webrtc::DecodedImageCallback {
72 public:
73  explicit Vp8UnitTestDecodeCompleteCallback(I420VideoFrame* frame)
74      : decoded_video_frame_(frame),
75        decode_complete(false) {}
76  int Decoded(webrtc::I420VideoFrame& frame);
77  bool DecodeComplete();
78 private:
79  I420VideoFrame* decoded_video_frame_;
80  bool decode_complete;
81};
82
83bool Vp8UnitTestDecodeCompleteCallback::DecodeComplete() {
84  if (decode_complete) {
85    decode_complete = false;
86    return true;
87  }
88  return false;
89}
90
91int Vp8UnitTestDecodeCompleteCallback::Decoded(I420VideoFrame& image) {
92  decoded_video_frame_->CopyFrame(image);
93  decode_complete = true;
94  return 0;
95}
96
97class TestVp8Impl : public ::testing::Test {
98 protected:
99  virtual void SetUp() {
100    encoder_.reset(VP8Encoder::Create());
101    decoder_.reset(VP8Decoder::Create());
102    memset(&codec_inst_, 0, sizeof(codec_inst_));
103    encode_complete_callback_.reset(new
104        Vp8UnitTestEncodeCompleteCallback(&encoded_video_frame_, 0, NULL));
105    decode_complete_callback_.reset(new
106        Vp8UnitTestDecodeCompleteCallback(&decoded_video_frame_));
107    encoder_->RegisterEncodeCompleteCallback(encode_complete_callback_.get());
108    decoder_->RegisterDecodeCompleteCallback(decode_complete_callback_.get());
109    // Using a QCIF image (aligned stride (u,v planes) > width).
110    // Processing only one frame.
111    const VideoSource source(test::ResourcePath("paris_qcif", "yuv"), kQCIF);
112    length_source_frame_ = source.GetFrameLength();
113    source_buffer_.reset(new uint8_t[length_source_frame_]);
114    source_file_ = fopen(source.GetFileName().c_str(), "rb");
115    ASSERT_TRUE(source_file_ != NULL);
116    // Set input frame.
117    ASSERT_EQ(fread(source_buffer_.get(), 1, length_source_frame_,
118        source_file_), length_source_frame_);
119    codec_inst_.width = source.GetWidth();
120    codec_inst_.height = source.GetHeight();
121    codec_inst_.maxFramerate = source.GetFrameRate();
122    // Setting aligned stride values.
123    int stride_uv = 0;
124    int stride_y = 0;
125    Calc16ByteAlignedStride(codec_inst_.width, &stride_y, &stride_uv);
126    EXPECT_EQ(stride_y, 176);
127    EXPECT_EQ(stride_uv, 96);
128
129    input_frame_.CreateEmptyFrame(codec_inst_.width, codec_inst_.height,
130                                  stride_y, stride_uv, stride_uv);
131    // Using ConvertToI420 to add stride to the image.
132    EXPECT_EQ(0, ConvertToI420(kI420, source_buffer_.get(), 0, 0,
133                               codec_inst_.width, codec_inst_.height,
134                               0, kRotateNone, &input_frame_));
135  }
136
137  void SetUpEncodeDecode() {
138    codec_inst_.startBitrate = 300;
139    codec_inst_.maxBitrate = 4000;
140    codec_inst_.qpMax = 56;
141    codec_inst_.codecSpecific.VP8.denoisingOn = true;
142
143    EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
144        encoder_->InitEncode(&codec_inst_, 1, 1440));
145    EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->InitDecode(&codec_inst_, 1));
146  }
147
148  int WaitForEncodedFrame() const {
149    int64_t startTime = TickTime::MillisecondTimestamp();
150    while (TickTime::MillisecondTimestamp() - startTime < kMaxWaitEncTimeMs) {
151      if (encode_complete_callback_->EncodeComplete()) {
152        return encoded_video_frame_.Length();
153      }
154    }
155    return 0;
156  }
157
158  int WaitForDecodedFrame() const {
159    int64_t startTime = TickTime::MillisecondTimestamp();
160    while (TickTime::MillisecondTimestamp() - startTime < kMaxWaitDecTimeMs) {
161      if (decode_complete_callback_->DecodeComplete()) {
162        return CalcBufferSize(kI420, decoded_video_frame_.width(),
163                              decoded_video_frame_.height());
164      }
165    }
166    return 0;
167  }
168
169  void VideoFrameToEncodedImage(VideoFrame& frame, EncodedImage &image) {
170    image._buffer = frame.Buffer();
171    image._length = frame.Length();
172    image._size = frame.Size();
173    image._timeStamp = frame.TimeStamp();
174    image._encodedWidth = frame.Width();
175    image._encodedHeight = frame.Height();
176    image._completeFrame = true;
177  }
178
179  scoped_ptr<Vp8UnitTestEncodeCompleteCallback> encode_complete_callback_;
180  scoped_ptr<Vp8UnitTestDecodeCompleteCallback> decode_complete_callback_;
181  scoped_array<uint8_t> source_buffer_;
182  FILE* source_file_;
183  I420VideoFrame input_frame_;
184  scoped_ptr<VideoEncoder> encoder_;
185  scoped_ptr<VideoDecoder> decoder_;
186  VideoFrame encoded_video_frame_;
187  I420VideoFrame decoded_video_frame_;
188  unsigned int length_source_frame_;
189  VideoCodec codec_inst_;
190};
191
192TEST_F(TestVp8Impl, DISABLED_ON_ANDROID(BaseUnitTest)) {
193  // TODO(mikhal): Remove dependency. Move all test code here.
194  EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
195  UnitTest unittest;
196  unittest.SetEncoder(encoder_.get());
197  unittest.SetDecoder(decoder_.get());
198  unittest.Setup();
199  unittest.Perform();
200  unittest.Print();
201}
202
203TEST_F(TestVp8Impl, EncoderParameterTest) {
204  strncpy(codec_inst_.plName, "VP8", 31);
205  codec_inst_.plType = 126;
206  codec_inst_.maxBitrate = 0;
207  codec_inst_.minBitrate = 0;
208  codec_inst_.width = 1440;
209  codec_inst_.height = 1080;
210  codec_inst_.maxFramerate = 30;
211  codec_inst_.startBitrate = 300;
212  codec_inst_.qpMax = 56;
213  codec_inst_.codecSpecific.VP8.complexity = kComplexityNormal;
214  codec_inst_.codecSpecific.VP8.numberOfTemporalLayers = 1;
215  // Calls before InitEncode().
216  EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
217  int bit_rate = 300;
218  EXPECT_EQ(WEBRTC_VIDEO_CODEC_UNINITIALIZED,
219            encoder_->SetRates(bit_rate, codec_inst_.maxFramerate));
220
221  EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
222            encoder_->InitEncode(&codec_inst_, 1, 1440));
223
224  // Decoder parameter tests.
225  // Calls before InitDecode().
226  EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Release());
227  EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->InitDecode(&codec_inst_, 1));
228}
229
230TEST_F(TestVp8Impl, DISABLED_ON_ANDROID(AlignedStrideEncodeDecode)) {
231  SetUpEncodeDecode();
232  encoder_->Encode(input_frame_, NULL, NULL);
233  EXPECT_GT(WaitForEncodedFrame(), 0);
234  EncodedImage encodedImage;
235  VideoFrameToEncodedImage(encoded_video_frame_, encodedImage);
236  // First frame should be a key frame.
237  encodedImage._frameType = kKeyFrame;
238  EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encodedImage, false, NULL));
239  EXPECT_GT(WaitForDecodedFrame(), 0);
240  // Compute PSNR on all planes (faster than SSIM).
241  EXPECT_GT(I420PSNR(&input_frame_, &decoded_video_frame_), 36);
242}
243
244TEST_F(TestVp8Impl, DISABLED_ON_ANDROID(DecodeWithACompleteKeyFrame)) {
245  SetUpEncodeDecode();
246  encoder_->Encode(input_frame_, NULL, NULL);
247  EXPECT_GT(WaitForEncodedFrame(), 0);
248  EncodedImage encodedImage;
249  VideoFrameToEncodedImage(encoded_video_frame_, encodedImage);
250  // Setting complete to false -> should return an error.
251  encodedImage._completeFrame = false;
252  EXPECT_EQ(WEBRTC_VIDEO_CODEC_ERROR,
253      decoder_->Decode(encodedImage, false, NULL));
254  // Setting complete back to true. Forcing a delta frame.
255  encodedImage._frameType = kDeltaFrame;
256  encodedImage._completeFrame = true;
257  EXPECT_EQ(WEBRTC_VIDEO_CODEC_ERROR,
258      decoder_->Decode(encodedImage, false, NULL));
259  // Now setting a key frame.
260  encodedImage._frameType = kKeyFrame;
261  EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
262      decoder_->Decode(encodedImage, false, NULL));
263  EXPECT_GT(I420PSNR(&input_frame_, &decoded_video_frame_), 36);
264}
265
266}  // namespace webrtc
Note: See TracBrowser for help on using the repository browser.