sps_vui_rewriter.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2016 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_SPS_VUI_REWRITER_H_
  12. #define COMMON_VIDEO_H264_SPS_VUI_REWRITER_H_
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #include "absl/types/optional.h"
  16. #include "api/video/color_space.h"
  17. #include "common_video/h264/sps_parser.h"
  18. #include "rtc_base/buffer.h"
  19. namespace webrtc {
  20. // A class that can parse an SPS+VUI and if necessary creates a copy with
  21. // updated parameters.
  22. // The rewriter disables frame buffering. This should force decoders to deliver
  23. // decoded frame immediately and, thus, reduce latency.
  24. // The rewriter updates video signal type parameters if external parameters are
  25. // provided.
  26. class SpsVuiRewriter : private SpsParser {
  27. public:
  28. enum class ParseResult { kFailure, kVuiOk, kVuiRewritten };
  29. enum class Direction { kIncoming, kOutgoing };
  30. // Parses an SPS block and if necessary copies it and rewrites the VUI.
  31. // Returns kFailure on failure, kParseOk if parsing succeeded and no update
  32. // was necessary and kParsedAndModified if an updated copy of buffer was
  33. // written to destination. destination may be populated with some data even if
  34. // no rewrite was necessary, but the end offset should remain unchanged.
  35. // Unless parsing fails, the sps parameter will be populated with the parsed
  36. // SPS state. This function assumes that any previous headers
  37. // (NALU start, type, Stap-A, etc) have already been parsed and that RBSP
  38. // decoding has been performed.
  39. static ParseResult ParseAndRewriteSps(
  40. const uint8_t* buffer,
  41. size_t length,
  42. absl::optional<SpsParser::SpsState>* sps,
  43. const ColorSpace* color_space,
  44. rtc::Buffer* destination,
  45. Direction Direction);
  46. // Parses NAL units from |buffer|, strips AUD blocks and rewrites VUI in SPS
  47. // blocks if necessary.
  48. static rtc::Buffer ParseOutgoingBitstreamAndRewrite(
  49. rtc::ArrayView<const uint8_t> buffer,
  50. const ColorSpace* color_space);
  51. private:
  52. static ParseResult ParseAndRewriteSps(
  53. const uint8_t* buffer,
  54. size_t length,
  55. absl::optional<SpsParser::SpsState>* sps,
  56. const ColorSpace* color_space,
  57. rtc::Buffer* destination);
  58. static void UpdateStats(ParseResult result, Direction direction);
  59. };
  60. } // namespace webrtc
  61. #endif // COMMON_VIDEO_H264_SPS_VUI_REWRITER_H_