xref: /aosp_15_r20/external/webrtc/common_video/h264/h264_bitstream_parser.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #ifndef COMMON_VIDEO_H264_H264_BITSTREAM_PARSER_H_
12 #define COMMON_VIDEO_H264_H264_BITSTREAM_PARSER_H_
13 #include <stddef.h>
14 #include <stdint.h>
15 
16 #include "absl/types/optional.h"
17 #include "api/video_codecs/bitstream_parser.h"
18 #include "common_video/h264/pps_parser.h"
19 #include "common_video/h264/sps_parser.h"
20 
21 namespace webrtc {
22 
23 // Stateful H264 bitstream parser (due to SPS/PPS). Used to parse out QP values
24 // from the bitstream.
25 // TODO(pbos): Unify with RTP SPS parsing and only use one H264 parser.
26 // TODO(pbos): If/when this gets used on the receiver side CHECKs must be
27 // removed and gracefully abort as we have no control over receive-side
28 // bitstreams.
29 class H264BitstreamParser : public BitstreamParser {
30  public:
31   H264BitstreamParser();
32   ~H264BitstreamParser() override;
33 
34   void ParseBitstream(rtc::ArrayView<const uint8_t> bitstream) override;
35   absl::optional<int> GetLastSliceQp() const override;
36 
37  protected:
38   enum Result {
39     kOk,
40     kInvalidStream,
41     kUnsupportedStream,
42   };
43   void ParseSlice(const uint8_t* slice, size_t length);
44   Result ParseNonParameterSetNalu(const uint8_t* source,
45                                   size_t source_length,
46                                   uint8_t nalu_type);
47 
48   // SPS/PPS state, updated when parsing new SPS/PPS, used to parse slices.
49   absl::optional<SpsParser::SpsState> sps_;
50   absl::optional<PpsParser::PpsState> pps_;
51 
52   // Last parsed slice QP.
53   absl::optional<int32_t> last_slice_qp_delta_;
54 };
55 
56 }  // namespace webrtc
57 
58 #endif  // COMMON_VIDEO_H264_H264_BITSTREAM_PARSER_H_
59