blob: 4becdb76085583bd2799768d65c766e562b55ef2 [file] [log] [blame]
philipel34852cf2016-11-03 04:03:01 -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/h264_sps_pps_tracker.h"
philipel34852cf2016-11-03 04:03:01 -070012
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010013#include <memory>
philipel34852cf2016-11-03 04:03:01 -070014#include <string>
philipel022b54e2016-12-20 04:15:59 -080015#include <utility>
philipel34852cf2016-11-03 04:03:01 -070016
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010017#include "absl/types/variant.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_video/h264/h264_common.h"
19#include "common_video/h264/pps_parser.h"
20#include "common_video/h264/sps_parser.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010021#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
philipel34852cf2016-11-03 04:03:01 -070024
25namespace webrtc {
26namespace video_coding {
27
28namespace {
29const uint8_t start_code_h264[] = {0, 0, 0, 1};
30} // namespace
31
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020032H264SpsPpsTracker::H264SpsPpsTracker() = default;
33H264SpsPpsTracker::~H264SpsPpsTracker() = default;
34
35H264SpsPpsTracker::PpsInfo::PpsInfo() = default;
36H264SpsPpsTracker::PpsInfo::PpsInfo(PpsInfo&& rhs) = default;
37H264SpsPpsTracker::PpsInfo& H264SpsPpsTracker::PpsInfo::operator=(
38 PpsInfo&& rhs) = default;
39H264SpsPpsTracker::PpsInfo::~PpsInfo() = default;
40
41H264SpsPpsTracker::SpsInfo::SpsInfo() = default;
42H264SpsPpsTracker::SpsInfo::SpsInfo(SpsInfo&& rhs) = default;
43H264SpsPpsTracker::SpsInfo& H264SpsPpsTracker::SpsInfo::operator=(
44 SpsInfo&& rhs) = default;
45H264SpsPpsTracker::SpsInfo::~SpsInfo() = default;
46
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010047H264SpsPpsTracker::FixedBitstream H264SpsPpsTracker::CopyAndFixBitstream(
48 rtc::ArrayView<const uint8_t> bitstream,
49 RTPVideoHeader* video_header) {
50 RTC_DCHECK(video_header);
51 RTC_DCHECK(video_header->codec == kVideoCodecH264);
Chema Gonzalezafdbf8e2020-06-24 10:05:22 -070052 RTC_DCHECK_GT(bitstream.size(), 0);
philipel34852cf2016-11-03 04:03:01 -070053
philipel7d745e52018-08-02 14:03:53 +020054 auto& h264_header =
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010055 absl::get<RTPVideoHeaderH264>(video_header->video_type_header);
philipel34852cf2016-11-03 04:03:01 -070056
philipel83c97da2017-06-21 07:22:40 -070057 bool append_sps_pps = false;
58 auto sps = sps_data_.end();
59 auto pps = pps_data_.end();
60
philipel7d745e52018-08-02 14:03:53 +020061 for (size_t i = 0; i < h264_header.nalus_length; ++i) {
62 const NaluInfo& nalu = h264_header.nalus[i];
philipel34852cf2016-11-03 04:03:01 -070063 switch (nalu.type) {
64 case H264::NaluType::kSps: {
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010065 SpsInfo& sps_info = sps_data_[nalu.sps_id];
66 sps_info.width = video_header->width;
67 sps_info.height = video_header->height;
philipel34852cf2016-11-03 04:03:01 -070068 break;
69 }
70 case H264::NaluType::kPps: {
philipel34852cf2016-11-03 04:03:01 -070071 pps_data_[nalu.pps_id].sps_id = nalu.sps_id;
philipel34852cf2016-11-03 04:03:01 -070072 break;
73 }
74 case H264::NaluType::kIdr: {
75 // If this is the first packet of an IDR, make sure we have the required
76 // SPS/PPS and also calculate how much extra space we need in the buffer
77 // to prepend the SPS/PPS to the bitstream with start codes.
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010078 if (video_header->is_first_packet_in_frame) {
philipel34852cf2016-11-03 04:03:01 -070079 if (nalu.pps_id == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010080 RTC_LOG(LS_WARNING) << "No PPS id in IDR nalu.";
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010081 return {kRequestKeyframe};
philipel34852cf2016-11-03 04:03:01 -070082 }
83
philipel83c97da2017-06-21 07:22:40 -070084 pps = pps_data_.find(nalu.pps_id);
philipel34852cf2016-11-03 04:03:01 -070085 if (pps == pps_data_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(LS_WARNING)
87 << "No PPS with id << " << nalu.pps_id << " received";
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010088 return {kRequestKeyframe};
philipel34852cf2016-11-03 04:03:01 -070089 }
90
philipel83c97da2017-06-21 07:22:40 -070091 sps = sps_data_.find(pps->second.sps_id);
philipel34852cf2016-11-03 04:03:01 -070092 if (sps == sps_data_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010093 RTC_LOG(LS_WARNING)
philipel83c97da2017-06-21 07:22:40 -070094 << "No SPS with id << " << pps->second.sps_id << " received";
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +010095 return {kRequestKeyframe};
philipel34852cf2016-11-03 04:03:01 -070096 }
97
philipel83c97da2017-06-21 07:22:40 -070098 // Since the first packet of every keyframe should have its width and
99 // height set we set it here in the case of it being supplied out of
100 // band.
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100101 video_header->width = sps->second.width;
102 video_header->height = sps->second.height;
philipel83c97da2017-06-21 07:22:40 -0700103
104 // If the SPS/PPS was supplied out of band then we will have saved
105 // the actual bitstream in |data|.
106 if (sps->second.data && pps->second.data) {
107 RTC_DCHECK_GT(sps->second.size, 0);
108 RTC_DCHECK_GT(pps->second.size, 0);
109 append_sps_pps = true;
110 }
philipel34852cf2016-11-03 04:03:01 -0700111 }
philipel83c97da2017-06-21 07:22:40 -0700112 break;
philipel34852cf2016-11-03 04:03:01 -0700113 }
philipel83c97da2017-06-21 07:22:40 -0700114 default:
115 break;
philipel34852cf2016-11-03 04:03:01 -0700116 }
117 }
118
philipel83c97da2017-06-21 07:22:40 -0700119 RTC_CHECK(!append_sps_pps ||
120 (sps != sps_data_.end() && pps != pps_data_.end()));
philipel34852cf2016-11-03 04:03:01 -0700121
122 // Calculate how much space we need for the rest of the bitstream.
philipel83c97da2017-06-21 07:22:40 -0700123 size_t required_size = 0;
124
125 if (append_sps_pps) {
126 required_size += sps->second.size + sizeof(start_code_h264);
127 required_size += pps->second.size + sizeof(start_code_h264);
128 }
129
philipel7d745e52018-08-02 14:03:53 +0200130 if (h264_header.packetization_type == kH264StapA) {
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100131 const uint8_t* nalu_ptr = bitstream.data() + 1;
Chema Gonzalezafdbf8e2020-06-24 10:05:22 -0700132 while (nalu_ptr < bitstream.data() + bitstream.size() - 1) {
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100133 RTC_DCHECK(video_header->is_first_packet_in_frame);
philipel34852cf2016-11-03 04:03:01 -0700134 required_size += sizeof(start_code_h264);
135
136 // The first two bytes describe the length of a segment.
137 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1];
138 nalu_ptr += 2;
139
140 required_size += segment_length;
141 nalu_ptr += segment_length;
142 }
143 } else {
philipel2c850af2019-07-12 15:32:17 +0200144 if (h264_header.nalus_length > 0) {
philipel34852cf2016-11-03 04:03:01 -0700145 required_size += sizeof(start_code_h264);
philipel5e953d72019-06-14 13:40:04 +0200146 }
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100147 required_size += bitstream.size();
philipel34852cf2016-11-03 04:03:01 -0700148 }
149
150 // Then we copy to the new buffer.
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100151 H264SpsPpsTracker::FixedBitstream fixed;
Danil Chapovalove3c48842019-12-02 15:54:27 +0100152 fixed.bitstream.EnsureCapacity(required_size);
philipel34852cf2016-11-03 04:03:01 -0700153
philipel83c97da2017-06-21 07:22:40 -0700154 if (append_sps_pps) {
philipel34852cf2016-11-03 04:03:01 -0700155 // Insert SPS.
Danil Chapovalove3c48842019-12-02 15:54:27 +0100156 fixed.bitstream.AppendData(start_code_h264);
157 fixed.bitstream.AppendData(sps->second.data.get(), sps->second.size);
philipel34852cf2016-11-03 04:03:01 -0700158
159 // Insert PPS.
Danil Chapovalove3c48842019-12-02 15:54:27 +0100160 fixed.bitstream.AppendData(start_code_h264);
161 fixed.bitstream.AppendData(pps->second.data.get(), pps->second.size);
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100162
163 // Update codec header to reflect the newly added SPS and PPS.
164 NaluInfo sps_info;
165 sps_info.type = H264::NaluType::kSps;
166 sps_info.sps_id = sps->first;
167 sps_info.pps_id = -1;
168 NaluInfo pps_info;
169 pps_info.type = H264::NaluType::kPps;
170 pps_info.sps_id = sps->first;
171 pps_info.pps_id = pps->first;
philipel7d745e52018-08-02 14:03:53 +0200172 if (h264_header.nalus_length + 2 <= kMaxNalusPerPacket) {
173 h264_header.nalus[h264_header.nalus_length++] = sps_info;
174 h264_header.nalus[h264_header.nalus_length++] = pps_info;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100175 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100176 RTC_LOG(LS_WARNING) << "Not enough space in H.264 codec header to insert "
177 "SPS/PPS provided out-of-band.";
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100178 }
philipel34852cf2016-11-03 04:03:01 -0700179 }
180
181 // Copy the rest of the bitstream and insert start codes.
philipel7d745e52018-08-02 14:03:53 +0200182 if (h264_header.packetization_type == kH264StapA) {
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100183 const uint8_t* nalu_ptr = bitstream.data() + 1;
Chema Gonzalezafdbf8e2020-06-24 10:05:22 -0700184 while (nalu_ptr < bitstream.data() + bitstream.size() - 1) {
Danil Chapovalove3c48842019-12-02 15:54:27 +0100185 fixed.bitstream.AppendData(start_code_h264);
philipel34852cf2016-11-03 04:03:01 -0700186
187 // The first two bytes describe the length of a segment.
188 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1];
189 nalu_ptr += 2;
190
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100191 size_t copy_end = nalu_ptr - bitstream.data() + segment_length;
192 if (copy_end > bitstream.size()) {
193 return {kDrop};
philipel34852cf2016-11-03 04:03:01 -0700194 }
195
Danil Chapovalove3c48842019-12-02 15:54:27 +0100196 fixed.bitstream.AppendData(nalu_ptr, segment_length);
philipel34852cf2016-11-03 04:03:01 -0700197 nalu_ptr += segment_length;
198 }
199 } else {
philipel2c850af2019-07-12 15:32:17 +0200200 if (h264_header.nalus_length > 0) {
Danil Chapovalove3c48842019-12-02 15:54:27 +0100201 fixed.bitstream.AppendData(start_code_h264);
philipel34852cf2016-11-03 04:03:01 -0700202 }
Danil Chapovalove3c48842019-12-02 15:54:27 +0100203 fixed.bitstream.AppendData(bitstream.data(), bitstream.size());
philipel34852cf2016-11-03 04:03:01 -0700204 }
205
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100206 fixed.action = kInsert;
207 return fixed;
philipel34852cf2016-11-03 04:03:01 -0700208}
209
johand2b092f2017-01-24 02:38:17 -0800210void H264SpsPpsTracker::InsertSpsPpsNalus(const std::vector<uint8_t>& sps,
211 const std::vector<uint8_t>& pps) {
212 constexpr size_t kNaluHeaderOffset = 1;
213 if (sps.size() < kNaluHeaderOffset) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100214 RTC_LOG(LS_WARNING) << "SPS size " << sps.size() << " is smaller than "
215 << kNaluHeaderOffset;
johand2b092f2017-01-24 02:38:17 -0800216 return;
217 }
218 if ((sps[0] & 0x1f) != H264::NaluType::kSps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100219 RTC_LOG(LS_WARNING) << "SPS Nalu header missing";
johand2b092f2017-01-24 02:38:17 -0800220 return;
221 }
222 if (pps.size() < kNaluHeaderOffset) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100223 RTC_LOG(LS_WARNING) << "PPS size " << pps.size() << " is smaller than "
224 << kNaluHeaderOffset;
johand2b092f2017-01-24 02:38:17 -0800225 return;
226 }
227 if ((pps[0] & 0x1f) != H264::NaluType::kPps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100228 RTC_LOG(LS_WARNING) << "SPS Nalu header missing";
johand2b092f2017-01-24 02:38:17 -0800229 return;
230 }
Danil Chapovalov0040b662018-06-18 10:48:16 +0200231 absl::optional<SpsParser::SpsState> parsed_sps = SpsParser::ParseSps(
johand2b092f2017-01-24 02:38:17 -0800232 sps.data() + kNaluHeaderOffset, sps.size() - kNaluHeaderOffset);
Danil Chapovalov0040b662018-06-18 10:48:16 +0200233 absl::optional<PpsParser::PpsState> parsed_pps = PpsParser::ParsePps(
johand2b092f2017-01-24 02:38:17 -0800234 pps.data() + kNaluHeaderOffset, pps.size() - kNaluHeaderOffset);
235
236 if (!parsed_sps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100237 RTC_LOG(LS_WARNING) << "Failed to parse SPS.";
johand2b092f2017-01-24 02:38:17 -0800238 }
239
240 if (!parsed_pps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100241 RTC_LOG(LS_WARNING) << "Failed to parse PPS.";
johand2b092f2017-01-24 02:38:17 -0800242 }
philipel022b54e2016-12-20 04:15:59 -0800243
244 if (!parsed_pps || !parsed_sps) {
philipel022b54e2016-12-20 04:15:59 -0800245 return;
246 }
247
248 SpsInfo sps_info;
249 sps_info.size = sps.size();
philipel6585f702017-03-17 06:12:33 -0700250 sps_info.width = parsed_sps->width;
251 sps_info.height = parsed_sps->height;
philipel022b54e2016-12-20 04:15:59 -0800252 uint8_t* sps_data = new uint8_t[sps_info.size];
253 memcpy(sps_data, sps.data(), sps_info.size);
254 sps_info.data.reset(sps_data);
255 sps_data_[parsed_sps->id] = std::move(sps_info);
256
257 PpsInfo pps_info;
258 pps_info.size = pps.size();
259 pps_info.sps_id = parsed_pps->sps_id;
260 uint8_t* pps_data = new uint8_t[pps_info.size];
261 memcpy(pps_data, pps.data(), pps_info.size);
262 pps_info.data.reset(pps_data);
263 pps_data_[parsed_pps->id] = std::move(pps_info);
johand2b092f2017-01-24 02:38:17 -0800264
Mirko Bonadei675513b2017-11-09 11:09:25 +0100265 RTC_LOG(LS_INFO) << "Inserted SPS id " << parsed_sps->id << " and PPS id "
266 << parsed_pps->id << " (referencing SPS "
267 << parsed_pps->sps_id << ")";
philipel022b54e2016-12-20 04:15:59 -0800268}
269
philipel34852cf2016-11-03 04:03:01 -0700270} // namespace video_coding
271} // namespace webrtc