]> 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 \r
29 #include <core/producer/frame/basic_frame.h>\r
30 \r
31 #if defined(_MSC_VER)\r
32 #pragma warning (push)\r
33 #pragma warning (disable : 4244)\r
34 #endif\r
35 extern "C" \r
36 {\r
37         #include <libavcodec/avcodec.h>\r
38         #include <libavformat/avformat.h>\r
39 }\r
40 #if defined(_MSC_VER)\r
41 #pragma warning (pop)\r
42 #endif\r
43 \r
44 namespace caspar { namespace ffmpeg {\r
45         \r
46 struct video_decoder::implementation : public Concurrency::agent, boost::noncopyable\r
47 {\r
48         video_decoder::source_t&                                source_;\r
49         video_decoder::target_t&                                target_;\r
50 \r
51         std::shared_ptr<AVCodecContext>                 codec_context_;\r
52         int                                                                             index_;\r
53         \r
54         double                                                                  fps_;\r
55         int64_t                                                                 nb_frames_;\r
56 \r
57         size_t                                                                  width_;\r
58         size_t                                                                  height_;\r
59         bool                                                                    is_progressive_;\r
60 \r
61         Concurrency::event                                              event_;\r
62 \r
63 public:\r
64         explicit implementation(video_decoder::source_t& source,\r
65                                                         video_decoder::target_t& target,\r
66                                                         const safe_ptr<AVFormatContext>& context,\r
67                                                         double fps) \r
68                 : source_(source)\r
69                 , target_(target)\r
70                 , fps_(fps)\r
71                 , nb_frames_(0)\r
72                 , width_(0)\r
73                 , height_(0)\r
74                 , is_progressive_(true)\r
75         {\r
76                 event_.set();\r
77 \r
78                 AVCodec* dec;\r
79                 index_ = THROW_ON_ERROR3(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0), "[video_decoder]", ffmpeg_stream_not_found);\r
80                 CASPAR_VERIFY(index_ > -1, ffmpeg_error());\r
81                                                 \r
82                 THROW_ON_ERROR2(avcodec_open(context->streams[index_]->codec, dec), "[video_decoder]");\r
83                 CASPAR_VERIFY(context->streams[index_]->codec, ffmpeg_error())\r
84                                                                 \r
85                 codec_context_.reset(context->streams[index_]->codec, 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                 width_  = codec_context_->width;\r
97                 height_ = codec_context_->height;\r
98 \r
99                 CASPAR_VERIFY(width_ > 0, ffmpeg_error());\r
100                 CASPAR_VERIFY(height_ > 0, ffmpeg_error());\r
101 \r
102                 start();\r
103         }\r
104         \r
105         ~implementation()\r
106         {\r
107                 agent::wait(this);\r
108         }\r
109         \r
110         virtual void run()\r
111         {\r
112                 try\r
113                 {\r
114                         while(true)\r
115                         {\r
116                                 auto packet = Concurrency::receive(source_, [this](const std::shared_ptr<AVPacket>& packet)\r
117                                 {\r
118                                         return packet && packet->stream_index == index_;\r
119                                 });\r
120 \r
121                                 //if(packet == eof_packet(index_) || packet == loop_packet(index_))\r
122                                 //{\r
123                                 //      if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
124                                 //      {\r
125                                 //              AVPacket pkt;\r
126                                 //              av_init_packet(&pkt);\r
127                                 //              pkt.data = nullptr;\r
128                                 //              pkt.size = 0;\r
129                                 //\r
130                                 //              while(decode(pkt)){}\r
131                                 //      }\r
132                                 //}\r
133                                                                 \r
134                                 if(packet == eof_packet(index_))                                \r
135                                         break;\r
136                                                                 \r
137                                 if(packet == loop_packet(index_))\r
138                                 {       \r
139                                         avcodec_flush_buffers(codec_context_.get());                                    \r
140                                         Concurrency::send(target_, loop_video());       \r
141                                 }\r
142                                 else if(packet->stream_index == index_)\r
143                                 {                                       \r
144                                         Concurrency::send(target_, decode(*packet));    \r
145                                 }\r
146                         }\r
147                 }\r
148                 catch(...)\r
149                 {\r
150                         CASPAR_LOG_CURRENT_EXCEPTION();\r
151                 }\r
152                 \r
153                 Concurrency::send(target_, eof_video());\r
154                                 \r
155                 done();\r
156         }\r
157 \r
158         std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
159         {\r
160                 event_.wait();\r
161 \r
162                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), [this](AVFrame* frame)\r
163                 {\r
164                         av_free(frame);\r
165                         event_.set();\r
166                 });\r
167 \r
168                 int frame_finished = 0;\r
169                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");\r
170                 event_.reset();\r
171 \r
172                 // If a decoder consumes less then the whole packet then something is wrong\r
173                 // that might be just harmless padding at the end, or a problem with the\r
174                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
175                 pkt.data = nullptr;\r
176                 pkt.size = 0;\r
177 \r
178                 if(frame_finished == 0) \r
179                         return nullptr;\r
180 \r
181                 if(decoded_frame->repeat_pict > 0)\r
182                         CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
183                 \r
184                 is_progressive_ = decoded_frame->interlaced_frame == 0;\r
185                                 \r
186                 return decoded_frame;\r
187         }\r
188                 \r
189         double fps() const\r
190         {\r
191                 return fps_;\r
192         }\r
193 };\r
194 \r
195 video_decoder::video_decoder(source_t& source,\r
196                                                          target_t& target,\r
197                                                          const safe_ptr<AVFormatContext>& context, \r
198                                                          double fps) \r
199         : impl_(new implementation(source, target, context, fps))\r
200 {\r
201 }\r
202 \r
203 double video_decoder::fps() const{return impl_->fps();}\r
204 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
205 size_t video_decoder::width() const{return impl_->width_;}\r
206 size_t video_decoder::height() const{return impl_->height_;}\r
207 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
208 \r
209 }}