+/*\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 <common/memory/safe_ptr.h>\r
+#include "../util.h"\r
+#include "../filter/filter.h"\r
\r
-#include <core/video_format.h>\r
+#include "../../ffmpeg_error.h"\r
+#include "../../tbb_avcodec.h"\r
\r
-#include <core/producer/frame/basic_frame.h>\r
#include <core/producer/frame/image_transform.h>\r
+#include <core/producer/frame/frame_factory.h>\r
\r
-#include <tbb/parallel_for.h>\r
+#include <boost/range/algorithm_ext/push_back.hpp>\r
+#include <boost/filesystem.hpp>\r
\r
-#include <algorithm>\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 <libswscale/swscale.h>\r
+ #include <libavcodec/avcodec.h>\r
#include <libavformat/avformat.h>\r
}\r
#if defined(_MSC_VER)\r
\r
namespace caspar {\r
\r
-core::pixel_format::type get_pixel_format(PixelFormat pix_fmt)\r
+struct video_decoder::implementation : boost::noncopyable\r
{\r
- switch(pix_fmt)\r
- {\r
- case PIX_FMT_BGRA: return core::pixel_format::bgra;\r
- case PIX_FMT_ARGB: return core::pixel_format::argb;\r
- case PIX_FMT_RGBA: return core::pixel_format::rgba;\r
- case PIX_FMT_ABGR: return core::pixel_format::abgr;\r
- case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;\r
- case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;\r
- case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;\r
- case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;\r
- case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;\r
- case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;\r
- default: return core::pixel_format::invalid;\r
- }\r
-}\r
+ const safe_ptr<core::frame_factory> frame_factory_;\r
+ std::shared_ptr<AVCodecContext> codec_context_;\r
+ int index_;\r
\r
-core::pixel_format_desc get_pixel_format_desc(PixelFormat pix_fmt, size_t width, size_t height)\r
-{\r
- // Get linesizes\r
- AVPicture dummy_pict; \r
- avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);\r
+ std::queue<std::shared_ptr<AVPacket>> packets_;\r
\r
- core::pixel_format_desc desc;\r
- desc.pix_fmt = get_pixel_format(pix_fmt);\r
- \r
- switch(desc.pix_fmt)\r
+ filter filter_;\r
+\r
+ double fps_;\r
+ int64_t nb_frames_;\r
+\r
+ size_t width_;\r
+ size_t height_;\r
+\r
+public:\r
+ explicit implementation(const safe_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) \r
+ : frame_factory_(frame_factory)\r
+ , filter_(filter)\r
+ , fps_(frame_factory_->get_video_format_desc().fps)\r
+ , nb_frames_(0)\r
+ , width_(0)\r
+ , height_(0)\r
{\r
- case core::pixel_format::bgra:\r
- case core::pixel_format::argb:\r
- case core::pixel_format::rgba:\r
- case core::pixel_format::abgr:\r
+ try\r
{\r
- desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4)); \r
- return desc;\r
+ AVCodec* dec;\r
+ index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0), "[video_decoder]");\r
+ \r
+ THROW_ON_ERROR2(tbb_avcodec_open(context->streams[index_]->codec, dec), "[video_decoder]");\r
+ \r
+ codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\r
+ \r
+ CASPAR_LOG(debug) << "[video_decoder] " << context->streams[index_]->codec->codec->long_name;\r
+\r
+ // Some files give an invalid time_base numerator, try to fix it.\r
+\r
+ fix_meta_data(*context);\r
+ \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
+\r
+ if(double_rate(filter))\r
+ fps_ *= 2;\r
+\r
+ width_ = codec_context_->width;\r
+ height_ = codec_context_->height;\r
+ }\r
+ catch(...)\r
+ {\r
+ index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0), "[video_decoder]");\r
+\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ CASPAR_LOG(warning) << "[video_decoder] Failed to open video-stream. Running without video."; \r
}\r
- case core::pixel_format::ycbcr:\r
- case core::pixel_format::ycbcra:\r
- { \r
- // Find chroma height\r
- size_t size2 = dummy_pict.data[2] - dummy_pict.data[1];\r
- size_t h2 = size2/dummy_pict.linesize[1]; \r
-\r
- desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));\r
- desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));\r
- desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));\r
-\r
- if(desc.pix_fmt == core::pixel_format::ycbcra) \r
- desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1)); \r
- return desc;\r
- } \r
- default: \r
- desc.pix_fmt = core::pixel_format::invalid;\r
- return desc;\r
}\r
-}\r
\r
-struct video_decoder::implementation : boost::noncopyable\r
-{ \r
- std::shared_ptr<core::frame_factory> frame_factory_;\r
- std::shared_ptr<SwsContext> sws_context_;\r
+ void push(const std::shared_ptr<AVPacket>& packet)\r
+ {\r
+ if(packet && packet->stream_index != index_)\r
+ return;\r
\r
- AVCodecContext* codec_context_;\r
+ packets_.push(packet);\r
+ }\r
\r
- const int width_;\r
- const int height_;\r
- const PixelFormat pix_fmt_;\r
- core::pixel_format_desc desc_;\r
+ std::vector<std::shared_ptr<AVFrame>> poll()\r
+ { \r
+ std::vector<std::shared_ptr<AVFrame>> result;\r
\r
-public:\r
- explicit implementation(AVCodecContext* codec_context, const safe_ptr<core::frame_factory>& frame_factory) \r
- : frame_factory_(frame_factory)\r
- , codec_context_(codec_context)\r
- , width_(codec_context_->width)\r
- , height_(codec_context_->height)\r
- , pix_fmt_(codec_context_->pix_fmt)\r
- , desc_(get_pixel_format_desc(pix_fmt_, width_, height_))\r
- {\r
- double frame_time = static_cast<double>(codec_context_->time_base.num) / static_cast<double>(codec_context_->time_base.den);\r
- double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;\r
- if(abs(frame_time - format_frame_time) > 0.0001)\r
- BOOST_THROW_EXCEPTION(file_read_error() << msg_info("Invalid video framerate.") << arg_value_info(boost::lexical_cast<std::string>(frame_time)));\r
+ if(packets_.empty())\r
+ return result;\r
+\r
+ if(!codec_context_)\r
+ return empty_poll();\r
\r
- if(desc_.pix_fmt == core::pixel_format::invalid)\r
+ auto packet = packets_.front();\r
+ \r
+ if(packet)\r
+ { \r
+ BOOST_FOREACH(auto& frame, decode(*packet))\r
+ boost::range::push_back(result, filter_.execute(frame));\r
+\r
+ if(packet->size == 0)\r
+ packets_.pop();\r
+ }\r
+ else\r
{\r
- CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";\r
-\r
- desc_ = get_pixel_format_desc(PIX_FMT_BGRA, width_, height_);\r
- double param;\r
- sws_context_.reset(sws_getContext(width_, height_, pix_fmt_, width_, height_, PIX_FMT_BGRA, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);\r
- if(!sws_context_)\r
- BOOST_THROW_EXCEPTION(operation_failed() <<\r
- msg_info("Could not create software scaling context.") << \r
- boost::errinfo_api_function("sws_getContext"));\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
+ BOOST_FOREACH(auto& frame, decode(pkt))\r
+ boost::range::push_back(result, filter_.execute(frame)); \r
+ }\r
+\r
+ if(result.empty())\r
+ { \r
+ packets_.pop();\r
+ avcodec_flush_buffers(codec_context_.get());\r
+ result.push_back(nullptr);\r
+ }\r
}\r
+ \r
+ return result;\r
}\r
- \r
- safe_ptr<core::write_frame> execute(const aligned_buffer& video_packet)\r
+\r
+ std::vector<std::shared_ptr<AVFrame>> empty_poll()\r
{ \r
- safe_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
+ auto packet = packets_.front();\r
+ packets_.pop();\r
+\r
+ if(!packet) \r
+ return boost::assign::list_of(nullptr);\r
+\r
+ std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);\r
+ frame->data[0] = nullptr;\r
+\r
+ return boost::assign::list_of(frame); \r
+ }\r
+\r
+ std::vector<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 result = avcodec_decode_video(codec_context_, decoded_frame.get(), &frame_finished, video_packet.data(), video_packet.size());\r
- \r
- if(result < 0)\r
- BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("avcodec_decode_video failed"));\r
+ THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");\r
\r
- auto write = frame_factory_->create_frame(desc_);\r
- if(sws_context_ == nullptr)\r
- {\r
- tbb::parallel_for(0, static_cast<int>(desc_.planes.size()), 1, [&](int n)\r
- {\r
- auto plane = desc_.planes[n];\r
- auto result = write->image_data(n).begin();\r
- auto decoded = decoded_frame->data[n];\r
- auto decoded_linesize = decoded_frame->linesize[n];\r
- \r
- tbb::parallel_for(0, static_cast<int>(desc_.planes[n].height), 1, [&](int y)\r
- {\r
- std::copy_n(decoded + y*decoded_linesize, plane.linesize, result + y*plane.linesize);\r
- });\r
- });\r
- }\r
- else\r
- {\r
- safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free); \r
- avcodec_get_frame_defaults(av_frame.get()); \r
- avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write->image_data().begin(), PIX_FMT_BGRA, width_, height_);\r
- \r
- sws_scale(sws_context_.get(), decoded_frame->data, decoded_frame->linesize, 0, height_, av_frame->data, av_frame->linesize); \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
+ pkt.data = nullptr;\r
+ pkt.size = 0;\r
\r
- if(codec_context_->codec_id == CODEC_ID_DVVIDEO && frame_factory_->get_video_format_desc().mode == core::video_mode::upper)\r
- write->get_image_transform().set_fill_translation(0.0f, 1.0/static_cast<double>(height_));\r
+ if(frame_finished == 0) \r
+ return std::vector<std::shared_ptr<AVFrame>>();\r
\r
- return write;\r
+ if(decoded_frame->repeat_pict % 2 > 0)\r
+ CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
+ \r
+ return std::vector<std::shared_ptr<AVFrame>>(1 + decoded_frame->repeat_pict/2, decoded_frame);\r
+ }\r
+ \r
+ bool ready() const\r
+ {\r
+ return !packets_.empty();\r
+ }\r
+ \r
+ double fps() const\r
+ {\r
+ return fps_;\r
}\r
};\r
\r
-video_decoder::video_decoder(AVCodecContext* codec_context, const safe_ptr<core::frame_factory>& frame_factory) : impl_(new implementation(codec_context, frame_factory)){}\r
-safe_ptr<core::write_frame> video_decoder::execute(const aligned_buffer& video_packet){return impl_->execute(video_packet);}\r
-\r
+video_decoder::video_decoder(const safe_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) : impl_(new implementation(context, frame_factory, filter)){}\r
+void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
+std::vector<std::shared_ptr<AVFrame>> video_decoder::poll(){return impl_->poll();}\r
+bool video_decoder::ready() const{return impl_->ready();}\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
}
\ No newline at end of file