forked from xueguoliang/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filter.cpp
185 lines (158 loc) · 5.61 KB
/
Filter.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
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV (from 2013)
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/Filter.h"
#include "QtAV/private/Filter_p.h"
#include "QtAV/Statistics.h"
#include "QtAV/AVOutput.h"
#include "QtAV/AVPlayer.h"
#include "filter/FilterManager.h"
#include "utils/Logger.h"
/*
* 1. parent == target (QObject):
* in ~target(), remove the filter but not delete it (parent not null now).
* in ~QObject, filter is deleted as a child
*-----------------------------------------------
* 2. parent != target.parent:
* if delete filter first, filter must notify FilterManager (uninstall in dtor here) to uninstall to avoid target to access it (in ~target())
* if delete target first, target remove the filter but not delete it (parent not null now).
*/
namespace QtAV {
Filter::Filter(FilterPrivate &d, QObject *parent)
: QObject(parent)
, DPTR_INIT(&d)
{
if (parent)
setOwnedByTarget(false);
}
Filter::~Filter()
{
uninstall();
}
void Filter::setEnabled(bool enabled)
{
DPTR_D(Filter);
if (d.enabled == enabled)
return;
d.enabled = enabled;
Q_EMIT enabledChanged(enabled);
}
bool Filter::isEnabled() const
{
DPTR_D(const Filter);
return d.enabled;
}
void Filter::setOwnedByTarget(bool value)
{
d_func().owned_by_target = value;
}
bool Filter::isOwnedByTarget() const
{
return d_func().owned_by_target;
}
bool Filter::uninstall()
{
return FilterManager::instance().uninstallFilter(this); // TODO: target
}
AudioFilter::AudioFilter(QObject *parent)
: Filter(*new AudioFilterPrivate(), parent)
{}
AudioFilter::AudioFilter(AudioFilterPrivate& d, QObject *parent)
: Filter(d, parent)
{}
/*TODO: move to AVPlayer.cpp to reduce dependency?*/
bool AudioFilter::installTo(AVPlayer *player)
{
return player->installFilter(this);
}
void AudioFilter::apply(Statistics *statistics, AudioFrame *frame)
{
process(statistics, frame);
}
VideoFilter::VideoFilter(QObject *parent)
: Filter(*new VideoFilterPrivate(), parent)
{}
VideoFilter::VideoFilter(VideoFilterPrivate &d, QObject *parent)
: Filter(d, parent)
{}
VideoFilterContext *VideoFilter::context()
{
DPTR_D(VideoFilter);
if (!d.context) {
//fake. only to store some parameters at the beginnig. it will be destroyed and set to a new instance if context type mismatch in prepareContext, with old parameters
d.context = VideoFilterContext::create(VideoFilterContext::QtPainter);
}
return d.context;
}
bool VideoFilter::isSupported(VideoFilterContext::Type ct) const
{
// TODO: return false
return VideoFilterContext::None == ct;
}
bool VideoFilter::installTo(AVPlayer *player)
{
return player->installFilter(this);
}
/*TODO: move to AVOutput.cpp to reduce dependency?*/
/*
* filter.installTo(target,...) calls target.installFilter(filter)
* If filter is already registered in FilterManager, then return false
* Otherwise, call FilterManager.register(filter) and target.filters.push_back(filter), return true
* NOTE: the installed filter will be deleted by the target if filter is owned by target AND it's parent (QObject) is null.
*/
bool VideoFilter::installTo(AVOutput *output)
{
return output->installFilter(this);
}
bool VideoFilter::prepareContext(VideoFilterContext *&ctx, Statistics *statistics, VideoFrame *frame)
{
DPTR_D(VideoFilter);
if (!ctx || !isSupported(ctx->type())) {
//qDebug("no context: %p, or context type %d is not supported", ctx, ctx? ctx->type() : 0);
return isSupported(VideoFilterContext::None);
}
if (!d.context || d.context->type() != ctx->type()) {
VideoFilterContext* c = VideoFilterContext::create(ctx->type());//each filter has it's own context instance, but share the common parameters
if (d.context) {
c->pen = d.context->pen;
c->brush = d.context->brush;
c->clip_path = d.context->clip_path;
c->rect = d.context->rect;
c->transform = d.context->transform;
c->font = d.context->font;
c->opacity = d.context->opacity;
c->paint_device = d.context->paint_device;
}
if (d.context) {
delete d.context;
}
d.context = c;
}
d.context->video_width = statistics->video_only.width;
d.context->video_height = statistics->video_only.height;
ctx->video_width = statistics->video_only.width;
ctx->video_height = statistics->video_only.height;
// share common data
d.context->shareFrom(ctx);
d.context->initializeOnFrame(frame);
ctx->shareFrom(d.context);
return true;
}
void VideoFilter::apply(Statistics *statistics, VideoFrame *frame)
{
process(statistics, frame);
}
} //namespace QtAV