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 | |
| 13 | #include <string> |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 14 | #include <utility> |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "common_video/h264/h264_common.h" |
| 17 | #include "common_video/h264/pps_parser.h" |
| 18 | #include "common_video/h264/sps_parser.h" |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame^] | 19 | #include "modules/video_coding/codecs/h264/include/h264_globals.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #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" |
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 | |
philipel | a75d12d | 2016-11-07 05:11:28 -0800 | [diff] [blame] | 32 | H264SpsPpsTracker::PacketAction H264SpsPpsTracker::CopyAndFixBitstream( |
| 33 | VCMPacket* packet) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 34 | RTC_DCHECK(packet->codec == kVideoCodecH264); |
| 35 | |
| 36 | const uint8_t* data = packet->dataPtr; |
| 37 | const size_t data_size = packet->sizeBytes; |
| 38 | const RTPVideoHeader& video_header = packet->video_header; |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame^] | 39 | RTPVideoHeaderH264* codec_header = &packet->video_header.codecHeader.H264; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 40 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 41 | bool append_sps_pps = false; |
| 42 | auto sps = sps_data_.end(); |
| 43 | auto pps = pps_data_.end(); |
| 44 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame^] | 45 | for (size_t i = 0; i < codec_header->nalus_length; ++i) { |
| 46 | const NaluInfo& nalu = codec_header->nalus[i]; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 47 | switch (nalu.type) { |
| 48 | case H264::NaluType::kSps: { |
philipel | 6585f70 | 2017-03-17 06:12:33 -0700 | [diff] [blame] | 49 | sps_data_[nalu.sps_id].width = packet->width; |
| 50 | sps_data_[nalu.sps_id].height = packet->height; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 51 | break; |
| 52 | } |
| 53 | case H264::NaluType::kPps: { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 54 | pps_data_[nalu.pps_id].sps_id = nalu.sps_id; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 55 | break; |
| 56 | } |
| 57 | case H264::NaluType::kIdr: { |
| 58 | // If this is the first packet of an IDR, make sure we have the required |
| 59 | // SPS/PPS and also calculate how much extra space we need in the buffer |
| 60 | // to prepend the SPS/PPS to the bitstream with start codes. |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 61 | if (video_header.is_first_packet_in_frame) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 62 | if (nalu.pps_id == -1) { |
| 63 | LOG(LS_WARNING) << "No PPS id in IDR nalu."; |
philipel | a75d12d | 2016-11-07 05:11:28 -0800 | [diff] [blame] | 64 | return kRequestKeyframe; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 65 | } |
| 66 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 67 | pps = pps_data_.find(nalu.pps_id); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 68 | if (pps == pps_data_.end()) { |
| 69 | LOG(LS_WARNING) << "No PPS with id << " << nalu.pps_id |
| 70 | << " received"; |
philipel | a75d12d | 2016-11-07 05:11:28 -0800 | [diff] [blame] | 71 | return kRequestKeyframe; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 72 | } |
| 73 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 74 | sps = sps_data_.find(pps->second.sps_id); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 75 | if (sps == sps_data_.end()) { |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 76 | LOG(LS_WARNING) |
| 77 | << "No SPS with id << " << pps->second.sps_id << " received"; |
philipel | a75d12d | 2016-11-07 05:11:28 -0800 | [diff] [blame] | 78 | return kRequestKeyframe; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 79 | } |
| 80 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 81 | // Since the first packet of every keyframe should have its width and |
| 82 | // height set we set it here in the case of it being supplied out of |
| 83 | // band. |
| 84 | packet->width = sps->second.width; |
| 85 | packet->height = sps->second.height; |
| 86 | |
| 87 | // If the SPS/PPS was supplied out of band then we will have saved |
| 88 | // the actual bitstream in |data|. |
| 89 | if (sps->second.data && pps->second.data) { |
| 90 | RTC_DCHECK_GT(sps->second.size, 0); |
| 91 | RTC_DCHECK_GT(pps->second.size, 0); |
| 92 | append_sps_pps = true; |
| 93 | } |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 94 | } |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 95 | break; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 96 | } |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 97 | default: |
| 98 | break; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 102 | RTC_CHECK(!append_sps_pps || |
| 103 | (sps != sps_data_.end() && pps != pps_data_.end())); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 104 | |
| 105 | // Calculate how much space we need for the rest of the bitstream. |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 106 | size_t required_size = 0; |
| 107 | |
| 108 | if (append_sps_pps) { |
| 109 | required_size += sps->second.size + sizeof(start_code_h264); |
| 110 | required_size += pps->second.size + sizeof(start_code_h264); |
| 111 | } |
| 112 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame^] | 113 | if (codec_header->packetization_type == kH264StapA) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 114 | const uint8_t* nalu_ptr = data + 1; |
| 115 | while (nalu_ptr < data + data_size) { |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 116 | RTC_DCHECK(video_header.is_first_packet_in_frame); |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 117 | required_size += sizeof(start_code_h264); |
| 118 | |
| 119 | // The first two bytes describe the length of a segment. |
| 120 | uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1]; |
| 121 | nalu_ptr += 2; |
| 122 | |
| 123 | required_size += segment_length; |
| 124 | nalu_ptr += segment_length; |
| 125 | } |
| 126 | } else { |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 127 | if (video_header.is_first_packet_in_frame) |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 128 | required_size += sizeof(start_code_h264); |
| 129 | required_size += data_size; |
| 130 | } |
| 131 | |
| 132 | // Then we copy to the new buffer. |
| 133 | uint8_t* buffer = new uint8_t[required_size]; |
| 134 | uint8_t* insert_at = buffer; |
| 135 | |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 136 | if (append_sps_pps) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 137 | // Insert SPS. |
| 138 | memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 139 | insert_at += sizeof(start_code_h264); |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 140 | memcpy(insert_at, sps->second.data.get(), sps->second.size); |
| 141 | insert_at += sps->second.size; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 142 | |
| 143 | // Insert PPS. |
| 144 | memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 145 | insert_at += sizeof(start_code_h264); |
philipel | 83c97da | 2017-06-21 07:22:40 -0700 | [diff] [blame] | 146 | memcpy(insert_at, pps->second.data.get(), pps->second.size); |
| 147 | insert_at += pps->second.size; |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame^] | 148 | |
| 149 | // Update codec header to reflect the newly added SPS and PPS. |
| 150 | NaluInfo sps_info; |
| 151 | sps_info.type = H264::NaluType::kSps; |
| 152 | sps_info.sps_id = sps->first; |
| 153 | sps_info.pps_id = -1; |
| 154 | NaluInfo pps_info; |
| 155 | pps_info.type = H264::NaluType::kPps; |
| 156 | pps_info.sps_id = sps->first; |
| 157 | pps_info.pps_id = pps->first; |
| 158 | if (codec_header->nalus_length + 2 <= kMaxNalusPerPacket) { |
| 159 | codec_header->nalus[codec_header->nalus_length++] = sps_info; |
| 160 | codec_header->nalus[codec_header->nalus_length++] = pps_info; |
| 161 | } else { |
| 162 | LOG(LS_WARNING) << "Not enough space in H.264 codec header to insert " |
| 163 | "SPS/PPS provided out-of-band."; |
| 164 | } |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Copy the rest of the bitstream and insert start codes. |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame^] | 168 | if (codec_header->packetization_type == kH264StapA) { |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 169 | const uint8_t* nalu_ptr = data + 1; |
| 170 | while (nalu_ptr < data + data_size) { |
| 171 | memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 172 | insert_at += sizeof(start_code_h264); |
| 173 | |
| 174 | // The first two bytes describe the length of a segment. |
| 175 | uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1]; |
| 176 | nalu_ptr += 2; |
| 177 | |
| 178 | size_t copy_end = nalu_ptr - data + segment_length; |
| 179 | if (copy_end > data_size) { |
| 180 | delete[] buffer; |
philipel | a75d12d | 2016-11-07 05:11:28 -0800 | [diff] [blame] | 181 | return kDrop; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | memcpy(insert_at, nalu_ptr, segment_length); |
| 185 | insert_at += segment_length; |
| 186 | nalu_ptr += segment_length; |
| 187 | } |
| 188 | } else { |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 189 | if (video_header.is_first_packet_in_frame) { |
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 | memcpy(insert_at, data, data_size); |
| 194 | } |
| 195 | |
| 196 | packet->dataPtr = buffer; |
| 197 | packet->sizeBytes = required_size; |
philipel | a75d12d | 2016-11-07 05:11:28 -0800 | [diff] [blame] | 198 | return kInsert; |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 199 | } |
| 200 | |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 201 | void H264SpsPpsTracker::InsertSpsPpsNalus(const std::vector<uint8_t>& sps, |
| 202 | const std::vector<uint8_t>& pps) { |
| 203 | constexpr size_t kNaluHeaderOffset = 1; |
| 204 | if (sps.size() < kNaluHeaderOffset) { |
| 205 | LOG(LS_WARNING) << "SPS size " << sps.size() << " is smaller than " |
| 206 | << kNaluHeaderOffset; |
| 207 | return; |
| 208 | } |
| 209 | if ((sps[0] & 0x1f) != H264::NaluType::kSps) { |
| 210 | LOG(LS_WARNING) << "SPS Nalu header missing"; |
| 211 | return; |
| 212 | } |
| 213 | if (pps.size() < kNaluHeaderOffset) { |
| 214 | LOG(LS_WARNING) << "PPS size " << pps.size() << " is smaller than " |
| 215 | << kNaluHeaderOffset; |
| 216 | return; |
| 217 | } |
| 218 | if ((pps[0] & 0x1f) != H264::NaluType::kPps) { |
| 219 | LOG(LS_WARNING) << "SPS Nalu header missing"; |
| 220 | return; |
| 221 | } |
| 222 | rtc::Optional<SpsParser::SpsState> parsed_sps = SpsParser::ParseSps( |
| 223 | sps.data() + kNaluHeaderOffset, sps.size() - kNaluHeaderOffset); |
| 224 | rtc::Optional<PpsParser::PpsState> parsed_pps = PpsParser::ParsePps( |
| 225 | pps.data() + kNaluHeaderOffset, pps.size() - kNaluHeaderOffset); |
| 226 | |
| 227 | if (!parsed_sps) { |
| 228 | LOG(LS_WARNING) << "Failed to parse SPS."; |
| 229 | } |
| 230 | |
| 231 | if (!parsed_pps) { |
| 232 | LOG(LS_WARNING) << "Failed to parse PPS."; |
| 233 | } |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 234 | |
| 235 | if (!parsed_pps || !parsed_sps) { |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
| 239 | SpsInfo sps_info; |
| 240 | sps_info.size = sps.size(); |
philipel | 6585f70 | 2017-03-17 06:12:33 -0700 | [diff] [blame] | 241 | sps_info.width = parsed_sps->width; |
| 242 | sps_info.height = parsed_sps->height; |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 243 | uint8_t* sps_data = new uint8_t[sps_info.size]; |
| 244 | memcpy(sps_data, sps.data(), sps_info.size); |
| 245 | sps_info.data.reset(sps_data); |
| 246 | sps_data_[parsed_sps->id] = std::move(sps_info); |
| 247 | |
| 248 | PpsInfo pps_info; |
| 249 | pps_info.size = pps.size(); |
| 250 | pps_info.sps_id = parsed_pps->sps_id; |
| 251 | uint8_t* pps_data = new uint8_t[pps_info.size]; |
| 252 | memcpy(pps_data, pps.data(), pps_info.size); |
| 253 | pps_info.data.reset(pps_data); |
| 254 | pps_data_[parsed_pps->id] = std::move(pps_info); |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 255 | |
| 256 | LOG(LS_INFO) << "Inserted SPS id " << parsed_sps->id << " and PPS id " |
| 257 | << parsed_pps->id << " (referencing SPS " << parsed_pps->sps_id |
| 258 | << ")"; |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 259 | } |
| 260 | |
philipel | 34852cf | 2016-11-03 04:03:01 -0700 | [diff] [blame] | 261 | } // namespace video_coding |
| 262 | } // namespace webrtc |