/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
*\r
-* This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\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
+* 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
+* 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
+* Author: Robert Nagy, ronag89@gmail.com\r
*/\r
+\r
#include "../../stdafx.h"\r
\r
#include "video_decoder.h"\r
-#include "../util.h"\r
-#include "../filter/filter.h"\r
\r
-#include "../../ffmpeg_error.h"\r
-#include "../../tbb_avcodec.h"\r
+#include "../util/util.h"\r
\r
-#include <common/memory/memcpy.h>\r
+#include "../../ffmpeg_error.h"\r
\r
-#include <core/video_format.h>\r
-#include <core/producer/frame/basic_frame.h>\r
-#include <core/mixer/write_frame.h>\r
-#include <core/producer/frame/image_transform.h>\r
-#include <core/producer/frame/frame_factory.h>\r
-#include <core/producer/color/color_producer.h>\r
+#include <common/log.h>\r
+#include <core/frame/frame_transform.h>\r
+#include <core/frame/frame_factory.h>\r
\r
-#include <tbb/task_group.h>\r
+#include <boost/range/algorithm_ext/push_back.hpp>\r
+#include <boost/filesystem.hpp>\r
\r
-#include <boost/range/algorithm_ext.hpp>\r
+#include <queue>\r
\r
#if defined(_MSC_VER)\r
#pragma warning (push)\r
#endif\r
extern "C" \r
{\r
- #define __STDC_CONSTANT_MACROS\r
- #define __STDC_LIMIT_MACROS\r
- #include <libavformat/avformat.h>\r
#include <libavcodec/avcodec.h>\r
+ #include <libavformat/avformat.h>\r
}\r
#if defined(_MSC_VER)\r
#pragma warning (pop)\r
#endif\r
\r
-namespace caspar {\r
- \r
-struct video_decoder::implementation : boost::noncopyable\r
+namespace caspar { namespace ffmpeg {\r
+ \r
+struct video_decoder::impl : boost::noncopyable\r
{\r
- const safe_ptr<core::frame_factory> frame_factory_;\r
- std::shared_ptr<AVCodecContext> codec_context_;\r
+ monitor::basic_subject event_subject_;\r
int index_;\r
- core::video_mode::type mode_;\r
+ const std::shared_ptr<AVCodecContext> codec_context_;\r
+\r
+ std::queue<spl::shared_ptr<AVPacket>> packets_;\r
+ \r
+ const uint32_t nb_frames_;\r
\r
- std::queue<std::shared_ptr<AVPacket>> packet_buffer_;\r
+ const int width_;\r
+ const int height_;\r
+ bool is_progressive_;\r
\r
- std::unique_ptr<filter> filter_;\r
- tbb::task_group filter_tasks_;\r
+ tbb::atomic<uint32_t> file_frame_number_;\r
\r
- double fps_;\r
public:\r
- explicit implementation(const std::shared_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) \r
- : frame_factory_(frame_factory)\r
- , mode_(core::video_mode::invalid)\r
- , filter_(filter.empty() ? nullptr : new caspar::filter(filter))\r
- , fps_(frame_factory_->get_video_format_desc().fps)\r
+ explicit impl() \r
+ : nb_frames_(0)\r
+ , width_(0)\r
+ , height_(0)\r
+ , is_progressive_(true)\r
{\r
- AVCodec* dec;\r
- index_ = av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);\r
-\r
- if(index_ < 0)\r
- return;\r
- \r
- int errn = tbb_avcodec_open(context->streams[index_]->codec, dec);\r
- if(errn < 0)\r
- return;\r
- \r
- codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\r
-\r
- // Some files give an invalid time_base numerator, try to fix it.\r
- if(codec_context_ && codec_context_->time_base.num == 1)\r
- codec_context_->time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(codec_context_->time_base.den)))-1)); \r
-\r
- fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);\r
- if(double_rate(filter))\r
- fps_ *= 2;\r
+ file_frame_number_ = 0;\r
}\r
\r
- ~implementation()\r
+ explicit impl(const spl::shared_ptr<AVFormatContext>& context) \r
+ : codec_context_(open_codec(*context, AVMEDIA_TYPE_VIDEO, index_))\r
+ , nb_frames_(static_cast<uint32_t>(context->streams[index_]->nb_frames))\r
+ , width_(codec_context_->width)\r
+ , height_(codec_context_->height)\r
{\r
- filter_tasks_.cancel();\r
- filter_tasks_.wait();\r
+ file_frame_number_ = 0;\r
}\r
- \r
+\r
void push(const std::shared_ptr<AVPacket>& packet)\r
{\r
- if(!codec_context_)\r
+ if(!packet)\r
return;\r
\r
- if(packet && packet->stream_index != index_)\r
- return;\r
-\r
- packet_buffer_.push(packet);\r
+ if(packet->stream_index == index_ || packet->data == nullptr)\r
+ packets_.push(spl::make_shared_ptr(packet));\r
}\r
\r
- std::vector<safe_ptr<core::write_frame>> poll()\r
+ std::shared_ptr<AVFrame> poll()\r
{ \r
- std::vector<safe_ptr<core::write_frame>> result;\r
-\r
- if(!codec_context_)\r
- result.push_back(make_safe<core::write_frame>(reinterpret_cast<int>(this)));\r
- else if(!packet_buffer_.empty())\r
- {\r
- std::vector<std::shared_ptr<AVFrame>> av_frames;\r
-\r
- auto packet = std::move(packet_buffer_.front());\r
- packet_buffer_.pop();\r
+ if(packets_.empty())\r
+ return nullptr;\r
+ \r
+ auto packet = packets_.front();\r
\r
- if(!packet) // eof\r
- { \r
- if(codec_context_->codec->capabilities | CODEC_CAP_DELAY)\r
+ if(!codec_context_) \r
+ {\r
+ packets_.pop();\r
+ return packet->data == nullptr ? flush_video() : empty_video();\r
+ }\r
+ else\r
+ {\r
+ if(packet->data == nullptr)\r
+ { \r
+ if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
{\r
- // FIXME: This might cause bad performance.\r
- AVPacket pkt = {0};\r
- av_frames.push_back(decode_frame(pkt));\r
+ auto video = decode(*packet);\r
+ if(video)\r
+ return video;\r
}\r
-\r
+ \r
+ packets_.pop();\r
+ file_frame_number_ = static_cast<uint32_t>(packet->pos);\r
avcodec_flush_buffers(codec_context_.get());\r
+ return flush_video(); \r
}\r
- else\r
- {\r
- av_frames.push_back(decode_frame(*packet)); \r
- }\r
-\r
- if(filter_)\r
- {\r
- auto av_frames2 = std::move(av_frames);\r
-\r
- filter_tasks_.wait();\r
-\r
- boost::range::push_back(av_frames, filter_->poll());\r
-\r
- filter_tasks_.run([=]\r
- {\r
- BOOST_FOREACH(auto& frame, av_frames2) \r
- filter_->push(frame);\r
- });\r
- }\r
- \r
- BOOST_FOREACH(auto& frame, av_frames)\r
- result.push_back(make_write_frame(this, make_safe(frame), frame_factory_));\r
+ \r
+ packets_.pop();\r
+ return decode(*packet);\r
}\r
\r
- return result;\r
}\r
\r
- std::shared_ptr<AVFrame> decode_frame(AVPacket& packet)\r
+ std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
{\r
std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
\r
int frame_finished = 0;\r
- const int errn = avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &packet);\r
+ THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");\r
\r
- if(errn < 0)\r
- {\r
- BOOST_THROW_EXCEPTION(\r
- invalid_operation() <<\r
- msg_info(av_error_str(errn)) <<\r
- boost::errinfo_api_function("avcodec_decode_video") <<\r
- boost::errinfo_errno(AVUNERROR(errn)));\r
- }\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
- decoded_frame.reset();\r
+ return nullptr;\r
+\r
+ is_progressive_ = !decoded_frame->interlaced_frame;\r
+\r
+ if(decoded_frame->repeat_pict > 0)\r
+ CASPAR_LOG(warning) << "[video_decoder] Field repeat_pict not implemented.";\r
+ \r
+ event_subject_ << monitor::event("file/video/width") % width_\r
+ << monitor::event("file/video/height") % height_\r
+ << monitor::event("file/video/field") % u8(!decoded_frame->interlaced_frame ? "progressive" : (decoded_frame->top_field_first ? "upper" : "lower"))\r
+ << monitor::event("file/video/codec") % u8(codec_context_->codec->long_name);\r
+\r
+ ++file_frame_number_;\r
\r
return decoded_frame;\r
}\r
-\r
+ \r
bool ready() const\r
{\r
- return !codec_context_ || !packet_buffer_.empty();\r
+ return !codec_context_ || !packets_.empty();\r
}\r
- \r
- core::video_mode::type mode()\r
- {\r
- if(!codec_context_)\r
- return frame_factory_->get_video_format_desc().mode;\r
\r
- return mode_;\r
+ void clear()\r
+ {\r
+ while(!packets_.empty())\r
+ packets_.pop();\r
}\r
\r
- double fps() const\r
+ uint32_t nb_frames() const\r
{\r
- return fps_;\r
+ return std::max<uint32_t>(nb_frames_, file_frame_number_);\r
+ }\r
+\r
+ std::wstring print() const\r
+ { \r
+ return L"[video-decoder] " + u16(codec_context_->codec->long_name);\r
}\r
};\r
\r
-video_decoder::video_decoder(const std::shared_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) : impl_(new implementation(context, frame_factory, filter)){}\r
+video_decoder::video_decoder() : impl_(new impl()){}\r
+video_decoder::video_decoder(const spl::shared_ptr<AVFormatContext>& context) : impl_(new impl(context)){}\r
+video_decoder::video_decoder(video_decoder&& other) : impl_(std::move(other.impl_)){}\r
+video_decoder& video_decoder::operator=(video_decoder&& other){impl_ = std::move(other.impl_); return *this;}\r
void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
-std::vector<safe_ptr<core::write_frame>> video_decoder::poll(){return impl_->poll();}\r
+std::shared_ptr<AVFrame> video_decoder::poll(){return impl_->poll();}\r
bool video_decoder::ready() const{return impl_->ready();}\r
-core::video_mode::type video_decoder::mode(){return impl_->mode();}\r
-double video_decoder::fps() const{return impl_->fps();}\r
-}
\ No newline at end of file
+int video_decoder::width() const{return impl_->width_;}\r
+int video_decoder::height() const{return impl_->height_;}\r
+uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}\r
+uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}\r
+bool video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
+std::wstring video_decoder::print() const{return impl_->print();}\r
+void video_decoder::clear(){impl_->clear();}\r
+void video_decoder::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}\r
+void video_decoder::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}\r
+\r
+}}
\ No newline at end of file