Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1887)

Side by Side Diff: webrtc/modules/video_coding/h264_sps_pps_tracker.cc

Issue 2750633003: Save width/height of SPS nalus and restore them on the first packet of an IDR. (Closed)
Patch Set: Test fix. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 packet->width = sps_data_[sps_id].width;
185 packet->height = sps_data_[sps_id].height;
180 packet->dataPtr = buffer; 186 packet->dataPtr = buffer;
181 packet->sizeBytes = required_size; 187 packet->sizeBytes = required_size;
182 return kInsert; 188 return kInsert;
183 } 189 }
184 190
185 void H264SpsPpsTracker::InsertSpsPpsNalus(const std::vector<uint8_t>& sps, 191 void H264SpsPpsTracker::InsertSpsPpsNalus(const std::vector<uint8_t>& sps,
186 const std::vector<uint8_t>& pps) { 192 const std::vector<uint8_t>& pps) {
187 constexpr size_t kNaluHeaderOffset = 1; 193 constexpr size_t kNaluHeaderOffset = 1;
188 if (sps.size() < kNaluHeaderOffset) { 194 if (sps.size() < kNaluHeaderOffset) {
189 LOG(LS_WARNING) << "SPS size " << sps.size() << " is smaller than " 195 LOG(LS_WARNING) << "SPS size " << sps.size() << " is smaller than "
(...skipping 25 matching lines...) Expand all
215 if (!parsed_pps) { 221 if (!parsed_pps) {
216 LOG(LS_WARNING) << "Failed to parse PPS."; 222 LOG(LS_WARNING) << "Failed to parse PPS.";
217 } 223 }
218 224
219 if (!parsed_pps || !parsed_sps) { 225 if (!parsed_pps || !parsed_sps) {
220 return; 226 return;
221 } 227 }
222 228
223 SpsInfo sps_info; 229 SpsInfo sps_info;
224 sps_info.size = sps.size(); 230 sps_info.size = sps.size();
231 sps_info.width = parsed_sps->width;
232 sps_info.height = parsed_sps->height;
225 uint8_t* sps_data = new uint8_t[sps_info.size]; 233 uint8_t* sps_data = new uint8_t[sps_info.size];
226 memcpy(sps_data, sps.data(), sps_info.size); 234 memcpy(sps_data, sps.data(), sps_info.size);
227 sps_info.data.reset(sps_data); 235 sps_info.data.reset(sps_data);
228 sps_data_[parsed_sps->id] = std::move(sps_info); 236 sps_data_[parsed_sps->id] = std::move(sps_info);
229 237
230 PpsInfo pps_info; 238 PpsInfo pps_info;
231 pps_info.size = pps.size(); 239 pps_info.size = pps.size();
232 pps_info.sps_id = parsed_pps->sps_id; 240 pps_info.sps_id = parsed_pps->sps_id;
233 uint8_t* pps_data = new uint8_t[pps_info.size]; 241 uint8_t* pps_data = new uint8_t[pps_info.size];
234 memcpy(pps_data, pps.data(), pps_info.size); 242 memcpy(pps_data, pps.data(), pps_info.size);
235 pps_info.data.reset(pps_data); 243 pps_info.data.reset(pps_data);
236 pps_data_[parsed_pps->id] = std::move(pps_info); 244 pps_data_[parsed_pps->id] = std::move(pps_info);
237 245
238 LOG(LS_INFO) << "Inserted SPS id " << parsed_sps->id << " and PPS id " 246 LOG(LS_INFO) << "Inserted SPS id " << parsed_sps->id << " and PPS id "
239 << parsed_pps->id << " (referencing SPS " << parsed_pps->sps_id 247 << parsed_pps->id << " (referencing SPS " << parsed_pps->sps_id
240 << ")"; 248 << ")";
241 } 249 }
242 250
243 } // namespace video_coding 251 } // namespace video_coding
244 } // namespace webrtc 252 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/h264_sps_pps_tracker.h ('k') | webrtc/modules/video_coding/h264_sps_pps_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698