]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/muxer/frame_muxer.cpp
2.0.2: Intergrated channel-exp branch:
[casparcg] / modules / ffmpeg / producer / muxer / frame_muxer.cpp
1 #include "../../StdAfx.h"\r
2 \r
3 #include "frame_muxer.h"\r
4 \r
5 #include "display_mode.h"\r
6 \r
7 #include "../filter/filter.h"\r
8 #include "../util/util.h"\r
9 \r
10 #include <core/producer/frame_producer.h>\r
11 #include <core/producer/frame/basic_frame.h>\r
12 #include <core/producer/frame/frame_transform.h>\r
13 #include <core/producer/frame/pixel_format.h>\r
14 #include <core/producer/frame/frame_factory.h>\r
15 #include <core/mixer/write_frame.h>\r
16 \r
17 #include <common/env.h>\r
18 #include <common/exception/exceptions.h>\r
19 #include <common/log/log.h>\r
20 \r
21 #if defined(_MSC_VER)\r
22 #pragma warning (push)\r
23 #pragma warning (disable : 4244)\r
24 #endif\r
25 extern "C" \r
26 {\r
27         #define __STDC_CONSTANT_MACROS\r
28         #define __STDC_LIMIT_MACROS\r
29         #include <libavcodec/avcodec.h>\r
30         #include <libavformat/avformat.h>\r
31 }\r
32 #if defined(_MSC_VER)\r
33 #pragma warning (pop)\r
34 #endif\r
35 \r
36 #include <boost/foreach.hpp>\r
37 #include <boost/range/algorithm_ext/push_back.hpp>\r
38 \r
39 #include <deque>\r
40 #include <queue>\r
41 #include <vector>\r
42 \r
43 using namespace caspar::core;\r
44 \r
45 namespace caspar { namespace ffmpeg {\r
46         \r
47 struct frame_muxer::implementation : boost::noncopyable\r
48 {       \r
49         std::queue<std::queue<safe_ptr<write_frame>>>   video_streams_;\r
50         std::queue<core::audio_buffer>                                  audio_streams_;\r
51         std::queue<safe_ptr<basic_frame>>                               frame_buffer_;\r
52         display_mode::type                                                              display_mode_;\r
53         const double                                                                    in_fps_;\r
54         const video_format_desc                                                 format_desc_;\r
55         bool                                                                                    auto_transcode_;\r
56 \r
57         size_t                                                                                  audio_sample_count_;\r
58         size_t                                                                                  video_frame_count_;\r
59                 \r
60         safe_ptr<core::frame_factory>                                   frame_factory_;\r
61         \r
62         filter                                                                                  filter_;\r
63         std::wstring                                                                    filter_str_;\r
64                 \r
65         implementation(double in_fps, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter_str)\r
66                 : display_mode_(display_mode::invalid)\r
67                 , in_fps_(in_fps)\r
68                 , format_desc_(frame_factory->get_video_format_desc())\r
69                 , auto_transcode_(env::properties().get("configuration.auto-transcode", true))\r
70                 , audio_sample_count_(0)\r
71                 , video_frame_count_(0)\r
72                 , frame_factory_(frame_factory)\r
73                 , filter_str_(filter_str)\r
74         {\r
75                 video_streams_.push(std::queue<safe_ptr<write_frame>>());\r
76                 audio_streams_.push(core::audio_buffer());\r
77         }\r
78 \r
79         void push(const std::shared_ptr<AVFrame>& video_frame, int hints)\r
80         {               \r
81                 if(!video_frame)\r
82                         return;\r
83                 \r
84                 if(video_frame == flush_video())\r
85                 {       \r
86                         CASPAR_LOG(trace) << L"video-frame-count: " << static_cast<float>(video_frame_count_);\r
87                         video_frame_count_ = 0;\r
88                         video_streams_.push(std::queue<safe_ptr<write_frame>>());\r
89                 }\r
90                 else if(video_frame == empty_video())\r
91                 {\r
92                         video_streams_.back().push(make_safe<core::write_frame>(this));\r
93                         ++video_frame_count_;\r
94                         display_mode_ = display_mode::simple;\r
95                 }\r
96                 else\r
97                 {\r
98                         if(display_mode_ == display_mode::invalid)\r
99                                 initialize_display_mode(*video_frame);\r
100                                 \r
101                         if(hints & core::frame_producer::ALPHA_HINT)\r
102                                 video_frame->format = make_alpha_format(video_frame->format);\r
103                 \r
104                         auto format = video_frame->format;\r
105                         if(video_frame->format == CASPAR_PIX_FMT_LUMA) // CASPAR_PIX_FMT_LUMA is not valid for filter, change it to GRAY8\r
106                                 video_frame->format = PIX_FMT_GRAY8;\r
107 \r
108                         filter_.push(video_frame);\r
109                         BOOST_FOREACH(auto& av_frame, filter_.poll_all())\r
110                         {\r
111                                 av_frame->format = format;\r
112                                 video_streams_.back().push(make_write_frame(this, av_frame, frame_factory_, hints));\r
113                                 ++video_frame_count_;\r
114                         }\r
115                 }\r
116 \r
117                 if(video_streams_.back().size() > 32)\r
118                         BOOST_THROW_EXCEPTION(invalid_operation() << source_info("frame_muxer") << msg_info("video-stream overflow. This can be caused by incorrect frame-rate. Check clip meta-data."));\r
119         }\r
120 \r
121         void push(const std::shared_ptr<core::audio_buffer>& audio)\r
122         {\r
123                 if(!audio)      \r
124                         return;\r
125 \r
126                 if(audio == flush_audio())\r
127                 {\r
128                         CASPAR_LOG(trace) << L"audio-frame-count: " << audio_sample_count_/format_desc_.audio_samples_per_frame;\r
129                         audio_sample_count_ = 0;\r
130                         audio_streams_.push(core::audio_buffer());\r
131                 }\r
132                 else if(audio == empty_audio())\r
133                 {\r
134                         boost::range::push_back(audio_streams_.back(), core::audio_buffer(format_desc_.audio_samples_per_frame, 0));\r
135                         audio_sample_count_ += audio->size();\r
136                 }\r
137                 else\r
138                 {\r
139                         boost::range::push_back(audio_streams_.back(), *audio);\r
140                         audio_sample_count_ += audio->size();\r
141                 }\r
142 \r
143                 if(audio_streams_.back().size() > 32*format_desc_.audio_samples_per_frame)\r
144                         BOOST_THROW_EXCEPTION(invalid_operation() << source_info("frame_muxer") << msg_info("audio-stream overflow. This can be caused by incorrect frame-rate. Check clip meta-data."));\r
145         }\r
146         \r
147         bool video_ready() const\r
148         {               \r
149                 return video_streams_.size() > 1 || (video_streams_.size() >= audio_streams_.size() && video_ready2());\r
150         }\r
151         \r
152         bool audio_ready() const\r
153         {\r
154                 return audio_streams_.size() > 1 || (audio_streams_.size() >= video_streams_.size() && audio_ready2());\r
155         }\r
156 \r
157         bool video_ready2() const\r
158         {               \r
159                 switch(display_mode_)\r
160                 {\r
161                 case display_mode::deinterlace_bob_reinterlace:                                 \r
162                 case display_mode::interlace:   \r
163                 case display_mode::half:\r
164                         return video_streams_.front().size() >= 2;\r
165                 default:                                                                                \r
166                         return video_streams_.front().size() >= 1;\r
167                 }\r
168         }\r
169         \r
170         bool audio_ready2() const\r
171         {\r
172                 switch(display_mode_)\r
173                 {\r
174                 case display_mode::duplicate:                                   \r
175                         return audio_streams_.front().size()/2 >= format_desc_.audio_samples_per_frame;\r
176                 default:                                                                                \r
177                         return audio_streams_.front().size() >= format_desc_.audio_samples_per_frame;\r
178                 }\r
179         }\r
180                 \r
181         std::shared_ptr<basic_frame> poll()\r
182         {\r
183                 if(!frame_buffer_.empty())\r
184                 {\r
185                         auto frame = frame_buffer_.front();\r
186                         frame_buffer_.pop();    \r
187                         return frame;\r
188                 }\r
189 \r
190                 if(video_streams_.size() > 1 && audio_streams_.size() > 1 && (!video_ready2() || !audio_ready2()))\r
191                 {\r
192                         if(!video_streams_.front().empty() || !audio_streams_.front().empty())\r
193                                 CASPAR_LOG(debug) << "Truncating: " << video_streams_.front().size() << L" video-frames, " << audio_streams_.front().size() << L" audio-samples.";\r
194 \r
195                         video_streams_.pop();\r
196                         audio_streams_.pop();\r
197                 }\r
198 \r
199                 if(!video_ready2() || !audio_ready2())\r
200                         return nullptr;\r
201                                 \r
202                 auto frame1                             = pop_video();\r
203                 frame1->audio_data()    = pop_audio();\r
204 \r
205                 switch(display_mode_)\r
206                 {\r
207                 case display_mode::simple:                                              \r
208                 case display_mode::deinterlace_bob:                             \r
209                 case display_mode::deinterlace: \r
210                 {\r
211                         frame_buffer_.push(frame1);\r
212                         break;\r
213                 }\r
214                 case display_mode::interlace:                                   \r
215                 case display_mode::deinterlace_bob_reinterlace: \r
216                 {                               \r
217                         auto frame2 = pop_video();\r
218 \r
219                         frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode));      \r
220                         break;\r
221                 }\r
222                 case display_mode::duplicate:   \r
223                 {\r
224                         auto frame2                             = make_safe<core::write_frame>(*frame1);\r
225                         frame2->audio_data()    = pop_audio();\r
226 \r
227                         frame_buffer_.push(frame1);\r
228                         frame_buffer_.push(frame2);\r
229                         break;\r
230                 }\r
231                 case display_mode::half:        \r
232                 {                               \r
233                         pop_video(); // Throw away\r
234 \r
235                         frame_buffer_.push(frame1);\r
236                         break;\r
237                 }\r
238                 default:                                                                                \r
239                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("invalid display-mode"));\r
240                 }\r
241                 \r
242                 return frame_buffer_.empty() ? nullptr : poll();\r
243         }\r
244         \r
245         safe_ptr<core::write_frame> pop_video()\r
246         {\r
247                 auto frame = video_streams_.front().front();\r
248                 video_streams_.front().pop();           \r
249                 return frame;\r
250         }\r
251 \r
252         core::audio_buffer pop_audio()\r
253         {\r
254                 CASPAR_VERIFY(audio_streams_.front().size() >= format_desc_.audio_samples_per_frame);\r
255 \r
256                 auto begin = audio_streams_.front().begin();\r
257                 auto end   = begin + format_desc_.audio_samples_per_frame;\r
258 \r
259                 auto samples = core::audio_buffer(begin, end);\r
260                 audio_streams_.front().erase(begin, end);\r
261 \r
262                 return samples;\r
263         }\r
264                                 \r
265         void initialize_display_mode(const AVFrame& frame)\r
266         {\r
267                 display_mode_ = display_mode::simple;\r
268                 if(auto_transcode_)\r
269                 {\r
270                         auto mode = get_mode(frame);\r
271                         auto fps  = in_fps_;\r
272 \r
273                         if(is_deinterlacing(filter_str_))\r
274                                 mode = core::field_mode::progressive;\r
275 \r
276                         if(is_double_rate(filter_str_))\r
277                                 fps *= 2;\r
278                         \r
279                         display_mode_ = get_display_mode(mode, fps, format_desc_.field_mode, format_desc_.fps);\r
280                         \r
281                         if((frame.height != 480 || format_desc_.height != 486) && \r
282                                 display_mode_ == display_mode::simple && mode != core::field_mode::progressive && format_desc_.field_mode != core::field_mode::progressive && \r
283                                 frame.height != static_cast<int>(format_desc_.height))\r
284                         {\r
285                                 display_mode_ = display_mode::deinterlace_bob_reinterlace; // The frame will most likely be scaled, we need to deinterlace->reinterlace \r
286                         }\r
287 \r
288                         if(display_mode_ == display_mode::deinterlace)\r
289                                 filter_str_ = append_filter(filter_str_, L"YADIF=0:-1");\r
290                         else if(display_mode_ == display_mode::deinterlace_bob || display_mode_ == display_mode::deinterlace_bob_reinterlace)\r
291                                 filter_str_ = append_filter(filter_str_, L"YADIF=1:-1");\r
292                 }\r
293 \r
294                 if(display_mode_ == display_mode::invalid)\r
295                 {\r
296                         CASPAR_LOG(warning) << L"[frame_muxer] Auto-transcode: Failed to detect display-mode.";\r
297                         display_mode_ = display_mode::simple;\r
298                 }\r
299                         \r
300                 filter_ = filter(filter_str_);\r
301 \r
302                 CASPAR_LOG(info) << "[frame_muxer] " << display_mode::print(display_mode_) \r
303                         << L" " << frame.width << L"x" << frame.height \r
304                         << (frame.interlaced_frame ? L"i" : L"p") \r
305                         << (frame.interlaced_frame ? in_fps_*2 : in_fps_);\r
306         }\r
307 \r
308         int64_t calc_nb_frames(int64_t nb_frames) const\r
309         {\r
310                 switch(display_mode_) // Take into account transformation in run.\r
311                 {\r
312                 case display_mode::deinterlace_bob_reinterlace:\r
313                 case display_mode::interlace:   \r
314                 case display_mode::half:\r
315                         nb_frames /= 2;\r
316                         break;\r
317                 case display_mode::duplicate:\r
318                         nb_frames *= 2;\r
319                         break;\r
320                 }\r
321 \r
322                 if(is_double_rate(widen(filter_.filter_str()))) // Take into account transformations in filter.\r
323                         nb_frames *= 2;\r
324 \r
325                 return nb_frames;\r
326         }\r
327 };\r
328 \r
329 frame_muxer::frame_muxer(double in_fps, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter)\r
330         : impl_(new implementation(in_fps, frame_factory, filter)){}\r
331 void frame_muxer::push(const std::shared_ptr<AVFrame>& video_frame, int hints){impl_->push(video_frame, hints);}\r
332 void frame_muxer::push(const std::shared_ptr<core::audio_buffer>& audio_samples){return impl_->push(audio_samples);}\r
333 std::shared_ptr<basic_frame> frame_muxer::poll(){return impl_->poll();}\r
334 int64_t frame_muxer::calc_nb_frames(int64_t nb_frames) const {return impl_->calc_nb_frames(nb_frames);}\r
335 bool frame_muxer::video_ready() const{return impl_->video_ready();}\r
336 bool frame_muxer::audio_ready() const{return impl_->audio_ready();}\r
337 \r
338 }}