]> git.sesse.net Git - nageru/blob - ffmpeg_capture.h
In FFmpegCapture, refactor command queue processing into its own function.
[nageru] / ffmpeg_capture.h
1 #ifndef _FFMPEG_CAPTURE_H
2 #define _FFMPEG_CAPTURE_H 1
3
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.
11 //
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.
18 //
19 // There is currently no audio support.
20
21 #include <assert.h>
22 #include <stdint.h>
23 #include <functional>
24 #include <map>
25 #include <memory>
26 #include <mutex>
27 #include <set>
28 #include <string>
29 #include <thread>
30
31 #include <movit/ycbcr.h>
32
33 #include "bmusb/bmusb.h"
34 #include "quittable_sleeper.h"
35
36 struct AVFormatContext;
37
38 class FFmpegCapture : public bmusb::CaptureInterface
39 {
40 public:
41         FFmpegCapture(const std::string &filename, unsigned width, unsigned height);
42         ~FFmpegCapture();
43
44         void set_card_index(int card_index)
45         {
46                 this->card_index = card_index;
47         }
48
49         int get_card_index() const
50         {
51                 return card_index;
52         }
53
54         void rewind()
55         {
56                 std::lock_guard<std::mutex> lock(queue_mu);
57                 command_queue.push_back(QueuedCommand { QueuedCommand::REWIND });
58         }
59
60         void change_rate(double new_rate)
61         {
62                 std::lock_guard<std::mutex> lock(queue_mu);
63                 command_queue.push_back(QueuedCommand { QueuedCommand::CHANGE_RATE, new_rate });
64         }
65
66         // CaptureInterface.
67         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
68         {
69                 video_frame_allocator = allocator;
70                 if (owned_video_frame_allocator.get() != allocator) {
71                         owned_video_frame_allocator.reset();
72                 }
73         }
74
75         bmusb::FrameAllocator *get_video_frame_allocator() override
76         {
77                 return video_frame_allocator;
78         }
79
80         // Does not take ownership.
81         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
82         {
83                 audio_frame_allocator = allocator;
84                 if (owned_audio_frame_allocator.get() != allocator) {
85                         owned_audio_frame_allocator.reset();
86                 }
87         }
88
89         bmusb::FrameAllocator *get_audio_frame_allocator() override
90         {
91                 return audio_frame_allocator;
92         }
93
94         void set_frame_callback(bmusb::frame_callback_t callback) override
95         {
96                 frame_callback = callback;
97         }
98
99         // Used to get precise information about the Y'CbCr format used
100         // for a given frame. Only valid to call during the frame callback,
101         // and only when receiving a frame with pixel format PixelFormat_8BitYCbCrPlanar.
102         movit::YCbCrFormat get_current_frame_ycbcr_format() const
103         {
104                 return current_frame_ycbcr_format;
105         }
106
107         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
108         {
109                 dequeue_init_callback = init;
110                 dequeue_cleanup_callback = cleanup;
111                 has_dequeue_callbacks = true;
112         }
113
114         std::string get_description() const override
115         {
116                 return description;
117         }
118
119         void configure_card() override;
120         void start_bm_capture() override;
121         void stop_dequeue_thread() override;
122         bool get_disconnected() const override { return false; }  // We never unplug.
123
124         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const;
125         void set_video_mode(uint32_t video_mode_id) override {}  // Ignore.
126         uint32_t get_current_video_mode() const override { return 0; }
127
128         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
129                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA, bmusb::PixelFormat_8BitYCbCrPlanar };
130         }
131         void set_pixel_format(bmusb::PixelFormat pixel_format) override {
132                 this->pixel_format = pixel_format;
133         }       
134         bmusb::PixelFormat get_current_pixel_format() const override {
135                 return pixel_format;
136         }
137
138         std::map<uint32_t, std::string> get_available_video_inputs() const override {
139                 return { { 0, "Auto" } }; }
140         void set_video_input(uint32_t video_input_id) override {}  // Ignore.
141         uint32_t get_current_video_input() const override { return 0; }
142
143         std::map<uint32_t, std::string> get_available_audio_inputs() const override {
144                 return { { 0, "Embedded" } };
145         }
146         void set_audio_input(uint32_t audio_input_id) override {}  // Ignore.
147         uint32_t get_current_audio_input() const override { return 0; }
148
149 private:
150         void producer_thread_func();
151         void send_disconnected_frame();
152         bool play_video(const std::string &pathname);
153         void internal_rewind();
154
155         // Returns true if there was an error.
156         bool process_queued_commands(AVFormatContext *format_ctx, const std::string &pathname, timespec last_modified);
157
158         std::string description, filename;
159         uint16_t timecode = 0;
160         unsigned width, height;
161         bmusb::PixelFormat pixel_format = bmusb::PixelFormat_8BitBGRA;
162         movit::YCbCrFormat current_frame_ycbcr_format;
163         bool running = false;
164         int card_index = -1;
165         double rate = 1.0;
166
167         bool has_dequeue_callbacks = false;
168         std::function<void()> dequeue_init_callback = nullptr;
169         std::function<void()> dequeue_cleanup_callback = nullptr;
170
171         bmusb::FrameAllocator *video_frame_allocator = nullptr;
172         bmusb::FrameAllocator *audio_frame_allocator = nullptr;
173         std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
174         std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
175         bmusb::frame_callback_t frame_callback = nullptr;
176
177         QuittableSleeper producer_thread_should_quit;
178         std::thread producer_thread;
179
180         int64_t pts_origin, last_pts;
181         std::chrono::steady_clock::time_point start, next_frame_start;
182
183         std::mutex queue_mu;
184         struct QueuedCommand {
185                 enum Command { REWIND, CHANGE_RATE } command;
186                 double new_rate;  // For CHANGE_RATE.
187         };
188         std::vector<QueuedCommand> command_queue;  // Protected by <queue_mu>.
189 };
190
191 #endif  // !defined(_FFMPEG_CAPTURE_H)