]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/input/input.cpp
[ffmpeg_producer] Resolved problem where video decoders having CODEC_CAP_DELAY needs...
[casparcg] / modules / ffmpeg / producer / input / input.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "input.h"
25
26 #include "../util/util.h"
27 #include "../util/flv.h"
28 #include "../../ffmpeg_error.h"
29 #include "../../ffmpeg.h"
30
31 #include <core/video_format.h>
32
33 #include <common/diagnostics/graph.h>
34 #include <common/executor.h>
35 #include <common/except.h>
36 #include <common/os/general_protection_fault.h>
37 #include <common/param.h>
38 #include <common/scope_exit.h>
39
40 #include <tbb/concurrent_queue.h>
41 #include <tbb/atomic.h>
42 #include <tbb/recursive_mutex.h>
43
44 #include <boost/range/algorithm.hpp>
45 #include <boost/thread/condition_variable.hpp>
46 #include <boost/thread/mutex.hpp>
47 #include <boost/thread/thread.hpp>
48
49 #if defined(_MSC_VER)
50 #pragma warning (push)
51 #pragma warning (disable : 4244)
52 #endif
53 extern "C"
54 {
55         #define __STDC_CONSTANT_MACROS
56         #define __STDC_LIMIT_MACROS
57         #include <libavformat/avformat.h>
58 }
59 #if defined(_MSC_VER)
60 #pragma warning (pop)
61 #endif
62
63 static const size_t MAX_BUFFER_COUNT    = 100;
64 static const size_t MAX_BUFFER_COUNT_RT = 3;
65 static const size_t MIN_BUFFER_COUNT    = 50;
66 static const size_t MAX_BUFFER_SIZE     = 64 * 1000000;
67
68 namespace caspar { namespace ffmpeg {
69 struct input::implementation : boost::noncopyable
70 {
71         const spl::shared_ptr<diagnostics::graph>                                       graph_;
72
73         const spl::shared_ptr<AVFormatContext>                                          format_context_; // Destroy this last
74         const int                                                                                                       default_stream_index_   = av_find_default_stream_index(format_context_.get());
75
76         const std::wstring                                                                                      filename_;
77         tbb::atomic<uint32_t>                                                                           start_;
78         tbb::atomic<uint32_t>                                                                           length_;
79         const bool                                                                                                      thumbnail_mode_;
80         tbb::atomic<bool>                                                                                       loop_;
81         uint32_t                                                                                                        frame_number_                   = 0;
82
83         tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>>        buffer_;
84         tbb::atomic<size_t>                                                                                     buffer_size_;
85
86         executor                                                                                                        executor_;
87
88         explicit implementation(const spl::shared_ptr<diagnostics::graph> graph, const std::wstring& url_or_file, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_options& vid_params)
89                 : graph_(graph)
90                 , format_context_(open_input(url_or_file, vid_params))
91                 , filename_(url_or_file)
92                 , thumbnail_mode_(thumbnail_mode)
93                 , executor_(print())
94         {
95                 if (thumbnail_mode_)
96                         executor_.invoke([]
97                         {
98                                 enable_quiet_logging_for_thread();
99                         });
100
101                 start_                  = start;
102                 length_                 = length;
103                 loop_                   = loop;
104                 buffer_size_    = 0;
105
106                 if(start_ > 0)
107                         queued_seek(start_);
108
109                 graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));
110                 graph_->set_color("buffer-count", diagnostics::color(0.7f, 0.4f, 0.4f));
111                 graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f));
112
113                 tick();
114         }
115
116         bool try_pop(std::shared_ptr<AVPacket>& packet)
117         {
118                 auto result = buffer_.try_pop(packet);
119
120                 if(result)
121                 {
122                         if(packet)
123                                 buffer_size_ -= packet->size;
124                         tick();
125                 }
126
127                 graph_->set_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);
128                 graph_->set_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));
129
130                 return result;
131         }
132
133         std::ptrdiff_t get_max_buffer_count() const
134         {
135                 return thumbnail_mode_ ? 1 : MAX_BUFFER_COUNT;
136         }
137
138         std::ptrdiff_t get_min_buffer_count() const
139         {
140                 return thumbnail_mode_ ? 0 : MIN_BUFFER_COUNT;
141         }
142
143         std::future<bool> seek(uint32_t target)
144         {
145                 if (!executor_.is_running())
146                         return make_ready_future(false);
147
148                 return executor_.begin_invoke([=]() -> bool
149                 {
150                         std::shared_ptr<AVPacket> packet;
151                         while(buffer_.try_pop(packet) && packet)
152                                 buffer_size_ -= packet->size;
153
154                         queued_seek(target);
155
156                         tick();
157
158                         return true;
159                 }, task_priority::high_priority);
160         }
161
162         std::wstring print() const
163         {
164                 return L"ffmpeg_input[" + filename_ + L")]";
165         }
166
167         bool full() const
168         {
169                 return (buffer_size_ > MAX_BUFFER_SIZE || buffer_.size() > get_max_buffer_count()) && buffer_.size() > get_min_buffer_count();
170         }
171
172         void tick()
173         {
174                 if(!executor_.is_running())
175                         return;
176
177                 executor_.begin_invoke([this]
178                 {
179                         if(full())
180                                 return;
181
182                         try
183                         {
184                                 auto packet = create_packet();
185
186                                 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.
187
188                                 if(is_eof(ret))
189                                 {
190                                         frame_number_   = 0;
191
192                                         if(loop_)
193                                         {
194                                                 queued_seek(start_);
195                                                 graph_->set_tag(diagnostics::tag_severity::INFO, "seek");
196                                                 CASPAR_LOG(trace) << print() << " Looping.";
197                                         }
198                                         else
199                                         {
200                                                 // Needed by some decoders to decode remaining frames based on last packet.
201                                                 auto flush_packet = create_packet();
202                                                 flush_packet->data = nullptr;
203                                                 flush_packet->size = 0;
204                                                 flush_packet->pos = -1;
205
206                                                 buffer_.push(flush_packet);
207
208                                                 executor_.stop();
209                                         }
210                                 }
211                                 else
212                                 {
213                                         THROW_ON_ERROR(ret, "av_read_frame", print());
214
215                                         if(packet->stream_index == default_stream_index_)
216                                                 ++frame_number_;
217
218                                         THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
219
220                                         // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
221                                         auto size = packet->size;
222                                         auto data = packet->data;
223
224                                         packet = spl::shared_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)
225                                         {
226                                                 packet->size = size;
227                                                 packet->data = data;
228                                         });
229
230                                         buffer_.try_push(packet);
231                                         buffer_size_ += packet->size;
232
233                                         graph_->set_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);
234                                         graph_->set_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));
235                                 }
236
237                                 tick();
238                         }
239                         catch(...)
240                         {
241                                 if (!thumbnail_mode_)
242                                         CASPAR_LOG_CURRENT_EXCEPTION();
243                                 executor_.stop();
244                         }
245                 });
246         }
247
248         spl::shared_ptr<AVFormatContext> open_input(const std::wstring& url_or_file, const ffmpeg_options& vid_params)
249         {
250                 AVDictionary* format_options = nullptr;
251
252                 CASPAR_SCOPE_EXIT
253                 {
254                         if (format_options)
255                                 av_dict_free(&format_options);
256                 };
257
258                 for (auto& option : vid_params)
259                         av_dict_set(&format_options, option.first.c_str(), option.second.c_str(), 0);
260
261                 auto resource_name                      = std::wstring();
262                 auto parts                                      = caspar::protocol_split(url_or_file);
263                 auto protocol                           = parts.at(0);
264                 auto path                                       = parts.at(1);
265                 AVInputFormat* input_format     = nullptr;
266
267                 static const std::set<std::wstring> PROTOCOLS_TREATED_AS_FORMATS = { L"dshow", L"v4l2" };
268
269                 if (protocol.empty())
270                         resource_name = path;
271                 else if (PROTOCOLS_TREATED_AS_FORMATS.find(protocol) != PROTOCOLS_TREATED_AS_FORMATS.end())
272                 {
273                         input_format = av_find_input_format(u8(protocol).c_str());
274                         resource_name = path;
275                 }
276                 else
277                         resource_name = protocol + L"://" + path;
278
279                 AVFormatContext* weak_context = nullptr;
280                 THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(resource_name).c_str(), input_format, &format_options), resource_name);
281
282                 spl::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* ptr)
283                 {
284                         avformat_close_input(&ptr);
285                 });
286
287                 if (format_options)
288                 {
289                         std::string unsupported_tokens = "";
290                         AVDictionaryEntry *t = NULL;
291                         while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)
292                         {
293                                 if (!unsupported_tokens.empty())
294                                         unsupported_tokens += ", ";
295                                 unsupported_tokens += t->key;
296                         }
297                         CASPAR_THROW_EXCEPTION(user_error() << msg_info(unsupported_tokens));
298                 }
299
300                 THROW_ON_ERROR2(avformat_find_stream_info(context.get(), nullptr), resource_name);
301                 fix_meta_data(*context);
302                 return context;
303         }
304
305         void fix_meta_data(AVFormatContext& context)
306         {
307                 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
308
309                 if (video_index > -1)
310                 {
311                         auto video_stream = context.streams[video_index];
312                         auto video_context = context.streams[video_index]->codec;
313
314                         if (boost::filesystem::path(context.filename).extension().string() == ".flv")
315                         {
316                                 try
317                                 {
318                                         auto meta = read_flv_meta_info(context.filename);
319                                         double fps = boost::lexical_cast<double>(meta["framerate"]);
320                                         video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);
321                                 }
322                                 catch (...) {}
323                         }
324                         else
325                         {
326                                 auto stream_time = video_stream->time_base;
327                                 auto duration = video_stream->duration;
328                                 auto codec_time = video_context->time_base;
329                                 auto ticks = video_context->ticks_per_frame;
330
331                                 if (video_stream->nb_frames == 0)
332                                         video_stream->nb_frames = (duration*stream_time.num*codec_time.den) / (stream_time.den*codec_time.num*ticks);
333                         }
334                 }
335         }
336
337         void queued_seek(const uint32_t target)
338         {
339                 if (!thumbnail_mode_)
340                         CASPAR_LOG(debug) << print() << " Seeking: " << target;
341
342                 int flags = AVSEEK_FLAG_FRAME;
343                 if(target == 0)
344                 {
345                         // Fix VP6 seeking
346                         int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
347                         if(vid_stream_index >= 0)
348                         {
349                                 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;
350                                 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)
351                                         flags = AVSEEK_FLAG_BYTE;
352                         }
353                 }
354
355                 auto stream = format_context_->streams[default_stream_index_];
356
357
358                 auto fps = read_fps(*format_context_, 0.0);
359
360                 THROW_ON_ERROR2(avformat_seek_file(
361                         format_context_.get(),
362                         default_stream_index_,
363                         std::numeric_limits<int64_t>::min(),
364                         static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num),
365                         std::numeric_limits<int64_t>::max(),
366                         0), print());
367
368                 auto flush_packet       = create_packet();
369                 flush_packet->data      = nullptr;
370                 flush_packet->size      = 0;
371                 flush_packet->pos       = target;
372
373                 buffer_.push(flush_packet);
374         }
375
376         bool is_eof(int ret)
377         {
378                 if(ret == AVERROR(EIO))
379                         CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. ";
380                 if(ret == AVERROR_EOF)
381                         CASPAR_LOG(trace) << print() << " Received EOF. ";
382
383                 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
384         }
385
386         int num_audio_streams() const
387         {
388                 return 0; // TODO
389         }
390 };
391
392 input::input(const spl::shared_ptr<diagnostics::graph>& graph, const std::wstring& url_or_file, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_options& vid_params)
393         : impl_(new implementation(graph, url_or_file, loop, start, length, thumbnail_mode, vid_params)){}
394 bool input::eof() const {return !impl_->executor_.is_running();}
395 bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}
396 spl::shared_ptr<AVFormatContext> input::context(){return impl_->format_context_;}
397 void input::start(uint32_t value){impl_->start_ = value;}
398 uint32_t input::start() const{return impl_->start_;}
399 void input::length(uint32_t value){impl_->length_ = value;}
400 uint32_t input::length() const{return impl_->length_;}
401 void input::loop(bool value){impl_->loop_ = value;}
402 bool input::loop() const{return impl_->loop_;}
403 int input::num_audio_streams() const { return impl_->num_audio_streams(); }
404 std::future<bool> input::seek(uint32_t target){return impl_->seek(target);}
405 }}