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
33 #include <common/exception/exceptions.h>
\r
34 #include <common/exception/win32_exception.h>
\r
36 #include <tbb/concurrent_queue.h>
\r
37 #include <tbb/atomic.h>
\r
39 #include <boost/range/algorithm.hpp>
\r
40 #include <boost/thread/condition_variable.hpp>
\r
41 #include <boost/thread/mutex.hpp>
\r
42 #include <boost/thread/thread.hpp>
\r
44 #if defined(_MSC_VER)
\r
45 #pragma warning (push)
\r
46 #pragma warning (disable : 4244)
\r
50 #define __STDC_CONSTANT_MACROS
\r
51 #define __STDC_LIMIT_MACROS
\r
52 #include <libavformat/avformat.h>
\r
54 #if defined(_MSC_VER)
\r
55 #pragma warning (pop)
\r
60 static const size_t MAX_BUFFER_COUNT = 128;
\r
61 static const size_t MAX_BUFFER_SIZE = 32 * 1000000;
\r
63 struct input::implementation : boost::noncopyable
\r
65 std::shared_ptr<AVFormatContext> format_context_; // Destroy this last
\r
66 int default_stream_index_;
\r
68 safe_ptr<diagnostics::graph> graph_;
\r
70 const std::wstring filename_;
\r
74 tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> buffer_;
\r
75 tbb::atomic<size_t> buffer_size_;
\r
76 boost::condition_variable buffer_cond_;
\r
77 boost::mutex buffer_mutex_;
\r
79 boost::thread thread_;
\r
80 tbb::atomic<bool> is_running_;
\r
82 tbb::atomic<size_t> nb_frames_;
\r
83 tbb::atomic<size_t> nb_loops_;
\r
86 explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start)
\r
89 , filename_(filename)
\r
90 , start_(std::max(start, 0))
\r
96 AVFormatContext* weak_format_context_ = nullptr;
\r
97 THROW_ON_ERROR2(avformat_open_input(&weak_format_context_, narrow(filename).c_str(), nullptr, nullptr), print());
\r
99 format_context_.reset(weak_format_context_, av_close_input_file);
\r
101 av_dump_format(weak_format_context_, 0, narrow(filename).c_str(), 0);
\r
103 THROW_ON_ERROR2(avformat_find_stream_info(format_context_.get(), nullptr), print());
\r
105 default_stream_index_ = THROW_ON_ERROR2(av_find_default_stream_index(format_context_.get()), print());
\r
108 seek_frame(start_);
\r
110 for(int n = 0; n < 16 && !full(); ++n)
\r
111 read_next_packet();
\r
113 graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f));
\r
114 graph_->set_color("buffer-count", diagnostics::color(0.2f, 0.8f, 1.0f));
\r
115 graph_->set_color("buffer-size", diagnostics::color(0.2f, 0.4f, 1.0f));
\r
117 thread_ = boost::thread([this]{run();});
\r
122 is_running_ = false;
\r
123 buffer_cond_.notify_all();
\r
127 bool try_pop(std::shared_ptr<AVPacket>& packet)
\r
129 const bool result = buffer_.try_pop(packet);
\r
134 buffer_size_ -= packet->size;
\r
135 buffer_cond_.notify_all();
\r
138 graph_->update_value("buffer-size", MAX_BUFFER_SIZE/static_cast<double>(buffer_size_));
\r
139 graph_->update_value("buffer-count", MAX_BUFFER_SIZE/static_cast<double>(buffer_.size()));
\r
144 size_t nb_frames() const
\r
149 size_t nb_loops() const
\r
158 caspar::win32_exception::install_handler();
\r
162 CASPAR_LOG(info) << print() << " Thread Started.";
\r
167 boost::unique_lock<boost::mutex> lock(buffer_mutex_);
\r
168 buffer_cond_.wait(lock, [this]{return !full();});
\r
170 read_next_packet();
\r
173 CASPAR_LOG(info) << print() << " Thread Stopped.";
\r
177 CASPAR_LOG_CURRENT_EXCEPTION();
\r
178 is_running_ = false;
\r
182 void read_next_packet()
\r
186 std::shared_ptr<AVPacket> read_packet(new AVPacket, [](AVPacket* p)
\r
191 av_init_packet(read_packet.get());
\r
193 ret = av_read_frame(format_context_.get(), read_packet.get()); // read_packet is only valid until next call of av_read_frame. Use av_dup_packet to extend its life.
\r
201 seek_frame(start_, AVSEEK_FLAG_BACKWARD);
\r
202 graph_->add_tag("seek");
\r
203 CASPAR_LOG(trace) << print() << " Received EOF. Looping.";
\r
207 is_running_ = false;
\r
208 CASPAR_LOG(trace) << print() << " Received EOF. Stopping.";
\r
213 THROW_ON_ERROR(ret, print(), "av_read_frame");
\r
215 if(read_packet->stream_index == default_stream_index_ && nb_loops_ == 0)
\r
218 THROW_ON_ERROR2(av_dup_packet(read_packet.get()), print());
\r
220 // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
\r
221 auto size = read_packet->size;
\r
222 auto data = read_packet->data;
\r
224 read_packet = std::shared_ptr<AVPacket>(read_packet.get(), [=](AVPacket*)
\r
226 read_packet->size = size;
\r
227 read_packet->data = data;
\r
230 buffer_.try_push(read_packet);
\r
231 buffer_size_ += read_packet->size;
\r
233 graph_->update_value("buffer-count", MAX_BUFFER_SIZE/static_cast<double>(buffer_.size()));
\r
234 graph_->update_value("buffer-size", MAX_BUFFER_SIZE/static_cast<double>(buffer_size_));
\r
240 return is_running_ && buffer_size_ > MAX_BUFFER_SIZE && buffer_.size() > MAX_BUFFER_COUNT;
\r
243 void seek_frame(int64_t frame, int flags = 0)
\r
245 THROW_ON_ERROR2(av_seek_frame(format_context_.get(), default_stream_index_, frame, flags), print());
\r
246 buffer_.push(nullptr);
\r
249 bool is_eof(int ret)
\r
251 if(ret == AVERROR(EIO))
\r
252 CASPAR_LOG(debug) << print() << " Received EIO, assuming EOF.";
\r
253 //if(ret == AVERROR_EOF)
\r
254 // CASPAR_LOG(info) << print() << " Received EOF.";
\r
256 return ret == AVERROR_EOF || ret == AVERROR(EIO); // av_read_frame doesn't always correctly return AVERROR_EOF;
\r
259 std::wstring print() const
\r
261 return L"ffmpeg_input[" + filename_ + L")]";
\r
265 input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start, int length)
\r
266 : impl_(new implementation(graph, filename, loop, start)){}
\r
267 bool input::eof() const {return !impl_->is_running_;}
\r
268 bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}
\r
269 safe_ptr<AVFormatContext> input::context(){return make_safe(impl_->format_context_);}
\r
270 size_t input::nb_frames() const {return impl_->nb_frames();}
\r
271 size_t input::nb_loops() const {return impl_->nb_loops();}
\r