]> git.sesse.net Git - nageru/blob - nageru/ffmpeg_capture.h
fc584827a373e791ae85123a3a58de3d553b72c7
[nageru] / 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 // You can get out the audio either as decoded or in raw form (Kaeru uses this).
20 //
21 // If there's a subtitle track, you can also get out the last subtitle at the
22 // point of the frame. Note that once we get a video frame, we don't look for
23 // subtitle, so if subtitles and a frame comes at the same time, you might not
24 // see the subtitle until the next frame.
25
26 #include <assert.h>
27 #include <stdint.h>
28 #include <functional>
29 #include <map>
30 #include <memory>
31 #include <mutex>
32 #include <set>
33 #include <string>
34 #include <thread>
35
36 #include <movit/effect.h>
37 #include <movit/ycbcr.h>
38
39 extern "C" {
40 #include <libswresample/swresample.h>
41 #include <libavutil/channel_layout.h>
42 #include <libavutil/pixfmt.h>
43 #include <libavutil/rational.h>
44 #include <libavutil/samplefmt.h>
45 }
46
47 #include "bmusb/bmusb.h"
48 #include "shared/ffmpeg_raii.h"
49 #include "ref_counted_frame.h"
50 #include "quittable_sleeper.h"
51
52 struct AVFormatContext;
53 struct AVFrame;
54 struct AVRational;
55 struct AVPacket;
56
57 class FFmpegCapture : public bmusb::CaptureInterface
58 {
59 public:
60         FFmpegCapture(const std::string &filename, unsigned width, unsigned height);
61 #ifdef HAVE_SRT
62         // Takes ownership of the SRT client socket.
63         FFmpegCapture(int srt_sock, const std::string &stream_id);
64 #endif
65         ~FFmpegCapture();
66
67         void set_card_index(int card_index)
68         {
69                 this->card_index = card_index;
70         }
71
72         int get_card_index() const
73         {
74                 return card_index;
75         }
76
77         void rewind()
78         {
79                 std::lock_guard<std::mutex> lock(queue_mu);
80                 command_queue.push_back(QueuedCommand { QueuedCommand::REWIND });
81                 producer_thread_should_quit.wakeup();
82         }
83
84         void change_rate(double new_rate)
85         {
86                 std::lock_guard<std::mutex> lock(queue_mu);
87                 command_queue.push_back(QueuedCommand { QueuedCommand::CHANGE_RATE, new_rate });
88                 producer_thread_should_quit.wakeup();
89         }
90
91         std::string get_filename() const
92         {
93                 std::lock_guard<std::mutex> lock(filename_mu);
94                 return filename;
95         }
96
97         void change_filename(const std::string &new_filename)
98         {
99                 std::lock_guard<std::mutex> lock(filename_mu);
100                 filename = new_filename;
101                 should_interrupt = true;
102         }
103
104         // Will stop the stream even if it's hung on blocking I/O.
105         void disconnect()
106         {
107                 should_interrupt = true;
108         }
109
110         // CaptureInterface.
111         void set_video_frame_allocator(bmusb::FrameAllocator *allocator) override
112         {
113                 video_frame_allocator = allocator;
114                 if (owned_video_frame_allocator.get() != allocator) {
115                         owned_video_frame_allocator.reset();
116                 }
117         }
118
119         bmusb::FrameAllocator *get_video_frame_allocator() override
120         {
121                 return video_frame_allocator;
122         }
123
124         // Does not take ownership.
125         void set_audio_frame_allocator(bmusb::FrameAllocator *allocator) override
126         {
127                 audio_frame_allocator = allocator;
128                 if (owned_audio_frame_allocator.get() != allocator) {
129                         owned_audio_frame_allocator.reset();
130                 }
131         }
132
133         bmusb::FrameAllocator *get_audio_frame_allocator() override
134         {
135                 return audio_frame_allocator;
136         }
137
138         // FFmpegCapture-specific overload of set_frame_callback that also gives
139         // the raw original pts from the video. Negative pts means a dummy frame.
140         typedef std::function<void(int64_t video_pts, AVRational video_timebase, int64_t audio_pts, AVRational audio_timebase,
141                                    uint16_t timecode,
142                                    bmusb::FrameAllocator::Frame video_frame, size_t video_offset, bmusb::VideoFormat video_format,
143                                    bmusb::FrameAllocator::Frame audio_frame, size_t audio_offset, bmusb::AudioFormat audio_format)>
144                 frame_callback_t;
145         void set_frame_callback(frame_callback_t callback)
146         {
147                 frame_callback = callback;
148         }
149
150         void set_frame_callback(bmusb::frame_callback_t callback) override
151         {
152                 frame_callback = std::bind(
153                         callback,
154                         std::placeholders::_5,
155                         std::placeholders::_6,
156                         std::placeholders::_7,
157                         std::placeholders::_8,
158                         std::placeholders::_9,
159                         std::placeholders::_10,
160                         std::placeholders::_11);
161         }
162
163         // FFmpegCapture-specific callback that gives the raw audio/video.
164         typedef std::function<void(const AVPacket *pkt, const AVRational timebase)> packet_callback_t;
165         void set_video_callback(packet_callback_t callback)
166         {
167                 video_callback = callback;
168         }
169         void set_audio_callback(packet_callback_t callback)
170         {
171                 audio_callback = callback;
172         }
173
174         // Used to get precise information about the Y'CbCr format used
175         // for a given frame. Only valid to call during the frame callback,
176         // and only when receiving a frame with pixel format PixelFormat_8BitYCbCrPlanar.
177         movit::YCbCrFormat get_current_frame_ycbcr_format() const
178         {
179                 return current_frame_ycbcr_format;
180         }
181
182         // Only valid to call during the frame callback.
183         std::string get_last_subtitle() const
184         {
185                 return last_subtitle;
186         }
187
188         // Same.
189         bool get_has_last_subtitle() const
190         {
191                 return has_last_subtitle;
192         }
193
194         // Same.
195         movit::RGBTriplet get_last_neutral_color() const
196         {
197                 return last_neutral_color;
198         }
199
200         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
201         {
202                 dequeue_init_callback = init;
203                 dequeue_cleanup_callback = cleanup;
204                 has_dequeue_callbacks = true;
205         }
206
207         void set_card_disconnected_callback(bmusb::card_disconnected_callback_t callback)
208         {
209                 card_disconnected_callback = callback;
210         }
211
212         std::string get_description() const override
213         {
214                 return description;
215         }
216
217         void configure_card() override;
218         void start_bm_capture() override;
219         void stop_dequeue_thread() override;
220         bool get_disconnected() const override { return disconnected; }  // Only if play_once == true.
221         int get_srt_sock() const { return srt_sock; }
222
223         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const override;
224         void set_video_mode(uint32_t video_mode_id) override {}  // Ignore.
225         uint32_t get_current_video_mode() const override { return 0; }
226
227         static constexpr bmusb::PixelFormat PixelFormat_NV12 = static_cast<bmusb::PixelFormat>(100);  // In the private range.
228         std::set<bmusb::PixelFormat> get_available_pixel_formats() const override {
229                 return std::set<bmusb::PixelFormat>{ bmusb::PixelFormat_8BitBGRA, bmusb::PixelFormat_8BitYCbCrPlanar, PixelFormat_NV12 };
230         }
231         void set_pixel_format(bmusb::PixelFormat pixel_format) override {
232                 this->pixel_format = pixel_format;
233         }       
234         bmusb::PixelFormat get_current_pixel_format() const override {
235                 return pixel_format;
236         }
237
238         std::map<uint32_t, std::string> get_available_video_inputs() const override {
239                 return { { 0, "Auto" } }; }
240         void set_video_input(uint32_t video_input_id) override {}  // Ignore.
241         uint32_t get_current_video_input() const override { return 0; }
242
243         std::map<uint32_t, std::string> get_available_audio_inputs() const override {
244                 return { { 0, "Embedded" } };
245         }
246         void set_audio_input(uint32_t audio_input_id) override {}  // Ignore.
247         uint32_t get_current_audio_input() const override { return 0; }
248
249 private:
250         void producer_thread_func();
251         void send_disconnected_frame();
252         bool play_video(const std::string &pathname);
253         void internal_rewind();
254
255         // Returns true if there was an error.
256         bool process_queued_commands(AVFormatContext *format_ctx, const std::string &pathname, timespec last_modified, bool *rewound);
257
258         // Returns nullptr if no frame was decoded (e.g. EOF).
259         AVFrameWithDeleter decode_frame(AVFormatContext *format_ctx, AVCodecContext *video_codec_ctx, AVCodecContext *audio_codec_ctx,
260                                         const std::string &pathname, int video_stream_index, int audio_stream_index, int subtitle_stream_index,
261                                         bmusb::FrameAllocator::Frame *audio_frame, bmusb::AudioFormat *audio_format, int64_t *audio_pts, bool *error);
262         void convert_audio(const AVFrame *audio_avframe, bmusb::FrameAllocator::Frame *audio_frame, bmusb::AudioFormat *audio_format);
263
264         bmusb::VideoFormat construct_video_format(const AVFrame *frame, AVRational video_timebase);
265         UniqueFrame make_video_frame(const AVFrame *frame, const std::string &pathname, bool *error);
266
267         static int interrupt_cb_thunk(void *opaque);
268         int interrupt_cb();
269
270 #ifdef HAVE_SRT
271         static int read_srt_thunk(void *opaque, uint8_t *buf, int buf_size);
272         int read_srt(uint8_t *buf, int buf_size);
273 #endif
274
275         inline unsigned frame_width(const AVFrame *frame) const;
276         inline unsigned frame_height(const AVFrame *frame) const;
277
278         mutable std::mutex filename_mu;
279         std::string description, filename;
280         int srt_sock = -1;
281         uint16_t timecode = 0;
282         unsigned width, height;  // 0 means keep input size.
283         bmusb::PixelFormat pixel_format = bmusb::PixelFormat_8BitBGRA;
284         movit::YCbCrFormat current_frame_ycbcr_format;
285         bool running = false;
286         int card_index = -1;
287         double rate = 1.0;
288         bool play_as_fast_as_possible = false;  // Activated iff rate >= 10.0.
289         std::atomic<bool> should_interrupt{false};
290         bool last_frame_was_connected = true;
291
292         // TODO: Replace with std::optional if we go C++17.
293         bool frame_timeout_valid = false;  // If true, will time out any reads after ten seconds.
294         std::chrono::steady_clock::time_point frame_timeout_started;  // Only relevant if frame_timeout_valid == true.
295
296         bool has_dequeue_callbacks = false;
297         std::function<void()> dequeue_init_callback = nullptr;
298         std::function<void()> dequeue_cleanup_callback = nullptr;
299
300         bmusb::card_disconnected_callback_t card_disconnected_callback = nullptr;
301         bool play_once = false;  // End thread after playing. Only for SRT, since the socket is ephemeral.
302         std::atomic<bool> disconnected{false};
303
304         bmusb::FrameAllocator *video_frame_allocator = nullptr;
305         bmusb::FrameAllocator *audio_frame_allocator = nullptr;
306         std::unique_ptr<bmusb::FrameAllocator> owned_video_frame_allocator;
307         std::unique_ptr<bmusb::FrameAllocator> owned_audio_frame_allocator;
308         frame_callback_t frame_callback = nullptr;
309         packet_callback_t video_callback = nullptr;
310         packet_callback_t audio_callback = nullptr;
311
312         SwsContextWithDeleter sws_ctx;
313         int sws_last_width = -1, sws_last_height = -1, sws_last_src_format = -1;
314         AVPixelFormat sws_dst_format = AVPixelFormat(-1);  // In practice, always initialized.
315         AVRational video_timebase, audio_timebase;
316         bool is_mjpeg = false;
317
318         QuittableSleeper producer_thread_should_quit;
319         std::thread producer_thread;
320
321         int64_t pts_origin, last_pts;
322         std::chrono::steady_clock::time_point start, next_frame_start, last_frame;
323
324         std::mutex queue_mu;
325         struct QueuedCommand {
326                 enum Command { REWIND, CHANGE_RATE } command;
327                 double new_rate;  // For CHANGE_RATE.
328         };
329         std::vector<QueuedCommand> command_queue;  // Protected by <queue_mu>.
330
331         // Audio resampler.
332         SwrContext *resampler = nullptr;
333         AVSampleFormat last_src_format, last_dst_format;
334         AVChannelLayout last_channel_layout;
335         int last_sample_rate;
336
337         // Subtitles (no decoding done, really).
338         bool has_last_subtitle = false;
339         std::string last_subtitle;
340
341         movit::RGBTriplet last_neutral_color{1.0f, 1.0f, 1.0f};
342
343         // Used for suppressing repeated warnings. Reset when a video starts playing.
344         // -1 is strictly speaking outside the range of the enum, but hopefully, it will be alright.
345         AVColorSpace last_colorspace = static_cast<AVColorSpace>(-1);
346         AVChromaLocation last_chroma_location = static_cast<AVChromaLocation>(-1);
347 };
348
349 #endif  // !defined(_FFMPEG_CAPTURE_H)