h264_bitstream_parser.h 2.1 KB

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