]> git.sesse.net Git - casparcg/blob - core/producer/ffmpeg/input.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / ffmpeg / input.cpp
1 #include "..\..\stdafx.h"\r
2 \r
3 #include "input.h"\r
4 \r
5 #include "../../format/video_format.h"\r
6 \r
7 #include <common/concurrency/executor.h>\r
8 \r
9 #include <tbb/concurrent_queue.h>\r
10 #include <tbb/queuing_mutex.h>\r
11 \r
12 #include <boost/exception/error_info.hpp>\r
13 #include <boost/thread/once.hpp>\r
14 \r
15 #include <errno.h>\r
16 #include <system_error>\r
17                 \r
18 #if defined(_MSC_VER)\r
19 #pragma warning (disable : 4244)\r
20 #endif\r
21 \r
22 \r
23 extern "C" \r
24 {\r
25         #define __STDC_CONSTANT_MACROS\r
26         #define __STDC_LIMIT_MACROS\r
27         #include <libavformat/avformat.h>\r
28 }\r
29 \r
30 namespace caspar { namespace core { namespace ffmpeg{\r
31                 \r
32 struct input::implementation : boost::noncopyable\r
33 {                               \r
34         std::shared_ptr<AVFormatContext>        format_context_;        // Destroy this last\r
35 \r
36         std::shared_ptr<AVCodecContext>         video_codec_context_;\r
37         std::shared_ptr<AVCodecContext>         audio_codex_context_;\r
38 \r
39         tbb::queuing_mutex                                      seek_mutex_;\r
40 \r
41         const std::wstring                                      filename_;\r
42 \r
43         tbb::atomic<bool>                                       loop_;\r
44         int                                                                     video_s_index_;\r
45         int                                                                     audio_s_index_;\r
46 \r
47         tbb::atomic<size_t>     buffer_size_;\r
48         \r
49         tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>> video_packet_buffer_;\r
50         tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>> audio_packet_buffer_;\r
51         \r
52         executor executor_;\r
53 \r
54         static const size_t BUFFER_SIZE = 2 << 25;\r
55 \r
56 public:\r
57         explicit implementation(const std::wstring& filename) \r
58                 : video_s_index_(-1)\r
59                 , audio_s_index_(-1)\r
60                 , filename_(filename)\r
61         {               \r
62                 static boost::once_flag av_register_all_flag = BOOST_ONCE_INIT;\r
63                 boost::call_once(av_register_all, av_register_all_flag);        \r
64                 \r
65                 static boost::once_flag avcodec_init_flag = BOOST_ONCE_INIT;\r
66                 boost::call_once(avcodec_init, avcodec_init_flag);      \r
67 \r
68                 loop_ = false;  \r
69                 \r
70                 int errn;\r
71                 AVFormatContext* weak_format_context_;\r
72                 if((errn = -av_open_input_file(&weak_format_context_, narrow(filename).c_str(), nullptr, 0, nullptr)) > 0)\r
73                         BOOST_THROW_EXCEPTION(\r
74                                 file_read_error() << \r
75                                 msg_info("No format context found.") << \r
76                                 boost::errinfo_api_function("av_open_input_file") <<\r
77                                 boost::errinfo_errno(errn) <<\r
78                                 boost::errinfo_file_name(narrow(filename)));\r
79 \r
80                 format_context_.reset(weak_format_context_, av_close_input_file);\r
81                         \r
82                 if((errn = -av_find_stream_info(format_context_.get())) > 0)\r
83                         BOOST_THROW_EXCEPTION(\r
84                                 file_read_error() << \r
85                                 boost::errinfo_api_function("av_find_stream_info") <<\r
86                                 msg_info("No stream found.") << \r
87                                 boost::errinfo_errno(errn));\r
88 \r
89                 video_codec_context_ = open_stream(CODEC_TYPE_VIDEO, video_s_index_);\r
90                 if(!video_codec_context_)\r
91                         CASPAR_LOG(warning) << "Could not open any video stream.";\r
92                 \r
93                 audio_codex_context_ = open_stream(CODEC_TYPE_AUDIO, audio_s_index_);\r
94                 if(!audio_codex_context_)\r
95                         CASPAR_LOG(warning) << "Could not open any audio stream.";\r
96 \r
97                 if(!video_codec_context_ && !audio_codex_context_)\r
98                         BOOST_THROW_EXCEPTION(file_read_error() << msg_info("No video or audio codec context found."));         \r
99                         \r
100                 executor_.start();\r
101                 executor_.begin_invoke([this]{read_file();});\r
102                 CASPAR_LOG(info) << print() << " started.";\r
103         }\r
104 \r
105         ~implementation()\r
106         {\r
107                 CASPAR_LOG(info) << print() << " ended.";\r
108         }\r
109                                                         \r
110         std::shared_ptr<AVCodecContext> open_stream(int codec_type, int& s_index)\r
111         {               \r
112                 AVStream** streams_end = format_context_->streams+format_context_->nb_streams;\r
113                 AVStream** stream = std::find_if(format_context_->streams, streams_end, \r
114                         [&](AVStream* stream) { return stream != nullptr && stream->codec->codec_type == codec_type ;});\r
115                 \r
116                 if(stream == streams_end) \r
117                         return nullptr;\r
118 \r
119                 s_index = (*stream)->index;\r
120                 \r
121                 auto codec = avcodec_find_decoder((*stream)->codec->codec_id);                  \r
122                 if(codec == nullptr)\r
123                         return nullptr;\r
124                         \r
125                 if((-avcodec_open((*stream)->codec, codec)) > 0)                \r
126                         return nullptr;\r
127 \r
128                 return std::shared_ptr<AVCodecContext>((*stream)->codec, avcodec_close);\r
129         }\r
130                 \r
131         void read_file() // For every packet taken: read in a number of packets.\r
132         {               \r
133                 for(size_t n = 0; buffer_size_ < BUFFER_SIZE && (n < 3 || video_packet_buffer_.size() < 3 || audio_packet_buffer_.size() < 3) && executor_.is_running(); ++n)\r
134                 {\r
135                         AVPacket tmp_packet;\r
136                         safe_ptr<AVPacket> read_packet(&tmp_packet, av_free_packet);    \r
137                         tbb::queuing_mutex::scoped_lock lock(seek_mutex_);      \r
138 \r
139                         if (av_read_frame(format_context_.get(), read_packet.get()) >= 0) // NOTE: read_packet is only valid until next call of av_safe_ptr<read_frame> or av_close_input_file\r
140                         {\r
141                                 auto packet = std::make_shared<aligned_buffer>(read_packet->data, read_packet->data + read_packet->size);\r
142                                 if(read_packet->stream_index == video_s_index_)                                                 \r
143                                 {\r
144                                         buffer_size_ += packet->size();\r
145                                         video_packet_buffer_.try_push(std::move(packet));                                               \r
146                                 }\r
147                                 else if(read_packet->stream_index == audio_s_index_)    \r
148                                 {\r
149                                         buffer_size_ += packet->size();\r
150                                         audio_packet_buffer_.try_push(std::move(packet));\r
151                                 }\r
152                         }\r
153                         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
154                                 executor_.stop(executor::no_wait);\r
155                 }\r
156         }\r
157                 \r
158         aligned_buffer get_video_packet()\r
159         {\r
160                 return get_packet(video_packet_buffer_);\r
161         }\r
162 \r
163         aligned_buffer get_audio_packet()\r
164         {\r
165                 return get_packet(audio_packet_buffer_);\r
166         }\r
167         \r
168         aligned_buffer get_packet(tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>>& buffer)\r
169         {\r
170                 std::shared_ptr<aligned_buffer> packet;\r
171                 if(buffer.try_pop(packet))\r
172                 {\r
173                         buffer_size_ -= packet->size();\r
174                         if(executor_.size() < 4)\r
175                                 executor_.begin_invoke([this]{read_file();});\r
176                         return std::move(*packet);\r
177                 }\r
178                 return aligned_buffer();\r
179         }\r
180 \r
181         bool is_eof() const\r
182         {\r
183                 return !executor_.is_running() && video_packet_buffer_.empty() && audio_packet_buffer_.empty();\r
184         }\r
185                 \r
186         // TODO: Not properly done.\r
187         bool seek(unsigned long long seek_target)\r
188         {\r
189                 tbb::queuing_mutex::scoped_lock lock(seek_mutex_);\r
190                 if(av_seek_frame(format_context_.get(), -1, seek_target*AV_TIME_BASE, 0) < 0)\r
191                         return false;\r
192 \r
193                 return true;\r
194         }\r
195 \r
196         double fps() const\r
197         {\r
198                 return static_cast<double>(video_codec_context_->time_base.den) / static_cast<double>(video_codec_context_->time_base.num);\r
199         }\r
200 \r
201         std::wstring print() const\r
202         {\r
203                 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"] Buffer thread";\r
204         }\r
205 };\r
206 \r
207 input::input(const std::wstring& filename) : impl_(new implementation(filename)){}\r
208 void input::set_loop(bool value){impl_->loop_ = value;}\r
209 const std::shared_ptr<AVCodecContext>& input::get_video_codec_context() const{return impl_->video_codec_context_;}\r
210 const std::shared_ptr<AVCodecContext>& input::get_audio_codec_context() const{return impl_->audio_codex_context_;}\r
211 bool input::is_eof() const{return impl_->is_eof();}\r
212 aligned_buffer input::get_video_packet(){return impl_->get_video_packet();}\r
213 aligned_buffer input::get_audio_packet(){return impl_->get_audio_packet();}\r
214 bool input::seek(unsigned long long frame){return impl_->seek(frame);}\r
215 double input::fps() const { return impl_->fps(); }\r
216 }}}