-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\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
-*\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
-\r
-#include "../util/util.h"\r
-#include "../input/input.h"\r
-\r
-#include "../../ffmpeg_error.h"\r
-\r
-#include <common/log.h>\r
-#include <core/frame/frame_transform.h>\r
-#include <core/frame/frame_factory.h>\r
-\r
-#include <boost/range/algorithm_ext/push_back.hpp>\r
-#include <boost/filesystem.hpp>\r
-\r
-#include <queue>\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
-namespace caspar { namespace ffmpeg {\r
- \r
-struct video_decoder::impl : boost::noncopyable\r
-{\r
- monitor::basic_subject event_subject_;\r
- input* input_;\r
- int index_;\r
- const spl::shared_ptr<AVCodecContext> codec_context_;\r
-\r
- std::queue<spl::shared_ptr<AVPacket>> packets_;\r
- \r
- const AVStream* stream_;\r
- const uint32_t nb_frames_;\r
- const int width_;\r
- const int height_;\r
-\r
- bool is_progressive_;\r
- uint32_t file_frame_number_;\r
- double fps_;\r
- \r
- std::shared_ptr<AVPacket> current_packet_;\r
-\r
-public:\r
- explicit impl(input& in) \r
- : input_(&in)\r
- , codec_context_(open_codec(input_->context(), AVMEDIA_TYPE_VIDEO, index_))\r
- , stream_(input_->context().streams[index_])\r
- , nb_frames_(static_cast<uint32_t>(stream_->nb_frames))\r
- , width_(codec_context_->width)\r
- , height_(codec_context_->height)\r
- , file_frame_number_(0)\r
- , fps_(read_fps(input_->context(), 0.0))\r
- {\r
- }\r
- \r
- std::shared_ptr<AVFrame> poll()\r
- { \r
- if(!current_packet_ && !input_->try_pop_video(current_packet_))\r
- return nullptr;\r
- \r
- std::shared_ptr<AVFrame> frame;\r
-\r
- if(!current_packet_) \r
- {\r
- avcodec_flush_buffers(codec_context_.get()); \r
- }\r
- else if(!current_packet_->data)\r
- {\r
- if(codec_context_->codec->capabilities & CODEC_CAP_DELAY) \r
- frame = decode(*current_packet_);\r
- \r
- if(!frame)\r
- current_packet_.reset();\r
- }\r
- else\r
- {\r
- frame = decode(*current_packet_);\r
- \r
- if(current_packet_->size == 0)\r
- current_packet_.reset();\r
- }\r
- \r
- return frame ? frame : poll();\r
- }\r
-\r
- std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
- {\r
- auto frame = create_frame();\r
-\r
- int got_frame = 0;\r
- auto len = THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), frame.get(), &got_frame, &pkt), "[video_decocer]");\r
- \r
- if(len == 0)\r
- {\r
- pkt.size = 0;\r
- return nullptr;\r
- }\r
-\r
- pkt.data += len;\r
- pkt.size -= len;\r
-\r
- if(got_frame == 0) \r
- return nullptr;\r
- \r
- auto stream_time_base = stream_->time_base;\r
- auto packet_frame_number = static_cast<uint32_t>((static_cast<double>(pkt.pts * stream_time_base.num)/stream_time_base.den)*fps_);\r
-\r
- file_frame_number_ = packet_frame_number;\r
-\r
- is_progressive_ = !frame->interlaced_frame;\r
- \r
- if(frame->repeat_pict > 0)\r
- CASPAR_LOG(warning) << "[video_decoder] 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(!frame->interlaced_frame ? "progressive" : (frame->top_field_first ? "upper" : "lower"))\r
- << monitor::event("file/video/codec") % u8(codec_context_->codec->long_name);\r
- \r
- return frame;\r
- }\r
- \r
- uint32_t nb_frames() const\r
- {\r
- return std::max(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(input& in) : impl_(new impl(in)){}\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
-std::shared_ptr<AVFrame> video_decoder::operator()(){return impl_->poll();}\r
-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::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
+/*
+* 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 = std::shared_ptr<AVFrame>(av_frame_alloc(), [](AVFrame* frame)
+ {
+ av_frame_free(&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();}
+
+}}