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 
VCMFrameBuffer()22 VCMFrameBuffer::VCMFrameBuffer()
23     : _state(kStateEmpty), _nackCount(0), _latestPacketTimeMs(-1) {}
24 
~VCMFrameBuffer()25 VCMFrameBuffer::~VCMFrameBuffer() {}
26 
VCMFrameBuffer(const VCMFrameBuffer & rhs)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 
FrameType() const37 webrtc::FrameType VCMFrameBuffer::FrameType() const {
38   return _sessionInfo.FrameType();
39 }
40 
GetLowSeqNum() const41 int32_t VCMFrameBuffer::GetLowSeqNum() const {
42   return _sessionInfo.LowSequenceNumber();
43 }
44 
GetHighSeqNum() const45 int32_t VCMFrameBuffer::GetHighSeqNum() const {
46   return _sessionInfo.HighSequenceNumber();
47 }
48 
PictureId() const49 int VCMFrameBuffer::PictureId() const {
50   return _sessionInfo.PictureId();
51 }
52 
TemporalId() const53 int VCMFrameBuffer::TemporalId() const {
54   return _sessionInfo.TemporalId();
55 }
56 
LayerSync() const57 bool VCMFrameBuffer::LayerSync() const {
58   return _sessionInfo.LayerSync();
59 }
60 
Tl0PicId() const61 int VCMFrameBuffer::Tl0PicId() const {
62   return _sessionInfo.Tl0PicId();
63 }
64 
NonReference() const65 bool VCMFrameBuffer::NonReference() const {
66   return _sessionInfo.NonReference();
67 }
68 
GetNaluInfos() const69 std::vector<NaluInfo> VCMFrameBuffer::GetNaluInfos() const {
70   return _sessionInfo.GetNaluInfos();
71 }
72 
SetGofInfo(const GofInfoVP9 & gof_info,size_t idx)73 void VCMFrameBuffer::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) {
74   _sessionInfo.SetGofInfo(gof_info, idx);
75   // TODO(asapersson): Consider adding hdr->VP9.ref_picture_id for testing.
76   _codecSpecificInfo.codecSpecific.VP9.temporal_idx =
77       gof_info.temporal_idx[idx];
78   _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch =
79       gof_info.temporal_up_switch[idx];
80 }
81 
IsSessionComplete() const82 bool VCMFrameBuffer::IsSessionComplete() const {
83   return _sessionInfo.complete();
84 }
85 
86 // Insert packet
InsertPacket(const VCMPacket & packet,int64_t timeInMs,VCMDecodeErrorMode decode_error_mode,const FrameData & frame_data)87 VCMFrameBufferEnum VCMFrameBuffer::InsertPacket(
88     const VCMPacket& packet,
89     int64_t timeInMs,
90     VCMDecodeErrorMode decode_error_mode,
91     const FrameData& frame_data) {
92   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);
107     }
108   }
109 
110   // add safety margin because STAP-A packets can cause it to expand by
111   // ~two bytes per NAL
112   uint32_t requiredSizeBytes =
113       Length() + packet.sizeBytes +
114       (packet.insertStartCode ? kH264StartCodeLengthBytes : 0) +
115       kBufferSafetyMargin +
116       EncodedImage::GetBufferPaddingBytes(packet.codec);
117   if (requiredSizeBytes >= _size) {
118     const uint8_t* prevBuffer = _buffer;
119     const uint32_t increments =
120         requiredSizeBytes / kBufferIncStepSizeBytes +
121         (requiredSizeBytes % kBufferIncStepSizeBytes > 0);
122     const uint32_t newSize = _size + increments * kBufferIncStepSizeBytes;
123     if (newSize > kMaxJBFrameSizeBytes) {
124       LOG(LS_ERROR) << "Failed to insert packet due to frame being too "
125                        "big.";
126       return kSizeError;
127     }
128     VerifyAndAllocate(newSize);
129     _sessionInfo.UpdateDataPointers(prevBuffer, _buffer);
130   }
131 
132   if (packet.width > 0 && packet.height > 0) {
133     _encodedWidth = packet.width;
134     _encodedHeight = packet.height;
135   }
136 
137   // Don't copy payload specific data for empty packets (e.g padding packets).
138   if (packet.sizeBytes > 0)
139     CopyCodecSpecific(&packet.video_header);
140 
141   int retVal =
142       _sessionInfo.InsertPacket(packet, _buffer, decode_error_mode, frame_data);
143   if (retVal == -1) {
144     return kSizeError;
145   } else if (retVal == -2) {
146     return kDuplicatePacket;
147   } else if (retVal == -3) {
148     return kOutOfBoundsPacket;
149   }
150   // update length
151   _length = Length() + static_cast<uint32_t>(retVal);
152 
153   _latestPacketTimeMs = timeInMs;
154 
155   // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
156   // ts_126114v120700p.pdf Section 7.4.5.
157   // The MTSI client shall add the payload bytes as defined in this clause
158   // onto the last RTP packet in each group of packets which make up a key
159   // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
160   // (HEVC)).
161   if (packet.markerBit) {
162     //RTC_DCHECK(!_rotation_set);
163     rotation_ = packet.video_header.rotation;
164     _rotation_set = true;
165   }
166 
167   if (packet.is_first_packet_in_frame) {
168     playout_delay_ = packet.video_header.playout_delay;
169   }
170 
171   if (_sessionInfo.complete()) {
172     SetState(kStateComplete);
173     return kCompleteSession;
174   } else if (_sessionInfo.decodable()) {
175     SetState(kStateDecodable);
176     return kDecodableSession;
177   }
178   return kIncomplete;
179 }
180 
LatestPacketTimeMs() const181 int64_t VCMFrameBuffer::LatestPacketTimeMs() const {
182   return _latestPacketTimeMs;
183 }
184 
IncrementNackCount()185 void VCMFrameBuffer::IncrementNackCount() {
186   _nackCount++;
187 }
188 
GetNackCount() const189 int16_t VCMFrameBuffer::GetNackCount() const {
190   return _nackCount;
191 }
192 
HaveFirstPacket() const193 bool VCMFrameBuffer::HaveFirstPacket() const {
194   return _sessionInfo.HaveFirstPacket();
195 }
196 
HaveLastPacket() const197 bool VCMFrameBuffer::HaveLastPacket() const {
198   return _sessionInfo.HaveLastPacket();
199 }
200 
NumPackets() const201 int VCMFrameBuffer::NumPackets() const {
202   return _sessionInfo.NumPackets();
203 }
204 
Reset()205 void VCMFrameBuffer::Reset() {
206   _length = 0;
207   _timeStamp = 0;
208   _sessionInfo.Reset();
209   _payloadType = 0;
210   _nackCount = 0;
211   _latestPacketTimeMs = -1;
212   _state = kStateEmpty;
213   VCMEncodedFrame::Reset();
214 }
215 
216 // Set state of frame
SetState(VCMFrameBufferStateEnum state)217 void VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) {
218   if (_state == state) {
219     return;
220   }
221   switch (state) {
222     case kStateIncomplete:
223       // we can go to this state from state kStateEmpty
224       assert(_state == kStateEmpty);
225 
226       // Do nothing, we received a packet
227       break;
228 
229     case kStateComplete:
230       assert(_state == kStateEmpty || _state == kStateIncomplete ||
231              _state == kStateDecodable);
232 
233       break;
234 
235     case kStateEmpty:
236       // Should only be set to empty through Reset().
237       assert(false);
238       break;
239 
240     case kStateDecodable:
241       assert(_state == kStateEmpty || _state == kStateIncomplete);
242       break;
243   }
244   _state = state;
245 }
246 
247 // Get current state of frame
GetState() const248 VCMFrameBufferStateEnum VCMFrameBuffer::GetState() const {
249   return _state;
250 }
251 
252 // Get current state of frame
GetState(uint32_t & timeStamp) const253 VCMFrameBufferStateEnum VCMFrameBuffer::GetState(uint32_t& timeStamp) const {
254   timeStamp = TimeStamp();
255   return GetState();
256 }
257 
IsRetransmitted() const258 bool VCMFrameBuffer::IsRetransmitted() const {
259   return _sessionInfo.session_nack();
260 }
261 
PrepareForDecode(bool continuous)262 void VCMFrameBuffer::PrepareForDecode(bool continuous) {
263   size_t bytes_removed = _sessionInfo.MakeDecodable();
264   _length -= bytes_removed;
265   // Transfer frame information to EncodedFrame and create any codec
266   // specific information.
267   _frameType = _sessionInfo.FrameType();
268   _completeFrame = _sessionInfo.complete();
269   _missingFrame = !continuous;
270 }
271 
272 }  // namespace webrtc
273