blob: c49cde67d1acc7ce43a3ab939d35efbce893bf7e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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/frame_buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <string.h>
15
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "api/video/encoded_image.h"
17#include "api/video/video_timing.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "modules/video_coding/include/video_codec_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/packet.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
22#include "rtc_base/trace_event.h"
agalusza@google.comd818dcb2013-07-29 21:48:11 +000023
niklase@google.com470e71d2011-07-07 08:21:25 +000024namespace webrtc {
25
stefan@webrtc.orgc3d89102011-09-08 06:50:28 +000026VCMFrameBuffer::VCMFrameBuffer()
philipel9d3ab612015-12-21 04:12:39 -080027 : _state(kStateEmpty), _nackCount(0), _latestPacketTimeMs(-1) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000028
philipel9d3ab612015-12-21 04:12:39 -080029VCMFrameBuffer::~VCMFrameBuffer() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000030
Niels Möller87e2d782019-03-07 10:18:23 +010031webrtc::VideoFrameType VCMFrameBuffer::FrameType() const {
philipel9d3ab612015-12-21 04:12:39 -080032 return _sessionInfo.FrameType();
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
philipel9d3ab612015-12-21 04:12:39 -080035int32_t VCMFrameBuffer::GetLowSeqNum() const {
36 return _sessionInfo.LowSequenceNumber();
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
philipel9d3ab612015-12-21 04:12:39 -080039int32_t VCMFrameBuffer::GetHighSeqNum() const {
40 return _sessionInfo.HighSequenceNumber();
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
stefan@webrtc.orgffd28f92011-10-19 15:55:39 +000043int VCMFrameBuffer::PictureId() const {
44 return _sessionInfo.PictureId();
45}
46
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:47 +000047int VCMFrameBuffer::TemporalId() const {
48 return _sessionInfo.TemporalId();
49}
50
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:06 +000051bool VCMFrameBuffer::LayerSync() const {
52 return _sessionInfo.LayerSync();
53}
54
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:47 +000055int VCMFrameBuffer::Tl0PicId() const {
56 return _sessionInfo.Tl0PicId();
57}
58
stefana669a3a2016-10-06 05:04:52 -070059std::vector<NaluInfo> VCMFrameBuffer::GetNaluInfos() const {
60 return _sessionInfo.GetNaluInfos();
61}
62
asapersson9a4cd872015-10-23 00:27:14 -070063void VCMFrameBuffer::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) {
tommidb23ea62017-03-03 07:21:18 -080064 TRACE_EVENT0("webrtc", "VCMFrameBuffer::SetGofInfo");
asapersson9a4cd872015-10-23 00:27:14 -070065 _sessionInfo.SetGofInfo(gof_info, idx);
66 // TODO(asapersson): Consider adding hdr->VP9.ref_picture_id for testing.
67 _codecSpecificInfo.codecSpecific.VP9.temporal_idx =
68 gof_info.temporal_idx[idx];
69 _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch =
70 gof_info.temporal_up_switch[idx];
71}
72
philipel9d3ab612015-12-21 04:12:39 -080073bool VCMFrameBuffer::IsSessionComplete() const {
tommidb23ea62017-03-03 07:21:18 -080074 TRACE_EVENT0("webrtc", "VCMFrameBuffer::IsSessionComplete");
philipel9d3ab612015-12-21 04:12:39 -080075 return _sessionInfo.complete();
niklase@google.com470e71d2011-07-07 08:21:25 +000076}
77
78// Insert packet
Jonas Olssona4d87372019-07-05 19:08:33 +020079VCMFrameBufferEnum VCMFrameBuffer::InsertPacket(const VCMPacket& packet,
80 int64_t timeInMs,
81 const FrameData& frame_data) {
tommidb23ea62017-03-03 07:21:18 -080082 TRACE_EVENT0("webrtc", "VCMFrameBuffer::InsertPacket");
philipel9d3ab612015-12-21 04:12:39 -080083 assert(!(NULL == packet.dataPtr && packet.sizeBytes > 0));
84 if (packet.dataPtr != NULL) {
85 _payloadType = packet.payloadType;
86 }
87
88 if (kStateEmpty == _state) {
89 // First packet (empty and/or media) inserted into this frame.
90 // store some info and set some initial values.
Niels Möller23775882018-08-16 10:24:12 +020091 SetTimestamp(packet.timestamp);
philipel9d3ab612015-12-21 04:12:39 -080092 // We only take the ntp timestamp of the first packet of a frame.
93 ntp_time_ms_ = packet.ntp_time_ms_;
Niels Möllerd5e02f02019-02-20 13:12:21 +010094 _codec = packet.codec();
Niels Möllerabbc50e2019-04-24 09:41:16 +020095 if (packet.video_header.frame_type != VideoFrameType::kEmptyFrame) {
philipel9d3ab612015-12-21 04:12:39 -080096 // first media packet
97 SetState(kStateIncomplete);
niklase@google.com470e71d2011-07-07 08:21:25 +000098 }
philipel9d3ab612015-12-21 04:12:39 -080099 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
philipel9d3ab612015-12-21 04:12:39 -0800101 uint32_t requiredSizeBytes =
Niels Möllerf0eee002018-11-28 16:31:29 +0100102 size() + packet.sizeBytes +
Niels Möller009ab3c2019-03-08 11:26:58 +0100103 (packet.insertStartCode ? kH264StartCodeLengthBytes : 0);
Niels Möller48a79462018-12-07 16:21:18 +0100104 if (requiredSizeBytes >= capacity()) {
Niels Möller24871e42019-01-17 11:31:13 +0100105 const uint8_t* prevBuffer = data();
philipel9d3ab612015-12-21 04:12:39 -0800106 const uint32_t increments =
107 requiredSizeBytes / kBufferIncStepSizeBytes +
108 (requiredSizeBytes % kBufferIncStepSizeBytes > 0);
Niels Möller48a79462018-12-07 16:21:18 +0100109 const uint32_t newSize = capacity() + increments * kBufferIncStepSizeBytes;
philipel9d3ab612015-12-21 04:12:39 -0800110 if (newSize > kMaxJBFrameSizeBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100111 RTC_LOG(LS_ERROR) << "Failed to insert packet due to frame being too "
112 "big.";
philipel9d3ab612015-12-21 04:12:39 -0800113 return kSizeError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114 }
philipel9d3ab612015-12-21 04:12:39 -0800115 VerifyAndAllocate(newSize);
Niels Möller24871e42019-01-17 11:31:13 +0100116 _sessionInfo.UpdateDataPointers(prevBuffer, data());
philipel9d3ab612015-12-21 04:12:39 -0800117 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
Niels Möllerd5e02f02019-02-20 13:12:21 +0100119 if (packet.width() > 0 && packet.height() > 0) {
120 _encodedWidth = packet.width();
121 _encodedHeight = packet.height();
philipel9d3ab612015-12-21 04:12:39 -0800122 }
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000123
philipel9d3ab612015-12-21 04:12:39 -0800124 // Don't copy payload specific data for empty packets (e.g padding packets).
125 if (packet.sizeBytes > 0)
isheriff6b4b5f32016-06-08 00:24:21 -0700126 CopyCodecSpecific(&packet.video_header);
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000127
Niels Möller24871e42019-01-17 11:31:13 +0100128 int retVal = _sessionInfo.InsertPacket(packet, data(), frame_data);
philipel9d3ab612015-12-21 04:12:39 -0800129 if (retVal == -1) {
130 return kSizeError;
131 } else if (retVal == -2) {
132 return kDuplicatePacket;
133 } else if (retVal == -3) {
134 return kOutOfBoundsPacket;
135 }
Niels Möller77536a22019-01-15 08:50:01 +0100136 // update size
137 set_size(size() + static_cast<uint32_t>(retVal));
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000138
philipel9d3ab612015-12-21 04:12:39 -0800139 _latestPacketTimeMs = timeInMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
philipel9d3ab612015-12-21 04:12:39 -0800141 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
142 // ts_126114v120700p.pdf Section 7.4.5.
143 // The MTSI client shall add the payload bytes as defined in this clause
144 // onto the last RTP packet in each group of packets which make up a key
145 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
146 // (HEVC)).
147 if (packet.markerBit) {
perkj414dda12016-07-04 01:45:23 -0700148 rotation_ = packet.video_header.rotation;
ilnik00d802b2017-04-11 10:34:31 -0700149 content_type_ = packet.video_header.content_type;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200150 if (packet.video_header.video_timing.flags != VideoSendTiming::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -0700151 timing_.encode_start_ms =
152 ntp_time_ms_ + packet.video_header.video_timing.encode_start_delta_ms;
153 timing_.encode_finish_ms =
154 ntp_time_ms_ +
155 packet.video_header.video_timing.encode_finish_delta_ms;
156 timing_.packetization_finish_ms =
157 ntp_time_ms_ +
158 packet.video_header.video_timing.packetization_finish_delta_ms;
159 timing_.pacer_exit_ms =
160 ntp_time_ms_ + packet.video_header.video_timing.pacer_exit_delta_ms;
161 timing_.network_timestamp_ms =
162 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100163 packet.video_header.video_timing.network_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700164 timing_.network2_timestamp_ms =
165 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100166 packet.video_header.video_timing.network2_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700167 }
sprangba050a62017-08-18 02:51:12 -0700168 timing_.flags = packet.video_header.video_timing.flags;
philipel9d3ab612015-12-21 04:12:39 -0800169 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
Niels Möllerd5e02f02019-02-20 13:12:21 +0100171 if (packet.is_first_packet_in_frame()) {
isheriff6b4b5f32016-06-08 00:24:21 -0700172 playout_delay_ = packet.video_header.playout_delay;
173 }
174
philipel9d3ab612015-12-21 04:12:39 -0800175 if (_sessionInfo.complete()) {
176 SetState(kStateComplete);
177 return kCompleteSession;
philipel9d3ab612015-12-21 04:12:39 -0800178 }
179 return kIncomplete;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180}
181
philipel9d3ab612015-12-21 04:12:39 -0800182int64_t VCMFrameBuffer::LatestPacketTimeMs() const {
tommidb23ea62017-03-03 07:21:18 -0800183 TRACE_EVENT0("webrtc", "VCMFrameBuffer::LatestPacketTimeMs");
philipel9d3ab612015-12-21 04:12:39 -0800184 return _latestPacketTimeMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
philipel9d3ab612015-12-21 04:12:39 -0800187void VCMFrameBuffer::IncrementNackCount() {
tommidb23ea62017-03-03 07:21:18 -0800188 TRACE_EVENT0("webrtc", "VCMFrameBuffer::IncrementNackCount");
philipel9d3ab612015-12-21 04:12:39 -0800189 _nackCount++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
philipel9d3ab612015-12-21 04:12:39 -0800192int16_t VCMFrameBuffer::GetNackCount() const {
tommidb23ea62017-03-03 07:21:18 -0800193 TRACE_EVENT0("webrtc", "VCMFrameBuffer::GetNackCount");
philipel9d3ab612015-12-21 04:12:39 -0800194 return _nackCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
philipel9d3ab612015-12-21 04:12:39 -0800197bool VCMFrameBuffer::HaveFirstPacket() const {
tommidb23ea62017-03-03 07:21:18 -0800198 TRACE_EVENT0("webrtc", "VCMFrameBuffer::HaveFirstPacket");
philipel9d3ab612015-12-21 04:12:39 -0800199 return _sessionInfo.HaveFirstPacket();
stefan@webrtc.org885cd132013-04-16 09:38:26 +0000200}
201
philipel9d3ab612015-12-21 04:12:39 -0800202int VCMFrameBuffer::NumPackets() const {
tommidb23ea62017-03-03 07:21:18 -0800203 TRACE_EVENT0("webrtc", "VCMFrameBuffer::NumPackets");
philipel9d3ab612015-12-21 04:12:39 -0800204 return _sessionInfo.NumPackets();
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000205}
206
philipel9d3ab612015-12-21 04:12:39 -0800207void VCMFrameBuffer::Reset() {
tommidb23ea62017-03-03 07:21:18 -0800208 TRACE_EVENT0("webrtc", "VCMFrameBuffer::Reset");
Niels Möller77536a22019-01-15 08:50:01 +0100209 set_size(0);
philipel9d3ab612015-12-21 04:12:39 -0800210 _sessionInfo.Reset();
211 _payloadType = 0;
212 _nackCount = 0;
213 _latestPacketTimeMs = -1;
214 _state = kStateEmpty;
215 VCMEncodedFrame::Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000216}
217
niklase@google.com470e71d2011-07-07 08:21:25 +0000218// Set state of frame
philipel9d3ab612015-12-21 04:12:39 -0800219void VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) {
tommidb23ea62017-03-03 07:21:18 -0800220 TRACE_EVENT0("webrtc", "VCMFrameBuffer::SetState");
philipel9d3ab612015-12-21 04:12:39 -0800221 if (_state == state) {
222 return;
223 }
224 switch (state) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 case kStateIncomplete:
philipel9d3ab612015-12-21 04:12:39 -0800226 // we can go to this state from state kStateEmpty
227 assert(_state == kStateEmpty);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
philipel9d3ab612015-12-21 04:12:39 -0800229 // Do nothing, we received a packet
230 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
232 case kStateComplete:
Niels Möller375b3462019-01-10 15:35:56 +0100233 assert(_state == kStateEmpty || _state == kStateIncomplete);
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
philipel9d3ab612015-12-21 04:12:39 -0800235 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
237 case kStateEmpty:
philipel9d3ab612015-12-21 04:12:39 -0800238 // Should only be set to empty through Reset().
239 assert(false);
240 break;
philipel9d3ab612015-12-21 04:12:39 -0800241 }
242 _state = state;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
niklase@google.com470e71d2011-07-07 08:21:25 +0000245// Get current state of frame
philipel9d3ab612015-12-21 04:12:39 -0800246VCMFrameBufferStateEnum VCMFrameBuffer::GetState() const {
247 return _state;
niklase@google.com470e71d2011-07-07 08:21:25 +0000248}
249
philipel9d3ab612015-12-21 04:12:39 -0800250void VCMFrameBuffer::PrepareForDecode(bool continuous) {
tommidb23ea62017-03-03 07:21:18 -0800251 TRACE_EVENT0("webrtc", "VCMFrameBuffer::PrepareForDecode");
philipel9d3ab612015-12-21 04:12:39 -0800252 size_t bytes_removed = _sessionInfo.MakeDecodable();
Niels Möller77536a22019-01-15 08:50:01 +0100253 set_size(size() - bytes_removed);
philipel9d3ab612015-12-21 04:12:39 -0800254 // Transfer frame information to EncodedFrame and create any codec
255 // specific information.
256 _frameType = _sessionInfo.FrameType();
257 _completeFrame = _sessionInfo.complete();
258 _missingFrame = !continuous;
niklase@google.com470e71d2011-07-07 08:21:25 +0000259}
260
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000261} // namespace webrtc