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 SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_ 13 #define SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_ 14 15 #include "modules/video_coding/codecs/h264/include/h264.h" 16 17 #include <CoreMedia/CoreMedia.h> 18 #include <vector> 19 20 #include "common_video/h264/h264_common.h" 21 #include "modules/include/module_common_types.h" 22 #include "rtc_base/buffer.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 std::unique_ptr<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 CMMemoryPoolRef memory_pool); 49 50 // Returns a video format description created from the sps/pps information in 51 // the Annex B buffer. If there is no such information, nullptr is returned. 52 // The caller is responsible for releasing the description. 53 CMVideoFormatDescriptionRef CreateVideoFormatDescription( 54 const uint8_t* annexb_buffer, 55 size_t annexb_buffer_size); 56 57 // Helper class for reading NALUs from an RTP Annex B buffer. 58 class AnnexBBufferReader final { 59 public: 60 AnnexBBufferReader(const uint8_t* annexb_buffer, size_t length); 61 ~AnnexBBufferReader(); 62 AnnexBBufferReader(const AnnexBBufferReader& other) = delete; 63 void operator=(const AnnexBBufferReader& other) = delete; 64 65 // Returns a pointer to the beginning of the next NALU slice without the 66 // header bytes and its length. Returns false if no more slices remain. 67 bool ReadNalu(const uint8_t** out_nalu, size_t* out_length); 68 69 // Returns the number of unread NALU bytes, including the size of the header. 70 // If the buffer has no remaining NALUs this will return zero. 71 size_t BytesRemaining() const; 72 73 // Reset the reader to start reading from the first NALU 74 void SeekToStart(); 75 76 // Seek to the next position that holds a NALU of the desired type, 77 // or the end if no such NALU is found. 78 // Return true if a NALU of the desired type is found, false if we 79 // reached the end instead 80 bool SeekToNextNaluOfType(H264::NaluType type); 81 82 private: 83 // Returns the the next offset that contains NALU data. 84 size_t FindNextNaluHeader(const uint8_t* start, 85 size_t length, 86 size_t offset) const; 87 88 const uint8_t* const start_; 89 std::vector<NaluIndex> offsets_; 90 std::vector<NaluIndex>::iterator offset_; 91 const size_t length_; 92 }; 93 94 // Helper class for writing NALUs using avcc format into a buffer. 95 class AvccBufferWriter final { 96 public: 97 AvccBufferWriter(uint8_t* const avcc_buffer, size_t length); ~AvccBufferWriter()98 ~AvccBufferWriter() {} 99 AvccBufferWriter(const AvccBufferWriter& other) = delete; 100 void operator=(const AvccBufferWriter& other) = delete; 101 102 // Writes the data slice into the buffer. Returns false if there isn't 103 // enough space left. 104 bool WriteNalu(const uint8_t* data, size_t data_size); 105 106 // Returns the unused bytes in the buffer. 107 size_t BytesRemaining() const; 108 109 private: 110 uint8_t* const start_; 111 size_t offset_; 112 const size_t length_; 113 }; 114 115 } // namespace webrtc 116 117 #endif // SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_ 118