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 #if defined(_MSC_VER)
\r
21 #pragma warning (disable : 4244)
\r
24 #include "..\stdafx.h"
\r
27 #include "../ffmpeg_error.h"
\r
28 #include "../tbb_avcodec.h"
\r
30 #include <core/video_format.h>
\r
32 #include <common/concurrency/executor.h>
\r
33 #include <common/diagnostics/graph.h>
\r
35 #include <tbb/concurrent_queue.h>
\r
36 #include <tbb/atomic.h>
\r
38 #include <boost/range/algorithm.hpp>
\r
39 #include <boost/thread/condition_variable.hpp>
\r
40 #include <boost/thread/mutex.hpp>
\r
41 #include <boost/range/iterator_range.hpp>
\r
45 #define __STDC_CONSTANT_MACROS
\r
46 #define __STDC_LIMIT_MACROS
\r
47 #include <libavformat/avformat.h>
\r
52 static const size_t MAX_BUFFER_COUNT = 128;
\r
53 static const size_t MAX_BUFFER_SIZE = 32 * 1000000;
\r
55 struct input::implementation : boost::noncopyable
\r
57 safe_ptr<diagnostics::graph> graph_;
\r
59 std::shared_ptr<AVFormatContext> format_context_; // Destroy this last
\r
61 const std::wstring filename_;
\r
65 tbb::atomic<size_t> buffer_size_;
\r
66 boost::condition_variable cond_;
\r
67 boost::mutex mutex_;
\r
69 tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> buffer_;
\r
73 explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start)
\r
76 , filename_(filename)
\r
77 , executor_(print())
\r
78 , start_(std::max(start, 0))
\r
82 AVFormatContext* weak_format_context_ = nullptr;
\r
83 errn = avformat_open_input(&weak_format_context_, narrow(filename).c_str(), nullptr, nullptr);
\r
84 if(errn < 0 || weak_format_context_ == nullptr)
\r
86 BOOST_THROW_EXCEPTION(
\r
87 file_read_error() <<
\r
88 source_info(narrow(print())) <<
\r
89 msg_info(av_error_str(errn)) <<
\r
90 boost::errinfo_api_function("av_open_input_file") <<
\r
91 boost::errinfo_errno(AVUNERROR(errn)) <<
\r
92 boost::errinfo_file_name(narrow(filename)));
\r
95 format_context_.reset(weak_format_context_, av_close_input_file);
\r
97 errn = avformat_find_stream_info(format_context_.get(), nullptr);
\r
100 BOOST_THROW_EXCEPTION(
\r
101 file_read_error() <<
\r
102 source_info(narrow(print())) <<
\r
103 msg_info(av_error_str(errn)) <<
\r
104 boost::errinfo_api_function("av_find_stream_info") <<
\r
105 boost::errinfo_errno(AVUNERROR(errn)));
\r
109 seek_frame(start_);
\r
111 for(int n = 0; n < 16 && buffer_size_ < MAX_BUFFER_SIZE && buffer_.size() < MAX_BUFFER_COUNT; ++n)
\r
112 read_next_packet();
\r
114 buffer_.set_capacity(MAX_BUFFER_COUNT);
\r
116 graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f));
\r
117 graph_->set_color("buffer-count", diagnostics::color(0.2f, 0.8f, 1.0f));
\r
118 graph_->set_color("buffer-size", diagnostics::color(0.2f, 0.4f, 1.0f));
\r
120 executor_.begin_invoke([this]{read_file();});
\r
121 CASPAR_LOG(info) << print() << " Started.";
\r
128 std::shared_ptr<AVPacket> packet;
\r
129 buffer_.try_pop(packet);
\r
131 cond_.notify_all();
\r
134 bool try_pop(std::shared_ptr<AVPacket>& packet)
\r
136 bool result = buffer_.try_pop(packet);
\r
137 graph_->update_value("buffer-count", MAX_BUFFER_SIZE/static_cast<double>(buffer_.size()));
\r
141 buffer_size_ -= packet->size;
\r
142 graph_->update_value("buffer-size", MAX_BUFFER_SIZE/static_cast<double>(buffer_size_));
\r
143 cond_.notify_all();
\r
154 CASPAR_LOG(debug) << print() << " Stopping.";
\r
159 read_next_packet();
\r
160 executor_.begin_invoke([this]{read_file();});
\r
163 void read_next_packet()
\r
167 std::shared_ptr<AVPacket> read_packet(new AVPacket, [](AVPacket* p)
\r
172 av_init_packet(read_packet.get());
\r
174 const int errn = av_read_frame(format_context_.get(), read_packet.get()); // read_packet is only valid until next call of av_read_frame.
\r
175 if(is_eof(errn)) // Use av_dup_packet to extend its life.
\r
179 seek_frame(start_, AVSEEK_FLAG_BACKWARD);
\r
180 graph_->add_tag("seek");
\r
181 CASPAR_LOG(trace) << print() << " Received EOF. Looping.";
\r
186 CASPAR_LOG(trace) << print() << " Received EOF. Stopping.";
\r
191 BOOST_THROW_EXCEPTION(
\r
192 file_read_error() <<
\r
193 msg_info(av_error_str(errn)) <<
\r
194 source_info(narrow(print())) <<
\r
195 boost::errinfo_api_function("av_read_frame") <<
\r
196 boost::errinfo_errno(AVUNERROR(errn)));
\r
200 av_dup_packet(read_packet.get());
\r
202 // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
\r
203 auto size = read_packet->size;
\r
204 auto data = read_packet->data;
\r
206 read_packet = std::shared_ptr<AVPacket>(read_packet.get(), [=](AVPacket* pkt)
\r
208 read_packet->size = size;
\r
209 read_packet->data = data;
\r
212 graph_->update_value("buffer-count", MAX_BUFFER_SIZE/static_cast<double>(buffer_.size()));
\r
214 boost::unique_lock<boost::mutex> lock(mutex_);
\r
215 while(buffer_size_ > MAX_BUFFER_SIZE && buffer_.size() > MAX_BUFFER_COUNT)
\r
218 buffer_.push(read_packet);
\r
219 buffer_size_ += read_packet->size;
\r
221 graph_->update_value("buffer-size", MAX_BUFFER_SIZE/static_cast<double>(buffer_size_));
\r
227 CASPAR_LOG_CURRENT_EXCEPTION();
\r
232 void seek_frame(int64_t frame, int flags = 0)
\r
234 static const AVRational base_q = {1, AV_TIME_BASE};
\r
236 int stream_index = av_find_default_stream_index(format_context_.get());
\r
238 if(stream_index < 0)
\r
240 BOOST_THROW_EXCEPTION(
\r
241 invalid_operation() <<
\r
242 source_info(narrow(print())) <<
\r
243 msg_info(av_error_str(stream_index)) <<
\r
244 boost::errinfo_api_function("av_find_default_stream_index") <<
\r
245 boost::errinfo_errno(AVUNERROR(stream_index)));
\r
248 const int errn = av_seek_frame(format_context_.get(), stream_index, frame, flags);
\r
251 BOOST_THROW_EXCEPTION(
\r
252 invalid_operation() <<
\r
253 source_info(narrow(print())) <<
\r
254 msg_info(av_error_str(errn)) <<
\r
255 boost::errinfo_api_function("av_seek_frame") <<
\r
256 boost::errinfo_errno(AVUNERROR(errn)));
\r
259 buffer_.push(nullptr);
\r
262 bool is_eof(int errn)
\r
264 //if(errn == AVERROR(EIO))
\r
265 // CASPAR_LOG(warning) << print() << " Received EIO, assuming EOF.";
\r
266 //if(errn == AVERROR_EOF)
\r
267 // CASPAR_LOG(info) << print() << " Received EOF.";
\r
269 return errn == AVERROR_EOF || errn == AVERROR(EIO); // av_read_frame doesn't always correctly return AVERROR_EOF;
\r
272 std::wstring print() const
\r
274 return L"ffmpeg_input[" + filename_ + L")]";
\r
278 input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start, int length)
\r
279 : impl_(new implementation(graph, filename, loop, start)){}
\r
280 bool input::eof() const {return !impl_->executor_.is_running();}
\r
281 bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}
\r
282 std::shared_ptr<AVFormatContext> input::context(){return impl_->format_context_;}
\r