1// Copyright 2014 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 "media/formats/mp4/cenc.h"
6
7#include <cstring>
8
9#include "media/formats/mp4/box_reader.h"
10#include "media/formats/mp4/rcheck.h"
11
12namespace media {
13namespace mp4 {
14
15FrameCENCInfo::FrameCENCInfo() {}
16FrameCENCInfo::~FrameCENCInfo() {}
17
18bool FrameCENCInfo::Parse(int iv_size, BufferReader* reader) {
19  const int kEntrySize = 6;
20  // Mandated by CENC spec
21  RCHECK(iv_size == 8 || iv_size == 16);
22
23  memset(iv, 0, sizeof(iv));
24  for (int i = 0; i < iv_size; i++)
25    RCHECK(reader->Read1(&iv[i]));
26
27  if (!reader->HasBytes(1)) return true;
28
29  uint16 subsample_count;
30  RCHECK(reader->Read2(&subsample_count) &&
31         reader->HasBytes(subsample_count * kEntrySize));
32
33  subsamples.resize(subsample_count);
34  for (int i = 0; i < subsample_count; i++) {
35    uint16 clear_bytes;
36    uint32 cypher_bytes;
37    RCHECK(reader->Read2(&clear_bytes) &&
38           reader->Read4(&cypher_bytes));
39    subsamples[i].clear_bytes = clear_bytes;
40    subsamples[i].cypher_bytes = cypher_bytes;
41  }
42  return true;
43}
44
45bool FrameCENCInfo::GetTotalSizeOfSubsamples(size_t* total_size) const {
46  size_t size = 0;
47  for (size_t i = 0; i < subsamples.size(); i++) {
48    size += subsamples[i].clear_bytes;
49    RCHECK(size >= subsamples[i].clear_bytes);  // overflow
50    size += subsamples[i].cypher_bytes;
51    RCHECK(size >= subsamples[i].cypher_bytes);  // overflow
52  }
53  *total_size = size;
54  return true;
55}
56
57}  // namespace mp4
58}  // namespace media
59