2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/cenc.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/buffer_reader.h"
|
|
|
|
#include "packager/media/base/buffer_writer.h"
|
|
|
|
#include "packager/media/formats/mp4/rcheck.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 02:23:30 +00:00
|
|
|
namespace {
|
|
|
|
// According to ISO/IEC FDIS 23001-7: CENC spec, IV should be either
|
|
|
|
// 64-bit (8-byte) or 128-bit (16-byte).
|
|
|
|
bool IsIvSizeValid(size_t iv_size) { return iv_size == 8 || iv_size == 16; }
|
|
|
|
|
2014-08-25 22:51:19 +00:00
|
|
|
// 16-bit |clear_bytes| and 32-bit |cipher_bytes|.
|
2014-09-30 21:52:21 +00:00
|
|
|
const size_t kSubsampleEntrySize = sizeof(uint16_t) + sizeof(uint32_t);
|
2013-11-27 02:23:30 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
|
|
|
FrameCENCInfo::FrameCENCInfo() {}
|
2014-09-30 21:52:21 +00:00
|
|
|
FrameCENCInfo::FrameCENCInfo(const std::vector<uint8_t>& iv) : iv_(iv) {
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
FrameCENCInfo::~FrameCENCInfo() {}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool FrameCENCInfo::Parse(uint8_t iv_size, BufferReader* reader) {
|
2013-11-27 02:23:30 +00:00
|
|
|
DCHECK(reader);
|
|
|
|
// Mandated by CENC spec.
|
|
|
|
RCHECK(IsIvSizeValid(iv_size));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 02:23:30 +00:00
|
|
|
iv_.resize(iv_size);
|
|
|
|
RCHECK(reader->ReadToVector(&iv_, iv_size));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 02:23:30 +00:00
|
|
|
if (!reader->HasBytes(1))
|
|
|
|
return true;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint16_t subsample_count;
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->Read2(&subsample_count) &&
|
2013-11-27 02:23:30 +00:00
|
|
|
reader->HasBytes(subsample_count * kSubsampleEntrySize));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-27 02:23:30 +00:00
|
|
|
subsamples_.resize(subsample_count);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint16_t i = 0; i < subsample_count; ++i) {
|
|
|
|
uint16_t clear_bytes;
|
|
|
|
uint32_t cipher_bytes;
|
2013-09-24 01:35:40 +00:00
|
|
|
RCHECK(reader->Read2(&clear_bytes) &&
|
2014-08-25 22:51:19 +00:00
|
|
|
reader->Read4(&cipher_bytes));
|
2013-11-27 02:23:30 +00:00
|
|
|
subsamples_[i].clear_bytes = clear_bytes;
|
2014-08-25 22:51:19 +00:00
|
|
|
subsamples_[i].cipher_bytes = cipher_bytes;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-27 02:23:30 +00:00
|
|
|
void FrameCENCInfo::Write(BufferWriter* writer) const {
|
|
|
|
DCHECK(writer);
|
|
|
|
DCHECK(IsIvSizeValid(iv_.size()));
|
|
|
|
writer->AppendVector(iv_);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint16_t subsample_count = subsamples_.size();
|
2013-11-27 02:23:30 +00:00
|
|
|
if (subsample_count == 0)
|
|
|
|
return;
|
|
|
|
writer->AppendInt(subsample_count);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint16_t i = 0; i < subsample_count; ++i) {
|
2013-11-27 02:23:30 +00:00
|
|
|
writer->AppendInt(subsamples_[i].clear_bytes);
|
2014-08-25 22:51:19 +00:00
|
|
|
writer->AppendInt(subsamples_[i].cipher_bytes);
|
2013-11-27 02:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t FrameCENCInfo::ComputeSize() const {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint16_t subsample_count = subsamples_.size();
|
2013-11-27 02:23:30 +00:00
|
|
|
if (subsample_count == 0)
|
|
|
|
return iv_.size();
|
|
|
|
|
|
|
|
return iv_.size() + sizeof(subsample_count) +
|
|
|
|
subsample_count * kSubsampleEntrySize;
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
size_t FrameCENCInfo::GetTotalSizeOfSubsamples() const {
|
|
|
|
size_t size = 0;
|
2013-11-27 02:23:30 +00:00
|
|
|
for (size_t i = 0; i < subsamples_.size(); ++i) {
|
2014-08-25 22:51:19 +00:00
|
|
|
size += subsamples_[i].clear_bytes + subsamples_[i].cipher_bytes;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|