Skip to content

Commit

Permalink
Merge pull request xbmc#24584 from quietvoid/select_hdr_fmt_new
Browse files Browse the repository at this point in the history
[Android] Add HDR allowed dynamic metadata formats setting
  • Loading branch information
neo1973 committed Jan 28, 2024
2 parents dd86968 + 449abdc commit 2347e67
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 11 deletions.
24 changes: 24 additions & 0 deletions addons/resource.language.en_gb/resources/strings.po
Expand Up @@ -23765,6 +23765,30 @@ msgctxt "#39197"
msgid "If enabled, Dolby Vision profile 7 will be converted to profile 8.1, which is more commonly supported by devices. Enable if your device supports Dolby Vision, but has issues with some videos."
msgstr ""

#. Title of "Allowed HDR dynamic metadata formats" setting
#: system/settings/settings.xml
msgctxt "#39198"
msgid "Allowed HDR dynamic metadata formats"
msgstr ""

#. Help text for setting "Allowed HDR dynamic metadata formats" of label #39198
#: system/settings/settings.xml
msgctxt "#39199"
msgid "Alters the video bitstream to remove dynamic HDR metadata. Select the HDR formats your device and display supports."
msgstr ""

#. Label of Dolby Vision option for setting "Allowed HDR dynamic metadata formats" of label #39198
#: system/settings/settings.xml
msgctxt "#39200"
msgid "Dolby Vision"
msgstr ""

#. Label of HDR10+ option for setting "Allowed HDR dynamic metadata formats" of label #39198
#: system/settings/settings.xml
msgctxt "#39201"
msgid "HDR10+"
msgstr ""

# 40000 to 40800 are reserved for Video Versions feature

