| 1 |
/* |
| 2 |
* Copyright (c) 2010-2023 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-decoder-filter.h" |
| 22 |
|
| 23 |
#include "filter-wrapper/decoding-filter-wrapper.h" |
| 24 |
|
| 25 |
using namespace std; |
| 26 |
|
| 27 |
namespace mediastreamer { |
| 28 |
|
| 29 |
Av1DecoderFilter::Av1DecoderFilter(MSFilter *f, Av1Decoder *codec) |
| 30 |
: DecoderFilter(f), mVsize{0, 0}, mCodec(codec), mUnpacker() { |
| 31 |
ms_average_fps_init(&mFps, " Av1DecoderFilter: FPS: %f"); |
| 32 |
} |
| 33 |
|
| 34 |
void Av1DecoderFilter::preprocess() { |
| 35 |
} |
| 36 |
|
| 37 |
void Av1DecoderFilter::process() { |
| 38 |
bool requestPli = false; |
| 39 |
|
| 40 |
if (mCodec == nullptr) { |
| 41 |
ms_queue_flush(getInput(0)); |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
MSQueue frame; |
| 46 |
ms_queue_init(&frame); |
| 47 |
|
| 48 |
while (mblk_t *im = ms_queue_get(getInput(0))) { |
| 49 |
ObuUnpacker::Status ret = mUnpacker.unpack(im, &frame); |
| 50 |
|
| 51 |
if (ret == ObuUnpacker::Status::NoFrame) continue; |
| 52 |
else if (ret == ObuUnpacker::Status::FrameCorrupted) { |
| 53 |
ms_warning("Av1DecoderFilter: corrupted frame."); |
| 54 |
requestPli = true; |
| 55 |
if (mFreezeOnError) { |
| 56 |
ms_queue_flush(&frame); |
| 57 |
mCodec->waitForKeyFrame(); |
| 58 |
continue; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if (!mCodec->feed(&frame, ms_get_cur_time_ms())) requestPli = true; |
| 63 |
|
| 64 |
if (requestPli && mFreezeOnError) { |
| 65 |
/* In freeze on error mode, regardless of the decoding failure cause, we must restart with a key-frame. */ |
| 66 |
mCodec->waitForKeyFrame(); |
| 67 |
} |
| 68 |
|
| 69 |
ms_queue_flush(&frame); |
| 70 |
} |
| 71 |
|
| 72 |
mblk_t *om; |
| 73 |
VideoDecoder::Status status; |
| 74 |
while ((status = mCodec->fetch(om)) != VideoDecoder::Status::NoFrameAvailable) { |
| 75 |
if (status == VideoDecoder::DecodingFailure) { |
| 76 |
ms_error("Av1DecoderFilter: decoding failure"); |
| 77 |
requestPli = true; |
| 78 |
ms_average_fps_activity(&mFps, getTime(), FALSE); |
| 79 |
continue; |
| 80 |
} |
| 81 |
if (!om) { |
| 82 |
ms_warning("Av1DecoderFilter: no frame."); |
| 83 |
ms_average_fps_activity(&mFps, getTime(), FALSE); |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
if (!mFirstImageDecoded) { |
| 88 |
MSPicture pic; |
| 89 |
|
| 90 |
ms_yuv_buf_init_from_mblk(&pic, om); |
| 91 |
mVsize.width = pic.w; |
| 92 |
mVsize.height = pic.h; |
| 93 |
|
| 94 |
ms_message("Av1DecoderFilter: first frame decoded %ix%i", mVsize.width, mVsize.height); |
| 95 |
|
| 96 |
mFirstImageDecoded = true; |
| 97 |
notify(MS_VIDEO_DECODER_FIRST_IMAGE_DECODED); |
| 98 |
} |
| 99 |
|
| 100 |
ms_average_fps_activity(&mFps, getTime(), TRUE); |
| 101 |
ms_queue_put(getOutput(0), om); |
| 102 |
} |
| 103 |
|
| 104 |
if (requestPli) { |
| 105 |
notify(mAvpfEnabled ? MS_VIDEO_DECODER_SEND_PLI : MS_VIDEO_DECODER_DECODING_ERRORS); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void Av1DecoderFilter::postprocess() { |
| 110 |
} |
| 111 |
|
| 112 |
void Av1DecoderFilter::resetFirstImage() { |
| 113 |
mFirstImageDecoded = false; |
| 114 |
} |
| 115 |
|
| 116 |
MSVideoSize Av1DecoderFilter::getVideoSize() const { |
| 117 |
return mFirstImageDecoded ? mVsize : MS_VIDEO_SIZE_UNKNOWN; |
| 118 |
} |
| 119 |
|
| 120 |
float Av1DecoderFilter::getFps() const { |
| 121 |
return ms_average_fps_get(&mFps); |
| 122 |
} |
| 123 |
|
| 124 |
const MSFmtDescriptor *Av1DecoderFilter::getOutputFmt() const { |
| 125 |
return ms_factory_get_video_format(getFactory(), "YUV420P", ms_video_size_make(mVsize.width, mVsize.height), 0, |
| 126 |
nullptr); |
| 127 |
} |
| 128 |
|
| 129 |
void Av1DecoderFilter::enableAvpf(bool enable) { |
| 130 |
mAvpfEnabled = enable; |
| 131 |
ms_message("Av1DecoderFilter: %s AVPF mode", mAvpfEnabled ? "enabling" : "disabling"); |
| 132 |
} |
| 133 |
|
| 134 |
void Av1DecoderFilter::enableFreezeOnError(bool enable) { |
| 135 |
mFreezeOnError = enable; |
| 136 |
ms_message("Av1DecoderFilter: freeze on error %s", mFreezeOnError ? "enabled" : "disabled"); |
| 137 |
} |
| 138 |
|
| 139 |
class Av1DecoderFilterImpl : public Av1DecoderFilter { |
| 140 |
public: |
| 141 |
Av1DecoderFilterImpl(MSFilter *f) : Av1DecoderFilter(f, new Av1Decoder()) { |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
} // namespace mediastreamer |
| 146 |
|
| 147 |
using namespace mediastreamer; |
| 148 |
|
| 149 |
MS_DECODING_FILTER_WRAPPER_METHODS_DECLARATION(Av1Decoder); |
| 150 |
MS_DECODING_FILTER_WRAPPER_DESCRIPTION_DECLARATION( |
| 151 |
Av1Decoder, MS_AV1_DEC_ID, "An Av1 decoder based on dav1d.", "AV1", MS_FILTER_IS_PUMP); |
| 152 |
|
| 153 |
MS_FILTER_DESC_EXPORT(ms_Av1Decoder_desc) |