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
24 #include "../../util/util.h"
\r
25 #include "../../util/filter.h"
\r
27 #include <common/memory/memcpy.h>
\r
29 #include <core/video_format.h>
\r
30 #include <core/producer/frame/basic_frame.h>
\r
31 #include <core/mixer/write_frame.h>
\r
32 #include <core/producer/frame/image_transform.h>
\r
33 #include <core/producer/frame/pixel_format.h>
\r
34 #include <core/producer/frame/frame_factory.h>
\r
36 #include <tbb/parallel_for.h>
\r
38 #include <boost/range/algorithm_ext.hpp>
\r
40 #if defined(_MSC_VER)
\r
41 #pragma warning (push)
\r
42 #pragma warning (disable : 4244)
\r
46 #define __STDC_CONSTANT_MACROS
\r
47 #define __STDC_LIMIT_MACROS
\r
48 #include <libswscale/swscale.h>
\r
49 #include <libavformat/avformat.h>
\r
50 #include <libavcodec/avcodec.h>
\r
52 #if defined(_MSC_VER)
\r
53 #pragma warning (pop)
\r
58 struct video_decoder::implementation : boost::noncopyable
\r
61 std::shared_ptr<SwsContext> sws_context_;
\r
62 const safe_ptr<core::frame_factory> frame_factory_;
\r
63 AVCodecContext& codec_context_;
\r
66 const PixelFormat pix_fmt_;
\r
67 core::pixel_format_desc desc_;
\r
68 size_t frame_number_;
\r
69 std::shared_ptr<filter> filter_;
\r
72 explicit implementation(input& input, const safe_ptr<core::frame_factory>& frame_factory, const std::string& filter_str)
\r
74 , frame_factory_(frame_factory)
\r
75 , codec_context_(*input_.get_video_codec_context())
\r
76 , width_(codec_context_.width)
\r
77 , height_(codec_context_.height)
\r
78 , pix_fmt_(codec_context_.pix_fmt)
\r
79 , desc_(get_pixel_format_desc(pix_fmt_, width_, height_))
\r
82 if(desc_.pix_fmt == core::pixel_format::invalid)
\r
84 CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
\r
86 desc_ = get_pixel_format_desc(PIX_FMT_BGRA, width_, height_);
\r
88 sws_context_.reset(sws_getContext(width_, height_, pix_fmt_, width_, height_, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
\r
90 BOOST_THROW_EXCEPTION(operation_failed() <<
\r
91 msg_info("Could not create software scaling context.") <<
\r
92 boost::errinfo_api_function("sws_getContext"));
\r
95 if(!filter_str.empty())
\r
96 filter_.reset(new filter(filter_str));
\r
99 std::deque<std::pair<int, safe_ptr<core::write_frame>>> receive()
\r
101 std::deque<std::pair<int, safe_ptr<core::write_frame>>> result;
\r
103 std::shared_ptr<AVPacket> pkt;
\r
104 for(int n = 0; n < 32 && result.empty() && input_.try_pop_video_packet(pkt); ++n)
\r
105 boost::range::push_back(result, decode(pkt));
\r
110 std::deque<std::pair<int, safe_ptr<core::write_frame>>> decode(const std::shared_ptr<AVPacket>& video_packet)
\r
112 std::deque<std::pair<int, safe_ptr<core::write_frame>>> result;
\r
114 if(!video_packet) // eof
\r
116 avcodec_flush_buffers(&codec_context_);
\r
121 safe_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
123 int frame_finished = 0;
\r
124 const int errn = avcodec_decode_video2(&codec_context_, decoded_frame.get(), &frame_finished, video_packet.get());
\r
128 BOOST_THROW_EXCEPTION(
\r
129 invalid_operation() <<
\r
130 msg_info(av_error_str(errn)) <<
\r
131 boost::errinfo_api_function("avcodec_decode_video") <<
\r
132 boost::errinfo_errno(AVUNERROR(errn)));
\r
135 if(frame_finished != 0)
\r
139 filter_->push(decoded_frame);
\r
141 std::shared_ptr<AVFrame> frame;
\r
142 while(filter_->try_pop(frame))
\r
143 result.push_back(std::make_pair(frame_number_, make_write_frame(make_safe(frame))));
\r
146 result.push_back(std::make_pair(frame_number_, make_write_frame(decoded_frame)));
\r
148 if(!result.empty())
\r
151 CASPAR_LOG(trace) << " Filter delay.";
\r
154 CASPAR_LOG(trace) << " Decoder delay.";
\r
159 safe_ptr<core::write_frame> make_write_frame(safe_ptr<AVFrame> decoded_frame)
\r
161 auto write = frame_factory_->create_frame(this, desc_);
\r
162 write->set_is_interlaced(decoded_frame->interlaced_frame != 0);
\r
164 if(sws_context_ == nullptr)
\r
166 tbb::parallel_for(0, static_cast<int>(desc_.planes.size()), 1, [&](int n)
\r
168 auto plane = desc_.planes[n];
\r
169 auto result = write->image_data(n).begin();
\r
170 auto decoded = decoded_frame->data[n];
\r
171 auto decoded_linesize = decoded_frame->linesize[n];
\r
173 // Copy line by line since ffmpeg sometimes pads each line.
\r
174 tbb::parallel_for(tbb::blocked_range<size_t>(0, static_cast<int>(desc_.planes[n].height)), [&](const tbb::blocked_range<size_t>& r)
\r
176 for(size_t y = r.begin(); y != r.end(); ++y)
\r
177 memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
\r
185 // Use sws_scale when provided colorspace has no hw-accel.
\r
186 safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
187 avcodec_get_frame_defaults(av_frame.get());
\r
188 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width_, height_);
\r
190 sws_scale(sws_context_.get(), decoded_frame->data, decoded_frame->linesize, 0, height_, av_frame->data, av_frame->linesize);
\r
195 // DVVIDEO is in lower field. Make it upper field if needed.
\r
196 if(codec_context_.codec_id == CODEC_ID_DVVIDEO && frame_factory_->get_video_format_desc().mode == core::video_mode::upper)
\r
197 write->get_image_transform().set_fill_translation(0.0f, 0.5/static_cast<double>(height_));
\r
203 video_decoder::video_decoder(input& input, const safe_ptr<core::frame_factory>& frame_factory, const std::string& filter) : impl_(new implementation(input, frame_factory, filter)){}
\r
204 std::deque<std::pair<int, safe_ptr<core::write_frame>>> video_decoder::receive(){return impl_->receive();}
\r