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