File: resolution_monitor.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (303 lines) | stat: -rw-r--r-- 10,476 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "third_party/blink/renderer/platform/peerconnection/resolution_monitor.h"

#include <bitset>

#include "base/containers/span.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "media/base/decoder_buffer.h"
#include "media/parsers/vp8_parser.h"
#include "media/parsers/vp9_parser.h"
#include "third_party/libgav1/src/src/buffer_pool.h"
#include "third_party/libgav1/src/src/decoder_state.h"
#include "third_party/libgav1/src/src/obu_parser.h"
#include "third_party/webrtc/api/array_view.h"
#include "third_party/webrtc/common_video/h264/h264_common.h"
#include "third_party/webrtc/common_video/h264/sps_parser.h"

namespace blink {

namespace {

class Vp8ResolutionMonitor : public ResolutionMonitor {
 public:
  Vp8ResolutionMonitor() = default;
  std::optional<gfx::Size> GetResolution(
      const media::DecoderBuffer& buffer) override {
    if (!buffer.is_key_frame()) {
      return current_resolution_;
    }

    media::Vp8Parser parser;
    media::Vp8FrameHeader frame_header;
    auto buffer_span = base::span(buffer);
    if (!parser.ParseFrame(buffer_span.data(), buffer_span.size(),
                           &frame_header)) {
      DLOG(ERROR) << "Failed to parse vp8 stream";
      current_resolution_ = std::nullopt;
    } else {
      current_resolution_ =
          gfx::Size(base::saturated_cast<int>(frame_header.width),
                    base::saturated_cast<int>(frame_header.height));
    }

    return current_resolution_;
  }
  media::VideoCodec codec() const override { return media::VideoCodec::kVP8; }

 private:
  std::optional<gfx::Size> current_resolution_;
};

class Vp9ResolutionMonitor : public ResolutionMonitor {
 public:
  Vp9ResolutionMonitor() = default;

  ~Vp9ResolutionMonitor() override = default;

  std::optional<gfx::Size> GetResolution(
      const media::DecoderBuffer& buffer) override {
    std::vector<uint32_t> frame_sizes;
    if (buffer.side_data()) {
      frame_sizes = buffer.side_data()->spatial_layers;
    }

    auto buffer_span = base::span(buffer);
    parser_.SetStream(buffer_span.data(),
                      base::checked_cast<off_t>(buffer_span.size()),
                      frame_sizes,
                      /*stream_config=*/nullptr);

    gfx::Size frame_size;
    bool parse_error = false;
    // Get the maximum resolution in spatial layers.
    std::optional<gfx::Size> max_resolution;
    while (GetNextFrameSize(frame_size, parse_error)) {
      if (max_resolution.value_or(gfx::Size()).GetArea() <
          frame_size.GetArea()) {
        max_resolution = frame_size;
      }
    }

    return parse_error ? std::nullopt : max_resolution;
  }

  media::VideoCodec codec() const override { return media::VideoCodec::kVP9; }

 private:
  bool GetNextFrameSize(gfx::Size& frame_size, bool& parse_error) {
    media::Vp9FrameHeader frame_header;
    gfx::Size allocate_size;
    media::Vp9Parser::Result result = parser_.ParseNextFrame(
        &frame_header, &allocate_size, /*frame_decrypt_config=*/nullptr);
    switch (result) {
      case media::Vp9Parser::Result::kOk:
        frame_size.SetSize(frame_header.frame_width, frame_header.frame_height);
        return true;
      case media::Vp9Parser::Result::kEOStream:
        return false;
      case media::Vp9Parser::Result::kInvalidStream:
        DLOG(ERROR) << "Failed parsing vp9 frame";
        parse_error = true;
        return false;
    }
    NOTREACHED() << "Unexpected result: " << static_cast<int>(result);
  }

  media::Vp9Parser parser_;
};

class Av1ResolutionMonitor : public ResolutionMonitor {
 public:
  constexpr static unsigned int kDefaultOperatingPoint = 0;

  Av1ResolutionMonitor()
      : buffer_pool_(/*on_frame_buffer_size_changed=*/nullptr,
                     /*get_frame_buffer=*/nullptr,
                     /*release_frame_buffer=*/nullptr,
                     /*callback_private_data=*/nullptr) {}

  ~Av1ResolutionMonitor() override = default;

  std::optional<gfx::Size> GetResolution(
      const media::DecoderBuffer& buffer) override {
    auto buffer_span = base::span(buffer);
    auto parser = base::WrapUnique(new (std::nothrow) libgav1::ObuParser(
        buffer_span.data(), buffer_span.size(), kDefaultOperatingPoint,
        &buffer_pool_, &decoder_state_));
    if (current_sequence_header_) {
      parser->set_sequence_header(*current_sequence_header_);
    }

    std::optional<gfx::Size> max_resolution;
    while (parser->HasData()) {
      libgav1::RefCountedBufferPtr current_frame;
      libgav1::StatusCode status_code = parser->ParseOneFrame(&current_frame);
      if (status_code != libgav1::kStatusOk) {
        DLOG(ERROR) << "Failed parsing av1 frame: "
                    << static_cast<int>(status_code);
        return std::nullopt;
      }
      if (!current_frame) {
        // No frame is found. Finish the stream.
        break;
      }

      if (parser->sequence_header_changed() &&
          !UpdateCurrentSequenceHeader(parser->sequence_header())) {
        return std::nullopt;
      }

      std::optional<gfx::Size> frame_size =
          GetFrameSizeFromHeader(parser->frame_header());
      if (!frame_size) {
        return std::nullopt;
      }
      if (max_resolution.value_or(gfx::Size()).GetArea() <
          frame_size->GetArea()) {
        max_resolution = *frame_size;
      }

      decoder_state_.UpdateReferenceFrames(
          current_frame,
          base::strict_cast<int>(parser->frame_header().refresh_frame_flags));
    }

    return max_resolution;
  }

