File: video_frame_init_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,243 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
// Copyright 2021 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/modules/webcodecs/video_frame_init_util.h"

#include <stdint.h>
#include <cmath>
#include <limits>

#include "media/base/limits.h"
#include "media/base/video_frame.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_dom_rect_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_frame_buffer_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_frame_init.h"
#include "third_party/blink/renderer/modules/webcodecs/video_frame_rect_util.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

template <typename T>
gfx::Size ParseAndValidateDisplaySizeImpl(T* init,
                                          ExceptionState& exception_state) {
  DCHECK(init->hasDisplayWidth() || init->hasDisplayHeight());

  if (!init->hasDisplayWidth()) {
    exception_state.ThrowTypeError(
        "displayHeight specified without displayWidth.");
    return gfx::Size();
  }
  if (!init->hasDisplayHeight()) {
    exception_state.ThrowTypeError(
        "displayWidth specified without displayHeight.");
    return gfx::Size();
  }

  uint32_t display_width = init->displayWidth();
  uint32_t display_height = init->displayHeight();
  if (display_width == 0) {
    exception_state.ThrowTypeError("displayWidth must be nonzero.");
    return gfx::Size();
  }
  if (display_height == 0) {
    exception_state.ThrowTypeError("displayHeight must be nonzero.");
    return gfx::Size();
  }

  // Check that display size does not exceed dimension limits in
  // media::VideoFrame::IsValidSize().
  //
  // Note that at large display sizes, it can become impossible to allocate
  // a texture large enough to render into. It may be impossible, for example,
  // to create an ImageBitmap without also scaling down.
  if (display_width > media::limits::kMaxDimension ||
      display_height > media::limits::kMaxDimension ||
      display_width * display_height > media::limits::kMaxCanvas) {
    exception_state.ThrowTypeError(
        String::Format("Invalid display size (%u, %u); exceeds "
                       "implementation limit.",
                       display_width, display_height));
    return gfx::Size();
  }

  return gfx::Size(static_cast<int>(display_width),
                   static_cast<int>(display_height));
}

gfx::Size ParseAndValidateDisplaySize(const VideoFrameInit* init,
                                      ExceptionState& exception_state) {
  return ParseAndValidateDisplaySizeImpl(init, exception_state);
}

gfx::Size ParseAndValidateDisplaySize(const VideoFrameBufferInit* init,
                                      ExceptionState& exception_state) {
  return ParseAndValidateDisplaySizeImpl(init, exception_state);
}

// Depending on |init|, this method potentially _overrides_ given "default"
// values for |visible_rect| and |display_size|.
ParsedVideoFrameInit::ParsedVideoFrameInit(
    const VideoFrameInit* init,
    media::VideoPixelFormat format,
    const gfx::Size& coded_size,
    const gfx::Rect& default_visible_rect,
    const gfx::Size& default_display_size,
    ExceptionState& exception_state) {
  // Defaults shouldn't be empty.
  DCHECK(!default_visible_rect.IsEmpty());
  DCHECK(!default_display_size.IsEmpty());
  visible_rect = default_visible_rect;
  display_size = default_display_size;

  // Override visible rect from init.
  if (init->hasVisibleRect()) {
    visible_rect = ToGfxRect(init->visibleRect(), "visibleRect", coded_size,
                             exception_state);
    if (exception_state.HadException())
      return;

    if (visible_rect.width() == 0) {
      exception_state.ThrowTypeError("visibleRect.width must be nonzero.");
      return;
    }

    if (visible_rect.height() == 0) {
      exception_state.ThrowTypeError("visibleRect.height must be nonzero.");
      return;
    }

    ValidateOffsetAlignment(format, visible_rect, "visibleRect",
                            exception_state);
    if (exception_state.HadException())
      return;
  }

  // Override display size from init.
  if (init->hasDisplayWidth() || init->hasDisplayHeight()) {
    display_size = ParseAndValidateDisplaySize(init, exception_state);
    if (exception_state.HadException())
      return;

    // Override display size with computed size scaled from visible rect.
  } else if (init->hasVisibleRect()) {
    double widthScale =
        default_display_size.width() / default_visible_rect.width();
    double heightScale =
        default_display_size.height() / default_visible_rect.height();
    display_size = gfx::Size(std::round(visible_rect.width() * widthScale),
                             std::round(visible_rect.height() * heightScale));
    if (display_size.width() == 0) {
      exception_state.ThrowTypeError("computed displayWidth must be nonzero");
      return;
    }

    if (display_size.height() == 0) {
      exception_state.ThrowTypeError("computed displayHeight must be nonzero");
      return;
    }
  }
}

}  // namespace blink