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