]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/filter.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / ffmpeg / producer / filter / filter.cpp
1 #include "../../stdafx.h"\r
2 \r
3 #include "filter.h"\r
4 \r
5 #include "parallel_yadif.h"\r
6 \r
7 #include "../../ffmpeg_error.h"\r
8 \r
9 #include <common/exception/exceptions.h>\r
10 \r
11 #include <boost/assign.hpp>\r
12 \r
13 #include <cstdio>\r
14 #include <sstream>\r
15 \r
16 #if defined(_MSC_VER)\r
17 #pragma warning (push)\r
18 #pragma warning (disable : 4244)\r
19 #endif\r
20 extern "C" \r
21 {\r
22         #include <libavutil/avutil.h>\r
23         #include <libavutil/imgutils.h>\r
24         #include <libavfilter/avfilter.h>\r
25         #include <libavfilter/avcodec.h>\r
26         #include <libavfilter/avfiltergraph.h>\r
27         #include <libavfilter/buffersink.h>\r
28         #include <libavfilter/vsrc_buffer.h>\r
29 }\r
30 #if defined(_MSC_VER)\r
31 #pragma warning (pop)\r
32 #endif\r
33 \r
34 namespace caspar { namespace ffmpeg {\r
35         \r
36 struct filter::implementation\r
37 {\r
38         std::string                                             filters_;\r
39         std::shared_ptr<AVFilterGraph>  graph_; \r
40         AVFilterContext*                                buffersink_ctx_;\r
41         AVFilterContext*                                buffersrc_ctx_;\r
42         std::shared_ptr<void>                   parallel_yadif_ctx_;\r
43         std::vector<PixelFormat>                pix_fmts_;\r
44         std::queue<std::shared_ptr<AVFrame>> bypass_;\r
45         int                                                             in_format_;\r
46         bool                                                    warned_;\r
47                 \r
48         implementation(const std::wstring& filters, const std::vector<PixelFormat>& pix_fmts) \r
49                 : filters_(narrow(filters))\r
50                 , parallel_yadif_ctx_(nullptr)\r
51                 , pix_fmts_(pix_fmts)\r
52                 , in_format_(0)\r
53                 , warned_(false)\r
54         {\r
55                 if(pix_fmts_.empty())\r
56                 {\r
57                         pix_fmts_.push_back(PIX_FMT_YUVA420P);\r
58                         pix_fmts_.push_back(PIX_FMT_YUV444P);\r
59                         pix_fmts_.push_back(PIX_FMT_YUV422P);\r
60                         pix_fmts_.push_back(PIX_FMT_YUV420P);\r
61                         pix_fmts_.push_back(PIX_FMT_YUV411P);\r
62                         pix_fmts_.push_back(PIX_FMT_ARGB);\r
63                         pix_fmts_.push_back(PIX_FMT_RGBA);\r
64                         pix_fmts_.push_back(PIX_FMT_ABGR);\r
65                         pix_fmts_.push_back(PIX_FMT_GRAY8);\r
66                         pix_fmts_.push_back(PIX_FMT_NONE);\r
67                 }\r
68                 else\r
69                         pix_fmts_.push_back(PIX_FMT_NONE);\r
70 \r
71                 std::transform(filters_.begin(), filters_.end(), filters_.begin(), ::tolower);\r
72         }\r
73         \r
74         void push(const std::shared_ptr<AVFrame>& frame)\r
75         {               \r
76                 if(!frame)\r
77                         return;\r
78 \r
79                 if(frame->data[0] == nullptr || frame->width < 1)\r
80                         BOOST_THROW_EXCEPTION(invalid_argument());\r
81 \r
82                 if(filters_.empty())\r
83                 {\r
84                         bypass_.push(frame);\r
85                         return;\r
86                 }\r
87                 \r
88                 try\r
89                 {\r
90                         if(!graph_)\r
91                         {\r
92                                 try\r
93                                 {\r
94                                         in_format_ = frame->format;\r
95                                         graph_.reset(avfilter_graph_alloc(), [](AVFilterGraph* p){avfilter_graph_free(&p);});\r
96                                                                 \r
97                                         // Input\r
98                                         std::stringstream args;\r
99                                         args << frame->width << ":" << frame->height << ":" << frame->format << ":" << 0 << ":" << 0 << ":" << 0 << ":" << 0; // don't care about pts and aspect_ratio\r
100                                         THROW_ON_ERROR2(avfilter_graph_create_filter(&buffersrc_ctx_, avfilter_get_by_name("buffer"), "src", args.str().c_str(), NULL, graph_.get()), "[filter]");\r
101 \r
102                                         //safe_ptr<AVBufferSinkParams> buffersink_params(av_buffersink_params_alloc(), av_free);\r
103                                         //buffersink_params->pixel_fmts = pix_fmts_.data();\r
104                                         THROW_ON_ERROR2(avfilter_graph_create_filter(&buffersink_ctx_, avfilter_get_by_name("buffersink"), "out", NULL, pix_fmts_.data(), graph_.get()), "[filter]");\r
105                         \r
106                                         AVFilterInOut* outputs = avfilter_inout_alloc();\r
107                                         AVFilterInOut* inputs  = avfilter_inout_alloc();\r
108                         \r
109                                         outputs->name                   = av_strdup("in");\r
110                                         outputs->filter_ctx             = buffersrc_ctx_;\r
111                                         outputs->pad_idx                = 0;\r
112                                         outputs->next                   = NULL;\r
113 \r
114                                         inputs->name                    = av_strdup("out");\r
115                                         inputs->filter_ctx              = buffersink_ctx_;\r
116                                         inputs->pad_idx                 = 0;\r
117                                         inputs->next                    = NULL;\r
118                         \r
119                                         THROW_ON_ERROR2(avfilter_graph_parse(graph_.get(), filters_.c_str(), &inputs, &outputs, NULL), "[filter]");\r
120                         \r
121                                         avfilter_inout_free(&inputs);\r
122                                         avfilter_inout_free(&outputs);\r
123 \r
124                                         THROW_ON_ERROR2(avfilter_graph_config(graph_.get(), NULL), "[filter]");                 \r
125 \r
126                                         for(size_t n = 0; n < graph_->filter_count; ++n)\r
127                                         {\r
128                                                 auto filter_name = graph_->filters[n]->name;\r
129                                                 if(strstr(filter_name, "yadif") != 0)\r
130                                                         parallel_yadif_ctx_ = make_parallel_yadif(graph_->filters[n]);\r
131                                         }\r
132                                 }\r
133                                 catch(...)\r
134                                 {\r
135                                         graph_ = nullptr;\r
136                                         throw;\r
137                                 }\r
138                         }\r
139                 \r
140                         THROW_ON_ERROR2(av_vsrc_buffer_add_frame(buffersrc_ctx_, frame.get(), 0), "[filter]");\r
141                 }\r
142                 catch(ffmpeg_error&)\r
143                 {\r
144                         throw;\r
145                 }\r
146                 catch(...)\r
147                 {\r
148                         BOOST_THROW_EXCEPTION(ffmpeg_error() << boost::errinfo_nested_exception(boost::current_exception()));\r
149                 }\r
150         }\r
151 \r
152         std::shared_ptr<AVFrame> poll()\r
153         {\r
154                 if(filters_.empty())\r
155                 {\r
156                         if(bypass_.empty())\r
157                                 return nullptr;\r
158                         auto frame = bypass_.front();\r
159                         bypass_.pop();\r
160                         return frame;\r
161                 }\r
162 \r
163                 if(!graph_)\r
164                         return nullptr;\r
165                 \r
166                 try\r
167                 {\r
168                         if(avfilter_poll_frame(buffersink_ctx_->inputs[0])) \r
169                         {\r
170                                 AVFilterBufferRef *picref;\r
171                                 THROW_ON_ERROR2(av_buffersink_get_buffer_ref(buffersink_ctx_, &picref, 0), "[filter]");\r
172 \r
173                                 if (picref) \r
174                                 {               \r
175                                         safe_ptr<AVFrame> frame(avcodec_alloc_frame(), [=](AVFrame* p)\r
176                                         {\r
177                                                 av_free(p);\r
178                                                 avfilter_unref_buffer(picref);\r
179                                         });\r
180 \r
181                                         avcodec_get_frame_defaults(frame.get());        \r
182 \r
183                                         memcpy(frame->data,     picref->data,     sizeof(frame->data));\r
184                                         memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));\r
185                                         frame->format                           = picref->format;\r
186                                         frame->width                            = picref->video->w;\r
187                                         frame->height                           = picref->video->h;\r
188                                         frame->pkt_pos                          = picref->pos;\r
189                                         frame->interlaced_frame         = picref->video->interlaced;\r
190                                         frame->top_field_first          = picref->video->top_field_first;\r
191                                         frame->key_frame                        = picref->video->key_frame;\r
192                                         frame->pict_type                        = picref->video->pict_type;\r
193                                         frame->sample_aspect_ratio      = picref->video->sample_aspect_ratio;\r
194 \r
195                                         // HACK: Workaround for missing format bug in libavfilter.\r
196                                         if(frame->format == 0)\r
197                                         {\r
198                                                 if(in_format_ == PIX_FMT_YUV444P10)\r
199                                                         frame->format = PIX_FMT_YUV420P;//PIX_FMT_YUV444P;\r
200                                                 else if(in_format_ == PIX_FMT_YUV422P10)\r
201                                                         frame->format = PIX_FMT_YUV420P;//PIX_FMT_YUV422P;\r
202                                                 else\r
203                                                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("libavfilter bug: frame format == 0."));\r
204                                                 \r
205                                                 if(!warned_)\r
206                                                 {\r
207                                                         CASPAR_LOG(warning) << "[ffmpeg] libavfilter bug workaround: frame format == 0, guessing PIX_FMT_YUV420P.";\r
208                                                         warned_ = true;\r
209                                                 }\r
210                                         }\r
211 \r
212                                         return frame;\r
213                                 }\r
214                         }\r
215                 }\r
216                 catch(ffmpeg_error&)\r
217                 {\r
218                         throw;\r
219                 }\r
220                 catch(...)\r
221                 {\r
222                         BOOST_THROW_EXCEPTION(ffmpeg_error() << boost::errinfo_nested_exception(boost::current_exception()));\r
223                 }\r
224 \r
225                 return nullptr;\r
226         }\r
227 };\r
228 \r
229 filter::filter(const std::wstring& filters, const std::vector<PixelFormat>& pix_fmts) : impl_(new implementation(filters, pix_fmts)){}\r
230 filter::filter(filter&& other) : impl_(std::move(other.impl_)){}\r
231 filter& filter::operator=(filter&& other){impl_ = std::move(other.impl_); return *this;}\r
232 void filter::push(const std::shared_ptr<AVFrame>& frame){impl_->push(frame);}\r
233 std::shared_ptr<AVFrame> filter::poll(){return impl_->poll();}\r
234 std::string filter::filter_str() const{return impl_->filters_;}\r
235 std::vector<safe_ptr<AVFrame>> filter::poll_all()\r
236 {       \r
237         std::vector<safe_ptr<AVFrame>> frames;\r
238         for(auto frame = poll(); frame; frame = poll())\r
239                 frames.push_back(make_safe_ptr(frame));\r
240         return frames;\r
241 }\r
242 \r
243 }}