@@ -345,69 +345,6 @@ func createAnalysisFilterGraph(
345345 decCtx * ffmpeg.AVCodecContext ,
346346 targetI , targetTP , targetLRA float64 ,
347347) (* ffmpeg.AVFilterGraph , * ffmpeg.AVFilterContext , * ffmpeg.AVFilterContext , error ) {
348-
349- filterGraph := ffmpeg .AVFilterGraphAlloc ()
350- if filterGraph == nil {
351- return nil , nil , nil , fmt .Errorf ("failed to allocate filter graph" )
352- }
353-
354- // Get abuffer and abuffersink filters
355- bufferSrc := ffmpeg .AVFilterGetByName (ffmpeg .GlobalCStr ("abuffer" ))
356- bufferSink := ffmpeg .AVFilterGetByName (ffmpeg .GlobalCStr ("abuffersink" ))
357- if bufferSrc == nil || bufferSink == nil {
358- ffmpeg .AVFilterGraphFree (& filterGraph )
359- return nil , nil , nil , fmt .Errorf ("abuffer or abuffersink filter not found" )
360- }
361-
362- // Get channel layout description
363- layoutPtr := ffmpeg .AllocCStr (64 )
364- defer layoutPtr .Free ()
365-
366- if _ , err := ffmpeg .AVChannelLayoutDescribe (decCtx .ChLayout (), layoutPtr , 64 ); err != nil {
367- ffmpeg .AVFilterGraphFree (& filterGraph )
368- return nil , nil , nil , fmt .Errorf ("failed to get channel layout: %w" , err )
369- }
370-
371- // Create abuffer source
372- pktTimebase := decCtx .PktTimebase ()
373- args := fmt .Sprintf (
374- "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=%s" ,
375- pktTimebase .Num (), pktTimebase .Den (),
376- decCtx .SampleRate (),
377- ffmpeg .AVGetSampleFmtName (decCtx .SampleFmt ()).String (),
378- layoutPtr .String (),
379- )
380-
381- argsC := ffmpeg .ToCStr (args )
382- defer argsC .Free ()
383-
384- var bufferSrcCtx * ffmpeg.AVFilterContext
385- if _ , err := ffmpeg .AVFilterGraphCreateFilter (
386- & bufferSrcCtx ,
387- bufferSrc ,
388- ffmpeg .GlobalCStr ("in" ),
389- argsC ,
390- nil ,
391- filterGraph ,
392- ); err != nil {
393- ffmpeg .AVFilterGraphFree (& filterGraph )
394- return nil , nil , nil , fmt .Errorf ("failed to create abuffer: %w" , err )
395- }
396-
397- // Create abuffersink
398- var bufferSinkCtx * ffmpeg.AVFilterContext
399- if _ , err := ffmpeg .AVFilterGraphCreateFilter (
400- & bufferSinkCtx ,
401- bufferSink ,
402- ffmpeg .GlobalCStr ("out" ),
403- nil ,
404- nil ,
405- filterGraph ,
406- ); err != nil {
407- ffmpeg .AVFilterGraphFree (& filterGraph )
408- return nil , nil , nil , fmt .Errorf ("failed to create abuffersink: %w" , err )
409- }
410-
411348 // Build filter string for analysis pass
412349 // astats provides noise floor and dynamic range measurements for adaptive gate and compression
413350 // aspectralstats measures spectral centroid and rolloff for adaptive de-esser targeting
@@ -417,35 +354,5 @@ func createAnalysisFilterGraph(
417354 filterSpec := fmt .Sprintf ("astats=metadata=1:measure_overall=Noise_floor+Dynamic_range+RMS_level+Peak_level,aspectralstats=win_size=2048:win_func=hann:measure=centroid+rolloff,ebur128=metadata=1:target=%.0f" ,
418355 targetI )
419356
420- // Parse filter graph
421- outputs := ffmpeg .AVFilterInoutAlloc ()
422- inputs := ffmpeg .AVFilterInoutAlloc ()
423- defer ffmpeg .AVFilterInoutFree (& outputs )
424- defer ffmpeg .AVFilterInoutFree (& inputs )
425-
426- outputs .SetName (ffmpeg .ToCStr ("in" ))
427- outputs .SetFilterCtx (bufferSrcCtx )
428- outputs .SetPadIdx (0 )
429- outputs .SetNext (nil )
430-
431- inputs .SetName (ffmpeg .ToCStr ("out" ))
432- inputs .SetFilterCtx (bufferSinkCtx )
433- inputs .SetPadIdx (0 )
434- inputs .SetNext (nil )
435-
436- filterSpecC := ffmpeg .ToCStr (filterSpec )
437- defer filterSpecC .Free ()
438-
439- if _ , err := ffmpeg .AVFilterGraphParsePtr (filterGraph , filterSpecC , & inputs , & outputs , nil ); err != nil {
440- ffmpeg .AVFilterGraphFree (& filterGraph )
441- return nil , nil , nil , fmt .Errorf ("failed to parse filter graph: %w" , err )
442- }
443-
444- // Configure filter graph
445- if _ , err := ffmpeg .AVFilterGraphConfig (filterGraph , nil ); err != nil {
446- ffmpeg .AVFilterGraphFree (& filterGraph )
447- return nil , nil , nil , fmt .Errorf ("failed to configure filter graph: %w" , err )
448- }
449-
450- return filterGraph , bufferSrcCtx , bufferSinkCtx , nil
357+ return setupFilterGraph (decCtx , filterSpec )
451358}
0 commit comments