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