1 #include "../../stdafx.h"
\r
5 #include "parallel_yadif.h"
\r
7 #include "../../ffmpeg_error.h"
\r
9 #include <boost/assign.hpp>
\r
14 #if defined(_MSC_VER)
\r
15 #pragma warning (push)
\r
16 #pragma warning (disable : 4244)
\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/buffersink.h>
\r
26 #include <libavfilter/vsrc_buffer.h>
\r
28 #if defined(_MSC_VER)
\r
29 #pragma warning (pop)
\r
32 namespace caspar { namespace ffmpeg {
\r
34 struct filter::implementation
\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
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
48 if(pix_fmts_.empty())
\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
62 pix_fmts_.push_back(PIX_FMT_NONE);
\r
64 std::transform(filters_.begin(), filters_.end(), filters_.begin(), ::tolower);
\r
67 std::vector<safe_ptr<AVFrame>> execute(const std::shared_ptr<AVFrame>& frame)
\r
70 return std::vector<safe_ptr<AVFrame>>();
\r
72 if(filters_.empty())
\r
73 return boost::assign::list_of(frame);
\r
79 void push(const std::shared_ptr<AVFrame>& frame)
\r
83 graph_.reset(avfilter_graph_alloc(), [](AVFilterGraph* p){avfilter_graph_free(&p);});
\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
90 // OPIX_FMT_BGRAutput
\r
91 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
\r
92 buffersink_params->pixel_fmts = pix_fmts_.data();
\r
93 THROW_ON_ERROR2(avfilter_graph_create_filter(&buffersink_ctx_, avfilter_get_by_name("buffersink"), "out", NULL, buffersink_params, graph_.get()), "[filter]");
\r
95 AVFilterInOut* outputs = avfilter_inout_alloc();
\r
96 AVFilterInOut* inputs = avfilter_inout_alloc();
\r
98 outputs->name = av_strdup("in");
\r
99 outputs->filter_ctx = buffersrc_ctx_;
\r
100 outputs->pad_idx = 0;
\r
101 outputs->next = NULL;
\r
103 inputs->name = av_strdup("out");
\r
104 inputs->filter_ctx = buffersink_ctx_;
\r
105 inputs->pad_idx = 0;
\r
106 inputs->next = NULL;
\r
108 THROW_ON_ERROR2(avfilter_graph_parse(graph_.get(), filters_.c_str(), &inputs, &outputs, NULL), "[filter]");
\r
110 avfilter_inout_free(&inputs);
\r
111 avfilter_inout_free(&outputs);
\r
113 THROW_ON_ERROR2(avfilter_graph_config(graph_.get(), NULL), "[filter]");
\r
115 for(size_t n = 0; n < graph_->filter_count; ++n)
\r
117 auto filter_name = graph_->filters[n]->name;
\r
118 if(strstr(filter_name, "yadif") != 0)
\r
119 parallel_yadif_ctx_ = make_parallel_yadif(graph_->filters[n]);
\r
123 THROW_ON_ERROR2(av_vsrc_buffer_add_frame(buffersrc_ctx_, frame.get(), 0), "[filter]");
\r
126 std::vector<safe_ptr<AVFrame>> poll()
\r
128 std::vector<safe_ptr<AVFrame>> result;
\r
133 while (avfilter_poll_frame(buffersink_ctx_->inputs[0]))
\r
135 AVFilterBufferRef *picref;
\r
136 THROW_ON_ERROR2(av_buffersink_get_buffer_ref(buffersink_ctx_, &picref, 0), "[filter]");
\r
140 safe_ptr<AVFrame> frame(avcodec_alloc_frame(), [=](AVFrame* p)
\r
143 avfilter_unref_buffer(picref);
\r
146 avcodec_get_frame_defaults(frame.get());
\r
148 memcpy(frame->data, picref->data, sizeof(frame->data));
\r
149 memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
\r
150 frame->format = picref->format;
\r
151 frame->width = picref->video->w;
\r
152 frame->height = picref->video->h;
\r
153 frame->pkt_pos = picref->pos;
\r
154 frame->interlaced_frame = picref->video->interlaced;
\r
155 frame->top_field_first = picref->video->top_field_first;
\r
156 frame->key_frame = picref->video->key_frame;
\r
157 frame->pict_type = picref->video->pict_type;
\r
158 frame->sample_aspect_ratio = picref->video->sample_aspect_ratio;
\r
160 result.push_back(frame);
\r
168 filter::filter(const std::wstring& filters, const std::vector<PixelFormat>& pix_fmts) : impl_(new implementation(filters, pix_fmts)){}
\r
169 filter::filter(filter&& other) : impl_(std::move(other.impl_)){}
\r
170 filter& filter::operator=(filter&& other){impl_ = std::move(other.impl_); return *this;}
\r
171 std::vector<safe_ptr<AVFrame>> filter::execute(const std::shared_ptr<AVFrame>& frame) {return impl_->execute(frame);}
\r