-/*\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
-#include "../../stdafx.h"\r
-\r
-#include "video_decoder.h"\r
-\r
-#include "../util.h"\r
-#include "../filter/filter.h"\r
-\r
-#include "../../ffmpeg_error.h"\r
-\r
-#include <core/producer/frame/basic_frame.h>\r
-#include <common/memory/memcpy.h>\r
-#include <common/concurrency/governor.h>\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (push)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-extern "C" \r
-{\r
- #include <libavcodec/avcodec.h>\r
- #include <libavformat/avformat.h>\r
-}\r
-#if defined(_MSC_VER)\r
-#pragma warning (pop)\r
-#endif\r
-\r
-#include <tbb/scalable_allocator.h>\r
-\r
-#undef Yield\r
-using namespace Concurrency;\r
-\r
-namespace caspar { namespace ffmpeg {\r
- \r
-struct video_decoder::implementation : public Concurrency::agent, boost::noncopyable\r
-{ \r
- int index_;\r
- std::shared_ptr<AVCodecContext> codec_context_;\r
- \r
- double fps_;\r
- int64_t nb_frames_;\r
-\r
- size_t width_;\r
- size_t height_;\r
- bool is_progressive_;\r
- \r
- unbounded_buffer<video_decoder::source_element_t> source_;\r
- ITarget<video_decoder::target_element_t>& target_;\r
-\r
- governor governor_;\r
- \r
-public:\r
- explicit implementation(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context) \r
- : codec_context_(open_codec(context, AVMEDIA_TYPE_VIDEO, index_))\r
- , fps_(static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num))\r
- , nb_frames_(context.streams[index_]->nb_frames)\r
- , width_(codec_context_->width)\r
- , height_(codec_context_->height)\r
- , is_progressive_(true)\r
- , source_([this](const video_decoder::source_element_t& packet){return packet->stream_index == index_;})\r
- , target_(target)\r
- , governor_(2)\r
- { \r
- CASPAR_LOG(debug) << "[video_decoder] " << context.streams[index_]->codec->codec->long_name;\r
- \r
- source.link_target(&source_);\r
- \r
- start();\r
- }\r
-\r
- ~implementation()\r
- {\r
- agent::wait(this);\r
- }\r
- \r
- std::shared_ptr<AVFrame> decode(AVPacket& packet)\r
- {\r
- std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
-\r
- int frame_finished = 0;\r
- THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &packet), "[video_decocer]");\r
-\r
- // 1 packet <=> 1 frame.\r
- // If a decoder consumes less then the whole packet then something is wrong\r
- // that might be just harmless padding at the end, or a problem with the\r
- // AVParser or demuxer which puted more then one frame in a AVPacket.\r
-\r
- if(frame_finished == 0) \r
- return nullptr;\r
- \r
- if(decoded_frame->repeat_pict > 0)\r
- CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
-\r
- return decoded_frame;\r
- }\r
-\r
- virtual void run()\r
- {\r
- win32_exception::install_handler();\r
-\r
- try\r
- {\r
- while(true)\r
- {\r
- auto ticket = governor_.acquire();\r
- auto packet = receive(source_);\r
- \r
- if(packet == loop_packet(index_))\r
- { \r
- if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
- {\r
- AVPacket pkt;\r
- av_init_packet(&pkt);\r
- pkt.data = nullptr;\r
- pkt.size = 0;\r
-\r
- for(auto decoded_frame = decode(pkt); decoded_frame; decoded_frame = decode(pkt))\r
- {\r
- auto frame = dup_frame(make_safe_ptr(decoded_frame)); \r
- send(target_, safe_ptr<AVFrame>(frame.get(), [frame, ticket](AVFrame*){}));\r
- Context::Yield();\r
- }\r
- }\r
-\r
- avcodec_flush_buffers(codec_context_.get());\r
- send(target_, loop_video());\r
- continue;\r
- }\r
-\r
- if(packet == eof_packet(index_))\r
- break;\r
-\r
- auto decoded_frame = decode(*packet);\r
- if(!decoded_frame)\r
- continue;\r
- \r
- is_progressive_ = decoded_frame->interlaced_frame == 0;\r
- \r
- // C-TODO: Avoid duplication.\r
- // Need to dupliace frame data since avcodec_decode_video2 reuses it.\r
- auto frame = dup_frame(make_safe_ptr(decoded_frame));\r
- send(target_, safe_ptr<AVFrame>(frame.get(), [frame, ticket](AVFrame*){})); \r
- Context::Yield();\r
- }\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- }\r
- \r
- send(target_, eof_video());\r
-\r
- done();\r
- }\r
-\r
- safe_ptr<AVFrame> dup_frame(const safe_ptr<AVFrame>& frame)\r
- {\r
- auto desc = get_pixel_format_desc(static_cast<PixelFormat>(frame->format), frame->width, frame->height);\r
-\r
- auto count = desc.planes.size();\r
- std::array<uint8_t*, 4> org_ptrs;\r
- std::array<safe_ptr<uint8_t>, 4> new_ptrs;\r
- parallel_for<size_t>(0, count, [&](size_t n)\r
- {\r
- CASPAR_ASSERT(frame->data[n]);\r
- auto size = frame->linesize[n]*desc.planes[n].height;\r
- new_ptrs[n] = fast_memdup(frame->data[n], size);\r
- org_ptrs[n] = frame->data[n];\r
- frame->data[n] = new_ptrs[n].get();\r
- });\r
-\r
- return safe_ptr<AVFrame>(frame.get(), [frame, org_ptrs, new_ptrs, count](AVFrame*)\r
- {\r
- for(size_t n = 0; n < count; ++n)\r
- frame->data[n] = org_ptrs[n];\r
- });\r
- }\r
- \r
- double fps() const\r
- {\r
- return fps_;\r
- }\r
-};\r
-\r
-video_decoder::video_decoder(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context) \r
- : impl_(new implementation(source, target, context))\r
-{\r
-}\r
-\r
-double video_decoder::fps() const{return impl_->fps();}\r
-int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
-size_t video_decoder::width() const{return impl_->width_;}\r
-size_t video_decoder::height() const{return impl_->height_;}\r
-bool video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
-\r
-}}
\ No newline at end of file
+/*
+* 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 "video_decoder.h"
+
+#include "../util/util.h"
+
+#include "../../ffmpeg_error.h"
+
+#include <core/frame/frame_transform.h>
+#include <core/frame/frame_factory.h>
+
+#include <boost/range/algorithm_ext/push_back.hpp>
+#include <boost/filesystem.hpp>
+
+#include <queue>
+
+#if defined(_MSC_VER)
+#pragma warning (push)
+#pragma warning (disable : 4244)
+#endif
+extern "C"
+{
+ #include <libavcodec/avcodec.h>
+ #include <libavformat/avformat.h>
+}
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+
+namespace caspar { namespace ffmpeg {
+
+struct video_decoder::implementation : boost::noncopyable
+{
+ int index_ = -1;
+ const spl::shared_ptr<AVCodecContext> codec_context_;
+
+ std::queue<spl::shared_ptr<AVPacket>> packets_;
+
+ const uint32_t nb_frames_;
+
+ const int width_ = codec_context_->width;
+ const int height_ = codec_context_->height;
+ bool is_progressive_;
+
+ tbb::atomic<uint32_t> file_frame_number_;
+
+public:
+ explicit implementation(const spl::shared_ptr<AVFormatContext>& context)
+ : codec_context_(open_codec(*context, AVMEDIA_TYPE_VIDEO, index_, false))
+ , nb_frames_(static_cast<uint32_t>(context->streams[index_]->nb_frames))
+ {
+ file_frame_number_ = 0;
+
+ codec_context_->refcounted_frames = 1;
+ }
+
+ void push(const std::shared_ptr<AVPacket>& packet)
+ {
+ if(!packet)
+ return;
+
+ if(packet->stream_index == index_ || packet->data == nullptr)
+ packets_.push(spl::make_shared_ptr(packet));
+ }
+
+ std::shared_ptr<AVFrame> poll()
+ {
+ if(packets_.empty())
+ return nullptr;
+
+ auto packet = packets_.front();
+
+ if(packet->data == nullptr)
+ {
+ if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
+ {
+ auto video = decode(packet);
+ if(video)
+ return video;
+ }
+
+ packets_.pop();
+ file_frame_number_ = static_cast<uint32_t>(packet->pos);
+ avcodec_flush_buffers(codec_context_.get());
+ return flush_video();
+ }
+
+ packets_.pop();
+ return decode(packet);
+ }
+
+ std::shared_ptr<AVFrame> decode(spl::shared_ptr<AVPacket> pkt)
+ {
+ auto decoded_frame = create_frame();
+
+ int frame_finished = 0;
+ THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, pkt.get()), "[video_decoder]");
+
+ // If a decoder consumes less then the whole packet then something is wrong
+ // that might be just harmless padding at the end, or a problem with the
+ // AVParser or demuxer which puted more then one frame in a AVPacket.
+
+ if(frame_finished == 0)
+ return nullptr;
+
+ is_progressive_ = !decoded_frame->interlaced_frame;
+
+ if(decoded_frame->repeat_pict > 0)
+ CASPAR_LOG(warning) << "[video_decoder] Field repeat_pict not implemented.";
+
+ ++file_frame_number_;
+
+ // This ties the life of the decoded_frame to the packet that it came from. For the
+ // current version of ffmpeg (0.8 or c17808c) the RAW_VIDEO codec returns frame data
+ // owned by the packet.
+ return std::shared_ptr<AVFrame>(decoded_frame.get(), [decoded_frame, pkt](AVFrame*){});
+ }
+
+ bool ready() const
+ {
+ return packets_.size() >= 8;
+ }
+
+ uint32_t nb_frames() const
+ {
+ return std::max(nb_frames_, static_cast<uint32_t>(file_frame_number_));
+ }
+
+ std::wstring print() const
+ {
+ return L"[video-decoder] " + u16(codec_context_->codec->long_name);
+ }
+};
+
+video_decoder::video_decoder(const spl::shared_ptr<AVFormatContext>& context) : impl_(new implementation(context)){}
+void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
+std::shared_ptr<AVFrame> video_decoder::poll(){return impl_->poll();}
+bool video_decoder::ready() const{return impl_->ready();}
+int video_decoder::width() const{return impl_->width_;}
+int video_decoder::height() const{return impl_->height_;}
+uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}
+uint32_t video_decoder::file_frame_number() const{return static_cast<uint32_t>(impl_->file_frame_number_);}
+bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
+std::wstring video_decoder::print() const{return impl_->print();}
+
+}}