]> 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/memory/memcpy.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 <tbb/scalable_allocator.h>\r
46 \r
47 #undef Yield\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         std::shared_ptr<AVCodecContext>                 codec_context_;\r
56         \r
57         double                                                                  fps_;\r
58         int64_t                                                                 nb_frames_;\r
59 \r
60         size_t                                                                  width_;\r
61         size_t                                                                  height_;\r
62         bool                                                                    is_progressive_;\r
63         \r
64         unbounded_buffer<video_decoder::source_element_t>       source_;\r
65         ITarget<video_decoder::target_element_t>&                       target_;\r
66         \r
67 public:\r
68         explicit implementation(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context) \r
69                 : codec_context_(open_codec(context, AVMEDIA_TYPE_VIDEO, index_))\r
70                 , fps_(static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num))\r
71                 , nb_frames_(context.streams[index_]->nb_frames)\r
72                 , width_(codec_context_->width)\r
73                 , height_(codec_context_->height)\r
74                 , is_progressive_(true)\r
75                 , source_([this](const video_decoder::source_element_t& element){return element.first->stream_index == index_;})\r
76                 , target_(target)\r
77         {               \r
78                 CASPAR_LOG(debug) << "[video_decoder] " << context.streams[index_]->codec->codec->long_name;\r
79                 \r
80                 CASPAR_VERIFY(width_ > 0, ffmpeg_error());\r
81                 CASPAR_VERIFY(height_ > 0, ffmpeg_error());\r
82 \r
83                 source.link_target(&source_);\r
84 \r
85                 start();\r
86         }\r
87 \r
88         ~implementation()\r
89         {\r
90                 agent::wait(this);\r
91         }\r
92         \r
93         std::shared_ptr<AVFrame> decode(AVPacket& packet)\r
94         {\r
95                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
96 \r
97                 int frame_finished = 0;\r
98                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &packet), "[video_decocer]");\r
99 \r
100                 // 1 packet <=> 1 frame.\r
101                 // If a decoder consumes less then the whole packet then something is wrong\r
102                 // that might be just harmless padding at the end, or a problem with the\r
103                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
104 \r
105                 if(frame_finished == 0) \r
106                         return nullptr;\r
107                                 \r
108                 if(decoded_frame->repeat_pict > 0)\r
109                         CASPAR_LOG(warning) << "[video_decoder]: Field repeat_pict not implemented.";\r
110 \r
111                 return decoded_frame;\r
112         }\r
113 \r
114         virtual void run()\r
115         {\r
116                 try\r
117                 {\r
118                         while(true)\r
119                         {\r
120                                 auto element = receive(source_);\r
121                                 auto packet = element.first;\r
122                         \r
123                                 if(packet == loop_packet(index_))\r
124                                 {                                       \r
125                                         if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
126                                         {\r
127                                                 AVPacket pkt;\r
128                                                 av_init_packet(&pkt);\r
129                                                 pkt.data = nullptr;\r
130                                                 pkt.size = 0;\r
131 \r
132                                                 for(auto decoded_frame = decode(pkt); decoded_frame; decoded_frame = decode(pkt))\r
133                                                 {\r
134                                                         send(target_, target_element_t(dup_frame(make_safe_ptr(decoded_frame)), element.second));\r
135                                                         Context::Yield();\r
136                                                 }\r
137                                         }\r
138 \r
139                                         avcodec_flush_buffers(codec_context_.get());\r
140                                         send(target_, target_element_t(loop_video(), ticket_t()));\r
141                                         continue;\r
142                                 }\r
143 \r
144                                 if(packet == eof_packet(index_))\r
145                                         break;\r
146 \r
147                                 auto decoded_frame = decode(*packet);\r
148                                 if(!decoded_frame)\r
149                                         continue;\r
150                 \r
151                                 is_progressive_ = decoded_frame->interlaced_frame == 0;\r
152                                 \r
153                                 // C-TODO: Avoid duplication.\r
154                                 // Need to dupliace frame data since avcodec_decode_video2 reuses it.\r
155                                 send(target_, target_element_t(dup_frame(make_safe_ptr(decoded_frame)), element.second));                               \r
156                                 Context::Yield();\r
157                         }\r
158                 }\r
159                 catch(...)\r
160                 {\r
161                         CASPAR_LOG_CURRENT_EXCEPTION();\r
162                 }\r
163                 \r
164                 send(target_, target_element_t(eof_video(), ticket_t()));\r
165 \r
166                 done();\r
167         }\r
168 \r
169         safe_ptr<AVFrame> dup_frame(const safe_ptr<AVFrame>& frame)\r
170         {\r
171                 auto desc = get_pixel_format_desc(static_cast<PixelFormat>(frame->format), frame->width, frame->height);\r
172 \r
173                 auto count = desc.planes.size();\r
174                 std::array<uint8_t*, 4> org_ptrs;\r
175                 std::array<uint8_t*, 4> real_ptrs; // We need to store the "real" pointers, due to alignment hack.\r
176                 parallel_for<size_t>(0, count, [&](size_t n)\r
177                 {\r
178                         auto size               = frame->linesize[n]*desc.planes[n].height;\r
179                         org_ptrs[n]             = frame->data[n];\r
180                         real_ptrs[n]    = reinterpret_cast<uint8_t*>(scalable_aligned_malloc(size+16, 32)); // Allocate 16 byte extra for alignment hack.\r
181                         frame->data[n]  = reinterpret_cast<uint8_t*>(fast_memcpy_w_align_hack(real_ptrs[n], org_ptrs[n], size));\r
182                 });\r
183 \r
184                 return safe_ptr<AVFrame>(frame.get(), [frame, org_ptrs, real_ptrs, count](AVFrame*)\r
185                 {\r
186                         for(size_t n = 0; n < count; ++n)\r
187                         {\r
188                                 scalable_aligned_free(real_ptrs[n]);\r
189                                 frame->data[n] = org_ptrs[n];\r
190                         }\r
191                 });\r
192         }\r
193                 \r
194         double fps() const\r
195         {\r
196                 return fps_;\r
197         }\r
198 };\r
199 \r
200 video_decoder::video_decoder(video_decoder::source_t& source, video_decoder::target_t& target, AVFormatContext& context) \r
201         : impl_(new implementation(source, target, context))\r
202 {\r
203 }\r
204 \r
205 double video_decoder::fps() const{return impl_->fps();}\r
206 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
207 size_t video_decoder::width() const{return impl_->width_;}\r
208 size_t video_decoder::height() const{return impl_->height_;}\r
209 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
210 \r
211 }}