]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/video/video_decoder.cpp
2.0. Updated namespaces.
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "video_decoder.h"\r
23 \r
24 #include "../util.h"\r
25 #include "../filter/filter.h"\r
26 \r
27 #include "../../ffmpeg_error.h"\r
28 #include "../../tbb_avcodec.h"\r
29 \r
30 #include <core/producer/frame/frame_transform.h>\r
31 #include <core/producer/frame/frame_factory.h>\r
32 \r
33 #include <boost/range/algorithm_ext/push_back.hpp>\r
34 #include <boost/filesystem.hpp>\r
35 \r
36 #include <queue>\r
37 \r
38 #if defined(_MSC_VER)\r
39 #pragma warning (push)\r
40 #pragma warning (disable : 4244)\r
41 #endif\r
42 extern "C" \r
43 {\r
44         #include <libavcodec/avcodec.h>\r
45         #include <libavformat/avformat.h>\r
46 }\r
47 #if defined(_MSC_VER)\r
48 #pragma warning (pop)\r
49 #endif\r
50 \r
51 namespace caspar { namespace ffmpeg {\r
52         \r
53 struct video_decoder::implementation : boost::noncopyable\r
54 {\r
55         const safe_ptr<core::frame_factory>             frame_factory_;\r
56         std::shared_ptr<AVCodecContext>                 codec_context_;\r
57         int                                                                             index_;\r
58 \r
59         std::queue<std::shared_ptr<AVPacket>>   packets_;\r
60 \r
61         filter                                                                  filter_;\r
62 \r
63         double                                                                  fps_;\r
64         int64_t                                                                 nb_frames_;\r
65 \r
66         size_t                                                                  width_;\r
67         size_t                                                                  height_;\r
68 \r
69 public:\r
70         explicit implementation(const safe_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) \r
71                 : frame_factory_(frame_factory)\r
72                 , filter_(filter)\r
73                 , fps_(frame_factory_->get_video_format_desc().fps)\r
74                 , nb_frames_(0)\r
75                 , width_(0)\r
76                 , height_(0)\r
77         {\r
78                 try\r
79                 {\r
80                         AVCodec* dec;\r
81                         index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0), "[video_decoder]");\r
82                                                 \r
83                         THROW_ON_ERROR2(tbb_avcodec_open(context->streams[index_]->codec, dec), "[video_decoder]");\r
84                                                                 \r
85                         codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\r
86                 \r
87                         CASPAR_LOG(debug) << "[video_decoder] " << context->streams[index_]->codec->codec->long_name;\r
88 \r
89                         // Some files give an invalid time_base numerator, try to fix it.\r
90 \r
91                         fix_meta_data(*context);\r
92                         \r
93                         fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);\r
94                         nb_frames_ = context->streams[index_]->nb_frames;\r
95 \r
96                         if(double_rate(filter))\r
97                                 fps_ *= 2;\r
98 \r
99                         width_  = codec_context_->width;\r
100                         height_ = codec_context_->height;\r
101                 }\r
102                 catch(...)\r
103                 {\r
104                         index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0), "[video_decoder]");\r
105 \r
106                         CASPAR_LOG_CURRENT_EXCEPTION();\r
107                         CASPAR_LOG(warning) << "[video_decoder] Failed to open video-stream. Running without video.";   \r
108                 }\r
109         }\r
110 \r
111         void push(const std::shared_ptr<AVPacket>& packet)\r
112         {\r
113                 if(packet && packet->stream_index != index_)\r
114                         return;\r
115 \r
116                 packets_.push(packet);\r
117         }\r
118 \r
119         std::vector<std::shared_ptr<AVFrame>> poll()\r
120         {               \r
121                 std::vector<std::shared_ptr<AVFrame>> result;\r
122 \r
123                 if(packets_.empty())\r
124                         return result;\r
125 \r
126                 if(!codec_context_)\r
127                         return empty_poll();\r
128 \r
129                 auto packet = packets_.front();\r
130                                         \r
131                 if(packet)\r
132                 {                       \r
133                         BOOST_FOREACH(auto& frame, decode(*packet))\r
134                                 boost::range::push_back(result, filter_.execute(frame));\r
135 \r
136                         if(packet->size == 0)\r
137                                 packets_.pop();\r
138                 }\r
139                 else\r
140                 {\r
141                         if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
142                         {\r
143                                 AVPacket pkt;\r
144                                 av_init_packet(&pkt);\r
145                                 pkt.data = nullptr;\r
146                                 pkt.size = 0;\r
147 \r
148                                 BOOST_FOREACH(auto& frame, decode(pkt))\r
149                                         boost::range::push_back(result, filter_.execute(frame));        \r
150                         }\r
151 \r
152                         if(result.empty())\r
153                         {                                       \r
154                                 packets_.pop();\r
155                                 avcodec_flush_buffers(codec_context_.get());\r
156                                 result.push_back(nullptr);\r
157                         }\r
158                 }\r
159                 \r
160                 return result;\r
161         }\r
162 \r
163         std::vector<std::shared_ptr<AVFrame>> empty_poll()\r
164         {                               \r
165                 auto packet = packets_.front();\r
166                 packets_.pop();\r
167 \r
168                 if(!packet)                     \r
169                         return boost::assign::list_of(nullptr);\r
170 \r
171                 std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);\r
172                 frame->data[0] = nullptr;\r
173 \r
174                 return boost::assign::list_of(frame);                                   \r
175         }\r
176 \r
177         std::vector<std::shared_ptr<AVFrame>> decode(AVPacket& pkt)\r
178         {\r
179                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
180 \r
181                 int frame_finished = 0;\r
182                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");\r
183                 \r
184                 // If a decoder consumes less then the whole packet then something is wrong\r
185                 // that might be just harmless padding at the end, or a problem with the\r
186                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
187                 pkt.data = nullptr;\r
188                 pkt.size = 0;\r
189 \r
190                 if(frame_finished == 0) \r
191                         return std::vector<std::shared_ptr<AVFrame>>();\r
192 \r
193                 if(decoded_frame->repeat_pict % 2 > 0)\r
194                         CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
195                 \r
196                 return std::vector<std::shared_ptr<AVFrame>>(1 + decoded_frame->repeat_pict/2, decoded_frame);\r
197         }\r
198         \r
199         bool ready() const\r
200         {\r
201                 return !packets_.empty();\r
202         }\r
203         \r
204         double fps() const\r
205         {\r
206                 return fps_;\r
207         }\r
208 };\r
209 \r
210 video_decoder::video_decoder(const safe_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) : impl_(new implementation(context, frame_factory, filter)){}\r
211 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
212 std::vector<std::shared_ptr<AVFrame>> video_decoder::poll(){return impl_->poll();}\r
213 bool video_decoder::ready() const{return impl_->ready();}\r
214 double video_decoder::fps() const{return impl_->fps();}\r
215 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
216 size_t video_decoder::width() const{return impl_->width_;}\r
217 size_t video_decoder::height() const{return impl_->height_;}\r
218 \r
219 }}