1 #include "../../stdafx.h"
\r
7 #include "../../ffmpeg_error.h"
\r
9 #include <tbb/concurrent_unordered_map.h>
\r
10 #include <tbb/concurrent_queue.h>
\r
12 #include <core/producer/frame/frame_transform.h>
\r
13 #include <core/producer/frame/frame_factory.h>
\r
14 #include <core/producer/frame_producer.h>
\r
15 #include <core/mixer/write_frame.h>
\r
17 #include <common/exception/exceptions.h>
\r
18 #include <common/utility/assert.h>
\r
19 #include <common/memory/memcpy.h>
\r
21 #include <tbb/parallel_for.h>
\r
23 #include <boost/filesystem.hpp>
\r
24 #include <boost/lexical_cast.hpp>
\r
26 #if defined(_MSC_VER)
\r
27 #pragma warning (push)
\r
28 #pragma warning (disable : 4244)
\r
32 #include <libswscale/swscale.h>
\r
33 #include <libavcodec/avcodec.h>
\r
34 #include <libavformat/avformat.h>
\r
36 #if defined(_MSC_VER)
\r
37 #pragma warning (pop)
\r
40 namespace caspar { namespace ffmpeg {
\r
42 safe_ptr<AVPacket> flush_packet()
\r
44 static auto packet = create_packet();
\r
48 std::shared_ptr<core::audio_buffer> flush_audio()
\r
50 static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());
\r
54 std::shared_ptr<core::audio_buffer> empty_audio()
\r
56 static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());
\r
60 std::shared_ptr<AVFrame> flush_video()
\r
62 static std::shared_ptr<AVFrame> video(avcodec_alloc_frame(), av_free);
\r
66 std::shared_ptr<AVFrame> empty_video()
\r
68 static std::shared_ptr<AVFrame> video(avcodec_alloc_frame(), av_free);
\r
72 core::field_mode::type get_mode(const AVFrame& frame)
\r
74 if(!frame.interlaced_frame)
\r
75 return core::field_mode::progressive;
\r
77 return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;
\r
80 core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
\r
84 case CASPAR_PIX_FMT_LUMA: return core::pixel_format::luma;
\r
85 case PIX_FMT_GRAY8: return core::pixel_format::gray;
\r
86 case PIX_FMT_BGRA: return core::pixel_format::bgra;
\r
87 case PIX_FMT_ARGB: return core::pixel_format::argb;
\r
88 case PIX_FMT_RGBA: return core::pixel_format::rgba;
\r
89 case PIX_FMT_ABGR: return core::pixel_format::abgr;
\r
90 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
\r
91 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
\r
92 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
\r
93 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
\r
94 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
\r
95 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
\r
96 default: return core::pixel_format::invalid;
\r
100 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)
\r
103 AVPicture dummy_pict;
\r
104 avpicture_fill(&dummy_pict, nullptr, pix_fmt == CASPAR_PIX_FMT_LUMA ? PIX_FMT_GRAY8 : pix_fmt, width, height);
\r
106 core::pixel_format_desc desc;
\r
107 desc.pix_fmt = get_pixel_format(pix_fmt);
\r
109 switch(desc.pix_fmt)
\r
111 case core::pixel_format::gray:
\r
112 case core::pixel_format::luma:
\r
114 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
117 case core::pixel_format::bgra:
\r
118 case core::pixel_format::argb:
\r
119 case core::pixel_format::rgba:
\r
120 case core::pixel_format::abgr:
\r
122 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
\r
125 case core::pixel_format::ycbcr:
\r
126 case core::pixel_format::ycbcra:
\r
128 // Find chroma height
\r
129 size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];
\r
130 size_t h2 = size2/dummy_pict.linesize[1];
\r
132 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
133 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
\r
134 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
\r
136 if(desc.pix_fmt == core::pixel_format::ycbcra)
\r
137 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
\r
141 desc.pix_fmt = core::pixel_format::invalid;
\r
146 int make_alpha_format(int format)
\r
148 switch(get_pixel_format(static_cast<PixelFormat>(format)))
\r
150 case core::pixel_format::ycbcr:
\r
151 case core::pixel_format::ycbcra:
\r
152 return CASPAR_PIX_FMT_LUMA;
\r
158 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
160 static tbb::concurrent_unordered_map<size_t, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contexts_;
\r
162 if(decoded_frame->width < 1 || decoded_frame->height < 1)
\r
163 return make_safe<core::write_frame>(tag);
\r
165 const auto width = decoded_frame->width;
\r
166 const auto height = decoded_frame->height;
\r
167 auto desc = get_pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);
\r
169 if(hints & core::frame_producer::ALPHA_HINT)
\r
170 desc = get_pixel_format_desc(static_cast<PixelFormat>(make_alpha_format(decoded_frame->format)), width, height);
\r
172 std::shared_ptr<core::write_frame> write;
\r
174 if(desc.pix_fmt == core::pixel_format::invalid)
\r
176 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);
\r
178 write = frame_factory->create_frame(tag, get_pixel_format_desc(PIX_FMT_BGRA, width, height));
\r
179 write->set_type(get_mode(*decoded_frame));
\r
181 std::shared_ptr<SwsContext> sws_context;
\r
183 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
\r
185 size_t key = width << 20 | height << 8 | pix_fmt;
\r
187 auto& pool = sws_contexts_[key];
\r
189 if(!pool.try_pop(sws_context))
\r
192 sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
\r
197 BOOST_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") <<
\r
198 boost::errinfo_api_function("sws_getContext"));
\r
201 // Use sws_scale when provided colorspace has no hw-accel.
\r
202 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
203 avcodec_get_frame_defaults(av_frame.get());
\r
204 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width, height);
\r
206 sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);
\r
207 pool.push(sws_context);
\r
213 write = frame_factory->create_frame(tag, desc);
\r
214 write->set_type(get_mode(*decoded_frame));
\r
216 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)
\r
218 auto plane = desc.planes[n];
\r
219 auto result = write->image_data(n).begin();
\r
220 auto decoded = decoded_frame->data[n];
\r
221 auto decoded_linesize = decoded_frame->linesize[n];
\r
223 CASPAR_ASSERT(decoded);
\r
224 CASPAR_ASSERT(write->image_data(n).begin());
\r
226 if(decoded_linesize != static_cast<int>(plane.width))
\r
228 // Copy line by line since ffmpeg sometimes pads each line.
\r
229 tbb::parallel_for<size_t>(0, desc.planes[n].height, [&](size_t y)
\r
231 fast_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
236 fast_memcpy(result, decoded, plane.size);
\r
243 if(decoded_frame->height == 480) // NTSC DV
\r
245 write->get_frame_transform().fill_translation[1] += 2.0/static_cast<double>(frame_factory->get_video_format_desc().height);
\r
246 write->get_frame_transform().fill_scale[1] = 1.0 - 6.0*1.0/static_cast<double>(frame_factory->get_video_format_desc().height);
\r
249 // Fix field-order if needed
\r
250 if(write->get_type() == core::field_mode::lower && frame_factory->get_video_format_desc().field_mode == core::field_mode::upper)
\r
251 write->get_frame_transform().fill_translation[1] += 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);
\r
252 else if(write->get_type() == core::field_mode::upper && frame_factory->get_video_format_desc().field_mode == core::field_mode::lower)
\r
253 write->get_frame_transform().fill_translation[1] -= 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);
\r
255 return make_safe_ptr(write);
\r
258 bool is_sane_fps(AVRational time_base)
\r
260 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
\r
261 return fps > 20.0 && fps < 65.0;
\r
264 void fix_meta_data(AVFormatContext& context)
\r
266 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
\r
267 auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);
\r
269 if(video_index < 0)
\r
272 auto& video_context = *context.streams[video_index]->codec;
\r
273 auto& video_stream = *context.streams[video_index];
\r
275 if(boost::filesystem2::path(context.filename).extension() == ".flv")
\r
279 auto meta = read_flv_meta_info(context.filename);
\r
280 double fps = boost::lexical_cast<double>(meta["framerate"]);
\r
281 video_context.time_base.num = 1000000;
\r
282 video_context.time_base.den = static_cast<int>(fps*1000000.0);
\r
283 video_stream.nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);
\r
289 if(video_stream.nb_frames == 0)
\r
290 video_stream.nb_frames = video_stream.duration;
\r
292 if(!is_sane_fps(video_context.time_base))
\r
294 if(video_context.time_base.num == 1)
\r
295 video_context.time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(video_context.time_base.den)))-1));
\r
297 if(!is_sane_fps(video_context.time_base) && audio_index > -1)
\r
299 auto& audio_context = *context.streams[audio_index]->codec;
\r
300 auto& audio_stream = *context.streams[audio_index];
\r
302 double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);
\r
304 video_context.time_base.num = static_cast<int>(duration_sec*100000.0);
\r
305 video_context.time_base.den = static_cast<int>(video_stream.nb_frames*100000);
\r
309 if(audio_index > -1) // Check for invalid double frame-rate
\r
311 auto& audio_context = *context.streams[audio_index]->codec;
\r
312 auto& audio_stream = *context.streams[audio_index];
\r
314 double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);
\r
315 double fps = static_cast<double>(video_context.time_base.den) / static_cast<double>(video_context.time_base.num);
\r
317 double fps_nb_frames = static_cast<double>(duration_sec*fps);
\r
318 double stream_nb_frames = static_cast<double>(video_stream.nb_frames);
\r
319 double diff = std::abs(fps_nb_frames - stream_nb_frames*2.0);
\r
320 if(diff < fps_nb_frames*0.05)
\r
321 video_context.time_base.num *= 2;
\r
325 video_context.time_base.den = video_stream.r_frame_rate.num;
\r
326 video_context.time_base.num = video_stream.r_frame_rate.den;
\r
330 double fps = static_cast<double>(video_context.time_base.den) / static_cast<double>(video_context.time_base.num);
\r
332 double closest_fps = 0.0;
\r
333 for(int n = 0; n < core::video_format::count; ++n)
\r
335 auto format = core::video_format_desc::get(static_cast<core::video_format::type>(n));
\r
337 double diff1 = std::abs(format.fps - fps);
\r
338 double diff2 = std::abs(closest_fps - fps);
\r
341 closest_fps = format.fps;
\r
344 video_context.time_base.num = 1000000;
\r
345 video_context.time_base.den = static_cast<int>(closest_fps*1000000.0);
\r
348 safe_ptr<AVPacket> create_packet()
\r
350 safe_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)
\r
356 av_init_packet(packet.get());
\r
360 safe_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index)
\r
363 index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");
\r
364 THROW_ON_ERROR2(avcodec_open(context.streams[index]->codec, decoder), "");
\r
365 return safe_ptr<AVCodecContext>(context.streams[index]->codec, avcodec_close);
\r
368 safe_ptr<AVFormatContext> open_input(const std::wstring& filename)
\r
370 AVFormatContext* weak_context = nullptr;
\r
371 THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(filename).c_str(), nullptr, nullptr), filename);
\r
372 safe_ptr<AVFormatContext> context(weak_context, av_close_input_file);
\r
373 THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);
\r
374 fix_meta_data(*context);
\r
378 //void av_dup_frame(AVFrame* frame)
\r
380 // AVFrame* new_frame = avcodec_alloc_frame();
\r
383 // const uint8_t *src_data[4] = {0};
\r
384 // memcpy(const_cast<uint8_t**>(&src_data[0]), frame->data, 4);
\r
385 // const int src_linesizes[4] = {0};
\r
386 // memcpy(const_cast<int*>(&src_linesizes[0]), frame->linesize, 4);
\r
388 // av_image_alloc(new_frame->data, new_frame->linesize, new_frame->width, new_frame->height, frame->format, 16);
\r
390 // av_image_copy(new_frame->data, new_frame->linesize, src_data, src_linesizes, frame->format, new_frame->width, new_frame->height);
\r