diff --git a/webrtc/modules/video_coding/BUILD.gn b/webrtc/modules/video_coding/BUILD.gn index 643260a946..effe45961a 100644 --- a/webrtc/modules/video_coding/BUILD.gn +++ b/webrtc/modules/video_coding/BUILD.gn @@ -311,6 +311,8 @@ if (rtc_include_tests) { "codecs/test/predictive_packet_manipulator.h", "codecs/test/stats.cc", "codecs/test/stats.h", + "codecs/test/video_codec_test.cc", + "codecs/test/video_codec_test.h", "codecs/test/videoprocessor.cc", "codecs/test/videoprocessor.h", ] @@ -328,6 +330,7 @@ if (rtc_include_tests) { "../../common_video:common_video", "../../system_wrappers:system_wrappers", "../../test:test_support", + "../../test:video_test_common", "../../test:video_test_support", ] } @@ -378,13 +381,16 @@ if (rtc_include_tests) { testonly = true sources = [ + "codecs/h264/test/h264_impl_unittest.cc", "codecs/test/videoprocessor_integrationtest.cc", "codecs/vp8/test/vp8_impl_unittest.cc", "codecs/vp9/test/vp9_impl_unittest.cc", ] deps = [ + ":video_codecs_test_framework", ":video_coding_videoprocessor_integration_test", + ":webrtc_h264", ":webrtc_vp8", ":webrtc_vp9", "../../api:video_frame_api", diff --git a/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc b/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc index c26b94c04d..8407451bd4 100644 --- a/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc +++ b/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.cc @@ -357,7 +357,14 @@ int32_t H264DecoderImpl::Decode(const EncodedImage& input_image, video_frame->video_frame_buffer()->DataV()); video_frame->set_timestamp(input_image._timeStamp); - int32_t ret; + rtc::Optional qp; + // TODO(sakal): Maybe it is possible to get QP directly from FFmpeg. + h264_bitstream_parser_.ParseBitstream(input_image._buffer, + input_image._length); + int qp_int; + if (h264_bitstream_parser_.GetLastSliceQp(&qp_int)) { + qp.emplace(qp_int); + } // The decoded image may be larger than what is supposed to be visible, see // |AVGetBuffer2|'s use of |avcodec_align_dimensions|. This crops the image @@ -376,19 +383,17 @@ int32_t H264DecoderImpl::Decode(const EncodedImage& input_image, video_frame->rotation()); // TODO(nisse): Timestamp and rotation are all zero here. Change decoder // interface to pass a VideoFrameBuffer instead of a VideoFrame? - ret = decoded_image_callback_->Decoded(cropped_frame); + decoded_image_callback_->Decoded(cropped_frame, rtc::Optional(), + qp); } else { // Return decoded frame. - ret = decoded_image_callback_->Decoded(*video_frame); + decoded_image_callback_->Decoded(*video_frame, rtc::Optional(), + qp); } // Stop referencing it, possibly freeing |video_frame|. av_frame_unref(av_frame_.get()); video_frame = nullptr; - if (ret) { - LOG(LS_WARNING) << "DecodedImageCallback::Decoded returned " << ret; - return ret; - } return WEBRTC_VIDEO_CODEC_OK; } diff --git a/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.h b/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.h index fc730a4f4d..44c2acb2e0 100644 --- a/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.h +++ b/webrtc/modules/video_coding/codecs/h264/h264_decoder_impl.h @@ -20,6 +20,7 @@ extern "C" { #include "third_party/ffmpeg/libavcodec/avcodec.h" } // extern "C" +#include "webrtc/common_video/h264/h264_bitstream_parser.h" #include "webrtc/common_video/include/i420_buffer_pool.h" namespace webrtc { @@ -77,6 +78,8 @@ class H264DecoderImpl : public H264Decoder { bool has_reported_init_; bool has_reported_error_; + + webrtc::H264BitstreamParser h264_bitstream_parser_; }; } // namespace webrtc diff --git a/webrtc/modules/video_coding/codecs/h264/test/h264_impl_unittest.cc b/webrtc/modules/video_coding/codecs/h264/test/h264_impl_unittest.cc new file mode 100644 index 0000000000..82992058c9 --- /dev/null +++ b/webrtc/modules/video_coding/codecs/h264/test/h264_impl_unittest.cc @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" +#include "webrtc/modules/video_coding/codecs/h264/include/h264.h" +#include "webrtc/modules/video_coding/codecs/test/video_codec_test.h" + +namespace webrtc { + +class TestH264Impl : public VideoCodecTest { + protected: + VideoEncoder* CreateEncoder() override { + return H264Encoder::Create(cricket::VideoCodec(cricket::kH264CodecName)); + } + + VideoDecoder* CreateDecoder() override { return H264Decoder::Create(); } + + VideoCodec codec_settings() override { + VideoCodec codec_inst; + codec_inst.codecType = webrtc::kVideoCodecH264; + // If frame dropping is false, we get a warning that bitrate can't + // be controlled for RC_QUALITY_MODE; RC_BITRATE_MODE and RC_TIMESTAMP_MODE + codec_inst.H264()->frameDroppingOn = true; + return codec_inst; + } +}; + +#ifdef WEBRTC_VIDEOPROCESSOR_H264_TESTS +#define MAYBE_EncodeDecode EncodeDecode +#define MAYBE_DecodedQpEqualsEncodedQp DecodedQpEqualsEncodedQp +#else +#define MAYBE_EncodeDecode DISABLED_EncodeDecode +#define MAYBE_DecodedQpEqualsEncodedQp DISABLED_DecodedQpEqualsEncodedQp +#endif + +TEST_F(TestH264Impl, MAYBE_EncodeDecode) { + EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, + encoder_->Encode(*input_frame_, nullptr, nullptr)); + EncodedImage encoded_frame; + ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame)); + // First frame should be a key frame. + encoded_frame._frameType = kVideoFrameKey; + EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, + decoder_->Decode(encoded_frame, false, nullptr)); + std::unique_ptr decoded_frame; + rtc::Optional decoded_qp; + ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp)); + ASSERT_TRUE(decoded_frame); + EXPECT_GT(I420PSNR(input_frame_.get(), decoded_frame.get()), 36); +} + +TEST_F(TestH264Impl, MAYBE_DecodedQpEqualsEncodedQp) { + EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, + encoder_->Encode(*input_frame_, nullptr, nullptr)); + EncodedImage encoded_frame; + ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame)); + // First frame should be a key frame. + encoded_frame._frameType = kVideoFrameKey; + EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, + decoder_->Decode(encoded_frame, false, nullptr)); + std::unique_ptr decoded_frame; + rtc::Optional decoded_qp; + ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp)); + ASSERT_TRUE(decoded_frame); + ASSERT_TRUE(decoded_qp); + EXPECT_EQ(encoded_frame.qp_, *decoded_qp); +} + +} // namespace webrtc diff --git a/webrtc/modules/video_coding/codecs/test/video_codec_test.cc b/webrtc/modules/video_coding/codecs/test/video_codec_test.cc new file mode 100644 index 0000000000..d0d86ce4d7 --- /dev/null +++ b/webrtc/modules/video_coding/codecs/test/video_codec_test.cc @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/modules/video_coding/codecs/test/video_codec_test.h" + +#include "webrtc/modules/video_coding/include/video_error_codes.h" +#include "webrtc/test/frame_utils.h" +#include "webrtc/test/testsupport/fileutils.h" + +static const int kEncodeTimeoutMs = 100; +static const int kDecodeTimeoutMs = 25; +// Set bitrate to get higher quality. +static const int kStartBitrate = 300; +static const int kTargetBitrate = 2000; +static const int kMaxBitrate = 4000; +static const int kWidth = 172; // Width of the input image. +static const int kHeight = 144; // Height of the input image. +static const int kMaxFramerate = 30; // Arbitrary value. + +namespace webrtc { + +EncodedImageCallback::Result +VideoCodecTest::FakeEncodeCompleteCallback::OnEncodedImage( + const EncodedImage& frame, + const CodecSpecificInfo* codec_specific_info, + const RTPFragmentationHeader* fragmentation) { + rtc::CritScope lock(&test_->encoded_frame_section_); + test_->encoded_frame_.emplace(frame); + test_->encoded_frame_event_.Set(); + return Result(Result::OK); +} + +void VideoCodecTest::FakeDecodeCompleteCallback::Decoded( + VideoFrame& frame, + rtc::Optional decode_time_ms, + rtc::Optional qp) { + rtc::CritScope lock(&test_->decoded_frame_section_); + test_->decoded_frame_.emplace(frame); + test_->decoded_qp_ = qp; + test_->decoded_frame_event_.Set(); +} + +void VideoCodecTest::SetUp() { + // Using a QCIF image. Processing only one frame. + FILE* source_file_ = + fopen(test::ResourcePath("paris_qcif", "yuv").c_str(), "rb"); + ASSERT_TRUE(source_file_ != NULL); + rtc::scoped_refptr video_frame_buffer( + test::ReadI420Buffer(kWidth, kHeight, source_file_)); + input_frame_.reset(new VideoFrame(video_frame_buffer, kVideoRotation_0, 0)); + fclose(source_file_); + + encoder_.reset(CreateEncoder()); + decoder_.reset(CreateDecoder()); + encoder_->RegisterEncodeCompleteCallback(&encode_complete_callback_); + decoder_->RegisterDecodeCompleteCallback(&decode_complete_callback_); + + InitCodecs(); +} + +bool VideoCodecTest::WaitForEncodedFrame(EncodedImage* frame) { + bool ret = encoded_frame_event_.Wait(kEncodeTimeoutMs); + EXPECT_TRUE(ret) << "Timed out while waiting for an encoded frame."; + // This becomes unsafe if there are multiple threads waiting for frames. + rtc::CritScope lock(&encoded_frame_section_); + EXPECT_TRUE(encoded_frame_); + if (encoded_frame_) { + *frame = std::move(*encoded_frame_); + encoded_frame_.reset(); + return true; + } else { + return false; + } +} + +bool VideoCodecTest::WaitForDecodedFrame(std::unique_ptr* frame, + rtc::Optional* qp) { + bool ret = decoded_frame_event_.Wait(kDecodeTimeoutMs); + EXPECT_TRUE(ret) << "Timed out while waiting for a decoded frame."; + // This becomes unsafe if there are multiple threads waiting for frames. + rtc::CritScope lock(&decoded_frame_section_); + EXPECT_TRUE(decoded_frame_); + if (decoded_frame_) { + frame->reset(new VideoFrame(std::move(*decoded_frame_))); + *qp = decoded_qp_; + decoded_frame_.reset(); + return true; + } else { + return false; + } +} + +void VideoCodecTest::InitCodecs() { + VideoCodec codec_inst = codec_settings(); + codec_inst.startBitrate = kStartBitrate; + codec_inst.targetBitrate = kTargetBitrate; + codec_inst.maxBitrate = kMaxBitrate; + codec_inst.maxFramerate = kMaxFramerate; + codec_inst.width = kWidth; + codec_inst.height = kHeight; + EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, + encoder_->InitEncode(&codec_inst, 1 /* number of cores */, + 0 /* max payload size (unused) */)); + EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, + decoder_->InitDecode(&codec_inst, 1 /* number of cores */)); +} + +} // namespace webrtc diff --git a/webrtc/modules/video_coding/codecs/test/video_codec_test.h b/webrtc/modules/video_coding/codecs/test/video_codec_test.h new file mode 100644 index 0000000000..65d20ee898 --- /dev/null +++ b/webrtc/modules/video_coding/codecs/test/video_codec_test.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEO_CODEC_TEST_H_ +#define WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEO_CODEC_TEST_H_ + +#include + +#include "webrtc/base/criticalsection.h" +#include "webrtc/base/event.h" +#include "webrtc/base/thread_annotations.h" +#include "webrtc/test/gtest.h" +#include "webrtc/video_decoder.h" +#include "webrtc/video_encoder.h" + +namespace webrtc { + +class VideoCodecTest : public ::testing::Test { + public: + VideoCodecTest() + : encode_complete_callback_(this), + decode_complete_callback_(this), + encoded_frame_event_(false /* manual reset */, + false /* initially signaled */), + decoded_frame_event_(false /* manual reset */, + false /* initially signaled */) {} + + protected: + class FakeEncodeCompleteCallback : public webrtc::EncodedImageCallback { + public: + explicit FakeEncodeCompleteCallback(VideoCodecTest* test) : test_(test) {} + + Result OnEncodedImage(const EncodedImage& frame, + const CodecSpecificInfo* codec_specific_info, + const RTPFragmentationHeader* fragmentation); + + private: + VideoCodecTest* const test_; + }; + + class FakeDecodeCompleteCallback : public webrtc::DecodedImageCallback { + public: + explicit FakeDecodeCompleteCallback(VideoCodecTest* test) : test_(test) {} + + int32_t Decoded(VideoFrame& frame) override { + RTC_NOTREACHED(); + return -1; + } + int32_t Decoded(VideoFrame& frame, int64_t decode_time_ms) override { + RTC_NOTREACHED(); + return -1; + } + void Decoded(VideoFrame& frame, + rtc::Optional decode_time_ms, + rtc::Optional qp) override; + + private: + VideoCodecTest* const test_; + }; + + virtual VideoEncoder* CreateEncoder() = 0; + virtual VideoDecoder* CreateDecoder() = 0; + virtual VideoCodec codec_settings() = 0; + + void SetUp() override; + + bool WaitForEncodedFrame(EncodedImage* frame); + bool WaitForDecodedFrame(std::unique_ptr* frame, + rtc::Optional* qp); + + std::unique_ptr input_frame_; + + std::unique_ptr encoder_; + std::unique_ptr decoder_; + + private: + void InitCodecs(); + + FakeEncodeCompleteCallback encode_complete_callback_; + FakeDecodeCompleteCallback decode_complete_callback_; + + rtc::Event encoded_frame_event_; + rtc::CriticalSection encoded_frame_section_; + rtc::Optional encoded_frame_ GUARDED_BY(encoded_frame_section_); + + rtc::Event decoded_frame_event_; + rtc::CriticalSection decoded_frame_section_; + rtc::Optional decoded_frame_ GUARDED_BY(decoded_frame_section_); + rtc::Optional decoded_qp_ GUARDED_BY(decoded_frame_section_); +}; + +} // namespace webrtc + +#endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEO_CODEC_TEST_H_ diff --git a/webrtc/modules/video_coding/codecs/vp9/test/vp9_impl_unittest.cc b/webrtc/modules/video_coding/codecs/vp9/test/vp9_impl_unittest.cc index fd4cbfd3bc..823543d325 100644 --- a/webrtc/modules/video_coding/codecs/vp9/test/vp9_impl_unittest.cc +++ b/webrtc/modules/video_coding/codecs/vp9/test/vp9_impl_unittest.cc @@ -8,161 +8,25 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "webrtc/base/criticalsection.h" -#include "webrtc/base/event.h" -#include "webrtc/base/thread_annotations.h" #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" #include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h" -#include "webrtc/test/frame_utils.h" -#include "webrtc/test/gtest.h" -#include "webrtc/test/testsupport/fileutils.h" +#include "webrtc/modules/video_coding/codecs/test/video_codec_test.h" namespace webrtc { -static const int kEncodeTimeoutMs = 100; -static const int kDecodeTimeoutMs = 25; -// Set a start bitrate to get higher quality. -static const int kStartBitrate = 300; -static const int kWidth = 172; // Width of the input image. -static const int kHeight = 144; // Height of the input image. -static const int kMaxFramerate = 30; // Arbitrary value. - -class TestVp9Impl : public ::testing::Test { - public: - TestVp9Impl() - : encode_complete_callback_(this), - decode_complete_callback_(this), - encoded_frame_event_(false /* manual reset */, - false /* initially signaled */), - decoded_frame_event_(false /* manual reset */, - false /* initially signaled */) {} - +class TestVp9Impl : public VideoCodecTest { protected: - class FakeEncodeCompleteCallback : public webrtc::EncodedImageCallback { - public: - explicit FakeEncodeCompleteCallback(TestVp9Impl* test) : test_(test) {} + VideoEncoder* CreateEncoder() override { return VP9Encoder::Create(); } - Result OnEncodedImage(const EncodedImage& frame, - const CodecSpecificInfo* codec_specific_info, - const RTPFragmentationHeader* fragmentation) { - rtc::CritScope lock(&test_->encoded_frame_section_); - test_->encoded_frame_ = rtc::Optional(frame); - test_->encoded_frame_event_.Set(); - return Result(Result::OK); - } + VideoDecoder* CreateDecoder() override { return VP9Decoder::Create(); } - private: - TestVp9Impl* const test_; - }; - - class FakeDecodeCompleteCallback : public webrtc::DecodedImageCallback { - public: - explicit FakeDecodeCompleteCallback(TestVp9Impl* test) : test_(test) {} - - int32_t Decoded(VideoFrame& frame) override { - RTC_NOTREACHED(); - return -1; - } - int32_t Decoded(VideoFrame& frame, int64_t decode_time_ms) override { - RTC_NOTREACHED(); - return -1; - } - void Decoded(VideoFrame& frame, - rtc::Optional decode_time_ms, - rtc::Optional qp) override { - rtc::CritScope lock(&test_->decoded_frame_section_); - test_->decoded_frame_ = rtc::Optional(frame); - test_->decoded_qp_ = qp; - test_->decoded_frame_event_.Set(); - } - - private: - TestVp9Impl* const test_; - }; - - void SetUp() override { - // Using a QCIF image. Processing only one frame. - FILE* source_file_ = - fopen(test::ResourcePath("paris_qcif", "yuv").c_str(), "rb"); - ASSERT_TRUE(source_file_ != NULL); - rtc::scoped_refptr video_frame_buffer( - test::ReadI420Buffer(kWidth, kHeight, source_file_)); - input_frame_.reset(new VideoFrame(video_frame_buffer, kVideoRotation_0, 0)); - fclose(source_file_); - - encoder_.reset(VP9Encoder::Create()); - decoder_.reset(VP9Decoder::Create()); - encoder_->RegisterEncodeCompleteCallback(&encode_complete_callback_); - decoder_->RegisterDecodeCompleteCallback(&decode_complete_callback_); - - InitCodecs(); + VideoCodec codec_settings() override { + VideoCodec codec_inst; + codec_inst.codecType = webrtc::kVideoCodecVP9; + codec_inst.VP9()->numberOfTemporalLayers = 1; + codec_inst.VP9()->numberOfSpatialLayers = 1; + return codec_inst; } - - void InitCodecs() { - VideoCodec codec_inst_; - codec_inst_.startBitrate = kStartBitrate; - codec_inst_.codecType = webrtc::kVideoCodecVP9; - codec_inst_.maxFramerate = kMaxFramerate; - codec_inst_.width = kWidth; - codec_inst_.height = kHeight; - codec_inst_.VP9()->numberOfTemporalLayers = 1; - codec_inst_.VP9()->numberOfSpatialLayers = 1; - EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, - encoder_->InitEncode(&codec_inst_, 1 /* number of cores */, - 0 /* max payload size (unused) */)); - EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, - decoder_->InitDecode(&codec_inst_, 1 /* number of cores */)); - } - - bool WaitForEncodedFrame(EncodedImage* frame) { - bool ret = encoded_frame_event_.Wait(kEncodeTimeoutMs); - EXPECT_TRUE(ret) << "Timed out while waiting for an encoded frame."; - // This becomes unsafe if there are multiple threads waiting for frames. - rtc::CritScope lock(&encoded_frame_section_); - EXPECT_TRUE(encoded_frame_); - if (encoded_frame_) { - *frame = std::move(*encoded_frame_); - encoded_frame_.reset(); - return true; - } else { - return false; - } - } - - bool WaitForDecodedFrame(std::unique_ptr* frame, - rtc::Optional* qp) { - bool ret = decoded_frame_event_.Wait(kDecodeTimeoutMs); - EXPECT_TRUE(ret) << "Timed out while waiting for a decoded frame."; - // This becomes unsafe if there are multiple threads waiting for frames. - rtc::CritScope lock(&decoded_frame_section_); - EXPECT_TRUE(decoded_frame_); - if (decoded_frame_) { - frame->reset(new VideoFrame(std::move(*decoded_frame_))); - *qp = decoded_qp_; - decoded_frame_.reset(); - return true; - } else { - return false; - } - } - - std::unique_ptr input_frame_; - - std::unique_ptr encoder_; - std::unique_ptr decoder_; - - private: - FakeEncodeCompleteCallback encode_complete_callback_; - FakeDecodeCompleteCallback decode_complete_callback_; - - rtc::Event encoded_frame_event_; - rtc::CriticalSection encoded_frame_section_; - rtc::Optional encoded_frame_ GUARDED_BY(encoded_frame_section_); - - rtc::Event decoded_frame_event_; - rtc::CriticalSection decoded_frame_section_; - rtc::Optional decoded_frame_ GUARDED_BY(decoded_frame_section_); - rtc::Optional decoded_qp_ GUARDED_BY(decoded_frame_section_); }; TEST_F(TestVp9Impl, EncodeDecode) {