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"
32 #include "image_input.h"
34 #define FRAME_SIZE (8 << 20) // 8 MB.
37 using namespace std::chrono;
38 using namespace bmusb;
42 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)
44 const duration<double> pts((frame_pts - pts_origin) * double(video_timebase.num) / double(video_timebase.den));
45 return origin + duration_cast<steady_clock::duration>(pts / rate);
48 bool changed_since(const std::string &pathname, const timespec &ts)
54 if (stat(pathname.c_str(), &buf) != 0) {
55 fprintf(stderr, "%s: Couldn't check for new version, leaving the old in place.\n", pathname.c_str());
58 return (buf.st_mtim.tv_sec != ts.tv_sec || buf.st_mtim.tv_nsec != ts.tv_nsec);
63 FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned height)
64 : filename(filename), width(width), height(height)
66 // Not really used for anything.
67 description = "Video: " + filename;
70 FFmpegCapture::~FFmpegCapture()
72 if (has_dequeue_callbacks) {
73 dequeue_cleanup_callback();
77 void FFmpegCapture::configure_card()
79 if (video_frame_allocator == nullptr) {
80 owned_video_frame_allocator.reset(new MallocFrameAllocator(FRAME_SIZE, NUM_QUEUED_VIDEO_FRAMES));
81 set_video_frame_allocator(owned_video_frame_allocator.get());
83 if (audio_frame_allocator == nullptr) {
84 owned_audio_frame_allocator.reset(new MallocFrameAllocator(65536, NUM_QUEUED_AUDIO_FRAMES));
85 set_audio_frame_allocator(owned_audio_frame_allocator.get());
89 void FFmpegCapture::start_bm_capture()
95 producer_thread_should_quit.unquit();
96 producer_thread = thread(&FFmpegCapture::producer_thread_func, this);
99 void FFmpegCapture::stop_dequeue_thread()
105 producer_thread_should_quit.quit();
106 producer_thread.join();
109 std::map<uint32_t, VideoMode> FFmpegCapture::get_available_video_modes() const
111 // Note: This will never really be shown in the UI.
115 snprintf(buf, sizeof(buf), "%ux%u", width, height);
118 mode.autodetect = false;
120 mode.height = height;
121 mode.frame_rate_num = 60;
122 mode.frame_rate_den = 1;
123 mode.interlaced = false;
125 return {{ 0, mode }};
128 void FFmpegCapture::producer_thread_func()
130 char thread_name[16];
131 snprintf(thread_name, sizeof(thread_name), "FFmpeg_C_%d", card_index);
132 pthread_setname_np(pthread_self(), thread_name);
134 while (!producer_thread_should_quit.should_quit()) {
135 string pathname = search_for_file(filename);
136 if (filename.empty()) {
137 fprintf(stderr, "%s not found, sleeping one second and trying again...\n", filename.c_str());
138 producer_thread_should_quit.sleep_for(seconds(1));
141 if (!play_video(pathname)) {
143 fprintf(stderr, "Error when playing %s, sleeping one second and trying again...\n", pathname.c_str());
144 producer_thread_should_quit.sleep_for(seconds(1));
148 // Probably just EOF, will exit the loop above on next test.
151 if (has_dequeue_callbacks) {
152 dequeue_cleanup_callback();
153 has_dequeue_callbacks = false;
157 bool FFmpegCapture::play_video(const string &pathname)
159 // Note: Call before open, not after; otherwise, there's a race.
160 // (There is now, too, but it tips the correct way. We could use fstat()
161 // if we had the file descriptor.)
162 timespec last_modified;
164 if (stat(pathname.c_str(), &buf) != 0) {
165 // Probably some sort of protocol, so can't stat.
166 last_modified.tv_sec = -1;
168 last_modified = buf.st_mtim;
171 auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, nullptr);
172 if (format_ctx == nullptr) {
173 fprintf(stderr, "%s: Error opening file\n", pathname.c_str());
177 if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
178 fprintf(stderr, "%s: Error finding stream info\n", pathname.c_str());
182 int video_stream_index = -1, audio_stream_index = -1;
183 AVRational video_timebase{ 1, 1 };
184 for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
185 if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
186 video_stream_index == -1) {
187 video_stream_index = i;
188 video_timebase = format_ctx->streams[i]->time_base;
190 if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
191 audio_stream_index == -1) {
192 audio_stream_index = i;
195 if (video_stream_index == -1) {
196 fprintf(stderr, "%s: No video stream found\n", pathname.c_str());
200 const AVCodecParameters *codecpar = format_ctx->streams[video_stream_index]->codecpar;
201 AVCodecContextWithDeleter codec_ctx = avcodec_alloc_context3_unique(nullptr);
202 if (avcodec_parameters_to_context(codec_ctx.get(), codecpar) < 0) {
203 fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str());
206 AVCodec *codec = avcodec_find_decoder(codecpar->codec_id);
207 if (codec == nullptr) {
208 fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str());
211 if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) {
212 fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str());
215 unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
216 codec_ctx.get(), avcodec_close);
221 unique_ptr<SwsContext, decltype(sws_freeContext)*> sws_ctx(nullptr, sws_freeContext);
222 int sws_last_width = -1, sws_last_height = -1;
225 while (!producer_thread_should_quit.should_quit()) {
226 // Process any queued commands from other threads.
227 vector<QueuedCommand> commands;
229 lock_guard<mutex> lock(queue_mu);
230 swap(commands, command_queue);
232 for (const QueuedCommand &cmd : commands) {
233 switch (cmd.command) {
234 case QueuedCommand::REWIND:
235 if (av_seek_frame(format_ctx.get(), /*stream_index=*/-1, /*timestamp=*/0, /*flags=*/0) < 0) {
236 fprintf(stderr, "%s: Rewind failed, stopping play.\n", pathname.c_str());
238 // If the file has changed since last time, return to get it reloaded.
239 // Note that depending on how you move the file into place, you might
240 // end up corrupting the one you're already playing, so this path
241 // might not trigger.
242 if (changed_since(pathname, last_modified)) {
248 case QueuedCommand::CHANGE_RATE:
249 start = next_frame_start;
250 pts_origin = last_pts;
256 // Read packets until we have a frame or there are none left.
257 int frame_finished = 0;
258 AVFrameWithDeleter frame = av_frame_alloc_unique();
262 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
263 &pkt, av_packet_unref);
264 av_init_packet(&pkt);
267 if (av_read_frame(format_ctx.get(), &pkt) == 0) {
268 if (pkt.stream_index != video_stream_index) {
269 // Ignore audio for now.
272 if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) {
273 fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
277 eof = true; // Or error, but ignore that for the time being.
280 int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
282 frame_finished = true;
284 } else if (err != AVERROR(EAGAIN)) {
285 fprintf(stderr, "%s: Cannot receive frame from codec.\n", pathname.c_str());
290 if (!frame_finished) {
291 // EOF. Loop back to the start if we can.
292 if (av_seek_frame(format_ctx.get(), /*stream_index=*/-1, /*timestamp=*/0, /*flags=*/0) < 0) {
293 fprintf(stderr, "%s: Rewind failed, not looping.\n", pathname.c_str());
296 // If the file has changed since last time, return to get it reloaded.
297 // Note that depending on how you move the file into place, you might
298 // end up corrupting the one you're already playing, so this path
299 // might not trigger.
300 if (changed_since(pathname, last_modified)) {
307 if (sws_ctx == nullptr || sws_last_width != frame->width || sws_last_height != frame->height) {
309 sws_getContext(frame->width, frame->height, (AVPixelFormat)frame->format,
310 width, height, AV_PIX_FMT_RGBA,
311 SWS_BICUBIC, nullptr, nullptr, nullptr));
312 sws_last_width = frame->width;
313 sws_last_height = frame->height;
315 if (sws_ctx == nullptr) {
316 fprintf(stderr, "%s: Could not create scaler context\n", pathname.c_str());
320 VideoFormat video_format;
321 video_format.width = width;
322 video_format.height = height;
323 video_format.stride = width * 4;
324 video_format.frame_rate_nom = video_timebase.den;
325 video_format.frame_rate_den = av_frame_get_pkt_duration(frame.get()) * video_timebase.num;
326 video_format.has_signal = true;
327 video_format.is_connected = true;
329 next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate);
330 last_pts = frame->pts;
332 FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
333 if (video_frame.data != nullptr) {
334 uint8_t *pic_data[4] = { video_frame.data, nullptr, nullptr, nullptr };
335 int linesizes[4] = { int(video_format.stride), 0, 0, 0 };
336 sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
337 video_frame.len = video_format.stride * height;
338 video_frame.received_timestamp = next_frame_start;
341 FrameAllocator::Frame audio_frame;
342 AudioFormat audio_format;
343 audio_format.bits_per_sample = 32;
344 audio_format.num_channels = 8;
346 producer_thread_should_quit.sleep_until(next_frame_start);
347 frame_callback(timecode++,
348 video_frame, 0, video_format,
349 audio_frame, 0, audio_format);
354 void FFmpegCapture::internal_rewind()
356 pts_origin = last_pts = 0;
357 start = next_frame_start = steady_clock::now();