  media::VideoCodec codec() const override { return media::VideoCodec::kAV1; }

 private:
  // Returns true iff the current decode sequence has multiple spatial layers.
  bool IsSpatialLayerDecoding(int operating_point_idc) const {
    // Spec 6.4.1.
    constexpr int kTemporalLayerBitMaskBits = 8;
    const int kUsedSpatialLayerBitMask =
        (operating_point_idc >> kTemporalLayerBitMaskBits) & 0b1111;
    // In case of an only temporal layer encoding e.g. L1T3, spatial layer#0 bit
    // is 1. We allow this case.
    return kUsedSpatialLayerBitMask > 1;
  }

  bool UpdateCurrentSequenceHeader(
      const libgav1::ObuSequenceHeader& sequence_header) {
    int operating_point_idc =
        sequence_header.operating_point_idc[kDefaultOperatingPoint];
    if (IsSpatialLayerDecoding(operating_point_idc)) {
      constexpr size_t kOperatingPointIdcBits = 12;
      DVLOG(1) << "Spatial layer decoding is not supported: "
               << "operating_point_idc="
               << std::bitset<kOperatingPointIdcBits>(operating_point_idc);
      return false;
    }

    current_sequence_header_ = sequence_header;
    return true;
  }

  std::optional<gfx::Size> GetFrameSizeFromHeader(
      const libgav1::ObuFrameHeader& frame_header) const {
    if (!frame_header.show_existing_frame) {
      return gfx::Size(frame_header.width, frame_header.height);
    }
    const size_t frame_to_show =
        base::checked_cast<size_t>(frame_header.frame_to_show);
    CHECK_LT(frame_to_show,
             static_cast<size_t>(libgav1::kNumReferenceFrameTypes));
    const libgav1::RefCountedBufferPtr show_frame =
        decoder_state_.reference_frame[frame_to_show];
    if (!show_frame) {
      DLOG(ERROR) << "Show existing frame references an invalid frame";
      return std::nullopt;
    }
    return gfx::Size(show_frame->frame_width(), show_frame->frame_height());
  }

  std::optional<libgav1::ObuSequenceHeader> current_sequence_header_;
  libgav1::BufferPool buffer_pool_;
  libgav1::DecoderState decoder_state_;
};

// H264ResolutionMonitor has two assumptions.
// (1) SPS and PPS come in bundle with IDR.
// (2) The buffer has only one IDR and it associates with the SPS in the bundle.
// This is satisfied in WebRTC use case.
class H264ResolutionMonitor : public ResolutionMonitor {
 public:
  H264ResolutionMonitor() = default;
  ~H264ResolutionMonitor() override = default;

  std::optional<gfx::Size> GetResolution(
      const media::DecoderBuffer& buffer) override {
    if (!buffer.is_key_frame()) {
      return current_resolution_;
    }

    std::optional<gfx::Size> resolution;
    auto buffer_span = base::span(buffer);
    webrtc::ArrayView<const uint8_t> webrtc_buffer(buffer_span.data(),
                                                   buffer_span.size());
    std::vector<webrtc::H264::NaluIndex> nalu_indices =
        webrtc::H264::FindNaluIndices(webrtc_buffer);
    for (const auto& nalu_index : nalu_indices) {
      if (nalu_index.payload_size < webrtc::H264::kNaluTypeSize) {
        DLOG(ERROR) << "H.264 SPS NALU size too small for parsing NALU type.";
        return std::nullopt;
      }
      auto nalu_payload = webrtc_buffer.subview(nalu_index.payload_start_offset,
                                                nalu_index.payload_size);
      if (webrtc::H264::ParseNaluType(nalu_payload[0]) ==
          webrtc::H264::NaluType::kSps) {
        // Parse without NALU header.
        std::optional<webrtc::SpsParser::SpsState> sps =
            webrtc::SpsParser::ParseSps(
                nalu_payload.subview(webrtc::H264::kNaluTypeSize));
        if (!sps || !sps->width || !sps->height) {
          DLOG(ERROR) << "Failed parsing H.264 SPS.";
          return std::nullopt;
        }
        resolution = gfx::Size(sps->width, sps->height);
        break;
      }
    }

    current_resolution_ = resolution;
    return current_resolution_;
  }

  media::VideoCodec codec() const override { return media::VideoCodec::kH264; }

 private:
  std::optional<gfx::Size> current_resolution_;
};
}  // namespace

ResolutionMonitor::~ResolutionMonitor() = default;

// static
std::unique_ptr<ResolutionMonitor> ResolutionMonitor::Create(
    media::VideoCodec codec) {
  switch (codec) {
    case media::VideoCodec::kH264:
      return std::make_unique<H264ResolutionMonitor>();
    case media::VideoCodec::kVP8:
      return std::make_unique<Vp8ResolutionMonitor>();
    case media::VideoCodec::kVP9:
      return std::make_unique<Vp9ResolutionMonitor>();
    case media::VideoCodec::kAV1:
      return std::make_unique<Av1ResolutionMonitor>();
    default:
      DLOG(ERROR) << "Unsupported codec: " << media::GetCodecName(codec);
      return nullptr;
  }
}

}  // namespace blink