]> 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 \r
70         double                                                                  fps_;\r
71         int64_t                                                                 nb_frames_;\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                 , fps_(frame_factory_->get_video_format_desc().fps)\r
78                 , nb_frames_(0)\r
79         {\r
80                 AVCodec* dec;\r
81                 index_ = av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);\r
82 \r
83                 if(index_ < 0)\r
84                         return;\r
85                         \r
86                 int errn = tbb_avcodec_open(context->streams[index_]->codec, dec);\r
87                 if(errn < 0)\r
88                         return;\r
89                                 \r
90                 codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\r
91                 \r
92                 // Some files give an invalid time_base numerator, try to fix it.\r
93                 if(codec_context_ && codec_context_->time_base.num == 1)\r
94                         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
95                 \r
96                 nb_frames_ = context->streams[index_]->nb_frames;\r
97                 if(nb_frames_ == 0)\r
98                         nb_frames_ = context->streams[index_]->duration;// * context->streams[index_]->time_base.den;\r
99 \r
100                 fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);\r
101                 if(double_rate(filter))\r
102                         fps_ *= 2;\r
103         }\r
104 \r
105         void push(const std::shared_ptr<AVPacket>& packet)\r
106         {\r
107                 if(!codec_context_)\r
108                         return;\r
109 \r
110                 if(packet && packet->stream_index != index_)\r
111                         return;\r
112 \r
113                 packet_buffer_.push(packet);\r
114         }\r
115 \r
116         std::vector<std::shared_ptr<AVFrame>> poll()\r
117         {               \r
118                 std::vector<std::shared_ptr<AVFrame>> result;\r
119 \r
120                 if(!codec_context_)\r
121                 {\r
122                         std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);\r
123                         frame->data[0] = nullptr;\r
124                         result.push_back(frame);\r
125                 }\r
126                 else if(!packet_buffer_.empty())\r
127                 {\r
128                         std::shared_ptr<AVFrame> frame;\r
129 \r
130                         auto packet = packet_buffer_.front();\r
131 \r
132                         bool eof = false;\r
133                 \r
134                         if(packet)\r
135                         {\r
136                                 frame = decode(*packet);        \r
137                                 if(packet->size == 0)\r
138                                         packet_buffer_.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                                         frame = decode(pkt);\r
149                                 }\r
150 \r
151                                 if(!frame)\r
152                                 {                                       \r
153                                         packet_buffer_.pop();\r
154                                         avcodec_flush_buffers(codec_context_.get());\r
155                                         eof = true;\r
156                                 }\r
157                         }\r
158                         \r
159                         std::vector<safe_ptr<AVFrame>> av_frames;\r
160 \r
161                         if(filter_)                     \r
162                                 av_frames = filter_->execute(frame);                    \r
163                         else if(frame)\r
164                                 av_frames.push_back(make_safe(frame));\r
165                                                 \r
166                         BOOST_FOREACH(auto& frame, av_frames)\r
167                                 result.push_back(frame);\r
168 \r
169                         if(eof)\r
170                                 result.push_back(nullptr);\r
171                 }\r
172                 \r
173                 return result;\r
174         }\r
175 \r
176         std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
177         {\r
178                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
179 \r
180                 int frame_finished = 0;\r
181                 const int ret = avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt);\r
182                 \r
183                 if(ret < 0)\r
184                 {\r
185                         BOOST_THROW_EXCEPTION(\r
186                                 invalid_operation() <<\r
187                                 msg_info(av_error_str(ret)) <<\r
188                                 boost::errinfo_api_function("avcodec_decode_video") <<\r
189                                 boost::errinfo_errno(AVUNERROR(ret)));\r
190                 }\r
191                 \r
192                 // if a decoder consumes less then the whole packet then something is wrong\r
193                 // that might be just harmless padding at the end or a problem with the\r
194                 // AVParser or demuxer which puted more then one frame in a AVPacket\r
195                 pkt.data = nullptr;\r
196                 pkt.size = 0;\r
197 \r
198                 if(frame_finished != 0) \r
199                 {\r
200                         if(decoded_frame->repeat_pict != 0)\r
201                                 CASPAR_LOG(warning) << "video_decoder: repeat_pict not implemented.";\r
202                         return decoded_frame;\r
203                 }\r
204 \r
205                 return nullptr;\r
206         }\r
207         \r
208         bool ready() const\r
209         {\r
210                 return !codec_context_ || !packet_buffer_.empty();\r
211         }\r
212         \r
213         core::video_mode::type mode()\r
214         {\r
215                 if(!codec_context_)\r
216                         return frame_factory_->get_video_format_desc().mode;\r
217 \r
218                 return mode_;\r
219         }\r
220 \r
221         double fps() const\r
222         {\r
223                 return fps_;\r
224         }\r
225 };\r
226 \r
227 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
228 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
229 std::vector<std::shared_ptr<AVFrame>> video_decoder::poll(){return impl_->poll();}\r
230 bool video_decoder::ready() const{return impl_->ready();}\r
231 core::video_mode::type video_decoder::mode(){return impl_->mode();}\r
232 double video_decoder::fps() const{return impl_->fps();}\r
233 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
234 }