1 #include "../../StdAfx.h"
\r
3 #include "frame_muxer.h"
\r
5 #include "display_mode.h"
\r
7 #include "../filter/filter.h"
\r
8 #include "../util/util.h"
\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
17 #include <common/env.h>
\r
18 #include <common/exception/exceptions.h>
\r
19 #include <common/log/log.h>
\r
21 #if defined(_MSC_VER)
\r
22 #pragma warning (push)
\r
23 #pragma warning (disable : 4244)
\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
32 #if defined(_MSC_VER)
\r
33 #pragma warning (pop)
\r
36 #include <boost/foreach.hpp>
\r
37 #include <boost/range/algorithm_ext/push_back.hpp>
\r
43 using namespace caspar::core;
\r
45 namespace caspar { namespace ffmpeg {
\r
47 struct frame_muxer::implementation : boost::noncopyable
\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
57 size_t audio_sample_count_;
\r
58 size_t video_frame_count_;
\r
60 safe_ptr<core::frame_factory> frame_factory_;
\r
63 std::wstring filter_str_;
\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
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
75 video_streams_.push(std::queue<safe_ptr<write_frame>>());
\r
76 audio_streams_.push(core::audio_buffer());
\r
79 void push(const std::shared_ptr<AVFrame>& video_frame, int hints)
\r
84 if(video_frame == flush_video())
\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
90 else if(video_frame == empty_video())
\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
98 if(display_mode_ == display_mode::invalid)
\r
99 initialize_display_mode(*video_frame);
\r
101 if(hints & core::frame_producer::ALPHA_HINT)
\r
102 video_frame->format = make_alpha_format(video_frame->format);
\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
108 filter_.push(video_frame);
\r
109 BOOST_FOREACH(auto& av_frame, filter_.poll_all())
\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
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
121 void push(const std::shared_ptr<core::audio_buffer>& audio)
\r
126 if(audio == flush_audio())
\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
132 else if(audio == empty_audio())
\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
139 boost::range::push_back(audio_streams_.back(), *audio);
\r
140 audio_sample_count_ += audio->size();
\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
147 bool video_ready() const
\r
149 return video_streams_.size() > 1 || (video_streams_.size() >= audio_streams_.size() && video_ready2());
\r
152 bool audio_ready() const
\r
154 return audio_streams_.size() > 1 || (audio_streams_.size() >= video_streams_.size() && audio_ready2());
\r
157 bool video_ready2() const
\r
159 switch(display_mode_)
\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
166 return video_streams_.front().size() >= 1;
\r
170 bool audio_ready2() const
\r
172 switch(display_mode_)
\r
174 case display_mode::duplicate:
\r
175 return audio_streams_.front().size()/2 >= format_desc_.audio_samples_per_frame;
\r
177 return audio_streams_.front().size() >= format_desc_.audio_samples_per_frame;
\r
181 std::shared_ptr<basic_frame> poll()
\r
183 if(!frame_buffer_.empty())
\r
185 auto frame = frame_buffer_.front();
\r
186 frame_buffer_.pop();
\r
190 if(video_streams_.size() > 1 && audio_streams_.size() > 1 && (!video_ready2() || !audio_ready2()))
\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
195 video_streams_.pop();
\r
196 audio_streams_.pop();
\r
199 if(!video_ready2() || !audio_ready2())
\r
202 auto frame1 = pop_video();
\r
203 frame1->audio_data() = pop_audio();
\r
205 switch(display_mode_)
\r
207 case display_mode::simple:
\r
208 case display_mode::deinterlace_bob:
\r
209 case display_mode::deinterlace:
\r
211 frame_buffer_.push(frame1);
\r
214 case display_mode::interlace:
\r
215 case display_mode::deinterlace_bob_reinterlace:
\r
217 auto frame2 = pop_video();
\r
219 frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode));
\r
222 case display_mode::duplicate:
\r
224 auto frame2 = make_safe<core::write_frame>(*frame1);
\r
225 frame2->audio_data() = pop_audio();
\r
227 frame_buffer_.push(frame1);
\r
228 frame_buffer_.push(frame2);
\r
231 case display_mode::half:
\r
233 pop_video(); // Throw away
\r
235 frame_buffer_.push(frame1);
\r
239 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("invalid display-mode"));
\r
242 return frame_buffer_.empty() ? nullptr : poll();
\r
245 safe_ptr<core::write_frame> pop_video()
\r
247 auto frame = video_streams_.front().front();
\r
248 video_streams_.front().pop();
\r
252 core::audio_buffer pop_audio()
\r
254 CASPAR_VERIFY(audio_streams_.front().size() >= format_desc_.audio_samples_per_frame);
\r
256 auto begin = audio_streams_.front().begin();
\r
257 auto end = begin + format_desc_.audio_samples_per_frame;
\r
259 auto samples = core::audio_buffer(begin, end);
\r
260 audio_streams_.front().erase(begin, end);
\r
265 void initialize_display_mode(const AVFrame& frame)
\r
267 display_mode_ = display_mode::simple;
\r
268 if(auto_transcode_)
\r
270 auto mode = get_mode(frame);
\r
271 auto fps = in_fps_;
\r
273 if(is_deinterlacing(filter_str_))
\r
274 mode = core::field_mode::progressive;
\r
276 if(is_double_rate(filter_str_))
\r
279 display_mode_ = get_display_mode(mode, fps, format_desc_.field_mode, format_desc_.fps);
\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
285 display_mode_ = display_mode::deinterlace_bob_reinterlace; // The frame will most likely be scaled, we need to deinterlace->reinterlace
\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
294 if(display_mode_ == display_mode::invalid)
\r
296 CASPAR_LOG(warning) << L"[frame_muxer] Auto-transcode: Failed to detect display-mode.";
\r
297 display_mode_ = display_mode::simple;
\r
300 filter_ = filter(filter_str_);
\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
308 int64_t calc_nb_frames(int64_t nb_frames) const
\r
310 switch(display_mode_) // Take into account transformation in run.
\r
312 case display_mode::deinterlace_bob_reinterlace:
\r
313 case display_mode::interlace:
\r
314 case display_mode::half:
\r
317 case display_mode::duplicate:
\r
322 if(is_double_rate(widen(filter_.filter_str()))) // Take into account transformations in filter.
\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