]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/consumer/streaming_consumer.cpp
f5d2997e8e010212f4921b5244c157d910e35c30
[casparcg] / modules / ffmpeg / consumer / streaming_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 { namespace {
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                         video_encoder_executor_.begin_invoke([&] { encode_video(core::const_frame::empty(), nullptr); });
251                         audio_encoder_executor_.begin_invoke([&] { encode_audio(core::const_frame::empty(), nullptr); });
252
253                         video_encoder_executor_.stop();
254                         audio_encoder_executor_.stop();
255                         video_encoder_executor_.join();
256                         audio_encoder_executor_.join();
257
258                         video_graph_.reset();
259                         audio_filter_.reset();
260                         video_st_.reset();
261                         audio_sts_.clear();
262
263                         write_packet(nullptr, nullptr);
264
265                         write_executor_.stop();
266                         write_executor_.join();
267
268                         FF(av_write_trailer(oc_.get()));
269
270                         if (!(oc_->oformat->flags & AVFMT_NOFILE) && oc_->pb)
271                                 avio_close(oc_->pb);
272
273                         oc_.reset();
274                 }
275         }
276
277         void initialize(
278                         const core::video_format_desc& format_desc,
279                         const core::audio_channel_layout& channel_layout)
280         {
281                 try
282                 {
283                         static boost::regex prot_exp("^.+:.*" );
284
285                         if(!boost::regex_match(
286                                         path_,
287                                         prot_exp))
288                         {
289                                 if(!full_path_.is_complete())
290                                 {
291                                         full_path_ =
292                                                 u8(
293                                                         env::media_folder()) +
294                                                         path_;
295                                 }
296
297                                 if(boost::filesystem::exists(full_path_))
298                                         boost::filesystem::remove(full_path_);
299
300                                 boost::filesystem::create_directories(full_path_.parent_path());
301                         }
302
303                         graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
304                         graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
305                         graph_->set_text(print());
306                         diagnostics::register_graph(graph_);
307
308                         const auto oformat_name =
309                                 try_remove_arg<std::string>(
310                                         options_,
311                                         boost::regex("^f|format$"));
312
313                         AVFormatContext* oc;
314
315                         FF(avformat_alloc_output_context2(
316                                 &oc,
317                                 nullptr,
318                                 oformat_name && !oformat_name->empty() ? oformat_name->c_str() : nullptr,
319                                 full_path_.string().c_str()));
320
321                         oc_.reset(
322                                 oc,
323                                 avformat_free_context);
324
325                         CASPAR_VERIFY(oc_->oformat);
326
327                         oc_->interrupt_callback.callback = ffmpeg_consumer::interrupt_cb;
328                         oc_->interrupt_callback.opaque   = this;
329
330                         CASPAR_VERIFY(format_desc.format != core::video_format::invalid);
331
332                         in_video_format_ = format_desc;
333                         in_channel_layout_ = channel_layout;
334
335                         CASPAR_VERIFY(oc_->oformat);
336
337                         const auto video_codec_name =
338                                 try_remove_arg<std::string>(
339                                         options_,
340                                         boost::regex("^c:v|codec:v|vcodec$"));
341
342                         const auto video_codec =
343                                 video_codec_name
344                                         ? avcodec_find_encoder_by_name(video_codec_name->c_str())
345                                         : avcodec_find_encoder(oc_->oformat->video_codec);
346
347                         const auto audio_codec_name =
348                                 try_remove_arg<std::string>(
349                                         options_,
350                                          boost::regex("^c:a|codec:a|acodec$"));
351
352                         const auto audio_codec =
353                                 audio_codec_name
354                                         ? avcodec_find_encoder_by_name(audio_codec_name->c_str())
355                                         : (is_pcm_s24le_not_supported(*oc_)
356                                                 ? avcodec_find_encoder(oc_->oformat->audio_codec)
357                                                 : avcodec_find_encoder_by_name("pcm_s24le"));
358
359                         if (!video_codec)
360                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
361                                                 "Failed to find video codec " + (video_codec_name
362                                                                 ? *video_codec_name
363                                                                 : "with id " + boost::lexical_cast<std::string>(
364                                                                                 oc_->oformat->video_codec))));
365                         if (!audio_codec)
366                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
367                                                 "Failed to find audio codec " + (audio_codec_name
368                                                                 ? *audio_codec_name
369                                                                 : "with id " + boost::lexical_cast<std::string>(
370                                                                                 oc_->oformat->audio_codec))));
371
372                         // Filters
373
374                         {
375                                 configure_video_filters(
376                                         *video_codec,
377                                         try_remove_arg<std::string>(options_,
378                                         boost::regex("vf|f:v|filter:v")).get_value_or(""));
379
380                                 configure_audio_filters(
381                                         *audio_codec,
382                                         try_remove_arg<std::string>(options_,
383                                         boost::regex("af|f:a|filter:a")).get_value_or(""));
384                         }
385
386                         // Encoders
387
388                         {
389                                 auto video_options = options_;
390                                 auto audio_options = options_;
391
392                                 video_st_ = open_encoder(
393                                         *video_codec,
394                                         video_options,
395                                         0);
396
397                                 for (int i = 0; i < audio_filter_->get_num_output_pads(); ++i)
398                                         audio_sts_.push_back(open_encoder(
399                                                         *audio_codec,
400                                                         audio_options,
401                                                         i));
402
403                                 auto it = options_.begin();
404                                 while(it != options_.end())
405                                 {
406                                         if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
407                                                 it = options_.erase(it);
408                                         else
409                                                 ++it;
410                                 }
411                         }
412
413                         // Output
414                         {
415                                 AVDictionary* av_opts = nullptr;
416
417                                 to_dict(
418                                         &av_opts,
419                                         std::move(options_));
420
421                                 CASPAR_SCOPE_EXIT
422                                 {
423                                         av_dict_free(&av_opts);
424                                 };
425
426                                 if (!(oc_->oformat->flags & AVFMT_NOFILE))
427                                 {
428                                         FF(avio_open2(
429                                                 &oc_->pb,
430                                                 full_path_.string().c_str(),
431                                                 AVIO_FLAG_WRITE,
432                                                 &oc_->interrupt_callback,
433                                                 &av_opts));
434                                 }
435
436                                 FF(avformat_write_header(
437                                         oc_.get(),
438                                         &av_opts));
439
440                                 options_ = to_map(av_opts);
441                         }
442
443                         // Dump Info
444
445                         av_dump_format(
446                                 oc_.get(),
447                                 0,
448                                 oc_->filename,
449                                 1);
450
451                         for (const auto& option : options_)
452                         {
453                                 CASPAR_LOG(warning)
454                                         << L"Invalid option: -"
455                                         << u16(option.first)
456                                         << L" "
457                                         << u16(option.second);
458                         }
459                 }
460                 catch(...)
461                 {
462                         video_st_.reset();
463                         audio_sts_.clear();
464                         oc_.reset();
465                         throw;
466                 }
467         }
468
469         core::monitor::subject& monitor_output()
470         {
471                 return subject_;
472         }
473
474         void send(core::const_frame frame)
475         {
476                 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
477
478                 auto frame_timer = spl::make_shared<caspar::timer>();
479
480                 std::shared_ptr<void> token(
481                         nullptr,
482                         [this, frame, frame_timer](void*)
483                         {
484                                 tokens_.release();
485                                 current_encoding_delay_ = frame.get_age_millis();
486                                 graph_->set_value("frame-time", frame_timer->elapsed() * in_video_format_.fps * 0.5);
487                         });
488                 tokens_.acquire();
489
490                 video_encoder_executor_.begin_invoke([=]() mutable
491                 {
492                         encode_video(
493                                 frame,
494                                 token);
495                 });
496
497                 audio_encoder_executor_.begin_invoke([=]() mutable
498                 {
499                         encode_audio(
500                                 frame,
501                                 token);
502                 });
503         }
504
505         bool ready_for_frame() const
506         {
507                 return tokens_.permits() > 0;
508         }
509
510         void mark_dropped()
511         {
512                 graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
513         }
514
515         std::wstring print() const
516         {
517                 return L"ffmpeg_consumer[" + u16(path_) + L"]";
518         }
519
520         int64_t presentation_frame_age_millis() const
521         {
522                 return current_encoding_delay_;
523         }
524
525 private:
526
527         static int interrupt_cb(void* ctx)
528         {
529                 CASPAR_ASSERT(ctx);
530                 return reinterpret_cast<ffmpeg_consumer*>(ctx)->abort_request_;
531         }
532
533         std::shared_ptr<AVStream> open_encoder(
534                         const AVCodec& codec,
535                         std::map<std::string,
536                         std::string>& options,
537                         int stream_number_for_media_type)
538         {
539                 auto st =
540                         avformat_new_stream(
541                                 oc_.get(),
542                                 &codec);
543
544                 if (!st)
545                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("avformat_new_stream"));
546
547                 auto enc = st->codec;
548
549                 CASPAR_VERIFY(enc);
550
551                 switch(enc->codec_type)
552                 {
553                         case AVMEDIA_TYPE_VIDEO:
554                         {
555                                 enc->time_base                          = video_graph_out_->inputs[0]->time_base;
556                                 enc->pix_fmt                                    = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
557                                 enc->sample_aspect_ratio                = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
558                                 enc->width                                      = video_graph_out_->inputs[0]->w;
559                                 enc->height                                     = video_graph_out_->inputs[0]->h;
560                                 enc->bit_rate_tolerance         = 400 * 1000000;
561
562                                 break;
563                         }
564                         case AVMEDIA_TYPE_AUDIO:
565                         {
566                                 enc->time_base                          = audio_filter_->get_output_pad_info(stream_number_for_media_type).time_base;
567                                 enc->sample_fmt                         = static_cast<AVSampleFormat>(audio_filter_->get_output_pad_info(stream_number_for_media_type).format);
568                                 enc->sample_rate                                = audio_filter_->get_output_pad_info(stream_number_for_media_type).sample_rate;
569                                 enc->channel_layout                     = audio_filter_->get_output_pad_info(stream_number_for_media_type).channel_layout;
570                                 enc->channels                           = audio_filter_->get_output_pad_info(stream_number_for_media_type).channels;
571
572                                 break;
573                         }
574                 }
575
576                 setup_codec_defaults(*enc);
577
578                 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
579                         enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
580
581                 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
582
583                 const auto char_id = char_id_map.at(enc->codec_type);
584
585                 const auto codec_opts =
586                         remove_options(
587                                 options,
588                                 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
589
590                 AVDictionary* av_codec_opts = nullptr;
591
592                 to_dict(
593                         &av_codec_opts,
594                         options);
595
596                 to_dict(
597                         &av_codec_opts,
598                         codec_opts);
599
600                 options.clear();
601
602                 FF(avcodec_open2(
603                         enc,
604                         &codec,
605                         av_codec_opts ? &av_codec_opts : nullptr));
606
607                 if(av_codec_opts)
608                 {
609                         auto t =
610                                 av_dict_get(
611                                         av_codec_opts,
612                                         "",
613                                          nullptr,
614                                         AV_DICT_IGNORE_SUFFIX);
615
616                         while(t)
617                         {
618                                 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
619
620                                 t = av_dict_get(
621                                                 av_codec_opts,
622                                                 "",
623                                                 t,
624                                                 AV_DICT_IGNORE_SUFFIX);
625                         }
626
627                         av_dict_free(&av_codec_opts);
628                 }
629
630                 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
631                 {
632                         CASPAR_ASSERT(enc->frame_size > 0);
633                         audio_filter_->set_guaranteed_output_num_samples_per_frame(
634                                         stream_number_for_media_type,
635                                         enc->frame_size);
636                 }
637
638                 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
639                 {
640                         avcodec_close(st->codec);
641                 });
642         }
643
644         void configure_video_filters(
645                         const AVCodec& codec,
646                         std::string filtergraph)
647         {
648                 video_graph_.reset(
649                                 avfilter_graph_alloc(),
650                                 [](AVFilterGraph* p)
651                                 {
652                                         avfilter_graph_free(&p);
653                                 });
654
655                 video_graph_->nb_threads  = boost::thread::hardware_concurrency()/2;
656                 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
657
658                 const auto sample_aspect_ratio =
659                         boost::rational<int>(
660                                         in_video_format_.square_width,
661                                         in_video_format_.square_height) /
662                         boost::rational<int>(
663                                         in_video_format_.width,
664                                         in_video_format_.height);
665
666                 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%")
667                         % in_video_format_.width % in_video_format_.height
668                         % AVPixelFormat::AV_PIX_FMT_BGRA
669                         % in_video_format_.duration     % in_video_format_.time_scale
670                         % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
671                         % in_video_format_.time_scale % in_video_format_.duration).str();
672
673                 AVFilterContext* filt_vsrc = nullptr;
674                 FF(avfilter_graph_create_filter(
675                                 &filt_vsrc,
676                                 avfilter_get_by_name("buffer"),
677                                 "ffmpeg_consumer_buffer",
678                                 vsrc_options.c_str(),
679                                 nullptr,
680                                 video_graph_.get()));
681
682                 AVFilterContext* filt_vsink = nullptr;
683                 FF(avfilter_graph_create_filter(
684                                 &filt_vsink,
685                                 avfilter_get_by_name("buffersink"),
686                                 "ffmpeg_consumer_buffersink",
687                                 nullptr,
688                                 nullptr,
689                                 video_graph_.get()));
690
691 #pragma warning (push)
692 #pragma warning (disable : 4245)
693
694                 FF(av_opt_set_int_list(
695                                 filt_vsink,
696                                 "pix_fmts",
697                                 codec.pix_fmts,
698                                 -1,
699                                 AV_OPT_SEARCH_CHILDREN));
700
701 #pragma warning (pop)
702
703                 adjust_video_filter(codec, in_video_format_, filt_vsink, filtergraph);
704
705                 configure_filtergraph(
706                                 *video_graph_,
707                                 filtergraph,
708                                 *filt_vsrc,
709                                 *filt_vsink);
710
711                 video_graph_in_  = filt_vsrc;
712                 video_graph_out_ = filt_vsink;
713
714                 CASPAR_LOG(info)
715                         <<      u16(std::string("\n")
716                                 + avfilter_graph_dump(
717                                                 video_graph_.get(),
718                                                 nullptr));
719         }
720
721         void configure_audio_filters(
722                         const AVCodec& codec,
723                         std::string filtergraph)
724         {
725                 int num_output_pads = 1;
726
727                 if (mono_streams_)
728                 {
729                         num_output_pads = in_channel_layout_.num_channels;
730                 }
731
732                 if (num_output_pads > 1)
733                 {
734                         std::string splitfilter = "[a:0]channelsplit=channel_layout=";
735
736                         splitfilter += (boost::format("0x%|1$x|") % create_channel_layout_bitmask(in_channel_layout_.num_channels)).str();
737
738                         for (int i = 0; i < num_output_pads; ++i)
739                                 splitfilter += "[aout:" + boost::lexical_cast<std::string>(i) + "]";
740
741                         filtergraph = u8(append_filter(u16(filtergraph), u16(splitfilter)));
742                 }
743
744                 std::vector<audio_output_pad> output_pads(
745                                 num_output_pads,
746                                 audio_output_pad(
747                                                 from_terminated_array<int>(                             codec.supported_samplerates,    0),
748                                                 from_terminated_array<AVSampleFormat>(  codec.sample_fmts,                              AVSampleFormat::AV_SAMPLE_FMT_NONE),
749                                                 from_terminated_array<uint64_t>(                codec.channel_layouts,                  0ull)));
750
751                 audio_filter_.reset(new audio_filter(
752                                 { audio_input_pad(
753                                                 boost::rational<int>(1, in_video_format_.audio_sample_rate),
754                                                 in_video_format_.audio_sample_rate,
755                                                 AVSampleFormat::AV_SAMPLE_FMT_S32,
756                                                 create_channel_layout_bitmask(in_channel_layout_.num_channels)) },
757                                                 output_pads,
758                                                 filtergraph));
759         }
760
761         void configure_filtergraph(
762                         AVFilterGraph& graph,
763                         const std::string& filtergraph,
764                         AVFilterContext& source_ctx,
765                         AVFilterContext& sink_ctx)
766         {
767                 AVFilterInOut* outputs = nullptr;
768                 AVFilterInOut* inputs = nullptr;
769
770                 if(!filtergraph.empty())
771                 {
772                         outputs = avfilter_inout_alloc();
773                         inputs  = avfilter_inout_alloc();
774
775                         try
776                         {
777                                 CASPAR_VERIFY(outputs && inputs);
778
779                                 outputs->name           = av_strdup("in");
780                                 outputs->filter_ctx     = &source_ctx;
781                                 outputs->pad_idx                = 0;
782                                 outputs->next           = nullptr;
783
784                                 inputs->name                    = av_strdup("out");
785                                 inputs->filter_ctx      = &sink_ctx;
786                                 inputs->pad_idx         = 0;
787                                 inputs->next                    = nullptr;
788                         }
789                         catch (...)
790                         {
791                                 avfilter_inout_free(&outputs);
792                                 avfilter_inout_free(&inputs);
793                                 throw;
794                         }
795
796                         FF(avfilter_graph_parse(
797                                         &graph,
798                                         filtergraph.c_str(),
799                                         inputs,
800                                         outputs,
801                                         nullptr));
802                 }
803                 else
804                 {
805                         FF(avfilter_link(
806                                         &source_ctx,
807                                         0,
808                                         &sink_ctx,
809                                         0));
810                 }
811
812                 FF(avfilter_graph_config(
813                                 &graph,
814                                 nullptr));
815         }
816
817         void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
818         {
819                 if(!video_st_)
820                         return;
821
822                 auto enc = video_st_->codec;
823
824                 if(frame_ptr != core::const_frame::empty())
825                 {
826                         auto src_av_frame = create_frame();
827
828                         const auto sample_aspect_ratio =
829                                 boost::rational<int>(
830                                         in_video_format_.square_width,
831                                         in_video_format_.square_height) /
832                                 boost::rational<int>(
833                                         in_video_format_.width,
834                                         in_video_format_.height);
835
836                         src_av_frame->format                                            = AVPixelFormat::AV_PIX_FMT_BGRA;
837                         src_av_frame->width                                             = in_video_format_.width;
838                         src_av_frame->height                                            = in_video_format_.height;
839                         src_av_frame->sample_aspect_ratio.num   = sample_aspect_ratio.numerator();
840                         src_av_frame->sample_aspect_ratio.den   = sample_aspect_ratio.denominator();
841                         src_av_frame->pts                                               = video_pts_;
842
843                         video_pts_ += 1;
844
845                         subject_
846                                         << core::monitor::message("/frame")     % video_pts_
847                                         << core::monitor::message("/path")      % path_
848                                         << core::monitor::message("/fps")       % in_video_format_.fps;
849
850                         FF(av_image_fill_arrays(
851                                 src_av_frame->data,
852                                 src_av_frame->linesize,
853                                 frame_ptr.image_data().begin(),
854                                 static_cast<AVPixelFormat>(src_av_frame->format),
855                                 in_video_format_.width,
856                                 in_video_format_.height,
857                                 1));
858
859                         FF(av_buffersrc_add_frame(
860                                 video_graph_in_,
861                                 src_av_frame.get()));
862                 }
863
864                 int ret = 0;
865
866                 while(ret >= 0)
867                 {
868                         auto filt_frame = create_frame();
869
870                         ret = av_buffersink_get_frame(
871                                 video_graph_out_,
872                                 filt_frame.get());
873
874                         video_encoder_executor_.begin_invoke([=]
875                         {
876                                 if(ret == AVERROR_EOF)
877                                 {
878                                         if(enc->codec->capabilities & CODEC_CAP_DELAY)
879                                         {
880                                                 while(encode_av_frame(
881                                                                 *video_st_,
882                                                                 avcodec_encode_video2,
883                                                                 nullptr, token))
884                                                 {
885                                                         boost::this_thread::yield(); // TODO:
886                                                 }
887                                         }
888                                 }
889                                 else if(ret != AVERROR(EAGAIN))
890                                 {
891                                         FF_RET(ret, "av_buffersink_get_frame");
892
893                                         if (filt_frame->interlaced_frame)
894                                         {
895                                                 if (enc->codec->id == AV_CODEC_ID_MJPEG)
896                                                         enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
897                                                 else
898                                                         enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
899                                         }
900                                         else
901                                                 enc->field_order = AV_FIELD_PROGRESSIVE;
902
903                                         filt_frame->quality = enc->global_quality;
904
905                                         if (!enc->me_threshold)
906                                                 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
907
908                                         encode_av_frame(
909                                                 *video_st_,
910                                                 avcodec_encode_video2,
911                                                 filt_frame,
912                                                 token);
913
914                                         boost::this_thread::yield(); // TODO:
915                                 }
916                         });
917                 }
918         }
919
920         void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
921         {
922                 if(audio_sts_.empty())
923                         return;
924
925                 if(frame_ptr != core::const_frame::empty())
926                 {
927                         auto src_av_frame = create_frame();
928
929                         src_av_frame->channels                  = in_channel_layout_.num_channels;
930                         src_av_frame->channel_layout            = create_channel_layout_bitmask(in_channel_layout_.num_channels);
931                         src_av_frame->sample_rate               = in_video_format_.audio_sample_rate;
932                         src_av_frame->nb_samples                        = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
933                         src_av_frame->format                            = AV_SAMPLE_FMT_S32;
934                         src_av_frame->pts                               = audio_pts_;
935
936                         audio_pts_ += src_av_frame->nb_samples;
937
938                         FF(av_samples_fill_arrays(
939                                         src_av_frame->extended_data,
940                                         src_av_frame->linesize,
941                                         reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
942                                         src_av_frame->channels,
943                                         src_av_frame->nb_samples,
944                                         static_cast<AVSampleFormat>(src_av_frame->format),
945                                         16));
946
947                         audio_filter_->push(0, src_av_frame);
948                 }
949
950                 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
951                 {
952                         for (auto filt_frame : audio_filter_->poll_all(pad_id))
953                         {
954                                 audio_encoder_executor_.begin_invoke([=]
955                                 {
956                                         encode_av_frame(
957                                                         *audio_sts_.at(pad_id),
958                                                         avcodec_encode_audio2,
959                                                         filt_frame,
960                                                         token);
961
962                                         boost::this_thread::yield(); // TODO:
963                                 });
964                         }
965                 }
966
967                 bool eof = frame_ptr == core::const_frame::empty();
968
969                 if (eof)
970                 {
971                         audio_encoder_executor_.begin_invoke([=]
972                         {
973                                 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
974                                 {
975                                         auto enc = audio_sts_.at(pad_id)->codec;
976
977                                         if (enc->codec->capabilities & CODEC_CAP_DELAY)
978                                         {
979                                                 while (encode_av_frame(
980                                                                 *audio_sts_.at(pad_id),
981                                                                 avcodec_encode_audio2,
982                                                                 nullptr,
983                                                                 token))
984                                                 {
985                                                         boost::this_thread::yield(); // TODO:
986                                                 }
987                                         }
988                                 }
989                         });
990                 }
991         }
992
993         template<typename F>
994         bool encode_av_frame(
995                         AVStream& st,
996                         const F& func,
997                         const std::shared_ptr<AVFrame>& src_av_frame,
998                         std::shared_ptr<void> token)
999         {
1000                 AVPacket pkt = {};
1001                 av_init_packet(&pkt);
1002
1003                 int got_packet = 0;
1004
1005                 FF(func(
1006                         st.codec,
1007                         &pkt,
1008                         src_av_frame.get(),
1009                         &got_packet));
1010
1011                 if(!got_packet || pkt.size <= 0)
1012                         return false;
1013
1014                 pkt.stream_index = st.index;
1015
1016                 if (pkt.pts != AV_NOPTS_VALUE)
1017                 {
1018                         pkt.pts =
1019                                 av_rescale_q(
1020                                         pkt.pts,
1021                                         st.codec->time_base,
1022                                         st.time_base);
1023                 }
1024
1025                 if (pkt.dts != AV_NOPTS_VALUE)
1026                 {
1027                         pkt.dts =
1028                                 av_rescale_q(
1029                                         pkt.dts,
1030                                         st.codec->time_base,
1031                                         st.time_base);
1032                 }
1033
1034                 pkt.duration =
1035                         static_cast<int>(
1036                                 av_rescale_q(
1037                                         pkt.duration,
1038                                         st.codec->time_base, st.time_base));
1039
1040                 write_packet(
1041                         std::shared_ptr<AVPacket>(
1042                                 new AVPacket(pkt),
1043                                 [](AVPacket* p)
1044                                 {
1045                                         av_free_packet(p);
1046                                         delete p;
1047                                 }), token);
1048
1049                 return true;
1050         }
1051
1052         void write_packet(
1053                         const std::shared_ptr<AVPacket>& pkt_ptr,
1054                         std::shared_ptr<void> token)
1055         {
1056                 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1057                 {
1058                         FF(av_interleaved_write_frame(
1059                                 oc_.get(),
1060                                 pkt_ptr.get()));
1061                 });
1062         }
1063
1064         template<typename T>
1065         static boost::optional<T> try_remove_arg(
1066                         std::map<std::string, std::string>& options,
1067                         const boost::regex& expr)
1068         {
1069                 for(auto it = options.begin(); it != options.end(); ++it)
1070                 {
1071                         if(boost::regex_search(it->first, expr))
1072                         {
1073                                 auto arg = it->second;
1074                                 options.erase(it);
1075                                 return boost::lexical_cast<T>(arg);
1076                         }
1077                 }
1078
1079                 return boost::optional<T>();
1080         }
1081
1082         static std::map<std::string, std::string> remove_options(
1083                         std::map<std::string, std::string>& options,
1084                         const boost::regex& expr)
1085         {
1086                 std::map<std::string, std::string> result;
1087
1088                 auto it = options.begin();
1089                 while(it != options.end())
1090                 {
1091                         boost::smatch what;
1092                         if(boost::regex_search(it->first, what, expr))
1093                         {
1094                                 result[
1095                                         what.size() > 0 && what[1].matched
1096                                                 ? what[1].str()
1097                                                 : it->first] = it->second;
1098                                 it = options.erase(it);
1099                         }
1100                         else
1101                                 ++it;
1102                 }
1103
1104                 return result;
1105         }
1106
1107         static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1108         {
1109                 for (const auto& entry : c)
1110                 {
1111                         av_dict_set(
1112                                 dest,
1113                                 entry.first.c_str(),
1114                                 entry.second.c_str(), 0);
1115                 }
1116         }
1117
1118         static std::map<std::string, std::string> to_map(AVDictionary* dict)
1119         {
1120                 std::map<std::string, std::string> result;
1121
1122                 for(auto t = dict
1123                                 ? av_dict_get(
1124                                         dict,
1125                                         "",
1126                                         nullptr,
1127                                         AV_DICT_IGNORE_SUFFIX)
1128                                 : nullptr;
1129                         t;
1130                         t = av_dict_get(
1131                                 dict,
1132                                 "",
1133                                 t,
1134                                 AV_DICT_IGNORE_SUFFIX))
1135                 {
1136                         result[t->key] = t->value;
1137                 }
1138
1139                 return result;
1140         }
1141 };
1142
1143 int crc16(const std::string& str)
1144 {
1145         boost::crc_16_type result;
1146
1147         result.process_bytes(str.data(), str.length());
1148
1149         return result.checksum();
1150 }
1151
1152 struct ffmpeg_consumer_proxy : public core::frame_consumer
1153 {
1154         const std::string                                       path_;
1155         const std::string                                       options_;
1156         const bool                                                      separate_key_;
1157         const bool                                                      mono_streams_;
1158         const bool                                                      compatibility_mode_;
1159         int                                                                     consumer_index_offset_;
1160
1161         std::unique_ptr<ffmpeg_consumer>        consumer_;
1162         std::unique_ptr<ffmpeg_consumer>        key_only_consumer_;
1163
1164 public:
1165
1166         ffmpeg_consumer_proxy(const std::string& path, const std::string& options, bool separate_key, bool mono_streams, bool compatibility_mode)
1167                 : path_(path)
1168                 , options_(options)
1169                 , separate_key_(separate_key)
1170                 , mono_streams_(mono_streams)
1171                 , compatibility_mode_(compatibility_mode)
1172                 , consumer_index_offset_(crc16(path))
1173         {
1174         }
1175
1176         void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int) override
1177         {
1178                 if (consumer_)
1179                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
1180
1181                 consumer_.reset(new ffmpeg_consumer(path_, options_, mono_streams_));
1182                 consumer_->initialize(format_desc, channel_layout);
1183
1184                 if (separate_key_)
1185                 {
1186                         boost::filesystem::path fill_file(path_);
1187                         auto without_extension = u16(fill_file.parent_path().string() + "/" + fill_file.stem().string());
1188                         auto key_file = without_extension + L"_A" + u16(fill_file.extension().string());
1189
1190                         key_only_consumer_.reset(new ffmpeg_consumer(u8(key_file), options_, mono_streams_));
1191                         key_only_consumer_->initialize(format_desc, channel_layout);
1192                 }
1193         }
1194
1195         int64_t presentation_frame_age_millis() const override
1196         {
1197                 return consumer_ ? static_cast<int64_t>(consumer_->presentation_frame_age_millis()) : 0;
1198         }
1199
1200         std::future<bool> send(core::const_frame frame) override
1201         {
1202                 bool ready_for_frame = consumer_->ready_for_frame();
1203
1204                 if (ready_for_frame && separate_key_)
1205                         ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
1206
1207                 if (ready_for_frame)
1208                 {
1209                         consumer_->send(frame);
1210
1211                         if (separate_key_)
1212                                 key_only_consumer_->send(frame.key_only());
1213                 }
1214                 else
1215                 {
1216                         consumer_->mark_dropped();
1217
1218                         if (separate_key_)
1219                                 key_only_consumer_->mark_dropped();
1220                 }
1221
1222                 return make_ready_future(true);
1223         }
1224
1225         std::wstring print() const override
1226         {
1227                 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
1228         }
1229
1230         std::wstring name() const override
1231         {
1232                 return L"ffmpeg";
1233         }
1234
1235         boost::property_tree::wptree info() const override
1236         {
1237                 boost::property_tree::wptree info;
1238
1239                 info.add(L"type",                       L"ffmpeg");
1240                 info.add(L"path",                       u16(path_));
1241                 info.add(L"separate_key",       separate_key_);
1242                 info.add(L"mono_streams",       mono_streams_);
1243
1244                 return info;
1245         }
1246
1247         bool has_synchronization_clock() const override
1248         {
1249                 return false;
1250         }
1251
1252         int buffer_depth() const override
1253         {
1254                 return -1;
1255         }
1256
1257         int index() const override
1258         {
1259                 return compatibility_mode_ ? 200 : 100000 + consumer_index_offset_;
1260         }
1261
1262         core::monitor::subject& monitor_output() override
1263         {
1264                 return consumer_->monitor_output();
1265         }
1266 };
1267
1268 }
1269
1270 void describe_streaming_consumer(core::help_sink& sink, const core::help_repository& repo)
1271 {
1272         sink.short_description(L"For streaming/recording the contents of a channel using FFmpeg.");
1273         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]}");
1274         sink.para()->text(L"For recording or streaming the contents of a channel using FFmpeg");
1275         sink.definitions()
1276                 ->item(L"filename",                     L"The filename under the media folder including the extension (decides which kind of container format that will be used).")
1277                 ->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.")
1278                 ->item(L"ffmpeg_paramX",                L"A parameter supported by FFmpeg. For example vcodec or acodec etc.")
1279                 ->item(L"separate_key",         L"If defined will create two files simultaneously -- One for fill and one for key (_A will be appended).")
1280                 ->item(L"mono_streams",         L"If defined every audio channel will be written to its own audio stream.");
1281         sink.para()->text(L"Examples:");
1282         sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd");
1283         sink.example(L">> ADD 1 FILE output.mov -vcodec prores");
1284         sink.example(L">> ADD 1 FILE output.mov -vcodec dvvideo");
1285         sink.example(L">> ADD 1 FILE output.mov -vcodec libx264 -preset ultrafast -tune fastdecode -crf 25");
1286         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");
1287         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.");
1288         sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast",
1289                 L"for streaming over UDP instead of creating a local file.");
1290 }
1291
1292 spl::shared_ptr<core::frame_consumer> create_streaming_consumer(
1293                 const std::vector<std::wstring>& params, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1294 {
1295         if (params.size() < 1 || (!boost::iequals(params.at(0), L"STREAM") && !boost::iequals(params.at(0), L"FILE")))
1296                 return core::frame_consumer::empty();
1297
1298         auto params2                    = params;
1299         bool separate_key               = get_and_consume_flag(L"SEPARATE_KEY", params2);
1300         bool mono_streams               = get_and_consume_flag(L"MONO_STREAMS", params2);
1301         auto compatibility_mode = boost::iequals(params.at(0), L"FILE");
1302         auto path                               = u8(params2.size() > 1 ? params2.at(1) : L"");
1303         auto args                               = u8(boost::join(params2, L" "));
1304
1305         return spl::make_shared<ffmpeg_consumer_proxy>(path, args, separate_key, mono_streams, compatibility_mode);
1306 }
1307
1308 spl::shared_ptr<core::frame_consumer> create_preconfigured_streaming_consumer(
1309                 const boost::property_tree::wptree& ptree, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1310 {
1311         return spl::make_shared<ffmpeg_consumer_proxy>(
1312                         u8(ptree_get<std::wstring>(ptree, L"path")),
1313                         u8(ptree.get<std::wstring>(L"args", L"")),
1314                         ptree.get<bool>(L"separate-key", false),
1315                         ptree.get<bool>(L"mono-streams", false),
1316                         false);
1317 }
1318
1319 }}