blob: 99abf4363f26509989857615d1401c4523adcfd6 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/video_coding/packet.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
19#include "rtc_base/trace_event.h"
agalusza@google.comd818dcb2013-07-29 21:48:11 +000020
niklase@google.com470e71d2011-07-07 08:21:25 +000021namespace webrtc {
22
stefan@webrtc.orgc3d89102011-09-08 06:50:28 +000023VCMFrameBuffer::VCMFrameBuffer()
philipel9d3ab612015-12-21 04:12:39 -080024 : _state(kStateEmpty), _nackCount(0), _latestPacketTimeMs(-1) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000025
philipel9d3ab612015-12-21 04:12:39 -080026VCMFrameBuffer::~VCMFrameBuffer() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000027
agalusza@google.comd818dcb2013-07-29 21:48:11 +000028VCMFrameBuffer::VCMFrameBuffer(const VCMFrameBuffer& rhs)
philipel9d3ab612015-12-21 04:12:39 -080029 : VCMEncodedFrame(rhs),
30 _state(rhs._state),
31 _sessionInfo(),
32 _nackCount(rhs._nackCount),
33 _latestPacketTimeMs(rhs._latestPacketTimeMs) {
34 _sessionInfo = rhs._sessionInfo;
35 _sessionInfo.UpdateDataPointers(rhs._buffer, _buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
philipel9d3ab612015-12-21 04:12:39 -080038webrtc::FrameType VCMFrameBuffer::FrameType() const {
39 return _sessionInfo.FrameType();
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
philipel9d3ab612015-12-21 04:12:39 -080042int32_t VCMFrameBuffer::GetLowSeqNum() const {
43 return _sessionInfo.LowSequenceNumber();
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
philipel9d3ab612015-12-21 04:12:39 -080046int32_t VCMFrameBuffer::GetHighSeqNum() const {
47 return _sessionInfo.HighSequenceNumber();
niklase@google.com470e71d2011-07-07 08:21:25 +000048}
49
stefan@webrtc.orgffd28f92011-10-19 15:55:39 +000050int VCMFrameBuffer::PictureId() const {
51 return _sessionInfo.PictureId();
52}
53
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:47 +000054int VCMFrameBuffer::TemporalId() const {
55 return _sessionInfo.TemporalId();
56}
57
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:06 +000058bool VCMFrameBuffer::LayerSync() const {
59 return _sessionInfo.LayerSync();
60}
61
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:47 +000062int VCMFrameBuffer::Tl0PicId() const {
63 return _sessionInfo.Tl0PicId();
64}
65
stefana669a3a2016-10-06 05:04:52 -070066std::vector<NaluInfo> VCMFrameBuffer::GetNaluInfos() const {
67 return _sessionInfo.GetNaluInfos();
68}
69
asapersson9a4cd872015-10-23 00:27:14 -070070void VCMFrameBuffer::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) {
tommidb23ea62017-03-03 07:21:18 -080071 TRACE_EVENT0("webrtc", "VCMFrameBuffer::SetGofInfo");
asapersson9a4cd872015-10-23 00:27:14 -070072 _sessionInfo.SetGofInfo(gof_info, idx);
73 // TODO(asapersson): Consider adding hdr->VP9.ref_picture_id for testing.
74 _codecSpecificInfo.codecSpecific.VP9.temporal_idx =
75 gof_info.temporal_idx[idx];
76 _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch =
77 gof_info.temporal_up_switch[idx];
78}
79
philipel9d3ab612015-12-21 04:12:39 -080080bool VCMFrameBuffer::IsSessionComplete() const {
tommidb23ea62017-03-03 07:21:18 -080081 TRACE_EVENT0("webrtc", "VCMFrameBuffer::IsSessionComplete");
philipel9d3ab612015-12-21 04:12:39 -080082 return _sessionInfo.complete();
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
85// Insert packet
philipel9d3ab612015-12-21 04:12:39 -080086VCMFrameBufferEnum VCMFrameBuffer::InsertPacket(
87 const VCMPacket& packet,
88 int64_t timeInMs,
89 VCMDecodeErrorMode decode_error_mode,
90 const FrameData& frame_data) {
tommidb23ea62017-03-03 07:21:18 -080091 TRACE_EVENT0("webrtc", "VCMFrameBuffer::InsertPacket");
philipel9d3ab612015-12-21 04:12:39 -080092 assert(!(NULL == packet.dataPtr && packet.sizeBytes > 0));
93 if (packet.dataPtr != NULL) {
94 _payloadType = packet.payloadType;
95 }
96
97 if (kStateEmpty == _state) {
98 // First packet (empty and/or media) inserted into this frame.
99 // store some info and set some initial values.
100 _timeStamp = packet.timestamp;
101 // We only take the ntp timestamp of the first packet of a frame.
102 ntp_time_ms_ = packet.ntp_time_ms_;
103 _codec = packet.codec;
104 if (packet.frameType != kEmptyFrame) {
105 // first media packet
106 SetState(kStateIncomplete);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 }
philipel9d3ab612015-12-21 04:12:39 -0800108 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
philipel9d3ab612015-12-21 04:12:39 -0800110 uint32_t requiredSizeBytes =
111 Length() + packet.sizeBytes +
hbosd6648362016-01-21 05:43:11 -0800112 (packet.insertStartCode ? kH264StartCodeLengthBytes : 0) +
113 EncodedImage::GetBufferPaddingBytes(packet.codec);
philipel9d3ab612015-12-21 04:12:39 -0800114 if (requiredSizeBytes >= _size) {
115 const uint8_t* prevBuffer = _buffer;
116 const uint32_t increments =
117 requiredSizeBytes / kBufferIncStepSizeBytes +
118 (requiredSizeBytes % kBufferIncStepSizeBytes > 0);
119 const uint32_t newSize = _size + increments * kBufferIncStepSizeBytes;
120 if (newSize > kMaxJBFrameSizeBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100121 RTC_LOG(LS_ERROR) << "Failed to insert packet due to frame being too "
122 "big.";
philipel9d3ab612015-12-21 04:12:39 -0800123 return kSizeError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 }
philipel9d3ab612015-12-21 04:12:39 -0800125 VerifyAndAllocate(newSize);
126 _sessionInfo.UpdateDataPointers(prevBuffer, _buffer);
127 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
philipel9d3ab612015-12-21 04:12:39 -0800129 if (packet.width > 0 && packet.height > 0) {
130 _encodedWidth = packet.width;
131 _encodedHeight = packet.height;
132 }
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000133
philipel9d3ab612015-12-21 04:12:39 -0800134 // Don't copy payload specific data for empty packets (e.g padding packets).
135 if (packet.sizeBytes > 0)
isheriff6b4b5f32016-06-08 00:24:21 -0700136 CopyCodecSpecific(&packet.video_header);
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000137
philipel9d3ab612015-12-21 04:12:39 -0800138 int retVal =
139 _sessionInfo.InsertPacket(packet, _buffer, decode_error_mode, frame_data);
140 if (retVal == -1) {
141 return kSizeError;
142 } else if (retVal == -2) {
143 return kDuplicatePacket;
144 } else if (retVal == -3) {
145 return kOutOfBoundsPacket;
146 }
147 // update length
148 _length = Length() + static_cast<uint32_t>(retVal);
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000149
philipel9d3ab612015-12-21 04:12:39 -0800150 _latestPacketTimeMs = timeInMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
philipel9d3ab612015-12-21 04:12:39 -0800152 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
153 // ts_126114v120700p.pdf Section 7.4.5.
154 // The MTSI client shall add the payload bytes as defined in this clause
155 // onto the last RTP packet in each group of packets which make up a key
156 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
157 // (HEVC)).
158 if (packet.markerBit) {
159 RTC_DCHECK(!_rotation_set);
perkj414dda12016-07-04 01:45:23 -0700160 rotation_ = packet.video_header.rotation;
philipel9d3ab612015-12-21 04:12:39 -0800161 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -0700162 content_type_ = packet.video_header.content_type;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200163 if (packet.video_header.video_timing.flags != VideoSendTiming::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -0700164 timing_.encode_start_ms =
165 ntp_time_ms_ + packet.video_header.video_timing.encode_start_delta_ms;
166 timing_.encode_finish_ms =
167 ntp_time_ms_ +
168 packet.video_header.video_timing.encode_finish_delta_ms;
169 timing_.packetization_finish_ms =
170 ntp_time_ms_ +
171 packet.video_header.video_timing.packetization_finish_delta_ms;
172 timing_.pacer_exit_ms =
173 ntp_time_ms_ + packet.video_header.video_timing.pacer_exit_delta_ms;
174 timing_.network_timestamp_ms =
175 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100176 packet.video_header.video_timing.network_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700177 timing_.network2_timestamp_ms =
178 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100179 packet.video_header.video_timing.network2_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700180 }
sprangba050a62017-08-18 02:51:12 -0700181 timing_.flags = packet.video_header.video_timing.flags;
philipel9d3ab612015-12-21 04:12:39 -0800182 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
johan0d1b2b62017-01-10 04:21:35 -0800184 if (packet.is_first_packet_in_frame) {
isheriff6b4b5f32016-06-08 00:24:21 -0700185 playout_delay_ = packet.video_header.playout_delay;
186 }
187
philipel9d3ab612015-12-21 04:12:39 -0800188 if (_sessionInfo.complete()) {
189 SetState(kStateComplete);
190 return kCompleteSession;
191 } else if (_sessionInfo.decodable()) {
192 SetState(kStateDecodable);
193 return kDecodableSession;
194 }
195 return kIncomplete;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
philipel9d3ab612015-12-21 04:12:39 -0800198int64_t VCMFrameBuffer::LatestPacketTimeMs() const {
tommidb23ea62017-03-03 07:21:18 -0800199 TRACE_EVENT0("webrtc", "VCMFrameBuffer::LatestPacketTimeMs");
philipel9d3ab612015-12-21 04:12:39 -0800200 return _latestPacketTimeMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
202
philipel9d3ab612015-12-21 04:12:39 -0800203void VCMFrameBuffer::IncrementNackCount() {
tommidb23ea62017-03-03 07:21:18 -0800204 TRACE_EVENT0("webrtc", "VCMFrameBuffer::IncrementNackCount");
philipel9d3ab612015-12-21 04:12:39 -0800205 _nackCount++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000206}
207
philipel9d3ab612015-12-21 04:12:39 -0800208int16_t VCMFrameBuffer::GetNackCount() const {
tommidb23ea62017-03-03 07:21:18 -0800209 TRACE_EVENT0("webrtc", "VCMFrameBuffer::GetNackCount");
philipel9d3ab612015-12-21 04:12:39 -0800210 return _nackCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
philipel9d3ab612015-12-21 04:12:39 -0800213bool VCMFrameBuffer::HaveFirstPacket() const {
tommidb23ea62017-03-03 07:21:18 -0800214 TRACE_EVENT0("webrtc", "VCMFrameBuffer::HaveFirstPacket");
philipel9d3ab612015-12-21 04:12:39 -0800215 return _sessionInfo.HaveFirstPacket();
stefan@webrtc.org885cd132013-04-16 09:38:26 +0000216}
217
philipel9d3ab612015-12-21 04:12:39 -0800218int VCMFrameBuffer::NumPackets() const {
tommidb23ea62017-03-03 07:21:18 -0800219 TRACE_EVENT0("webrtc", "VCMFrameBuffer::NumPackets");
philipel9d3ab612015-12-21 04:12:39 -0800220 return _sessionInfo.NumPackets();
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000221}
222
philipel9d3ab612015-12-21 04:12:39 -0800223void VCMFrameBuffer::Reset() {
tommidb23ea62017-03-03 07:21:18 -0800224 TRACE_EVENT0("webrtc", "VCMFrameBuffer::Reset");
philipel9d3ab612015-12-21 04:12:39 -0800225 _length = 0;
226 _timeStamp = 0;
227 _sessionInfo.Reset();
228 _payloadType = 0;
229 _nackCount = 0;
230 _latestPacketTimeMs = -1;
231 _state = kStateEmpty;
232 VCMEncodedFrame::Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000233}
234
niklase@google.com470e71d2011-07-07 08:21:25 +0000235// Set state of frame
philipel9d3ab612015-12-21 04:12:39 -0800236void VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) {
tommidb23ea62017-03-03 07:21:18 -0800237 TRACE_EVENT0("webrtc", "VCMFrameBuffer::SetState");
philipel9d3ab612015-12-21 04:12:39 -0800238 if (_state == state) {
239 return;
240 }
241 switch (state) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 case kStateIncomplete:
philipel9d3ab612015-12-21 04:12:39 -0800243 // we can go to this state from state kStateEmpty
244 assert(_state == kStateEmpty);
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
philipel9d3ab612015-12-21 04:12:39 -0800246 // Do nothing, we received a packet
247 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000248
249 case kStateComplete:
philipel9d3ab612015-12-21 04:12:39 -0800250 assert(_state == kStateEmpty || _state == kStateIncomplete ||
251 _state == kStateDecodable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000252
philipel9d3ab612015-12-21 04:12:39 -0800253 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
255 case kStateEmpty:
philipel9d3ab612015-12-21 04:12:39 -0800256 // Should only be set to empty through Reset().
257 assert(false);
258 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000259
260 case kStateDecodable:
philipel9d3ab612015-12-21 04:12:39 -0800261 assert(_state == kStateEmpty || _state == kStateIncomplete);
262 break;
263 }
264 _state = state;
niklase@google.com470e71d2011-07-07 08:21:25 +0000265}
266
niklase@google.com470e71d2011-07-07 08:21:25 +0000267// Get current state of frame
philipel9d3ab612015-12-21 04:12:39 -0800268VCMFrameBufferStateEnum VCMFrameBuffer::GetState() const {
269 return _state;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270}
271
philipel9d3ab612015-12-21 04:12:39 -0800272void VCMFrameBuffer::PrepareForDecode(bool continuous) {
tommidb23ea62017-03-03 07:21:18 -0800273 TRACE_EVENT0("webrtc", "VCMFrameBuffer::PrepareForDecode");
philipel9d3ab612015-12-21 04:12:39 -0800274 size_t bytes_removed = _sessionInfo.MakeDecodable();
275 _length -= bytes_removed;
philipel9d3ab612015-12-21 04:12:39 -0800276 // Transfer frame information to EncodedFrame and create any codec
277 // specific information.
278 _frameType = _sessionInfo.FrameType();
279 _completeFrame = _sessionInfo.complete();
280 _missingFrame = !continuous;
niklase@google.com470e71d2011-07-07 08:21:25 +0000281}
282
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000283} // namespace webrtc