]> git.sesse.net Git - nageru/blob - ffmpeg_capture.h
Add support for FFmpeg inputs.
[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 <set>
30 #include <string>
31 #include <thread>
32
33 #include "bmusb/bmusb.h"
34
35 class FFmpegCapture : public bmusb::CaptureInterface
36 {
37 public:
38         FFmpegCapture(const std::string &filename, unsigned width, unsigned height);
39         ~FFmpegCapture();
40
41         void set_card_index(int card_index)
42         {
43                 this->card_index = card_index;
44         }
45
46         int get_card_index() const
47         {
48                 return card_index;
49         }
50
51         // CaptureInterface.
52         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
53         {
54                 video_frame_allocator = allocator;
55                 if (owned_video_frame_allocator.get() != allocator) {
56                         owned_video_frame_allocator.reset();
57                 }
58         }
59
60         bmusb::FrameAllocator *get_video_frame_allocator() override
61         {
62                 return video_frame_allocator;
63         }
64
65         // Does not take ownership.
66         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
67         {
68                 audio_frame_allocator = allocator;
69                 if (owned_audio_frame_allocator.get() != allocator) {
70                         owned_audio_frame_allocator.reset();
71                 }
72         }
73
74         bmusb::FrameAllocator *get_audio_frame_allocator() override
75         {
76                 return audio_frame_allocator;
77         }
78
79         void set_frame_callback(bmusb::frame_callback_t callback) override
80         {
81                 frame_callback = callback;
82         }
83
84         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
85         {
86                 dequeue_init_callback = init;
87                 dequeue_cleanup_callback = cleanup;
88                 has_dequeue_callbacks = true;
89         }
90
91         std::string get_description() const override
92         {
93                 return description;
94         }
95
96         void configure_card() override;
97         void start_bm_capture() override;
98         void stop_dequeue_thread() override;
99
100         // TODO: Specify error status through this.
101         bool get_disconnected() const override { return false; }
102
103         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const;
104         void set_video_mode(uint32_t video_mode_id) override {}  // Ignore.
105         uint32_t get_current_video_mode() const override { return 0; }
106
107         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
108                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitRGBA };
109         }
110         void set_pixel_format(bmusb::PixelFormat pixel_format) override {
111                 assert(pixel_format == bmusb::PixelFormat_8BitRGBA);
112         }       
113         bmusb::PixelFormat get_current_pixel_format() const override {
114                 return bmusb::PixelFormat_8BitRGBA;
115         }
116
117         std::map<uint32_t, std::string> get_available_video_inputs() const override {
118                 return { { 0, "Auto" } }; }
119         void set_video_input(uint32_t video_input_id) override {}  // Ignore.
120         uint32_t get_current_video_input() const override { return 0; }
121
122         std::map<uint32_t, std::string> get_available_audio_inputs() const override {
123                 return { { 0, "Embedded" } };
124         }
125         void set_audio_input(uint32_t audio_input_id) override {}  // Ignore.
126         uint32_t get_current_audio_input() const override { return 0; }
127
128 private:
129         void producer_thread_func();
130         bool play_video(const std::string &pathname);
131
132         std::string description, filename;
133         uint16_t timecode = 0;
134         unsigned width, height;
135         bool running = false;
136         int card_index = -1;
137
138         bool has_dequeue_callbacks = false;
139         std::function<void()> dequeue_init_callback = nullptr;
140         std::function<void()> dequeue_cleanup_callback = nullptr;
141
142         bmusb::FrameAllocator *video_frame_allocator = nullptr;
143         bmusb::FrameAllocator *audio_frame_allocator = nullptr;
144         std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
145         std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
146         bmusb::frame_callback_t frame_callback = nullptr;
147
148         std::atomic<bool> producer_thread_should_quit{false};
149         std::thread producer_thread;
150 };
151
152 #endif  // !defined(_FFMPEG_CAPTURE_H)