2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../../stdafx.h"
\r
24 #include "video_decoder.h"
\r
26 #include "../util/util.h"
\r
28 #include "../../ffmpeg_error.h"
\r
30 #include <common/log.h>
\r
31 #include <core/frame/frame_transform.h>
\r
32 #include <core/frame/frame_factory.h>
\r
34 #include <boost/range/algorithm_ext/push_back.hpp>
\r
35 #include <boost/filesystem.hpp>
\r
39 #if defined(_MSC_VER)
\r
40 #pragma warning (push)
\r
41 #pragma warning (disable : 4244)
\r
45 #include <libavcodec/avcodec.h>
\r
46 #include <libavformat/avformat.h>
\r
48 #if defined(_MSC_VER)
\r
49 #pragma warning (pop)
\r
52 namespace caspar { namespace ffmpeg {
\r
54 struct video_decoder::impl : boost::noncopyable
\r
57 const safe_ptr<AVCodecContext> codec_context_;
\r
59 std::queue<safe_ptr<AVPacket>> packets_;
\r
61 const uint32_t nb_frames_;
\r
65 bool is_progressive_;
\r
67 tbb::atomic<uint32_t> file_frame_number_;
\r
70 explicit impl(const safe_ptr<AVFormatContext>& context)
\r
71 : codec_context_(open_codec(*context, AVMEDIA_TYPE_VIDEO, index_))
\r
72 , nb_frames_(static_cast<uint32_t>(context->streams[index_]->nb_frames))
\r
73 , width_(codec_context_->width)
\r
74 , height_(codec_context_->height)
\r
76 file_frame_number_ = 0;
\r
79 void push(const std::shared_ptr<AVPacket>& packet)
\r
84 if(packet->stream_index == index_ || packet->data == nullptr)
\r
85 packets_.push(make_safe_ptr(packet));
\r
88 std::shared_ptr<AVFrame> poll()
\r
90 if(packets_.empty())
\r
93 auto packet = packets_.front();
\r
95 if(packet->data == nullptr)
\r
97 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
\r
99 auto video = decode(*packet);
\r
105 file_frame_number_ = static_cast<uint32_t>(packet->pos);
\r
106 avcodec_flush_buffers(codec_context_.get());
\r
107 return flush_video();
\r
111 return decode(*packet);
\r
114 std::shared_ptr<AVFrame> decode(AVPacket& pkt)
\r
116 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);
\r
118 int frame_finished = 0;
\r
119 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");
\r
121 // If a decoder consumes less then the whole packet then something is wrong
\r
122 // that might be just harmless padding at the end, or a problem with the
\r
123 // AVParser or demuxer which puted more then one frame in a AVPacket.
\r
125 if(frame_finished == 0)
\r
128 is_progressive_ = !decoded_frame->interlaced_frame;
\r
130 if(decoded_frame->repeat_pict > 0)
\r
131 CASPAR_LOG(warning) << "[video_decoder] Field repeat_pict not implemented.";
\r
133 ++file_frame_number_;
\r
135 return decoded_frame;
\r
140 return packets_.size() >= 8;
\r
143 uint32_t nb_frames() const
\r
145 return std::max<uint32_t>(nb_frames_, file_frame_number_);
\r
148 std::wstring print() const
\r
150 return L"[video-decoder] " + u16(codec_context_->codec->long_name);
\r
154 video_decoder::video_decoder(const safe_ptr<AVFormatContext>& context) : impl_(new impl(context)){}
\r
155 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}
\r
156 std::shared_ptr<AVFrame> video_decoder::poll(){return impl_->poll();}
\r
157 bool video_decoder::ready() const{return impl_->ready();}
\r
158 int video_decoder::width() const{return impl_->width_;}
\r
159 int video_decoder::height() const{return impl_->height_;}
\r
160 uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}
\r
161 uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}
\r
162 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
\r
163 std::wstring video_decoder::print() const{return impl_->print();}
\r