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
28 #include "../util/util.h"
\r
29 #include "../../ffmpeg_error.h"
\r
31 #include <core/video_format.h>
\r
33 #include <common/diagnostics/graph.h>
\r
34 #include <common/exception/exceptions.h>
\r
35 #include <common/exception/win32_exception.h>
\r
37 #include <tbb/atomic.h>
\r
40 #include <concrt_extras.h>
\r
42 #if defined(_MSC_VER)
\r
43 #pragma warning (push)
\r
44 #pragma warning (disable : 4244)
\r
48 #define __STDC_CONSTANT_MACROS
\r
49 #define __STDC_LIMIT_MACROS
\r
50 #include <libavformat/avformat.h>
\r
52 #if defined(_MSC_VER)
\r
53 #pragma warning (pop)
\r
58 using namespace Concurrency;
\r
60 namespace caspar { namespace ffmpeg {
\r
62 static const size_t MAX_PACKETS_SIZE = 16 * 1000000;
\r
63 static const size_t MAX_PACKETS_COUNT = 50;
\r
65 struct input::implementation : public Concurrency::agent
\r
66 , public std::enable_shared_from_this<implementation>
\r
67 , boost::noncopyable
\r
69 input::target_t& target_;
\r
71 const std::wstring filename_;
\r
72 const safe_ptr<AVFormatContext> format_context_; // Destroy this last
\r
73 int default_stream_index_;
\r
74 const boost::iterator_range<AVStream**> streams_;
\r
76 safe_ptr<diagnostics::graph> graph_;
\r
78 tbb::atomic<bool> loop_;
\r
79 const size_t start_;
\r
80 const size_t length_;
\r
81 size_t frame_number_;
\r
83 tbb::atomic<size_t> nb_frames_;
\r
84 tbb::atomic<size_t> nb_loops_;
\r
85 tbb::atomic<size_t> packets_count_;
\r
86 tbb::atomic<size_t> packets_size_;
\r
88 tbb::atomic<bool> is_running_;
\r
90 Concurrency::event event_;
\r
93 explicit implementation(input::target_t& target,
\r
94 const safe_ptr<diagnostics::graph>& graph,
\r
95 const std::wstring& filename,
\r
100 , filename_(filename)
\r
101 , format_context_(open_input(filename))
\r
102 , default_stream_index_(av_find_default_stream_index(format_context_.get()))
\r
103 , streams_(format_context_->streams, format_context_->streams + format_context_->nb_streams)
\r
112 //av_dump_format(format_context_.get(), 0, narrow(filename).c_str(), 0);
\r
115 seek_frame(start_);
\r
117 graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));
\r
118 graph_->set_color("buffer-count", diagnostics::color(0.7f, 0.4f, 0.4f));
\r
119 graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f));
\r
121 is_running_ = true;
\r
133 is_running_ = false;
\r
140 win32_exception::install_handler();
\r
144 for(auto packet = read_next_packet(); packet && is_running_; packet = read_next_packet())
\r
146 Concurrency::asend(target_, make_safe_ptr(packet));
\r
153 CASPAR_LOG_CURRENT_EXCEPTION();
\r
156 BOOST_FOREACH(auto stream, streams_)
\r
157 Concurrency::send(target_, flush_packet(stream->index));
\r
158 BOOST_FOREACH(auto stream, streams_)
\r
159 Concurrency::send(target_, eof_packet(stream->index));
\r
164 std::shared_ptr<AVPacket> read_next_packet()
\r
166 auto packet = create_packet();
\r
168 int ret = [&]() -> int
\r
170 Concurrency::scoped_oversubcription_token oversubscribe;
\r
171 return av_read_frame(format_context_.get(), packet.get()); // packet is only valid until next call of av_read_frame. Use av_dup_packet to extend its life.
\r
181 CASPAR_LOG(trace) << print() << " Looping.";
\r
182 seek_frame(start_, AVSEEK_FLAG_BACKWARD);
\r
183 return read_next_packet();
\r
187 CASPAR_LOG(trace) << print() << " Stopping.";
\r
192 THROW_ON_ERROR(ret, "av_read_frame", print());
\r
194 if(packet->stream_index == default_stream_index_)
\r
201 THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
\r
203 // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
\r
204 auto size = packet->size;
\r
205 auto data = packet->data;
\r
208 packets_size_ += size;
\r
210 graph_->update_value("buffer-size", (packets_size_+0.001)/MAX_PACKETS_SIZE);
\r
211 graph_->update_value("buffer-count", (packets_count_+0.001)/MAX_PACKETS_COUNT);
\r
213 auto self = shared_from_this();
\r
214 packet = safe_ptr<AVPacket>(packet.get(), [this, self, packet, size, data](AVPacket*)
\r
216 packet->size = size;
\r
217 packet->data = data;
\r
219 packets_size_ -= size;
\r
222 graph_->update_value("buffer-size", (packets_size_+0.001)/MAX_PACKETS_SIZE);
\r
223 graph_->update_value("buffer-count", (packets_count_+0.001)/MAX_PACKETS_COUNT);
\r
226 if(is_running_ && (packets_count_ > MAX_PACKETS_COUNT || packets_size_ > MAX_PACKETS_SIZE))
\r
232 void seek_frame(int64_t frame, int flags = 0)
\r
234 if(flags == AVSEEK_FLAG_BACKWARD)
\r
237 int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
\r
238 if(vid_stream_index >= 0)
\r
240 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;
\r
241 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)
\r
242 flags |= AVSEEK_FLAG_BYTE;
\r
246 THROW_ON_ERROR2(av_seek_frame(format_context_.get(), default_stream_index_, frame, flags), print());
\r
247 auto packet = create_packet();
\r
250 BOOST_FOREACH(auto stream, streams_)
\r
251 Concurrency::asend(target_, flush_packet(stream->index));
\r
253 graph_->add_tag("seek");
\r
256 bool is_eof(int ret)
\r
258 if(ret == AVERROR(EIO))
\r
259 CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. " << nb_frames_;
\r
260 if(ret == AVERROR_EOF)
\r
261 CASPAR_LOG(trace) << print() << " Received EOF. " << nb_frames_;
\r
263 CASPAR_VERIFY(ret >= 0 || ret == AVERROR_EOF || ret == AVERROR(EIO), ffmpeg_error() << source_info(narrow(print())));
\r
265 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
\r
268 std::wstring print() const
\r
270 return L"ffmpeg_input[" + filename_ + L")]";
\r
274 input::input(input::target_t& target,
\r
275 const safe_ptr<diagnostics::graph>& graph,
\r
276 const std::wstring& filename,
\r
280 : impl_(new implementation(target, graph, filename, loop, start, length))
\r
284 safe_ptr<AVFormatContext> input::context()
\r
286 return safe_ptr<AVFormatContext>(impl_->format_context_);
\r
289 size_t input::nb_frames() const
\r
291 return impl_->nb_frames_;
\r
294 size_t input::nb_loops() const
\r
296 return impl_->nb_loops_;
\r
304 bool input::loop() const
\r
306 return impl_->loop_;
\r
308 void input::loop(bool value)
\r
310 impl_->loop_ = value;
\r