2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../StdAfx.h"
28 #include "../tbb_avcodec.h"
29 #include "../../ffmpeg_error.h"
31 #include <tbb/concurrent_unordered_map.h>
32 #include <tbb/concurrent_queue.h>
34 #include <core/frame/frame_transform.h>
35 #include <core/frame/frame_factory.h>
36 #include <core/frame/frame.h>
37 #include <core/producer/frame_producer.h>
39 #include <common/except.h>
40 #include <common/array.h>
41 #include <common/os/filesystem.h>
43 #include <tbb/parallel_for.h>
45 #include <common/assert.h>
46 #include <boost/filesystem.hpp>
47 #include <boost/lexical_cast.hpp>
48 #include <boost/rational.hpp>
55 #pragma warning (push)
56 #pragma warning (disable : 4244)
60 #include <libswscale/swscale.h>
61 #include <libavcodec/avcodec.h>
62 #include <libavformat/avformat.h>
68 namespace caspar { namespace ffmpeg {
70 core::field_mode get_mode(const AVFrame& frame)
72 if(!frame.interlaced_frame)
73 return core::field_mode::progressive;
75 return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;
78 core::pixel_format get_pixel_format(PixelFormat pix_fmt)
82 case PIX_FMT_GRAY8: return core::pixel_format::gray;
83 case PIX_FMT_RGB24: return core::pixel_format::rgb;
84 case PIX_FMT_BGR24: return core::pixel_format::bgr;
85 case PIX_FMT_BGRA: return core::pixel_format::bgra;
86 case PIX_FMT_ARGB: return core::pixel_format::argb;
87 case PIX_FMT_RGBA: return core::pixel_format::rgba;
88 case PIX_FMT_ABGR: return core::pixel_format::abgr;
89 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
90 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
91 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
92 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
93 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
94 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
95 default: return core::pixel_format::invalid;
99 core::pixel_format_desc pixel_format_desc(PixelFormat pix_fmt, int width, int height)
102 AVPicture dummy_pict;
103 avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);
105 core::pixel_format_desc desc = get_pixel_format(pix_fmt);
109 case core::pixel_format::gray:
110 case core::pixel_format::luma:
112 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
115 case core::pixel_format::bgr:
116 case core::pixel_format::rgb:
118 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/3, height, 3));
121 case core::pixel_format::bgra:
122 case core::pixel_format::argb:
123 case core::pixel_format::rgba:
124 case core::pixel_format::abgr:
126 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
129 case core::pixel_format::ycbcr:
130 case core::pixel_format::ycbcra:
132 // Find chroma height
133 int size2 = static_cast<int>(dummy_pict.data[2] - dummy_pict.data[1]);
134 int h2 = size2/dummy_pict.linesize[1];
136 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
137 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
138 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
140 if(desc.format == core::pixel_format::ycbcra)
141 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
145 desc.format = core::pixel_format::invalid;
150 core::mutable_frame make_frame(const void* tag, const spl::shared_ptr<AVFrame>& decoded_frame, double fps, core::frame_factory& frame_factory)
152 static tbb::concurrent_unordered_map<int64_t, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contvalid_exts_;
154 if(decoded_frame->width < 1 || decoded_frame->height < 1)
155 return frame_factory.create_frame(tag, core::pixel_format_desc(core::pixel_format::invalid));
157 const auto width = decoded_frame->width;
158 const auto height = decoded_frame->height;
159 auto desc = pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);
161 if(desc.format == core::pixel_format::invalid)
163 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);
164 auto target_pix_fmt = PIX_FMT_BGRA;
166 if(pix_fmt == PIX_FMT_UYVY422)
167 target_pix_fmt = PIX_FMT_YUV422P;
168 else if(pix_fmt == PIX_FMT_YUYV422)
169 target_pix_fmt = PIX_FMT_YUV422P;
170 else if(pix_fmt == PIX_FMT_UYYVYY411)
171 target_pix_fmt = PIX_FMT_YUV411P;
172 else if(pix_fmt == PIX_FMT_YUV420P10)
173 target_pix_fmt = PIX_FMT_YUV420P;
174 else if(pix_fmt == PIX_FMT_YUV422P10)
175 target_pix_fmt = PIX_FMT_YUV422P;
176 else if(pix_fmt == PIX_FMT_YUV444P10)
177 target_pix_fmt = PIX_FMT_YUV444P;
179 auto target_desc = pixel_format_desc(target_pix_fmt, width, height);
181 auto write = frame_factory.create_frame(tag, target_desc);
183 std::shared_ptr<SwsContext> sws_context;
185 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
187 int64_t key = ((static_cast<int64_t>(width) << 32) & 0xFFFF00000000) |
188 ((static_cast<int64_t>(height) << 16) & 0xFFFF0000) |
189 ((static_cast<int64_t>(pix_fmt) << 8) & 0xFF00) |
190 ((static_cast<int64_t>(target_pix_fmt) << 0) & 0xFF);
192 auto& pool = sws_contvalid_exts_[key];
194 if(!pool.try_pop(sws_context))
197 sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, target_pix_fmt, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
202 CASPAR_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") <<
203 boost::errinfo_api_function("sws_getContext"));
206 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
207 avcodec_get_frame_defaults(av_frame.get());
208 if(target_pix_fmt == PIX_FMT_BGRA)
210 auto size = avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write.image_data(0).begin(), PIX_FMT_BGRA, width, height);
211 CASPAR_VERIFY(size == write.image_data(0).size());
215 av_frame->width = width;
216 av_frame->height = height;
217 for(int n = 0; n < target_desc.planes.size(); ++n)
219 av_frame->data[n] = write.image_data(n).begin();
220 av_frame->linesize[n] = target_desc.planes[n].linesize;
224 sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);
225 pool.push(sws_context);
227 return std::move(write);
231 auto write = frame_factory.create_frame(tag, desc);
233 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)
235 auto plane = desc.planes[n];
236 auto result = write.image_data(n).begin();
237 auto decoded = decoded_frame->data[n];
238 auto decoded_linesize = decoded_frame->linesize[n];
240 CASPAR_ASSERT(decoded);
241 CASPAR_ASSERT(write.image_data(n).begin());
243 // Copy line by line since ffmpeg sometimes pads each line.
244 tbb::affinity_partitioner ap;
245 tbb::parallel_for(tbb::blocked_range<int>(0, desc.planes[n].height), [&](const tbb::blocked_range<int>& r)
247 for(int y = r.begin(); y != r.end(); ++y)
248 A_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
252 return std::move(write);
256 spl::shared_ptr<AVFrame> make_av_frame(core::mutable_frame& frame)
258 std::array<uint8_t*, 4> data = {};
259 for(int n = 0; n < frame.pixel_format_desc().planes.size(); ++n)
260 data[n] = frame.image_data(n).begin();
262 return make_av_frame(data, frame.pixel_format_desc());
265 spl::shared_ptr<AVFrame> make_av_frame(std::array<uint8_t*, 4> data, const core::pixel_format_desc& pix_desc)
267 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
268 avcodec_get_frame_defaults(av_frame.get());
270 auto planes = pix_desc.planes;
271 auto format = pix_desc.format;
273 av_frame->width = planes[0].width;
274 av_frame->height = planes[0].height;
275 for(int n = 0; n < planes.size(); ++n)
277 av_frame->data[n] = data[n];
278 av_frame->linesize[n] = planes[n].linesize;
283 case core::pixel_format::rgb:
284 av_frame->format = PIX_FMT_RGB24;
286 case core::pixel_format::bgr:
287 av_frame->format = PIX_FMT_BGR24;
289 case core::pixel_format::rgba:
290 av_frame->format = PIX_FMT_RGBA;
292 case core::pixel_format::argb:
293 av_frame->format = PIX_FMT_ARGB;
295 case core::pixel_format::bgra:
296 av_frame->format = PIX_FMT_BGRA;
298 case core::pixel_format::abgr:
299 av_frame->format = PIX_FMT_ABGR;
301 case core::pixel_format::gray:
302 av_frame->format = PIX_FMT_GRAY8;
304 case core::pixel_format::ycbcr:
306 int y_w = planes[0].width;
307 int y_h = planes[0].height;
308 int c_w = planes[1].width;
309 int c_h = planes[1].height;
311 if(c_h == y_h && c_w == y_w)
312 av_frame->format = PIX_FMT_YUV444P;
313 else if(c_h == y_h && c_w*2 == y_w)
314 av_frame->format = PIX_FMT_YUV422P;
315 else if(c_h == y_h && c_w*4 == y_w)
316 av_frame->format = PIX_FMT_YUV411P;
317 else if(c_h*2 == y_h && c_w*2 == y_w)
318 av_frame->format = PIX_FMT_YUV420P;
319 else if(c_h*2 == y_h && c_w*4 == y_w)
320 av_frame->format = PIX_FMT_YUV410P;
324 case core::pixel_format::ycbcra:
325 av_frame->format = PIX_FMT_YUVA420P;
331 bool is_sane_fps(AVRational time_base)
333 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
334 return fps > 20.0 && fps < 65.0;
337 AVRational fix_time_base(AVRational time_base)
339 if(time_base.num == 1)
340 time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(time_base.den)))-1));
342 if(!is_sane_fps(time_base))
344 auto tmp = time_base;
353 double read_fps(AVFormatContext& context, double fail_value)
355 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
356 auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);
360 const auto video_context = context.streams[video_index]->codec;
361 const auto video_stream = context.streams[video_index];
363 AVRational time_base = video_context->time_base;
365 if(boost::filesystem::path(context.filename).extension().string() == ".flv")
369 auto meta = read_flv_meta_info(context.filename);
370 return boost::lexical_cast<double>(meta["framerate"]);
379 time_base.num *= video_context->ticks_per_frame;
381 if(!is_sane_fps(time_base))
383 time_base = fix_time_base(time_base);
385 if(!is_sane_fps(time_base) && audio_index > -1)
387 auto& audio_context = *context.streams[audio_index]->codec;
388 auto& audio_stream = *context.streams[audio_index];
390 double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);
392 time_base.num = static_cast<int>(duration_sec*100000.0);
393 time_base.den = static_cast<int>(video_stream->nb_frames*100000);
398 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
400 double closest_fps = 0.0;
402 for (auto video_mode : enum_constants<core::video_format>())
404 auto format = core::video_format_desc(core::video_format(video_mode));
406 double diff1 = std::abs(format.fps - fps);
407 double diff2 = std::abs(closest_fps - fps);
410 closest_fps = format.fps;
419 void fix_meta_data(AVFormatContext& context)
421 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
425 auto video_stream = context.streams[video_index];
426 auto video_context = context.streams[video_index]->codec;
428 if(boost::filesystem::path(context.filename).extension().string() == ".flv")
432 auto meta = read_flv_meta_info(context.filename);
433 double fps = boost::lexical_cast<double>(meta["framerate"]);
434 video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);
440 auto stream_time = video_stream->time_base;
441 auto duration = video_stream->duration;
442 auto codec_time = video_context->time_base;
443 auto ticks = video_context->ticks_per_frame;
445 if(video_stream->nb_frames == 0)
446 video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);
451 spl::shared_ptr<AVPacket> create_packet()
453 spl::shared_ptr<AVPacket> packet(new AVPacket(), [](AVPacket* p)
459 av_init_packet(packet.get());
463 spl::shared_ptr<AVFrame> create_frame()
465 spl::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);
466 avcodec_get_frame_defaults(frame.get());
470 spl::shared_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index)
473 index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");
474 //if(strcmp(decoder->name, "prores") == 0 && decoder->next && strcmp(decoder->next->name, "prores_lgpl") == 0)
475 // decoder = decoder->next;
477 THROW_ON_ERROR2(tbb_avcodec_open(context.streams[index]->codec, decoder), "");
478 return spl::shared_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);
481 spl::shared_ptr<AVFormatContext> open_input(const std::wstring& filename)
483 AVFormatContext* weak_context = nullptr;
484 THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr), filename);
485 spl::shared_ptr<AVFormatContext> context(weak_context, av_close_input_file);
486 THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);
487 fix_meta_data(*context);
491 std::wstring print_mode(int width, int height, double fps, bool interlaced)
493 std::wostringstream fps_ss;
494 fps_ss << std::fixed << std::setprecision(2) << (!interlaced ? fps : 2.0 * fps);
496 return boost::lexical_cast<std::wstring>(width) + L"x" + boost::lexical_cast<std::wstring>(height) + (!interlaced ? L"p" : L"i") + fps_ss.str();
499 bool is_valid_file(const std::wstring& filename)
501 static const auto invalid_exts = {
517 static const auto valid_exts = {
531 auto ext = boost::to_lower_copy(boost::filesystem::path(filename).extension().wstring());
533 if(std::find(valid_exts.begin(), valid_exts.end(), ext) != valid_exts.end())
536 if(std::find(invalid_exts.begin(), invalid_exts.end(), ext) != invalid_exts.end())
539 auto u8filename = u8(filename);
543 pb.filename = u8filename.c_str();
545 if(av_probe_input_format2(&pb, false, &score) != nullptr)
548 std::ifstream file(u8filename);
550 std::vector<unsigned char> buf;
551 for(auto file_it = std::istreambuf_iterator<char>(file); file_it != std::istreambuf_iterator<char>() && buf.size() < 1024; ++file_it)
552 buf.push_back(*file_it);
558 pb.buf_size = static_cast<int>(buf.size());
560 return av_probe_input_format2(&pb, true, &score) != nullptr;
563 bool try_get_duration(const std::wstring filename, std::int64_t& duration, boost::rational<std::int64_t>& time_base)
565 AVFormatContext* weak_context = nullptr;
566 if (avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr) < 0)
569 std::shared_ptr<AVFormatContext> context(weak_context, av_close_input_file);
571 context->probesize = context->probesize / 10;
572 context->max_analyze_duration = context->probesize / 10;
574 if (avformat_find_stream_info(context.get(), nullptr) < 0)
577 const auto fps = read_fps(*context, 1.0);
579 const auto rational_fps = boost::rational<std::int64_t>(static_cast<int>(fps * AV_TIME_BASE), AV_TIME_BASE);
581 duration = boost::rational_cast<std::int64_t>(context->duration * rational_fps / AV_TIME_BASE);
583 if (rational_fps == 0)
586 time_base = 1 / rational_fps;
591 std::wstring probe_stem(const std::wstring& stem)
593 auto stem2 = boost::filesystem::path(stem);
594 auto parent = find_case_insensitive(stem2.parent_path().wstring());
599 auto dir = boost::filesystem::path(*parent);
601 for(auto it = boost::filesystem::directory_iterator(dir); it != boost::filesystem::directory_iterator(); ++it)
603 if(boost::iequals(it->path().stem().wstring(), stem2.filename().wstring()) && is_valid_file(it->path().wstring()))
604 return it->path().wstring();
609 //void av_dup_frame(AVFrame* frame)
611 // AVFrame* new_frame = avcodec_alloc_frame();
614 // const uint8_t *src_data[4] = {0};
615 // memcpy(const_cast<uint8_t**>(&src_data[0]), frame->data, 4);
616 // const int src_linesizes[4] = {0};
617 // memcpy(const_cast<int*>(&src_linesizes[0]), frame->linesize, 4);
619 // av_image_alloc(new_frame->data, new_frame->linesize, new_frame->width, new_frame->height, frame->format, 16);
621 // av_image_copy(new_frame->data, new_frame->linesize, src_data, src_linesizes, frame->format, new_frame->width, new_frame->height);