]> 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 #include <connect.h>\r
45 #include <semaphore.h>\r
46 \r
47 using namespace Concurrency;\r
48 \r
49 namespace caspar { namespace ffmpeg {\r
50         \r
51 struct video_decoder::implementation : public Concurrency::agent, boost::noncopyable\r
52 {       \r
53         int                                                                             index_;\r
54         std::shared_ptr<AVCodecContext>                 codec_context_;\r
55         \r
56         double                                                                  fps_;\r
57         int64_t                                                                 nb_frames_;\r
58 \r
59         size_t                                                                  width_;\r
60         size_t                                                                  height_;\r
61         bool                                                                    is_progressive_;\r
62         \r
63         overwrite_buffer<bool>                                  is_running_;\r
64         unbounded_buffer<packet_message_t>              source_;\r
65         ITarget<video_message_t>&                               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 packet_message_t& message)\r
78                         {\r
79                                 return message->payload && message->payload->stream_index == index_;\r
80                         })\r
81                 , target_(target)\r
82                 , semaphore_(make_safe<Concurrency::semaphore>(1))\r
83         {               \r
84                 CASPAR_LOG(debug) << "[video_decoder] " << context.streams[index_]->codec->codec->long_name;\r
85                 \r
86                 CASPAR_VERIFY(width_ > 0, ffmpeg_error());\r
87                 CASPAR_VERIFY(height_ > 0, ffmpeg_error());\r
88 \r
89                 Concurrency::connect(source, source_);\r
90 \r
91                 start();\r
92         }\r
93 \r
94         ~implementation()\r
95         {\r
96                 send(is_running_, false);\r
97                 agent::wait(this);\r
98         }\r
99 \r
100         virtual void run()\r
101         {\r
102                 try\r
103                 {\r
104                         send(is_running_, true);\r
105                         while(is_running_.value())\r
106                         {\r
107                                 auto message = receive(source_);\r
108                                 auto packet = message->payload;\r
109                         \r
110                                 if(!packet)\r
111                                         continue;\r
112 \r
113                                 if(packet == loop_packet(index_))\r
114                                 {\r
115                                         send(target_, make_message(loop_video()));\r
116                                         continue;\r
117                                 }\r
118 \r
119                                 if(packet == eof_packet(index_))\r
120                                         break;\r
121 \r
122                                 token token(semaphore_);\r
123                                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), [this, token](AVFrame* frame)\r
124                                 {\r
125                                         av_free(frame);\r
126                                 });\r
127 \r
128                                 int frame_finished = 0;\r
129                                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, packet.get()), "[video_decocer]");\r
130 \r
131                                 // 1 packet <=> 1 frame.\r
132                                 // If a decoder consumes less then the whole packet then something is wrong\r
133                                 // that might be just harmless padding at the end, or a problem with the\r
134                                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
135 \r
136                                 if(frame_finished == 0) \r
137                                         continue;\r
138 \r
139                                 if(decoded_frame->repeat_pict > 0)\r
140                                         CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
141                 \r
142                                 is_progressive_ = decoded_frame->interlaced_frame == 0;\r
143                                 \r
144                                 send(target_, make_message(decoded_frame, message->token));\r
145                                 Concurrency::wait(10);\r
146                         }\r
147                 }\r
148                 catch(...)\r
149                 {\r
150                         CASPAR_LOG_CURRENT_EXCEPTION();\r
151                 }\r
152                 \r
153                 send(is_running_, false),\r
154                 send(target_, make_message(eof_video()));\r
155 \r
156                 done();\r
157         }\r
158                 \r
159         double fps() const\r
160         {\r
161                 return fps_;\r
162         }\r
163 };\r
164 \r
165 video_decoder::video_decoder(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context) \r
166         : impl_(new implementation(source, target, context))\r
167 {\r
168 }\r
169 \r
170 double video_decoder::fps() const{return impl_->fps();}\r
171 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
172 size_t video_decoder::width() const{return impl_->width_;}\r
173 size_t video_decoder::height() const{return impl_->height_;}\r
174 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
175 \r
176 }}