]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/consumer/ffmpeg_consumer.cpp
b0c3460e659cba4a3c0f66630810e6e6a4ca1a35
[casparcg] / modules / ffmpeg / consumer / ffmpeg_consumer.cpp
1 #include "../StdAfx.h"
2
3 #include "ffmpeg_consumer.h"
4
5 #include "../ffmpeg_error.h"
6 #include "../producer/util/util.h"
7 #include "../producer/filter/filter.h"
8 #include "../producer/filter/audio_filter.h"
9
10 #include <common/except.h>
11 #include <common/executor.h>
12 #include <common/assert.h>
13 #include <common/utf.h>
14 #include <common/future.h>
15 #include <common/diagnostics/graph.h>
16 #include <common/env.h>
17 #include <common/scope_exit.h>
18 #include <common/ptree.h>
19 #include <common/param.h>
20 #include <common/semaphore.h>
21
22 #include <core/consumer/frame_consumer.h>
23 #include <core/frame/frame.h>
24 #include <core/frame/audio_channel_layout.h>
25 #include <core/video_format.h>
26 #include <core/monitor/monitor.h>
27 #include <core/help/help_repository.h>
28 #include <core/help/help_sink.h>
29
30 #include <boost/noncopyable.hpp>
31 #include <boost/rational.hpp>
32 #include <boost/format.hpp>
33 #include <boost/algorithm/string/predicate.hpp>
34 #include <boost/property_tree/ptree.hpp>
35
36 #pragma warning(push)
37 #pragma warning(disable: 4244)
38 #pragma warning(disable: 4245)
39 #include <boost/crc.hpp>
40 #pragma warning(pop)
41
42 #include <tbb/atomic.h>
43 #include <tbb/concurrent_queue.h>
44 #include <tbb/parallel_invoke.h>
45 #include <tbb/parallel_for.h>
46
47 #include <numeric>
48
49 #pragma warning(push)
50 #pragma warning(disable: 4244)
51
52 extern "C"
53 {
54         #define __STDC_CONSTANT_MACROS
55         #define __STDC_LIMIT_MACROS
56         #include <libavformat/avformat.h>
57         #include <libavcodec/avcodec.h>
58         #include <libavutil/avutil.h>
59         #include <libavutil/frame.h>
60         #include <libavutil/opt.h>
61         #include <libavutil/imgutils.h>
62         #include <libavutil/parseutils.h>
63         #include <libavfilter/avfilter.h>
64         #include <libavfilter/buffersink.h>
65         #include <libavfilter/buffersrc.h>
66 }
67
68 #pragma warning(pop)
69
70 namespace caspar { namespace ffmpeg {
71
72 void set_pixel_format(AVFilterContext* sink, AVPixelFormat pix_fmt)
73 {
74 #pragma warning (push)
75 #pragma warning (disable : 4245)
76
77         FF(av_opt_set_int_list(
78                 sink,
79                 "pix_fmts",
80                 std::vector<AVPixelFormat>({ pix_fmt, AVPixelFormat::AV_PIX_FMT_NONE }).data(),
81                 -1,
82                 AV_OPT_SEARCH_CHILDREN));
83
84 #pragma warning (pop)
85 }
86
87 void adjust_video_filter(const AVCodec& codec, const core::video_format_desc& in_format, AVFilterContext* sink, std::string& filter)
88 {
89         switch (codec.id)
90         {
91         case AV_CODEC_ID_DVVIDEO:
92                 // Crop
93                 if (in_format.format == core::video_format::ntsc)
94                         filter = u8(append_filter(u16(filter), L"crop=720:480:0:2"));
95
96                 // Pixel format selection
97                 if (in_format.format == core::video_format::ntsc)
98                         set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV411P);
99                 else if (in_format.format == core::video_format::pal)
100                         set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV420P);
101                 else
102                         set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV422P);
103
104                 // Scale
105                 if (in_format.height == 1080)
106                         filter = u8(append_filter(u16(filter), in_format.duration == 1001
107                                 ? L"scale=1280:1080"
108                                 : L"scale=1440:1080"));
109                 else if (in_format.height == 720)
110                         filter = u8(append_filter(u16(filter), L"scale=960:720"));
111
112                 break;
113         }
114 }
115
116 void setup_codec_defaults(AVCodecContext& encoder)
117 {
118         static const int MEGABIT = 1000000;
119
120         switch (encoder.codec_id)
121         {
122         case AV_CODEC_ID_DNXHD:
123                 encoder.bit_rate = 220 * MEGABIT;
124
125                 break;
126         case AV_CODEC_ID_PRORES:
127                 encoder.bit_rate = encoder.width < 1280
128                                 ?  63 * MEGABIT
129                                 : 220 * MEGABIT;
130
131                 break;
132         case AV_CODEC_ID_H264:
133                 av_opt_set(encoder.priv_data,   "preset",       "ultrafast",    0);
134                 av_opt_set(encoder.priv_data,   "tune",         "fastdecode",   0);
135                 av_opt_set(encoder.priv_data,   "crf",          "5",                    0);
136
137                 break;
138         }
139 }
140
141 bool is_pcm_s24le_not_supported(const AVFormatContext& container)
142 {
143         auto name = std::string(container.oformat->name);
144
145         if (name == "mp4" || name == "dv")
146                 return true;
147
148         return false;
149 }
150
151 template<typename Out, typename In>
152 std::vector<Out> from_terminated_array(const In* array, In terminator)
153 {
154         std::vector<Out> result;
155
156         while (array != nullptr && *array != terminator)
157         {
158                 In val          = *array;
159                 Out casted      = static_cast<Out>(val);
160
161                 result.push_back(casted);
162
163                 ++array;
164         }
165
166         return result;
167 }
168
169 class ffmpeg_consumer
170 {
171 private:
172         const spl::shared_ptr<diagnostics::graph>       graph_;
173         core::monitor::subject                                          subject_;
174         std::string                                                                     path_;
175         boost::filesystem::path                                         full_path_;
176
177         std::map<std::string, std::string>                      options_;
178         bool                                                                            mono_streams_;
179
180         core::video_format_desc                                         in_video_format_;
181         core::audio_channel_layout                                      in_channel_layout_                      = core::audio_channel_layout::invalid();
182
183         std::shared_ptr<AVFormatContext>                        oc_;
184         tbb::atomic<bool>                                                       abort_request_;
185
186         std::shared_ptr<AVStream>                                       video_st_;
187         std::vector<std::shared_ptr<AVStream>>          audio_sts_;
188
189         std::int64_t                                                            video_pts_                                      = 0;
190         std::int64_t                                                            audio_pts_                                      = 0;
191
192         std::unique_ptr<audio_filter>                           audio_filter_;
193
194         // TODO: make use of already existent avfilter abstraction for video also
195     AVFilterContext*                                                    video_graph_in_;
196     AVFilterContext*                                                    video_graph_out_;
197     std::shared_ptr<AVFilterGraph>                              video_graph_;
198
199         executor                                                                        video_encoder_executor_;
200         executor                                                                        audio_encoder_executor_;
201
202         semaphore                                                                       tokens_                                         { 0 };
203
204         tbb::atomic<int64_t>                                            current_encoding_delay_;
205
206         executor                                                                        write_executor_;
207
208 public:
209
210         ffmpeg_consumer(
211                         std::string path,
212                         std::string options,
213                         bool mono_streams)
214                 : path_(path)
215                 , full_path_(path)
216                 , mono_streams_(mono_streams)
217                 , audio_encoder_executor_(print() + L" audio_encoder")
218                 , video_encoder_executor_(print() + L" video_encoder")
219                 , write_executor_(print() + L" io")
220         {
221                 abort_request_ = false;
222                 current_encoding_delay_ = 0;
223
224                 for(auto it =
225                                 boost::sregex_iterator(
226                                         options.begin(),
227                                         options.end(),
228                                         boost::regex("-(?<NAME>[^-\\s]+)(\\s+(?<VALUE>[^\\s]+))?"));
229                         it != boost::sregex_iterator();
230                         ++it)
231                 {
232                         options_[(*it)["NAME"].str()] = (*it)["VALUE"].matched ? (*it)["VALUE"].str() : "";
233                 }
234
235         if (options_.find("threads") == options_.end())
236             options_["threads"] = "auto";
237
238                 tokens_.release(
239                         std::max(
240                                 1,
241                                 try_remove_arg<int>(
242                                         options_,
243                                         boost::regex("tokens")).get_value_or(2)));
244         }
245
246         ~ffmpeg_consumer()
247         {
248                 if(oc_)
249                 {
250                         try
251                         {
252                                 video_encoder_executor_.begin_invoke([&] { encode_video(core::const_frame::empty(), nullptr); });
253                                 audio_encoder_executor_.begin_invoke([&] { encode_audio(core::const_frame::empty(), nullptr); });
254
255                                 video_encoder_executor_.stop();
256                                 audio_encoder_executor_.stop();
257                                 video_encoder_executor_.join();
258                                 audio_encoder_executor_.join();
259
260                                 video_graph_.reset();
261                                 audio_filter_.reset();
262                                 video_st_.reset();
263                                 audio_sts_.clear();
264
265                                 write_packet(nullptr, nullptr);
266
267                                 write_executor_.stop();
268                                 write_executor_.join();
269
270                                 FF(av_write_trailer(oc_.get()));
271
272                                 if (!(oc_->oformat->flags & AVFMT_NOFILE) && oc_->pb)
273                                         avio_close(oc_->pb);
274
275                                 oc_.reset();
276                         }
277                         catch (...)
278                         {
279                                 CASPAR_LOG_CURRENT_EXCEPTION();
280                         }
281                 }
282         }
283
284         void initialize(
285                         const core::video_format_desc& format_desc,
286                         const core::audio_channel_layout& channel_layout)
287         {
288                 try
289                 {
290                         static boost::regex prot_exp("^.+:.*" );
291
292                         if(!boost::regex_match(
293                                         path_,
294                                         prot_exp))
295                         {
296                                 if(!full_path_.is_complete())
297                                 {
298                                         full_path_ =
299                                                 u8(
300                                                         env::media_folder()) +
301                                                         path_;
302                                 }
303
304                                 if(boost::filesystem::exists(full_path_))
305                                         boost::filesystem::remove(full_path_);
306
307                                 boost::filesystem::create_directories(full_path_.parent_path());
308                         }
309
310                         graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
311                         graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
312                         graph_->set_text(print());
313                         diagnostics::register_graph(graph_);
314
315                         const auto oformat_name =
316                                 try_remove_arg<std::string>(
317                                         options_,
318                                         boost::regex("^f|format$"));
319
320                         AVFormatContext* oc;
321
322                         FF(avformat_alloc_output_context2(
323                                 &oc,
324                                 nullptr,
325                                 oformat_name && !oformat_name->empty() ? oformat_name->c_str() : nullptr,
326                                 full_path_.string().c_str()));
327
328                         oc_.reset(
329                                 oc,
330                                 avformat_free_context);
331
332                         CASPAR_VERIFY(oc_->oformat);
333
334                         oc_->interrupt_callback.callback = ffmpeg_consumer::interrupt_cb;
335                         oc_->interrupt_callback.opaque   = this;
336
337                         CASPAR_VERIFY(format_desc.format != core::video_format::invalid);
338
339                         in_video_format_ = format_desc;
340                         in_channel_layout_ = channel_layout;
341
342                         CASPAR_VERIFY(oc_->oformat);
343
344                         const auto video_codec_name =
345                                 try_remove_arg<std::string>(
346                                         options_,
347                                         boost::regex("^c:v|codec:v|vcodec$"));
348
349                         const auto video_codec =
350                                 video_codec_name
351                                         ? avcodec_find_encoder_by_name(video_codec_name->c_str())
352                                         : avcodec_find_encoder(oc_->oformat->video_codec);
353
354                         const auto audio_codec_name =
355                                 try_remove_arg<std::string>(
356                                         options_,
357                                          boost::regex("^c:a|codec:a|acodec$"));
358
359                         const auto audio_codec =
360                                 audio_codec_name
361                                         ? avcodec_find_encoder_by_name(audio_codec_name->c_str())
362                                         : (is_pcm_s24le_not_supported(*oc_)
363                                                 ? avcodec_find_encoder(oc_->oformat->audio_codec)
364                                                 : avcodec_find_encoder_by_name("pcm_s24le"));
365
366                         if (!video_codec)
367                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
368                                                 "Failed to find video codec " + (video_codec_name
369                                                                 ? *video_codec_name
370                                                                 : "with id " + boost::lexical_cast<std::string>(
371                                                                                 oc_->oformat->video_codec))));
372                         if (!audio_codec)
373                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
374                                                 "Failed to find audio codec " + (audio_codec_name
375                                                                 ? *audio_codec_name
376                                                                 : "with id " + boost::lexical_cast<std::string>(
377                                                                                 oc_->oformat->audio_codec))));
378
379                         // Filters
380
381                         {
382                                 configure_video_filters(
383                                         *video_codec,
384                                         try_remove_arg<std::string>(options_, boost::regex("vf|f:v|filter:v"))
385                                                         .get_value_or(""),
386                                         try_remove_arg<std::string>(options_, boost::regex("pix_fmt")));
387
388                                 configure_audio_filters(
389                                         *audio_codec,
390                                         try_remove_arg<std::string>(options_,
391                                         boost::regex("af|f:a|filter:a")).get_value_or(""));
392                         }
393
394                         // Encoders
395
396                         {
397                                 auto video_options = options_;
398                                 auto audio_options = options_;
399
400                                 video_st_ = open_encoder(
401                                         *video_codec,
402                                         video_options,
403                                         0);
404
405                                 for (int i = 0; i < audio_filter_->get_num_output_pads(); ++i)
406                                         audio_sts_.push_back(open_encoder(
407                                                         *audio_codec,
408                                                         audio_options,
409                                                         i));
410
411                                 auto it = options_.begin();
412                                 while(it != options_.end())
413                                 {
414                                         if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
415                                                 it = options_.erase(it);
416                                         else
417                                                 ++it;
418                                 }
419                         }
420
421                         // Output
422                         {
423                                 AVDictionary* av_opts = nullptr;
424
425                                 to_dict(
426                                         &av_opts,
427                                         std::move(options_));
428
429                                 CASPAR_SCOPE_EXIT
430                                 {
431                                         av_dict_free(&av_opts);
432                                 };
433
434                                 if (!(oc_->oformat->flags & AVFMT_NOFILE))
435                                 {
436                                         FF(avio_open2(
437                                                 &oc_->pb,
438                                                 full_path_.string().c_str(),
439                                                 AVIO_FLAG_WRITE,
440                                                 &oc_->interrupt_callback,
441                                                 &av_opts));
442                                 }
443
444                                 FF(avformat_write_header(
445                                         oc_.get(),
446                                         &av_opts));
447
448                                 options_ = to_map(av_opts);
449                         }
450
451                         // Dump Info
452
453                         av_dump_format(
454                                 oc_.get(),
455                                 0,
456                                 oc_->filename,
457                                 1);
458
459                         for (const auto& option : options_)
460                         {
461                                 CASPAR_LOG(warning)
462                                         << L"Invalid option: -"
463                                         << u16(option.first)
464                                         << L" "
465                                         << u16(option.second);
466                         }
467                 }
468                 catch(...)
469                 {
470                         video_st_.reset();
471                         audio_sts_.clear();
472                         oc_.reset();
473                         throw;
474                 }
475         }
476
477         core::monitor::subject& monitor_output()
478         {
479                 return subject_;
480         }
481
482         void send(core::const_frame frame)
483         {
484                 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
485
486                 auto frame_timer = spl::make_shared<caspar::timer>();
487
488                 std::shared_ptr<void> token(
489                         nullptr,
490                         [this, frame, frame_timer](void*)
491                         {
492                                 tokens_.release();
493                                 current_encoding_delay_ = frame.get_age_millis();
494                                 graph_->set_value("frame-time", frame_timer->elapsed() * in_video_format_.fps * 0.5);
495                         });
496                 tokens_.acquire();
497
498                 video_encoder_executor_.begin_invoke([=]() mutable
499                 {
500                         encode_video(
501                                 frame,
502                                 token);
503                 });
504
505                 audio_encoder_executor_.begin_invoke([=]() mutable
506                 {
507                         encode_audio(
508                                 frame,
509                                 token);
510                 });
511         }
512
513         bool ready_for_frame() const
514         {
515                 return tokens_.permits() > 0;
516         }
517
518         void mark_dropped()
519         {
520                 graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
521         }
522
523         std::wstring print() const
524         {
525                 return L"ffmpeg_consumer[" + u16(path_) + L"]";
526         }
527
528         int64_t presentation_frame_age_millis() const
529         {
530                 return current_encoding_delay_;
531         }
532
533 private:
534
535         static int interrupt_cb(void* ctx)
536         {
537                 CASPAR_ASSERT(ctx);
538                 return reinterpret_cast<ffmpeg_consumer*>(ctx)->abort_request_;
539         }
540
541         std::shared_ptr<AVStream> open_encoder(
542                         const AVCodec& codec,
543                         std::map<std::string,
544                         std::string>& options,
545                         int stream_number_for_media_type)
546         {
547                 auto st =
548                         avformat_new_stream(
549                                 oc_.get(),
550                                 &codec);
551
552                 if (!st)
553                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("avformat_new_stream"));
554
555                 auto enc = st->codec;
556
557                 CASPAR_VERIFY(enc);
558
559                 switch(enc->codec_type)
560                 {
561                         case AVMEDIA_TYPE_VIDEO:
562                         {
563                                 enc->time_base                          = video_graph_out_->inputs[0]->time_base;
564                                 enc->pix_fmt                                    = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
565                                 enc->sample_aspect_ratio                = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
566                                 enc->width                                      = video_graph_out_->inputs[0]->w;
567                                 enc->height                                     = video_graph_out_->inputs[0]->h;
568                                 enc->bit_rate_tolerance         = 400 * 1000000;
569
570                                 break;
571                         }
572                         case AVMEDIA_TYPE_AUDIO:
573                         {
574                                 enc->time_base                          = audio_filter_->get_output_pad_info(stream_number_for_media_type).time_base;
575                                 enc->sample_fmt                         = static_cast<AVSampleFormat>(audio_filter_->get_output_pad_info(stream_number_for_media_type).format);
576                                 enc->sample_rate                                = audio_filter_->get_output_pad_info(stream_number_for_media_type).sample_rate;
577                                 enc->channel_layout                     = audio_filter_->get_output_pad_info(stream_number_for_media_type).channel_layout;
578                                 enc->channels                           = audio_filter_->get_output_pad_info(stream_number_for_media_type).channels;
579
580                                 break;
581                         }
582                 }
583
584                 setup_codec_defaults(*enc);
585
586                 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
587                         enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
588
589                 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
590
591                 const auto char_id = char_id_map.at(enc->codec_type);
592
593                 const auto codec_opts =
594                         remove_options(
595                                 options,
596                                 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
597
598                 AVDictionary* av_codec_opts = nullptr;
599
600                 to_dict(
601                         &av_codec_opts,
602                         options);
603
604                 to_dict(
605                         &av_codec_opts,
606                         codec_opts);
607
608                 options.clear();
609
610                 FF(avcodec_open2(
611                         enc,
612                         &codec,
613                         av_codec_opts ? &av_codec_opts : nullptr));
614
615                 if(av_codec_opts)
616                 {
617                         auto t =
618                                 av_dict_get(
619                                         av_codec_opts,
620                                         "",
621                                          nullptr,
622                                         AV_DICT_IGNORE_SUFFIX);
623
624                         while(t)
625                         {
626                                 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
627
628                                 t = av_dict_get(
629                                                 av_codec_opts,
630                                                 "",
631                                                 t,
632                                                 AV_DICT_IGNORE_SUFFIX);
633                         }
634
635                         av_dict_free(&av_codec_opts);
636                 }
637
638                 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
639                 {
640                         CASPAR_ASSERT(enc->frame_size > 0);
641                         audio_filter_->set_guaranteed_output_num_samples_per_frame(
642                                         stream_number_for_media_type,
643                                         enc->frame_size);
644                 }
645
646                 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
647                 {
648                         avcodec_close(st->codec);
649                 });
650         }
651
652         void configure_video_filters(
653                         const AVCodec& codec,
654                         std::string filtergraph,
655                         const boost::optional<std::string>& preferred_pix_fmt)
656         {
657                 video_graph_.reset(
658                                 avfilter_graph_alloc(),
659                                 [](AVFilterGraph* p)
660                                 {
661                                         avfilter_graph_free(&p);
662                                 });
663
664                 video_graph_->nb_threads  = boost::thread::hardware_concurrency()/2;
665                 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
666
667                 const auto sample_aspect_ratio =
668                         boost::rational<int>(
669                                         in_video_format_.square_width,
670                                         in_video_format_.square_height) /
671                         boost::rational<int>(
672                                         in_video_format_.width,
673                                         in_video_format_.height);
674
675                 const auto vsrc_options = (boost::format("video_size=%1%x%2%:pix_fmt=%3%:time_base=%4%/%5%:pixel_aspect=%6%/%7%:frame_rate=%8%/%9%")
676                         % in_video_format_.width % in_video_format_.height
677                         % AVPixelFormat::AV_PIX_FMT_BGRA
678                         % in_video_format_.duration     % in_video_format_.time_scale
679                         % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
680                         % in_video_format_.time_scale % in_video_format_.duration).str();
681
682                 AVFilterContext* filt_vsrc = nullptr;
683                 FF(avfilter_graph_create_filter(
684                                 &filt_vsrc,
685                                 avfilter_get_by_name("buffer"),
686                                 "ffmpeg_consumer_buffer",
687                                 vsrc_options.c_str(),
688                                 nullptr,
689                                 video_graph_.get()));
690
691                 AVFilterContext* filt_vsink = nullptr;
692                 FF(avfilter_graph_create_filter(
693                                 &filt_vsink,
694                                 avfilter_get_by_name("buffersink"),
695                                 "ffmpeg_consumer_buffersink",
696                                 nullptr,
697                                 nullptr,
698                                 video_graph_.get()));
699
700 #pragma warning (push)
701 #pragma warning (disable : 4245)
702
703                 if (preferred_pix_fmt)
704                 {
705                         auto requested_fmt = av_get_pix_fmt(preferred_pix_fmt->c_str());
706                         auto valid_fmts = from_terminated_array<AVPixelFormat>(codec.pix_fmts, AVPixelFormat::AV_PIX_FMT_NONE);
707
708                         if (!cpplinq::from(valid_fmts).contains(requested_fmt))
709                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(*preferred_pix_fmt + " is not supported by codec."));
710
711                         std::vector<AVPixelFormat> fmts = { requested_fmt, AVPixelFormat::AV_PIX_FMT_NONE };
712
713                         FF(av_opt_set_int_list(
714                                 filt_vsink,
715                                 "pix_fmts",
716                                 fmts.data(),
717                                 -1,
718                                 AV_OPT_SEARCH_CHILDREN));
719                 }
720                 else
721                 {
722                         FF(av_opt_set_int_list(
723                                 filt_vsink,
724                                 "pix_fmts",
725                                 codec.pix_fmts,
726                                 -1,
727                                 AV_OPT_SEARCH_CHILDREN));
728                 }
729
730
731 #pragma warning (pop)
732
733                 adjust_video_filter(codec, in_video_format_, filt_vsink, filtergraph);
734
735                 if (in_video_format_.width < 1280)
736                         video_graph_->scale_sws_opts = "out_color_matrix=bt601";
737                 else
738                         video_graph_->scale_sws_opts = "out_color_matrix=bt709";
739
740                 configure_filtergraph(
741                                 *video_graph_,
742                                 filtergraph,
743                                 *filt_vsrc,
744                                 *filt_vsink);
745
746                 video_graph_in_  = filt_vsrc;
747                 video_graph_out_ = filt_vsink;
748
749                 CASPAR_LOG(info)
750                         <<      u16(std::string("\n")
751                                 + avfilter_graph_dump(
752                                                 video_graph_.get(),
753                                                 nullptr));
754         }
755
756         void configure_audio_filters(
757                         const AVCodec& codec,
758                         std::string filtergraph)
759         {
760                 int num_output_pads = 1;
761
762                 if (mono_streams_)
763                 {
764                         num_output_pads = in_channel_layout_.num_channels;
765                 }
766
767                 if (num_output_pads > 1)
768                 {
769                         std::string splitfilter = "[a:0]channelsplit=channel_layout=";
770
771                         splitfilter += (boost::format("0x%|1$x|") % create_channel_layout_bitmask(in_channel_layout_.num_channels)).str();
772
773                         for (int i = 0; i < num_output_pads; ++i)
774                                 splitfilter += "[aout:" + boost::lexical_cast<std::string>(i) + "]";
775
776                         filtergraph = u8(append_filter(u16(filtergraph), u16(splitfilter)));
777                 }
778
779                 std::vector<audio_output_pad> output_pads(
780                                 num_output_pads,
781                                 audio_output_pad(
782                                                 from_terminated_array<int>(                             codec.supported_samplerates,    0),
783                                                 from_terminated_array<AVSampleFormat>(  codec.sample_fmts,                              AVSampleFormat::AV_SAMPLE_FMT_NONE),
784                                                 from_terminated_array<uint64_t>(                codec.channel_layouts,                  static_cast<uint64_t>(0))));
785
786                 audio_filter_.reset(new audio_filter(
787                                 { audio_input_pad(
788                                                 boost::rational<int>(1, in_video_format_.audio_sample_rate),
789                                                 in_video_format_.audio_sample_rate,
790                                                 AVSampleFormat::AV_SAMPLE_FMT_S32,
791                                                 create_channel_layout_bitmask(in_channel_layout_.num_channels)) },
792                                                 output_pads,
793                                                 filtergraph));
794         }
795
796         void configure_filtergraph(
797                         AVFilterGraph& graph,
798                         const std::string& filtergraph,
799                         AVFilterContext& source_ctx,
800                         AVFilterContext& sink_ctx)
801         {
802                 AVFilterInOut* outputs = nullptr;
803                 AVFilterInOut* inputs = nullptr;
804
805                 if(!filtergraph.empty())
806                 {
807                         outputs = avfilter_inout_alloc();
808                         inputs  = avfilter_inout_alloc();
809
810                         try
811                         {
812                                 CASPAR_VERIFY(outputs && inputs);
813
814                                 outputs->name           = av_strdup("in");
815                                 outputs->filter_ctx     = &source_ctx;
816                                 outputs->pad_idx                = 0;
817                                 outputs->next           = nullptr;
818
819                                 inputs->name                    = av_strdup("out");
820                                 inputs->filter_ctx      = &sink_ctx;
821                                 inputs->pad_idx         = 0;
822                                 inputs->next                    = nullptr;
823                         }
824                         catch (...)
825                         {
826                                 avfilter_inout_free(&outputs);
827                                 avfilter_inout_free(&inputs);
828                                 throw;
829                         }
830
831                         FF(avfilter_graph_parse(
832                                         &graph,
833                                         filtergraph.c_str(),
834                                         inputs,
835                                         outputs,
836                                         nullptr));
837                 }
838                 else
839                 {
840                         FF(avfilter_link(
841                                         &source_ctx,
842                                         0,
843                                         &sink_ctx,
844                                         0));
845                 }
846
847                 FF(avfilter_graph_config(
848                                 &graph,
849                                 nullptr));
850         }
851
852         void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
853         {
854                 if(!video_st_)
855                         return;
856
857                 auto enc = video_st_->codec;
858
859                 if(frame_ptr != core::const_frame::empty())
860                 {
861                         auto src_av_frame = create_frame();
862
863                         const auto sample_aspect_ratio =
864                                 boost::rational<int>(
865                                         in_video_format_.square_width,
866                                         in_video_format_.square_height) /
867                                 boost::rational<int>(
868                                         in_video_format_.width,
869                                         in_video_format_.height);
870
871                         src_av_frame->format                                            = AVPixelFormat::AV_PIX_FMT_BGRA;
872                         src_av_frame->width                                             = in_video_format_.width;
873                         src_av_frame->height                                            = in_video_format_.height;
874                         src_av_frame->sample_aspect_ratio.num   = sample_aspect_ratio.numerator();
875                         src_av_frame->sample_aspect_ratio.den   = sample_aspect_ratio.denominator();
876                         src_av_frame->pts                                               = video_pts_;
877
878                         video_pts_ += 1;
879
880                         subject_
881                                         << core::monitor::message("/frame")     % video_pts_
882                                         << core::monitor::message("/path")      % path_
883                                         << core::monitor::message("/fps")       % in_video_format_.fps;
884
885                         FF(av_image_fill_arrays(
886                                 src_av_frame->data,
887                                 src_av_frame->linesize,
888                                 frame_ptr.image_data().begin(),
889                                 static_cast<AVPixelFormat>(src_av_frame->format),
890                                 in_video_format_.width,
891                                 in_video_format_.height,
892                                 1));
893
894                         FF(av_buffersrc_add_frame(
895                                 video_graph_in_,
896                                 src_av_frame.get()));
897                 }
898
899                 int ret = 0;
900
901                 while(ret >= 0)
902                 {
903                         auto filt_frame = create_frame();
904
905                         ret = av_buffersink_get_frame(
906                                 video_graph_out_,
907                                 filt_frame.get());
908
909                         video_encoder_executor_.begin_invoke([=]
910                         {
911                                 if(ret == AVERROR_EOF)
912                                 {
913                                         if(enc->codec->capabilities & CODEC_CAP_DELAY)
914                                         {
915                                                 while(encode_av_frame(
916                                                                 *video_st_,
917                                                                 avcodec_encode_video2,
918                                                                 nullptr, token))
919                                                 {
920                                                         boost::this_thread::yield(); // TODO:
921                                                 }
922                                         }
923                                 }
924                                 else if(ret != AVERROR(EAGAIN))
925                                 {
926                                         FF_RET(ret, "av_buffersink_get_frame");
927
928                                         if (filt_frame->interlaced_frame)
929                                         {
930                                                 if (enc->codec->id == AV_CODEC_ID_MJPEG)
931                                                         enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
932                                                 else
933                                                         enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
934                                         }
935                                         else
936                                                 enc->field_order = AV_FIELD_PROGRESSIVE;
937
938                                         filt_frame->quality = enc->global_quality;
939
940                                         if (!enc->me_threshold)
941                                                 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
942
943                                         encode_av_frame(
944                                                 *video_st_,
945                                                 avcodec_encode_video2,
946                                                 filt_frame,
947                                                 token);
948
949                                         boost::this_thread::yield(); // TODO:
950                                 }
951                         });
952                 }
953         }
954
955         void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
956         {
957                 if(audio_sts_.empty())
958                         return;
959
960                 if(frame_ptr != core::const_frame::empty())
961                 {
962                         auto src_av_frame = create_frame();
963
964                         src_av_frame->channels                  = in_channel_layout_.num_channels;
965                         src_av_frame->channel_layout            = create_channel_layout_bitmask(in_channel_layout_.num_channels);
966                         src_av_frame->sample_rate               = in_video_format_.audio_sample_rate;
967                         src_av_frame->nb_samples                        = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
968                         src_av_frame->format                            = AV_SAMPLE_FMT_S32;
969                         src_av_frame->pts                               = audio_pts_;
970
971                         audio_pts_ += src_av_frame->nb_samples;
972
973                         FF(av_samples_fill_arrays(
974                                         src_av_frame->extended_data,
975                                         src_av_frame->linesize,
976                                         reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
977                                         src_av_frame->channels,
978                                         src_av_frame->nb_samples,
979                                         static_cast<AVSampleFormat>(src_av_frame->format),
980                                         16));
981
982                         audio_filter_->push(0, src_av_frame);
983                 }
984
985                 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
986                 {
987                         for (auto filt_frame : audio_filter_->poll_all(pad_id))
988                         {
989                                 audio_encoder_executor_.begin_invoke([=]
990                                 {
991                                         encode_av_frame(
992                                                         *audio_sts_.at(pad_id),
993                                                         avcodec_encode_audio2,
994                                                         filt_frame,
995                                                         token);
996
997                                         boost::this_thread::yield(); // TODO:
998                                 });
999                         }
1000                 }
1001
1002                 bool eof = frame_ptr == core::const_frame::empty();
1003
1004                 if (eof)
1005                 {
1006                         audio_encoder_executor_.begin_invoke([=]
1007                         {
1008                                 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
1009                                 {
1010                                         auto enc = audio_sts_.at(pad_id)->codec;
1011
1012                                         if (enc->codec->capabilities & CODEC_CAP_DELAY)
1013                                         {
1014                                                 while (encode_av_frame(
1015                                                                 *audio_sts_.at(pad_id),
1016                                                                 avcodec_encode_audio2,
1017                                                                 nullptr,
1018                                                                 token))
1019                                                 {
1020                                                         boost::this_thread::yield(); // TODO:
1021                                                 }
1022                                         }
1023                                 }
1024                         });
1025                 }
1026         }
1027
1028         template<typename F>
1029         bool encode_av_frame(
1030                         AVStream& st,
1031                         const F& func,
1032                         const std::shared_ptr<AVFrame>& src_av_frame,
1033                         std::shared_ptr<void> token)
1034         {
1035                 AVPacket pkt = {};
1036                 av_init_packet(&pkt);
1037
1038                 int got_packet = 0;
1039
1040                 FF(func(
1041                         st.codec,
1042                         &pkt,
1043                         src_av_frame.get(),
1044                         &got_packet));
1045
1046                 if(!got_packet || pkt.size <= 0)
1047                         return false;
1048
1049                 pkt.stream_index = st.index;
1050
1051                 if (pkt.pts != AV_NOPTS_VALUE)
1052                 {
1053                         pkt.pts =
1054                                 av_rescale_q(
1055                                         pkt.pts,
1056                                         st.codec->time_base,
1057                                         st.time_base);
1058                 }
1059
1060                 if (pkt.dts != AV_NOPTS_VALUE)
1061                 {
1062                         pkt.dts =
1063                                 av_rescale_q(
1064                                         pkt.dts,
1065                                         st.codec->time_base,
1066                                         st.time_base);
1067                 }
1068
1069                 pkt.duration =
1070                         static_cast<int>(
1071                                 av_rescale_q(
1072                                         pkt.duration,
1073                                         st.codec->time_base, st.time_base));
1074
1075                 write_packet(
1076                         std::shared_ptr<AVPacket>(
1077                                 new AVPacket(pkt),
1078                                 [](AVPacket* p)
1079                                 {
1080                                         av_free_packet(p);
1081                                         delete p;
1082                                 }), token);
1083
1084                 return true;
1085         }
1086
1087         void write_packet(
1088                         const std::shared_ptr<AVPacket>& pkt_ptr,
1089                         std::shared_ptr<void> token)
1090         {
1091                 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1092                 {
1093                         FF(av_interleaved_write_frame(
1094                                 oc_.get(),
1095                                 pkt_ptr.get()));
1096                 });
1097         }
1098
1099         template<typename T>
1100         static boost::optional<T> try_remove_arg(
1101                         std::map<std::string, std::string>& options,
1102                         const boost::regex& expr)
1103         {
1104                 for(auto it = options.begin(); it != options.end(); ++it)
1105                 {
1106                         if(boost::regex_search(it->first, expr))
1107                         {
1108                                 auto arg = it->second;
1109                                 options.erase(it);
1110                                 return boost::lexical_cast<T>(arg);
1111                         }
1112                 }
1113
1114                 return boost::optional<T>();
1115         }
1116
1117         static std::map<std::string, std::string> remove_options(
1118                         std::map<std::string, std::string>& options,
1119                         const boost::regex& expr)
1120         {
1121                 std::map<std::string, std::string> result;
1122
1123                 auto it = options.begin();
1124                 while(it != options.end())
1125                 {
1126                         boost::smatch what;
1127                         if(boost::regex_search(it->first, what, expr))
1128                         {
1129                                 result[
1130                                         what.size() > 0 && what[1].matched
1131                                                 ? what[1].str()
1132                                                 : it->first] = it->second;
1133                                 it = options.erase(it);
1134                         }
1135                         else
1136                                 ++it;
1137                 }
1138
1139                 return result;
1140         }
1141
1142         static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1143         {
1144                 for (const auto& entry : c)
1145                 {
1146                         av_dict_set(
1147                                 dest,
1148                                 entry.first.c_str(),
1149                                 entry.second.c_str(), 0);
1150                 }
1151         }
1152
1153         static std::map<std::string, std::string> to_map(AVDictionary* dict)
1154         {
1155                 std::map<std::string, std::string> result;
1156
1157                 for(auto t = dict
1158                                 ? av_dict_get(
1159                                         dict,
1160                                         "",
1161                                         nullptr,
1162                                         AV_DICT_IGNORE_SUFFIX)
1163                                 : nullptr;
1164                         t;
1165                         t = av_dict_get(
1166                                 dict,
1167                                 "",
1168                                 t,
1169                                 AV_DICT_IGNORE_SUFFIX))
1170                 {
1171                         result[t->key] = t->value;
1172                 }
1173
1174                 return result;
1175         }
1176 };
1177
1178 int crc16(const std::string& str)
1179 {
1180         boost::crc_16_type result;
1181
1182         result.process_bytes(str.data(), str.length());
1183
1184         return result.checksum();
1185 }
1186
1187 struct ffmpeg_consumer_proxy : public core::frame_consumer
1188 {
1189         const std::string                                       path_;
1190         const std::string                                       options_;
1191         const bool                                                      separate_key_;
1192         const bool                                                      mono_streams_;
1193         const bool                                                      compatibility_mode_;
1194         int                                                                     consumer_index_offset_;
1195
1196         std::unique_ptr<ffmpeg_consumer>        consumer_;
1197         std::unique_ptr<ffmpeg_consumer>        key_only_consumer_;
1198
1199 public:
1200
1201         ffmpeg_consumer_proxy(const std::string& path, const std::string& options, bool separate_key, bool mono_streams, bool compatibility_mode)
1202                 : path_(path)
1203                 , options_(options)
1204                 , separate_key_(separate_key)
1205                 , mono_streams_(mono_streams)
1206                 , compatibility_mode_(compatibility_mode)
1207                 , consumer_index_offset_(crc16(path))
1208         {
1209         }
1210
1211         void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int) override
1212         {
1213                 if (consumer_)
1214                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
1215
1216                 consumer_.reset(new ffmpeg_consumer(path_, options_, mono_streams_));
1217                 consumer_->initialize(format_desc, channel_layout);
1218
1219                 if (separate_key_)
1220                 {
1221                         boost::filesystem::path fill_file(path_);
1222                         auto without_extension = u16(fill_file.parent_path().string() + "/" + fill_file.stem().string());
1223                         auto key_file = without_extension + L"_A" + u16(fill_file.extension().string());
1224
1225                         key_only_consumer_.reset(new ffmpeg_consumer(u8(key_file), options_, mono_streams_));
1226                         key_only_consumer_->initialize(format_desc, channel_layout);
1227                 }
1228         }
1229
1230         int64_t presentation_frame_age_millis() const override
1231         {
1232                 return consumer_ ? static_cast<int64_t>(consumer_->presentation_frame_age_millis()) : 0;
1233         }
1234
1235         std::future<bool> send(core::const_frame frame) override
1236         {
1237                 bool ready_for_frame = consumer_->ready_for_frame();
1238
1239                 if (ready_for_frame && separate_key_)
1240                         ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
1241
1242                 if (ready_for_frame)
1243                 {
1244                         consumer_->send(frame);
1245
1246                         if (separate_key_)
1247                                 key_only_consumer_->send(frame.key_only());
1248                 }
1249                 else
1250                 {
1251                         consumer_->mark_dropped();
1252
1253                         if (separate_key_)
1254                                 key_only_consumer_->mark_dropped();
1255                 }
1256
1257                 return make_ready_future(true);
1258         }
1259
1260         std::wstring print() const override
1261         {
1262                 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
1263         }
1264
1265         std::wstring name() const override
1266         {
1267                 return L"ffmpeg";
1268         }
1269
1270         boost::property_tree::wptree info() const override
1271         {
1272                 boost::property_tree::wptree info;
1273
1274                 info.add(L"type",                       L"ffmpeg");
1275                 info.add(L"path",                       u16(path_));
1276                 info.add(L"separate_key",       separate_key_);
1277                 info.add(L"mono_streams",       mono_streams_);
1278
1279                 return info;
1280         }
1281
1282         bool has_synchronization_clock() const override
1283         {
1284                 return false;
1285         }
1286
1287         int buffer_depth() const override
1288         {
1289                 return -1;
1290         }
1291
1292         int index() const override
1293         {
1294                 return compatibility_mode_ ? 200 : 100000 + consumer_index_offset_;
1295         }
1296
1297         core::monitor::subject& monitor_output() override
1298         {
1299                 return consumer_->monitor_output();
1300         }
1301 };
1302
1303 void describe_ffmpeg_consumer(core::help_sink& sink, const core::help_repository& repo)
1304 {
1305         sink.short_description(L"For streaming/recording the contents of a channel using FFmpeg.");
1306         sink.syntax(L"FILE,STREAM [filename:string],[url:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}} {[separate_key:SEPARATE_KEY]} {[mono_streams:MONO_STREAMS]}");
1307         sink.para()->text(L"For recording or streaming the contents of a channel using FFmpeg");
1308         sink.definitions()
1309                 ->item(L"filename",                     L"The filename under the media folder including the extension (decides which kind of container format that will be used).")
1310                 ->item(L"url",                          L"If the filename is given in the form of an URL a network stream will be created instead of a file on disk.")
1311                 ->item(L"ffmpeg_paramX",                L"A parameter supported by FFmpeg. For example vcodec or acodec etc.")
1312                 ->item(L"separate_key",         L"If defined will create two files simultaneously -- One for fill and one for key (_A will be appended).")
1313                 ->item(L"mono_streams",         L"If defined every audio channel will be written to its own audio stream.");
1314         sink.para()->text(L"Examples:");
1315         sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd");
1316         sink.example(L">> ADD 1 FILE output.mov -vcodec prores");
1317         sink.example(L">> ADD 1 FILE output.mov -vcodec dvvideo");
1318         sink.example(L">> ADD 1 FILE output.mov -vcodec libx264 -preset ultrafast -tune fastdecode -crf 25");
1319         sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd SEPARATE_KEY", L"for creating output.mov with fill and output_A.mov with key/alpha");
1320         sink.example(L">> ADD 1 FILE output.mxf -vcodec dnxhd MONO_STREAMS", L"for creating output.mxf with every audio channel encoded in its own mono stream.");
1321         sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast",
1322                 L"for streaming over UDP instead of creating a local file.");
1323 }
1324
1325 spl::shared_ptr<core::frame_consumer> create_ffmpeg_consumer(
1326                 const std::vector<std::wstring>& params, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1327 {
1328         if (params.size() < 1 || (!boost::iequals(params.at(0), L"STREAM") && !boost::iequals(params.at(0), L"FILE")))
1329                 return core::frame_consumer::empty();
1330
1331         auto params2                    = params;
1332         bool separate_key               = get_and_consume_flag(L"SEPARATE_KEY", params2);
1333         bool mono_streams               = get_and_consume_flag(L"MONO_STREAMS", params2);
1334         auto compatibility_mode = boost::iequals(params.at(0), L"FILE");
1335         auto path                               = u8(params2.size() > 1 ? params2.at(1) : L"");
1336
1337         // remove FILE or STREAM
1338         params2.erase(params2.begin());
1339
1340         // remove path
1341         if (!path.empty())
1342                 params2.erase(params2.begin());
1343
1344         // join only the args
1345         auto args                               = u8(boost::join(params2, L" "));
1346
1347         return spl::make_shared<ffmpeg_consumer_proxy>(path, args, separate_key, mono_streams, compatibility_mode);
1348 }
1349
1350 spl::shared_ptr<core::frame_consumer> create_preconfigured_ffmpeg_consumer(
1351                 const boost::property_tree::wptree& ptree, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1352 {
1353         return spl::make_shared<ffmpeg_consumer_proxy>(
1354                         u8(ptree_get<std::wstring>(ptree, L"path")),
1355                         u8(ptree.get<std::wstring>(L"args", L"")),
1356                         ptree.get<bool>(L"separate-key", false),
1357                         ptree.get<bool>(L"mono-streams", false),
1358                         false);
1359 }
1360
1361 }}