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