]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/filter.cpp
2.0. ffmpeg_producer: Filter cleanup and bug fixes.
[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/vsrc_buffer.h>\r
26         #include <libavfilter/avfiltergraph.h>\r
27 }\r
28 \r
29 namespace caspar {\r
30         \r
31 struct filter::implementation\r
32 {\r
33         std::string                                                             filters_;\r
34         std::shared_ptr<AVFilterGraph>                  graph_;\r
35         AVFilterContext*                                                video_in_filter_;\r
36         AVFilterContext*                                                video_out_filter_;\r
37 \r
38         implementation(const std::string& filters) \r
39                 : filters_(filters)\r
40         {\r
41                 std::transform(filters_.begin(), filters_.end(), filters_.begin(), ::tolower);\r
42         }\r
43 \r
44         void push(const safe_ptr<AVFrame>& frame)\r
45         {               \r
46                 int errn = 0;   \r
47 \r
48                 if(!graph_)\r
49                 {\r
50                         graph_.reset(avfilter_graph_alloc(), [](AVFilterGraph* p){avfilter_graph_free(&p);});\r
51                         \r
52                         // Input\r
53                         std::stringstream buffer_ss;\r
54                         buffer_ss << frame->width << ":" << frame->height << ":" << frame->format << ":" << 0 << ":" << 0 << ":" << 0 << ":" << 0; // don't care about pts and aspect_ratio\r
55                         errn = avfilter_graph_create_filter(&video_in_filter_, avfilter_get_by_name("buffer"), "src", buffer_ss.str().c_str(), NULL, graph_.get());\r
56                         if(errn < 0 || !video_in_filter_)\r
57                         {\r
58                                 BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) <<\r
59                                         boost::errinfo_api_function("avfilter_graph_create_filter") <<  boost::errinfo_errno(AVUNERROR(errn)));\r
60                         }\r
61 \r
62                         // Output\r
63                         errn = avfilter_graph_create_filter(&video_out_filter_, avfilter_get_by_name("nullsink"), "out", NULL, NULL, graph_.get());\r
64                         if(errn < 0 || !video_out_filter_)\r
65                         {\r
66                                 BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) <<\r
67                                         boost::errinfo_api_function("avfilter_graph_create_filter") << boost::errinfo_errno(AVUNERROR(errn)));\r
68                         }\r
69                         \r
70                         AVFilterInOut* outputs = reinterpret_cast<AVFilterInOut*>(av_malloc(sizeof(AVFilterInOut)));\r
71                         AVFilterInOut* inputs  = reinterpret_cast<AVFilterInOut*>(av_malloc(sizeof(AVFilterInOut)));\r
72 \r
73                         outputs->name                   = av_strdup("in");\r
74                         outputs->filter_ctx             = video_in_filter_;\r
75                         outputs->pad_idx                = 0;\r
76                         outputs->next                   = NULL;\r
77 \r
78                         inputs->name                    = av_strdup("out");\r
79                         inputs->filter_ctx              = video_out_filter_;\r
80                         inputs->pad_idx                 = 0;\r
81                         inputs->next                    = NULL;\r
82                         \r
83                         errn = avfilter_graph_parse(graph_.get(), filters_.c_str(), inputs, outputs, NULL);\r
84                         if(errn < 0)\r
85                         {\r
86                                 BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) <<\r
87                                         boost::errinfo_api_function("avfilter_graph_parse") << boost::errinfo_errno(AVUNERROR(errn)));\r
88                         }\r
89 \r
90 //                      av_free(outputs);\r
91 //                      av_free(inputs);\r
92 \r
93                         errn = avfilter_graph_config(graph_.get(), NULL);\r
94                         if(errn < 0)\r
95                         {\r
96                                 BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) \r
97                                         <<      boost::errinfo_api_function("avfilter_graph_config") << boost::errinfo_errno(AVUNERROR(errn)));\r
98                         }\r
99                 }\r
100         \r
101                 errn = av_vsrc_buffer_add_frame(video_in_filter_, frame.get(), 0);\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("av_vsrc_buffer_add_frame") << boost::errinfo_errno(AVUNERROR(errn)));\r
106                 }\r
107         }\r
108 \r
109         std::vector<safe_ptr<AVFrame>> poll()\r
110         {\r
111                 int errn = avfilter_poll_frame(video_out_filter_->inputs[0]);\r
112                 if(errn < 0)\r
113                 {\r
114                         BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) <<\r
115                                 boost::errinfo_api_function("avfilter_poll_frame") << boost::errinfo_errno(AVUNERROR(errn)));\r
116                 }\r
117 \r
118                 std::vector<safe_ptr<AVFrame>> result;\r
119 \r
120                 std::generate_n(std::back_inserter(result), errn, [&]{return request_frame();});\r
121 \r
122                 return result;\r
123         }\r
124                 \r
125         safe_ptr<AVFrame> request_frame()\r
126         {               \r
127                 auto link = video_out_filter_->inputs[0];\r
128                 \r
129                 int errn = avfilter_request_frame(link);                        \r
130                 if(errn < 0)\r
131                 {\r
132                         BOOST_THROW_EXCEPTION(caspar_exception() <<     msg_info(av_error_str(errn)) <<\r
133                                 boost::errinfo_api_function("avfilter_request_frame") << boost::errinfo_errno(AVUNERROR(errn)));\r
134                 }\r
135                 \r
136                 auto cur_buf = link->cur_buf;\r
137                 auto pic = reinterpret_cast<AVPicture*>(link->cur_buf->buf);\r
138                 \r
139                 safe_ptr<AVFrame> frame(avcodec_alloc_frame(), [=](AVFrame* p)\r
140                 {\r
141                         av_free(p);\r
142                         avfilter_unref_buffer(cur_buf);\r
143                 });\r
144 \r
145                 avcodec_get_frame_defaults(frame.get());        \r
146 \r
147                 for(size_t n = 0; n < 4; ++n)\r
148                 {\r
149                         frame->data[n]          = pic->data[n];\r
150                         frame->linesize[n]      = pic->linesize[n];\r
151                 }\r
152 \r
153                 frame->width                    = link->cur_buf->video->w;\r
154                 frame->height                   = link->cur_buf->video->h;\r
155                 frame->format                   = link->cur_buf->format;\r
156                 frame->interlaced_frame = link->cur_buf->video->interlaced;\r
157                 frame->top_field_first  = link->cur_buf->video->top_field_first;\r
158                 frame->key_frame                = link->cur_buf->video->key_frame;\r
159 \r
160                 return frame;\r
161         }\r
162 };\r
163 \r
164 filter::filter(const std::string& filters) : impl_(new implementation(filters)){}\r
165 void filter::push(const safe_ptr<AVFrame>& frame) {impl_->push(frame);}\r
166 std::vector<safe_ptr<AVFrame>> filter::poll() {return impl_->poll();}\r
167 bool filter::is_ready() const{return impl_->graph_ != nullptr;}\r
168 \r
169 }