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/diagnostics/graph.h>
\r
34 #include <tbb/concurrent_queue.h>
\r
35 #include <tbb/atomic.h>
\r
37 #include <boost/range/algorithm.hpp>
\r
38 #include <boost/thread/condition_variable.hpp>
\r
39 #include <boost/thread/mutex.hpp>
\r
40 #include <boost/thread/thread.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 std::shared_ptr<AVFormatContext> format_context_; // Destroy this last
\r
59 safe_ptr<diagnostics::graph> graph_;
\r
61 const std::wstring filename_;
\r
65 tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> buffer_;
\r
66 tbb::atomic<size_t> buffer_size_;
\r
67 boost::condition_variable buffer_cond_;
\r
68 boost::mutex buffer_mutex_;
\r
70 boost::thread thread_;
\r
71 tbb::atomic<bool> is_running_;
\r
73 explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start)
\r
76 , filename_(filename)
\r
77 , start_(std::max(start, 0))
\r
83 AVFormatContext* weak_format_context_ = nullptr;
\r
84 ret = avformat_open_input(&weak_format_context_, narrow(filename).c_str(), nullptr, nullptr);
\r
85 if(ret < 0 || weak_format_context_ == nullptr)
\r
87 BOOST_THROW_EXCEPTION(
\r
88 file_read_error() <<
\r
89 source_info(narrow(print())) <<
\r
90 msg_info(av_error_str(ret)) <<
\r
91 boost::errinfo_api_function("av_open_input_file") <<
\r
92 boost::errinfo_errno(AVUNERROR(ret)) <<
\r
93 boost::errinfo_file_name(narrow(filename)));
\r
96 format_context_.reset(weak_format_context_, av_close_input_file);
\r
98 ret = avformat_find_stream_info(format_context_.get(), nullptr);
\r
101 BOOST_THROW_EXCEPTION(
\r
102 file_read_error() <<
\r
103 source_info(narrow(print())) <<
\r
104 msg_info(av_error_str(ret)) <<
\r
105 boost::errinfo_api_function("av_find_stream_info") <<
\r
106 boost::errinfo_errno(AVUNERROR(ret)));
\r
110 seek_frame(start_);
\r
112 for(int n = 0; n < 16 && !full(); ++n)
\r
113 read_next_packet();
\r
115 graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f));
\r
116 graph_->set_color("buffer-count", diagnostics::color(0.2f, 0.8f, 1.0f));
\r
117 graph_->set_color("buffer-size", diagnostics::color(0.2f, 0.4f, 1.0f));
\r
119 thread_ = boost::thread([this]{run();});
\r
124 is_running_ = false;
\r
125 buffer_cond_.notify_all();
\r
129 bool try_pop(std::shared_ptr<AVPacket>& packet)
\r
131 const bool result = buffer_.try_pop(packet);
\r
136 buffer_size_ -= packet->size;
\r
137 buffer_cond_.notify_all();
\r
140 graph_->update_value("buffer-size", MAX_BUFFER_SIZE/static_cast<double>(buffer_size_));
\r
141 graph_->update_value("buffer-count", MAX_BUFFER_SIZE/static_cast<double>(buffer_.size()));
\r
150 caspar::win32_exception::install_handler();
\r
154 CASPAR_LOG(info) << print() << " Thread Started.";
\r
159 boost::unique_lock<boost::mutex> lock(buffer_mutex_);
\r
160 buffer_cond_.wait(lock, [this]{return !full();});
\r
162 read_next_packet();
\r
165 CASPAR_LOG(info) << print() << " Thread Stopped.";
\r
169 CASPAR_LOG_CURRENT_EXCEPTION();
\r
170 is_running_ = false;
\r
174 void read_next_packet()
\r
176 std::shared_ptr<AVPacket> read_packet(new AVPacket, [](AVPacket* p)
\r
181 av_init_packet(read_packet.get());
\r
183 const int ret = av_read_frame(format_context_.get(), read_packet.get()); // read_packet is only valid until next call of av_read_frame.
\r
184 if(is_eof(ret)) // Use av_dup_packet to extend its life.
\r
188 seek_frame(start_, AVSEEK_FLAG_BACKWARD);
\r
189 graph_->add_tag("seek");
\r
190 CASPAR_LOG(trace) << print() << " Received EOF. Looping.";
\r
194 is_running_ = false;
\r
195 CASPAR_LOG(trace) << print() << " Received EOF. Stopping.";
\r
200 BOOST_THROW_EXCEPTION(
\r
201 file_read_error() <<
\r
202 msg_info(av_error_str(ret)) <<
\r
203 source_info(narrow(print())) <<
\r
204 boost::errinfo_api_function("av_read_frame") <<
\r
205 boost::errinfo_errno(AVUNERROR(ret)));
\r
209 av_dup_packet(read_packet.get());
\r
211 // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
\r
212 auto size = read_packet->size;
\r
213 auto data = read_packet->data;
\r
215 read_packet = std::shared_ptr<AVPacket>(read_packet.get(), [=](AVPacket* pkt)
\r
217 read_packet->size = size;
\r
218 read_packet->data = data;
\r
221 buffer_.try_push(read_packet);
\r
222 buffer_size_ += read_packet->size;
\r
224 graph_->update_value("buffer-count", MAX_BUFFER_SIZE/static_cast<double>(buffer_.size()));
\r
225 graph_->update_value("buffer-size", MAX_BUFFER_SIZE/static_cast<double>(buffer_size_));
\r
231 return is_running_ && buffer_size_ > MAX_BUFFER_SIZE && buffer_.size() > MAX_BUFFER_COUNT;
\r
234 void seek_frame(int64_t frame, int flags = 0)
\r
236 static const AVRational base_q = {1, AV_TIME_BASE};
\r
238 int stream_index = av_find_default_stream_index(format_context_.get());
\r
240 if(stream_index < 0)
\r
242 BOOST_THROW_EXCEPTION(
\r
243 invalid_operation() <<
\r
244 source_info(narrow(print())) <<
\r
245 msg_info(av_error_str(stream_index)) <<
\r
246 boost::errinfo_api_function("av_find_default_stream_index") <<
\r
247 boost::errinfo_errno(AVUNERROR(stream_index)));
\r
250 const int ret = av_seek_frame(format_context_.get(), stream_index, frame, flags);
\r
253 BOOST_THROW_EXCEPTION(
\r
254 invalid_operation() <<
\r
255 source_info(narrow(print())) <<
\r
256 msg_info(av_error_str(ret)) <<
\r
257 boost::errinfo_api_function("av_seek_frame") <<
\r
258 boost::errinfo_errno(AVUNERROR(ret)));
\r
261 buffer_.push(nullptr);
\r
264 bool is_eof(int ret)
\r
266 if(ret == AVERROR(EIO))
\r
267 CASPAR_LOG(debug) << print() << " Received EIO, assuming EOF.";
\r
268 //if(ret == AVERROR_EOF)
\r
269 // CASPAR_LOG(info) << print() << " Received EOF.";
\r
271 return ret == AVERROR_EOF || ret == AVERROR(EIO); // av_read_frame doesn't always correctly return AVERROR_EOF;
\r
274 std::wstring print() const
\r
276 return L"ffmpeg_input[" + filename_ + L")]";
\r
280 input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start, int length)
\r
281 : impl_(new implementation(graph, filename, loop, start)){}
\r
282 bool input::eof() const {return !impl_->is_running_;}
\r
283 bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}
\r
284 std::shared_ptr<AVFormatContext> input::context(){return impl_->format_context_;}
\r