OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 const RTPVideoHeaderH264& codec_header = video_header.codecHeader.H264; | 38 const RTPVideoHeaderH264& codec_header = video_header.codecHeader.H264; |
39 | 39 |
40 // Packets that only contains SPS/PPS are not decodable by themselves, and | 40 // Packets that only contains SPS/PPS are not decodable by themselves, and |
41 // to avoid frames being created containing only these two nalus we don't | 41 // to avoid frames being created containing only these two nalus we don't |
42 // insert them into the PacketBuffer. Instead we save the SPS/PPS and | 42 // insert them into the PacketBuffer. Instead we save the SPS/PPS and |
43 // prepend the bitstream of first packet of an IDR referring to the | 43 // prepend the bitstream of first packet of an IDR referring to the |
44 // corresponding SPS/PPS id. | 44 // corresponding SPS/PPS id. |
45 bool insert_packet = codec_header.nalus_length == 0 ? true : false; | 45 bool insert_packet = codec_header.nalus_length == 0 ? true : false; |
46 | 46 |
47 int pps_id = -1; | 47 int pps_id = -1; |
| 48 int sps_id = -1; |
48 size_t required_size = 0; | 49 size_t required_size = 0; |
49 for (size_t i = 0; i < codec_header.nalus_length; ++i) { | 50 for (size_t i = 0; i < codec_header.nalus_length; ++i) { |
50 const NaluInfo& nalu = codec_header.nalus[i]; | 51 const NaluInfo& nalu = codec_header.nalus[i]; |
51 switch (nalu.type) { | 52 switch (nalu.type) { |
52 case H264::NaluType::kSps: { | 53 case H264::NaluType::kSps: { |
53 // Save SPS. | 54 // Save SPS. |
54 sps_data_[nalu.sps_id].size = nalu.size; | 55 sps_data_[nalu.sps_id].size = nalu.size; |
55 sps_data_[nalu.sps_id].data.reset(new uint8_t[nalu.size]); | 56 sps_data_[nalu.sps_id].data.reset(new uint8_t[nalu.size]); |
56 memcpy(sps_data_[nalu.sps_id].data.get(), data + nalu.offset, | 57 memcpy(sps_data_[nalu.sps_id].data.get(), data + nalu.offset, |
57 nalu.size); | 58 nalu.size); |
| 59 sps_data_[nalu.sps_id].width = packet->width; |
| 60 sps_data_[nalu.sps_id].height = packet->height; |
58 break; | 61 break; |
59 } | 62 } |
60 case H264::NaluType::kPps: { | 63 case H264::NaluType::kPps: { |
61 // Save PPS. | 64 // Save PPS. |
62 pps_data_[nalu.pps_id].sps_id = nalu.sps_id; | 65 pps_data_[nalu.pps_id].sps_id = nalu.sps_id; |
63 pps_data_[nalu.pps_id].size = nalu.size; | 66 pps_data_[nalu.pps_id].size = nalu.size; |
64 pps_data_[nalu.pps_id].data.reset(new uint8_t[nalu.size]); | 67 pps_data_[nalu.pps_id].data.reset(new uint8_t[nalu.size]); |
65 memcpy(pps_data_[nalu.pps_id].data.get(), data + nalu.offset, | 68 memcpy(pps_data_[nalu.pps_id].data.get(), data + nalu.offset, |
66 nalu.size); | 69 nalu.size); |
67 break; | 70 break; |
68 } | 71 } |
69 case H264::NaluType::kIdr: { | 72 case H264::NaluType::kIdr: { |
70 // If this is the first packet of an IDR, make sure we have the required | 73 // If this is the first packet of an IDR, make sure we have the required |
71 // SPS/PPS and also calculate how much extra space we need in the buffer | 74 // SPS/PPS and also calculate how much extra space we need in the buffer |
72 // to prepend the SPS/PPS to the bitstream with start codes. | 75 // to prepend the SPS/PPS to the bitstream with start codes. |
73 if (video_header.is_first_packet_in_frame) { | 76 if (video_header.is_first_packet_in_frame) { |
74 if (nalu.pps_id == -1) { | 77 if (nalu.pps_id == -1) { |
75 LOG(LS_WARNING) << "No PPS id in IDR nalu."; | 78 LOG(LS_WARNING) << "No PPS id in IDR nalu."; |
76 return kRequestKeyframe; | 79 return kRequestKeyframe; |
77 } | 80 } |
78 | 81 |
79 auto pps = pps_data_.find(nalu.pps_id); | 82 auto pps = pps_data_.find(nalu.pps_id); |
80 if (pps == pps_data_.end()) { | 83 if (pps == pps_data_.end()) { |
81 LOG(LS_WARNING) << "No PPS with id << " << nalu.pps_id | 84 LOG(LS_WARNING) << "No PPS with id << " << nalu.pps_id |
82 << " received"; | 85 << " received"; |
83 return kRequestKeyframe; | 86 return kRequestKeyframe; |
84 } | 87 } |
85 | 88 |
86 auto sps = sps_data_.find(pps->second.sps_id); | 89 sps_id = pps->second.sps_id; |
| 90 auto sps = sps_data_.find(sps_id); |
87 if (sps == sps_data_.end()) { | 91 if (sps == sps_data_.end()) { |
88 LOG(LS_WARNING) << "No SPS with id << " | 92 LOG(LS_WARNING) << "No SPS with id << " |
89 << pps_data_[nalu.pps_id].sps_id << " received"; | 93 << pps_data_[nalu.pps_id].sps_id << " received"; |
90 return kRequestKeyframe; | 94 return kRequestKeyframe; |
91 } | 95 } |
92 | 96 |
93 pps_id = nalu.pps_id; | 97 pps_id = nalu.pps_id; |
94 required_size += pps->second.size + sizeof(start_code_h264); | 98 required_size += pps->second.size + sizeof(start_code_h264); |
95 required_size += sps->second.size + sizeof(start_code_h264); | 99 required_size += sps->second.size + sizeof(start_code_h264); |
96 } | 100 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 nalu_ptr += segment_length; | 174 nalu_ptr += segment_length; |
171 } | 175 } |
172 } else { | 176 } else { |
173 if (video_header.is_first_packet_in_frame) { | 177 if (video_header.is_first_packet_in_frame) { |
174 memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); | 178 memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
175 insert_at += sizeof(start_code_h264); | 179 insert_at += sizeof(start_code_h264); |
176 } | 180 } |
177 memcpy(insert_at, data, data_size); | 181 memcpy(insert_at, data, data_size); |
178 } | 182 } |
179 | 183 |
| 184 if (sps_id != -1) { |
| 185 packet->width = sps_data_[sps_id].width; |
| 186 packet->height = sps_data_[sps_id].height; |
| 187 } |
| 188 |
180 packet->dataPtr = buffer; | 189 packet->dataPtr = buffer; |
181 packet->sizeBytes = required_size; | 190 packet->sizeBytes = required_size; |
182 return kInsert; | 191 return kInsert; |
183 } | 192 } |
184 | 193 |
185 void H264SpsPpsTracker::InsertSpsPpsNalus(const std::vector<uint8_t>& sps, | 194 void H264SpsPpsTracker::InsertSpsPpsNalus(const std::vector<uint8_t>& sps, |
186 const std::vector<uint8_t>& pps) { | 195 const std::vector<uint8_t>& pps) { |
187 constexpr size_t kNaluHeaderOffset = 1; | 196 constexpr size_t kNaluHeaderOffset = 1; |
188 if (sps.size() < kNaluHeaderOffset) { | 197 if (sps.size() < kNaluHeaderOffset) { |
189 LOG(LS_WARNING) << "SPS size " << sps.size() << " is smaller than " | 198 LOG(LS_WARNING) << "SPS size " << sps.size() << " is smaller than " |
(...skipping 25 matching lines...) Expand all Loading... |
215 if (!parsed_pps) { | 224 if (!parsed_pps) { |
216 LOG(LS_WARNING) << "Failed to parse PPS."; | 225 LOG(LS_WARNING) << "Failed to parse PPS."; |
217 } | 226 } |
218 | 227 |
219 if (!parsed_pps || !parsed_sps) { | 228 if (!parsed_pps || !parsed_sps) { |
220 return; | 229 return; |
221 } | 230 } |
222 | 231 |
223 SpsInfo sps_info; | 232 SpsInfo sps_info; |
224 sps_info.size = sps.size(); | 233 sps_info.size = sps.size(); |
| 234 sps_info.width = parsed_sps->width; |
| 235 sps_info.height = parsed_sps->height; |
225 uint8_t* sps_data = new uint8_t[sps_info.size]; | 236 uint8_t* sps_data = new uint8_t[sps_info.size]; |
226 memcpy(sps_data, sps.data(), sps_info.size); | 237 memcpy(sps_data, sps.data(), sps_info.size); |
227 sps_info.data.reset(sps_data); | 238 sps_info.data.reset(sps_data); |
228 sps_data_[parsed_sps->id] = std::move(sps_info); | 239 sps_data_[parsed_sps->id] = std::move(sps_info); |
229 | 240 |
230 PpsInfo pps_info; | 241 PpsInfo pps_info; |
231 pps_info.size = pps.size(); | 242 pps_info.size = pps.size(); |
232 pps_info.sps_id = parsed_pps->sps_id; | 243 pps_info.sps_id = parsed_pps->sps_id; |
233 uint8_t* pps_data = new uint8_t[pps_info.size]; | 244 uint8_t* pps_data = new uint8_t[pps_info.size]; |
234 memcpy(pps_data, pps.data(), pps_info.size); | 245 memcpy(pps_data, pps.data(), pps_info.size); |
235 pps_info.data.reset(pps_data); | 246 pps_info.data.reset(pps_data); |
236 pps_data_[parsed_pps->id] = std::move(pps_info); | 247 pps_data_[parsed_pps->id] = std::move(pps_info); |
237 | 248 |
238 LOG(LS_INFO) << "Inserted SPS id " << parsed_sps->id << " and PPS id " | 249 LOG(LS_INFO) << "Inserted SPS id " << parsed_sps->id << " and PPS id " |
239 << parsed_pps->id << " (referencing SPS " << parsed_pps->sps_id | 250 << parsed_pps->id << " (referencing SPS " << parsed_pps->sps_id |
240 << ")"; | 251 << ")"; |
241 } | 252 } |
242 | 253 |
243 } // namespace video_coding | 254 } // namespace video_coding |
244 } // namespace webrtc | 255 } // namespace webrtc |
OLD | NEW |