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 // but it would require some more plumbing, and it would also fail if the file
17 // changes parameters midway, which is allowed in some formats.
19 // There is currently no audio support.
31 #include <movit/ycbcr.h>
34 #include <libavutil/pixfmt.h>
37 #include "bmusb/bmusb.h"
38 #include "ffmpeg_raii.h"
39 #include "quittable_sleeper.h"
41 struct AVFormatContext;
45 class FFmpegCapture : public bmusb::CaptureInterface
48 FFmpegCapture(const std::string &filename, unsigned width, unsigned height);
51 void set_card_index(int card_index)
53 this->card_index = card_index;
56 int get_card_index() const
63 std::lock_guard<std::mutex> lock(queue_mu);
64 command_queue.push_back(QueuedCommand { QueuedCommand::REWIND });
67 void change_rate(double new_rate)
69 std::lock_guard<std::mutex> lock(queue_mu);
70 command_queue.push_back(QueuedCommand { QueuedCommand::CHANGE_RATE, new_rate });
74 void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
76 video_frame_allocator = allocator;
77 if (owned_video_frame_allocator.get() != allocator) {
78 owned_video_frame_allocator.reset();
82 bmusb::FrameAllocator *get_video_frame_allocator() override
84 return video_frame_allocator;
87 // Does not take ownership.
88 void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
90 audio_frame_allocator = allocator;
91 if (owned_audio_frame_allocator.get() != allocator) {
92 owned_audio_frame_allocator.reset();
96 bmusb::FrameAllocator *get_audio_frame_allocator() override
98 return audio_frame_allocator;
101 void set_frame_callback(bmusb::frame_callback_t callback) override
103 frame_callback = callback;
106 // Used to get precise information about the Y'CbCr format used
107 // for a given frame. Only valid to call during the frame callback,
108 // and only when receiving a frame with pixel format PixelFormat_8BitYCbCrPlanar.
109 movit::YCbCrFormat get_current_frame_ycbcr_format() const
111 return current_frame_ycbcr_format;
114 void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
116 dequeue_init_callback = init;
117 dequeue_cleanup_callback = cleanup;
118 has_dequeue_callbacks = true;
121 std::string get_description() const override
126 void configure_card() override;
127 void start_bm_capture() override;
128 void stop_dequeue_thread() override;
129 bool get_disconnected() const override { return false; } // We never unplug.
131 std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const;
132 void set_video_mode(uint32_t video_mode_id) override {} // Ignore.
133 uint32_t get_current_video_mode() const override { return 0; }
135 std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
136 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA, bmusb::PixelFormat_8BitYCbCrPlanar };
138 void set_pixel_format(bmusb::PixelFormat pixel_format) override {
139 this->pixel_format = pixel_format;
141 bmusb::PixelFormat get_current_pixel_format() const override {
145 std::map<uint32_t, std::string> get_available_video_inputs() const override {
146 return { { 0, "Auto" } }; }
147 void set_video_input(uint32_t video_input_id) override {} // Ignore.
148 uint32_t get_current_video_input() const override { return 0; }
150 std::map<uint32_t, std::string> get_available_audio_inputs() const override {
151 return { { 0, "Embedded" } };
153 void set_audio_input(uint32_t audio_input_id) override {} // Ignore.
154 uint32_t get_current_audio_input() const override { return 0; }
157 void producer_thread_func();
158 void send_disconnected_frame();
159 bool play_video(const std::string &pathname);
160 void internal_rewind();
162 // Returns true if there was an error.
163 bool process_queued_commands(AVFormatContext *format_ctx, const std::string &pathname, timespec last_modified);
165 // Returns nullptr if no frame was decoded (e.g. EOF).
166 AVFrameWithDeleter decode_frame(AVFormatContext *format_ctx, AVCodecContext *codec_ctx, const std::string &pathname, int video_stream_index, bool *error);
168 bmusb::VideoFormat construct_video_format(const AVFrame *frame, AVRational video_timebase);
169 bmusb::FrameAllocator::Frame make_video_frame(const AVFrame *frame, const std::string &pathname, bool *error);
171 std::string description, filename;
172 uint16_t timecode = 0;
173 unsigned width, height;
174 bmusb::PixelFormat pixel_format = bmusb::PixelFormat_8BitBGRA;
175 movit::YCbCrFormat current_frame_ycbcr_format;
176 bool running = false;
180 bool has_dequeue_callbacks = false;
181 std::function<void()> dequeue_init_callback = nullptr;
182 std::function<void()> dequeue_cleanup_callback = nullptr;
184 bmusb::FrameAllocator *video_frame_allocator = nullptr;
185 bmusb::FrameAllocator *audio_frame_allocator = nullptr;
186 std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
187 std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
188 bmusb::frame_callback_t frame_callback = nullptr;
190 SwsContextWithDeleter sws_ctx;
191 int sws_last_width = -1, sws_last_height = -1, sws_last_src_format = -1;
192 AVPixelFormat sws_dst_format = AVPixelFormat(-1); // In practice, always initialized.
194 QuittableSleeper producer_thread_should_quit;
195 std::thread producer_thread;
197 int64_t pts_origin, last_pts;
198 std::chrono::steady_clock::time_point start, next_frame_start;
201 struct QueuedCommand {
202 enum Command { REWIND, CHANGE_RATE } command;
203 double new_rate; // For CHANGE_RATE.
205 std::vector<QueuedCommand> command_queue; // Protected by <queue_mu>.
208 #endif // !defined(_FFMPEG_CAPTURE_H)