]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/input/input.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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 "../../ffmpeg_error.h"\r
28 \r
29 #include <common/diagnostics/graph.h>\r
30 #include <common/executor.h>\r
31 #include <common/lock.h>\r
32 #include <common/except.h>\r
33 #include <common/log.h>\r
34 \r
35 #include <core/video_format.h>\r
36 \r
37 #include <tbb/concurrent_queue.h>\r
38 #include <tbb/atomic.h>\r
39 #include <tbb/recursive_mutex.h>\r
40 \r
41 #include <boost/range/algorithm.hpp>\r
42 #include <boost/thread/condition_variable.hpp>\r
43 #include <boost/thread/mutex.hpp>\r
44 #include <boost/thread/thread.hpp>\r
45 \r
46 #if defined(_MSC_VER)\r
47 #pragma warning (push)\r
48 #pragma warning (disable : 4244)\r
49 #endif\r
50 extern "C" \r
51 {\r
52         #define __STDC_CONSTANT_MACROS\r
53         #define __STDC_LIMIT_MACROS\r
54         #include <libavformat/avformat.h>\r
55 }\r
56 #if defined(_MSC_VER)\r
57 #pragma warning (pop)\r
58 #endif\r
59 \r
60 namespace caspar { namespace ffmpeg {\r
61 \r
62 static const int MIN_FRAMES = 25;\r
63 \r
64 class stream\r
65 {\r
66         stream(const stream&);\r
67         stream& operator=(const stream&);\r
68 \r
69         typedef tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>>::size_type size_type;\r
70 \r
71         int                                                                                                              index_;\r
72         tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> packets_;\r
73 public:\r
74 \r
75         stream(int index) \r
76                 : index_(index)\r
77         {\r
78         }\r
79         \r
80         void push(const std::shared_ptr<AVPacket>& packet)\r
81         {\r
82                 if(packet && packet->data && packet->stream_index != index_)\r
83                         return;\r
84 \r
85                 packets_.push(packet);\r
86         }\r
87 \r
88         bool try_pop(std::shared_ptr<AVPacket>& packet)\r
89         {\r
90                 return packets_.try_pop(packet);\r
91         }\r
92 \r
93         void clear()\r
94         {\r
95                 std::shared_ptr<AVPacket> packet;\r
96                 while(packets_.try_pop(packet));\r
97         }\r
98                 \r
99         size_type size() const\r
100         {\r
101                 return index_ != -1 ? packets_.size() : std::numeric_limits<size_type>::max();\r
102         }\r
103 };\r
104                 \r
105 struct input::impl : boost::noncopyable\r
106 {               \r
107         const spl::shared_ptr<diagnostics::graph>                                       graph_;\r
108 \r
109         const spl::shared_ptr<AVFormatContext>                                          format_context_; // Destroy this last\r
110         const int                                                                                                       default_stream_index_;\r
111                         \r
112         const std::wstring                                                                                      filename_;\r
113         tbb::atomic<uint32_t>                                                                           start_;         \r
114         tbb::atomic<uint32_t>                                                                           length_;\r
115         tbb::atomic<bool>                                                                                       loop_;\r
116         double                                                                                                          fps_;\r
117         uint32_t                                                                                                        frame_number_;\r
118         \r
119         stream                                                                                                          video_stream_;\r
120         stream                                                                                                          audio_stream_;\r
121 \r
122         boost::optional<uint32_t>                                                                       seek_target_;\r
123 \r
124         tbb::atomic<bool>                                                                                       is_running_;\r
125         boost::mutex                                                                                            mutex_;\r
126         boost::condition_variable                                                                       cond_;\r
127         boost::thread                                                                                           thread_;\r
128         \r
129         impl(const spl::shared_ptr<diagnostics::graph> graph, const std::wstring& filename, const bool loop, const uint32_t start, const uint32_t length) \r
130                 : graph_(graph)\r
131                 , format_context_(open_input(filename))         \r
132                 , default_stream_index_(av_find_default_stream_index(format_context_.get()))\r
133                 , filename_(filename)\r
134                 , frame_number_(0)\r
135                 , fps_(read_fps(*format_context_, 0.0))\r
136                 , video_stream_(av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0))\r
137                 , audio_stream_(av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0))\r
138         {               \r
139                 start_                  = start;\r
140                 length_                 = length;\r
141                 loop_                   = loop;\r
142                 is_running_             = true;\r
143 \r
144                 if(start_ != 0)\r
145                         seek_target_ = start_;\r
146                                                                                                                 \r
147                 graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));        \r
148                 graph_->set_color("audio-buffer", diagnostics::color(0.7f, 0.4f, 0.4f));\r
149                 graph_->set_color("video-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));        \r
150                 \r
151                 for(int n = 0; n < 8; ++n)\r
152                         tick();\r
153 \r
154                 thread_                 = boost::thread([this]{run();});\r
155         }\r
156 \r
157         ~impl()\r
158         {\r
159                 is_running_ = false;\r
160                 cond_.notify_one();\r
161                 thread_.join();\r
162         }\r
163         \r
164         bool try_pop_video(std::shared_ptr<AVPacket>& packet)\r
165         {                               \r
166                 bool result = video_stream_.try_pop(packet);\r
167                 if(result)\r
168                         cond_.notify_one();\r
169                 \r
170                 graph_->set_value("video-buffer", std::min(1.0, static_cast<double>(video_stream_.size()/MIN_FRAMES)));\r
171                                 \r
172                 return result;\r
173         }\r
174         \r
175         bool try_pop_audio(std::shared_ptr<AVPacket>& packet)\r
176         {                               \r
177                 bool result = audio_stream_.try_pop(packet);\r
178                 if(result)\r
179                         cond_.notify_one();\r
180                                 \r
181                 graph_->set_value("audio-buffer", std::min(1.0, static_cast<double>(audio_stream_.size()/MIN_FRAMES)));\r
182 \r
183                 return result;\r
184         }\r
185 \r
186         void seek(uint32_t target)\r
187         {\r
188                 {\r
189                         boost::lock_guard<boost::mutex> lock(mutex_);\r
190 \r
191                         seek_target_ = target;\r
192                         video_stream_.clear();\r
193                         audio_stream_.clear();\r
194                 }\r
195                 \r
196                 cond_.notify_one();\r
197         }\r
198                 \r
199         std::wstring print() const\r
200         {\r
201                 return L"ffmpeg_input[" + filename_ + L")]";\r
202         }\r
203 \r
204 private:\r
205         void internal_seek(uint32_t target)\r
206         {\r
207                 CASPAR_LOG(debug) << print() << " Seeking: " << target;\r
208 \r
209                 int flags = AVSEEK_FLAG_FRAME;\r
210                 if(target == 0)\r
211                 {\r
212                         // Fix VP6 seeking\r
213                         int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
214                         if(vid_stream_index >= 0)\r
215                         {\r
216                                 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;\r
217                                 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)\r
218                                         flags = AVSEEK_FLAG_BYTE;\r
219                         }\r
220                 }\r
221                 \r
222                 auto stream = format_context_->streams[default_stream_index_];\r
223                 auto codec  = stream->codec;\r
224                 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
225                 \r
226                 THROW_ON_ERROR2(avformat_seek_file(format_context_.get(), default_stream_index_, std::numeric_limits<int64_t>::min(), fixed_target, fixed_target, 0), print());         \r
227                 \r
228                 video_stream_.push(nullptr);\r
229                 audio_stream_.push(nullptr);\r
230         }\r
231                 \r
232         bool full() const\r
233         {\r
234                 return video_stream_.size() > MIN_FRAMES && audio_stream_.size() > MIN_FRAMES;\r
235         }\r
236 \r
237         void tick()\r
238         {\r
239                 if(seek_target_)                                \r
240                 {\r
241                         internal_seek(*seek_target_);\r
242                         seek_target_.reset();\r
243                 }\r
244 \r
245                 auto packet = create_packet();\r
246                 \r
247                 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
248                 \r
249                 if(is_eof(ret))                                                                                                              \r
250                 {\r
251                         std::shared_ptr<AVPacket> eof_packet(new AVPacket());\r
252                         memset(eof_packet.get(), 0, sizeof(AVPacket));\r
253                         av_init_packet(eof_packet.get());\r
254 \r
255                         video_stream_.push(eof_packet);\r
256                         audio_stream_.push(eof_packet);\r
257 \r
258                         if(loop_)\r
259                         {\r
260                                 internal_seek(start_);\r
261                                 graph_->set_tag("seek");                \r
262                         }\r
263                 }\r
264                 else\r
265                 {               \r
266                         THROW_ON_ERROR(ret, "av_read_frame", print());\r
267                                         \r
268                         THROW_ON_ERROR2(av_dup_packet(packet.get()), print());\r
269                                 \r
270                         // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.\r
271                         auto size = packet->size;\r
272                         auto data = packet->data;\r
273                         \r
274                         packet = spl::shared_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)\r
275                         {\r
276                                 packet->size = size;\r
277                                 packet->data = data;                            \r
278                         });\r
279                                         \r
280                         auto stream_time_base = format_context_->streams[packet->stream_index]->time_base;\r
281                         auto packet_frame_number = static_cast<uint32_t>((static_cast<double>(packet->pts * stream_time_base.num)/stream_time_base.den)*fps_);\r
282 \r
283                         if(packet->stream_index == default_stream_index_)\r
284                                 frame_number_ = packet_frame_number;\r
285                                         \r
286                         if(packet_frame_number >= start_ && packet_frame_number < length_)\r
287                         {\r
288                                 video_stream_.push(packet);\r
289                                 audio_stream_.push(packet);\r
290                                                 \r
291                                 graph_->set_value("video-buffer", std::min(1.0, static_cast<double>(video_stream_.size()/MIN_FRAMES)));\r
292                                 graph_->set_value("audio-buffer", std::min(1.0, static_cast<double>(audio_stream_.size()/MIN_FRAMES)));\r
293                         }\r
294                 }       \r
295         }\r
296 \r
297         void run()\r
298         {\r
299                 win32_exception::install_handler();\r
300 \r
301                 while(is_running_)\r
302                 {\r
303                         try\r
304                         {\r
305                                 boost::this_thread::sleep(boost::posix_time::milliseconds(1));\r
306                                 \r
307                                 {\r
308                                         boost::unique_lock<boost::mutex> lock(mutex_);\r
309 \r
310                                         while(full() && !seek_target_ && is_running_)\r
311                                                 cond_.wait(lock);\r
312                                         \r
313                                         tick();\r
314                                 }\r
315                         }\r
316                         catch(...)\r
317                         {\r
318                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
319                                 is_running_ = false;\r
320                         }\r
321                 }\r
322         }\r
323                         \r
324         bool is_eof(int ret)\r
325         {\r
326                 #pragma warning (disable : 4146)\r
327 \r
328                 //if(ret == AVERROR(EIO))\r
329                 //      CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. ";\r
330                 //if(ret == AVERROR_EOF)\r
331                 //      CASPAR_LOG(trace) << print() << " Received EOF. ";\r
332 \r
333                 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ > length_; // av_read_frame doesn't always correctly return AVERROR_EOF;\r
334         }\r
335 };\r
336 \r
337 input::input(const spl::shared_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, uint32_t start, uint32_t length) \r
338         : impl_(new impl(graph, filename, loop, start, length)){}\r
339 bool input::try_pop_video(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_video(packet);}\r
340 bool input::try_pop_audio(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_audio(packet);}\r
341 AVFormatContext& input::context(){return *impl_->format_context_;}\r
342 void input::loop(bool value){impl_->loop_ = value;}\r
343 bool input::loop() const{return impl_->loop_;}\r
344 void input::seek(uint32_t target){impl_->seek(target);}\r
345 void input::start(uint32_t value){impl_->start_ = value;}\r
346 uint32_t input::start() const{return impl_->start_;}\r
347 void input::length(uint32_t value){impl_->length_ = value;}\r
348 uint32_t input::length() const{return impl_->length_;}\r
349 }}\r