1 #include "../../stdafx.h"
\r
5 #include "parallel_yadif.h"
\r
7 #include "../../ffmpeg_error.h"
\r
9 #include <common/exception/exceptions.h>
\r
11 #include <boost/assign.hpp>
\r
16 #if defined(_MSC_VER)
\r
17 #pragma warning (push)
\r
18 #pragma warning (disable : 4244)
\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
30 #if defined(_MSC_VER)
\r
31 #pragma warning (pop)
\r
34 namespace caspar { namespace ffmpeg {
\r
36 struct filter::implementation
\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
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
55 if(pix_fmts_.empty())
\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
69 pix_fmts_.push_back(PIX_FMT_NONE);
\r
71 std::transform(filters_.begin(), filters_.end(), filters_.begin(), ::tolower);
\r
74 void push(const std::shared_ptr<AVFrame>& frame)
\r
79 if(frame->data[0] == nullptr || frame->width < 1)
\r
80 BOOST_THROW_EXCEPTION(invalid_argument());
\r
82 if(filters_.empty())
\r
84 bypass_.push(frame);
\r
94 in_format_ = frame->format;
\r
95 graph_.reset(avfilter_graph_alloc(), [](AVFilterGraph* p){avfilter_graph_free(&p);});
\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
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
106 AVFilterInOut* outputs = avfilter_inout_alloc();
\r
107 AVFilterInOut* inputs = avfilter_inout_alloc();
\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
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
119 THROW_ON_ERROR2(avfilter_graph_parse(graph_.get(), filters_.c_str(), &inputs, &outputs, NULL), "[filter]");
\r
121 avfilter_inout_free(&inputs);
\r
122 avfilter_inout_free(&outputs);
\r
124 THROW_ON_ERROR2(avfilter_graph_config(graph_.get(), NULL), "[filter]");
\r
126 for(size_t n = 0; n < graph_->filter_count; ++n)
\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
140 THROW_ON_ERROR2(av_vsrc_buffer_add_frame(buffersrc_ctx_, frame.get(), 0), "[filter]");
\r
142 catch(ffmpeg_error&)
\r
148 BOOST_THROW_EXCEPTION(ffmpeg_error() << boost::errinfo_nested_exception(boost::current_exception()));
\r
152 std::shared_ptr<AVFrame> poll()
\r
154 if(filters_.empty())
\r
156 if(bypass_.empty())
\r
158 auto frame = bypass_.front();
\r
168 if(avfilter_poll_frame(buffersink_ctx_->inputs[0]))
\r
170 AVFilterBufferRef *picref;
\r
171 THROW_ON_ERROR2(av_buffersink_get_buffer_ref(buffersink_ctx_, &picref, 0), "[filter]");
\r
175 safe_ptr<AVFrame> frame(avcodec_alloc_frame(), [=](AVFrame* p)
\r
178 avfilter_unref_buffer(picref);
\r
181 avcodec_get_frame_defaults(frame.get());
\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
195 // HACK: Workaround for missing format bug in libavfilter.
\r
196 if(frame->format == 0)
\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
203 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("libavfilter bug: frame format == 0."));
\r
207 CASPAR_LOG(warning) << "[ffmpeg] libavfilter bug workaround: frame format == 0, guessing PIX_FMT_YUV420P.";
\r
216 catch(ffmpeg_error&)
\r
222 BOOST_THROW_EXCEPTION(ffmpeg_error() << boost::errinfo_nested_exception(boost::current_exception()));
\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
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