DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
opus_packet_builder.cc
1 // Copyright (c) 2015 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 "packager/base/logging.h"
6 #include "packager/media/formats/webm/opus_packet_builder.h"
7 #include "packager/media/formats/webm/webm_cluster_parser.h"
8 
9 namespace edash_packager {
10 namespace media {
11 
12 OpusPacket::OpusPacket(uint8_t config, uint8_t frame_count, bool is_VBR) {
13  DCHECK_GE(config, 0);
14  DCHECK_LT(config, kNumPossibleOpusConfigs);
15  DCHECK_GE(frame_count, kMinOpusPacketFrameCount);
16  DCHECK_LE(frame_count, kMaxOpusPacketFrameCount);
17 
18  duration_ms_ = frame_count *
19  WebMClusterParser::kOpusFrameDurationsMu[config] /
20  static_cast<float>(1000);
21 
22  uint8_t frame_count_code;
23  uint8_t frame_count_byte;
24 
25  if (frame_count == 1) {
26  frame_count_code = 0;
27  } else if (frame_count == 2) {
28  frame_count_code = is_VBR ? 2 : 1;
29  } else {
30  frame_count_code = 3;
31  frame_count_byte = (is_VBR ? 1 << 7 : 0) | frame_count;
32  }
33 
34  // All opus packets must have TOC byte.
35  uint8_t opus_toc_byte = (config << 3) | frame_count_code;
36  data_.push_back(opus_toc_byte);
37 
38  // For code 3 packets, the number of frames is signaled in the "frame
39  // count byte".
40  if (frame_count_code == 3) {
41  data_.push_back(frame_count_byte);
42  }
43 
44  // Packet will only conform to layout specification for the TOC byte
45  // and optional frame count bytes appended above. This last byte
46  // is purely dummy padding where frame size data or encoded data might
47  // otherwise start.
48  data_.push_back(static_cast<uint8_t>(0));
49 }
50 
51 OpusPacket::~OpusPacket() {
52 }
53 
54 const uint8_t* OpusPacket::data() const {
55  return &(data_[0]);
56 }
57 
58 int OpusPacket::size() const {
59  return data_.size();
60 }
61 
62 double OpusPacket::duration_ms() const {
63  return duration_ms_;
64 }
65 
66 ScopedVector<OpusPacket> BuildAllOpusPackets() {
67  ScopedVector<OpusPacket> opus_packets;
68 
69  for (int frame_count = kMinOpusPacketFrameCount;
70  frame_count <= kMaxOpusPacketFrameCount; frame_count++) {
71  for (int opus_config_num = 0; opus_config_num < kNumPossibleOpusConfigs;
72  opus_config_num++) {
73  bool is_VBR = false;
74  opus_packets.push_back(
75  new OpusPacket(opus_config_num, frame_count, is_VBR));
76 
77  if (frame_count >= 2) {
78  // Add another packet with VBR flag toggled. For frame counts >= 2,
79  // VBR triggers changes to packet framing.
80  is_VBR = true;
81  opus_packets.push_back(
82  new OpusPacket(opus_config_num, frame_count, is_VBR));
83  }
84  }
85  }
86 
87  return opus_packets.Pass();
88 }
89 
90 } // namespace media
91 } // namespace edash_packager