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