qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 COMMON_VIDEO_H265_H265_BITSTREAM_PARSER_H_ |
| 12 | #define COMMON_VIDEO_H265_H265_BITSTREAM_PARSER_H_ |
| 13 | |
| 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 17 | #include <optional> |
qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 20 | #include "api/video_codecs/bitstream_parser.h" |
| 21 | #include "common_video/h265/h265_pps_parser.h" |
| 22 | #include "common_video/h265/h265_sps_parser.h" |
| 23 | #include "common_video/h265/h265_vps_parser.h" |
| 24 | #include "rtc_base/containers/flat_map.h" |
Qiu Jianlin | c32a509 | 2024-02-23 02:34:57 | [diff] [blame] | 25 | #include "rtc_base/system/rtc_export.h" |
qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | // Stateful H265 bitstream parser (due to VPS/SPS/PPS). Used to parse out QP |
| 30 | // values from the bitstream. |
Qiu Jianlin | c32a509 | 2024-02-23 02:34:57 | [diff] [blame] | 31 | class RTC_EXPORT H265BitstreamParser : public BitstreamParser { |
qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 32 | public: |
| 33 | H265BitstreamParser(); |
| 34 | ~H265BitstreamParser() override; |
| 35 | |
| 36 | // New interface. |
| 37 | void ParseBitstream(rtc::ArrayView<const uint8_t> bitstream) override; |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 38 | std::optional<int> GetLastSliceQp() const override; |
qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 39 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 40 | std::optional<uint32_t> GetLastSlicePpsId() const; |
Qiu Jianlin | c32a509 | 2024-02-23 02:34:57 | [diff] [blame] | 41 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 42 | static std::optional<uint32_t> ParsePpsIdFromSliceSegmentLayerRbsp( |
Sergio Garcia Murillo | 45e5e38 | 2024-07-17 17:33:37 | [diff] [blame] | 43 | rtc::ArrayView<const uint8_t> data, |
qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 44 | uint8_t nalu_type); |
| 45 | |
| 46 | protected: |
| 47 | enum Result { |
| 48 | kOk, |
| 49 | kInvalidStream, |
| 50 | kUnsupportedStream, |
| 51 | }; |
Sergio Garcia Murillo | 45e5e38 | 2024-07-17 17:33:37 | [diff] [blame] | 52 | void ParseSlice(rtc::ArrayView<const uint8_t> slice); |
| 53 | Result ParseNonParameterSetNalu(rtc::ArrayView<const uint8_t> source, |
qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 54 | uint8_t nalu_type); |
| 55 | |
| 56 | const H265PpsParser::PpsState* GetPPS(uint32_t id) const; |
| 57 | const H265SpsParser::SpsState* GetSPS(uint32_t id) const; |
| 58 | |
| 59 | // VPS/SPS/PPS state, updated when parsing new VPS/SPS/PPS, used to parse |
| 60 | // slices. |
| 61 | flat_map<uint32_t, H265VpsParser::VpsState> vps_; |
| 62 | flat_map<uint32_t, H265SpsParser::SpsState> sps_; |
| 63 | flat_map<uint32_t, H265PpsParser::PpsState> pps_; |
| 64 | |
| 65 | // Last parsed slice QP. |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 66 | std::optional<int32_t> last_slice_qp_delta_; |
| 67 | std::optional<uint32_t> last_slice_pps_id_; |
qwu16 | 972f283 | 2023-08-15 09:16:54 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | } // namespace webrtc |
| 71 | |
| 72 | #endif // COMMON_VIDEO_H265_H265_BITSTREAM_PARSER_H_ |