blob: 7c9c41b72f71deb35018500eaaa687dc886ecfac [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
2 * Copyright (c) 2016 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/frame_object.h"
12#include "common_video/h264/h264_common.h"
13#include "modules/video_coding/packet_buffer.h"
14#include "rtc_base/checks.h"
Rasmus Brandtedf4ff72017-10-24 10:07:48 +020015#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070016
17namespace webrtc {
18namespace video_coding {
19
philipela1059872016-05-09 11:41:48 +020020FrameObject::FrameObject()
21 : picture_id(0),
22 spatial_layer(0),
philipelbe7a9e52016-05-19 12:19:35 +020023 timestamp(0),
philipela1059872016-05-09 11:41:48 +020024 num_references(0),
25 inter_layer_predicted(false) {}
26
philipelc707ab72016-04-01 02:01:54 -070027RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer,
philipel02447bc2016-05-13 06:01:03 -070028 uint16_t first_seq_num,
philipel5ceaaae2016-05-24 10:20:47 +020029 uint16_t last_seq_num,
30 size_t frame_size,
philipelb4d31082016-07-11 08:46:29 -070031 int times_nacked,
32 int64_t received_time)
philipelc707ab72016-04-01 02:01:54 -070033 : packet_buffer_(packet_buffer),
philipel02447bc2016-05-13 06:01:03 -070034 first_seq_num_(first_seq_num),
philipel5ceaaae2016-05-24 10:20:47 +020035 last_seq_num_(last_seq_num),
sprangba050a62017-08-18 02:51:12 -070036 timestamp_(0),
philipelb4d31082016-07-11 08:46:29 -070037 received_time_(received_time),
philipel5ceaaae2016-05-24 10:20:47 +020038 times_nacked_(times_nacked) {
philipel266f0a42016-11-28 08:49:07 -080039 VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num);
philipel7d79e632017-05-23 08:19:11 -070040 RTC_CHECK(first_packet);
philipel36928452016-11-07 10:42:36 +010041
philipel266f0a42016-11-28 08:49:07 -080042 // RtpFrameObject members
43 frame_type_ = first_packet->frameType;
44 codec_type_ = first_packet->codec;
philipel36928452016-11-07 10:42:36 +010045
philipel266f0a42016-11-28 08:49:07 -080046 // TODO(philipel): Remove when encoded image is replaced by FrameObject.
47 // VCMEncodedFrame members
48 CopyCodecSpecific(&first_packet->video_header);
49 _completeFrame = true;
50 _payloadType = first_packet->payloadType;
51 _timeStamp = first_packet->timestamp;
52 ntp_time_ms_ = first_packet->ntp_time_ms_;
philipel36928452016-11-07 10:42:36 +010053
gnishb2a318b2017-05-10 09:21:33 -070054 // Setting frame's playout delays to the same values
55 // as of the first packet's.
56 SetPlayoutDelay(first_packet->video_header.playout_delay);
57
philipel266f0a42016-11-28 08:49:07 -080058 // Since FFmpeg use an optimized bitstream reader that reads in chunks of
59 // 32/64 bits we have to add at least that much padding to the buffer
60 // to make sure the decoder doesn't read out of bounds.
61 // NOTE! EncodedImage::_size is the size of the buffer (think capacity of
62 // an std::vector) and EncodedImage::_length is the actual size of
63 // the bitstream (think size of an std::vector).
64 if (codec_type_ == kVideoCodecH264)
65 _size = frame_size + EncodedImage::kBufferPaddingBytesH264;
66 else
67 _size = frame_size;
philipel552866c2016-07-04 16:18:55 +020068
philipel266f0a42016-11-28 08:49:07 -080069 _buffer = new uint8_t[_size];
70 _length = frame_size;
philipele87c8762017-05-19 06:38:50 -070071
72 // For H264 frames we can't determine the frame type by just looking at the
Rasmus Brandtedf4ff72017-10-24 10:07:48 +020073 // first packet. Instead we consider the frame to be a keyframe if it contains
74 // an IDR, and SPS/PPS if the field trial is set.
philipele87c8762017-05-19 06:38:50 -070075 if (codec_type_ == kVideoCodecH264) {
76 _frameType = kVideoFrameDelta;
77 frame_type_ = kVideoFrameDelta;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +020078 bool contains_sps = false;
79 bool contains_pps = false;
80 bool contains_idr = false;
philipele87c8762017-05-19 06:38:50 -070081 for (uint16_t seq_num = first_seq_num;
philipel7d79e632017-05-23 08:19:11 -070082 seq_num != static_cast<uint16_t>(last_seq_num + 1) &&
83 _frameType == kVideoFrameDelta;
philipele87c8762017-05-19 06:38:50 -070084 ++seq_num) {
85 VCMPacket* packet = packet_buffer_->GetPacket(seq_num);
philipel7d79e632017-05-23 08:19:11 -070086 RTC_CHECK(packet);
philipele87c8762017-05-19 06:38:50 -070087 const RTPVideoHeaderH264& header = packet->video_header.codecHeader.H264;
88 for (size_t i = 0; i < header.nalus_length; ++i) {
Rasmus Brandtedf4ff72017-10-24 10:07:48 +020089 if (header.nalus[i].type == H264::NaluType::kSps) {
90 contains_sps = true;
91 } else if (header.nalus[i].type == H264::NaluType::kPps) {
92 contains_pps = true;
93 } else if (header.nalus[i].type == H264::NaluType::kIdr) {
94 contains_idr = true;
philipele87c8762017-05-19 06:38:50 -070095 }
96 }
97 }
Rasmus Brandtedf4ff72017-10-24 10:07:48 +020098 const bool sps_pps_idr_is_keyframe =
99 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe");
100 if ((sps_pps_idr_is_keyframe && contains_idr && contains_sps &&
101 contains_pps) ||
102 (!sps_pps_idr_is_keyframe && contains_idr)) {
103 _frameType = kVideoFrameKey;
104 frame_type_ = kVideoFrameKey;
105 }
philipele87c8762017-05-19 06:38:50 -0700106 } else {
107 _frameType = first_packet->frameType;
108 frame_type_ = first_packet->frameType;
109 }
110
philipel227f8b92017-08-04 06:39:31 -0700111 bool bitstream_copied = GetBitstream(_buffer);
112 RTC_DCHECK(bitstream_copied);
philipel59e99b72017-01-12 03:26:04 -0800113 _encodedWidth = first_packet->width;
114 _encodedHeight = first_packet->height;
philipel266f0a42016-11-28 08:49:07 -0800115
116 // FrameObject members
117 timestamp = first_packet->timestamp;
118
119 VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num);
kwibergee89e782017-08-09 17:22:01 -0700120 RTC_CHECK(last_packet);
121 RTC_CHECK(last_packet->markerBit);
philipel266f0a42016-11-28 08:49:07 -0800122 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
123 // ts_126114v120700p.pdf Section 7.4.5.
124 // The MTSI client shall add the payload bytes as defined in this clause
125 // onto the last RTP packet in each group of packets which make up a key
126 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
127 // (HEVC)).
128 rotation_ = last_packet->video_header.rotation;
129 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -0700130 content_type_ = last_packet->video_header.content_type;
sprangba050a62017-08-18 02:51:12 -0700131 if (last_packet->video_header.video_timing.flags !=
132 TimingFrameFlags::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -0700133 // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem,
134 // as this will be dealt with at the time of reporting.
ilnik04f4d122017-06-19 07:18:55 -0700135 timing_.encode_start_ms =
136 ntp_time_ms_ +
137 last_packet->video_header.video_timing.encode_start_delta_ms;
138 timing_.encode_finish_ms =
139 ntp_time_ms_ +
140 last_packet->video_header.video_timing.encode_finish_delta_ms;
141 timing_.packetization_finish_ms =
142 ntp_time_ms_ +
143 last_packet->video_header.video_timing.packetization_finish_delta_ms;
144 timing_.pacer_exit_ms =
145 ntp_time_ms_ +
146 last_packet->video_header.video_timing.pacer_exit_delta_ms;
147 timing_.network_timestamp_ms =
148 ntp_time_ms_ +
149 last_packet->video_header.video_timing.network_timstamp_delta_ms;
150 timing_.network2_timestamp_ms =
151 ntp_time_ms_ +
152 last_packet->video_header.video_timing.network2_timstamp_delta_ms;
153
154 timing_.receive_start_ms = first_packet->receive_time_ms;
155 timing_.receive_finish_ms = last_packet->receive_time_ms;
ilnik04f4d122017-06-19 07:18:55 -0700156 }
sprangba050a62017-08-18 02:51:12 -0700157 timing_.flags = last_packet->video_header.video_timing.flags;
philipel02447bc2016-05-13 06:01:03 -0700158}
philipelc707ab72016-04-01 02:01:54 -0700159
160RtpFrameObject::~RtpFrameObject() {
161 packet_buffer_->ReturnFrame(this);
162}
163
philipelf4139332016-04-20 10:26:34 +0200164uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700165 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700166}
167
philipelf4139332016-04-20 10:26:34 +0200168uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700169 return last_seq_num_;
170}
171
philipel5ceaaae2016-05-24 10:20:47 +0200172int RtpFrameObject::times_nacked() const {
173 return times_nacked_;
174}
175
philipel02447bc2016-05-13 06:01:03 -0700176FrameType RtpFrameObject::frame_type() const {
177 return frame_type_;
178}
179
180VideoCodecType RtpFrameObject::codec_type() const {
181 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700182}
183
philipelc707ab72016-04-01 02:01:54 -0700184bool RtpFrameObject::GetBitstream(uint8_t* destination) const {
185 return packet_buffer_->GetBitstream(*this, destination);
186}
187
philipelb4d31082016-07-11 08:46:29 -0700188uint32_t RtpFrameObject::Timestamp() const {
189 return timestamp_;
190}
191
192int64_t RtpFrameObject::ReceivedTime() const {
193 return received_time_;
194}
195
196int64_t RtpFrameObject::RenderTime() const {
197 return _renderTimeMs;
198}
199
philipele0754302017-01-25 08:56:23 -0800200bool RtpFrameObject::delayed_by_retransmission() const {
201 return times_nacked() > 0;
202}
203
philipel88488282016-11-03 08:56:54 -0700204rtc::Optional<RTPVideoTypeHeader> RtpFrameObject::GetCodecHeader() const {
205 rtc::CritScope lock(&packet_buffer_->crit_);
philipel02447bc2016-05-13 06:01:03 -0700206 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
207 if (!packet)
philipel88488282016-11-03 08:56:54 -0700208 return rtc::Optional<RTPVideoTypeHeader>();
209 return rtc::Optional<RTPVideoTypeHeader>(packet->video_header.codecHeader);
philipel02447bc2016-05-13 06:01:03 -0700210}
211
philipelc707ab72016-04-01 02:01:54 -0700212} // namespace video_coding
213} // namespace webrtc