]> 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 #include <common/concurrency/message.h>\r
31 \r
32 #if defined(_MSC_VER)\r
33 #pragma warning (push)\r
34 #pragma warning (disable : 4244)\r
35 #endif\r
36 extern "C" \r
37 {\r
38         #include <libavcodec/avcodec.h>\r
39         #include <libavformat/avformat.h>\r
40 }\r
41 #if defined(_MSC_VER)\r
42 #pragma warning (pop)\r
43 #endif\r
44 \r
45 #include <connect.h>\r
46 #include <semaphore.h>\r
47 \r
48 using namespace Concurrency;\r
49 \r
50 namespace caspar { namespace ffmpeg {\r
51         \r
52 struct video_decoder::implementation : public Concurrency::agent, boost::noncopyable\r
53 {       \r
54         int                                                                             index_;\r
55         safe_ptr<AVCodecContext>                                codec_context_; \r
56         const double                                                    fps_;\r
57         const int64_t                                                   nb_frames_;\r
58         const size_t                                                    width_;\r
59         const size_t                                                    height_;\r
60 \r
61         bool                                                                    is_progressive_;\r
62         \r
63         overwrite_buffer<bool>                                  is_running_;\r
64         unbounded_buffer<safe_ptr<AVPacket>>    source_;\r
65         ITarget<safe_ptr<AVFrame>>&                             target_;\r
66         \r
67         safe_ptr<semaphore>                                             semaphore_;\r
68 \r
69 public:\r
70         explicit implementation(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context) \r
71                 : codec_context_(open_codec(context, AVMEDIA_TYPE_VIDEO, index_))\r
72                 , fps_(static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num))\r
73                 , nb_frames_(context.streams[index_]->nb_frames)\r
74                 , width_(codec_context_->width)\r
75                 , height_(codec_context_->height)\r
76                 , is_progressive_(true)\r
77                 , source_([this](const safe_ptr<AVPacket>& packet){return packet->stream_index == index_;})\r
78                 , target_(target)\r
79                 , semaphore_(make_safe<Concurrency::semaphore>(1)) // IMPORTANT: Must be 1 since avcodec_decode_video2 reuses frame memory.\r
80         {               \r
81                 CASPAR_LOG(debug) << "[video_decoder] " << context.streams[index_]->codec->codec->long_name;\r
82                 \r
83                 Concurrency::connect(source, source_);\r
84 \r
85                 start();\r
86         }\r
87 \r
88         ~implementation()\r
89         {\r
90                 send(is_running_, false);\r
91                 semaphore_->release();\r
92                 agent::wait(this);\r
93         }\r
94 \r
95         virtual void run()\r
96         {\r
97                 try\r
98                 {\r
99                         send(is_running_, true);\r
100                         while(is_running_.value())\r
101                         {\r
102                                 auto packet = receive(source_);\r
103                         \r
104                                 if(packet == loop_packet(index_))\r
105                                 {\r
106                                         send(target_, loop_video());\r
107                                         continue;\r
108                                 }\r
109 \r
110                                 if(packet == eof_packet(index_))\r
111                                         break;\r
112 \r
113                                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), [this](AVFrame* frame)\r
114                                 {\r
115                                         av_free(frame);\r
116                                         semaphore_->release();\r
117                                 });\r
118                                 semaphore_->acquire();\r
119 \r
120                                 int frame_finished = 0;\r
121                                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, packet.get()), "[video_decocer]");\r
122 \r
123                                 // 1 packet <=> 1 frame.\r
124                                 // If a decoder consumes less then the whole packet then something is wrong\r
125                                 // that might be just harmless padding at the end, or a problem with the\r
126                                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
127 \r
128                                 if(frame_finished == 0) \r
129                                         continue;\r
130 \r
131                                 if(decoded_frame->repeat_pict > 0)\r
132                                         CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
133                 \r
134                                 is_progressive_ = decoded_frame->interlaced_frame == 0;\r
135                                 \r
136                                 send(target_, make_safe_ptr(decoded_frame));\r
137                                 Concurrency::wait(2);\r
138                         }\r
139                 }\r
140                 catch(...)\r
141                 {\r
142                         CASPAR_LOG_CURRENT_EXCEPTION();\r
143                 }\r
144                 \r
145                 send(is_running_, false),\r
146                 send(target_, eof_video());\r
147 \r
148                 done();\r
149         }\r
150                 \r
151         double fps() const\r
152         {\r
153                 return fps_;\r
154         }\r
155 };\r
156 \r
157 video_decoder::video_decoder(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context) \r
158         : impl_(new implementation(source, target, context))\r
159 {\r
160 }\r
161 \r
162 double video_decoder::fps() const{return impl_->fps();}\r
163 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
164 size_t video_decoder::width() const{return impl_->width_;}\r
165 size_t video_decoder::height() const{return impl_->height_;}\r
166 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
167 \r
168 }}