]> git.sesse.net Git - nageru/blob - ffmpeg_capture.h
Add a Lua function to rewind the video.
[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 // (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.
19 //
20 // There is currently no audio support. There is also no support for changing
21 // the video underway (unlike images), although there really should be.
22 // Finally, there is currently no support for controlling the video from Lua.
23
24 #include <assert.h>
25 #include <stdint.h>
26 #include <functional>
27 #include <map>
28 #include <memory>
29 #include <mutex>
30 #include <set>
31 #include <string>
32 #include <thread>
33
34 #include "bmusb/bmusb.h"
35
36 class FFmpegCapture : public bmusb::CaptureInterface
37 {
38 public:
39         FFmpegCapture(const std::string &filename, unsigned width, unsigned height);
40         ~FFmpegCapture();
41
42         void set_card_index(int card_index)
43         {
44                 this->card_index = card_index;
45         }
46
47         int get_card_index() const
48         {
49                 return card_index;
50         }
51
52         void rewind()
53         {
54                 std::lock_guard<std::mutex> lock(queue_mu);
55                 command_queue.push_back(QueuedCommand { QueuedCommand::REWIND });
56         }
57
58         // CaptureInterface.
59         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
60         {
61                 video_frame_allocator = allocator;
62                 if (owned_video_frame_allocator.get() != allocator) {
63                         owned_video_frame_allocator.reset();
64                 }
65         }
66
67         bmusb::FrameAllocator *get_video_frame_allocator() override
68         {
69                 return video_frame_allocator;
70         }
71
72         // Does not take ownership.
73         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
74         {
75                 audio_frame_allocator = allocator;
76                 if (owned_audio_frame_allocator.get() != allocator) {
77                         owned_audio_frame_allocator.reset();
78                 }
79         }
80
81         bmusb::FrameAllocator *get_audio_frame_allocator() override
82         {
83                 return audio_frame_allocator;
84         }
85
86         void set_frame_callback(bmusb::frame_callback_t callback) override
87         {
88                 frame_callback = callback;
89         }
90
91         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
92         {
93                 dequeue_init_callback = init;
94                 dequeue_cleanup_callback = cleanup;
95                 has_dequeue_callbacks = true;
96         }
97
98         std::string get_description() const override
99         {
100                 return description;
101         }
102
103         void configure_card() override;
104         void start_bm_capture() override;
105         void stop_dequeue_thread() override;
106
107         // TODO: Specify error status through this.
108         bool get_disconnected() const override { return false; }
109
110         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const;
111         void set_video_mode(uint32_t video_mode_id) override {}  // Ignore.
112         uint32_t get_current_video_mode() const override { return 0; }
113
114         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
115                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitRGBA };
116         }
117         void set_pixel_format(bmusb::PixelFormat pixel_format) override {
118                 assert(pixel_format == bmusb::PixelFormat_8BitRGBA);
119         }       
120         bmusb::PixelFormat get_current_pixel_format() const override {
121                 return bmusb::PixelFormat_8BitRGBA;
122         }
123
124         std::map<uint32_t, std::string> get_available_video_inputs() const override {
125                 return { { 0, "Auto" } }; }
126         void set_video_input(uint32_t video_input_id) override {}  // Ignore.
127         uint32_t get_current_video_input() const override { return 0; }
128
129         std::map<uint32_t, std::string> get_available_audio_inputs() const override {
130                 return { { 0, "Embedded" } };
131         }
132         void set_audio_input(uint32_t audio_input_id) override {}  // Ignore.
133         uint32_t get_current_audio_input() const override { return 0; }
134
135 private:
136         void producer_thread_func();
137         bool play_video(const std::string &pathname);
138
139         std::string description, filename;
140         uint16_t timecode = 0;
141         unsigned width, height;
142         bool running = false;
143         int card_index = -1;
144
145         bool has_dequeue_callbacks = false;
146         std::function<void()> dequeue_init_callback = nullptr;
147         std::function<void()> dequeue_cleanup_callback = nullptr;
148
149         bmusb::FrameAllocator *video_frame_allocator = nullptr;
150         bmusb::FrameAllocator *audio_frame_allocator = nullptr;
151         std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
152         std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
153         bmusb::frame_callback_t frame_callback = nullptr;
154
155         std::atomic<bool> producer_thread_should_quit{false};
156         std::thread producer_thread;
157
158         std::mutex queue_mu;
159         struct QueuedCommand {
160                 enum Command { REWIND } command;
161         };
162         std::vector<QueuedCommand> command_queue;  // Protected by <queue_mu>.
163 };
164
165 #endif  // !defined(_FFMPEG_CAPTURE_H)