]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/input.cpp
2.0.0.2: ffmpeg_input: Fixed memory leak wheen looping files with large audio vs...
[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 #if defined(_MSC_VER)\r
21 #pragma warning (disable : 4244)\r
22 #endif\r
23 \r
24 #include "..\stdafx.h"\r
25 \r
26 #include "input.h"\r
27 #include "../ffmpeg_error.h"\r
28 #include "../tbb_avcodec.h"\r
29 \r
30 #include <core/video_format.h>\r
31 \r
32 #include <common/concurrency/executor.h>\r
33 #include <common/diagnostics/graph.h>\r
34 \r
35 #include <tbb/concurrent_queue.h>\r
36 #include <tbb/mutex.h>\r
37 \r
38 #include <boost/range/iterator_range.hpp>\r
39 #include <boost/range/algorithm.hpp>\r
40 \r
41 extern "C" \r
42 {\r
43         #define __STDC_CONSTANT_MACROS\r
44         #define __STDC_LIMIT_MACROS\r
45         #include <libavformat/avformat.h>\r
46 }\r
47 \r
48 namespace caspar {\r
49                 \r
50 struct input::implementation : boost::noncopyable\r
51 {               \r
52         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
53 \r
54         safe_ptr<diagnostics::graph> graph_;\r
55 \r
56         std::shared_ptr<AVFormatContext> format_context_;       // Destroy this last\r
57 \r
58         std::shared_ptr<AVCodecContext> video_codec_context_;\r
59         std::shared_ptr<AVCodecContext> audio_codex_context_;\r
60         \r
61         const std::wstring      filename_;\r
62         const bool                      loop_;\r
63         int                                     video_s_index_;\r
64         int                                     audio_s_index_;\r
65         const int                       start_;\r
66         const int                       length_;\r
67         int                                     eof_count_;\r
68                 \r
69         tbb::concurrent_bounded_queue<packet> video_packet_buffer_;\r
70         tbb::concurrent_bounded_queue<packet> audio_packet_buffer_;\r
71                 \r
72         std::exception_ptr exception_;\r
73         executor executor_;\r
74 public:\r
75         explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start, int length) \r
76                 : graph_(graph)\r
77                 , loop_(loop)\r
78                 , video_s_index_(-1)\r
79                 , audio_s_index_(-1)\r
80                 , filename_(filename)\r
81                 , executor_(print())\r
82                 , start_(std::max(start, 0))\r
83                 , length_(length)\r
84                 , eof_count_(length)\r
85         {               \r
86                 graph_->set_color("input-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));\r
87                 graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f));        \r
88                 \r
89                 int errn;\r
90 \r
91                 AVFormatContext* weak_format_context_ = nullptr;\r
92                 errn = errn = av_open_input_file(&weak_format_context_, narrow(filename).c_str(), nullptr, 0, nullptr);\r
93                 if(errn < 0 || weak_format_context_ == nullptr)\r
94                 {       \r
95                         BOOST_THROW_EXCEPTION(\r
96                                 file_read_error() << \r
97                                 source_info(narrow(print())) << \r
98                                 msg_info(av_error_str(errn)) <<\r
99                                 boost::errinfo_api_function("av_open_input_file") <<\r
100                                 boost::errinfo_errno(AVUNERROR(errn)) <<\r
101                                 boost::errinfo_file_name(narrow(filename)));\r
102                 }\r
103 \r
104                 format_context_.reset(weak_format_context_, av_close_input_file);\r
105                         \r
106                 errn = errn = av_find_stream_info(format_context_.get());\r
107                 if(errn < 0)\r
108                 {       \r
109                         BOOST_THROW_EXCEPTION(\r
110                                 file_read_error() << \r
111                                 source_info(narrow(print())) << \r
112                                 msg_info(av_error_str(errn)) <<\r
113                                 boost::errinfo_api_function("av_find_stream_info") <<\r
114                                 boost::errinfo_errno(AVUNERROR(errn)));\r
115                 }\r
116                 \r
117                 errn = open_stream(video_codec_context_, AVMEDIA_TYPE_VIDEO, video_s_index_);\r
118                 if(errn < 0 || !video_codec_context_)\r
119                         CASPAR_LOG(warning) << print() << L" Could not open video stream: " << widen(av_error_str(errn));\r
120                 \r
121                 errn = open_stream(audio_codex_context_, AVMEDIA_TYPE_AUDIO, audio_s_index_);\r
122                 if(errn < 0 || !audio_codex_context_)\r
123                         CASPAR_LOG(warning) << print() << L" Could not open audio stream: " << widen(av_error_str(errn));\r
124                 \r
125                 if(!video_codec_context_ && !audio_codex_context_)\r
126                 {       \r
127                         BOOST_THROW_EXCEPTION(\r
128                                 file_read_error() << \r
129                                 source_info(narrow(print())) << \r
130                                 msg_info("No video or audio codec context found."));    \r
131                 }\r
132 \r
133                 video_packet_buffer_.set_capacity(PACKET_BUFFER_COUNT);\r
134                 audio_packet_buffer_.set_capacity(PACKET_BUFFER_COUNT);\r
135                 \r
136                 if(start_ != 0)                 \r
137                         seek_frame(start_);\r
138                                         \r
139                 executor_.start();\r
140                 executor_.begin_invoke([this]{read_file();});\r
141                 CASPAR_LOG(info) << print() << " Started.";\r
142         }\r
143 \r
144         ~implementation()\r
145         {\r
146                 stop();\r
147         }\r
148                 \r
149         packet get_video_packet()\r
150         {\r
151                 return get_packet(video_packet_buffer_);\r
152         }\r
153 \r
154         packet get_audio_packet()\r
155         {\r
156                 return get_packet(audio_packet_buffer_);\r
157         }\r
158 \r
159         bool has_packet() const\r
160         {\r
161                 return !video_packet_buffer_.empty() || !audio_packet_buffer_.empty();\r
162         }\r
163                                 \r
164         double fps()\r
165         {\r
166                 return static_cast<double>(get_default_context()->time_base.den) / static_cast<double>(get_default_context()->time_base.num);\r
167         }\r
168 \r
169 private:\r
170 \r
171         void stop()\r
172         {\r
173                 executor_.stop();\r
174                 get_video_packet();\r
175                 get_audio_packet();\r
176                 CASPAR_LOG(info) << print() << " Stopping.";\r
177         }\r
178                         \r
179         int open_stream(std::shared_ptr<AVCodecContext>& ctx, int codec_type, int& s_index) const\r
180         {               \r
181                 const auto streams = boost::iterator_range<AVStream**>(format_context_->streams, format_context_->streams+format_context_->nb_streams);\r
182                 const auto stream = boost::find_if(streams, [&](AVStream* stream) \r
183                 {\r
184                         return stream != nullptr && stream->codec->codec_type == codec_type;\r
185                 });\r
186                 \r
187                 if(stream == streams.end()) \r
188                         return AVERROR_STREAM_NOT_FOUND;\r
189                 \r
190                 auto codec = avcodec_find_decoder((*stream)->codec->codec_id);                  \r
191                 if(codec == nullptr)\r
192                         return AVERROR_DECODER_NOT_FOUND;\r
193                         \r
194                 s_index = (*stream)->index;\r
195 \r
196                 int errn = tbb_avcodec_open((*stream)->codec, codec);\r
197                 if(errn >= 0)\r
198                 {\r
199                         ctx.reset((*stream)->codec, tbb_avcodec_close);\r
200 \r
201                         // Some files give an invalid time_base numerator, try to fix it.\r
202                         if(ctx && ctx->time_base.num == 1)\r
203                                 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
204                 }\r
205                 return errn;    \r
206         }\r
207         \r
208         std::shared_ptr<AVCodecContext>& get_default_context()\r
209         {\r
210                 return video_codec_context_ ? video_codec_context_ : audio_codex_context_;\r
211         }\r
212                 \r
213         void read_file()\r
214         {               \r
215                 if(audio_packet_buffer_.size() > 4 && video_packet_buffer_.size() > 4)\r
216                         boost::this_thread::yield(); // There are enough packets, no hurry.\r
217 \r
218                 try\r
219                 {\r
220                         std::shared_ptr<AVPacket> read_packet(new AVPacket(), [](AVPacket* p)\r
221                         {\r
222                                 av_free_packet(p);\r
223                                 delete p;\r
224                         });\r
225 \r
226                         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
227                         if(is_eof(errn))                                                                                                                  // Use av_dup_packet to extend its life.\r
228                         {\r
229                                 if(loop_)\r
230                                 {\r
231                                         seek_frame(start_, AVSEEK_FLAG_BACKWARD);\r
232                                         eof_count_ += length_; // AVCodecContext.frame_number is not reset. Increase the target frame_number.\r
233                                         graph_->add_tag("seek");                \r
234                                         CASPAR_LOG(info) << print() << " Received EOF. Looping.";                       \r
235                                 }       \r
236                                 else\r
237                                 {\r
238                                         stop();\r
239                                         CASPAR_LOG(info) << print() << " Received EOF. Stopping.";\r
240                                 }\r
241                         }\r
242                         else if(errn < 0)\r
243                         {\r
244                                 BOOST_THROW_EXCEPTION(\r
245                                         file_read_error() <<\r
246                                         msg_info(av_error_str(errn)) <<\r
247                                         source_info(narrow(print())) << \r
248                                         boost::errinfo_api_function("av_read_frame") <<\r
249                                         boost::errinfo_errno(AVUNERROR(errn)));\r
250                         }\r
251                         else\r
252                         {\r
253                                 if(read_packet->stream_index == video_s_index_)                 \r
254                                         push_packet(video_packet_buffer_, read_packet);\r
255                                 else if(read_packet->stream_index == audio_s_index_)    \r
256                                         push_packet(audio_packet_buffer_, read_packet);\r
257                         }\r
258                                                 \r
259                         graph_->update_value("input-buffer", static_cast<float>(video_packet_buffer_.size())/static_cast<float>(PACKET_BUFFER_COUNT));          \r
260                 }\r
261                 catch(...)\r
262                 {\r
263                         stop();\r
264                         CASPAR_LOG_CURRENT_EXCEPTION();\r
265                         return;\r
266                 }\r
267                                                 \r
268                 executor_.begin_invoke([this]{read_file();});           \r
269         }\r
270 \r
271         void seek_frame(int64_t frame, int flags = 0)\r
272         {       \r
273                 // Convert from frames into seconds.\r
274                 const auto ts = frame*static_cast<int64_t>((AV_TIME_BASE*get_default_context()->time_base.num) / get_default_context()->time_base.den);\r
275 \r
276                 const int errn = av_seek_frame(format_context_.get(), -1, ts, flags | AVSEEK_FLAG_FRAME);\r
277                 if(errn < 0)\r
278                 {       \r
279                         BOOST_THROW_EXCEPTION(\r
280                                 invalid_operation() << \r
281                                 source_info(narrow(print())) << \r
282                                 msg_info(av_error_str(errn)) <<\r
283                                 boost::errinfo_api_function("seek_frame") <<\r
284                                 boost::errinfo_errno(AVUNERROR(errn)));\r
285                 }\r
286 \r
287                 // Notify decoders to flush buffers.\r
288                 video_packet_buffer_.try_push(flush_packet);    \r
289                 audio_packet_buffer_.try_push(flush_packet);\r
290         }               \r
291 \r
292         bool is_eof(int errn)\r
293         {\r
294                 if(length_ != -1)\r
295                         return get_default_context()->frame_number > eof_count_;                \r
296 \r
297                 if(errn == AVERROR(EIO))\r
298                         CASPAR_LOG(warning) << print() << " Received EIO, assuming EOF";\r
299 \r
300                 return errn == AVERROR_EOF || errn == AVERROR(EIO); // av_read_frame doesn't always correctly return AVERROR_EOF;\r
301         }\r
302         \r
303         void push_packet(tbb::concurrent_bounded_queue<packet>& buffer, const std::shared_ptr<AVPacket>& read_packet)\r
304         {\r
305                 av_dup_packet(read_packet.get());\r
306                 buffer.push(packet(read_packet));       \r
307         }\r
308 \r
309         packet get_packet(tbb::concurrent_bounded_queue<packet>& buffer)\r
310         {\r
311                 packet packet;\r
312                 buffer.try_pop(packet);\r
313                 return packet;\r
314         }\r
315 \r
316         std::wstring print() const\r
317         {\r
318                 return L"ffmpeg_input[" + filename_ + L"]";\r
319         }\r
320 };\r
321 \r
322 input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start, int length) \r
323         : impl_(new implementation(graph, filename, loop, start, length)){}\r
324 const std::shared_ptr<AVCodecContext>& input::get_video_codec_context() const{return impl_->video_codec_context_;}\r
325 const std::shared_ptr<AVCodecContext>& input::get_audio_codec_context() const{return impl_->audio_codex_context_;}\r
326 bool input::has_packet() const{return impl_->has_packet();}\r
327 bool input::is_running() const {return impl_->executor_.is_running();}\r
328 packet input::get_video_packet(){return impl_->get_video_packet();}\r
329 packet input::get_audio_packet(){return impl_->get_audio_packet();}\r
330 double input::fps() const { return impl_->fps(); }\r
331 }