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