]> git.sesse.net Git - nageru/blob - ffmpeg_capture.h
Fix an outdated comment.
[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 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         void change_rate(double new_rate)
59         {
60                 std::lock_guard<std::mutex> lock(queue_mu);
61                 command_queue.push_back(QueuedCommand { QueuedCommand::CHANGE_RATE, new_rate });
62         }
63
64         // CaptureInterface.
65         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
66         {
67                 video_frame_allocator = allocator;
68                 if (owned_video_frame_allocator.get() != allocator) {
69                         owned_video_frame_allocator.reset();
70                 }
71         }
72
73         bmusb::FrameAllocator *get_video_frame_allocator() override
74         {
75                 return video_frame_allocator;
76         }
77
78         // Does not take ownership.
79         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
80         {
81                 audio_frame_allocator = allocator;
82                 if (owned_audio_frame_allocator.get() != allocator) {
83                         owned_audio_frame_allocator.reset();
84                 }
85         }
86
87         bmusb::FrameAllocator *get_audio_frame_allocator() override
88         {
89                 return audio_frame_allocator;
90         }
91
92         void set_frame_callback(bmusb::frame_callback_t callback) override
93         {
94                 frame_callback = callback;
95         }
96
97         // Used to get precise information about the Y'CbCr format used
98         // for a given frame. Only valid to call during the frame callback,
99         // and only when receiving a frame with pixel format PixelFormat_8BitYCbCrPlanar.
100         movit::YCbCrFormat get_current_frame_ycbcr_format() const
101         {
102                 return current_frame_ycbcr_format;
103         }
104
105         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
106         {
107                 dequeue_init_callback = init;
108                 dequeue_cleanup_callback = cleanup;
109                 has_dequeue_callbacks = true;
110         }
111
112         std::string get_description() const override
113         {
114                 return description;
115         }
116
117         void configure_card() override;
118         void start_bm_capture() override;
119         void stop_dequeue_thread() override;
120         bool get_disconnected() const override { return false; }  // We never unplug.
121
122         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const;
123         void set_video_mode(uint32_t video_mode_id) override {}  // Ignore.
124         uint32_t get_current_video_mode() const override { return 0; }
125
126         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
127                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA, bmusb::PixelFormat_8BitYCbCrPlanar };
128         }
129         void set_pixel_format(bmusb::PixelFormat pixel_format) override {
130                 this->pixel_format = pixel_format;
131         }       
132         bmusb::PixelFormat get_current_pixel_format() const override {
133                 return pixel_format;
134         }
135
136         std::map<uint32_t, std::string> get_available_video_inputs() const override {
137                 return { { 0, "Auto" } }; }
138         void set_video_input(uint32_t video_input_id) override {}  // Ignore.
139         uint32_t get_current_video_input() const override { return 0; }
140
141         std::map<uint32_t, std::string> get_available_audio_inputs() const override {
142                 return { { 0, "Embedded" } };
143         }
144         void set_audio_input(uint32_t audio_input_id) override {}  // Ignore.
145         uint32_t get_current_audio_input() const override { return 0; }
146
147 private:
148         void producer_thread_func();
149         void send_disconnected_frame();
150         bool play_video(const std::string &pathname);
151         void internal_rewind();
152
153         std::string description, filename;
154         uint16_t timecode = 0;
155         unsigned width, height;
156         bmusb::PixelFormat pixel_format = bmusb::PixelFormat_8BitBGRA;
157         movit::YCbCrFormat current_frame_ycbcr_format;
158         bool running = false;
159         int card_index = -1;
160
161         bool has_dequeue_callbacks = false;
162         std::function<void()> dequeue_init_callback = nullptr;
163         std::function<void()> dequeue_cleanup_callback = nullptr;
164
165         bmusb::FrameAllocator *video_frame_allocator = nullptr;
166         bmusb::FrameAllocator *audio_frame_allocator = nullptr;
167         std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
168         std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
169         bmusb::frame_callback_t frame_callback = nullptr;
170
171         QuittableSleeper producer_thread_should_quit;
172         std::thread producer_thread;
173
174         int64_t pts_origin, last_pts;
175         std::chrono::steady_clock::time_point start, next_frame_start;
176
177         std::mutex queue_mu;
178         struct QueuedCommand {
179                 enum Command { REWIND, CHANGE_RATE } command;
180                 double new_rate;  // For CHANGE_RATE.
181         };
182         std::vector<QueuedCommand> command_queue;  // Protected by <queue_mu>.
183 };
184
185 #endif  // !defined(_FFMPEG_CAPTURE_H)