]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/ffmpeg_producer.cpp
2.0.0.2: ffmpeg_producer: Refactored.
[casparcg] / modules / ffmpeg / producer / ffmpeg_producer.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 #include "../stdafx.h"\r
21 \r
22 #include "ffmpeg_producer.h"\r
23 \r
24 #include "input.h"\r
25 #include "audio/audio_decoder.h"\r
26 #include "video/video_decoder.h"\r
27 \r
28 #include <common/utility/timer.h>\r
29 #include <common/diagnostics/graph.h>\r
30 \r
31 #include <core/producer/frame/basic_frame.h>\r
32 #include <core/mixer/write_frame.h>\r
33 #include <core/producer/frame/audio_transform.h>\r
34 #include <core/video_format.h>\r
35 \r
36 #include <common/env.h>\r
37 \r
38 #include <tbb/parallel_invoke.h>\r
39 \r
40 #include <boost/timer.hpp>\r
41 #include <boost/range/algorithm.hpp>\r
42 #include <boost/range/algorithm_ext.hpp>\r
43 \r
44 #include <deque>\r
45 \r
46 namespace caspar {\r
47         \r
48 struct ffmpeg_producer : public core::frame_producer\r
49 {\r
50         const std::wstring                                              filename_;\r
51         \r
52         const safe_ptr<diagnostics::graph>              graph_;\r
53         boost::timer                                                    frame_timer_;\r
54                                         \r
55         const safe_ptr<core::frame_factory>             frame_factory_;\r
56 \r
57         input                                                                   input_; \r
58         std::unique_ptr<video_decoder>                  video_decoder_;\r
59         std::unique_ptr<audio_decoder>                  audio_decoder_;\r
60 public:\r
61         explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, bool loop, int start, int length) \r
62                 : filename_(filename)\r
63                 , graph_(diagnostics::create_graph(narrow(print())))\r
64                 , frame_factory_(frame_factory)         \r
65                 , input_(safe_ptr<diagnostics::graph>(graph_), filename_, loop, start)\r
66         {\r
67                 graph_->add_guide("frame-time", 0.5);\r
68                 graph_->set_color("frame-time",  diagnostics::color(1.0f, 0.0f, 0.0f));\r
69                 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));           \r
70                 \r
71                 double frame_time = 1.0f/input_.fps();\r
72                 double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;\r
73                 if(abs(frame_time - format_frame_time) > 0.0001 && abs(frame_time - format_frame_time/2) > 0.0001)\r
74                         CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time;\r
75                 \r
76                 video_decoder_.reset(input_.get_video_codec_context() ? \r
77                         new video_decoder(*input_.get_video_codec_context(), frame_factory) : nullptr);\r
78                         \r
79                 audio_decoder_.reset(input_.get_audio_codec_context() ? \r
80                         new audio_decoder(*input_.get_audio_codec_context(), frame_factory->get_video_format_desc()) : nullptr);                \r
81                                         \r
82                 // Fill buffers.\r
83                 decode_next_packets();\r
84         }\r
85 \r
86         virtual safe_ptr<core::basic_frame> receive()\r
87         {\r
88                 frame_timer_.restart();\r
89 \r
90                 auto result = decode_frame();\r
91                                 \r
92                 graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()*frame_factory_->get_video_format_desc().fps*0.5));\r
93                                         \r
94                 return result;\r
95         }\r
96         \r
97         virtual std::wstring print() const\r
98         {\r
99                 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
100         }\r
101 \r
102         void decode_next_packets()\r
103         {\r
104                 tbb::parallel_invoke\r
105                 (\r
106                         [&]\r
107                         {\r
108                                 std::shared_ptr<AVPacket> pkt;\r
109                                 for(int n = 0; n < 8 && video_decoder_ && video_decoder_->empty() && input_.try_pop_video_packet(pkt); ++n)\r
110                                         video_decoder_->push(std::move(pkt));\r
111                         }, \r
112                         [&]\r
113                         {\r
114                                 std::shared_ptr<AVPacket> pkt;\r
115                                 for(int n = 0; n < 8 && audio_decoder_ && audio_decoder_->empty() && input_.try_pop_audio_packet(pkt); ++n)\r
116                                         audio_decoder_->push(std::move(pkt));\r
117                         }\r
118                 );      \r
119         }\r
120 \r
121         safe_ptr<core::basic_frame> decode_frame()\r
122         {\r
123                 decode_next_packets();\r
124 \r
125                 if(video_decoder_ && !video_decoder_->empty() && audio_decoder_ && !audio_decoder_->empty())\r
126                 {\r
127                         auto frame = std::move(video_decoder_->front());                                \r
128                         video_decoder_->pop();\r
129                                 \r
130                         frame->audio_data() = std::move(audio_decoder_->front());\r
131                         audio_decoder_->pop();\r
132 \r
133                         return frame;\r
134                 }\r
135                 else if(video_decoder_ && !video_decoder_->empty() && !audio_decoder_)\r
136                 {\r
137                         auto frame = std::move(video_decoder_->front());                                \r
138                         video_decoder_->pop();\r
139                         frame->get_audio_transform().set_has_audio(false);      \r
140 \r
141                         return frame;\r
142                 }\r
143                 else if(audio_decoder_ && !audio_decoder_->empty() && !video_decoder_)\r
144                 {\r
145                         auto frame = frame_factory_->create_frame(this, 1, 1);\r
146                         std::fill(frame->image_data().begin(), frame->image_data().end(), 0);\r
147                                 \r
148                         frame->audio_data() = std::move(audio_decoder_->front());\r
149                         audio_decoder_->pop();\r
150 \r
151                         return frame;\r
152                 }\r
153                 else if(!input_.is_running() || (!video_decoder_ && !audio_decoder_))\r
154                 {\r
155                         return core::basic_frame::eof();\r
156                 }\r
157                 else\r
158                 {\r
159                         graph_->add_tag("underflow");\r
160                         return core::basic_frame::late();\r
161                 }\r
162         }\r
163 };\r
164 \r
165 safe_ptr<core::frame_producer> create_ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
166 {               \r
167         static const std::vector<std::wstring> extensions = boost::assign::list_of\r
168                 (L"mpg")(L"mpeg")(L"avi")(L"mov")(L"qt")(L"webm")(L"dv")(L"mp4")(L"f4v")(L"flv")(L"mkv")(L"mka")(L"wmv")(L"wma")(L"ogg")(L"divx")(L"xvid")(L"wav")(L"mp3")(L"m2v");\r
169         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
170         \r
171         auto ext = boost::find_if(extensions, [&](const std::wstring& ex)\r
172         {                                       \r
173                 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
174         });\r
175 \r
176         if(ext == extensions.end())\r
177                 return core::frame_producer::empty();\r
178 \r
179         std::wstring path = filename + L"." + *ext;\r
180         bool loop = boost::find(params, L"LOOP") != params.end();\r
181 \r
182         static const boost::wregex expr(L"\\((?<START>\\d+)(,(?<LENGTH>\\d+)?)?\\)");//(,(?<END>\\d+))?\\]"); // boost::regex has no repeated captures?\r
183         boost::wsmatch what;\r
184         auto it = std::find_if(params.begin(), params.end(), [&](const std::wstring& str)\r
185         {\r
186                 return boost::regex_match(str, what, expr);\r
187         });\r
188 \r
189         int start = -1;\r
190         int length = -1;\r
191 \r
192         if(it != params.end())\r
193         {\r
194                 start = lexical_cast_or_default(what["START"].str(), -1);\r
195                 if(what["LENGTH"].matched)\r
196                         length = lexical_cast_or_default(what["LENGTH"].str(), -1);\r
197         }\r
198         \r
199         return make_safe<ffmpeg_producer>(frame_factory, path, loop, start, length);\r
200 }\r
201 \r
202 }