|
| 1 | +// Copyright 2021 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "third_party/blink/renderer/modules/webcodecs/video_color_space.h" |
| 6 | + |
| 7 | +#include "media/base/video_color_space.h" |
| 8 | +#include "third_party/blink/renderer/bindings/modules/v8/v8_video_color_space_init.h" |
| 9 | +#include "ui/gfx/color_space.h" |
| 10 | + |
| 11 | +namespace blink { |
| 12 | + |
| 13 | +// static |
| 14 | +VideoColorSpace* VideoColorSpace::Create(const VideoColorSpaceInit* init) { |
| 15 | + return MakeGarbageCollected<VideoColorSpace>(init); |
| 16 | +} |
| 17 | + |
| 18 | +VideoColorSpace::VideoColorSpace(const VideoColorSpaceInit* init) { |
| 19 | + if (init->hasPrimaries()) |
| 20 | + primaries_ = init->primaries(); |
| 21 | + if (init->hasTransfer()) |
| 22 | + transfer_ = init->transfer(); |
| 23 | + if (init->hasMatrix()) |
| 24 | + matrix_ = init->matrix(); |
| 25 | + if (init->hasFullRange()) |
| 26 | + full_range_ = init->fullRange(); |
| 27 | +} |
| 28 | + |
| 29 | +VideoColorSpace::VideoColorSpace(const gfx::ColorSpace& color_space) { |
| 30 | + switch (color_space.GetPrimaryID()) { |
| 31 | + case gfx::ColorSpace::PrimaryID::BT709: |
| 32 | + primaries_ = V8VideoColorPrimaries(V8VideoColorPrimaries::Enum::kBt709); |
| 33 | + break; |
| 34 | + case gfx::ColorSpace::PrimaryID::BT470BG: |
| 35 | + primaries_ = V8VideoColorPrimaries(V8VideoColorPrimaries::Enum::kBt470Bg); |
| 36 | + break; |
| 37 | + case gfx::ColorSpace::PrimaryID::SMPTE170M: |
| 38 | + primaries_ = |
| 39 | + V8VideoColorPrimaries(V8VideoColorPrimaries::Enum::kSmpte170M); |
| 40 | + break; |
| 41 | + default: |
| 42 | + // Other values map to unspecified for now. |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + switch (color_space.GetTransferID()) { |
| 47 | + case gfx::ColorSpace::TransferID::BT709: |
| 48 | + transfer_ = V8VideoTransferCharacteristics( |
| 49 | + V8VideoTransferCharacteristics::Enum::kBt709); |
| 50 | + break; |
| 51 | + case gfx::ColorSpace::TransferID::SMPTE170M: |
| 52 | + transfer_ = V8VideoTransferCharacteristics( |
| 53 | + V8VideoTransferCharacteristics::Enum::kSmpte170M); |
| 54 | + break; |
| 55 | + case gfx::ColorSpace::TransferID::IEC61966_2_1: |
| 56 | + transfer_ = V8VideoTransferCharacteristics( |
| 57 | + V8VideoTransferCharacteristics::Enum::kIec6196621); |
| 58 | + break; |
| 59 | + default: |
| 60 | + // Other values map to unspecified for now. |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + switch (color_space.GetMatrixID()) { |
| 65 | + case gfx::ColorSpace::MatrixID::RGB: |
| 66 | + matrix_ = |
| 67 | + V8VideoMatrixCoefficients(V8VideoMatrixCoefficients::Enum::kRgb); |
| 68 | + break; |
| 69 | + case gfx::ColorSpace::MatrixID::BT709: |
| 70 | + matrix_ = |
| 71 | + V8VideoMatrixCoefficients(V8VideoMatrixCoefficients::Enum::kBt709); |
| 72 | + break; |
| 73 | + case gfx::ColorSpace::MatrixID::BT470BG: |
| 74 | + matrix_ = |
| 75 | + V8VideoMatrixCoefficients(V8VideoMatrixCoefficients::Enum::kBt470Bg); |
| 76 | + break; |
| 77 | + case gfx::ColorSpace::MatrixID::SMPTE170M: |
| 78 | + matrix_ = V8VideoMatrixCoefficients( |
| 79 | + V8VideoMatrixCoefficients::Enum::kSmpte170M); |
| 80 | + break; |
| 81 | + default: |
| 82 | + // Other values map to unspecified for now. |
| 83 | + break; |
| 84 | + } |
| 85 | + |
| 86 | + switch (color_space.GetRangeID()) { |
| 87 | + case gfx::ColorSpace::RangeID::LIMITED: |
| 88 | + full_range_ = false; |
| 89 | + break; |
| 90 | + case gfx::ColorSpace::RangeID::FULL: |
| 91 | + full_range_ = true; |
| 92 | + break; |
| 93 | + default: |
| 94 | + // Other values map to unspecified. We could probably map DERIVED to a |
| 95 | + // specific value, though. |
| 96 | + break; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +gfx::ColorSpace VideoColorSpace::ToGfxColorSpace() const { |
| 101 | + gfx::ColorSpace::PrimaryID primaries = gfx::ColorSpace::PrimaryID::INVALID; |
| 102 | + if (primaries_) { |
| 103 | + switch (primaries_->AsEnum()) { |
| 104 | + case V8VideoColorPrimaries::Enum::kBt709: |
| 105 | + primaries = gfx::ColorSpace::PrimaryID::BT709; |
| 106 | + break; |
| 107 | + case V8VideoColorPrimaries::Enum::kBt470Bg: |
| 108 | + primaries = gfx::ColorSpace::PrimaryID::BT470BG; |
| 109 | + break; |
| 110 | + case V8VideoColorPrimaries::Enum::kSmpte170M: |
| 111 | + primaries = gfx::ColorSpace::PrimaryID::SMPTE170M; |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + gfx::ColorSpace::TransferID transfer = gfx::ColorSpace::TransferID::INVALID; |
| 117 | + if (transfer_) { |
| 118 | + switch (transfer_->AsEnum()) { |
| 119 | + case V8VideoTransferCharacteristics::Enum::kBt709: |
| 120 | + transfer = gfx::ColorSpace::TransferID::BT709; |
| 121 | + break; |
| 122 | + case V8VideoTransferCharacteristics::Enum::kSmpte170M: |
| 123 | + transfer = gfx::ColorSpace::TransferID::SMPTE170M; |
| 124 | + break; |
| 125 | + case V8VideoTransferCharacteristics::Enum::kIec6196621: |
| 126 | + transfer = gfx::ColorSpace::TransferID::IEC61966_2_1; |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + gfx::ColorSpace::MatrixID matrix = gfx::ColorSpace::MatrixID::INVALID; |
| 132 | + if (matrix_) { |
| 133 | + switch (matrix_->AsEnum()) { |
| 134 | + case V8VideoMatrixCoefficients::Enum::kRgb: |
| 135 | + matrix = gfx::ColorSpace::MatrixID::RGB; |
| 136 | + break; |
| 137 | + case V8VideoMatrixCoefficients::Enum::kBt709: |
| 138 | + matrix = gfx::ColorSpace::MatrixID::BT709; |
| 139 | + break; |
| 140 | + case V8VideoMatrixCoefficients::Enum::kBt470Bg: |
| 141 | + matrix = gfx::ColorSpace::MatrixID::BT470BG; |
| 142 | + break; |
| 143 | + case V8VideoMatrixCoefficients::Enum::kSmpte170M: |
| 144 | + matrix = gfx::ColorSpace::MatrixID::SMPTE170M; |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + gfx::ColorSpace::RangeID range = gfx::ColorSpace::RangeID::INVALID; |
| 150 | + if (full_range_) { |
| 151 | + range = *full_range_ ? gfx::ColorSpace::RangeID::FULL |
| 152 | + : gfx::ColorSpace::RangeID::LIMITED; |
| 153 | + } |
| 154 | + |
| 155 | + return gfx::ColorSpace(primaries, transfer, matrix, range); |
| 156 | +} |
| 157 | + |
| 158 | +media::VideoColorSpace VideoColorSpace::ToMediaColorSpace() const { |
| 159 | + media::VideoColorSpace::PrimaryID primaries = |
| 160 | + media::VideoColorSpace::PrimaryID::UNSPECIFIED; |
| 161 | + if (primaries_) { |
| 162 | + switch (primaries_->AsEnum()) { |
| 163 | + case V8VideoColorPrimaries::Enum::kBt709: |
| 164 | + primaries = media::VideoColorSpace::PrimaryID::BT709; |
| 165 | + break; |
| 166 | + case V8VideoColorPrimaries::Enum::kBt470Bg: |
| 167 | + primaries = media::VideoColorSpace::PrimaryID::BT470BG; |
| 168 | + break; |
| 169 | + case V8VideoColorPrimaries::Enum::kSmpte170M: |
| 170 | + primaries = media::VideoColorSpace::PrimaryID::SMPTE170M; |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + media::VideoColorSpace::TransferID transfer = |
| 176 | + media::VideoColorSpace::TransferID::UNSPECIFIED; |
| 177 | + if (transfer_) { |
| 178 | + switch (transfer_->AsEnum()) { |
| 179 | + case V8VideoTransferCharacteristics::Enum::kBt709: |
| 180 | + transfer = media::VideoColorSpace::TransferID::BT709; |
| 181 | + break; |
| 182 | + case V8VideoTransferCharacteristics::Enum::kSmpte170M: |
| 183 | + transfer = media::VideoColorSpace::TransferID::SMPTE170M; |
| 184 | + break; |
| 185 | + case V8VideoTransferCharacteristics::Enum::kIec6196621: |
| 186 | + transfer = media::VideoColorSpace::TransferID::IEC61966_2_1; |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + media::VideoColorSpace::MatrixID matrix = |
| 192 | + media::VideoColorSpace::MatrixID::UNSPECIFIED; |
| 193 | + if (matrix_) { |
| 194 | + switch (matrix_->AsEnum()) { |
| 195 | + case V8VideoMatrixCoefficients::Enum::kRgb: |
| 196 | + matrix = media::VideoColorSpace::MatrixID::RGB; |
| 197 | + break; |
| 198 | + case V8VideoMatrixCoefficients::Enum::kBt709: |
| 199 | + matrix = media::VideoColorSpace::MatrixID::BT709; |
| 200 | + break; |
| 201 | + case V8VideoMatrixCoefficients::Enum::kBt470Bg: |
| 202 | + matrix = media::VideoColorSpace::MatrixID::BT470BG; |
| 203 | + break; |
| 204 | + case V8VideoMatrixCoefficients::Enum::kSmpte170M: |
| 205 | + matrix = media::VideoColorSpace::MatrixID::SMPTE170M; |
| 206 | + break; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + gfx::ColorSpace::RangeID range = gfx::ColorSpace::RangeID::INVALID; |
| 211 | + if (full_range_) { |
| 212 | + range = *full_range_ ? gfx::ColorSpace::RangeID::FULL |
| 213 | + : gfx::ColorSpace::RangeID::LIMITED; |
| 214 | + } |
| 215 | + |
| 216 | + return media::VideoColorSpace(primaries, transfer, matrix, range); |
| 217 | +} |
| 218 | + |
| 219 | +VideoColorSpaceInit* VideoColorSpace::toJSON() const { |
| 220 | + auto* init = MakeGarbageCollected<VideoColorSpaceInit>(); |
| 221 | + if (primaries_) |
| 222 | + init->setPrimaries(*primaries_); |
| 223 | + if (transfer_) |
| 224 | + init->setTransfer(*transfer_); |
| 225 | + if (matrix_) |
| 226 | + init->setMatrix(*matrix_); |
| 227 | + if (full_range_) |
| 228 | + init->setFullRange(*full_range_); |
| 229 | + return init; |
| 230 | +} |
| 231 | + |
| 232 | +} // namespace blink |
0 commit comments