]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/input/input.cpp
Seeking fix from Robert Nagy. This needs testing!
[casparcg] / modules / ffmpeg / producer / input / input.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "input.h"\r
25 \r
26 #include "../util/util.h"\r
27 #include "../util/flv.h"\r
28 #include "../../ffmpeg_error.h"\r
29 #include "../../ffmpeg_params.h"\r
30 #include "../../ffmpeg.h"\r
31 \r
32 #include <core/video_format.h>\r
33 \r
34 #include <common/diagnostics/graph.h>\r
35 #include <common/concurrency/executor.h>\r
36 #include <common/concurrency/future_util.h>\r
37 #include <common/exception/exceptions.h>\r
38 #include <common/exception/win32_exception.h>\r
39 \r
40 #include <tbb/concurrent_queue.h>\r
41 #include <tbb/atomic.h>\r
42 #include <tbb/recursive_mutex.h>\r
43 \r
44 #include <boost/rational.hpp>\r
45 #include <boost/range/algorithm.hpp>\r
46 #include <boost/thread/condition_variable.hpp>\r
47 #include <boost/thread/mutex.hpp>\r
48 #include <boost/thread/thread.hpp>\r
49 \r
50 #if defined(_MSC_VER)\r
51 #pragma warning (push)\r
52 #pragma warning (disable : 4244)\r
53 #endif\r
54 extern "C" \r
55 {\r
56         #define __STDC_CONSTANT_MACROS\r
57         #define __STDC_LIMIT_MACROS\r
58         #include <libavformat/avformat.h>\r
59 }\r
60 #if defined(_MSC_VER)\r
61 #pragma warning (pop)\r
62 #endif\r
63 \r
64 static const size_t MAX_BUFFER_COUNT    = 100;\r
65 static const size_t MAX_BUFFER_COUNT_RT = 3;\r
66 static const size_t MIN_BUFFER_COUNT    = 50;\r
67 static const size_t MAX_BUFFER_SIZE     = 64 * 1000000;\r
68 \r
69 namespace caspar { namespace ffmpeg {\r
70                 \r
71 struct input::implementation : boost::noncopyable\r
72 {               \r
73         const safe_ptr<diagnostics::graph>                                                      graph_;\r
74 \r
75         const safe_ptr<AVFormatContext>                                                         format_context_; // Destroy this last\r
76         const int                                                                                                       default_stream_index_;\r
77                         \r
78         const std::wstring                                                                                      filename_;\r
79         const uint32_t                                                                                          start_;         \r
80         const uint32_t                                                                                          length_;\r
81         const bool                                                                                                      thumbnail_mode_;\r
82         tbb::atomic<bool>                                                                                       loop_;\r
83         uint32_t                                                                                                        frame_number_;\r
84         \r
85         tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>>        buffer_;\r
86         tbb::atomic<size_t>                                                                                     buffer_size_;\r
87                 \r
88         executor                                                                                                        executor_;\r
89         \r
90         explicit implementation(const safe_ptr<diagnostics::graph> graph, const std::wstring& filename, FFMPEG_Resource resource_type, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_producer_params& vid_params) \r
91                 : graph_(graph)\r
92                 , format_context_(open_input(filename, resource_type, vid_params))              \r
93                 , default_stream_index_(av_find_default_stream_index(format_context_.get()))\r
94                 , filename_(filename)\r
95                 , start_(start)\r
96                 , length_(length)\r
97                 , thumbnail_mode_(thumbnail_mode)\r
98                 , frame_number_(0)\r
99                 , executor_(print())\r
100         {\r
101                 if (thumbnail_mode_)\r
102                         executor_.invoke([]\r
103                         {\r
104                                 disable_logging_for_thread();\r
105                         });\r
106 \r
107                 loop_                   = loop;\r
108                 buffer_size_    = 0;\r
109 \r
110                 if(start_ > 0)                  \r
111                         queued_seek(start_);\r
112                                                                 \r
113                 graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));        \r
114                 graph_->set_color("buffer-count", diagnostics::color(0.7f, 0.4f, 0.4f));\r
115                 graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f)); \r
116 \r
117                 tick();\r
118         }\r
119         \r
120         bool try_pop(std::shared_ptr<AVPacket>& packet)\r
121         {\r
122                 auto result = buffer_.try_pop(packet);\r
123                 \r
124                 if(result)\r
125                 {\r
126                         if(packet)\r
127                                 buffer_size_ -= packet->size;\r
128                         tick();\r
129                 }\r
130 \r
131                 graph_->set_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);\r
132                 graph_->set_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));\r
133                 \r
134                 return result;\r
135         }\r
136 \r
137         std::ptrdiff_t get_max_buffer_count() const\r
138         {\r
139                 return thumbnail_mode_ ? 1 : MAX_BUFFER_COUNT;\r
140         }\r
141 \r
142         std::ptrdiff_t get_min_buffer_count() const\r
143         {\r
144                 return thumbnail_mode_ ? 0 : MIN_BUFFER_COUNT;\r
145         }\r
146 \r
147         boost::unique_future<bool> seek(uint32_t target)\r
148         {\r
149                 if (!executor_.is_running())\r
150                         return wrap_as_future(false);\r
151 \r
152                 return executor_.begin_invoke([=]() -> bool\r
153                 {\r
154                         std::shared_ptr<AVPacket> packet;\r
155                         while(buffer_.try_pop(packet) && packet)\r
156                                 buffer_size_ -= packet->size;\r
157 \r
158                         queued_seek(target);\r
159 \r
160                         tick();\r
161 \r
162                         return true;\r
163                 }, high_priority);\r
164         }\r
165         \r
166         std::wstring print() const\r
167         {\r
168                 return L"ffmpeg_input[" + filename_ + L")]";\r
169         }\r
170         \r
171         bool full() const\r
172         {\r
173                 return (buffer_size_ > MAX_BUFFER_SIZE || buffer_.size() > get_max_buffer_count()) && buffer_.size() > get_min_buffer_count();\r
174         }\r
175 \r
176         void tick()\r
177         {       \r
178                 if(!executor_.is_running())\r
179                         return;\r
180                 \r
181                 executor_.begin_invoke([this]\r
182                 {                       \r
183                         if(full())\r
184                                 return;\r
185 \r
186                         try\r
187                         {\r
188                                 auto packet = create_packet();\r
189                 \r
190                                 auto ret = av_read_frame(format_context_.get(), packet.get()); // packet is only valid until next call of av_read_frame. Use av_dup_packet to extend its life.  \r
191                 \r
192                                 if(is_eof(ret))                                                                                                              \r
193                                 {\r
194                                         frame_number_   = 0;\r
195 \r
196                                         if(loop_)\r
197                                         {\r
198                                                 queued_seek(start_);\r
199                                                 graph_->set_tag("seek");                \r
200                                                 CASPAR_LOG(trace) << print() << " Looping.";                    \r
201                                         }               \r
202                                         else\r
203                                                 executor_.stop();\r
204                                 }\r
205                                 else\r
206                                 {               \r
207                                         THROW_ON_ERROR(ret, "av_read_frame", print());\r
208 \r
209                                         if(packet->stream_index == default_stream_index_)\r
210                                                 ++frame_number_;\r
211 \r
212                                         THROW_ON_ERROR2(av_dup_packet(packet.get()), print());\r
213                                 \r
214                                         // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.\r
215                                         auto size = packet->size;\r
216                                         auto data = packet->data;\r
217                         \r
218                                         packet = safe_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)\r
219                                         {\r
220                                                 packet->size = size;\r
221                                                 packet->data = data;                            \r
222                                         });\r
223 \r
224                                         buffer_.try_push(packet);\r
225                                         buffer_size_ += packet->size;\r
226                                 \r
227                                         graph_->set_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);\r
228                                         graph_->set_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));\r
229                                 }       \r
230                 \r
231                                 tick();         \r
232                         }\r
233                         catch(...)\r
234                         {\r
235                                 if (!thumbnail_mode_)\r
236                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
237                                 executor_.stop();\r
238                         }\r
239                 });\r
240         }       \r
241 \r
242         safe_ptr<AVFormatContext> open_input(const std::wstring resource_name, FFMPEG_Resource resource_type, const ffmpeg_producer_params& vid_params)\r
243         {\r
244                 AVFormatContext* weak_context = nullptr;\r
245 \r
246                 switch (resource_type) {\r
247                         case FFMPEG_FILE:\r
248                                 THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), nullptr, nullptr), resource_name);\r
249                                 break;\r
250                         case FFMPEG_DEVICE: {\r
251                                 AVDictionary* format_options = NULL;\r
252                                 for (auto it = vid_params.options.begin(); it != vid_params.options.end(); ++it)\r
253                                 {\r
254                                         av_dict_set(&format_options, (*it).name.c_str(), (*it).value.c_str(), 0);\r
255                                 }\r
256                                 AVInputFormat* input_format = av_find_input_format("dshow");\r
257                                 THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), input_format, &format_options), resource_name);\r
258                                 if (format_options != nullptr)\r
259                                 {\r
260                                         std::string unsupported_tokens = "";\r
261                                         AVDictionaryEntry *t = NULL;\r
262                                         while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)\r
263                                         {\r
264                                                 if (!unsupported_tokens.empty())\r
265                                                         unsupported_tokens += ", ";\r
266                                                 unsupported_tokens += t->key;\r
267                                         }\r
268                                         av_close_input_file(weak_context);\r
269                                         BOOST_THROW_EXCEPTION(ffmpeg_error() << msg_info(unsupported_tokens));\r
270                                 }\r
271                                 av_dict_free(&format_options);\r
272                         } break;\r
273                         case FFMPEG_STREAM: {\r
274                                 AVDictionary* format_options = NULL;\r
275                                 for (auto it = vid_params.options.begin(); it != vid_params.options.end(); ++it)\r
276                                 {\r
277                                         av_dict_set(&format_options, (*it).name.c_str(), (*it).value.c_str(), 0);\r
278                                 }\r
279                                 THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), nullptr, &format_options), resource_name);\r
280                                 if (format_options != nullptr)\r
281                                 {\r
282                                         std::string unsupported_tokens = "";\r
283                                         AVDictionaryEntry *t = NULL;\r
284                                         while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)\r
285                                         {\r
286                                                 if (!unsupported_tokens.empty())\r
287                                                         unsupported_tokens += ", ";\r
288                                                 unsupported_tokens += t->key;\r
289                                         }\r
290                                         av_close_input_file(weak_context);\r
291                                         BOOST_THROW_EXCEPTION(ffmpeg_error() << msg_info(unsupported_tokens));\r
292                                 }\r
293                                 av_dict_free(&format_options);\r
294                         } break;\r
295                 };\r
296                 safe_ptr<AVFormatContext> context(weak_context, av_close_input_file);      \r
297                 THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), resource_name);\r
298                 fix_meta_data(*context);\r
299                 return context;\r
300         }\r
301 \r
302   void fix_meta_data(AVFormatContext& context)\r
303   {\r
304     auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
305 \r
306     if(video_index > -1)\r
307     {\r
308      auto video_stream   = context.streams[video_index];\r
309       auto video_context  = context.streams[video_index]->codec;\r
310             \r
311       if(boost::filesystem2::path(context.filename).extension() == ".flv")\r
312       {\r
313         try\r
314         {\r
315           auto meta = read_flv_meta_info(context.filename);\r
316           double fps = boost::lexical_cast<double>(meta["framerate"]);\r
317           video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);\r
318         }\r
319         catch(...){}\r
320       }\r
321       else\r
322       {\r
323         auto stream_time = video_stream->time_base;\r
324         auto duration   = video_stream->duration;\r
325         auto codec_time  = video_context->time_base;\r
326         auto ticks     = video_context->ticks_per_frame;\r
327 \r
328         if(video_stream->nb_frames == 0)\r
329           video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);  \r
330       }\r
331     }\r
332   }\r
333                         \r
334         void queued_seek(const uint32_t target)\r
335         {       \r
336                 if (!thumbnail_mode_)\r
337                         CASPAR_LOG(debug) << print() << " Seeking: " << target;\r
338 \r
339                 int flags = AVSEEK_FLAG_FRAME;\r
340                 if(target == 0)\r
341                 {\r
342                         // Fix VP6 seeking\r
343                         int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
344                         if(vid_stream_index >= 0)\r
345                         {\r
346                                 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;\r
347                                 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)\r
348                                         flags = AVSEEK_FLAG_BYTE;\r
349                         }\r
350                 }\r
351                 \r
352                 auto stream = format_context_->streams[default_stream_index_];\r
353                 \r
354                 \r
355                 auto fps = read_fps(*format_context_, 0.0);\r
356                                 \r
357                 THROW_ON_ERROR2(avformat_seek_file(\r
358                         format_context_.get(), \r
359                         default_stream_index_, \r
360                         std::numeric_limits<int64_t>::min(),\r
361                         static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num),\r
362                         std::numeric_limits<int64_t>::max(), \r
363                         0), print());\r
364 \r
365                 auto flush_packet       = create_packet();\r
366                 flush_packet->data      = nullptr;\r
367                 flush_packet->size      = 0;\r
368                 flush_packet->pos       = target;\r
369 \r
370                 buffer_.push(flush_packet);\r
371         }       \r
372 \r
373         bool is_eof(int ret)\r
374         {\r
375                 if(ret == AVERROR(EIO))\r
376                         CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. ";\r
377                 if(ret == AVERROR_EOF)\r
378                         CASPAR_LOG(trace) << print() << " Received EOF. ";\r
379 \r
380                 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;\r
381         }\r
382 };\r
383 \r
384 input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, FFMPEG_Resource resource_type, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_producer_params& vid_params) \r
385         : impl_(new implementation(graph, filename, resource_type, loop, start, length, thumbnail_mode, vid_params)){}\r
386 bool input::eof() const {return !impl_->executor_.is_running();}\r
387 bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}\r
388 safe_ptr<AVFormatContext> input::context(){return impl_->format_context_;}\r
389 void input::loop(bool value){impl_->loop_ = value;}\r
390 bool input::loop() const{return impl_->loop_;}\r
391 boost::unique_future<bool> input::seek(uint32_t target){return impl_->seek(target);}\r
392 }}\r