]> 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 #include "../util.h"\r
24 #include "../filter/filter.h"\r
25 \r
26 #include "../../ffmpeg_error.h"\r
27 #include "../../tbb_avcodec.h"\r
28 \r
29 #include <common/memory/memcpy.h>\r
30 \r
31 #include <core/video_format.h>\r
32 #include <core/producer/frame/basic_frame.h>\r
33 #include <core/mixer/write_frame.h>\r
34 #include <core/producer/frame/image_transform.h>\r
35 #include <core/producer/frame/frame_factory.h>\r
36 #include <core/producer/color/color_producer.h>\r
37 \r
38 #include <tbb/task_group.h>\r
39 \r
40 #include <boost/range/algorithm_ext.hpp>\r
41 \r
42 #if defined(_MSC_VER)\r
43 #pragma warning (push)\r
44 #pragma warning (disable : 4244)\r
45 #endif\r
46 extern "C" \r
47 {\r
48         #define __STDC_CONSTANT_MACROS\r
49         #define __STDC_LIMIT_MACROS\r
50         #include <libavformat/avformat.h>\r
51         #include <libavcodec/avcodec.h>\r
52 }\r
53 #if defined(_MSC_VER)\r
54 #pragma warning (pop)\r
55 #endif\r
56 \r
57 namespace caspar {\r
58                 \r
59 struct video_decoder::implementation : boost::noncopyable\r
60 {\r
61         const safe_ptr<core::frame_factory>             frame_factory_;\r
62         std::shared_ptr<AVCodecContext>                 codec_context_;\r
63         int                                                                             index_;\r
64         core::video_mode::type                                  mode_;\r
65 \r
66         std::queue<std::shared_ptr<AVPacket>>   packet_buffer_;\r
67 \r
68         std::unique_ptr<filter>                                 filter_;\r
69         tbb::task_group                                                 filter_tasks_;\r
70 \r
71         double                                                                  fps_;\r
72 public:\r
73         explicit implementation(const std::shared_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) \r
74                 : frame_factory_(frame_factory)\r
75                 , mode_(core::video_mode::invalid)\r
76                 //, filter_(filter.empty() ? nullptr : new caspar::filter(filter))\r
77         {\r
78                 AVCodec* dec;\r
79                 index_ = av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);\r
80 \r
81                 if(index_ < 0)\r
82                         return;\r
83                         \r
84                 int errn = tbb_avcodec_open(context->streams[index_]->codec, dec);\r
85                 if(errn < 0)\r
86                         return;\r
87                                 \r
88                 codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\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                 fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);\r
95                 if(double_rate(filter))\r
96                         fps_ *= 2;\r
97         }\r
98                 \r
99         void push(const std::shared_ptr<AVPacket>& packet)\r
100         {\r
101                 if(!codec_context_)\r
102                         return;\r
103 \r
104                 if(packet && packet->stream_index != index_)\r
105                         return;\r
106 \r
107                 packet_buffer_.push(packet);\r
108         }\r
109 \r
110         std::vector<safe_ptr<core::write_frame>> poll()\r
111         {               \r
112                 std::vector<safe_ptr<core::write_frame>> result;\r
113 \r
114                 if(!codec_context_)\r
115                         result.push_back(make_safe<core::write_frame>());\r
116                 else if(!packet_buffer_.empty())\r
117                 {\r
118                         std::vector<std::shared_ptr<AVFrame>> av_frames;\r
119 \r
120                         auto packet = std::move(packet_buffer_.front());\r
121                         packet_buffer_.pop();\r
122                 \r
123                         if(!packet) // eof\r
124                         {                               \r
125                                 if(codec_context_->codec->capabilities | CODEC_CAP_DELAY)\r
126                                 {\r
127                                         // FIXME: This might cause bad performance.\r
128                                         AVPacket pkt = {0};\r
129                                         av_frames.push_back(decode_frame(pkt));\r
130                                 }\r
131 \r
132                                 avcodec_flush_buffers(codec_context_.get());\r
133                         }\r
134                         else\r
135                         {\r
136                                 av_frames.push_back(decode_frame(*packet));                             \r
137                         }\r
138 \r
139                         if(filter_)\r
140                         {\r
141                                 filter_tasks_.wait();\r
142                                 \r
143                                 av_frames.clear();\r
144                                 boost::range::push_back(av_frames, filter_->poll());\r
145 \r
146                                 filter_tasks_.run([=]\r
147                                 {\r
148                                         BOOST_FOREACH(auto& frame, av_frames)                           \r
149                                                 filter_->push(frame);\r
150                                 });\r
151                         }\r
152                                                 \r
153                         BOOST_FOREACH(auto& frame, av_frames)\r
154                                 result.push_back(make_write_frame(this, make_safe(frame), frame_factory_));\r
155                 }\r
156                 \r
157                 return result;\r
158         }\r
159 \r
160         std::shared_ptr<AVFrame> decode_frame(AVPacket& packet)\r
161         {\r
162                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
163 \r
164                 int frame_finished = 0;\r
165                 const int errn = avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &packet);\r
166                 \r
167                 if(errn < 0)\r
168                 {\r
169                         BOOST_THROW_EXCEPTION(\r
170                                 invalid_operation() <<\r
171                                 msg_info(av_error_str(errn)) <<\r
172                                 boost::errinfo_api_function("avcodec_decode_video") <<\r
173                                 boost::errinfo_errno(AVUNERROR(errn)));\r
174                 }\r
175 \r
176                 if(frame_finished == 0) \r
177                         decoded_frame.reset();\r
178 \r
179                 return decoded_frame;\r
180         }\r
181 \r
182         bool ready() const\r
183         {\r
184                 return !codec_context_ || !packet_buffer_.empty();\r
185         }\r
186         \r
187         core::video_mode::type mode()\r
188         {\r
189                 if(!codec_context_)\r
190                         return frame_factory_->get_video_format_desc().mode;\r
191 \r
192                 return mode_;\r
193         }\r
194 \r
195         double fps() const\r
196         {\r
197                 return fps_;\r
198         }\r
199 };\r
200 \r
201 video_decoder::video_decoder(const std::shared_ptr<AVFormatContext>& context, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter) : impl_(new implementation(context, frame_factory, filter)){}\r
202 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
203 std::vector<safe_ptr<core::write_frame>> video_decoder::poll(){return impl_->poll();}\r
204 bool video_decoder::ready() const{return impl_->ready();}\r
205 core::video_mode::type video_decoder::mode(){return impl_->mode();}\r
206 double video_decoder::fps() const{return impl_->fps();}\r
207 }