Skip to content

Commit

Permalink
Support Dolby audio Enhanced AC3 in ISO BMFF (Part 2)
Browse files Browse the repository at this point in the history
- Box definitions for box type DEC3.
- Parser/muxer changes to support Enhanced-AC3 audio codecs.
- MPD signaling will come in Part 3.

Issue #64

Change-Id: Ifcd5efa1f61b470ec225127925631e4329853259
  • Loading branch information
Bei Li committed Jan 15, 2016
1 parent d1d75f4 commit 8563db4
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packager/media/base/audio_stream_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ std::string AudioCodecToString(AudioCodec audio_codec) {
return "DTS-";
case kCodecDTSP:
return "DTS+";
case kCodecEAC3:
return "EAC3";
case kCodecOpus:
return "Opus";
case kCodecVorbis:
Expand Down Expand Up @@ -115,6 +117,8 @@ std::string AudioStreamInfo::GetCodecString(AudioCodec codec,
return "dts-";
case kCodecAC3:
return "ac-3";
case kCodecEAC3:
return "ec-3";
default:
NOTIMPLEMENTED() << "Codec: " << codec;
return "unknown";
Expand Down
1 change: 1 addition & 0 deletions packager/media/base/audio_stream_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum AudioCodec {
kCodecDTSL,
kCodecDTSM,
kCodecDTSP,
kCodecEAC3,
kCodecOpus,
kCodecVorbis,

Expand Down
51 changes: 51 additions & 0 deletions packager/media/formats/mp4/box_definitions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "packager/base/logging.h"
#include "packager/media/base/bit_reader.h"
#include "packager/media/base/macros.h"
#include "packager/media/formats/mp4/box_buffer.h"
#include "packager/media/formats/mp4/rcheck.h"

Expand Down Expand Up @@ -1435,6 +1436,54 @@ uint32_t AC3Specific::ComputeSizeInternal() {
return HeaderSize() + data.size();
}

EC3Specific::EC3Specific() : number_independent_substreams(0) {}
EC3Specific::~EC3Specific() {}

FourCC EC3Specific::BoxType() const { return FOURCC_DEC3; }

bool EC3Specific::ReadWriteInternal(BoxBuffer* buffer) {
RCHECK(ReadWriteHeaderInternal(buffer));
uint32_t size = buffer->Reading() ? buffer->BytesLeft() : data.size();
RCHECK(buffer->ReadWriteVector(&data, size));

// Skip data rate, read number of independent substreams and parse the
// independent substreams.
BitReader bit_reader(&data[0], size);
RCHECK(bit_reader.SkipBits(13) &&
bit_reader.ReadBits(3, &number_independent_substreams));

// The value of this field is one less than the number of independent
// substreams present.
++number_independent_substreams;
IndependentSubstream substream;
for (size_t i = 0; i < number_independent_substreams; ++i) {
RCHECK(bit_reader.ReadBits(2, &substream.sample_rate_code));
RCHECK(bit_reader.ReadBits(5, &substream.bit_stream_identification));
RCHECK(bit_reader.SkipBits(1));
RCHECK(bit_reader.ReadBits(1, &substream.audio_service));
RCHECK(bit_reader.ReadBits(3, &substream.bit_stream_mode));
RCHECK(bit_reader.ReadBits(3, &substream.audio_coding_mode));
RCHECK(bit_reader.ReadBits(1, &substream.lfe_channel_on));
RCHECK(bit_reader.SkipBits(3));
RCHECK(bit_reader.ReadBits(4, &substream.number_dependent_substreams));
if (substream.number_dependent_substreams > 0) {
RCHECK(bit_reader.ReadBits(9, &substream.channel_location));
} else {
RCHECK(bit_reader.SkipBits(1));
}
independent_substreams.push_back(substream);
}

return true;
}

uint32_t EC3Specific::ComputeSizeInternal() {
// This box is optional. Skip it if not initialized.
if (data.empty())
return 0;
return HeaderSize() + data.size();
}

AudioSampleEntry::AudioSampleEntry()
: format(FOURCC_NULL),
data_reference_index(1),
Expand Down Expand Up @@ -1489,13 +1538,15 @@ bool AudioSampleEntry::ReadWriteInternal(BoxBuffer* buffer) {
RCHECK(buffer->TryReadWriteChild(&esds));
RCHECK(buffer->TryReadWriteChild(&ddts));
RCHECK(buffer->TryReadWriteChild(&dac3));
RCHECK(buffer->TryReadWriteChild(&dec3));
return true;
}

uint32_t AudioSampleEntry::ComputeSizeInternal() {
return HeaderSize() + sizeof(data_reference_index) + sizeof(channelcount) +
sizeof(samplesize) + sizeof(samplerate) + sinf.ComputeSize() +
esds.ComputeSize() + ddts.ComputeSize() + dac3.ComputeSize() +
dec3.ComputeSize() +
6 + 8 + // 6 + 8 bytes reserved.
4; // 4 bytes predefined.
}
Expand Down
30 changes: 30 additions & 0 deletions packager/media/formats/mp4/box_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,34 @@ struct AC3Specific : Box {
std::vector<uint8_t> data;
};

// Independent substream in EC3Specific box.
struct IndependentSubstream {
uint8_t sample_rate_code; // fscod: 2 bits
uint8_t bit_stream_identification; // bsid: 5 bits
// reserved_1: 1 bit
uint8_t audio_service; // asvc: 1 bit
uint8_t bit_stream_mode; // bsmod: 3 bits
uint8_t audio_coding_mode; // acmod: 3 bits
uint8_t lfe_channel_on; // lfeon: 1 bit
// reserved_2: 3 bit
uint8_t number_dependent_substreams; // num_dep_sub: 4 bits.
// If num_dep_sub > 0, chan_loc is present and the size is 9 bits.
// Otherwise, reserved_3 is present and the size is 1 bit.
uint16_t channel_location; // chan_loc: 9 bits.
// reserved_3: 1 bit
};

struct EC3Specific : Box {
DECLARE_BOX_METHODS(EC3Specific);

// Before we know the number of independent substreams, data in EC3Specific
// box is store for parsing later.
std::vector<uint8_t> data;

size_t number_independent_substreams; // num_id_sub: 3 bits.
std::vector<IndependentSubstream> independent_substreams;
};

struct AudioSampleEntry : Box {
DECLARE_BOX_METHODS(AudioSampleEntry);
// Returns actual format of this sample entry.
Expand All @@ -329,9 +357,11 @@ struct AudioSampleEntry : Box {
uint32_t samplerate;

ProtectionSchemeInfo sinf;

ElementaryStreamDescriptor esds;
DTSSpecific ddts;
AC3Specific dac3;
EC3Specific dec3;
};

struct WebVTTConfigurationBox : Box {
Expand Down
7 changes: 6 additions & 1 deletion packager/media/formats/mp4/box_definitions_comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,19 @@ inline bool operator==(const AC3Specific& lhs,
return lhs.data == rhs.data;
}

inline bool operator==(const EC3Specific& lhs,
const EC3Specific& rhs) {
return lhs.data == rhs.data;
}

inline bool operator==(const AudioSampleEntry& lhs,
const AudioSampleEntry& rhs) {
return lhs.format == rhs.format &&
lhs.data_reference_index == rhs.data_reference_index &&
lhs.channelcount == rhs.channelcount &&
lhs.samplesize == rhs.samplesize && lhs.samplerate == rhs.samplerate &&
lhs.sinf == rhs.sinf && lhs.esds == rhs.esds && lhs.ddts == rhs.ddts &&
lhs.dac3 == rhs.dac3;
lhs.dac3 == rhs.dac3 && lhs.dec3 == rhs.dec3;
}

inline bool operator==(const WebVTTConfigurationBox& lhs,
Expand Down
28 changes: 27 additions & 1 deletion packager/media/formats/mp4/box_definitions_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@ class BoxDefinitionsTestGeneral : public testing::Test {
dac3->data.assign(kAc3Data, kAc3Data + arraysize(kAc3Data));
}

void Fill(EC3Specific* dec3) {
const uint8_t kEc3Data[] = {0x08, 0x00, 0x20, 0x0f, 0x00};
dec3->data.assign(kEc3Data, kEc3Data + arraysize(kEc3Data));
}

void Modify(EC3Specific* dec3) {
const uint8_t kEc3Data[] = {0x07, 0x00, 0x60, 0x04, 0x00};
dec3->data.assign(kEc3Data, kEc3Data + arraysize(kEc3Data));
}

void Fill(AudioSampleEntry* enca) {
enca->format = FOURCC_ENCA;
enca->data_reference_index = 2;
Expand Down Expand Up @@ -895,6 +905,7 @@ class BoxDefinitionsTestGeneral : public testing::Test {
bool IsOptional(const PixelAspectRatio* box) { return true; }
bool IsOptional(const ElementaryStreamDescriptor* box) { return true; }
bool IsOptional(const AC3Specific* box) { return true; }
bool IsOptional(const EC3Specific* box) { return true; }
// Recommended, but optional.
bool IsOptional(const WebVTTSourceLabelBox* box) { return true; }
bool IsOptional(const CompositionTimeToSample* box) { return true; }
Expand Down Expand Up @@ -938,6 +949,7 @@ typedef testing::Types<FileType,
ElementaryStreamDescriptor,
DTSSpecific,
AC3Specific,
EC3Specific,
AudioSampleEntry,
WebVTTConfigurationBox,
WebVTTSourceLabelBox,
Expand Down Expand Up @@ -1078,7 +1090,6 @@ TEST_F(BoxDefinitionsTest, DTSSampleEntry) {
entry.samplerate = 44100;
Fill(&entry.ddts);
entry.Write(this->buffer_.get());

AudioSampleEntry entry_readback;
ASSERT_TRUE(ReadBack(&entry_readback));
ASSERT_EQ(entry, entry_readback);
Expand All @@ -1099,6 +1110,21 @@ TEST_F(BoxDefinitionsTest, AC3SampleEntry) {
ASSERT_EQ(entry, entry_readback);
}

TEST_F(BoxDefinitionsTest, EC3SampleEntry) {
AudioSampleEntry entry;
entry.format = FOURCC_EAC3;
entry.data_reference_index = 2;
entry.channelcount = 5;
entry.samplesize = 16;
entry.samplerate = 44100;
Fill(&entry.dec3);
entry.Write(this->buffer_.get());

AudioSampleEntry entry_readback;
ASSERT_TRUE(ReadBack(&entry_readback));
ASSERT_EQ(entry, entry_readback);
}

TEST_F(BoxDefinitionsTest, ProtectionSystemSpecificHeader) {
ProtectionSystemSpecificHeader pssh;
Fill(&pssh);
Expand Down
2 changes: 2 additions & 0 deletions packager/media/formats/mp4/fourccs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum FourCC {
FOURCC_DAC3 = 0x64616333,
FOURCC_DASH = 0x64617368,
FOURCC_DDTS = 0x64647473,
FOURCC_DEC3 = 0x64656333,
FOURCC_DINF = 0x64696e66,
FOURCC_DREF = 0x64726566,
FOURCC_DTSC = 0x64747363,
Expand All @@ -34,6 +35,7 @@ enum FourCC {
FOURCC_DTSL = 0x6474736c,
FOURCC_DTSM = 0x6474732d,
FOURCC_DTSP = 0x6474732b,
FOURCC_EAC3 = 0x65632d33, // This fourcc is "ec-3".
FOURCC_EDTS = 0x65647473,
FOURCC_ELST = 0x656c7374,
FOURCC_ENCA = 0x656e6361,
Expand Down
7 changes: 7 additions & 0 deletions packager/media/formats/mp4/mp4_media_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ AudioCodec FourCCToAudioCodec(FourCC fourcc) {
return kCodecDTSM;
case FOURCC_AC3:
return kCodecAC3;
case FOURCC_EAC3:
return kCodecEAC3;
default:
return kUnknownAudioCodec;
}
Expand Down Expand Up @@ -415,6 +417,11 @@ bool MP4MediaParser::ParseMoov(BoxReader* reader) {
num_channels = entry.channelcount;
sampling_frequency = entry.samplerate;
break;
case FOURCC_EAC3:
extra_data = entry.dec3.data;
num_channels = entry.channelcount;
sampling_frequency = entry.samplerate;
break;
default:
LOG(ERROR) << "Unsupported audio format 0x" << std::hex
<< actual_format << " in stsd box.";
Expand Down
5 changes: 5 additions & 0 deletions packager/media/formats/mp4/mp4_muxer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ FourCC AudioCodecToFourCC(AudioCodec codec) {
return FOURCC_DTSE;
case kCodecDTSM:
return FOURCC_DTSM;
case kCodecEAC3:
return FOURCC_EAC3;
default:
return FOURCC_NULL;
}
Expand Down Expand Up @@ -266,6 +268,9 @@ void MP4Muxer::GenerateAudioTrak(const AudioStreamInfo* audio_info,
case kCodecAC3:
audio.dac3.data = audio_info->extra_data();
break;
case kCodecEAC3:
audio.dec3.data = audio_info->extra_data();
break;
default:
NOTIMPLEMENTED();
break;
Expand Down

0 comments on commit 8563db4

Please sign in to comment.