]> 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 b4fec063ce702a01dde9d2f7655a553fc471336c..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 {
 
@@ -191,7 +196,7 @@ YCbCrFormat decode_ycbcr_format(const AVPixFmtDescriptor *desc, const AVFrame *f
                format.cb_y_position = 1.0;
                break;
        default:
-               fprintf(stderr, "Unknown chroma location coefficient enum %d from FFmpeg; choosing Rec. 709.\n",
+               fprintf(stderr, "Unknown chroma location coefficient enum %d from FFmpeg; choosing center.\n",
                        frame->chroma_location);
                format.cb_x_position = 0.5;
                format.cb_y_position = 0.5;
@@ -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);
@@ -793,12 +825,12 @@ void FFmpegCapture::convert_audio(const AVFrame *audio_avframe, FrameAllocator::
 
                if (resampler == nullptr) {
                        fprintf(stderr, "Allocating resampler failed.\n");
-                       exit(1);
+                       abort();
                }
 
                if (swr_init(resampler) < 0) {
                        fprintf(stderr, "Could not open resample context.\n");
-                       exit(1);
+                       abort();
                }
 
                last_src_format = AVSampleFormat(audio_avframe->format);
@@ -815,7 +847,7 @@ void FFmpegCapture::convert_audio(const AVFrame *audio_avframe, FrameAllocator::
                const_cast<const uint8_t **>(audio_avframe->data), audio_avframe->nb_samples);
        if (out_samples < 0) {
                 fprintf(stderr, "Audio conversion failed.\n");
-                exit(1);
+                abort();
         }
 
        audio_frame->len += out_samples * bytes_per_sample;