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