X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Fmjpeg_encoder.cpp;fp=nageru%2Fmjpeg_encoder.cpp;h=39a5959ed3bfeeedbd6b5715ba93b180239f1457;hb=0a338eed8f26adaf202e0cadc88b533ce9fa173d;hp=e39e802c843cbef0bcd6f130db17702d722c2ce3;hpb=fa42ce69a2508ea129a6cf7f97ace905be91aa91;p=nageru diff --git a/nageru/mjpeg_encoder.cpp b/nageru/mjpeg_encoder.cpp index e39e802..39a5959 100644 --- a/nageru/mjpeg_encoder.cpp +++ b/nageru/mjpeg_encoder.cpp @@ -21,11 +21,15 @@ extern "C" { #include "shared/timebase.h" #include "va_display_with_cleanup.h" +#include + #include #include #include +using namespace Eigen; using namespace bmusb; +using namespace movit; using namespace std; static VAImageFormat uyvy_format; @@ -286,7 +290,7 @@ unique_ptr MJPEGEncoder::try_open_va(const string &va_disp return va_dpy; } -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 audio) +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 audio, const RGBTriplet &white_balance) { PBOFrameAllocator::Userdata *userdata = (PBOFrameAllocator::Userdata *)frame->userdata; if (video_format.width == 0 || video_format.height == 0) { @@ -317,7 +321,7 @@ void MJPEGEncoder::upload_frame(int64_t pts, unsigned card_index, RefCountedFram return; } ++metric_mjpeg_overrun_submitted; - frames_to_be_encoded.push(QueuedFrame{ pts, card_index, frame, video_format, y_offset, cbcr_offset, move(audio) }); + frames_to_be_encoded.push(QueuedFrame{ pts, card_index, frame, video_format, y_offset, cbcr_offset, move(audio), white_balance }); any_frames_to_be_encoded.notify_all(); } @@ -495,7 +499,25 @@ void MJPEGEncoder::release_va_resources(MJPEGEncoder::VAResources resources) va_resources_freelist.push_front(resources); } -void MJPEGEncoder::init_jpeg_422(unsigned width, unsigned height, VectorDestinationManager *dest, jpeg_compress_struct *cinfo) +namespace { + +void push16(uint16_t val, string *str) +{ + str->push_back(val >> 8); + str->push_back(val & 0xff); +} + +void push32(uint32_t val, string *str) +{ + str->push_back(val >> 24); + str->push_back((val >> 16) & 0xff); + str->push_back((val >> 8) & 0xff); + str->push_back(val & 0xff); +} + +} // namespace + +void MJPEGEncoder::init_jpeg_422(unsigned width, unsigned height, const RGBTriplet &white_balance, VectorDestinationManager *dest, jpeg_compress_struct *cinfo) { jpeg_error_mgr jerr; cinfo->err = jpeg_std_error(&jerr); @@ -520,15 +542,70 @@ void MJPEGEncoder::init_jpeg_422(unsigned width, unsigned height, VectorDestinat cinfo->CCIR601_sampling = true; // Seems to be mostly ignored by libjpeg, though. jpeg_start_compress(cinfo, true); + if (fabs(white_balance.r - 1.0f) > 1e-3 || + fabs(white_balance.g - 1.0f) > 1e-3 || + fabs(white_balance.b - 1.0f) > 1e-3) { + // Convert from (linear) RGB to XYZ. + Matrix3d rgb_to_xyz_matrix = movit::ColorspaceConversionEffect::get_xyz_matrix(COLORSPACE_sRGB); + Vector3d xyz = rgb_to_xyz_matrix * Vector3d(white_balance.r, white_balance.g, white_balance.b); + + // Convert from XYZ to xyz by normalizing. + xyz /= (xyz[0] + xyz[1] + xyz[2]); + + // Create a very rudimentary EXIF header to hold our white point. + string exif; + + // Exif header, followed by some padding. + exif = "Exif"; + push16(0, &exif); + + // TIFF header first: + exif += "MM"; // Big endian. + + // Magic number. + push16(42, &exif); + + // Offset of first IFD (relative to the MM, immediately after the header). + push32(exif.size() - 6 + 4, &exif); + + // Now the actual IFD. + + // One entry. + push16(1, &exif); + + // WhitePoint tag ID. + push16(0x13e, &exif); + + // Rational type. + push16(5, &exif); + + // Two values (x and y; z is implicit due to normalization). + push32(2, &exif); + + // Offset (relative to the MM, immediately after the last IFD). + push32(exif.size() - 6 + 8, &exif); + + // No more IFDs. + push32(0, &exif); + + // The actual values. + push32(lrintf(xyz[0] * 10000.0f), &exif); + push32(10000, &exif); + push32(lrintf(xyz[1] * 10000.0f), &exif); + push32(10000, &exif); + + jpeg_write_marker(cinfo, JPEG_APP0 + 1, (const JOCTET *)exif.data(), exif.size()); + } + // This comment marker is private to FFmpeg. It signals limited Y'CbCr range // (and nothing else). jpeg_write_marker(cinfo, JPEG_COM, (const JOCTET *)"CS=ITU601", strlen("CS=ITU601")); } -vector MJPEGEncoder::get_jpeg_header(unsigned width, unsigned height, jpeg_compress_struct *cinfo) +vector MJPEGEncoder::get_jpeg_header(unsigned width, unsigned height, const RGBTriplet &white_balance, jpeg_compress_struct *cinfo) { VectorDestinationManager dest; - init_jpeg_422(width, height, &dest, cinfo); + init_jpeg_422(width, height, white_balance, &dest, cinfo); // Make a dummy black image; there's seemingly no other easy way of // making libjpeg outputting all of its headers. @@ -560,7 +637,7 @@ vector MJPEGEncoder::get_jpeg_header(unsigned width, unsigned height, j return dest.dest; } -MJPEGEncoder::VAData MJPEGEncoder::get_va_data_for_resolution(unsigned width, unsigned height) +MJPEGEncoder::VAData MJPEGEncoder::get_va_data_for_resolution(unsigned width, unsigned height, const RGBTriplet &white_balance) { pair key(width, height); if (va_data_for_resolution.count(key)) { @@ -570,7 +647,7 @@ MJPEGEncoder::VAData MJPEGEncoder::get_va_data_for_resolution(unsigned width, un // Use libjpeg to generate a header and set sane defaults for e.g. // quantization tables. Then do the actual encode with VA-API. jpeg_compress_struct cinfo; - vector jpeg_header = get_jpeg_header(width, height, &cinfo); + vector jpeg_header = get_jpeg_header(width, height, white_balance, &cinfo); // Picture parameters. VAEncPictureParameterBufferJPEG pic_param; @@ -686,7 +763,7 @@ void MJPEGEncoder::encode_jpeg_va(QueuedFrame &&qf) release = ReleaseVAResources(this, resources); } - VAData va_data = get_va_data_for_resolution(width, height); + VAData va_data = get_va_data_for_resolution(width, height, qf.white_balance); va_data.pic_param.coded_buf = resources.data_buffer; VABufferID pic_param_buffer; @@ -822,7 +899,7 @@ vector MJPEGEncoder::encode_jpeg_libjpeg(const QueuedFrame &qf) VectorDestinationManager dest; jpeg_compress_struct cinfo; - init_jpeg_422(width, height, &dest, &cinfo); + init_jpeg_422(width, height, qf.white_balance, &dest, &cinfo); size_t field_start_line = qf.video_format.extra_lines_top; // No interlacing support. size_t field_start = qf.cbcr_offset * 2 + qf.video_format.width * field_start_line * 2;