Avi Drissman | d387f092 | 2022-09-14 20:51:31 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Miguel Casas-Sanchez | 15e2e59 | 2017-07-19 01:45:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Miyoung Shin | 25bb8c09 | 2019-08-03 00:56:57 | [diff] [blame] | 5 | #include "media/mojo/mojom/video_encode_accelerator_mojom_traits.h" |
Miguel Casas-Sanchez | 15e2e59 | 2017-07-19 01:45:25 | [diff] [blame] | 6 | |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 7 | #include <optional> |
| 8 | |
Hans Wennborg | 7e26d908e | 2020-04-24 20:55:49 | [diff] [blame] | 9 | #include "base/notreached.h" |
Erik Språng | f169a57 | 2018-06-29 08:44:29 | [diff] [blame] | 10 | #include "media/base/video_bitrate_allocation.h" |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 11 | #include "media/mojo/mojom/video_encode_accelerator.mojom.h" |
Clarissa Garvey | 9d93d11 | 2022-05-24 20:59:13 | [diff] [blame] | 12 | #include "media/video/video_encode_accelerator.h" |
Erik Språng | e24828d | 2018-07-10 11:33:15 | [diff] [blame] | 13 | #include "mojo/public/cpp/base/time_mojom_traits.h" |
Miguel Casas-Sanchez | 15e2e59 | 2017-07-19 01:45:25 | [diff] [blame] | 14 | |
| 15 | namespace mojo { |
| 16 | |
| 17 | // static |
Clarissa Garvey | 9d93d11 | 2022-05-24 20:59:13 | [diff] [blame] | 18 | media::mojom::VideoEncodeAcceleratorSupportedRateControlMode |
| 19 | EnumTraits<media::mojom::VideoEncodeAcceleratorSupportedRateControlMode, |
| 20 | media::VideoEncodeAccelerator::SupportedRateControlMode>:: |
| 21 | ToMojom(media::VideoEncodeAccelerator::SupportedRateControlMode mode) { |
| 22 | switch (mode) { |
| 23 | case media::VideoEncodeAccelerator::kNoMode: |
| 24 | return media::mojom::VideoEncodeAcceleratorSupportedRateControlMode:: |
| 25 | kNoMode; |
| 26 | case media::VideoEncodeAccelerator::kConstantMode: |
| 27 | return media::mojom::VideoEncodeAcceleratorSupportedRateControlMode:: |
| 28 | kConstantMode; |
| 29 | case media::VideoEncodeAccelerator::kVariableMode: |
| 30 | return media::mojom::VideoEncodeAcceleratorSupportedRateControlMode:: |
| 31 | kVariableMode; |
Eugene Zemtsov | a48f4a2 | 2023-06-27 21:43:40 | [diff] [blame] | 32 | case media::VideoEncodeAccelerator::kExternalMode: |
| 33 | return media::mojom::VideoEncodeAcceleratorSupportedRateControlMode:: |
| 34 | kExternalMode; |
Clarissa Garvey | 9d93d11 | 2022-05-24 20:59:13 | [diff] [blame] | 35 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 36 | NOTREACHED_NORETURN(); |
Clarissa Garvey | 9d93d11 | 2022-05-24 20:59:13 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // static |
| 40 | bool EnumTraits<media::mojom::VideoEncodeAcceleratorSupportedRateControlMode, |
| 41 | media::VideoEncodeAccelerator::SupportedRateControlMode>:: |
| 42 | FromMojom(media::mojom::VideoEncodeAcceleratorSupportedRateControlMode mode, |
| 43 | media::VideoEncodeAccelerator::SupportedRateControlMode* out) { |
| 44 | switch (mode) { |
| 45 | case media::mojom::VideoEncodeAcceleratorSupportedRateControlMode::kNoMode: |
| 46 | *out = media::VideoEncodeAccelerator::kNoMode; |
| 47 | return true; |
| 48 | case media::mojom::VideoEncodeAcceleratorSupportedRateControlMode:: |
| 49 | kConstantMode: |
| 50 | *out = media::VideoEncodeAccelerator::kConstantMode; |
| 51 | return true; |
| 52 | case media::mojom::VideoEncodeAcceleratorSupportedRateControlMode:: |
| 53 | kVariableMode: |
| 54 | *out = media::VideoEncodeAccelerator::kVariableMode; |
| 55 | return true; |
Eugene Zemtsov | a48f4a2 | 2023-06-27 21:43:40 | [diff] [blame] | 56 | case media::mojom::VideoEncodeAcceleratorSupportedRateControlMode:: |
| 57 | kExternalMode: |
| 58 | *out = media::VideoEncodeAccelerator::kExternalMode; |
| 59 | return true; |
Clarissa Garvey | 9d93d11 | 2022-05-24 20:59:13 | [diff] [blame] | 60 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 61 | NOTREACHED_NORETURN(); |
Clarissa Garvey | 9d93d11 | 2022-05-24 20:59:13 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // static |
Hirokazu Honda | eb11e4a9 | 2020-02-19 11:25:00 | [diff] [blame] | 65 | bool StructTraits<media::mojom::VideoEncodeAcceleratorSupportedProfileDataView, |
| 66 | media::VideoEncodeAccelerator::SupportedProfile>:: |
| 67 | Read(media::mojom::VideoEncodeAcceleratorSupportedProfileDataView data, |
| 68 | media::VideoEncodeAccelerator::SupportedProfile* out) { |
| 69 | if (!data.ReadMinResolution(&out->min_resolution) || |
| 70 | !data.ReadMaxResolution(&out->max_resolution) || |
| 71 | !data.ReadProfile(&out->profile)) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | out->max_framerate_numerator = data.max_framerate_numerator(); |
| 76 | out->max_framerate_denominator = data.max_framerate_denominator(); |
Clarissa Garvey | 9d93d11 | 2022-05-24 20:59:13 | [diff] [blame] | 77 | out->rate_control_modes = media::VideoEncodeAccelerator::kNoMode; |
| 78 | std::vector<media::VideoEncodeAccelerator::SupportedRateControlMode> modes; |
| 79 | if (!data.ReadRateControlModes(&modes)) |
| 80 | return false; |
| 81 | for (const auto& mode : modes) { |
| 82 | out->rate_control_modes |= mode; |
| 83 | } |
Zhaoliang Ma | 64040273 | 2021-08-30 07:14:26 | [diff] [blame] | 84 | |
| 85 | std::vector<media::SVCScalabilityMode> scalability_modes; |
| 86 | if (!data.ReadScalabilityModes(&scalability_modes)) |
| 87 | return false; |
| 88 | out->scalability_modes = std::move(scalability_modes); |
| 89 | |
Dale Curtis | a86d7aa | 2022-12-20 08:06:09 | [diff] [blame] | 90 | out->is_software_codec = data.is_software_codec(); |
| 91 | |
Hirokazu Honda | eb11e4a9 | 2020-02-19 11:25:00 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | // static |
Clarissa Garvey | b5e8c6b | 2022-03-30 18:49:10 | [diff] [blame] | 96 | bool StructTraits<media::mojom::VariableBitratePeakDataView, uint32_t>::Read( |
| 97 | media::mojom::VariableBitratePeakDataView data, |
| 98 | uint32_t* out_peak_bps) { |
| 99 | uint32_t peak_bps = data.bps(); |
| 100 | if (peak_bps == 0) |
| 101 | return false; |
| 102 | *out_peak_bps = peak_bps; |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // static |
Clarissa Garvey | 1726b82 | 2022-01-28 20:06:07 | [diff] [blame] | 107 | std::vector<uint32_t> StructTraits<media::mojom::VideoBitrateAllocationDataView, |
| 108 | media::VideoBitrateAllocation>:: |
Erik Språng | f169a57 | 2018-06-29 08:44:29 | [diff] [blame] | 109 | bitrates(const media::VideoBitrateAllocation& bitrate_allocation) { |
Clarissa Garvey | 1726b82 | 2022-01-28 20:06:07 | [diff] [blame] | 110 | std::vector<uint32_t> bitrates; |
| 111 | uint32_t sum_bps = 0; |
Erik Språng | f169a57 | 2018-06-29 08:44:29 | [diff] [blame] | 112 | for (size_t si = 0; si < media::VideoBitrateAllocation::kMaxSpatialLayers; |
| 113 | ++si) { |
| 114 | for (size_t ti = 0; ti < media::VideoBitrateAllocation::kMaxTemporalLayers; |
| 115 | ++ti) { |
| 116 | if (sum_bps == bitrate_allocation.GetSumBps()) { |
| 117 | // The rest is all zeros, no need to iterate further. |
| 118 | return bitrates; |
| 119 | } |
Clarissa Garvey | 1726b82 | 2022-01-28 20:06:07 | [diff] [blame] | 120 | const uint32_t layer_bitrate = bitrate_allocation.GetBitrateBps(si, ti); |
Erik Språng | f169a57 | 2018-06-29 08:44:29 | [diff] [blame] | 121 | bitrates.emplace_back(layer_bitrate); |
| 122 | sum_bps += layer_bitrate; |
| 123 | } |
| 124 | } |
| 125 | return bitrates; |
| 126 | } |
| 127 | |
| 128 | // static |
| 129 | bool StructTraits<media::mojom::VideoBitrateAllocationDataView, |
| 130 | media::VideoBitrateAllocation>:: |
| 131 | Read(media::mojom::VideoBitrateAllocationDataView data, |
| 132 | media::VideoBitrateAllocation* out_bitrate_allocation) { |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 133 | std::optional<uint32_t> peak_bps; |
Clarissa Garvey | b5e8c6b | 2022-03-30 18:49:10 | [diff] [blame] | 134 | if (!data.ReadVariableBitratePeak(&peak_bps)) |
| 135 | return false; |
| 136 | if (peak_bps.has_value()) { |
| 137 | *out_bitrate_allocation = |
| 138 | media::VideoBitrateAllocation(media::Bitrate::Mode::kVariable); |
| 139 | } else { |
| 140 | *out_bitrate_allocation = |
| 141 | media::VideoBitrateAllocation(media::Bitrate::Mode::kConstant); |
| 142 | } |
Clarissa Garvey | 1726b82 | 2022-01-28 20:06:07 | [diff] [blame] | 143 | ArrayDataView<uint32_t> bitrates; |
Erik Språng | f169a57 | 2018-06-29 08:44:29 | [diff] [blame] | 144 | data.GetBitratesDataView(&bitrates); |
| 145 | size_t size = bitrates.size(); |
| 146 | if (size > media::VideoBitrateAllocation::kMaxSpatialLayers * |
| 147 | media::VideoBitrateAllocation::kMaxTemporalLayers) { |
| 148 | return false; |
| 149 | } |
| 150 | for (size_t i = 0; i < size; ++i) { |
Clarissa Garvey | 1726b82 | 2022-01-28 20:06:07 | [diff] [blame] | 151 | const uint32_t bitrate = bitrates[i]; |
Erik Språng | f169a57 | 2018-06-29 08:44:29 | [diff] [blame] | 152 | const size_t si = i / media::VideoBitrateAllocation::kMaxTemporalLayers; |
| 153 | const size_t ti = i % media::VideoBitrateAllocation::kMaxTemporalLayers; |
| 154 | if (!out_bitrate_allocation->SetBitrate(si, ti, bitrate)) { |
| 155 | return false; |
| 156 | } |
| 157 | } |
Clarissa Garvey | b5e8c6b | 2022-03-30 18:49:10 | [diff] [blame] | 158 | |
| 159 | if (peak_bps.has_value()) { |
| 160 | if (!out_bitrate_allocation->SetPeakBps(*peak_bps)) { |
| 161 | // Invalid (too low) peak for the sum of the bitrates. |
| 162 | return false; |
| 163 | } |
| 164 | } |
| 165 | |
Erik Språng | f169a57 | 2018-06-29 08:44:29 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
Erik Språng | e24828d | 2018-07-10 11:33:15 | [diff] [blame] | 169 | // static |
Eugene Zemtsov | 0402883 | 2023-06-23 00:22:25 | [diff] [blame] | 170 | bool StructTraits<media::mojom::VideoEncodeOptionsDataView, |
| 171 | media::VideoEncoder::EncodeOptions>:: |
| 172 | Read(media::mojom::VideoEncodeOptionsDataView data, |
| 173 | media::VideoEncoder::EncodeOptions* out_options) { |
| 174 | out_options->key_frame = data.force_keyframe(); |
| 175 | int32_t quantizer = data.quantizer(); |
| 176 | if (quantizer < 0) { |
| 177 | out_options->quantizer.reset(); |
| 178 | } else { |
| 179 | out_options->quantizer = data.quantizer(); |
| 180 | } |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | // static |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 186 | bool UnionTraits<media::mojom::CodecMetadataDataView, |
| 187 | media::BitstreamBufferMetadata>:: |
| 188 | Read(media::mojom::CodecMetadataDataView data, |
| 189 | media::BitstreamBufferMetadata* out) { |
| 190 | switch (data.tag()) { |
Daniel Cheng | 40fbe2f | 2022-04-20 22:12:53 | [diff] [blame] | 191 | case media::mojom::CodecMetadataDataView::Tag::kH264: { |
Hirokazu Honda | b7a2f88 | 2021-08-04 03:19:24 | [diff] [blame] | 192 | return data.ReadH264(&out->h264); |
| 193 | } |
Daniel Cheng | 40fbe2f | 2022-04-20 22:12:53 | [diff] [blame] | 194 | case media::mojom::CodecMetadataDataView::Tag::kVp8: { |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 195 | return data.ReadVp8(&out->vp8); |
| 196 | } |
Daniel Cheng | 40fbe2f | 2022-04-20 22:12:53 | [diff] [blame] | 197 | case media::mojom::CodecMetadataDataView::Tag::kVp9: { |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 198 | return data.ReadVp9(&out->vp9); |
| 199 | } |
Daniel Cheng | 40fbe2f | 2022-04-20 22:12:53 | [diff] [blame] | 200 | case media::mojom::CodecMetadataDataView::Tag::kAv1: { |
Qiu Jianlin | b683c4b | 2021-12-14 01:55:08 | [diff] [blame] | 201 | return data.ReadAv1(&out->av1); |
| 202 | } |
Qiu Jianlin | 3d5e716 | 2022-10-26 10:26:24 | [diff] [blame] | 203 | case media::mojom::CodecMetadataDataView::Tag::kH265: { |
| 204 | return data.ReadH265(&out->h265); |
| 205 | } |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 206 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 207 | NOTREACHED_NORETURN(); |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // static |
Erik Språng | e24828d | 2018-07-10 11:33:15 | [diff] [blame] | 211 | bool StructTraits<media::mojom::BitstreamBufferMetadataDataView, |
| 212 | media::BitstreamBufferMetadata>:: |
| 213 | Read(media::mojom::BitstreamBufferMetadataDataView data, |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 214 | media::BitstreamBufferMetadata* metadata) { |
| 215 | metadata->payload_size_bytes = data.payload_size_bytes(); |
| 216 | metadata->key_frame = data.key_frame(); |
| 217 | if (!data.ReadTimestamp(&metadata->timestamp)) { |
Erik Språng | e24828d | 2018-07-10 11:33:15 | [diff] [blame] | 218 | return false; |
| 219 | } |
Hirokazu Honda | 76b8214 | 2024-01-17 03:27:17 | [diff] [blame] | 220 | metadata->end_of_picture = data.end_of_picture(); |
Zhaoliang Ma | c27f343 | 2021-12-16 05:49:47 | [diff] [blame] | 221 | metadata->qp = data.qp(); |
Dale Curtis | c7e4002 | 2022-12-17 00:37:52 | [diff] [blame] | 222 | if (!data.ReadEncodedSize(&metadata->encoded_size)) { |
| 223 | return false; |
| 224 | } |
Dale Curtis | f2f10a1 | 2023-03-20 17:42:21 | [diff] [blame] | 225 | if (!data.ReadEncodedColorSpace(&metadata->encoded_color_space)) { |
| 226 | return false; |
| 227 | } |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 228 | |
| 229 | return data.ReadCodecMetadata(metadata); |
Erik Språng | e24828d | 2018-07-10 11:33:15 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // static |
Hirokazu Honda | b7a2f88 | 2021-08-04 03:19:24 | [diff] [blame] | 233 | bool StructTraits<media::mojom::H264MetadataDataView, media::H264Metadata>:: |
| 234 | Read(media::mojom::H264MetadataDataView data, |
| 235 | media::H264Metadata* out_metadata) { |
| 236 | out_metadata->temporal_idx = data.temporal_idx(); |
| 237 | out_metadata->layer_sync = data.layer_sync(); |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | // static |
Qiu Jianlin | 3d5e716 | 2022-10-26 10:26:24 | [diff] [blame] | 242 | bool StructTraits<media::mojom::H265MetadataDataView, media::H265Metadata>:: |
| 243 | Read(media::mojom::H265MetadataDataView data, |
| 244 | media::H265Metadata* out_metadata) { |
| 245 | out_metadata->temporal_idx = data.temporal_idx(); |
Qiu Jianlin | 3d5e716 | 2022-10-26 10:26:24 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | |
| 249 | // static |
Erik Språng | e24828d | 2018-07-10 11:33:15 | [diff] [blame] | 250 | bool StructTraits<media::mojom::Vp8MetadataDataView, media::Vp8Metadata>::Read( |
| 251 | media::mojom::Vp8MetadataDataView data, |
| 252 | media::Vp8Metadata* out_metadata) { |
| 253 | out_metadata->non_reference = data.non_reference(); |
| 254 | out_metadata->temporal_idx = data.temporal_idx(); |
| 255 | out_metadata->layer_sync = data.layer_sync(); |
| 256 | return true; |
| 257 | } |
Pin-chih Lin | e6ff3d97 | 2018-07-27 11:15:04 | [diff] [blame] | 258 | |
| 259 | // static |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 260 | bool StructTraits<media::mojom::Vp9MetadataDataView, media::Vp9Metadata>::Read( |
| 261 | media::mojom::Vp9MetadataDataView data, |
| 262 | media::Vp9Metadata* out_metadata) { |
Zhaoliang Ma | def7827b | 2021-08-05 07:19:29 | [diff] [blame] | 263 | out_metadata->inter_pic_predicted = data.inter_pic_predicted(); |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 264 | out_metadata->temporal_up_switch = data.temporal_up_switch(); |
Zhaoliang Ma | 641a7b0 | 2021-06-15 06:53:13 | [diff] [blame] | 265 | out_metadata->referenced_by_upper_spatial_layers = |
| 266 | data.referenced_by_upper_spatial_layers(); |
| 267 | out_metadata->reference_lower_spatial_layers = |
| 268 | data.reference_lower_spatial_layers(); |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 269 | out_metadata->temporal_idx = data.temporal_idx(); |
Zhaoliang Ma | 641a7b0 | 2021-06-15 06:53:13 | [diff] [blame] | 270 | out_metadata->spatial_idx = data.spatial_idx(); |
Hirokazu Honda | f574a26 | 2023-08-31 01:53:37 | [diff] [blame] | 271 | out_metadata->begin_active_spatial_layer_index = |
| 272 | data.begin_active_spatial_layer_index(); |
| 273 | out_metadata->end_active_spatial_layer_index = |
| 274 | data.end_active_spatial_layer_index(); |
Zhaoliang Ma | 641a7b0 | 2021-06-15 06:53:13 | [diff] [blame] | 275 | return data.ReadSpatialLayerResolutions( |
| 276 | &out_metadata->spatial_layer_resolutions) && |
| 277 | data.ReadPDiffs(&out_metadata->p_diffs); |
Hirokazu Honda | bf652e7 | 2020-07-02 04:08:00 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // static |
Qiu Jianlin | b683c4b | 2021-12-14 01:55:08 | [diff] [blame] | 281 | bool StructTraits<media::mojom::Av1MetadataDataView, media::Av1Metadata>::Read( |
| 282 | media::mojom::Av1MetadataDataView data, |
| 283 | media::Av1Metadata* out_metadata) { |
Qiu Jianlin | b683c4b | 2021-12-14 01:55:08 | [diff] [blame] | 284 | out_metadata->temporal_idx = data.temporal_idx(); |
Hirokazu Honda | 827b499 | 2023-04-05 01:21:17 | [diff] [blame] | 285 | return true; |
Qiu Jianlin | b683c4b | 2021-12-14 01:55:08 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // static |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 289 | media::mojom::VideoEncodeAcceleratorConfig_StorageType |
| 290 | EnumTraits<media::mojom::VideoEncodeAcceleratorConfig_StorageType, |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 291 | media::VideoEncodeAccelerator::Config::StorageType>:: |
| 292 | ToMojom(media::VideoEncodeAccelerator::Config::StorageType input) { |
| 293 | switch (input) { |
Hirokazu Honda | a1de52c | 2021-02-17 03:24:16 | [diff] [blame] | 294 | case media::VideoEncodeAccelerator::Config::StorageType::kGpuMemoryBuffer: |
| 295 | return media::mojom::VideoEncodeAcceleratorConfig_StorageType:: |
| 296 | kGpuMemoryBuffer; |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 297 | case media::VideoEncodeAccelerator::Config::StorageType::kShmem: |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 298 | return media::mojom::VideoEncodeAcceleratorConfig_StorageType::kShmem; |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 299 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 300 | NOTREACHED_NORETURN(); |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | // static |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 304 | bool EnumTraits<media::mojom::VideoEncodeAcceleratorConfig_StorageType, |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 305 | media::VideoEncodeAccelerator::Config::StorageType>:: |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 306 | FromMojom(media::mojom::VideoEncodeAcceleratorConfig_StorageType input, |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 307 | media::VideoEncodeAccelerator::Config::StorageType* output) { |
| 308 | switch (input) { |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 309 | case media::mojom::VideoEncodeAcceleratorConfig_StorageType::kShmem: |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 310 | *output = media::VideoEncodeAccelerator::Config::StorageType::kShmem; |
| 311 | return true; |
Hirokazu Honda | a1de52c | 2021-02-17 03:24:16 | [diff] [blame] | 312 | case media::mojom::VideoEncodeAcceleratorConfig_StorageType:: |
| 313 | kGpuMemoryBuffer: |
| 314 | *output = |
| 315 | media::VideoEncodeAccelerator::Config::StorageType::kGpuMemoryBuffer; |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 316 | return true; |
| 317 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 318 | NOTREACHED_NORETURN(); |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // static |
Dale Curtis | 13f72b7 | 2022-11-17 23:37:23 | [diff] [blame] | 322 | media::mojom::VideoEncodeAcceleratorConfig_EncoderType |
| 323 | EnumTraits<media::mojom::VideoEncodeAcceleratorConfig_EncoderType, |
| 324 | media::VideoEncodeAccelerator::Config::EncoderType>:: |
| 325 | ToMojom(media::VideoEncodeAccelerator::Config::EncoderType input) { |
| 326 | switch (input) { |
| 327 | case media::VideoEncodeAccelerator::Config::EncoderType::kHardware: |
| 328 | return media::mojom::VideoEncodeAcceleratorConfig_EncoderType::kHardware; |
| 329 | case media::VideoEncodeAccelerator::Config::EncoderType::kSoftware: |
| 330 | return media::mojom::VideoEncodeAcceleratorConfig_EncoderType::kSoftware; |
| 331 | case media::VideoEncodeAccelerator::Config::EncoderType::kNoPreference: |
| 332 | return media::mojom::VideoEncodeAcceleratorConfig_EncoderType:: |
| 333 | kNoPreference; |
| 334 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 335 | NOTREACHED_NORETURN(); |
Dale Curtis | 13f72b7 | 2022-11-17 23:37:23 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // static |
| 339 | bool EnumTraits<media::mojom::VideoEncodeAcceleratorConfig_EncoderType, |
| 340 | media::VideoEncodeAccelerator::Config::EncoderType>:: |
| 341 | FromMojom(media::mojom::VideoEncodeAcceleratorConfig_EncoderType input, |
| 342 | media::VideoEncodeAccelerator::Config::EncoderType* output) { |
| 343 | switch (input) { |
| 344 | case media::mojom::VideoEncodeAcceleratorConfig_EncoderType::kHardware: |
| 345 | *output = media::VideoEncodeAccelerator::Config::EncoderType::kHardware; |
| 346 | return true; |
| 347 | case media::mojom::VideoEncodeAcceleratorConfig_EncoderType::kSoftware: |
| 348 | *output = media::VideoEncodeAccelerator::Config::EncoderType::kSoftware; |
| 349 | return true; |
| 350 | case media::mojom::VideoEncodeAcceleratorConfig_EncoderType::kNoPreference: |
| 351 | *output = |
| 352 | media::VideoEncodeAccelerator::Config::EncoderType::kNoPreference; |
| 353 | return true; |
| 354 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 355 | NOTREACHED_NORETURN(); |
Dale Curtis | 13f72b7 | 2022-11-17 23:37:23 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // static |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 359 | media::mojom::VideoEncodeAcceleratorConfig_ContentType |
| 360 | EnumTraits<media::mojom::VideoEncodeAcceleratorConfig_ContentType, |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 361 | media::VideoEncodeAccelerator::Config::ContentType>:: |
| 362 | ToMojom(media::VideoEncodeAccelerator::Config::ContentType input) { |
| 363 | switch (input) { |
| 364 | case media::VideoEncodeAccelerator::Config::ContentType::kDisplay: |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 365 | return media::mojom::VideoEncodeAcceleratorConfig_ContentType::kDisplay; |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 366 | case media::VideoEncodeAccelerator::Config::ContentType::kCamera: |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 367 | return media::mojom::VideoEncodeAcceleratorConfig_ContentType::kCamera; |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 368 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 369 | NOTREACHED_NORETURN(); |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 370 | } |
Zhaoliang Ma | 2bae0df | 2021-06-07 02:58:01 | [diff] [blame] | 371 | |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 372 | // static |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 373 | bool EnumTraits<media::mojom::VideoEncodeAcceleratorConfig_ContentType, |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 374 | media::VideoEncodeAccelerator::Config::ContentType>:: |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 375 | FromMojom(media::mojom::VideoEncodeAcceleratorConfig_ContentType input, |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 376 | media::VideoEncodeAccelerator::Config::ContentType* output) { |
| 377 | switch (input) { |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 378 | case media::mojom::VideoEncodeAcceleratorConfig_ContentType::kCamera: |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 379 | *output = media::VideoEncodeAccelerator::Config::ContentType::kCamera; |
| 380 | return true; |
Ken Rockot | 5a4c9e6 | 2020-07-22 21:55:16 | [diff] [blame] | 381 | case media::mojom::VideoEncodeAcceleratorConfig_ContentType::kDisplay: |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 382 | *output = media::VideoEncodeAccelerator::Config::ContentType::kDisplay; |
| 383 | return true; |
| 384 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 385 | NOTREACHED_NORETURN(); |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // static |
Joey Arhar | 60b3f6a | 2020-04-03 21:52:17 | [diff] [blame] | 389 | bool StructTraits<media::mojom::SpatialLayerDataView, |
| 390 | media::VideoEncodeAccelerator::Config::SpatialLayer>:: |
| 391 | Read(media::mojom::SpatialLayerDataView input, |
| 392 | media::VideoEncodeAccelerator::Config::SpatialLayer* output) { |
| 393 | output->width = input.width(); |
| 394 | output->height = input.height(); |
| 395 | output->bitrate_bps = input.bitrate_bps(); |
| 396 | output->framerate = input.framerate(); |
| 397 | output->max_qp = input.max_qp(); |
| 398 | output->num_of_temporal_layers = input.num_of_temporal_layers(); |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | // static |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 403 | bool StructTraits<media::mojom::ConstantBitrateDataView, media::Bitrate>::Read( |
| 404 | media::mojom::ConstantBitrateDataView input, |
| 405 | media::Bitrate* output) { |
Clarissa Garvey | 86223e4 | 2022-03-02 19:02:01 | [diff] [blame] | 406 | *output = media::Bitrate::ConstantBitrate(input.target_bps()); |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 407 | return true; |
| 408 | } |
| 409 | |
| 410 | // static |
| 411 | bool StructTraits<media::mojom::VariableBitrateDataView, media::Bitrate>::Read( |
| 412 | media::mojom::VariableBitrateDataView input, |
| 413 | media::Bitrate* output) { |
Clarissa Garvey | 86223e4 | 2022-03-02 19:02:01 | [diff] [blame] | 414 | if (input.target_bps() > input.peak_bps()) |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 415 | return false; |
Clarissa Garvey | 86223e4 | 2022-03-02 19:02:01 | [diff] [blame] | 416 | if (input.peak_bps() == 0u) |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 417 | return false; |
Clarissa Garvey | 86223e4 | 2022-03-02 19:02:01 | [diff] [blame] | 418 | *output = |
| 419 | media::Bitrate::VariableBitrate(input.target_bps(), input.peak_bps()); |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 420 | return true; |
| 421 | } |
| 422 | |
| 423 | // static |
Eugene Zemtsov | 01fd002 | 2023-02-24 23:00:33 | [diff] [blame] | 424 | bool StructTraits<media::mojom::ExternalBitrateDataView, media::Bitrate>::Read( |
| 425 | media::mojom::ExternalBitrateDataView input, |
| 426 | media::Bitrate* output) { |
| 427 | *output = media::Bitrate::ExternalRateControl(); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | // static |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 432 | media::mojom::BitrateDataView::Tag |
| 433 | UnionTraits<media::mojom::BitrateDataView, media::Bitrate>::GetTag( |
| 434 | const media::Bitrate& input) { |
| 435 | switch (input.mode()) { |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 436 | case media::Bitrate::Mode::kConstant: |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 437 | return media::mojom::BitrateDataView::Tag::kConstant; |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 438 | case media::Bitrate::Mode::kVariable: |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 439 | return media::mojom::BitrateDataView::Tag::kVariable; |
Eugene Zemtsov | 01fd002 | 2023-02-24 23:00:33 | [diff] [blame] | 440 | case media::Bitrate::Mode::kExternal: |
| 441 | return media::mojom::BitrateDataView::Tag::kExternal; |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 442 | } |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 443 | NOTREACHED_NORETURN(); |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // static |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 447 | bool UnionTraits<media::mojom::BitrateDataView, media::Bitrate>::Read( |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 448 | media::mojom::BitrateDataView input, |
| 449 | media::Bitrate* output) { |
Clarissa Garvey | 3b4dc7b1 | 2022-02-25 02:52:01 | [diff] [blame] | 450 | switch (input.tag()) { |
| 451 | case media::mojom::BitrateDataView::Tag::kConstant: |
| 452 | return input.ReadConstant(output); |
| 453 | case media::mojom::BitrateDataView::Tag::kVariable: |
| 454 | return input.ReadVariable(output); |
Eugene Zemtsov | 01fd002 | 2023-02-24 23:00:33 | [diff] [blame] | 455 | case media::mojom::BitrateDataView::Tag::kExternal: |
| 456 | return input.ReadExternal(output); |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 457 | } |
| 458 | |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 459 | NOTREACHED_NORETURN(); |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // static |
Pin-chih Lin | e6ff3d97 | 2018-07-27 11:15:04 | [diff] [blame] | 463 | bool StructTraits<media::mojom::VideoEncodeAcceleratorConfigDataView, |
| 464 | media::VideoEncodeAccelerator::Config>:: |
| 465 | Read(media::mojom::VideoEncodeAcceleratorConfigDataView input, |
| 466 | media::VideoEncodeAccelerator::Config* output) { |
| 467 | media::VideoPixelFormat input_format; |
| 468 | if (!input.ReadInputFormat(&input_format)) |
| 469 | return false; |
| 470 | |
| 471 | gfx::Size input_visible_size; |
| 472 | if (!input.ReadInputVisibleSize(&input_visible_size)) |
| 473 | return false; |
| 474 | |
| 475 | media::VideoCodecProfile output_profile; |
| 476 | if (!input.ReadOutputProfile(&output_profile)) |
| 477 | return false; |
| 478 | |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 479 | media::Bitrate bitrate; |
| 480 | if (!input.ReadBitrate(&bitrate)) |
| 481 | return false; |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 482 | |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 483 | std::optional<uint32_t> initial_framerate; |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 484 | if (input.has_initial_framerate()) |
Pin-chih Lin | fc7b5a8 | 2018-08-13 05:44:17 | [diff] [blame] | 485 | initial_framerate = input.initial_framerate(); |
Pin-chih Lin | fc7b5a8 | 2018-08-13 05:44:17 | [diff] [blame] | 486 | |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 487 | std::optional<uint32_t> gop_length; |
Fish Lin | 46827d0 | 2019-04-03 03:46:54 | [diff] [blame] | 488 | if (input.has_gop_length()) |
| 489 | gop_length = input.gop_length(); |
| 490 | |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 491 | std::optional<uint8_t> h264_output_level; |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 492 | if (input.has_h264_output_level()) |
Pin-chih Lin | e6ff3d97 | 2018-07-27 11:15:04 | [diff] [blame] | 493 | h264_output_level = input.h264_output_level(); |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 494 | |
Chunbo Hua | 738df53 | 2020-07-16 16:02:45 | [diff] [blame] | 495 | bool is_constrained_h264 = input.is_constrained_h264(); |
| 496 | |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 497 | std::optional<media::VideoEncodeAccelerator::Config::StorageType> |
Fish Lin | 752b4919 | 2019-05-31 05:04:21 | [diff] [blame] | 498 | storage_type; |
| 499 | if (input.has_storage_type()) { |
| 500 | if (!input.ReadStorageType(&storage_type)) |
| 501 | return false; |
| 502 | } |
| 503 | |
Erik Språng | f5420d1 | 2018-09-03 09:09:40 | [diff] [blame] | 504 | media::VideoEncodeAccelerator::Config::ContentType content_type; |
| 505 | if (!input.ReadContentType(&content_type)) |
| 506 | return false; |
Pin-chih Lin | e6ff3d97 | 2018-07-27 11:15:04 | [diff] [blame] | 507 | |
Hirokazu Honda | 24e3492 | 2023-12-12 13:31:40 | [diff] [blame] | 508 | uint8_t drop_frame_thresh_percentage = input.drop_frame_thresh_percentage(); |
| 509 | if (drop_frame_thresh_percentage > 100) { |
| 510 | return false; |
| 511 | } |
Joey Arhar | 60b3f6a | 2020-04-03 21:52:17 | [diff] [blame] | 512 | std::vector<media::VideoEncodeAccelerator::Config::SpatialLayer> |
| 513 | spatial_layers; |
| 514 | if (!input.ReadSpatialLayers(&spatial_layers)) |
| 515 | return false; |
| 516 | |
Hirokazu Honda | 4723b5e3 | 2023-07-03 00:31:04 | [diff] [blame] | 517 | media::SVCInterLayerPredMode inter_layer_pred; |
Zhaoliang Ma | 2bae0df | 2021-06-07 02:58:01 | [diff] [blame] | 518 | if (!input.ReadInterLayerPred(&inter_layer_pred)) |
| 519 | return false; |
| 520 | |
Dale Curtis | 13f72b7 | 2022-11-17 23:37:23 | [diff] [blame] | 521 | media::VideoEncodeAccelerator::Config::EncoderType required_encoder_type; |
| 522 | if (!input.ReadRequiredEncoderType(&required_encoder_type)) |
| 523 | return false; |
| 524 | |
Hirokazu Honda | f9ba5f4 | 2023-12-01 08:51:00 | [diff] [blame] | 525 | struct CheckVEAConfig { |
| 526 | // The variable declaration order must be the same as |
| 527 | // VideoEncodeAccelerator::Config. |
| 528 | media::VideoPixelFormat input_format; |
| 529 | gfx::Size input_visible_size; |
| 530 | media::VideoCodecProfile output_profile; |
| 531 | media::Bitrate bitrate; |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 532 | std::optional<uint32_t> initial_framerate; |
| 533 | std::optional<uint32_t> gop_length; |
| 534 | std::optional<uint8_t> h264_output_level; |
Hirokazu Honda | f9ba5f4 | 2023-12-01 08:51:00 | [diff] [blame] | 535 | bool is_constrained_h264; |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 536 | std::optional<media::VideoEncodeAccelerator::Config::StorageType> |
Hirokazu Honda | f9ba5f4 | 2023-12-01 08:51:00 | [diff] [blame] | 537 | storage_type; |
| 538 | media::VideoEncodeAccelerator::Config::ContentType content_type; |
Hirokazu Honda | 24e3492 | 2023-12-12 13:31:40 | [diff] [blame] | 539 | uint8_t drop_frame_thresh_percentage; |
Hirokazu Honda | f9ba5f4 | 2023-12-01 08:51:00 | [diff] [blame] | 540 | std::vector<media::VideoEncodeAccelerator::Config::SpatialLayer> |
| 541 | spatial_layers; |
| 542 | media::SVCInterLayerPredMode inter_layer_pred; |
| 543 | bool require_low_delay = true; |
| 544 | media::VideoEncodeAccelerator::Config::EncoderType required_encoder_type; |
| 545 | }; |
| 546 | static_assert( |
| 547 | sizeof(CheckVEAConfig) == sizeof(media::VideoEncodeAccelerator::Config), |
| 548 | "Please apply removed/added values in VideoEncodeAccelerator::Config " |
| 549 | "to the following copy and then remove/add the values in CheckVEAConfig"); |
Eugene Zemtsov | cc67543 | 2021-05-19 04:04:30 | [diff] [blame] | 550 | |
Hirokazu Honda | f9ba5f4 | 2023-12-01 08:51:00 | [diff] [blame] | 551 | *output = media::VideoEncodeAccelerator::Config( |
| 552 | input_format, input_visible_size, output_profile, bitrate); |
| 553 | output->initial_framerate = initial_framerate; |
| 554 | output->gop_length = gop_length; |
| 555 | output->h264_output_level = h264_output_level; |
| 556 | output->is_constrained_h264 = is_constrained_h264; |
| 557 | output->storage_type = storage_type; |
| 558 | output->content_type = content_type; |
Hirokazu Honda | 24e3492 | 2023-12-12 13:31:40 | [diff] [blame] | 559 | output->drop_frame_thresh_percentage = drop_frame_thresh_percentage; |
Hirokazu Honda | f9ba5f4 | 2023-12-01 08:51:00 | [diff] [blame] | 560 | output->spatial_layers = spatial_layers; |
| 561 | output->inter_layer_pred = inter_layer_pred; |
Eugene Zemtsov | cc67543 | 2021-05-19 04:04:30 | [diff] [blame] | 562 | output->require_low_delay = input.require_low_delay(); |
Dale Curtis | 13f72b7 | 2022-11-17 23:37:23 | [diff] [blame] | 563 | output->required_encoder_type = required_encoder_type; |
Clarissa Garvey | 76708b6 | 2021-06-30 22:53:26 | [diff] [blame] | 564 | |
Pin-chih Lin | e6ff3d97 | 2018-07-27 11:15:04 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | |
Miguel Casas-Sanchez | 15e2e59 | 2017-07-19 01:45:25 | [diff] [blame] | 568 | } // namespace mojo |