]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/channel_producer.cpp
[channel_producer] #590 Added NO_AUTO_DEINTERLACE parameter to channel route AMCP...
[casparcg] / modules / reroute / producer / channel_producer.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "channel_producer.h"
25
26 #include <core/monitor/monitor.h>
27 #include <core/consumer/frame_consumer.h>
28 #include <core/consumer/output.h>
29 #include <core/producer/frame_producer.h>
30 #include <core/producer/framerate/framerate_producer.h>
31 #include <core/video_channel.h>
32
33 #include <core/frame/frame.h>
34 #include <core/frame/pixel_format.h>
35 #include <core/frame/audio_channel_layout.h>
36 #include <core/frame/draw_frame.h>
37 #include <core/frame/frame_factory.h>
38 #include <core/video_format.h>
39
40 #include <boost/thread/once.hpp>
41 #include <boost/lexical_cast.hpp>
42 #include <boost/property_tree/ptree.hpp>
43 #include <boost/range/algorithm/copy.hpp>
44
45 #include <common/except.h>
46 #include <common/memory.h>
47 #include <common/memcpy.h>
48 #include <common/semaphore.h>
49 #include <common/future.h>
50
51 #include <tbb/concurrent_queue.h>
52
53 #if defined(_MSC_VER)
54 #pragma warning (push)
55 #pragma warning (disable : 4244)
56 #endif
57 extern "C"
58 {
59 #define __STDC_CONSTANT_MACROS
60 #define __STDC_LIMIT_MACROS
61 #include <libavcodec/avcodec.h>
62 #include <libavformat/avformat.h>
63 }
64 #if defined(_MSC_VER)
65 #pragma warning (pop)
66 #endif
67
68 #include <modules/ffmpeg/producer/muxer/frame_muxer.h>
69 #include <modules/ffmpeg/producer/util/util.h>
70
71 #include <asmlib.h>
72
73 #include <queue>
74
75 namespace caspar { namespace reroute {
76
77 class channel_consumer : public core::frame_consumer
78 {
79         core::monitor::subject                                                          monitor_subject_;
80         tbb::concurrent_bounded_queue<core::const_frame>        frame_buffer_;
81         core::video_format_desc                                                         format_desc_;
82         core::audio_channel_layout                                                      channel_layout_                 = core::audio_channel_layout::invalid();
83         int                                                                                                     channel_index_;
84         int                                                                                                     consumer_index_;
85         tbb::atomic<bool>                                                                       is_running_;
86         tbb::atomic<int64_t>                                                            current_age_;
87         semaphore                                                                                       frames_available_ { 0 };
88         int                                                                                                     frames_delay_;
89
90 public:
91         channel_consumer(int frames_delay)
92                 : consumer_index_(next_consumer_index())
93                 , frames_delay_(frames_delay)
94         {
95                 is_running_ = true;
96                 current_age_ = 0;
97                 frame_buffer_.set_capacity(3 + frames_delay);
98         }
99
100         static int next_consumer_index()
101         {
102                 static tbb::atomic<int> consumer_index_counter;
103                 static boost::once_flag consumer_index_counter_initialized;
104
105                 boost::call_once(consumer_index_counter_initialized, [&]()
106                 {
107                         consumer_index_counter = 0;
108                 });
109
110                 return ++consumer_index_counter;
111         }
112
113         ~channel_consumer()
114         {
115         }
116
117         // frame_consumer
118
119         std::future<bool> send(core::const_frame frame) override
120         {
121                 bool pushed = frame_buffer_.try_push(frame);
122
123                 if (pushed)
124                         frames_available_.release();
125
126                 return make_ready_future(is_running_.load());
127         }
128
129         void initialize(
130                         const core::video_format_desc& format_desc,
131                         const core::audio_channel_layout& channel_layout,
132                         int channel_index) override
133         {
134                 format_desc_    = format_desc;
135                 channel_layout_ = channel_layout;
136                 channel_index_  = channel_index;
137         }
138
139         std::wstring name() const override
140         {
141                 return L"channel-consumer";
142         }
143
144         int64_t presentation_frame_age_millis() const override
145         {
146                 return current_age_;
147         }
148
149         std::wstring print() const override
150         {
151                 return L"[channel-consumer|" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
152         }
153
154         boost::property_tree::wptree info() const override
155         {
156                 boost::property_tree::wptree info;
157                 info.add(L"type", L"channel-consumer");
158                 info.add(L"channel-index", channel_index_);
159                 return info;
160         }
161
162         bool has_synchronization_clock() const override
163         {
164                 return false;
165         }
166
167         int buffer_depth() const override
168         {
169                 return -1;
170         }
171
172         int index() const override
173         {
174                 return 78500 + consumer_index_;
175         }
176
177         core::monitor::subject& monitor_output() override
178         {
179                 return monitor_subject_;
180         }
181
182         // channel_consumer
183
184         const core::video_format_desc& get_video_format_desc()
185         {
186                 return format_desc_;
187         }
188
189         const core::audio_channel_layout& get_audio_channel_layout()
190         {
191                 return channel_layout_;
192         }
193
194         void block_until_first_frame_available()
195         {
196                 if (!frames_available_.try_acquire(1 + frames_delay_, boost::chrono::seconds(2)))
197                         CASPAR_LOG(warning)
198                                         << print() << L" Timed out while waiting for first frame";
199         }
200
201         core::const_frame receive()
202         {
203                 core::const_frame frame = core::const_frame::empty();
204
205                 if (!is_running_)
206                         return frame;
207
208                 if (frame_buffer_.try_pop(frame))
209                         current_age_ = frame.get_age_millis();
210
211                 return frame;
212         }
213
214         void stop()
215         {
216                 is_running_ = false;
217         }
218 };
219
220 core::video_format_desc get_progressive_format(core::video_format_desc format_desc)
221 {
222         if (format_desc.field_count == 1)
223                 return format_desc;
224
225         format_desc.framerate           *= 2;
226         format_desc.fps                         *= 2.0;
227         format_desc.audio_cadence        = core::find_audio_cadence(format_desc.framerate);
228         format_desc.time_scale          *= 2;
229         format_desc.field_count          = 1;
230
231         return format_desc;
232 }
233
234 class channel_producer : public core::frame_producer_base
235 {
236         core::monitor::subject                                          monitor_subject_;
237
238         const spl::shared_ptr<core::frame_factory>      frame_factory_;
239         const core::video_format_desc                           output_format_desc_;
240         const spl::shared_ptr<channel_consumer>         consumer_;
241         core::constraints                                                       pixel_constraints_;
242         ffmpeg::frame_muxer                                                     muxer_;
243
244         std::queue<core::draw_frame>                            frame_buffer_;
245
246 public:
247         explicit channel_producer(
248                         const core::frame_producer_dependencies& dependecies,
249                         const spl::shared_ptr<core::video_channel>& channel,
250                         int frames_delay,
251                         bool no_auto_deinterlace)
252                 : frame_factory_(dependecies.frame_factory)
253                 , output_format_desc_(dependecies.format_desc)
254                 , consumer_(spl::make_shared<channel_consumer>(frames_delay))
255                 , muxer_(
256                                 channel->video_format_desc().framerate,
257                                 { ffmpeg::create_input_pad(channel->video_format_desc(), channel->audio_channel_layout().num_channels) },
258                                 dependecies.frame_factory,
259                                 no_auto_deinterlace ? channel->video_format_desc() : get_progressive_format(channel->video_format_desc()),
260                                 channel->audio_channel_layout(),
261                                 L"",
262                                 false,
263                                 !no_auto_deinterlace)
264         {
265                 pixel_constraints_.width.set(output_format_desc_.width);
266                 pixel_constraints_.height.set(output_format_desc_.height);
267                 channel->output().add(consumer_);
268                 consumer_->block_until_first_frame_available();
269                 CASPAR_LOG(info) << print() << L" Initialized";
270         }
271
272         ~channel_producer()
273         {
274                 consumer_->stop();
275                 CASPAR_LOG(info) << print() << L" Uninitialized";
276         }
277
278         // frame_producer
279
280         core::draw_frame receive_impl() override
281         {
282                 if (!muxer_.video_ready() || !muxer_.audio_ready())
283                 {
284                         auto read_frame = consumer_->receive();
285
286                         if (read_frame == core::const_frame::empty() || read_frame.image_data().empty())
287                                 return core::draw_frame::late();
288
289                         auto video_frame = ffmpeg::create_frame();
290
291                         video_frame->data[0]                            = const_cast<uint8_t*>(read_frame.image_data().begin());
292                         video_frame->linesize[0]                        = static_cast<int>(read_frame.width()) * 4;
293                         video_frame->format                             = AVPixelFormat::AV_PIX_FMT_BGRA;
294                         video_frame->width                              = static_cast<int>(read_frame.width());
295                         video_frame->height                             = static_cast<int>(read_frame.height());
296                         video_frame->interlaced_frame   = consumer_->get_video_format_desc().field_mode != core::field_mode::progressive;
297                         video_frame->top_field_first            = consumer_->get_video_format_desc().field_mode == core::field_mode::upper ? 1 : 0;
298                         video_frame->key_frame                  = 1;
299
300                         muxer_.push(video_frame);
301                         muxer_.push(
302                                         {
303                                                 std::make_shared<core::mutable_audio_buffer>(
304                                                                 read_frame.audio_data().begin(),
305                                                                 read_frame.audio_data().end())
306                                         });
307                 }
308
309                 auto frame = muxer_.poll();
310
311                 if (frame == core::draw_frame::empty())
312                         return core::draw_frame::late();
313
314                 return frame;
315         }
316
317         std::wstring name() const override
318         {
319                 return L"channel-producer";
320         }
321
322         std::wstring print() const override
323         {
324                 return L"channel-producer[]";
325         }
326
327         core::constraints& pixel_constraints() override
328         {
329                 return pixel_constraints_;
330         }
331
332         boost::property_tree::wptree info() const override
333         {
334                 boost::property_tree::wptree info;
335                 info.add(L"type", L"channel-producer");
336                 return info;
337         }
338
339         core::monitor::subject& monitor_output() override
340         {
341                 return monitor_subject_;
342         }
343
344         boost::rational<int> current_framerate() const
345         {
346                 return muxer_.out_framerate();
347         }
348 };
349
350 spl::shared_ptr<core::frame_producer> create_channel_producer(
351                 const core::frame_producer_dependencies& dependencies,
352                 const spl::shared_ptr<core::video_channel>& channel,
353                 int frames_delay,
354                 bool no_auto_deinterlace)
355 {
356         auto producer = spl::make_shared<channel_producer>(dependencies, channel, frames_delay, no_auto_deinterlace);
357
358         return core::create_framerate_producer(
359                         producer,
360                         [producer] { return producer->current_framerate(); },
361                         dependencies.format_desc.framerate,
362                         dependencies.format_desc.field_mode,
363                         dependencies.format_desc.audio_cadence);
364 }
365
366 }}