blob: 496da894a15ced550dc4b470b447eaa4d5a6b2c7 [file] [log] [blame]
sprang3911c262016-04-15 01:24:14 -07001/*
2 * Copyright (c) 2016 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/utility/ivf_file_writer.h"
sprang3911c262016-04-15 01:24:14 -070012
palmkviste75f2042016-09-28 06:19:48 -070013#include <utility>
14
Mirko Bonadei04255172018-09-10 16:48:02 +020015#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/rtp_rtcp/source/byte_io.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
sprang3911c262016-04-15 01:24:14 -070019
palmkviste75f2042016-09-28 06:19:48 -070020// TODO(palmkvist): make logging more informative in the absence of a file name
21// (or get one)
22
sprang3911c262016-04-15 01:24:14 -070023namespace webrtc {
24
palmkviste75f2042016-09-28 06:19:48 -070025const size_t kIvfHeaderSize = 32;
26
Niels Möllerb7edf692019-02-08 16:40:53 +010027IvfFileWriter::IvfFileWriter(FileWrapper file, size_t byte_limit)
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +020028 : codec_type_(kVideoCodecGeneric),
palmkviste75f2042016-09-28 06:19:48 -070029 bytes_written_(0),
30 byte_limit_(byte_limit),
sprang3911c262016-04-15 01:24:14 -070031 num_frames_(0),
32 width_(0),
33 height_(0),
34 last_timestamp_(-1),
35 using_capture_timestamps_(false),
palmkviste75f2042016-09-28 06:19:48 -070036 file_(std::move(file)) {
37 RTC_DCHECK(byte_limit == 0 || kIvfHeaderSize <= byte_limit)
38 << "The byte_limit is too low, not even the header will fit.";
39}
sprang3911c262016-04-15 01:24:14 -070040
41IvfFileWriter::~IvfFileWriter() {
42 Close();
43}
44
Niels Möllerb7edf692019-02-08 16:40:53 +010045std::unique_ptr<IvfFileWriter> IvfFileWriter::Wrap(FileWrapper file,
palmkviste75f2042016-09-28 06:19:48 -070046 size_t byte_limit) {
47 return std::unique_ptr<IvfFileWriter>(
48 new IvfFileWriter(std::move(file), byte_limit));
sprang3911c262016-04-15 01:24:14 -070049}
50
51bool IvfFileWriter::WriteHeader() {
Niels Möllerb7edf692019-02-08 16:40:53 +010052 if (!file_.Rewind()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010053 RTC_LOG(LS_WARNING) << "Unable to rewind ivf output file.";
sprang3911c262016-04-15 01:24:14 -070054 return false;
55 }
56
57 uint8_t ivf_header[kIvfHeaderSize] = {0};
58 ivf_header[0] = 'D';
59 ivf_header[1] = 'K';
60 ivf_header[2] = 'I';
61 ivf_header[3] = 'F';
62 ByteWriter<uint16_t>::WriteLittleEndian(&ivf_header[4], 0); // Version.
63 ByteWriter<uint16_t>::WriteLittleEndian(&ivf_header[6], 32); // Header size.
64
65 switch (codec_type_) {
kjellander02b3d272016-04-20 05:05:54 -070066 case kVideoCodecVP8:
sprang3911c262016-04-15 01:24:14 -070067 ivf_header[8] = 'V';
68 ivf_header[9] = 'P';
69 ivf_header[10] = '8';
70 ivf_header[11] = '0';
71 break;
kjellander02b3d272016-04-20 05:05:54 -070072 case kVideoCodecVP9:
sprang3911c262016-04-15 01:24:14 -070073 ivf_header[8] = 'V';
74 ivf_header[9] = 'P';
75 ivf_header[10] = '9';
76 ivf_header[11] = '0';
77 break;
Emil Lundmark91c04772020-08-25 11:43:25 +020078 case kVideoCodecAV1:
79 ivf_header[8] = 'A';
80 ivf_header[9] = 'V';
81 ivf_header[10] = '0';
82 ivf_header[11] = '1';
83 break;
kjellander02b3d272016-04-20 05:05:54 -070084 case kVideoCodecH264:
sprang3911c262016-04-15 01:24:14 -070085 ivf_header[8] = 'H';
86 ivf_header[9] = '2';
87 ivf_header[10] = '6';
88 ivf_header[11] = '4';
89 break;
90 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +010091 RTC_LOG(LS_ERROR) << "Unknown CODEC type: " << codec_type_;
sprang3911c262016-04-15 01:24:14 -070092 return false;
93 }
94
95 ByteWriter<uint16_t>::WriteLittleEndian(&ivf_header[12], width_);
96 ByteWriter<uint16_t>::WriteLittleEndian(&ivf_header[14], height_);
97 // Render timestamps are in ms (1/1000 scale), while RTP timestamps use a
98 // 90kHz clock.
99 ByteWriter<uint32_t>::WriteLittleEndian(
100 &ivf_header[16], using_capture_timestamps_ ? 1000 : 90000);
101 ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[20], 1);
102 ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[24],
103 static_cast<uint32_t>(num_frames_));
104 ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[28], 0); // Reserved.
105
Niels Möllerb7edf692019-02-08 16:40:53 +0100106 if (!file_.Write(ivf_header, kIvfHeaderSize)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100107 RTC_LOG(LS_ERROR) << "Unable to write IVF header for ivf output file.";
sprang3911c262016-04-15 01:24:14 -0700108 return false;
109 }
110
palmkviste75f2042016-09-28 06:19:48 -0700111 if (bytes_written_ < kIvfHeaderSize) {
112 bytes_written_ = kIvfHeaderSize;
113 }
114
sprang3911c262016-04-15 01:24:14 -0700115 return true;
116}
117
palmkviste75f2042016-09-28 06:19:48 -0700118bool IvfFileWriter::InitFromFirstFrame(const EncodedImage& encoded_image,
119 VideoCodecType codec_type) {
sprang3911c262016-04-15 01:24:14 -0700120 width_ = encoded_image._encodedWidth;
121 height_ = encoded_image._encodedHeight;
122 RTC_CHECK_GT(width_, 0);
123 RTC_CHECK_GT(height_, 0);
Niels Möller23775882018-08-16 10:24:12 +0200124 using_capture_timestamps_ = encoded_image.Timestamp() == 0;
sprang3911c262016-04-15 01:24:14 -0700125
palmkviste75f2042016-09-28 06:19:48 -0700126 codec_type_ = codec_type;
127
sprang3911c262016-04-15 01:24:14 -0700128 if (!WriteHeader())
129 return false;
130
Yves Gerey665174f2018-06-19 15:03:05 +0200131 const char* codec_name = CodecTypeToPayloadString(codec_type_);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100132 RTC_LOG(LS_WARNING) << "Created IVF file for codec data of type "
133 << codec_name << " at resolution " << width_ << " x "
134 << height_ << ", using "
135 << (using_capture_timestamps_ ? "1" : "90")
136 << "kHz clock resolution.";
sprang3911c262016-04-15 01:24:14 -0700137 return true;
138}
139
palmkviste75f2042016-09-28 06:19:48 -0700140bool IvfFileWriter::WriteFrame(const EncodedImage& encoded_image,
141 VideoCodecType codec_type) {
Niels Möllerb7edf692019-02-08 16:40:53 +0100142 if (!file_.is_open())
sprang3911c262016-04-15 01:24:14 -0700143 return false;
144
palmkviste75f2042016-09-28 06:19:48 -0700145 if (num_frames_ == 0 && !InitFromFirstFrame(encoded_image, codec_type))
146 return false;
147 RTC_DCHECK_EQ(codec_type_, codec_type);
148
sprang3911c262016-04-15 01:24:14 -0700149 if ((encoded_image._encodedWidth > 0 || encoded_image._encodedHeight > 0) &&
150 (encoded_image._encodedHeight != height_ ||
151 encoded_image._encodedWidth != width_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(LS_WARNING)
Elad Alon4cde9ad2019-03-04 17:38:05 +0100153 << "Incoming frame has resolution different from previous: (" << width_
sprang3911c262016-04-15 01:24:14 -0700154 << "x" << height_ << ") -> (" << encoded_image._encodedWidth << "x"
155 << encoded_image._encodedHeight << ")";
156 }
157
158 int64_t timestamp = using_capture_timestamps_
159 ? encoded_image.capture_time_ms_
Niels Möller23775882018-08-16 10:24:12 +0200160 : wrap_handler_.Unwrap(encoded_image.Timestamp());
sprang3911c262016-04-15 01:24:14 -0700161 if (last_timestamp_ != -1 && timestamp <= last_timestamp_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100162 RTC_LOG(LS_WARNING) << "Timestamp no increasing: " << last_timestamp_
163 << " -> " << timestamp;
sprang3911c262016-04-15 01:24:14 -0700164 }
165 last_timestamp_ = timestamp;
166
Ilya Nikolaevskiye93b1fe2019-10-08 13:17:09 +0200167 bool written_frames = false;
168 size_t max_sl_index = encoded_image.SpatialIndex().value_or(0);
169 const uint8_t* data = encoded_image.data();
170 for (size_t sl_idx = 0; sl_idx <= max_sl_index; ++sl_idx) {
171 size_t cur_size = encoded_image.SpatialLayerFrameSize(sl_idx).value_or(0);
172 if (cur_size > 0) {
173 written_frames = true;
174 if (!WriteOneSpatialLayer(timestamp, data, cur_size)) {
175 return false;
176 }
177 data += cur_size;
178 }
179 }
180
181 // If frame has only one spatial layer it won't have any spatial layers'
182 // sizes. Therefore this case should be addressed separately.
183 if (!written_frames) {
184 return WriteOneSpatialLayer(timestamp, data, encoded_image.size());
185 } else {
186 return true;
187 }
188}
189
190bool IvfFileWriter::WriteOneSpatialLayer(int64_t timestamp,
191 const uint8_t* data,
192 size_t size) {
sprang3911c262016-04-15 01:24:14 -0700193 const size_t kFrameHeaderSize = 12;
palmkviste75f2042016-09-28 06:19:48 -0700194 if (byte_limit_ != 0 &&
Ilya Nikolaevskiye93b1fe2019-10-08 13:17:09 +0200195 bytes_written_ + kFrameHeaderSize + size > byte_limit_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100196 RTC_LOG(LS_WARNING) << "Closing IVF file due to reaching size limit: "
197 << byte_limit_ << " bytes.";
palmkviste75f2042016-09-28 06:19:48 -0700198 Close();
199 return false;
200 }
sprang3911c262016-04-15 01:24:14 -0700201 uint8_t frame_header[kFrameHeaderSize] = {};
Ilya Nikolaevskiye93b1fe2019-10-08 13:17:09 +0200202 ByteWriter<uint32_t>::WriteLittleEndian(&frame_header[0],
203 static_cast<uint32_t>(size));
sprang3911c262016-04-15 01:24:14 -0700204 ByteWriter<uint64_t>::WriteLittleEndian(&frame_header[4], timestamp);
Niels Möllerb7edf692019-02-08 16:40:53 +0100205 if (!file_.Write(frame_header, kFrameHeaderSize) ||
Ilya Nikolaevskiye93b1fe2019-10-08 13:17:09 +0200206 !file_.Write(data, size)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100207 RTC_LOG(LS_ERROR) << "Unable to write frame to file.";
sprang3911c262016-04-15 01:24:14 -0700208 return false;
209 }
210
Ilya Nikolaevskiye93b1fe2019-10-08 13:17:09 +0200211 bytes_written_ += kFrameHeaderSize + size;
palmkviste75f2042016-09-28 06:19:48 -0700212
sprang3911c262016-04-15 01:24:14 -0700213 ++num_frames_;
214 return true;
215}
216
217bool IvfFileWriter::Close() {
Niels Möllerb7edf692019-02-08 16:40:53 +0100218 if (!file_.is_open())
sprang3911c262016-04-15 01:24:14 -0700219 return false;
220
221 if (num_frames_ == 0) {
palmkviste75f2042016-09-28 06:19:48 -0700222 file_.Close();
sprang3911c262016-04-15 01:24:14 -0700223 return true;
224 }
225
tommia6219cc2016-06-15 10:30:14 -0700226 bool ret = WriteHeader();
palmkviste75f2042016-09-28 06:19:48 -0700227 file_.Close();
tommia6219cc2016-06-15 10:30:14 -0700228 return ret;
sprang3911c262016-04-15 01:24:14 -0700229}
230
231} // namespace webrtc