| 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 |
| 11 #include "webrtc/modules/video_coding/h264_sps_pps_tracker.h" | 11 #include "webrtc/modules/video_coding/h264_sps_pps_tracker.h" |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
| 17 #include "webrtc/common_video/h264/h264_common.h" | 17 #include "webrtc/common_video/h264/h264_common.h" |
| 18 #include "webrtc/modules/video_coding/frame_object.h" | 18 #include "webrtc/modules/video_coding/frame_object.h" |
| 19 #include "webrtc/modules/video_coding/packet_buffer.h" | 19 #include "webrtc/modules/video_coding/packet_buffer.h" |
| 20 | 20 |
| 21 namespace webrtc { | 21 namespace webrtc { |
| 22 namespace video_coding { | 22 namespace video_coding { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 const uint8_t start_code_h264[] = {0, 0, 0, 1}; | 25 const uint8_t start_code_h264[] = {0, 0, 0, 1}; |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 bool H264SpsPpsTracker::CopyAndFixBitstream(VCMPacket* packet) { | 28 H264SpsPpsTracker::PacketAction H264SpsPpsTracker::CopyAndFixBitstream( |
| 29 VCMPacket* packet) { |
| 29 RTC_DCHECK(packet->codec == kVideoCodecH264); | 30 RTC_DCHECK(packet->codec == kVideoCodecH264); |
| 30 | 31 |
| 31 const uint8_t* data = packet->dataPtr; | 32 const uint8_t* data = packet->dataPtr; |
| 32 const size_t data_size = packet->sizeBytes; | 33 const size_t data_size = packet->sizeBytes; |
| 33 const RTPVideoHeader& video_header = packet->video_header; | 34 const RTPVideoHeader& video_header = packet->video_header; |
| 34 const RTPVideoHeaderH264& codec_header = video_header.codecHeader.H264; | 35 const RTPVideoHeaderH264& codec_header = video_header.codecHeader.H264; |
| 35 | 36 |
| 36 // Packets that only contains SPS/PPS are not decodable by themselves, and | 37 // Packets that only contains SPS/PPS are not decodable by themselves, and |
| 37 // to avoid frames being created containing only these two nalus we don't | 38 // to avoid frames being created containing only these two nalus we don't |
| 38 // insert them into the PacketBuffer. Instead we save the SPS/PPS and | 39 // insert them into the PacketBuffer. Instead we save the SPS/PPS and |
| (...skipping 23 matching lines...) Expand all Loading... |
| 62 nalu.size); | 63 nalu.size); |
| 63 break; | 64 break; |
| 64 } | 65 } |
| 65 case H264::NaluType::kIdr: { | 66 case H264::NaluType::kIdr: { |
| 66 // If this is the first packet of an IDR, make sure we have the required | 67 // If this is the first packet of an IDR, make sure we have the required |
| 67 // SPS/PPS and also calculate how much extra space we need in the buffer | 68 // SPS/PPS and also calculate how much extra space we need in the buffer |
| 68 // to prepend the SPS/PPS to the bitstream with start codes. | 69 // to prepend the SPS/PPS to the bitstream with start codes. |
| 69 if (video_header.isFirstPacket) { | 70 if (video_header.isFirstPacket) { |
| 70 if (nalu.pps_id == -1) { | 71 if (nalu.pps_id == -1) { |
| 71 LOG(LS_WARNING) << "No PPS id in IDR nalu."; | 72 LOG(LS_WARNING) << "No PPS id in IDR nalu."; |
| 72 return false; | 73 return kRequestKeyframe; |
| 73 } | 74 } |
| 74 | 75 |
| 75 auto pps = pps_data_.find(nalu.pps_id); | 76 auto pps = pps_data_.find(nalu.pps_id); |
| 76 if (pps == pps_data_.end()) { | 77 if (pps == pps_data_.end()) { |
| 77 LOG(LS_WARNING) << "No PPS with id << " << nalu.pps_id | 78 LOG(LS_WARNING) << "No PPS with id << " << nalu.pps_id |
| 78 << " received"; | 79 << " received"; |
| 79 return false; | 80 return kRequestKeyframe; |
| 80 } | 81 } |
| 81 | 82 |
| 82 auto sps = sps_data_.find(pps->second.sps_id); | 83 auto sps = sps_data_.find(pps->second.sps_id); |
| 83 if (sps == sps_data_.end()) { | 84 if (sps == sps_data_.end()) { |
| 84 LOG(LS_WARNING) << "No SPS with id << " | 85 LOG(LS_WARNING) << "No SPS with id << " |
| 85 << pps_data_[nalu.pps_id].sps_id << " received"; | 86 << pps_data_[nalu.pps_id].sps_id << " received"; |
| 86 return false; | 87 return kRequestKeyframe; |
| 87 } | 88 } |
| 88 | 89 |
| 89 pps_id = nalu.pps_id; | 90 pps_id = nalu.pps_id; |
| 90 required_size += pps->second.size + sizeof(start_code_h264); | 91 required_size += pps->second.size + sizeof(start_code_h264); |
| 91 required_size += sps->second.size + sizeof(start_code_h264); | 92 required_size += sps->second.size + sizeof(start_code_h264); |
| 92 } | 93 } |
| 93 FALLTHROUGH(); | 94 FALLTHROUGH(); |
| 94 } | 95 } |
| 95 default: { | 96 default: { |
| 96 // Something other than an SPS/PPS nalu in this packet, then it should | 97 // Something other than an SPS/PPS nalu in this packet, then it should |
| 97 // be inserted into the PacketBuffer. | 98 // be inserted into the PacketBuffer. |
| 98 insert_packet = true; | 99 insert_packet = true; |
| 99 } | 100 } |
| 100 } | 101 } |
| 101 } | 102 } |
| 102 | 103 |
| 103 if (!insert_packet) | 104 if (!insert_packet) |
| 104 return false; | 105 return kDrop; |
| 105 | 106 |
| 106 // Calculate how much space we need for the rest of the bitstream. | 107 // Calculate how much space we need for the rest of the bitstream. |
| 107 if (codec_header.packetization_type == kH264StapA) { | 108 if (codec_header.packetization_type == kH264StapA) { |
| 108 const uint8_t* nalu_ptr = data + 1; | 109 const uint8_t* nalu_ptr = data + 1; |
| 109 while (nalu_ptr < data + data_size) { | 110 while (nalu_ptr < data + data_size) { |
| 110 RTC_DCHECK(video_header.isFirstPacket); | 111 RTC_DCHECK(video_header.isFirstPacket); |
| 111 required_size += sizeof(start_code_h264); | 112 required_size += sizeof(start_code_h264); |
| 112 | 113 |
| 113 // The first two bytes describe the length of a segment. | 114 // The first two bytes describe the length of a segment. |
| 114 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1]; | 115 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1]; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); | 152 memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 152 insert_at += sizeof(start_code_h264); | 153 insert_at += sizeof(start_code_h264); |
| 153 | 154 |
| 154 // The first two bytes describe the length of a segment. | 155 // The first two bytes describe the length of a segment. |
| 155 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1]; | 156 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1]; |
| 156 nalu_ptr += 2; | 157 nalu_ptr += 2; |
| 157 | 158 |
| 158 size_t copy_end = nalu_ptr - data + segment_length; | 159 size_t copy_end = nalu_ptr - data + segment_length; |
| 159 if (copy_end > data_size) { | 160 if (copy_end > data_size) { |
| 160 delete[] buffer; | 161 delete[] buffer; |
| 161 return false; | 162 return kDrop; |
| 162 } | 163 } |
| 163 | 164 |
| 164 memcpy(insert_at, nalu_ptr, segment_length); | 165 memcpy(insert_at, nalu_ptr, segment_length); |
| 165 insert_at += segment_length; | 166 insert_at += segment_length; |
| 166 nalu_ptr += segment_length; | 167 nalu_ptr += segment_length; |
| 167 } | 168 } |
| 168 } else { | 169 } else { |
| 169 if (video_header.isFirstPacket) { | 170 if (video_header.isFirstPacket) { |
| 170 memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); | 171 memcpy(insert_at, start_code_h264, sizeof(start_code_h264)); |
| 171 insert_at += sizeof(start_code_h264); | 172 insert_at += sizeof(start_code_h264); |
| 172 } | 173 } |
| 173 memcpy(insert_at, data, data_size); | 174 memcpy(insert_at, data, data_size); |
| 174 } | 175 } |
| 175 | 176 |
| 176 packet->dataPtr = buffer; | 177 packet->dataPtr = buffer; |
| 177 packet->sizeBytes = required_size; | 178 packet->sizeBytes = required_size; |
| 178 return true; | 179 return kInsert; |
| 179 } | 180 } |
| 180 | 181 |
| 181 } // namespace video_coding | 182 } // namespace video_coding |
| 182 } // namespace webrtc | 183 } // namespace webrtc |
| OLD | NEW |