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
29 #include <core/producer/frame/basic_frame.h>
\r
30 #include <common/memory/memcpy.h>
\r
31 #include <common/concurrency/governor.h>
\r
33 #if defined(_MSC_VER)
\r
34 #pragma warning (push)
\r
35 #pragma warning (disable : 4244)
\r
39 #include <libavcodec/avcodec.h>
\r
40 #include <libavformat/avformat.h>
\r
42 #if defined(_MSC_VER)
\r
43 #pragma warning (pop)
\r
46 #include <tbb/scalable_allocator.h>
\r
49 using namespace Concurrency;
\r
51 namespace caspar { namespace ffmpeg {
\r
53 struct video_decoder::implementation : public Concurrency::agent, boost::noncopyable
\r
56 std::shared_ptr<AVCodecContext> codec_context_;
\r
63 bool is_progressive_;
\r
65 unbounded_buffer<video_decoder::source_element_t> source_;
\r
66 ITarget<video_decoder::target_element_t>& target_;
\r
71 explicit implementation(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context)
\r
72 : codec_context_(open_codec(context, AVMEDIA_TYPE_VIDEO, index_))
\r
73 , fps_(static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num))
\r
74 , nb_frames_(context.streams[index_]->nb_frames)
\r
75 , width_(codec_context_->width)
\r
76 , height_(codec_context_->height)
\r
77 , is_progressive_(true)
\r
78 , source_([this](const video_decoder::source_element_t& packet){return packet->stream_index == index_;})
\r
82 CASPAR_LOG(debug) << "[video_decoder] " << context.streams[index_]->codec->codec->long_name;
\r
84 source.link_target(&source_);
\r
94 std::shared_ptr<AVFrame> decode(AVPacket& packet)
\r
96 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
98 int frame_finished = 0;
\r
99 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &packet), "[video_decocer]");
\r
101 // 1 packet <=> 1 frame.
\r
102 // If a decoder consumes less then the whole packet then something is wrong
\r
103 // that might be just harmless padding at the end, or a problem with the
\r
104 // AVParser or demuxer which puted more then one frame in a AVPacket.
\r
106 if(frame_finished == 0)
\r
109 if(decoded_frame->repeat_pict > 0)
\r
110 CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";
\r
112 return decoded_frame;
\r
119 win32_exception::install_handler();
\r
122 auto ticket = governor_.acquire();
\r
123 auto packet = receive(source_);
\r
125 if(packet == loop_packet(index_))
\r
127 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
\r
130 av_init_packet(&pkt);
\r
131 pkt.data = nullptr;
\r
134 for(auto decoded_frame = decode(pkt); decoded_frame; decoded_frame = decode(pkt))
\r
136 auto frame = dup_frame(make_safe_ptr(decoded_frame));
\r
137 send(target_, safe_ptr<AVFrame>(frame.get(), [frame, ticket](AVFrame*){}));
\r
142 avcodec_flush_buffers(codec_context_.get());
\r
143 send(target_, loop_video());
\r
147 if(packet == eof_packet(index_))
\r
150 auto decoded_frame = decode(*packet);
\r
154 is_progressive_ = decoded_frame->interlaced_frame == 0;
\r
156 // C-TODO: Avoid duplication.
\r
157 // Need to dupliace frame data since avcodec_decode_video2 reuses it.
\r
158 auto frame = dup_frame(make_safe_ptr(decoded_frame));
\r
159 send(target_, safe_ptr<AVFrame>(frame.get(), [frame, ticket](AVFrame*){}));
\r
165 CASPAR_LOG_CURRENT_EXCEPTION();
\r
168 send(target_, eof_video());
\r
173 safe_ptr<AVFrame> dup_frame(const safe_ptr<AVFrame>& frame)
\r
175 auto desc = get_pixel_format_desc(static_cast<PixelFormat>(frame->format), frame->width, frame->height);
\r
177 auto count = desc.planes.size();
\r
178 std::array<uint8_t*, 4> org_ptrs;
\r
179 std::array<safe_ptr<uint8_t>, 4> new_ptrs;
\r
180 parallel_for<size_t>(0, count, [&](size_t n)
\r
182 CASPAR_ASSERT(frame->data[n]);
\r
183 auto size = frame->linesize[n]*desc.planes[n].height;
\r
184 new_ptrs[n] = fast_memdup(frame->data[n], size);
\r
185 org_ptrs[n] = frame->data[n];
\r
186 frame->data[n] = new_ptrs[n].get();
\r
189 return safe_ptr<AVFrame>(frame.get(), [frame, org_ptrs, new_ptrs, count](AVFrame*)
\r
191 for(size_t n = 0; n < count; ++n)
\r
192 frame->data[n] = org_ptrs[n];
\r
202 video_decoder::video_decoder(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context)
\r
203 : impl_(new implementation(source, target, context))
\r
207 double video_decoder::fps() const{return impl_->fps();}
\r
208 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}
\r
209 size_t video_decoder::width() const{return impl_->width_;}
\r
210 size_t video_decoder::height() const{return impl_->height_;}
\r
211 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
\r