OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 | 11 |
12 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h" | 12 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h" |
13 | 13 |
14 #if defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) | 14 #if defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
15 | 15 |
16 #include <CoreFoundation/CoreFoundation.h> | 16 #include <CoreFoundation/CoreFoundation.h> |
17 #include <memory> | 17 #include <memory> |
18 #include <vector> | 18 #include <vector> |
19 | 19 |
20 #include "webrtc/base/checks.h" | 20 #include "webrtc/base/checks.h" |
21 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
22 #include "webrtc/common_video/h264/h264_common.h" | |
23 | 22 |
24 namespace webrtc { | 23 namespace webrtc { |
25 | 24 |
26 using H264::NaluType; | 25 using H264::NaluType; |
27 using H264::kAud; | 26 using H264::kAud; |
28 using H264::kSps; | 27 using H264::kSps; |
29 using H264::ParseNaluType; | 28 using H264::ParseNaluType; |
29 using H264::NaluIndex; | |
tkchin_webrtc
2016/09/27 11:29:35
nit: consider alphabetizing
kthelgason
2016/09/28 08:48:27
Done.
| |
30 | 30 |
31 const char kAnnexBHeaderBytes[4] = {0, 0, 0, 1}; | 31 const char kAnnexBHeaderBytes[4] = {0, 0, 0, 1}; |
32 const size_t kAvccHeaderByteSize = sizeof(uint32_t); | 32 const size_t kAvccHeaderByteSize = sizeof(uint32_t); |
33 | 33 |
34 bool H264CMSampleBufferToAnnexBBuffer( | 34 bool H264CMSampleBufferToAnnexBBuffer( |
35 CMSampleBufferRef avcc_sample_buffer, | 35 CMSampleBufferRef avcc_sample_buffer, |
36 bool is_keyframe, | 36 bool is_keyframe, |
37 rtc::Buffer* annexb_buffer, | 37 rtc::Buffer* annexb_buffer, |
38 webrtc::RTPFragmentationHeader** out_header) { | 38 webrtc::RTPFragmentationHeader** out_header) { |
39 RTC_DCHECK(avcc_sample_buffer); | 39 RTC_DCHECK(avcc_sample_buffer); |
(...skipping 10 matching lines...) Expand all Loading... | |
50 | 50 |
51 // Get parameter set information. | 51 // Get parameter set information. |
52 int nalu_header_size = 0; | 52 int nalu_header_size = 0; |
53 size_t param_set_count = 0; | 53 size_t param_set_count = 0; |
54 OSStatus status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex( | 54 OSStatus status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex( |
55 description, 0, nullptr, nullptr, ¶m_set_count, &nalu_header_size); | 55 description, 0, nullptr, nullptr, ¶m_set_count, &nalu_header_size); |
56 if (status != noErr) { | 56 if (status != noErr) { |
57 LOG(LS_ERROR) << "Failed to get parameter set."; | 57 LOG(LS_ERROR) << "Failed to get parameter set."; |
58 return false; | 58 return false; |
59 } | 59 } |
60 // TODO(tkchin): handle other potential sizes. | |
61 RTC_DCHECK_EQ(nalu_header_size, 4); | |
tkchin_webrtc
2016/09/27 11:29:35
why is this removed? Perhaps should be checked tha
kthelgason
2016/09/28 08:48:27
That doesn't make sense to me, won't kAvccHeaderBy
tkchin_webrtc
2016/09/28 11:33:24
iiuc nalu_header_size here is not the 0001/001. It
| |
62 RTC_DCHECK_EQ(param_set_count, 2u); | 60 RTC_DCHECK_EQ(param_set_count, 2u); |
63 | 61 |
64 // Truncate any previous data in the buffer without changing its capacity. | 62 // Truncate any previous data in the buffer without changing its capacity. |
65 annexb_buffer->SetSize(0); | 63 annexb_buffer->SetSize(0); |
66 | 64 |
67 size_t nalu_offset = 0; | 65 size_t nalu_offset = 0; |
68 std::vector<size_t> frag_offsets; | 66 std::vector<size_t> frag_offsets; |
69 std::vector<size_t> frag_lengths; | 67 std::vector<size_t> frag_lengths; |
70 | 68 |
71 // Place all parameter sets at the front of buffer. | 69 // Place all parameter sets at the front of buffer. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 uint32_t* uint32_data_ptr = reinterpret_cast<uint32_t*>(data_ptr); | 131 uint32_t* uint32_data_ptr = reinterpret_cast<uint32_t*>(data_ptr); |
134 uint32_t packet_size = CFSwapInt32BigToHost(*uint32_data_ptr); | 132 uint32_t packet_size = CFSwapInt32BigToHost(*uint32_data_ptr); |
135 // Update buffer. | 133 // Update buffer. |
136 annexb_buffer->AppendData(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes)); | 134 annexb_buffer->AppendData(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes)); |
137 annexb_buffer->AppendData(data_ptr + nalu_header_size, packet_size); | 135 annexb_buffer->AppendData(data_ptr + nalu_header_size, packet_size); |
138 // Update fragmentation. | 136 // Update fragmentation. |
139 frag_offsets.push_back(nalu_offset + sizeof(kAnnexBHeaderBytes)); | 137 frag_offsets.push_back(nalu_offset + sizeof(kAnnexBHeaderBytes)); |
140 frag_lengths.push_back(packet_size); | 138 frag_lengths.push_back(packet_size); |
141 nalu_offset += sizeof(kAnnexBHeaderBytes) + packet_size; | 139 nalu_offset += sizeof(kAnnexBHeaderBytes) + packet_size; |
142 | 140 |
143 size_t bytes_written = packet_size + nalu_header_size; | 141 size_t bytes_written = packet_size + sizeof(kAnnexBHeaderBytes); |
tkchin_webrtc
2016/09/27 11:29:35
good catch
| |
144 bytes_remaining -= bytes_written; | 142 bytes_remaining -= bytes_written; |
145 data_ptr += bytes_written; | 143 data_ptr += bytes_written; |
146 } | 144 } |
147 RTC_DCHECK_EQ(bytes_remaining, (size_t)0); | 145 RTC_DCHECK_EQ(bytes_remaining, (size_t)0); |
148 | 146 |
149 std::unique_ptr<webrtc::RTPFragmentationHeader> header; | 147 std::unique_ptr<webrtc::RTPFragmentationHeader> header; |
150 header.reset(new webrtc::RTPFragmentationHeader()); | 148 header.reset(new webrtc::RTPFragmentationHeader()); |
151 header->VerifyAndAllocateFragmentationHeader(frag_offsets.size()); | 149 header->VerifyAndAllocateFragmentationHeader(frag_offsets.size()); |
152 RTC_DCHECK_EQ(frag_lengths.size(), frag_offsets.size()); | 150 RTC_DCHECK_EQ(frag_lengths.size(), frag_offsets.size()); |
153 for (size_t i = 0; i < frag_offsets.size(); ++i) { | 151 for (size_t i = 0; i < frag_offsets.size(); ++i) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 &description); | 303 &description); |
306 if (status != noErr) { | 304 if (status != noErr) { |
307 LOG(LS_ERROR) << "Failed to create video format description."; | 305 LOG(LS_ERROR) << "Failed to create video format description."; |
308 return nullptr; | 306 return nullptr; |
309 } | 307 } |
310 return description; | 308 return description; |
311 } | 309 } |
312 | 310 |
313 AnnexBBufferReader::AnnexBBufferReader(const uint8_t* annexb_buffer, | 311 AnnexBBufferReader::AnnexBBufferReader(const uint8_t* annexb_buffer, |
314 size_t length) | 312 size_t length) |
315 : start_(annexb_buffer), offset_(0), next_offset_(0), length_(length) { | 313 : start_(annexb_buffer), length_(length) { |
316 RTC_DCHECK(annexb_buffer); | 314 RTC_DCHECK(annexb_buffer); |
317 offset_ = FindNextNaluHeader(start_, length_, 0); | 315 offsets_ = H264::FindNaluIndices(annexb_buffer, length); |
tkchin_webrtc
2016/09/27 11:29:34
How large do we expect these buffers to be? Is it
kthelgason
2016/09/28 08:48:27
I have no idea, didn't consider that. In what cont
tkchin_webrtc
2016/09/28 11:33:24
It probably doesn't matter in this particular case
| |
318 next_offset_ = | 316 offset_ = offsets_.begin(); |
319 FindNextNaluHeader(start_, length_, offset_ + sizeof(kAnnexBHeaderBytes)); | |
320 } | 317 } |
321 | 318 |
322 bool AnnexBBufferReader::ReadNalu(const uint8_t** out_nalu, | 319 bool AnnexBBufferReader::ReadNalu(const uint8_t** out_nalu, |
323 size_t* out_length) { | 320 size_t* out_length) { |
324 RTC_DCHECK(out_nalu); | 321 RTC_DCHECK(out_nalu); |
325 RTC_DCHECK(out_length); | 322 RTC_DCHECK(out_length); |
326 *out_nalu = nullptr; | 323 *out_nalu = nullptr; |
327 *out_length = 0; | 324 *out_length = 0; |
328 | 325 |
329 size_t data_offset = offset_ + sizeof(kAnnexBHeaderBytes); | 326 if (offset_ == offsets_.end()) { |
330 if (data_offset > length_) { | |
331 return false; | 327 return false; |
332 } | 328 } |
333 *out_nalu = start_ + data_offset; | 329 *out_nalu = start_ + offset_->payload_start_offset; |
334 *out_length = next_offset_ - data_offset; | 330 *out_length = offset_->payload_size; |
335 offset_ = next_offset_; | 331 ++offset_; |
336 next_offset_ = | |
337 FindNextNaluHeader(start_, length_, offset_ + sizeof(kAnnexBHeaderBytes)); | |
338 return true; | 332 return true; |
339 } | 333 } |
340 | 334 |
341 size_t AnnexBBufferReader::BytesRemaining() const { | 335 size_t AnnexBBufferReader::BytesRemaining() const { |
342 return length_ - offset_; | 336 if (offset_ == offsets_.end()) { |
343 } | 337 return 0; |
344 | |
345 size_t AnnexBBufferReader::FindNextNaluHeader(const uint8_t* start, | |
346 size_t length, | |
347 size_t offset) const { | |
348 RTC_DCHECK(start); | |
349 if (offset + sizeof(kAnnexBHeaderBytes) > length) { | |
350 return length; | |
351 } | 338 } |
352 // NALUs are separated by an 00 00 00 01 header. Scan the byte stream | 339 return length_ - offset_->start_offset; |
353 // starting from the offset for the next such sequence. | |
354 const uint8_t* current = start + offset; | |
355 // The loop reads sizeof(kAnnexBHeaderBytes) at a time, so stop when there | |
356 // aren't enough bytes remaining. | |
357 const uint8_t* const end = start + length - sizeof(kAnnexBHeaderBytes); | |
358 while (current < end) { | |
359 if (current[3] > 1) { | |
360 current += 4; | |
361 } else if (current[3] == 1 && current[2] == 0 && current[1] == 0 && | |
362 current[0] == 0) { | |
363 return current - start; | |
364 } else { | |
365 ++current; | |
366 } | |
367 } | |
368 return length; | |
369 } | 340 } |
370 | 341 |
371 AvccBufferWriter::AvccBufferWriter(uint8_t* const avcc_buffer, size_t length) | 342 AvccBufferWriter::AvccBufferWriter(uint8_t* const avcc_buffer, size_t length) |
372 : start_(avcc_buffer), offset_(0), length_(length) { | 343 : start_(avcc_buffer), offset_(0), length_(length) { |
373 RTC_DCHECK(avcc_buffer); | 344 RTC_DCHECK(avcc_buffer); |
374 } | 345 } |
375 | 346 |
376 bool AvccBufferWriter::WriteNalu(const uint8_t* data, size_t data_size) { | 347 bool AvccBufferWriter::WriteNalu(const uint8_t* data, size_t data_size) { |
377 // Check if we can write this length of data. | 348 // Check if we can write this length of data. |
378 if (data_size + kAvccHeaderByteSize > BytesRemaining()) { | 349 if (data_size + kAvccHeaderByteSize > BytesRemaining()) { |
379 return false; | 350 return false; |
380 } | 351 } |
381 // Write length header, which needs to be big endian. | 352 // Write length header, which needs to be big endian. |
382 uint32_t big_endian_length = CFSwapInt32HostToBig(data_size); | 353 uint32_t big_endian_length = CFSwapInt32HostToBig(data_size); |
383 memcpy(start_ + offset_, &big_endian_length, sizeof(big_endian_length)); | 354 memcpy(start_ + offset_, &big_endian_length, sizeof(big_endian_length)); |
384 offset_ += sizeof(big_endian_length); | 355 offset_ += sizeof(big_endian_length); |
385 // Write data. | 356 // Write data. |
386 memcpy(start_ + offset_, data, data_size); | 357 memcpy(start_ + offset_, data, data_size); |
387 offset_ += data_size; | 358 offset_ += data_size; |
388 return true; | 359 return true; |
389 } | 360 } |
390 | 361 |
391 size_t AvccBufferWriter::BytesRemaining() const { | 362 size_t AvccBufferWriter::BytesRemaining() const { |
392 return length_ - offset_; | 363 return length_ - offset_; |
393 } | 364 } |
394 | 365 |
395 } // namespace webrtc | 366 } // namespace webrtc |
396 | 367 |
397 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) | 368 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
OLD | NEW |