h26x_packet_buffer.h (3787B)
1 /* 2 * Copyright (c) 2021 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 #ifndef MODULES_VIDEO_CODING_H26X_PACKET_BUFFER_H_ 12 #define MODULES_VIDEO_CODING_H26X_PACKET_BUFFER_H_ 13 14 #include <array> 15 #include <cstddef> 16 #include <cstdint> 17 #include <map> 18 #include <memory> 19 #include <string> 20 #include <vector> 21 22 #include "absl/base/attributes.h" 23 #include "modules/video_coding/packet_buffer.h" 24 25 namespace webrtc { 26 27 class H26xPacketBuffer { 28 public: 29 // The H26xPacketBuffer does the same job as the PacketBuffer but for H264 and 30 // H265 only. To make it fit in with surronding code the PacketBuffer 31 // input/output classes are used. 32 using Packet = video_coding::PacketBuffer::Packet; 33 using InsertResult = video_coding::PacketBuffer::InsertResult; 34 35 // |h264_idr_only_keyframes_allowed| is ignored if H.265 is used. 36 explicit H26xPacketBuffer(bool h264_idr_only_keyframes_allowed); 37 38 ABSL_MUST_USE_RESULT InsertResult 39 InsertPacket(std::unique_ptr<Packet> packet); 40 ABSL_MUST_USE_RESULT InsertResult InsertPadding(uint16_t unwrapped_seq_num); 41 42 // Out of band supplied codec parameters for H.264. 43 void SetSpropParameterSets(const std::string& sprop_parameter_sets); 44 45 private: 46 // Stores PPS payload and the active SPS ID. 47 struct PpsInfo { 48 PpsInfo() = default; 49 PpsInfo(PpsInfo&& rhs) = default; 50 PpsInfo& operator=(PpsInfo&& rhs) = default; 51 ~PpsInfo() = default; 52 53 // The value of sps_seq_parameter_set_id for the active SPS. 54 uint32_t sps_id = 0; 55 // Payload size. 56 size_t size = 0; 57 std::unique_ptr<uint8_t[]> payload; 58 }; 59 60 // Stores SPS payload and picture size. 61 struct SpsInfo { 62 SpsInfo() = default; 63 SpsInfo(SpsInfo&& rhs) = default; 64 SpsInfo& operator=(SpsInfo&& rhs) = default; 65 ~SpsInfo() = default; 66 67 // The width and height of decoded pictures. 68 int width = -1; 69 int height = -1; 70 // Payload size. 71 size_t size = 0; 72 std::unique_ptr<uint8_t[]> payload; 73 }; 74 75 static constexpr int kBufferSize = 2048; 76 static constexpr int kNumTrackedSequences = 5; 77 78 std::unique_ptr<Packet>& GetPacket(int64_t unwrapped_seq_num); 79 bool BeginningOfStream(const Packet& packet) const; 80 InsertResult FindFrames(int64_t unwrapped_seq_num); 81 bool MaybeAssembleFrame(int64_t start_seq_num_unwrapped, 82 int64_t end_sequence_number_unwrapped, 83 InsertResult& result); 84 // Store SPS and PPS nalus. They will be used later when an IDR frame is 85 // received without SPS/PPS. 86 void InsertSpsPpsNalus(const std::vector<uint8_t>& sps, 87 const std::vector<uint8_t>& pps); 88 // Insert start code and paramter sets for H.264 payload, also update header 89 // if parameter sets are inserted. Return false if required SPS or PPS is not 90 // found. 91 bool FixH264Packet(Packet& packet); 92 93 // Indicates whether IDR frames without SPS and PPS are allowed. 94 const bool h264_idr_only_keyframes_allowed_; 95 std::array<std::unique_ptr<Packet>, kBufferSize> buffer_; 96 std::array<int64_t, kNumTrackedSequences> last_continuous_in_sequence_; 97 int64_t last_continuous_in_sequence_index_ = 0; 98 99 // Map from pps_pic_parameter_set_id to the PPS payload associated with this 100 // ID. 101 std::map<int, PpsInfo> pps_data_; 102 // Map from sps_video_parameter_set_id to the SPS payload associated with this 103 // ID. 104 std::map<int, SpsInfo> sps_data_; 105 }; 106 107 } // namespace webrtc 108 109 #endif // MODULES_VIDEO_CODING_H26X_PACKET_BUFFER_H_