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 WEBRTC_MODULES_RTP_RTCP_SOURCE_H264_BITSTREAM_PARSER_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_H264_BITSTREAM_PARSER_H_
13
14#include <stddef.h>
15#include <stdint.h>
16
17namespace rtc {
18class BitBuffer;
19}
20
21namespace 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.
29class H264BitstreamParser {
30 public:
31  // Parse an additional chunk of H264 bitstream.
32  void ParseBitstream(const uint8_t* bitstream, size_t length);
33
34  // Get the last extracted QP value from the parsed bitstream.
35  bool GetLastSliceQp(int* qp) const;
36
37 private:
38  // Captured in SPS and used when parsing slice NALUs.
39  struct SpsState {
40    SpsState();
41
42    uint32_t delta_pic_order_always_zero_flag = 0;
43    uint32_t separate_colour_plane_flag = 0;
44    uint32_t frame_mbs_only_flag = 0;
45    uint32_t log2_max_frame_num_minus4 = 0;
46    uint32_t log2_max_pic_order_cnt_lsb_minus4 = 0;
47    uint32_t pic_order_cnt_type = 0;
48  };
49
50  struct PpsState {
51    PpsState();
52
53    bool bottom_field_pic_order_in_frame_present_flag = false;
54    bool weighted_pred_flag = false;
55    uint32_t weighted_bipred_idc = false;
56    uint32_t redundant_pic_cnt_present_flag = 0;
57    int pic_init_qp_minus26 = 0;
58  };
59
60  void ParseSlice(const uint8_t* slice, size_t length);
61  bool ParseSpsNalu(const uint8_t* sps_nalu, size_t length);
62  bool ParsePpsNalu(const uint8_t* pps_nalu, size_t length);
63  bool ParseNonParameterSetNalu(const uint8_t* source,
64                                size_t source_length,
65                                uint8_t nalu_type);
66
67  // SPS/PPS state, updated when parsing new SPS/PPS, used to parse slices.
68  bool sps_parsed_ = false;
69  SpsState sps_;
70  bool pps_parsed_ = false;
71  PpsState pps_;
72
73  // Last parsed slice QP.
74  bool last_slice_qp_delta_parsed_ = false;
75  int32_t last_slice_qp_delta_ = 0;
76};
77
78}  // namespace webrtc
79
80#endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_H264_BITSTREAM_PARSER_H_
81