]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/filter.cpp
2.0. Updated namespaces.
[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 <boost/assign.hpp>\r
10 \r
11 #include <cstdio>\r
12 #include <sstream>\r
13 \r
14 #if defined(_MSC_VER)\r
15 #pragma warning (push)\r
16 #pragma warning (disable : 4244)\r
17 #endif\r
18 extern "C" \r
19 {\r
20         #include <libavutil/avutil.h>\r
21         #include <libavutil/imgutils.h>\r
22         #include <libavfilter/avfilter.h>\r
23         #include <libavfilter/avcodec.h>\r
24         #include <libavfilter/avfiltergraph.h>\r
25         #include <libavfilter/vsink_buffer.h>\r
26         #include <libavfilter/vsrc_buffer.h>\r
27 }\r
28 #if defined(_MSC_VER)\r
29 #pragma warning (pop)\r
30 #endif\r
31 \r
32 namespace caspar { namespace ffmpeg {\r
33         \r
34 struct filter::implementation\r
35 {\r
36         std::string                                             filters_;\r
37         std::shared_ptr<AVFilterGraph>  graph_; \r
38         AVFilterContext*                                buffersink_ctx_;\r
39         AVFilterContext*                                buffersrc_ctx_;\r
40         std::shared_ptr<void>                   parallel_yadif_ctx_;\r
41         std::vector<PixelFormat>                pix_fmts_;\r
42                 \r
43         implementation(const std::wstring& filters, const std::vector<PixelFormat>& pix_fmts) \r
44                 : filters_(narrow(filters))\r
45                 , parallel_yadif_ctx_(nullptr)\r
46                 , pix_fmts_(pix_fmts)\r
47         {\r
48                 if(pix_fmts_.empty())\r
49                 {\r
50                         pix_fmts_.push_back(PIX_FMT_YUV420P);\r
51                         pix_fmts_.push_back(PIX_FMT_YUVA420P);\r
52                         pix_fmts_.push_back(PIX_FMT_YUV422P);\r
53                         pix_fmts_.push_back(PIX_FMT_YUV444P);\r
54                         pix_fmts_.push_back(PIX_FMT_YUV411P);\r
55                         pix_fmts_.push_back(PIX_FMT_ARGB);\r
56                         pix_fmts_.push_back(PIX_FMT_RGBA);\r
57                         pix_fmts_.push_back(PIX_FMT_ABGR);\r
58                         pix_fmts_.push_back(PIX_FMT_GRAY8);\r
59                         pix_fmts_.push_back(PIX_FMT_NONE);\r
60                 }\r
61                 else\r
62                         pix_fmts_.push_back(PIX_FMT_NONE);\r
63 \r
64                 std::transform(filters_.begin(), filters_.end(), filters_.begin(), ::tolower);\r
65         }\r
66         \r
67         std::vector<safe_ptr<AVFrame>> execute(const std::shared_ptr<AVFrame>& frame)\r
68         {\r
69                 if(!frame)\r
70                         return std::vector<safe_ptr<AVFrame>>();\r
71 \r
72                 if(filters_.empty())\r
73                         return boost::assign::list_of(frame);\r
74 \r
75                 push(frame);\r
76                 return poll();\r
77         }\r
78 \r
79         void push(const std::shared_ptr<AVFrame>& frame)\r
80         {               \r
81                 if(!graph_)\r
82                 {\r
83                         graph_.reset(avfilter_graph_alloc(), [](AVFilterGraph* p){avfilter_graph_free(&p);});\r
84                                                                 \r
85                         // Input\r
86                         std::stringstream args;\r
87                         args << frame->width << ":" << frame->height << ":" << frame->format << ":" << 0 << ":" << 0 << ":" << 0 << ":" << 0; // don't care about pts and aspect_ratio\r
88                         THROW_ON_ERROR2(avfilter_graph_create_filter(&buffersrc_ctx_, avfilter_get_by_name("buffer"), "src", args.str().c_str(), NULL, graph_.get()), "[filter]");\r
89 \r
90                         // OPIX_FMT_BGRAutput\r
91                         THROW_ON_ERROR2(avfilter_graph_create_filter(&buffersink_ctx_, avfilter_get_by_name("buffersink"), "out", NULL, pix_fmts_.data(), graph_.get()), "[filter]");\r
92                         \r
93                         AVFilterInOut* outputs = avfilter_inout_alloc();\r
94                         AVFilterInOut* inputs  = avfilter_inout_alloc();\r
95                         \r
96                         outputs->name                   = av_strdup("in");\r
97                         outputs->filter_ctx             = buffersrc_ctx_;\r
98                         outputs->pad_idx                = 0;\r
99                         outputs->next                   = NULL;\r
100 \r
101                         inputs->name                    = av_strdup("out");\r
102                         inputs->filter_ctx              = buffersink_ctx_;\r
103                         inputs->pad_idx                 = 0;\r
104                         inputs->next                    = NULL;\r
105                         \r
106                         THROW_ON_ERROR2(avfilter_graph_parse(graph_.get(), filters_.c_str(), &inputs, &outputs, NULL), "[filter]");\r
107                         \r
108                         avfilter_inout_free(&inputs);\r
109                         avfilter_inout_free(&outputs);\r
110 \r
111                         THROW_ON_ERROR2(avfilter_graph_config(graph_.get(), NULL), "[filter]");                 \r
112 \r
113                         for(size_t n = 0; n < graph_->filter_count; ++n)\r
114                         {\r
115                                 auto filter_name = graph_->filters[n]->name;\r
116                                 if(strstr(filter_name, "yadif") != 0)\r
117                                         parallel_yadif_ctx_ = make_parallel_yadif(graph_->filters[n]);\r
118                         }\r
119                 }\r
120         \r
121                 THROW_ON_ERROR2(av_vsrc_buffer_add_frame(buffersrc_ctx_, frame.get(), 0), "[filter]");\r
122         }\r
123 \r
124         std::vector<safe_ptr<AVFrame>> poll()\r
125         {\r
126                 std::vector<safe_ptr<AVFrame>> result;\r
127 \r
128                 if(!graph_)\r
129                         return result;\r
130                 \r
131                 while (avfilter_poll_frame(buffersink_ctx_->inputs[0])) \r
132                 {\r
133                         AVFilterBufferRef *picref;\r
134                         THROW_ON_ERROR2(av_vsink_buffer_get_video_buffer_ref(buffersink_ctx_, &picref, 0), "[filter]");\r
135 \r
136             if (picref) \r
137                         {               \r
138                                 safe_ptr<AVFrame> frame(avcodec_alloc_frame(), [=](AVFrame* p)\r
139                                 {\r
140                                         av_free(p);\r
141                                         avfilter_unref_buffer(picref);\r
142                                 });\r
143 \r
144                                 avcodec_get_frame_defaults(frame.get());        \r
145 \r
146                                 memcpy(frame->data,     picref->data,     sizeof(frame->data));\r
147                                 memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));\r
148                                 frame->format                           = picref->format;\r
149                                 frame->width                            = picref->video->w;\r
150                                 frame->height                           = picref->video->h;\r
151                                 frame->pkt_pos                          = picref->pos;\r
152                                 frame->interlaced_frame         = picref->video->interlaced;\r
153                                 frame->top_field_first          = picref->video->top_field_first;\r
154                                 frame->key_frame                        = picref->video->key_frame;\r
155                                 frame->pict_type                        = picref->video->pict_type;\r
156                                 frame->sample_aspect_ratio      = picref->video->sample_aspect_ratio;\r
157 \r
158                                 result.push_back(frame);\r
159             }\r
160         }\r
161 \r
162                 return result;\r
163         }\r
164 };\r
165 \r
166 filter::filter(const std::wstring& filters, const std::vector<PixelFormat>& pix_fmts) : impl_(new implementation(filters, pix_fmts)){}\r
167 filter::filter(filter&& other) : impl_(std::move(other.impl_)){}\r
168 filter& filter::operator=(filter&& other){impl_ = std::move(other.impl_); return *this;}\r
169 std::vector<safe_ptr<AVFrame>> filter::execute(const std::shared_ptr<AVFrame>& frame) {return impl_->execute(frame);}\r
170 \r
171 }}