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"
26 #include "../util/util.h"
27 #include "../../ffmpeg_error.h"
28 #include "../../ffmpeg.h"
30 #include <common/diagnostics/graph.h>
31 #include <common/executor.h>
32 #include <common/lock.h>
33 //#include <common/except.h>
34 #include <common/os/general_protection_fault.h>
35 #include <common/log.h>
37 #include <core/video_format.h>
39 #include <tbb/concurrent_queue.h>
40 #include <tbb/atomic.h>
41 #include <tbb/recursive_mutex.h>
43 #include <boost/thread/condition_variable.hpp>
44 #include <boost/thread/mutex.hpp>
45 #include <boost/thread/thread.hpp>
48 #pragma warning (push)
49 #pragma warning (disable : 4244)
53 #define __STDC_CONSTANT_MACROS
54 #define __STDC_LIMIT_MACROS
55 #include <libavformat/avformat.h>
61 namespace caspar { namespace ffmpeg {
63 static const int MAX_PUSH_WITHOUT_POP = 200;
64 static const int MIN_FRAMES = 25;
68 stream(const stream&);
69 stream& operator=(const stream&);
71 typedef tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>>::size_type size_type;
74 tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> packets_;
75 tbb::atomic<int> push_since_pop_;
84 stream(stream&&) = default;
86 bool is_available() const
96 void push(const std::shared_ptr<AVPacket>& packet)
98 if(packet && packet->data && packet->stream_index != index_)
101 if (++push_since_pop_ > MAX_PUSH_WITHOUT_POP) // Out of memory protection for streams never being used.
106 packets_.push(packet);
109 bool try_pop(std::shared_ptr<AVPacket>& packet)
113 return packets_.try_pop(packet);
118 std::shared_ptr<AVPacket> packet;
120 while(packets_.try_pop(packet));
123 size_type size() const
125 return is_available() ? packets_.size() : std::numeric_limits<size_type>::max();
129 struct input::impl : boost::noncopyable
131 const spl::shared_ptr<diagnostics::graph> graph_;
133 const std::wstring filename_;
134 const spl::shared_ptr<AVFormatContext> format_context_ = open_input(filename_); // Destroy this last
135 const int default_stream_index_ = av_find_default_stream_index(format_context_.get());
137 tbb::atomic<uint32_t> start_;
138 tbb::atomic<uint32_t> length_;
139 tbb::atomic<bool> loop_;
140 tbb::atomic<bool> eof_;
141 double fps_ = read_fps(*format_context_, 0.0);
142 uint32_t frame_number_ = 0;
144 stream video_stream_ { av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0) };
145 std::vector<stream> audio_streams_;
147 boost::optional<uint32_t> seek_target_;
149 tbb::atomic<bool> is_running_;
151 boost::condition_variable cond_;
152 boost::thread thread_;
155 const spl::shared_ptr<diagnostics::graph> graph,
156 const std::wstring& filename,
158 const uint32_t start,
159 const uint32_t length,
162 , filename_(filename)
171 seek_target_ = start_;
173 graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));
176 for (unsigned i = 0; i < format_context_->nb_streams; ++i)
177 if (format_context_->streams[i]->codec->codec_type == AVMediaType::AVMEDIA_TYPE_AUDIO)
178 audio_streams_.emplace_back(i);
180 for (int i = 0; i < audio_streams_.size(); ++i)
181 graph_->set_color("audio-buffer" + boost::lexical_cast<std::string>(i + 1), diagnostics::color(0.7f, 0.4f, 0.4f));
183 if (video_stream_.is_available())
184 graph_->set_color("video-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));
186 for(int n = 0; n < 8; ++n)
189 thread_ = boost::thread([this, thumbnail_mode]{run(thumbnail_mode);});
199 bool try_pop_video(std::shared_ptr<AVPacket>& packet)
201 if (!video_stream_.is_available())
204 bool result = video_stream_.try_pop(packet);
209 graph_->set_value("video-buffer", std::min(1.0, static_cast<double>(video_stream_.size())/MIN_FRAMES));
214 bool try_pop_audio(std::shared_ptr<AVPacket>& packet, int audio_stream_index)
216 if (audio_streams_.size() < audio_stream_index + 1)
219 auto& audio_stream = audio_streams_.at(audio_stream_index);
220 bool result = audio_stream.try_pop(packet);
224 auto buffer_nr = boost::lexical_cast<std::string>(audio_stream_index + 1);
225 graph_->set_value("audio-buffer" + buffer_nr, std::min(1.0, static_cast<double>(audio_stream.size())/MIN_FRAMES));
230 void seek(uint32_t target)
233 boost::lock_guard<boost::mutex> lock(mutex_);
235 seek_target_ = target;
236 video_stream_.clear();
238 for (auto& audio_stream : audio_streams_)
239 audio_stream.clear();
245 int get_actual_audio_stream_index(int audio_stream_index) const
247 if (audio_stream_index + 1 > audio_streams_.size())
248 CASPAR_THROW_EXCEPTION(averror_stream_not_found());
250 return audio_streams_.at(audio_stream_index).index();
253 std::wstring print() const
255 return L"ffmpeg_input[" + filename_ + L")]";
259 void internal_seek(uint32_t target)
262 graph_->set_tag(diagnostics::tag_severity::INFO, "seek");
264 if (is_logging_quiet_for_thread())
265 CASPAR_LOG(trace) << print() << " Seeking: " << target;
267 CASPAR_LOG(debug) << print() << " Seeking: " << target;
269 int flags = AVSEEK_FLAG_FRAME;
273 int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
274 if(vid_stream_index >= 0)
276 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;
277 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)
278 flags = AVSEEK_FLAG_BYTE;
282 auto stream = format_context_->streams[default_stream_index_];
283 auto fps = read_fps(*format_context_, 0.0);
284 auto target_timestamp = static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num);
286 THROW_ON_ERROR2(avformat_seek_file(
287 format_context_.get(),
288 default_stream_index_,
289 std::numeric_limits<int64_t>::min(),
291 std::numeric_limits<int64_t>::max(),
294 auto flush_packet = create_packet();
295 flush_packet->data = nullptr;
296 flush_packet->size = 0;
297 flush_packet->pos = target;
299 video_stream_.push(flush_packet);
301 for (auto& audio_stream : audio_streams_)
302 audio_stream.push(flush_packet);
309 internal_seek(*seek_target_);
310 seek_target_.reset();
313 auto packet = create_packet();
315 auto ret = av_read_frame(format_context_.get(), packet.get()); // packet is only valid until next call of av_read_frame. Use av_dup_packet to extend its life.
320 internal_seek(start_);
328 THROW_ON_ERROR(ret, "av_read_frame", print());
330 THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
332 // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
333 const auto size = packet->size;
334 const auto data = packet->data;
336 packet = spl::shared_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)
342 const auto stream_time_base = format_context_->streams[packet->stream_index]->time_base;
343 const auto packet_frame_number = static_cast<uint32_t>((static_cast<double>(packet->pts * stream_time_base.num)/stream_time_base.den)*fps_);
345 if(packet->stream_index == default_stream_index_)
346 frame_number_ = packet_frame_number;
348 if(packet_frame_number >= start_ && packet_frame_number < length_)
350 video_stream_.push(packet);
352 for (auto& audio_stream : audio_streams_)
353 audio_stream.push(packet);
357 if (video_stream_.is_available())
358 graph_->set_value("video-buffer", std::min(1.0, static_cast<double>(video_stream_.size())/MIN_FRAMES));
360 for (int i = 0; i < audio_streams_.size(); ++i)
362 "audio-buffer" + boost::lexical_cast<std::string>(i + 1),
363 std::min(1.0, static_cast<double>(audio_streams_[i].size())/MIN_FRAMES));
368 bool video_full = video_stream_.size() >= MIN_FRAMES;
373 for (auto& audio_stream : audio_streams_)
374 if (audio_stream.size() < MIN_FRAMES)
380 void run(bool thumbnail_mode)
382 ensure_gpf_handler_installed_for_thread(u8(print()).c_str());
383 auto quiet_logging = temporary_enable_quiet_logging_for_thread(thumbnail_mode);
391 boost::unique_lock<boost::mutex> lock(mutex_);
393 while((eof_ || full()) && !seek_target_ && is_running_)
401 CASPAR_LOG_CURRENT_EXCEPTION();
409 #pragma warning (disable : 4146)
410 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
414 input::input(const spl::shared_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode)
415 : impl_(new impl(graph, filename, loop, start, length, thumbnail_mode)){}
416 int input::get_actual_audio_stream_index(int audio_stream_index) const { return impl_->get_actual_audio_stream_index(audio_stream_index); };
417 int input::num_audio_streams() const { return static_cast<int>(impl_->audio_streams_.size()); }
418 bool input::try_pop_video(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_video(packet);}
419 bool input::try_pop_audio(std::shared_ptr<AVPacket>& packet, int audio_stream_index){return impl_->try_pop_audio(packet, audio_stream_index);}
420 AVFormatContext& input::context(){return *impl_->format_context_;}
421 void input::loop(bool value){impl_->loop_ = value;}
422 bool input::loop() const{return impl_->loop_;}
423 void input::seek(uint32_t target){impl_->seek(target);}
424 void input::start(uint32_t value){impl_->start_ = value;}
425 uint32_t input::start() const{return impl_->start_;}
426 void input::length(uint32_t value){impl_->length_ = value;}
427 uint32_t input::length() const{return impl_->length_;}
428 bool input::eof() const { return impl_->eof_; }