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