forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVEncoder.cpp
215 lines (188 loc) · 5.25 KB
/
AVEncoder.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
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV (from 2015)
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/AVEncoder.h>
#include <QtAV/private/AVEncoder_p.h>
#include <QtAV/version.h>
#include "utils/internal.h"
#include "utils/Logger.h"
namespace QtAV {
AVEncoder::AVEncoder(AVEncoderPrivate &d)
:DPTR_INIT(&d)
{
}
AVEncoder::~AVEncoder()
{
close();
}
QString AVEncoder::description() const
{
return QString();
}
void AVEncoder::setCodecName(const QString &name)
{
DPTR_D(AVEncoder);
if (d.codec_name == name)
return;
d.codec_name = name;
Q_EMIT codecNameChanged();
}
QString AVEncoder::codecName() const
{
DPTR_D(const AVEncoder);
if (!d.codec_name.isEmpty())
return d.codec_name;
if (d.avctx)
return QLatin1String(avcodec_get_name(d.avctx->codec_id));
return QString();
}
void AVEncoder::setBitRate(int value)
{
DPTR_D(AVEncoder);
if (d.bit_rate == value)
return;
d.bit_rate = value;
emit bitRateChanged();
}
int AVEncoder::bitRate() const
{
return d_func().bit_rate;
}
AVEncoder::TimestampMode AVEncoder::timestampMode() const
{
return TimestampMode(d_func().timestamp_mode);
}
void AVEncoder::setTimestampMode(TimestampMode value)
{
DPTR_D(AVEncoder);
if (d.timestamp_mode == (int)value)
return;
d.timestamp_mode = (int)value;
Q_EMIT timestampModeChanged(value);
}
bool AVEncoder::open()
{
DPTR_D(AVEncoder);
if (d.avctx) {
d.applyOptionsForDict();
}
if (!d.open()) {
d.close();
return false;
}
d.is_open = true;
return true;
}
bool AVEncoder::close()
{
if (!isOpen()) {
return true;
}
DPTR_D(AVEncoder);
d.is_open = false;
// hwa extra finalize can be here
d.close();
return true;
}
bool AVEncoder::isOpen() const
{
return d_func().is_open;
}
void AVEncoder::flush()
{
if (!isOpen())
return;
if (d_func().avctx)
avcodec_flush_buffers(d_func().avctx);
}
Packet AVEncoder::encoded() const
{
return d_func().packet;
}
void* AVEncoder::codecContext() const
{
return d_func().avctx;
}
void AVEncoder::copyAVCodecContext(void* ctx)
{
if (!ctx)
return;
DPTR_D(AVEncoder);
AVCodecContext* c = static_cast<AVCodecContext*>(ctx);
if (d.avctx) {
// dest should be avcodec_alloc_context3(NULL)
AV_ENSURE_OK(avcodec_copy_context(d.avctx, c));
d.is_open = false;
return;
}
}
void AVEncoder::setOptions(const QVariantHash &dict)
{
DPTR_D(AVEncoder);
d.options = dict;
// if dict is empty, can not return here, default options will be set for AVCodecContext
// apply to AVCodecContext
d.applyOptionsForContext();
/* set AVEncoder meta properties.
* we do not check whether the property exists thus we can set dynamic properties.
*/
if (dict.isEmpty())
return;
if (name() == QLatin1String("avcodec"))
return;
QVariant opt(dict);
if (dict.contains(name()))
opt = dict.value(name());
else if (dict.contains(name().toLower()))
opt = dict.value(name().toLower());
Internal::setOptionsForQObject(opt, this);
}
QVariantHash AVEncoder::options() const
{
return d_func().options;
}
void AVEncoderPrivate::applyOptionsForDict()
{
if (dict) {
av_dict_free(&dict);
dict = 0; //aready 0 in av_free
}
if (options.isEmpty())
return;
qDebug("set AVCodecContext dict:");
// TODO: use QVariantMap only
if (!options.contains(QStringLiteral("avcodec")))
return;
// workaround for VideoDecoderFFmpeg. now it does not call av_opt_set_xxx, so set here in dict
// TODO: wrong if opt is empty
Internal::setOptionsToDict(options.value(QStringLiteral("avcodec")), &dict);
}
void AVEncoderPrivate::applyOptionsForContext()
{
if (!avctx)
return;
if (options.isEmpty()) {
// av_opt_set_defaults(avctx); //can't set default values! result maybe unexpected
return;
}
// TODO: use QVariantMap only
if (!options.contains(QStringLiteral("avcodec")))
return;
// workaround for VideoDecoderFFmpeg. now it does not call av_opt_set_xxx, so set here in dict
// TODO: wrong if opt is empty
Internal::setOptionsToFFmpegObj(options.value(QStringLiteral("avcodec")), avctx);
}
} //namespace QtAV