]> git.sesse.net Git - nageru/blob - nageru/av1_encoder.cpp
a0dbdd1a8e17a4e3297a42fa32c67af2bf312ec9
[nageru] / nageru / av1_encoder.cpp
1 #include "av1_encoder.h"
2
3 #include <assert.h>
4 #include <dlfcn.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <atomic>
10 #include <cstdint>
11 #include <functional>
12 #include <mutex>
13
14 #include <EbSvtAv1.h>
15 #include <EbSvtAv1Enc.h>
16
17 #include "defs.h"
18 #include "flags.h"
19 #include "shared/metrics.h"
20 #include "shared/mux.h"
21 #include "print_latency.h"
22 #include "shared/timebase.h"
23 #include "shared/memcpy_interleaved.h"
24
25 extern "C" {
26 #include <libavcodec/avcodec.h>
27 #include <libavformat/avformat.h>
28 }
29
30 using namespace movit;
31 using namespace std;
32 using namespace std::chrono;
33 using namespace std::placeholders;
34
35 namespace {
36
37 // AV1Encoder can be restarted if --record-av1-video is set, so make these
38 // metrics global.
39 atomic<int64_t> metric_av1_queued_frames{0};
40 atomic<int64_t> metric_av1_max_queued_frames{AV1_QUEUE_LENGTH};
41 atomic<int64_t> metric_av1_dropped_frames{0};
42 atomic<int64_t> metric_av1_output_frames_i{0};
43 atomic<int64_t> metric_av1_output_frames_p{0};
44 Histogram metric_av1_qp;
45 LatencyHistogram av1_latency_histogram;
46
47 once_flag av1_metrics_inited;
48
49 }  // namespace
50
51 AV1Encoder::AV1Encoder(const AVOutputFormat *oformat)
52         : wants_global_headers(oformat->flags & AVFMT_GLOBALHEADER)
53 {
54                 call_once(av1_metrics_inited, []{
55                         global_metrics.add("av1_queued_frames",  {}, &metric_av1_queued_frames, Metrics::TYPE_GAUGE);
56                         global_metrics.add("av1_max_queued_frames", {},  &metric_av1_max_queued_frames, Metrics::TYPE_GAUGE);
57                         global_metrics.add("av1_dropped_frames", {},  &metric_av1_dropped_frames);
58                         global_metrics.add("av1_output_frames", {{ "type", "i" }}, &metric_av1_output_frames_i);
59                         global_metrics.add("av1_output_frames", {{ "type", "p" }}, &metric_av1_output_frames_p);
60
61                         metric_av1_qp.init_uniform(50);
62                         global_metrics.add("av1_qp", {}, &metric_av1_qp);
63                         av1_latency_histogram.init("av1");
64                 });
65
66         const size_t bytes_per_pixel = 1;  // TODO: 10-bit support.
67         frame_pool.reset(new uint8_t[global_flags.width * global_flags.height * 2 * bytes_per_pixel * AV1_QUEUE_LENGTH]);
68         for (unsigned i = 0; i < AV1_QUEUE_LENGTH; ++i) {
69                 free_frames.push(frame_pool.get() + i * (global_flags.width * global_flags.height * 2 * bytes_per_pixel));
70         }
71         encoder_thread = thread(&AV1Encoder::encoder_thread_func, this);
72 }
73
74 AV1Encoder::~AV1Encoder()
75 {
76         should_quit = true;
77         queued_frames_nonempty.notify_all();
78         encoder_thread.join();
79 }
80
81 void AV1Encoder::add_frame(int64_t pts, int64_t duration, YCbCrLumaCoefficients ycbcr_coefficients, const uint8_t *data, const ReceivedTimestamps &received_ts)
82 {
83         assert(!should_quit);
84
85         QueuedFrame qf;
86         qf.pts = pts;
87         qf.duration = duration;
88         qf.ycbcr_coefficients = ycbcr_coefficients;
89         qf.received_ts = received_ts;
90
91         {
92                 lock_guard<mutex> lock(mu);
93                 if (free_frames.empty()) {
94                         fprintf(stderr, "WARNING: AV1 queue full, dropping frame with pts %" PRId64 "\n", pts);
95                         ++metric_av1_dropped_frames;
96                         return;
97                 }
98
99                 qf.data = free_frames.front();
100                 free_frames.pop();
101         }
102
103         // Since we're copying anyway, we can unpack from NV12 to fully planar on the fly.
104         // SVT-AV1 makes its own copy, though, and it would have been nice to avoid the
105         // double-copy.
106         size_t bytes_per_pixel = 1;  // TODO: 10-bit support.
107         size_t frame_size = global_flags.width * global_flags.height * bytes_per_pixel;
108         assert(global_flags.width % 2 == 0);
109         assert(global_flags.height % 2 == 0);
110         uint8_t *y = qf.data;   
111         uint8_t *cb = y + frame_size;
112         uint8_t *cr = cb + frame_size / 4;
113         memcpy(y, data, frame_size);
114         memcpy_interleaved(cb, cr, data + frame_size, frame_size / 2);
115
116         {
117                 lock_guard<mutex> lock(mu);
118                 queued_frames.push(qf);
119                 queued_frames_nonempty.notify_all();
120                 metric_av1_queued_frames = queued_frames.size();
121         }
122 }
123         
124 void AV1Encoder::init_av1()
125 {
126         EbSvtAv1EncConfiguration config;
127         EbErrorType ret = svt_av1_enc_init_handle(&encoder, nullptr, &config);
128         if (ret != EB_ErrorNone) {
129                 fprintf(stderr, "Error initializing SVT-AV1 handle (error %08x)\n", ret);
130                 exit(EXIT_FAILURE);
131         }
132
133         config.enc_mode = global_flags.av1_preset;
134         config.intra_period_length = 63;  // Approx. one second, conforms to the (n % 8) - 1 == 0 rule.
135         config.source_width = global_flags.width;
136         config.source_height = global_flags.height;
137         config.frame_rate_numerator = global_flags.av1_fps_num;
138         config.frame_rate_denominator = global_flags.av1_fps_den;
139         config.encoder_bit_depth = 8;  // TODO: 10-bit support.
140         config.rate_control_mode = 2;  // CBR.
141         config.pred_structure = 1;  // PRED_LOW_DELAY_B (needed for CBR).
142         config.target_bit_rate = global_flags.av1_bitrate * 1000;
143
144         // NOTE: These should be in sync with the ones in quicksync_encoder.cpp (sps_rbsp()).
145         config.color_primaries = EB_CICP_CP_BT_709;
146         config.transfer_characteristics = EB_CICP_TC_SRGB;
147         if (global_flags.ycbcr_rec709_coefficients) {
148                 config.matrix_coefficients = EB_CICP_MC_BT_709;
149         } else {
150                 config.matrix_coefficients = EB_CICP_MC_BT_601;
151         }
152         config.color_range = EB_CR_STUDIO_RANGE;
153 #if SVT_AV1_CHECK_VERSION(1, 0, 0)
154         config.chroma_sample_position = EB_CSP_VERTICAL;
155 #endif
156
157         const vector<string> &extra_param = global_flags.av1_extra_param;
158         for (const string &str : extra_param) {
159                 const size_t pos = str.find(',');
160                 if (pos == string::npos) {
161                         if (svt_av1_enc_parse_parameter(&config, str.c_str(), nullptr) != EB_ErrorNone) {
162                                 fprintf(stderr, "ERROR: SVT-AV1 rejected parameter '%s' with no value\n", str.c_str());
163                                 exit(EXIT_FAILURE);
164                         }
165                 } else {
166                         const string key = str.substr(0, pos);
167                         const string value = str.substr(pos + 1);
168                         if (svt_av1_enc_parse_parameter(&config, key.c_str(), value.c_str()) != EB_ErrorNone) {
169                                 fprintf(stderr, "ERROR: SVT-AV1 rejected parameter '%s' set to '%s'\n",
170                                         key.c_str(), value.c_str());
171                                 exit(EXIT_FAILURE);
172                         }
173                 }
174         }
175         
176         ret = svt_av1_enc_set_parameter(encoder, &config);
177         if (ret != EB_ErrorNone) {
178                 fprintf(stderr, "Error configuring SVT-AV1 (error %08x)\n", ret);
179                 exit(EXIT_FAILURE);
180         }
181
182         ret = svt_av1_enc_init(encoder);
183         if (ret != EB_ErrorNone) {
184                 fprintf(stderr, "Error initializing SVT-AV1 (error %08x)\n", ret);
185                 exit(EXIT_FAILURE);
186         }
187
188         if (wants_global_headers) {
189                 EbBufferHeaderType *header = NULL;
190
191                 ret = svt_av1_enc_stream_header(encoder, &header);
192                 if (ret != EB_ErrorNone) {
193                         fprintf(stderr, "Error building SVT-AV1 header (error %08x)\n", ret);
194                         exit(EXIT_FAILURE);
195                 }
196                 
197                 global_headers = string(reinterpret_cast<const char *>(header->p_buffer), header->n_filled_len);
198
199                 svt_av1_enc_stream_header_release(header);  // Don't care about errors.
200           }
201 }
202
203 void AV1Encoder::encoder_thread_func()
204 {
205         if (nice(5) == -1) {
206                 perror("nice()");
207                 // No exit; it's not fatal.
208         }
209         pthread_setname_np(pthread_self(), "AV1_encode");
210         init_av1();
211         av1_init_done = true;
212
213         bool frames_left;
214
215         do {
216                 QueuedFrame qf;
217
218                 // Wait for a queued frame, then dequeue it.
219                 {
220                         unique_lock<mutex> lock(mu);
221                         queued_frames_nonempty.wait(lock, [this]() { return !queued_frames.empty() || should_quit; });
222                         if (!queued_frames.empty()) {
223                                 qf = queued_frames.front();
224                                 queued_frames.pop();
225                         } else {
226                                 qf.pts = -1;
227                                 qf.duration = -1;
228                                 qf.data = nullptr;
229                         }
230
231                         metric_av1_queued_frames = queued_frames.size();
232                         frames_left = !queued_frames.empty();
233                 }
234
235                 encode_frame(qf);
236                 
237                 {
238                         lock_guard<mutex> lock(mu);
239                         free_frames.push(qf.data);
240                 }
241
242                 // We should quit only if the should_quit flag is set _and_ we have nothing
243                 // in our queue.
244         } while (!should_quit || frames_left);
245
246         // Signal end of stream.
247         EbBufferHeaderType hdr;
248         hdr.n_alloc_len   = 0;
249         hdr.n_filled_len  = 0;
250         hdr.n_tick_count  = 0;
251         hdr.p_app_private = nullptr;
252         hdr.pic_type      = EB_AV1_INVALID_PICTURE;
253         hdr.p_buffer      = nullptr;
254         hdr.metadata      = nullptr;
255         hdr.flags         = EB_BUFFERFLAG_EOS;
256         svt_av1_enc_send_picture(encoder, &hdr);
257
258         bool seen_eof = false;
259         do {
260                 EbBufferHeaderType *buf;
261                 EbErrorType ret = svt_av1_enc_get_packet(encoder, &buf, /*pic_send_done=*/true);
262                 if (ret == EB_NoErrorEmptyQueue) {
263                         assert(false);
264                 }
265                 seen_eof = (buf->flags & EB_BUFFERFLAG_EOS);
266                 process_packet(buf);
267         } while (!seen_eof);
268
269         svt_av1_enc_deinit(encoder);
270         svt_av1_enc_deinit_handle(encoder);
271 }
272
273 void AV1Encoder::encode_frame(AV1Encoder::QueuedFrame qf)
274 {
275         if (qf.data) {
276                 EbSvtIOFormat pic;
277                 pic.luma = qf.data;     
278                 pic.cb = pic.luma + global_flags.width * global_flags.height;
279                 pic.cr = pic.cb + global_flags.width * global_flags.height / 4;
280                 pic.y_stride = global_flags.width;
281                 pic.cb_stride = global_flags.width / 2;
282                 pic.cr_stride = global_flags.width / 2;
283                 pic.width = global_flags.width;
284                 pic.height = global_flags.height;
285                 pic.origin_x = 0;
286                 pic.origin_y = 0;
287                 pic.color_fmt = EB_YUV420;
288                 pic.bit_depth = EB_EIGHT_BIT;  // TODO: 10-bit.
289
290                 EbBufferHeaderType hdr;
291                 hdr.p_buffer      = reinterpret_cast<uint8_t *>(&pic);
292                 hdr.n_alloc_len   = global_flags.width * global_flags.height * 3 / 2;  // TODO: 10-bit.
293                 hdr.n_filled_len  = hdr.n_alloc_len;
294                 hdr.n_tick_count  = 0;
295                 hdr.p_app_private = reinterpret_cast<void *>(intptr_t(qf.duration));
296                 hdr.pic_type      = EB_AV1_INVALID_PICTURE;  // Actually means auto, according to FFmpeg.
297                 hdr.metadata      = nullptr;
298                 hdr.flags         = 0;
299                 hdr.pts           = av_rescale_q(qf.pts, AVRational{ 1, TIMEBASE }, AVRational{ global_flags.av1_fps_den, global_flags.av1_fps_num });
300                 if (hdr.pts <= last_pts) {
301                         fprintf(stderr, "WARNING: Receiving frames faster than given --av1-fps value (%d/%d); dropping frame.\n",
302                                 global_flags.av1_fps_num, global_flags.av1_fps_den);
303                 } else {
304                         svt_av1_enc_send_picture(encoder, &hdr);
305                         frames_being_encoded[hdr.pts] = qf.received_ts;
306                         last_pts = hdr.pts;
307                 }
308         }
309
310         for ( ;; ) {
311                 EbBufferHeaderType *buf;
312                 EbErrorType ret = svt_av1_enc_get_packet(encoder, &buf, /*pic_send_done=*/false);
313                 if (ret == EB_NoErrorEmptyQueue) {
314                         return;
315                 }
316                 process_packet(buf);
317         }
318 }
319
320 void AV1Encoder::process_packet(EbBufferHeaderType *buf)
321 {
322         if (buf->n_filled_len == 0) {
323                 // TODO: Can this ever happen?
324                 svt_av1_enc_release_out_buffer(&buf);
325                 return;
326         }
327
328         switch (buf->pic_type) {
329                 case EB_AV1_KEY_PICTURE:
330                 case EB_AV1_INTRA_ONLY_PICTURE:
331                         ++metric_av1_output_frames_i;
332                         break;
333                 case EB_AV1_INTER_PICTURE:  // We don't really know whether it's P or B.
334                         ++metric_av1_output_frames_p;
335                         break;
336                 default:
337                         break;
338         }
339         metric_av1_qp.count_event(buf->qp);
340
341         if (frames_being_encoded.count(buf->pts)) {
342                 ReceivedTimestamps received_ts = frames_being_encoded[buf->pts];
343                 frames_being_encoded.erase(buf->pts);
344
345                 static int frameno = 0;
346                 print_latency("Current AV1 latency (video inputs → network mux):",
347                                 received_ts, /*b_frame=*/false, &frameno, &av1_latency_histogram);
348         } else {
349                 assert(false);
350         }
351
352         AVPacket pkt;
353         memset(&pkt, 0, sizeof(pkt));
354         pkt.buf = nullptr;
355         pkt.data = buf->p_buffer;
356         pkt.size = buf->n_filled_len;
357         pkt.stream_index = 0;
358         if (buf->pic_type == EB_AV1_KEY_PICTURE) {
359                 pkt.flags = AV_PKT_FLAG_KEY;
360         } else if (buf->pic_type == EB_AV1_NON_REF_PICTURE) {
361                 // I have no idea if this does anything in practice,
362                 // but the libavcodec plugin does it.
363                 pkt.flags = AV_PKT_FLAG_DISPOSABLE;
364         } else {
365                 pkt.flags = 0;
366         }
367         pkt.pts = av_rescale_q(buf->pts, AVRational{ global_flags.av1_fps_den, global_flags.av1_fps_num }, AVRational{ 1, TIMEBASE });
368         pkt.dts = av_rescale_q(buf->dts, AVRational{ global_flags.av1_fps_den, global_flags.av1_fps_num }, AVRational{ 1, TIMEBASE });
369
370         for (Mux *mux : muxes) {
371                 mux->add_packet(pkt, pkt.pts, pkt.dts);
372         }
373
374         svt_av1_enc_release_out_buffer(&buf);
375 }