ZacS

Zac

Nov 22nd, 2011
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool VideoEncodeInternal::SetupFilters(bool bFlipH, bool bFlipV,
  2.                        bool bCrop, int nCropX, int nCropY, int nCropWidth, int nCropHeight,
  3.                        bool bScale, int nScaleWidth, int nScaleHeight,
  4.                        bool bCropFirst)
  5. {
  6.     //Check that VideoEncodeInternal has been initialised
  7.     if (!m_bInitialised)
  8.         return false;
  9.  
  10.     //Check that VideoStream & CodecContext have been created
  11.     if (!m_pVideoStream)
  12.         return false;
  13.     if (!m_pVideoStream->codec)
  14.         return false;
  15.  
  16.     AVCodecContext* pCodecContext = m_pVideoStream->codec;
  17.     int nReturn = 0;
  18.  
  19.     //Setup Filter Graph
  20.     if (m_pFilterGraph)
  21.         return false;
  22.  
  23.     m_pFilterGraph = avfilter_graph_alloc();
  24.     if (!m_pFilterGraph)
  25.         return false;
  26.  
  27.     //Setup Filter Input (buffer)
  28.     char szBufferArgs[255];
  29.  
  30.     AVRational sample_aspect_ratio;
  31.     sample_aspect_ratio.num = 1;
  32.     sample_aspect_ratio.den = 1;
  33.  
  34.     sprintf_s(szBufferArgs, 255,
  35.               "%d:%d:%d:%d:%d:%d:%d",
  36.               m_nWidth,
  37.               m_nHeight,
  38.               PIX_FMT_YUV420P,
  39.               1, AV_TIME_BASE,
  40.               sample_aspect_ratio.num, sample_aspect_ratio.den);
  41.  
  42.     nReturn = avfilter_graph_create_filter(&m_pInputFilter, avfilter_get_by_name("buffer"),
  43.                            "src", szBufferArgs, NULL, m_pFilterGraph);
  44.     if (nReturn < 0)
  45.         return false;
  46.  
  47.     //Setup Filter Output (buffersink)
  48.     enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
  49.     AVBufferSinkParams* buffersink_params = av_buffersink_params_alloc();
  50.  
  51.     buffersink_params->pixel_fmts = pix_fmts;
  52.     nReturn = avfilter_graph_create_filter(&m_pOutputFilter, avfilter_get_by_name("buffersink"),
  53.                            "out", NULL, buffersink_params, m_pFilterGraph);
  54.  
  55.     av_freep(&buffersink_params);
  56.     if (nReturn < 0)
  57.         return false;
  58.  
  59.     //Setup sws_scale flags
  60.     if (m_pSwsContext)
  61.     {
  62.         int64_t sws_flags = 0;
  63.         char szSwsScaleArgs[64];
  64.         memset(szSwsScaleArgs, 0, 64);
  65.         av_opt_get_int(m_pSwsContext, "sws_flags", 0, &sws_flags);
  66.         sprintf_s(szSwsScaleArgs, 64, "flags=0x%X", (unsigned)sws_flags);
  67.         m_pFilterGraph->scale_sws_opts = av_strdup(szSwsScaleArgs);
  68.     }
  69.    
  70.  
  71.     AVFilterInOut *outputs = avfilter_inout_alloc();
  72.     AVFilterInOut *inputs  = avfilter_inout_alloc();
  73.  
  74.     outputs->name    = av_strdup("in");
  75.     outputs->filter_ctx = m_pInputFilter;//last_filter;
  76.     outputs->pad_idx = 0;
  77.     outputs->next    = NULL;
  78.  
  79.     inputs->name    = av_strdup("out");
  80.     inputs->filter_ctx = m_pOutputFilter;
  81.     inputs->pad_idx = 0;
  82.     inputs->next    = NULL;
  83.  
  84.     if ((nReturn = avfilter_graph_parse(m_pFilterGraph, "hflip", &inputs, &outputs, NULL)) < 0)
  85.         return false;
  86.  
  87.     fflush(stderr);
  88.  
  89.     if ((nReturn = avfilter_graph_config(m_pFilterGraph, NULL)) < 0)
  90.     {
  91.         char szError[255];
  92.         av_strerror(nReturn, szError, 255);
  93.         printf(szError);
  94.         return false;
  95.     }
  96.  
  97.     pCodecContext->width  = m_pOutputFilter->inputs[0]->w;
  98.     pCodecContext->height = m_pOutputFilter->inputs[0]->h;
  99.     pCodecContext->sample_aspect_ratio = m_pOutputFilter->inputs[0]->sample_aspect_ratio;
  100.  
  101.     return true;
  102. }
RAW Paste Data