Miam-Player  0.8.0
A nice music player
FilterContext.h
Go to the documentation of this file.
1 /******************************************************************************
2  QtAV: Media play library based on Qt and FFmpeg
3  Copyright (C) 2012-2016 Wang Bin <wbsecg1@gmail.com>
4 
5 * This file is part of QtAV (from 2013)
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 ******************************************************************************/
21 
22 #ifndef QTAV_FILTERCONTEXT_H
23 #define QTAV_FILTERCONTEXT_H
24 
25 #include <QtAV/QtAV_Global.h>
26 #include <QtCore/QByteArray>
27 #include <QtCore/QRect>
28 #include <QtGui/QPainter>
29 /*
30  * QPainterFilterContext, D2DFilterContext, ...
31  */
32 
33 QT_BEGIN_NAMESPACE
34 class QPainter;
35 class QPaintDevice;
36 class QTextDocument;
37 QT_END_NAMESPACE
38 namespace QtAV {
39 
40 class VideoFrame;
42 {
43 public:
44  enum Type {
47  Direct2D, //Not implemeted
48  GdiPlus, //Not implemented
49  X11,
50  None //user defined filters, no context can be used
51  };
52  static VideoFilterContext* create(Type t);
54  virtual ~VideoFilterContext();
55  virtual Type type() const = 0;
56 
57  // map to Qt types
58  //drawSurface?
59  virtual void drawImage(const QPointF& pos, const QImage& image, const QRectF& source = QRectF(), Qt::ImageConversionFlags flags = Qt::AutoColor);
60  // if target is null, draw image at target.topLeft(). if source is null, draw the whole image
61  virtual void drawImage(const QRectF& target, const QImage& image, const QRectF& source = QRectF(), Qt::ImageConversionFlags flags = Qt::AutoColor);
62  virtual void drawPlainText(const QPointF& pos, const QString& text);
63  // if rect is null, draw single line text at rect.topLeft(), ignoring flags
64  virtual void drawPlainText(const QRectF& rect, int flags, const QString& text);
65  virtual void drawRichText(const QRectF& rect, const QString& text, bool wordWrap = true);
66  /*
67  * TODO: x, y, width, height: |?|>=1 is in pixel unit, otherwise is ratio of video context rect
68  * filter.x(a).y(b).width(c).height(d)
69  */
70  QRectF rect;
71  // Fallback to QPainter if no other paint engine implemented
72  QPainter *painter; //TODO: shared_ptr?
73  qreal opacity;
74  QTransform transform;
75  QPainterPath clip_path;
76  QFont font;
77  QPen pen;
78  QBrush brush;
79  /*
80  * for the filters apply on decoded data, paint_device must be initialized once the data changes
81  * can we allocate memory on stack?
82  */
83  QPaintDevice *paint_device;
84  int video_width, video_height; //original size
85 
86 protected:
89 protected:
90  virtual bool isReady() const = 0;
91  // save the state then setup new parameters
92  virtual bool prepare() = 0;
93 
94  virtual void initializeOnFrame(VideoFrame *frame); //private?
95  // share qpainter etc.
96  virtual void shareFrom(VideoFilterContext *vctx);
97  friend class VideoFilter;
98 };
99 
100 class VideoFrameConverter;
101 //TODO: font, pen, brush etc?
102 class Q_AV_EXPORT QPainterFilterContext Q_DECL_FINAL: public VideoFilterContext
103 {
104 public:
105  QPainterFilterContext();
106  virtual ~QPainterFilterContext();
108  // empty source rect equals to the whole source rect
109  void drawImage(const QPointF& pos, const QImage& image, const QRectF& source = QRectF(), Qt::ImageConversionFlags flags = Qt::AutoColor) Q_DECL_OVERRIDE;
110  void drawImage(const QRectF& target, const QImage& image, const QRectF& source = QRectF(), Qt::ImageConversionFlags flags = Qt::AutoColor) Q_DECL_OVERRIDE;
111  void drawPlainText(const QPointF& pos, const QString& text) Q_DECL_OVERRIDE;
112  // if rect is null, draw single line text at rect.topLeft(), ignoring flags
113  void drawPlainText(const QRectF& rect, int flags, const QString& text) Q_DECL_OVERRIDE;
114  void drawRichText(const QRectF& rect, const QString& text, bool wordWrap = true) Q_DECL_OVERRIDE;
115 
116 protected:
117  bool isReady() const Q_DECL_OVERRIDE;
118  bool prepare() Q_DECL_OVERRIDE;
119  void initializeOnFrame(VideoFrame *vframe) Q_DECL_OVERRIDE;
120 
121  QTextDocument *doc;
123 };
124 
125 #if QTAV_HAVE(X11)
126 class Q_AV_EXPORT X11FilterContext Q_DECL_FINAL: public VideoFilterContext
127 {
128 public:
129  typedef struct _XDisplay Display;
130  typedef struct _XGC *GC;
131  typedef quintptr Drawable;
132  typedef quintptr Pixmap;
133  struct XImage;
134 
135  X11FilterContext();
136  virtual ~X11FilterContext();
137  Type type() const Q_DECL_OVERRIDE { return VideoFilterContext::X11;}
138  void resetX11(Display* dpy = 0, GC g = 0, Drawable d = 0);
139  // empty source rect equals to the whole source rect
140  void drawImage(const QPointF& pos, const QImage& image, const QRectF& source = QRectF(), Qt::ImageConversionFlags flags = Qt::AutoColor) Q_DECL_OVERRIDE;
141  void drawImage(const QRectF& target, const QImage& image, const QRectF& source = QRectF(), Qt::ImageConversionFlags flags = Qt::AutoColor) Q_DECL_OVERRIDE;
142  void drawPlainText(const QPointF& pos, const QString& text) Q_DECL_OVERRIDE;
143  // if rect is null, draw single line text at rect.topLeft(), ignoring flags
144  void drawPlainText(const QRectF& rect, int flags, const QString& text) Q_DECL_OVERRIDE;
145  void drawRichText(const QRectF& rect, const QString& text, bool wordWrap = true) Q_DECL_OVERRIDE;
146 protected:
147  bool isReady() const Q_DECL_OVERRIDE;
148  bool prepare() Q_DECL_OVERRIDE;
149  void initializeOnFrame(VideoFrame *vframe) Q_DECL_OVERRIDE;
150  void shareFrom(VideoFilterContext *vctx) Q_DECL_OVERRIDE;
151  // null image: use the old x11 image/pixmap
152  void renderTextImageX11(QImage* img, const QPointF &pos);
153  void destroyX11Resources();
154 
155  QTextDocument *doc;
156  VideoFrameConverter *cvt;
157 
158  Display* display;
159  GC gc;
160  Drawable drawable;
161  XImage *text_img;
162  XImage *mask_img;
163  Pixmap mask_pix;
164  QImage text_q;
165  QImage mask_q;
166 
167  bool plain;
168  QString text;
169  QImage test_img; //for computing bounding rect
170 };
171 #endif //QTAV_HAVE(X11)
172 } //namespace QtAV
173 
174 #endif // QTAV_FILTERCONTEXT_H
bool own_paint_device
Definition: FilterContext.h:88
Definition: Filter.h:77
qreal opacity
Definition: FilterContext.h:73
#define Q_AV_EXPORT
Definition: QtAV_Global.h:40
QPainter * painter
Definition: FilterContext.h:72
Definition: VideoFrame.h:132
QRectF rect
Definition: FilterContext.h:70
Definition: FilterContext.h:46
Definition: FilterContext.h:47
VideoFrameConverter * cvt
Definition: FilterContext.h:122
QPainterPath clip_path
Definition: FilterContext.h:75
Definition: FilterContext.h:41
Type type() const Q_DECL_OVERRIDE
Definition: FilterContext.h:107
Definition: FilterContext.h:48
int video_width
Definition: FilterContext.h:84
Definition: VideoFrame.h:32
QTextDocument * doc
Definition: FilterContext.h:121
Definition: FilterContext.h:45
QPaintDevice * paint_device
Definition: FilterContext.h:83
Definition: FilterContext.h:102
QBrush brush
Definition: FilterContext.h:78
AudioOutput ao; ao.setAudioFormat(fmt); ao.open(); while (has_data) { data = read_data(ao->bufferSize...
Definition: AudioDecoder.h:31
QFont font
Definition: FilterContext.h:76
Definition: FilterContext.h:49
Type
Definition: FilterContext.h:44
#define Q_DECL_OVERRIDE
Definition: QtAV_Global.h:177
QPen pen
Definition: FilterContext.h:77
bool own_painter
Definition: FilterContext.h:87
QTransform transform
Definition: FilterContext.h:74