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
27 #include "../input/input.h"
\r
29 #include "../../ffmpeg_error.h"
\r
31 #include <common/log.h>
\r
32 #include <core/frame/frame_transform.h>
\r
33 #include <core/frame/frame_factory.h>
\r
35 #include <boost/range/algorithm_ext/push_back.hpp>
\r
36 #include <boost/filesystem.hpp>
\r
40 #if defined(_MSC_VER)
\r
41 #pragma warning (push)
\r
42 #pragma warning (disable : 4244)
\r
46 #include <libavcodec/avcodec.h>
\r
47 #include <libavformat/avformat.h>
\r
49 #if defined(_MSC_VER)
\r
50 #pragma warning (pop)
\r
53 namespace caspar { namespace ffmpeg {
\r
55 struct video_decoder::impl : boost::noncopyable
\r
57 monitor::basic_subject event_subject_;
\r
60 const std::shared_ptr<AVCodecContext> codec_context_;
\r
62 std::queue<spl::shared_ptr<AVPacket>> packets_;
\r
64 const AVStream* stream_;
\r
65 const uint32_t nb_frames_;
\r
69 bool is_progressive_;
\r
70 uint32_t file_frame_number_;
\r
79 , file_frame_number_(0)
\r
84 explicit impl(input& in)
\r
86 , codec_context_(open_codec(input_->context(), AVMEDIA_TYPE_VIDEO, index_))
\r
87 , stream_(input_->context().streams[index_])
\r
88 , nb_frames_(static_cast<uint32_t>(stream_->nb_frames))
\r
89 , width_(codec_context_->width)
\r
90 , height_(codec_context_->height)
\r
91 , file_frame_number_(0)
\r
92 , fps_(read_fps(input_->context(), 0.0))
\r
96 std::shared_ptr<AVFrame> poll()
\r
99 return empty_video();
\r
101 std::shared_ptr<AVPacket> packet;
\r
102 if(!input_->try_pop_video(packet))
\r
105 if(packet == flush_packet())
\r
107 avcodec_flush_buffers(codec_context_.get());
\r
110 else if(packet == null_packet())
\r
112 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)
\r
115 av_init_packet(&pkt);
\r
116 pkt.data = nullptr;
\r
118 return decode(pkt);
\r
123 return decode(*packet);
\r
126 std::shared_ptr<AVFrame> decode(AVPacket& pkt)
\r
128 std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);
\r
131 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), frame.get(), &got_frame, &pkt), "[video_decocer]");
\r
133 // If a decoder consumes less then the whole packet then something is wrong
\r
134 // that might be just harmless padding at the end, or a problem with the
\r
135 // AVParser or demuxer which puted more then one frame in a AVPacket.
\r
137 if(got_frame == 0)
\r
140 auto stream_time_base = stream_->time_base;
\r
141 auto packet_frame_number = static_cast<uint32_t>((static_cast<double>(pkt.pts * stream_time_base.num)/stream_time_base.den)*fps_);
\r
143 file_frame_number_ = packet_frame_number;
\r
145 is_progressive_ = !frame->interlaced_frame;
\r
147 if(frame->repeat_pict > 0)
\r
148 CASPAR_LOG(warning) << "[video_decoder] Field repeat_pict not implemented.";
\r
150 event_subject_ << monitor::event("file/video/width") % width_
\r
151 << monitor::event("file/video/height") % height_
\r
152 << monitor::event("file/video/field") % u8(!frame->interlaced_frame ? "progressive" : (frame->top_field_first ? "upper" : "lower"))
\r
153 << monitor::event("file/video/codec") % u8(codec_context_->codec->long_name);
\r
158 uint32_t nb_frames() const
\r
160 return std::max(nb_frames_, file_frame_number_);
\r
163 std::wstring print() const
\r
165 return L"[video-decoder] " + u16(codec_context_->codec->long_name);
\r
169 video_decoder::video_decoder() : impl_(new impl()){}
\r
170 video_decoder::video_decoder(input& in) : impl_(new impl(in)){}
\r
171 video_decoder::video_decoder(video_decoder&& other) : impl_(std::move(other.impl_)){}
\r
172 video_decoder& video_decoder::operator=(video_decoder&& other){impl_ = std::move(other.impl_); return *this;}
\r
173 std::shared_ptr<AVFrame> video_decoder::operator()(){return impl_->poll();}
\r
174 int video_decoder::width() const{return impl_->width_;}
\r
175 int video_decoder::height() const{return impl_->height_;}
\r
176 uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}
\r
177 uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}
\r
178 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
\r
179 std::wstring video_decoder::print() const{return impl_->print();}
\r
180 void video_decoder::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}
\r
181 void video_decoder::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}
\r