Debian Codegraph @

src/videofilters/av1/encoder/av1-encoder-filter.cpp

raw
1 /*
2 * Copyright (c) 2010-2022 Belledonne Communications SARL.
3 *
4 * This file is part of mediastreamer2
5 * (see https://gitlab.linphone.org/BC/public/mediastreamer2).
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program 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
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "av1-encoder-filter.h"
22
23 #include "filter-wrapper/encoding-filter-wrapper.h"
24
25 #ifdef __APPLE__
26 #include <TargetConditionals.h>
27 #endif
28
29 using namespace std;
30
31 #define MS_AV1_CONF(required_bitrate, bitrate_limit, resolution, fps, ncpus) \
32 { \
33 required_bitrate, bitrate_limit, {MS_VIDEO_SIZE_##resolution##_W, MS_VIDEO_SIZE_##resolution##_H}, fps, ncpus, \
34 nullptr \
35 }
36
37 static const MSVideoConfiguration _av1_conf_list[] = {
38 // High resolutions are for now too demanding for the encoding software on mobile phones.
39 #if !defined(ANDROID) && !defined(TARGET_OS_IPHONE)
40 MS_AV1_CONF(1024000, 2048000, 720P, 30, 8),
41 #endif
42 // MS_AV1_CONF(850000, 2048000, XGA, 25, 4),
43 MS_AV1_CONF(400000, 1500000, VGA, 30, 2),
44 MS_AV1_CONF(128000, 512000, CIF, 30, 1),
45 MS_AV1_CONF(0, 170000, QVGA, 15, 1),
46 };
47
48 namespace mediastreamer {
49
50 Av1EncoderFilter::Av1EncoderFilter(MSFilter *f, Av1Encoder *encoder, const MSVideoConfiguration *vconfList)
51 : EncoderFilter(f), mEncoder(encoder), mPacker(ms_factory_get_payload_max_size(f->factory)), mVconfList(vconfList) {
52
53 mVconf =
54 ms_video_find_best_configuration_for_size(mVconfList, MS_VIDEO_SIZE_CIF, ms_factory_get_cpu_count(f->factory));
55 ms_video_starter_init(&mStarter);
56 }
57
58 void Av1EncoderFilter::preprocess() {
59 mEncoder->configure(mAvpfEnabled);
60 mEncoder->start();
61 ms_video_starter_init(&mStarter);
62 ms_iframe_requests_limiter_init(&mIframeLimiter, 1000);
63 }
64
65 void Av1EncoderFilter::process() {
66 /*First queue input image*/
67 if (mblk_t *im = ms_queue_peek_last(getInput(0))) {
68 bool requestIFrame = false;
69 if (ms_iframe_requests_limiter_iframe_requested(&mIframeLimiter, getTime()) ||
70 (!mAvpfEnabled && ms_video_starter_need_i_frame(&mStarter, getTime()))) {
71 ms_message("Av1EncoderFilter: requesting I-frame to the encoder.");
72 requestIFrame = true;
73 ms_iframe_requests_limiter_notify_iframe_sent(&mIframeLimiter, getTime());
74 }
75 mEncoder->feed(dupmsg(im), getTime(), requestIFrame);
76 }
77 ms_queue_flush(getInput(0));
78
79 /*Second, dequeue possibly pending encoded frames*/
80 MSQueue encodedFrames;
81 ms_queue_init(&encodedFrames);
82 while (mEncoder->fetch(&encodedFrames)) {
83 if (!mFirstFrameEncoded) {
84 mFirstFrameEncoded = true;
85 ms_video_starter_first_frame(&mStarter, getTime());
86 }
87
88 mPacker.pack(&encodedFrames, getOutput(0), static_cast<uint32_t>(getTime() * 90LL));
89 }
90 }
91
92 void Av1EncoderFilter::postprocess() {
93 mEncoder->stop();
94 mFirstFrameEncoded = false;
95 }
96
97 void Av1EncoderFilter::setVideoConfiguration(MSVideoConfiguration vconf) {
98 ostringstream os{};
99 os << "bitrate=" << vconf.required_bitrate << "b/s, fps=" << vconf.fps << ", vsize=" << vconf.vsize.width << "x"
100 << vconf.vsize.height;
101
102 try {
103 if (mEncoder->isRunning() && !ms_video_size_equal(mVconf.vsize, vconf.vsize)) {
104 ms_warning("Av1EncoderFilter: ignoring video size change because the encoder is started");
105 vconf.vsize = mEncoder->getVideoSize();
106 } else {
107 mEncoder->setVideoSize(vconf.vsize);
108 }
109 mEncoder->setFps(vconf.fps);
110 mEncoder->setBitrate(vconf.required_bitrate);
111 mVconf = vconf;
112 ms_message("Av1EncoderFilter: video configuration set (%s)", os.str().c_str());
113 } catch (const runtime_error &e) {
114 ms_error("Av1EncoderFilter: video configuration failed (%s): %s", os.str().c_str(), e.what());
115 }
116 }
117
118 void Av1EncoderFilter::enableAvpf(bool enable) {
119 ms_message("Av1EncoderFilter: AVPF %s", enable ? "enabled" : "disabled");
120 mAvpfEnabled = enable;
121 }
122
123 void Av1EncoderFilter::requestVfu() {
124 ms_message("Av1EncoderFilter: VFU requested");
125 ms_iframe_requests_limiter_request_iframe(&mIframeLimiter);
126 }
127
128 void Av1EncoderFilter::notifyPli() {
129 ms_message("Av1EncoderFilter: PLI requested");
130 ms_iframe_requests_limiter_request_iframe(&mIframeLimiter);
131 }
132
133 void Av1EncoderFilter::notifyFir() {
134 ms_message("Av1EncoderFilter: FIR requested");
135 ms_iframe_requests_limiter_request_iframe(&mIframeLimiter);
136 }
137
138 void Av1EncoderFilter::notifySli() {
139 ms_message("Av1EncoderFilter: SLI requested");
140 ms_iframe_requests_limiter_request_iframe(&mIframeLimiter);
141 }
142
143 const MSVideoConfiguration *Av1EncoderFilter::getVideoConfigurations() const {
144 return mVconfList;
145 }
146
147 class Av1EncoderFilterImpl : public Av1EncoderFilter {
148 public:
149 Av1EncoderFilterImpl(MSFilter *f) : Av1EncoderFilter(f, new Av1Encoder(f->factory), _av1_conf_list) {
150 }
151 };
152
153 } // namespace mediastreamer
154
155 using namespace mediastreamer;
156
157 MS_ENCODING_FILTER_WRAPPER_METHODS_DECLARATION(Av1Encoder);
158 MS_ENCODING_FILTER_WRAPPER_DESCRIPTION_DECLARATION(
159 Av1Encoder, MS_AV1_ENC_ID, "An AV1 encoder based on aom.", "AV1", MS_FILTER_IS_PUMP);
160
161 MS_FILTER_DESC_EXPORT(ms_Av1Encoder_desc)