]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/input.cpp
2.0.0.2:
[casparcg] / modules / ffmpeg / producer / input.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */\r
20 #include "..\stdafx.h"\r
21 \r
22 #include "input.h"\r
23 #include "../ffmpeg_error.h"\r
24 \r
25 #include <core/video_format.h>\r
26 \r
27 #include <common/concurrency/executor.h>\r
28 #include <common/diagnostics/graph.h>\r
29 \r
30 #include <tbb/concurrent_queue.h>\r
31 #include <tbb/queuing_mutex.h>\r
32 \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
37 \r
38 #include <errno.h>\r
39 #include <system_error>\r
40                 \r
41 #if defined(_MSC_VER)\r
42 #pragma warning (disable : 4244)\r
43 #endif\r
44 \r
45 extern "C" \r
46 {\r
47         #define __STDC_CONSTANT_MACROS\r
48         #define __STDC_LIMIT_MACROS\r
49         #include <libavformat/avformat.h>\r
50 }\r
51 \r
52 namespace caspar {\r
53                 \r
54 struct input::implementation : boost::noncopyable\r
55 {               \r
56         static const size_t PACKET_BUFFER_COUNT = 25;\r
57 \r
58         safe_ptr<diagnostics::graph> graph_;\r
59 \r
60         std::shared_ptr<AVFormatContext> format_context_;       // Destroy this last\r
61 \r
62         std::shared_ptr<AVCodecContext> video_codec_context_;\r
63         std::shared_ptr<AVCodecContext> audio_codex_context_;\r
64         \r
65         const std::wstring filename_;\r
66 \r
67         bool loop_;\r
68         int video_s_index_;\r
69         int     audio_s_index_;\r
70         const int start_frame_;\r
71         const int end_frame_;\r
72         int eof_count_;\r
73                 \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
76 \r
77         boost::condition_variable cond_;\r
78         boost::mutex mutex_;\r
79         \r
80         std::exception_ptr exception_;\r
81         executor executor_;\r
82 public:\r
83         explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start_frame, int end_frame) \r
84                 : graph_(graph)\r
85                 , loop_(loop)\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
93         {                       \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
99 \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
102 \r
103                 int errn;\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
113 \r
114                 format_context_.reset(weak_format_context_, av_close_input_file);\r
115                         \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
123 \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
127                 else\r
128                         fix_time_base(video_codec_context_.get());\r
129                 \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
133                 else\r
134                         fix_time_base(video_codec_context_.get());\r
135 \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
142                 \r
143                 if(start_frame_ != 0)                   \r
144                         seek_frame(start_frame_);\r
145                                         \r
146                 executor_.start();\r
147                 executor_.begin_invoke([this]{read_file();});\r
148                 CASPAR_LOG(info) << print() << " Started.";\r
149         }\r
150 \r
151         ~implementation()\r
152         {\r
153                 executor_.clear();\r
154                 executor_.stop();\r
155                 cond_.notify_all();\r
156                 CASPAR_LOG(info) << print() << " Stopped.";\r
157         }\r
158                         \r
159         void fix_time_base(AVCodecContext* context) // Some files give an invalid numerator, try to fix it.\r
160         {\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
163         }\r
164 \r
165         std::shared_ptr<AVCodecContext> open_stream(int codec_type, int& s_index)\r
166         {               \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
170                 \r
171                 if(stream == streams_end) \r
172                         return nullptr;\r
173                 \r
174                 auto codec = avcodec_find_decoder((*stream)->codec->codec_id);                  \r
175                 if(codec == nullptr)\r
176                         return nullptr;\r
177                         \r
178                 if((-avcodec_open((*stream)->codec, codec)) > 0)                \r
179                         return nullptr;\r
180                 \r
181                 s_index = (*stream)->index;\r
182 \r
183                 return std::shared_ptr<AVCodecContext>((*stream)->codec, avcodec_close);\r
184         }\r
185         \r
186         std::shared_ptr<AVCodecContext>& get_default_context()\r
187         {\r
188                 return video_codec_context_ ? video_codec_context_ : audio_codex_context_;\r
189         }\r
190 \r
191         bool is_eof(int errn)\r
192         {\r
193                 if(end_frame_ != -1)\r
194                         return get_default_context()->frame_number > eof_count_;                \r
195 \r
196                 return errn == AVERROR_EOF || errn == AVERROR_IO;\r
197         }\r
198                 \r
199         void read_file()\r
200         {               \r
201                 try\r
202                 {\r
203                         AVPacket tmp_packet;\r
204                         safe_ptr<AVPacket> read_packet(&tmp_packet, av_free_packet);    \r
205 \r
206                         auto read_frame_ret = av_read_frame(format_context_.get(), read_packet.get());\r
207                         if(is_eof(read_frame_ret))\r
208                         {\r
209                                 if(loop_)\r
210                                 {\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
215                                 }       \r
216                                 else\r
217                                         stop();\r
218                         }\r
219                         else if(read_frame_ret < 0)\r
220                         {\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
227                         }\r
228                         else\r
229                         {\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
235                         }\r
236                                                 \r
237                         graph_->update_value("input-buffer", static_cast<float>(video_packet_buffer_.size())/static_cast<float>(PACKET_BUFFER_COUNT));          \r
238                 }\r
239                 catch(...)\r
240                 {\r
241                         stop();\r
242                         CASPAR_LOG_CURRENT_EXCEPTION();\r
243                         return;\r
244                 }\r
245                                 \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
249                         cond_.wait(lock);               \r
250         }\r
251 \r
252         void stop()\r
253         {\r
254                 executor_.stop();\r
255                 CASPAR_LOG(info) << print() << " eof";\r
256         }\r
257                 \r
258         std::shared_ptr<aligned_buffer> get_video_packet()\r
259         {\r
260                 return get_packet(video_packet_buffer_);\r
261         }\r
262 \r
263         std::shared_ptr<aligned_buffer> get_audio_packet()\r
264         {\r
265                 return get_packet(audio_packet_buffer_);\r
266         }\r
267 \r
268         bool has_packet() const\r
269         {\r
270                 return !video_packet_buffer_.empty() || !audio_packet_buffer_.empty();\r
271         }\r
272         \r
273         void seek_frame(int64_t frame, int flags = 0)\r
274         {       \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
277 \r
278                 const int errn = av_seek_frame(format_context_.get(), -1, ts, flags | AVSEEK_FLAG_FRAME);\r
279 \r
280                 if(errn < 0)\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
287                 \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
291         }\r
292         \r
293         std::shared_ptr<aligned_buffer> get_packet(tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>>& buffer)\r
294         {\r
295                 cond_.notify_all();\r
296                 std::shared_ptr<aligned_buffer> packet;\r
297                 return buffer.try_pop(packet) ? packet : nullptr;\r
298         }\r
299                         \r
300         double fps()\r
301         {\r
302                 return static_cast<double>(get_default_context()->time_base.den) / static_cast<double>(get_default_context()->time_base.num);\r
303         }\r
304 \r
305         std::wstring print() const\r
306         {\r
307                 return L"ffmpeg_input[" + filename_ + L"]";\r
308         }\r
309 };\r
310 \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
319 }