2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../StdAfx.h"
24 #include "frame_muxer.h"
26 #include "../filter/filter.h"
27 #include "../util/util.h"
29 #include <core/producer/frame_producer.h>
30 #include <core/frame/draw_frame.h>
31 #include <core/frame/frame_transform.h>
32 #include <core/frame/pixel_format.h>
33 #include <core/frame/frame_factory.h>
34 #include <core/frame/frame.h>
36 #include <common/env.h>
37 #include <common/except.h>
38 #include <common/log.h>
41 #pragma warning (push)
42 #pragma warning (disable : 4244)
46 #define __STDC_CONSTANT_MACROS
47 #define __STDC_LIMIT_MACROS
48 #include <libavcodec/avcodec.h>
49 #include <libavformat/avformat.h>
55 #include <common/assert.h>
56 #include <boost/foreach.hpp>
57 #include <boost/range/algorithm_ext/push_back.hpp>
58 #include <boost/algorithm/string/predicate.hpp>
64 using namespace caspar::core;
66 namespace caspar { namespace ffmpeg {
68 struct frame_muxer::impl : boost::noncopyable
70 std::queue<core::mutable_frame> video_stream_;
71 core::audio_buffer audio_stream_;
72 std::queue<draw_frame> frame_buffer_;
73 display_mode display_mode_;
75 const video_format_desc format_desc_;
77 std::vector<int> audio_cadence_;
79 spl::shared_ptr<core::frame_factory> frame_factory_;
81 std::unique_ptr<filter> filter_;
82 const std::wstring filter_str_;
83 bool force_deinterlacing_;
85 impl(double in_fps, const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::wstring& filter_str)
86 : display_mode_(display_mode::invalid)
88 , format_desc_(format_desc)
89 , audio_cadence_(format_desc_.audio_cadence)
90 , frame_factory_(frame_factory)
91 , filter_str_(filter_str)
92 , force_deinterlacing_(env::properties().get(L"configuration.force-deinterlace", true))
94 // Note: Uses 1 step rotated cadence for 1001 modes (1602, 1602, 1601, 1602, 1601)
95 // This cadence fills the audio mixer most optimally.
96 boost::range::rotate(audio_cadence_, std::end(audio_cadence_)-1);
99 void push_video(const std::shared_ptr<AVFrame>& video)
106 auto empty_frame = frame_factory_->create_frame(this, core::pixel_format_desc(core::pixel_format::invalid));
107 video_stream_.push(std::move(empty_frame));
108 display_mode_ = display_mode::simple;
112 if(!filter_ || display_mode_ == display_mode::invalid)
113 update_display_mode(video);
115 filter_->push(video);
116 BOOST_FOREACH(auto& av_frame, filter_->poll_all())
117 video_stream_.push(make_frame(this, av_frame, format_desc_.fps, *frame_factory_));
123 void push_audio(const std::shared_ptr<AVFrame>& audio)
130 boost::range::push_back(audio_stream_, core::audio_buffer(audio_cadence_.front() * format_desc_.audio_channels, 0));
134 auto ptr = reinterpret_cast<int32_t*>(audio->data[0]);
135 audio_stream_.insert(audio_stream_.end(), ptr, ptr + audio->linesize[0]/sizeof(int32_t));
141 bool video_ready() const
143 switch(display_mode_)
145 case display_mode::deinterlace_bob_reinterlace:
146 case display_mode::interlace:
147 case display_mode::half:
148 return video_stream_.size() >= 2;
150 return video_stream_.size() >= 1;
154 bool audio_ready() const
156 switch(display_mode_)
158 case display_mode::duplicate:
159 return audio_stream_.size() >= static_cast<size_t>(audio_cadence_[0] + audio_cadence_[1 % audio_cadence_.size()]) * format_desc_.audio_channels;
161 return audio_stream_.size() >= static_cast<size_t>(audio_cadence_.front()) * format_desc_.audio_channels;
167 return frame_buffer_.empty();
170 core::draw_frame front() const
172 return frame_buffer_.front();
182 while(video_ready() && audio_ready() && display_mode_ != display_mode::invalid)
184 auto frame1 = pop_video();
185 frame1.audio_data() = pop_audio();
187 switch(display_mode_)
189 case display_mode::simple:
190 case display_mode::deinterlace_bob:
191 case display_mode::deinterlace:
193 frame_buffer_.push(core::draw_frame(std::move(frame1)));
196 case display_mode::interlace:
197 case display_mode::deinterlace_bob_reinterlace:
199 auto frame2 = pop_video();
201 frame_buffer_.push(core::draw_frame::interlace(
202 core::draw_frame(std::move(frame1)),
203 core::draw_frame(std::move(frame2)),
204 format_desc_.field_mode));
207 case display_mode::duplicate:
209 //boost::range::push_back(frame1.audio_data(), pop_audio());
211 auto second_audio_frame = core::mutable_frame(
212 std::vector<array<std::uint8_t>>(),
215 core::pixel_format_desc());
216 auto first_frame = core::draw_frame(std::move(frame1));
217 auto muted_first_frame = core::draw_frame(first_frame);
218 muted_first_frame.transform().audio_transform.volume = 0;
219 auto second_frame = core::draw_frame({ core::draw_frame(std::move(second_audio_frame)), muted_first_frame });
221 // Same video but different audio.
222 frame_buffer_.push(first_frame);
223 frame_buffer_.push(second_frame);
226 case display_mode::half:
228 pop_video(); // Throw away
230 frame_buffer_.push(core::draw_frame(std::move(frame1)));
234 CASPAR_THROW_EXCEPTION(invalid_operation());
239 core::mutable_frame pop_video()
241 auto frame = std::move(video_stream_.front());
243 return std::move(frame);
246 core::audio_buffer pop_audio()
248 if (audio_stream_.size() < audio_cadence_.front() * format_desc_.audio_channels)
249 CASPAR_THROW_EXCEPTION(out_of_range());
251 auto begin = audio_stream_.begin();
252 auto end = begin + audio_cadence_.front() * format_desc_.audio_channels;
254 core::audio_buffer samples(begin, end);
255 audio_stream_.erase(begin, end);
257 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
262 void update_display_mode(const std::shared_ptr<AVFrame>& frame)
264 std::wstring filter_str = filter_str_;
266 display_mode_ = display_mode::simple;
268 auto mode = get_mode(*frame);
269 if(mode == core::field_mode::progressive && frame->height < 720 && in_fps_ < 50.0) // SD frames are interlaced. Probably incorrect meta-data. Fix it.
270 mode = core::field_mode::upper;
274 if(filter::is_deinterlacing(filter_str_))
275 mode = core::field_mode::progressive;
277 if(filter::is_double_rate(filter_str_))
280 display_mode_ = get_display_mode(mode, fps, format_desc_.field_mode, format_desc_.fps);
282 if((frame->height != 480 || format_desc_.height != 486) && // don't deinterlace for NTSC DV
283 display_mode_ == display_mode::simple && mode != core::field_mode::progressive && format_desc_.field_mode != core::field_mode::progressive &&
284 frame->height != format_desc_.height)
286 display_mode_ = display_mode::deinterlace_bob_reinterlace; // The frame will most likely be scaled, we need to deinterlace->reinterlace
289 // ALWAYS de-interlace, until we have GPU de-interlacing.
290 if(force_deinterlacing_ && frame->interlaced_frame && display_mode_ != display_mode::deinterlace_bob && display_mode_ != display_mode::deinterlace)
291 display_mode_ = display_mode::deinterlace_bob_reinterlace;
293 if(display_mode_ == display_mode::deinterlace)
294 filter_str = append_filter(filter_str, L"YADIF=0:-1");
295 else if(display_mode_ == display_mode::deinterlace_bob || display_mode_ == display_mode::deinterlace_bob_reinterlace)
296 filter_str = append_filter(filter_str, L"YADIF=1:-1");
298 if(display_mode_ == display_mode::invalid)
300 CASPAR_LOG(warning) << L"[frame_muxer] Auto-transcode: Failed to detect display-mode.";
301 display_mode_ = display_mode::simple;
304 if(frame->height == 480) // NTSC DV
306 auto pad_str = L"PAD=" + boost::lexical_cast<std::wstring>(frame->width) + L":486:0:2:black";
307 filter_str = append_filter(filter_str, pad_str);
310 filter_.reset (new filter(
313 boost::rational<int>(1000000, static_cast<int>(in_fps_ * 1000000)),
314 boost::rational<int>(static_cast<int>(in_fps_ * 1000000), 1000000),
315 boost::rational<int>(frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den),
316 static_cast<AVPixelFormat>(frame->format),
317 std::vector<AVPixelFormat>(),
320 CASPAR_LOG(info) << L"[frame_muxer] " << display_mode_ << L" " << print_mode(frame->width, frame->height, in_fps_, frame->interlaced_frame > 0);
323 uint32_t calc_nb_frames(uint32_t nb_frames) const
325 uint64_t nb_frames2 = nb_frames;
327 if(filter_ && filter_->is_double_rate()) // Take into account transformations in filter.
330 switch(display_mode_) // Take into account transformation in run.
332 case display_mode::deinterlace_bob_reinterlace:
333 case display_mode::interlace:
334 case display_mode::half:
337 case display_mode::duplicate:
342 return static_cast<uint32_t>(nb_frames2);
347 while(!video_stream_.empty())
350 audio_stream_.clear();
352 while(!frame_buffer_.empty())
359 frame_muxer::frame_muxer(double in_fps, const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::wstring& filter)
360 : impl_(new impl(in_fps, frame_factory, format_desc, filter)){}
361 void frame_muxer::push_video(const std::shared_ptr<AVFrame>& frame){impl_->push_video(frame);}
362 void frame_muxer::push_audio(const std::shared_ptr<AVFrame>& frame){impl_->push_audio(frame);}
363 bool frame_muxer::empty() const{return impl_->empty();}
364 core::draw_frame frame_muxer::front() const{return impl_->front();}
365 void frame_muxer::pop(){return impl_->pop();}
366 void frame_muxer::clear(){impl_->clear();}
367 uint32_t frame_muxer::calc_nb_frames(uint32_t nb_frames) const {return impl_->calc_nb_frames(nb_frames);}
368 bool frame_muxer::video_ready() const{return impl_->video_ready();}
369 bool frame_muxer::audio_ready() const{return impl_->audio_ready();}