Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit ee6a610

Browse files
committed
Bug 1891082 - Add a function for making AAC specific config r=media-playback-reviewers,padenot
Depends on D219146 Differential Revision: https://phabricator.services.mozilla.com/D219147
1 parent 9a9557d commit ee6a610

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

dom/media/platforms/agnostic/bytestreams/Adts.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
#include "Adts.h"
6+
#include "BitWriter.h"
67
#include "MediaData.h"
78
#include "PlatformDecoderModule.h"
89
#include "mozilla/Array.h"
@@ -286,6 +287,52 @@ void InitAudioSpecificConfig(const ADTS::Frame& frame,
286287
aBuffer->AppendElements(asc, 2);
287288
}
288289

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+
289336
}; // namespace ADTS
290337
}; // namespace mozilla
291338

dom/media/platforms/agnostic/bytestreams/Adts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Result<uint8_t, bool> GetFrequencyIndex(uint32_t aSamplesPerSecond);
123123
bool ConvertSample(uint16_t aChannelCount, uint8_t aFrequencyIndex,
124124
uint8_t aProfile, mozilla::MediaRawData* aSample);
125125
bool RevertSample(MediaRawData* aSample);
126+
Result<already_AddRefed<MediaByteBuffer>, nsresult> MakeSpecificConfig(
127+
uint8_t aObjectType, uint32_t aFrequency, uint32_t aChannelCount);
126128
} // namespace ADTS
127129
} // namespace mozilla
128130

0 commit comments

Comments
 (0)