niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/frame_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 16 | #include "webrtc/base/checks.h" |
pbos | 854e84c | 2015-11-16 16:39:06 -0800 | [diff] [blame] | 17 | #include "webrtc/base/logging.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/video_coding/packet.h" |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 +0000 | [diff] [blame] | 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 +0000 | [diff] [blame] | 22 | VCMFrameBuffer::VCMFrameBuffer() |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 23 | : _state(kStateEmpty), _nackCount(0), _latestPacketTimeMs(-1) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 25 | VCMFrameBuffer::~VCMFrameBuffer() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 +0000 | [diff] [blame] | 27 | VCMFrameBuffer::VCMFrameBuffer(const VCMFrameBuffer& rhs) |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 28 | : VCMEncodedFrame(rhs), |
| 29 | _state(rhs._state), |
| 30 | _sessionInfo(), |
| 31 | _nackCount(rhs._nackCount), |
| 32 | _latestPacketTimeMs(rhs._latestPacketTimeMs) { |
| 33 | _sessionInfo = rhs._sessionInfo; |
| 34 | _sessionInfo.UpdateDataPointers(rhs._buffer, _buffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
| 36 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 37 | webrtc::FrameType VCMFrameBuffer::FrameType() const { |
| 38 | return _sessionInfo.FrameType(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 41 | int32_t VCMFrameBuffer::GetLowSeqNum() const { |
| 42 | return _sessionInfo.LowSequenceNumber(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 45 | int32_t VCMFrameBuffer::GetHighSeqNum() const { |
| 46 | return _sessionInfo.HighSequenceNumber(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
stefan@webrtc.org | ffd28f9 | 2011-10-19 15:55:39 +0000 | [diff] [blame] | 49 | int VCMFrameBuffer::PictureId() const { |
| 50 | return _sessionInfo.PictureId(); |
| 51 | } |
| 52 | |
mikhal@webrtc.org | f5ee1dc | 2011-12-08 19:04:47 +0000 | [diff] [blame] | 53 | int VCMFrameBuffer::TemporalId() const { |
| 54 | return _sessionInfo.TemporalId(); |
| 55 | } |
| 56 | |
henrik.lundin@webrtc.org | eda86dc | 2011-12-13 14:11:06 +0000 | [diff] [blame] | 57 | bool VCMFrameBuffer::LayerSync() const { |
| 58 | return _sessionInfo.LayerSync(); |
| 59 | } |
| 60 | |
mikhal@webrtc.org | f5ee1dc | 2011-12-08 19:04:47 +0000 | [diff] [blame] | 61 | int VCMFrameBuffer::Tl0PicId() const { |
| 62 | return _sessionInfo.Tl0PicId(); |
| 63 | } |
| 64 | |
mikhal@webrtc.org | ea71440 | 2011-12-12 02:29:34 +0000 | [diff] [blame] | 65 | bool VCMFrameBuffer::NonReference() const { |
| 66 | return _sessionInfo.NonReference(); |
| 67 | } |
| 68 | |
asapersson | 9a4cd87 | 2015-10-23 00:27:14 -0700 | [diff] [blame] | 69 | void VCMFrameBuffer::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) { |
| 70 | _sessionInfo.SetGofInfo(gof_info, idx); |
| 71 | // TODO(asapersson): Consider adding hdr->VP9.ref_picture_id for testing. |
| 72 | _codecSpecificInfo.codecSpecific.VP9.temporal_idx = |
| 73 | gof_info.temporal_idx[idx]; |
| 74 | _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch = |
| 75 | gof_info.temporal_up_switch[idx]; |
| 76 | } |
| 77 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 78 | bool VCMFrameBuffer::IsSessionComplete() const { |
| 79 | return _sessionInfo.complete(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Insert packet |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 83 | VCMFrameBufferEnum VCMFrameBuffer::InsertPacket( |
| 84 | const VCMPacket& packet, |
| 85 | int64_t timeInMs, |
| 86 | VCMDecodeErrorMode decode_error_mode, |
| 87 | const FrameData& frame_data) { |
| 88 | assert(!(NULL == packet.dataPtr && packet.sizeBytes > 0)); |
| 89 | if (packet.dataPtr != NULL) { |
| 90 | _payloadType = packet.payloadType; |
| 91 | } |
| 92 | |
| 93 | if (kStateEmpty == _state) { |
| 94 | // First packet (empty and/or media) inserted into this frame. |
| 95 | // store some info and set some initial values. |
| 96 | _timeStamp = packet.timestamp; |
| 97 | // We only take the ntp timestamp of the first packet of a frame. |
| 98 | ntp_time_ms_ = packet.ntp_time_ms_; |
| 99 | _codec = packet.codec; |
| 100 | if (packet.frameType != kEmptyFrame) { |
| 101 | // first media packet |
| 102 | SetState(kStateIncomplete); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 104 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 106 | uint32_t requiredSizeBytes = |
| 107 | Length() + packet.sizeBytes + |
hbos | d664836 | 2016-01-21 05:43:11 -0800 | [diff] [blame] | 108 | (packet.insertStartCode ? kH264StartCodeLengthBytes : 0) + |
| 109 | EncodedImage::GetBufferPaddingBytes(packet.codec); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 110 | if (requiredSizeBytes >= _size) { |
| 111 | const uint8_t* prevBuffer = _buffer; |
| 112 | const uint32_t increments = |
| 113 | requiredSizeBytes / kBufferIncStepSizeBytes + |
| 114 | (requiredSizeBytes % kBufferIncStepSizeBytes > 0); |
| 115 | const uint32_t newSize = _size + increments * kBufferIncStepSizeBytes; |
| 116 | if (newSize > kMaxJBFrameSizeBytes) { |
| 117 | LOG(LS_ERROR) << "Failed to insert packet due to frame being too " |
| 118 | "big."; |
| 119 | return kSizeError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 121 | VerifyAndAllocate(newSize); |
| 122 | _sessionInfo.UpdateDataPointers(prevBuffer, _buffer); |
| 123 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 125 | if (packet.width > 0 && packet.height > 0) { |
| 126 | _encodedWidth = packet.width; |
| 127 | _encodedHeight = packet.height; |
| 128 | } |
henrik.lundin@webrtc.org | 473bac8 | 2011-08-17 09:47:33 +0000 | [diff] [blame] | 129 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 130 | // Don't copy payload specific data for empty packets (e.g padding packets). |
| 131 | if (packet.sizeBytes > 0) |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame^] | 132 | CopyCodecSpecific(&packet.video_header); |
stefan@webrtc.org | 3417eb4 | 2013-05-21 15:25:53 +0000 | [diff] [blame] | 133 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 134 | int retVal = |
| 135 | _sessionInfo.InsertPacket(packet, _buffer, decode_error_mode, frame_data); |
| 136 | if (retVal == -1) { |
| 137 | return kSizeError; |
| 138 | } else if (retVal == -2) { |
| 139 | return kDuplicatePacket; |
| 140 | } else if (retVal == -3) { |
| 141 | return kOutOfBoundsPacket; |
| 142 | } |
| 143 | // update length |
| 144 | _length = Length() + static_cast<uint32_t>(retVal); |
henrik.lundin@webrtc.org | 473bac8 | 2011-08-17 09:47:33 +0000 | [diff] [blame] | 145 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 146 | _latestPacketTimeMs = timeInMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 148 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ |
| 149 | // ts_126114v120700p.pdf Section 7.4.5. |
| 150 | // The MTSI client shall add the payload bytes as defined in this clause |
| 151 | // onto the last RTP packet in each group of packets which make up a key |
| 152 | // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265 |
| 153 | // (HEVC)). |
| 154 | if (packet.markerBit) { |
| 155 | RTC_DCHECK(!_rotation_set); |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame^] | 156 | _rotation = packet.video_header.rotation; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 157 | _rotation_set = true; |
| 158 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame^] | 160 | if (packet.isFirstPacket) { |
| 161 | playout_delay_ = packet.video_header.playout_delay; |
| 162 | } |
| 163 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 164 | if (_sessionInfo.complete()) { |
| 165 | SetState(kStateComplete); |
| 166 | return kCompleteSession; |
| 167 | } else if (_sessionInfo.decodable()) { |
| 168 | SetState(kStateDecodable); |
| 169 | return kDecodableSession; |
| 170 | } |
| 171 | return kIncomplete; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | } |
| 173 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 174 | int64_t VCMFrameBuffer::LatestPacketTimeMs() const { |
| 175 | return _latestPacketTimeMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | } |
| 177 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 178 | void VCMFrameBuffer::IncrementNackCount() { |
| 179 | _nackCount++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | } |
| 181 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 182 | int16_t VCMFrameBuffer::GetNackCount() const { |
| 183 | return _nackCount; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 186 | bool VCMFrameBuffer::HaveFirstPacket() const { |
| 187 | return _sessionInfo.HaveFirstPacket(); |
stefan@webrtc.org | 885cd13 | 2013-04-16 09:38:26 +0000 | [diff] [blame] | 188 | } |
| 189 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 190 | bool VCMFrameBuffer::HaveLastPacket() const { |
| 191 | return _sessionInfo.HaveLastPacket(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 192 | } |
| 193 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 194 | int VCMFrameBuffer::NumPackets() const { |
| 195 | return _sessionInfo.NumPackets(); |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 +0000 | [diff] [blame] | 196 | } |
| 197 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 198 | void VCMFrameBuffer::Reset() { |
| 199 | _length = 0; |
| 200 | _timeStamp = 0; |
| 201 | _sessionInfo.Reset(); |
| 202 | _payloadType = 0; |
| 203 | _nackCount = 0; |
| 204 | _latestPacketTimeMs = -1; |
| 205 | _state = kStateEmpty; |
| 206 | VCMEncodedFrame::Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | } |
| 208 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | // Set state of frame |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 210 | void VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) { |
| 211 | if (_state == state) { |
| 212 | return; |
| 213 | } |
| 214 | switch (state) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | case kStateIncomplete: |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 216 | // we can go to this state from state kStateEmpty |
| 217 | assert(_state == kStateEmpty); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 219 | // Do nothing, we received a packet |
| 220 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | |
| 222 | case kStateComplete: |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 223 | assert(_state == kStateEmpty || _state == kStateIncomplete || |
| 224 | _state == kStateDecodable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 226 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | |
| 228 | case kStateEmpty: |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 229 | // Should only be set to empty through Reset(). |
| 230 | assert(false); |
| 231 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | |
| 233 | case kStateDecodable: |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 234 | assert(_state == kStateEmpty || _state == kStateIncomplete); |
| 235 | break; |
| 236 | } |
| 237 | _state = state; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 238 | } |
| 239 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | // Get current state of frame |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 241 | VCMFrameBufferStateEnum VCMFrameBuffer::GetState() const { |
| 242 | return _state; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Get current state of frame |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 246 | VCMFrameBufferStateEnum VCMFrameBuffer::GetState(uint32_t& timeStamp) const { |
| 247 | timeStamp = TimeStamp(); |
| 248 | return GetState(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 249 | } |
| 250 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 251 | bool VCMFrameBuffer::IsRetransmitted() const { |
| 252 | return _sessionInfo.session_nack(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | } |
| 254 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 255 | void VCMFrameBuffer::PrepareForDecode(bool continuous) { |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 +0000 | [diff] [blame] | 256 | #ifdef INDEPENDENT_PARTITIONS |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 257 | if (_codec == kVideoCodecVP8) { |
| 258 | _length = _sessionInfo.BuildVP8FragmentationHeader(_buffer, _length, |
| 259 | &_fragmentation); |
| 260 | } else { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 261 | size_t bytes_removed = _sessionInfo.MakeDecodable(); |
stefan@webrtc.org | 4cf1a8a | 2013-06-27 15:20:14 +0000 | [diff] [blame] | 262 | _length -= bytes_removed; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 263 | } |
| 264 | #else |
| 265 | size_t bytes_removed = _sessionInfo.MakeDecodable(); |
| 266 | _length -= bytes_removed; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 +0000 | [diff] [blame] | 267 | #endif |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 268 | // Transfer frame information to EncodedFrame and create any codec |
| 269 | // specific information. |
| 270 | _frameType = _sessionInfo.FrameType(); |
| 271 | _completeFrame = _sessionInfo.complete(); |
| 272 | _missingFrame = !continuous; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | } |
| 274 | |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 +0000 | [diff] [blame] | 275 | } // namespace webrtc |