xref: /aosp_15_r20/external/webrtc/modules/video_coding/utility/qp_parser.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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 #include "modules/video_coding/utility/qp_parser.h"
12 
13 #include "modules/video_coding/utility/vp8_header_parser.h"
14 #include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
15 
16 namespace webrtc {
17 
Parse(VideoCodecType codec_type,size_t spatial_idx,const uint8_t * frame_data,size_t frame_size)18 absl::optional<uint32_t> QpParser::Parse(VideoCodecType codec_type,
19                                          size_t spatial_idx,
20                                          const uint8_t* frame_data,
21                                          size_t frame_size) {
22   if (frame_data == nullptr || frame_size == 0 ||
23       spatial_idx >= kMaxSimulcastStreams) {
24     return absl::nullopt;
25   }
26 
27   if (codec_type == kVideoCodecVP8) {
28     int qp = -1;
29     if (vp8::GetQp(frame_data, frame_size, &qp)) {
30       return qp;
31     }
32   } else if (codec_type == kVideoCodecVP9) {
33     int qp = -1;
34     if (vp9::GetQp(frame_data, frame_size, &qp)) {
35       return qp;
36     }
37   } else if (codec_type == kVideoCodecH264) {
38     return h264_parsers_[spatial_idx].Parse(frame_data, frame_size);
39   }
40 
41   return absl::nullopt;
42 }
43 
Parse(const uint8_t * frame_data,size_t frame_size)44 absl::optional<uint32_t> QpParser::H264QpParser::Parse(
45     const uint8_t* frame_data,
46     size_t frame_size) {
47   MutexLock lock(&mutex_);
48   bitstream_parser_.ParseBitstream(
49       rtc::ArrayView<const uint8_t>(frame_data, frame_size));
50   return bitstream_parser_.GetLastSliceQp();
51 }
52 
53 }  // namespace webrtc
54