1// Copyright (c) 2012 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/mp4/es_descriptor.h"
6
7#include "media/base/bit_reader.h"
8#include "media/mp4/rcheck.h"
9
10// The elementary stream size is specific by up to 4 bytes.
11// The MSB of a byte indicates if there are more bytes for the size.
12static bool ReadESSize(media::BitReader* reader, uint32* size) {
13  uint8 msb;
14  uint8 byte;
15
16  *size = 0;
17
18  for (size_t i = 0; i < 4; ++i) {
19    RCHECK(reader->ReadBits(1, &msb));
20    RCHECK(reader->ReadBits(7, &byte));
21    *size = (*size << 7) + byte;
22
23    if (msb == 0)
24      break;
25  }
26
27  return true;
28}
29
30namespace media {
31
32namespace mp4 {
33
34// static
35bool ESDescriptor::IsAAC(uint8 object_type) {
36  return object_type == kISO_14496_3 || object_type == kISO_13818_7_AAC_LC;
37}
38
39ESDescriptor::ESDescriptor()
40    : object_type_(kForbidden) {
41}
42
43ESDescriptor::~ESDescriptor() {}
44
45bool ESDescriptor::Parse(const std::vector<uint8>& data) {
46  BitReader reader(&data[0], data.size());
47  uint8 tag;
48  uint32 size;
49  uint8 stream_dependency_flag;
50  uint8 url_flag;
51  uint8 ocr_stream_flag;
52  uint16 dummy;
53
54  RCHECK(reader.ReadBits(8, &tag));
55  RCHECK(tag == kESDescrTag);
56  RCHECK(ReadESSize(&reader, &size));
57
58  RCHECK(reader.ReadBits(16, &dummy));  // ES_ID
59  RCHECK(reader.ReadBits(1, &stream_dependency_flag));
60  RCHECK(reader.ReadBits(1, &url_flag));
61  RCHECK(!url_flag);  // We don't support url flag
62  RCHECK(reader.ReadBits(1, &ocr_stream_flag));
63  RCHECK(reader.ReadBits(5, &dummy));  // streamPriority
64
65  if (stream_dependency_flag)
66    RCHECK(reader.ReadBits(16, &dummy));  // dependsOn_ES_ID
67  if (ocr_stream_flag)
68    RCHECK(reader.ReadBits(16, &dummy));  // OCR_ES_Id
69
70  RCHECK(ParseDecoderConfigDescriptor(&reader));
71
72  return true;
73}
74
75uint8 ESDescriptor::object_type() const {
76  return object_type_;
77}
78
79const std::vector<uint8>& ESDescriptor::decoder_specific_info() const {
80  return decoder_specific_info_;
81}
82
83bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
84  uint8 tag;
85  uint32 size;
86  uint64 dummy;
87
88  RCHECK(reader->ReadBits(8, &tag));
89  RCHECK(tag == kDecoderConfigDescrTag);
90  RCHECK(ReadESSize(reader, &size));
91
92  RCHECK(reader->ReadBits(8, &object_type_));
93  RCHECK(reader->ReadBits(64, &dummy));
94  RCHECK(reader->ReadBits(32, &dummy));
95  RCHECK(ParseDecoderSpecificInfo(reader));
96
97  return true;
98}
99
100bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
101  uint8 tag;
102  uint32 size;
103
104  RCHECK(reader->ReadBits(8, &tag));
105  RCHECK(tag == kDecoderSpecificInfoTag);
106  RCHECK(ReadESSize(reader, &size));
107
108  decoder_specific_info_.resize(size);
109  for (uint32 i = 0; i < size; ++i)
110    RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
111
112  return true;
113}
114
115}  // namespace mp4
116
117}  // namespace media
118