1 #include "av1_encoder.h"
15 #include <EbSvtAv1Enc.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"
26 #include <libavcodec/avcodec.h>
27 #include <libavformat/avformat.h>
30 using namespace movit;
32 using namespace std::chrono;
33 using namespace std::placeholders;
37 // AV1Encoder can be restarted if --record-av1-video is set, so make these
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;
47 once_flag av1_metrics_inited;
51 AV1Encoder::AV1Encoder(const AVOutputFormat *oformat)
52 : wants_global_headers(oformat->flags & AVFMT_GLOBALHEADER)
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);
61 metric_av1_qp.init_uniform(50);
62 global_metrics.add("av1_qp", {}, &metric_av1_qp);
63 av1_latency_histogram.init("av1");
66 const size_t bytes_per_pixel = global_flags.bit_depth > 8 ? 2 : 1;
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));
71 encoder_thread = thread(&AV1Encoder::encoder_thread_func, this);
74 AV1Encoder::~AV1Encoder()
77 queued_frames_nonempty.notify_all();
78 encoder_thread.join();
81 void AV1Encoder::add_frame(int64_t pts, int64_t duration, YCbCrLumaCoefficients ycbcr_coefficients, const uint8_t *data, const ReceivedTimestamps &received_ts)
87 qf.duration = duration;
88 qf.ycbcr_coefficients = ycbcr_coefficients;
89 qf.received_ts = received_ts;
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;
99 qf.data = free_frames.front();
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 (and also perhaps let the GPU do the 10-bit compression SVT-AV1
106 // wants, instead of doing it on the CPU).
107 const size_t bytes_per_pixel = global_flags.bit_depth > 8 ? 2 : 1;
108 size_t frame_size = global_flags.width * global_flags.height * bytes_per_pixel;
109 assert(global_flags.width % 2 == 0);
110 assert(global_flags.height % 2 == 0);
111 uint8_t *y = qf.data;
112 uint8_t *cb = y + frame_size;
113 uint8_t *cr = cb + frame_size / 4;
114 memcpy(y, data, frame_size);
115 if (global_flags.bit_depth == 8) {
116 memcpy_interleaved(cb, cr, data + frame_size, frame_size / 2);
118 const uint16_t *src = reinterpret_cast<const uint16_t *>(data + frame_size);
119 uint16_t *cb16 = reinterpret_cast<uint16_t *>(cb);
120 uint16_t *cr16 = reinterpret_cast<uint16_t *>(cr);
121 memcpy_interleaved_word(cb16, cr16, src, frame_size / 4);
125 lock_guard<mutex> lock(mu);
126 queued_frames.push(qf);
127 queued_frames_nonempty.notify_all();
128 metric_av1_queued_frames = queued_frames.size();
132 void AV1Encoder::init_av1()
134 EbSvtAv1EncConfiguration config;
135 EbErrorType ret = svt_av1_enc_init_handle(&encoder, nullptr, &config);
136 if (ret != EB_ErrorNone) {
137 fprintf(stderr, "Error initializing SVT-AV1 handle (error %08x)\n", ret);
141 // NOTE: We don't set CBR, as it requires low-delay mode, which is
142 // generally problematic wrt. quality and performance.
143 config.enc_mode = global_flags.av1_preset;
144 config.intra_period_length = 63; // Approx. one second, conforms to the (n % 8) - 1 == 0 rule.
145 config.source_width = global_flags.width;
146 config.source_height = global_flags.height;
147 config.frame_rate_numerator = global_flags.av1_fps_num;
148 config.frame_rate_denominator = global_flags.av1_fps_den;
149 config.encoder_bit_depth = global_flags.bit_depth;
150 config.target_bit_rate = global_flags.av1_bitrate * 1000;
152 // NOTE: These should be in sync with the ones in quicksync_encoder.cpp (sps_rbsp()).
153 config.color_primaries = EB_CICP_CP_BT_709;
154 config.transfer_characteristics = EB_CICP_TC_SRGB;
155 if (global_flags.ycbcr_rec709_coefficients) {
156 config.matrix_coefficients = EB_CICP_MC_BT_709;
158 config.matrix_coefficients = EB_CICP_MC_BT_601;
160 config.color_range = EB_CR_STUDIO_RANGE;
161 #if SVT_AV1_CHECK_VERSION(1, 0, 0)
162 config.chroma_sample_position = EB_CSP_VERTICAL;
165 const vector<string> &extra_param = global_flags.av1_extra_param;
166 for (const string &str : extra_param) {
167 const size_t pos = str.find(',');
168 if (pos == string::npos) {
169 if (svt_av1_enc_parse_parameter(&config, str.c_str(), nullptr) != EB_ErrorNone) {
170 fprintf(stderr, "ERROR: SVT-AV1 rejected parameter '%s' with no value\n", str.c_str());
174 const string key = str.substr(0, pos);
175 const string value = str.substr(pos + 1);
176 if (svt_av1_enc_parse_parameter(&config, key.c_str(), value.c_str()) != EB_ErrorNone) {
177 fprintf(stderr, "ERROR: SVT-AV1 rejected parameter '%s' set to '%s'\n",
178 key.c_str(), value.c_str());
184 ret = svt_av1_enc_set_parameter(encoder, &config);
185 if (ret != EB_ErrorNone) {
186 fprintf(stderr, "Error configuring SVT-AV1 (error %08x)\n", ret);
190 ret = svt_av1_enc_init(encoder);
191 if (ret != EB_ErrorNone) {
192 fprintf(stderr, "Error initializing SVT-AV1 (error %08x)\n", ret);
196 if (wants_global_headers) {
197 EbBufferHeaderType *header = NULL;
199 ret = svt_av1_enc_stream_header(encoder, &header);
200 if (ret != EB_ErrorNone) {
201 fprintf(stderr, "Error building SVT-AV1 header (error %08x)\n", ret);
205 global_headers = string(reinterpret_cast<const char *>(header->p_buffer), header->n_filled_len);
207 svt_av1_enc_stream_header_release(header); // Don't care about errors.
211 void AV1Encoder::encoder_thread_func()
215 // No exit; it's not fatal.
217 pthread_setname_np(pthread_self(), "AV1_encode");
219 av1_init_done = true;
226 // Wait for a queued frame, then dequeue it.
228 unique_lock<mutex> lock(mu);
229 queued_frames_nonempty.wait(lock, [this]() { return !queued_frames.empty() || should_quit; });
230 if (!queued_frames.empty()) {
231 qf = queued_frames.front();
239 metric_av1_queued_frames = queued_frames.size();
240 frames_left = !queued_frames.empty();
246 lock_guard<mutex> lock(mu);
247 free_frames.push(qf.data);
250 // We should quit only if the should_quit flag is set _and_ we have nothing
252 } while (!should_quit || frames_left);
254 // Signal end of stream.
255 EbBufferHeaderType hdr;
257 hdr.n_filled_len = 0;
258 hdr.n_tick_count = 0;
259 hdr.p_app_private = nullptr;
260 hdr.pic_type = EB_AV1_INVALID_PICTURE;
261 hdr.p_buffer = nullptr;
262 hdr.metadata = nullptr;
263 hdr.flags = EB_BUFFERFLAG_EOS;
264 svt_av1_enc_send_picture(encoder, &hdr);
266 bool seen_eof = false;
268 EbBufferHeaderType *buf;
269 EbErrorType ret = svt_av1_enc_get_packet(encoder, &buf, /*pic_send_done=*/true);
270 if (ret == EB_NoErrorEmptyQueue) {
273 seen_eof = (buf->flags & EB_BUFFERFLAG_EOS);
277 svt_av1_enc_deinit(encoder);
278 svt_av1_enc_deinit_handle(encoder);
281 void AV1Encoder::encode_frame(AV1Encoder::QueuedFrame qf)
284 const size_t bytes_per_pixel = global_flags.bit_depth > 8 ? 2 : 1;
288 pic.cb = pic.luma + global_flags.width * global_flags.height * bytes_per_pixel;
289 pic.cr = pic.cb + (global_flags.width * global_flags.height / 4) * bytes_per_pixel;
290 pic.y_stride = global_flags.width; // In pixels, so no bytes_per_pixel.
291 pic.cb_stride = global_flags.width / 2; // Likewise.
292 pic.cr_stride = global_flags.width / 2; // Likewise.
293 pic.width = global_flags.width;
294 pic.height = global_flags.height;
297 pic.color_fmt = EB_YUV420;
298 pic.bit_depth = global_flags.bit_depth > 8 ? EB_TEN_BIT : EB_EIGHT_BIT;
300 EbBufferHeaderType hdr;
301 hdr.p_buffer = reinterpret_cast<uint8_t *>(&pic);
302 hdr.n_alloc_len = (global_flags.width * global_flags.height * 3 / 2) * bytes_per_pixel;
303 hdr.n_filled_len = hdr.n_alloc_len;
304 hdr.n_tick_count = 0;
305 hdr.p_app_private = reinterpret_cast<void *>(intptr_t(qf.duration));
306 hdr.pic_type = EB_AV1_INVALID_PICTURE; // Actually means auto, according to FFmpeg.
307 hdr.metadata = nullptr;
309 hdr.pts = av_rescale_q(qf.pts, AVRational{ 1, TIMEBASE }, AVRational{ global_flags.av1_fps_den, global_flags.av1_fps_num });
310 if (hdr.pts <= last_pts) {
311 fprintf(stderr, "WARNING: Receiving frames faster than given --av1-fps value (%d/%d); dropping frame.\n",
312 global_flags.av1_fps_num, global_flags.av1_fps_den);
314 svt_av1_enc_send_picture(encoder, &hdr);
315 frames_being_encoded[hdr.pts] = qf.received_ts;
321 EbBufferHeaderType *buf;
322 EbErrorType ret = svt_av1_enc_get_packet(encoder, &buf, /*pic_send_done=*/false);
323 if (ret == EB_NoErrorEmptyQueue) {
330 void AV1Encoder::process_packet(EbBufferHeaderType *buf)
332 if (buf->n_filled_len == 0) {
333 // TODO: Can this ever happen?
334 svt_av1_enc_release_out_buffer(&buf);
338 switch (buf->pic_type) {
339 case EB_AV1_KEY_PICTURE:
340 case EB_AV1_INTRA_ONLY_PICTURE:
341 ++metric_av1_output_frames_i;
343 case EB_AV1_INTER_PICTURE: // We don't really know whether it's P or B.
344 ++metric_av1_output_frames_p;
349 metric_av1_qp.count_event(buf->qp);
351 if (frames_being_encoded.count(buf->pts)) {
352 ReceivedTimestamps received_ts = frames_being_encoded[buf->pts];
353 frames_being_encoded.erase(buf->pts);
355 static int frameno = 0;
356 print_latency("Current AV1 latency (video inputs → network mux):",
357 received_ts, /*b_frame=*/false, &frameno, &av1_latency_histogram);
363 memset(&pkt, 0, sizeof(pkt));
365 pkt.data = buf->p_buffer;
366 pkt.size = buf->n_filled_len;
367 pkt.stream_index = 0;
368 if (buf->pic_type == EB_AV1_KEY_PICTURE) {
369 pkt.flags = AV_PKT_FLAG_KEY;
370 } else if (buf->pic_type == EB_AV1_NON_REF_PICTURE) {
371 // I have no idea if this does anything in practice,
372 // but the libavcodec plugin does it.
373 pkt.flags = AV_PKT_FLAG_DISPOSABLE;
377 pkt.pts = av_rescale_q(buf->pts, AVRational{ global_flags.av1_fps_den, global_flags.av1_fps_num }, AVRational{ 1, TIMEBASE });
378 pkt.dts = av_rescale_q(buf->dts, AVRational{ global_flags.av1_fps_den, global_flags.av1_fps_num }, AVRational{ 1, TIMEBASE });
380 for (Mux *mux : muxes) {
381 mux->add_packet(pkt, pkt.pts, pkt.dts);
384 svt_av1_enc_release_out_buffer(&buf);