]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/util/util.cpp
Use asmlib instead of our own custom memory functions.
[casparcg] / modules / ffmpeg / producer / util / util.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 "util.h"\r
25 \r
26 #include "flv.h"\r
27 \r
28 #include "../tbb_avcodec.h"\r
29 #include "../../ffmpeg_error.h"\r
30 \r
31 #include <tbb/concurrent_unordered_map.h>\r
32 #include <tbb/concurrent_queue.h>\r
33 \r
34 #include <core/producer/frame/frame_transform.h>\r
35 #include <core/producer/frame/frame_factory.h>\r
36 #include <core/producer/frame_producer.h>\r
37 #include <core/mixer/write_frame.h>\r
38 \r
39 #include <common/exception/exceptions.h>\r
40 #include <common/utility/assert.h>\r
41 \r
42 #include <tbb/parallel_for.h>\r
43 \r
44 #include <boost/filesystem.hpp>\r
45 #include <boost/lexical_cast.hpp>\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         #include <libswscale/swscale.h>\r
54         #include <libavcodec/avcodec.h>\r
55         #include <libavformat/avformat.h>\r
56 }\r
57 #if defined(_MSC_VER)\r
58 #pragma warning (pop)\r
59 #endif\r
60 \r
61 namespace caspar { namespace ffmpeg {\r
62                 \r
63 std::shared_ptr<core::audio_buffer> flush_audio()\r
64 {\r
65         static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());\r
66         return audio;\r
67 }\r
68 \r
69 std::shared_ptr<core::audio_buffer> empty_audio()\r
70 {\r
71         static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());\r
72         return audio;\r
73 }\r
74 \r
75 std::shared_ptr<AVFrame>                        flush_video()\r
76 {\r
77         static std::shared_ptr<AVFrame> video(avcodec_alloc_frame(), av_free);\r
78         return video;\r
79 }\r
80 \r
81 std::shared_ptr<AVFrame>                        empty_video()\r
82 {\r
83         static std::shared_ptr<AVFrame> video(avcodec_alloc_frame(), av_free);\r
84         return video;\r
85 }\r
86 \r
87 core::field_mode::type get_mode(const AVFrame& frame)\r
88 {\r
89         if(!frame.interlaced_frame)\r
90                 return core::field_mode::progressive;\r
91 \r
92         return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;\r
93 }\r
94 \r
95 core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)\r
96 {\r
97         switch(pix_fmt)\r
98         {\r
99         case CASPAR_PIX_FMT_LUMA:       return core::pixel_format::luma;\r
100         case PIX_FMT_GRAY8:                     return core::pixel_format::gray;\r
101         case PIX_FMT_BGRA:                      return core::pixel_format::bgra;\r
102         case PIX_FMT_ARGB:                      return core::pixel_format::argb;\r
103         case PIX_FMT_RGBA:                      return core::pixel_format::rgba;\r
104         case PIX_FMT_ABGR:                      return core::pixel_format::abgr;\r
105         case PIX_FMT_YUV444P:           return core::pixel_format::ycbcr;\r
106         case PIX_FMT_YUV422P:           return core::pixel_format::ycbcr;\r
107         case PIX_FMT_YUV420P:           return core::pixel_format::ycbcr;\r
108         case PIX_FMT_YUV411P:           return core::pixel_format::ycbcr;\r
109         case PIX_FMT_YUV410P:           return core::pixel_format::ycbcr;\r
110         case PIX_FMT_YUVA420P:          return core::pixel_format::ycbcra;\r
111         default:                                        return core::pixel_format::invalid;\r
112         }\r
113 }\r
114 \r
115 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, int width, int height)\r
116 {\r
117         // Get linesizes\r
118         AVPicture dummy_pict;   \r
119         avpicture_fill(&dummy_pict, nullptr, pix_fmt == CASPAR_PIX_FMT_LUMA ? PIX_FMT_GRAY8 : pix_fmt, width, height);\r
120 \r
121         core::pixel_format_desc desc;\r
122         desc.pix_fmt = get_pixel_format(pix_fmt);\r
123                 \r
124         switch(desc.pix_fmt)\r
125         {\r
126         case core::pixel_format::gray:\r
127         case core::pixel_format::luma:\r
128                 {\r
129                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));                                               \r
130                         return desc;\r
131                 }\r
132         case core::pixel_format::bgra:\r
133         case core::pixel_format::argb:\r
134         case core::pixel_format::rgba:\r
135         case core::pixel_format::abgr:\r
136                 {\r
137                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));                                             \r
138                         return desc;\r
139                 }\r
140         case core::pixel_format::ycbcr:\r
141         case core::pixel_format::ycbcra:\r
142                 {               \r
143                         // Find chroma height\r
144                         int size2 = static_cast<int>(dummy_pict.data[2] - dummy_pict.data[1]);\r
145                         int h2 = size2/dummy_pict.linesize[1];                  \r
146 \r
147                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));\r
148                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));\r
149                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));\r
150 \r
151                         if(desc.pix_fmt == core::pixel_format::ycbcra)                                          \r
152                                 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));       \r
153                         return desc;\r
154                 }               \r
155         default:                \r
156                 desc.pix_fmt = core::pixel_format::invalid;\r
157                 return desc;\r
158         }\r
159 }\r
160 \r
161 int make_alpha_format(int format)\r
162 {\r
163         switch(get_pixel_format(static_cast<PixelFormat>(format)))\r
164         {\r
165         case core::pixel_format::ycbcr:\r
166         case core::pixel_format::ycbcra:\r
167                 return CASPAR_PIX_FMT_LUMA;\r
168         default:\r
169                 return format;\r
170         }\r
171 }\r
172 \r
173 safe_ptr<core::write_frame> make_write_frame(const void* tag, const safe_ptr<AVFrame>& decoded_frame, const safe_ptr<core::frame_factory>& frame_factory, int flags)\r
174 {                       \r
175         static tbb::concurrent_unordered_map<int, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contexts_;\r
176         \r
177         if(decoded_frame->width < 1 || decoded_frame->height < 1)\r
178                 return make_safe<core::write_frame>(tag);\r
179 \r
180         const auto width  = decoded_frame->width;\r
181         const auto height = decoded_frame->height;\r
182         auto desc                 = get_pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);\r
183         \r
184         if(flags & core::frame_producer::ALPHA_ONLY_FLAG)\r
185                 desc = get_pixel_format_desc(static_cast<PixelFormat>(make_alpha_format(decoded_frame->format)), width, height);\r
186 \r
187         std::shared_ptr<core::write_frame> write;\r
188 \r
189         if(desc.pix_fmt == core::pixel_format::invalid)\r
190         {\r
191                 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);\r
192                 auto target_pix_fmt = PIX_FMT_BGRA;\r
193 \r
194                 if(pix_fmt == PIX_FMT_UYVY422)\r
195                         target_pix_fmt = PIX_FMT_YUV422P;\r
196                 else if(pix_fmt == PIX_FMT_YUYV422)\r
197                         target_pix_fmt = PIX_FMT_YUV422P;\r
198                 else if(pix_fmt == PIX_FMT_UYYVYY411)\r
199                         target_pix_fmt = PIX_FMT_YUV411P;\r
200                 else if(pix_fmt == PIX_FMT_YUV420P10)\r
201                         target_pix_fmt = PIX_FMT_YUV420P;\r
202                 else if(pix_fmt == PIX_FMT_YUV422P10)\r
203                         target_pix_fmt = PIX_FMT_YUV422P;\r
204                 else if(pix_fmt == PIX_FMT_YUV444P10)\r
205                         target_pix_fmt = PIX_FMT_YUV444P;\r
206                 \r
207                 auto target_desc = get_pixel_format_desc(target_pix_fmt, width, height);\r
208 \r
209                 write = frame_factory->create_frame(tag, target_desc);\r
210                 write->set_type(get_mode(*decoded_frame));\r
211 \r
212                 std::shared_ptr<SwsContext> sws_context;\r
213 \r
214                 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";\r
215 \r
216                 int key = ((width << 22) & 0xFFC00000) | ((height << 6) & 0x003FC000) | ((pix_fmt << 7) & 0x00007F00) | ((target_pix_fmt << 0) & 0x0000007F);\r
217                         \r
218                 auto& pool = sws_contexts_[key];\r
219                                                 \r
220                 if(!pool.try_pop(sws_context))\r
221                 {\r
222                         double param;\r
223                         sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, target_pix_fmt, SWS_BILINEAR, nullptr, nullptr, &param), sws_freeContext);\r
224                 }\r
225                         \r
226                 if(!sws_context)\r
227                 {\r
228                         BOOST_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") << \r
229                                                                         boost::errinfo_api_function("sws_getContext"));\r
230                 }       \r
231                 \r
232                 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);     \r
233                 avcodec_get_frame_defaults(av_frame.get());                     \r
234                 if(target_pix_fmt == PIX_FMT_BGRA)\r
235                 {\r
236                         auto size = avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width, height);\r
237                         CASPAR_VERIFY(size == write->image_data().size()); \r
238                 }\r
239                 else\r
240                 {\r
241                         av_frame->width  = width;\r
242                         av_frame->height = height;\r
243                         for(int n = 0; n < target_desc.planes.size(); ++n)\r
244                         {\r
245                                 av_frame->data[n]               = write->image_data(n).begin();\r
246                                 av_frame->linesize[n]   = target_desc.planes[n].linesize;\r
247                         }\r
248                 }\r
249 \r
250                 sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);      \r
251                 pool.push(sws_context);\r
252 \r
253                 write->commit();                \r
254         }\r
255         else\r
256         {\r
257                 write = frame_factory->create_frame(tag, desc);\r
258                 write->set_type(get_mode(*decoded_frame));\r
259 \r
260                 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)\r
261                 {\r
262                         auto plane            = desc.planes[n];\r
263                         auto result           = write->image_data(n).begin();\r
264                         auto decoded          = decoded_frame->data[n];\r
265                         auto decoded_linesize = decoded_frame->linesize[n];\r
266                         \r
267                         CASPAR_ASSERT(decoded);\r
268                         CASPAR_ASSERT(write->image_data(n).begin());\r
269 \r
270                         // Copy line by line since ffmpeg sometimes pads each line.\r
271                         tbb::parallel_for<int>(0, desc.planes[n].height, [&](int y)\r
272                         {\r
273                                 memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);\r
274                         });\r
275 \r
276                         write->commit(n);\r
277                 }\r
278         }\r
279 \r
280         if(decoded_frame->height == 480) // NTSC DV\r
281         {\r
282                 write->get_frame_transform().fill_translation[1] += 2.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
283                 write->get_frame_transform().fill_scale[1] = 1.0 - 6.0*1.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
284         }\r
285         \r
286         // Fix field-order if needed\r
287         if(write->get_type() == core::field_mode::lower && frame_factory->get_video_format_desc().field_mode == core::field_mode::upper)\r
288                 write->get_frame_transform().fill_translation[1] += 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
289         else if(write->get_type() == core::field_mode::upper && frame_factory->get_video_format_desc().field_mode == core::field_mode::lower)\r
290                 write->get_frame_transform().fill_translation[1] -= 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);\r
291 \r
292         return make_safe_ptr(write);\r
293 }\r
294 \r
295 bool is_sane_fps(AVRational time_base)\r
296 {\r
297         double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);\r
298         return fps > 20.0 && fps < 65.0;\r
299 }\r
300 \r
301 AVRational fix_time_base(AVRational time_base)\r
302 {\r
303         if(time_base.num == 1)\r
304                 time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(time_base.den)))-1));    \r
305                         \r
306         if(!is_sane_fps(time_base))\r
307         {\r
308                 auto tmp = time_base;\r
309                 tmp.den /= 2;\r
310                 if(is_sane_fps(tmp))\r
311                         time_base = tmp;\r
312         }\r
313 \r
314         return time_base;\r
315 }\r
316 \r
317 double read_fps(AVFormatContext& context, double fail_value)\r
318 {                                               \r
319         auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
320         auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);\r
321         \r
322         if(video_index > -1)\r
323         {\r
324                 const auto video_context = context.streams[video_index]->codec;\r
325                 const auto video_stream  = context.streams[video_index];\r
326                                                 \r
327                 AVRational time_base = video_context->time_base;\r
328 \r
329                 if(boost::filesystem::path(context.filename).extension().string() == ".flv")\r
330                 {\r
331                         try\r
332                         {\r
333                                 auto meta = read_flv_meta_info(context.filename);\r
334                                 return boost::lexical_cast<double>(meta["framerate"]);\r
335                         }\r
336                         catch(...)\r
337                         {\r
338                                 return 0.0;\r
339                         }\r
340                 }\r
341                 else\r
342                 {\r
343                         time_base.num *= video_context->ticks_per_frame;\r
344 \r
345                         if(!is_sane_fps(time_base))\r
346                         {                       \r
347                                 time_base = fix_time_base(time_base);\r
348 \r
349                                 if(!is_sane_fps(time_base) && audio_index > -1)\r
350                                 {\r
351                                         auto& audio_context = *context.streams[audio_index]->codec;\r
352                                         auto& audio_stream  = *context.streams[audio_index];\r
353 \r
354                                         double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);\r
355                                                                 \r
356                                         time_base.num = static_cast<int>(duration_sec*100000.0);\r
357                                         time_base.den = static_cast<int>(video_stream->nb_frames*100000);\r
358                                 }\r
359                         }\r
360                 }\r
361                 \r
362                 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);\r
363 \r
364                 double closest_fps = 0.0;\r
365                 for(int n = 0; n < core::video_format::count; ++n)\r
366                 {\r
367                         auto format = core::video_format_desc::get(static_cast<core::video_format::type>(n));\r
368 \r
369                         double diff1 = std::abs(format.fps - fps);\r
370                         double diff2 = std::abs(closest_fps - fps);\r
371 \r
372                         if(diff1 < diff2)\r
373                                 closest_fps = format.fps;\r
374                 }\r
375         \r
376                 return closest_fps;\r
377         }\r
378 \r
379         return fail_value;      \r
380 }\r
381 \r
382 void fix_meta_data(AVFormatContext& context)\r
383 {\r
384         auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
385 \r
386         if(video_index > -1)\r
387         {\r
388                 auto video_stream   = context.streams[video_index];\r
389                 auto video_context  = context.streams[video_index]->codec;\r
390                                                 \r
391                 if(boost::filesystem::path(context.filename).extension().string() == ".flv")\r
392                 {\r
393                         try\r
394                         {\r
395                                 auto meta = read_flv_meta_info(context.filename);\r
396                                 double fps = boost::lexical_cast<double>(meta["framerate"]);\r
397                                 video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);\r
398                         }\r
399                         catch(...){}\r
400                 }\r
401                 else\r
402                 {\r
403                         auto stream_time = video_stream->time_base;\r
404                         auto duration    = video_stream->duration;\r
405                         auto codec_time  = video_context->time_base;\r
406                         auto ticks               = video_context->ticks_per_frame;\r
407 \r
408                         if(video_stream->nb_frames == 0)\r
409                                 video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);     \r
410                 }\r
411         }\r
412 }\r
413 \r
414 safe_ptr<AVPacket> create_packet()\r
415 {\r
416         safe_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)\r
417         {\r
418                 av_free_packet(p);\r
419                 delete p;\r
420         });\r
421         \r
422         av_init_packet(packet.get());\r
423         return packet;\r
424 }\r
425 \r
426 safe_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index)\r
427 {       \r
428         AVCodec* decoder;\r
429         index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");\r
430         //if(strcmp(decoder->name, "prores") == 0 && decoder->next && strcmp(decoder->next->name, "prores_lgpl") == 0)\r
431         //      decoder = decoder->next;\r
432 \r
433         THROW_ON_ERROR2(tbb_avcodec_open(context.streams[index]->codec, decoder), "");\r
434         return safe_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);\r
435 }\r
436 \r
437 safe_ptr<AVFormatContext> open_input(const std::wstring& filename)\r
438 {\r
439         AVFormatContext* weak_context = nullptr;\r
440         THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr), filename);\r
441         safe_ptr<AVFormatContext> context(weak_context, av_close_input_file);                   \r
442         THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);\r
443         fix_meta_data(*context);\r
444         return context;\r
445 }\r
446 \r
447 std::wstring print_mode(int width, int height, double fps, bool interlaced)\r
448 {\r
449         std::wostringstream fps_ss;\r
450         fps_ss << std::fixed << std::setprecision(2) << (!interlaced ? fps : 2.0 * fps);\r
451 \r
452         return boost::lexical_cast<std::wstring>(width) + L"x" + boost::lexical_cast<std::wstring>(height) + (!interlaced ? L"p" : L"i") + fps_ss.str();\r
453 }\r
454 \r
455 bool is_valid_file(const std::wstring filename)\r
456 {                       \r
457         std::ifstream file(filename);\r
458 \r
459         std::vector<unsigned char> buf;\r
460         for(auto file_it = std::istreambuf_iterator<char>(file); file_it != std::istreambuf_iterator<char>() && buf.size() < 2048; ++file_it)\r
461                 buf.push_back(*file_it);\r
462 \r
463         if(buf.empty())\r
464                 return nullptr;\r
465 \r
466         AVProbeData pb;\r
467         pb.filename = u8(filename).c_str();\r
468         pb.buf          = buf.data();\r
469         pb.buf_size = static_cast<int>(buf.size());\r
470 \r
471         int score = 0;\r
472         return av_probe_input_format2(&pb, true, &score) != nullptr;\r
473 }\r
474 \r
475 std::wstring probe_stem(const std::wstring stem)\r
476 {\r
477         auto stem2 = boost::filesystem::path(stem);\r
478         auto dir = stem2.parent_path();\r
479         for(auto it = boost::filesystem::directory_iterator(dir); it != boost::filesystem::directory_iterator(); ++it)\r
480         {\r
481                 if(boost::iequals(it->path().stem().wstring(), stem2.filename().wstring()) && is_valid_file(it->path().wstring()))\r
482                         return it->path().wstring();\r
483         }\r
484         return L"";\r
485 }\r
486 //\r
487 //void av_dup_frame(AVFrame* frame)\r
488 //{\r
489 //      AVFrame* new_frame = avcodec_alloc_frame();\r
490 //\r
491 //\r
492 //      const uint8_t *src_data[4] = {0};\r
493 //      memcpy(const_cast<uint8_t**>(&src_data[0]), frame->data, 4);\r
494 //      const int src_linesizes[4] = {0};\r
495 //      memcpy(const_cast<int*>(&src_linesizes[0]), frame->linesize, 4);\r
496 //\r
497 //      av_image_alloc(new_frame->data, new_frame->linesize, new_frame->width, new_frame->height, frame->format, 16);\r
498 //\r
499 //      av_image_copy(new_frame->data, new_frame->linesize, src_data, src_linesizes, frame->format, new_frame->width, new_frame->height);\r
500 //\r
501 //      frame =\r
502 //}\r
503 \r
504 }}