blob: 25d0e29d218c1538c89637e783d402993bf18020 [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
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020011#include "webrtc/modules/video_coding/frame_object.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020012#include "webrtc/common_video/h264/h264_common.h"
philipelc707ab72016-04-01 02:01:54 -070013#include "webrtc/modules/video_coding/packet_buffer.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020014#include "webrtc/rtc_base/checks.h"
philipelc707ab72016-04-01 02:01:54 -070015
16namespace webrtc {
17namespace video_coding {
18
philipela1059872016-05-09 11:41:48 +020019FrameObject::FrameObject()
20 : picture_id(0),
21 spatial_layer(0),
philipelbe7a9e52016-05-19 12:19:35 +020022 timestamp(0),
philipela1059872016-05-09 11:41:48 +020023 num_references(0),
24 inter_layer_predicted(false) {}
25
philipelc707ab72016-04-01 02:01:54 -070026RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer,
philipel02447bc2016-05-13 06:01:03 -070027 uint16_t first_seq_num,
philipel5ceaaae2016-05-24 10:20:47 +020028 uint16_t last_seq_num,
29 size_t frame_size,
philipelb4d31082016-07-11 08:46:29 -070030 int times_nacked,
31 int64_t received_time)
philipelc707ab72016-04-01 02:01:54 -070032 : packet_buffer_(packet_buffer),
philipel02447bc2016-05-13 06:01:03 -070033 first_seq_num_(first_seq_num),
philipel5ceaaae2016-05-24 10:20:47 +020034 last_seq_num_(last_seq_num),
philipelb4d31082016-07-11 08:46:29 -070035 received_time_(received_time),
philipel5ceaaae2016-05-24 10:20:47 +020036 times_nacked_(times_nacked) {
philipel266f0a42016-11-28 08:49:07 -080037 VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num);
philipel7d79e632017-05-23 08:19:11 -070038 RTC_CHECK(first_packet);
philipel36928452016-11-07 10:42:36 +010039
philipel266f0a42016-11-28 08:49:07 -080040 // RtpFrameObject members
41 frame_type_ = first_packet->frameType;
42 codec_type_ = first_packet->codec;
philipel36928452016-11-07 10:42:36 +010043
philipel266f0a42016-11-28 08:49:07 -080044 // TODO(philipel): Remove when encoded image is replaced by FrameObject.
45 // VCMEncodedFrame members
46 CopyCodecSpecific(&first_packet->video_header);
47 _completeFrame = true;
48 _payloadType = first_packet->payloadType;
49 _timeStamp = first_packet->timestamp;
50 ntp_time_ms_ = first_packet->ntp_time_ms_;
philipel36928452016-11-07 10:42:36 +010051
gnishb2a318b2017-05-10 09:21:33 -070052 // Setting frame's playout delays to the same values
53 // as of the first packet's.
54 SetPlayoutDelay(first_packet->video_header.playout_delay);
55
philipel266f0a42016-11-28 08:49:07 -080056 // Since FFmpeg use an optimized bitstream reader that reads in chunks of
57 // 32/64 bits we have to add at least that much padding to the buffer
58 // to make sure the decoder doesn't read out of bounds.
59 // NOTE! EncodedImage::_size is the size of the buffer (think capacity of
60 // an std::vector) and EncodedImage::_length is the actual size of
61 // the bitstream (think size of an std::vector).
62 if (codec_type_ == kVideoCodecH264)
63 _size = frame_size + EncodedImage::kBufferPaddingBytesH264;
64 else
65 _size = frame_size;
philipel552866c2016-07-04 16:18:55 +020066
philipel266f0a42016-11-28 08:49:07 -080067 _buffer = new uint8_t[_size];
68 _length = frame_size;
philipele87c8762017-05-19 06:38:50 -070069
70 // For H264 frames we can't determine the frame type by just looking at the
71 // first packet. Instead we consider the frame to be a keyframe if it
72 // contains an IDR NALU.
73 if (codec_type_ == kVideoCodecH264) {
74 _frameType = kVideoFrameDelta;
75 frame_type_ = kVideoFrameDelta;
76 for (uint16_t seq_num = first_seq_num;
philipel7d79e632017-05-23 08:19:11 -070077 seq_num != static_cast<uint16_t>(last_seq_num + 1) &&
78 _frameType == kVideoFrameDelta;
philipele87c8762017-05-19 06:38:50 -070079 ++seq_num) {
80 VCMPacket* packet = packet_buffer_->GetPacket(seq_num);
philipel7d79e632017-05-23 08:19:11 -070081 RTC_CHECK(packet);
philipele87c8762017-05-19 06:38:50 -070082 const RTPVideoHeaderH264& header = packet->video_header.codecHeader.H264;
83 for (size_t i = 0; i < header.nalus_length; ++i) {
84 if (header.nalus[i].type == H264::NaluType::kIdr) {
85 _frameType = kVideoFrameKey;
86 frame_type_ = kVideoFrameKey;
87 break;
88 }
89 }
90 }
91 } else {
92 _frameType = first_packet->frameType;
93 frame_type_ = first_packet->frameType;
94 }
95
philipel227f8b92017-08-04 06:39:31 -070096 bool bitstream_copied = GetBitstream(_buffer);
97 RTC_DCHECK(bitstream_copied);
philipel59e99b72017-01-12 03:26:04 -080098 _encodedWidth = first_packet->width;
99 _encodedHeight = first_packet->height;
philipel266f0a42016-11-28 08:49:07 -0800100
101 // FrameObject members
102 timestamp = first_packet->timestamp;
103
104 VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num);
philipel7d79e632017-05-23 08:19:11 -0700105 RTC_CHECK(last_packet && last_packet->markerBit);
philipel266f0a42016-11-28 08:49:07 -0800106 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
107 // ts_126114v120700p.pdf Section 7.4.5.
108 // The MTSI client shall add the payload bytes as defined in this clause
109 // onto the last RTP packet in each group of packets which make up a key
110 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
111 // (HEVC)).
112 rotation_ = last_packet->video_header.rotation;
113 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -0700114 content_type_ = last_packet->video_header.content_type;
ilnik04f4d122017-06-19 07:18:55 -0700115 if (last_packet->video_header.video_timing.is_timing_frame) {
116 // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem,
117 // as this will be dealt with at the time of reporting.
118 timing_.is_timing_frame = true;
119 timing_.encode_start_ms =
120 ntp_time_ms_ +
121 last_packet->video_header.video_timing.encode_start_delta_ms;
122 timing_.encode_finish_ms =
123 ntp_time_ms_ +
124 last_packet->video_header.video_timing.encode_finish_delta_ms;
125 timing_.packetization_finish_ms =
126 ntp_time_ms_ +
127 last_packet->video_header.video_timing.packetization_finish_delta_ms;
128 timing_.pacer_exit_ms =
129 ntp_time_ms_ +
130 last_packet->video_header.video_timing.pacer_exit_delta_ms;
131 timing_.network_timestamp_ms =
132 ntp_time_ms_ +
133 last_packet->video_header.video_timing.network_timstamp_delta_ms;
134 timing_.network2_timestamp_ms =
135 ntp_time_ms_ +
136 last_packet->video_header.video_timing.network2_timstamp_delta_ms;
137
138 timing_.receive_start_ms = first_packet->receive_time_ms;
139 timing_.receive_finish_ms = last_packet->receive_time_ms;
140 } else {
141 timing_.is_timing_frame = false;
142 }
philipel02447bc2016-05-13 06:01:03 -0700143}
philipelc707ab72016-04-01 02:01:54 -0700144
145RtpFrameObject::~RtpFrameObject() {
146 packet_buffer_->ReturnFrame(this);
147}
148
philipelf4139332016-04-20 10:26:34 +0200149uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700150 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700151}
152
philipelf4139332016-04-20 10:26:34 +0200153uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700154 return last_seq_num_;
155}
156
philipel5ceaaae2016-05-24 10:20:47 +0200157int RtpFrameObject::times_nacked() const {
158 return times_nacked_;
159}
160
philipel02447bc2016-05-13 06:01:03 -0700161FrameType RtpFrameObject::frame_type() const {
162 return frame_type_;
163}
164
165VideoCodecType RtpFrameObject::codec_type() const {
166 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700167}
168
philipelc707ab72016-04-01 02:01:54 -0700169bool RtpFrameObject::GetBitstream(uint8_t* destination) const {
170 return packet_buffer_->GetBitstream(*this, destination);
171}
172
philipelb4d31082016-07-11 08:46:29 -0700173uint32_t RtpFrameObject::Timestamp() const {
174 return timestamp_;
175}
176
177int64_t RtpFrameObject::ReceivedTime() const {
178 return received_time_;
179}
180
181int64_t RtpFrameObject::RenderTime() const {
182 return _renderTimeMs;
183}
184
philipele0754302017-01-25 08:56:23 -0800185bool RtpFrameObject::delayed_by_retransmission() const {
186 return times_nacked() > 0;
187}
188
philipel88488282016-11-03 08:56:54 -0700189rtc::Optional<RTPVideoTypeHeader> RtpFrameObject::GetCodecHeader() const {
190 rtc::CritScope lock(&packet_buffer_->crit_);
philipel02447bc2016-05-13 06:01:03 -0700191 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
192 if (!packet)
philipel88488282016-11-03 08:56:54 -0700193 return rtc::Optional<RTPVideoTypeHeader>();
194 return rtc::Optional<RTPVideoTypeHeader>(packet->video_header.codecHeader);
philipel02447bc2016-05-13 06:01:03 -0700195}
196
philipelc707ab72016-04-01 02:01:54 -0700197} // namespace video_coding
198} // namespace webrtc