Chromium Code Reviews| Index: media/formats/webm/opus_packet_builder.cc |
| diff --git a/media/formats/webm/opus_packet_builder.cc b/media/formats/webm/opus_packet_builder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..21aff160630baba37449f52b1b74241b773d4133 |
| --- /dev/null |
| +++ b/media/formats/webm/opus_packet_builder.cc |
| @@ -0,0 +1,81 @@ |
| +#include "media/formats/webm/opus_packet_builder.h" |
| +#include "media/formats/webm/webm_cluster_parser.h" |
| + |
| +namespace media { |
| + |
| +// From Opus RFC. See http://goo.gl/2RmoxA |
| +enum OpusConstants { |
| + kNumPossibleOpusConfigs = 32, |
| + kMinOpusPacketFrameCount = 1, |
| + kMaxOpusPacketFrameCount = 48 |
| +}; |
| + |
| +OpusPacketInfo::OpusPacketInfo() { |
| +} |
| +OpusPacketInfo::~OpusPacketInfo() { |
| +} |
| + |
| +scoped_ptr<OpusPacketInfo> BuildOpusPacket(int config, |
|
wolenetz
2015/02/03 22:47:01
ISTM that this could be a constructor. Also, the d
chcunningham
2015/02/05 02:48:20
Done.
|
| + int frame_count, |
| + bool is_VBR) { |
| + scoped_ptr<OpusPacketInfo> packet_info(new OpusPacketInfo()); |
| + packet_info->toc_config = config; |
| + packet_info->frame_count = frame_count; |
| + packet_info->duration_ms = |
| + frame_count * (WebMClusterParser::kOpusFrameDurationsMu[config] / |
| + static_cast<float>(1000)); |
| + |
| + int frame_count_code; |
| + uint8 frame_count_byte; |
|
wolenetz
2015/02/03 22:47:01
_t
chcunningham
2015/02/05 02:48:21
Done.
|
| + |
| + if (frame_count == 1) { |
| + frame_count_code = 0; |
| + } else if (frame_count == 2) { |
| + frame_count_code = is_VBR ? 2 : 1; |
| + } else { |
| + frame_count_code = 3; |
| + frame_count_byte = (is_VBR ? 1 << 7 : 0) | frame_count; |
| + } |
| + |
| + // All opus packets must have TOC byte. |
| + uint8 opus_toc_byte = (config << 3) | frame_count_code; |
| + packet_info->data.push_back(opus_toc_byte); |
| + |
| + // For code 3 packets, the number of frames is signalled in the "frame |
| + // count byte". |
| + if (frame_count_code == 3) { |
| + packet_info->data.push_back(frame_count_byte); |
| + } |
| + |
| + // Simulating encoded frame data. |
| + packet_info->data.push_back(static_cast<uint8>(0)); |
| + |
| + return packet_info.Pass(); |
| +} |
| + |
| +// Builds an exhaustive collection of Opus packet configurations. |
|
wolenetz
2015/02/03 22:47:01
nit: move method comment to .h
chcunningham
2015/02/05 02:48:21
Done.
|
| +std::vector<scoped_ptr<OpusPacketInfo>> BuildAllOpusPackets() { |
| + std::vector<scoped_ptr<OpusPacketInfo>> opus_packets; |
| + |
| + for (int frame_count = kMinOpusPacketFrameCount; |
| + frame_count <= kMaxOpusPacketFrameCount; frame_count++) { |
| + for (int opus_config_num = 0; opus_config_num < kNumPossibleOpusConfigs; |
| + opus_config_num++) { |
| + bool is_VBR = false; |
| + opus_packets.push_back( |
| + BuildOpusPacket(opus_config_num, frame_count, is_VBR)); |
| + |
| + if (frame_count >= 2) { |
| + // Add another packet with VBR flag toggled. For frame counts >= 2, |
| + // VBR triggers changes to packet framing. |
| + is_VBR = true; |
| + opus_packets.push_back( |
| + BuildOpusPacket(opus_config_num, frame_count, is_VBR)); |
| + } |
| + } |
| + } |
| + |
| + return opus_packets; |
| +} |
| + |
| +} // namespace media |