blob: 11bf88a76629a45fda661c649d753f8b70a35a8f [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
Henrik Kjellander2557b862015-11-18 22:00:21 +010011#include "webrtc/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
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000016#include "webrtc/base/checks.h"
pbos854e84c2015-11-16 16:39:06 -080017#include "webrtc/base/logging.h"
tommidb23ea62017-03-03 07:21:18 -080018#include "webrtc/base/trace_event.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010019#include "webrtc/modules/video_coding/packet.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
mikhal@webrtc.orgea714402011-12-12 02:29:34 +000066bool VCMFrameBuffer::NonReference() const {
67 return _sessionInfo.NonReference();
68}
69
stefana669a3a2016-10-06 05:04:52 -070070std::vector<NaluInfo> VCMFrameBuffer::GetNaluInfos() const {
71 return _sessionInfo.GetNaluInfos();
72}
73
asapersson9a4cd872015-10-23 00:27:14 -070074void VCMFrameBuffer::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) {
tommidb23ea62017-03-03 07:21:18 -080075 TRACE_EVENT0("webrtc", "VCMFrameBuffer::SetGofInfo");
asapersson9a4cd872015-10-23 00:27:14 -070076 _sessionInfo.SetGofInfo(gof_info, idx);
77 // TODO(asapersson): Consider adding hdr->VP9.ref_picture_id for testing.
78 _codecSpecificInfo.codecSpecific.VP9.temporal_idx =
79 gof_info.temporal_idx[idx];
80 _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch =
81 gof_info.temporal_up_switch[idx];
82}
83
philipel9d3ab612015-12-21 04:12:39 -080084bool VCMFrameBuffer::IsSessionComplete() const {
tommidb23ea62017-03-03 07:21:18 -080085 TRACE_EVENT0("webrtc", "VCMFrameBuffer::IsSessionComplete");
philipel9d3ab612015-12-21 04:12:39 -080086 return _sessionInfo.complete();
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
89// Insert packet
philipel9d3ab612015-12-21 04:12:39 -080090VCMFrameBufferEnum VCMFrameBuffer::InsertPacket(
91 const VCMPacket& packet,
92 int64_t timeInMs,
93 VCMDecodeErrorMode decode_error_mode,
94 const FrameData& frame_data) {
tommidb23ea62017-03-03 07:21:18 -080095 TRACE_EVENT0("webrtc", "VCMFrameBuffer::InsertPacket");
philipel9d3ab612015-12-21 04:12:39 -080096 assert(!(NULL == packet.dataPtr && packet.sizeBytes > 0));
97 if (packet.dataPtr != NULL) {
98 _payloadType = packet.payloadType;
99 }
100
101 if (kStateEmpty == _state) {
102 // First packet (empty and/or media) inserted into this frame.
103 // store some info and set some initial values.
104 _timeStamp = packet.timestamp;
105 // We only take the ntp timestamp of the first packet of a frame.
106 ntp_time_ms_ = packet.ntp_time_ms_;
107 _codec = packet.codec;
108 if (packet.frameType != kEmptyFrame) {
109 // first media packet
110 SetState(kStateIncomplete);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 }
philipel9d3ab612015-12-21 04:12:39 -0800112 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
philipel9d3ab612015-12-21 04:12:39 -0800114 uint32_t requiredSizeBytes =
115 Length() + packet.sizeBytes +
hbosd6648362016-01-21 05:43:11 -0800116 (packet.insertStartCode ? kH264StartCodeLengthBytes : 0) +
117 EncodedImage::GetBufferPaddingBytes(packet.codec);
philipel9d3ab612015-12-21 04:12:39 -0800118 if (requiredSizeBytes >= _size) {
119 const uint8_t* prevBuffer = _buffer;
120 const uint32_t increments =
121 requiredSizeBytes / kBufferIncStepSizeBytes +
122 (requiredSizeBytes % kBufferIncStepSizeBytes > 0);
123 const uint32_t newSize = _size + increments * kBufferIncStepSizeBytes;
124 if (newSize > kMaxJBFrameSizeBytes) {
125 LOG(LS_ERROR) << "Failed to insert packet due to frame being too "
126 "big.";
127 return kSizeError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 }
philipel9d3ab612015-12-21 04:12:39 -0800129 VerifyAndAllocate(newSize);
130 _sessionInfo.UpdateDataPointers(prevBuffer, _buffer);
131 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
philipel9d3ab612015-12-21 04:12:39 -0800133 if (packet.width > 0 && packet.height > 0) {
134 _encodedWidth = packet.width;
135 _encodedHeight = packet.height;
136 }
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000137
philipel9d3ab612015-12-21 04:12:39 -0800138 // Don't copy payload specific data for empty packets (e.g padding packets).
139 if (packet.sizeBytes > 0)
isheriff6b4b5f32016-06-08 00:24:21 -0700140 CopyCodecSpecific(&packet.video_header);
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000141
philipel9d3ab612015-12-21 04:12:39 -0800142 int retVal =
143 _sessionInfo.InsertPacket(packet, _buffer, decode_error_mode, frame_data);
144 if (retVal == -1) {
145 return kSizeError;
146 } else if (retVal == -2) {
147 return kDuplicatePacket;
148 } else if (retVal == -3) {
149 return kOutOfBoundsPacket;
150 }
151 // update length
152 _length = Length() + static_cast<uint32_t>(retVal);
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000153
philipel9d3ab612015-12-21 04:12:39 -0800154 _latestPacketTimeMs = timeInMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
philipel9d3ab612015-12-21 04:12:39 -0800156 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
157 // ts_126114v120700p.pdf Section 7.4.5.
158 // The MTSI client shall add the payload bytes as defined in this clause
159 // onto the last RTP packet in each group of packets which make up a key
160 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
161 // (HEVC)).
162 if (packet.markerBit) {
163 RTC_DCHECK(!_rotation_set);
perkj414dda12016-07-04 01:45:23 -0700164 rotation_ = packet.video_header.rotation;
philipel9d3ab612015-12-21 04:12:39 -0800165 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -0700166 content_type_ = packet.video_header.content_type;
ilnik04f4d122017-06-19 07:18:55 -0700167 if (packet.video_header.video_timing.is_timing_frame) {
168 timing_.is_timing_frame = true;
169 timing_.encode_start_ms =
170 ntp_time_ms_ + packet.video_header.video_timing.encode_start_delta_ms;
171 timing_.encode_finish_ms =
172 ntp_time_ms_ +
173 packet.video_header.video_timing.encode_finish_delta_ms;
174 timing_.packetization_finish_ms =
175 ntp_time_ms_ +
176 packet.video_header.video_timing.packetization_finish_delta_ms;
177 timing_.pacer_exit_ms =
178 ntp_time_ms_ + packet.video_header.video_timing.pacer_exit_delta_ms;
179 timing_.network_timestamp_ms =
180 ntp_time_ms_ +
181 packet.video_header.video_timing.network_timstamp_delta_ms;
182 timing_.network2_timestamp_ms =
183 ntp_time_ms_ +
184 packet.video_header.video_timing.network2_timstamp_delta_ms;
185 } else {
186 timing_.is_timing_frame = false;
187 }
philipel9d3ab612015-12-21 04:12:39 -0800188 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
johan0d1b2b62017-01-10 04:21:35 -0800190 if (packet.is_first_packet_in_frame) {
isheriff6b4b5f32016-06-08 00:24:21 -0700191 playout_delay_ = packet.video_header.playout_delay;
192 }
193
philipel9d3ab612015-12-21 04:12:39 -0800194 if (_sessionInfo.complete()) {
195 SetState(kStateComplete);
196 return kCompleteSession;
197 } else if (_sessionInfo.decodable()) {
198 SetState(kStateDecodable);
199 return kDecodableSession;
200 }
201 return kIncomplete;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
philipel9d3ab612015-12-21 04:12:39 -0800204int64_t VCMFrameBuffer::LatestPacketTimeMs() const {
tommidb23ea62017-03-03 07:21:18 -0800205 TRACE_EVENT0("webrtc", "VCMFrameBuffer::LatestPacketTimeMs");
philipel9d3ab612015-12-21 04:12:39 -0800206 return _latestPacketTimeMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207}
208
philipel9d3ab612015-12-21 04:12:39 -0800209void VCMFrameBuffer::IncrementNackCount() {
tommidb23ea62017-03-03 07:21:18 -0800210 TRACE_EVENT0("webrtc", "VCMFrameBuffer::IncrementNackCount");
philipel9d3ab612015-12-21 04:12:39 -0800211 _nackCount++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
philipel9d3ab612015-12-21 04:12:39 -0800214int16_t VCMFrameBuffer::GetNackCount() const {
tommidb23ea62017-03-03 07:21:18 -0800215 TRACE_EVENT0("webrtc", "VCMFrameBuffer::GetNackCount");
philipel9d3ab612015-12-21 04:12:39 -0800216 return _nackCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000217}
218
philipel9d3ab612015-12-21 04:12:39 -0800219bool VCMFrameBuffer::HaveFirstPacket() const {
tommidb23ea62017-03-03 07:21:18 -0800220 TRACE_EVENT0("webrtc", "VCMFrameBuffer::HaveFirstPacket");
philipel9d3ab612015-12-21 04:12:39 -0800221 return _sessionInfo.HaveFirstPacket();
stefan@webrtc.org885cd132013-04-16 09:38:26 +0000222}
223
philipel9d3ab612015-12-21 04:12:39 -0800224bool VCMFrameBuffer::HaveLastPacket() const {
tommidb23ea62017-03-03 07:21:18 -0800225 TRACE_EVENT0("webrtc", "VCMFrameBuffer::HaveLastPacket");
philipel9d3ab612015-12-21 04:12:39 -0800226 return _sessionInfo.HaveLastPacket();
niklase@google.com470e71d2011-07-07 08:21:25 +0000227}
228
philipel9d3ab612015-12-21 04:12:39 -0800229int VCMFrameBuffer::NumPackets() const {
tommidb23ea62017-03-03 07:21:18 -0800230 TRACE_EVENT0("webrtc", "VCMFrameBuffer::NumPackets");
philipel9d3ab612015-12-21 04:12:39 -0800231 return _sessionInfo.NumPackets();
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000232}
233
philipel9d3ab612015-12-21 04:12:39 -0800234void VCMFrameBuffer::Reset() {
tommidb23ea62017-03-03 07:21:18 -0800235 TRACE_EVENT0("webrtc", "VCMFrameBuffer::Reset");
philipel9d3ab612015-12-21 04:12:39 -0800236 _length = 0;
237 _timeStamp = 0;
238 _sessionInfo.Reset();
239 _payloadType = 0;
240 _nackCount = 0;
241 _latestPacketTimeMs = -1;
242 _state = kStateEmpty;
243 VCMEncodedFrame::Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
niklase@google.com470e71d2011-07-07 08:21:25 +0000246// Set state of frame
philipel9d3ab612015-12-21 04:12:39 -0800247void VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) {
tommidb23ea62017-03-03 07:21:18 -0800248 TRACE_EVENT0("webrtc", "VCMFrameBuffer::SetState");
philipel9d3ab612015-12-21 04:12:39 -0800249 if (_state == state) {
250 return;
251 }
252 switch (state) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000253 case kStateIncomplete:
philipel9d3ab612015-12-21 04:12:39 -0800254 // we can go to this state from state kStateEmpty
255 assert(_state == kStateEmpty);
niklase@google.com470e71d2011-07-07 08:21:25 +0000256
philipel9d3ab612015-12-21 04:12:39 -0800257 // Do nothing, we received a packet
258 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000259
260 case kStateComplete:
philipel9d3ab612015-12-21 04:12:39 -0800261 assert(_state == kStateEmpty || _state == kStateIncomplete ||
262 _state == kStateDecodable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
philipel9d3ab612015-12-21 04:12:39 -0800264 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000265
266 case kStateEmpty:
philipel9d3ab612015-12-21 04:12:39 -0800267 // Should only be set to empty through Reset().
268 assert(false);
269 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
271 case kStateDecodable:
philipel9d3ab612015-12-21 04:12:39 -0800272 assert(_state == kStateEmpty || _state == kStateIncomplete);
273 break;
274 }
275 _state = state;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276}
277
niklase@google.com470e71d2011-07-07 08:21:25 +0000278// Get current state of frame
philipel9d3ab612015-12-21 04:12:39 -0800279VCMFrameBufferStateEnum VCMFrameBuffer::GetState() const {
280 return _state;
niklase@google.com470e71d2011-07-07 08:21:25 +0000281}
282
283// Get current state of frame
philipel9d3ab612015-12-21 04:12:39 -0800284VCMFrameBufferStateEnum VCMFrameBuffer::GetState(uint32_t& timeStamp) const {
tommidb23ea62017-03-03 07:21:18 -0800285 TRACE_EVENT0("webrtc", "VCMFrameBuffer::GetState");
philipel9d3ab612015-12-21 04:12:39 -0800286 timeStamp = TimeStamp();
287 return GetState();
niklase@google.com470e71d2011-07-07 08:21:25 +0000288}
289
philipel9d3ab612015-12-21 04:12:39 -0800290bool VCMFrameBuffer::IsRetransmitted() const {
291 return _sessionInfo.session_nack();
niklase@google.com470e71d2011-07-07 08:21:25 +0000292}
293
philipel9d3ab612015-12-21 04:12:39 -0800294void VCMFrameBuffer::PrepareForDecode(bool continuous) {
tommidb23ea62017-03-03 07:21:18 -0800295 TRACE_EVENT0("webrtc", "VCMFrameBuffer::PrepareForDecode");
philipel9d3ab612015-12-21 04:12:39 -0800296 size_t bytes_removed = _sessionInfo.MakeDecodable();
297 _length -= bytes_removed;
philipel9d3ab612015-12-21 04:12:39 -0800298 // Transfer frame information to EncodedFrame and create any codec
299 // specific information.
300 _frameType = _sessionInfo.FrameType();
301 _completeFrame = _sessionInfo.complete();
302 _missingFrame = !continuous;
niklase@google.com470e71d2011-07-07 08:21:25 +0000303}
304
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000305} // namespace webrtc