]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/consumer/ffmpeg_consumer.cpp
fb2045b6f5cd65f3334e648b53dfae171c96a1f1
[casparcg] / modules / ffmpeg / consumer / ffmpeg_consumer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21  
22 #include "../StdAfx.h"
23
24 #include "../ffmpeg_error.h"
25
26 #include "ffmpeg_consumer.h"
27
28 #include "../producer/tbb_avcodec.h"
29
30 #include <core/frame/frame.h>
31 #include <core/frame/audio_channel_layout.h>
32 #include <core/mixer/audio/audio_util.h>
33 #include <core/consumer/frame_consumer.h>
34 #include <core/video_format.h>
35 #include <core/help/help_repository.h>
36 #include <core/help/help_sink.h>
37
38 #include <common/array.h>
39 #include <common/env.h>
40 #include <common/except.h>
41 #include <common/executor.h>
42 #include <common/future.h>
43 #include <common/diagnostics/graph.h>
44 #include <common/lock.h>
45 #include <common/memory.h>
46 #include <common/param.h>
47 #include <common/utf.h>
48 #include <common/assert.h>
49 #include <common/memshfl.h>
50 #include <common/timer.h>
51 #include <common/ptree.h>
52
53 #include <boost/algorithm/string.hpp>
54 #include <boost/property_tree/ptree.hpp>
55 #include <boost/filesystem.hpp>
56 #include <boost/range/algorithm.hpp>
57 #include <boost/range/algorithm_ext.hpp>
58 #include <boost/lexical_cast.hpp>
59
60 #include <tbb/spin_mutex.h>
61
62 #include <numeric>
63 #include <cstring>
64
65 #if defined(_MSC_VER)
66 #pragma warning (push)
67 #pragma warning (disable : 4244)
68 #endif
69 extern "C" 
70 {
71         #define __STDC_CONSTANT_MACROS
72         #define __STDC_LIMIT_MACROS
73         #include <libavformat/avformat.h>
74         #include <libswscale/swscale.h>
75         #include <libavutil/opt.h>
76         #include <libavutil/pixdesc.h>
77         #include <libavutil/parseutils.h>
78         #include <libavutil/samplefmt.h>
79         #include <libswresample/swresample.h>
80 }
81 #if defined(_MSC_VER)
82 #pragma warning (pop)
83 #endif
84
85 namespace caspar { namespace ffmpeg {
86         
87 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
88 {
89         AVClass* av_class = *(AVClass**)obj;
90
91         if((strcmp(name, "pix_fmt") == 0 || strcmp(name, "pixel_format") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
92         {
93                 AVCodecContext* c = (AVCodecContext*)obj;               
94                 auto pix_fmt = av_get_pix_fmt(val);
95                 if(pix_fmt == PIX_FMT_NONE)
96                         return -1;              
97                 c->pix_fmt = pix_fmt;
98                 return 0;
99         }
100         //if((strcmp(name, "r") == 0 || strcmp(name, "frame_rate") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
101         //{
102         //      AVCodecContext* c = (AVCodecContext*)obj;       
103
104         //      if(c->codec_type != AVMEDIA_TYPE_VIDEO)
105         //              return -1;
106
107         //      AVRational rate;
108         //      int ret = av_parse_video_rate(&rate, val);
109         //      if(ret < 0)
110         //              return ret;
111
112         //      c->time_base.num = rate.den;
113         //      c->time_base.den = rate.num;
114         //      return 0;
115         //}
116
117         return ::av_opt_set(obj, name, val, search_flags);
118 }
119
120 struct option
121 {
122         std::string name;
123         std::string value;
124
125         option(std::string name, std::string value)
126                 : name(std::move(name))
127                 , value(std::move(value))
128         {
129         }
130 };
131         
132 struct output_format
133 {
134         AVOutputFormat* format;
135         int                             width;
136         int                             height;
137         AVCodecID               vcodec;
138         AVCodecID               acodec;
139         int                             croptop;
140         int                             cropbot;
141
142         output_format(const core::video_format_desc& format_desc, const std::string& filename, std::vector<option>& options)
143                 : format(av_guess_format(nullptr, filename.c_str(), nullptr))
144                 , width(format_desc.width)
145                 , height(format_desc.height)
146                 , vcodec(CODEC_ID_NONE)
147                 , acodec(CODEC_ID_NONE)
148                 , croptop(0)
149                 , cropbot(0)
150         {
151                 if(boost::iequals(boost::filesystem::path(filename).extension().string(), ".dv"))
152                         set_opt("f", "dv");
153
154                 boost::range::remove_erase_if(options, [&](const option& o)
155                 {
156                         return set_opt(o.name, o.value);
157                 });
158                 
159                 if(vcodec == CODEC_ID_NONE && format)
160                         vcodec = format->video_codec;
161
162                 if(acodec == CODEC_ID_NONE && format)
163                         acodec = format->audio_codec;
164                 
165                 if(vcodec == CODEC_ID_NONE)
166                         vcodec = CODEC_ID_H264;
167                 
168                 if(acodec == CODEC_ID_NONE)
169                         acodec = CODEC_ID_PCM_S16LE;
170         }
171         
172         bool set_opt(const std::string& name, const std::string& value)
173         {
174                 //if(name == "target")
175                 //{ 
176                 //      enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
177                 //      
178                 //      if(name.find("pal-") != std::string::npos)
179                 //              norm = PAL;
180                 //      else if(name.find("ntsc-") != std::string::npos)
181                 //              norm = NTSC;
182
183                 //      if(norm == UNKNOWN)
184                 //              CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("target"));
185                 //      
186                 //      if (name.find("-dv") != std::string::npos) 
187                 //      {
188                 //              set_opt("f", "dv");
189                 //              if(norm == PAL)
190                 //              {
191                 //                      set_opt("s", "720x576");
192                 //              }
193                 //              else
194                 //              {
195                 //                      set_opt("s", "720x480");
196                 //                      if(height == 486)
197                 //                      {
198                 //                              set_opt("croptop", "2");
199                 //                              set_opt("cropbot", "4");
200                 //                      }
201                 //              }
202                 //              set_opt("s", norm == PAL ? "720x576" : "720x480");
203                 //      } 
204
205                 //      return true;
206                 //}
207                 //else 
208                 if(name == "f")
209                 {
210                         format = av_guess_format(value.c_str(), nullptr, nullptr);
211
212                         if(format == nullptr)
213                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown format " + value));
214
215                         return true;
216                 }
217                 else if(name == "vcodec" || name == "v:codec")
218                 {
219                         auto c = avcodec_find_encoder_by_name(value.c_str());
220                         if(c == nullptr)
221                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown video codec " + value));
222
223                         vcodec = avcodec_find_encoder_by_name(value.c_str())->id;
224                         return true;
225
226                 }
227                 else if(name == "acodec" || name == "a:codec")
228                 {
229                         auto c = avcodec_find_encoder_by_name(value.c_str());
230                         if(c == nullptr)
231                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown audio codec " + value));
232
233                         acodec = avcodec_find_encoder_by_name(value.c_str())->id;
234
235                         return true;
236                 }
237                 else if(name == "s")
238                 {
239                         if(av_parse_video_size(&width, &height, value.c_str()) < 0)
240                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown video size " + value));
241                         
242                         return true;
243                 }
244                 else if(name == "croptop")
245                 {
246                         croptop = boost::lexical_cast<int>(value);
247
248                         return true;
249                 }
250                 else if(name == "cropbot")
251                 {
252                         cropbot = boost::lexical_cast<int>(value);
253
254                         return true;
255                 }
256                 
257                 return false;
258         }
259 };
260
261 typedef cache_aligned_vector<uint8_t> byte_vector;
262
263 struct ffmpeg_consumer : boost::noncopyable
264 {               
265         const spl::shared_ptr<diagnostics::graph>       graph_;
266         const std::string                                                       filename_;
267         const std::string                                                       full_filename_          = u8(env::media_folder()) + filename_;
268         const std::shared_ptr<AVFormatContext>          oc_                                     { avformat_alloc_context(), avformat_free_context };
269         const core::video_format_desc                           format_desc_;
270         const core::audio_channel_layout                        channel_layout_;
271
272         core::monitor::subject                                          monitor_subject_;
273         
274         tbb::spin_mutex                                                         exception_mutex_;
275         std::exception_ptr                                                      exception_;
276         
277         std::shared_ptr<AVStream>                                       audio_st_;
278         std::shared_ptr<AVStream>                                       video_st_;
279         
280         byte_vector                                                                     picture_buffer_;
281         byte_vector                                                                     key_picture_buf_;
282         byte_vector                                                                     audio_buffer_;
283         std::shared_ptr<SwrContext>                                     swr_;
284         std::shared_ptr<SwsContext>                                     sws_;
285
286         int64_t                                                                         frame_number_           = 0;
287
288         output_format                                                           output_format_;
289         bool                                                                            key_only_;
290         tbb::atomic<int64_t>                                            current_encoding_delay_;
291
292         executor                                                                        executor_;
293 public:
294         ffmpeg_consumer(
295                         const std::string& filename,
296                         const core::video_format_desc& format_desc,
297                         const core::audio_channel_layout& channel_layout,
298                         std::vector<option> options,
299                         bool key_only)
300                 : filename_(filename)
301                 , format_desc_(format_desc)
302                 , channel_layout_(channel_layout)
303                 , output_format_(format_desc, full_filename_, options)
304                 , key_only_(key_only)
305                 , executor_(print())
306         {
307                 current_encoding_delay_ = 0;
308                 check_space();
309
310                 // TODO: Ask stakeholders about case where file already exists.
311                 boost::filesystem::remove(boost::filesystem::path(full_filename_)); // Delete the file if it exists
312
313                 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
314                 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
315                 graph_->set_text(print());
316                 diagnostics::register_graph(graph_);
317
318                 executor_.set_capacity(8);
319
320                 oc_->oformat = output_format_.format;
321                                 
322                 std::strcpy(oc_->filename, full_filename_.c_str());
323                 
324                 //  Add the audio and video streams using the default format codecs     and initialize the codecs.
325                 video_st_ = add_video_stream(options);
326
327                 if (!key_only)
328                         audio_st_ = add_audio_stream(options);
329                                 
330                 av_dump_format(oc_.get(), 0, full_filename_.c_str(), 1);
331                  
332                 // Open the output ffmpeg, if needed.
333                 if (!(oc_->oformat->flags & AVFMT_NOFILE)) 
334                         THROW_ON_ERROR2(avio_open(&oc_->pb, full_filename_.c_str(), AVIO_FLAG_WRITE), "[ffmpeg_consumer]");
335                                 
336                 THROW_ON_ERROR2(avformat_write_header(oc_.get(), nullptr), "[ffmpeg_consumer]");
337
338                 if(options.size() > 0)
339                 {
340                         for (auto& option : options)
341                                 CASPAR_LOG(warning) << L"Invalid option: -" << u16(option.name) << L" " << u16(option.value);
342                 }
343         }
344
345         ~ffmpeg_consumer()
346         {    
347                 try
348                 {
349                         executor_.wait();
350                 }
351                 catch(...)
352                 {
353                         CASPAR_LOG_CURRENT_EXCEPTION();
354                 }
355
356                 LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");
357                 
358                 if (!key_only_)
359                         audio_st_.reset();
360
361                 video_st_.reset();
362                           
363                 if (!(oc_->oformat->flags & AVFMT_NOFILE)) 
364                         LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]");
365         }
366         
367         // frame_consumer
368
369         void send(core::const_frame& frame)
370         {
371                 auto exception = lock(exception_mutex_, [&]
372                 {
373                         return exception_;
374                 });
375
376                 if(exception != nullptr)
377                         std::rethrow_exception(exception);
378
379                 executor_.begin_invoke([=]
380                 {               
381                         encode(frame);
382                         current_encoding_delay_ = frame.get_age_millis();
383                 });
384         }
385
386         bool ready_for_frame() const
387         {
388                 return !executor_.is_full();
389         }
390
391         void mark_dropped()
392         {
393                 graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
394         }
395
396         std::wstring print() const
397         {
398                 return L"ffmpeg[" + u16(filename_) + L"]";
399         }
400         
401         core::monitor::subject& monitor_output()
402         {
403                 return monitor_subject_;
404         }
405
406 private:
407         std::shared_ptr<AVStream> add_video_stream(std::vector<option>& options)
408         { 
409                 if(output_format_.vcodec == CODEC_ID_NONE)
410                         return nullptr;
411
412                 auto st = avformat_new_stream(oc_.get(), 0);
413                 if (!st)                
414                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));             
415
416                 auto encoder = avcodec_find_encoder(output_format_.vcodec);
417                 if (!encoder)
418                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Codec not found."));
419
420                 auto c = st->codec;
421
422                 avcodec_get_context_defaults3(c, encoder);
423                                 
424                 c->codec_id                     = output_format_.vcodec;
425                 c->codec_type           = AVMEDIA_TYPE_VIDEO;
426                 c->width                        = output_format_.width;
427                 c->height                       = output_format_.height - output_format_.croptop - output_format_.cropbot;
428                 c->time_base.den        = format_desc_.time_scale;
429                 c->time_base.num        = format_desc_.duration;
430                 c->gop_size                     = 25;
431                 c->flags                   |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);
432                 c->pix_fmt                      = c->pix_fmt != PIX_FMT_NONE ? c->pix_fmt : PIX_FMT_YUV420P;
433
434                 if(c->codec_id == CODEC_ID_PRORES)
435                 {                       
436                         c->bit_rate     = output_format_.width < 1280 ? 63*1000000 : 220*1000000;
437                         c->pix_fmt      = PIX_FMT_YUV422P10;
438                 }
439                 else if(c->codec_id == CODEC_ID_DNXHD)
440                 {
441                         if(c->width < 1280 || c->height < 720)
442                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Unsupported video dimensions."));
443
444                         c->bit_rate     = 220*1000000;
445                         c->pix_fmt      = PIX_FMT_YUV422P;
446                 }
447                 else if(c->codec_id == CODEC_ID_DVVIDEO)
448                 {
449                         c->width = c->height == 1280 ? 960  : c->width;
450                         
451                         if(format_desc_.format == core::video_format::ntsc)
452                         {
453                                 c->pix_fmt = PIX_FMT_YUV411P;
454                                 output_format_.croptop = 2;
455                                 output_format_.cropbot = 4;
456                                 c->height                          = output_format_.height - output_format_.croptop - output_format_.cropbot;
457                         }
458                         else if(format_desc_.format == core::video_format::pal)
459                                 c->pix_fmt = PIX_FMT_YUV420P;
460                         else // dv50
461                                 c->pix_fmt = PIX_FMT_YUV422P;
462                         
463                         if(format_desc_.duration == 1001)                       
464                                 c->width = c->height == 1080 ? 1280 : c->width;                 
465                         else
466                                 c->width = c->height == 1080 ? 1440 : c->width;                 
467                 }
468                 else if(c->codec_id == CODEC_ID_H264)
469                 {                          
470                         c->pix_fmt = PIX_FMT_YUV420P;    
471                         av_opt_set(c->priv_data, "preset", "ultrafast", 0);
472                         av_opt_set(c->priv_data, "tune",   "fastdecode",   0);
473                         av_opt_set(c->priv_data, "crf",    "5",     0);
474                 }
475                 else if(c->codec_id == CODEC_ID_QTRLE)
476                 {
477                         c->pix_fmt = PIX_FMT_ARGB;
478                 }
479                                                                 
480                 boost::range::remove_erase_if(options, [&](const option& o)
481                 {
482                         return o.name.at(0) != 'a' && ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
483                 });
484                                 
485                 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
486                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
487                 
488                 THROW_ON_ERROR2(tbb_avcodec_open(c, encoder, false), "[ffmpeg_consumer]");
489
490                 return std::shared_ptr<AVStream>(st, [](AVStream* st)
491                 {
492                         LOG_ON_ERROR2(tbb_avcodec_close(st->codec), "[ffmpeg_consumer]");
493                 });
494         }
495                 
496         std::shared_ptr<AVStream> add_audio_stream(std::vector<option>& options)
497         {
498                 if(output_format_.acodec == CODEC_ID_NONE)
499                         return nullptr;
500
501                 auto st = avformat_new_stream(oc_.get(), nullptr);
502                 if(!st)
503                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));              
504                 
505                 auto encoder = avcodec_find_encoder(output_format_.acodec);
506                 if (!encoder)
507                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));
508                 
509                 auto c = st->codec;
510
511                 avcodec_get_context_defaults3(c, encoder);
512
513                 c->codec_id                     = output_format_.acodec;
514                 c->codec_type           = AVMEDIA_TYPE_AUDIO;
515                 c->sample_rate          = 48000;
516                 c->channels                     = 2;
517                 c->sample_fmt           = AV_SAMPLE_FMT_S16;
518                 c->time_base.num        = 1;
519                 c->time_base.den        = c->sample_rate;
520
521                 if(output_format_.vcodec == CODEC_ID_FLV1)              
522                         c->sample_rate  = 44100;                
523
524                 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
525                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
526                                 
527                 boost::range::remove_erase_if(options, [&](const option& o)
528                 {
529                         return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
530                 });
531
532                 THROW_ON_ERROR2(avcodec_open2(c, encoder, nullptr), "[ffmpeg_consumer]");
533
534                 return std::shared_ptr<AVStream>(st, [](AVStream* st)
535                 {
536                         LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");
537                 });
538         }
539   
540         void encode_video_frame(core::const_frame frame)
541         { 
542                 if(!video_st_)
543                         return;
544                 
545                 auto enc = video_st_->codec;
546          
547                 auto av_frame                           = convert_video(frame, enc);
548                 av_frame->interlaced_frame      = format_desc_.field_mode != core::field_mode::progressive;
549                 av_frame->top_field_first       = format_desc_.field_mode == core::field_mode::upper;
550                 av_frame->pts = frame_number_++;
551
552                 monitor_subject_
553                         << core::monitor::message("/frame") % static_cast<int64_t>(frame_number_)
554                         << core::monitor::message("/path") % filename_
555                         << core::monitor::message("/fps") % format_desc_.fps;
556
557                 AVPacket pkt;
558                 av_init_packet(&pkt);
559                 pkt.data = nullptr;
560                 pkt.size = 0;
561
562                 int got_packet = 0;
563                 THROW_ON_ERROR2(avcodec_encode_video2(enc, &pkt, av_frame.get(), &got_packet), "[ffmpeg_consumer]");
564                 std::shared_ptr<AVPacket> guard(&pkt, av_free_packet);
565
566                 if(!got_packet)
567                         return;
568                  
569                 if (pkt.pts != AV_NOPTS_VALUE)
570                         pkt.pts = av_rescale_q(pkt.pts, enc->time_base, video_st_->time_base);
571                 if (pkt.dts != AV_NOPTS_VALUE)
572                         pkt.dts = av_rescale_q(pkt.dts, enc->time_base, video_st_->time_base);
573                  
574                 pkt.stream_index = video_st_->index;
575                         
576                 THROW_ON_ERROR2(av_interleaved_write_frame(oc_.get(), &pkt), "[ffmpeg_consumer]");
577         }
578                 
579         uint64_t get_channel_layout(AVCodecContext* dec)
580         {
581                 auto layout = (dec->channel_layout && dec->channels == av_get_channel_layout_nb_channels(dec->channel_layout)) ? dec->channel_layout : av_get_default_channel_layout(dec->channels);
582                 return layout;
583         }
584                 
585         void encode_audio_frame(core::const_frame frame)
586         {               
587                 if(!audio_st_)
588                         return;
589                 
590                 auto enc = audio_st_->codec;
591
592                 boost::push_back(audio_buffer_, convert_audio(frame, enc));
593                         
594                 auto frame_size = enc->frame_size != 0 ? enc->frame_size * enc->channels * av_get_bytes_per_sample(enc->sample_fmt) : static_cast<int>(audio_buffer_.size());
595                         
596                 while(audio_buffer_.size() >= frame_size)
597                 {                       
598                         std::shared_ptr<AVFrame> av_frame(av_frame_alloc(), [=](AVFrame* p) { av_frame_free(&p); });
599                         avcodec_get_frame_defaults(av_frame.get());             
600                         av_frame->nb_samples = frame_size / (enc->channels * av_get_bytes_per_sample(enc->sample_fmt));
601
602                         AVPacket pkt;
603                         av_init_packet(&pkt);
604                         pkt.data = nullptr;
605                         pkt.size = 0;                           
606                         
607                         THROW_ON_ERROR2(avcodec_fill_audio_frame(av_frame.get(), enc->channels, enc->sample_fmt, audio_buffer_.data(), frame_size, 1), "[ffmpeg_consumer]");
608
609                         int got_packet = 0;
610                         THROW_ON_ERROR2(avcodec_encode_audio2(enc, &pkt, av_frame.get(), &got_packet), "[ffmpeg_consumer]");
611                         std::shared_ptr<AVPacket> guard(&pkt, av_free_packet);
612                                 
613                         audio_buffer_.erase(audio_buffer_.begin(), audio_buffer_.begin() + frame_size);
614
615                         if(!got_packet)
616                                 return;
617                 
618                         if (pkt.pts != AV_NOPTS_VALUE)
619                                 pkt.pts      = av_rescale_q(pkt.pts, enc->time_base, audio_st_->time_base);
620                         if (pkt.dts != AV_NOPTS_VALUE)
621                                 pkt.dts      = av_rescale_q(pkt.dts, enc->time_base, audio_st_->time_base);
622                         if (pkt.duration > 0)
623                                 pkt.duration = static_cast<int>(av_rescale_q(pkt.duration, enc->time_base, audio_st_->time_base));
624                 
625                         pkt.stream_index = audio_st_->index;
626                                                 
627                         THROW_ON_ERROR2(av_interleaved_write_frame(oc_.get(), &pkt), "[ffmpeg_consumer]");
628                 }
629         }                
630         
631         std::shared_ptr<AVFrame> convert_video(core::const_frame frame, AVCodecContext* c)
632         {
633                 if(!sws_) 
634                 {
635                         sws_.reset(sws_getContext(format_desc_.width, 
636                                                                           format_desc_.height - output_format_.croptop  - output_format_.cropbot, 
637                                                                           PIX_FMT_BGRA,
638                                                                           c->width,
639                                                                           c->height, 
640                                                                           c->pix_fmt, 
641                                                                           SWS_BICUBIC, nullptr, nullptr, nullptr), 
642                                                 sws_freeContext);
643                         if (sws_ == nullptr) 
644                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));
645                 }
646
647                 // #in_frame
648
649                 std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);
650
651                 auto in_picture = reinterpret_cast<AVPicture*>(in_frame.get());
652                 
653                 if (key_only_)
654                 {
655                         key_picture_buf_.resize(frame.image_data().size());
656                         in_picture->linesize[0] = format_desc_.width * 4;
657                         in_picture->data[0] = key_picture_buf_.data();
658
659                         aligned_memshfl(in_picture->data[0], frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
660                 }
661                 else
662                 {
663                         avpicture_fill(
664                                         in_picture,
665                                         const_cast<uint8_t*>(frame.image_data().begin()),
666                                         PIX_FMT_BGRA,
667                                         format_desc_.width,
668                                         format_desc_.height - output_format_.croptop  - output_format_.cropbot);
669                 }
670
671                 // crop-top
672
673                 for(int n = 0; n < 4; ++n)              
674                         in_frame->data[n] += in_frame->linesize[n] * output_format_.croptop;            
675                 
676                 // #out_frame
677
678                 std::shared_ptr<AVFrame> out_frame(avcodec_alloc_frame(), av_free);
679                 
680                 av_image_fill_linesizes(out_frame->linesize, c->pix_fmt, c->width);
681                 for(int n = 0; n < 4; ++n)
682                         out_frame->linesize[n] += 32 - (out_frame->linesize[n] % 32); // align
683
684                 picture_buffer_.resize(av_image_fill_pointers(out_frame->data, c->pix_fmt, c->height, nullptr, out_frame->linesize));
685                 av_image_fill_pointers(out_frame->data, c->pix_fmt, c->height, picture_buffer_.data(), out_frame->linesize);
686                 
687                 // #scale
688
689                 sws_scale(sws_.get(), 
690                                   in_frame->data, 
691                                   in_frame->linesize,
692                                   0, 
693                                   format_desc_.height - output_format_.cropbot - output_format_.croptop, 
694                                   out_frame->data, 
695                                   out_frame->linesize);
696
697                 out_frame->format       = c->pix_fmt;
698                 out_frame->width        = c->width;
699                 out_frame->height       = c->height;
700
701                 return out_frame;
702         }
703         
704         byte_vector convert_audio(core::const_frame& frame, AVCodecContext* c)
705         {
706                 if(!swr_) 
707                 {
708                         swr_ = std::shared_ptr<SwrContext>(swr_alloc_set_opts(nullptr,
709                                                                                 get_channel_layout(c), c->sample_fmt, c->sample_rate,
710                                                                                 av_get_default_channel_layout(channel_layout_.num_channels), AV_SAMPLE_FMT_S32, format_desc_.audio_sample_rate,
711                                                                                 0, nullptr), [](SwrContext* p){swr_free(&p);});
712
713                         if(!swr_)
714                                 CASPAR_THROW_EXCEPTION(bad_alloc());
715
716                         THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
717                 }
718                                 
719                 byte_vector buffer(48000);
720
721                 const uint8_t* in[]  = {reinterpret_cast<const uint8_t*>(frame.audio_data().data())};
722                 uint8_t*       out[] = {buffer.data()};
723
724                 auto channel_samples = swr_convert(swr_.get(), 
725                                                                                    out, static_cast<int>(buffer.size()) / c->channels / av_get_bytes_per_sample(c->sample_fmt), 
726                                                                                    in, static_cast<int>(frame.audio_data().size()/channel_layout_.num_channels));
727
728                 buffer.resize(channel_samples * c->channels * av_get_bytes_per_sample(c->sample_fmt));  
729
730                 return buffer;
731         }
732
733         void check_space()
734         {
735                 auto space = boost::filesystem::space(boost::filesystem::path(full_filename_).parent_path());
736                 if(space.available < 512*1000000)
737                         CASPAR_THROW_EXCEPTION(file_write_error() << msg_info("out of space"));
738         }
739
740         void encode(const core::const_frame& frame)
741         {
742                 try
743                 {
744                         if(frame_number_ % 25 == 0)
745                                 check_space();
746
747                         caspar::timer frame_timer;
748
749                         encode_video_frame(frame);
750                         encode_audio_frame(frame);
751
752                         graph_->set_value("frame-time", frame_timer.elapsed()*format_desc_.fps*0.5);
753                 }
754                 catch(...)
755                 {                       
756                         lock(exception_mutex_, [&]
757                         {
758                                 exception_ = std::current_exception();
759                         });
760                 }
761         }
762 };
763
764 struct ffmpeg_consumer_proxy : public core::frame_consumer
765 {
766         const std::wstring                      filename_;
767         const std::vector<option>       options_;
768         const bool                                      separate_key_;
769
770         std::unique_ptr<ffmpeg_consumer> consumer_;
771         std::unique_ptr<ffmpeg_consumer> key_only_consumer_;
772
773 public:
774
775         ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options, bool separate_key)
776                 : filename_(filename)
777                 , options_(options)
778                 , separate_key_(separate_key)
779         {
780         }
781         
782         void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int) override
783         {
784                 if(consumer_)
785                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
786
787                 consumer_.reset(new ffmpeg_consumer(u8(filename_), format_desc, channel_layout, options_, false));
788
789                 if (separate_key_)
790                 {
791                         boost::filesystem::path fill_file(filename_);
792                         auto without_extension = u16(fill_file.stem().string());
793                         auto key_file = without_extension + L"_A" + u16(fill_file.extension().string());
794
795                         key_only_consumer_.reset(new ffmpeg_consumer(u8(key_file), format_desc, channel_layout, options_, true));
796                 }
797         }
798
799         int64_t presentation_frame_age_millis() const override
800         {
801                 return consumer_ ? static_cast<int64_t>(consumer_->current_encoding_delay_) : 0;
802         }
803
804         std::future<bool> send(core::const_frame frame) override
805         {
806                 bool ready_for_frame = consumer_->ready_for_frame();
807                 
808                 if (ready_for_frame && separate_key_)
809                         ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
810
811                 if (ready_for_frame)
812                 {
813                         consumer_->send(frame);
814                         
815                         if (separate_key_)
816                                 key_only_consumer_->send(frame);
817                 }
818                 else
819                 {
820                         consumer_->mark_dropped();
821                         
822                         if (separate_key_)
823                                 key_only_consumer_->mark_dropped();
824                 }
825                 
826                 return make_ready_future(true);
827         }
828         
829         std::wstring print() const override
830         {
831                 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
832         }
833
834         std::wstring name() const override
835         {
836                 return L"file";
837         }
838
839         boost::property_tree::wptree info() const override
840         {
841                 boost::property_tree::wptree info;
842                 info.add(L"type", L"file");
843                 info.add(L"filename", filename_);
844                 info.add(L"separate_key", separate_key_);
845                 return info;
846         }
847                 
848         bool has_synchronization_clock() const override
849         {
850                 return false;
851         }
852
853         int buffer_depth() const override
854         {
855                 return -1;
856         }
857
858         int index() const override
859         {
860                 return 200;
861         }
862
863         core::monitor::subject& monitor_output()
864         {
865                 return consumer_->monitor_output();
866         }
867 };
868
869 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
870 {
871         sink.short_description(L"Can record a channel to a file supported by FFmpeg.");
872         sink.syntax(L"FILE [filename:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}} {[separate_key:SEPARATE_KEY]}");
873         sink.para()->text(L"Can record a channel to a file supported by FFmpeg.");
874         sink.definitions()
875                 ->item(L"filename", L"The filename under the media folder including the extension (decides which kind of container format that will be used).")
876                 ->item(L"ffmpeg_paramX", L"A parameter supported by FFmpeg. For example vcodec or acodec etc.")
877                 ->item(L"separate_key", L"If defined will create two files simultaneously -- One for fill and one for key (_A will be appended).")
878                 ;
879         sink.para()->text(L"Examples:");
880         sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd");
881         sink.example(L">> ADD 1 FILE output.mov -vcodec prores");
882         sink.example(L">> ADD 1 FILE output.mov -vcodec dvvideo");
883         sink.example(L">> ADD 1 FILE output.mov -vcodec libx264 -preset ultrafast -tune fastdecode -crf 25");
884         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");
885 }
886
887 spl::shared_ptr<core::frame_consumer> create_consumer(
888                 const std::vector<std::wstring>& params, core::interaction_sink*)
889 {
890         auto params2 = params;
891         auto separate_key_it = std::find_if(params2.begin(), params2.end(), param_comparer(L"SEPARATE_KEY"));
892         bool separate_key = false;
893
894         if (separate_key_it != params2.end())
895         {
896                 separate_key = true;
897                 params2.erase(separate_key_it);
898         }
899
900         auto str = std::accumulate(params2.begin(), params2.end(), std::wstring(), [](const std::wstring& lhs, const std::wstring& rhs) {return lhs + L" " + rhs;});
901         
902         boost::wregex path_exp(LR"(\s*FILE(\s(?<PATH>.+\.[^\s]+))?.*)", boost::regex::icase);
903
904         boost::wsmatch path;
905         if(!boost::regex_match(str, path, path_exp))
906                 return core::frame_consumer::empty();
907         
908         boost::wregex opt_exp(LR"(-((?<NAME>[^\s]+)\s+(?<VALUE>[^\s]+)))");     
909         
910         std::vector<option> options;
911         for(boost::wsregex_iterator it(str.begin(), str.end(), opt_exp); it != boost::wsregex_iterator(); ++it)
912         {
913                 auto name  = u8(boost::trim_copy(boost::to_lower_copy((*it)["NAME"].str())));
914                 auto value = u8(boost::trim_copy(boost::to_lower_copy((*it)["VALUE"].str())));
915                 
916                 if(value == "h264")
917                         value = "libx264";
918                 else if(value == "dvcpro")
919                         value = "dvvideo";
920
921                 options.push_back(option(name, value));
922         }
923                                 
924         return spl::make_shared<ffmpeg_consumer_proxy>(path["PATH"].str(), options, separate_key);
925 }
926
927 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
928                 const boost::property_tree::wptree& ptree, core::interaction_sink*)
929 {
930         auto filename           = ptree_get<std::wstring>(ptree, L"path");
931         auto codec                      = ptree.get(L"vcodec", L"libx264");
932         auto separate_key       = ptree.get(L"separate-key", false);
933
934         std::vector<option> options;
935         options.push_back(option("vcodec", u8(codec)));
936         
937         return spl::make_shared<ffmpeg_consumer_proxy>(filename, options, separate_key);
938 }
939
940 }}