2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../StdAfx.h"
24 #include "video_decoder.h"
26 #include "../util/util.h"
27 #include "../input/input.h"
29 #include "../../ffmpeg_error.h"
31 #include <common/log.h>
32 #include <core/frame/frame_transform.h>
33 #include <core/frame/frame_factory.h>
35 #include <boost/filesystem.hpp>
40 #pragma warning (push)
41 #pragma warning (disable : 4244)
45 #include <libavcodec/avcodec.h>
46 #include <libavformat/avformat.h>
52 namespace caspar { namespace ffmpeg {
54 struct video_decoder::impl : boost::noncopyable
56 core::monitor::subject monitor_subject_;
59 const spl::shared_ptr<AVCodecContext> codec_context_;
61 std::queue<spl::shared_ptr<AVPacket>> packets_;
63 const AVStream* stream_;
64 const uint32_t nb_frames_;
69 uint32_t file_frame_number_;
72 std::shared_ptr<AVPacket> current_packet_;
75 explicit impl(input& in)
77 , codec_context_(open_codec(input_->context(), AVMEDIA_TYPE_VIDEO, index_))
78 , stream_(input_->context().streams[index_])
79 , nb_frames_(static_cast<uint32_t>(stream_->nb_frames))
80 , width_(codec_context_->width)
81 , height_(codec_context_->height)
82 , file_frame_number_(0)
83 , fps_(read_fps(input_->context(), 0.0))
87 std::shared_ptr<AVFrame> poll()
89 if(!current_packet_ && !input_->try_pop_video(current_packet_))
92 std::shared_ptr<AVFrame> frame;
96 avcodec_flush_buffers(codec_context_.get());
98 else if(!current_packet_->data)
100 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
101 frame = decode(*current_packet_);
104 current_packet_.reset();
108 frame = decode(*current_packet_);
110 if(current_packet_->size == 0)
111 current_packet_.reset();
114 return frame ? frame : poll();
117 std::shared_ptr<AVFrame> decode(AVPacket& pkt)
119 auto frame = create_frame();
122 auto len = THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), frame.get(), &got_frame, &pkt), "[video_decocer]");
136 auto stream_time_base = stream_->time_base;
137 auto packet_frame_number = static_cast<uint32_t>((static_cast<double>(pkt.pts * stream_time_base.num)/stream_time_base.den)*fps_);
139 file_frame_number_ = packet_frame_number;
141 is_progressive_ = !frame->interlaced_frame;
143 if(frame->repeat_pict > 0)
144 CASPAR_LOG(warning) << "[video_decoder] repeat_pict not implemented.";
146 monitor_subject_ << core::monitor::message("/file/video/width") % width_
147 << core::monitor::message("/file/video/height") % height_
148 << core::monitor::message("/file/video/field") % u8(!frame->interlaced_frame ? "progressive" : (frame->top_field_first ? "upper" : "lower"))
149 << core::monitor::message("/file/video/codec") % u8(codec_context_->codec->long_name);
154 uint32_t nb_frames() const
156 return std::max(nb_frames_, file_frame_number_);
159 std::wstring print() const
161 return L"[video-decoder] " + u16(codec_context_->codec->long_name);
165 video_decoder::video_decoder(input& in) : impl_(new impl(in)){}
166 video_decoder::video_decoder(video_decoder&& other) : impl_(std::move(other.impl_)){}
167 video_decoder& video_decoder::operator=(video_decoder&& other){impl_ = std::move(other.impl_); return *this;}
168 std::shared_ptr<AVFrame> video_decoder::operator()(){return impl_->poll();}
169 int video_decoder::width() const{return impl_->width_;}
170 int video_decoder::height() const{return impl_->height_;}
171 uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}
172 uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}
173 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
174 std::wstring video_decoder::print() const{return impl_->print();}
175 core::monitor::subject& video_decoder::monitor_output() { return impl_->monitor_subject_; }