1 #include "..\..\stdafx.h"
\r
5 #include "../../format/video_format.h"
\r
6 #include "../../../common/utility/scope_exit.h"
\r
8 #include <tbb/concurrent_queue.h>
\r
9 #include <tbb/queuing_mutex.h>
\r
11 #include <boost/thread.hpp>
\r
12 #include <boost/exception/error_info.hpp>
\r
15 #include <system_error>
\r
17 #if defined(_MSC_VER)
\r
18 #pragma warning (push)
\r
19 #pragma warning (disable : 4244)
\r
23 #define __STDC_CONSTANT_MACROS
\r
24 #define __STDC_LIMIT_MACROS
\r
25 #include <libavformat/avformat.h>
\r
27 #if defined(_MSC_VER)
\r
28 #pragma warning (pop)
\r
31 namespace caspar { namespace core { namespace ffmpeg{
\r
33 struct input::implementation : boost::noncopyable
\r
35 implementation() : video_s_index_(-1), audio_s_index_(-1), video_codec_(nullptr), audio_codec_(nullptr)
\r
38 video_packet_buffer_.set_capacity(50);
\r
39 audio_packet_buffer_.set_capacity(50);
\r
44 is_running_ = false;
\r
45 std::shared_ptr<aligned_buffer> buffer;
\r
46 audio_packet_buffer_.try_pop(buffer);
\r
47 video_packet_buffer_.try_pop(buffer);
\r
51 void load(const std::string& filename)
\r
56 AVFormatContext* weak_format_context_;
\r
57 if((errn = -av_open_input_file(&weak_format_context_, filename.c_str(), nullptr, 0, nullptr)) > 0)
\r
58 BOOST_THROW_EXCEPTION(
\r
59 file_read_error() <<
\r
60 msg_info("No format context found.") <<
\r
61 boost::errinfo_api_function("av_open_input_file") <<
\r
62 boost::errinfo_errno(errn) <<
\r
63 boost::errinfo_file_name(filename));
\r
65 format_context_.reset(weak_format_context_, av_close_input_file);
\r
67 if((errn = -av_find_stream_info(format_context_.get())) > 0)
\r
68 BOOST_THROW_EXCEPTION(
\r
69 file_read_error() <<
\r
70 boost::errinfo_api_function("av_find_stream_info") <<
\r
71 msg_info("No stream found.") <<
\r
72 boost::errinfo_errno(errn));
\r
74 video_codec_context_ = open_video_stream();
\r
75 if(!video_codec_context_)
\r
76 CASPAR_LOG(warning) << "No video stream found.";
\r
78 audio_codex_context_ = open_audio_stream();
\r
79 if(!audio_codex_context_)
\r
80 CASPAR_LOG(warning) << "No audio stream found.";
\r
82 if(!video_codec_context_ && !audio_codex_context_)
\r
83 BOOST_THROW_EXCEPTION(file_read_error() << msg_info("No video or audio codec context found."));
\r
87 video_codec_context_.reset();
\r
88 audio_codex_context_.reset();
\r
89 format_context_.reset();
\r
90 video_s_index_ = -1;
\r
91 audio_s_index_ = -1;
\r
94 filename_ = filename;
\r
95 io_thread_ = boost::thread([=]{read_file();});
\r
98 std::shared_ptr<AVCodecContext> open_video_stream()
\r
100 AVStream** streams_end = format_context_->streams+format_context_->nb_streams;
\r
101 AVStream** video_stream = std::find_if(format_context_->streams, streams_end,
\r
102 [](AVStream* stream) { return stream != nullptr && stream->codec->codec_type == CODEC_TYPE_VIDEO ;});
\r
104 video_s_index_ = video_stream != streams_end ? (*video_stream)->index : -1;
\r
105 if(video_s_index_ == -1)
\r
108 video_codec_ = avcodec_find_decoder((*video_stream)->codec->codec_id);
\r
109 if(video_codec_ == nullptr)
\r
112 if((-avcodec_open((*video_stream)->codec, video_codec_)) > 0)
\r
115 return std::shared_ptr<AVCodecContext>((*video_stream)->codec, avcodec_close);
\r
118 std::shared_ptr<AVCodecContext> open_audio_stream()
\r
120 AVStream** streams_end = format_context_->streams+format_context_->nb_streams;
\r
121 AVStream** audio_stream = std::find_if(format_context_->streams, streams_end,
\r
122 [](AVStream* stream) { return stream != nullptr && stream->codec->codec_type == CODEC_TYPE_AUDIO;});
\r
124 audio_s_index_ = audio_stream != streams_end ? (*audio_stream)->index : -1;
\r
125 if(audio_s_index_ == -1)
\r
128 audio_codec_ = avcodec_find_decoder((*audio_stream)->codec->codec_id);
\r
129 if(audio_codec_ == nullptr)
\r
132 if((-avcodec_open((*audio_stream)->codec, audio_codec_)) > 0)
\r
135 return std::shared_ptr<AVCodecContext>((*audio_stream)->codec, avcodec_close);
\r
140 CASPAR_LOG(info) << "Started ffmpeg_producer::read_file Thread for " << filename_.c_str();
\r
141 win32_exception::install_handler();
\r
143 is_running_ = true;
\r
144 AVPacket tmp_packet;
\r
147 std::shared_ptr<AVPacket> packet(&tmp_packet, av_free_packet);
\r
148 tbb::queuing_mutex::scoped_lock lock(seek_mutex_);
\r
150 if (av_read_frame(format_context_.get(), packet.get()) >= 0) // NOTE: Packet is only valid until next call of av_safe_ptr<read_frame> or av_close_input_file
\r
152 auto buffer = std::make_shared<aligned_buffer>(packet->data, packet->data + packet->size);
\r
153 if(packet->stream_index == video_s_index_)
\r
154 video_packet_buffer_.push(buffer);
\r
155 else if(packet->stream_index == audio_s_index_)
\r
156 audio_packet_buffer_.push(buffer);
\r
158 else if(!loop_ || av_seek_frame(format_context_.get(), -1, 0, AVSEEK_FLAG_BACKWARD) < 0) // TODO: av_seek_frame does not work for all formats
\r
159 is_running_ = false;
\r
162 is_running_ = false;
\r
164 CASPAR_LOG(info) << " Ended ffmpeg_producer::read_file Thread for " << filename_.c_str();
\r
167 aligned_buffer get_video_packet()
\r
169 std::shared_ptr<aligned_buffer> video_packet;
\r
170 if(video_packet_buffer_.try_pop(video_packet))
\r
171 return std::move(*video_packet);
\r
172 return aligned_buffer();
\r
175 aligned_buffer get_audio_packet()
\r
177 std::shared_ptr<aligned_buffer> audio_packet;
\r
178 if(audio_packet_buffer_.try_pop(audio_packet))
\r
179 return std::move(*audio_packet);
\r
180 return aligned_buffer();
\r
183 bool is_eof() const
\r
185 return !is_running_ && video_packet_buffer_.empty() && audio_packet_buffer_.empty();
\r
188 bool seek(unsigned long long seek_target)
\r
190 tbb::queuing_mutex::scoped_lock lock(seek_mutex_);
\r
191 if(av_seek_frame(format_context_.get(), -1, seek_target*AV_TIME_BASE, 0) < 0)
\r
194 video_packet_buffer_.clear();
\r
195 audio_packet_buffer_.clear();
\r
196 // TODO: Not sure its enough to jsut flush in input class
\r
197 if(video_codec_context_)
\r
198 avcodec_flush_buffers(video_codec_context_.get());
\r
199 if(audio_codex_context_)
\r
200 avcodec_flush_buffers(audio_codex_context_.get());
\r
204 std::shared_ptr<AVFormatContext> format_context_; // Destroy this last
\r
206 tbb::queuing_mutex seek_mutex_;
\r
208 std::string filename_;
\r
210 std::shared_ptr<AVCodecContext> video_codec_context_;
\r
211 AVCodec* video_codec_;
\r
213 std::shared_ptr<AVCodecContext> audio_codex_context_;
\r
214 AVCodec* audio_codec_;
\r
216 tbb::atomic<bool> loop_;
\r
217 int video_s_index_;
\r
218 int audio_s_index_;
\r
220 tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>> video_packet_buffer_;
\r
221 tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>> audio_packet_buffer_;
\r
222 boost::thread io_thread_;
\r
223 tbb::atomic<bool> is_running_;
\r
226 input::input() : impl_(new implementation()){}
\r
227 void input::load(const std::string& filename){impl_->load(filename);}
\r
228 void input::set_loop(bool value){impl_->loop_ = value;}
\r
229 const std::shared_ptr<AVCodecContext>& input::get_video_codec_context() const{return impl_->video_codec_context_;}
\r
230 const std::shared_ptr<AVCodecContext>& input::get_audio_codec_context() const{return impl_->audio_codex_context_;}
\r
231 bool input::is_eof() const{return impl_->is_eof();}
\r
232 aligned_buffer input::get_video_packet(){return impl_->get_video_packet();}
\r
233 aligned_buffer input::get_audio_packet(){return impl_->get_audio_packet();}
\r
234 bool input::seek(unsigned long long frame){return impl_->seek(frame);}
\r