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 "../util.h"
\r
25 #include "../../ffmpeg_error.h"
\r
26 #include "../filter/filter.h"
\r
28 #include <common/memory/memcpy.h>
\r
30 #include <core/video_format.h>
\r
31 #include <core/producer/frame/basic_frame.h>
\r
32 #include <core/mixer/write_frame.h>
\r
33 #include <core/producer/frame/image_transform.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 <libavformat/avformat.h>
\r
49 #include <libavcodec/avcodec.h>
\r
51 #if defined(_MSC_VER)
\r
52 #pragma warning (pop)
\r
57 struct video_decoder::implementation : boost::noncopyable
\r
60 const safe_ptr<core::frame_factory> frame_factory_;
\r
61 AVCodecContext& codec_context_;
\r
62 size_t frame_number_;
\r
64 std::shared_ptr<filter> filter_;
\r
67 std::string filter_str_;
\r
70 explicit implementation(input& input, const safe_ptr<core::frame_factory>& frame_factory, const std::string& filter_str)
\r
72 , frame_factory_(frame_factory)
\r
73 , codec_context_(*input_.get_video_codec_context())
\r
75 , filter_(filter_str.empty() ? nullptr : new filter(filter_str))
\r
76 , filter_str_(filter_str)
\r
77 , eof_count_(std::numeric_limits<int>::max())
\r
81 std::deque<std::pair<int, safe_ptr<core::write_frame>>> receive()
\r
83 std::deque<std::pair<int, safe_ptr<core::write_frame>>> result;
\r
85 std::shared_ptr<AVPacket> pkt;
\r
86 for(int n = 0; n < 32 && result.empty() && input_.try_pop_video_packet(pkt); ++n)
\r
87 boost::range::push_back(result, decode(pkt));
\r
92 std::deque<std::pair<int, safe_ptr<core::write_frame>>> decode(const std::shared_ptr<AVPacket>& video_packet)
\r
94 std::deque<std::pair<int, safe_ptr<core::write_frame>>> result;
\r
96 if(!video_packet) // eof
\r
98 eof_count_ = frame_number_ + (filter_ ? filter_->delay()+1 : 0);
\r
99 avcodec_flush_buffers(&codec_context_);
\r
103 frame_number_ = frame_number_ % eof_count_;
\r
105 const void* tag = this;
\r
109 std::shared_ptr<AVFrame> frame;
\r
111 tbb::parallel_invoke(
\r
114 frame = decode_frame(video_packet);
\r
118 boost::range::transform(filter_->poll(), std::back_inserter(result), [&](const safe_ptr<AVFrame>& frame)
\r
120 return std::make_pair(frame_number_, make_write_frame(tag, frame, frame_factory_));
\r
123 if(!result.empty())
\r
128 filter_->push(make_safe(frame));
\r
132 auto frame = decode_frame(video_packet);
\r
135 result.push_back(std::make_pair(frame_number_++, make_write_frame(tag, make_safe(frame), frame_factory_)));
\r
141 std::shared_ptr<AVFrame> decode_frame(const std::shared_ptr<AVPacket>& video_packet)
\r
143 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
145 int frame_finished = 0;
\r
146 const int errn = avcodec_decode_video2(&codec_context_, decoded_frame.get(), &frame_finished, video_packet.get());
\r
150 BOOST_THROW_EXCEPTION(
\r
151 invalid_operation() <<
\r
152 msg_info(av_error_str(errn)) <<
\r
153 boost::errinfo_api_function("avcodec_decode_video") <<
\r
154 boost::errinfo_errno(AVUNERROR(errn)));
\r
157 if(frame_finished == 0)
\r
158 decoded_frame = nullptr;
\r
160 return decoded_frame;
\r
164 video_decoder::video_decoder(input& input, const safe_ptr<core::frame_factory>& frame_factory, const std::string& filter_str) : impl_(new implementation(input, frame_factory, filter_str)){}
\r
165 std::deque<std::pair<int, safe_ptr<core::write_frame>>> video_decoder::receive(){return impl_->receive();}
\r