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| based on |nalu_offsets| and |nalu_lengths| 54 // and rewrites VUI in SPS blocks if necessary. 55 // The result is written to |output_buffer| and modified NAL unit offsets 56 // and lenghts are written to |output_nalu_offsets| and |output_nalu_lenghts| 57 // to account for any added data. 58 static void ParseOutgoingBitstreamAndRewriteSps( 59 rtc::ArrayView<const uint8_t> buffer, 60 size_t num_nalus, 61 const size_t* nalu_offsets, 62 const size_t* nalu_lengths, 63 const ColorSpace* color_space, 64 rtc::Buffer* output_buffer, 65 size_t* output_nalu_offsets, 66 size_t* output_nalu_lengths); 67 68 private: 69 static ParseResult ParseAndRewriteSps( 70 const uint8_t* buffer, 71 size_t length, 72 absl::optional<SpsParser::SpsState>* sps, 73 const ColorSpace* color_space, 74 rtc::Buffer* destination); 75 76 static void UpdateStats(ParseResult result, Direction direction); 77 }; 78 79 } // namespace webrtc 80 81 #endif // COMMON_VIDEO_H264_SPS_VUI_REWRITER_H_ 82