]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/video/video_decoder.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\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
10 *\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
15 *\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
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "video_decoder.h"\r
25 \r
26 #include "../util/util.h"\r
27 #include "../input/input.h"\r
28 \r
29 #include "../../ffmpeg_error.h"\r
30 \r
31 #include <common/log.h>\r
32 #include <core/frame/frame_transform.h>\r
33 #include <core/frame/frame_factory.h>\r
34 \r
35 #include <boost/range/algorithm_ext/push_back.hpp>\r
36 #include <boost/filesystem.hpp>\r
37 \r
38 #include <queue>\r
39 \r
40 #if defined(_MSC_VER)\r
41 #pragma warning (push)\r
42 #pragma warning (disable : 4244)\r
43 #endif\r
44 extern "C" \r
45 {\r
46         #include <libavcodec/avcodec.h>\r
47         #include <libavformat/avformat.h>\r
48 }\r
49 #if defined(_MSC_VER)\r
50 #pragma warning (pop)\r
51 #endif\r
52 \r
53 namespace caspar { namespace ffmpeg {\r
54         \r
55 struct video_decoder::impl : boost::noncopyable\r
56 {\r
57         monitor::basic_subject                                  event_subject_;\r
58         input*                                                                  input_;\r
59         int                                                                             index_;\r
60         const std::shared_ptr<AVCodecContext>   codec_context_;\r
61 \r
62         std::queue<spl::shared_ptr<AVPacket>>   packets_;\r
63         \r
64         const uint32_t                                                  nb_frames_;\r
65 \r
66         const int                                                               width_;\r
67         const int                                                               height_;\r
68         bool                                                                    is_progressive_;\r
69         uint32_t                                                                file_frame_number_;\r
70         double                                                                  fps_;\r
71 \r
72 public:\r
73         explicit impl() \r
74                 : input_(nullptr)\r
75                 , nb_frames_(0)\r
76                 , width_(0)\r
77                 , height_(0)\r
78                 , file_frame_number_(0)\r
79                 , fps_(0.0)\r
80         {\r
81         }\r
82 \r
83         explicit impl(input& in) \r
84                 : input_(&in)\r
85                 , codec_context_(open_codec(*input_->context(), AVMEDIA_TYPE_VIDEO, index_))\r
86                 , nb_frames_(static_cast<uint32_t>(input_->context()->streams[index_]->nb_frames))\r
87                 , width_(codec_context_->width)\r
88                 , height_(codec_context_->height)\r
89                 , file_frame_number_(0)\r
90                 , fps_(read_fps(*input_->context(), 0.0))\r
91         {\r
92         }\r
93         \r
94         std::shared_ptr<AVFrame> poll()\r
95         {                       \r
96                 if(!codec_context_)\r
97                         return empty_video();\r
98 \r
99                 std::shared_ptr<AVPacket> packet;\r
100                 if(!input_->try_pop_video(packet))\r
101                         return nullptr;\r
102 \r
103                 if(packet == flush_packet())\r
104                 {\r
105                         avcodec_flush_buffers(codec_context_.get());\r
106                         return nullptr;\r
107                 }\r
108                 else if(packet == null_packet())\r
109                 {\r
110                         if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
111                         {\r
112                                 AVPacket pkt;                \r
113                                 av_init_packet(&pkt);\r
114                 pkt.data = nullptr;\r
115                 pkt.size = 0;\r
116                                 return decode(pkt);\r
117                         }\r
118                         return nullptr;\r
119                 }\r
120                 \r
121                 return decode(*packet);\r
122         }\r
123 \r
124         std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
125         {\r
126                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
127 \r
128                 int frame_finished = 0;\r
129                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");\r
130                 \r
131                 // If a decoder consumes less then the whole packet then something is wrong\r
132                 // that might be just harmless padding at the end, or a problem with the\r
133                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
134 \r
135                 if(frame_finished == 0) \r
136                         return nullptr;\r
137                 \r
138                 auto stream_time_base = input_->context()->streams[pkt.stream_index]->time_base;\r
139                 auto packet_frame_number = static_cast<uint32_t>((static_cast<double>(pkt.pts * stream_time_base.num)/stream_time_base.den)*fps_);\r
140 \r
141                 file_frame_number_ = packet_frame_number;\r
142 \r
143                 is_progressive_ = !decoded_frame->interlaced_frame;\r
144 \r
145                 if(decoded_frame->repeat_pict > 0)\r
146                         CASPAR_LOG(warning) << "[video_decoder] Field repeat_pict not implemented.";\r
147                                 \r
148                 event_subject_  << monitor::event("file/video/width")   % width_\r
149                                                 << monitor::event("file/video/height")  % height_\r
150                                                 << monitor::event("file/video/field")   % u8(!decoded_frame->interlaced_frame ? "progressive" : (decoded_frame->top_field_first ? "upper" : "lower"))\r
151                                                 << monitor::event("file/video/codec")   % u8(codec_context_->codec->long_name);\r
152                 \r
153                 return decoded_frame;\r
154         }\r
155         \r
156         uint32_t nb_frames() const\r
157         {\r
158                 return std::max(nb_frames_, file_frame_number_);\r
159         }\r
160 \r
161         std::wstring print() const\r
162         {               \r
163                 return L"[video-decoder] " + u16(codec_context_->codec->long_name);\r
164         }\r
165 };\r
166 \r
167 video_decoder::video_decoder() : impl_(new impl()){}\r
168 video_decoder::video_decoder(input& in) : impl_(new impl(in)){}\r
169 video_decoder::video_decoder(video_decoder&& other) : impl_(std::move(other.impl_)){}\r
170 video_decoder& video_decoder::operator=(video_decoder&& other){impl_ = std::move(other.impl_); return *this;}\r
171 std::shared_ptr<AVFrame> video_decoder::operator()(){return impl_->poll();}\r
172 int video_decoder::width() const{return impl_->width_;}\r
173 int video_decoder::height() const{return impl_->height_;}\r
174 uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}\r
175 uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}\r
176 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
177 std::wstring video_decoder::print() const{return impl_->print();}\r
178 void video_decoder::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}\r
179 void video_decoder::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}\r
180 \r
181 }}