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 #include "..\stdafx.h"
\r
23 #include "../ffmpeg_error.h"
\r
25 #include <core/video_format.h>
\r
27 #include <common/concurrency/executor.h>
\r
28 #include <common/diagnostics/graph.h>
\r
30 #include <tbb/concurrent_queue.h>
\r
31 #include <tbb/queuing_mutex.h>
\r
33 #include <boost/exception/error_info.hpp>
\r
34 #include <boost/thread/once.hpp>
\r
35 #include <boost/thread/thread.hpp>
\r
36 #include <boost/regex.hpp>
\r
39 #include <system_error>
\r
41 #if defined(_MSC_VER)
\r
42 #pragma warning (disable : 4244)
\r
47 #define __STDC_CONSTANT_MACROS
\r
48 #define __STDC_LIMIT_MACROS
\r
49 #include <libavformat/avformat.h>
\r
54 struct input::implementation : boost::noncopyable
\r
56 static const size_t PACKET_BUFFER_COUNT = 25;
\r
58 safe_ptr<diagnostics::graph> graph_;
\r
60 std::shared_ptr<AVFormatContext> format_context_; // Destroy this last
\r
62 std::shared_ptr<AVCodecContext> video_codec_context_;
\r
63 std::shared_ptr<AVCodecContext> audio_codex_context_;
\r
65 const std::wstring filename_;
\r
70 const int start_frame_;
\r
71 const int end_frame_;
\r
74 tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>> video_packet_buffer_;
\r
75 tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>> audio_packet_buffer_;
\r
77 boost::condition_variable cond_;
\r
78 boost::mutex mutex_;
\r
80 std::exception_ptr exception_;
\r
83 explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start_frame, int end_frame)
\r
86 , video_s_index_(-1)
\r
87 , audio_s_index_(-1)
\r
88 , filename_(filename)
\r
89 , executor_(print())
\r
90 , start_frame_(std::max(start_frame, 0))
\r
91 , end_frame_(end_frame)
\r
92 , eof_count_(end_frame-start_frame)
\r
94 if(end_frame_ > 0 && end_frame <= start_frame_)
\r
95 BOOST_THROW_EXCEPTION(
\r
96 invalid_argument() <<
\r
97 source_info(narrow(print())) <<
\r
98 msg_info("End-frame cannot be lower than start-frame."));
\r
100 graph_->set_color("input-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));
\r
101 graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f));
\r
104 AVFormatContext* weak_format_context_ = nullptr;
\r
105 if((errn = av_open_input_file(&weak_format_context_, narrow(filename).c_str(), nullptr, 0, nullptr)) < 0 || weak_format_context_ == nullptr)
\r
106 BOOST_THROW_EXCEPTION(
\r
107 file_read_error() <<
\r
108 source_info(narrow(print())) <<
\r
109 msg_info(av_error_str(errn)) <<
\r
110 boost::errinfo_api_function("av_open_input_file") <<
\r
111 boost::errinfo_errno(AVUNERROR(errn)) <<
\r
112 boost::errinfo_file_name(narrow(filename)));
\r
114 format_context_.reset(weak_format_context_, av_close_input_file);
\r
116 if((errn = av_find_stream_info(format_context_.get())) < 0)
\r
117 BOOST_THROW_EXCEPTION(
\r
118 file_read_error() <<
\r
119 source_info(narrow(print())) <<
\r
120 msg_info(av_error_str(errn)) <<
\r
121 boost::errinfo_api_function("av_find_stream_info") <<
\r
122 boost::errinfo_errno(AVUNERROR(errn)));
\r
124 video_codec_context_ = open_stream(CODEC_TYPE_VIDEO, video_s_index_);
\r
125 if(!video_codec_context_)
\r
126 CASPAR_LOG(warning) << print() << " Could not open any video stream.";
\r
128 fix_time_base(video_codec_context_.get());
\r
130 audio_codex_context_ = open_stream(CODEC_TYPE_AUDIO, audio_s_index_);
\r
131 if(!audio_codex_context_)
\r
132 CASPAR_LOG(warning) << print() << " Could not open any audio stream.";
\r
134 fix_time_base(video_codec_context_.get());
\r
136 if(!video_codec_context_ && !audio_codex_context_)
\r
137 BOOST_THROW_EXCEPTION(
\r
138 file_read_error() <<
\r
139 msg_info(av_error_str(errn)) <<
\r
140 source_info(narrow(print())) <<
\r
141 msg_info("No video or audio codec context found."));
\r
143 if(start_frame_ != 0)
\r
144 seek_frame(start_frame_);
\r
147 executor_.begin_invoke([this]{read_file();});
\r
148 CASPAR_LOG(info) << print() << " Started.";
\r
155 cond_.notify_all();
\r
156 CASPAR_LOG(info) << print() << " Stopped.";
\r
159 void fix_time_base(AVCodecContext* context) // Some files give an invalid numerator, try to fix it.
\r
161 if(context && context->time_base.num == 1)
\r
162 context->time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(context->time_base.den)))-1));
\r
165 std::shared_ptr<AVCodecContext> open_stream(int codec_type, int& s_index)
\r
167 AVStream** streams_end = format_context_->streams+format_context_->nb_streams;
\r
168 AVStream** stream = std::find_if(format_context_->streams, streams_end,
\r
169 [&](AVStream* stream) { return stream != nullptr && stream->codec->codec_type == codec_type ;});
\r
171 if(stream == streams_end)
\r
174 auto codec = avcodec_find_decoder((*stream)->codec->codec_id);
\r
175 if(codec == nullptr)
\r
178 if((-avcodec_open((*stream)->codec, codec)) > 0)
\r
181 s_index = (*stream)->index;
\r
183 return std::shared_ptr<AVCodecContext>((*stream)->codec, avcodec_close);
\r
186 std::shared_ptr<AVCodecContext>& get_default_context()
\r
188 return video_codec_context_ ? video_codec_context_ : audio_codex_context_;
\r
191 bool is_eof(int errn)
\r
193 if(end_frame_ != -1)
\r
194 return get_default_context()->frame_number > eof_count_;
\r
196 return errn == AVERROR_EOF || errn == AVERROR_IO;
\r
203 AVPacket tmp_packet;
\r
204 safe_ptr<AVPacket> read_packet(&tmp_packet, av_free_packet);
\r
206 auto read_frame_ret = av_read_frame(format_context_.get(), read_packet.get());
\r
207 if(is_eof(read_frame_ret))
\r
211 seek_frame(start_frame_, AVSEEK_FLAG_BACKWARD);
\r
212 // AVCodecContext.frame_number is not reset. Increase the target frame_number.
\r
213 eof_count_ += end_frame_ - start_frame_;
\r
214 graph_->add_tag("seek");
\r
219 else if(read_frame_ret < 0)
\r
221 BOOST_THROW_EXCEPTION(
\r
222 invalid_operation() <<
\r
223 msg_info(av_error_str(read_frame_ret)) <<
\r
224 source_info(narrow(print())) <<
\r
225 boost::errinfo_api_function("av_read_frame") <<
\r
226 boost::errinfo_errno(AVUNERROR(read_frame_ret)));
\r
230 auto packet = std::make_shared<aligned_buffer>(read_packet->data, read_packet->data + read_packet->size);
\r
231 if(read_packet->stream_index == video_s_index_)
\r
232 video_packet_buffer_.try_push(std::move(packet));
\r
233 else if(read_packet->stream_index == audio_s_index_)
\r
234 audio_packet_buffer_.try_push(std::move(packet));
\r
237 graph_->update_value("input-buffer", static_cast<float>(video_packet_buffer_.size())/static_cast<float>(PACKET_BUFFER_COUNT));
\r
242 CASPAR_LOG_CURRENT_EXCEPTION();
\r
246 executor_.begin_invoke([this]{read_file();});
\r
247 boost::unique_lock<boost::mutex> lock(mutex_);
\r
248 while(executor_.is_running() && audio_packet_buffer_.size() > PACKET_BUFFER_COUNT && video_packet_buffer_.size() > PACKET_BUFFER_COUNT)
\r
255 CASPAR_LOG(info) << print() << " eof";
\r
258 std::shared_ptr<aligned_buffer> get_video_packet()
\r
260 return get_packet(video_packet_buffer_);
\r
263 std::shared_ptr<aligned_buffer> get_audio_packet()
\r
265 return get_packet(audio_packet_buffer_);
\r
268 bool has_packet() const
\r
270 return !video_packet_buffer_.empty() || !audio_packet_buffer_.empty();
\r
273 void seek_frame(int64_t frame, int flags = 0)
\r
275 // Convert from frames into seconds.
\r
276 auto ts = frame*static_cast<int64_t>((AV_TIME_BASE*get_default_context()->time_base.num) / get_default_context()->time_base.den);
\r
278 const int errn = av_seek_frame(format_context_.get(), -1, ts, flags | AVSEEK_FLAG_FRAME);
\r
281 BOOST_THROW_EXCEPTION(
\r
282 invalid_operation() <<
\r
283 source_info(narrow(print())) <<
\r
284 msg_info(av_error_str(errn)) <<
\r
285 boost::errinfo_api_function("seek_frame") <<
\r
286 boost::errinfo_errno(AVUNERROR(errn)));
\r
288 // Notify decoders to flush buffers.
\r
289 video_packet_buffer_.try_push(std::make_shared<aligned_buffer>());
\r
290 audio_packet_buffer_.try_push(std::make_shared<aligned_buffer>());
\r
293 std::shared_ptr<aligned_buffer> get_packet(tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>>& buffer)
\r
295 cond_.notify_all();
\r
296 std::shared_ptr<aligned_buffer> packet;
\r
297 return buffer.try_pop(packet) ? packet : nullptr;
\r
302 return static_cast<double>(get_default_context()->time_base.den) / static_cast<double>(get_default_context()->time_base.num);
\r
305 std::wstring print() const
\r
307 return L"ffmpeg_input[" + filename_ + L"]";
\r
311 input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start_frame, int end_frame) : impl_(new implementation(graph, filename, loop, start_frame, end_frame)){}
\r
312 const std::shared_ptr<AVCodecContext>& input::get_video_codec_context() const{return impl_->video_codec_context_;}
\r
313 const std::shared_ptr<AVCodecContext>& input::get_audio_codec_context() const{return impl_->audio_codex_context_;}
\r
314 bool input::has_packet() const{return impl_->has_packet();}
\r
315 bool input::is_running() const {return impl_->executor_.is_running();}
\r
316 std::shared_ptr<aligned_buffer> input::get_video_packet(){return impl_->get_video_packet();}
\r
317 std::shared_ptr<aligned_buffer> input::get_audio_packet(){return impl_->get_audio_packet();}
\r
318 double input::fps() const { return impl_->fps(); }
\r