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