]> git.sesse.net Git - nageru/blob - ffmpeg_capture.cpp
Initialize FFmpeg networking correctly.
[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 "ffmpeg_util.h"
32 #include "flags.h"
33 #include "image_input.h"
34
35 #define FRAME_SIZE (8 << 20)  // 8 MB.
36
37 using namespace std;
38 using namespace std::chrono;
39 using namespace bmusb;
40
41 namespace {
42
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)
44 {
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);
47 }
48
49 bool changed_since(const std::string &pathname, const timespec &ts)
50 {
51         if (ts.tv_sec < 0) {
52                 return false;
53         }
54         struct stat buf;
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());
57                 return false;
58         }
59         return (buf.st_mtim.tv_sec != ts.tv_sec || buf.st_mtim.tv_nsec != ts.tv_nsec);
60 }
61
62 }  // namespace
63
64 FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned height)
65         : filename(filename), width(width), height(height)
66 {
67         // Not really used for anything.
68         description = "Video: " + filename;
69
70         avformat_network_init();  // In case someone wants this.
71 }
72
73 FFmpegCapture::~FFmpegCapture()
74 {
75         if (has_dequeue_callbacks) {
76                 dequeue_cleanup_callback();
77         }
78 }
79
80 void FFmpegCapture::configure_card()
81 {
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());
85         }
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());
89         }
90 }
91
92 void FFmpegCapture::start_bm_capture()
93 {
94         if (running) {
95                 return;
96         }
97         running = true;
98         producer_thread_should_quit.unquit();
99         producer_thread = thread(&FFmpegCapture::producer_thread_func, this);
100 }
101
102 void FFmpegCapture::stop_dequeue_thread()
103 {
104         if (!running) {
105                 return;
106         }
107         running = false;
108         producer_thread_should_quit.quit();
109         producer_thread.join();
110 }
111
112 std::map<uint32_t, VideoMode> FFmpegCapture::get_available_video_modes() const
113 {
114         // Note: This will never really be shown in the UI.
115         VideoMode mode;
116
117         char buf[256];
118         snprintf(buf, sizeof(buf), "%ux%u", width, height);
119         mode.name = buf;
120         
121         mode.autodetect = false;
122         mode.width = width;
123         mode.height = height;
124         mode.frame_rate_num = 60;
125         mode.frame_rate_den = 1;
126         mode.interlaced = false;
127
128         return {{ 0, mode }};
129 }
130
131 void FFmpegCapture::producer_thread_func()
132 {
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);
136
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                         producer_thread_should_quit.sleep_for(seconds(1));
142                         continue;
143                 }
144                 if (!play_video(pathname)) {
145                         // Error.
146                         fprintf(stderr, "Error when playing %s, sleeping one second and trying again...\n", pathname.c_str());
147                         producer_thread_should_quit.sleep_for(seconds(1));
148                         continue;
149                 }
150
151                 // Probably just EOF, will exit the loop above on next test.
152         }
153
154         if (has_dequeue_callbacks) {
155                 dequeue_cleanup_callback();
156                 has_dequeue_callbacks = false;
157         }
158 }
159
160 bool FFmpegCapture::play_video(const string &pathname)
161 {
162         // Note: Call before open, not after; otherwise, there's a race.
163         // (There is now, too, but it tips the correct way. We could use fstat()
164         // if we had the file descriptor.)
165         timespec last_modified;
166         struct stat buf;
167         if (stat(pathname.c_str(), &buf) != 0) {
168                 // Probably some sort of protocol, so can't stat.
169                 last_modified.tv_sec = -1;
170         } else {
171                 last_modified = buf.st_mtim;
172         }
173
174         auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, nullptr);
175         if (format_ctx == nullptr) {
176                 fprintf(stderr, "%s: Error opening file\n", pathname.c_str());
177                 return false;
178         }
179
180         if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
181                 fprintf(stderr, "%s: Error finding stream info\n", pathname.c_str());
182                 return false;
183         }
184
185         int video_stream_index = find_stream_index(format_ctx.get(), AVMEDIA_TYPE_VIDEO);
186         if (video_stream_index == -1) {
187                 fprintf(stderr, "%s: No video stream found\n", pathname.c_str());
188                 return false;
189         }
190
191         const AVCodecParameters *codecpar = format_ctx->streams[video_stream_index]->codecpar;
192         AVRational video_timebase = format_ctx->streams[video_stream_index]->time_base;
193         AVCodecContextWithDeleter codec_ctx = avcodec_alloc_context3_unique(nullptr);
194         if (avcodec_parameters_to_context(codec_ctx.get(), codecpar) < 0) {
195                 fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str());
196                 return false;
197         }
198         AVCodec *codec = avcodec_find_decoder(codecpar->codec_id);
199         if (codec == nullptr) {
200                 fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str());
201                 return false;
202         }
203         if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) {
204                 fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str());
205                 return false;
206         }
207         unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
208                 codec_ctx.get(), avcodec_close);
209
210         internal_rewind();
211         double rate = 1.0;
212
213         unique_ptr<SwsContext, decltype(sws_freeContext)*> sws_ctx(nullptr, sws_freeContext);
214         int sws_last_width = -1, sws_last_height = -1;
215
216         // Main loop.
217         while (!producer_thread_should_quit.should_quit()) {
218                 // Process any queued commands from other threads.
219                 vector<QueuedCommand> commands;
220                 {
221                         lock_guard<mutex> lock(queue_mu);
222                         swap(commands, command_queue);
223                 }
224                 for (const QueuedCommand &cmd : commands) {
225                         switch (cmd.command) {
226                         case QueuedCommand::REWIND:
227                                 if (av_seek_frame(format_ctx.get(), /*stream_index=*/-1, /*timestamp=*/0, /*flags=*/0) < 0) {
228                                         fprintf(stderr, "%s: Rewind failed, stopping play.\n", pathname.c_str());
229                                 }
230                                 // If the file has changed since last time, return to get it reloaded.
231                                 // Note that depending on how you move the file into place, you might
232                                 // end up corrupting the one you're already playing, so this path
233                                 // might not trigger.
234                                 if (changed_since(pathname, last_modified)) {
235                                         return true;
236                                 }
237                                 internal_rewind();
238                                 break;
239
240                         case QueuedCommand::CHANGE_RATE:
241                                 start = next_frame_start;
242                                 pts_origin = last_pts;
243                                 rate = cmd.new_rate;
244                                 break;
245                         }
246                 }
247
248                 // Read packets until we have a frame or there are none left.
249                 int frame_finished = 0;
250                 AVFrameWithDeleter frame = av_frame_alloc_unique();
251                 bool eof = false;
252                 do {
253                         AVPacket pkt;
254                         unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
255                                 &pkt, av_packet_unref);
256                         av_init_packet(&pkt);
257                         pkt.data = nullptr;
258                         pkt.size = 0;
259                         if (av_read_frame(format_ctx.get(), &pkt) == 0) {
260                                 if (pkt.stream_index != video_stream_index) {
261                                         // Ignore audio for now.
262                                         continue;
263                                 }
264                                 if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) {
265                                         fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
266                                         return false;
267                                 }
268                         } else {
269                                 eof = true;  // Or error, but ignore that for the time being.
270                         }
271
272                         int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
273                         if (err == 0) {
274                                 frame_finished = true;
275                                 break;
276                         } else if (err != AVERROR(EAGAIN)) {
277                                 fprintf(stderr, "%s: Cannot receive frame from codec.\n", pathname.c_str());
278                                 return false;
279                         }
280                 } while (!eof);
281
282                 if (!frame_finished) {
283                         // EOF. Loop back to the start if we can.
284                         if (av_seek_frame(format_ctx.get(), /*stream_index=*/-1, /*timestamp=*/0, /*flags=*/0) < 0) {
285                                 fprintf(stderr, "%s: Rewind failed, not looping.\n", pathname.c_str());
286                                 return true;
287                         }
288                         // If the file has changed since last time, return to get it reloaded.
289                         // Note that depending on how you move the file into place, you might
290                         // end up corrupting the one you're already playing, so this path
291                         // might not trigger.
292                         if (changed_since(pathname, last_modified)) {
293                                 return true;
294                         }
295                         internal_rewind();
296                         continue;
297                 }
298
299                 if (sws_ctx == nullptr || sws_last_width != frame->width || sws_last_height != frame->height) {
300                         sws_ctx.reset(
301                                 sws_getContext(frame->width, frame->height, (AVPixelFormat)frame->format,
302                                         width, height, AV_PIX_FMT_BGRA,
303                                         SWS_BICUBIC, nullptr, nullptr, nullptr));
304                         sws_last_width = frame->width;
305                         sws_last_height = frame->height;
306                 }
307                 if (sws_ctx == nullptr) {
308                         fprintf(stderr, "%s: Could not create scaler context\n", pathname.c_str());
309                         return false;
310                 }
311
312                 VideoFormat video_format;
313                 video_format.width = width;
314                 video_format.height = height;
315                 video_format.stride = width * 4;
316                 video_format.frame_rate_nom = video_timebase.den;
317                 video_format.frame_rate_den = av_frame_get_pkt_duration(frame.get()) * video_timebase.num;
318                 if (video_format.frame_rate_nom == 0 || video_format.frame_rate_den == 0) {
319                         // Invalid frame rate.
320                         video_format.frame_rate_nom = 60;
321                         video_format.frame_rate_den = 1;
322                 }
323                 video_format.has_signal = true;
324                 video_format.is_connected = true;
325
326                 next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate);
327                 last_pts = frame->pts;
328
329                 FrameAllocator::Frame video_frame = video_frame_allocator->alloc_frame();
330                 if (video_frame.data != nullptr) {
331                         uint8_t *pic_data[4] = { video_frame.data, nullptr, nullptr, nullptr };
332                         int linesizes[4] = { int(video_format.stride), 0, 0, 0 };
333                         sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
334                         video_frame.len = video_format.stride * height;
335                         video_frame.received_timestamp = next_frame_start;
336                 }
337
338                 FrameAllocator::Frame audio_frame;
339                 AudioFormat audio_format;
340                 audio_format.bits_per_sample = 32;
341                 audio_format.num_channels = 8;
342
343                 producer_thread_should_quit.sleep_until(next_frame_start);
344                 frame_callback(timecode++,
345                         video_frame, 0, video_format,
346                         audio_frame, 0, audio_format);
347         }
348         return true;
349 }
350
351 void FFmpegCapture::internal_rewind()
352 {                               
353         pts_origin = last_pts = 0;
354         start = next_frame_start = steady_clock::now();
355 }