| 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 #ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_NALU_H_ | |
| 13 #define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_NALU_H_ | |
| 14 | |
| 15 #include "webrtc/modules/video_coding/codecs/h264/include/h264.h" | |
| 16 | |
| 17 #include <CoreMedia/CoreMedia.h> | |
| 18 #include <vector> | |
| 19 | |
| 20 #include "webrtc/base/buffer.h" | |
| 21 #include "webrtc/common_video/h264/h264_common.h" | |
| 22 #include "webrtc/modules/include/module_common_types.h" | |
| 23 | |
| 24 using webrtc::H264::NaluIndex; | |
| 25 | |
| 26 namespace webrtc { | |
| 27 | |
| 28 // Converts a sample buffer emitted from the VideoToolbox encoder into a buffer | |
| 29 // suitable for RTP. The sample buffer is in avcc format whereas the rtp buffer | |
| 30 // needs to be in Annex B format. Data is written directly to |annexb_buffer| | |
| 31 // and a new RTPFragmentationHeader is returned in |out_header|. | |
| 32 bool H264CMSampleBufferToAnnexBBuffer( | |
| 33 CMSampleBufferRef avcc_sample_buffer, | |
| 34 bool is_keyframe, | |
| 35 rtc::Buffer* annexb_buffer, | |
| 36 webrtc::RTPFragmentationHeader** out_header); | |
| 37 | |
| 38 // Converts a buffer received from RTP into a sample buffer suitable for the | |
| 39 // VideoToolbox decoder. The RTP buffer is in annex b format whereas the sample | |
| 40 // buffer is in avcc format. | |
| 41 // If |is_keyframe| is true then |video_format| is ignored since the format will | |
| 42 // be read from the buffer. Otherwise |video_format| must be provided. | |
| 43 // Caller is responsible for releasing the created sample buffer. | |
| 44 bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer, | |
| 45 size_t annexb_buffer_size, | |
| 46 CMVideoFormatDescriptionRef video_format, | |
| 47 CMSampleBufferRef* out_sample_buffer); | |
| 48 | |
| 49 // Returns true if the type of the first NALU in the supplied Annex B buffer is | |
| 50 // the SPS type. | |
| 51 bool H264AnnexBBufferHasVideoFormatDescription(const uint8_t* annexb_buffer, | |
| 52 size_t annexb_buffer_size); | |
| 53 | |
| 54 // Returns a video format description created from the sps/pps information in | |
| 55 // the Annex B buffer. If there is no such information, nullptr is returned. | |
| 56 // The caller is responsible for releasing the description. | |
| 57 CMVideoFormatDescriptionRef CreateVideoFormatDescription( | |
| 58 const uint8_t* annexb_buffer, | |
| 59 size_t annexb_buffer_size); | |
| 60 | |
| 61 // Helper class for reading NALUs from an RTP Annex B buffer. | |
| 62 class AnnexBBufferReader final { | |
| 63 public: | |
| 64 AnnexBBufferReader(const uint8_t* annexb_buffer, size_t length); | |
| 65 ~AnnexBBufferReader() {} | |
| 66 AnnexBBufferReader(const AnnexBBufferReader& other) = delete; | |
| 67 void operator=(const AnnexBBufferReader& other) = delete; | |
| 68 | |
| 69 // Returns a pointer to the beginning of the next NALU slice without the | |
| 70 // header bytes and its length. Returns false if no more slices remain. | |
| 71 bool ReadNalu(const uint8_t** out_nalu, size_t* out_length); | |
| 72 | |
| 73 // Returns the number of unread NALU bytes, including the size of the header. | |
| 74 // If the buffer has no remaining NALUs this will return zero. | |
| 75 size_t BytesRemaining() const; | |
| 76 | |
| 77 private: | |
| 78 // Returns the the next offset that contains NALU data. | |
| 79 size_t FindNextNaluHeader(const uint8_t* start, | |
| 80 size_t length, | |
| 81 size_t offset) const; | |
| 82 | |
| 83 const uint8_t* const start_; | |
| 84 std::vector<NaluIndex> offsets_; | |
| 85 std::vector<NaluIndex>::iterator offset_; | |
| 86 const size_t length_; | |
| 87 }; | |
| 88 | |
| 89 // Helper class for writing NALUs using avcc format into a buffer. | |
| 90 class AvccBufferWriter final { | |
| 91 public: | |
| 92 AvccBufferWriter(uint8_t* const avcc_buffer, size_t length); | |
| 93 ~AvccBufferWriter() {} | |
| 94 AvccBufferWriter(const AvccBufferWriter& other) = delete; | |
| 95 void operator=(const AvccBufferWriter& other) = delete; | |
| 96 | |
| 97 // Writes the data slice into the buffer. Returns false if there isn't | |
| 98 // enough space left. | |
| 99 bool WriteNalu(const uint8_t* data, size_t data_size); | |
| 100 | |
| 101 // Returns the unused bytes in the buffer. | |
| 102 size_t BytesRemaining() const; | |
| 103 | |
| 104 private: | |
| 105 uint8_t* const start_; | |
| 106 size_t offset_; | |
| 107 const size_t length_; | |
| 108 }; | |
| 109 | |
| 110 } // namespace webrtc | |
| 111 | |
| 112 #endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_NALU_H_ | |
| OLD | NEW |