]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/ffmpeg_producer.cpp
2.0.0.2: ffmpeg-producer: Double rate video is interlaced when running in interlaced...
[casparcg] / modules / ffmpeg / producer / ffmpeg_producer.cpp
1 #include "../stdafx.h"\r
2 \r
3 #include "ffmpeg_producer.h"\r
4 \r
5 #include "input.h"\r
6 #include "audio/audio_decoder.h"\r
7 #include "video/video_decoder.h"\r
8 \r
9 #include <common/utility/timer.h>\r
10 #include <common/diagnostics/graph.h>\r
11 \r
12 #include <core/producer/frame/basic_frame.h>\r
13 #include <core/producer/frame/write_frame.h>\r
14 #include <core/producer/frame/audio_transform.h>\r
15 #include <core/video_format.h>\r
16 \r
17 #include <common/env.h>\r
18 #include <common/utility/timer.h>\r
19 #include <common/utility/assert.h>\r
20 \r
21 #include <tbb/parallel_invoke.h>\r
22 \r
23 #include <deque>\r
24 #include <functional>\r
25 \r
26 namespace caspar {\r
27         \r
28 struct ffmpeg_producer : public core::frame_producer\r
29 {\r
30         const std::wstring                                      filename_;\r
31         const bool                                                      loop_;\r
32         printer                                                         parent_printer_;\r
33         \r
34         std::shared_ptr<diagnostics::graph>     graph_;\r
35         timer                                                           perf_timer_;\r
36                 \r
37         std::unique_ptr<audio_decoder>          audio_decoder_;\r
38         std::unique_ptr<video_decoder>          video_decoder_;\r
39 \r
40         std::deque<safe_ptr<core::write_frame>> video_frame_channel_;   \r
41         std::deque<std::vector<short>>                  audio_chunk_channel_;\r
42 \r
43         std::queue<safe_ptr<core::basic_frame>> ouput_channel_;\r
44                 \r
45         safe_ptr<core::basic_frame>                             last_frame_;\r
46         std::shared_ptr<core::frame_factory>    frame_factory_;\r
47 \r
48         std::unique_ptr<input>                          input_; \r
49 public:\r
50         explicit ffmpeg_producer(const std::wstring& filename, bool loop) \r
51                 : filename_(filename)\r
52                 , loop_(loop) \r
53                 , last_frame_(core::basic_frame(core::basic_frame::empty()))\r
54                 \r
55         {\r
56                 graph_ = diagnostics::create_graph(boost::bind(&ffmpeg_producer::print, this)); \r
57                 graph_->add_guide("frame-time", 0.5);\r
58                 graph_->set_color("frame-time",  diagnostics::color(1.0f, 0.0f, 0.0f));\r
59         }\r
60         \r
61         virtual void initialize(const safe_ptr<core::frame_factory>& frame_factory)\r
62         {\r
63                 frame_factory_ = frame_factory;\r
64                 input_.reset(new input(safe_ptr<diagnostics::graph>(graph_), filename_, loop_, std::bind(&ffmpeg_producer::print, this)));\r
65                 video_decoder_.reset(input_->get_video_codec_context().get() ? new video_decoder(input_->get_video_codec_context().get(), frame_factory) : nullptr);\r
66                 audio_decoder_.reset(input_->get_audio_codec_context().get() ? new audio_decoder(input_->get_audio_codec_context().get(), frame_factory->get_video_format_desc().fps) : nullptr);\r
67 \r
68                 double frame_time = 1.0f/input_->fps();\r
69                 double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;\r
70                 if(abs(frame_time - format_frame_time) > 0.0001)\r
71                         CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time;\r
72 \r
73         }\r
74                 \r
75         virtual void set_parent_printer(const printer& parent_printer) \r
76         {\r
77                 parent_printer_ = parent_printer;\r
78         }\r
79 \r
80         virtual safe_ptr<core::basic_frame> receive()\r
81         {\r
82                 perf_timer_.reset();\r
83 \r
84                 while(ouput_channel_.size() < 2 && !input_->is_eof())\r
85                 {       \r
86                         aligned_buffer video_packet;\r
87                         if(video_frame_channel_.size() < 3 && video_decoder_)   \r
88                                 video_packet = input_->get_video_packet();              \r
89                         \r
90                         aligned_buffer audio_packet;\r
91                         if(audio_chunk_channel_.size() < 3 && audio_decoder_)   \r
92                                 audio_packet = input_->get_audio_packet();              \r
93 \r
94                         tbb::parallel_invoke(\r
95                         [&]\r
96                         { // Video Decoding and Scaling\r
97                                 if(!video_packet.empty() && video_decoder_)\r
98                                 {\r
99                                         try\r
100                                         {\r
101                                                 auto frame = video_decoder_->execute(video_packet);\r
102                                                 frame->tag(reinterpret_cast<int>(this));\r
103                                                 video_frame_channel_.push_back(std::move(frame));\r
104                                         }\r
105                                         catch(...)\r
106                                         {\r
107                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
108                                                 video_decoder_.reset();\r
109                                                 CASPAR_LOG(warning) << print() << " removed video-stream.";\r
110                                         }\r
111                                 }\r
112                         }, \r
113                         [&] \r
114                         { // Audio Decoding\r
115                                 if(!audio_packet.empty() && audio_decoder_)\r
116                                 {\r
117                                         try\r
118                                         {\r
119                                                 auto chunks = audio_decoder_->execute(audio_packet);\r
120                                                 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
121                                         }\r
122                                         catch(...)\r
123                                         {\r
124                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
125                                                 audio_decoder_.reset();\r
126                                                 CASPAR_LOG(warning) << print() << " removed audio-stream.";\r
127                                         }\r
128                                 }\r
129                         });\r
130 \r
131                         while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))\r
132                         {\r
133                                 std::shared_ptr<core::write_frame> frame;\r
134 \r
135                                 if(video_decoder_)\r
136                                 {\r
137                                         frame = video_frame_channel_.front();\r
138                                         video_frame_channel_.pop_front();\r
139                                 }\r
140 \r
141                                 if(audio_decoder_) \r
142                                 {\r
143                                         if(!frame)\r
144                                         {\r
145                                                 frame = frame_factory_->create_frame(1, 1);\r
146                                                 std::fill(frame->image_data().begin(), frame->image_data().end(), 0);\r
147                                         }\r
148                                         \r
149                                         frame->audio_data() = std::move(audio_chunk_channel_.front());\r
150                                         audio_chunk_channel_.pop_front();\r
151                                 }\r
152                                                         \r
153                                 ouput_channel_.push(safe_ptr<core::write_frame>(frame));                                \r
154                         }                               \r
155 \r
156                         if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())                      \r
157                                 return last_frame_;                     \r
158                 }\r
159                 \r
160                 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));\r
161 \r
162                 auto result = last_frame_;\r
163                 if(!ouput_channel_.empty())\r
164                 {\r
165                         result = get_frame();\r
166 \r
167                         bool interlace = abs(input_->fps()/2.0 - frame_factory_->get_video_format_desc().fps) < 0.001;\r
168                         if(interlace && !ouput_channel_.empty())\r
169                                 result = core::basic_frame::interlace(result, get_frame(), frame_factory_->get_video_format_desc().mode);\r
170                 }\r
171                 else if(input_->is_eof())\r
172                         result = core::basic_frame::eof();\r
173                 \r
174                 return result;\r
175         }\r
176 \r
177         core::basic_frame get_frame()\r
178         {\r
179                 CASPAR_ASSERT(!ouput_channel_.empty());\r
180                 auto result = std::move(ouput_channel_.front());\r
181                 last_frame_ = core::basic_frame(result);\r
182                 last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio\r
183                 ouput_channel_.pop();\r
184                 return result;\r
185         }\r
186 \r
187         virtual std::wstring print() const\r
188         {\r
189                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
190         }\r
191 };\r
192 \r
193 safe_ptr<core::frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)\r
194 {                       \r
195         static const std::vector<std::wstring> extensions = boost::assign::list_of\r
196                 (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"wmw")(L"wma")(L"ogg")(L"divx")(L"wav")(L"mp3");\r
197         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
198         \r
199         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
200                 {                                       \r
201                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
202                 });\r
203 \r
204         if(ext == extensions.end())\r
205                 return core::frame_producer::empty();\r
206 \r
207         std::wstring path = filename + L"." + *ext;\r
208         bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();\r
209         \r
210         return make_safe<ffmpeg_producer>(path, loop);\r
211 }\r
212 \r
213 }