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