-
Notifications
You must be signed in to change notification settings - Fork 45
/
video_filterer.cpp
339 lines (278 loc) · 12.8 KB
/
video_filterer.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "video_filterer.h"
#include <cmath>
#include <iostream>
#include <string>
#include "ffmpeg.h"
#include "string_utils.h"
constexpr char VIDEO_FILTER_GROUP_DELIMITER = '|';
VideoFilterer::VideoFilterer(const Demuxer* demuxer,
const VideoDecoder* video_decoder,
int peak_luminance_nits,
const std::string& custom_video_filters,
const std::string& custom_color_space,
const std::string& custom_color_range,
const std::string& custom_color_primaries,
const std::string& custom_color_trc,
const Demuxer* other_demuxer,
const VideoDecoder* other_video_decoder,
int other_peak_luminance_nits,
const ToneMapping tone_mapping_mode,
const float boost_tone,
const bool disable_auto_filters)
: demuxer_(demuxer),
video_decoder_(video_decoder),
width_(video_decoder->width()),
height_(video_decoder->height()),
pixel_format_(video_decoder->pixel_format()),
color_space_(video_decoder->color_space()),
color_range_(video_decoder->color_range()) {
std::vector<std::string> filters;
// up to two filter groups are allowed ("pre" and "post"), if only a single group is specified it is assigned to the "post" group
const std::vector<std::string> custom_filter_groups = string_split(custom_video_filters, VIDEO_FILTER_GROUP_DELIMITER);
std::string custom_pre_filters, custom_post_filters;
if (custom_filter_groups.size() == 2 || (custom_filter_groups.size() == 1 && custom_video_filters.back() == VIDEO_FILTER_GROUP_DELIMITER)) {
custom_pre_filters = custom_filter_groups[0];
if (custom_filter_groups.size() > 1) {
custom_post_filters = custom_filter_groups[1];
}
} else if (custom_filter_groups.size() == 1) {
custom_post_filters = custom_filter_groups[0];
} else if (custom_filter_groups.size() > 2) {
throw std::runtime_error("No more than 2 filter groups supported");
}
// custom pre-filtering can for example be used to override the color space, primaries and trc settings in case of incorrect metadata before any tone-mapping is performed
// for example: 'setparams=colorspace=bt709|' (if not post-filtering is desired)
if (!custom_pre_filters.empty()) {
filters.push_back(custom_pre_filters);
}
if (!disable_auto_filters) {
// deinterlacing
const bool this_is_interlaced = video_decoder->codec_context()->field_order != AV_FIELD_PROGRESSIVE && video_decoder->codec_context()->field_order != AV_FIELD_UNKNOWN;
const bool other_is_interlaced = other_video_decoder->codec_context()->field_order != AV_FIELD_PROGRESSIVE && other_video_decoder->codec_context()->field_order != AV_FIELD_UNKNOWN;
if (this_is_interlaced) {
filters.push_back("bwdif");
}
double this_frame_rate_dbl = av_q2d(demuxer->guess_frame_rate());
double other_frame_rate_dbl = av_q2d(other_demuxer->guess_frame_rate());
if (this_is_interlaced) {
this_frame_rate_dbl *= 2.0;
}
if (other_is_interlaced) {
other_frame_rate_dbl *= 2.0;
}
// stretch to display aspect ratio
if (video_decoder->is_anamorphic()) {
const AVRational sample_aspect_ratio = video_decoder->sample_aspect_ratio();
if (sample_aspect_ratio.num > sample_aspect_ratio.den) {
filters.push_back("scale=iw*sar:ih");
} else {
filters.push_back("scale=iw:ih/sar");
}
}
// harmonize the frame rate to the most frames per second
if (this_frame_rate_dbl < (other_frame_rate_dbl * 0.9995)) {
filters.push_back(string_sprintf("fps=%.3f", other_frame_rate_dbl));
}
// rotation
if (demuxer->rotation() == 90) {
filters.push_back("transpose=clock");
} else if (demuxer->rotation() == 270) {
filters.push_back("transpose=cclock");
} else if (demuxer->rotation() == 180) {
filters.push_back("hflip");
filters.push_back("vflip");
} else if (demuxer->rotation() != 0) {
filters.push_back(string_sprintf("rotate=%d*PI/180", demuxer->rotation()));
}
}
// set color space and range (+ primaries and TRC if tone-mapping is required) to limited range Rec. 709 if metadata is unspecified or pass any user-provided values
const bool must_tonemap = tone_mapping_mode != ToneMapping::off;
if (!disable_auto_filters || must_tonemap || !custom_color_space.empty() || !custom_color_range.empty() || !custom_color_primaries.empty() || !custom_color_trc.empty()) {
std::vector<std::string> notes, setparams_options;
if ((video_decoder->color_space() == AVCOL_SPC_UNSPECIFIED) || !custom_color_space.empty()) {
if (custom_color_space.empty()) {
notes.push_back("'Color space' (colorspace)");
}
setparams_options.push_back("colorspace=" + (custom_color_space.empty() ? "bt709" : custom_color_space));
}
if ((video_decoder->color_range() == AVCOL_RANGE_UNSPECIFIED) || !custom_color_range.empty()) {
if (custom_color_range.empty()) {
notes.push_back("'Color range' (range)");
}
setparams_options.push_back("range=" + (custom_color_range.empty() ? "tv" : custom_color_range));
}
if ((must_tonemap && video_decoder->color_primaries() == AVCOL_PRI_UNSPECIFIED) || !custom_color_primaries.empty()) {
if (custom_color_primaries.empty()) {
notes.push_back("'Color primaries' (color_primaries)");
}
setparams_options.push_back("color_primaries=" + (custom_color_primaries.empty() ? "bt709" : custom_color_primaries));
}
if ((must_tonemap && video_decoder->color_trc() == AVCOL_TRC_UNSPECIFIED) || !custom_color_trc.empty()) {
if (custom_color_trc.empty()) {
notes.push_back("'Transfer characteristics' (color_trc)");
}
setparams_options.push_back("color_trc=" + (custom_color_trc.empty() ? "bt709" : custom_color_trc));
}
if (!notes.empty()) {
std::cout << string_sprintf("Note: Metadata is missing for %s; assuming limited range Rec. 709. It is recommended to manually set the missing properties to their correct values.", string_join(notes, ", ").c_str()) << std::endl;
}
if (!setparams_options.empty()) {
filters.push_back(string_sprintf("setparams=%s", string_join(setparams_options, ":").c_str()));
}
}
// tone-mapping
if (must_tonemap) {
const std::string display_primaries = "bt709";
const std::string display_trc = "iec61966-2-1"; // sRGB
std::vector<std::string> warnings;
if (!avfilter_get_by_name("zscale")) {
warnings.push_back("zscale filter missing in libavfilter build");
}
if (warnings.empty()) {
float tone_adjustment = (tone_mapping_mode == ToneMapping::relative && peak_luminance_nits < other_peak_luminance_nits) ? static_cast<float>(peak_luminance_nits) / other_peak_luminance_nits : 1.0F;
tone_adjustment *= boost_tone;
if (std::fabs(tone_adjustment - 1.0F) > 1e-5) {
filters.push_back("format=gbrpf32");
filters.push_back(string_sprintf("zscale=t=linear:npl=%d", peak_luminance_nits));
filters.push_back(string_sprintf("tonemap=clip:param=%.5f", tone_adjustment));
filters.push_back(string_sprintf("zscale=p=%s:t=%s", display_primaries.c_str(), display_trc.c_str()));
} else {
filters.push_back("format=rgb48");
filters.push_back(string_sprintf("zscale=p=%s:t=%s:npl=%d", display_primaries.c_str(), display_trc.c_str(), peak_luminance_nits));
}
} else {
std::cout << string_sprintf("Warning: Cannot add tone mapping filters: %s", string_join(warnings, ", ").c_str()) << std::endl;
}
}
if (!custom_post_filters.empty()) {
filters.push_back(custom_post_filters);
} else if (filters.empty()) {
filters.push_back("copy");
}
filter_description_ = string_join(filters, ",");
init();
}
VideoFilterer::~VideoFilterer() {
free();
}
void VideoFilterer::init() {
filter_graph_ = avfilter_graph_alloc();
ffmpeg::check(init_filters(video_decoder_->codec_context(), demuxer_->time_base()));
}
void VideoFilterer::free() {
avfilter_graph_free(&filter_graph_);
}
void VideoFilterer::reinit() {
free();
init();
}
int VideoFilterer::init_filters(const AVCodecContext* dec_ctx, const AVRational time_base) {
AVFilterInOut* outputs = avfilter_inout_alloc();
AVFilterInOut* inputs = avfilter_inout_alloc();
int ret = 0;
if ((outputs == nullptr) || (inputs == nullptr) || (filter_graph_ == nullptr)) {
ret = AVERROR(ENOMEM);
} else {
const int sample_aspect_ratio_den = FFMAX(dec_ctx->sample_aspect_ratio.den, 1);
const std::string args =
#if (LIBAVFILTER_VERSION_INT < AV_VERSION_INT(10, 1, 100))
string_sprintf("video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", width_, height_, pixel_format_, time_base.num, time_base.den, dec_ctx->sample_aspect_ratio.num, sample_aspect_ratio_den);
#else
string_sprintf("video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d:colorspace=%d:range=%d", width_, height_, pixel_format_, time_base.num, time_base.den, dec_ctx->sample_aspect_ratio.num, sample_aspect_ratio_den,
color_space_, color_range_);
#endif
// buffer video source: the decoded frames go here
const AVFilter* buffersrc = avfilter_get_by_name("buffer");
ret = avfilter_graph_create_filter(&buffersrc_ctx_, buffersrc, "in", args.c_str(), nullptr, filter_graph_);
if (ret < 0) {
throw ffmpeg::Error{"Cannot create buffer source"};
}
// buffer video sink: terminate the filter chain
const AVFilter* buffersink = avfilter_get_by_name("buffersink");
ret = avfilter_graph_create_filter(&buffersink_ctx_, buffersink, "out", nullptr, nullptr, filter_graph_);
if (ret < 0) {
throw ffmpeg::Error{"Cannot create buffer sink"};
}
outputs->name = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx_;
outputs->pad_idx = 0;
outputs->next = nullptr;
inputs->name = av_strdup("out");
inputs->filter_ctx = buffersink_ctx_;
inputs->pad_idx = 0;
inputs->next = nullptr;
if ((ret = avfilter_graph_parse_ptr(filter_graph_, filter_description_.c_str(), &inputs, &outputs, nullptr)) >= 0) {
ret = avfilter_graph_config(filter_graph_, nullptr);
}
}
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs);
return ret;
}
bool VideoFilterer::send(AVFrame* decoded_frame) {
if (decoded_frame != nullptr) {
bool must_reinit = false;
if (width_ != decoded_frame->width) {
width_ = decoded_frame->width;
must_reinit = true;
}
if (height_ != decoded_frame->height) {
height_ = decoded_frame->height;
must_reinit = true;
}
if (pixel_format_ != decoded_frame->format) {
if (decoded_frame->format == AV_PIX_FMT_NONE) {
throw ffmpeg::Error{"Decoded frame with invalid pixel format received"};
}
pixel_format_ = static_cast<AVPixelFormat>(decoded_frame->format);
must_reinit = true;
}
if (color_space_ != decoded_frame->colorspace) {
color_space_ = static_cast<AVColorSpace>(decoded_frame->colorspace);
must_reinit = true;
}
if (color_range_ != decoded_frame->color_range) {
color_range_ = static_cast<AVColorRange>(decoded_frame->color_range);
must_reinit = true;
}
if (must_reinit) {
reinit();
}
}
return av_buffersrc_add_frame_flags(buffersrc_ctx_, decoded_frame, AV_BUFFERSRC_FLAG_KEEP_REF) >= 0;
}
void VideoFilterer::close_src() {
av_buffersrc_close(buffersrc_ctx_, video_decoder_->next_pts(), AV_BUFFERSRC_FLAG_PUSH);
}
bool VideoFilterer::receive(AVFrame* filtered_frame) {
auto ret = av_buffersink_get_frame_flags(buffersink_ctx_, filtered_frame, 0);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return false;
}
ffmpeg::check(ret);
// convert PTS and duration to microseconds
filtered_frame->pts = av_rescale_q(filtered_frame->pts, av_buffersink_get_time_base(buffersink_ctx_), AV_R_MICROSECONDS) - demuxer_->start_time();
ffmpeg::frame_duration(filtered_frame) = av_rescale_q(ffmpeg::frame_duration(filtered_frame), demuxer_->time_base(), AV_R_MICROSECONDS);
return true;
}
std::string VideoFilterer::filter_description() const {
return filter_description_;
}
size_t VideoFilterer::src_width() const {
return buffersrc_ctx_->outputs[0]->w;
}
size_t VideoFilterer::src_height() const {
return buffersrc_ctx_->outputs[0]->h;
}
AVPixelFormat VideoFilterer::src_pixel_format() const {
return static_cast<AVPixelFormat>(buffersrc_ctx_->outputs[0]->format);
}
size_t VideoFilterer::dest_width() const {
return buffersink_ctx_->inputs[0]->w;
}
size_t VideoFilterer::dest_height() const {
return buffersink_ctx_->inputs[0]->h;
}
AVPixelFormat VideoFilterer::dest_pixel_format() const {
return static_cast<AVPixelFormat>(buffersink_ctx_->inputs[0]->format);
}