MythTV  master
mythavutil.h
Go to the documentation of this file.
1 #ifndef MYTHAVUTIL_H
2 #define MYTHAVUTIL_H
3 
4 #include <utility>
5 
6 // Qt
7 #include <QMap>
8 #include <QRecursiveMutex>
9 #include <QVector>
10 
11 // FFmpeg
12 extern "C" {
13 #include "libavcodec/avcodec.h"
14 
15 #include "libavutil/buffer.h"
16 }
17 
18 // MythTV
20 #include "libmythtv/mythframe.h"
21 #include "libmythui/mythhdr.h"
22 
23 struct SwsContext;
24 struct AVStream;
25 struct AVCodecContext;
26 
28 {
29  public:
30  MythCodecMap() = default;
31  ~MythCodecMap();
32  AVCodecContext* GetCodecContext(const AVStream* Stream,
33  const AVCodec* Codec = nullptr,
34  bool NullCodec = false);
35  AVCodecContext* FindCodecContext(const AVStream* Stream);
36  void FreeCodecContext(const AVStream* Stream);
37  void FreeAllContexts();
38 
39  private:
40  QMap<const AVStream*, AVCodecContext*> m_streamMap;
41  QRecursiveMutex m_mapLock;
42 };
43 
45 {
46  public:
47  MythAVCopy() = default;
48  ~MythAVCopy();
49  int Copy(AVFrame* To, const MythVideoFrame* From, unsigned char* Buffer,
50  AVPixelFormat Fmt = AV_PIX_FMT_YUV420P);
51  int Copy(AVFrame* To, AVPixelFormat ToFmt, const AVFrame* From, AVPixelFormat FromFmt,
52  int Width, int Height);
53 
54  private:
55  Q_DISABLE_COPY(MythAVCopy)
56  int SizeData(int Width, int Height, AVPixelFormat Fmt);
57 
58  AVPixelFormat m_format { AV_PIX_FMT_NONE };
59  SwsContext* m_swsctx { nullptr };
60  int m_width { 0 };
61  int m_height { 0 };
62  int m_size { 0 };
63 };
64 
66 {
67  public:
68  static void DeinterlaceAVFrame(AVFrame* Frame);
69  static int FillAVFrame(AVFrame* Frame, const MythVideoFrame* From, AVPixelFormat Fmt = AV_PIX_FMT_NONE);
70  static AVPixelFormat FrameTypeToPixelFormat(VideoFrameType Type);
71  static VideoFrameType PixelFormatToFrameType(AVPixelFormat Fmt);
72  static MythHDR::HDRType FFmpegTransferToHDRType(int Transfer);
73 };
74 
76 public:
77  // These are for All types
78  char m_codecType {' '}; // V=video, A=audio, S=subtitle
79  QString m_codecName;
80  int64_t m_duration {0};
81  // These are for Video only
82  int m_width {0};
83  int m_height {0};
84  float m_SampleAspectRatio {0.0};
85  // AV_FIELD_TT, //< Top coded_first, top displayed first
86  // AV_FIELD_BB, //< Bottom coded first, bottom displayed first
87  // AV_FIELD_TB, //< Top coded first, bottom displayed first
88  // AV_FIELD_BT, //< Bottom coded first, top displayed first
89  QString m_fieldOrder {"UN"}; // UNknown, PRogressive, TT, BB, TB, BT
90  float m_frameRate {0.0};
91  float m_avgFrameRate {0.0};
92  // This is for audio only
93  int m_channels {0};
94 };
95 
96 
97 /*
98 * Class to get stream info, used by service Video/GetStreamInfo
99 */
101 public:
102  explicit MythStreamInfoList(const QString& filename);
103  int m_errorCode {0};
104  QString m_errorMsg;
105  QVector<MythStreamInfo> m_streamInfoList;
106 };
107 
116  public:
120  explicit MythAVBufferRef(const AVBufferRef* buf = nullptr) : m_buffer(ref(buf)) {}
121  ~MythAVBufferRef() { unref(); }
122 
123  // Copy constructor
124  MythAVBufferRef(const MythAVBufferRef& other) : MythAVBufferRef(other.m_buffer) {}
125  // Move constructor
126  MythAVBufferRef(MythAVBufferRef&& other) noexcept : MythAVBufferRef() { swap(*this, other); }
127  // Copy assignment operator
129  {
130  swap(*this, other);
131  return *this;
132  }
133  // Move assignment operator
135  {
136  // release resources held by this, but prevent suicide on self-assignment
137  MythAVBufferRef tmp {std::move(other)};
138  swap(*this, tmp);
139  return *this;
140  }
141 
142  friend void swap(MythAVBufferRef& a, MythAVBufferRef& b) noexcept
143  {
144  using std::swap;
145  swap(a.m_buffer, b.m_buffer);
146  }
147 
148  bool has_buffer() { return m_buffer != nullptr; }
149 
150  const uint8_t* data() { return m_buffer->data; }
151  size_t size() { return m_buffer->size; }
152  private:
153  static AVBufferRef* ref(const AVBufferRef* buf)
154  {
155  if (buf == nullptr)
156  {
157  return nullptr;
158  }
159  AVBufferRef* reference = av_buffer_ref(buf);
160  if (reference == nullptr)
161  {
162  LOG(VB_GENERAL, LOG_ERR, "av_buffer_ref() failed to allocate memory.");
163  }
164  return reference;
165  }
166  void unref() { av_buffer_unref(&m_buffer); }
167 
168  AVBufferRef* m_buffer {nullptr};
169 };
170 
171 
172 #endif
MythAVBufferRef::MythAVBufferRef
MythAVBufferRef(const AVBufferRef *buf=nullptr)
Definition: mythavutil.h:120
MythStreamInfoList::m_errorMsg
QString m_errorMsg
Definition: mythavutil.h:104
MythStreamInfo::m_codecName
QString m_codecName
Definition: mythavutil.h:79
MythAVBufferRef::has_buffer
bool has_buffer()
Definition: mythavutil.h:148
MythAVBufferRef::MythAVBufferRef
MythAVBufferRef(MythAVBufferRef &&other) noexcept
Definition: mythavutil.h:126
Frame
Definition: zmdefines.h:102
MythStreamInfo
Definition: mythavutil.h:75
MythStreamInfoList
Definition: mythavutil.h:100
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
mythframe.h
MythAVBufferRef::operator=
MythAVBufferRef & operator=(MythAVBufferRef &&other) noexcept
Definition: mythavutil.h:134
MythHDR::HDRType
HDRType
Definition: mythhdr.h:36
tmp
static guint32 * tmp
Definition: goom_core.cpp:26
AVFrame
struct AVFrame AVFrame
Definition: BorderDetector.h:15
mythlogging.h
MythAVBufferRef::swap
friend void swap(MythAVBufferRef &a, MythAVBufferRef &b) noexcept
Definition: mythavutil.h:142
MythAVBufferRef
C++ wrapper for AVBufferRef.
Definition: mythavutil.h:115
VideoFrameType
VideoFrameType
Definition: mythframe.h:19
MythAVBufferRef::~MythAVBufferRef
~MythAVBufferRef()
Definition: mythavutil.h:121
MythAVBufferRef::ref
static AVBufferRef * ref(const AVBufferRef *buf)
Definition: mythavutil.h:153
MythAVUtil
Definition: mythavutil.h:65
MythCodecMap
Definition: mythavutil.h:27
mythhdr.h
MTV_PUBLIC
#define MTV_PUBLIC
Definition: mythtvexp.h:15
Buffer
Definition: MythExternControl.h:36
MythAVCopy
Definition: mythavutil.h:44
MythAVBufferRef::data
const uint8_t * data()
Definition: mythavutil.h:150
MythCodecMap::m_streamMap
QMap< const AVStream *, AVCodecContext * > m_streamMap
Definition: mythavutil.h:40
MythAVBufferRef::operator=
MythAVBufferRef & operator=(MythAVBufferRef other)
Definition: mythavutil.h:128
MythAVBufferRef::size
size_t size()
Definition: mythavutil.h:151
MythVideoFrame
Definition: mythframe.h:87
MythAVBufferRef::MythAVBufferRef
MythAVBufferRef(const MythAVBufferRef &other)
Definition: mythavutil.h:124
build_compdb.filename
filename
Definition: build_compdb.py:21
MythAVBufferRef::unref
void unref()
Definition: mythavutil.h:166
MythCodecMap::m_mapLock
QRecursiveMutex m_mapLock
Definition: mythavutil.h:41
MythStreamInfoList::m_streamInfoList
QVector< MythStreamInfo > m_streamInfoList
Definition: mythavutil.h:105