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