2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
20 #include "../../stdafx.h"
\r
22 #include "video_decoder.h"
\r
23 #include "../../ffmpeg_error.h"
\r
25 #include <common/memory/memcpy.h>
\r
27 #include <core/video_format.h>
\r
28 #include <core/producer/frame/basic_frame.h>
\r
29 #include <core/mixer/write_frame.h>
\r
30 #include <core/producer/frame/image_transform.h>
\r
31 #include <core/producer/frame/pixel_format.h>
\r
32 #include <core/producer/frame/frame_factory.h>
\r
34 #include <tbb/parallel_for.h>
\r
36 #include <boost/range/algorithm_ext.hpp>
\r
38 #if defined(_MSC_VER)
\r
39 #pragma warning (push)
\r
40 #pragma warning (disable : 4244)
\r
44 #define __STDC_CONSTANT_MACROS
\r
45 #define __STDC_LIMIT_MACROS
\r
46 #include <libswscale/swscale.h>
\r
47 #include <libavformat/avformat.h>
\r
48 #include <libavcodec/avcodec.h>
\r
50 #if defined(_MSC_VER)
\r
51 #pragma warning (pop)
\r
56 core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)
\r
60 case PIX_FMT_GRAY8: return core::pixel_format::gray;
\r
61 case PIX_FMT_BGRA: return core::pixel_format::bgra;
\r
62 case PIX_FMT_ARGB: return core::pixel_format::argb;
\r
63 case PIX_FMT_RGBA: return core::pixel_format::rgba;
\r
64 case PIX_FMT_ABGR: return core::pixel_format::abgr;
\r
65 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
\r
66 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
\r
67 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
\r
68 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
\r
69 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
\r
70 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
\r
71 default: return core::pixel_format::invalid;
\r
75 core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)
\r
78 AVPicture dummy_pict;
\r
79 avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);
\r
81 core::pixel_format_desc desc;
\r
82 desc.pix_fmt = get_pixel_format(pix_fmt);
\r
84 switch(desc.pix_fmt)
\r
86 case core::pixel_format::gray:
\r
88 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 1));
\r
91 case core::pixel_format::bgra:
\r
92 case core::pixel_format::argb:
\r
93 case core::pixel_format::rgba:
\r
94 case core::pixel_format::abgr:
\r
96 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
\r
99 case core::pixel_format::ycbcr:
\r
100 case core::pixel_format::ycbcra:
\r
102 // Find chroma height
\r
103 size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];
\r
104 size_t h2 = size2/dummy_pict.linesize[1];
\r
106 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
\r
107 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
\r
108 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
\r
110 if(desc.pix_fmt == core::pixel_format::ycbcra)
\r
111 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
\r
115 desc.pix_fmt = core::pixel_format::invalid;
\r
120 struct video_decoder::implementation : boost::noncopyable
\r
123 std::shared_ptr<SwsContext> sws_context_;
\r
124 const std::shared_ptr<core::frame_factory> frame_factory_;
\r
125 AVCodecContext& codec_context_;
\r
128 const PixelFormat pix_fmt_;
\r
129 core::pixel_format_desc desc_;
\r
130 size_t frame_number_;
\r
133 explicit implementation(input& input, const safe_ptr<core::frame_factory>& frame_factory)
\r
135 , frame_factory_(frame_factory)
\r
136 , codec_context_(*input_.get_video_codec_context())
\r
137 , width_(codec_context_.width)
\r
138 , height_(codec_context_.height)
\r
139 , pix_fmt_(codec_context_.pix_fmt)
\r
140 , desc_(get_pixel_format_desc(pix_fmt_, width_, height_))
\r
143 if(desc_.pix_fmt == core::pixel_format::invalid)
\r
145 CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
\r
147 desc_ = get_pixel_format_desc(PIX_FMT_BGRA, width_, height_);
\r
149 sws_context_.reset(sws_getContext(width_, height_, pix_fmt_, width_, height_, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
\r
151 BOOST_THROW_EXCEPTION(operation_failed() <<
\r
152 msg_info("Could not create software scaling context.") <<
\r
153 boost::errinfo_api_function("sws_getContext"));
\r
157 std::deque<std::pair<int, safe_ptr<core::write_frame>>> receive()
\r
159 std::deque<std::pair<int, safe_ptr<core::write_frame>>> result;
\r
161 std::shared_ptr<AVPacket> pkt;
\r
162 for(int n = 0; n < 32 && result.empty() && input_.try_pop_video_packet(pkt); ++n)
\r
163 boost::range::push_back(result, decode(pkt));
\r
168 std::deque<std::pair<int, safe_ptr<core::write_frame>>> decode(const std::shared_ptr<AVPacket>& video_packet)
\r
170 std::deque<std::pair<int, safe_ptr<core::write_frame>>> result;
\r
172 if(!video_packet) // eof
\r
174 avcodec_flush_buffers(&codec_context_);
\r
179 safe_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
181 int frame_finished = 0;
\r
182 const int errn = avcodec_decode_video2(&codec_context_, decoded_frame.get(), &frame_finished, video_packet.get());
\r
186 BOOST_THROW_EXCEPTION(
\r
187 invalid_operation() <<
\r
188 msg_info(av_error_str(errn)) <<
\r
189 boost::errinfo_api_function("avcodec_decode_video") <<
\r
190 boost::errinfo_errno(AVUNERROR(errn)));
\r
193 if(frame_finished != 0)
\r
194 result.push_back(std::make_pair(frame_number_++, make_write_frame(decoded_frame)));
\r
199 safe_ptr<core::write_frame> make_write_frame(safe_ptr<AVFrame> decoded_frame)
\r
201 auto write = frame_factory_->create_frame(this, desc_);
\r
202 write->set_is_interlaced(decoded_frame->interlaced_frame != 0);
\r
204 if(sws_context_ == nullptr)
\r
206 tbb::parallel_for(0, static_cast<int>(desc_.planes.size()), 1, [&](int n)
\r
208 auto plane = desc_.planes[n];
\r
209 auto result = write->image_data(n).begin();
\r
210 auto decoded = decoded_frame->data[n];
\r
211 auto decoded_linesize = decoded_frame->linesize[n];
\r
213 // Copy line by line since ffmpeg sometimes pads each line.
\r
214 tbb::parallel_for(tbb::blocked_range<size_t>(0, static_cast<int>(desc_.planes[n].height)), [&](const tbb::blocked_range<size_t>& r)
\r
216 for(size_t y = r.begin(); y != r.end(); ++y)
\r
217 memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
225 // Use sws_scale when provided colorspace has no hw-accel.
\r
226 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
227 avcodec_get_frame_defaults(av_frame.get());
\r
228 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width_, height_);
\r
230 sws_scale(sws_context_.get(), decoded_frame->data, decoded_frame->linesize, 0, height_, av_frame->data, av_frame->linesize);
\r
235 // DVVIDEO is in lower field. Make it upper field if needed.
\r
236 if(codec_context_.codec_id == CODEC_ID_DVVIDEO && frame_factory_->get_video_format_desc().mode == core::video_mode::upper)
\r
237 write->get_image_transform().set_fill_translation(0.0f, 0.5/static_cast<double>(height_));
\r
243 video_decoder::video_decoder(input& input, const safe_ptr<core::frame_factory>& frame_factory) : impl_(new implementation(input, frame_factory)){}
\r
244 std::deque<std::pair<int, safe_ptr<core::write_frame>>> video_decoder::receive(){return impl_->receive();}
\r