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