1 #include "../../stdafx.h"
\r
7 #include "../tbb_avcodec.h"
\r
8 #include "../../ffmpeg_error.h"
\r
10 #include <tbb/concurrent_unordered_map.h>
\r
11 #include <tbb/concurrent_queue.h>
\r
13 #include <core/producer/frame/frame_transform.h>
\r
14 #include <core/producer/frame/frame_factory.h>
\r
15 #include <core/producer/frame_producer.h>
\r
16 #include <core/mixer/write_frame.h>
\r
18 #include <common/exception/exceptions.h>
\r
19 #include <common/utility/assert.h>
\r
20 #include <common/memory/memcpy.h>
\r
22 #include <tbb/parallel_for.h>
\r
24 #include <boost/filesystem.hpp>
\r
25 #include <boost/lexical_cast.hpp>
\r
27 #if defined(_MSC_VER)
\r
28 #pragma warning (push)
\r
29 #pragma warning (disable : 4244)
\r
33 #include <libswscale/swscale.h>
\r
34 #include <libavcodec/avcodec.h>
\r
35 #include <libavformat/avformat.h>
\r
37 #if defined(_MSC_VER)
\r
38 #pragma warning (pop)
\r
41 namespace caspar { namespace ffmpeg {
\r
43 safe_ptr<AVPacket> flush_packet()
\r
45 static auto packet = create_packet();
\r
49 std::shared_ptr<core::audio_buffer> flush_audio()
\r
51 static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());
\r
55 std::shared_ptr<core::audio_buffer> empty_audio()
\r
57 static std::shared_ptr<core::audio_buffer> audio(new core::audio_buffer());
\r
61 std::shared_ptr<AVFrame> flush_video()
\r
63 static std::shared_ptr<AVFrame> video(avcodec_alloc_frame(), av_free);
\r
67 std::shared_ptr<AVFrame> empty_video()
\r
69 static std::shared_ptr<AVFrame> video(avcodec_alloc_frame(), av_free);
\r
73 core::field_mode::type get_mode(const AVFrame& frame)
\r
75 if(!frame.interlaced_frame)
\r
76 return core::field_mode::progressive;
\r
78 return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;
\r
81 core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
\r
85 case CASPAR_PIX_FMT_LUMA: return core::pixel_format::luma;
\r
86 case PIX_FMT_GRAY8: return core::pixel_format::gray;
\r
87 case PIX_FMT_BGRA: return core::pixel_format::bgra;
\r
88 case PIX_FMT_ARGB: return core::pixel_format::argb;
\r
89 case PIX_FMT_RGBA: return core::pixel_format::rgba;
\r
90 case PIX_FMT_ABGR: return core::pixel_format::abgr;
\r
91 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
\r
92 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
\r
93 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
\r
94 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
\r
95 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
\r
96 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
\r
97 default: return core::pixel_format::invalid;
\r
101 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)
\r
104 AVPicture dummy_pict;
\r
105 avpicture_fill(&dummy_pict, nullptr, pix_fmt == CASPAR_PIX_FMT_LUMA ? PIX_FMT_GRAY8 : pix_fmt, width, height);
\r
107 core::pixel_format_desc desc;
\r
108 desc.pix_fmt = get_pixel_format(pix_fmt);
\r
110 switch(desc.pix_fmt)
\r
112 case core::pixel_format::gray:
\r
113 case core::pixel_format::luma:
\r
115 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
118 case core::pixel_format::bgra:
\r
119 case core::pixel_format::argb:
\r
120 case core::pixel_format::rgba:
\r
121 case core::pixel_format::abgr:
\r
123 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
\r
126 case core::pixel_format::ycbcr:
\r
127 case core::pixel_format::ycbcra:
\r
129 // Find chroma height
\r
130 size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];
\r
131 size_t h2 = size2/dummy_pict.linesize[1];
\r
133 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
134 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
\r
135 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
\r
137 if(desc.pix_fmt == core::pixel_format::ycbcra)
\r
138 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
\r
142 desc.pix_fmt = core::pixel_format::invalid;
\r
147 int make_alpha_format(int format)
\r
149 switch(get_pixel_format(static_cast<PixelFormat>(format)))
\r
151 case core::pixel_format::ycbcr:
\r
152 case core::pixel_format::ycbcra:
\r
153 return CASPAR_PIX_FMT_LUMA;
\r
159 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
161 static tbb::concurrent_unordered_map<size_t, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contexts_;
\r
163 if(decoded_frame->width < 1 || decoded_frame->height < 1)
\r
164 return make_safe<core::write_frame>(tag);
\r
166 const auto width = decoded_frame->width;
\r
167 const auto height = decoded_frame->height;
\r
168 auto desc = get_pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);
\r
170 if(hints & core::frame_producer::ALPHA_HINT)
\r
171 desc = get_pixel_format_desc(static_cast<PixelFormat>(make_alpha_format(decoded_frame->format)), width, height);
\r
173 std::shared_ptr<core::write_frame> write;
\r
175 if(desc.pix_fmt == core::pixel_format::invalid)
\r
177 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);
\r
179 write = frame_factory->create_frame(tag, get_pixel_format_desc(PIX_FMT_BGRA, width, height));
\r
180 write->set_type(get_mode(*decoded_frame));
\r
182 std::shared_ptr<SwsContext> sws_context;
\r
184 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
\r
186 size_t key = width << 20 | height << 8 | pix_fmt;
\r
188 auto& pool = sws_contexts_[key];
\r
190 if(!pool.try_pop(sws_context))
\r
193 sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
\r
198 BOOST_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") <<
\r
199 boost::errinfo_api_function("sws_getContext"));
\r
202 // Use sws_scale when provided colorspace has no hw-accel.
\r
203 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
204 avcodec_get_frame_defaults(av_frame.get());
\r
205 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width, height);
\r
207 sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);
\r
208 pool.push(sws_context);
\r
214 write = frame_factory->create_frame(tag, desc);
\r
215 write->set_type(get_mode(*decoded_frame));
\r
217 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)
\r
219 auto plane = desc.planes[n];
\r
220 auto result = write->image_data(n).begin();
\r
221 auto decoded = decoded_frame->data[n];
\r
222 auto decoded_linesize = decoded_frame->linesize[n];
\r
224 CASPAR_ASSERT(decoded);
\r
225 CASPAR_ASSERT(write->image_data(n).begin());
\r
227 if(decoded_linesize != static_cast<int>(plane.width))
\r
229 // Copy line by line since ffmpeg sometimes pads each line.
\r
230 tbb::parallel_for<size_t>(0, desc.planes[n].height, [&](size_t y)
\r
232 fast_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
237 fast_memcpy(result, decoded, plane.size);
\r
244 if(decoded_frame->height == 480) // NTSC DV
\r
246 write->get_frame_transform().fill_translation[1] += 2.0/static_cast<double>(frame_factory->get_video_format_desc().height);
\r
247 write->get_frame_transform().fill_scale[1] = 1.0 - 6.0*1.0/static_cast<double>(frame_factory->get_video_format_desc().height);
\r
250 // Fix field-order if needed
\r
251 if(write->get_type() == core::field_mode::lower && frame_factory->get_video_format_desc().field_mode == core::field_mode::upper)
\r
252 write->get_frame_transform().fill_translation[1] += 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);
\r
253 else if(write->get_type() == core::field_mode::upper && frame_factory->get_video_format_desc().field_mode == core::field_mode::lower)
\r
254 write->get_frame_transform().fill_translation[1] -= 1.0/static_cast<double>(frame_factory->get_video_format_desc().height);
\r
256 return make_safe_ptr(write);
\r
259 bool is_sane_fps(AVRational time_base)
\r
261 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
\r
262 return fps > 20.0 && fps < 65.0;
\r
265 void fix_meta_data(AVFormatContext& context)
\r
267 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
\r
268 auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);
\r
270 if(video_index < 0)
\r
273 auto& video_context = *context.streams[video_index]->codec;
\r
274 auto& video_stream = *context.streams[video_index];
\r
276 if(boost::filesystem2::path(context.filename).extension() == ".flv")
\r
280 auto meta = read_flv_meta_info(context.filename);
\r
281 double fps = boost::lexical_cast<double>(meta["framerate"]);
\r
282 video_context.time_base.num = 1000000;
\r
283 video_context.time_base.den = static_cast<int>(fps*1000000.0);
\r
284 video_stream.nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);
\r
290 if(video_stream.nb_frames == 0)
\r
291 video_stream.nb_frames = video_stream.duration;
\r
293 if(!is_sane_fps(video_context.time_base))
\r
295 if(video_context.time_base.num == 1)
\r
296 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
298 if(!is_sane_fps(video_context.time_base) && audio_index > -1)
\r
300 auto& audio_context = *context.streams[audio_index]->codec;
\r
301 auto& audio_stream = *context.streams[audio_index];
\r
303 double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);
\r
305 video_context.time_base.num = static_cast<int>(duration_sec*100000.0);
\r
306 video_context.time_base.den = static_cast<int>(video_stream.nb_frames*100000);
\r
310 if(audio_index > -1) // Check for invalid double frame-rate
\r
312 auto& audio_context = *context.streams[audio_index]->codec;
\r
313 auto& audio_stream = *context.streams[audio_index];
\r
315 double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);
\r
316 double fps = static_cast<double>(video_context.time_base.den) / static_cast<double>(video_context.time_base.num);
\r
318 double fps_nb_frames = static_cast<double>(duration_sec*fps);
\r
319 double stream_nb_frames = static_cast<double>(video_stream.nb_frames);
\r
320 double diff = std::abs(fps_nb_frames - stream_nb_frames*2.0);
\r
321 if(diff < fps_nb_frames*0.05)
\r
322 video_context.time_base.num *= 2;
\r
326 video_context.time_base.den = video_stream.r_frame_rate.num;
\r
327 video_context.time_base.num = video_stream.r_frame_rate.den;
\r
331 double fps = static_cast<double>(video_context.time_base.den) / static_cast<double>(video_context.time_base.num);
\r
333 double closest_fps = 0.0;
\r
334 for(int n = 0; n < core::video_format::count; ++n)
\r
336 auto format = core::video_format_desc::get(static_cast<core::video_format::type>(n));
\r
338 double diff1 = std::abs(format.fps - fps);
\r
339 double diff2 = std::abs(closest_fps - fps);
\r
342 closest_fps = format.fps;
\r
345 video_context.time_base.num = 1000000;
\r
346 video_context.time_base.den = static_cast<int>(closest_fps*1000000.0);
\r
349 safe_ptr<AVPacket> create_packet()
\r
351 safe_ptr<AVPacket> packet(new AVPacket, [](AVPacket* p)
\r
357 av_init_packet(packet.get());
\r
361 safe_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index)
\r
364 index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");
\r
365 if(strcmp(decoder->name, "prores") == 0 && strcmp(decoder->next->name, "prores_lgpl") == 0)
\r
366 decoder = decoder->next;
\r
368 THROW_ON_ERROR2(tbb_avcodec_open(context.streams[index]->codec, decoder), "");
\r
369 return safe_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);
\r
372 safe_ptr<AVFormatContext> open_input(const std::wstring& filename)
\r
374 AVFormatContext* weak_context = nullptr;
\r
375 THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(filename).c_str(), nullptr, nullptr), filename);
\r
376 safe_ptr<AVFormatContext> context(weak_context, av_close_input_file);
\r
377 THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);
\r
378 fix_meta_data(*context);
\r
382 //void av_dup_frame(AVFrame* frame)
\r
384 // AVFrame* new_frame = avcodec_alloc_frame();
\r
387 // const uint8_t *src_data[4] = {0};
\r
388 // memcpy(const_cast<uint8_t**>(&src_data[0]), frame->data, 4);
\r
389 // const int src_linesizes[4] = {0};
\r
390 // memcpy(const_cast<int*>(&src_linesizes[0]), frame->linesize, 4);
\r
392 // av_image_alloc(new_frame->data, new_frame->linesize, new_frame->width, new_frame->height, frame->format, 16);
\r
394 // av_image_copy(new_frame->data, new_frame->linesize, src_data, src_linesizes, frame->format, new_frame->width, new_frame->height);
\r