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

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

Issue 2466993003: H264SpsPpsTracker class which keep tracks of SPS/PPS. (Closed)
Patch Set: Created 4 years, 1 month 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
(Empty)
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
11 #include "webrtc/modules/video_coding/h264_sps_pps_tracker.h"
12
13 #include <string>
14
15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/logging.h"
17 #include "webrtc/common_video/h264/h264_common.h"
18 #include "webrtc/modules/video_coding/frame_object.h"
19 #include "webrtc/modules/video_coding/packet_buffer.h"
20
21 namespace webrtc {
22 namespace video_coding {
23
24 namespace {
25 const uint8_t start_code_h264[] = {0, 0, 0, 1};
26 } // namespace
27
28 bool H264SpsPpsTracker::CopyAndFixBitstream(VCMPacket* packet) {
29 RTC_DCHECK(packet->codec == kVideoCodecH264);
30
31 const uint8_t* data = packet->dataPtr;
32 const size_t data_size = packet->sizeBytes;
33 const RTPVideoHeader& video_header = packet->video_header;
34 const RTPVideoHeaderH264& codec_header = video_header.codecHeader.H264;
35
36 // If we have nalus in this packet then we only want to insert this packet
37 // if it contains anything other than just an SPS/PPS nalu.
stefan-webrtc 2016/11/02 13:54:17 It would be better if this comment explained why t
philipel 2016/11/02 14:39:43 Done.
38 bool insert_packet = codec_header.nalus_length == 0 ? true : false;
39
40 int pps_id = -1;
41 size_t required_size = 0;
42 for (size_t i = 0; i < codec_header.nalus_length; ++i) {
43 const NaluInfo& nalu = codec_header.nalus[i];
44 switch (nalu.type) {
45 case H264::NaluType::kSps: {
46 // Save SPS.
47 sps_data_[nalu.sps_id].size = nalu.size;
48 sps_data_[nalu.sps_id].data.reset(new uint8_t[nalu.size]);
49 memcpy(sps_data_[nalu.sps_id].data.get(), data + nalu.offset,
50 nalu.size);
51 break;
52 }
53 case H264::NaluType::kPps: {
54 // Save PPS.
55 pps_data_[nalu.pps_id].sps_id = nalu.sps_id;
56 pps_data_[nalu.pps_id].size = nalu.size;
57 pps_data_[nalu.pps_id].data.reset(new uint8_t[nalu.size]);
58 memcpy(pps_data_[nalu.pps_id].data.get(), data + nalu.offset,
59 nalu.size);
60 break;
61 }
62 case H264::NaluType::kIdr: {
63 // If this is the first packet of an IDR, make sure we have the required
64 // SPS/PPS and also calculate how much extra space we need in the buffer
65 // to prepend the SPS/PPS to the bitstream with start codes.
66 if (video_header.isFirstPacket) {
67 if (nalu.pps_id == -1) {
68 LOG(LS_WARNING) << "No PPS id in IDR nalu.";
69 return false;
70 }
71
72 if (!pps_data_[nalu.pps_id].data) {
stefan-webrtc 2016/11/02 13:54:17 I think a find would be more appropriate here, or?
philipel 2016/11/02 14:39:43 Done.
73 LOG(LS_WARNING) << "No PPS with id << " << nalu.pps_id
74 << " received";
75 return false;
76 }
77
78 if (!sps_data_[pps_data_[nalu.pps_id].sps_id].data) {
79 LOG(LS_WARNING) << "No SPS with id << "
80 << pps_data_[nalu.pps_id].sps_id << " received";
81 return false;
82 }
83
84 pps_id = nalu.pps_id;
85 required_size += pps_data_[pps_id].size + sizeof(start_code_h264);
86 required_size += sps_data_[pps_data_[pps_id].sps_id].size +
87 sizeof(start_code_h264);
88 }
89 FALLTHROUGH();
90 }
91 default: {
stefan-webrtc 2016/11/02 13:54:17 What if this is a non-idr but referring to a pps w
philipel 2016/11/02 14:39:43 Then that packet will be inserted into the PacketB
92 // Something other than an SPS/PPS nalu in this packet, then it should
93 // be inserted into the PacketBuffer.
94 insert_packet = true;
95 }
96 }
97 }
98
99 if (!insert_packet)
100 return false;
101
102 // Calculate how much space we need for the rest of the bitstream.
103 if (codec_header.packetization_type == kH264StapA) {
104 const uint8_t* nalu_ptr = data + 1;
105 while (nalu_ptr < data + data_size) {
106 // StapA is always the first packet so start codes should always be
107 // inserted.
stefan-webrtc 2016/11/02 13:54:17 I think this comment is a bit weird. We should alw
philipel 2016/11/02 14:39:43 Right, removed confusing comment.
108 RTC_DCHECK(video_header.isFirstPacket);
109 required_size += sizeof(start_code_h264);
110
111 // The first two bytes describe the length of a segment.
112 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1];
113 nalu_ptr += 2;
114
115 required_size += segment_length;
116 nalu_ptr += segment_length;
117 }
118 } else {
119 if (video_header.isFirstPacket)
120 required_size += sizeof(start_code_h264);
121 required_size += data_size;
122 }
123
124 // Then we copy to the new buffer.
125 uint8_t* buffer = new uint8_t[required_size];
126 uint8_t* insert_at = buffer;
127
128 // If pps_id != -1 then we have the SPS/PPS and they should be prepended
129 // to the bitstream with start codes inserted.
stefan-webrtc 2016/11/02 13:54:17 So here we basically always add pps/sps in front o
philipel 2016/11/02 14:39:43 We only do it for the first packet of an IDR. If y
stefan-webrtc 2016/11/02 14:54:38 Right, maybe add a comment that we rely on sequenc
130 if (pps_id != -1) {
131 // Insert SPS.
132 memcpy(insert_at, start_code_h264, sizeof(start_code_h264));
133 insert_at += sizeof(start_code_h264);
134 memcpy(insert_at, sps_data_[pps_data_[pps_id].sps_id].data.get(),
135 sps_data_[pps_data_[pps_id].sps_id].size);
136 insert_at += sps_data_[pps_data_[pps_id].sps_id].size;
137
138 // Insert PPS.
139 memcpy(insert_at, start_code_h264, sizeof(start_code_h264));
140 insert_at += sizeof(start_code_h264);
141 memcpy(insert_at, pps_data_[pps_id].data.get(), pps_data_[pps_id].size);
142 insert_at += pps_data_[pps_id].size;
143 }
144
145 // Copy the rest of the bitstream and insert start codes.
146 if (codec_header.packetization_type == kH264StapA) {
147 const uint8_t* nalu_ptr = data + 1;
148 while (nalu_ptr < data + data_size) {
149 memcpy(insert_at, start_code_h264, sizeof(start_code_h264));
150 insert_at += sizeof(start_code_h264);
151
152 // The first two bytes describes the length of a segment.
153 uint16_t segment_length = nalu_ptr[0] << 8 | nalu_ptr[1];
154 nalu_ptr += 2;
155
156 memcpy(insert_at, nalu_ptr, segment_length);
157 insert_at += segment_length;
158 nalu_ptr += segment_length;
159 }
160 } else {
161 // Might be a FuA, which may or may not be the first packet.
162 if (video_header.isFirstPacket) {
163 memcpy(insert_at, start_code_h264, sizeof(start_code_h264));
164 insert_at += sizeof(start_code_h264);
165 }
166
167 memcpy(insert_at, data, data_size);
168 }
169
170 packet->dataPtr = buffer;
171 packet->sizeBytes = required_size;
172 return true;
173 }
174
175 } // namespace video_coding
176 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698