[MD settings] moving attached() code
[chromium-blink-merge.git] / media / formats / webm / opus_packet_builder.cc
blobb2dd8dca1e1de13dfc2dbafb665a1b51ec2b74eb
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.
5 #include "base/logging.h"
6 #include "media/formats/webm/opus_packet_builder.h"
7 #include "media/formats/webm/webm_cluster_parser.h"
9 namespace media {
11 OpusPacket::OpusPacket(uint8_t config, uint8_t frame_count, bool is_VBR) {
12 DCHECK_GE(config, 0);
13 DCHECK_LT(config, kNumPossibleOpusConfigs);
14 DCHECK_GE(frame_count, kMinOpusPacketFrameCount);
15 DCHECK_LE(frame_count, kMaxOpusPacketFrameCount);
17 duration_ms_ = frame_count *
18 WebMClusterParser::kOpusFrameDurationsMu[config] /
19 static_cast<float>(1000);
21 uint8_t frame_count_code;
22 uint8_t frame_count_byte;
24 if (frame_count == 1) {
25 frame_count_code = 0;
26 } else if (frame_count == 2) {
27 frame_count_code = is_VBR ? 2 : 1;
28 } else {
29 frame_count_code = 3;
30 frame_count_byte = (is_VBR ? 1 << 7 : 0) | frame_count;
33 // All opus packets must have TOC byte.
34 uint8_t opus_toc_byte = (config << 3) | frame_count_code;
35 data_.push_back(opus_toc_byte);
37 // For code 3 packets, the number of frames is signaled in the "frame
38 // count byte".
39 if (frame_count_code == 3) {
40 data_.push_back(frame_count_byte);
43 // Packet will only conform to layout specification for the TOC byte
44 // and optional frame count bytes appended above. This last byte
45 // is purely dummy padding where frame size data or encoded data might
46 // otherwise start.
47 data_.push_back(static_cast<uint8_t>(0));
50 OpusPacket::~OpusPacket() {
53 const uint8_t* OpusPacket::data() const {
54 return &(data_[0]);
57 int OpusPacket::size() const {
58 return data_.size();
61 double OpusPacket::duration_ms() const {
62 return duration_ms_;
65 ScopedVector<OpusPacket> BuildAllOpusPackets() {
66 ScopedVector<OpusPacket> opus_packets;
68 for (int frame_count = kMinOpusPacketFrameCount;
69 frame_count <= kMaxOpusPacketFrameCount; frame_count++) {
70 for (int opus_config_num = 0; opus_config_num < kNumPossibleOpusConfigs;
71 opus_config_num++) {
72 bool is_VBR = false;
73 opus_packets.push_back(
74 new OpusPacket(opus_config_num, frame_count, is_VBR));
76 if (frame_count >= 2) {
77 // Add another packet with VBR flag toggled. For frame counts >= 2,
78 // VBR triggers changes to packet framing.
79 is_VBR = true;
80 opus_packets.push_back(
81 new OpusPacket(opus_config_num, frame_count, is_VBR));
86 return opus_packets.Pass();
89 } // namespace media