Advertisement
Guest User

Untitled

a guest
Nov 28th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1.     int result;
  2.  
  3.     avfilter_register_all();
  4.     AVFilter* buffer_src = avfilter_get_by_name("buffer");          // Input to the filter chain
  5.     AVFilter* buffer_sink = avfilter_get_by_name("ffbuffersink");   // Output from the filter chain
  6.     AVBufferSinkParams* buffer_sink_params;
  7.     AVFilterInOut* outputs = avfilter_inout_alloc();
  8.     AVFilterInOut* inputs = avfilter_inout_alloc();
  9.  
  10.     // Supported output pixel format is only YUV420P
  11.     enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
  12.  
  13.     // Initialize filter graph
  14.     this->filter_graph = avfilter_graph_alloc();
  15.  
  16.     // Create input pad
  17.     std::string args = (boost::format("video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d") % this->input_codec_context->width
  18.                                                                                                         % this->input_codec_context->height
  19.                                                                                                         % this->input_codec_context->pix_fmt
  20.                                                                                                         % this->input_time_base.num
  21.                                                                                                         % this->input_time_base.den
  22.                                                                                                         % this->input_codec_context->sample_aspect_ratio.num
  23.                                                                                                         % this->input_codec_context->sample_aspect_ratio.den).str();
  24.     result = avfilter_graph_create_filter(&buffersrc_ctx, buffer_src, "in", args.c_str(), NULL, filter_graph);
  25.     if (result < 0)
  26.     {
  27.         LOG4CXX_FATAL(logger, "Could not create filter chain input sink: " + std::to_string(result));
  28.         throw std::runtime_error("Could not create filter chain input sink: " + std::to_string(result));
  29.     }
  30.  
  31.     // Create output pad
  32.     buffer_sink_params = av_buffersink_params_alloc();
  33.     buffer_sink_params->pixel_fmts = pix_fmts;
  34.     result = avfilter_graph_create_filter(&buffersink_ctx, buffer_sink, "out", NULL, buffer_sink_params, filter_graph);
  35.     av_free(buffer_sink_params);
  36.  
  37.     if (result < 0)
  38.     {
  39.         LOG4CXX_FATAL(logger, "Could not create filter chain output sink: " + std::to_string(result));
  40.         throw std::runtime_error("Could not create filter chain output sink: " + std::to_string(result));
  41.     }
  42.  
  43.     // Register source and sink
  44.     outputs->name = av_strdup("in");
  45.     outputs->filter_ctx = this->buffersrc_ctx;
  46.     outputs->pad_idx = 0;
  47.     outputs->next = NULL;
  48.  
  49.     inputs->name = av_strdup("out");
  50.     inputs->filter_ctx = this->buffersink_ctx;
  51.     inputs->pad_idx = 0;
  52.     inputs->next = NULL;
  53.  
  54.     // Now initialize actual filters for scaling and deinterlacing
  55.     std::string filters = (boost::format("yadif,scale=%d:%d:flags=512,setpts=PTS") % this->output_codec_context->width % this->output_codec_context->height).str();
  56.  
  57.     LOG4CXX_INFO(logger, "Configuring filter chain with " + filters);
  58.  
  59.     result = avfilter_graph_parse(filter_graph, filters.c_str(), &inputs, &outputs, NULL);
  60.     if (result < 0)
  61.     {
  62.         LOG4CXX_FATAL(logger, "Could not parse filter chain: " + filters + " ERR: " + std::to_string(result));
  63.         throw std::runtime_error("Could not parse filter chain: " + filters + " ERR: " + std::to_string(result));
  64.     }
  65.  
  66.     result = avfilter_graph_config(filter_graph, NULL);
  67.     if (result < 0)
  68.     {
  69.         LOG4CXX_FATAL(logger, "Could not configure filter chain: " + filters + " ERR: " + std::to_string(result));
  70.         throw std::runtime_error("Could not configure filter chain: " + filters + " ERR: " + std::to_string(result));
  71.     }
  72.  
  73.     LOG4CXX_INFO(logger, "Filters configured.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement