xref: /aosp_15_r20/external/webrtc/test/testsupport/ivf_video_frame_generator.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 TimeDelta kMaxNextFrameWaitTimeout = TimeDelta::Seconds(1);
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   VideoDecoder::Settings decoder_settings;
42   decoder_settings.set_codec_type(file_reader_->GetVideoCodecType());
43   decoder_settings.set_max_render_resolution(
44       {file_reader_->GetFrameWidth(), 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   decoder_settings.set_buffer_pool_size(std::numeric_limits<int>::max());
49   RTC_CHECK_EQ(video_decoder_->RegisterDecodeCompleteCallback(&callback_),
50                WEBRTC_VIDEO_CODEC_OK);
51   RTC_CHECK(video_decoder_->Configure(decoder_settings));
52 }
~IvfVideoFrameGenerator()53 IvfVideoFrameGenerator::~IvfVideoFrameGenerator() {
54   MutexLock lock(&lock_);
55   if (!file_reader_) {
56     return;
57   }
58   file_reader_->Close();
59   file_reader_.reset();
60   // Reset decoder to prevent it from async access to `this`.
61   video_decoder_.reset();
62   {
63     MutexLock frame_lock(&frame_decode_lock_);
64     next_frame_ = absl::nullopt;
65     // Set event in case another thread is waiting on it.
66     next_frame_decoded_.Set();
67   }
68 }
69 
NextFrame()70 FrameGeneratorInterface::VideoFrameData IvfVideoFrameGenerator::NextFrame() {
71   MutexLock lock(&lock_);
72   next_frame_decoded_.Reset();
73   RTC_CHECK(file_reader_);
74   if (!file_reader_->HasMoreFrames()) {
75     file_reader_->Reset();
76   }
77   absl::optional<EncodedImage> image = file_reader_->NextFrame();
78   RTC_CHECK(image);
79   // Last parameter is undocumented and there is no usage of it found.
80   RTC_CHECK_EQ(WEBRTC_VIDEO_CODEC_OK,
81                video_decoder_->Decode(*image, /*missing_frames=*/false,
82                                       /*render_time_ms=*/0));
83   bool decoded = next_frame_decoded_.Wait(kMaxNextFrameWaitTimeout);
84   RTC_CHECK(decoded) << "Failed to decode next frame in "
85                      << kMaxNextFrameWaitTimeout << ". Can't continue";
86 
87   MutexLock frame_lock(&frame_decode_lock_);
88   rtc::scoped_refptr<VideoFrameBuffer> buffer =
89       next_frame_->video_frame_buffer();
90   if (width_ != static_cast<size_t>(buffer->width()) ||
91       height_ != static_cast<size_t>(buffer->height())) {
92     // Video adapter has requested a down-scale. Allocate a new buffer and
93     // return scaled version.
94     rtc::scoped_refptr<I420Buffer> scaled_buffer =
95         I420Buffer::Create(width_, height_);
96     scaled_buffer->ScaleFrom(*buffer->ToI420());
97     buffer = scaled_buffer;
98   }
99   return VideoFrameData(buffer, next_frame_->update_rect());
100 }
101 
ChangeResolution(size_t width,size_t height)102 void IvfVideoFrameGenerator::ChangeResolution(size_t width, size_t height) {
103   MutexLock lock(&lock_);
104   width_ = width;
105   height_ = height;
106 }
107 
Decoded(VideoFrame & decoded_image)108 int32_t IvfVideoFrameGenerator::DecodedCallback::Decoded(
109     VideoFrame& decoded_image) {
110   Decoded(decoded_image, 0, 0);
111   return WEBRTC_VIDEO_CODEC_OK;
112 }
Decoded(VideoFrame & decoded_image,int64_t decode_time_ms)113 int32_t IvfVideoFrameGenerator::DecodedCallback::Decoded(
114     VideoFrame& decoded_image,
115     int64_t decode_time_ms) {
116   Decoded(decoded_image, decode_time_ms, 0);
117   return WEBRTC_VIDEO_CODEC_OK;
118 }
Decoded(VideoFrame & decoded_image,absl::optional<int32_t> decode_time_ms,absl::optional<uint8_t> qp)119 void IvfVideoFrameGenerator::DecodedCallback::Decoded(
120     VideoFrame& decoded_image,
121     absl::optional<int32_t> decode_time_ms,
122     absl::optional<uint8_t> qp) {
123   reader_->OnFrameDecoded(decoded_image);
124 }
125 
OnFrameDecoded(const VideoFrame & decoded_frame)126 void IvfVideoFrameGenerator::OnFrameDecoded(const VideoFrame& decoded_frame) {
127   MutexLock lock(&frame_decode_lock_);
128   next_frame_ = decoded_frame;
129   next_frame_decoded_.Set();
130 }
131 
CreateVideoDecoder(VideoCodecType codec_type)132 std::unique_ptr<VideoDecoder> IvfVideoFrameGenerator::CreateVideoDecoder(
133     VideoCodecType codec_type) {
134   if (codec_type == VideoCodecType::kVideoCodecVP8) {
135     return VP8Decoder::Create();
136   }
137   if (codec_type == VideoCodecType::kVideoCodecVP9) {
138     return VP9Decoder::Create();
139   }
140   if (codec_type == VideoCodecType::kVideoCodecH264) {
141     return H264Decoder::Create();
142   }
143   return nullptr;
144 }
145 
146 }  // namespace test
147 }  // namespace webrtc
148