Skip to content

Commit

Permalink
write iTunes compatible "Encoding Params" tag
Browse files Browse the repository at this point in the history
  • Loading branch information
nu774 committed Jun 28, 2015
1 parent 410f3b5 commit 5ba4c74
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
34 changes: 34 additions & 0 deletions main.cpp
Expand Up @@ -1339,6 +1339,35 @@ bool insert_pce(uint32_t channel_layout, std::vector<uint8_t> *asc)
return true;
}

static
void set_encoding_params(AudioConverterX &converter, ITagStore *store)
{
UInt32 mode = converter.getBitRateControlMode();
UInt32 bitrate = converter.getEncodeBitRate();
AudioStreamBasicDescription asbd;
converter.getOutputStreamDescription(&asbd);
UInt32 codec = asbd.mFormatID;
AudioComponentDescription cd = { 'aenc', codec, 'appl', 0, 0 };
auto ac = AudioComponentFindNext(nullptr, &cd);
UInt32 vers = 0;
AudioComponentGetVersion(ac, &vers);

char buf[32] = "vers\0\0\0\1acbf\0\0\0\0brat\0\0\0\0cdcv\0\0\0";
buf[12] = mode >> 24;
buf[13] = mode >> 16;
buf[14] = mode >> 8;
buf[15] = mode;
buf[20] = bitrate >> 24;
buf[21] = bitrate >> 16;
buf[22] = bitrate >> 8;
buf[23] = bitrate;
buf[28] = vers >> 24;
buf[29] = vers >> 16;
buf[30] = vers >> 8;
buf[31] = vers;
store->setTag("Encoding Params", std::string(buf, buf + 32));
}

static
void encode_file(const std::shared_ptr<ISeekableSource> &src,
const std::wstring &ofilename, const Options &opts,
Expand Down Expand Up @@ -1388,6 +1417,11 @@ void encode_file(const std::shared_ptr<ISeekableSource> &src,
sink = open_sink(ofilename, opts, oasbd,
channel_layout, cookie);
encoder->setSink(sink);
if (opts.isAAC()) {
MP4Sink *mp4sink = dynamic_cast<MP4Sink*>(sink.get());
if (mp4sink)
set_encoding_params(converter, mp4sink);
}
set_tags(src.get(), sink.get(), opts, encoder_config);
CAFSink *cafsink = dynamic_cast<CAFSink*>(sink.get());
if (cafsink)
Expand Down
5 changes: 4 additions & 1 deletion sink.cpp
Expand Up @@ -331,8 +331,11 @@ void MP4SinkBase::writeShortTag(uint32_t fcc, const std::string &value)
void MP4SinkBase::writeLongTag(const std::string &key, const std::string &value)
{
const uint8_t *v = reinterpret_cast<const uint8_t *>(value.c_str());
auto type = mp4v2::impl::itmf::BT_UTF8;
if (key == "Encoding Params")
type = mp4v2::impl::itmf::BT_IMPLICIT;
m_mp4file.SetMetadataFreeForm(key.c_str(), "com.apple.iTunes",
v, value.size());
v, value.size(), type);
}

void MP4SinkBase::writeTrackTag(const char *fcc, const std::string &value)
Expand Down

0 comments on commit 5ba4c74

Please sign in to comment.