]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/video/video_decoder.cpp
2.1.0: -ffmpeg_producer: seek while paused.
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "video_decoder.h"\r
25 \r
26 #include "../util/util.h"\r
27 \r
28 #include "../../ffmpeg_error.h"\r
29 \r
30 #include <common/log.h>\r
31 #include <core/frame/frame_transform.h>\r
32 #include <core/frame/frame_factory.h>\r
33 \r
34 #include <boost/range/algorithm_ext/push_back.hpp>\r
35 #include <boost/filesystem.hpp>\r
36 \r
37 #include <queue>\r
38 \r
39 #if defined(_MSC_VER)\r
40 #pragma warning (push)\r
41 #pragma warning (disable : 4244)\r
42 #endif\r
43 extern "C" \r
44 {\r
45         #include <libavcodec/avcodec.h>\r
46         #include <libavformat/avformat.h>\r
47 }\r
48 #if defined(_MSC_VER)\r
49 #pragma warning (pop)\r
50 #endif\r
51 \r
52 namespace caspar { namespace ffmpeg {\r
53         \r
54 struct video_decoder::impl : boost::noncopyable\r
55 {\r
56         monitor::basic_subject                                  event_subject_;\r
57         int                                                                             index_;\r
58         const std::shared_ptr<AVCodecContext>   codec_context_;\r
59 \r
60         std::queue<spl::shared_ptr<AVPacket>>   packets_;\r
61         \r
62         const uint32_t                                                  nb_frames_;\r
63 \r
64         const int                                                               width_;\r
65         const int                                                               height_;\r
66         bool                                                                    is_progressive_;\r
67 \r
68         tbb::atomic<uint32_t>                                   file_frame_number_;\r
69 \r
70 public:\r
71         explicit impl() \r
72                 : nb_frames_(0)\r
73                 , width_(0)\r
74                 , height_(0)\r
75                 , is_progressive_(true)\r
76         {\r
77                 file_frame_number_ = 0;\r
78         }\r
79 \r
80         explicit impl(const spl::shared_ptr<AVFormatContext>& context) \r
81                 : codec_context_(open_codec(*context, AVMEDIA_TYPE_VIDEO, index_))\r
82                 , nb_frames_(static_cast<uint32_t>(context->streams[index_]->nb_frames))\r
83                 , width_(codec_context_->width)\r
84                 , height_(codec_context_->height)\r
85         {\r
86                 file_frame_number_ = 0;\r
87         }\r
88 \r
89         void push(const std::shared_ptr<AVPacket>& packet)\r
90         {\r
91                 if(!packet)\r
92                         return;\r
93 \r
94                 if(packet->stream_index == index_ || packet->data == nullptr)\r
95                         packets_.push(spl::make_shared_ptr(packet));\r
96         }\r
97 \r
98         std::shared_ptr<AVFrame> poll()\r
99         {               \r
100                 if(packets_.empty())\r
101                         return nullptr;\r
102                 \r
103                 auto packet = packets_.front();\r
104                 \r
105                 if(!codec_context_)             \r
106                 {\r
107                         packets_.pop();\r
108                         return packet->data == nullptr ? flush_video() : empty_video();\r
109                 }\r
110                 else\r
111                 {\r
112                         if(packet->data == nullptr)\r
113                         {                       \r
114                                 if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
115                                 {\r
116                                         auto video = decode(*packet);\r
117                                         if(video)\r
118                                                 return video;\r
119                                 }\r
120                                         \r
121                                 packets_.pop();\r
122                                 file_frame_number_ = static_cast<uint32_t>(packet->pos);\r
123                                 avcodec_flush_buffers(codec_context_.get());\r
124                                 return flush_video();   \r
125                         }\r
126                         \r
127                         packets_.pop();\r
128                         return decode(*packet);\r
129                 }\r
130                 \r
131         }\r
132 \r
133         std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
134         {\r
135                 std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
136 \r
137                 int frame_finished = 0;\r
138                 THROW_ON_ERROR2(avcodec_decode_video2(codec_context_.get(), decoded_frame.get(), &frame_finished, &pkt), "[video_decocer]");\r
139                 \r
140                 // If a decoder consumes less then the whole packet then something is wrong\r
141                 // that might be just harmless padding at the end, or a problem with the\r
142                 // AVParser or demuxer which puted more then one frame in a AVPacket.\r
143 \r
144                 if(frame_finished == 0) \r
145                         return nullptr;\r
146 \r
147                 is_progressive_ = !decoded_frame->interlaced_frame;\r
148 \r
149                 if(decoded_frame->repeat_pict > 0)\r
150                         CASPAR_LOG(warning) << "[video_decoder] Field repeat_pict not implemented.";\r
151                                 \r
152                 event_subject_  << monitor::event("file/video/width")   % width_\r
153                                                 << monitor::event("file/video/height")  % height_\r
154                                                 << monitor::event("file/video/field")   % u8(!decoded_frame->interlaced_frame ? "progressive" : (decoded_frame->top_field_first ? "upper" : "lower"))\r
155                                                 << monitor::event("file/video/codec")   % u8(codec_context_->codec->long_name);\r
156 \r
157                 ++file_frame_number_;\r
158 \r
159                 return decoded_frame;\r
160         }\r
161         \r
162         bool ready() const\r
163         {\r
164                 return !codec_context_ || !packets_.empty();\r
165         }\r
166 \r
167         void clear()\r
168         {\r
169                 while(!packets_.empty())\r
170                         packets_.pop();\r
171         }\r
172 \r
173         uint32_t nb_frames() const\r
174         {\r
175                 return std::max<uint32_t>(nb_frames_, file_frame_number_);\r
176         }\r
177 \r
178         std::wstring print() const\r
179         {               \r
180                 return L"[video-decoder] " + u16(codec_context_->codec->long_name);\r
181         }\r
182 };\r
183 \r
184 video_decoder::video_decoder() : impl_(new impl()){}\r
185 video_decoder::video_decoder(const spl::shared_ptr<AVFormatContext>& context) : impl_(new impl(context)){}\r
186 video_decoder::video_decoder(video_decoder&& other) : impl_(std::move(other.impl_)){}\r
187 video_decoder& video_decoder::operator=(video_decoder&& other){impl_ = std::move(other.impl_); return *this;}\r
188 void video_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
189 std::shared_ptr<AVFrame> video_decoder::poll(){return impl_->poll();}\r
190 bool video_decoder::ready() const{return impl_->ready();}\r
191 int video_decoder::width() const{return impl_->width_;}\r
192 int video_decoder::height() const{return impl_->height_;}\r
193 uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}\r
194 uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}\r
195 bool    video_decoder::is_progressive() const{return impl_->is_progressive_;}\r
196 std::wstring video_decoder::print() const{return impl_->print();}\r
197 void video_decoder::clear(){impl_->clear();}\r
198 void video_decoder::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}\r
199 void video_decoder::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}\r
200 \r
201 }}