1 #include "ffmpeg_capture.h"
13 #include <libavcodec/avcodec.h>
14 #include <libavformat/avformat.h>
15 #include <libavutil/avutil.h>
16 #include <libavutil/error.h>
17 #include <libavutil/frame.h>
18 #include <libavutil/imgutils.h>
19 #include <libavutil/mem.h>
20 #include <libavutil/pixfmt.h>
21 #include <libswscale/swscale.h>
29 #include "bmusb/bmusb.h"
30 #include "ffmpeg_raii.h"
31 #include "ffmpeg_util.h"
33 #include "image_input.h"
35 #define FRAME_SIZE (8 << 20) // 8 MB.
38 using namespace std::chrono;
39 using namespace bmusb;
43 steady_clock::time_point compute_frame_start(int64_t frame_pts, int64_t pts_origin, const AVRational &video_timebase, const steady_clock::time_point &origin, double rate)
45 const duration<double> pts((frame_pts - pts_origin) * double(video_timebase.num) / double(video_timebase.den));
46 return origin + duration_cast<steady_clock::duration>(pts / rate);
49 bool changed_since(const std::string &pathname, const timespec &ts)
55 if (stat(pathname.c_str(), &buf) != 0) {
56 fprintf(stderr, "%s: Couldn't check for new version, leaving the old in place.\n", pathname.c_str());
59 return (buf.st_mtim.tv_sec != ts.tv_sec || buf.st_mtim.tv_nsec != ts.tv_nsec);
64 FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned height)
65 : filename(filename), width(width), height(height)
67 // Not really used for anything.
68 description = "Video: " + filename;
70 avformat_network_init(); // In case someone wants this.
73 FFmpegCapture::~FFmpegCapture()
75 if (has_dequeue_callbacks) {
76 dequeue_cleanup_callback();
80 void FFmpegCapture::configure_card()
82 if (video_frame_allocator == nullptr) {
83 owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
84 set_video_frame_allocator(owned_video_frame_allocator.get());
86 if (audio_frame_allocator == nullptr) {
87 owned_audio_frame_allocator.reset(new MallocFrameAllocator(65536, NUM_QUEUED_AUDIO_FRAMES));
88 set_audio_frame_allocator(owned_audio_frame_allocator.get());
92 void FFmpegCapture::start_bm_capture()
98 producer_thread_should_quit.unquit();
99 producer_thread = thread(&FFmpegCapture::producer_thread_func, this);
102 void FFmpegCapture::stop_dequeue_thread()
108 producer_thread_should_quit.quit();
109 producer_thread.join();
112 std::map<uint32_t, VideoMode> FFmpegCapture::get_available_video_modes() const
114 // Note: This will never really be shown in the UI.
118 snprintf(buf, sizeof(buf), "%ux%u", width, height);
121 mode.autodetect = false;
123 mode.height = height;
124 mode.frame_rate_num = 60;
125 mode.frame_rate_den = 1;
126 mode.interlaced = false;
128 return {{ 0, mode }};
131 void FFmpegCapture::producer_thread_func()
133 char thread_name[16];
134 snprintf(thread_name, sizeof(thread_name), "FFmpeg_C_%d", card_index);
135 pthread_setname_np(pthread_self(), thread_name);
137 while (!producer_thread_should_quit.should_quit()) {
138 string pathname = search_for_file(filename);
139 if (filename.empty()) {
140 fprintf(stderr, "%s not found, sleeping one second and trying again...\n", filename.c_str());
141 send_disconnected_frame();
142 producer_thread_should_quit.sleep_for(seconds(1));
145 if (!play_video(pathname)) {
147 fprintf(stderr, "Error when playing %s, sleeping one second and trying again...\n", pathname.c_str());
148 send_disconnected_frame();
149 producer_thread_should_quit.sleep_for(seconds(1));
153 // Probably just EOF, will exit the loop above on next test.
156 if (has_dequeue_callbacks) {
157 dequeue_cleanup_callback();
158 has_dequeue_callbacks = false;
162 void FFmpegCapture::send_disconnected_frame()
164 // Send an empty frame to signal that we have no signal anymore.
165 FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
166 if (video_frame.data) {
167 VideoFormat video_format;
168 video_format.width = width;
169 video_format.height = height;
170 video_format.stride = width * 4;
171 video_format.frame_rate_nom = 60;
172 video_format.frame_rate_den = 1;
173 video_format.is_connected = false;
175 video_frame.len = width * height * 4;
176 memset(video_frame.data, 0, video_frame.len);
178 frame_callback(timecode++,
179 video_frame, /*video_offset=*/0, video_format,
180 FrameAllocator::Frame(), /*audio_offset=*/0, AudioFormat());
184 bool FFmpegCapture::play_video(const string &pathname)
186 // Note: Call before open, not after; otherwise, there's a race.
187 // (There is now, too, but it tips the correct way. We could use fstat()
188 // if we had the file descriptor.)
189 timespec last_modified;
191 if (stat(pathname.c_str(), &buf) != 0) {
192 // Probably some sort of protocol, so can't stat.
193 last_modified.tv_sec = -1;
195 last_modified = buf.st_mtim;
198 auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, nullptr);
199 if (format_ctx == nullptr) {
200 fprintf(stderr, "%s: Error opening file\n", pathname.c_str());
204 if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
205 fprintf(stderr, "%s: Error finding stream info\n", pathname.c_str());
209 int video_stream_index = find_stream_index(format_ctx.get(), AVMEDIA_TYPE_VIDEO);
210 if (video_stream_index == -1) {
211 fprintf(stderr, "%s: No video stream found\n", pathname.c_str());
215 const AVCodecParameters *codecpar = format_ctx->streams[video_stream_index]->codecpar;
216 AVRational video_timebase = format_ctx->streams[video_stream_index]->time_base;
217 AVCodecContextWithDeleter codec_ctx = avcodec_alloc_context3_unique(nullptr);
218 if (avcodec_parameters_to_context(codec_ctx.get(), codecpar) < 0) {
219 fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str());
222 AVCodec *codec = avcodec_find_decoder(codecpar->codec_id);
223 if (codec == nullptr) {
224 fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str());
227 if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) {
228 fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str());
231 unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
232 codec_ctx.get(), avcodec_close);
237 unique_ptr<SwsContext, decltype(sws_freeContext)*> sws_ctx(nullptr, sws_freeContext);
238 int sws_last_width = -1, sws_last_height = -1;
241 while (!producer_thread_should_quit.should_quit()) {
242 // Process any queued commands from other threads.
243 vector<QueuedCommand> commands;
245 lock_guard<mutex> lock(queue_mu);
246 swap(commands, command_queue);
248 for (const QueuedCommand &cmd : commands) {
249 switch (cmd.command) {
250 case QueuedCommand::REWIND:
251 if (av_seek_frame(format_ctx.get(), /*stream_index=*/-1, /*timestamp=*/0, /*flags=*/0) < 0) {
252 fprintf(stderr, "%s: Rewind failed, stopping play.\n", pathname.c_str());
254 // If the file has changed since last time, return to get it reloaded.
255 // Note that depending on how you move the file into place, you might
256 // end up corrupting the one you're already playing, so this path
257 // might not trigger.
258 if (changed_since(pathname, last_modified)) {
264 case QueuedCommand::CHANGE_RATE:
265 start = next_frame_start;
266 pts_origin = last_pts;
272 // Read packets until we have a frame or there are none left.
273 int frame_finished = 0;
274 AVFrameWithDeleter frame = av_frame_alloc_unique();
278 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
279 &pkt, av_packet_unref);
280 av_init_packet(&pkt);
283 if (av_read_frame(format_ctx.get(), &pkt) == 0) {
284 if (pkt.stream_index != video_stream_index) {
285 // Ignore audio for now.
288 if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) {
289 fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
293 eof = true; // Or error, but ignore that for the time being.
296 int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
298 frame_finished = true;
300 } else if (err != AVERROR(EAGAIN)) {
301 fprintf(stderr, "%s: Cannot receive frame from codec.\n", pathname.c_str());
306 if (!frame_finished) {
307 // EOF. Loop back to the start if we can.
308 if (av_seek_frame(format_ctx.get(), /*stream_index=*/-1, /*timestamp=*/0, /*flags=*/0) < 0) {
309 fprintf(stderr, "%s: Rewind failed, not looping.\n", pathname.c_str());
312 // If the file has changed since last time, return to get it reloaded.
313 // Note that depending on how you move the file into place, you might
314 // end up corrupting the one you're already playing, so this path
315 // might not trigger.
316 if (changed_since(pathname, last_modified)) {
323 if (sws_ctx == nullptr || sws_last_width != frame->width || sws_last_height != frame->height) {
325 sws_getContext(frame->width, frame->height, (AVPixelFormat)frame->format,
326 width, height, AV_PIX_FMT_BGRA,
327 SWS_BICUBIC, nullptr, nullptr, nullptr));
328 sws_last_width = frame->width;
329 sws_last_height = frame->height;
331 if (sws_ctx == nullptr) {
332 fprintf(stderr, "%s: Could not create scaler context\n", pathname.c_str());
336 VideoFormat video_format;
337 video_format.width = width;
338 video_format.height = height;
339 video_format.stride = width * 4;
340 video_format.frame_rate_nom = video_timebase.den;
341 video_format.frame_rate_den = av_frame_get_pkt_duration(frame.get()) * video_timebase.num;
342 if (video_format.frame_rate_nom == 0 || video_format.frame_rate_den == 0) {
343 // Invalid frame rate.
344 video_format.frame_rate_nom = 60;
345 video_format.frame_rate_den = 1;
347 video_format.has_signal = true;
348 video_format.is_connected = true;
350 next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate);
351 last_pts = frame->pts;
353 FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
354 if (video_frame.data != nullptr) {
355 uint8_t *pic_data[4] = { video_frame.data, nullptr, nullptr, nullptr };
356 int linesizes[4] = { int(video_format.stride), 0, 0, 0 };
357 sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
358 video_frame.len = video_format.stride * height;
359 video_frame.received_timestamp = next_frame_start;
362 FrameAllocator::Frame audio_frame;
363 AudioFormat audio_format;
364 audio_format.bits_per_sample = 32;
365 audio_format.num_channels = 8;
367 producer_thread_should_quit.sleep_until(next_frame_start);
368 frame_callback(timecode++,
369 video_frame, 0, video_format,
370 audio_frame, 0, audio_format);
375 void FFmpegCapture::internal_rewind()
377 pts_origin = last_pts = 0;
378 start = next_frame_start = steady_clock::now();