]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/filter.cpp
6006d5e72f2c691b5e68ca678d7e70fd3d47794e
[casparcg] / modules / ffmpeg / producer / filter / filter.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 "filter.h"\r
25 \r
26 #include "parallel_yadif.h"\r
27 \r
28 #include "../../ffmpeg_error.h"\r
29 \r
30 #include <common/exception/exceptions.h>\r
31 \r
32 #include <boost/assign.hpp>\r
33 \r
34 #include <cstdio>\r
35 #include <sstream>\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 <libavutil/avutil.h>\r
44         #include <libavutil/imgutils.h>\r
45         #include <libavfilter/avfilter.h>\r
46         #include <libavfilter/avcodec.h>\r
47         #include <libavfilter/avfiltergraph.h>\r
48         #include <libavfilter/buffersink.h>\r
49         #include <libavfilter/vsrc_buffer.h>\r
50 }\r
51 #if defined(_MSC_VER)\r
52 #pragma warning (pop)\r
53 #endif\r
54 \r
55 namespace caspar { namespace ffmpeg {\r
56         \r
57 struct filter::implementation\r
58 {\r
59         std::string                                             filters_;\r
60         std::shared_ptr<AVFilterGraph>  graph_; \r
61         AVFilterContext*                                buffersink_ctx_;\r
62         AVFilterContext*                                buffersrc_ctx_;\r
63         std::shared_ptr<void>                   parallel_yadif_ctx_;\r
64         std::vector<PixelFormat>                pix_fmts_;\r
65         std::queue<std::shared_ptr<AVFrame>> bypass_;\r
66                 \r
67         implementation(const std::wstring& filters, const std::vector<PixelFormat>& pix_fmts) \r
68                 : filters_(narrow(filters))\r
69                 , parallel_yadif_ctx_(nullptr)\r
70                 , pix_fmts_(pix_fmts)\r
71         {\r
72                 if(pix_fmts_.empty())\r
73                 {\r
74                         pix_fmts_.push_back(PIX_FMT_YUVA420P);\r
75                         pix_fmts_.push_back(PIX_FMT_YUV444P);\r
76                         pix_fmts_.push_back(PIX_FMT_YUV422P);\r
77                         pix_fmts_.push_back(PIX_FMT_YUV420P);\r
78                         pix_fmts_.push_back(PIX_FMT_YUV411P);\r
79                         pix_fmts_.push_back(PIX_FMT_BGRA);\r
80                         pix_fmts_.push_back(PIX_FMT_ARGB);\r
81                         pix_fmts_.push_back(PIX_FMT_RGBA);\r
82                         pix_fmts_.push_back(PIX_FMT_ABGR);\r
83                         pix_fmts_.push_back(PIX_FMT_GRAY8);\r
84                         pix_fmts_.push_back(PIX_FMT_NONE);\r
85                 }\r
86                 else\r
87                         pix_fmts_.push_back(PIX_FMT_NONE);\r
88 \r
89                 std::transform(filters_.begin(), filters_.end(), filters_.begin(), ::tolower);\r
90         }\r
91         \r
92         void push(const std::shared_ptr<AVFrame>& frame)\r
93         {               \r
94                 if(!frame)\r
95                         return;\r
96 \r
97                 if(frame->data[0] == nullptr || frame->width < 1)\r
98                         BOOST_THROW_EXCEPTION(invalid_argument());\r
99 \r
100                 if(filters_.empty())\r
101                 {\r
102                         bypass_.push(frame);\r
103                         return;\r
104                 }\r
105                 \r
106                 try\r
107                 {\r
108                         if(!graph_)\r
109                         {\r
110                                 try\r
111                                 {\r
112                                         graph_.reset(avfilter_graph_alloc(), [](AVFilterGraph* p){avfilter_graph_free(&p);});\r
113                                                                 \r
114                                         // Input\r
115                                         std::stringstream args;\r
116                                         args << frame->width << ":" << frame->height << ":" << frame->format << ":" << 0 << ":" << 0 << ":" << 0 << ":" << 0; // don't care about pts and aspect_ratio\r
117                                         THROW_ON_ERROR2(avfilter_graph_create_filter(&buffersrc_ctx_, avfilter_get_by_name("buffer"), "src", args.str().c_str(), NULL, graph_.get()), "[filter]");\r
118 \r
119                                         //safe_ptr<AVBufferSinkParams> buffersink_params(av_buffersink_params_alloc(), av_free);\r
120                                         //buffersink_params->pixel_fmts = pix_fmts_.data();\r
121                                         THROW_ON_ERROR2(avfilter_graph_create_filter(&buffersink_ctx_, avfilter_get_by_name("buffersink"), "out", NULL, pix_fmts_.data(), graph_.get()), "[filter]");\r
122                         \r
123                                         AVFilterInOut* outputs = avfilter_inout_alloc();\r
124                                         AVFilterInOut* inputs  = avfilter_inout_alloc();\r
125                         \r
126                                         outputs->name                   = av_strdup("in");\r
127                                         outputs->filter_ctx             = buffersrc_ctx_;\r
128                                         outputs->pad_idx                = 0;\r
129                                         outputs->next                   = NULL;\r
130 \r
131                                         inputs->name                    = av_strdup("out");\r
132                                         inputs->filter_ctx              = buffersink_ctx_;\r
133                                         inputs->pad_idx                 = 0;\r
134                                         inputs->next                    = NULL;\r
135                         \r
136                                         THROW_ON_ERROR2(avfilter_graph_parse(graph_.get(), filters_.c_str(), &inputs, &outputs, NULL), "[filter]");\r
137                         \r
138                                         avfilter_inout_free(&inputs);\r
139                                         avfilter_inout_free(&outputs);\r
140 \r
141                                         THROW_ON_ERROR2(avfilter_graph_config(graph_.get(), NULL), "[filter]");                 \r
142 \r
143                                         for(size_t n = 0; n < graph_->filter_count; ++n)\r
144                                         {\r
145                                                 auto filter_name = graph_->filters[n]->name;\r
146                                                 if(strstr(filter_name, "yadif") != 0)\r
147                                                         parallel_yadif_ctx_ = make_parallel_yadif(graph_->filters[n]);\r
148                                         }\r
149                                 }\r
150                                 catch(...)\r
151                                 {\r
152                                         graph_ = nullptr;\r
153                                         throw;\r
154                                 }\r
155                         }\r
156                 \r
157                         THROW_ON_ERROR2(av_vsrc_buffer_add_frame(buffersrc_ctx_, frame.get(), 0), "[filter]");\r
158                 }\r
159                 catch(ffmpeg_error&)\r
160                 {\r
161                         throw;\r
162                 }\r
163                 catch(...)\r
164                 {\r
165                         BOOST_THROW_EXCEPTION(ffmpeg_error() << boost::errinfo_nested_exception(boost::current_exception()));\r
166                 }\r
167         }\r
168 \r
169         std::shared_ptr<AVFrame> poll()\r
170         {\r
171                 if(filters_.empty())\r
172                 {\r
173                         if(bypass_.empty())\r
174                                 return nullptr;\r
175                         auto frame = bypass_.front();\r
176                         bypass_.pop();\r
177                         return frame;\r
178                 }\r
179 \r
180                 if(!graph_)\r
181                         return nullptr;\r
182                 \r
183                 try\r
184                 {\r
185                         if(avfilter_poll_frame(buffersink_ctx_->inputs[0])) \r
186                         {\r
187                                 AVFilterBufferRef *picref;\r
188                                 THROW_ON_ERROR2(av_buffersink_get_buffer_ref(buffersink_ctx_, &picref, 0), "[filter]");\r
189 \r
190                                 if (picref) \r
191                                 {               \r
192                                         safe_ptr<AVFrame> frame(avcodec_alloc_frame(), [=](AVFrame* p)\r
193                                         {\r
194                                                 av_free(p);\r
195                                                 avfilter_unref_buffer(picref);\r
196                                         });\r
197 \r
198                                         avcodec_get_frame_defaults(frame.get());        \r
199 \r
200                                         memcpy(frame->data,     picref->data,     sizeof(frame->data));\r
201                                         memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));\r
202                                         frame->format                           = picref->format;\r
203                                         frame->width                            = picref->video->w;\r
204                                         frame->height                           = picref->video->h;\r
205                                         frame->pkt_pos                          = picref->pos;\r
206                                         frame->interlaced_frame         = picref->video->interlaced;\r
207                                         frame->top_field_first          = picref->video->top_field_first;\r
208                                         frame->key_frame                        = picref->video->key_frame;\r
209                                         frame->pict_type                        = picref->video->pict_type;\r
210                                         frame->sample_aspect_ratio      = picref->video->sample_aspect_ratio;\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 }}