blob: b623dd607b28838fe9e2ea977a8b0afd7ac52f57 [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"
18#include "modules/rtp_rtcp/source/rtp_video_header.h"
19#include "modules/video_coding/include/video_codec_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/video_coding/packet.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
23#include "rtc_base/trace_event.h"
agalusza@google.comd818dcb2013-07-29 21:48:11 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025namespace webrtc {
26
stefan@webrtc.orgc3d89102011-09-08 06:50:28 +000027VCMFrameBuffer::VCMFrameBuffer()
philipel9d3ab612015-12-21 04:12:39 -080028 : _state(kStateEmpty), _nackCount(0), _latestPacketTimeMs(-1) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000029
philipel9d3ab612015-12-21 04:12:39 -080030VCMFrameBuffer::~VCMFrameBuffer() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000031
philipel9d3ab612015-12-21 04:12:39 -080032webrtc::FrameType VCMFrameBuffer::FrameType() const {
33 return _sessionInfo.FrameType();
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
philipel9d3ab612015-12-21 04:12:39 -080036int32_t VCMFrameBuffer::GetLowSeqNum() const {
37 return _sessionInfo.LowSequenceNumber();
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
philipel9d3ab612015-12-21 04:12:39 -080040int32_t VCMFrameBuffer::GetHighSeqNum() const {
41 return _sessionInfo.HighSequenceNumber();
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
stefan@webrtc.orgffd28f92011-10-19 15:55:39 +000044int VCMFrameBuffer::PictureId() const {
45 return _sessionInfo.PictureId();
46}
47
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:47 +000048int VCMFrameBuffer::TemporalId() const {
49 return _sessionInfo.TemporalId();
50}
51
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:06 +000052bool VCMFrameBuffer::LayerSync() const {
53 return _sessionInfo.LayerSync();
54}
55
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:47 +000056int VCMFrameBuffer::Tl0PicId() const {
57 return _sessionInfo.Tl0PicId();
58}
59
stefana669a3a2016-10-06 05:04:52 -070060std::vector<NaluInfo> VCMFrameBuffer::GetNaluInfos() const {
61 return _sessionInfo.GetNaluInfos();
62}
63
asapersson9a4cd872015-10-23 00:27:14 -070064void VCMFrameBuffer::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) {
tommidb23ea62017-03-03 07:21:18 -080065 TRACE_EVENT0("webrtc", "VCMFrameBuffer::SetGofInfo");
asapersson9a4cd872015-10-23 00:27:14 -070066 _sessionInfo.SetGofInfo(gof_info, idx);
67 // TODO(asapersson): Consider adding hdr->VP9.ref_picture_id for testing.
68 _codecSpecificInfo.codecSpecific.VP9.temporal_idx =
69 gof_info.temporal_idx[idx];
70 _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch =
71 gof_info.temporal_up_switch[idx];
72}
73
philipel9d3ab612015-12-21 04:12:39 -080074bool VCMFrameBuffer::IsSessionComplete() const {
tommidb23ea62017-03-03 07:21:18 -080075 TRACE_EVENT0("webrtc", "VCMFrameBuffer::IsSessionComplete");
philipel9d3ab612015-12-21 04:12:39 -080076 return _sessionInfo.complete();
niklase@google.com470e71d2011-07-07 08:21:25 +000077}
78
79// Insert packet
philipel9d3ab612015-12-21 04:12:39 -080080VCMFrameBufferEnum VCMFrameBuffer::InsertPacket(
81 const VCMPacket& packet,
82 int64_t timeInMs,
83 VCMDecodeErrorMode decode_error_mode,
84 const FrameData& frame_data) {
tommidb23ea62017-03-03 07:21:18 -080085 TRACE_EVENT0("webrtc", "VCMFrameBuffer::InsertPacket");
philipel9d3ab612015-12-21 04:12:39 -080086 assert(!(NULL == packet.dataPtr && packet.sizeBytes > 0));
87 if (packet.dataPtr != NULL) {
88 _payloadType = packet.payloadType;
89 }
90
91 if (kStateEmpty == _state) {
92 // First packet (empty and/or media) inserted into this frame.
93 // store some info and set some initial values.
Niels Möller23775882018-08-16 10:24:12 +020094 SetTimestamp(packet.timestamp);
philipel9d3ab612015-12-21 04:12:39 -080095 // We only take the ntp timestamp of the first packet of a frame.
96 ntp_time_ms_ = packet.ntp_time_ms_;
97 _codec = packet.codec;
98 if (packet.frameType != kEmptyFrame) {
99 // first media packet
100 SetState(kStateIncomplete);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 }
philipel9d3ab612015-12-21 04:12:39 -0800102 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
philipel9d3ab612015-12-21 04:12:39 -0800104 uint32_t requiredSizeBytes =
105 Length() + packet.sizeBytes +
hbosd6648362016-01-21 05:43:11 -0800106 (packet.insertStartCode ? kH264StartCodeLengthBytes : 0) +
107 EncodedImage::GetBufferPaddingBytes(packet.codec);
philipel9d3ab612015-12-21 04:12:39 -0800108 if (requiredSizeBytes >= _size) {
109 const uint8_t* prevBuffer = _buffer;
110 const uint32_t increments =
111 requiredSizeBytes / kBufferIncStepSizeBytes +
112 (requiredSizeBytes % kBufferIncStepSizeBytes > 0);
113 const uint32_t newSize = _size + increments * kBufferIncStepSizeBytes;
114 if (newSize > kMaxJBFrameSizeBytes) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_ERROR) << "Failed to insert packet due to frame being too "
116 "big.";
philipel9d3ab612015-12-21 04:12:39 -0800117 return kSizeError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000118 }
philipel9d3ab612015-12-21 04:12:39 -0800119 VerifyAndAllocate(newSize);
120 _sessionInfo.UpdateDataPointers(prevBuffer, _buffer);
121 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
philipel9d3ab612015-12-21 04:12:39 -0800123 if (packet.width > 0 && packet.height > 0) {
124 _encodedWidth = packet.width;
125 _encodedHeight = packet.height;
126 }
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000127
philipel9d3ab612015-12-21 04:12:39 -0800128 // Don't copy payload specific data for empty packets (e.g padding packets).
129 if (packet.sizeBytes > 0)
isheriff6b4b5f32016-06-08 00:24:21 -0700130 CopyCodecSpecific(&packet.video_header);
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000131
philipel9d3ab612015-12-21 04:12:39 -0800132 int retVal =
133 _sessionInfo.InsertPacket(packet, _buffer, decode_error_mode, frame_data);
134 if (retVal == -1) {
135 return kSizeError;
136 } else if (retVal == -2) {
137 return kDuplicatePacket;
138 } else if (retVal == -3) {
139 return kOutOfBoundsPacket;
140 }
141 // update length
142 _length = Length() + static_cast<uint32_t>(retVal);
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000143
philipel9d3ab612015-12-21 04:12:39 -0800144 _latestPacketTimeMs = timeInMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
philipel9d3ab612015-12-21 04:12:39 -0800146 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
147 // ts_126114v120700p.pdf Section 7.4.5.
148 // The MTSI client shall add the payload bytes as defined in this clause
149 // onto the last RTP packet in each group of packets which make up a key
150 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
151 // (HEVC)).
152 if (packet.markerBit) {
153 RTC_DCHECK(!_rotation_set);
perkj414dda12016-07-04 01:45:23 -0700154 rotation_ = packet.video_header.rotation;
philipel9d3ab612015-12-21 04:12:39 -0800155 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -0700156 content_type_ = packet.video_header.content_type;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200157 if (packet.video_header.video_timing.flags != VideoSendTiming::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -0700158 timing_.encode_start_ms =
159 ntp_time_ms_ + packet.video_header.video_timing.encode_start_delta_ms;
160 timing_.encode_finish_ms =
161 ntp_time_ms_ +
162 packet.video_header.video_timing.encode_finish_delta_ms;
163 timing_.packetization_finish_ms =
164 ntp_time_ms_ +
165 packet.video_header.video_timing.packetization_finish_delta_ms;
166 timing_.pacer_exit_ms =
167 ntp_time_ms_ + packet.video_header.video_timing.pacer_exit_delta_ms;
168 timing_.network_timestamp_ms =
169 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100170 packet.video_header.video_timing.network_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700171 timing_.network2_timestamp_ms =
172 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100173 packet.video_header.video_timing.network2_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700174 }
sprangba050a62017-08-18 02:51:12 -0700175 timing_.flags = packet.video_header.video_timing.flags;
philipel9d3ab612015-12-21 04:12:39 -0800176 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
johan0d1b2b62017-01-10 04:21:35 -0800178 if (packet.is_first_packet_in_frame) {
isheriff6b4b5f32016-06-08 00:24:21 -0700179 playout_delay_ = packet.video_header.playout_delay;
180 }
181
philipel9d3ab612015-12-21 04:12:39 -0800182 if (_sessionInfo.complete()) {
183 SetState(kStateComplete);
184 return kCompleteSession;
185 } else if (_sessionInfo.decodable()) {
186 SetState(kStateDecodable);
187 return kDecodableSession;
188 }
189 return kIncomplete;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
philipel9d3ab612015-12-21 04:12:39 -0800192int64_t VCMFrameBuffer::LatestPacketTimeMs() const {
tommidb23ea62017-03-03 07:21:18 -0800193 TRACE_EVENT0("webrtc", "VCMFrameBuffer::LatestPacketTimeMs");
philipel9d3ab612015-12-21 04:12:39 -0800194 return _latestPacketTimeMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
philipel9d3ab612015-12-21 04:12:39 -0800197void VCMFrameBuffer::IncrementNackCount() {
tommidb23ea62017-03-03 07:21:18 -0800198 TRACE_EVENT0("webrtc", "VCMFrameBuffer::IncrementNackCount");
philipel9d3ab612015-12-21 04:12:39 -0800199 _nackCount++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200}
201
philipel9d3ab612015-12-21 04:12:39 -0800202int16_t VCMFrameBuffer::GetNackCount() const {
tommidb23ea62017-03-03 07:21:18 -0800203 TRACE_EVENT0("webrtc", "VCMFrameBuffer::GetNackCount");
philipel9d3ab612015-12-21 04:12:39 -0800204 return _nackCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
philipel9d3ab612015-12-21 04:12:39 -0800207bool VCMFrameBuffer::HaveFirstPacket() const {
tommidb23ea62017-03-03 07:21:18 -0800208 TRACE_EVENT0("webrtc", "VCMFrameBuffer::HaveFirstPacket");
philipel9d3ab612015-12-21 04:12:39 -0800209 return _sessionInfo.HaveFirstPacket();
stefan@webrtc.org885cd132013-04-16 09:38:26 +0000210}
211
philipel9d3ab612015-12-21 04:12:39 -0800212int VCMFrameBuffer::NumPackets() const {
tommidb23ea62017-03-03 07:21:18 -0800213 TRACE_EVENT0("webrtc", "VCMFrameBuffer::NumPackets");
philipel9d3ab612015-12-21 04:12:39 -0800214 return _sessionInfo.NumPackets();
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000215}
216
philipel9d3ab612015-12-21 04:12:39 -0800217void VCMFrameBuffer::Reset() {
tommidb23ea62017-03-03 07:21:18 -0800218 TRACE_EVENT0("webrtc", "VCMFrameBuffer::Reset");
philipel9d3ab612015-12-21 04:12:39 -0800219 _length = 0;
philipel9d3ab612015-12-21 04:12:39 -0800220 _sessionInfo.Reset();
221 _payloadType = 0;
222 _nackCount = 0;
223 _latestPacketTimeMs = -1;
224 _state = kStateEmpty;
225 VCMEncodedFrame::Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000226}
227
niklase@google.com470e71d2011-07-07 08:21:25 +0000228// Set state of frame
philipel9d3ab612015-12-21 04:12:39 -0800229void VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) {
tommidb23ea62017-03-03 07:21:18 -0800230 TRACE_EVENT0("webrtc", "VCMFrameBuffer::SetState");
philipel9d3ab612015-12-21 04:12:39 -0800231 if (_state == state) {
232 return;
233 }
234 switch (state) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 case kStateIncomplete:
philipel9d3ab612015-12-21 04:12:39 -0800236 // we can go to this state from state kStateEmpty
237 assert(_state == kStateEmpty);
niklase@google.com470e71d2011-07-07 08:21:25 +0000238
philipel9d3ab612015-12-21 04:12:39 -0800239 // Do nothing, we received a packet
240 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
242 case kStateComplete:
philipel9d3ab612015-12-21 04:12:39 -0800243 assert(_state == kStateEmpty || _state == kStateIncomplete ||
244 _state == kStateDecodable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
philipel9d3ab612015-12-21 04:12:39 -0800246 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
248 case kStateEmpty:
philipel9d3ab612015-12-21 04:12:39 -0800249 // Should only be set to empty through Reset().
250 assert(false);
251 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252
253 case kStateDecodable:
philipel9d3ab612015-12-21 04:12:39 -0800254 assert(_state == kStateEmpty || _state == kStateIncomplete);
255 break;
256 }
257 _state = state;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
niklase@google.com470e71d2011-07-07 08:21:25 +0000260// Get current state of frame
philipel9d3ab612015-12-21 04:12:39 -0800261VCMFrameBufferStateEnum VCMFrameBuffer::GetState() const {
262 return _state;
niklase@google.com470e71d2011-07-07 08:21:25 +0000263}
264
philipel9d3ab612015-12-21 04:12:39 -0800265void VCMFrameBuffer::PrepareForDecode(bool continuous) {
tommidb23ea62017-03-03 07:21:18 -0800266 TRACE_EVENT0("webrtc", "VCMFrameBuffer::PrepareForDecode");
philipel9d3ab612015-12-21 04:12:39 -0800267 size_t bytes_removed = _sessionInfo.MakeDecodable();
268 _length -= bytes_removed;
philipel9d3ab612015-12-21 04:12:39 -0800269 // Transfer frame information to EncodedFrame and create any codec
270 // specific information.
271 _frameType = _sessionInfo.FrameType();
272 _completeFrame = _sessionInfo.complete();
273 _missingFrame = !continuous;
niklase@google.com470e71d2011-07-07 08:21:25 +0000274}
275
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000276} // namespace webrtc