]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/video/video_decoder.cpp
2.0.2: MAYOR CHANGES => needs testing
[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/util.h"\r
25 \r
26 #include "../../ffmpeg_error.h"\r
27 \r
28 #include <core/producer/frame/frame_transform.h>\r
29 #include <core/producer/frame/frame_factory.h>\r
30 \r
31 #include <boost/range/algorithm_ext/push_back.hpp>\r
32 #include <boost/filesystem.hpp>\r
33 \r
34 #include <queue>\r
35 \r
36 #if defined(_MSC_VER)\r
37 #pragma warning (push)\r
38 #pragma warning (disable : 4244)\r
39 #endif\r
40 extern "C" \r
41 {\r
42         #include <libavcodec/avcodec.h>\r
43         #include <libavformat/avformat.h>\r
44 }\r
45 #if defined(_MSC_VER)\r
46 #pragma warning (pop)\r
47 #endif\r
48 \r
49 namespace caspar { namespace ffmpeg {\r
50         \r
51 struct video_decoder::implementation : boost::noncopyable\r
52 {\r
53         int                                                                             index_;\r
54         const safe_ptr<AVCodecContext>                  codec_context_;\r
55 \r
56         std::queue<safe_ptr<AVPacket>>                  packets_;\r
57         \r
58         const double                                                    fps_;\r
59         const int64_t                                                   nb_frames_;\r
60 \r
61         const size_t                                                    width_;\r
62         const size_t                                                    height_;\r
63         bool                                                                    is_progressive_;\r
64 \r
65 public:\r
66         explicit implementation(const safe_ptr<AVFormatContext>& context) \r
67                 : codec_context_(open_codec(*context, AVMEDIA_TYPE_VIDEO, index_))\r
68                 , fps_(static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num))\r
69                 , nb_frames_(context->streams[index_]->nb_frames)\r
70                 , width_(codec_context_->width)\r
71                 , height_(codec_context_->height)\r
72         {\r
73                 CASPAR_LOG(debug) << "[video_decoder] " << context->streams[index_]->codec->codec->long_name;\r
74         }\r
75 \r
76         void push(const std::shared_ptr<AVPacket>& packet)\r
77         {\r
78                 if(!packet)\r
79                         return;\r
80 \r
81                 if(packet->stream_index == index_ || packet == flush_packet())\r
82                         packets_.push(make_safe_ptr(packet));\r
83         }\r
84 \r
85         std::shared_ptr<AVFrame> poll()\r
86         {               \r
87                 if(packets_.empty())\r
88                         return nullptr;\r
89                 \r
90                 auto packet = packets_.front();\r
91                                         \r
92                 if(packet == flush_packet())\r
93                 {                       \r
94                         if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
95                         {\r
96                                 AVPacket pkt;\r
97                                 av_init_packet(&pkt);\r
98                                 pkt.data = nullptr;\r
99                                 pkt.size = 0;\r
100 \r
101                                 auto video = decode(pkt);\r
102                                 if(video)\r
103                                         return video;\r
104                         }\r
105                 \r
106                         packets_.pop();\r
107                         avcodec_flush_buffers(codec_context_.get());\r
108                         return flush_video();   \r
109                 }\r
110                         \r
111                 packets_.pop();\r
112                 return decode(*packet);\r
113         }\r
114 \r
115         std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
116         {\r
117                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
118 \r
119                 int frame_finished = 0;\r
120                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");\r
121                 \r
122                 // If a decoder consumes less then the whole packet then something is wrong\r
123                 // that might be just harmless padding at the end, or a problem with the\r
124                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
125 \r
126                 if(frame_finished == 0) \r
127                         return nullptr;\r
128 \r
129                 is_progressive_ = !decoded_frame->interlaced_frame;\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                 return decoded_frame;\r
135         }\r
136         \r
137         bool ready() const\r
138         {\r
139                 return packets_.size() > 10;\r
140         }\r
141         \r
142         double fps() const\r
143         {\r
144                 return fps_;\r
145         }\r
146 };\r
147 \r
148 video_decoder::video_decoder(const safe_ptr<AVFormatContext>& context) : impl_(new implementation(context)){}\r
149 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
150 std::shared_ptr<AVFrame> video_decoder::poll(){return impl_->poll();}\r
151 bool video_decoder::ready() const{return impl_->ready();}\r
152 double video_decoder::fps() const{return impl_->fps();}\r
153 int64_t video_decoder::nb_frames() const{return impl_->nb_frames_;}\r
154 size_t video_decoder::width() const{return impl_->width_;}\r
155 size_t video_decoder::height() const{return impl_->height_;}\r
156 bool    video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
157 \r
158 }}