blob: 1122364644bd4f22eef2f9c8734fb0e30a0ed0bc [file] [log] [blame]
philipel34852cf2016-11-03 11:03:011/*
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 04:47:3111#include "modules/video_coding/h264_sps_pps_tracker.h"
philipel34852cf2016-11-03 11:03:0112
13#include <string>
philipel022b54e2016-12-20 12:15:5914#include <utility>
philipel34852cf2016-11-03 11:03:0115
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "common_video/h264/h264_common.h"
17#include "common_video/h264/pps_parser.h"
18#include "common_video/h264/sps_parser.h"
Rasmus Brandt88f080a2017-11-02 13:28:0619#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "modules/video_coding/frame_object.h"
21#include "modules/video_coding/packet_buffer.h"
22#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
philipel34852cf2016-11-03 11:03:0124
25namespace webrtc {
26namespace video_coding {
27
28namespace {
29const uint8_t start_code_h264[] = {0, 0, 0, 1};
30} // namespace
31
Mirko Bonadei8fdcac32018-08-28 14:30:1832H264SpsPpsTracker::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
philipela75d12d2016-11-07 13:11:2847H264SpsPpsTracker::PacketAction H264SpsPpsTracker::CopyAndFixBitstream(
48 VCMPacket* packet) {
philipel34852cf2016-11-03 11:03:0149 RTC_DCHECK(packet->codec == kVideoCodecH264);
50
51 const uint8_t* data = packet->dataPtr;
52 const size_t data_size = packet->sizeBytes;
53 const RTPVideoHeader& video_header = packet->video_header;
philipel7d745e52018-08-02 12:03:5354 auto& h264_header =
55 absl::get<RTPVideoHeaderH264>(packet->video_header.video_type_header);
philipel34852cf2016-11-03 11:03:0156
philipel83c97da2017-06-21 14:22:4057 bool append_sps_pps = false;
58 auto sps = sps_data_.end();
59 auto pps = pps_data_.end();
60
philipel7d745e52018-08-02 12:03:5361 for (size_t i = 0; i < h264_header.nalus_length; ++i) {
62 const NaluInfo& nalu = h264_header.nalus[i];
philipel34852cf2016-11-03 11:03:0163 switch (nalu.type) {
64 case H264::NaluType::kSps: {
philipel6585f702017-03-17 13:12:3365 sps_data_[nalu.sps_id].width = packet->width;
66 sps_data_[nalu.sps_id].height = packet->height;
philipel34852cf2016-11-03 11:03:0167 break;
68 }
69 case H264::NaluType::kPps: {
philipel34852cf2016-11-03 11:03:0170 pps_data_[nalu.pps_id].sps_id = nalu.sps_id;
philipel34852cf2016-11-03 11:03:0171 break;
72 }
73 case H264::NaluType::kIdr: {
74 // If this is the first packet of an IDR, make sure we have the required
75 // SPS/PPS and also calculate how much extra space we need in the buffer
76 // to prepend the SPS/PPS to the bitstream with start codes.
johan0d1b2b62017-01-10 12:21:3577 if (video_header.is_first_packet_in_frame) {
philipel34852cf2016-11-03 11:03:0178 if (nalu.pps_id == -1) {
Mirko Bonadei675513b2017-11-09 10:09:2579 RTC_LOG(LS_WARNING) << "No PPS id in IDR nalu.";
philipela75d12d2016-11-07 13:11:2880 return kRequestKeyframe;
philipel34852cf2016-11-03 11:03:0181 }
82
philipel83c97da2017-06-21 14:22:4083 pps = pps_data_.find(nalu.pps_id);
philipel34852cf2016-11-03 11:03:0184 if (pps == pps_data_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:2585 RTC_LOG(LS_WARNING)
86 << "No PPS with id << " << nalu.pps_id << " received";
philipela75d12d2016-11-07 13:11:2887 return kRequestKeyframe;
philipel34852cf2016-11-03 11:03:0188 }
89
philipel83c97da2017-06-21 14:22:4090 sps = sps_data_.find(pps->second.sps_id);
philipel34852cf2016-11-03 11:03:0191 if (sps == sps_data_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:2592 RTC_LOG(LS_WARNING)
philipel83c97da2017-06-21 14:22:4093 << "No SPS with id << " << pps->second.sps_id << " received";
philipela75d12d2016-11-07 13:11:2894 return kRequestKeyframe;
philipel34852cf2016-11-03 11:03:0195 }
96
philipel83c97da2017-06-21 14:22:4097 // Since the first packet of every keyframe should have its width and
98 // height set we set it here in the case of it being supplied out of
99 // band.
100 packet->width = sps->second.width;
101 packet->height = sps->second.height;
102
103 // If the SPS/PPS was supplied out of band then we will have saved
104 // the actual bitstream in |data|.
105 if (sps->second.data && pps->second.data) {
106 RTC_DCHECK_GT(sps->second.size, 0);
107 RTC_DCHECK_GT(pps->second.size, 0);
108 append_sps_pps = true;
109 }
philipel34852cf2016-11-03 11:03:01110 }
philipel83c97da2017-06-21 14:22:40111 break;
philipel34852cf2016-11-03 11:03:01112 }
philipel83c97da2017-06-21 14:22:40113 default:
114 break;
philipel34852cf2016-11-03 11:03:01115 }
116 }
117
philipel83c97da2017-06-21 14:22:40118 RTC_CHECK(!append_sps_pps ||
119 (sps != sps_data_.end() && pps != pps_data_.end()));
philipel34852cf2016-11-03 11:03:01120
121 // Calculate how much space we need for the rest of the bitstream.
philipel83c97da2017-06-21 14:22:40122 size_t required_size = 0;
123
124 if (append_sps_pps) {
125 required_size += sps->second.size + sizeof(start_code_h264);
126 required_size += pps->second.size + sizeof(start_code_h264);
127 }
128
philipel7d745e52018-08-02 12:03:53129 if (h264_header.packetization_type == kH264StapA) {
philipel34852cf2016-11-03 11:03:01130 const uint8_t* nalu_ptr = data + 1;
131 while (nalu_ptr < data + data_size) {
johan0d1b2b62017-01-10 12:21:35132 RTC_DCHECK(video_header.is_first_packet_in_frame);
philipel34852cf2016-11-03 11:03:01133 required_size += sizeof(start_code_h264);
134
135 // The first two bytes describe the length of a segment.
136 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1];
137 nalu_ptr += 2;
138
139 required_size += segment_length;
140 nalu_ptr += segment_length;
141 }
142 } else {
johan0d1b2b62017-01-10 12:21:35143 if (video_header.is_first_packet_in_frame)
philipel34852cf2016-11-03 11:03:01144 required_size += sizeof(start_code_h264);
145 required_size += data_size;
146 }
147
148 // Then we copy to the new buffer.
149 uint8_t* buffer = new uint8_t[required_size];
150 uint8_t* insert_at = buffer;
151
philipel83c97da2017-06-21 14:22:40152 if (append_sps_pps) {
philipel34852cf2016-11-03 11:03:01153 // Insert SPS.
154 memcpy(insert_at, start_code_h264, sizeof(start_code_h264));
155 insert_at += sizeof(start_code_h264);
philipel83c97da2017-06-21 14:22:40156 memcpy(insert_at, sps->second.data.get(), sps->second.size);
157 insert_at += sps->second.size;
philipel34852cf2016-11-03 11:03:01158
159 // Insert PPS.
160 memcpy(insert_at, start_code_h264, sizeof(start_code_h264));
161 insert_at += sizeof(start_code_h264);
philipel83c97da2017-06-21 14:22:40162 memcpy(insert_at, pps->second.data.get(), pps->second.size);
163 insert_at += pps->second.size;
Rasmus Brandt88f080a2017-11-02 13:28:06164
165 // Update codec header to reflect the newly added SPS and PPS.
166 NaluInfo sps_info;
167 sps_info.type = H264::NaluType::kSps;
168 sps_info.sps_id = sps->first;
169 sps_info.pps_id = -1;
170 NaluInfo pps_info;
171 pps_info.type = H264::NaluType::kPps;
172 pps_info.sps_id = sps->first;
173 pps_info.pps_id = pps->first;
philipel7d745e52018-08-02 12:03:53174 if (h264_header.nalus_length + 2 <= kMaxNalusPerPacket) {
175 h264_header.nalus[h264_header.nalus_length++] = sps_info;
176 h264_header.nalus[h264_header.nalus_length++] = pps_info;
Rasmus Brandt88f080a2017-11-02 13:28:06177 } else {
Mirko Bonadei675513b2017-11-09 10:09:25178 RTC_LOG(LS_WARNING) << "Not enough space in H.264 codec header to insert "
179 "SPS/PPS provided out-of-band.";
Rasmus Brandt88f080a2017-11-02 13:28:06180 }
philipel34852cf2016-11-03 11:03:01181 }
182
183 // Copy the rest of the bitstream and insert start codes.
philipel7d745e52018-08-02 12:03:53184 if (h264_header.packetization_type == kH264StapA) {
philipel34852cf2016-11-03 11:03:01185 const uint8_t* nalu_ptr = data + 1;
186 while (nalu_ptr < data + data_size) {
187 memcpy(insert_at, start_code_h264, sizeof(start_code_h264));
188 insert_at += sizeof(start_code_h264);
189
190 // The first two bytes describe the length of a segment.
191 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1];
192 nalu_ptr += 2;
193
194 size_t copy_end = nalu_ptr - data + segment_length;
195 if (copy_end > data_size) {
196 delete[] buffer;
philipela75d12d2016-11-07 13:11:28197 return kDrop;
philipel34852cf2016-11-03 11:03:01198 }
199
200 memcpy(insert_at, nalu_ptr, segment_length);
201 insert_at += segment_length;
202 nalu_ptr += segment_length;
203 }
204 } else {
johan0d1b2b62017-01-10 12:21:35205 if (video_header.is_first_packet_in_frame) {
philipel34852cf2016-11-03 11:03:01206 memcpy(insert_at, start_code_h264, sizeof(start_code_h264));
207 insert_at += sizeof(start_code_h264);
208 }
209 memcpy(insert_at, data, data_size);
210 }
211
212 packet->dataPtr = buffer;
213 packet->sizeBytes = required_size;
philipela75d12d2016-11-07 13:11:28214 return kInsert;
philipel34852cf2016-11-03 11:03:01215}
216
johand2b092f2017-01-24 10:38:17217void H264SpsPpsTracker::InsertSpsPpsNalus(const std::vector<uint8_t>& sps,
218 const std::vector<uint8_t>& pps) {
219 constexpr size_t kNaluHeaderOffset = 1;
220 if (sps.size() < kNaluHeaderOffset) {
Mirko Bonadei675513b2017-11-09 10:09:25221 RTC_LOG(LS_WARNING) << "SPS size " << sps.size() << " is smaller than "
222 << kNaluHeaderOffset;
johand2b092f2017-01-24 10:38:17223 return;
224 }
225 if ((sps[0] & 0x1f) != H264::NaluType::kSps) {
Mirko Bonadei675513b2017-11-09 10:09:25226 RTC_LOG(LS_WARNING) << "SPS Nalu header missing";
johand2b092f2017-01-24 10:38:17227 return;
228 }
229 if (pps.size() < kNaluHeaderOffset) {
Mirko Bonadei675513b2017-11-09 10:09:25230 RTC_LOG(LS_WARNING) << "PPS size " << pps.size() << " is smaller than "
231 << kNaluHeaderOffset;
johand2b092f2017-01-24 10:38:17232 return;
233 }
234 if ((pps[0] & 0x1f) != H264::NaluType::kPps) {
Mirko Bonadei675513b2017-11-09 10:09:25235 RTC_LOG(LS_WARNING) << "SPS Nalu header missing";
johand2b092f2017-01-24 10:38:17236 return;
237 }
Danil Chapovalov0040b662018-06-18 08:48:16238 absl::optional<SpsParser::SpsState> parsed_sps = SpsParser::ParseSps(
johand2b092f2017-01-24 10:38:17239 sps.data() + kNaluHeaderOffset, sps.size() - kNaluHeaderOffset);
Danil Chapovalov0040b662018-06-18 08:48:16240 absl::optional<PpsParser::PpsState> parsed_pps = PpsParser::ParsePps(
johand2b092f2017-01-24 10:38:17241 pps.data() + kNaluHeaderOffset, pps.size() - kNaluHeaderOffset);
242
243 if (!parsed_sps) {
Mirko Bonadei675513b2017-11-09 10:09:25244 RTC_LOG(LS_WARNING) << "Failed to parse SPS.";
johand2b092f2017-01-24 10:38:17245 }
246
247 if (!parsed_pps) {
Mirko Bonadei675513b2017-11-09 10:09:25248 RTC_LOG(LS_WARNING) << "Failed to parse PPS.";
johand2b092f2017-01-24 10:38:17249 }
philipel022b54e2016-12-20 12:15:59250
251 if (!parsed_pps || !parsed_sps) {
philipel022b54e2016-12-20 12:15:59252 return;
253 }
254
255 SpsInfo sps_info;
256 sps_info.size = sps.size();
philipel6585f702017-03-17 13:12:33257 sps_info.width = parsed_sps->width;
258 sps_info.height = parsed_sps->height;
philipel022b54e2016-12-20 12:15:59259 uint8_t* sps_data = new uint8_t[sps_info.size];
260 memcpy(sps_data, sps.data(), sps_info.size);
261 sps_info.data.reset(sps_data);
262 sps_data_[parsed_sps->id] = std::move(sps_info);
263
264 PpsInfo pps_info;
265 pps_info.size = pps.size();
266 pps_info.sps_id = parsed_pps->sps_id;
267 uint8_t* pps_data = new uint8_t[pps_info.size];
268 memcpy(pps_data, pps.data(), pps_info.size);
269 pps_info.data.reset(pps_data);
270 pps_data_[parsed_pps->id] = std::move(pps_info);
johand2b092f2017-01-24 10:38:17271
Mirko Bonadei675513b2017-11-09 10:09:25272 RTC_LOG(LS_INFO) << "Inserted SPS id " << parsed_sps->id << " and PPS id "
273 << parsed_pps->id << " (referencing SPS "
274 << parsed_pps->sps_id << ")";
philipel022b54e2016-12-20 12:15:59275}
276
philipel34852cf2016-11-03 11:03:01277} // namespace video_coding
278} // namespace webrtc