forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterContext.cpp
287 lines (253 loc) · 7.51 KB
/
FilterContext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2013 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/FilterContext.h"
#include <QtGui/QImage>
#include <QtGui/QPainter>
#include "QtAV/VideoFrame.h"
namespace QtAV {
FilterContext* FilterContext::create(Type t)
{
FilterContext *ctx = 0;
switch (t) {
case QtPainter:
ctx = new QPainterFilterContext();
break;
case OpenGL:
ctx = new GLFilterContext();
break;
default:
break;
}
return ctx;
}
FilterContext::FilterContext():
video_width(0)
, video_height(0)
{
}
FilterContext::~FilterContext()
{
}
void FilterContext::initializeOnFrame(Frame *frame)
{
Q_UNUSED(frame);
}
void FilterContext::shareFrom(FilterContext *ctx)
{
Q_UNUSED(ctx);
video_width = ctx->video_width;
video_height = ctx->video_height;
}
VideoFilterContext::VideoFilterContext():
FilterContext()
, painter(0)
, paint_device(0)
, own_painter(false)
, own_paint_device(false)
, opacity(1)
{
font.setBold(true);
font.setPixelSize(26);
pen.setColor(Qt::white);
rect = QRect(32, 32, 0, 0); //TODO: why painting will above the visible area if the draw at (0, 0)?
}
VideoFilterContext::~VideoFilterContext()
{
if (painter) {
// painter is shared, so may be end() multiple times.
// TODO: use shared ptr
//if (painter->isActive())
// painter->end();
if (own_painter) {
qDebug("VideoFilterContext %p delete painter %p", this, painter);
delete painter;
painter = 0;
}
}
if (paint_device) {
qDebug("VideoFilterContext %p delete paint device in %p", this, paint_device);
if (own_paint_device)
delete paint_device; //delete recursively for widget
paint_device = 0;
}
}
void VideoFilterContext::drawImage(const QPointF &pos, const QImage &image, const QRectF& source, Qt::ImageConversionFlags flags)
{
Q_UNUSED(pos);
Q_UNUSED(image);
Q_UNUSED(source);
Q_UNUSED(flags);
}
void VideoFilterContext::drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
{
Q_UNUSED(target);
Q_UNUSED(image);
Q_UNUSED(source);
Q_UNUSED(flags);
}
void VideoFilterContext::drawPlainText(const QPointF &pos, const QString &text)
{
Q_UNUSED(pos);
Q_UNUSED(text);
}
void VideoFilterContext::drawPlainText(const QRectF &rect, int flags, const QString &text)
{
Q_UNUSED(rect);
Q_UNUSED(flags);
Q_UNUSED(text);
}
void VideoFilterContext::drawRichText(const QRectF &rect, const QString &text)
{
Q_UNUSED(rect);
Q_UNUSED(text);
}
void VideoFilterContext::shareFrom(FilterContext *ctx)
{
if (!ctx) {
qWarning("shared filter context is null!");
return;
}
VideoFilterContext *vctx = static_cast<VideoFilterContext*>(ctx);
painter = vctx->painter;
paint_device = vctx->paint_device;
own_painter = false;
own_paint_device = false;
video_width = vctx->video_width;
video_height = vctx->video_height;
}
FilterContext::Type QPainterFilterContext::type() const
{
return FilterContext::QtPainter;
}
void QPainterFilterContext::drawImage(const QPointF &pos, const QImage &image, const QRectF& source, Qt::ImageConversionFlags flags)
{
if (!prepare())
return;
painter->drawImage(pos, image, source, flags);
painter->restore();
}
void QPainterFilterContext::drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
{
if (!prepare())
return;
painter->drawImage(target, image, source, flags);
painter->restore();
}
void QPainterFilterContext::drawPlainText(const QPointF &pos, const QString &text)
{
if (!prepare())
return;
painter->drawText(pos, text);
painter->restore();
}
void QPainterFilterContext::drawPlainText(const QRectF &rect, int flags, const QString &text)
{
if (!prepare())
return;
if (rect.isNull())
painter->drawText(rect.topLeft(), text);
else
painter->drawText(rect, flags, text);
painter->restore();
}
void QPainterFilterContext::drawRichText(const QRectF &rect, const QString &text)
{
Q_UNUSED(rect);
Q_UNUSED(text);
if (!prepare())
return;
//QTextDocument
painter->restore();
}
bool QPainterFilterContext::isReady() const
{
return !!painter && painter->isActive();
}
bool QPainterFilterContext::prepare()
{
if (!isReady())
return false;
painter->save(); //is it necessary?
painter->setBrush(brush);
painter->setPen(pen);
painter->setFont(font);
painter->setOpacity(opacity);
if (!clip_path.isEmpty()) {
painter->setClipPath(clip_path);
}
//transform at last! because clip_path is relative to paint device coord
painter->setTransform(transform);
return true;
}
void QPainterFilterContext::initializeOnFrame(Frame *frame)
{
if (!frame) {
if (!painter) {
painter = new QPainter(); //warning: more than 1 painter on 1 device
}
if (!paint_device) {
paint_device = painter->device();
}
if (!paint_device && !painter->isActive()) {
qWarning("No paint device and painter is not active. No painting!");
return;
}
if (!painter->isActive())
painter->begin(paint_device);
return;
}
VideoFrame *vframe = static_cast<VideoFrame*>(frame);
VideoFormat format = vframe->format();
if (!format.isValid()) {
qWarning("Not a valid format");
return;
}
if (format.imageFormat() == QImage::Format_Invalid) {
format.setPixelFormat(VideoFormat::Format_RGB32);
vframe->convertTo(format);
}
if (paint_device) {
if (painter && painter->isActive()) {
painter->end(); //destroy a paint device that is being painted is not allowed!
}
delete paint_device;
paint_device = 0;
}
Q_ASSERT(video_width > 0 && video_height > 0);
// direct draw on frame data, so use VideoFrame::bits()
paint_device = new QImage((uchar*)vframe->bits(0), video_width, video_height, format.imageFormat());
if (!painter)
painter = new QPainter();
own_painter = true;
own_paint_device = true; //TODO: what about renderer is not a widget?
painter->begin((QImage*)paint_device);
}
FilterContext::Type GLFilterContext::type() const
{
return FilterContext::OpenGL;
}
bool GLFilterContext::isReady() const
{
return false;
}
bool GLFilterContext::prepare()
{
if (!isReady())
return false;
return true;
}
} //namespace QtAV