#. Generic video versions label (plural)
Expand Down
16 changes: 16 additions & 0 deletions system/settings/settings.xml
Expand Up @@ -190,6 +190,22 @@
<default>false</default>
<control type="toggle" />
</setting>
<setting id="videoplayer.allowedhdrformats" type="list[integer]" label="39198" help="39199">
<requirement>HAS_MEDIACODEC</requirement>
<level>2</level>
<default>0,1</default> <!-- Allow all HDR formats -->
<constraints>
<options>
<option label="39200">0</option> <!-- Dolby Vision -->
<option label="39201">1</option> <!-- HDR10+ -->
</options>
<delimiter>,</delimiter>
</constraints>
<control type="list" format="string">
<multiselect>true</multiselect>
<hidevalue>false</hidevalue>
</control>
</setting>
</group>
<group id="4" label="14232">
<setting id="videoplayer.stereoscopicplaybackmode" type="integer" label="36520" help="36537">
Expand Down
Expand Up @@ -23,8 +23,10 @@
#include "cores/VideoPlayer/VideoRenderers/RenderManager.h"
#include "media/decoderfilter/DecoderFilterManager.h"
#include "messaging/ApplicationMessenger.h"
#include "settings/SettingUtils.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "settings/lib/Setting.h"
#include "utils/BitstreamConverter.h"
#include "utils/BitstreamWriter.h"
#include "utils/CPUInfo.h"
Expand Down Expand Up @@ -501,8 +503,22 @@ bool CDVDVideoCodecAndroidMediaCodec::Open(CDVDStreamInfo &hints, CDVDCodecOptio
m_formatname = "amc-hevc";

const auto settings = CServiceBroker::GetSettingsComponent()->GetSettings();
const bool convertDovi =
(settings) ? settings->GetBool(CSettings::SETTING_VIDEOPLAYER_CONVERTDOVI) : false;
bool convertDovi{false};
bool removeDovi{false};
bool removeHdr10Plus{false};

if (settings)
{
convertDovi = settings->GetBool(CSettings::SETTING_VIDEOPLAYER_CONVERTDOVI);

const std::shared_ptr<CSettingList> allowedHdrFormatsSetting(
std::dynamic_pointer_cast<CSettingList>(
settings->GetSetting(CSettings::SETTING_VIDEOPLAYER_ALLOWEDHDRFORMATS)));
removeDovi = !CSettingUtils::FindIntInList(
allowedHdrFormatsSetting, CSettings::VIDEOPLAYER_ALLOWED_HDR_TYPE_DOLBY_VISION);
removeHdr10Plus = !CSettingUtils::FindIntInList(
allowedHdrFormatsSetting, CSettings::VIDEOPLAYER_ALLOWED_HDR_TYPE_HDR10PLUS);
}

bool isDvhe = (m_hints.codec_tag == MKTAG('d', 'v', 'h', 'e'));
bool isDvh1 = (m_hints.codec_tag == MKTAG('d', 'v', 'h', '1'));
Expand All @@ -517,7 +533,7 @@ bool CDVDVideoCodecAndroidMediaCodec::Open(CDVDStreamInfo &hints, CDVDCodecOptio
isDvhe = true;
}

if (isDvhe || isDvh1)
if (!removeDovi && (isDvhe || isDvh1))
{
bool displaySupportsDovi{false};
bool mediaCodecSupportsDovi{false};
Expand Down Expand Up @@ -589,15 +605,21 @@ bool CDVDVideoCodecAndroidMediaCodec::Open(CDVDStreamInfo &hints, CDVDCodecOptio
m_bitstream.reset();
}

// Only set for profile 7, container hint allows to skip parsing unnecessarily
if (m_bitstream && m_hints.dovi.dv_profile == 7)
if (m_bitstream)
{
CLog::Log(LOGDEBUG,
"CDVDVideoCodecAndroidMediaCodec::Open Dolby Vision compatibility mode "
"enabled: {}",
convertDovi);
m_bitstream->SetRemoveDovi(removeDovi);
m_bitstream->SetRemoveHdr10Plus(removeHdr10Plus);

m_bitstream->SetConvertDovi(convertDovi);
// Only set for profile 7, container hint allows to skip parsing unnecessarily
if (m_hints.dovi.dv_profile == 7)
{
CLog::Log(LOGDEBUG,
"CDVDVideoCodecAndroidMediaCodec::Open Dolby Vision compatibility mode "
"enabled: {}",
convertDovi);

m_bitstream->SetConvertDovi(convertDovi);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions xbmc/settings/Settings.h
Expand Up @@ -135,6 +135,7 @@ class CSettings : public CSettingsBase, public CSettingCreator, public CSettingC
static constexpr auto SETTING_VIDEOPLAYER_LIMITGUIUPDATE = "videoplayer.limitguiupdate";
static constexpr auto SETTING_VIDEOPLAYER_SUPPORTMVC = "videoplayer.supportmvc";
static constexpr auto SETTING_VIDEOPLAYER_CONVERTDOVI = "videoplayer.convertdovi";
static constexpr auto SETTING_VIDEOPLAYER_ALLOWEDHDRFORMATS = "videoplayer.allowedhdrformats";
static constexpr auto SETTING_MYVIDEOS_SELECTACTION = "myvideos.selectaction";
static constexpr auto SETTING_MYVIDEOS_SELECTDEFAULTVERSION = "myvideos.selectdefaultversion";
static constexpr auto SETTING_MYVIDEOS_PLAYACTION = "myvideos.playaction";
Expand Down Expand Up @@ -498,6 +499,10 @@ class CSettings : public CSettingsBase, public CSettingCreator, public CSettingC
static constexpr int SETTING_AUTOPLAYNEXT_MOVIES = 3;
static constexpr int SETTING_AUTOPLAYNEXT_UNCATEGORIZED = 4;

// values for SETTING_VIDEOPLAYER_ALLOWEDHDRFORMATS
static const int VIDEOPLAYER_ALLOWED_HDR_TYPE_DOLBY_VISION = 0;
static const int VIDEOPLAYER_ALLOWED_HDR_TYPE_HDR10PLUS = 1;

/*!
\brief Creates a new settings wrapper around a new settings manager.
Expand Down
35 changes: 34 additions & 1 deletion xbmc/utils/BitstreamConverter.cpp
Expand Up @@ -17,6 +17,7 @@
#include "BitstreamConverter.h"
#include "BitstreamReader.h"
#include "BitstreamWriter.h"
#include "HevcSei.h"

#include <algorithm>

Expand Down Expand Up @@ -356,6 +357,8 @@ CBitstreamConverter::CBitstreamConverter()
m_sps_pps_context.sps_pps_data = NULL;
m_start_decode = true;
m_convert_dovi = false;
m_removeDovi = false;
m_removeHdr10Plus = false;
}

CBitstreamConverter::~CBitstreamConverter()
Expand Down Expand Up @@ -913,6 +916,8 @@ bool CBitstreamConverter::BitstreamConvert(uint8_t* pData, int iSize, uint8_t **
const DoviData* rpu_data = NULL;
#endif

std::vector<uint8_t> finalPrefixSeiNalu;

switch (m_codec)
{
case AV_CODEC_ID_H264:
Expand Down Expand Up @@ -970,13 +975,38 @@ bool CBitstreamConverter::BitstreamConvert(uint8_t* pData, int iSize, uint8_t **
const uint8_t* buf_to_write = buf;
int32_t final_nal_size = nal_size;

bool containsHdr10Plus{false};

if (!m_sps_pps_context.first_idr && IsSlice(unit_type))
{
m_sps_pps_context.first_idr = 1;
m_sps_pps_context.idr_sps_pps_seen = 0;
}

if (m_convert_dovi)
if (m_removeDovi && (unit_type == HEVC_NAL_UNSPEC62 || unit_type == HEVC_NAL_UNSPEC63))
write_buf = false;

// Try removing HDR10+ only if the NAL is big enough, optimization
if (m_removeHdr10Plus && unit_type == HEVC_NAL_SEI_PREFIX && nal_size >= 7)
{
std::tie(containsHdr10Plus, finalPrefixSeiNalu) =
CHevcSei::RemoveHdr10PlusFromSeiNalu(buf, nal_size);

if (containsHdr10Plus)
{
if (!finalPrefixSeiNalu.empty())
{
buf_to_write = finalPrefixSeiNalu.data();
final_nal_size = finalPrefixSeiNalu.size();
}
else
{
write_buf = false;
}
}
}

if (write_buf && m_convert_dovi)
{
if (unit_type == HEVC_NAL_UNSPEC62)
{
Expand Down Expand Up @@ -1008,6 +1038,9 @@ bool CBitstreamConverter::BitstreamConvert(uint8_t* pData, int iSize, uint8_t **
rpu_data = NULL;
}
#endif

if (containsHdr10Plus && !finalPrefixSeiNalu.empty())
finalPrefixSeiNalu.clear();
}

buf += nal_size;
Expand Down
4 changes: 4 additions & 0 deletions xbmc/utils/BitstreamConverter.h
Expand Up @@ -100,6 +100,8 @@ class CBitstreamConverter
void ResetStartDecode(void);
bool CanStartDecode() const;
void SetConvertDovi(bool value) { m_convert_dovi = value; }
void SetRemoveDovi(bool value) { m_removeDovi = value; }
void SetRemoveHdr10Plus(bool value) { m_removeHdr10Plus = value; }

static bool mpeg2_sequence_header(const uint8_t *data, const uint32_t size, mpeg2_sequence *sequence);

Expand Down Expand Up @@ -145,4 +147,6 @@ class CBitstreamConverter
AVCodecID m_codec;
bool m_start_decode;
bool m_convert_dovi;
bool m_removeDovi;
bool m_removeHdr10Plus;
};
4 changes: 4 additions & 0 deletions xbmc/utils/BitstreamReader.cpp
Expand Up @@ -21,6 +21,8 @@ uint32_t CBitstreamReader::ReadBits(int nbits)
buffer += offbits / 8;
offbits %= 8;

m_posBits += nbits;

return ret;
}

Expand All @@ -30,6 +32,8 @@ void CBitstreamReader::SkipBits(int nbits)
buffer += offbits / 8;
offbits %= 8;

m_posBits += nbits;

if (buffer > (start + length))
oflow = 1;
}
Expand Down
3 changes: 3 additions & 0 deletions xbmc/utils/BitstreamReader.h
Expand Up @@ -17,10 +17,13 @@ class CBitstreamReader
uint32_t ReadBits(int nbits);
void SkipBits(int nbits);
uint32_t GetBits(int nbits);
unsigned int Position() { return m_posBits; }
unsigned int AvailableBits() { return length * 8 - m_posBits; }

private:
const uint8_t *buffer, *start;
int offbits = 0, length, oflow = 0;
int m_posBits{0};
};

const uint8_t* find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state);
Expand Down
2 changes: 2 additions & 0 deletions xbmc/utils/CMakeLists.txt
Expand Up @@ -29,6 +29,7 @@ set(SOURCES ActorProtocol.cpp
FontUtils.cpp
GpuInfo.cpp
GroupUtils.cpp
HevcSei.cpp
HTMLUtil.cpp
HttpHeader.cpp
HttpParser.cpp
Expand Down Expand Up @@ -115,6 +116,7 @@ set(HEADERS ActorProtocol.h
GpuInfo.h
GroupUtils.h
HDRCapabilities.h
HevcSei.h
HTMLUtil.h
HttpHeader.h
HttpParser.h
Expand Down

0 comments on commit 2347e67

Please sign in to comment.