]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/input/input.cpp
[ffmpeg] Removed FFMPEG_Resource to simplify code and documented streaming video...
[casparcg] / modules / ffmpeg / producer / input / input.cpp
index 1622c6cc0d08afd174b1d5b02b42a4baa07cf745..9103c7b261e8370ad14bfb60aa53118215d269de 100644 (file)
-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-*  This file is part of CasparCG.\r
-*\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
-*\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-*/\r
-#if defined(_MSC_VER)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-\r
-#include "../../stdafx.h"\r
-\r
-#include "input.h"\r
-\r
-#include "../util/util.h"\r
-#include "../../ffmpeg_error.h"\r
-\r
-#include <core/video_format.h>\r
-\r
-#include <common/diagnostics/graph.h>\r
-#include <common/exception/exceptions.h>\r
-#include <common/exception/win32_exception.h>\r
-\r
-#include <tbb/concurrent_queue.h>\r
-#include <tbb/atomic.h>\r
-#include <tbb/recursive_mutex.h>\r
-\r
-#include <boost/range/algorithm.hpp>\r
-#include <boost/thread/condition_variable.hpp>\r
-#include <boost/thread/mutex.hpp>\r
-#include <boost/thread/thread.hpp>\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (push)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-extern "C" \r
-{\r
-       #define __STDC_CONSTANT_MACROS\r
-       #define __STDC_LIMIT_MACROS\r
-       #include <libavformat/avformat.h>\r
-}\r
-#if defined(_MSC_VER)\r
-#pragma warning (pop)\r
-#endif\r
-\r
-namespace caspar { namespace ffmpeg {\r
-\r
-static const size_t MAX_BUFFER_COUNT = 100;\r
-static const size_t MIN_BUFFER_COUNT = 4;\r
-static const size_t MAX_BUFFER_SIZE  = 16 * 1000000;\r
-       \r
-struct input::implementation : boost::noncopyable\r
-{              \r
-       safe_ptr<diagnostics::graph>                                                            graph_;\r
-\r
-       const safe_ptr<AVFormatContext>                                                         format_context_; // Destroy this last\r
-       const int                                                                                                       default_stream_index_;\r
-                       \r
-       const std::wstring                                                                                      filename_;\r
-       tbb::atomic<bool>                                                                                       loop_;\r
-       const size_t                                                                                            start_;         \r
-       const size_t                                                                                            length_;\r
-       size_t                                                                                                          frame_number_;\r
-       \r
-       tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>>        buffer_;\r
-       tbb::atomic<size_t>                                                                                     buffer_size_;\r
-       boost::condition_variable                                                                       buffer_cond_;\r
-       boost::mutex                                                                                            buffer_mutex_;\r
-               \r
-       tbb::atomic<size_t>                                                                                     nb_frames_;\r
-       tbb::atomic<size_t>                                                                                     nb_loops_;\r
-\r
-       boost::thread                                                                                           thread_;\r
-       tbb::atomic<bool>                                                                                       is_running_;\r
-       tbb::atomic<bool>                                                                                       is_eof_;\r
-\r
-       tbb::recursive_mutex                                                                            mutex_;\r
-\r
-       explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, size_t start, size_t length) \r
-               : graph_(graph)\r
-               , format_context_(open_input(filename))         \r
-               , default_stream_index_(av_find_default_stream_index(format_context_.get()))\r
-               , filename_(filename)\r
-               , start_(start)\r
-               , length_(length)\r
-               , frame_number_(0)\r
-       {               \r
-               is_eof_                 = false;\r
-               loop_                   = loop;\r
-               buffer_size_    = 0;\r
-               nb_frames_              = 0;\r
-               nb_loops_               = 0;\r
-\r
-               buffer_size_    = 0;\r
-               nb_frames_              = 0;\r
-               nb_loops_               = 0;\r
-\r
-               if(start_ > 0)                  \r
-                       do_seek(start_);\r
-                                                               \r
-               graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));        \r
-               graph_->set_color("buffer-count", diagnostics::color(0.7f, 0.4f, 0.4f));\r
-               graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f)); \r
-               \r
-               is_running_ = true;\r
-               thread_ = boost::thread([this]{run();});\r
-\r
-               CASPAR_LOG(info) << print() << L" Initialized.";\r
-       }\r
-\r
-       ~implementation()\r
-       {\r
-               is_running_ = false;\r
-               buffer_cond_.notify_all();\r
-               thread_.join();\r
-       }\r
-               \r
-       bool try_pop(std::shared_ptr<AVPacket>& packet)\r
-       {\r
-               const bool result = buffer_.try_pop(packet);\r
-\r
-               if(result)\r
-               {\r
-                       if(packet)\r
-                               buffer_size_ -= packet->size;\r
-                       buffer_cond_.notify_all();\r
-               }\r
-\r
-               graph_->update_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);\r
-               graph_->update_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));\r
-\r
-               return result;\r
-       }\r
-                       \r
-       void run()\r
-       {               \r
-               caspar::win32_exception::install_handler();\r
-\r
-               try\r
-               {\r
-                       CASPAR_LOG(info) << print() << " Thread Started.";\r
-                       \r
-                       CASPAR_ASSERT(nb_frames_ < 1000);\r
-\r
-                       while(is_running_)\r
-                       {\r
-                               {\r
-                                       boost::unique_lock<boost::mutex> lock(buffer_mutex_);\r
-                                       while(full())\r
-                                               buffer_cond_.timed_wait(lock, boost::posix_time::millisec(20));\r
-                               }\r
-                               read_next_packet();                     \r
-                       }\r
-\r
-                       CASPAR_LOG(info) << print() << " Thread Stopped.";\r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       is_running_ = false;\r
-               }\r
-       }\r
-                       \r
-       void read_next_packet()\r
-       {               \r
-               tbb::recursive_mutex::scoped_lock lock(mutex_);\r
-\r
-               auto packet = create_packet();\r
-               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.   \r
-               \r
-               if(is_eof(ret))                                                                                                              \r
-               {\r
-                       ++nb_loops_;\r
-                       frame_number_   = 0;\r
-                       is_eof_                 = true;\r
-\r
-                       if(loop_)\r
-                       {\r
-                               do_seek(start_);\r
-                               graph_->add_tag("seek");                \r
-                               CASPAR_LOG(debug) << print() << " Looping.";                    \r
-                       }                                       \r
-               }\r
-               else\r
-               {               \r
-                       THROW_ON_ERROR(ret, "av_read_frame", print());\r
-\r
-                       if(packet->stream_index == default_stream_index_)\r
-                       {\r
-                               if(nb_loops_ == 0)\r
-                                       ++nb_frames_;\r
-                               ++frame_number_;\r
-                       }\r
-\r
-                       THROW_ON_ERROR2(av_dup_packet(packet.get()), print());\r
-                               \r
-                       // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.\r
-                       auto size = packet->size;\r
-                       auto data = packet->data;\r
-                       \r
-                       packet = safe_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)\r
-                       {\r
-                               packet->size = size;\r
-                               packet->data = data;\r
-                       });\r
-\r
-                       buffer_.try_push(packet);\r
-                       buffer_size_ += packet->size;\r
-                               \r
-                       graph_->update_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);\r
-                       graph_->update_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));\r
-               }                       \r
-       }\r
-\r
-       bool full() const\r
-       {\r
-               return is_running_ && (is_eof_ || (buffer_size_ > MAX_BUFFER_SIZE || buffer_.size() > MAX_BUFFER_COUNT) && buffer_.size() > MIN_BUFFER_COUNT);\r
-       }\r
-       \r
-       void do_seek(const int64_t target)\r
-       {       \r
-               CASPAR_LOG(debug) << print() << " Seeking: " << target;\r
-\r
-               int flags = AVSEEK_FLAG_FRAME;\r
-               if(target == 0)\r
-               {\r
-                       // Fix VP6 seeking\r
-                       int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
-                       if(vid_stream_index >= 0)\r
-                       {\r
-                               auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;\r
-                               if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)\r
-                                       flags = AVSEEK_FLAG_BYTE;\r
-                       }\r
-               }\r
-               \r
-               auto time_base = format_context_->streams[default_stream_index_]->time_base;\r
-               auto fixed_target = (target*time_base.den)/time_base.num;\r
-               auto fixed_time_base = fix_time_base(time_base);\r
-               fixed_target = (target * fixed_time_base.num) / fixed_time_base.den;\r
-\r
-               THROW_ON_ERROR2(avformat_seek_file(format_context_.get(), default_stream_index_, std::numeric_limits<int64_t>::min(), fixed_target, std::numeric_limits<int64_t>::max(), 0), print());          \r
-\r
-               is_eof_ = false;\r
-               buffer_cond_.notify_all();\r
-\r
-               auto flush_packet       = create_packet();\r
-               flush_packet->data      = nullptr;\r
-               flush_packet->size      = 0;\r
-               flush_packet->pos       = target;\r
-\r
-               buffer_.push(flush_packet);\r
-       }       \r
-\r
-       void seek(int64_t target)\r
-       {\r
-               tbb::recursive_mutex::scoped_lock lock(mutex_);\r
-\r
-               std::shared_ptr<AVPacket> packet;\r
-               while(try_pop(packet))\r
-               {\r
-               }\r
-\r
-               do_seek(target);\r
-       }\r
-\r
-       bool is_eof(int ret)\r
-       {\r
-               if(ret == AVERROR(EIO))\r
-                       CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. " << nb_frames_;\r
-               if(ret == AVERROR_EOF)\r
-                       CASPAR_LOG(debug) << print() << " Received EOF. " << nb_frames_;\r
-\r
-               return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;\r
-       }\r
-       \r
-       std::wstring print() const\r
-       {\r
-               return L"ffmpeg_input[" + filename_ + L")]";\r
-       }\r
-};\r
-\r
-input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, size_t start, size_t length) \r
-       : impl_(new implementation(graph, filename, loop, start, length)){}\r
-bool input::eof() const {return impl_->is_eof_;}\r
-bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}\r
-safe_ptr<AVFormatContext> input::context(){return impl_->format_context_;}\r
-size_t input::nb_frames() const {return impl_->nb_frames_;}\r
-size_t input::nb_loops() const {return impl_->nb_loops_;}\r
-void input::loop(bool value){impl_->loop_ = value;}\r
-bool input::loop() const{return impl_->loop_;}\r
-void input::seek(int64_t target){impl_->seek(target);}\r
-}}\r
+/*
+* Copyright 2013 Sveriges Television AB http://casparcg.com/
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../stdafx.h"
+
+#include "input.h"
+
+#include "../util/util.h"
+#include "../util/flv.h"
+#include "../../ffmpeg_error.h"
+#include "../../ffmpeg.h"
+
+#include <core/video_format.h>
+
+#include <common/diagnostics/graph.h>
+#include <common/executor.h>
+#include <common/except.h>
+#include <common/os/general_protection_fault.h>
+#include <common/param.h>
+#include <common/scope_exit.h>
+
+#include <tbb/concurrent_queue.h>
+#include <tbb/atomic.h>
+#include <tbb/recursive_mutex.h>
+
+#include <boost/rational.hpp>
+#include <boost/range/algorithm.hpp>
+#include <boost/thread/condition_variable.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+
+#if defined(_MSC_VER)
+#pragma warning (push)
+#pragma warning (disable : 4244)
+#endif
+extern "C"
+{
+       #define __STDC_CONSTANT_MACROS
+       #define __STDC_LIMIT_MACROS
+       #include <libavformat/avformat.h>
+}
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+
+static const size_t MAX_BUFFER_COUNT    = 100;
+static const size_t MAX_BUFFER_COUNT_RT = 3;
+static const size_t MIN_BUFFER_COUNT    = 50;
+static const size_t MAX_BUFFER_SIZE     = 64 * 1000000;
+
+namespace caspar { namespace ffmpeg {
+struct input::implementation : boost::noncopyable
+{
+       const spl::shared_ptr<diagnostics::graph>                                       graph_;
+
+       const spl::shared_ptr<AVFormatContext>                                          format_context_; // Destroy this last
+       const int                                                                                                       default_stream_index_   = av_find_default_stream_index(format_context_.get());
+
+       const std::wstring                                                                                      filename_;
+       tbb::atomic<uint32_t>                                                                           start_;
+       tbb::atomic<uint32_t>                                                                           length_;
+       const bool                                                                                                      thumbnail_mode_;
+       tbb::atomic<bool>                                                                                       loop_;
+       uint32_t                                                                                                        frame_number_                   = 0;
+       boost::rational<int>                                                                            framerate_                              = read_framerate(*format_context_, 1);
+
+       tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>>        buffer_;
+       tbb::atomic<size_t>                                                                                     buffer_size_;
+
+       executor                                                                                                        executor_;
+
+       explicit implementation(const spl::shared_ptr<diagnostics::graph> graph, const std::wstring& url_or_file, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_options& vid_params)
+               : graph_(graph)
+               , format_context_(open_input(url_or_file, vid_params))
+               , filename_(url_or_file)
+               , thumbnail_mode_(thumbnail_mode)
+               , executor_(print())
+       {
+               if (thumbnail_mode_)
+                       executor_.invoke([]
+                       {
+                               enable_quiet_logging_for_thread();
+                       });
+
+               start_                  = start;
+               length_                 = length;
+               loop_                   = loop;
+               buffer_size_    = 0;
+
+               if(start_ > 0)
+                       queued_seek(start_);
+
+               graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));
+               graph_->set_color("buffer-count", diagnostics::color(0.7f, 0.4f, 0.4f));
+               graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f));
+
+               tick();
+       }
+
+       bool try_pop(std::shared_ptr<AVPacket>& packet)
+       {
+               auto result = buffer_.try_pop(packet);
+
+               if(result)
+               {
+                       if(packet)
+                               buffer_size_ -= packet->size;
+                       tick();
+               }
+
+               graph_->set_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);
+               graph_->set_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));
+
+               return result;
+       }
+
+       std::ptrdiff_t get_max_buffer_count() const
+       {
+               return thumbnail_mode_ ? 1 : MAX_BUFFER_COUNT;
+       }
+
+       std::ptrdiff_t get_min_buffer_count() const
+       {
+               return thumbnail_mode_ ? 0 : MIN_BUFFER_COUNT;
+       }
+
+       std::future<bool> seek(uint32_t target)
+       {
+               if (!executor_.is_running())
+                       return make_ready_future(false);
+
+               return executor_.begin_invoke([=]() -> bool
+               {
+                       std::shared_ptr<AVPacket> packet;
+                       while(buffer_.try_pop(packet) && packet)
+                               buffer_size_ -= packet->size;
+
+                       queued_seek(target);
+
+                       tick();
+
+                       return true;
+               }, task_priority::high_priority);
+       }
+
+       std::wstring print() const
+       {
+               return L"ffmpeg_input[" + filename_ + L")]";
+       }
+
+       bool full() const
+       {
+               return (buffer_size_ > MAX_BUFFER_SIZE || buffer_.size() > get_max_buffer_count()) && buffer_.size() > get_min_buffer_count();
+       }
+
+       void tick()
+       {
+               if(!executor_.is_running())
+                       return;
+
+               executor_.begin_invoke([this]
+               {
+                       if(full())
+                               return;
+
+                       try
+                       {
+                               auto packet = create_packet();
+
+                               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.
+
+                               if(is_eof(ret))
+                               {
+                                       frame_number_   = 0;
+
+                                       if(loop_)
+                                       {
+                                               queued_seek(start_);
+                                               graph_->set_tag(diagnostics::tag_severity::INFO, "seek");
+                                               CASPAR_LOG(trace) << print() << " Looping.";
+                                       }
+                                       else
+                                               executor_.stop();
+                               }
+                               else
+                               {
+                                       THROW_ON_ERROR(ret, "av_read_frame", print());
+
+                                       if(packet->stream_index == default_stream_index_)
+                                               ++frame_number_;
+
+                                       THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
+
+                                       // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
+                                       auto size = packet->size;
+                                       auto data = packet->data;
+
+                                       packet = spl::shared_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)
+                                       {
+                                               packet->size = size;
+                                               packet->data = data;
+                                       });
+
+                                       buffer_.try_push(packet);
+                                       buffer_size_ += packet->size;
+
+                                       graph_->set_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);
+                                       graph_->set_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));
+                               }
+
+                               tick();
+                       }
+                       catch(...)
+                       {
+                               if (!thumbnail_mode_)
+                                       CASPAR_LOG_CURRENT_EXCEPTION();
+                               executor_.stop();
+                       }
+               });
+       }
+
+       spl::shared_ptr<AVFormatContext> open_input(const std::wstring& url_or_file, const ffmpeg_options& vid_params)
+       {
+               AVDictionary* format_options = nullptr;
+
+               CASPAR_SCOPE_EXIT
+               {
+                       if (format_options)
+                               av_dict_free(&format_options);
+               };
+
+               for (auto& option : vid_params)
+                       av_dict_set(&format_options, option.first.c_str(), option.second.c_str(), 0);
+
+               auto resource_name                      = std::wstring();
+               auto parts                                      = caspar::protocol_split(url_or_file);
+               AVInputFormat* input_format     = nullptr;
+
+               if (parts.at(0).empty())
+                       resource_name = parts.at(1);
+               else if (parts.at(0) == L"dshow")
+               {
+                       input_format = av_find_input_format("dshow");
+                       resource_name = parts.at(1);
+               }
+               else
+                       resource_name = parts.at(0) + L"://" + parts.at(1);
+
+               AVFormatContext* weak_context = nullptr;
+               THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(resource_name).c_str(), input_format, &format_options), resource_name);
+
+               spl::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* ptr)
+               {
+                       avformat_close_input(&ptr);
+               });
+
+               if (format_options)
+               {
+                       std::string unsupported_tokens = "";
+                       AVDictionaryEntry *t = NULL;
+                       while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)
+                       {
+                               if (!unsupported_tokens.empty())
+                                       unsupported_tokens += ", ";
+                               unsupported_tokens += t->key;
+                       }
+                       CASPAR_THROW_EXCEPTION(user_error() << msg_info(unsupported_tokens));
+               }
+
+               THROW_ON_ERROR2(avformat_find_stream_info(context.get(), nullptr), resource_name);
+               fix_meta_data(*context);
+               return context;
+       }
+
+       void fix_meta_data(AVFormatContext& context)
+       {
+               auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
+
+               if (video_index > -1)
+               {
+                       auto video_stream = context.streams[video_index];
+                       auto video_context = context.streams[video_index]->codec;
+
+                       if (boost::filesystem::path(context.filename).extension().string() == ".flv")
+                       {
+                               try
+                               {
+                                       auto meta = read_flv_meta_info(context.filename);
+                                       double fps = boost::lexical_cast<double>(meta["framerate"]);
+                                       video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);
+                               }
+                               catch (...) {}
+                       }
+                       else
+                       {
+                               auto stream_time = video_stream->time_base;
+                               auto duration = video_stream->duration;
+                               auto codec_time = video_context->time_base;
+                               auto ticks = video_context->ticks_per_frame;
+
+                               if (video_stream->nb_frames == 0)
+                                       video_stream->nb_frames = (duration*stream_time.num*codec_time.den) / (stream_time.den*codec_time.num*ticks);
+                       }
+               }
+       }
+
+       void queued_seek(const uint32_t target)
+       {
+               if (!thumbnail_mode_)
+                       CASPAR_LOG(debug) << print() << " Seeking: " << target;
+
+               int flags = AVSEEK_FLAG_FRAME;
+               if(target == 0)
+               {
+                       // Fix VP6 seeking
+                       int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
+                       if(vid_stream_index >= 0)
+                       {
+                               auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;
+                               if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)
+                                       flags = AVSEEK_FLAG_BYTE;
+                       }
+               }
+
+               auto stream = format_context_->streams[default_stream_index_];
+
+
+               auto fps = read_fps(*format_context_, 0.0);
+
+               THROW_ON_ERROR2(avformat_seek_file(
+                       format_context_.get(),
+                       default_stream_index_,
+                       std::numeric_limits<int64_t>::min(),
+                       static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num),
+                       std::numeric_limits<int64_t>::max(),
+                       0), print());
+
+               auto flush_packet       = create_packet();
+               flush_packet->data      = nullptr;
+               flush_packet->size      = 0;
+               flush_packet->pos       = target;
+
+               buffer_.push(flush_packet);
+       }
+
+       bool is_eof(int ret)
+       {
+               if(ret == AVERROR(EIO))
+                       CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. ";
+               if(ret == AVERROR_EOF)
+                       CASPAR_LOG(trace) << print() << " Received EOF. ";
+
+               return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
+       }
+
+       int num_audio_streams() const
+       {
+               return 0; // TODO
+       }
+
+       boost::rational<int> framerate() const
+       {
+               return framerate_;
+       }
+};
+
+input::input(const spl::shared_ptr<diagnostics::graph>& graph, const std::wstring& url_or_file, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_options& vid_params)
+       : impl_(new implementation(graph, url_or_file, loop, start, length, thumbnail_mode, vid_params)){}
+bool input::eof() const {return !impl_->executor_.is_running();}
+bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}
+spl::shared_ptr<AVFormatContext> input::context(){return impl_->format_context_;}
+void input::start(uint32_t value){impl_->start_ = value;}
+uint32_t input::start() const{return impl_->start_;}
+void input::length(uint32_t value){impl_->length_ = value;}
+uint32_t input::length() const{return impl_->length_;}
+void input::loop(bool value){impl_->loop_ = value;}
+bool input::loop() const{return impl_->loop_;}
+int input::num_audio_streams() const { return impl_->num_audio_streams(); }
+boost::rational<int> input::framerate() const { return impl_->framerate(); }
+std::future<bool> input::seek(uint32_t target){return impl_->seek(target);}
+}}