X-Git-Url: https://git.sesse.net/?p=nageru;a=blobdiff_plain;f=nageru%2Fffmpeg_capture.cpp;h=2d1b3385b4ac0d64c137bba4c3590819f06f05f6;hp=b9eb982008f166fbc7cab97b0e3acc990ee19340;hb=6cf87ad853439f8565c575fb29dc539a15fdba87;hpb=0a338eed8f26adaf202e0cadc88b533ce9fa173d diff --git a/nageru/ffmpeg_capture.cpp b/nageru/ffmpeg_capture.cpp index b9eb982..2d1b338 100644 --- a/nageru/ffmpeg_capture.cpp +++ b/nageru/ffmpeg_capture.cpp @@ -27,6 +27,10 @@ extern "C" { #include #include +#include +#include +#include + #include "bmusb/bmusb.h" #include "shared/ffmpeg_raii.h" #include "ffmpeg_util.h" @@ -41,6 +45,7 @@ using namespace std; using namespace std::chrono; using namespace bmusb; using namespace movit; +using namespace Eigen; namespace { @@ -214,6 +219,32 @@ YCbCrFormat decode_ycbcr_format(const AVPixFmtDescriptor *desc, const AVFrame *f return format; } +RGBTriplet get_neutral_color(AVDictionary *metadata) +{ + if (metadata == nullptr) { + return RGBTriplet(1.0f, 1.0f, 1.0f); + } + AVDictionaryEntry *entry = av_dict_get(metadata, "WhitePoint", nullptr, 0); + if (entry == nullptr) { + return RGBTriplet(1.0f, 1.0f, 1.0f); + } + + unsigned x_nom, x_den, y_nom, y_den; + if (sscanf(entry->value, " %u:%u , %u:%u", &x_nom, &x_den, &y_nom, &y_den) != 4) { + fprintf(stderr, "WARNING: Unable to parse white point '%s', using default white point\n", entry->value); + return RGBTriplet(1.0f, 1.0f, 1.0f); + } + + double x = double(x_nom) / x_den; + double y = double(y_nom) / y_den; + double z = 1.0 - x - y; + + Matrix3d rgb_to_xyz_matrix = movit::ColorspaceConversionEffect::get_xyz_matrix(COLORSPACE_sRGB); + Vector3d rgb = rgb_to_xyz_matrix.inverse() * Vector3d(x, y, z); + + return RGBTriplet(rgb[0], rgb[1], rgb[2]); +} + } // namespace FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned height) @@ -575,6 +606,7 @@ bool FFmpegCapture::play_video(const string &pathname) // audio discontinuity.) timecode += MAX_FPS * 2 + 1; } + last_neutral_color = get_neutral_color(frame->metadata); frame_callback(frame->pts, video_timebase, audio_pts, audio_timebase, timecode++, video_frame.get_and_release(), 0, video_format, audio_frame.get_and_release(), 0, audio_format);