philipel | 34852cf | 2016-11-03 04:03:01 -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/h264_sps_pps_tracker.h" |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 12 | |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 13 | #include <memory> |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 14 | #include <string> |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 15 | #include <utility> |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 16 | |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 17 | #include "absl/types/variant.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "common_video/h264/h264_common.h" |
| 19 | #include "common_video/h264/pps_parser.h" |
| 20 | #include "common_video/h264/sps_parser.h" |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 21 | #include "modules/video_coding/codecs/h264/include/h264_globals.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/logging.h" |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace video_coding { |
| 27 | |
| 28 | namespace { |
| 29 | const uint8_t start_code_h264[] = {0, 0, 0, 1}; |
| 30 | } // namespace |
| 31 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 32 | H264SpsPpsTracker::H264SpsPpsTracker() = default; |
| 33 | H264SpsPpsTracker::~H264SpsPpsTracker() = default; |
| 34 | |
| 35 | H264SpsPpsTracker::PpsInfo::PpsInfo() = default; |
| 36 | H264SpsPpsTracker::PpsInfo::PpsInfo(PpsInfo&& rhs) = default; |
| 37 | H264SpsPpsTracker::PpsInfo& H264SpsPpsTracker::PpsInfo::operator=( |
| 38 | PpsInfo&& rhs) = default; |
| 39 | H264SpsPpsTracker::PpsInfo::~PpsInfo() = default; |
| 40 | |
| 41 | H264SpsPpsTracker::SpsInfo::SpsInfo() = default; |
| 42 | H264SpsPpsTracker::SpsInfo::SpsInfo(SpsInfo&& rhs) = default; |
| 43 | H264SpsPpsTracker::SpsInfo& H264SpsPpsTracker::SpsInfo::operator=( |
| 44 | SpsInfo&& rhs) = default; |
| 45 | H264SpsPpsTracker::SpsInfo::~SpsInfo() = default; |
| 46 | |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 47 | H264SpsPpsTracker::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); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 52 | |
philipel | 7d745e5 | 2018-08-02 14:03:53 +0200 | [diff] [blame] | 53 | auto& h264_header = |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 54 | absl::get<RTPVideoHeaderH264>(video_header->video_type_header); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 55 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 56 | bool append_sps_pps = false; |
| 57 | auto sps = sps_data_.end(); |
| 58 | auto pps = pps_data_.end(); |
| 59 | |
philipel | 7d745e5 | 2018-08-02 14:03:53 +0200 | [diff] [blame] | 60 | for (size_t i = 0; i < h264_header.nalus_length; ++i) { |
| 61 | const NaluInfo& nalu = h264_header.nalus[i]; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 62 | switch (nalu.type) { |
| 63 | case H264::NaluType::kSps: { |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 64 | SpsInfo& sps_info = sps_data_[nalu.sps_id]; |
| 65 | sps_info.width = video_header->width; |
| 66 | sps_info.height = video_header->height; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 67 | break; |
| 68 | } |
| 69 | case H264::NaluType::kPps: { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 70 | pps_data_[nalu.pps_id].sps_id = nalu.sps_id; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 71 | 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. |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 77 | if (video_header->is_first_packet_in_frame) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 78 | if (nalu.pps_id == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 79 | RTC_LOG(LS_WARNING) << "No PPS id in IDR nalu."; |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 80 | return {kRequestKeyframe}; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 81 | } |
| 82 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 83 | pps = pps_data_.find(nalu.pps_id); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 84 | if (pps == pps_data_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 85 | RTC_LOG(LS_WARNING) |
| 86 | << "No PPS with id << " << nalu.pps_id << " received"; |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 87 | return {kRequestKeyframe}; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 88 | } |
| 89 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 90 | sps = sps_data_.find(pps->second.sps_id); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 91 | if (sps == sps_data_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 92 | RTC_LOG(LS_WARNING) |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 93 | << "No SPS with id << " << pps->second.sps_id << " received"; |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 94 | return {kRequestKeyframe}; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 95 | } |
| 96 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 97 | // 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. |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 100 | video_header->width = sps->second.width; |
| 101 | video_header->height = sps->second.height; |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 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 | } |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 110 | } |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 111 | break; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 112 | } |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 113 | default: |
| 114 | break; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 118 | RTC_CHECK(!append_sps_pps || |
| 119 | (sps != sps_data_.end() && pps != pps_data_.end())); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 120 | |
| 121 | // Calculate how much space we need for the rest of the bitstream. |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 122 | 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 | |
philipel | 7d745e5 | 2018-08-02 14:03:53 +0200 | [diff] [blame] | 129 | if (h264_header.packetization_type == kH264StapA) { |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 130 | const uint8_t* nalu_ptr = bitstream.data() + 1; |
| 131 | while (nalu_ptr < bitstream.data() + bitstream.size()) { |
| 132 | RTC_DCHECK(video_header->is_first_packet_in_frame); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 133 | 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 { |
philipel | 2c850af | 2019-07-12 15:32:17 +0200 | [diff] [blame] | 143 | if (h264_header.nalus_length > 0) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 144 | required_size += sizeof(start_code_h264); |
philipel | 5e953d7 | 2019-06-14 13:40:04 +0200 | [diff] [blame] | 145 | } |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 146 | required_size += bitstream.size(); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Then we copy to the new buffer. |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 150 | H264SpsPpsTracker::FixedBitstream fixed; |
| 151 | fixed.data = std::make_unique<uint8_t[]>(required_size); |
| 152 | fixed.size = required_size; |
| 153 | uint8_t* insert_at = fixed.data.get(); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 154 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 155 | if (append_sps_pps) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 156 | // Insert SPS. |
| 157 | memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 158 | insert_at += sizeof(start_code_h264); |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 159 | memcpy(insert_at, sps->second.data.get(), sps->second.size); |
| 160 | insert_at += sps->second.size; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 161 | |
| 162 | // Insert PPS. |
| 163 | memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 164 | insert_at += sizeof(start_code_h264); |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 165 | memcpy(insert_at, pps->second.data.get(), pps->second.size); |
| 166 | insert_at += pps->second.size; |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 167 | |
| 168 | // Update codec header to reflect the newly added SPS and PPS. |
| 169 | NaluInfo sps_info; |
| 170 | sps_info.type = H264::NaluType::kSps; |
| 171 | sps_info.sps_id = sps->first; |
| 172 | sps_info.pps_id = -1; |
| 173 | NaluInfo pps_info; |
| 174 | pps_info.type = H264::NaluType::kPps; |
| 175 | pps_info.sps_id = sps->first; |
| 176 | pps_info.pps_id = pps->first; |
philipel | 7d745e5 | 2018-08-02 14:03:53 +0200 | [diff] [blame] | 177 | if (h264_header.nalus_length + 2 <= kMaxNalusPerPacket) { |
| 178 | h264_header.nalus[h264_header.nalus_length++] = sps_info; |
| 179 | h264_header.nalus[h264_header.nalus_length++] = pps_info; |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 180 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 181 | RTC_LOG(LS_WARNING) << "Not enough space in H.264 codec header to insert " |
| 182 | "SPS/PPS provided out-of-band."; |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 183 | } |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Copy the rest of the bitstream and insert start codes. |
philipel | 7d745e5 | 2018-08-02 14:03:53 +0200 | [diff] [blame] | 187 | if (h264_header.packetization_type == kH264StapA) { |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 188 | const uint8_t* nalu_ptr = bitstream.data() + 1; |
| 189 | while (nalu_ptr < bitstream.data() + bitstream.size()) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 190 | memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 191 | insert_at += sizeof(start_code_h264); |
| 192 | |
| 193 | // The first two bytes describe the length of a segment. |
| 194 | uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1]; |
| 195 | nalu_ptr += 2; |
| 196 | |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 197 | size_t copy_end = nalu_ptr - bitstream.data() + segment_length; |
| 198 | if (copy_end > bitstream.size()) { |
| 199 | return {kDrop}; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | memcpy(insert_at, nalu_ptr, segment_length); |
| 203 | insert_at += segment_length; |
| 204 | nalu_ptr += segment_length; |
| 205 | } |
| 206 | } else { |
philipel | 2c850af | 2019-07-12 15:32:17 +0200 | [diff] [blame] | 207 | if (h264_header.nalus_length > 0) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 208 | memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 209 | insert_at += sizeof(start_code_h264); |
| 210 | } |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 211 | memcpy(insert_at, bitstream.data(), bitstream.size()); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Danil Chapovalov | fbec2ec | 2019-10-28 13:27:05 +0100 | [diff] [blame] | 214 | fixed.action = kInsert; |
| 215 | return fixed; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 216 | } |
| 217 | |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 218 | void H264SpsPpsTracker::InsertSpsPpsNalus(const std::vector<uint8_t>& sps, |
| 219 | const std::vector<uint8_t>& pps) { |
| 220 | constexpr size_t kNaluHeaderOffset = 1; |
| 221 | if (sps.size() < kNaluHeaderOffset) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 222 | RTC_LOG(LS_WARNING) << "SPS size " << sps.size() << " is smaller than " |
| 223 | << kNaluHeaderOffset; |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | if ((sps[0] & 0x1f) != H264::NaluType::kSps) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 227 | RTC_LOG(LS_WARNING) << "SPS Nalu header missing"; |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | if (pps.size() < kNaluHeaderOffset) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 231 | RTC_LOG(LS_WARNING) << "PPS size " << pps.size() << " is smaller than " |
| 232 | << kNaluHeaderOffset; |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | if ((pps[0] & 0x1f) != H264::NaluType::kPps) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 236 | RTC_LOG(LS_WARNING) << "SPS Nalu header missing"; |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 237 | return; |
| 238 | } |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 239 | absl::optional<SpsParser::SpsState> parsed_sps = SpsParser::ParseSps( |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 240 | sps.data() + kNaluHeaderOffset, sps.size() - kNaluHeaderOffset); |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 241 | absl::optional<PpsParser::PpsState> parsed_pps = PpsParser::ParsePps( |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 242 | pps.data() + kNaluHeaderOffset, pps.size() - kNaluHeaderOffset); |
| 243 | |
| 244 | if (!parsed_sps) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 245 | RTC_LOG(LS_WARNING) << "Failed to parse SPS."; |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (!parsed_pps) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 249 | RTC_LOG(LS_WARNING) << "Failed to parse PPS."; |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 250 | } |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 251 | |
| 252 | if (!parsed_pps || !parsed_sps) { |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
| 256 | SpsInfo sps_info; |
| 257 | sps_info.size = sps.size(); |
philipel | 6585f70 | 2017-03-17 06:12:33 -0700 | [diff] [blame] | 258 | sps_info.width = parsed_sps->width; |
| 259 | sps_info.height = parsed_sps->height; |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 260 | uint8_t* sps_data = new uint8_t[sps_info.size]; |
| 261 | memcpy(sps_data, sps.data(), sps_info.size); |
| 262 | sps_info.data.reset(sps_data); |
| 263 | sps_data_[parsed_sps->id] = std::move(sps_info); |
| 264 | |
| 265 | PpsInfo pps_info; |
| 266 | pps_info.size = pps.size(); |
| 267 | pps_info.sps_id = parsed_pps->sps_id; |
| 268 | uint8_t* pps_data = new uint8_t[pps_info.size]; |
| 269 | memcpy(pps_data, pps.data(), pps_info.size); |
| 270 | pps_info.data.reset(pps_data); |
| 271 | pps_data_[parsed_pps->id] = std::move(pps_info); |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 272 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 273 | RTC_LOG(LS_INFO) << "Inserted SPS id " << parsed_sps->id << " and PPS id " |
| 274 | << parsed_pps->id << " (referencing SPS " |
| 275 | << parsed_pps->sps_id << ")"; |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 276 | } |
| 277 | |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 278 | } // namespace video_coding |
| 279 | } // namespace webrtc |