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