1 #include "mjpeg_encoder.h"
12 #include <libavformat/avformat.h>
16 #include "shared/ffmpeg_raii.h"
18 #include "shared/httpd.h"
19 #include "shared/memcpy_interleaved.h"
20 #include "shared/metrics.h"
21 #include "pbo_frame_allocator.h"
22 #include "shared/timebase.h"
23 #include "va_display_with_cleanup.h"
25 #include <movit/colorspace_conversion_effect.h>
28 #include <va/va_drm.h>
29 #include <va/va_x11.h>
31 using namespace Eigen;
32 using namespace bmusb;
33 using namespace movit;
36 static VAImageFormat uyvy_format;
38 extern void memcpy_with_pitch(uint8_t *dst, const uint8_t *src, size_t src_width, size_t dst_pitch, size_t height);
40 // From libjpeg (although it's of course identical between implementations).
41 static const int jpeg_natural_order[DCTSIZE2] = {
42 0, 1, 8, 16, 9, 2, 3, 10,
43 17, 24, 32, 25, 18, 11, 4, 5,
44 12, 19, 26, 33, 40, 48, 41, 34,
45 27, 20, 13, 6, 7, 14, 21, 28,
46 35, 42, 49, 56, 57, 50, 43, 36,
47 29, 22, 15, 23, 30, 37, 44, 51,
48 58, 59, 52, 45, 38, 31, 39, 46,
49 53, 60, 61, 54, 47, 55, 62, 63,
52 struct VectorDestinationManager {
53 jpeg_destination_mgr pub;
54 std::vector<uint8_t> dest;
56 VectorDestinationManager()
58 pub.init_destination = init_destination_thunk;
59 pub.empty_output_buffer = empty_output_buffer_thunk;
60 pub.term_destination = term_destination_thunk;
63 static void init_destination_thunk(j_compress_ptr ptr)
65 ((VectorDestinationManager *)(ptr->dest))->init_destination();
68 inline void init_destination()
73 static boolean empty_output_buffer_thunk(j_compress_ptr ptr)
75 return ((VectorDestinationManager *)(ptr->dest))->empty_output_buffer();
78 inline bool empty_output_buffer()
80 make_room(dest.size()); // Should ignore pub.free_in_buffer!
84 inline void make_room(size_t bytes_used)
86 dest.resize(bytes_used + 4096);
87 dest.resize(dest.capacity());
88 pub.next_output_byte = dest.data() + bytes_used;
89 pub.free_in_buffer = dest.size() - bytes_used;
92 static void term_destination_thunk(j_compress_ptr ptr)
94 ((VectorDestinationManager *)(ptr->dest))->term_destination();
97 inline void term_destination()
99 dest.resize(dest.size() - pub.free_in_buffer);
102 static_assert(std::is_standard_layout<VectorDestinationManager>::value, "");
104 int MJPEGEncoder::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
106 WritePacket2Context *ctx = (WritePacket2Context *)opaque;
107 return ctx->mjpeg_encoder->write_packet2(ctx->stream_id, buf, buf_size, type, time);
110 int MJPEGEncoder::write_packet2(HTTPD::StreamID stream_id, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
112 string *mux_header = &streams[stream_id].mux_header;
113 if (type == AVIO_DATA_MARKER_HEADER) {
114 mux_header->append((char *)buf, buf_size);
115 httpd->set_header(stream_id, *mux_header);
117 httpd->add_data(stream_id, (char *)buf, buf_size, /*keyframe=*/true, AV_NOPTS_VALUE, AVRational{ AV_TIME_BASE, 1 });
124 void add_video_stream(AVFormatContext *avctx)
126 AVStream *stream = avformat_new_stream(avctx, nullptr);
127 if (stream == nullptr) {
128 fprintf(stderr, "avformat_new_stream() failed\n");
132 // FFmpeg is very picky about having audio at 1/48000 timebase,
133 // no matter what we write. Even though we'd prefer our usual 1/120000,
134 // put the video on the same one, so that we can have locked audio.
135 stream->time_base = AVRational{ 1, OUTPUT_FREQUENCY };
136 stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
137 stream->codecpar->codec_id = AV_CODEC_ID_MJPEG;
139 // Used for aspect ratio only. Can change without notice (the mux won't care).
140 stream->codecpar->width = global_flags.width;
141 stream->codecpar->height = global_flags.height;
143 // TODO: We could perhaps use the interpretation for each card here
144 // (or at least the command-line flags) instead of the defaults,
145 // but what would we do when they change?
146 stream->codecpar->color_primaries = AVCOL_PRI_BT709;
147 stream->codecpar->color_trc = AVCOL_TRC_IEC61966_2_1;
148 stream->codecpar->color_space = AVCOL_SPC_BT709;
149 stream->codecpar->color_range = AVCOL_RANGE_MPEG;
150 stream->codecpar->chroma_location = AVCHROMA_LOC_LEFT;
151 stream->codecpar->field_order = AV_FIELD_PROGRESSIVE;
154 void add_audio_stream(AVFormatContext *avctx)
156 AVStream *stream = avformat_new_stream(avctx, nullptr);
157 if (stream == nullptr) {
158 fprintf(stderr, "avformat_new_stream() failed\n");
161 stream->time_base = AVRational{ 1, OUTPUT_FREQUENCY };
162 stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
163 stream->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
164 stream->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
165 stream->codecpar->channels = 2;
166 stream->codecpar->sample_rate = OUTPUT_FREQUENCY;
169 void finalize_mux(AVFormatContext *avctx)
171 AVDictionary *options = NULL;
172 vector<pair<string, string>> opts = MUX_OPTS;
173 for (pair<string, string> opt : opts) {
174 av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
176 if (avformat_write_header(avctx, &options) < 0) {
177 fprintf(stderr, "avformat_write_header() failed\n");
184 MJPEGEncoder::MJPEGEncoder(HTTPD *httpd, const string &va_display)
187 create_ffmpeg_context(HTTPD::StreamID{ HTTPD::MULTICAM_STREAM, 0 });
188 for (unsigned stream_idx = 0; stream_idx < MAX_VIDEO_CARDS; ++stream_idx) {
189 create_ffmpeg_context(HTTPD::StreamID{ HTTPD::SIPHON_STREAM, stream_idx });
192 add_stream(HTTPD::StreamID{ HTTPD::MULTICAM_STREAM, 0 });
194 // Initialize VA-API.
196 va_dpy = try_open_va(va_display, &error, &config_id);
197 if (va_dpy == nullptr) {
198 fprintf(stderr, "Could not initialize VA-API for MJPEG encoding: %s. JPEGs will be encoded in software if needed.\n", error.c_str());
201 encoder_thread = thread(&MJPEGEncoder::encoder_thread_func, this);
202 if (va_dpy != nullptr) {
203 va_receiver_thread = thread(&MJPEGEncoder::va_receiver_thread_func, this);
206 global_metrics.add("mjpeg_frames", {{ "status", "dropped" }, { "reason", "zero_size" }}, &metric_mjpeg_frames_zero_size_dropped);
207 global_metrics.add("mjpeg_frames", {{ "status", "dropped" }, { "reason", "interlaced" }}, &metric_mjpeg_frames_interlaced_dropped);
208 global_metrics.add("mjpeg_frames", {{ "status", "dropped" }, { "reason", "unsupported_pixel_format" }}, &metric_mjpeg_frames_unsupported_pixel_format_dropped);
209 global_metrics.add("mjpeg_frames", {{ "status", "dropped" }, { "reason", "oversized" }}, &metric_mjpeg_frames_oversized_dropped);
210 global_metrics.add("mjpeg_frames", {{ "status", "dropped" }, { "reason", "overrun" }}, &metric_mjpeg_overrun_dropped);
211 global_metrics.add("mjpeg_frames", {{ "status", "submitted" }}, &metric_mjpeg_overrun_submitted);
216 MJPEGEncoder::~MJPEGEncoder()
218 for (auto &id_and_stream : streams) {
219 av_free(id_and_stream.second.avctx->pb->buffer);
222 global_metrics.remove("mjpeg_frames", {{ "status", "dropped" }, { "reason", "zero_size" }});
223 global_metrics.remove("mjpeg_frames", {{ "status", "dropped" }, { "reason", "interlaced" }});
224 global_metrics.remove("mjpeg_frames", {{ "status", "dropped" }, { "reason", "unsupported_pixel_format" }});
225 global_metrics.remove("mjpeg_frames", {{ "status", "dropped" }, { "reason", "oversized" }});
226 global_metrics.remove("mjpeg_frames", {{ "status", "dropped" }, { "reason", "overrun" }});
227 global_metrics.remove("mjpeg_frames", {{ "status", "submitted" }});
230 void MJPEGEncoder::stop()
237 any_frames_to_be_encoded.notify_all();
238 any_frames_encoding.notify_all();
239 encoder_thread.join();
240 if (va_dpy != nullptr) {
241 va_receiver_thread.join();
245 unique_ptr<VADisplayWithCleanup> MJPEGEncoder::try_open_va(const string &va_display, string *error, VAConfigID *config_id)
247 unique_ptr<VADisplayWithCleanup> va_dpy = va_open_display(va_display);
248 if (va_dpy == nullptr) {
249 if (error) *error = "Opening VA display failed";
252 int major_ver, minor_ver;
253 VAStatus va_status = vaInitialize(va_dpy->va_dpy, &major_ver, &minor_ver);
254 if (va_status != VA_STATUS_SUCCESS) {
256 snprintf(buf, sizeof(buf), "vaInitialize() failed with status %d\n", va_status);
257 if (error != nullptr) *error = buf;
261 VAConfigAttrib attr = { VAConfigAttribRTFormat, VA_RT_FORMAT_YUV422 };
262 va_status = vaCreateConfig(va_dpy->va_dpy, VAProfileJPEGBaseline, VAEntrypointEncPicture,
263 &attr, 1, config_id);
264 if (va_status == VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT) {
265 if (error != nullptr) *error = "No hardware support";
267 } else if (va_status != VA_STATUS_SUCCESS) {
269 snprintf(buf, sizeof(buf), "vaCreateConfig() failed with status %d\n", va_status);
270 if (error != nullptr) *error = buf;
274 // TODO: Unify with the code in Futatabi.
275 int num_formats = vaMaxNumImageFormats(va_dpy->va_dpy);
276 assert(num_formats > 0);
278 unique_ptr<VAImageFormat[]> formats(new VAImageFormat[num_formats]);
279 va_status = vaQueryImageFormats(va_dpy->va_dpy, formats.get(), &num_formats);
280 if (va_status != VA_STATUS_SUCCESS) {
282 snprintf(buf, sizeof(buf), "vaQueryImageFormats() failed with status %d\n", va_status);
283 if (error != nullptr) *error = buf;
288 for (int i = 0; i < num_formats; ++i) {
289 if (formats[i].fourcc == VA_FOURCC_UYVY) {
290 memcpy(&uyvy_format, &formats[i], sizeof(VAImageFormat));
296 if (error != nullptr) *error = "UYVY format not found";
303 void MJPEGEncoder::upload_frame(int64_t pts, unsigned card_index, RefCountedFrame frame, const bmusb::VideoFormat &video_format, size_t y_offset, size_t cbcr_offset, vector<int32_t> audio, const RGBTriplet &white_balance)
305 PBOFrameAllocator::Userdata *userdata = (PBOFrameAllocator::Userdata *)frame->userdata;
306 if (video_format.width == 0 || video_format.height == 0) {
307 ++metric_mjpeg_frames_zero_size_dropped;
310 if (video_format.interlaced) {
311 fprintf(stderr, "Card %u: Ignoring JPEG encoding for interlaced frame\n", card_index);
312 ++metric_mjpeg_frames_interlaced_dropped;
315 if (userdata->pixel_format != PixelFormat_8BitYCbCr ||
316 !frame->interleaved) {
317 fprintf(stderr, "Card %u: Ignoring JPEG encoding for unsupported pixel format\n", card_index);
318 ++metric_mjpeg_frames_unsupported_pixel_format_dropped;
321 if (video_format.width > 4096 || video_format.height > 4096) {
322 fprintf(stderr, "Card %u: Ignoring JPEG encoding for oversized frame\n", card_index);
323 ++metric_mjpeg_frames_oversized_dropped;
327 lock_guard<mutex> lock(mu);
328 if (frames_to_be_encoded.size() + frames_encoding.size() > 50) {
329 fprintf(stderr, "WARNING: MJPEG encoding doesn't keep up, discarding frame.\n");
330 ++metric_mjpeg_overrun_dropped;
333 ++metric_mjpeg_overrun_submitted;
334 frames_to_be_encoded.push(QueuedFrame{ pts, card_index, frame, video_format, y_offset, cbcr_offset, move(audio), white_balance });
335 any_frames_to_be_encoded.notify_all();
338 bool MJPEGEncoder::should_encode_mjpeg_for_card(unsigned card_index)
340 // Only bother doing MJPEG encoding if there are any connected clients
341 // that want the stream.
342 if (httpd->get_num_connected_multicam_clients() == 0 &&
343 httpd->get_num_connected_siphon_clients(card_index) == 0) {
347 auto it = global_flags.card_to_mjpeg_stream_export.find(card_index);
348 return (it != global_flags.card_to_mjpeg_stream_export.end());
351 void MJPEGEncoder::encoder_thread_func()
353 pthread_setname_np(pthread_self(), "MJPEG_Encode");
354 posix_memalign((void **)&tmp_y, 4096, 4096 * 8);
355 posix_memalign((void **)&tmp_cbcr, 4096, 4096 * 8);
356 posix_memalign((void **)&tmp_cb, 4096, 4096 * 8);
357 posix_memalign((void **)&tmp_cr, 4096, 4096 * 8);
362 unique_lock<mutex> lock(mu);
363 any_frames_to_be_encoded.wait(lock, [this] { return !frames_to_be_encoded.empty() || should_quit; });
364 if (should_quit) break;
365 qf = move(frames_to_be_encoded.front());
366 frames_to_be_encoded.pop();
369 assert(global_flags.card_to_mjpeg_stream_export.count(qf.card_index)); // Or should_encode_mjpeg_for_card() would have returned false.
370 int stream_index = global_flags.card_to_mjpeg_stream_export[qf.card_index];
372 if (va_dpy != nullptr) {
373 // Will call back in the receiver thread.
374 encode_jpeg_va(move(qf));
376 update_siphon_streams();
378 HTTPD::StreamID multicam_id{ HTTPD::MULTICAM_STREAM, 0 };
379 HTTPD::StreamID siphon_id{ HTTPD::SIPHON_STREAM, qf.card_index };
380 assert(streams.count(multicam_id));
382 // Write audio before video, since Futatabi expects it.
383 if (qf.audio.size() > 0) {
384 write_audio_packet(streams[multicam_id].avctx.get(), qf.pts, stream_index + global_flags.card_to_mjpeg_stream_export.size(), qf.audio);
385 if (streams.count(siphon_id)) {
386 write_audio_packet(streams[siphon_id].avctx.get(), qf.pts, /*stream_index=*/1, qf.audio);
390 // Encode synchronously, in the same thread.
391 vector<uint8_t> jpeg = encode_jpeg_libjpeg(qf);
392 write_mjpeg_packet(streams[multicam_id].avctx.get(), qf.pts, stream_index, jpeg.data(), jpeg.size());
393 if (streams.count(siphon_id)) {
394 write_mjpeg_packet(streams[siphon_id].avctx.get(), qf.pts, /*stream_index=*/0, jpeg.data(), jpeg.size());
405 void MJPEGEncoder::write_mjpeg_packet(AVFormatContext *avctx, int64_t pts, unsigned stream_index, const uint8_t *jpeg, size_t jpeg_size)
408 memset(&pkt, 0, sizeof(pkt));
410 pkt.data = const_cast<uint8_t *>(jpeg);
411 pkt.size = jpeg_size;
412 pkt.stream_index = stream_index;
413 pkt.flags = AV_PKT_FLAG_KEY;
414 AVRational time_base = avctx->streams[pkt.stream_index]->time_base;
415 pkt.pts = pkt.dts = av_rescale_q(pts, AVRational{ 1, TIMEBASE }, time_base);
418 if (av_write_frame(avctx, &pkt) < 0) {
419 fprintf(stderr, "av_write_frame() failed\n");
424 void MJPEGEncoder::write_audio_packet(AVFormatContext *avctx, int64_t pts, unsigned stream_index, const vector<int32_t> &audio)
427 memset(&pkt, 0, sizeof(pkt));
429 pkt.data = reinterpret_cast<uint8_t *>(const_cast<int32_t *>(&audio[0]));
430 pkt.size = audio.size() * sizeof(audio[0]);
431 pkt.stream_index = stream_index;
432 pkt.flags = AV_PKT_FLAG_KEY;
433 AVRational time_base = avctx->streams[pkt.stream_index]->time_base;
434 pkt.pts = pkt.dts = av_rescale_q(pts, AVRational{ 1, TIMEBASE }, time_base);
435 size_t num_stereo_samples = audio.size() / 2;
436 pkt.duration = av_rescale_q(num_stereo_samples, AVRational{ 1, OUTPUT_FREQUENCY }, time_base);
438 if (av_write_frame(avctx, &pkt) < 0) {
439 fprintf(stderr, "av_write_frame() failed\n");
444 class VABufferDestroyer {
446 VABufferDestroyer(VADisplay dpy, VABufferID buf)
447 : dpy(dpy), buf(buf) {}
449 ~VABufferDestroyer() {
450 VAStatus va_status = vaDestroyBuffer(dpy, buf);
451 CHECK_VASTATUS(va_status, "vaDestroyBuffer");
459 MJPEGEncoder::VAResources MJPEGEncoder::get_va_resources(unsigned width, unsigned height)
462 lock_guard<mutex> lock(va_resources_mutex);
463 for (auto it = va_resources_freelist.begin(); it != va_resources_freelist.end(); ++it) {
464 if (it->width == width && it->height == height) {
465 VAResources ret = *it;
466 va_resources_freelist.erase(it);
477 VASurfaceAttrib attrib;
478 attrib.flags = VA_SURFACE_ATTRIB_SETTABLE;
479 attrib.type = VASurfaceAttribPixelFormat;
480 attrib.value.type = VAGenericValueTypeInteger;
481 attrib.value.value.i = VA_FOURCC_UYVY;
483 VAStatus va_status = vaCreateSurfaces(va_dpy->va_dpy, VA_RT_FORMAT_YUV422,
485 &ret.surface, 1, &attrib, 1);
486 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
488 va_status = vaCreateContext(va_dpy->va_dpy, config_id, width, height, 0, &ret.surface, 1, &ret.context);
489 CHECK_VASTATUS(va_status, "vaCreateContext");
491 va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAEncCodedBufferType, width * height * 3 + 8192, 1, nullptr, &ret.data_buffer);
492 CHECK_VASTATUS(va_status, "vaCreateBuffer");
494 va_status = vaCreateImage(va_dpy->va_dpy, &uyvy_format, width, height, &ret.image);
495 CHECK_VASTATUS(va_status, "vaCreateImage");
500 void MJPEGEncoder::release_va_resources(MJPEGEncoder::VAResources resources)
502 lock_guard<mutex> lock(va_resources_mutex);
503 if (va_resources_freelist.size() > 50) {
504 auto it = va_resources_freelist.end();
507 VAStatus va_status = vaDestroyBuffer(va_dpy->va_dpy, it->data_buffer);
508 CHECK_VASTATUS(va_status, "vaDestroyBuffer");
510 va_status = vaDestroyContext(va_dpy->va_dpy, it->context);
511 CHECK_VASTATUS(va_status, "vaDestroyContext");
513 va_status = vaDestroySurfaces(va_dpy->va_dpy, &it->surface, 1);
514 CHECK_VASTATUS(va_status, "vaDestroySurfaces");
516 va_status = vaDestroyImage(va_dpy->va_dpy, it->image.image_id);
517 CHECK_VASTATUS(va_status, "vaDestroyImage");
519 va_resources_freelist.erase(it);
522 va_resources_freelist.push_front(resources);
527 void push16(uint16_t val, string *str)
529 str->push_back(val >> 8);
530 str->push_back(val & 0xff);
533 void push32(uint32_t val, string *str)
535 str->push_back(val >> 24);
536 str->push_back((val >> 16) & 0xff);
537 str->push_back((val >> 8) & 0xff);
538 str->push_back(val & 0xff);
543 void MJPEGEncoder::init_jpeg_422(unsigned width, unsigned height, const RGBTriplet &white_balance, VectorDestinationManager *dest, jpeg_compress_struct *cinfo)
546 cinfo->err = jpeg_std_error(&jerr);
547 jpeg_create_compress(cinfo);
549 cinfo->dest = (jpeg_destination_mgr *)dest;
551 cinfo->input_components = 3;
552 jpeg_set_defaults(cinfo);
553 jpeg_set_quality(cinfo, quality, /*force_baseline=*/false);
555 cinfo->image_width = width;
556 cinfo->image_height = height;
557 cinfo->raw_data_in = true;
558 jpeg_set_colorspace(cinfo, JCS_YCbCr);
559 cinfo->comp_info[0].h_samp_factor = 2;
560 cinfo->comp_info[0].v_samp_factor = 1;
561 cinfo->comp_info[1].h_samp_factor = 1;
562 cinfo->comp_info[1].v_samp_factor = 1;
563 cinfo->comp_info[2].h_samp_factor = 1;
564 cinfo->comp_info[2].v_samp_factor = 1;
565 cinfo->CCIR601_sampling = true; // Seems to be mostly ignored by libjpeg, though.
566 jpeg_start_compress(cinfo, true);
568 if (fabs(white_balance.r - 1.0f) > 1e-3 ||
569 fabs(white_balance.g - 1.0f) > 1e-3 ||
570 fabs(white_balance.b - 1.0f) > 1e-3) {
571 // Convert from (linear) RGB to XYZ.
572 Matrix3d rgb_to_xyz_matrix = movit::ColorspaceConversionEffect::get_xyz_matrix(COLORSPACE_sRGB);
573 Vector3d xyz = rgb_to_xyz_matrix * Vector3d(white_balance.r, white_balance.g, white_balance.b);
575 // Convert from XYZ to xyz by normalizing.
576 xyz /= (xyz[0] + xyz[1] + xyz[2]);
578 // Create a very rudimentary EXIF header to hold our white point.
581 // Exif header, followed by some padding.
585 // TIFF header first:
586 exif += "MM"; // Big endian.
591 // Offset of first IFD (relative to the MM, immediately after the header).
592 push32(exif.size() - 6 + 4, &exif);
594 // Now the actual IFD.
599 // WhitePoint tag ID.
600 push16(0x13e, &exif);
605 // Two values (x and y; z is implicit due to normalization).
608 // Offset (relative to the MM, immediately after the last IFD).
609 push32(exif.size() - 6 + 8, &exif);
614 // The actual values.
615 push32(lrintf(xyz[0] * 10000.0f), &exif);
616 push32(10000, &exif);
617 push32(lrintf(xyz[1] * 10000.0f), &exif);
618 push32(10000, &exif);
620 jpeg_write_marker(cinfo, JPEG_APP0 + 1, (const JOCTET *)exif.data(), exif.size());
623 // This comment marker is private to FFmpeg. It signals limited Y'CbCr range
624 // (and nothing else).
625 jpeg_write_marker(cinfo, JPEG_COM, (const JOCTET *)"CS=ITU601", strlen("CS=ITU601"));
628 vector<uint8_t> MJPEGEncoder::get_jpeg_header(unsigned width, unsigned height, const RGBTriplet &white_balance, jpeg_compress_struct *cinfo)
630 VectorDestinationManager dest;
631 init_jpeg_422(width, height, white_balance, &dest, cinfo);
633 // Make a dummy black image; there's seemingly no other easy way of
634 // making libjpeg outputting all of its headers.
635 JSAMPROW yptr[8], cbptr[8], crptr[8];
636 JSAMPARRAY data[3] = { yptr, cbptr, crptr };
637 memset(tmp_y, 0, 4096);
638 memset(tmp_cb, 0, 4096);
639 memset(tmp_cr, 0, 4096);
640 for (unsigned yy = 0; yy < 8; ++yy) {
645 for (unsigned y = 0; y < height; y += 8) {
646 jpeg_write_raw_data(cinfo, data, /*num_lines=*/8);
648 jpeg_finish_compress(cinfo);
650 // We're only interested in the header, not the data after it.
651 dest.term_destination();
652 for (size_t i = 0; i < dest.dest.size() - 1; ++i) {
653 if (dest.dest[i] == 0xff && dest.dest[i + 1] == 0xda) { // Start of scan (SOS).
654 unsigned len = dest.dest[i + 2] * 256 + dest.dest[i + 3];
655 dest.dest.resize(i + len + 2);
663 MJPEGEncoder::VAData MJPEGEncoder::get_va_data_for_resolution(unsigned width, unsigned height, const RGBTriplet &white_balance)
665 pair<unsigned, unsigned> key(width, height);
666 if (va_data_for_resolution.count(key)) {
667 return va_data_for_resolution[key];
670 // Use libjpeg to generate a header and set sane defaults for e.g.
671 // quantization tables. Then do the actual encode with VA-API.
672 jpeg_compress_struct cinfo;
673 vector<uint8_t> jpeg_header = get_jpeg_header(width, height, white_balance, &cinfo);
675 // Picture parameters.
676 VAEncPictureParameterBufferJPEG pic_param;
677 memset(&pic_param, 0, sizeof(pic_param));
678 pic_param.reconstructed_picture = VA_INVALID_ID;
679 pic_param.picture_width = cinfo.image_width;
680 pic_param.picture_height = cinfo.image_height;
681 for (int component_idx = 0; component_idx < cinfo.num_components; ++component_idx) {
682 const jpeg_component_info *comp = &cinfo.comp_info[component_idx];
683 pic_param.component_id[component_idx] = comp->component_id;
684 pic_param.quantiser_table_selector[component_idx] = comp->quant_tbl_no;
686 pic_param.num_components = cinfo.num_components;
687 pic_param.num_scan = 1;
688 pic_param.sample_bit_depth = 8;
689 pic_param.coded_buf = VA_INVALID_ID; // To be filled out by caller.
690 pic_param.pic_flags.bits.huffman = 1;
691 pic_param.quality = 50; // Don't scale the given quantization matrices. (See gen8_mfc_jpeg_fqm_state)
693 // Quantization matrices.
694 VAQMatrixBufferJPEG q;
695 memset(&q, 0, sizeof(q));
697 q.load_lum_quantiser_matrix = true;
698 q.load_chroma_quantiser_matrix = true;
699 for (int quant_tbl_idx = 0; quant_tbl_idx < min(4, NUM_QUANT_TBLS); ++quant_tbl_idx) {
700 const JQUANT_TBL *qtbl = cinfo.quant_tbl_ptrs[quant_tbl_idx];
701 assert((qtbl == nullptr) == (quant_tbl_idx >= 2));
702 if (qtbl == nullptr) continue;
704 uint8_t *qmatrix = (quant_tbl_idx == 0) ? q.lum_quantiser_matrix : q.chroma_quantiser_matrix;
705 for (int i = 0; i < 64; ++i) {
706 if (qtbl->quantval[i] > 255) {
707 fprintf(stderr, "Baseline JPEG only!\n");
710 qmatrix[i] = qtbl->quantval[jpeg_natural_order[i]];
714 // Huffman tables (arithmetic is not supported).
715 VAHuffmanTableBufferJPEGBaseline huff;
716 memset(&huff, 0, sizeof(huff));
718 for (int huff_tbl_idx = 0; huff_tbl_idx < min(2, NUM_HUFF_TBLS); ++huff_tbl_idx) {
719 const JHUFF_TBL *ac_hufftbl = cinfo.ac_huff_tbl_ptrs[huff_tbl_idx];
720 const JHUFF_TBL *dc_hufftbl = cinfo.dc_huff_tbl_ptrs[huff_tbl_idx];
721 if (ac_hufftbl == nullptr) {
722 assert(dc_hufftbl == nullptr);
723 huff.load_huffman_table[huff_tbl_idx] = 0;
725 assert(dc_hufftbl != nullptr);
726 huff.load_huffman_table[huff_tbl_idx] = 1;
728 for (int i = 0; i < 16; ++i) {
729 huff.huffman_table[huff_tbl_idx].num_dc_codes[i] = dc_hufftbl->bits[i + 1];
731 for (int i = 0; i < 12; ++i) {
732 huff.huffman_table[huff_tbl_idx].dc_values[i] = dc_hufftbl->huffval[i];
734 for (int i = 0; i < 16; ++i) {
735 huff.huffman_table[huff_tbl_idx].num_ac_codes[i] = ac_hufftbl->bits[i + 1];
737 for (int i = 0; i < 162; ++i) {
738 huff.huffman_table[huff_tbl_idx].ac_values[i] = ac_hufftbl->huffval[i];
743 // Slice parameters (metadata about the slice).
744 VAEncSliceParameterBufferJPEG parms;
745 memset(&parms, 0, sizeof(parms));
746 for (int component_idx = 0; component_idx < cinfo.num_components; ++component_idx) {
747 const jpeg_component_info *comp = &cinfo.comp_info[component_idx];
748 parms.components[component_idx].component_selector = comp->component_id;
749 parms.components[component_idx].dc_table_selector = comp->dc_tbl_no;
750 parms.components[component_idx].ac_table_selector = comp->ac_tbl_no;
751 if (parms.components[component_idx].dc_table_selector > 1 ||
752 parms.components[component_idx].ac_table_selector > 1) {
753 fprintf(stderr, "Uses too many Huffman tables\n");
757 parms.num_components = cinfo.num_components;
758 parms.restart_interval = cinfo.restart_interval;
760 jpeg_destroy_compress(&cinfo);
763 ret.jpeg_header = move(jpeg_header);
764 ret.pic_param = pic_param;
768 va_data_for_resolution[key] = ret;
772 void MJPEGEncoder::encode_jpeg_va(QueuedFrame &&qf)
774 PBOFrameAllocator::Userdata *userdata = (PBOFrameAllocator::Userdata *)qf.frame->userdata;
775 unsigned width = qf.video_format.width;
776 unsigned height = qf.video_format.height;
778 VAResources resources;
779 ReleaseVAResources release;
780 if (userdata->data_copy_current_src == PBOFrameAllocator::Userdata::FROM_VA_API) {
781 resources = move(userdata->va_resources);
782 release = move(userdata->va_resources_release);
784 assert(userdata->data_copy_current_src == PBOFrameAllocator::Userdata::FROM_MALLOC);
785 resources = get_va_resources(width, height);
786 release = ReleaseVAResources(this, resources);
789 VAData va_data = get_va_data_for_resolution(width, height, qf.white_balance);
790 va_data.pic_param.coded_buf = resources.data_buffer;
792 VABufferID pic_param_buffer;
793 VAStatus va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAEncPictureParameterBufferType, sizeof(va_data.pic_param), 1, &va_data.pic_param, &pic_param_buffer);
794 CHECK_VASTATUS(va_status, "vaCreateBuffer");
795 VABufferDestroyer destroy_pic_param(va_dpy->va_dpy, pic_param_buffer);
798 va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAQMatrixBufferType, sizeof(va_data.q), 1, &va_data.q, &q_buffer);
799 CHECK_VASTATUS(va_status, "vaCreateBuffer");
800 VABufferDestroyer destroy_iq(va_dpy->va_dpy, q_buffer);
802 VABufferID huff_buffer;
803 va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAHuffmanTableBufferType, sizeof(va_data.huff), 1, &va_data.huff, &huff_buffer);
804 CHECK_VASTATUS(va_status, "vaCreateBuffer");
805 VABufferDestroyer destroy_huff(va_dpy->va_dpy, huff_buffer);
807 VABufferID slice_param_buffer;
808 va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAEncSliceParameterBufferType, sizeof(va_data.parms), 1, &va_data.parms, &slice_param_buffer);
809 CHECK_VASTATUS(va_status, "vaCreateBuffer");
810 VABufferDestroyer destroy_slice_param(va_dpy->va_dpy, slice_param_buffer);
812 if (userdata->data_copy_current_src == PBOFrameAllocator::Userdata::FROM_VA_API) {
813 // The pixel data is already put into the image by the caller.
814 va_status = vaUnmapBuffer(va_dpy->va_dpy, resources.image.buf);
815 CHECK_VASTATUS(va_status, "vaUnmapBuffer");
817 assert(userdata->data_copy_current_src == PBOFrameAllocator::Userdata::FROM_MALLOC);
819 // Upload the pixel data.
820 uint8_t *surface_p = nullptr;
821 vaMapBuffer(va_dpy->va_dpy, resources.image.buf, (void **)&surface_p);
823 size_t field_start_line = qf.video_format.extra_lines_top; // No interlacing support.
824 size_t field_start = qf.cbcr_offset * 2 + qf.video_format.width * field_start_line * 2;
827 const uint8_t *src = qf.frame->data_copy + field_start;
828 uint8_t *dst = (unsigned char *)surface_p + resources.image.offsets[0];
829 memcpy_with_pitch(dst, src, qf.video_format.width * 2, resources.image.pitches[0], qf.video_format.height);
832 va_status = vaUnmapBuffer(va_dpy->va_dpy, resources.image.buf);
833 CHECK_VASTATUS(va_status, "vaUnmapBuffer");
836 qf.frame->data_copy = nullptr;
838 // Seemingly vaPutImage() (which triggers a GPU copy) is much nicer to the
839 // CPU than vaDeriveImage() and copying directly into the GPU's buffers.
840 // Exactly why is unclear, but it seems to involve L3 cache usage when there
841 // are many high-res (1080p+) images in play.
842 va_status = vaPutImage(va_dpy->va_dpy, resources.surface, resources.image.image_id, 0, 0, width, height, 0, 0, width, height);
843 CHECK_VASTATUS(va_status, "vaPutImage");
845 // Finally, stick in the JPEG header.
846 VAEncPackedHeaderParameterBuffer header_parm;
847 header_parm.type = VAEncPackedHeaderRawData;
848 header_parm.bit_length = 8 * va_data.jpeg_header.size();
850 VABufferID header_parm_buffer;
851 va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAEncPackedHeaderParameterBufferType, sizeof(header_parm), 1, &header_parm, &header_parm_buffer);
852 CHECK_VASTATUS(va_status, "vaCreateBuffer");
853 VABufferDestroyer destroy_header(va_dpy->va_dpy, header_parm_buffer);
855 VABufferID header_data_buffer;
856 va_status = vaCreateBuffer(va_dpy->va_dpy, config_id, VAEncPackedHeaderDataBufferType, va_data.jpeg_header.size(), 1, va_data.jpeg_header.data(), &header_data_buffer);
857 CHECK_VASTATUS(va_status, "vaCreateBuffer");
858 VABufferDestroyer destroy_header_data(va_dpy->va_dpy, header_data_buffer);
860 va_status = vaBeginPicture(va_dpy->va_dpy, resources.context, resources.surface);
861 CHECK_VASTATUS(va_status, "vaBeginPicture");
862 va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &pic_param_buffer, 1);
863 CHECK_VASTATUS(va_status, "vaRenderPicture(pic_param)");
864 va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &q_buffer, 1);
865 CHECK_VASTATUS(va_status, "vaRenderPicture(q)");
866 va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &huff_buffer, 1);
867 CHECK_VASTATUS(va_status, "vaRenderPicture(huff)");
868 va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &slice_param_buffer, 1);
869 CHECK_VASTATUS(va_status, "vaRenderPicture(slice_param)");
870 va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &header_parm_buffer, 1);
871 CHECK_VASTATUS(va_status, "vaRenderPicture(header_parm)");
872 va_status = vaRenderPicture(va_dpy->va_dpy, resources.context, &header_data_buffer, 1);
873 CHECK_VASTATUS(va_status, "vaRenderPicture(header_data)");
874 va_status = vaEndPicture(va_dpy->va_dpy, resources.context);
875 CHECK_VASTATUS(va_status, "vaEndPicture");
877 qf.resources = move(resources);
878 qf.resource_releaser = move(release);
880 lock_guard<mutex> lock(mu);
881 frames_encoding.push(move(qf));
882 any_frames_encoding.notify_all();
885 void MJPEGEncoder::va_receiver_thread_func()
887 pthread_setname_np(pthread_self(), "MJPEG_Receive");
891 unique_lock<mutex> lock(mu);
892 any_frames_encoding.wait(lock, [this] { return !frames_encoding.empty() || should_quit; });
893 if (should_quit) return;
894 qf = move(frames_encoding.front());
895 frames_encoding.pop();
898 update_siphon_streams();
900 assert(global_flags.card_to_mjpeg_stream_export.count(qf.card_index)); // Or should_encode_mjpeg_for_card() would have returned false.
901 int stream_index = global_flags.card_to_mjpeg_stream_export[qf.card_index];
903 HTTPD::StreamID multicam_id{ HTTPD::MULTICAM_STREAM, 0 };
904 HTTPD::StreamID siphon_id{ HTTPD::SIPHON_STREAM, qf.card_index };
905 assert(streams.count(multicam_id));
906 assert(streams[multicam_id].avctx != nullptr);
908 // Write audio before video, since Futatabi expects it.
909 if (qf.audio.size() > 0) {
910 write_audio_packet(streams[multicam_id].avctx.get(), qf.pts, stream_index + global_flags.card_to_mjpeg_stream_export.size(), qf.audio);
911 if (streams.count(siphon_id)) {
912 write_audio_packet(streams[siphon_id].avctx.get(), qf.pts, /*stream_index=*/1, qf.audio);
916 VAStatus va_status = vaSyncSurface(va_dpy->va_dpy, qf.resources.surface);
917 CHECK_VASTATUS(va_status, "vaSyncSurface");
919 VACodedBufferSegment *segment;
920 va_status = vaMapBuffer(va_dpy->va_dpy, qf.resources.data_buffer, (void **)&segment);
921 CHECK_VASTATUS(va_status, "vaMapBuffer");
923 const uint8_t *coded_buf = reinterpret_cast<uint8_t *>(segment->buf);
924 write_mjpeg_packet(streams[multicam_id].avctx.get(), qf.pts, stream_index, coded_buf, segment->size);
925 if (streams.count(siphon_id)) {
926 write_mjpeg_packet(streams[siphon_id].avctx.get(), qf.pts, /*stream_index=*/0, coded_buf, segment->size);
929 va_status = vaUnmapBuffer(va_dpy->va_dpy, qf.resources.data_buffer);
930 CHECK_VASTATUS(va_status, "vaUnmapBuffer");
934 vector<uint8_t> MJPEGEncoder::encode_jpeg_libjpeg(const QueuedFrame &qf)
936 unsigned width = qf.video_format.width;
937 unsigned height = qf.video_format.height;
939 VectorDestinationManager dest;
940 jpeg_compress_struct cinfo;
941 init_jpeg_422(width, height, qf.white_balance, &dest, &cinfo);
943 size_t field_start_line = qf.video_format.extra_lines_top; // No interlacing support.
944 size_t field_start = qf.cbcr_offset * 2 + qf.video_format.width * field_start_line * 2;
946 JSAMPROW yptr[8], cbptr[8], crptr[8];
947 JSAMPARRAY data[3] = { yptr, cbptr, crptr };
948 for (unsigned y = 0; y < qf.video_format.height; y += 8) {
949 const uint8_t *src = qf.frame->data_copy + field_start + y * qf.video_format.width * 2;
951 memcpy_interleaved(tmp_cbcr, tmp_y, src, qf.video_format.width * 8 * 2);
952 memcpy_interleaved(tmp_cb, tmp_cr, tmp_cbcr, qf.video_format.width * 8);
953 for (unsigned yy = 0; yy < 8; ++yy) {
954 yptr[yy] = tmp_y + yy * width;
955 cbptr[yy] = tmp_cb + yy * width / 2;
956 crptr[yy] = tmp_cr + yy * width / 2;
958 jpeg_write_raw_data(&cinfo, data, /*num_lines=*/8);
960 jpeg_finish_compress(&cinfo);
965 void MJPEGEncoder::add_stream(HTTPD::StreamID stream_id)
967 AVFormatContextWithCloser avctx;
969 // Set up the mux. We don't use the Mux wrapper, because it's geared towards
970 // a situation with only one video stream (and possibly one audio stream)
971 // with known width/height, and we don't need the extra functionality it provides.
972 avctx.reset(avformat_alloc_context());
973 avctx->oformat = av_guess_format("nut", nullptr, nullptr);
975 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
976 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, &ffmpeg_contexts[stream_id], nullptr, nullptr, nullptr);
977 avctx->pb->write_data_type = &MJPEGEncoder::write_packet2_thunk;
978 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
980 if (stream_id.type == HTTPD::MULTICAM_STREAM) {
981 for (unsigned card_idx = 0; card_idx < global_flags.card_to_mjpeg_stream_export.size(); ++card_idx) {
982 add_video_stream(avctx.get());
984 for (unsigned card_idx = 0; card_idx < global_flags.card_to_mjpeg_stream_export.size(); ++card_idx) {
985 add_audio_stream(avctx.get());
988 assert(stream_id.type == HTTPD::SIPHON_STREAM);
989 add_video_stream(avctx.get());
990 add_audio_stream(avctx.get());
992 finalize_mux(avctx.get());
995 s.avctx = move(avctx);
996 streams[stream_id] = move(s);
999 void MJPEGEncoder::update_siphon_streams()
1001 // Bring the list of streams into sync with what the clients need.
1002 for (auto it = streams.begin(); it != streams.end(); ) {
1003 if (it->first.type != HTTPD::SIPHON_STREAM) {
1007 if (httpd->get_num_connected_siphon_clients(it->first.index) == 0) {
1008 av_free(it->second.avctx->pb->buffer);
1009 streams.erase(it++);
1014 for (unsigned stream_idx = 0; stream_idx < MAX_VIDEO_CARDS; ++stream_idx) {
1015 HTTPD::StreamID stream_id{ HTTPD::SIPHON_STREAM, stream_idx };
1016 if (streams.count(stream_id) == 0 && httpd->get_num_connected_siphon_clients(stream_idx) > 0) {
1017 add_stream(stream_id);
1022 void MJPEGEncoder::create_ffmpeg_context(HTTPD::StreamID stream_id)
1024 ffmpeg_contexts.emplace(stream_id, WritePacket2Context{ this, stream_id });