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