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
86 explicit implementation(input::target_t& target,
\r
87 const safe_ptr<diagnostics::graph>& graph,
\r
88 const std::wstring& filename,
\r
93 , filename_(filename)
\r
94 , format_context_(open_input(filename))
\r
95 , default_stream_index_(av_find_default_stream_index(format_context_.get()))
\r
96 , streams_(format_context_->streams, format_context_->streams + format_context_->nb_streams)
\r
104 packets_count_ = 0;
\r
109 av_dump_format(format_context_.get(), 0, narrow(filename).c_str(), 0);
\r
112 seek_frame(start_);
\r
114 graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));
\r
129 auto packet = read_next_packet();
\r
133 Concurrency::asend(target_, make_safe_ptr(packet));
\r
134 Concurrency::wait(40);
\r
139 CASPAR_LOG_CURRENT_EXCEPTION();
\r
142 BOOST_FOREACH(auto stream, streams_)
\r
143 Concurrency::send(target_, eof_packet(stream->index));
\r
148 std::shared_ptr<AVPacket> read_next_packet()
\r
150 auto packet = create_packet();
\r
152 int ret = [&]() -> int
\r
154 Concurrency::scoped_oversubcription_token oversubscribe;
\r
155 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
165 CASPAR_LOG(trace) << print() << " Looping.";
\r
166 seek_frame(start_, AVSEEK_FLAG_BACKWARD);
\r
167 return read_next_packet();
\r
171 CASPAR_LOG(trace) << print() << " Stopping.";
\r
176 THROW_ON_ERROR(ret, print(), "av_read_frame");
\r
178 if(packet->stream_index == default_stream_index_)
\r
185 THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
\r
187 // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
\r
188 auto size = packet->size;
\r
189 auto data = packet->data;
\r
191 packet = safe_ptr<AVPacket>(packet.get(), [=](AVPacket*)
\r
193 packet->size = size;
\r
194 packet->data = data;
\r
203 void seek_frame(int64_t frame, int flags = 0)
\r
205 if(flags == AVSEEK_FLAG_BACKWARD)
\r
208 int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
\r
209 if(vid_stream_index >= 0)
\r
211 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;
\r
212 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)
\r
213 flags |= AVSEEK_FLAG_BYTE;
\r
217 THROW_ON_ERROR2(av_seek_frame(format_context_.get(), default_stream_index_, frame, flags), print());
\r
218 auto packet = create_packet();
\r
221 BOOST_FOREACH(auto stream, streams_)
\r
222 Concurrency::asend(target_, loop_packet(stream->index));
\r
224 graph_->add_tag("seek");
\r
227 bool is_eof(int ret)
\r
229 if(ret == AVERROR(EIO))
\r
230 CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. " << nb_frames_;
\r
231 if(ret == AVERROR_EOF)
\r
232 CASPAR_LOG(trace) << print() << " Received EOF. " << nb_frames_;
\r
234 CASPAR_VERIFY(ret >= 0 || ret == AVERROR_EOF || ret == AVERROR(EIO), ffmpeg_error() << source_info(narrow(print())));
\r
236 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
\r
239 std::wstring print() const
\r
241 return L"ffmpeg_input[" + filename_ + L")]";
\r
245 input::input(input::target_t& target,
\r
246 const safe_ptr<diagnostics::graph>& graph,
\r
247 const std::wstring& filename,
\r
251 : impl_(new implementation(target, graph, filename, loop, start, length))
\r
255 safe_ptr<AVFormatContext> input::context()
\r
257 return safe_ptr<AVFormatContext>(impl_->format_context_);
\r
260 size_t input::nb_frames() const
\r
262 return impl_->nb_frames_;
\r
265 size_t input::nb_loops() const
\r
267 return impl_->nb_loops_;
\r
270 void input::start()
\r