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
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580
  581
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604

media / cdm / cdm_type_conversion.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/cdm/cdm_type_conversion.h"

#include <stdint.h>

#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/geometry/size.h"

// Note: Unexpected values must be handled explicitly since some of these
// functions may be used at either side of the CDM interface, and it's possible
// invalid values are passed in. For example, Chromium loading an older CDM, or
// the CDM is loaded by a non-Chromium browser.

namespace media {

namespace {

cdm::ColorRange ToCdmColorRange(gfx::ColorSpace::RangeID range) {
  switch (range) {
    case gfx::ColorSpace::RangeID::INVALID:
      return cdm::ColorRange::kInvalid;
    case gfx::ColorSpace::RangeID::LIMITED:
      return cdm::ColorRange::kLimited;
    case gfx::ColorSpace::RangeID::FULL:
      return cdm::ColorRange::kFull;
    case gfx::ColorSpace::RangeID::DERIVED:
      return cdm::ColorRange::kDerived;
  }

  NOTREACHED() << "Unexpected color range";
}

gfx::ColorSpace::RangeID ToGfxColorRange(cdm::ColorRange range) {
  switch (range) {
    case cdm::ColorRange::kInvalid:
      return gfx::ColorSpace::RangeID::INVALID;
    case cdm::ColorRange::kLimited:
      return gfx::ColorSpace::RangeID::LIMITED;
    case cdm::ColorRange::kFull:
      return gfx::ColorSpace::RangeID::FULL;
    case cdm::ColorRange::kDerived:
      return gfx::ColorSpace::RangeID::DERIVED;
  }

  NOTREACHED() << "Unexpected color range";
}

}  // namespace

// Color Converters

cdm::ColorSpace ToCdmColorSpace(const VideoColorSpace& color_space) {
  // Cast is okay because both VideoColorSpace and cdm::ColorSpace follow the
  // standard ISO 23001-8:2016.
  return {base::checked_cast<uint8_t>(color_space.primaries),
          base::checked_cast<uint8_t>(color_space.transfer),
          base::checked_cast<uint8_t>(color_space.matrix),
          ToCdmColorRange(color_space.range)};
}

VideoColorSpace ToMediaColorSpace(const cdm::ColorSpace& color_space) {
  return VideoColorSpace(color_space.primary_id, color_space.transfer_id,
                         color_space.matrix_id,
                         ToGfxColorRange(color_space.range));
}

// CDM Converters

cdm::HdcpVersion ToCdmHdcpVersion(HdcpVersion hdcp_version) {
  switch (hdcp_version) {
    case HdcpVersion::kHdcpVersionNone:
      return cdm::kHdcpVersionNone;
    case HdcpVersion::kHdcpVersion1_0:
      return cdm::kHdcpVersion1_0;
    case HdcpVersion::kHdcpVersion1_1:
      return cdm::kHdcpVersion1_1;
    case HdcpVersion::kHdcpVersion1_2:
      return cdm::kHdcpVersion1_2;
    case HdcpVersion::kHdcpVersion1_3:
      return cdm::kHdcpVersion1_3;
    case HdcpVersion::kHdcpVersion1_4:
      return cdm::kHdcpVersion1_4;
    case HdcpVersion::kHdcpVersion2_0:
      return cdm::kHdcpVersion2_0;
    case HdcpVersion::kHdcpVersion2_1:
      return cdm::kHdcpVersion2_1;
    case HdcpVersion::kHdcpVersion2_2:
      return cdm::kHdcpVersion2_2;
    case HdcpVersion::kHdcpVersion2_3:
      return cdm::kHdcpVersion2_3;
  }

  NOTREACHED() << "Unexpected HdcpVersion";
}

cdm::SessionType ToCdmSessionType(CdmSessionType session_type) {
  switch (session_type) {
    case CdmSessionType::kTemporary:
      return cdm::kTemporary;
    case CdmSessionType::kPersistentLicense:
      return cdm::kPersistentLicense;
  }

  NOTREACHED() << "Unexpected session type " << static_cast<int>(session_type);
}

CdmSessionType ToMediaSessionType(cdm::SessionType session_type) {
  switch (session_type) {
    case cdm::kTemporary:
      return CdmSessionType::kTemporary;
    case cdm::kPersistentLicense:
      return CdmSessionType::kPersistentLicense;
    // TODO(crbug.com/40170205): Remove after `kPersistentUsageRecord` is
    // removed from the CDM interface.
    case cdm::kPersistentUsageRecord:
      break;
  }

  NOTREACHED() << "Unexpected cdm::SessionType " << session_type;
}

cdm::InitDataType ToCdmInitDataType(EmeInitDataType init_data_type) {
  switch (init_data_type) {
    case EmeInitDataType::CENC:
      return cdm::kCenc;
    case EmeInitDataType::KEYIDS:
      return cdm::kKeyIds;
    case EmeInitDataType::WEBM:
      return cdm::kWebM;
    case EmeInitDataType::UNKNOWN:
      break;
  }

  NOTREACHED() << "Unexpected EmeInitDataType";
}

EmeInitDataType ToEmeInitDataType(cdm::InitDataType init_data_type) {
  switch (init_data_type) {
    case cdm::kCenc:
      return EmeInitDataType::CENC;
    case cdm::kKeyIds:
      return EmeInitDataType::KEYIDS;
    case cdm::kWebM:
      return EmeInitDataType::WEBM;
  }

  NOTREACHED() << "Unexpected cdm::InitDataType " << init_data_type;
}

CdmKeyInformation::KeyStatus ToMediaKeyStatus(cdm::KeyStatus status) {
  switch (status) {
    case cdm::kUsable:
      return CdmKeyInformation::USABLE;
    case cdm::kInternalError:
      return CdmKeyInformation::INTERNAL_ERROR;
    case cdm::kExpired:
      return CdmKeyInformation::EXPIRED;
    case cdm::kOutputRestricted:
      return CdmKeyInformation::OUTPUT_RESTRICTED;
    case cdm::kOutputDownscaled:
      return CdmKeyInformation::OUTPUT_DOWNSCALED;
    case cdm::kStatusPending:
      return CdmKeyInformation::KEY_STATUS_PENDING;
    case cdm::kReleased:
      return CdmKeyInformation::RELEASED;
  }

  NOTREACHED() << "Unexpected cdm::KeyStatus " << status;
}

cdm::KeyStatus ToCdmKeyStatus(CdmKeyInformation::KeyStatus status) {
  switch (status) {
    case CdmKeyInformation::KeyStatus::USABLE:
      return cdm::kUsable;
    case CdmKeyInformation::KeyStatus::INTERNAL_ERROR:
      return cdm::kInternalError;
    case CdmKeyInformation::KeyStatus::EXPIRED:
      return cdm::kExpired;
    case CdmKeyInformation::KeyStatus::OUTPUT_RESTRICTED:
      return cdm::kOutputRestricted;
    case CdmKeyInformation::KeyStatus::OUTPUT_DOWNSCALED:
      return cdm::kOutputDownscaled;
    case CdmKeyInformation::KeyStatus::KEY_STATUS_PENDING:
      return cdm::kStatusPending;
    case CdmKeyInformation::KeyStatus::RELEASED:
      return cdm::kReleased;
  }

  NOTREACHED() << "Unexpected CdmKeyInformation::KeyStatus " << status;
}

cdm::EncryptionScheme ToCdmEncryptionScheme(EncryptionScheme scheme) {
  switch (scheme) {
    case EncryptionScheme::kUnencrypted:
      return cdm::EncryptionScheme::kUnencrypted;
    case EncryptionScheme::kCenc:
      return cdm::EncryptionScheme::kCenc;
    case EncryptionScheme::kCbcs:
      return cdm::EncryptionScheme::kCbcs;
  }

  NOTREACHED() << "Unexpected EncryptionScheme";
}

CdmPromise::Exception ToMediaCdmPromiseException(cdm::Exception exception) {
  switch (exception) {
    case cdm::kExceptionTypeError:
      return CdmPromise::Exception::TYPE_ERROR;
    case cdm::kExceptionNotSupportedError:
      return CdmPromise::Exception::NOT_SUPPORTED_ERROR;
    case cdm::kExceptionInvalidStateError:
      return CdmPromise::Exception::INVALID_STATE_ERROR;
    case cdm::kExceptionQuotaExceededError:
      return CdmPromise::Exception::QUOTA_EXCEEDED_ERROR;
  }

  NOTREACHED() << "Unexpected cdm::Exception " << exception;
}

cdm::Exception ToCdmException(CdmPromise::Exception exception) {
  switch (exception) {
    case CdmPromise::Exception::NOT_SUPPORTED_ERROR:
      return cdm::kExceptionNotSupportedError;
    case CdmPromise::Exception::INVALID_STATE_ERROR:
      return cdm::kExceptionInvalidStateError;
    case CdmPromise::Exception::TYPE_ERROR:
      return cdm::kExceptionTypeError;
    case CdmPromise::Exception::QUOTA_EXCEEDED_ERROR:
      return cdm::kExceptionQuotaExceededError;
  }

  NOTREACHED() << "Unexpected CdmPromise::Exception";
}

CdmMessageType ToMediaMessageType(cdm::MessageType message_type) {
  switch (message_type) {
    case cdm::kLicenseRequest:
      return CdmMessageType::LICENSE_REQUEST;
    case cdm::kLicenseRenewal:
      return CdmMessageType::LICENSE_RENEWAL;
    case cdm::kLicenseRelease:
      return CdmMessageType::LICENSE_RELEASE;
    case cdm::kIndividualizationRequest:
      return CdmMessageType::INDIVIDUALIZATION_REQUEST;
  }

  NOTREACHED() << "Unexpected cdm::MessageType " << message_type;
}

cdm::MessageType ToCdmMessageType(CdmMessageType message_type) {
  switch (message_type) {
    case CdmMessageType::LICENSE_REQUEST:
      return cdm::kLicenseRequest;
    case CdmMessageType::LICENSE_RENEWAL:
      return cdm::kLicenseRenewal;
    case CdmMessageType::LICENSE_RELEASE:
      return cdm::kLicenseRelease;
    case CdmMessageType::INDIVIDUALIZATION_REQUEST:
      return cdm::kIndividualizationRequest;
  }

  NOTREACHED() << "Unexpected CdmMessageType";
}

cdm::StreamType ToCdmStreamType(Decryptor::StreamType stream_type) {
  switch (stream_type) {
    case Decryptor::kAudio:
      return cdm::kStreamTypeAudio;
    case Decryptor::kVideo:
      return cdm::kStreamTypeVideo;
  }

  NOTREACHED() << "Unexpected Decryptor::StreamType " << stream_type;
}

Decryptor::Status ToMediaDecryptorStatus(cdm::Status status) {
  switch (status) {
    case cdm::kSuccess:
      return Decryptor::kSuccess;
    case cdm::kNoKey:
      return Decryptor::kNoKey;
    case cdm::kNeedMoreData:
      return Decryptor::kNeedMoreData;
    case cdm::kDecryptError:
      return Decryptor::kError;
    case cdm::kDecodeError:
      return Decryptor::kError;
    case cdm::kInitializationError:
    case cdm::kDeferredInitialization:
      break;
  }

  NOTREACHED() << "Unexpected cdm::Status " << status;
}

// Audio Converters

cdm::AudioCodec ToCdmAudioCodec(AudioCodec codec) {
  switch (codec) {
    case AudioCodec::kVorbis:
      return cdm::kCodecVorbis;
    case AudioCodec::kAAC:
      return cdm::kCodecAac;
    default:
      DVLOG(1) << "Unsupported AudioCodec " << codec;
      return cdm::kUnknownAudioCodec;
  }
}

SampleFormat ToMediaSampleFormat(cdm::AudioFormat format) {
  switch (format) {
    case cdm::kAudioFormatU8:
      return kSampleFormatU8;
    case cdm::kAudioFormatS16:
      return kSampleFormatS16;
    case cdm::kAudioFormatS32:
      return kSampleFormatS32;
    case cdm::kAudioFormatF32:
      return kSampleFormatF32;
    case cdm::kAudioFormatPlanarS16:
      return kSampleFormatPlanarS16;
    case cdm::kAudioFormatPlanarF32:
      return kSampleFormatPlanarF32;
    case cdm::kUnknownAudioFormat:
      return kUnknownSampleFormat;
  }

  NOTREACHED() << "Unexpected cdm::AudioFormat " << format;
}

// Video Converters

cdm::VideoCodec ToCdmVideoCodec(VideoCodec codec) {
  switch (codec) {
    case VideoCodec::kVP8:
      return cdm::kCodecVp8;
    case VideoCodec::kH264:
      return cdm::kCodecH264;
    case VideoCodec::kVP9:
      return cdm::kCodecVp9;
    case VideoCodec::kAV1:
      return cdm::kCodecAv1;
    default:
      DVLOG(1) << "Unsupported VideoCodec " << codec;
      return cdm::kUnknownVideoCodec;
  }
}

VideoCodec ToMediaVideoCodec(cdm::VideoCodec codec) {
  switch (codec) {
    case cdm::kUnknownVideoCodec:
      return VideoCodec::kUnknown;
    case cdm::kCodecVp8:
      return VideoCodec::kVP8;
    case cdm::kCodecH264:
      return VideoCodec::kH264;
    case cdm::kCodecVp9:
      return VideoCodec::kVP9;
    case cdm::kCodecAv1:
      return VideoCodec::kAV1;
  }

  NOTREACHED() << "Unexpected cdm::VideoCodec " << codec;
}

cdm::VideoCodecProfile ToCdmVideoCodecProfile(VideoCodecProfile profile) {
  switch (profile) {
    case VP8PROFILE_ANY:
      return cdm::kProfileNotNeeded;
    case VP9PROFILE_PROFILE0:
      return cdm::kVP9Profile0;
    case VP9PROFILE_PROFILE1:
      return cdm::kVP9Profile1;
    case VP9PROFILE_PROFILE2:
      return cdm::kVP9Profile2;
    case VP9PROFILE_PROFILE3:
      return cdm::kVP9Profile3;
    case H264PROFILE_BASELINE:
      return cdm::kH264ProfileBaseline;
    case H264PROFILE_MAIN:
      return cdm::kH264ProfileMain;
    case H264PROFILE_EXTENDED:
      return cdm::kH264ProfileExtended;
    case H264PROFILE_HIGH:
      return cdm::kH264ProfileHigh;
    case H264PROFILE_HIGH10PROFILE:
      return cdm::kH264ProfileHigh10;
    case H264PROFILE_HIGH422PROFILE:
      return cdm::kH264ProfileHigh422;
    case H264PROFILE_HIGH444PREDICTIVEPROFILE:
      return cdm::kH264ProfileHigh444Predictive;
    case AV1PROFILE_PROFILE_MAIN:
      return cdm::kAv1ProfileMain;
    case AV1PROFILE_PROFILE_HIGH:
      return cdm::kAv1ProfileHigh;
    case AV1PROFILE_PROFILE_PRO:
      return cdm::kAv1ProfilePro;
    default:
      DVLOG(1) << "Unsupported VideoCodecProfile " << profile;
      return cdm::kUnknownVideoCodecProfile;
  }
}

VideoCodecProfile ToMediaVideoCodecProfile(cdm::VideoCodecProfile profile) {
  switch (profile) {
    case cdm::kUnknownVideoCodecProfile:
      return VIDEO_CODEC_PROFILE_UNKNOWN;
    case cdm::kProfileNotNeeded:
      // There's no corresponding value for "not needed". Given CdmAdapter only
      // converts VP8PROFILE_ANY to cdm::kProfileNotNeeded, and this code is
      // only used for testing, it's okay to convert it back to VP8PROFILE_ANY.
      return VP8PROFILE_ANY;
    case cdm::kVP9Profile0:
      return VP9PROFILE_PROFILE0;
    case cdm::kVP9Profile1:
      return VP9PROFILE_PROFILE1;
    case cdm::kVP9Profile2:
      return VP9PROFILE_PROFILE2;
    case cdm::kVP9Profile3:
      return VP9PROFILE_PROFILE3;
    case cdm::kH264ProfileBaseline:
      return H264PROFILE_BASELINE;
    case cdm::kH264ProfileMain:
      return H264PROFILE_MAIN;
    case cdm::kH264ProfileExtended:
      return H264PROFILE_EXTENDED;
    case cdm::kH264ProfileHigh:
      return H264PROFILE_HIGH;
    case cdm::kH264ProfileHigh10:
      return H264PROFILE_HIGH10PROFILE;
    case cdm::kH264ProfileHigh422:
      return H264PROFILE_HIGH422PROFILE;
    case cdm::kH264ProfileHigh444Predictive:
      return H264PROFILE_HIGH444PREDICTIVEPROFILE;
    case cdm::kAv1ProfileMain:
      return AV1PROFILE_PROFILE_MAIN;
    case cdm::kAv1ProfileHigh:
      return AV1PROFILE_PROFILE_HIGH;
    case cdm::kAv1ProfilePro:
      return AV1PROFILE_PROFILE_PRO;
  }

  NOTREACHED() << "Unexpected cdm::VideoCodecProfile " << profile;
}

cdm::VideoFormat ToCdmVideoFormat(VideoPixelFormat format) {
  switch (format) {
    case PIXEL_FORMAT_YV12:
      return cdm::kYv12;
    case PIXEL_FORMAT_I420:
      return cdm::kI420;
    case PIXEL_FORMAT_YUV420P9:
      return cdm::kYUV420P9;
    case PIXEL_FORMAT_YUV420P10:
      return cdm::kYUV420P10;
    case PIXEL_FORMAT_YUV422P9:
      return cdm::kYUV422P9;
    case PIXEL_FORMAT_YUV422P10:
      return cdm::kYUV422P10;
    case PIXEL_FORMAT_YUV444P9:
      return cdm::kYUV444P9;
    case PIXEL_FORMAT_YUV444P10:
      return cdm::kYUV444P10;
    case PIXEL_FORMAT_YUV420P12:
      return cdm::kYUV420P12;
    case PIXEL_FORMAT_YUV422P12:
      return cdm::kYUV422P12;
    case PIXEL_FORMAT_YUV444P12:
      return cdm::kYUV444P12;
    default:
      DVLOG(1) << "Unsupported VideoPixelFormat " << format;
      return cdm::kUnknownVideoFormat;
  }
}

VideoPixelFormat ToMediaVideoFormat(cdm::VideoFormat format) {
  switch (format) {
    case cdm::kYv12:
      return PIXEL_FORMAT_YV12;
    case cdm::kI420:
      return PIXEL_FORMAT_I420;
    case cdm::kYUV420P9:
      return PIXEL_FORMAT_YUV420P9;
    case cdm::kYUV420P10:
      return PIXEL_FORMAT_YUV420P10;
    case cdm::kYUV422P9:
      return PIXEL_FORMAT_YUV422P9;
    case cdm::kYUV422P10:
      return PIXEL_FORMAT_YUV422P10;
    case cdm::kYUV444P9:
      return PIXEL_FORMAT_YUV444P9;
    case cdm::kYUV444P10:
      return PIXEL_FORMAT_YUV444P10;
    case cdm::kYUV420P12:
      return PIXEL_FORMAT_YUV420P12;
    case cdm::kYUV422P12:
      return PIXEL_FORMAT_YUV422P12;
    case cdm::kYUV444P12:
      return PIXEL_FORMAT_YUV444P12;
    default:
      DVLOG(1) << "Unsupported cdm::VideoFormat " << format;
      return PIXEL_FORMAT_UNKNOWN;
  }
}

// Aggregate Types

// Warning: The returned config contains raw pointers to the extra data in the
// input |config|. Hence, the caller must make sure the input |config| outlives
// the returned config.
cdm::AudioDecoderConfig_2 ToCdmAudioDecoderConfig(
    const AudioDecoderConfig& config) {
  cdm::AudioDecoderConfig_2 cdm_config = {};
  cdm_config.codec = ToCdmAudioCodec(config.codec());
  cdm_config.channel_count =
      ChannelLayoutToChannelCount(config.channel_layout());
  cdm_config.bits_per_channel = config.bytes_per_channel() * 8;
  cdm_config.samples_per_second = config.samples_per_second();
  cdm_config.extra_data = const_cast<uint8_t*>(config.extra_data().data());
  cdm_config.extra_data_size = config.extra_data().size();
  cdm_config.encryption_scheme =
      ToCdmEncryptionScheme(config.encryption_scheme());
  return cdm_config;
}

// Warning: The returned config contains raw pointers to the extra data in the
// input |config|. Hence, the caller must make sure the input |config| outlives
// the returned config.
cdm::VideoDecoderConfig_3 ToCdmVideoDecoderConfig(
    const VideoDecoderConfig& config) {
  cdm::VideoDecoderConfig_3 cdm_config = {};
  cdm_config.codec = ToCdmVideoCodec(config.codec());
  cdm_config.profile = ToCdmVideoCodecProfile(config.profile());

  // TODO(dalecurtis): CDM doesn't support alpha, so delete |format|.
  DCHECK_EQ(config.alpha_mode(), VideoDecoderConfig::AlphaMode::kIsOpaque);
  cdm_config.format = cdm::kI420;

  cdm_config.color_space = ToCdmColorSpace(config.color_space_info());
  cdm_config.coded_size.width = config.coded_size().width();
  cdm_config.coded_size.height = config.coded_size().height();
  cdm_config.extra_data = const_cast<uint8_t*>(config.extra_data().data());
  cdm_config.extra_data_size = config.extra_data().size();
  cdm_config.encryption_scheme =
      ToCdmEncryptionScheme(config.encryption_scheme());
  return cdm_config;
}

// Fill |input_buffer| based on the values in |encrypted|. |subsamples|
// is used to hold some of the data. |input_buffer| will contain pointers
// to data contained in |encrypted| and |subsamples|, so the lifetime of
// |input_buffer| must be <= the lifetime of |encrypted| and |subsamples|.
void ToCdmInputBuffer(const DecoderBuffer& encrypted_buffer,
                      std::vector<cdm::SubsampleEntry>* subsamples,
                      cdm::InputBuffer_2* input_buffer) {
  // End of stream buffers are represented as empty resources.
  DCHECK(!input_buffer->data);
  if (encrypted_buffer.end_of_stream())
    return;

  input_buffer->data = encrypted_buffer.data();
  input_buffer->data_size = encrypted_buffer.size();
  input_buffer->timestamp = encrypted_buffer.timestamp().InMicroseconds();

  const DecryptConfig* decrypt_config = encrypted_buffer.decrypt_config();
  if (!decrypt_config) {
    DVLOG(3) << __func__ << ": Clear buffer.";
    return;
  }

  input_buffer->key_id =
      reinterpret_cast<const uint8_t*>(decrypt_config->key_id().data());
  input_buffer->key_id_size = decrypt_config->key_id().size();
  input_buffer->iv =
      reinterpret_cast<const uint8_t*>(decrypt_config->iv().data());
  input_buffer->iv_size = decrypt_config->iv().size();

  DCHECK(subsamples->empty());
  size_t num_subsamples = decrypt_config->subsamples().size();
  if (num_subsamples > 0) {
    subsamples->reserve(num_subsamples);
    for (const auto& sample : decrypt_config->subsamples()) {
      subsamples->push_back({sample.clear_bytes, sample.cypher_bytes});
    }
  }

  input_buffer->subsamples = subsamples->data();
  input_buffer->num_subsamples = num_subsamples;

  input_buffer->encryption_scheme =
      ToCdmEncryptionScheme(decrypt_config->encryption_scheme());
  if (decrypt_config->HasPattern()) {
    input_buffer->pattern = {
        decrypt_config->encryption_pattern()->crypt_byte_block(),
        decrypt_config->encryption_pattern()->skip_byte_block()};
  }
}

}  // namespace media