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
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 #include <connect.h>
\r
46 #include <semaphore.h>
\r
48 #include <tbb/scalable_allocator.h>
\r
50 using namespace Concurrency;
\r
52 namespace caspar { namespace ffmpeg {
\r
54 struct video_decoder::implementation : public Concurrency::agent, boost::noncopyable
\r
57 std::shared_ptr<AVCodecContext> codec_context_;
\r
64 bool is_progressive_;
\r
66 overwrite_buffer<bool> is_running_;
\r
67 unbounded_buffer<safe_ptr<AVPacket>> source_;
\r
68 ITarget<safe_ptr<AVFrame>>& 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 safe_ptr<AVPacket>& packet)
\r
80 return packet->stream_index == index_;
\r
84 CASPAR_LOG(debug) << "[video_decoder] " << context.streams[index_]->codec->codec->long_name;
\r
86 CASPAR_VERIFY(width_ > 0, ffmpeg_error());
\r
87 CASPAR_VERIFY(height_ > 0, ffmpeg_error());
\r
89 Concurrency::connect(source, source_);
\r
96 send(is_running_, false);
\r
104 send(is_running_, true);
\r
105 while(is_running_.value())
\r
107 auto packet = receive(source_);
\r
109 if(packet == loop_packet(index_))
\r
111 send(target_, loop_video());
\r
115 if(packet == eof_packet(index_))
\r
118 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
120 int frame_finished = 0;
\r
121 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, packet.get()), "[video_decocer]");
\r
123 // 1 packet <=> 1 frame.
\r
124 // If a decoder consumes less then the whole packet then something is wrong
\r
125 // that might be just harmless padding at the end, or a problem with the
\r
126 // AVParser or demuxer which puted more then one frame in a AVPacket.
\r
128 if(frame_finished == 0)
\r
131 if(decoded_frame->repeat_pict > 0)
\r
132 CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";
\r
134 is_progressive_ = decoded_frame->interlaced_frame == 0;
\r
136 // Need to dupliace frame data since avcodec_decode_video2 reuses it.
\r
137 // C-TODO: Avoid duplication.
\r
138 send(target_, dup_frame(make_safe_ptr(decoded_frame)));
\r
139 Concurrency::wait(10);
\r
144 CASPAR_LOG_CURRENT_EXCEPTION();
\r
147 send(is_running_, false),
\r
148 send(target_, eof_video());
\r
153 safe_ptr<AVFrame> dup_frame(const safe_ptr<AVFrame>& frame)
\r
155 std::array<uint8_t*, 4> data;
\r
156 parallel_for(0, 4, [&](int n)
\r
158 data[n] = frame->data[n];
\r
159 frame->data[n] = reinterpret_cast<uint8_t*>(scalable_aligned_malloc(frame->linesize[n]*frame->height, 32));
\r
160 memcpy(frame->data[n], data[n], frame->linesize[n]*frame->height);
\r
163 return safe_ptr<AVFrame>(frame.get(), [frame, data](AVFrame*)
\r
165 for(int n = 0; n < 4; ++n)
\r
167 scalable_aligned_free(frame->data[n]);
\r
168 frame->data[n] = data[n];
\r
179 video_decoder::video_decoder(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context)
\r
180 : impl_(new implementation(source, target, context))
\r
184 double video_decoder::fps() const{return impl_->fps();}
\r
185 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}
\r
186 size_t video_decoder::width() const{return impl_->width_;}
\r
187 size_t video_decoder::height() const{return impl_->height_;}
\r
188 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
\r