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