philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #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" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace video_coding { |
| 18 | |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 19 | FrameObject::FrameObject() |
| 20 | : picture_id(0), |
| 21 | spatial_layer(0), |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 22 | timestamp(0), |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 23 | num_references(0), |
| 24 | inter_layer_predicted(false) {} |
| 25 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 26 | RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 27 | uint16_t first_seq_num, |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 28 | uint16_t last_seq_num, |
| 29 | size_t frame_size, |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 30 | int times_nacked, |
| 31 | int64_t received_time) |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 32 | : packet_buffer_(packet_buffer), |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 33 | first_seq_num_(first_seq_num), |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 34 | last_seq_num_(last_seq_num), |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 35 | timestamp_(0), |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 36 | received_time_(received_time), |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 37 | times_nacked_(times_nacked) { |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 38 | VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num); |
philipel | 7d79e63 | 2017-05-23 08:19:11 -0700 | [diff] [blame] | 39 | RTC_CHECK(first_packet); |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 40 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 41 | // RtpFrameObject members |
| 42 | frame_type_ = first_packet->frameType; |
| 43 | codec_type_ = first_packet->codec; |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 44 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 45 | // TODO(philipel): Remove when encoded image is replaced by FrameObject. |
| 46 | // VCMEncodedFrame members |
| 47 | CopyCodecSpecific(&first_packet->video_header); |
| 48 | _completeFrame = true; |
| 49 | _payloadType = first_packet->payloadType; |
| 50 | _timeStamp = first_packet->timestamp; |
| 51 | ntp_time_ms_ = first_packet->ntp_time_ms_; |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 52 | |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 53 | // Setting frame's playout delays to the same values |
| 54 | // as of the first packet's. |
| 55 | SetPlayoutDelay(first_packet->video_header.playout_delay); |
| 56 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 57 | // Since FFmpeg use an optimized bitstream reader that reads in chunks of |
| 58 | // 32/64 bits we have to add at least that much padding to the buffer |
| 59 | // to make sure the decoder doesn't read out of bounds. |
| 60 | // NOTE! EncodedImage::_size is the size of the buffer (think capacity of |
| 61 | // an std::vector) and EncodedImage::_length is the actual size of |
| 62 | // the bitstream (think size of an std::vector). |
| 63 | if (codec_type_ == kVideoCodecH264) |
| 64 | _size = frame_size + EncodedImage::kBufferPaddingBytesH264; |
| 65 | else |
| 66 | _size = frame_size; |
philipel | 552866c | 2016-07-04 16:18:55 +0200 | [diff] [blame] | 67 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 68 | _buffer = new uint8_t[_size]; |
| 69 | _length = frame_size; |
philipel | e87c876 | 2017-05-19 06:38:50 -0700 | [diff] [blame] | 70 | |
| 71 | // For H264 frames we can't determine the frame type by just looking at the |
| 72 | // first packet. Instead we consider the frame to be a keyframe if it |
| 73 | // contains an IDR NALU. |
| 74 | if (codec_type_ == kVideoCodecH264) { |
| 75 | _frameType = kVideoFrameDelta; |
| 76 | frame_type_ = kVideoFrameDelta; |
| 77 | for (uint16_t seq_num = first_seq_num; |
philipel | 7d79e63 | 2017-05-23 08:19:11 -0700 | [diff] [blame] | 78 | seq_num != static_cast<uint16_t>(last_seq_num + 1) && |
| 79 | _frameType == kVideoFrameDelta; |
philipel | e87c876 | 2017-05-19 06:38:50 -0700 | [diff] [blame] | 80 | ++seq_num) { |
| 81 | VCMPacket* packet = packet_buffer_->GetPacket(seq_num); |
philipel | 7d79e63 | 2017-05-23 08:19:11 -0700 | [diff] [blame] | 82 | RTC_CHECK(packet); |
philipel | e87c876 | 2017-05-19 06:38:50 -0700 | [diff] [blame] | 83 | const RTPVideoHeaderH264& header = packet->video_header.codecHeader.H264; |
| 84 | for (size_t i = 0; i < header.nalus_length; ++i) { |
| 85 | if (header.nalus[i].type == H264::NaluType::kIdr) { |
| 86 | _frameType = kVideoFrameKey; |
| 87 | frame_type_ = kVideoFrameKey; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } else { |
| 93 | _frameType = first_packet->frameType; |
| 94 | frame_type_ = first_packet->frameType; |
| 95 | } |
| 96 | |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 97 | bool bitstream_copied = GetBitstream(_buffer); |
| 98 | RTC_DCHECK(bitstream_copied); |
philipel | 59e99b7 | 2017-01-12 03:26:04 -0800 | [diff] [blame] | 99 | _encodedWidth = first_packet->width; |
| 100 | _encodedHeight = first_packet->height; |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 101 | |
| 102 | // FrameObject members |
| 103 | timestamp = first_packet->timestamp; |
| 104 | |
| 105 | VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num); |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 106 | RTC_CHECK(last_packet); |
| 107 | RTC_CHECK(last_packet->markerBit); |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 108 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ |
| 109 | // ts_126114v120700p.pdf Section 7.4.5. |
| 110 | // The MTSI client shall add the payload bytes as defined in this clause |
| 111 | // onto the last RTP packet in each group of packets which make up a key |
| 112 | // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265 |
| 113 | // (HEVC)). |
| 114 | rotation_ = last_packet->video_header.rotation; |
| 115 | _rotation_set = true; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 116 | content_type_ = last_packet->video_header.content_type; |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 117 | if (last_packet->video_header.video_timing.flags != |
| 118 | TimingFrameFlags::kInvalid) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 119 | // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem, |
| 120 | // as this will be dealt with at the time of reporting. |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 121 | timing_.encode_start_ms = |
| 122 | ntp_time_ms_ + |
| 123 | last_packet->video_header.video_timing.encode_start_delta_ms; |
| 124 | timing_.encode_finish_ms = |
| 125 | ntp_time_ms_ + |
| 126 | last_packet->video_header.video_timing.encode_finish_delta_ms; |
| 127 | timing_.packetization_finish_ms = |
| 128 | ntp_time_ms_ + |
| 129 | last_packet->video_header.video_timing.packetization_finish_delta_ms; |
| 130 | timing_.pacer_exit_ms = |
| 131 | ntp_time_ms_ + |
| 132 | last_packet->video_header.video_timing.pacer_exit_delta_ms; |
| 133 | timing_.network_timestamp_ms = |
| 134 | ntp_time_ms_ + |
| 135 | last_packet->video_header.video_timing.network_timstamp_delta_ms; |
| 136 | timing_.network2_timestamp_ms = |
| 137 | ntp_time_ms_ + |
| 138 | last_packet->video_header.video_timing.network2_timstamp_delta_ms; |
| 139 | |
| 140 | timing_.receive_start_ms = first_packet->receive_time_ms; |
| 141 | timing_.receive_finish_ms = last_packet->receive_time_ms; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 142 | } |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 143 | timing_.flags = last_packet->video_header.video_timing.flags; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 144 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 145 | |
| 146 | RtpFrameObject::~RtpFrameObject() { |
| 147 | packet_buffer_->ReturnFrame(this); |
| 148 | } |
| 149 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 150 | uint16_t RtpFrameObject::first_seq_num() const { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 151 | return first_seq_num_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 152 | } |
| 153 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 154 | uint16_t RtpFrameObject::last_seq_num() const { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 155 | return last_seq_num_; |
| 156 | } |
| 157 | |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 158 | int RtpFrameObject::times_nacked() const { |
| 159 | return times_nacked_; |
| 160 | } |
| 161 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 162 | FrameType RtpFrameObject::frame_type() const { |
| 163 | return frame_type_; |
| 164 | } |
| 165 | |
| 166 | VideoCodecType RtpFrameObject::codec_type() const { |
| 167 | return codec_type_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 168 | } |
| 169 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 170 | bool RtpFrameObject::GetBitstream(uint8_t* destination) const { |
| 171 | return packet_buffer_->GetBitstream(*this, destination); |
| 172 | } |
| 173 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 174 | uint32_t RtpFrameObject::Timestamp() const { |
| 175 | return timestamp_; |
| 176 | } |
| 177 | |
| 178 | int64_t RtpFrameObject::ReceivedTime() const { |
| 179 | return received_time_; |
| 180 | } |
| 181 | |
| 182 | int64_t RtpFrameObject::RenderTime() const { |
| 183 | return _renderTimeMs; |
| 184 | } |
| 185 | |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 186 | bool RtpFrameObject::delayed_by_retransmission() const { |
| 187 | return times_nacked() > 0; |
| 188 | } |
| 189 | |
philipel | 8848828 | 2016-11-03 08:56:54 -0700 | [diff] [blame] | 190 | rtc::Optional<RTPVideoTypeHeader> RtpFrameObject::GetCodecHeader() const { |
| 191 | rtc::CritScope lock(&packet_buffer_->crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 192 | VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_); |
| 193 | if (!packet) |
philipel | 8848828 | 2016-11-03 08:56:54 -0700 | [diff] [blame] | 194 | return rtc::Optional<RTPVideoTypeHeader>(); |
| 195 | return rtc::Optional<RTPVideoTypeHeader>(packet->video_header.codecHeader); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 196 | } |
| 197 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 198 | } // namespace video_coding |
| 199 | } // namespace webrtc |