1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*
* Copyright (c) 2019 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 "modules/video_coding/utility/ivf_file_reader.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "api/scoped_refptr.h"
#include "api/video/encoded_image.h"
#include "api/video/video_codec_type.h"
#include "api/video/video_frame_type.h"
#include "api/video_codecs/video_codec.h"
#include "modules/rtp_rtcp/source/byte_io.h"
#include "modules/video_coding/utility/ivf_defines.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/system/file_wrapper.h"
namespace webrtc {
namespace {
constexpr size_t kIvfFrameHeaderSize = 12;
constexpr int kCodecTypeBytesCount = 4;
constexpr uint8_t kFileHeaderStart[kCodecTypeBytesCount] = {'D', 'K', 'I', 'F'};
constexpr uint8_t kVp8Header[kCodecTypeBytesCount] = {'V', 'P', '8', '0'};
constexpr uint8_t kVp9Header[kCodecTypeBytesCount] = {'V', 'P', '9', '0'};
constexpr uint8_t kAv1Header[kCodecTypeBytesCount] = {'A', 'V', '0', '1'};
constexpr uint8_t kH264Header[kCodecTypeBytesCount] = {'H', '2', '6', '4'};
constexpr uint8_t kH265Header[kCodecTypeBytesCount] = {'H', '2', '6', '5'};
// RTP standard required 90kHz clock rate.
constexpr int32_t kRtpClockRateHz = 90000;
} // namespace
std::unique_ptr<IvfFileReader> IvfFileReader::Create(FileWrapper file) {
auto reader =
std::unique_ptr<IvfFileReader>(new IvfFileReader(std::move(file)));
if (!reader->Reset()) {
return nullptr;
}
return reader;
}
IvfFileReader::~IvfFileReader() {
Close();
}
bool IvfFileReader::Reset() {
// Set error to true while initialization.
has_error_ = true;
if (!file_.Rewind()) {
RTC_LOG(LS_ERROR) << "Failed to rewind IVF file";
return false;
}
uint8_t ivf_header[kIvfHeaderSize] = {0};
size_t read = file_.Read(&ivf_header, kIvfHeaderSize);
if (read != kIvfHeaderSize) {
RTC_LOG(LS_ERROR) << "Failed to read IVF header";
return false;
}
if (memcmp(&ivf_header[0], kFileHeaderStart, 4) != 0) {
RTC_LOG(LS_ERROR) << "File is not in IVF format: DKIF header expected";
return false;
}
std::optional<VideoCodecType> codec_type = ParseCodecType(ivf_header, 8);
if (!codec_type) {
return false;
}
codec_type_ = *codec_type;
width_ = ByteReader<uint16_t>::ReadLittleEndian(&ivf_header[12]);
height_ = ByteReader<uint16_t>::ReadLittleEndian(&ivf_header[14]);
if (width_ == 0 || height_ == 0) {
RTC_LOG(LS_ERROR) << "Invalid IVF header: width or height is 0";
return false;
}
time_scale_ = ByteReader<uint32_t>::ReadLittleEndian(&ivf_header[16]);
if (time_scale_ == 0) {
RTC_LOG(LS_ERROR) << "Invalid IVF header: time scale can't be 0";
return false;
}
num_frames_ = static_cast<size_t>(
ByteReader<uint32_t>::ReadLittleEndian(&ivf_header[24]));
if (num_frames_ <= 0) {
RTC_LOG(LS_ERROR) << "Invalid IVF header: number of frames 0 or negative";
return false;
}
num_read_frames_ = 0;
next_frame_header_ = ReadNextFrameHeader();
if (!next_frame_header_) {
RTC_LOG(LS_ERROR) << "Failed to read 1st frame header";
return false;
}
// Initialization succeed: reset error.
has_error_ = false;
const char* codec_name = CodecTypeToPayloadString(codec_type_);
RTC_LOG(LS_INFO) << "Opened IVF file with codec data of type " << codec_name
<< " at resolution " << width_ << " x " << height_
<< ", using " << time_scale_ << "Hz clock resolution.";
return true;
}
std::optional<EncodedImage> IvfFileReader::NextFrame() {
if (has_error_ || !HasMoreFrames()) {
return std::nullopt;
}
scoped_refptr<EncodedImageBuffer> payload = EncodedImageBuffer::Create();
std::vector<size_t> layer_sizes;
// next_frame_header_ have to be presented by the way how it was loaded. If it
// is missing it means there is a bug in error handling.
RTC_DCHECK(next_frame_header_);
int64_t current_timestamp = next_frame_header_->timestamp;
// The first frame from the file should be marked as Key frame.
bool is_first_frame = num_read_frames_ == 0;
while (next_frame_header_ &&
current_timestamp == next_frame_header_->timestamp) {
// Resize payload to fit next spatial layer.
size_t current_layer_size = next_frame_header_->frame_size;
size_t current_layer_start_pos = payload->size();
payload->Realloc(payload->size() + current_layer_size);
layer_sizes.push_back(current_layer_size);
// Read next layer into payload
size_t read = file_.Read(&payload->data()[current_layer_start_pos],
current_layer_size);
if (read != current_layer_size) {
RTC_LOG(LS_ERROR) << "Frame #" << num_read_frames_
<< ": failed to read frame payload";
has_error_ = true;
return std::nullopt;
}
num_read_frames_++;
current_timestamp = next_frame_header_->timestamp;
next_frame_header_ = ReadNextFrameHeader();
}
if (!next_frame_header_) {
// If EOF was reached, we need to check that all frames were met.
if (!has_error_ && num_read_frames_ != num_frames_) {
RTC_LOG(LS_ERROR) << "Unexpected EOF";
has_error_ = true;
return std::nullopt;
}
}
EncodedImage image;
image.capture_time_ms_ = current_timestamp;
image.SetRtpTimestamp(
static_cast<uint32_t>(current_timestamp * kRtpClockRateHz / time_scale_));
image.SetEncodedData(payload);
image.SetSpatialIndex(static_cast<int>(layer_sizes.size()) - 1);
for (size_t i = 0; i < layer_sizes.size(); ++i) {
image.SetSpatialLayerFrameSize(static_cast<int>(i), layer_sizes[i]);
}
if (is_first_frame) {
image._frameType = VideoFrameType::kVideoFrameKey;
}
return image;
}
bool IvfFileReader::Close() {
if (!file_.is_open())
return false;
file_.Close();
return true;
}
std::optional<VideoCodecType> IvfFileReader::ParseCodecType(uint8_t* buffer,
size_t start_pos) {
if (memcmp(&buffer[start_pos], kVp8Header, kCodecTypeBytesCount) == 0) {
return VideoCodecType::kVideoCodecVP8;
}
if (memcmp(&buffer[start_pos], kVp9Header, kCodecTypeBytesCount) == 0) {
return VideoCodecType::kVideoCodecVP9;
}
if (memcmp(&buffer[start_pos], kAv1Header, kCodecTypeBytesCount) == 0) {
return VideoCodecType::kVideoCodecAV1;
}
if (memcmp(&buffer[start_pos], kH264Header, kCodecTypeBytesCount) == 0) {
return VideoCodecType::kVideoCodecH264;
}
if (memcmp(&buffer[start_pos], kH265Header, kCodecTypeBytesCount) == 0) {
return VideoCodecType::kVideoCodecH265;
}
has_error_ = true;
RTC_LOG(LS_ERROR) << "Unknown codec type: "
<< std::string(
reinterpret_cast<char const*>(&buffer[start_pos]),
kCodecTypeBytesCount);
return std::nullopt;
}
std::optional<IvfFileReader::FrameHeader> IvfFileReader::ReadNextFrameHeader() {
uint8_t ivf_frame_header[kIvfFrameHeaderSize] = {0};
size_t read = file_.Read(&ivf_frame_header, kIvfFrameHeaderSize);
if (read != kIvfFrameHeaderSize) {
if (read != 0 || !file_.ReadEof()) {
has_error_ = true;
RTC_LOG(LS_ERROR) << "Frame #" << num_read_frames_
<< ": failed to read IVF frame header";
}
return std::nullopt;
}
FrameHeader header;
header.frame_size = static_cast<size_t>(
ByteReader<uint32_t>::ReadLittleEndian(&ivf_frame_header[0]));
header.timestamp =
ByteReader<uint64_t>::ReadLittleEndian(&ivf_frame_header[4]);
if (header.frame_size == 0) {
has_error_ = true;
RTC_LOG(LS_ERROR) << "Frame #" << num_read_frames_
<< ": invalid frame size";
return std::nullopt;
}
if (header.timestamp < 0) {
has_error_ = true;
RTC_LOG(LS_ERROR) << "Frame #" << num_read_frames_
<< ": negative timestamp";
return std::nullopt;
}
return header;
}
} // namespace webrtc
|