1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 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 11 #include "webrtc/modules/video_coding/frame_buffer.h" 12 13 #include <assert.h> 14 #include <string.h> 15 16 #include "webrtc/base/checks.h" 17 #include "webrtc/base/logging.h" 18 #include "webrtc/modules/video_coding/packet.h" 19 20 namespace webrtc { 21 22 VCMFrameBuffer::VCMFrameBuffer() 23 : _state(kStateEmpty), _nackCount(0), _latestPacketTimeMs(-1) {} 24 25 VCMFrameBuffer::~VCMFrameBuffer() {} 26 27 VCMFrameBuffer::VCMFrameBuffer(const VCMFrameBuffer& rhs) 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); 35 } 36 37 webrtc::FrameType VCMFrameBuffer::FrameType() const { 38 return _sessionInfo.FrameType(); 39 } 40 41 int32_t VCMFrameBuffer::GetLowSeqNum() const { 42 return _sessionInfo.LowSequenceNumber(); 43 } 44 45 int32_t VCMFrameBuffer::GetHighSeqNum() const { 46 return _sessionInfo.HighSequenceNumber(); 47 } 48 49 int VCMFrameBuffer::PictureId() const { 50 return _sessionInfo.PictureId(); 51 } 52 53 int VCMFrameBuffer::TemporalId() const { 54 return _sessionInfo.TemporalId(); 55 } 56 57 bool VCMFrameBuffer::LayerSync() const { 58 return _sessionInfo.LayerSync(); 59 } 60 61 int VCMFrameBuffer::Tl0PicId() const { 62 return _sessionInfo.Tl0PicId(); 63 } 64 65 bool VCMFrameBuffer::NonReference() const { 66 return _sessionInfo.NonReference(); 67 } 68 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 78 bool VCMFrameBuffer::IsSessionComplete() const { 79 return _sessionInfo.complete(); 80 } 81 82 // Insert packet 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); 103 } 104 } 105 106 uint32_t requiredSizeBytes = 107 Length() + packet.sizeBytes + 108 (packet.insertStartCode ? kH264StartCodeLengthBytes : 0); 109 if (requiredSizeBytes >= _size) { 110 const uint8_t* prevBuffer = _buffer; 111 const uint32_t increments = 112 requiredSizeBytes / kBufferIncStepSizeBytes + 113 (requiredSizeBytes % kBufferIncStepSizeBytes > 0); 114 const uint32_t newSize = _size + increments * kBufferIncStepSizeBytes; 115 if (newSize > kMaxJBFrameSizeBytes) { 116 LOG(LS_ERROR) << "Failed to insert packet due to frame being too " 117 "big."; 118 return kSizeError; 119 } 120 VerifyAndAllocate(newSize); 121 _sessionInfo.UpdateDataPointers(prevBuffer, _buffer); 122 } 123 124 if (packet.width > 0 && packet.height > 0) { 125 _encodedWidth = packet.width; 126 _encodedHeight = packet.height; 127 } 128 129 // Don't copy payload specific data for empty packets (e.g padding packets). 130 if (packet.sizeBytes > 0) 131 CopyCodecSpecific(&packet.codecSpecificHeader); 132 133 int retVal = 134 _sessionInfo.InsertPacket(packet, _buffer, decode_error_mode, frame_data); 135 if (retVal == -1) { 136 return kSizeError; 137 } else if (retVal == -2) { 138 return kDuplicatePacket; 139 } else if (retVal == -3) { 140 return kOutOfBoundsPacket; 141 } 142 // update length 143 _length = Length() + static_cast<uint32_t>(retVal); 144 145 _latestPacketTimeMs = timeInMs; 146 147 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ 148 // ts_126114v120700p.pdf Section 7.4.5. 149 // The MTSI client shall add the payload bytes as defined in this clause 150 // onto the last RTP packet in each group of packets which make up a key 151 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265 152 // (HEVC)). 153 if (packet.markerBit) { 154 RTC_DCHECK(!_rotation_set); 155 _rotation = packet.codecSpecificHeader.rotation; 156 _rotation_set = true; 157 } 158 159 if (_sessionInfo.complete()) { 160 SetState(kStateComplete); 161 return kCompleteSession; 162 } else if (_sessionInfo.decodable()) { 163 SetState(kStateDecodable); 164 return kDecodableSession; 165 } 166 return kIncomplete; 167 } 168 169 int64_t VCMFrameBuffer::LatestPacketTimeMs() const { 170 return _latestPacketTimeMs; 171 } 172 173 void VCMFrameBuffer::IncrementNackCount() { 174 _nackCount++; 175 } 176 177 int16_t VCMFrameBuffer::GetNackCount() const { 178 return _nackCount; 179 } 180 181 bool VCMFrameBuffer::HaveFirstPacket() const { 182 return _sessionInfo.HaveFirstPacket(); 183 } 184 185 bool VCMFrameBuffer::HaveLastPacket() const { 186 return _sessionInfo.HaveLastPacket(); 187 } 188 189 int VCMFrameBuffer::NumPackets() const { 190 return _sessionInfo.NumPackets(); 191 } 192 193 void VCMFrameBuffer::Reset() { 194 _length = 0; 195 _timeStamp = 0; 196 _sessionInfo.Reset(); 197 _payloadType = 0; 198 _nackCount = 0; 199 _latestPacketTimeMs = -1; 200 _state = kStateEmpty; 201 VCMEncodedFrame::Reset(); 202 } 203 204 // Set state of frame 205 void VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) { 206 if (_state == state) { 207 return; 208 } 209 switch (state) { 210 case kStateIncomplete: 211 // we can go to this state from state kStateEmpty 212 assert(_state == kStateEmpty); 213 214 // Do nothing, we received a packet 215 break; 216 217 case kStateComplete: 218 assert(_state == kStateEmpty || _state == kStateIncomplete || 219 _state == kStateDecodable); 220 221 break; 222 223 case kStateEmpty: 224 // Should only be set to empty through Reset(). 225 assert(false); 226 break; 227 228 case kStateDecodable: 229 assert(_state == kStateEmpty || _state == kStateIncomplete); 230 break; 231 } 232 _state = state; 233 } 234 235 // Get current state of frame 236 VCMFrameBufferStateEnum VCMFrameBuffer::GetState() const { 237 return _state; 238 } 239 240 // Get current state of frame 241 VCMFrameBufferStateEnum VCMFrameBuffer::GetState(uint32_t& timeStamp) const { 242 timeStamp = TimeStamp(); 243 return GetState(); 244 } 245 246 bool VCMFrameBuffer::IsRetransmitted() const { 247 return _sessionInfo.session_nack(); 248 } 249 250 void VCMFrameBuffer::PrepareForDecode(bool continuous) { 251 #ifdef INDEPENDENT_PARTITIONS 252 if (_codec == kVideoCodecVP8) { 253 _length = _sessionInfo.BuildVP8FragmentationHeader(_buffer, _length, 254 &_fragmentation); 255 } else { 256 size_t bytes_removed = _sessionInfo.MakeDecodable(); 257 _length -= bytes_removed; 258 } 259 #else 260 size_t bytes_removed = _sessionInfo.MakeDecodable(); 261 _length -= bytes_removed; 262 #endif 263 // Transfer frame information to EncodedFrame and create any codec 264 // specific information. 265 _frameType = _sessionInfo.FrameType(); 266 _completeFrame = _sessionInfo.complete(); 267 _missingFrame = !continuous; 268 } 269 270 } // namespace webrtc 271