]> git.sesse.net Git - nageru/blobdiff - nageru/ffmpeg_capture.cpp
Heed the Exif white point when playing back (MJPEG) video.
[nageru] / nageru / ffmpeg_capture.cpp
index b9eb982008f166fbc7cab97b0e3acc990ee19340..2d1b3385b4ac0d64c137bba4c3590819f06f05f6 100644 (file)
@@ -27,6 +27,10 @@ extern "C" {
 #include <utility>
 #include <vector>
 
+#include <Eigen/Core>
+#include <Eigen/LU>
+#include <movit/colorspace_conversion_effect.h>
+
 #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);