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 bool thumbnail_mode_;
142 double fps_ = read_fps(*format_context_, 0.0);
143 uint32_t frame_number_ = 0;
145 stream video_stream_ { av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0) };
146 std::vector<stream> audio_streams_;
148 boost::optional<uint32_t> seek_target_;
150 tbb::atomic<bool> is_running_;
152 boost::condition_variable cond_;
153 boost::thread thread_;
156 const spl::shared_ptr<diagnostics::graph> graph,
157 const std::wstring& filename,
159 const uint32_t start,
160 const uint32_t length,
163 , filename_(filename)
164 , thumbnail_mode_(thumbnail_mode)
173 seek_target_ = start_;
175 graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));
177 if (!thumbnail_mode_)
178 for (unsigned i = 0; i < format_context_->nb_streams; ++i)
179 if (format_context_->streams[i]->codec->codec_type == AVMediaType::AVMEDIA_TYPE_AUDIO)
180 audio_streams_.emplace_back(i);
182 for (int i = 0; i < audio_streams_.size(); ++i)
183 graph_->set_color("audio-buffer" + boost::lexical_cast<std::string>(i + 1), diagnostics::color(0.7f, 0.4f, 0.4f));
185 if (video_stream_.is_available())
186 graph_->set_color("video-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));
188 for(int n = 0; n < 8; ++n)
192 thread_ = boost::thread([this]{run();});
200 if (!thumbnail_mode_)
204 bool try_pop_video(std::shared_ptr<AVPacket>& packet)
206 if (!video_stream_.is_available())
212 while (!video_stream_.try_pop(packet))
215 if (++ticks > 32) // Infinite loop should not be possible
219 boost::this_thread::sleep_for(boost::chrono::milliseconds(5));
225 bool result = video_stream_.try_pop(packet);
230 graph_->set_value("video-buffer", std::min(1.0, static_cast<double>(video_stream_.size())/MIN_FRAMES));
235 bool try_pop_audio(std::shared_ptr<AVPacket>& packet, int audio_stream_index)
237 if (audio_streams_.size() < audio_stream_index + 1)
240 auto& audio_stream = audio_streams_.at(audio_stream_index);
241 bool result = audio_stream.try_pop(packet);
245 auto buffer_nr = boost::lexical_cast<std::string>(audio_stream_index + 1);
246 graph_->set_value("audio-buffer" + buffer_nr, std::min(1.0, static_cast<double>(audio_stream.size())/MIN_FRAMES));
251 void seek(uint32_t target)
254 boost::lock_guard<boost::mutex> lock(mutex_);
256 seek_target_ = target;
257 video_stream_.clear();
259 for (auto& audio_stream : audio_streams_)
260 audio_stream.clear();
266 int get_actual_audio_stream_index(int audio_stream_index) const
268 if (audio_stream_index + 1 > audio_streams_.size())
269 CASPAR_THROW_EXCEPTION(averror_stream_not_found());
271 return audio_streams_.at(audio_stream_index).index();
274 std::wstring print() const
276 return L"ffmpeg_input[" + filename_ + L")]";
280 void internal_seek(uint32_t target)
283 graph_->set_tag(diagnostics::tag_severity::INFO, "seek");
285 if (is_logging_quiet_for_thread())
286 CASPAR_LOG(trace) << print() << " Seeking: " << target;
288 CASPAR_LOG(debug) << print() << " Seeking: " << target;
290 int flags = AVSEEK_FLAG_FRAME;
294 int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
295 if(vid_stream_index >= 0)
297 auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;
298 if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)
299 flags = AVSEEK_FLAG_BYTE;
303 auto stream = format_context_->streams[default_stream_index_];
304 auto fps = read_fps(*format_context_, 0.0);
305 auto target_timestamp = static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num);
307 THROW_ON_ERROR2(avformat_seek_file(
308 format_context_.get(),
309 default_stream_index_,
310 std::numeric_limits<int64_t>::min(),
312 std::numeric_limits<int64_t>::max(),
315 video_stream_.push(nullptr);
317 for (auto& audio_stream : audio_streams_)
318 audio_stream.push(nullptr);
325 internal_seek(*seek_target_);
326 seek_target_.reset();
329 auto packet = create_packet();
331 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.
336 internal_seek(start_);
344 THROW_ON_ERROR(ret, "av_read_frame", print());
346 THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
348 // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
349 const auto size = packet->size;
350 const auto data = packet->data;
352 packet = spl::shared_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)
358 const auto stream_time_base = format_context_->streams[packet->stream_index]->time_base;
359 const auto packet_frame_number = static_cast<uint32_t>((static_cast<double>(packet->pts * stream_time_base.num)/stream_time_base.den)*fps_);
361 if(packet->stream_index == default_stream_index_)
362 frame_number_ = packet_frame_number;
364 if(packet_frame_number >= start_ && packet_frame_number < length_)
366 video_stream_.push(packet);
368 for (auto& audio_stream : audio_streams_)
369 audio_stream.push(packet);
373 if (video_stream_.is_available())
374 graph_->set_value("video-buffer", std::min(1.0, static_cast<double>(video_stream_.size())/MIN_FRAMES));
376 for (int i = 0; i < audio_streams_.size(); ++i)
378 "audio-buffer" + boost::lexical_cast<std::string>(i + 1),
379 std::min(1.0, static_cast<double>(audio_streams_[i].size())/MIN_FRAMES));
384 bool video_full = video_stream_.size() >= MIN_FRAMES;
389 for (auto& audio_stream : audio_streams_)
390 if (audio_stream.size() < MIN_FRAMES)
398 ensure_gpf_handler_installed_for_thread(u8(print()).c_str());
406 boost::unique_lock<boost::mutex> lock(mutex_);
408 while(full() && !seek_target_ && is_running_)
416 CASPAR_LOG_CURRENT_EXCEPTION();
424 #pragma warning (disable : 4146)
425 return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
429 input::input(const spl::shared_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode)
430 : impl_(new impl(graph, filename, loop, start, length, thumbnail_mode)){}
431 int input::get_actual_audio_stream_index(int audio_stream_index) const { return impl_->get_actual_audio_stream_index(audio_stream_index); };
432 int input::num_audio_streams() const { return static_cast<int>(impl_->audio_streams_.size()); }
433 bool input::try_pop_video(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_video(packet);}
434 bool input::try_pop_audio(std::shared_ptr<AVPacket>& packet, int audio_stream_index){return impl_->try_pop_audio(packet, audio_stream_index);}
435 AVFormatContext& input::context(){return *impl_->format_context_;}
436 void input::loop(bool value){impl_->loop_ = value;}
437 bool input::loop() const{return impl_->loop_;}
438 void input::seek(uint32_t target){impl_->seek(target);}
439 void input::start(uint32_t value){impl_->start_ = value;}
440 uint32_t input::start() const{return impl_->start_;}
441 void input::length(uint32_t value){impl_->length_ = value;}
442 uint32_t input::length() const{return impl_->length_;}
443 bool input::eof() const { return impl_->eof_; }