]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/input.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / ffmpeg / producer / input.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #if defined(_MSC_VER)\r
21 #pragma warning (disable : 4244)\r
22 #endif\r
23 \r
24 #include "../stdafx.h"\r
25 \r
26 #include "input.h"\r
27 #include "util.h"\r
28 #include "../ffmpeg_error.h"\r
29 \r
30 #include <core/video_format.h>\r
31 \r
32 #include <common/diagnostics/graph.h>\r
33 #include <common/exception/exceptions.h>\r
34 #include <common/exception/win32_exception.h>\r
35 \r
36 #include <tbb/atomic.h>\r
37 \r
38 #include <agents.h>\r
39 #include <concrt_extras.h>\r
40 #include <semaphore.h>\r
41 \r
42 #if defined(_MSC_VER)\r
43 #pragma warning (push)\r
44 #pragma warning (disable : 4244)\r
45 #endif\r
46 extern "C" \r
47 {\r
48         #define __STDC_CONSTANT_MACROS\r
49         #define __STDC_LIMIT_MACROS\r
50         #include <libavformat/avformat.h>\r
51 }\r
52 #if defined(_MSC_VER)\r
53 #pragma warning (pop)\r
54 #endif\r
55 \r
56 using namespace Concurrency;\r
57 \r
58 namespace caspar { namespace ffmpeg {\r
59 \r
60 static const size_t MAX_BUFFER_COUNT = 32;\r
61         \r
62 struct input::implementation : public Concurrency::agent, boost::noncopyable\r
63 {\r
64         input::target_t&                                                target_;\r
65 \r
66         const std::wstring                                              filename_;\r
67         const safe_ptr<AVFormatContext>                 format_context_; // Destroy this last\r
68         int                                                                             default_stream_index_;\r
69         const boost::iterator_range<AVStream**> streams_;\r
70 \r
71         safe_ptr<diagnostics::graph>                    graph_;\r
72                 \r
73         const bool                                                              loop_;\r
74         const size_t                                                    start_;         \r
75         const size_t                                                    length_;\r
76         size_t                                                                  frame_number_;\r
77                         \r
78         tbb::atomic<size_t>                                             nb_frames_;\r
79         tbb::atomic<size_t>                                             nb_loops_;      \r
80         tbb::atomic<size_t>                                             packets_count_;\r
81 \r
82         bool                                                                    stop_;\r
83         \r
84 public:\r
85         explicit implementation(input::target_t& target,\r
86                                                         const safe_ptr<diagnostics::graph>& graph, \r
87                                                         const std::wstring& filename, \r
88                                                         bool loop, \r
89                                                         size_t start,\r
90                                                         size_t length)\r
91                 : target_(target)\r
92                 , filename_(filename)\r
93                 , format_context_(open_input(filename))         \r
94                 , default_stream_index_(av_find_default_stream_index(format_context_.get()))\r
95                 , streams_(format_context_->streams, format_context_->streams + format_context_->nb_streams)\r
96                 , graph_(graph)\r
97                 , loop_(loop)\r
98                 , start_(start)\r
99                 , length_(length)\r
100                 , frame_number_(0)\r
101                 , stop_(false)\r
102         {               \r
103                 packets_count_  = 0;\r
104                 nb_frames_              = 0;\r
105                 nb_loops_               = 0;\r
106                                 \r
107                 av_dump_format(format_context_.get(), 0, narrow(filename).c_str(), 0);\r
108                                 \r
109                 if(start_ > 0)                  \r
110                         seek_frame(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 \r
117         ~implementation()\r
118         {\r
119                 agent::wait(this);\r
120         }\r
121         \r
122         virtual void run()\r
123         {\r
124                 try\r
125                 {\r
126                         while(!stop_ && read_next_packet())\r
127                         {\r
128                         }\r
129                 }\r
130                 catch(...)\r
131                 {\r
132                         CASPAR_LOG_CURRENT_EXCEPTION();\r
133                 }       \r
134         \r
135                 BOOST_FOREACH(auto stream, streams_)\r
136                         Concurrency::send(target_, eof_packet(stream->index));  \r
137                                                         \r
138                 done();\r
139         }\r
140 \r
141         bool read_next_packet()\r
142         {               \r
143                 auto packet = create_packet();\r
144                 \r
145                 int ret = [&]() -> int\r
146                 {\r
147                         Concurrency::scoped_oversubcription_token oversubscribe;\r
148                         return 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
149                 }();\r
150 \r
151                 if(is_eof(ret))                                                                                                              \r
152                 {\r
153                         ++nb_loops_;\r
154                         frame_number_ = 0;\r
155 \r
156                         if(loop_)\r
157                         {\r
158                                 CASPAR_LOG(trace) << print() << " Looping.";\r
159                                 seek_frame(start_, AVSEEK_FLAG_BACKWARD);                       \r
160                         }       \r
161                         else\r
162                         {\r
163                                 CASPAR_LOG(trace) << print() << " Stopping.";\r
164                                 return false;\r
165                         }\r
166                 }\r
167                 else\r
168                 {               \r
169                         THROW_ON_ERROR(ret, print(), "av_read_frame");\r
170 \r
171                         if(packet->stream_index == default_stream_index_)\r
172                         {\r
173                                 if(nb_loops_ == 0)\r
174                                         ++nb_frames_;\r
175                                 ++frame_number_;\r
176                         }\r
177 \r
178                         THROW_ON_ERROR2(av_dup_packet(packet.get()), print());\r
179                                 \r
180                         // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.\r
181                         auto size = packet->size;\r
182                         auto data = packet->data;                       \r
183 \r
184                         packet = std::shared_ptr<AVPacket>(packet.get(), [=](AVPacket*)\r
185                         {\r
186                                 packet->size = size;\r
187                                 packet->data = data;\r
188                                 --packets_count_;\r
189                                 graph_->update_value("buffer-count", (static_cast<double>(packets_count_)+0.001)/MAX_BUFFER_COUNT);     \r
190                         });\r
191 \r
192                         ++packets_count_;\r
193 \r
194                         Concurrency::asend(target_, packet);\r
195                                         \r
196                         graph_->update_value("buffer-count", (static_cast<double>(packets_count_)+0.001)/MAX_BUFFER_COUNT);\r
197                 }       \r
198 \r
199                 return true;\r
200         }\r
201 \r
202         void seek_frame(int64_t frame, int flags = 0)\r
203         {                       \r
204                 if(flags == AVSEEK_FLAG_BACKWARD)\r
205                 {\r
206                         // Fix VP6 seeking\r
207                         int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
208                         if(vid_stream_index >= 0)\r
209                         {\r
210                                 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;\r
211                                 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)\r
212                                         flags |= AVSEEK_FLAG_BYTE;\r
213                         }\r
214                 }\r
215 \r
216                 THROW_ON_ERROR2(av_seek_frame(format_context_.get(), default_stream_index_, frame, flags), print());    \r
217                 auto packet = create_packet();\r
218                 packet->size = 0;               \r
219 \r
220                 BOOST_FOREACH(auto stream, streams_)\r
221                         Concurrency::send(target_, loop_packet(stream->index)); \r
222 \r
223                 graph_->add_tag("seek");                \r
224         }               \r
225 \r
226         bool is_eof(int ret)\r
227         {\r
228                 if(ret == AVERROR(EIO))\r
229                         CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. " << nb_frames_;\r
230                 if(ret == AVERROR_EOF)\r
231                         CASPAR_LOG(trace) << print() << " Received EOF. " << nb_frames_;\r
232 \r
233                 CASPAR_VERIFY(ret >= 0 || ret == AVERROR_EOF || ret == AVERROR(EIO), ffmpeg_error() << source_info(narrow(print())));\r
234 \r
235                 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;\r
236         }\r
237         \r
238         std::wstring print() const\r
239         {\r
240                 return L"ffmpeg_input[" + filename_ + L")]";\r
241         }\r
242 };\r
243 \r
244 input::input(input::target_t& target,\r
245                          const safe_ptr<diagnostics::graph>& graph, \r
246                          const std::wstring& filename, \r
247                          bool loop, \r
248                          size_t start, \r
249                          size_t length)\r
250         : impl_(new implementation(target, graph, filename, loop, start, length))\r
251 {\r
252 }\r
253 \r
254 safe_ptr<AVFormatContext> input::context()\r
255 {\r
256         return safe_ptr<AVFormatContext>(impl_->format_context_);\r
257 }\r
258 \r
259 size_t input::nb_frames() const\r
260 {\r
261         return impl_->nb_frames_;\r
262 }\r
263 \r
264 size_t input::nb_loops() const \r
265 {\r
266         return impl_->nb_loops_;\r
267 }\r
268 \r
269 void input::start()\r
270 {\r
271         impl_->start();\r
272 }\r
273 \r
274 void input::stop()\r
275 {\r
276         impl_->stop_ = true;\r
277 }\r
278 \r
279 }}