#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"
#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 {
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)
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) {
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.
}
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)
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;
// 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);
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