]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/filter.cpp
2.0.0.2: filter: Use null filter with empty filter string. Fixed memory leak.
[casparcg] / modules / ffmpeg / producer / filter / filter.cpp
1 #include "../../stdafx.h"\r
2 \r
3 #include "filter.h"\r
4 \r
5 #include "../../ffmpeg_error.h"\r
6 \r
7 #include <common/exception/exceptions.h>\r
8 #include <core/producer/frame/basic_frame.h>\r
9 #include <core/producer/frame/frame_factory.h>\r
10 #include <core/mixer/write_frame.h>\r
11 \r
12 #include <boost/circular_buffer.hpp>\r
13 \r
14 #include <cstdio>\r
15 #include <sstream>\r
16 \r
17 extern "C" \r
18 {\r
19         #define __STDC_CONSTANT_MACROS\r
20         #define __STDC_LIMIT_MACROS\r
21         #include <libavutil/avutil.h>\r
22         #include <libavutil/imgutils.h>\r
23         #include <libavfilter/avfilter.h>\r
24         #include <libavfilter/avcodec.h>\r
25         #include <libavfilter/avfiltergraph.h>\r
26         #include <libavfilter/vsink_buffer.h>\r
27         #include <libavfilter/vsrc_buffer.h>\r
28 }\r
29 \r
30 namespace caspar {\r
31         \r
32 struct filter::implementation\r
33 {\r
34         std::string                                             filters_;\r
35         std::shared_ptr<AVFilterGraph>  graph_; \r
36         AVFilterContext*                                buffersink_ctx_;\r
37         AVFilterContext*                                buffersrc_ctx_;\r
38         \r
39         implementation(const std::wstring& filters) \r
40                 : filters_(filters.empty() ? "null" : narrow(filters))\r
41         {\r
42                 std::transform(filters_.begin(), filters_.end(), filters_.begin(), ::tolower);\r
43         }\r
44 \r
45         void push(const std::shared_ptr<AVFrame>& frame)\r
46         {               \r
47                 int errn = 0;   \r
48 \r
49                 if(!graph_)\r
50                 {\r
51                         graph_.reset(avfilter_graph_alloc(), [](AVFilterGraph* p){avfilter_graph_free(&p);});\r
52                                                                 \r
53                         // Input\r
54                         std::stringstream args;\r
55                         args << frame->width << ":" << frame->height << ":" << frame->format << ":" << 0 << ":" << 0 << ":" << 0 << ":" << 0; // don't care about pts and aspect_ratio\r
56                         errn = avfilter_graph_create_filter(&buffersrc_ctx_, avfilter_get_by_name("buffer"), "src", args.str().c_str(), NULL, graph_.get());\r
57                         if(errn < 0)\r
58                         {\r
59                                 BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) <<\r
60                                         boost::errinfo_api_function("avfilter_graph_create_filter") <<  boost::errinfo_errno(AVUNERROR(errn)));\r
61                         }\r
62 \r
63                         PixelFormat pix_fmts[] = \r
64                         {\r
65                                 PIX_FMT_YUV420P,\r
66                                 PIX_FMT_YUVA420P,\r
67                                 PIX_FMT_YUV422P,\r
68                                 PIX_FMT_YUV444P,\r
69                                 PIX_FMT_YUV411P,\r
70                                 PIX_FMT_ARGB, \r
71                                 PIX_FMT_RGBA,\r
72                                 PIX_FMT_ABGR,\r
73                                 PIX_FMT_GRAY8,\r
74                                 PIX_FMT_NONE\r
75                         };      \r
76                         // OPIX_FMT_BGRAutput\r
77                         errn = avfilter_graph_create_filter(&buffersink_ctx_, avfilter_get_by_name("buffersink"), "out", NULL, pix_fmts, graph_.get());\r
78                         if(errn < 0)\r
79                         {\r
80                                 BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) <<\r
81                                         boost::errinfo_api_function("avfilter_graph_create_filter") << boost::errinfo_errno(AVUNERROR(errn)));\r
82                         }\r
83                         \r
84                         AVFilterInOut* outputs = avfilter_inout_alloc();\r
85                         AVFilterInOut* inputs  = avfilter_inout_alloc();\r
86 \r
87                         outputs->name                   = av_strdup("in");\r
88                         outputs->filter_ctx             = buffersrc_ctx_;\r
89                         outputs->pad_idx                = 0;\r
90                         outputs->next                   = NULL;\r
91 \r
92                         inputs->name                    = av_strdup("out");\r
93                         inputs->filter_ctx              = buffersink_ctx_;\r
94                         inputs->pad_idx                 = 0;\r
95                         inputs->next                    = NULL;\r
96                         \r
97                         errn = avfilter_graph_parse(graph_.get(), filters_.c_str(), &inputs, &outputs, NULL);\r
98 \r
99                         avfilter_inout_free(&inputs);\r
100                         avfilter_inout_free(&outputs);\r
101 \r
102                         if(errn < 0)\r
103                         {\r
104                                 BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) <<\r
105                                         boost::errinfo_api_function("avfilter_graph_parse") << boost::errinfo_errno(AVUNERROR(errn)));\r
106                         }\r
107                         \r
108                         errn = avfilter_graph_config(graph_.get(), NULL);\r
109                         if(errn < 0)\r
110                         {\r
111                                 BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) \r
112                                         <<      boost::errinfo_api_function("avfilter_graph_config") << boost::errinfo_errno(AVUNERROR(errn)));\r
113                         }\r
114                 }\r
115         \r
116                 errn = av_vsrc_buffer_add_frame(buffersrc_ctx_, frame.get(), 0);\r
117                 if(errn < 0)\r
118                 {\r
119                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(av_error_str(errn)) <<\r
120                                 boost::errinfo_api_function("av_vsrc_buffer_add_frame") << boost::errinfo_errno(AVUNERROR(errn)));\r
121                 }\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             av_vsink_buffer_get_video_buffer_ref(buffersink_ctx_, &picref, 0);\r
135             if (picref) \r
136                         {               \r
137                                 safe_ptr<AVFrame> frame(avcodec_alloc_frame(), [=](AVFrame* p)\r
138                                 {\r
139                                         av_free(p);\r
140                                         avfilter_unref_buffer(picref);\r
141                                 });\r
142 \r
143                                 avcodec_get_frame_defaults(frame.get());        \r
144 \r
145                                 memcpy(frame->data,     picref->data,     sizeof(frame->data));\r
146                                 memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));\r
147                                 frame->format                           = picref->format;\r
148                                 frame->width                            = picref->video->w;\r
149                                 frame->height                           = picref->video->h;\r
150                                 frame->pkt_pos                          = picref->pos;\r
151                                 frame->interlaced_frame         = picref->video->interlaced;\r
152                                 frame->top_field_first          = picref->video->top_field_first;\r
153                                 frame->key_frame                        = picref->video->key_frame;\r
154                                 frame->pict_type                        = picref->video->pict_type;\r
155                                 frame->sample_aspect_ratio      = picref->video->sample_aspect_ratio;\r
156 \r
157                                 result.push_back(frame);\r
158             }\r
159         }\r
160 \r
161                 return result;\r
162         }\r
163 };\r
164 \r
165 filter::filter(const std::wstring& filters) : impl_(new implementation(filters)){}\r
166 void filter::push(const std::shared_ptr<AVFrame>& frame) {impl_->push(frame);}\r
167 std::vector<safe_ptr<AVFrame>> filter::poll() {return impl_->poll();}\r
168 }