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