1 #include "../../stdafx.h"
\r
5 #include <tbb/concurrent_unordered_map.h>
\r
6 #include <tbb/concurrent_queue.h>
\r
8 #include <core/producer/frame/image_transform.h>
\r
9 #include <core/producer/frame/frame_factory.h>
\r
10 #include <core/producer/frame_producer.h>
\r
11 #include <core/mixer/write_frame.h>
\r
13 #include <common/exception/exceptions.h>
\r
15 #include <tbb/parallel_for.h>
\r
17 #if defined(_MSC_VER)
\r
18 #pragma warning (push)
\r
19 #pragma warning (disable : 4244)
\r
23 #include <libswscale/swscale.h>
\r
24 #include <libavcodec/avcodec.h>
\r
26 #if defined(_MSC_VER)
\r
27 #pragma warning (pop)
\r
32 core::video_mode::type get_mode(AVFrame& frame)
\r
34 if(!frame.interlaced_frame)
\r
35 return core::video_mode::progressive;
\r
37 return frame.top_field_first ? core::video_mode::upper : core::video_mode::lower;
\r
40 core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
\r
44 case CASPAR_PIX_FMT_LUMA: return core::pixel_format::luma;
\r
45 case PIX_FMT_GRAY8: return core::pixel_format::gray;
\r
46 case PIX_FMT_BGRA: return core::pixel_format::bgra;
\r
47 case PIX_FMT_ARGB: return core::pixel_format::argb;
\r
48 case PIX_FMT_RGBA: return core::pixel_format::rgba;
\r
49 case PIX_FMT_ABGR: return core::pixel_format::abgr;
\r
50 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
\r
51 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
\r
52 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
\r
53 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
\r
54 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
\r
55 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
\r
56 default: return core::pixel_format::invalid;
\r
60 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)
\r
63 AVPicture dummy_pict;
\r
64 avpicture_fill(&dummy_pict, nullptr, pix_fmt == CASPAR_PIX_FMT_LUMA ? PIX_FMT_GRAY8 : pix_fmt, width, height);
\r
66 core::pixel_format_desc desc;
\r
67 desc.pix_fmt = get_pixel_format(pix_fmt);
\r
69 switch(desc.pix_fmt)
\r
71 case core::pixel_format::gray:
\r
72 case core::pixel_format::luma:
\r
74 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
77 case core::pixel_format::bgra:
\r
78 case core::pixel_format::argb:
\r
79 case core::pixel_format::rgba:
\r
80 case core::pixel_format::abgr:
\r
82 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
\r
85 case core::pixel_format::ycbcr:
\r
86 case core::pixel_format::ycbcra:
\r
88 // Find chroma height
\r
89 size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];
\r
90 size_t h2 = size2/dummy_pict.linesize[1];
\r
92 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
93 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
\r
94 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
\r
96 if(desc.pix_fmt == core::pixel_format::ycbcra)
\r
97 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
\r
101 desc.pix_fmt = core::pixel_format::invalid;
\r
106 int make_alpha_format(int format)
\r
108 switch(get_pixel_format(static_cast<PixelFormat>(format)))
\r
110 case core::pixel_format::luma:
\r
111 case core::pixel_format::gray:
\r
112 case core::pixel_format::invalid:
\r
114 case core::pixel_format::ycbcr:
\r
115 case core::pixel_format::ycbcra:
\r
116 return CASPAR_PIX_FMT_LUMA;
\r
118 return PIX_FMT_GRAY8;
\r
122 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
124 static tbb::concurrent_unordered_map<size_t, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contexts_;
\r
126 const auto width = decoded_frame->width;
\r
127 const auto height = decoded_frame->height;
\r
128 auto desc = get_pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);
\r
130 if(hints & core::frame_producer::ALPHA_HINT)
\r
131 desc = get_pixel_format_desc(static_cast<PixelFormat>(make_alpha_format(decoded_frame->format)), width, height);
\r
133 if(desc.pix_fmt == core::pixel_format::invalid)
\r
135 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);
\r
137 auto write = frame_factory->create_frame(tag, desc.pix_fmt != core::pixel_format::invalid ? desc : get_pixel_format_desc(PIX_FMT_BGRA, width, height));
\r
138 write->set_type(get_mode(*decoded_frame));
\r
140 std::shared_ptr<SwsContext> sws_context;
\r
142 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
\r
144 size_t key = width << 20 | height << 8 | pix_fmt;
\r
146 auto& pool = sws_contexts_[key];
\r
148 if(!pool.try_pop(sws_context))
\r
151 sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
\r
156 BOOST_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") <<
\r
157 boost::errinfo_api_function("sws_getContext"));
\r
160 // Use sws_scale when provided colorspace has no hw-accel.
\r
161 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
162 avcodec_get_frame_defaults(av_frame.get());
\r
163 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width, height);
\r
165 sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);
\r
166 pool.push(sws_context);
\r
174 auto write = frame_factory->create_frame(tag, desc);
\r
175 write->set_type(get_mode(*decoded_frame));
\r
177 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)
\r
179 auto plane = desc.planes[n];
\r
180 auto result = write->image_data(n).begin();
\r
181 auto decoded = decoded_frame->data[n];
\r
182 auto decoded_linesize = decoded_frame->linesize[n];
\r
184 // Copy line by line since ffmpeg sometimes pads each line.
\r
185 tbb::affinity_partitioner ap;
\r
186 tbb::parallel_for(tbb::blocked_range<size_t>(0, static_cast<int>(desc.planes[n].height)), [&](const tbb::blocked_range<size_t>& r)
\r
188 for(size_t y = r.begin(); y != r.end(); ++y)
\r
189 memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
195 //for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)
\r
197 // auto plane = desc.planes[n];
\r
198 // auto result = write->image_data(n).begin();
\r
199 // auto decoded = decoded_frame->data[n];
\r
200 // auto decoded_linesize = decoded_frame->linesize[n];
\r
202 // for(size_t y = 0; y < static_cast<int>(desc.planes[n].height); ++y)
\r
203 // fast_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
205 // write->commit(n);
\r