]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/consumer/ffmpeg_consumer.cpp
da55e707812c348c6ec232fe759ea0bcbd9dd4df
[casparcg] / modules / ffmpeg / consumer / ffmpeg_consumer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This ffmpeg is part of CasparCG.\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 */\r
20  \r
21 #include "../StdAfx.h"\r
22 \r
23 #include "../ffmpeg_error.h"\r
24 \r
25 #include "ffmpeg_consumer.h"\r
26 \r
27 #include <core/mixer/read_frame.h>\r
28 #include <core/mixer/audio/audio_util.h>\r
29 #include <core/consumer/frame_consumer.h>\r
30 #include <core/video_format.h>\r
31 \r
32 #include <common/concurrency/executor.h>\r
33 #include <common/diagnostics/graph.h>\r
34 #include <common/utility/string.h>\r
35 #include <common/env.h>\r
36 \r
37 #include <boost/timer.hpp>\r
38 #include <boost/thread/once.hpp>\r
39 #include <boost/thread.hpp>\r
40 #include <boost/algorithm/string.hpp>\r
41 \r
42 #include <tbb/cache_aligned_allocator.h>\r
43 #include <tbb/parallel_invoke.h>\r
44 \r
45 #include <cstdio>\r
46 \r
47 #if defined(_MSC_VER)\r
48 #pragma warning (push)\r
49 #pragma warning (disable : 4244)\r
50 #endif\r
51 extern "C" \r
52 {\r
53         #define __STDC_CONSTANT_MACROS\r
54         #define __STDC_LIMIT_MACROS\r
55         #include <libavformat/avformat.h>\r
56         #include <libswscale/swscale.h>\r
57         #include <libavutil/opt.h>\r
58 }\r
59 #if defined(_MSC_VER)\r
60 #pragma warning (pop)\r
61 #endif\r
62 \r
63 namespace caspar { namespace ffmpeg {\r
64         \r
65 struct ffmpeg_consumer : boost::noncopyable\r
66 {               \r
67         const std::string                                               filename_;\r
68                 \r
69         const std::shared_ptr<AVFormatContext>  oc_;\r
70         const core::video_format_desc                   format_desc_;\r
71         \r
72         const safe_ptr<diagnostics::graph>              graph_;\r
73         boost::timer                                                    frame_timer_;\r
74         boost::timer                                                    write_timer_;\r
75 \r
76         executor                                                                executor_;\r
77         executor                                                                file_write_executor_;\r
78 \r
79         // Audio\r
80         std::shared_ptr<AVStream>                               audio_st_;\r
81         std::vector<uint8_t>                                    audio_outbuf_;\r
82 \r
83         std::vector<int16_t>                                    audio_input_buffer_;\r
84 \r
85         // Video\r
86         std::shared_ptr<AVStream>                               video_st_;\r
87         std::vector<uint8_t>                                    video_outbuf_;\r
88 \r
89         std::vector<uint8_t>                                    picture_buf_;\r
90         std::shared_ptr<SwsContext>                             img_convert_ctx_;\r
91 \r
92         int64_t                                                                 frame_number_;\r
93         \r
94 public:\r
95         ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, const std::string& codec, const std::string& options)\r
96                 : filename_(filename + ".mov")\r
97                 , video_outbuf_(1920*1080*8)\r
98                 , audio_outbuf_(48000)\r
99                 , oc_(avformat_alloc_context(), av_free)\r
100                 , format_desc_(format_desc)\r
101                 , executor_(print())\r
102                 , file_write_executor_(print() + L"/output")\r
103                 , frame_number_(0)\r
104         {\r
105                 graph_->add_guide("frame-time", 0.5);\r
106                 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
107                 graph_->set_color("write-time", diagnostics::color(0.5f, 0.5f, 0.1f));\r
108                 graph_->set_text(print());\r
109                 diagnostics::register_graph(graph_);\r
110 \r
111                 executor_.set_capacity(8);\r
112                 file_write_executor_.set_capacity(8);\r
113 \r
114                 oc_->oformat = av_guess_format(nullptr, filename_.c_str(), nullptr);\r
115                 if (!oc_->oformat)\r
116                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not find suitable output format."));\r
117                 \r
118                 THROW_ON_ERROR2(av_set_parameters(oc_.get(), nullptr), "[ffmpeg_consumer]");\r
119 \r
120                 strcpy_s(oc_->filename, filename_.c_str());\r
121                 \r
122                 auto video_codec = avcodec_find_encoder_by_name(codec.c_str());\r
123                 if(video_codec == nullptr)\r
124                         BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info(codec));\r
125 \r
126                 //  Add the audio and video streams using the default format codecs     and initialize the codecs .\r
127                 video_st_ = add_video_stream(video_codec->id, options);\r
128                 audio_st_ = add_audio_stream();\r
129                                 \r
130                 dump_format(oc_.get(), 0, filename_.c_str(), 1);\r
131                  \r
132                 // Open the output ffmpeg, if needed.\r
133                 if (!(oc_->oformat->flags & AVFMT_NOFILE)) \r
134                         THROW_ON_ERROR2(avio_open(&oc_->pb, filename_.c_str(), URL_WRONLY), "[ffmpeg_consumer]");\r
135                                 \r
136                 THROW_ON_ERROR2(av_write_header(oc_.get()), "[ffmpeg_consumer]");\r
137 \r
138                 CASPAR_LOG(info) << print() << L" Successfully Initialized.";   \r
139         }\r
140 \r
141         ~ffmpeg_consumer()\r
142         {    \r
143                 file_write_executor_.stop();\r
144                 file_write_executor_.join();\r
145 \r
146                 executor_.stop();\r
147                 executor_.join();\r
148                 \r
149                 try\r
150                 {\r
151                         THROW_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");\r
152                 \r
153                         audio_st_.reset();\r
154                         video_st_.reset();\r
155                           \r
156                         for(size_t i = 0; i < oc_->nb_streams; i++) \r
157                         {\r
158                                 av_freep(&oc_->streams[i]->codec);\r
159                                 av_freep(&oc_->streams[i]);\r
160                         }\r
161 \r
162                         if (!(oc_->oformat->flags & AVFMT_NOFILE)) \r
163                                 THROW_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]"); // Close the output ffmpeg.\r
164 \r
165                         CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; \r
166                 }\r
167                 catch(...)\r
168                 {\r
169                         CASPAR_LOG_CURRENT_EXCEPTION();\r
170                 }\r
171 \r
172         }\r
173                         \r
174         std::wstring print() const\r
175         {\r
176                 return L"ffmpeg[" + widen(filename_) + L"]";\r
177         }\r
178 \r
179 \r
180         std::shared_ptr<AVStream> add_video_stream(enum CodecID codec_id, const std::string& options)\r
181         { \r
182                 auto st = av_new_stream(oc_.get(), 0);\r
183                 if (!st) \r
184                 {\r
185                         BOOST_THROW_EXCEPTION(caspar_exception() \r
186                                 << msg_info("Could not alloc video-stream")                             \r
187                                 << boost::errinfo_api_function("av_new_stream"));\r
188                 }\r
189 \r
190                 auto encoder = avcodec_find_encoder(codec_id);\r
191                 if (!encoder)\r
192                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));\r
193 \r
194                 auto c = st->codec;\r
195 \r
196                 avcodec_get_context_defaults3(c, encoder);\r
197                                 \r
198                 c->codec_id                     = codec_id;\r
199                 c->codec_type           = AVMEDIA_TYPE_VIDEO;\r
200                 c->width                        = format_desc_.width;\r
201                 c->height                       = format_desc_.height;\r
202                 c->time_base.den        = format_desc_.time_scale;\r
203                 c->time_base.num        = format_desc_.duration;\r
204                 c->gop_size                     = 25;\r
205 \r
206                 if(c->codec_id == CODEC_ID_PRORES)\r
207                 {                       \r
208                         c->bit_rate     = c->bit_rate > 0 ? c->bit_rate : format_desc_.width < 1280 ? 42*1000000 : 147*1000000;\r
209                         c->pix_fmt      = PIX_FMT_YUV422P10;\r
210                         CASPAR_LOG(info) << print() << L"Options set: " << av_set_options_string(c->priv_data, options.c_str(), "=", ":");\r
211                 }\r
212                 else if(c->codec_id == CODEC_ID_DNXHD)\r
213                 {\r
214                         if(format_desc_.width < 1280 || format_desc_.height < 720)\r
215                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("unsupported dimension"));\r
216 \r
217                         c->bit_rate     = c->bit_rate > 0 ? c->bit_rate : 220*1000000;\r
218                         c->pix_fmt      = PIX_FMT_YUV422P;\r
219                         \r
220                         CASPAR_LOG(info) << print() << L"Options set: " << av_set_options_string(c->priv_data, options.c_str(), "=", ":");\r
221                 }\r
222                 else if(c->codec_id == CODEC_ID_DVVIDEO)\r
223                 {\r
224                         c->bit_rate     = c->bit_rate > 0 ? c->bit_rate : format_desc_.width < 1280 ? 50*1000000 : 100*1000000;\r
225                         c->pix_fmt      = PIX_FMT_YUV422P;\r
226                         \r
227                         CASPAR_LOG(info) << print() << L"Options set: " << av_set_options_string(c->priv_data, options.c_str(), "=", ":");\r
228                 }\r
229                 else if(c->codec_id == CODEC_ID_H264)\r
230                 {                          \r
231                         c->pix_fmt              = PIX_FMT_YUV420P;    \r
232                         av_opt_set(c->priv_data, "preset", "faster", 0);\r
233                         \r
234                         CASPAR_LOG(info) << print() << L"Options set: " << av_set_options_string(c->priv_data, options.c_str(), "=", ":");\r
235 \r
236                         c->max_b_frames = 0; // b-franes bit supported.\r
237                 }\r
238                 else\r
239                 {\r
240                         CASPAR_LOG(info) << print() << L"Options set: " << av_set_options_string(c->priv_data, options.c_str(), "=", ":");\r
241                         CASPAR_LOG(warning) << " Potentially unsupported output parameters.";\r
242                 }\r
243                 \r
244                 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)\r
245                         c->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
246                 \r
247                 c->thread_count = boost::thread::hardware_concurrency();\r
248                 THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");\r
249 \r
250                 return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
251                 {\r
252                         avcodec_close(st->codec);\r
253                 });\r
254         }\r
255         \r
256         std::shared_ptr<AVStream> add_audio_stream()\r
257         {\r
258                 auto st = av_new_stream(oc_.get(), 1);\r
259                 if (!st) \r
260                 {\r
261                         BOOST_THROW_EXCEPTION(caspar_exception() \r
262                                 << msg_info("Could not alloc audio-stream")                             \r
263                                 << boost::errinfo_api_function("av_new_stream"));\r
264                 }\r
265 \r
266                 st->codec->codec_id                     = CODEC_ID_PCM_S16LE;\r
267                 st->codec->codec_type           = AVMEDIA_TYPE_AUDIO;\r
268                 st->codec->sample_rate          = 48000;\r
269                 st->codec->channels                     = 2;\r
270                 st->codec->sample_fmt           = SAMPLE_FMT_S16;\r
271                 \r
272                 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)\r
273                         st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
274                 \r
275                 auto codec = avcodec_find_encoder(st->codec->codec_id);\r
276                 if (!codec)\r
277                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));\r
278 \r
279                 THROW_ON_ERROR2(avcodec_open(st->codec, codec), "[ffmpeg_consumer]");\r
280 \r
281                 return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
282                 {\r
283                         avcodec_close(st->codec);\r
284                 });\r
285         }\r
286 \r
287         std::shared_ptr<AVFrame> convert_video_frame(const safe_ptr<core::read_frame>& frame, AVCodecContext* c)\r
288         {\r
289                 if(!img_convert_ctx_) \r
290                 {\r
291                         img_convert_ctx_.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
292                         if (img_convert_ctx_ == nullptr) \r
293                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));\r
294                 }\r
295 \r
296                 std::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);\r
297                 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), const_cast<uint8_t*>(frame->image_data().begin()), PIX_FMT_BGRA, format_desc_.width, format_desc_.height);\r
298                                 \r
299                 std::shared_ptr<AVFrame> local_av_frame(avcodec_alloc_frame(), av_free);\r
300                 picture_buf_.resize(avpicture_get_size(c->pix_fmt, format_desc_.width, format_desc_.height));\r
301                 avpicture_fill(reinterpret_cast<AVPicture*>(local_av_frame.get()), picture_buf_.data(), c->pix_fmt, format_desc_.width, format_desc_.height);\r
302 \r
303                 sws_scale(img_convert_ctx_.get(), av_frame->data, av_frame->linesize, 0, c->height, local_av_frame->data, local_av_frame->linesize);\r
304 \r
305                 return local_av_frame;\r
306         }\r
307   \r
308         std::shared_ptr<AVPacket> encode_video_frame(const safe_ptr<core::read_frame>& frame)\r
309         { \r
310                 auto c = video_st_->codec;\r
311  \r
312                 auto av_frame = convert_video_frame(frame, c);\r
313                 av_frame->interlaced_frame      = format_desc_.field_mode != core::field_mode::progressive;\r
314                 av_frame->top_field_first       = format_desc_.field_mode == core::field_mode::upper;\r
315                 av_frame->pts                           = frame_number_++;\r
316 \r
317                 int out_size = THROW_ON_ERROR2(avcodec_encode_video(c, video_outbuf_.data(), video_outbuf_.size(), av_frame.get()), "[ffmpeg_consumer]");\r
318                 if(out_size > 0)\r
319                 {\r
320                         safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
321                         {\r
322                                 av_free_packet(p);\r
323                                 delete p;\r
324                         });\r
325                         av_init_packet(pkt.get());\r
326  \r
327                         if (c->coded_frame->pts != AV_NOPTS_VALUE)\r
328                                 pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st_->time_base);\r
329 \r
330                         if(c->coded_frame->key_frame)\r
331                                 pkt->flags |= AV_PKT_FLAG_KEY;\r
332 \r
333                         pkt->stream_index       = video_st_->index;\r
334                         pkt->data                       = video_outbuf_.data();\r
335                         pkt->size                       = out_size;\r
336  \r
337                         av_dup_packet(pkt.get());\r
338                         return pkt;\r
339                 }       \r
340                 return nullptr;\r
341         }\r
342                 \r
343         std::shared_ptr<AVPacket> encode_audio_frame(const safe_ptr<core::read_frame>& frame)\r
344         {                       \r
345                 auto c = audio_st_->codec;\r
346 \r
347                 auto audio_data = core::audio_32_to_16(frame->audio_data());\r
348                 \r
349                 safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
350                 {\r
351                         av_free_packet(p);\r
352                         delete p;\r
353                 });\r
354                 av_init_packet(pkt.get());\r
355                 \r
356                 if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)\r
357                         pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, audio_st_->time_base);\r
358 \r
359                 pkt->flags               |= AV_PKT_FLAG_KEY;\r
360                 pkt->stream_index = audio_st_->index;\r
361                 pkt->size                = audio_data.size()*2;\r
362                 pkt->data                = reinterpret_cast<uint8_t*>(audio_data.data());\r
363                 \r
364                 av_dup_packet(pkt.get());\r
365                 return pkt;\r
366         }\r
367                  \r
368         void send(const safe_ptr<core::read_frame>& frame)\r
369         {\r
370                 executor_.begin_invoke([=]\r
371                 {               \r
372                         frame_timer_.restart();\r
373                         auto video = encode_video_frame(frame);\r
374                         auto audio = encode_audio_frame(frame);\r
375                         graph_->update_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5);\r
376                         \r
377                         file_write_executor_.begin_invoke([=]\r
378                         {\r
379                                 write_timer_.restart();\r
380                                 if(video)\r
381                                         av_write_frame(oc_.get(), video.get());\r
382                                 if(audio)\r
383                                         av_write_frame(oc_.get(), audio.get());\r
384                                 graph_->update_value("write-time", write_timer_.elapsed()*format_desc_.fps*0.5);\r
385                         });\r
386                 });\r
387         }\r
388 };\r
389 \r
390 struct ffmpeg_consumer_proxy : public core::frame_consumer\r
391 {\r
392         const std::wstring      filename_;\r
393         const bool                      key_only_;\r
394         const std::string       codec_;\r
395         const std::string       options_;\r
396 \r
397         std::unique_ptr<ffmpeg_consumer> consumer_;\r
398 \r
399 public:\r
400 \r
401         ffmpeg_consumer_proxy(const std::wstring& filename, bool key_only, const std::string codec, const std::string& options)\r
402                 : filename_(filename)\r
403                 , key_only_(key_only)\r
404                 , codec_(boost::to_lower_copy(codec))\r
405                 , options_(options)\r
406         {\r
407         }\r
408         \r
409         virtual void initialize(const core::video_format_desc& format_desc, int, int)\r
410         {\r
411                 consumer_.reset();\r
412                 consumer_.reset(new ffmpeg_consumer(narrow(filename_), format_desc, codec_, options_));\r
413         }\r
414         \r
415         virtual bool send(const safe_ptr<core::read_frame>& frame) override\r
416         {\r
417                 consumer_->send(frame);\r
418                 return true;\r
419         }\r
420         \r
421         virtual std::wstring print() const override\r
422         {\r
423                 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";\r
424         }\r
425                 \r
426         virtual bool has_synchronization_clock() const override\r
427         {\r
428                 return false;\r
429         }\r
430 \r
431         virtual size_t buffer_depth() const override\r
432         {\r
433                 return 1;\r
434         }\r
435 };      \r
436 \r
437 safe_ptr<core::frame_consumer> create_ffmpeg_consumer(const std::vector<std::wstring>& params)\r
438 {\r
439         if(params.size() < 2 || params[0] != L"FILE")\r
440                 return core::frame_consumer::empty();\r
441         \r
442         // TODO: Ask stakeholders about case where file already exists.\r
443         boost::filesystem::remove(boost::filesystem::wpath(env::media_folder() + params[1])); // Delete the file if it exists\r
444         bool key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end();\r
445 \r
446         std::string codec = "dnxhd";\r
447         auto codec_it = std::find(params.begin(), params.end(), L"CODEC");\r
448         if(codec_it != params.end() && codec_it++ != params.end())\r
449                 codec = narrow(*codec_it);\r
450 \r
451         if(codec == "H264" || codec == "h264")\r
452                 codec = "libx264";\r
453 \r
454         std::string options = "";\r
455         auto options_it = std::find(params.begin(), params.end(), L"OPTIONS");\r
456         if(options_it != params.end() && options_it++ != params.end())\r
457                 options = narrow(*options_it);\r
458 \r
459         return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + params[1], key_only, codec, boost::to_lower_copy(options));\r
460 }\r
461 \r
462 safe_ptr<core::frame_consumer> create_ffmpeg_consumer(const boost::property_tree::ptree& ptree)\r
463 {\r
464         std::string filename = ptree.get<std::string>("path");\r
465         auto key_only            = ptree.get("key-only", false);\r
466         auto codec                       = ptree.get("codec", "dnxhd");\r
467         auto options             = ptree.get("options", "");\r
468         \r
469         return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + widen(filename), key_only, codec, options);\r
470 }\r
471 \r
472 }}\r