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
23 #include "../util.h"
\r
24 #include "../filter/filter.h"
\r
26 #include "../../ffmpeg_error.h"
\r
27 #include "../../tbb_avcodec.h"
\r
29 #include <common/memory/memcpy.h>
\r
31 #include <core/video_format.h>
\r
32 #include <core/producer/frame/basic_frame.h>
\r
33 #include <core/mixer/write_frame.h>
\r
34 #include <core/producer/frame/image_transform.h>
\r
35 #include <core/producer/frame/frame_factory.h>
\r
36 #include <core/producer/color/color_producer.h>
\r
38 #include <tbb/task_group.h>
\r
40 #include <boost/range/algorithm_ext.hpp>
\r
42 #if defined(_MSC_VER)
\r
43 #pragma warning (push)
\r
44 #pragma warning (disable : 4244)
\r
48 #define __STDC_CONSTANT_MACROS
\r
49 #define __STDC_LIMIT_MACROS
\r
50 #include <libavformat/avformat.h>
\r
51 #include <libavcodec/avcodec.h>
\r
53 #if defined(_MSC_VER)
\r
54 #pragma warning (pop)
\r
59 struct video_decoder::implementation : boost::noncopyable
\r
61 const safe_ptr<core::frame_factory> frame_factory_;
\r
62 std::shared_ptr<AVCodecContext> codec_context_;
\r
65 std::queue<std::shared_ptr<AVPacket>> packet_buffer_;
\r
72 explicit implementation(const std::shared_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter)
\r
73 : frame_factory_(frame_factory)
\r
75 , fps_(frame_factory_->get_video_format_desc().fps)
\r
81 index_ = av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
\r
85 BOOST_THROW_EXCEPTION(
\r
86 file_read_error() <<
\r
87 msg_info(av_error_str(index_)) <<
\r
88 boost::errinfo_api_function("av_find_best_stream") <<
\r
89 boost::errinfo_errno(AVUNERROR(index_)));
\r
92 const int ret = tbb_avcodec_open(context->streams[index_]->codec, dec);
\r
95 BOOST_THROW_EXCEPTION(
\r
96 file_read_error() <<
\r
97 msg_info(av_error_str(ret)) <<
\r
98 boost::errinfo_api_function("tbb_avcodec_open") <<
\r
99 boost::errinfo_errno(AVUNERROR(ret)));
\r
104 CASPAR_LOG_CURRENT_EXCEPTION();
\r
105 CASPAR_LOG(warning) << "[video_decoder] Failed to open video. Running without audio.";
\r
109 codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);
\r
111 // Some files give an invalid time_base numerator, try to fix it.
\r
112 if(codec_context_ && codec_context_->time_base.num == 1)
\r
113 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
115 nb_frames_ = context->streams[index_]->nb_frames;
\r
116 if(nb_frames_ == 0)
\r
117 nb_frames_ = context->streams[index_]->duration;// * context->streams[index_]->time_base.den;
\r
119 fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);
\r
120 if(double_rate(filter))
\r
124 void push(const std::shared_ptr<AVPacket>& packet)
\r
126 if(!codec_context_)
\r
129 if(packet && packet->stream_index != index_)
\r
132 packet_buffer_.push(packet);
\r
135 std::vector<std::shared_ptr<AVFrame>> poll()
\r
137 std::vector<std::shared_ptr<AVFrame>> result;
\r
139 if(!codec_context_)
\r
141 std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);
\r
142 frame->data[0] = nullptr;
\r
143 result.push_back(frame);
\r
145 else if(!packet_buffer_.empty())
\r
147 auto packet = packet_buffer_.front();
\r
151 auto frame = decode(*packet);
\r
152 boost::range::push_back(result, filter_.execute(frame));
\r
153 if(packet->size == 0)
\r
154 packet_buffer_.pop();
\r
158 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
\r
161 av_init_packet(&pkt);
\r
162 pkt.data = nullptr;
\r
164 auto frame = decode(pkt);
\r
165 boost::range::push_back(result, filter_.execute(frame));
\r
170 packet_buffer_.pop();
\r
171 avcodec_flush_buffers(codec_context_.get());
\r
172 result.push_back(nullptr);
\r
180 std::shared_ptr<AVFrame> decode(AVPacket& pkt)
\r
182 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
184 int frame_finished = 0;
\r
185 const int ret = avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt);
\r
189 BOOST_THROW_EXCEPTION(
\r
190 invalid_operation() <<
\r
191 msg_info(av_error_str(ret)) <<
\r
192 boost::errinfo_api_function("avcodec_decode_video") <<
\r
193 boost::errinfo_errno(AVUNERROR(ret)));
\r
196 // if a decoder consumes less then the whole packet then something is wrong
\r
197 // that might be just harmless padding at the end or a problem with the
\r
198 // AVParser or demuxer which puted more then one frame in a AVPacket
\r
199 pkt.data = nullptr;
\r
202 if(frame_finished != 0)
\r
204 if(decoded_frame->repeat_pict != 0)
\r
205 CASPAR_LOG(warning) << "video_decoder: repeat_pict not implemented.";
\r
206 return decoded_frame;
\r
214 return !codec_context_ || !packet_buffer_.empty();
\r
223 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
224 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
\r
225 std::vector<std::shared_ptr<AVFrame>> video_decoder::poll(){return impl_->poll();}
\r
226 bool video_decoder::ready() const{return impl_->ready();}
\r
227 double video_decoder::fps() const{return impl_->fps();}
\r
228 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}
\r