MythTV  master
mythframe.h
Go to the documentation of this file.
1 #ifndef MYTHFRAME_H
2 #define MYTHFRAME_H
3 
4 // Qt
5 #include <QRect>
6 
7 // MythTV
9 #include "libmythtv/mythtvexp.h"
10 
11 // Std
12 #include <array>
13 #include <vector>
14 #include <memory>
15 
16 static constexpr uint8_t MYTH_WIDTH_ALIGNMENT { 64 };
17 static constexpr uint8_t MYTH_HEIGHT_ALIGNMENT { 16 };
18 
19 enum VideoFrameType : std::int8_t
20 {
21  FMT_NONE = -1,
22  // YV12 and variants
23  FMT_YV12 = 0,
29  // RGB variants
30  FMT_RGB24 = 6,
31  FMT_BGRA = 7,
32  FMT_RGB32 = 8,
34  FMT_RGBA32 = 10,
35  // YUV422P and variants
42  // YUV444P and variants
49  // Packed YUV
50  FMT_YUY2 = 23,
51  // NV12 and variants
52  FMT_NV12 = 24,
53  FMT_P010 = 25,
54  FMT_P016 = 26,
55  // hardware formats
56  FMT_VDPAU = 27,
57  FMT_VAAPI = 28,
58  FMT_DXVA2 = 29,
59  FMT_MMAL = 30,
61  FMT_VTB = 32,
62  FMT_NVDEC = 33,
64 };
65 
66 enum MythDeintType : std::uint8_t
67 {
68  DEINT_NONE = 0x0000,
69  DEINT_BASIC = 0x0001,
70  DEINT_MEDIUM = 0x0002,
71  DEINT_HIGH = 0x0004,
72  DEINT_CPU = 0x0010,
73  DEINT_SHADER = 0x0020,
74  DEINT_DRIVER = 0x0040,
75  DEINT_ALL = 0x0077
76 };
77 
78 inline MythDeintType operator| (MythDeintType a, MythDeintType b) { return static_cast<MythDeintType>(static_cast<int>(a) | static_cast<int>(b)); }
79 inline MythDeintType operator& (MythDeintType a, MythDeintType b) { return static_cast<MythDeintType>(static_cast<int>(a) & static_cast<int>(b)); }
80 inline MythDeintType operator~ (MythDeintType a) { return static_cast<MythDeintType>(~(static_cast<int>(a))); }
81 
82 using VideoFrameTypes = std::vector<VideoFrameType>;
83 using FramePitches = std::array<int,3>;
84 using FrameOffsets = std::array<int,3>;
85 using MythHDRVideoPtr = std::shared_ptr<class MythHDRVideoMetadata>;
86 
88 {
89  public:
90  static inline const VideoFrameTypes kDefaultRenderFormats { FMT_YV12 };
91 
92  MythVideoFrame() = default;
93  MythVideoFrame(VideoFrameType Type, int Width, int Height, const VideoFrameTypes* RenderFormats = nullptr);
94  MythVideoFrame(VideoFrameType Type, uint8_t* Buffer, size_t BufferSize,
95  int Width, int Height, const VideoFrameTypes* RenderFormats = nullptr,
96  int Alignment = MYTH_WIDTH_ALIGNMENT);
97  ~MythVideoFrame();
98 
99  void Init(VideoFrameType Type, int Width, int Height, const VideoFrameTypes* RenderFormats = nullptr);
100  void Init(VideoFrameType Type, uint8_t* Buffer, size_t BufferSize,
101  int Width, int Height, const VideoFrameTypes* RenderFormats = nullptr, int Alignment = MYTH_WIDTH_ALIGNMENT);
102  void ClearMetadata();
103  void ClearBufferToBlank();
104  bool CopyFrame(MythVideoFrame* From);
105  MythDeintType GetSingleRateOption(MythDeintType Type, MythDeintType Override = DEINT_NONE) const;
106  MythDeintType GetDoubleRateOption(MythDeintType Type, MythDeintType Override = DEINT_NONE) const;
107 
108  static void CopyPlane(uint8_t* To, int ToPitch, const uint8_t* From, int FromPitch,
109  int PlaneWidth, int PlaneHeight);
110  static QString FormatDescription(VideoFrameType Type);
111  static uint8_t* GetAlignedBuffer(size_t Size);
112  static uint8_t* CreateBuffer(VideoFrameType Type, int Width, int Height);
113  static size_t GetBufferSize(VideoFrameType Type, int Width, int Height, int Aligned = MYTH_WIDTH_ALIGNMENT);
114  static QString DeinterlacerPref(MythDeintType Deint);
115  static QString DeinterlacerName(MythDeintType Deint, bool DoubleRate, VideoFrameType Format = FMT_NONE);
116  static MythDeintType ParseDeinterlacer(const QString& Deinterlacer);
117 
119  uint8_t* m_buffer { nullptr };
120  int m_width { 0 };
121  int m_height { 0 };
122  int m_bitsPerPixel { 0 };
123  size_t m_bufferSize { 0 };
124 
125  // everthing below is considered metadata by ClearMetadata
126  float m_aspect { -1.0F };
127  double m_frameRate { -1.0 };
128  long long m_frameNumber { 0 };
129  uint64_t m_frameCounter { 0 };
130  std::chrono::milliseconds m_timecode { 0ms };
131  std::chrono::milliseconds m_displayTimecode { 0ms };
132  std::array<uint8_t*,4> m_priv { nullptr };
133  bool m_interlaced { false };
134  bool m_topFieldFirst { true };
135  bool m_interlacedReverse { false };
136  bool m_newGOP { false };
137  bool m_repeatPic { false };
138  bool m_forceKey { false };
139  bool m_dummy { false };
140  bool m_pauseFrame { false };
141  FramePitches m_pitches { 0 };
142  FrameOffsets m_offsets { 0 };
143  int m_pixFmt { 0 };
144  int m_swPixFmt { 0 };
145  bool m_directRendering { true };
146  const VideoFrameTypes* m_renderFormats { &kDefaultRenderFormats };
147  int m_colorspace { 1 };
148  int m_colorrange { 1 };
149  int m_colorprimaries { 1 };
150  int m_colortransfer { 1 };
151  int m_chromalocation { 1 };
152  bool m_colorshifted { false };
153  bool m_alreadyDeinterlaced { false };
154  int m_rotation { 0 };
155  uint m_stereo3D { 0 };
156  MythHDRVideoPtr m_hdrMetadata { nullptr };
157  MythDeintType m_deinterlaceSingle { DEINT_NONE };
158  MythDeintType m_deinterlaceDouble { DEINT_NONE };
159  MythDeintType m_deinterlaceAllowed { DEINT_NONE };
160  MythDeintType m_deinterlaceInuse { DEINT_NONE };
161  bool m_deinterlaceInuse2x { false };
162 
163  // Presentation details for 'pure' direct rendering methods (e.g. DRM)
164  // Experimental and may be removed.
165  bool m_displayed { false };
166  QRect m_srcRect;
167  QRect m_dstRect;
168 
169  static int BitsPerPixel(VideoFrameType Type)
170  {
171  switch (Type)
172  {
173  case FMT_BGRA:
174  case FMT_RGBA32:
175  case FMT_ARGB32:
176  case FMT_RGB32:
177  case FMT_YUV422P9:
178  case FMT_YUV422P10:
179  case FMT_YUV422P12:
180  case FMT_YUV422P14:
181  case FMT_YUV422P16: return 32;
182  case FMT_RGB24: return 24;
183  case FMT_YUV422P:
184  case FMT_YUY2: return 16;
185  case FMT_YV12:
186  case FMT_NV12: return 12;
187  case FMT_YUV444P:
188  case FMT_P010:
189  case FMT_P016:
190  case FMT_YUV420P9:
191  case FMT_YUV420P10:
192  case FMT_YUV420P12:
193  case FMT_YUV420P14:
194  case FMT_YUV420P16: return 24;
195  case FMT_YUV444P9:
196  case FMT_YUV444P10:
197  case FMT_YUV444P12:
198  case FMT_YUV444P14:
199  case FMT_YUV444P16: return 48;
200  case FMT_NONE:
201  case FMT_VDPAU:
202  case FMT_VAAPI:
203  case FMT_DXVA2:
204  case FMT_MMAL:
205  case FMT_MEDIACODEC:
206  case FMT_NVDEC:
207  case FMT_DRMPRIME:
208  case FMT_VTB: break;
209  }
210  return 8;
211  }
212 
214  {
215  switch (Type)
216  {
217  case FMT_YV12:
218  case FMT_YUV420P9:
219  case FMT_YUV420P10:
220  case FMT_YUV420P12:
221  case FMT_YUV420P14:
222  case FMT_YUV420P16:
223  case FMT_YUV422P:
224  case FMT_YUV422P9:
225  case FMT_YUV422P10:
226  case FMT_YUV422P12:
227  case FMT_YUV422P14:
228  case FMT_YUV422P16:
229  case FMT_YUV444P:
230  case FMT_YUV444P9:
231  case FMT_YUV444P10:
232  case FMT_YUV444P12:
233  case FMT_YUV444P14:
234  case FMT_YUV444P16: return 3;
235  case FMT_P010:
236  case FMT_P016:
237  case FMT_NV12: return 2;
238  case FMT_YUY2:
239  case FMT_BGRA:
240  case FMT_ARGB32:
241  case FMT_RGB24:
242  case FMT_RGB32:
243  case FMT_RGBA32: return 1;
244  case FMT_NONE:
245  case FMT_VDPAU:
246  case FMT_VAAPI:
247  case FMT_DXVA2:
248  case FMT_MEDIACODEC:
249  case FMT_NVDEC:
250  case FMT_MMAL:
251  case FMT_DRMPRIME:
252  case FMT_VTB: return 0;
253  }
254  return 0;
255  }
256 
257  static int GetHeightForPlane(VideoFrameType Type, int Height, uint Plane)
258  {
259  switch (Type)
260  {
261  case FMT_YV12:
262  case FMT_YUV420P9:
263  case FMT_YUV420P10:
264  case FMT_YUV420P12:
265  case FMT_YUV420P14:
266  case FMT_YUV420P16:
267  if (Plane == 0) return Height;
268  if (Plane < 3) return Height >> 1;
269  break;
270  case FMT_YUV422P:
271  case FMT_YUV422P9:
272  case FMT_YUV422P10:
273  case FMT_YUV422P12:
274  case FMT_YUV422P14:
275  case FMT_YUV422P16:
276  case FMT_YUV444P:
277  case FMT_YUV444P9:
278  case FMT_YUV444P10:
279  case FMT_YUV444P12:
280  case FMT_YUV444P14:
281  case FMT_YUV444P16:
282  if (Plane < 3) return Height;
283  break;
284  case FMT_NV12:
285  case FMT_P010:
286  case FMT_P016:
287  if (Plane == 0) return Height;
288  if (Plane < 2) return Height >> 1;
289  break;
290  case FMT_YUY2:
291  case FMT_RGB24:
292  case FMT_ARGB32:
293  case FMT_RGBA32:
294  case FMT_BGRA:
295  case FMT_RGB32:
296  if (Plane == 0) return Height;
297  break;
298  default: break; // None and hardware formats
299  }
300  return 0;
301  }
302 
303  static int GetPitchForPlane(VideoFrameType Type, int Width, uint Plane)
304  {
305  switch (Type)
306  {
307  case FMT_YV12:
308  case FMT_YUV422P:
309  if (Plane == 0) return Width;
310  if (Plane < 3) return (Width + 1) >> 1;
311  break;
312  case FMT_YUV420P9:
313  case FMT_YUV420P10:
314  case FMT_YUV420P12:
315  case FMT_YUV420P14:
316  case FMT_YUV420P16:
317  case FMT_YUV422P9:
318  case FMT_YUV422P10:
319  case FMT_YUV422P12:
320  case FMT_YUV422P14:
321  case FMT_YUV422P16:
322  if (Plane == 0) return Width << 1;
323  if (Plane < 3) return Width;
324  break;
325  case FMT_YUV444P:
326  case FMT_YUV444P9:
327  case FMT_YUV444P10:
328  case FMT_YUV444P12:
329  case FMT_YUV444P14:
330  case FMT_YUV444P16:
331  if (Plane < 3) return Width;
332  break;
333  case FMT_NV12:
334  if (Plane < 2) return Width;
335  break;
336  case FMT_P010:
337  case FMT_P016:
338  if (Plane < 2) return Width << 1;
339  break;
340  case FMT_YUY2:
341  case FMT_RGB24:
342  case FMT_ARGB32:
343  case FMT_RGBA32:
344  case FMT_BGRA:
345  case FMT_RGB32:
346  if (Plane == 0) return (MythVideoFrame::BitsPerPixel(Type) * Width) >> 3;
347  break;
348  default: break; // None and hardware formats
349  }
350  return 0;
351  }
352 
353  static int GetWidthForPlane(VideoFrameType Type, int Width, uint Plane)
354  {
355  switch (Type)
356  {
357  case FMT_YV12:
358  case FMT_YUV420P9:
359  case FMT_YUV420P10:
360  case FMT_YUV420P12:
361  case FMT_YUV420P14:
362  case FMT_YUV420P16:
363  case FMT_YUV422P:
364  case FMT_YUV422P9:
365  case FMT_YUV422P10:
366  case FMT_YUV422P12:
367  case FMT_YUV422P14:
368  case FMT_YUV422P16:
369  if (Plane == 0) return Width;
370  if (Plane < 3) return (Width + 1) >> 1;
371  break;
372  case FMT_YUV444P:
373  case FMT_YUV444P9:
374  case FMT_YUV444P10:
375  case FMT_YUV444P12:
376  case FMT_YUV444P14:
377  case FMT_YUV444P16:
378  if (Plane < 3) return Width;
379  break;
380  case FMT_NV12:
381  case FMT_P010:
382  case FMT_P016:
383  if (Plane < 2) return Width;
384  break;
385  case FMT_YUY2:
386  case FMT_RGB24:
387  case FMT_ARGB32:
388  case FMT_RGBA32:
389  case FMT_BGRA:
390  case FMT_RGB32:
391  if (Plane == 0) return Width;
392  break;
393  default: break; // None and hardware formats
394  }
395  return 0;
396  }
397 
398  static int ColorDepth(int Format)
399  {
400  switch (Format)
401  {
402  case FMT_YUV420P9:
403  case FMT_YUV422P9:
404  case FMT_YUV444P9: return 9;
405  case FMT_P010:
406  case FMT_YUV420P10:
407  case FMT_YUV422P10:
408  case FMT_YUV444P10: return 10;
409  case FMT_YUV420P12:
410  case FMT_YUV422P12:
411  case FMT_YUV444P12: return 12;
412  case FMT_YUV420P14:
413  case FMT_YUV422P14:
414  case FMT_YUV444P14: return 14;
415  case FMT_P016:
416  case FMT_YUV420P16:
417  case FMT_YUV422P16:
418  case FMT_YUV444P16: return 16;
419  default: break;
420  }
421  return 8;
422  }
423 
424  static bool HardwareFormat(VideoFrameType Type)
425  {
426  return (Type == FMT_VDPAU) || (Type == FMT_VAAPI) ||
427  (Type == FMT_DXVA2) || (Type == FMT_MMAL) ||
428  (Type == FMT_MEDIACODEC) || (Type == FMT_VTB) ||
429  (Type == FMT_NVDEC) || (Type == FMT_DRMPRIME);
430  }
431 
433  {
434  return (Type == FMT_VDPAU) || (Type == FMT_VAAPI) || (Type == FMT_NVDEC);
435  }
436 
437  static bool FormatIs420(VideoFrameType Type)
438  {
439  return (Type == FMT_YV12) || (Type == FMT_YUV420P9) || (Type == FMT_YUV420P10) ||
440  (Type == FMT_YUV420P12) || (Type == FMT_YUV420P14) || (Type == FMT_YUV420P16);
441  }
442 
443  static bool FormatIs422(VideoFrameType Type)
444  {
445  return (Type == FMT_YUV422P) || (Type == FMT_YUV422P9) || (Type == FMT_YUV422P10) ||
446  (Type == FMT_YUV422P12) || (Type == FMT_YUV422P14) || (Type == FMT_YUV422P16);
447  }
448 
449  static bool FormatIs444(VideoFrameType Type)
450  {
451  return (Type == FMT_YUV444P) || (Type == FMT_YUV444P9) || (Type == FMT_YUV444P10) ||
452  (Type == FMT_YUV444P12) || (Type == FMT_YUV444P14) || (Type == FMT_YUV444P16);
453  }
454 
455  static bool FormatIsNV12(VideoFrameType Type)
456  {
457  return (Type == FMT_NV12) || (Type == FMT_P010) || (Type == FMT_P016);
458  }
459 
460  static bool PackedFormat(VideoFrameType Type)
461  {
462  return Type == FMT_YUY2;
463  }
464 
465  static bool YUVFormat(VideoFrameType Type)
466  {
467  return FormatIs420(Type) || FormatIs422(Type) || FormatIs444(Type) ||
468  FormatIsNV12(Type) || PackedFormat(Type);
469  }
470 
471  static bool FormatIsRGB(VideoFrameType Type)
472  {
473  return (FMT_RGB24 == Type) || (FMT_BGRA == Type) || (FMT_RGB32 == Type) ||
474  (FMT_ARGB32 == Type) || (FMT_RGBA32 == Type);
475  }
476 
477  private:
478  static MythDeintType GetDeinterlacer(MythDeintType Option);
479 };
480 
481 #endif
FMT_VTB
@ FMT_VTB
Definition: mythframe.h:61
FMT_YUV444P16
@ FMT_YUV444P16
Definition: mythframe.h:48
FMT_ARGB32
@ FMT_ARGB32
Definition: mythframe.h:33
FMT_VDPAU
@ FMT_VDPAU
Definition: mythframe.h:56
MythVideoFrame::GetPitchForPlane
static int GetPitchForPlane(VideoFrameType Type, int Width, uint Plane)
Definition: mythframe.h:303
FMT_YUV420P14
@ FMT_YUV420P14
Definition: mythframe.h:27
FMT_YUV444P10
@ FMT_YUV444P10
Definition: mythframe.h:45
MythVideoFrame::PackedFormat
static bool PackedFormat(VideoFrameType Type)
Definition: mythframe.h:460
MythVideoFrame::HardwareFramesFormat
static bool HardwareFramesFormat(VideoFrameType Type)
Definition: mythframe.h:432
mythtvexp.h
operator|
MythDeintType operator|(MythDeintType a, MythDeintType b)
Definition: mythframe.h:78
FMT_NVDEC
@ FMT_NVDEC
Definition: mythframe.h:62
MythVideoFrame::FormatIsRGB
static bool FormatIsRGB(VideoFrameType Type)
Definition: mythframe.h:471
FMT_YUV420P16
@ FMT_YUV420P16
Definition: mythframe.h:28
operator~
MythDeintType operator~(MythDeintType a)
Definition: mythframe.h:80
FramePitches
std::array< int, 3 > FramePitches
Definition: mythframe.h:83
MythDate::Format
Format
Definition: mythdate.h:15
FMT_P016
@ FMT_P016
Definition: mythframe.h:54
FMT_NONE
@ FMT_NONE
Definition: mythframe.h:21
MythVideoFrame::HardwareFormat
static bool HardwareFormat(VideoFrameType Type)
Definition: mythframe.h:424
MYTH_WIDTH_ALIGNMENT
static constexpr uint8_t MYTH_WIDTH_ALIGNMENT
Definition: mythframe.h:16
FrameOffsets
std::array< int, 3 > FrameOffsets
Definition: mythframe.h:84
MythVideoFrame::FormatIs422
static bool FormatIs422(VideoFrameType Type)
Definition: mythframe.h:443
FMT_RGB24
@ FMT_RGB24
Definition: mythframe.h:30
MythDeintType
MythDeintType
Definition: mythframe.h:66
MythVideoFrame::BitsPerPixel
static int BitsPerPixel(VideoFrameType Type)
Definition: mythframe.h:169
MythVideoFrame::YUVFormat
static bool YUVFormat(VideoFrameType Type)
Definition: mythframe.h:465
FMT_VAAPI
@ FMT_VAAPI
Definition: mythframe.h:57
FMT_P010
@ FMT_P010
Definition: mythframe.h:53
FMT_YUY2
@ FMT_YUY2
Definition: mythframe.h:50
FMT_YUV422P12
@ FMT_YUV422P12
Definition: mythframe.h:39
FMT_YUV422P
@ FMT_YUV422P
Definition: mythframe.h:36
FMT_YUV444P12
@ FMT_YUV444P12
Definition: mythframe.h:46
DEINT_ALL
@ DEINT_ALL
Definition: mythframe.h:75
FMT_NV12
@ FMT_NV12
Definition: mythframe.h:52
VideoFrameTypes
std::vector< VideoFrameType > VideoFrameTypes
Definition: mythframe.h:82
DEINT_DRIVER
@ DEINT_DRIVER
Definition: mythframe.h:74
DEINT_SHADER
@ DEINT_SHADER
Definition: mythframe.h:73
FMT_YUV420P9
@ FMT_YUV420P9
Definition: mythframe.h:24
MythHDRVideoPtr
std::shared_ptr< class MythHDRVideoMetadata > MythHDRVideoPtr
Definition: mythframe.h:85
VideoFrameType
VideoFrameType
Definition: mythframe.h:19
DEINT_BASIC
@ DEINT_BASIC
Definition: mythframe.h:69
FMT_YUV444P14
@ FMT_YUV444P14
Definition: mythframe.h:47
FMT_YV12
@ FMT_YV12
Definition: mythframe.h:23
MythVideoFrame::ColorDepth
static int ColorDepth(int Format)
Definition: mythframe.h:398
FMT_DRMPRIME
@ FMT_DRMPRIME
Definition: mythframe.h:63
FMT_RGBA32
@ FMT_RGBA32
Definition: mythframe.h:34
MythVideoFrame::GetWidthForPlane
static int GetWidthForPlane(VideoFrameType Type, int Width, uint Plane)
Definition: mythframe.h:353
FMT_YUV422P9
@ FMT_YUV422P9
Definition: mythframe.h:37
DEINT_NONE
@ DEINT_NONE
Definition: mythframe.h:68
DEINT_HIGH
@ DEINT_HIGH
Definition: mythframe.h:71
operator&
MythDeintType operator&(MythDeintType a, MythDeintType b)
Definition: mythframe.h:79
DEINT_CPU
@ DEINT_CPU
Definition: mythframe.h:72
FMT_YUV422P16
@ FMT_YUV422P16
Definition: mythframe.h:41
MythVideoFrame::FormatIs444
static bool FormatIs444(VideoFrameType Type)
Definition: mythframe.h:449
FMT_YUV420P10
@ FMT_YUV420P10
Definition: mythframe.h:25
FMT_YUV444P
@ FMT_YUV444P
Definition: mythframe.h:43
MTV_PUBLIC
#define MTV_PUBLIC
Definition: mythtvexp.h:15
FMT_YUV444P9
@ FMT_YUV444P9
Definition: mythframe.h:44
DEINT_MEDIUM
@ DEINT_MEDIUM
Definition: mythframe.h:70
MythVideoFrame::FormatIs420
static bool FormatIs420(VideoFrameType Type)
Definition: mythframe.h:437
MythVideoFrame::GetHeightForPlane
static int GetHeightForPlane(VideoFrameType Type, int Height, uint Plane)
Definition: mythframe.h:257
Buffer
Definition: MythExternControl.h:36
FMT_MMAL
@ FMT_MMAL
Definition: mythframe.h:59
FMT_BGRA
@ FMT_BGRA
Definition: mythframe.h:31
mythchrono.h
MythVideoFrame::GetNumPlanes
static uint GetNumPlanes(VideoFrameType Type)
Definition: mythframe.h:213
MythVideoFrame::m_dstRect
QRect m_dstRect
Definition: mythframe.h:167
MythVideoFrame::m_srcRect
QRect m_srcRect
Definition: mythframe.h:166
FMT_DXVA2
@ FMT_DXVA2
Definition: mythframe.h:58
MYTH_HEIGHT_ALIGNMENT
static constexpr uint8_t MYTH_HEIGHT_ALIGNMENT
Definition: mythframe.h:17
FMT_RGB32
@ FMT_RGB32
endian dependent format, ARGB or BGRA
Definition: mythframe.h:32
MythVideoFrame
Definition: mythframe.h:87
FMT_YUV422P14
@ FMT_YUV422P14
Definition: mythframe.h:40
FMT_MEDIACODEC
@ FMT_MEDIACODEC
Definition: mythframe.h:60
MythVideoFrame::FormatIsNV12
static bool FormatIsNV12(VideoFrameType Type)
Definition: mythframe.h:455
FMT_YUV422P10
@ FMT_YUV422P10
Definition: mythframe.h:38
FMT_YUV420P12
@ FMT_YUV420P12
Definition: mythframe.h:26
uint
unsigned int uint
Definition: freesurround.h:24