1 #ifndef _FFMPEG_CAPTURE_H
2 #define _FFMPEG_CAPTURE_H 1
4 // FFmpegCapture looks much like a capture card, but the frames it spits out
5 // come from a video in real time, looping. Because it decodes the video using
6 // FFmpeg (thus the name), this means it can handle a very wide array of video
7 // formats, and also things like network streaming and V4L capture, but it is
8 // also significantly less integrated and optimized than the regular capture
9 // cards. In particular, the frames are always scaled and converted to 8-bit
10 // RGBA on the CPU before being sent on to the GPU.
12 // Since we don't really know much about the video when building the chains,
13 // there are some limitations. In particular, frames are always assumed to be
14 // sRGB even if the video container says something else. We could probably
15 // try to load the video on startup and pick out the parameters at that point
16 // (which would probably also allow us to send Y'CbCr video through without
17 // CPU conversion), but it would require some more plumbing, and it would also
18 // fail if the file changes parameters midway, which is allowed in some formats.
20 // There is currently no audio support.
32 #include "bmusb/bmusb.h"
33 #include "quittable_sleeper.h"
35 class FFmpegCapture : public bmusb::CaptureInterface
38 FFmpegCapture(const std::string &filename, unsigned width, unsigned height);
41 void set_card_index(int card_index)
43 this->card_index = card_index;
46 int get_card_index() const
53 std::lock_guard<std::mutex> lock(queue_mu);
54 command_queue.push_back(QueuedCommand { QueuedCommand::REWIND });
57 void change_rate(double new_rate)
59 std::lock_guard<std::mutex> lock(queue_mu);
60 command_queue.push_back(QueuedCommand { QueuedCommand::CHANGE_RATE, new_rate });
64 void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
66 video_frame_allocator = allocator;
67 if (owned_video_frame_allocator.get() != allocator) {
68 owned_video_frame_allocator.reset();
72 bmusb::FrameAllocator *get_video_frame_allocator() override
74 return video_frame_allocator;
77 // Does not take ownership.
78 void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
80 audio_frame_allocator = allocator;
81 if (owned_audio_frame_allocator.get() != allocator) {
82 owned_audio_frame_allocator.reset();
86 bmusb::FrameAllocator *get_audio_frame_allocator() override
88 return audio_frame_allocator;
91 void set_frame_callback(bmusb::frame_callback_t callback) override
93 frame_callback = callback;
96 void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
98 dequeue_init_callback = init;
99 dequeue_cleanup_callback = cleanup;
100 has_dequeue_callbacks = true;
103 std::string get_description() const override
108 void configure_card() override;
109 void start_bm_capture() override;
110 void stop_dequeue_thread() override;
111 bool get_disconnected() const override { return false; } // We never unplug.
113 std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const;
114 void set_video_mode(uint32_t video_mode_id) override {} // Ignore.
115 uint32_t get_current_video_mode() const override { return 0; }
117 std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
118 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA };
120 void set_pixel_format(bmusb::PixelFormat pixel_format) override {
121 assert(pixel_format == bmusb::PixelFormat_8BitBGRA);
123 bmusb::PixelFormat get_current_pixel_format() const override {
124 return bmusb::PixelFormat_8BitBGRA;
127 std::map<uint32_t, std::string> get_available_video_inputs() const override {
128 return { { 0, "Auto" } }; }
129 void set_video_input(uint32_t video_input_id) override {} // Ignore.
130 uint32_t get_current_video_input() const override { return 0; }
132 std::map<uint32_t, std::string> get_available_audio_inputs() const override {
133 return { { 0, "Embedded" } };
135 void set_audio_input(uint32_t audio_input_id) override {} // Ignore.
136 uint32_t get_current_audio_input() const override { return 0; }
139 void producer_thread_func();
140 void send_disconnected_frame();
141 bool play_video(const std::string &pathname);
142 void internal_rewind();
144 std::string description, filename;
145 uint16_t timecode = 0;
146 unsigned width, height;
147 bool running = false;
150 bool has_dequeue_callbacks = false;
151 std::function<void()> dequeue_init_callback = nullptr;
152 std::function<void()> dequeue_cleanup_callback = nullptr;
154 bmusb::FrameAllocator *video_frame_allocator = nullptr;
155 bmusb::FrameAllocator *audio_frame_allocator = nullptr;
156 std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
157 std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
158 bmusb::frame_callback_t frame_callback = nullptr;
160 QuittableSleeper producer_thread_should_quit;
161 std::thread producer_thread;
163 int64_t pts_origin, last_pts;
164 std::chrono::steady_clock::time_point start, next_frame_start;
167 struct QueuedCommand {
168 enum Command { REWIND, CHANGE_RATE } command;
169 double new_rate; // For CHANGE_RATE.
171 std::vector<QueuedCommand> command_queue; // Protected by <queue_mu>.
174 #endif // !defined(_FFMPEG_CAPTURE_H)