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/concurrency/executor.h>
\r
33 #include <common/diagnostics/graph.h>
\r
35 #include <tbb/concurrent_queue.h>
\r
36 #include <tbb/mutex.h>
\r
38 #include <boost/range/iterator_range.hpp>
\r
39 #include <boost/range/algorithm.hpp>
\r
43 #define __STDC_CONSTANT_MACROS
\r
44 #define __STDC_LIMIT_MACROS
\r
45 #include <libavformat/avformat.h>
\r
50 static const size_t PACKET_BUFFER_COUNT = 100; // Assume that av_read_frame distance between audio and video packets is less than PACKET_BUFFER_COUNT.
\r
54 std::shared_ptr<AVCodecContext> ctx_;
\r
56 tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> buffer_;
\r
60 stream() : index_(-1)
\r
62 buffer_.set_capacity(PACKET_BUFFER_COUNT);
\r
67 CASPAR_LOG(trace) << "##: " << size();
\r
70 int open(std::shared_ptr<AVFormatContext>& fctx, AVMediaType media_type)
\r
72 const auto streams = boost::iterator_range<AVStream**>(fctx->streams, fctx->streams+fctx->nb_streams);
\r
73 const auto stream = boost::find_if(streams, [&](AVStream* stream)
\r
75 return stream && stream->codec->codec_type == media_type;
\r
78 if(stream == streams.end())
\r
79 return AVERROR_STREAM_NOT_FOUND;
\r
81 auto codec = avcodec_find_decoder((*stream)->codec->codec_id);
\r
83 return AVERROR_DECODER_NOT_FOUND;
\r
85 index_ = (*stream)->index;
\r
87 int errn = tbb_avcodec_open((*stream)->codec, codec);
\r
90 ctx_.reset((*stream)->codec, tbb_avcodec_close);
\r
92 // Some files give an invalid time_base numerator, try to fix it.
\r
93 if(ctx_ && ctx_->time_base.num == 1)
\r
94 ctx_->time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(ctx_->time_base.den)))-1));
\r
99 bool try_pop(std::shared_ptr<AVPacket>& pkt)
\r
101 return buffer_.try_pop(pkt);
\r
104 void push(const std::shared_ptr<AVPacket>& pkt)
\r
106 if(pkt && pkt->stream_index != index_)
\r
110 av_dup_packet(pkt.get());
\r
112 buffer_.push(pkt);
\r
115 int index() const {return index_;}
\r
117 const std::shared_ptr<AVCodecContext>& ctx() { return ctx_; }
\r
119 operator bool(){return ctx_ != nullptr;}
\r
121 double fps() const { return !ctx_ ? -1.0 : static_cast<double>(ctx_->time_base.den) / static_cast<double>(ctx_->time_base.num); }
\r
123 bool empty() const { return buffer_.empty();}
\r
124 int size() const { return buffer_.size();}
\r
127 struct input::implementation : boost::noncopyable
\r
129 safe_ptr<diagnostics::graph> graph_;
\r
131 std::shared_ptr<AVFormatContext> format_context_; // Destroy this last
\r
133 const std::wstring filename_;
\r
138 stream video_stream_;
\r
139 stream audio_stream_;
\r
141 std::exception_ptr exception_;
\r
142 executor executor_;
\r
144 explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start)
\r
147 , filename_(filename)
\r
148 , executor_(print())
\r
149 , start_(std::max(start, 0))
\r
151 graph_->set_color("audio-input-buffer", diagnostics::color(0.5f, 1.0f, 0.2f));
\r
152 graph_->set_color("video-input-buffer", diagnostics::color(0.2f, 0.5f, 1.0f));
\r
153 graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f));
\r
157 AVFormatContext* weak_format_context_ = nullptr;
\r
158 errn = av_open_input_file(&weak_format_context_, narrow(filename).c_str(), nullptr, 0, nullptr);
\r
159 if(errn < 0 || weak_format_context_ == nullptr)
\r
161 BOOST_THROW_EXCEPTION(
\r
162 file_read_error() <<
\r
163 source_info(narrow(print())) <<
\r
164 msg_info(av_error_str(errn)) <<
\r
165 boost::errinfo_api_function("av_open_input_file") <<
\r
166 boost::errinfo_errno(AVUNERROR(errn)) <<
\r
167 boost::errinfo_file_name(narrow(filename)));
\r
170 format_context_.reset(weak_format_context_, av_close_input_file);
\r
172 errn = av_find_stream_info(format_context_.get());
\r
175 BOOST_THROW_EXCEPTION(
\r
176 file_read_error() <<
\r
177 source_info(narrow(print())) <<
\r
178 msg_info(av_error_str(errn)) <<
\r
179 boost::errinfo_api_function("av_find_stream_info") <<
\r
180 boost::errinfo_errno(AVUNERROR(errn)));
\r
183 errn = video_stream_.open(format_context_, AVMEDIA_TYPE_VIDEO);
\r
185 CASPAR_LOG(warning) << print() << L" Could not open video stream: " << widen(av_error_str(errn));
\r
187 errn = audio_stream_.open(format_context_, AVMEDIA_TYPE_AUDIO);
\r
189 CASPAR_LOG(warning) << print() << L" Could not open audio stream: " << widen(av_error_str(errn));
\r
191 if(!video_stream_ && !audio_stream_)
\r
193 BOOST_THROW_EXCEPTION(
\r
194 file_read_error() <<
\r
195 source_info(narrow(print())) <<
\r
196 msg_info("No video or audio codec context found."));
\r
199 fps_ = video_stream_ ? video_stream_.fps() : audio_stream_.fps();
\r
202 seek_frame(start_);
\r
204 for(size_t n = 0; n < 16; ++n) // Read some packets for pre-rolling.
\r
205 read_next_packet();
\r
208 executor_.begin_invoke([this]{read_file();});
\r
209 CASPAR_LOG(info) << print() << " Started.";
\r
217 bool try_pop_video_packet(std::shared_ptr<AVPacket>& packet)
\r
219 return video_stream_.try_pop(packet);
\r
222 bool try_pop_audio_packet(std::shared_ptr<AVPacket>& packet)
\r
224 return audio_stream_.try_pop(packet);
\r
239 std::shared_ptr<AVPacket> packet;
\r
240 try_pop_video_packet(packet);
\r
241 try_pop_audio_packet(packet);
\r
243 CASPAR_LOG(info) << print() << " Stopping.";
\r
248 if(video_stream_.size() > 4 || audio_stream_.size() > 4) // audio is always before video.
\r
249 Sleep(5); // There are enough packets, no hurry.
\r
251 read_next_packet();
\r
253 executor_.begin_invoke([this]{read_file();});
\r
256 void read_next_packet()
\r
260 std::shared_ptr<AVPacket> read_packet(new AVPacket(), [](AVPacket* p)
\r
266 const int errn = av_read_frame(format_context_.get(), read_packet.get()); // read_packet is only valid until next call of av_read_frame.
\r
267 if(is_eof(errn)) // Use av_dup_packet to extend its life.
\r
271 seek_frame(start_, AVSEEK_FLAG_BACKWARD);
\r
272 graph_->add_tag("seek");
\r
273 CASPAR_LOG(info) << print() << " Received EOF. Looping.";
\r
278 CASPAR_LOG(info) << print() << " Received EOF. Stopping.";
\r
283 BOOST_THROW_EXCEPTION(
\r
284 file_read_error() <<
\r
285 msg_info(av_error_str(errn)) <<
\r
286 source_info(narrow(print())) <<
\r
287 boost::errinfo_api_function("av_read_frame") <<
\r
288 boost::errinfo_errno(AVUNERROR(errn)));
\r
292 video_stream_.push(read_packet);
\r
293 audio_stream_.push(read_packet);
\r
296 graph_->update_value("video-input-buffer", static_cast<float>(video_stream_.size())/static_cast<float>(PACKET_BUFFER_COUNT));
\r
297 graph_->update_value("audio-input-buffer", static_cast<float>(audio_stream_.size())/static_cast<float>(PACKET_BUFFER_COUNT));
\r
302 CASPAR_LOG_CURRENT_EXCEPTION();
\r
307 void seek_frame(int64_t frame, int flags = 0)
\r
309 static const AVRational base_q = {1, AV_TIME_BASE};
\r
311 // Convert from frames into seconds.
\r
312 auto seek_target = frame*static_cast<int64_t>(AV_TIME_BASE/fps_);
\r
314 int stream_index = video_stream_.index() >= 0 ? video_stream_.index() : audio_stream_.index();
\r
316 if(stream_index >= 0)
\r
317 seek_target = av_rescale_q(seek_target, base_q, format_context_->streams[stream_index]->time_base);
\r
319 const int errn = av_seek_frame(format_context_.get(), stream_index, seek_target, flags);
\r
322 BOOST_THROW_EXCEPTION(
\r
323 invalid_operation() <<
\r
324 source_info(narrow(print())) <<
\r
325 msg_info(av_error_str(errn)) <<
\r
326 boost::errinfo_api_function("av_seek_frame") <<
\r
327 boost::errinfo_errno(AVUNERROR(errn)));
\r
330 video_stream_.push(nullptr);
\r
331 audio_stream_.push(nullptr);
\r
334 bool is_eof(int errn)
\r
336 if(errn == AVERROR(EIO))
\r
337 CASPAR_LOG(warning) << print() << " Received EIO, assuming EOF";
\r
339 return errn == AVERROR_EOF || errn == AVERROR(EIO); // av_read_frame doesn't always correctly return AVERROR_EOF;
\r
342 std::wstring print() const
\r
344 return L"ffmpeg_input[" + filename_ + L"]";
\r
348 input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start, int length)
\r
349 : impl_(new implementation(graph, filename, loop, start)){}
\r
350 const std::shared_ptr<AVCodecContext>& input::get_video_codec_context() const{return impl_->video_stream_.ctx();}
\r
351 const std::shared_ptr<AVCodecContext>& input::get_audio_codec_context() const{return impl_->audio_stream_.ctx();}
\r
352 bool input::is_running() const {return impl_->executor_.is_running();}
\r
353 bool input::try_pop_video_packet(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_video_packet(packet);}
\r
354 bool input::try_pop_audio_packet(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_audio_packet(packet);}
\r
355 double input::fps() const { return impl_->fps(); }
\r