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