|
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
4 | 4 |
|
5 | 5 | #include "Adts.h"
|
| 6 | +#include "BitWriter.h" |
6 | 7 | #include "MediaData.h"
|
7 | 8 | #include "PlatformDecoderModule.h"
|
8 | 9 | #include "mozilla/Array.h"
|
@@ -286,6 +287,52 @@ void InitAudioSpecificConfig(const ADTS::Frame& frame,
|
286 | 287 | aBuffer->AppendElements(asc, 2);
|
287 | 288 | }
|
288 | 289 |
|
| 290 | +// https://wiki.multimedia.cx/index.php/MPEG-4_Audio#Audio_Specific_Config |
| 291 | +Result<already_AddRefed<MediaByteBuffer>, nsresult> MakeSpecificConfig( |
| 292 | + uint8_t aObjectType, uint32_t aFrequency, uint32_t aChannelCount) { |
| 293 | + if (aObjectType > 45 /* USAC */ || aObjectType == 0x1F /* Escape value */) { |
| 294 | + return Err(NS_ERROR_INVALID_ARG); |
| 295 | + } |
| 296 | + |
| 297 | + if (aFrequency > 0x00FFFFFF /* max value of 24 bits */) { |
| 298 | + return Err(NS_ERROR_INVALID_ARG); |
| 299 | + } |
| 300 | + |
| 301 | + if (aChannelCount > 8 || aChannelCount == 7) { |
| 302 | + return Err(NS_ERROR_INVALID_ARG); |
| 303 | + } |
| 304 | + |
| 305 | + uint8_t index = GetFrequencyIndex(aFrequency) |
| 306 | + .unwrapOr(0x0F /* frequency is written explictly */); |
| 307 | + MOZ_ASSERT(index <= 0x0F /* index needs only 4 bits */); |
| 308 | + |
| 309 | + uint8_t channelConfig = |
| 310 | + aChannelCount == 8 ? aChannelCount - 1 : aChannelCount; |
| 311 | + |
| 312 | + RefPtr<MediaByteBuffer> buffer = new MediaByteBuffer(); |
| 313 | + BitWriter bw(buffer); |
| 314 | + |
| 315 | + if (aObjectType < 0x1F /* Escape value */) { |
| 316 | + bw.WriteBits(aObjectType, 5); |
| 317 | + } else { // If object type needs more than 5 bits |
| 318 | + MOZ_ASSERT(aObjectType >= 32); |
| 319 | + bw.WriteBits(0x1F, 5); |
| 320 | + // Since aObjectType < 0x3F + 32, it's safe to put it into 6 bits. |
| 321 | + bw.WriteBits(aObjectType - 32, 6); |
| 322 | + } |
| 323 | + |
| 324 | + bw.WriteBits(index, 4); |
| 325 | + if (index == 0x0F /* frequency is written explictly */) { |
| 326 | + bw.WriteBits(aFrequency, 24); |
| 327 | + } |
| 328 | + |
| 329 | + bw.WriteBits(channelConfig, 4); |
| 330 | + |
| 331 | + // Skip extension configuration for now. |
| 332 | + |
| 333 | + return buffer.forget(); |
| 334 | +} |
| 335 | + |
289 | 336 | }; // namespace ADTS
|
290 | 337 | }; // namespace mozilla
|
291 | 338 |
|
|
0 commit comments