2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
20 #include "../../stdafx.h"
\r
22 #include "video_decoder.h"
\r
24 #include "../util.h"
\r
25 #include "../filter/filter.h"
\r
27 #include "../../ffmpeg_error.h"
\r
28 #include "../../tbb_avcodec.h"
\r
30 #include <core/producer/frame/image_transform.h>
\r
31 #include <core/producer/frame/frame_factory.h>
\r
33 #include <boost/range/algorithm_ext/push_back.hpp>
\r
37 #if defined(_MSC_VER)
\r
38 #pragma warning (push)
\r
39 #pragma warning (disable : 4244)
\r
43 #include <libavcodec/avcodec.h>
\r
44 #include <libavformat/avformat.h>
\r
46 #if defined(_MSC_VER)
\r
47 #pragma warning (pop)
\r
52 struct video_decoder::implementation : boost::noncopyable
\r
54 const safe_ptr<core::frame_factory> frame_factory_;
\r
55 std::shared_ptr<AVCodecContext> codec_context_;
\r
58 std::queue<std::shared_ptr<AVPacket>> packets_;
\r
65 explicit implementation(const std::shared_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter)
\r
66 : frame_factory_(frame_factory)
\r
68 , fps_(frame_factory_->get_video_format_desc().fps)
\r
74 index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0), "[video_decoder]");
\r
76 THROW_ON_ERROR2(tbb_avcodec_open(context->streams[index_]->codec, dec), "[video_decoder]");
\r
78 codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);
\r
80 // Some files give an invalid time_base numerator, try to fix it.
\r
81 if(codec_context_ && codec_context_->time_base.num == 1)
\r
82 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
84 nb_frames_ = context->streams[index_]->nb_frames;
\r
86 nb_frames_ = context->streams[index_]->duration;// * context->streams[index_]->time_base.den;
\r
88 fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);
\r
89 if(double_rate(filter))
\r
94 index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0), "[video_decoder]");
\r
96 CASPAR_LOG_CURRENT_EXCEPTION();
\r
97 CASPAR_LOG(warning) << "[video_decoder] Failed to open video-stream. Running without video.";
\r
101 void push(const std::shared_ptr<AVPacket>& packet)
\r
103 if(packet && packet->stream_index != index_)
\r
106 packets_.push(packet);
\r
109 std::vector<std::shared_ptr<AVFrame>> poll()
\r
111 if(!codec_context_)
\r
112 return empty_poll();
\r
114 std::vector<std::shared_ptr<AVFrame>> result;
\r
116 while(!packets_.empty() && result.empty())
\r
118 auto packet = packets_.front();
\r
122 auto frame = decode(*packet);
\r
123 boost::range::push_back(result, filter_.execute(frame));
\r
124 if(packet->size == 0)
\r
129 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
\r
132 av_init_packet(&pkt);
\r
133 pkt.data = nullptr;
\r
135 auto frame = decode(pkt);
\r
136 boost::range::push_back(result, filter_.execute(frame));
\r
142 avcodec_flush_buffers(codec_context_.get());
\r
143 result.push_back(nullptr);
\r
151 std::vector<std::shared_ptr<AVFrame>> empty_poll()
\r
153 if(packets_.empty())
\r
154 return std::vector<std::shared_ptr<AVFrame>>();
\r
156 auto packet = packets_.front();
\r
160 return boost::assign::list_of(nullptr);
\r
162 std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);
\r
163 frame->data[0] = nullptr;
\r
165 return boost::assign::list_of(frame);
\r
168 std::shared_ptr<AVFrame> decode(AVPacket& pkt)
\r
170 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
172 int frame_finished = 0;
\r
173 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");
\r
175 // If a decoder consumes less then the whole packet then something is wrong
\r
176 // that might be just harmless padding at the end, or a problem with the
\r
177 // AVParser or demuxer which puted more then one frame in a AVPacket.
\r
178 pkt.data = nullptr;
\r
181 if(frame_finished != 0)
\r
183 if(decoded_frame->repeat_pict != 0)
\r
184 CASPAR_LOG(warning) << "video_decoder: repeat_pict not implemented.";
\r
185 return decoded_frame;
\r
193 return !codec_context_ || packets_.size() > 2;
\r
202 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
203 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
\r
204 std::vector<std::shared_ptr<AVFrame>> video_decoder::poll(){return impl_->poll();}
\r
205 bool video_decoder::ready() const{return impl_->ready();}
\r
206 double video_decoder::fps() const{return impl_->fps();}
\r
207 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}
\r