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