• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019 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 "test/testsupport/ivf_video_frame_generator.h"
12 
13 #include <limits>
14 
15 #include "api/video/encoded_image.h"
16 #include "api/video/i420_buffer.h"
17 #include "api/video_codecs/video_codec.h"
18 #include "media/base/media_constants.h"
19 #include "modules/video_coding/codecs/h264/include/h264.h"
20 #include "modules/video_coding/codecs/vp8/include/vp8.h"
21 #include "modules/video_coding/codecs/vp9/include/vp9.h"
22 #include "modules/video_coding/include/video_error_codes.h"
23 #include "rtc_base/checks.h"
24 #include "rtc_base/system/file_wrapper.h"
25 
26 namespace webrtc {
27 namespace test {
28 namespace {
29 
30 constexpr int kMaxNextFrameWaitTemeoutMs = 1000;
31 
32 }  // namespace
33 
IvfVideoFrameGenerator(const std::string & file_name)34 IvfVideoFrameGenerator::IvfVideoFrameGenerator(const std::string& file_name)
35     : callback_(this),
36       file_reader_(IvfFileReader::Create(FileWrapper::OpenReadOnly(file_name))),
37       video_decoder_(CreateVideoDecoder(file_reader_->GetVideoCodecType())),
38       width_(file_reader_->GetFrameWidth()),
39       height_(file_reader_->GetFrameHeight()) {
40   RTC_CHECK(video_decoder_) << "No decoder found for file's video codec type";
41   VideoCodec codec_settings;
42   codec_settings.codecType = file_reader_->GetVideoCodecType();
43   codec_settings.width = file_reader_->GetFrameWidth();
44   codec_settings.height = file_reader_->GetFrameHeight();
45   // Set buffer pool size to max value to ensure that if users of generator,
46   // ex. test frameworks, will retain frames for quite a long time, decoder
47   // won't crash with buffers pool overflow error.
48   codec_settings.buffer_pool_size = std::numeric_limits<int>::max();
49   RTC_CHECK_EQ(video_decoder_->RegisterDecodeCompleteCallback(&callback_),
50                WEBRTC_VIDEO_CODEC_OK);
51   RTC_CHECK_EQ(
52       video_decoder_->InitDecode(&codec_settings, /*number_of_cores=*/1),
53       WEBRTC_VIDEO_CODEC_OK);
54 }
~IvfVideoFrameGenerator()55 IvfVideoFrameGenerator::~IvfVideoFrameGenerator() {
56   MutexLock lock(&lock_);
57   if (!file_reader_) {
58     return;
59   }
60   file_reader_->Close();
61   file_reader_.reset();
62   // Reset decoder to prevent it from async access to |this|.
63   video_decoder_.reset();
64   {
65     MutexLock frame_lock(&frame_decode_lock_);
66     next_frame_ = absl::nullopt;
67     // Set event in case another thread is waiting on it.
68     next_frame_decoded_.Set();
69   }
70 }
71 
NextFrame()72 FrameGeneratorInterface::VideoFrameData IvfVideoFrameGenerator::NextFrame() {
73   MutexLock lock(&lock_);
74   next_frame_decoded_.Reset();
75   RTC_CHECK(file_reader_);
76   if (!file_reader_->HasMoreFrames()) {
77     file_reader_->Reset();
78   }
79   absl::optional<EncodedImage> image = file_reader_->NextFrame();
80   RTC_CHECK(image);
81   // Last parameter is undocumented and there is no usage of it found.
82   RTC_CHECK_EQ(WEBRTC_VIDEO_CODEC_OK,
83                video_decoder_->Decode(*image, /*missing_frames=*/false,
84                                       /*render_time_ms=*/0));
85   bool decoded = next_frame_decoded_.Wait(kMaxNextFrameWaitTemeoutMs);
86   RTC_CHECK(decoded) << "Failed to decode next frame in "
87                      << kMaxNextFrameWaitTemeoutMs << "ms. Can't continue";
88 
89   MutexLock frame_lock(&frame_decode_lock_);
90   rtc::scoped_refptr<VideoFrameBuffer> buffer =
91       next_frame_->video_frame_buffer();
92   if (width_ != static_cast<size_t>(buffer->width()) ||
93       height_ != static_cast<size_t>(buffer->height())) {
94     // Video adapter has requested a down-scale. Allocate a new buffer and
95     // return scaled version.
96     rtc::scoped_refptr<I420Buffer> scaled_buffer =
97         I420Buffer::Create(width_, height_);
98     scaled_buffer->ScaleFrom(*buffer->ToI420());
99     buffer = scaled_buffer;
100   }
101   return VideoFrameData(buffer, next_frame_->update_rect());
102 }
103 
ChangeResolution(size_t width,size_t height)104 void IvfVideoFrameGenerator::ChangeResolution(size_t width, size_t height) {
105   MutexLock lock(&lock_);
106   width_ = width;
107   height_ = height;
108 }
109 
Decoded(VideoFrame & decoded_image)110 int32_t IvfVideoFrameGenerator::DecodedCallback::Decoded(
111     VideoFrame& decoded_image) {
112   Decoded(decoded_image, 0, 0);
113   return WEBRTC_VIDEO_CODEC_OK;
114 }
Decoded(VideoFrame & decoded_image,int64_t decode_time_ms)115 int32_t IvfVideoFrameGenerator::DecodedCallback::Decoded(
116     VideoFrame& decoded_image,
117     int64_t decode_time_ms) {
118   Decoded(decoded_image, decode_time_ms, 0);
119   return WEBRTC_VIDEO_CODEC_OK;
120 }
Decoded(VideoFrame & decoded_image,absl::optional<int32_t> decode_time_ms,absl::optional<uint8_t> qp)121 void IvfVideoFrameGenerator::DecodedCallback::Decoded(
122     VideoFrame& decoded_image,
123     absl::optional<int32_t> decode_time_ms,
124     absl::optional<uint8_t> qp) {
125   reader_->OnFrameDecoded(decoded_image);
126 }
127 
OnFrameDecoded(const VideoFrame & decoded_frame)128 void IvfVideoFrameGenerator::OnFrameDecoded(const VideoFrame& decoded_frame) {
129   MutexLock lock(&frame_decode_lock_);
130   next_frame_ = decoded_frame;
131   next_frame_decoded_.Set();
132 }
133 
CreateVideoDecoder(VideoCodecType codec_type)134 std::unique_ptr<VideoDecoder> IvfVideoFrameGenerator::CreateVideoDecoder(
135     VideoCodecType codec_type) {
136   if (codec_type == VideoCodecType::kVideoCodecVP8) {
137     return VP8Decoder::Create();
138   }
139   if (codec_type == VideoCodecType::kVideoCodecVP9) {
140     return VP9Decoder::Create();
141   }
142   if (codec_type == VideoCodecType::kVideoCodecH264) {
143     return H264Decoder::Create();
144   }
145   return nullptr;
146 }
147 
148 }  // namespace test
149 }  // namespace webrtc
150