Debian Codegraph @

src/videofilters/h26x/h26x-decoder-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 "h26x-utils.h"
22
23 #include "h26x-decoder-filter.h"
24
25 using namespace std;
26
27 namespace mediastreamer {
28
29 H26xDecoderFilter::H26xDecoderFilter(MSFilter *f, H26xDecoder *decoder)
30 : DecoderFilter(f), _vsize{0, 0}, _unpacker(H26xToolFactory::get(decoder->getMime()).createNalUnpacker()),
31 _codec(decoder) {
32
33 ms_average_fps_init(&_fps, " H26x decoder: FPS: %f");
34 }
35
36 void H26xDecoderFilter::preprocess() {
37 _regulator = ms_stream_regulator_new(getTicker(), 90000);
38 }
39
40 void H26xDecoderFilter::process() {
41 bool requestPli = false;
42 int decodedFramesCount = 0;
43 int regulatorPendingCount = 0;
44 MSQueue frame;
45
46 if (_codec == nullptr) {
47 ms_queue_flush(getInput(0));
48 return;
49 }
50
51 ms_queue_init(&frame);
52
53 TimeReport feeding("H26x feeding");
54 while (mblk_t *im = ms_queue_get(getInput(0))) {
55 NalUnpacker::Status unpacking_ret = _unpacker->unpack(im, &frame);
56
57 if (!unpacking_ret.frameAvailable) continue;
58
59 if (unpacking_ret.frameCorrupted) {
60 ms_warning("H26xDecoder: corrupted frame");
61 requestPli = true;
62 if (_freezeOnError) {
63 /*in freeze on error mode we must not supply a corrupted frame to the decoder. */
64 ms_queue_flush(&frame);
65 _codec->waitForKeyFrame();
66 continue;
67 }
68 }
69 /*
70 * Feed the decoder implementation with the full frame, which is supposed to be complete.
71 * If a key-frame is requested, feed() will return false.
72 * In case of feeding error (such as too many buffers queued), we will request a PLI.
73 * Otherwise, if feed() succeeds, it means that decoding is on its way.
74 */
75 requestPli = !_codec->feed(&frame, ms_get_cur_time_ms());
76
77 if (requestPli && _freezeOnError) {
78 /* In freeze on error mode, regardless of the decoding failure cause, we must restart with a key-frame. */
79 _codec->waitForKeyFrame();
80 }
81
82 ms_queue_flush(&frame);
83 }
84 feeding.finished();
85
86 mblk_t *om = nullptr;
87 VideoDecoder::Status status;
88
89 MSQueue q;
90 ms_queue_init(&q);
91
92 if (_useRegulator) {
93 regulatorPendingCount = ms_stream_regulator_get_pending_buffers_count(_regulator);
94 // if (regulatorPendingCount > 0) ms_message("H26xDecoder: %i frames pending in StreamRegulator",
95 // regulatorPendingCount);
96 }
97 TimeReport fetching("H26x fetching");
98 while ((status = _codec->fetch(om)) != VideoDecoder::Status::NoFrameAvailable) {
99 if (status == VideoDecoder::DecodingFailure) {
100 ms_error("H26xDecoder: decoding failure");
101 requestPli = true;
102 continue;
103 }
104 decodedFramesCount++;
105 ms_queue_put(&q, om);
106 om = nullptr;
107 }
108 fetching.finished();
109 if (decodedFramesCount >= 10) {
110 if (!_useRegulator) {
111 ms_warning("H26xDecoder: [%i] frames decoded in a row - non real-time MediaCodec decoding detected. "
112 "Will now switch to StreamRegulator to smooth frame rendering based on presentation timestamps.",
113 decodedFramesCount);
114 _useRegulator = true;
115 } else if (regulatorPendingCount > 0) {
116 ms_warning(
117 "H26xDecoder: [%i] frames pending in regulator but new frames are decoded. Resynchonisation needed.",
118 regulatorPendingCount);
119 ms_stream_regulator_reset(_regulator);
120 }
121 }
122 if (_useRegulator) {
123 /* splice frames into the regulator's queue. */
124 while ((om = ms_queue_get(&q)) != nullptr) {
125 ms_stream_regulator_push(_regulator, om);
126 }
127 }
128
129 bool_t haveFrame = FALSE;
130 while ((_useRegulator && (om = ms_stream_regulator_get(_regulator)) != nullptr) ||
131 (!_useRegulator && (om = ms_queue_get(&q)) != nullptr)) {
132 MSPicture pic;
133 ms_yuv_buf_init_from_mblk(&pic, om);
134 _vsize.width = pic.w;
135 _vsize.height = pic.h;
136
137 if (!_firstImageDecoded) {
138 ms_message("H26xDecoder: first frame decoded %ix%i", _vsize.width, _vsize.height);
139 _firstImageDecoded = true;
140 notify(MS_VIDEO_DECODER_FIRST_IMAGE_DECODED);
141 }
142 haveFrame = TRUE;
143 ms_queue_put(getOutput(0), om);
144 om = nullptr;
145 }
146 ms_average_fps_activity(&_fps, getTime(), haveFrame);
147 if (requestPli) {
148 notify(_avpfEnabled ? MS_VIDEO_DECODER_SEND_PLI : MS_VIDEO_DECODER_DECODING_ERRORS);
149 }
150 }
151
152 void H26xDecoderFilter::postprocess() {
153 ms_stream_regulator_free(_regulator);
154 }
155
156 void H26xDecoderFilter::resetFirstImage() {
157 _firstImageDecoded = false;
158 }
159
160 MSVideoSize H26xDecoderFilter::getVideoSize() const {
161 return _firstImageDecoded ? _vsize : MS_VIDEO_SIZE_UNKNOWN;
162 }
163
164 float H26xDecoderFilter::getFps() const {
165 return ms_average_fps_get(&_fps);
166 }
167
168 const MSFmtDescriptor *H26xDecoderFilter::getOutputFmt() const {
169 return ms_factory_get_video_format(getFactory(), "YUV420P", ms_video_size_make(_vsize.width, _vsize.height), 0,
170 nullptr);
171 }
172
173 void H26xDecoderFilter::enableAvpf(bool enable) {
174 ms_message("H26xDecoder: %s AVPF mode", enable ? "enabling" : "disabling");
175 _avpfEnabled = enable;
176 }
177
178 void H26xDecoderFilter::enableFreezeOnError(bool enable) {
179 _freezeOnError = enable;
180 ms_message("H26xDecoder: freeze on error %s", _freezeOnError ? "enabled" : "disabled");
181 }
182
183 } // namespace mediastreamer