From 0a2a7495666f7d12dc5624a8ff2d86eee92545a1 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 18 Jul 2016 10:38:26 +0200 Subject: [PATCH] Language support for MP4 --- lib/mp4_generic.cpp | 35 ++++++++++++++++++++++----- lib/mp4_generic.h | 5 ++-- src/output/output_progressive_mp4.cpp | 1 + 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/mp4_generic.cpp b/lib/mp4_generic.cpp index 0c9c8591..8c67fe61 100644 --- a/lib/mp4_generic.cpp +++ b/lib/mp4_generic.cpp @@ -2241,20 +2241,43 @@ namespace MP4 { void MDHD::setLanguage(uint16_t newLanguage) { if (getVersion() == 0) { - setInt16(newLanguage & 0x7F, 20); + setInt16(newLanguage & 0x7FFF, 20); } else { - setInt16(newLanguage & 0x7F, 32); + setInt16(newLanguage & 0x7FFF, 32); } } - uint16_t MDHD::getLanguage() { + uint16_t MDHD::getLanguageInt() { if (getVersion() == 0) { - return getInt16(20) & 0x7F; + return getInt16(20) & 0x7FFF; } else { - return getInt16(32) & 0x7F; + return getInt16(32) & 0x7FFF; } } + void MDHD::setLanguage(const std::string & newLanguage) { + if (newLanguage.size() != 3){ + setLanguage("und"); + return; + } + uint16_t newLang = 0; + newLang += (newLanguage[0] - 0x60) & 0x1F; + newLang <<= 5; + newLang += (newLanguage[1] - 0x60) & 0x1F; + newLang <<= 5; + newLang += (newLanguage[2] - 0x60) & 0x1F; + setLanguage(newLang); + } + + std::string MDHD::getLanguage() { + uint16_t lInt = getLanguageInt(); + std::string ret; + ret += (char)(((lInt & 0x7C00) >> 10) + 0x60); + ret += (char)(((lInt & 0x3E0) >> 5) + 0x60); + ret += (char)((lInt & 0x1F) + 0x60); + return ret; + } + std::string MDHD::toPrettyString(uint32_t indent) { std::stringstream r; r << std::string(indent, ' ') << "[mdhd] Media Header Box (" << boxedSize() << ")" << std::endl; @@ -2263,7 +2286,7 @@ namespace MP4 { r << std::string(indent + 1, ' ') << "ModificationTime: " << getModificationTime() << std::endl; r << std::string(indent + 1, ' ') << "TimeScale: " << getTimeScale() << std::endl; r << std::string(indent + 1, ' ') << "Duration: " << getDuration() << std::endl; - r << std::string(indent + 1, ' ') << "Language: 0x" << std::hex << getLanguage() << std::dec << std::endl; + r << std::string(indent + 1, ' ') << "Language: " << getLanguage() << std::endl; return r.str(); } diff --git a/lib/mp4_generic.h b/lib/mp4_generic.h index 1f760f5e..82c798b7 100644 --- a/lib/mp4_generic.h +++ b/lib/mp4_generic.h @@ -463,9 +463,10 @@ namespace MP4 { uint32_t getTimeScale(); void setDuration(uint64_t newDuration); uint64_t getDuration(); - ///\todo return language properly void setLanguage(uint16_t newLanguage); - uint16_t getLanguage(); + uint16_t getLanguageInt(); + void setLanguage(const std::string & newLanguage); + std::string getLanguage(); std::string toPrettyString(uint32_t indent = 0); }; diff --git a/src/output/output_progressive_mp4.cpp b/src/output/output_progressive_mp4.cpp index b3652cc5..60af00ed 100644 --- a/src/output/output_progressive_mp4.cpp +++ b/src/output/output_progressive_mp4.cpp @@ -70,6 +70,7 @@ namespace Mist { unsigned int mdiaOffset = 0; { MP4::MDHD mdhdBox(thisTrack.lastms - thisTrack.firstms); + mdhdBox.setLanguage(thisTrack.lang); mdiaBox.setContent(mdhdBox, mdiaOffset++); }//MDHD box {