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 "../ffmpeg_error.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/atomic.h>
\r
39 #include <concrt_extras.h>
\r
40 #include <semaphore.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
56 using namespace Concurrency;
\r
58 namespace caspar { namespace ffmpeg {
\r
60 static const size_t MAX_TOKENS = 32;
\r
62 struct input::implementation : public Concurrency::agent, boost::noncopyable
\r
64 input::target_t& target_;
\r
66 const std::wstring filename_;
\r
67 const safe_ptr<AVFormatContext> format_context_; // Destroy this last
\r
68 int default_stream_index_;
\r
69 const boost::iterator_range<AVStream**> streams_;
\r
71 safe_ptr<diagnostics::graph> graph_;
\r
74 const size_t start_;
\r
75 const size_t length_;
\r
76 size_t frame_number_;
\r
78 tbb::atomic<size_t> nb_frames_;
\r
79 tbb::atomic<size_t> nb_loops_;
\r
80 tbb::atomic<size_t> packets_count_;
\r
81 tbb::atomic<size_t> packets_size_;
\r
85 safe_ptr<Concurrency::semaphore> semaphore_;
\r
88 explicit implementation(input::target_t& target,
\r
89 const safe_ptr<diagnostics::graph>& graph,
\r
90 const std::wstring& filename,
\r
95 , filename_(filename)
\r
96 , format_context_(open_input(filename))
\r
97 , default_stream_index_(av_find_default_stream_index(format_context_.get()))
\r
98 , streams_(format_context_->streams, format_context_->streams + format_context_->nb_streams)
\r
105 , semaphore_(make_safe<Concurrency::semaphore>(MAX_TOKENS))
\r
107 packets_count_ = 0;
\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
123 for(size_t n = 0; n < format_context_->nb_streams+1; ++n)
\r
124 semaphore_->release();
\r
134 auto packet = read_next_packet();
\r
138 Concurrency::asend(target_, make_message(packet, packet->stream_index == default_stream_index_ ? std::make_shared<token>(semaphore_) : nullptr));
\r
139 Concurrency::wait(0);
\r
141 //std::vector<std::shared_ptr<AVPacket>> buffer;
\r
143 //while(buffer.size() < 100 && !stop_)
\r
145 // Concurrency::scoped_oversubcription_token oversubscribe;
\r
146 // auto packet = read_next_packet();
\r
150 // buffer.push_back(packet);
\r
153 //std::stable_partition(buffer.begin(), buffer.end(), [this](const std::shared_ptr<AVPacket>& packet)
\r
155 // return packet->stream_index != default_stream_index_;
\r
158 //BOOST_FOREACH(auto packet, buffer)
\r
160 // Concurrency::asend(target_, make_message(packet, packet->stream_index == default_stream_index_ ? std::make_shared<token>(semaphore_) : nullptr));
\r
161 // Concurrency::wait(0);
\r
167 CASPAR_LOG_CURRENT_EXCEPTION();
\r
170 BOOST_FOREACH(auto stream, streams_)
\r
172 Concurrency::send(target_, make_message(eof_packet(stream->index), std::make_shared<token>(semaphore_)));
\r
173 Concurrency::send(target_, make_message(eof_packet(stream->index), std::make_shared<token>(semaphore_)));
\r
179 std::shared_ptr<AVPacket> read_next_packet()
\r
181 auto packet = create_packet();
\r
183 int ret = [&]() -> int
\r
185 Concurrency::scoped_oversubcription_token oversubscribe;
\r
186 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
196 CASPAR_LOG(trace) << print() << " Looping.";
\r
197 seek_frame(start_, AVSEEK_FLAG_BACKWARD);
\r
201 CASPAR_LOG(trace) << print() << " Stopping.";
\r
206 THROW_ON_ERROR(ret, print(), "av_read_frame");
\r
208 if(packet->stream_index == default_stream_index_)
\r
215 THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
\r
217 // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
\r
218 auto size = packet->size;
\r
219 auto data = packet->data;
\r
221 packet = std::shared_ptr<AVPacket>(packet.get(), [=](AVPacket*)
\r
223 packet->size = size;
\r
224 packet->data = data;
\r
233 void seek_frame(int64_t frame, int flags = 0)
\r
235 if(flags == AVSEEK_FLAG_BACKWARD)
\r
238 int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
\r
239 if(vid_stream_index >= 0)
\r
241 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;
\r
242 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)
\r
243 flags |= AVSEEK_FLAG_BYTE;
\r
247 THROW_ON_ERROR2(av_seek_frame(format_context_.get(), default_stream_index_, frame, flags), print());
\r
248 auto packet = create_packet();
\r
251 BOOST_FOREACH(auto stream, streams_)
\r
252 Concurrency::send(target_, make_message(loop_packet(stream->index), std::make_shared<token>(semaphore_)));
\r
254 graph_->add_tag("seek");
\r
257 bool is_eof(int ret)
\r
259 if(ret == AVERROR(EIO))
\r
260 CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. " << nb_frames_;
\r
261 if(ret == AVERROR_EOF)
\r
262 CASPAR_LOG(trace) << print() << " Received EOF. " << nb_frames_;
\r
264 CASPAR_VERIFY(ret >= 0 || ret == AVERROR_EOF || ret == AVERROR(EIO), ffmpeg_error() << source_info(narrow(print())));
\r
266 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
\r
269 std::wstring print() const
\r
271 return L"ffmpeg_input[" + filename_ + L")]";
\r
275 input::input(input::target_t& target,
\r
276 const safe_ptr<diagnostics::graph>& graph,
\r
277 const std::wstring& filename,
\r
281 : impl_(new implementation(target, graph, filename, loop, start, length))
\r
285 safe_ptr<AVFormatContext> input::context()
\r
287 return safe_ptr<AVFormatContext>(impl_->format_context_);
\r
290 size_t input::nb_frames() const
\r
292 return impl_->nb_frames_;
\r
295 size_t input::nb_loops() const
\r
297 return impl_->nb_loops_;
\r
300 void input::start()
\r