Shaka Packager SDK
Loading...
Searching...
No Matches
audio_stream_info.cc
1// Copyright 2014 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/media/base/audio_stream_info.h>
8
9#include <cinttypes>
10#include <cstddef>
11#include <cstdint>
12#include <memory>
13#include <string>
14
15#include <absl/log/log.h>
16#include <absl/strings/str_format.h>
17
18#include <packager/macros/logging.h>
19#include <packager/media/base/fourccs.h>
20#include <packager/media/base/limits.h>
21#include <packager/media/base/stream_info.h>
22
23namespace shaka {
24namespace media {
25
26namespace {
27std::string AudioCodecToString(Codec codec) {
28 switch (codec) {
29 case kCodecAAC:
30 return "AAC";
31 case kCodecAC3:
32 return "AC3";
33 case kCodecALAC:
34 return "ALAC";
35 case kCodecDTSC:
36 return "DTSC";
37 case kCodecDTSE:
38 return "DTSE";
39 case kCodecDTSH:
40 return "DTSH";
41 case kCodecDTSL:
42 return "DTSL";
43 case kCodecDTSM:
44 return "DTS-";
45 case kCodecDTSP:
46 return "DTS+";
47 case kCodecEAC3:
48 return "EAC3";
49 case kCodecAC4:
50 return "AC4";
51 case kCodecFlac:
52 return "FLAC";
53 case kCodecIAMF:
54 return "IAMF";
55 case kCodecOpus:
56 return "Opus";
57 case kCodecVorbis:
58 return "Vorbis";
59 case kCodecMP3:
60 return "MP3";
61 default:
62 NOTIMPLEMENTED() << "Unknown Audio Codec: " << codec;
63 return "UnknownCodec";
64 }
65}
66
67FourCC CodecToFourCC(Codec codec) {
68 switch (codec) {
69 case kCodecMha1:
70 return FOURCC_mha1;
71 case kCodecMhm1:
72 return FOURCC_mhm1;
73 default:
74 return FOURCC_NULL;
75 }
76}
77} // namespace
78
80 int32_t time_scale,
81 int64_t duration,
82 Codec codec,
83 const std::string& codec_string,
84 const uint8_t* codec_config,
85 size_t codec_config_size,
86 uint8_t sample_bits,
87 uint8_t num_channels,
88 uint32_t sampling_frequency,
89 uint64_t seek_preroll_ns,
90 uint64_t codec_delay_ns,
91 uint32_t max_bitrate,
92 uint32_t avg_bitrate,
93 const std::string& language,
94 bool is_encrypted)
95 : StreamInfo(kStreamAudio,
96 track_id,
97 time_scale,
98 duration,
99 codec,
100 codec_string,
101 codec_config,
102 codec_config_size,
103 language,
104 is_encrypted),
105 sample_bits_(sample_bits),
106 num_channels_(num_channels),
107 sampling_frequency_(sampling_frequency),
108 seek_preroll_ns_(seek_preroll_ns),
109 codec_delay_ns_(codec_delay_ns),
110 max_bitrate_(max_bitrate),
111 avg_bitrate_(avg_bitrate) {}
112
113AudioStreamInfo::~AudioStreamInfo() {}
114
116 return codec() != kUnknownCodec && num_channels_ != 0 &&
117 num_channels_ <= limits::kMaxChannels && sample_bits_ > 0 &&
118 sample_bits_ <= limits::kMaxBitsPerSample && sampling_frequency_ > 0 &&
119 sampling_frequency_ <= limits::kMaxSampleRate;
120}
121
122std::string AudioStreamInfo::ToString() const {
123 std::string str = absl::StrFormat(
124 "%s codec: %s\n sample_bits: %d\n num_channels: %d\n "
125 "sampling_frequency: %d\n language: %s\n",
126 StreamInfo::ToString().c_str(), AudioCodecToString(codec()).c_str(),
127 sample_bits_, num_channels_, sampling_frequency_, language().c_str());
128 if (seek_preroll_ns_ != 0) {
129 absl::StrAppendFormat(&str, " seek_preroll_ns: %" PRIu64 "\n",
130 seek_preroll_ns_);
131 }
132 if (codec_delay_ns_ != 0) {
133 absl::StrAppendFormat(&str, " codec_delay_ns: %" PRIu64 "\n",
134 codec_delay_ns_);
135 }
136 return str;
137}
138
139std::unique_ptr<StreamInfo> AudioStreamInfo::Clone() const {
140 return std::unique_ptr<StreamInfo>(new AudioStreamInfo(*this));
141}
142
143std::string AudioStreamInfo::GetCodecString(Codec codec,
144 uint8_t audio_object_type) {
145 switch (codec) {
146 case kCodecAAC:
147 return "mp4a.40." + absl::StrFormat("%hhu", audio_object_type);
148 case kCodecAC3:
149 return "ac-3";
150 case kCodecALAC:
151 return "alac";
152 case kCodecDTSC:
153 return "dtsc";
154 case kCodecDTSE:
155 return "dtse";
156 case kCodecDTSH:
157 return "dtsh";
158 case kCodecDTSL:
159 return "dtsl";
160 case kCodecDTSM:
161 return "dts-";
162 case kCodecDTSP:
163 return "dts+";
164 case kCodecDTSX:
165 return "dtsx";
166 case kCodecEAC3:
167 return "ec-3";
168 case kCodecAC4:
169 // ETSI TS 103 190-2 Digital Audio Compression (AC-4) Standard; Part 2:
170 // Immersive and personalized audio E.13. audio_object_type is composed of
171 // bitstream_version (3bits), presentation_version (2bits) and
172 // mdcompat (3bits).
173 return absl::StrFormat(
174 "ac-4.%02d.%02d.%02d", (audio_object_type & 0xE0) >> 5,
175 (audio_object_type & 0x18) >> 3, audio_object_type & 0x7);
176 case kCodecFlac:
177 return "flac";
178 case kCodecIAMF: {
179 // https://aomediacodec.github.io/iamf/#codecsparameter
180 // The codecs parameter string is composed as
181 //
182 // iamf.xxx.yyy.<standalone_codec_string>
183 //
184 // - xxx is the IAMF primary profile
185 // - yyy is the IAMF additional profile
186 // - <standalone_codec_string> are the elements of the codecs parameter
187 // string if that stream was carried in its own track
188 //
189 // audio_object_type is composed of primary_profile (2 bits),
190 // additional_profile (2 bits) and (IAMF codec - kCodecAudio) (4 bits).
191 const int iamf_codec = (audio_object_type & 0xF) + kCodecAudio;
192
193 const std::string iamf_codec_string =
194 absl::StrFormat("iamf.%03d.%03d", (audio_object_type & 0xC0) >> 6,
195 (audio_object_type & 0x30) >> 4);
196
197 switch (iamf_codec) {
198 case kCodecOpus:
199 return absl::StrFormat("%s.%s", iamf_codec_string, "Opus");
200 case kCodecAAC:
201 return absl::StrFormat("%s.%s", iamf_codec_string, "mp4a.40.2");
202 case kCodecFlac:
203 return absl::StrFormat("%s.%s", iamf_codec_string, "fLaC");
204 case kCodecPcm:
205 return absl::StrFormat("%s.%s", iamf_codec_string, "ipcm");
206 default:
207 LOG(WARNING) << "Unknown IAMF codec: " << iamf_codec;
208 return "unknown";
209 }
210 }
211 case kCodecOpus:
212 return "opus";
213 case kCodecMP3:
214 return "mp3";
215 case kCodecVorbis:
216 return "vorbis";
217 case kCodecMha1:
218 case kCodecMhm1:
219 // The signalling of the codecs parameters is according to RFC6381 [11]
220 // and ISO/IEC 23008-3 clause 21 [7].
221 // The value consists of the following two parts separated by a dot:
222 // - the sample entry 4CC code ('mha1', 'mha2', 'mhm1', 'mhm2')
223 // - ‘0x’ followed by the hex value of the profile-levelid, as defined
224 // in in ISO/IEC 23008-3 [7]
225 return absl::StrFormat("%s.0x%02x",
226 FourCCToString(CodecToFourCC(codec)).c_str(),
227 audio_object_type);
228 default:
229 NOTIMPLEMENTED() << "Codec: " << codec;
230 return "unknown";
231 }
232}
233
234} // namespace media
235} // namespace shaka
Holds audio stream information.
std::unique_ptr< StreamInfo > Clone() const override
bool IsValidConfig() const override
std::string ToString() const override
AudioStreamInfo(int track_id, int32_t time_scale, int64_t duration, Codec codec, const std::string &codec_string, const uint8_t *codec_config, size_t codec_config_size, uint8_t sample_bits, uint8_t num_channels, uint32_t sampling_frequency, uint64_t seek_preroll_ns, uint64_t codec_delay_ns, uint32_t max_bitrate, uint32_t avg_bitrate, const std::string &language, bool is_encrypted)
Construct an initialized audio stream info object.
static std::string GetCodecString(Codec codec, uint8_t audio_object_type)
Abstract class holds stream information.
Definition stream_info.h:73
virtual std::string ToString() const
All the methods that are virtual are virtual for mocking.