blob: 3ac8a5a689d4ee7144183e92a9f504b7603aa420 [file] [log] [blame]
qwu16972f2832023-08-15 09:16:541/*
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 Castelli8037fc62024-08-29 13:00:4017#include <optional>
qwu16972f2832023-08-15 09:16:5418#include <vector>
19
qwu16972f2832023-08-15 09:16:5420#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 Jianlinc32a5092024-02-23 02:34:5725#include "rtc_base/system/rtc_export.h"
qwu16972f2832023-08-15 09:16:5426
27namespace webrtc {
28
29// Stateful H265 bitstream parser (due to VPS/SPS/PPS). Used to parse out QP
30// values from the bitstream.
Qiu Jianlinc32a5092024-02-23 02:34:5731class RTC_EXPORT H265BitstreamParser : public BitstreamParser {
qwu16972f2832023-08-15 09:16:5432 public:
33 H265BitstreamParser();
34 ~H265BitstreamParser() override;
35
36 // New interface.
37 void ParseBitstream(rtc::ArrayView<const uint8_t> bitstream) override;
Florent Castelli8037fc62024-08-29 13:00:4038 std::optional<int> GetLastSliceQp() const override;
qwu16972f2832023-08-15 09:16:5439
Florent Castelli8037fc62024-08-29 13:00:4040 std::optional<uint32_t> GetLastSlicePpsId() const;
Qiu Jianlinc32a5092024-02-23 02:34:5741
Florent Castelli8037fc62024-08-29 13:00:4042 static std::optional<uint32_t> ParsePpsIdFromSliceSegmentLayerRbsp(
Sergio Garcia Murillo45e5e382024-07-17 17:33:3743 rtc::ArrayView<const uint8_t> data,
qwu16972f2832023-08-15 09:16:5444 uint8_t nalu_type);
45
46 protected:
47 enum Result {
48 kOk,
49 kInvalidStream,
50 kUnsupportedStream,
51 };
Sergio Garcia Murillo45e5e382024-07-17 17:33:3752 void ParseSlice(rtc::ArrayView<const uint8_t> slice);
53 Result ParseNonParameterSetNalu(rtc::ArrayView<const uint8_t> source,
qwu16972f2832023-08-15 09:16:5454 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 Castelli8037fc62024-08-29 13:00:4066 std::optional<int32_t> last_slice_qp_delta_;
67 std::optional<uint32_t> last_slice_pps_id_;
qwu16972f2832023-08-15 09:16:5468};
69
70} // namespace webrtc
71
72#endif // COMMON_VIDEO_H265_H265_BITSTREAM_PARSER_H_