]> git.sesse.net Git - nageru/blob - ffmpeg_capture.cpp
Support changing video files underway, just like images.
[nageru] / ffmpeg_capture.cpp
1 #include "ffmpeg_capture.h"
2
3 #include <assert.h>
4 #include <pthread.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11
12 extern "C" {
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>
22 }
23
24 #include <chrono>
25 #include <cstdint>
26 #include <utility>
27 #include <vector>
28
29 #include "bmusb/bmusb.h"
30 #include "ffmpeg_raii.h"
31 #include "flags.h"
32 #include "image_input.h"
33
34 #define FRAME_SIZE (8 << 20)  // 8 MB.
35
36 using namespace std;
37 using namespace std::chrono;
38 using namespace bmusb;
39
40 namespace {
41
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)
43 {
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);
46 }
47
48 bool changed_since(const std::string &pathname, const timespec &ts)
49 {
50         if (ts.tv_sec < 0) {
51                 return false;
52         }
53         struct stat buf;
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());
56                 return false;
57         }
58         return (buf.st_mtim.tv_sec != ts.tv_sec || buf.st_mtim.tv_nsec != ts.tv_nsec);
59 }
60
61 }  // namespace
62
63 FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned height)
64         : filename(filename), width(width), height(height)
65 {
66         // Not really used for anything.
67         description = "Video: " + filename;
68 }
69
70 FFmpegCapture::~FFmpegCapture()
71 {
72         if (has_dequeue_callbacks) {
73                 dequeue_cleanup_callback();
74         }
75 }
76
77 void FFmpegCapture::configure_card()
78 {
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());
82         }
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());
86         }
87 }
88
89 void FFmpegCapture::start_bm_capture()
90 {
91         if (running) {
92                 return;
93         }
94         running = true;
95         producer_thread_should_quit.unquit();
96         producer_thread = thread(&FFmpegCapture::producer_thread_func, this);
97 }
98
99 void FFmpegCapture::stop_dequeue_thread()
100 {
101         if (!running) {
102                 return;
103         }
104         running = false;
105         producer_thread_should_quit.quit();
106         producer_thread.join();
107 }
108
109 std::map<uint32_t, VideoMode> FFmpegCapture::get_available_video_modes() const
110 {
111         // Note: This will never really be shown in the UI.
112         VideoMode mode;
113
114         char buf[256];
115         snprintf(buf, sizeof(buf), "%ux%u", width, height);
116         mode.name = buf;
117         
118         mode.autodetect = false;
119         mode.width = width;
120         mode.height = height;
121         mode.frame_rate_num = 60;
122         mode.frame_rate_den = 1;
123         mode.interlaced = false;
124
125         return {{ 0, mode }};
126 }
127
128 void FFmpegCapture::producer_thread_func()
129 {
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);
133
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));
139                         continue;
140                 }
141                 if (!play_video(pathname)) {
142                         // Error.
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));
145                         continue;
146                 }
147
148                 // Probably just EOF, will exit the loop above on next test.
149         }
150
151         if (has_dequeue_callbacks) {
152                 dequeue_cleanup_callback();
153                 has_dequeue_callbacks = false;
154         }
155 }
156
157 bool FFmpegCapture::play_video(const string &pathname)
158 {
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;
163         struct stat buf;
164         if (stat(pathname.c_str(), &buf) != 0) {
165                 // Probably some sort of protocol, so can't stat.
166                 last_modified.tv_sec = -1;
167         } else {
168                 last_modified = buf.st_mtim;
169         }
170
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());
174                 return false;
175         }
176
177         if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
178                 fprintf(stderr, "%s: Error finding stream info\n", pathname.c_str());
179                 return false;
180         }
181
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;
189                 }
190                 if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
191                     audio_stream_index == -1) {
192                         audio_stream_index = i;
193                 }
194         }
195         if (video_stream_index == -1) {
196                 fprintf(stderr, "%s: No video stream found\n", pathname.c_str());
197                 return false;
198         }
199
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());
204                 return false;
205         }
206         AVCodec *codec = avcodec_find_decoder(codecpar->codec_id);
207         if (codec == nullptr) {
208                 fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str());
209                 return false;
210         }
211         if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) {
212                 fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str());
213                 return false;
214         }
215         unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
216                 codec_ctx.get(), avcodec_close);
217
218         internal_rewind();
219         double rate = 1.0;
220
221         unique_ptr<SwsContext, decltype(sws_freeContext)*> sws_ctx(nullptr, sws_freeContext);
222         int sws_last_width = -1, sws_last_height = -1;
223
224         // Main loop.
225         while (!producer_thread_should_quit.should_quit()) {
226                 // Process any queued commands from other threads.
227                 vector<QueuedCommand> commands;
228                 {
229                         lock_guard<mutex> lock(queue_mu);
230                         swap(commands, command_queue);
231                 }
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());
237                                 }
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)) {
243                                         return true;
244                                 }
245                                 internal_rewind();
246                                 break;
247
248                         case QueuedCommand::CHANGE_RATE:
249                                 start = next_frame_start;
250                                 pts_origin = last_pts;
251                                 rate = cmd.new_rate;
252                                 break;
253                         }
254                 }
255
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();
259                 bool eof = false;
260                 do {
261                         AVPacket pkt;
262                         unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
263                                 &pkt, av_packet_unref);
264                         av_init_packet(&pkt);
265                         pkt.data = nullptr;
266                         pkt.size = 0;
267                         if (av_read_frame(format_ctx.get(), &pkt) == 0) {
268                                 if (pkt.stream_index != video_stream_index) {
269                                         // Ignore audio for now.
270                                         continue;
271                                 }
272                                 if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) {
273                                         fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
274                                         return false;
275                                 }
276                         } else {
277                                 eof = true;  // Or error, but ignore that for the time being.
278                         }
279
280                         int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
281                         if (err == 0) {
282                                 frame_finished = true;
283                                 break;
284                         } else if (err != AVERROR(EAGAIN)) {
285                                 fprintf(stderr, "%s: Cannot receive frame from codec.\n", pathname.c_str());
286                                 return false;
287                         }
288                 } while (!eof);
289
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());
294                                 return true;
295                         }
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)) {
301                                 return true;
302                         }
303                         internal_rewind();
304                         continue;
305                 }
306
307                 if (sws_ctx == nullptr || sws_last_width != frame->width || sws_last_height != frame->height) {
308                         sws_ctx.reset(
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;
314                 }
315                 if (sws_ctx == nullptr) {
316                         fprintf(stderr, "%s: Could not create scaler context\n", pathname.c_str());
317                         return false;
318                 }
319
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;
328
329                 next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate);
330                 last_pts = frame->pts;
331
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;
339                 }
340
341                 FrameAllocator::Frame audio_frame;
342                 AudioFormat audio_format;
343                 audio_format.bits_per_sample = 32;
344                 audio_format.num_channels = 8;
345
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);
350         }
351         return true;
352 }
353
354 void FFmpegCapture::internal_rewind()
355 {                               
356         pts_origin = last_pts = 0;
357         start = next_frame_start = steady_clock::now();
358 }