]> git.sesse.net Git - nageru/blobdiff - nageru/ffmpeg_capture.cpp
Support SRT inputs.
[nageru] / nageru / ffmpeg_capture.cpp
index b9eb982008f166fbc7cab97b0e3acc990ee19340..4bc277af61fc05405c8c264fbecc134de2ce59ef 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"
@@ -35,12 +39,17 @@ extern "C" {
 #include "ref_counted_frame.h"
 #include "shared/timebase.h"
 
+#ifdef HAVE_SRT
+#include <srt/srt.h>
+#endif
+
 #define FRAME_SIZE (8 << 20)  // 8 MB.
 
 using namespace std;
 using namespace std::chrono;
 using namespace bmusb;
 using namespace movit;
+using namespace Eigen;
 
 namespace {
 
@@ -214,6 +223,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)
@@ -226,6 +261,25 @@ FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned he
        avformat_network_init();  // In case someone wants this.
 }
 
+#ifdef HAVE_SRT
+FFmpegCapture::FFmpegCapture(int srt_sock, const string &stream_id)
+       : srt_sock(srt_sock),
+         width(global_flags.width),
+         height(global_flags.height),
+         pixel_format(bmusb::PixelFormat_8BitYCbCrPlanar),
+         video_timebase{1, 1}
+{
+       if (stream_id.empty()) {
+               description = "SRT stream";
+       } else {
+               description = stream_id;
+       }
+       play_as_fast_as_possible = true;
+       play_once = true;
+       last_frame = steady_clock::now();
+}
+#endif
+
 FFmpegCapture::~FFmpegCapture()
 {
        if (has_dequeue_callbacks) {
@@ -299,22 +353,38 @@ void FFmpegCapture::producer_thread_func()
                        filename_copy = filename;
                }
 
-               string pathname = search_for_file(filename_copy);
+               string pathname;
+               if (srt_sock == -1) {
+                       pathname = search_for_file(filename_copy);
+               } else {
+                       pathname = description;
+               }
                if (pathname.empty()) {
-                       fprintf(stderr, "%s not found, sleeping one second and trying again...\n", filename_copy.c_str());
                        send_disconnected_frame();
+                       if (play_once) {
+                               break;
+                       }
                        producer_thread_should_quit.sleep_for(seconds(1));
+                       fprintf(stderr, "%s not found, sleeping one second and trying again...\n", filename_copy.c_str());
                        continue;
                }
                should_interrupt = false;
                if (!play_video(pathname)) {
                        // Error.
-                       fprintf(stderr, "Error when playing %s, sleeping one second and trying again...\n", pathname.c_str());
                        send_disconnected_frame();
+                       if (play_once) {
+                               break;
+                       }
+                       fprintf(stderr, "Error when playing %s, sleeping one second and trying again...\n", pathname.c_str());
                        producer_thread_should_quit.sleep_for(seconds(1));
                        continue;
                }
 
+               if (play_once) {
+                       send_disconnected_frame();
+                       break;
+               }
+
                // Probably just EOF, will exit the loop above on next test.
        }
 
@@ -360,6 +430,13 @@ void FFmpegCapture::send_disconnected_frame()
                        FrameAllocator::Frame(), /*audio_offset=*/0, AudioFormat());
                last_frame_was_connected = false;
        }
+
+       if (play_once) {
+               disconnected = true;
+               if (card_disconnected_callback != nullptr) {
+                       card_disconnected_callback();
+               }
+       }
 }
 
 bool FFmpegCapture::play_video(const string &pathname)
@@ -376,7 +453,23 @@ bool FFmpegCapture::play_video(const string &pathname)
                last_modified = buf.st_mtim;
        }
 
-       auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, nullptr, AVIOInterruptCB{ &FFmpegCapture::interrupt_cb_thunk, this });
+       AVFormatContextWithCloser format_ctx;
+       if (srt_sock == -1) {
+               // Regular file.
+               format_ctx = avformat_open_input_unique(pathname.c_str(), /*fmt=*/nullptr,
+                       /*options=*/nullptr,
+                       AVIOInterruptCB{ &FFmpegCapture::interrupt_cb_thunk, this });
+       } else {
+#ifdef HAVE_SRT
+               // SRT socket, already opened.
+               AVInputFormat *mpegts_fmt = av_find_input_format("mpegts");
+               format_ctx = avformat_open_input_unique(&FFmpegCapture::read_srt_thunk, this,
+                       mpegts_fmt, /*options=*/nullptr,
+                       AVIOInterruptCB{ &FFmpegCapture::interrupt_cb_thunk, this });
+#else
+               assert(false);
+#endif
+       }
        if (format_ctx == nullptr) {
                fprintf(stderr, "%s: Error opening file\n", pathname.c_str());
                return false;
@@ -575,6 +668,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);
@@ -916,12 +1010,25 @@ UniqueFrame FFmpegCapture::make_video_frame(const AVFrame *frame, const string &
        return video_frame;
 }
 
-int FFmpegCapture::interrupt_cb_thunk(void *unique)
+int FFmpegCapture::interrupt_cb_thunk(void *opaque)
 {
-       return reinterpret_cast<FFmpegCapture *>(unique)->interrupt_cb();
+       return reinterpret_cast<FFmpegCapture *>(opaque)->interrupt_cb();
 }
 
 int FFmpegCapture::interrupt_cb()
 {
        return should_interrupt.load();
 }
+
+#ifdef HAVE_SRT
+int FFmpegCapture::read_srt_thunk(void *opaque, uint8_t *buf, int buf_size)
+{
+       return reinterpret_cast<FFmpegCapture *>(opaque)->read_srt(buf, buf_size);
+}
+
+int FFmpegCapture::read_srt(uint8_t *buf, int buf_size)
+{
+       SRT_MSGCTRL mc = srt_msgctrl_default;
+       return srt_recvmsg2(srt_sock, reinterpret_cast<char *>(buf), buf_size, &mc);
+}
+#endif