]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/video/video_decoder.cpp
87ee744dc8a56133de3d5c9808f72ae30875c147
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "video_decoder.h"
25
26 #include "../util/util.h"
27 #include "../input/input.h"
28
29 #include "../../ffmpeg_error.h"
30
31 #include <common/log.h>
32 #include <core/frame/frame_transform.h>
33 #include <core/frame/frame_factory.h>
34
35 #include <boost/filesystem.hpp>
36
37 #include <queue>
38
39 #if defined(_MSC_VER)
40 #pragma warning (push)
41 #pragma warning (disable : 4244)
42 #endif
43 extern "C" 
44 {
45         #include <libavcodec/avcodec.h>
46         #include <libavformat/avformat.h>
47 }
48 #if defined(_MSC_VER)
49 #pragma warning (pop)
50 #endif
51
52 namespace caspar { namespace ffmpeg {
53         
54 struct video_decoder::impl : boost::noncopyable
55 {
56         core::monitor::subject                                  monitor_subject_;
57         input*                                                                  input_;
58         int                                                                             index_                          = -1;
59         const spl::shared_ptr<AVCodecContext>   codec_context_;
60
61         std::queue<spl::shared_ptr<AVPacket>>   packets_;
62         
63         const AVStream*                                                 stream_;
64         const uint32_t                                                  nb_frames_;
65         const int                                                               width_;
66         const int                                                               height_;
67
68         bool                                                                    is_progressive_;
69         uint32_t                                                                file_frame_number_;
70         boost::rational<int>                                    framerate_;
71         
72         std::shared_ptr<AVPacket>                               current_packet_;
73
74 public:
75         explicit impl(input& in, bool single_threaded)
76                 : input_(&in)
77                 , codec_context_(open_codec(input_->context(), AVMEDIA_TYPE_VIDEO, index_, single_threaded))
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                 , framerate_(read_framerate(input_->context(), 0))
84         {
85         }
86         
87         std::shared_ptr<AVFrame> poll()
88         {                       
89                 if(!current_packet_ && !input_->try_pop_video(current_packet_))
90                         return nullptr;
91                 
92                 std::shared_ptr<AVFrame> frame;
93
94                 if(!current_packet_)            
95                 {
96                         avcodec_flush_buffers(codec_context_.get());    
97                 }
98                 else if(!current_packet_->data)
99                 {
100                         if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)                       
101                                 frame = decode(*current_packet_);
102                         
103                         if(!frame)
104                                 current_packet_.reset();
105                 }
106                 else
107                 {
108                         frame = decode(*current_packet_);
109                         
110                         if(current_packet_->size == 0)
111                                 current_packet_.reset();
112                 }
113                         
114                 return frame;
115         }
116
117         std::shared_ptr<AVFrame> decode(AVPacket& pkt)
118         {
119                 auto frame = create_frame();
120
121                 int got_frame = 0;
122                 auto len = THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), frame.get(), &got_frame, &pkt), "[video_decocer]");
123                                 
124                 if(len == 0)
125                 {
126                         pkt.size = 0;
127                         return nullptr;
128                 }
129
130         pkt.data += len;
131         pkt.size -= len;
132
133                 if(got_frame == 0)      
134                         return nullptr;
135                 
136                 auto stream_time_base           = stream_->time_base;
137                 auto fps = static_cast<double>(framerate_.numerator()) / static_cast<double>(framerate_.denominator());
138                 auto packet_frame_number        = static_cast<uint32_t>((static_cast<double>(pkt.pts * stream_time_base.num) / stream_time_base.den) * fps);
139
140                 file_frame_number_ = packet_frame_number;
141
142                 is_progressive_ = !frame->interlaced_frame;
143                 
144                 if(frame->repeat_pict > 0)
145                         CASPAR_LOG(warning) << "[video_decoder] repeat_pict not implemented.";
146                                 
147                 monitor_subject_  << core::monitor::message("/file/video/width")        % width_
148                                                 << core::monitor::message("/file/video/height") % height_
149                                                 << core::monitor::message("/file/video/field")  % u8(!frame->interlaced_frame ? "progressive" : (frame->top_field_first ? "upper" : "lower"))
150                                                 << core::monitor::message("/file/video/codec")  % u8(codec_context_->codec->long_name);
151                 
152                 return frame;
153         }
154         
155         uint32_t nb_frames() const
156         {
157                 return std::max(nb_frames_, file_frame_number_);
158         }
159
160         std::wstring print() const
161         {               
162                 return L"[video-decoder] " + u16(codec_context_->codec->long_name);
163         }
164 };
165
166 video_decoder::video_decoder(input& in, bool single_threaded) : impl_(new impl(in, single_threaded)){}
167 video_decoder::video_decoder(video_decoder&& other) : impl_(std::move(other.impl_)){}
168 video_decoder& video_decoder::operator=(video_decoder&& other){impl_ = std::move(other.impl_); return *this;}
169 std::shared_ptr<AVFrame> video_decoder::operator()(){return impl_->poll();}
170 int video_decoder::width() const{return impl_->width_;}
171 int video_decoder::height() const{return impl_->height_;}
172 uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}
173 uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}
174 boost::rational<int> video_decoder::framerate() const { return impl_->framerate_; }
175 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
176 std::wstring video_decoder::print() const{return impl_->print();}
177 core::monitor::subject& video_decoder::monitor_output() { return impl_->monitor_subject_; }
178 }}