OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2015 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 |
| 12 #include "webrtc/modules/video_coding/codecs/h264/h264_video_toolbox_nalu.h" |
| 13 |
| 14 #if defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
| 15 |
| 16 #include <CoreFoundation/CoreFoundation.h> |
| 17 #include <vector> |
| 18 |
| 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/logging.h" |
| 21 |
| 22 namespace webrtc { |
| 23 |
| 24 const char kAnnexBHeaderBytes[4] = {0, 0, 0, 1}; |
| 25 const size_t kAvccHeaderByteSize = sizeof(uint32_t); |
| 26 |
| 27 bool H264CMSampleBufferToAnnexBBuffer( |
| 28 CMSampleBufferRef avcc_sample_buffer, |
| 29 bool is_keyframe, |
| 30 rtc::Buffer* annexb_buffer, |
| 31 webrtc::RTPFragmentationHeader** out_header) { |
| 32 DCHECK(avcc_sample_buffer); |
| 33 DCHECK(out_header); |
| 34 *out_header = nullptr; |
| 35 |
| 36 // Get format description from the sample buffer. |
| 37 CMVideoFormatDescriptionRef description = |
| 38 CMSampleBufferGetFormatDescription(avcc_sample_buffer); |
| 39 if (description == nullptr) { |
| 40 LOG(LS_ERROR) << "Failed to get sample buffer's description."; |
| 41 return false; |
| 42 } |
| 43 |
| 44 // Get parameter set information. |
| 45 int nalu_header_size = 0; |
| 46 size_t param_set_count = 0; |
| 47 OSStatus status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex( |
| 48 description, 0, nullptr, nullptr, ¶m_set_count, &nalu_header_size); |
| 49 if (status != noErr) { |
| 50 LOG(LS_ERROR) << "Failed to get parameter set."; |
| 51 return false; |
| 52 } |
| 53 // TODO(tkchin): handle other potential sizes. |
| 54 DCHECK_EQ(nalu_header_size, 4); |
| 55 DCHECK_EQ(param_set_count, 2u); |
| 56 |
| 57 // Truncate any previous data in the buffer without changing its capacity. |
| 58 annexb_buffer->SetSize(0); |
| 59 |
| 60 size_t nalu_offset = 0; |
| 61 std::vector<size_t> frag_offsets; |
| 62 std::vector<size_t> frag_lengths; |
| 63 |
| 64 // Place all parameter sets at the front of buffer. |
| 65 if (is_keyframe) { |
| 66 size_t param_set_size = 0; |
| 67 const uint8_t* param_set = nullptr; |
| 68 for (size_t i = 0; i < param_set_count; ++i) { |
| 69 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex( |
| 70 description, i, ¶m_set, ¶m_set_size, nullptr, nullptr); |
| 71 if (status != noErr) { |
| 72 LOG(LS_ERROR) << "Failed to get parameter set."; |
| 73 return false; |
| 74 } |
| 75 // Update buffer. |
| 76 annexb_buffer->AppendData(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes)); |
| 77 annexb_buffer->AppendData(reinterpret_cast<const char*>(param_set), |
| 78 param_set_size); |
| 79 // Update fragmentation. |
| 80 frag_offsets.push_back(nalu_offset + sizeof(kAnnexBHeaderBytes)); |
| 81 frag_lengths.push_back(param_set_size); |
| 82 nalu_offset += sizeof(kAnnexBHeaderBytes) + param_set_size; |
| 83 } |
| 84 } |
| 85 |
| 86 // Get block buffer from the sample buffer. |
| 87 CMBlockBufferRef block_buffer = |
| 88 CMSampleBufferGetDataBuffer(avcc_sample_buffer); |
| 89 if (block_buffer == nullptr) { |
| 90 LOG(LS_ERROR) << "Failed to get sample buffer's block buffer."; |
| 91 return false; |
| 92 } |
| 93 CMBlockBufferRef contiguous_buffer = nullptr; |
| 94 // Make sure block buffer is contiguous. |
| 95 if (!CMBlockBufferIsRangeContiguous(block_buffer, 0, 0)) { |
| 96 status = CMBlockBufferCreateContiguous( |
| 97 nullptr, block_buffer, nullptr, nullptr, 0, 0, 0, &contiguous_buffer); |
| 98 if (status != noErr) { |
| 99 LOG(LS_ERROR) << "Failed to flatten non-contiguous block buffer: " |
| 100 << status; |
| 101 return false; |
| 102 } |
| 103 } else { |
| 104 contiguous_buffer = block_buffer; |
| 105 // Retain to make cleanup easier. |
| 106 CFRetain(contiguous_buffer); |
| 107 block_buffer = nullptr; |
| 108 } |
| 109 |
| 110 // Now copy the actual data. |
| 111 char* data_ptr = nullptr; |
| 112 size_t block_buffer_size = CMBlockBufferGetDataLength(contiguous_buffer); |
| 113 status = CMBlockBufferGetDataPointer(contiguous_buffer, 0, nullptr, nullptr, |
| 114 &data_ptr); |
| 115 if (status != noErr) { |
| 116 LOG(LS_ERROR) << "Failed to get block buffer data."; |
| 117 CFRelease(contiguous_buffer); |
| 118 return false; |
| 119 } |
| 120 size_t bytes_remaining = block_buffer_size; |
| 121 while (bytes_remaining > 0) { |
| 122 // The size type here must match |nalu_header_size|, we expect 4 bytes. |
| 123 // Read the length of the next packet of data. Must convert from big endian |
| 124 // to host endian. |
| 125 DCHECK_GE(bytes_remaining, (size_t)nalu_header_size); |
| 126 uint32_t* uint32_data_ptr = reinterpret_cast<uint32*>(data_ptr); |
| 127 uint32_t packet_size = CFSwapInt32BigToHost(*uint32_data_ptr); |
| 128 // Update buffer. |
| 129 annexb_buffer->AppendData(kAnnexBHeaderBytes, sizeof(kAnnexBHeaderBytes)); |
| 130 annexb_buffer->AppendData(data_ptr + nalu_header_size, packet_size); |
| 131 // Update fragmentation. |
| 132 frag_offsets.push_back(nalu_offset + sizeof(kAnnexBHeaderBytes)); |
| 133 frag_lengths.push_back(packet_size); |
| 134 nalu_offset += sizeof(kAnnexBHeaderBytes) + packet_size; |
| 135 |
| 136 size_t bytes_written = packet_size + nalu_header_size; |
| 137 bytes_remaining -= bytes_written; |
| 138 data_ptr += bytes_written; |
| 139 } |
| 140 DCHECK_EQ(bytes_remaining, (size_t)0); |
| 141 |
| 142 rtc::scoped_ptr<webrtc::RTPFragmentationHeader> header; |
| 143 header.reset(new webrtc::RTPFragmentationHeader()); |
| 144 header->VerifyAndAllocateFragmentationHeader(frag_offsets.size()); |
| 145 DCHECK_EQ(frag_lengths.size(), frag_offsets.size()); |
| 146 for (size_t i = 0; i < frag_offsets.size(); ++i) { |
| 147 header->fragmentationOffset[i] = frag_offsets[i]; |
| 148 header->fragmentationLength[i] = frag_lengths[i]; |
| 149 header->fragmentationPlType[i] = 0; |
| 150 header->fragmentationTimeDiff[i] = 0; |
| 151 } |
| 152 *out_header = header.release(); |
| 153 CFRelease(contiguous_buffer); |
| 154 return true; |
| 155 } |
| 156 |
| 157 bool H264AnnexBBufferToCMSampleBuffer( |
| 158 const uint8_t* annexb_buffer, |
| 159 size_t annexb_buffer_size, |
| 160 CMVideoFormatDescriptionRef video_format, |
| 161 CMSampleBufferRef* out_sample_buffer) { |
| 162 DCHECK(annexb_buffer); |
| 163 DCHECK(out_sample_buffer); |
| 164 *out_sample_buffer = nullptr; |
| 165 |
| 166 // The buffer we receive via RTP has 00 00 00 01 start code artifically |
| 167 // embedded by the RTP depacketizer. Extract NALU information. |
| 168 // TODO(tkchin): handle potential case where sps and pps are delivered |
| 169 // separately. |
| 170 uint8_t first_nalu_type = annexb_buffer[4] & 0x1f; |
| 171 bool is_first_nalu_type_sps = first_nalu_type == 0x7; |
| 172 |
| 173 AnnexBBufferReader reader(annexb_buffer, annexb_buffer_size); |
| 174 CMVideoFormatDescriptionRef description = nullptr; |
| 175 OSStatus status = noErr; |
| 176 if (is_first_nalu_type_sps) { |
| 177 // Parse the SPS and PPS into a CMVideoFormatDescription. |
| 178 const uint8_t* param_set_ptrs[2] = {}; |
| 179 size_t param_set_sizes[2] = {}; |
| 180 if (!reader.ReadNalu(¶m_set_ptrs[0], ¶m_set_sizes[0])) { |
| 181 LOG(LS_ERROR) << "Failed to read SPS"; |
| 182 return false; |
| 183 } |
| 184 if (!reader.ReadNalu(¶m_set_ptrs[1], ¶m_set_sizes[1])) { |
| 185 LOG(LS_ERROR) << "Failed to read PPS"; |
| 186 return false; |
| 187 } |
| 188 status = CMVideoFormatDescriptionCreateFromH264ParameterSets( |
| 189 kCFAllocatorDefault, 2, param_set_ptrs, param_set_sizes, 4, |
| 190 &description); |
| 191 if (status != noErr) { |
| 192 LOG(LS_ERROR) << "Failed to create video format description."; |
| 193 return false; |
| 194 } |
| 195 } else { |
| 196 DCHECK(video_format); |
| 197 description = video_format; |
| 198 // We don't need to retain, but it makes logic easier since we are creating |
| 199 // in the other block. |
| 200 CFRetain(description); |
| 201 } |
| 202 |
| 203 // Allocate memory as a block buffer. |
| 204 // TODO(tkchin): figure out how to use a pool. |
| 205 CMBlockBufferRef block_buffer = nullptr; |
| 206 status = CMBlockBufferCreateWithMemoryBlock( |
| 207 nullptr, nullptr, reader.BytesRemaining(), nullptr, nullptr, 0, |
| 208 reader.BytesRemaining(), kCMBlockBufferAssureMemoryNowFlag, |
| 209 &block_buffer); |
| 210 if (status != kCMBlockBufferNoErr) { |
| 211 LOG(LS_ERROR) << "Failed to create block buffer."; |
| 212 CFRelease(description); |
| 213 return false; |
| 214 } |
| 215 |
| 216 // Make sure block buffer is contiguous. |
| 217 CMBlockBufferRef contiguous_buffer = nullptr; |
| 218 if (!CMBlockBufferIsRangeContiguous(block_buffer, 0, 0)) { |
| 219 status = CMBlockBufferCreateContiguous( |
| 220 nullptr, block_buffer, nullptr, nullptr, 0, 0, 0, &contiguous_buffer); |
| 221 if (status != noErr) { |
| 222 LOG(LS_ERROR) << "Failed to flatten non-contiguous block buffer: " |
| 223 << status; |
| 224 CFRelease(description); |
| 225 CFRelease(block_buffer); |
| 226 return false; |
| 227 } |
| 228 } else { |
| 229 contiguous_buffer = block_buffer; |
| 230 block_buffer = nullptr; |
| 231 } |
| 232 |
| 233 // Get a raw pointer into allocated memory. |
| 234 size_t block_buffer_size = 0; |
| 235 char* data_ptr = nullptr; |
| 236 status = CMBlockBufferGetDataPointer(contiguous_buffer, 0, nullptr, |
| 237 &block_buffer_size, &data_ptr); |
| 238 if (status != kCMBlockBufferNoErr) { |
| 239 LOG(LS_ERROR) << "Failed to get block buffer data pointer."; |
| 240 CFRelease(description); |
| 241 CFRelease(contiguous_buffer); |
| 242 return false; |
| 243 } |
| 244 DCHECK(block_buffer_size == reader.BytesRemaining()); |
| 245 |
| 246 // Write Avcc NALUs into block buffer memory. |
| 247 AvccBufferWriter writer(reinterpret_cast<uint8_t*>(data_ptr), |
| 248 block_buffer_size); |
| 249 while (reader.BytesRemaining() > 0) { |
| 250 const uint8_t* nalu_data_ptr = nullptr; |
| 251 size_t nalu_data_size = 0; |
| 252 if (reader.ReadNalu(&nalu_data_ptr, &nalu_data_size)) { |
| 253 writer.WriteNalu(nalu_data_ptr, nalu_data_size); |
| 254 } |
| 255 } |
| 256 |
| 257 // Create sample buffer. |
| 258 status = CMSampleBufferCreate(nullptr, contiguous_buffer, true, nullptr, |
| 259 nullptr, description, 1, 0, nullptr, 0, nullptr, |
| 260 out_sample_buffer); |
| 261 if (status != noErr) { |
| 262 LOG(LS_ERROR) << "Failed to create sample buffer."; |
| 263 CFRelease(description); |
| 264 CFRelease(contiguous_buffer); |
| 265 return false; |
| 266 } |
| 267 CFRelease(description); |
| 268 CFRelease(contiguous_buffer); |
| 269 return true; |
| 270 } |
| 271 |
| 272 AnnexBBufferReader::AnnexBBufferReader(const uint8_t* annexb_buffer, |
| 273 size_t length) |
| 274 : start_(annexb_buffer), offset_(0), next_offset_(0), length_(length) { |
| 275 DCHECK(annexb_buffer); |
| 276 offset_ = FindNextNaluHeader(start_, length_, 0); |
| 277 next_offset_ = |
| 278 FindNextNaluHeader(start_, length_, offset_ + sizeof(kAnnexBHeaderBytes)); |
| 279 } |
| 280 |
| 281 bool AnnexBBufferReader::ReadNalu(const uint8_t** out_nalu, |
| 282 size_t* out_length) { |
| 283 DCHECK(out_nalu); |
| 284 DCHECK(out_length); |
| 285 *out_nalu = nullptr; |
| 286 *out_length = 0; |
| 287 |
| 288 size_t data_offset = offset_ + sizeof(kAnnexBHeaderBytes); |
| 289 if (data_offset > length_) { |
| 290 return false; |
| 291 } |
| 292 *out_nalu = start_ + data_offset; |
| 293 *out_length = next_offset_ - data_offset; |
| 294 offset_ = next_offset_; |
| 295 next_offset_ = |
| 296 FindNextNaluHeader(start_, length_, offset_ + sizeof(kAnnexBHeaderBytes)); |
| 297 return true; |
| 298 } |
| 299 |
| 300 size_t AnnexBBufferReader::BytesRemaining() const { |
| 301 return length_ - offset_; |
| 302 } |
| 303 |
| 304 size_t AnnexBBufferReader::FindNextNaluHeader(const uint8_t* start, |
| 305 size_t length, |
| 306 size_t offset) const { |
| 307 DCHECK(start); |
| 308 if (offset + sizeof(kAnnexBHeaderBytes) > length) { |
| 309 return length; |
| 310 } |
| 311 // NALUs are separated by an 00 00 00 01 header. Scan the byte stream |
| 312 // starting from the offset for the next such sequence. |
| 313 const uint8_t* current = start + offset; |
| 314 // The loop reads sizeof(kAnnexBHeaderBytes) at a time, so stop when there |
| 315 // aren't enough bytes remaining. |
| 316 const uint8_t* const end = start + length - sizeof(kAnnexBHeaderBytes); |
| 317 while (current < end) { |
| 318 if (current[3] > 1) { |
| 319 current += 4; |
| 320 } else if (current[3] == 1 && current[2] == 0 && current[1] == 0 && |
| 321 current[0] == 0) { |
| 322 return current - start; |
| 323 } else { |
| 324 ++current; |
| 325 } |
| 326 } |
| 327 return length; |
| 328 } |
| 329 |
| 330 AvccBufferWriter::AvccBufferWriter(uint8_t* const avcc_buffer, size_t length) |
| 331 : start_(avcc_buffer), offset_(0), length_(length) { |
| 332 DCHECK(avcc_buffer); |
| 333 } |
| 334 |
| 335 bool AvccBufferWriter::WriteNalu(const uint8_t* data, size_t data_size) { |
| 336 // Check if we can write this length of data. |
| 337 if (data_size + kAvccHeaderByteSize > BytesRemaining()) { |
| 338 return false; |
| 339 } |
| 340 // Write length header, which needs to be big endian. |
| 341 uint32_t big_endian_length = CFSwapInt32HostToBig(data_size); |
| 342 memcpy(start_ + offset_, &big_endian_length, sizeof(big_endian_length)); |
| 343 offset_ += sizeof(big_endian_length); |
| 344 // Write data. |
| 345 memcpy(start_ + offset_, data, data_size); |
| 346 offset_ += data_size; |
| 347 return true; |
| 348 } |
| 349 |
| 350 size_t AvccBufferWriter::BytesRemaining() const { |
| 351 return length_ - offset_; |
| 352 } |
| 353 |
| 354 } // namespace webrtc |
| 355 |
| 356 #endif // defined(WEBRTC_VIDEO_TOOLBOX_SUPPORTED) |
OLD | NEW |