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/basic_frame.h>
\r
32 #if defined(_MSC_VER)
\r
33 #pragma warning (push)
\r
34 #pragma warning (disable : 4244)
\r
38 #include <libavcodec/avcodec.h>
\r
39 #include <libavformat/avformat.h>
\r
41 #if defined(_MSC_VER)
\r
42 #pragma warning (pop)
\r
45 namespace caspar { namespace ffmpeg {
\r
47 struct video_decoder::implementation : public Concurrency::agent, boost::noncopyable
\r
49 video_decoder::source_t& source_;
\r
50 video_decoder::forward_t& forward_;
\r
51 video_decoder::target_t& target_;
\r
53 std::shared_ptr<AVCodecContext> codec_context_;
\r
63 bool is_progressive_;
\r
66 explicit implementation(video_decoder::source_t& source,
\r
67 video_decoder::forward_t& forward,
\r
68 video_decoder::target_t& target,
\r
69 const safe_ptr<AVFormatContext>& context,
\r
71 const std::wstring& filter)
\r
75 , filter_(filter.empty() ? L"copy" : filter)
\r
80 , is_progressive_(true)
\r
85 index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0), "[video_decoder]");
\r
87 THROW_ON_ERROR2(tbb_avcodec_open(context->streams[index_]->codec, dec), "[video_decoder]");
\r
89 codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);
\r
91 CASPAR_LOG(debug) << "[video_decoder] " << context->streams[index_]->codec->codec->long_name;
\r
93 // Some files give an invalid time_base numerator, try to fix it.
\r
95 fix_meta_data(*context);
\r
97 fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);
\r
98 nb_frames_ = context->streams[index_]->nb_frames;
\r
100 if(double_rate(filter))
\r
103 width_ = codec_context_->width;
\r
104 height_ = codec_context_->height;
\r
108 index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0), "[video_decoder]");
\r
110 CASPAR_LOG_CURRENT_EXCEPTION();
\r
111 CASPAR_LOG(warning) << "[video_decoder] Failed to open video-stream. Running without video.";
\r
128 auto packet = Concurrency::receive(source_);
\r
130 if(packet == eof_packet() || packet == loop_packet() || packet->stream_index != index_)
\r
131 Concurrency::send(forward_, packet);
\r
133 if(packet == eof_packet())
\r
136 if(packet == loop_packet())
\r
140 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
\r
143 av_init_packet(&pkt);
\r
144 pkt.data = nullptr;
\r
147 BOOST_FOREACH(auto& frame1, decode(pkt))
\r
149 BOOST_FOREACH(auto& frame2, filter_.execute(frame1))
\r
150 Concurrency::send(target_, std::shared_ptr<AVFrame>(frame2));
\r
154 avcodec_flush_buffers(codec_context_.get());
\r
157 Concurrency::send(target_, loop_video());
\r
159 else if(packet->stream_index == index_)
\r
161 if(!codec_context_)
\r
163 Concurrency::send(target_, empty_video());
\r
167 while(packet->size > 0)
\r
169 BOOST_FOREACH(auto& frame1, decode(*packet))
\r
171 BOOST_FOREACH(auto& frame2, filter_.execute(frame1))
\r
172 Concurrency::send(target_, std::shared_ptr<AVFrame>(frame2));
\r
181 CASPAR_LOG_CURRENT_EXCEPTION();
\r
184 Concurrency::send(target_, eof_video());
\r
189 std::vector<std::shared_ptr<AVFrame>> decode(AVPacket& pkt)
\r
191 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
193 int frame_finished = 0;
\r
194 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");
\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
203 return std::vector<std::shared_ptr<AVFrame>>();
\r
205 if(decoded_frame->repeat_pict % 2 > 0)
\r
206 CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";
\r
208 is_progressive_ = decoded_frame->interlaced_frame == 0;
\r
210 return std::vector<std::shared_ptr<AVFrame>>(1 + decoded_frame->repeat_pict/2, decoded_frame);
\r
219 video_decoder::video_decoder(source_t& source,
\r
220 forward_t& forward,
\r
222 const safe_ptr<AVFormatContext>& context,
\r
224 const std::wstring& filter)
\r
225 : impl_(new implementation(source, forward, target, context, fps, filter))
\r
229 double video_decoder::fps() const{return impl_->fps();}
\r
230 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}
\r
231 size_t video_decoder::width() const{return impl_->width_;}
\r
232 size_t video_decoder::height() const{return impl_->height_;}
\r
233 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
\r