MythTV  master
mythhdrvideometadata.cpp
Go to the documentation of this file.
1 // MythTV
2 #include "mythhdrvideometadata.h"
3 #include "mythframe.h"
4 
5 // Std
6 #include <cmath>
7 
8 // FFmpeg
9 extern "C" {
10 #include "libavutil/mastering_display_metadata.h"
11 }
12 
13 #define UINT16(X) static_cast<uint16_t>(std::round(X))
14 
26 void MythHDRVideoMetadata::Update(const AVMasteringDisplayMetadata* Display,
27  const AVContentLightMetadata* Light)
28 {
29  bool luminance = Display && Display->has_luminance;
30  bool primaries = Display && Display->has_primaries;
31  m_maxMasteringLuminance = luminance ? UINT16(av_q2d(Display->max_luminance)) : 0;
32  m_minMasteringLuminance = luminance ? UINT16(av_q2d(Display->min_luminance) * 10000) : 0;
33  m_whitePoint[0] = primaries ? UINT16(av_q2d(Display->white_point[0]) * 50000) : 0;
34  m_whitePoint[1] = primaries ? UINT16(av_q2d(Display->white_point[1]) * 50000) : 0;
35  for (size_t i = 0; i < 3; i++)
36  {
37  m_displayPrimaries[i][0] = primaries ? UINT16(av_q2d(Display->display_primaries[i][0]) * 50000) : 0;
38  m_displayPrimaries[i][1] = primaries ? UINT16(av_q2d(Display->display_primaries[i][1]) * 50000) : 0;
39  }
40  m_maxContentLightLevel = Light ? static_cast<uint16_t>(Light->MaxCLL) : 0;
41  m_maxFrameAverageLightLevel = Light ? static_cast<uint16_t>(Light->MaxFALL) : 0;
42 }
43 
47 {
48  if (!Frame)
49  return;
50 
51  if (AvFrame)
52  {
53  const auto * display = av_frame_get_side_data(AvFrame, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
54  const auto * ddata = display ? reinterpret_cast<AVMasteringDisplayMetadata*>(display->data) : nullptr;
55  const auto * content = av_frame_get_side_data(AvFrame, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
56  const auto * cdata = content ? reinterpret_cast<AVContentLightMetadata*>(content->data) : nullptr;
57  if (ddata || cdata)
58  {
59  if (!Frame->m_hdrMetadata.get())
60  Frame->m_hdrMetadata = std::make_shared<MythHDRVideoMetadata>();
61  Frame->m_hdrMetadata->Update(ddata, cdata);
62  return;
63  }
64  }
65  Frame->m_hdrMetadata = nullptr;
66 }
MythHDRVideoMetadata::Populate
static void Populate(class MythVideoFrame *Frame, struct AVFrame *AvFrame)
Create, update or destroy HDR metadata for the given MythVideoFrame.
Definition: mythhdrvideometadata.cpp:46
MythHDRMetadata::m_displayPrimaries
MythPrimariesUInt16 m_displayPrimaries
Definition: mythhdr.h:22
MythHDRMetadata::m_maxContentLightLevel
uint16_t m_maxContentLightLevel
Definition: mythhdr.h:26
MythHDRMetadata::m_maxMasteringLuminance
uint16_t m_maxMasteringLuminance
Definition: mythhdr.h:24
Frame
Definition: zmdefines.h:93
build_compdb.content
content
Definition: build_compdb.py:38
mythhdrvideometadata.h
MythHDRVideoMetadata::Update
void Update(const struct AVMasteringDisplayMetadata *Display, const struct AVContentLightMetadata *Light)
Definition: mythhdrvideometadata.cpp:26
mythframe.h
AVFrame
struct AVFrame AVFrame
Definition: BorderDetector.h:15
UINT16
#define UINT16(X)
Definition: mythhdrvideometadata.cpp:13
MythHDRMetadata::m_minMasteringLuminance
uint16_t m_minMasteringLuminance
Definition: mythhdr.h:25
MythHDRMetadata::m_maxFrameAverageLightLevel
uint16_t m_maxFrameAverageLightLevel
Definition: mythhdr.h:27
uint16_t
unsigned short uint16_t
Definition: iso6937tables.h:3
MythVideoFrame
Definition: mythframe.h:88
MythHDRMetadata::m_whitePoint
MythPrimaryUInt16 m_whitePoint
Definition: mythhdr.h:23