MythTV  master
mythavutil.h
Go to the documentation of this file.
1 //
2 // mythavutil.h
3 // MythTV
4 //
5 // Created by Jean-Yves Avenard on 7/06/2014.
6 // Copyright (c) 2014 Bubblestuff Pty Ltd. All rights reserved.
7 //
8 
9 #ifndef MythTV_mythavutil_h
10 #define MythTV_mythavutil_h
11 
12 #include "mythframe.h"
13 extern "C" {
14 #include "libavcodec/avcodec.h"
15 }
16 
17 #include <QMap>
18 #include <QMutex>
19 
20 struct AVFilterGraph;
21 struct AVFilterContext;
22 struct AVStream;
23 struct AVCodecContext;
24 
43 {
44 public:
46  {
47  m_frame = av_frame_alloc();
48  }
50  {
51  av_frame_free(&m_frame);
52  }
53  bool operator !() const
54  {
55  return m_frame == nullptr;
56  }
58  {
59  return m_frame;
60  }
61  AVFrame& operator*() const
62  {
63  return *m_frame;
64  }
65  operator AVFrame*() const
66  {
67  return m_frame;
68  }
69  operator const AVFrame*() const
70  {
71  return m_frame;
72  }
73 
74 private:
75  AVFrame *m_frame {nullptr};
76 };
77 
89 {
90  public:
91  MythCodecMap() = default;
92  ~MythCodecMap();
93  static MythCodecMap *getInstance();
94  AVCodecContext *getCodecContext(const AVStream *stream,
95  const AVCodec *pCodec = nullptr, bool nullCodec = false);
96  AVCodecContext *hasCodecContext(const AVStream *stream);
97  void freeCodecContext(const AVStream *stream);
98  void freeAllCodecContexts();
99  protected:
100  QMap<const AVStream*, AVCodecContext*> m_streamMap;
101  QMutex m_mapLock {QMutex::Recursive};
102 };
103 
106 
107 
108 class MythAVCopyPrivate;
109 
115 {
116 public:
117  explicit MythAVCopy(bool USWC=true);
118  virtual ~MythAVCopy();
119  MythAVCopy(const MythAVCopy &) = delete; // not copyable
120  MythAVCopy &operator=(const MythAVCopy &) = delete; // not copyable
121 
122  int Copy(VideoFrame *dst, const VideoFrame *src);
131  int Copy(AVFrame *pic, const VideoFrame *frame,
132  unsigned char *buffer = nullptr,
133  AVPixelFormat fmt = AV_PIX_FMT_YUV420P);
139  int Copy(VideoFrame *frame, const AVFrame *pic,
140  AVPixelFormat fmt = AV_PIX_FMT_YUV420P);
141  int Copy(AVFrame *dst, AVPixelFormat dst_pix_fmt,
142  const AVFrame *src, AVPixelFormat pix_fmt,
143  int width, int height);
144 
145 private:
146  static void FillFrame(VideoFrame *frame, const AVFrame *pic, int pitch,
147  int width, int height, AVPixelFormat pix_fmt);
148  MythAVCopyPrivate *d {nullptr}; // NOLINT(readability-identifier-naming)
149 };
150 
155 int MTV_PUBLIC AVPictureFill(AVFrame *pic, const VideoFrame *frame,
156  AVPixelFormat fmt = AV_PIX_FMT_NONE);
157 
164 
167 MTV_PUBLIC QString DeinterlacerName(MythDeintType Deint, bool DoubleRate, VideoFrameType Format = FMT_NONE);
168 
170 
177 {
178 public:
179  MythPictureDeinterlacer(AVPixelFormat pixfmt, int width, int height, float ar = 1.0F);
181  // Will deinterlace src into dst. If EAGAIN is returned, more frames
182  // are needed to output a frame.
183  // To drain the deinterlacer, call Deinterlace with src = nullptr until you
184  // get no more frames. Once drained, you must call Flush() to start
185  // deinterlacing again.
186  int Deinterlace(AVFrame *dst, const AVFrame *src);
187  int DeinterlaceSingle(AVFrame *dst, const AVFrame *src);
188  // Flush and reset the deinterlacer.
189  int Flush();
190 private:
191  AVFilterGraph* m_filterGraph {nullptr};
193  AVFilterContext* m_bufferSinkCtx {nullptr};
194  AVFilterContext* m_bufferSrcCtx {nullptr};
195  AVPixelFormat m_pixfmt {AV_PIX_FMT_NONE};
196  int m_width {0};
197  int m_height {0};
198  float m_ar;
199  bool m_errored {false};
200 };
201 #endif
MythAVFrame m_filterFrame
Definition: mythavutil.h:192
MTV_PUBLIC AVPixelFormat FrameTypeToPixelFormat(VideoFrameType type)
Convert VideoFrameType into FFmpeg's PixelFormat equivalent and vice-versa.
Definition: mythavutil.cpp:24
MTV_PUBLIC QString DeinterlacerName(MythDeintType Deint, bool DoubleRate, VideoFrameType Format=FMT_NONE)
Return a user friendly description of the given deinterlacer.
Definition: mythavutil.cpp:114
AVFrame * operator->() const
Definition: mythavutil.h:57
bool operator!() const
Definition: mythavutil.h:53
AVFrame * m_frame
Definition: mythavutil.h:75
MTV_PUBLIC QString DeinterlacerPref(MythDeintType Deint)
Definition: mythavutil.cpp:182
MythAVCopy Copy AVFrame<->frame, performing the required conversion if any.
Definition: mythavutil.h:114
struct AVFrame AVFrame
int MTV_PUBLIC AVPictureFill(AVFrame *pic, const VideoFrame *frame, AVPixelFormat fmt=AV_PIX_FMT_NONE)
AVPictureFill Initialise AVFrame pic with content from VideoFrame frame.
Definition: mythavutil.cpp:196
VideoFrameType
Definition: mythframe.h:23
QMap< const AVStream *, AVCodecContext * > m_streamMap
Definition: mythavutil.h:100
MythDeintType
Definition: mythframe.h:120
MTV_PUBLIC VideoFrameType PixelFormatToFrameType(AVPixelFormat fmt)
Definition: mythavutil.cpp:68
#define MTV_PUBLIC
Definition: mythtvexp.h:15
static const uint16_t * d
MTV_PUBLIC MythCodecMap * gCodecMap
This global variable contains the MythCodecMap instance for the app.
Definition: mythavutil.cpp:508
MythAVFrame(void)
Definition: mythavutil.h:45
MythPictureDeinterlacer simple deinterlacer based on FFmpeg's yadif filter.
Definition: mythavutil.h:176
AVFrame & operator*() const
Definition: mythavutil.h:61
~MythAVFrame(void)
Definition: mythavutil.h:49
MythCodecMap Utility class that keeps pointers to an AVStream and its AVCodecContext.
Definition: mythavutil.h:88
MythAVFrame little utility class that act as a safe way to allocate an AVFrame which can then be allo...
Definition: mythavutil.h:42