Debian Codegraph @

src/videofilters/av1/decoder/av1-decoder.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-decoder.h"
22
23 using namespace mediastreamer;
24 using namespace std;
25
26 namespace mediastreamer {
27
28 Av1Decoder::Av1Decoder() : mKeyFrameIndicator() {
29 dav1d_default_settings(&mSettings);
30
31 mSettings.n_threads = 2;
32 mSettings.max_frame_delay = 1;
33
34 // Disable dav1d internal logging
35 mSettings.logger.callback = nullptr;
36
37 int res = 0;
38 if ((res = dav1d_open(&mContext, &mSettings))) {
39 ms_message("Av1Decoder: failed opening dav1d decoder %d", res);
40 }
41
42 mYuvBufAllocator = ms_yuv_buf_allocator_new();
43 }
44
45 Av1Decoder::~Av1Decoder() {
46 while (!mPendingFrames.empty()) {
47 Dav1dPicture &p = mPendingFrames.front();
48 dav1d_picture_unref(&p);
49 mPendingFrames.pop();
50 }
51
52 if (mContext) {
53 dav1d_flush(mContext);
54 dav1d_close(&mContext);
55 mContext = nullptr;
56 }
57
58 if (mYuvBufAllocator) ms_yuv_buf_allocator_free(mYuvBufAllocator);
59 }
60
61 static void releaseDecoderBuffer([[maybe_unused]] const uint8_t *buffer, void *opaque) {
62 if (opaque) freemsg(static_cast<mblk_t *>(opaque));
63 }
64
65 bool Av1Decoder::feed(MSQueue *encodedFrame, uint64_t timestamp) {
66 if (mContext == nullptr) {
67 ms_message("Av1Decoder: Trying to feed frames to decoder but decoder is not started");
68 ms_queue_flush(encodedFrame);
69 return true;
70 }
71
72 Dav1dData data{};
73 mblk_t *im;
74
75 // Keep going in case data cannot be consumed so we won't loose it
76 while (data.sz != 0 || (im = ms_queue_get(encodedFrame))) {
77 if (data.sz == 0) {
78 if (mKeyFrameNeeded) {
79 if (mKeyFrameIndicator.parseFrame(im)) {
80 ms_message("Av1Decoder: key frame received");
81 mKeyFrameNeeded = false;
82 mKeyFrameIndicator.reset();
83 } else {
84 ms_message("Av1Decoder: waiting for key frame");
85 freemsg(im);
86 continue;
87 }
88 }
89
90 if (dav1d_data_wrap(&data, im->b_rptr, msgdsize(im), releaseDecoderBuffer, im)) {
91 ms_message("Av1Decoder: Failed to wrap data");
92 freemsg(im);
93 return false;
94 }
95 data.m.timestamp = (int64_t)timestamp;
96 }
97
98 if (data.sz) {
99 int res = dav1d_send_data(mContext, &data);
100
101 if (res < 0) {
102 // EAGAIN means data cannot be consumed and we should retrieve frames
103 if (res == DAV1D_ERR(EAGAIN)) {
104 Dav1dPicture p{};
105
106 while ((res = dav1d_get_picture(mContext, &p)) == 0) {
107 mPendingFrames.push(std::move(p));
108 p = {};
109 }
110
111 if (res != DAV1D_ERR(EAGAIN)) {
112 ms_error("Av1Decoder: error during decoding");
113 }
114 } else {
115 ms_message("Av1Decoder: Error while sending data");
116 dav1d_data_unref(&data);
117 return false;
118 }
119 }
120 }
121 }
122
123 return !mKeyFrameNeeded;
124 }
125
126 VideoDecoder::Status Av1Decoder::fetch(mblk_t *&frame) {
127 Dav1dPicture p{};
128
129 if (!mPendingFrames.empty()) {
130 p = std::move(mPendingFrames.front());
131 mPendingFrames.pop();
132 } else {
133 int res = dav1d_get_picture(mContext, &p);
134
135 if (res < 0) {
136 if (res == DAV1D_ERR(EAGAIN)) {
137 return VideoDecoder::Status::NoFrameAvailable;
138 } else {
139 ms_error("Av1Decoder: error during decoding");
140 return VideoDecoder::Status::DecodingFailure;
141 }
142 }
143 }
144
145 if (p.p.bpc != 8) {
146 ms_error("Av1Decoder: bpc is not 8, which is not supported yet");
147 dav1d_picture_unref(&p);
148 return VideoDecoder::Status::DecodingFailure;
149 }
150
151 int rowStrides[3] = {static_cast<int>(p.stride[0]), static_cast<int>(p.stride[1]), static_cast<int>(p.stride[1])};
152 MSPicture pic;
153
154 frame = ms_yuv_buf_allocator_get(mYuvBufAllocator, &pic, p.p.w, p.p.h);
155 ms_yuv_buf_copy((uint8_t **)(&p.data), rowStrides, pic.planes, pic.strides, {p.p.w, p.p.h});
156
157 dav1d_picture_unref(&p);
158
159 return VideoDecoder::Status::NoError;
160 }
161
162 void Av1Decoder::flush() {
163 while (!mPendingFrames.empty()) {
164 Dav1dPicture &p = mPendingFrames.front();
165 dav1d_picture_unref(&p);
166 mPendingFrames.pop();
167 }
168
169 dav1d_flush(mContext);
170 mKeyFrameIndicator.reset();
171 }
172
173 } // namespace mediastreamer