]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/consumer/ffmpeg_consumer.cpp
ffmpeg: Bug fixes.
[casparcg] / modules / ffmpeg / consumer / ffmpeg_consumer.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21  \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "../ffmpeg_error.h"\r
25 \r
26 #include "ffmpeg_consumer.h"\r
27 \r
28 #include "../ffmpeg_params.h"\r
29 #include "../producer/audio/audio_resampler.h"\r
30 \r
31 #include <core/parameters/parameters.h>\r
32 #include <core/mixer/read_frame.h>\r
33 #include <core/mixer/audio/audio_util.h>\r
34 #include <core/consumer/frame_consumer.h>\r
35 #include <core/video_format.h>\r
36 \r
37 #include <common/concurrency/executor.h>\r
38 #include <common/concurrency/future_util.h>\r
39 #include <common/diagnostics/graph.h>\r
40 #include <common/env.h>\r
41 #include <common/utility/string.h>\r
42 #include <common/memory/memshfl.h>\r
43 \r
44 #include <boost/algorithm/string.hpp>\r
45 #include <boost/timer.hpp>\r
46 #include <boost/property_tree/ptree.hpp>\r
47 \r
48 #include <tbb/cache_aligned_allocator.h>\r
49 #include <tbb/parallel_invoke.h>\r
50 #include <tbb/atomic.h>\r
51 \r
52 #include <boost/range/algorithm.hpp>\r
53 #include <boost/range/algorithm_ext.hpp>\r
54 #include <boost/lexical_cast.hpp>\r
55 \r
56 #include <string>\r
57 \r
58 #if defined(_MSC_VER)\r
59 #pragma warning (push)\r
60 #pragma warning (disable : 4244)\r
61 #endif\r
62 extern "C" \r
63 {\r
64         #define __STDC_CONSTANT_MACROS\r
65         #define __STDC_LIMIT_MACROS\r
66         #include <libavformat/avformat.h>\r
67         #include <libswscale/swscale.h>\r
68         #include <libavutil/opt.h>\r
69         #include <libavutil/pixdesc.h>\r
70         #include <libavutil/parseutils.h>\r
71 }\r
72 #if defined(_MSC_VER)\r
73 #pragma warning (pop)\r
74 #endif\r
75 \r
76 namespace caspar { namespace ffmpeg {\r
77         \r
78 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)\r
79 {\r
80         AVClass* av_class = *(AVClass**)obj;\r
81 \r
82         if((strcmp(name, "pix_fmt") == 0 || strcmp(name, "pixel_format") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)\r
83         {\r
84                 AVCodecContext* c = (AVCodecContext*)obj;               \r
85                 auto pix_fmt = av_get_pix_fmt(val);\r
86                 if(pix_fmt == PIX_FMT_NONE)\r
87                         return -1;              \r
88                 c->pix_fmt = pix_fmt;\r
89                 return 0;\r
90         }\r
91         if((strcmp(name, "r") == 0 || strcmp(name, "frame_rate") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)\r
92         {\r
93                 AVCodecContext* c = (AVCodecContext*)obj;       \r
94 \r
95                 if(c->codec_type != AVMEDIA_TYPE_VIDEO)\r
96                         return -1;\r
97 \r
98                 AVRational rate;\r
99                 int ret = av_parse_video_rate(&rate, val);\r
100                 if(ret < 0)\r
101                         return ret;\r
102 \r
103                 c->time_base.num = rate.den;\r
104                 c->time_base.den = rate.num;\r
105                 return 0;\r
106         }\r
107 \r
108         return ::av_opt_set(obj, name, val, search_flags);\r
109 }\r
110 \r
111 struct output_format\r
112 {\r
113         AVOutputFormat* format;\r
114         int                             width;\r
115         int                             height;\r
116         AVCodecID               vcodec;\r
117         AVCodecID               acodec;\r
118 \r
119         output_format(const core::video_format_desc& format_desc, const std::string& filename, std::vector<option>& options)\r
120                 : format(av_guess_format(nullptr, filename.c_str(), nullptr))\r
121                 , width(format_desc.width)\r
122                 , height(format_desc.height)\r
123                 , vcodec(CODEC_ID_NONE)\r
124                 , acodec(CODEC_ID_NONE)\r
125         {\r
126                 if (format == nullptr)\r
127                         BOOST_THROW_EXCEPTION(caspar_exception()\r
128                                 << msg_info(filename + " not a supported file for recording"));\r
129 \r
130                 boost::range::remove_erase_if(options, [&](const option& o)\r
131                 {\r
132                         return set_opt(o.name, o.value);\r
133                 });\r
134                 \r
135                 if(vcodec == CODEC_ID_NONE)\r
136                         vcodec = format->video_codec;\r
137 \r
138                 if(acodec == CODEC_ID_NONE)\r
139                         acodec = format->audio_codec;\r
140                 \r
141                 if(vcodec == CODEC_ID_NONE)\r
142                         vcodec = CODEC_ID_H264;\r
143                 \r
144                 if(acodec == CODEC_ID_NONE)\r
145                         acodec = CODEC_ID_PCM_S16LE;\r
146         }\r
147         \r
148         bool set_opt(const std::string& name, const std::string& value)\r
149         {\r
150                 //if(name == "target")\r
151                 //{ \r
152                 //      enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;\r
153                 //      \r
154                 //      if(name.find("pal-") != std::string::npos)\r
155                 //              norm = PAL;\r
156                 //      else if(name.find("ntsc-") != std::string::npos)\r
157                 //              norm = NTSC;\r
158 \r
159                 //      if(norm == UNKNOWN)\r
160                 //              BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("target"));\r
161                 //      \r
162                 //      if (name.find("-dv") != std::string::npos) \r
163                 //      {\r
164                 //              set_opt("f", "dv");\r
165                 //              set_opt("s", norm == PAL ? "720x576" : "720x480");\r
166                 //              //set_opt("pix_fmt", name.find("-dv50") != std::string::npos ? "yuv422p" : norm == PAL ? "yuv420p" : "yuv411p");\r
167                 //              //set_opt("ar", "48000");\r
168                 //              //set_opt("ac", "2");\r
169                 //      } \r
170                 //}\r
171                 if(name == "f")\r
172                 {\r
173                         format = av_guess_format(value.c_str(), nullptr, nullptr);\r
174 \r
175                         if(format == nullptr)\r
176                                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("f"));\r
177 \r
178                         return true;\r
179                 }\r
180                 else if(name == "vcodec")\r
181                 {\r
182                         auto c = avcodec_find_encoder_by_name(value.c_str());\r
183                         if(c == nullptr)\r
184                                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("vcodec"));\r
185 \r
186                         vcodec = avcodec_find_encoder_by_name(value.c_str())->id;\r
187                         return true;\r
188 \r
189                 }\r
190                 else if(name == "acodec")\r
191                 {\r
192                         auto c = avcodec_find_encoder_by_name(value.c_str());\r
193                         if(c == nullptr)\r
194                                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("acodec"));\r
195 \r
196                         acodec = avcodec_find_encoder_by_name(value.c_str())->id;\r
197 \r
198                         return true;\r
199                 }\r
200                 else if(name == "s")\r
201                 {\r
202                         if(av_parse_video_size(&width, &height, value.c_str()) < 0)\r
203                                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("s"));\r
204                         \r
205                         return true;\r
206                 }\r
207 \r
208                 return false;\r
209         }\r
210 };\r
211 \r
212 typedef std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>>     byte_vector;\r
213 \r
214 struct ffmpeg_consumer : boost::noncopyable\r
215 {               \r
216         const std::string                                               filename_;\r
217                 \r
218         std::shared_ptr<AVFormatContext>                oc_;\r
219         const core::video_format_desc                   format_desc_;\r
220         const core::channel_layout                              channel_layout_;\r
221         \r
222         const safe_ptr<diagnostics::graph>              graph_;\r
223 \r
224         executor                                                                encode_executor_;\r
225         \r
226         std::shared_ptr<AVStream>                               audio_st_;\r
227         std::shared_ptr<AVStream>                               video_st_;\r
228         \r
229         byte_vector                                                             audio_outbuf_;\r
230         byte_vector                                                             audio_buf_;\r
231         byte_vector                                                             video_outbuf_;\r
232         byte_vector                                                             key_picture_buf_;\r
233         byte_vector                                                             picture_buf_;\r
234         std::shared_ptr<audio_resampler>                swr_;\r
235         std::shared_ptr<SwsContext>                             sws_;\r
236 \r
237         int64_t                                                                 in_frame_number_;\r
238         int64_t                                                                 out_frame_number_;\r
239 \r
240         output_format                                                   output_format_;\r
241         bool                                                                    key_only_;\r
242         tbb::atomic<int64_t>                                    current_encoding_delay_;\r
243         \r
244 public:\r
245         ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, std::vector<option> options, bool key_only, const core::channel_layout& audio_channel_layout)\r
246                 : filename_(filename)\r
247                 , video_outbuf_(1920*1080*8)\r
248                 , audio_outbuf_(10000)\r
249                 , format_desc_(format_desc)\r
250                 , channel_layout_(audio_channel_layout)\r
251                 , encode_executor_(print())\r
252                 , in_frame_number_(0)\r
253                 , out_frame_number_(0)\r
254                 , output_format_(format_desc, filename, options)\r
255                 , key_only_(key_only)\r
256         {\r
257                 current_encoding_delay_ = 0;\r
258 \r
259                 // TODO: Ask stakeholders about case where file already exists.\r
260                 boost::filesystem::remove(boost::filesystem::wpath(env::media_folder() + widen(filename))); // Delete the file if it exists\r
261 \r
262                 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
263                 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
264                 graph_->set_text(print());\r
265                 diagnostics::register_graph(graph_);\r
266 \r
267                 encode_executor_.set_capacity(8);\r
268                                 \r
269                 AVFormatContext* oc;\r
270 \r
271                 THROW_ON_ERROR2(avformat_alloc_output_context2(\r
272                         &oc, \r
273                         output_format_.format, \r
274                         nullptr, \r
275                         filename_.c_str()), "[ffmpeg_consumer]");\r
276 \r
277                 oc_.reset(oc);\r
278                                                                 \r
279                 //  Add the audio and video streams using the default format codecs     and initialize the codecs.\r
280                 auto options2 = options;\r
281                 video_st_ = add_video_stream(options2);\r
282 \r
283                 if (!key_only)\r
284                         audio_st_ = add_audio_stream(options);\r
285                                 \r
286                 av_dump_format(oc_.get(), 0, filename_.c_str(), 1);\r
287                  \r
288                 // Open the output ffmpeg, if needed.\r
289                 if (!(oc_->oformat->flags & AVFMT_NOFILE)) \r
290                         THROW_ON_ERROR2(avio_open2(&oc_->pb, filename_.c_str(), AVIO_FLAG_WRITE, NULL, NULL), "[ffmpeg_consumer]");\r
291                                 \r
292                 THROW_ON_ERROR2(avformat_write_header(oc_.get(), nullptr), "[ffmpeg_consumer]");\r
293 \r
294                 if(options.size() > 0)\r
295                 {\r
296                         BOOST_FOREACH(auto& option, options)\r
297                                 CASPAR_LOG(warning) << L"Invalid option: -" << widen(option.name) << L" " << widen(option.value);\r
298                 }\r
299 \r
300                 CASPAR_LOG(info) << print() << L" Successfully Initialized.";   \r
301         }\r
302 \r
303         ~ffmpeg_consumer()\r
304         {    \r
305                 encode_executor_.stop();\r
306                 encode_executor_.join();\r
307 \r
308                 LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");\r
309                 \r
310                 if (!key_only_)\r
311                         audio_st_.reset();\r
312                 video_st_.reset();\r
313                           \r
314                 if (!(oc_->oformat->flags & AVFMT_NOFILE)) \r
315                         LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]"); // Close the output ffmpeg.\r
316 \r
317                 CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; \r
318         }\r
319                         \r
320         std::wstring print() const\r
321         {\r
322                 return L"ffmpeg[" + widen(filename_) + L"]";\r
323         }\r
324 \r
325         std::shared_ptr<AVStream> add_video_stream(std::vector<option>& options)\r
326         { \r
327                 if(output_format_.vcodec == CODEC_ID_NONE)\r
328                         return nullptr;\r
329 \r
330                 auto st = av_new_stream(oc_.get(), 0);\r
331                 if (!st)                \r
332                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));              \r
333 \r
334                 auto encoder = avcodec_find_encoder(output_format_.vcodec);\r
335                 if (!encoder)\r
336                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Codec not found."));\r
337 \r
338                 auto c = st->codec;\r
339 \r
340                 avcodec_get_context_defaults3(c, encoder);\r
341                                 \r
342                 c->codec_id                     = output_format_.vcodec;\r
343                 c->codec_type           = AVMEDIA_TYPE_VIDEO;\r
344                 c->width                        = output_format_.width;\r
345                 c->height                       = output_format_.height;\r
346                 c->time_base.den        = format_desc_.time_scale;\r
347                 c->time_base.num        = format_desc_.duration;\r
348                 c->gop_size                     = 25;\r
349                 c->flags                   |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);\r
350                 if(c->pix_fmt == PIX_FMT_NONE)\r
351                         c->pix_fmt = PIX_FMT_YUV420P;\r
352 \r
353                 if(c->codec_id == CODEC_ID_PRORES)\r
354                 {                       \r
355                         c->bit_rate     = c->width < 1280 ? 63*1000000 : 220*1000000;\r
356                         c->pix_fmt      = PIX_FMT_YUV422P10;\r
357                 }\r
358                 else if(c->codec_id == CODEC_ID_DNXHD)\r
359                 {\r
360                         if(c->width < 1280 || c->height < 720)\r
361                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Unsupported video dimensions."));\r
362 \r
363                         c->bit_rate     = 220*1000000;\r
364                         c->pix_fmt      = PIX_FMT_YUV422P;\r
365                 }\r
366                 else if(c->codec_id == CODEC_ID_DVVIDEO)\r
367                 {\r
368                         c->width = c->height == 1280 ? 960  : c->width;\r
369                         \r
370                         if(format_desc_.format == core::video_format::ntsc)\r
371                                 c->pix_fmt = PIX_FMT_YUV411P;\r
372                         else if(format_desc_.format == core::video_format::pal)\r
373                                 c->pix_fmt = PIX_FMT_YUV420P;\r
374                         else // dv50\r
375                                 c->pix_fmt = PIX_FMT_YUV422P;\r
376                         \r
377                         if(format_desc_.duration == 1001)                       \r
378                                 c->width = c->height == 1080 ? 1280 : c->width;                 \r
379                         else\r
380                                 c->width = c->height == 1080 ? 1440 : c->width;                 \r
381                 }\r
382                 else if(c->codec_id == CODEC_ID_H264)\r
383                 {                          \r
384                         c->pix_fmt = PIX_FMT_YUV420P;    \r
385                         if(options.empty())\r
386                         {\r
387                                 av_opt_set(c->priv_data, "preset", "ultrafast", 0);\r
388                                 av_opt_set(c->priv_data, "tune",   "fastdecode",   0);\r
389                                 av_opt_set(c->priv_data, "crf",    "5",     0);\r
390                         }\r
391                 }\r
392                 else if(c->codec_id == CODEC_ID_QTRLE)\r
393                 {\r
394                         c->pix_fmt = PIX_FMT_ARGB;\r
395                 }\r
396                                 \r
397                 c->max_b_frames = 0; // b-frames not supported.\r
398                                 \r
399                 boost::range::remove_erase_if(options, [&](const option& o)\r
400                 {\r
401                         return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1 ||\r
402                                    ffmpeg::av_opt_set(c->priv_data, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;\r
403                 });\r
404                                 \r
405                 if(output_format_.format->flags & AVFMT_GLOBALHEADER)\r
406                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
407                 \r
408                 c->thread_count = boost::thread::hardware_concurrency();\r
409                 if(avcodec_open2(c, encoder, nullptr) < 0)\r
410                 {\r
411                         c->thread_count = 1;\r
412                         THROW_ON_ERROR2(avcodec_open2(c, encoder, nullptr), "[ffmpeg_consumer]");\r
413                 }\r
414 \r
415                 return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
416                 {\r
417                         LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");\r
418                         av_freep(&st->codec);\r
419                         av_freep(&st);\r
420                 });\r
421         }\r
422                 \r
423         std::shared_ptr<AVStream> add_audio_stream(std::vector<option>& options)\r
424         {\r
425                 if(output_format_.acodec == CODEC_ID_NONE)\r
426                         return nullptr;\r
427 \r
428                 auto st = av_new_stream(oc_.get(), 1);\r
429                 if(!st)\r
430                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));               \r
431                 \r
432                 auto encoder = avcodec_find_encoder(output_format_.acodec);\r
433                 if (!encoder)\r
434                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));\r
435                 \r
436                 auto c = st->codec;\r
437 \r
438                 avcodec_get_context_defaults3(c, encoder);\r
439 \r
440                 c->codec_id                     = output_format_.acodec;\r
441                 c->codec_type           = AVMEDIA_TYPE_AUDIO;\r
442                 c->sample_rate          = 48000;\r
443                 c->channels                     = channel_layout_.num_channels;\r
444                 c->sample_fmt           = AV_SAMPLE_FMT_S16;\r
445 \r
446                 if(output_format_.vcodec == CODEC_ID_FLV1)              \r
447                         c->sample_rate  = 44100;                \r
448 \r
449                 if(output_format_.format->flags & AVFMT_GLOBALHEADER)\r
450                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
451                                 \r
452                 boost::range::remove_erase_if(options, [&](const option& o)\r
453                 {\r
454                         return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;\r
455                 });\r
456 \r
457                 THROW_ON_ERROR2(avcodec_open2(c, encoder, nullptr), "[ffmpeg_consumer]");\r
458 \r
459                 return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
460                 {\r
461                         LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");;\r
462                         av_freep(&st->codec);\r
463                         av_freep(&st);\r
464                 });\r
465         }\r
466 \r
467         std::shared_ptr<AVFrame> convert_video(core::read_frame& frame, AVCodecContext* c)\r
468         {\r
469                 if(!sws_) \r
470                 {\r
471                         sws_.reset(sws_getContext(format_desc_.width, format_desc_.height, PIX_FMT_BGRA, c->width, c->height, c->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr), sws_freeContext);\r
472                         if (sws_ == nullptr) \r
473                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));\r
474                 }\r
475 \r
476                 std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);\r
477                 auto in_picture = reinterpret_cast<AVPicture*>(in_frame.get());\r
478 \r
479                 if (key_only_)\r
480                 {\r
481                         key_picture_buf_.resize(frame.image_data().size());\r
482                         in_picture->linesize[0] = format_desc_.width * 4;\r
483                         in_picture->data[0] = key_picture_buf_.data();\r
484 \r
485                         fast_memshfl(in_picture->data[0], frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
486                 }\r
487                 else\r
488                 {\r
489                         avpicture_fill(in_picture, const_cast<uint8_t*>(frame.image_data().begin()), PIX_FMT_BGRA, format_desc_.width, format_desc_.height);\r
490                 }\r
491 \r
492                 std::shared_ptr<AVFrame> out_frame(avcodec_alloc_frame(), av_free);\r
493                 picture_buf_.resize(avpicture_get_size(c->pix_fmt, c->width, c->height));\r
494                 avpicture_fill(reinterpret_cast<AVPicture*>(out_frame.get()), picture_buf_.data(), c->pix_fmt, c->width, c->height);\r
495 \r
496                 sws_scale(sws_.get(), in_frame->data, in_frame->linesize, 0, format_desc_.height, out_frame->data, out_frame->linesize);\r
497 \r
498                 return out_frame;\r
499         }\r
500   \r
501         void encode_video_frame(core::read_frame& frame)\r
502         { \r
503                 auto c = video_st_->codec;\r
504                 \r
505                 auto in_time  = static_cast<double>(in_frame_number_) / format_desc_.fps;\r
506                 auto out_time = static_cast<double>(out_frame_number_) / (static_cast<double>(c->time_base.den) / static_cast<double>(c->time_base.num));\r
507                 \r
508                 in_frame_number_++;\r
509 \r
510                 if(out_time - in_time > 0.01)\r
511                         return;\r
512  \r
513                 auto av_frame = convert_video(frame, c);\r
514                 av_frame->interlaced_frame      = format_desc_.field_mode != core::field_mode::progressive;\r
515                 av_frame->top_field_first       = format_desc_.field_mode == core::field_mode::upper;\r
516                 av_frame->pts                           = out_frame_number_++;\r
517 \r
518                 int out_size = THROW_ON_ERROR2(avcodec_encode_video(c, video_outbuf_.data(), video_outbuf_.size(), av_frame.get()), "[ffmpeg_consumer]");\r
519                 if(out_size == 0)\r
520                         return;\r
521                                 \r
522                 safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
523                 {\r
524                         av_free_packet(p);\r
525                         delete p;\r
526                 });\r
527                 av_init_packet(pkt.get());\r
528  \r
529                 if (c->coded_frame->pts != AV_NOPTS_VALUE)\r
530                         pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st_->time_base);\r
531 \r
532                 if(c->coded_frame->key_frame)\r
533                         pkt->flags |= AV_PKT_FLAG_KEY;\r
534 \r
535                 pkt->stream_index       = video_st_->index;\r
536                 pkt->data                       = video_outbuf_.data();\r
537                 pkt->size                       = out_size;\r
538                         \r
539                 av_interleaved_write_frame(oc_.get(), pkt.get());               \r
540         }\r
541                 \r
542         byte_vector convert_audio(core::read_frame& frame, AVCodecContext* c)\r
543         {\r
544                 if(!swr_)               \r
545                         swr_.reset(new audio_resampler(c->channels, frame.num_channels(), \r
546                                                                                    c->sample_rate, format_desc_.audio_sample_rate,\r
547                                                                                    c->sample_fmt, AV_SAMPLE_FMT_S32));\r
548                 \r
549 \r
550                 auto audio_data = frame.audio_data();\r
551 \r
552                 std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>> audio_resample_buffer;\r
553                 std::copy(reinterpret_cast<const uint8_t*>(audio_data.begin()), \r
554                                   reinterpret_cast<const uint8_t*>(audio_data.begin()) + audio_data.size()*4, \r
555                                   std::back_inserter(audio_resample_buffer));\r
556                 \r
557                 audio_resample_buffer = swr_->resample(std::move(audio_resample_buffer));\r
558                 \r
559                 return byte_vector(audio_resample_buffer.begin(), audio_resample_buffer.end());\r
560         }\r
561 \r
562         void encode_audio_frame(core::read_frame& frame)\r
563         {                       \r
564                 auto c = audio_st_->codec;\r
565 \r
566                 boost::range::push_back(audio_buf_, convert_audio(frame, c));\r
567                 \r
568                 std::size_t frame_size = c->frame_size;\r
569                 auto input_audio_size = frame_size * av_get_bytes_per_sample(c->sample_fmt) * c->channels;\r
570                 \r
571                 while(audio_buf_.size() >= input_audio_size)\r
572                 {\r
573                         safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
574                         {\r
575                                 av_free_packet(p);\r
576                                 delete p;\r
577                         });\r
578                         av_init_packet(pkt.get());\r
579 \r
580                         if(frame_size > 1)\r
581                         {                                                               \r
582                                 pkt->size = avcodec_encode_audio(c, audio_outbuf_.data(), audio_outbuf_.size(), reinterpret_cast<short*>(audio_buf_.data()));\r
583                                 audio_buf_.erase(audio_buf_.begin(), audio_buf_.begin() + input_audio_size);\r
584                         }\r
585                         else\r
586                         {\r
587                                 audio_outbuf_ = std::move(audio_buf_);          \r
588                                 audio_buf_.clear();\r
589                                 pkt->size = audio_outbuf_.size();\r
590                                 pkt->data = audio_outbuf_.data();\r
591                         }\r
592                 \r
593                         if(pkt->size == 0)\r
594                                 return;\r
595 \r
596                         if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)\r
597                                 pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, audio_st_->time_base);\r
598 \r
599                         pkt->flags               |= AV_PKT_FLAG_KEY;\r
600                         pkt->stream_index = audio_st_->index;\r
601                         pkt->data                 = reinterpret_cast<uint8_t*>(audio_outbuf_.data());\r
602                 \r
603                         av_interleaved_write_frame(oc_.get(), pkt.get());\r
604                 }\r
605         }\r
606                  \r
607         void send(const safe_ptr<core::read_frame>& frame)\r
608         {\r
609                 encode_executor_.begin_invoke([=]\r
610                 {               \r
611                         boost::timer frame_timer;\r
612 \r
613                         encode_video_frame(*frame);\r
614 \r
615                         if (!key_only_)\r
616                                 encode_audio_frame(*frame);\r
617 \r
618                         graph_->set_value("frame-time", frame_timer.elapsed()*format_desc_.fps*0.5);\r
619                         current_encoding_delay_ = frame->get_age_millis();\r
620                 });\r
621         }\r
622 \r
623         bool ready_for_frame()\r
624         {\r
625                 return encode_executor_.size() < encode_executor_.capacity();\r
626         }\r
627 \r
628         void mark_dropped()\r
629         {\r
630                 graph_->set_tag("dropped-frame");\r
631 \r
632                 // TODO: adjust PTS accordingly to make dropped frames contribute\r
633                 //       to the total playing time\r
634         }\r
635 };\r
636 \r
637 struct ffmpeg_consumer_proxy : public core::frame_consumer\r
638 {\r
639         const std::wstring                              filename_;\r
640         const std::vector<option>               options_;\r
641         const bool                                              separate_key_;\r
642         core::video_format_desc                 format_desc_;\r
643 \r
644         std::unique_ptr<ffmpeg_consumer> consumer_;\r
645         std::unique_ptr<ffmpeg_consumer> key_only_consumer_;\r
646 \r
647 public:\r
648 \r
649         ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options, bool separate_key_)\r
650                 : filename_(filename)\r
651                 , options_(options)\r
652                 , separate_key_(separate_key_)\r
653         {\r
654         }\r
655         \r
656         virtual void initialize(const core::video_format_desc& format_desc, int)\r
657         {\r
658                 format_desc_ = format_desc;\r
659         }\r
660 \r
661         virtual int64_t presentation_frame_age_millis() const override\r
662         {\r
663                 return consumer_ ? consumer_->current_encoding_delay_ : 0;\r
664         }\r
665         \r
666         virtual boost::unique_future<bool> send(const safe_ptr<core::read_frame>& frame) override\r
667         {\r
668                 if (!consumer_)\r
669                         do_initialize(frame->multichannel_view().channel_layout());\r
670 \r
671                 bool ready_for_frame = consumer_->ready_for_frame();\r
672 \r
673                 if (ready_for_frame && separate_key_)\r
674                         ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();\r
675 \r
676                 if (ready_for_frame)\r
677                 {\r
678                         consumer_->send(frame);\r
679 \r
680                         if (separate_key_)\r
681                                 key_only_consumer_->send(frame);\r
682                 }\r
683                 else\r
684                 {\r
685                         consumer_->mark_dropped();\r
686 \r
687                         if (separate_key_)\r
688                                 key_only_consumer_->mark_dropped();\r
689                 }\r
690 \r
691                 return caspar::wrap_as_future(true);\r
692         }\r
693         \r
694         virtual std::wstring print() const override\r
695         {\r
696                 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";\r
697         }\r
698 \r
699         virtual boost::property_tree::wptree info() const override\r
700         {\r
701                 boost::property_tree::wptree info;\r
702                 info.add(L"type", L"ffmpeg-consumer");\r
703                 info.add(L"filename", filename_);\r
704                 info.add(L"separate_key", separate_key_);\r
705                 return info;\r
706         }\r
707                 \r
708         virtual bool has_synchronization_clock() const override\r
709         {\r
710                 return false;\r
711         }\r
712 \r
713         virtual size_t buffer_depth() const override\r
714         {\r
715                 return 1;\r
716         }\r
717 \r
718         virtual int index() const override\r
719         {\r
720                 return 200;\r
721         }\r
722 private:\r
723         void do_initialize(const core::channel_layout& channel_layout)\r
724         {\r
725                 consumer_.reset();\r
726                 key_only_consumer_.reset();\r
727                 consumer_.reset(new ffmpeg_consumer(\r
728                                 narrow(filename_),\r
729                                 format_desc_,\r
730                                 options_,\r
731                                 false,\r
732                                 channel_layout));\r
733 \r
734                 if (separate_key_)\r
735                 {\r
736                         boost::filesystem::wpath fill_file(filename_);\r
737                         auto without_extension = fill_file.stem();\r
738                         auto key_file = env::media_folder() + without_extension + L"_A" + fill_file.extension();\r
739                         \r
740                         key_only_consumer_.reset(new ffmpeg_consumer(\r
741                                         narrow(key_file),\r
742                                         format_desc_,\r
743                                         options_,\r
744                                         true,\r
745                                         channel_layout));\r
746                 }\r
747         }\r
748 };      \r
749 \r
750 safe_ptr<core::frame_consumer> create_consumer(const core::parameters& params)\r
751 {\r
752         if(params.size() < 1 || params[0] != L"FILE")\r
753                 return core::frame_consumer::empty();\r
754 \r
755         auto params2 = params;\r
756         \r
757         auto filename   = (params2.size() > 1 ? params2[1] : L"");\r
758         bool separate_key = params2.remove_if_exists(L"SEPARATE_KEY");\r
759 \r
760         std::vector<option> options;\r
761         \r
762         if (params2.size() >= 3)\r
763         {\r
764                 for (auto opt_it = params2.begin() + 2; opt_it != params2.end();)\r
765                 {\r
766                         auto name  = narrow(boost::trim_copy(boost::to_lower_copy(*opt_it++))).substr(1);\r
767 \r
768                         if (opt_it == params2.end())\r
769                                 break;\r
770 \r
771                         auto value = narrow(boost::trim_copy(boost::to_lower_copy(*opt_it++)));\r
772                                 \r
773                         if (value == "h264")\r
774                                 value = "libx264";\r
775                         else if (value == "dvcpro")\r
776                                 value = "dvvideo";\r
777 \r
778                         options.push_back(option(name, value));\r
779                 }\r
780         }\r
781                 \r
782         return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, options, separate_key);\r
783 }\r
784 \r
785 safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)\r
786 {\r
787         auto filename           = ptree.get<std::wstring>(L"path");\r
788         auto codec                      = ptree.get(L"vcodec", L"libx264");\r
789         auto separate_key       = ptree.get(L"separate-key", false);\r
790 \r
791         std::vector<option> options;\r
792         options.push_back(option("vcodec", narrow(codec)));\r
793         \r
794         return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, options, separate_key);\r
795 }\r
796 \r
797 }}\r