]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/input.cpp
2.0: ffmpeg_producer: Fixed bug where last avpacket was discarded.
[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 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
51 \r
52 class stream\r
53 {\r
54         std::shared_ptr<AVCodecContext> ctx_;\r
55         int index_;\r
56         tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> buffer_;\r
57 \r
58 public:\r
59 \r
60         stream() : index_(-1)\r
61         {\r
62                 buffer_.set_capacity(PACKET_BUFFER_COUNT);\r
63         }\r
64         \r
65         int open(std::shared_ptr<AVFormatContext>& fctx, AVMediaType media_type)\r
66         {               \r
67                 const auto streams = boost::iterator_range<AVStream**>(fctx->streams, fctx->streams+fctx->nb_streams);\r
68                 const auto it = boost::find_if(streams, [&](AVStream* stream) \r
69                 {\r
70                         return stream && stream->codec->codec_type == media_type;\r
71                 });\r
72                 \r
73                 if(it == streams.end()) \r
74                         return AVERROR_STREAM_NOT_FOUND;\r
75                 \r
76                 auto codec = avcodec_find_decoder((*it)->codec->codec_id);                      \r
77                 if(!codec)\r
78                         return AVERROR_DECODER_NOT_FOUND;\r
79                         \r
80                 index_ = (*it)->index;\r
81 \r
82                 int errn = tbb_avcodec_open((*it)->codec, codec);\r
83                 if(errn < 0)\r
84                         return errn;\r
85                                 \r
86                 ctx_.reset((*it)->codec, tbb_avcodec_close);\r
87 \r
88                 // Some files give an invalid time_base numerator, try to fix it.\r
89                 if(ctx_ && ctx_->time_base.num == 1)\r
90                         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
91                 \r
92                 return errn;    \r
93         }\r
94 \r
95         bool try_pop(std::shared_ptr<AVPacket>& pkt)\r
96         {\r
97                 return buffer_.try_pop(pkt);\r
98         }\r
99 \r
100         void push(const std::shared_ptr<AVPacket>& pkt)\r
101         {\r
102                 if(pkt && pkt->stream_index != index_)\r
103                         return;\r
104 \r
105                 if(!ctx_)\r
106                         return;\r
107 \r
108                 if(pkt)\r
109                         av_dup_packet(pkt.get());\r
110 \r
111                 buffer_.push(pkt);      \r
112         }\r
113 \r
114         int index() const {return index_;}\r
115         \r
116         const std::shared_ptr<AVCodecContext>& ctx() const { return ctx_; }\r
117 \r
118         operator bool(){return ctx_ != nullptr;}\r
119 \r
120         double fps() const { return !ctx_ ? -1.0 : static_cast<double>(ctx_->time_base.den) / static_cast<double>(ctx_->time_base.num); }\r
121 \r
122         bool empty() const { return buffer_.empty();}\r
123         int size() const { return buffer_.size();}\r
124 };\r
125                 \r
126 struct input::implementation : boost::noncopyable\r
127 {               \r
128         safe_ptr<diagnostics::graph> graph_;\r
129 \r
130         std::shared_ptr<AVFormatContext> format_context_;       // Destroy this last\r
131                 \r
132         const std::wstring      filename_;\r
133         const bool                      loop_;\r
134         const int                       start_;         \r
135         double                          fps_;\r
136 \r
137         stream video_stream_;\r
138         stream audio_stream_;\r
139                 \r
140         std::exception_ptr exception_;\r
141         executor executor_;\r
142 public:\r
143         explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start) \r
144                 : graph_(graph)\r
145                 , loop_(loop)\r
146                 , filename_(filename)\r
147                 , executor_(print())\r
148                 , start_(std::max(start, 0))\r
149         {                               \r
150                 int errn;\r
151 \r
152                 AVFormatContext* weak_format_context_ = nullptr;\r
153                 errn = av_open_input_file(&weak_format_context_, narrow(filename).c_str(), nullptr, 0, nullptr);\r
154                 if(errn < 0 || weak_format_context_ == nullptr)\r
155                 {       \r
156                         BOOST_THROW_EXCEPTION(\r
157                                 file_read_error() << \r
158                                 source_info(narrow(print())) << \r
159                                 msg_info(av_error_str(errn)) <<\r
160                                 boost::errinfo_api_function("av_open_input_file") <<\r
161                                 boost::errinfo_errno(AVUNERROR(errn)) <<\r
162                                 boost::errinfo_file_name(narrow(filename)));\r
163                 }\r
164 \r
165                 format_context_.reset(weak_format_context_, av_close_input_file);\r
166                         \r
167                 errn = av_find_stream_info(format_context_.get());\r
168                 if(errn < 0)\r
169                 {       \r
170                         BOOST_THROW_EXCEPTION(\r
171                                 file_read_error() << \r
172                                 source_info(narrow(print())) << \r
173                                 msg_info(av_error_str(errn)) <<\r
174                                 boost::errinfo_api_function("av_find_stream_info") <<\r
175                                 boost::errinfo_errno(AVUNERROR(errn)));\r
176                 }\r
177                 \r
178                 errn = video_stream_.open(format_context_, AVMEDIA_TYPE_VIDEO);\r
179                 if(errn < 0)\r
180                         CASPAR_LOG(warning) << print() << L" Could not open video stream: " << widen(av_error_str(errn));\r
181                 \r
182                 errn = audio_stream_.open(format_context_, AVMEDIA_TYPE_AUDIO);\r
183                 if(errn < 0)\r
184                         CASPAR_LOG(warning) << print() << L" Could not open audio stream: " << widen(av_error_str(errn));\r
185                 \r
186                 if(!video_stream_ && !audio_stream_)\r
187                 {       \r
188                         BOOST_THROW_EXCEPTION(\r
189                                 file_read_error() << \r
190                                 source_info(narrow(print())) << \r
191                                 msg_info("No video or audio codec context found."));    \r
192                 }\r
193 \r
194                 fps_ = video_stream_ ? video_stream_.fps() : audio_stream_.fps();\r
195 \r
196                 if(start_ != 0)                 \r
197                         seek_frame(start_);\r
198 \r
199                 for(size_t n = 0; n < 32; ++n) // Read some packets for pre-rolling.\r
200                         read_next_packet();\r
201                                                 \r
202                 if(audio_stream_)\r
203                         graph_->set_color("audio-input-buffer", diagnostics::color(0.5f, 1.0f, 0.2f));\r
204                 \r
205                 if(video_stream_)\r
206                         graph_->set_color("video-input-buffer", diagnostics::color(0.2f, 0.5f, 1.0f));\r
207                 \r
208                 graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f));        \r
209 \r
210                 executor_.begin_invoke([this]{read_file();});\r
211                 CASPAR_LOG(info) << print() << " Started.";\r
212         }\r
213 \r
214         ~implementation()\r
215         {\r
216                 stop();\r
217                 // Unblock thread.\r
218                 std::shared_ptr<AVPacket> packet;\r
219                 try_pop_video_packet(packet);\r
220                 try_pop_audio_packet(packet);\r
221         }\r
222                 \r
223         bool try_pop_video_packet(std::shared_ptr<AVPacket>& packet)\r
224         {\r
225                 bool result = video_stream_.try_pop(packet);\r
226                 if(result && !packet)\r
227                         graph_->add_tag("video-input-buffer");\r
228                 return result;\r
229         }\r
230 \r
231         bool try_pop_audio_packet(std::shared_ptr<AVPacket>& packet)\r
232         {       \r
233                 bool result = audio_stream_.try_pop(packet);\r
234                 if(result && !packet)\r
235                         graph_->add_tag("audio-input-buffer");\r
236                 return result;\r
237         }\r
238 \r
239         double fps()\r
240         {\r
241                 return fps_;\r
242         }\r
243 \r
244 private:\r
245 \r
246         void stop()\r
247         {\r
248                 executor_.stop();\r
249                 \r
250                 CASPAR_LOG(info) << print() << " Stopping.";\r
251         }\r
252 \r
253         void read_file()\r
254         {               \r
255                 if(video_stream_.size() > 4 || audio_stream_.size() > 4) // audio is always before video.\r
256                         Sleep(5); // There are enough packets, no hurry.\r
257 \r
258                 read_next_packet();\r
259 \r
260                 executor_.begin_invoke([this]{read_file();});\r
261         }\r
262                         \r
263         void read_next_packet()\r
264         {               \r
265                 try\r
266                 {\r
267                         std::shared_ptr<AVPacket> read_packet(new AVPacket, [](AVPacket* p)\r
268                         {\r
269                                 av_free_packet(p);\r
270                                 delete p;\r
271                         });\r
272                         av_init_packet(read_packet.get());\r
273 \r
274                         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
275                         if(is_eof(errn))                                                                                                                  // Use av_dup_packet to extend its life.\r
276                         {\r
277                                 if(loop_)\r
278                                 {\r
279                                         seek_frame(start_, AVSEEK_FLAG_BACKWARD);\r
280                                         graph_->add_tag("seek");                \r
281                                         //CASPAR_LOG(info) << print() << " Received EOF. Looping.";                     \r
282                                 }       \r
283                                 else\r
284                                 {\r
285                                         stop();\r
286                                         //CASPAR_LOG(info) << print() << " Received EOF. Stopping.";\r
287                                 }\r
288                         }\r
289                         else if(errn < 0)\r
290                         {\r
291                                 BOOST_THROW_EXCEPTION(\r
292                                         file_read_error() <<\r
293                                         msg_info(av_error_str(errn)) <<\r
294                                         source_info(narrow(print())) << \r
295                                         boost::errinfo_api_function("av_read_frame") <<\r
296                                         boost::errinfo_errno(AVUNERROR(errn)));\r
297                         }\r
298                         else\r
299                         {\r
300                                 if(video_stream_)\r
301                                 {       \r
302                                         video_stream_.push(read_packet);\r
303                                         graph_->update_value("video-input-buffer", static_cast<float>(video_stream_.size())/static_cast<float>(PACKET_BUFFER_COUNT));           \r
304                                 }\r
305                                 if(audio_stream_)\r
306                                 {       \r
307                                         audio_stream_.push(read_packet);\r
308                                         graph_->update_value("audio-input-buffer", static_cast<float>(audio_stream_.size())/static_cast<float>(PACKET_BUFFER_COUNT));   \r
309                                 }\r
310                         }                                                       \r
311                 }\r
312                 catch(...)\r
313                 {\r
314                         stop();\r
315                         CASPAR_LOG_CURRENT_EXCEPTION();\r
316                         return;\r
317                 }       \r
318         }\r
319 \r
320         void seek_frame(int64_t frame, int flags = 0)\r
321         {       \r
322                 static const AVRational base_q = {1, AV_TIME_BASE};\r
323 \r
324                 // Convert from frames into seconds.\r
325                 auto seek_target = frame*static_cast<int64_t>(AV_TIME_BASE/fps_);\r
326 \r
327                 int stream_index = video_stream_.index() >= 0 ? video_stream_.index() : audio_stream_.index();\r
328 \r
329                 if(stream_index >= 0)           \r
330                         seek_target = av_rescale_q(seek_target, base_q, format_context_->streams[stream_index]->time_base);\r
331 \r
332                 const int errn = av_seek_frame(format_context_.get(), stream_index, seek_target, flags);\r
333                 if(errn < 0)\r
334                 {       \r
335                         BOOST_THROW_EXCEPTION(\r
336                                 invalid_operation() << \r
337                                 source_info(narrow(print())) << \r
338                                 msg_info(av_error_str(errn)) <<\r
339                                 boost::errinfo_api_function("av_seek_frame") <<\r
340                                 boost::errinfo_errno(AVUNERROR(errn)));\r
341                 }\r
342 \r
343                 video_stream_.push(nullptr);\r
344                 audio_stream_.push(nullptr);\r
345         }               \r
346 \r
347         bool is_eof(int errn)\r
348         {\r
349                 //if(errn == AVERROR(EIO))\r
350                 //      CASPAR_LOG(warning) << print() << " Received EIO, assuming EOF";\r
351 \r
352                 return errn == AVERROR_EOF || errn == AVERROR(EIO); // av_read_frame doesn't always correctly return AVERROR_EOF;\r
353         }\r
354         \r
355         std::wstring print() const\r
356         {\r
357                 const auto video = widen(video_stream_.ctx() ? video_stream_.ctx()->codec->name : "no-video");\r
358                 const auto audio = widen(audio_stream_.ctx() ? audio_stream_.ctx()->codec->name : "no-audio");\r
359 \r
360                 return L"ffmpeg_input[" + filename_ + L"(" + boost::lexical_cast<std::wstring>(static_cast<int>(100*fps_)) + L"|" + video + L"|" + audio + L")]";\r
361         }\r
362 };\r
363 \r
364 input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start, int length) \r
365         : impl_(new implementation(graph, filename, loop, start)){}\r
366 const std::shared_ptr<AVCodecContext>& input::get_video_codec_context() const{return impl_->video_stream_.ctx();}\r
367 const std::shared_ptr<AVCodecContext>& input::get_audio_codec_context() const{return impl_->audio_stream_.ctx();}\r
368 bool input::is_running() const {return impl_->executor_.is_running();}\r
369 bool input::try_pop_video_packet(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_video_packet(packet);}\r
370 bool input::try_pop_audio_packet(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_audio_packet(packet);}\r
371 double input::fps() const { return impl_->fps(); }\r
372 }