]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/video/video_decoder.cpp
Merged most recent OSC changes
[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/range/algorithm_ext/push_back.hpp>
36 #include <boost/filesystem.hpp>
37
38 #include <queue>
39
40 #if defined(_MSC_VER)
41 #pragma warning (push)
42 #pragma warning (disable : 4244)
43 #endif
44 extern "C" 
45 {
46         #include <libavcodec/avcodec.h>
47         #include <libavformat/avformat.h>
48 }
49 #if defined(_MSC_VER)
50 #pragma warning (pop)
51 #endif
52
53 namespace caspar { namespace ffmpeg {
54         
55 struct video_decoder::impl : boost::noncopyable
56 {
57         core::monitor::subject                                  monitor_subject_;
58         input*                                                                  input_;
59         int                                                                             index_;
60         const spl::shared_ptr<AVCodecContext>   codec_context_;
61
62         std::queue<spl::shared_ptr<AVPacket>>   packets_;
63         
64         const AVStream*                                                 stream_;
65         const uint32_t                                                  nb_frames_;
66         const int                                                               width_;
67         const int                                                               height_;
68
69         bool                                                                    is_progressive_;
70         uint32_t                                                                file_frame_number_;
71         double                                                                  fps_;
72         
73         std::shared_ptr<AVPacket>                               current_packet_;
74
75 public:
76         explicit impl(input& in) 
77                 : input_(&in)
78                 , codec_context_(open_codec(input_->context(), AVMEDIA_TYPE_VIDEO, index_))
79                 , stream_(input_->context().streams[index_])
80                 , nb_frames_(static_cast<uint32_t>(stream_->nb_frames))
81                 , width_(codec_context_->width)
82                 , height_(codec_context_->height)
83                 , file_frame_number_(0)
84                 , fps_(read_fps(input_->context(), 0.0))
85         {
86         }
87         
88         std::shared_ptr<AVFrame> poll()
89         {                       
90                 if(!current_packet_ && !input_->try_pop_video(current_packet_))
91                         return nullptr;
92                 
93                 std::shared_ptr<AVFrame> frame;
94
95                 if(!current_packet_)            
96                 {
97                         avcodec_flush_buffers(codec_context_.get());    
98                 }
99                 else if(!current_packet_->data)
100                 {
101                         if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)                       
102                                 frame = decode(*current_packet_);
103                         
104                         if(!frame)
105                                 current_packet_.reset();
106                 }
107                 else
108                 {
109                         frame = decode(*current_packet_);
110                         
111                         if(current_packet_->size == 0)
112                                 current_packet_.reset();
113                 }
114                         
115                 return frame ? frame : poll();
116         }
117
118         std::shared_ptr<AVFrame> decode(AVPacket& pkt)
119         {
120                 auto frame = create_frame();
121
122                 int got_frame = 0;
123                 auto len = THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), frame.get(), &got_frame, &pkt), "[video_decocer]");
124                                 
125                 if(len == 0)
126                 {
127                         pkt.size = 0;
128                         return nullptr;
129                 }
130
131         pkt.data += len;
132         pkt.size -= len;
133
134                 if(got_frame == 0)      
135                         return nullptr;
136                 
137                 auto stream_time_base    = stream_->time_base;
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) : impl_(new impl(in)){}
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 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
175 std::wstring video_decoder::print() const{return impl_->print();}
176 core::monitor::subject& video_decoder::monitor_output() { return impl_->monitor_subject_; }
177 }}