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