Log in

View Full Version : Memory leak on images (Avisynth + FFMpeg) Part 2


FranceBB
31st October 2023, 19:14
Some of you might remember the problem I faced a year and a half ago with BlankClip(length=0).
Since then, I made absolutely sure that all the scripts I was creating were created with BlankClip(length=1).
I've been living happily since then, but now the problem is back and for some different reason apparently... :(


The input is a simple file in H.264 lossless lasting 1 frame that has been automatically generated by other systems starting from a still .jpg image: https://we.tl/t-4itY7pznbY


General
Unique ID : 165233434693012697505000378656689019651 (0x7C4ECD4DD5D6EFFC697559114B54DB03)
Complete name : 1-6-6~5332406~2716~20171130-160622-775-39AAE0DAD704~dec_avmedia_v1.mkv
Format : Matroska
Format version : Version 4
File size : 83.0 KiB
Duration : 40 ms
Overall bit rate : 17.0 Mb/s
Frame rate : 25.000 FPS
Writing application : Lavf60.15.100
Writing library : Lavf60.15.100
ErrorDetectionType : Per level 1

Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High 4:4:4 Intra@L2.1
Format settings, CABAC : No
Codec ID : V_MPEG4/ISO/AVC
Duration : 40 ms
Width : 512 pixels
Height : 288 pixels
Display aspect ratio : 16:9
Frame rate mode : Constant
Frame rate : 25.000 FPS
Color space : YUV
Chroma subsampling : 4:2:2
Bit depth : 8 bits
Scan type : Progressive
Writing library : x264 core 164 r3155 9c3c716
Encoding settings : cabac=0 / ref=1 / deblock=0:0:0 / analyse=0:0 / me=dia / subme=0 / psy=0 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=0 / chroma_qp_offset=0 / threads=9 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=0 / weightp=0 / keyint=1 / keyint_min=1 / scenecut=0 / intra_refresh=0 / rc=cqp / mbtree=0 / qp=0
Default : No
Forced : No
Color range : Full
Matrix coefficients : BT.470 System B/G



That file is then indexed using the creation of an automatic Avisynth Script made by the system as such:


_ffas_video = "Image.JPG"
_ffas_audio = "Image.JPG"
_ffas_width = 512
_ffas_height = 288
_ffas_work_fdr = "20231031-1747-3338-479c-5fbf58300be4"
Import("mtmodes.avsi")
LoadPlugin("ffms2.dll")
LoadPlugin("BestAudioSource.dll")
LoadPlugin("plugins_JPSDR.dll")
FFIndex("1-6-6~5332406~2716~20171130-160622-775-39AAE0DAD704~dec_avmedia_v1.mkv", cachefile = "1-6-6~231031174754290~2716~20171130-160622-775-39AAE0DAD704~dec_avmedia~ffindex.dat")
video = FFVideoSource("1-6-6~5332406~2716~20171130-160622-775-39AAE0DAD704~dec_avmedia_v1.mkv", 0, cachefile = "1-6-6~231031174754290~2716~20171130-160622-775-39AAE0DAD704~dec_avmedia~ffindex.dat", fpsnum=25 , fpsden=1, seekmode = 1)
audio_null = BlankClip(length=1, width=512, height=288, color=$000000, channels=1, audio_rate=48000, fps=25)
audio = audio_null
Global m_clip = AudioDub(video, audio)
m_clip = ConvertToYUV422(m_clip)
m_clip = ConvertBits(m_clip, 8)
m_clip = AssumeFieldBased(m_clip)
m_clip = AssumeFieldBased(m_clip)
m_clip = propSet(m_clip, "_FieldBased", 0)
Import("SafeColorLimiter.avsi")
m_clip = SafeColorLimiter(m_clip)
Return m_clip


The .avs has no issues at all in AVSPmod mod and VirtualDub so much so that I can see it and play it just fine:


https://i.imgur.com/S4R5kH5.png

https://i.imgur.com/eBStQSt.png


When I tried to use it with the following automatically created FFMpeg command line to encode it in XDCAM-50, however, it lead to a memory leak until the whole system crashed:



"ffmpeg.exe" -stats_period 1.2 -hide_banner -i "1-6-6~231031174804286~2716~20220412-1017-4254-448c-0e506b117754~enc_av_xdcamhd.avs" -f lavfi -i "aevalsrc=0" -f lavfi -i "color=color=black:size=1920x1080" -shortest -map_metadata -1 -map 0:0 -filter_complex "[0:1]pan=1|c0=c0[a1],[0:1]pan=1|c0=0*c0[a2],[0:1]pan=1|c0=0*c0[a3],[0:1]pan=1|c0=0*c0[a4],[0:1]pan=1|c0=0*c0[a5],[0:1]pan=1|c0=0*c0[a6],[0:1]pan=1|c0=0*c0[a7],[0:1]pan=1|c0=0*c0[a8],[a1]amerge=1,apad[astr1],[a2]amerge=1,apad[astr2],[a3]amerge=1,apad[astr3],[a4]amerge=1,apad[astr4],[a5]amerge=1,apad[astr5],[a6]amerge=1,apad[astr6],[a7]amerge=1,apad[astr7],[a8]amerge=1,apad[astr8]" -map "[astr1]" -c:a:0 pcm_s24le -ar:a:0 48000 -map "[astr2]" -c:a:1 pcm_s24le -ar:a:1 48000 -map "[astr3]" -c:a:2 pcm_s24le -ar:a:2 48000 -map "[astr4]" -c:a:3 pcm_s24le -ar:a:3 48000 -map "[astr5]" -c:a:4 pcm_s24le -ar:a:4 48000 -map "[astr6]" -c:a:5 pcm_s24le -ar:a:5 48000 -map "[astr7]" -c:a:6 pcm_s24le -ar:a:6 48000 -map "[astr8]" -c:a:7 pcm_s24le -ar:a:7 48000 -vf "sidedata=delete,metadata=delete,setrange=pc,colorspace=fast=1:ispace=bt470bg:itrc=bt709:iprimaries=bt709:all=bt709,bwdif=mode=1:parity=0,scale=w=1920:h=1080:in_range=pc:out_range=tv:flags=lanczos,framerate=50,tinterlace=mode=4,setfield=tff,setsar=r=1:max=1" -timecode 00:00:00:00 -c:v mpeg2video -r 25/1 -pix_fmt yuv422p -b:v 50000000 -minrate 50000000 -maxrate 50000000 -intra_vlc 1 -dc 10 -g 12 -bf 2 -mpv_flags +strict_gop -qmin 5 -qmax 28 -qsquish 99 -color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv -seq_disp_ext 1 -video_format component -chroma_sample_location topleft -signal_standard 4 -field_order tt -top 1 -alternate_scan 1 -flags +ildct+ilme+cgop -sc_threshold 1000000000 -intra_matrix 8,10,22,27,29,37,37,40,9,12,14,28,29,37,39,40,9,14,27,31,34,37,40,48,12,22,27,29,34,37,40,58,26,27,29,34,37,38,48,58,26,27,29,36,38,38,48,69,18,27,34,36,38,38,48,69,26,26,34,34,38,40,58,79 -inter_matrix 16,20,22,26,28,32,32,36,18,20,22,28,28,32,34,36,18,22,26,30,30,32,36,38,20,22,26,28,30,32,36,42,24,26,28,30,32,34,38,40,24,26,28,32,34,34,38,42,24,26,30,32,34,34,38,42,24,24,30,30,34,36,40,44 -non_linear_quant 1 -f mxf -max_muxing_queue_size 700 -map_metadata -1 -metadata "creation_time=now" -y "output.mxf"

pause


https://i.imgur.com/MsKqWyw.png
https://i.imgur.com/oaX4lNu.png


So I was wondering: what the heck is wrong with it?
I started experimenting a bit and I found out that it wasn't related to the codec, in fact it also happened with the same command line but using v308 lossless:


"ffmpeg.exe" -analyzeduration 33554432 -stats_period 1.2 -hide_banner -i "1-6-6~231031174804286~2716~20220412-1017-4254-448c-0e506b117754~enc_av_xdcamhd.avs" -f lavfi -i "aevalsrc=0" -f lavfi -i "color=color=black:size=1920x1080" -shortest -map_metadata -1 -map 0:0 -filter_complex "[0:1]pan=1|c0=c0[a1],[0:1]pan=1|c0=0*c0[a2],[0:1]pan=1|c0=0*c0[a3],[0:1]pan=1|c0=0*c0[a4],[0:1]pan=1|c0=0*c0[a5],[0:1]pan=1|c0=0*c0[a6],[0:1]pan=1|c0=0*c0[a7],[0:1]pan=1|c0=0*c0[a8],[a1]amerge=1,apad[astr1],[a2]amerge=1,apad[astr2],[a3]amerge=1,apad[astr3],[a4]amerge=1,apad[astr4],[a5]amerge=1,apad[astr5],[a6]amerge=1,apad[astr6],[a7]amerge=1,apad[astr7],[a8]amerge=1,apad[astr8]" -map "[astr1]" -c:a:0 pcm_s24le -ar:a:0 48000 -map "[astr2]" -c:a:1 pcm_s24le -ar:a:1 48000 -map "[astr3]" -c:a:2 pcm_s24le -ar:a:2 48000 -map "[astr4]" -c:a:3 pcm_s24le -ar:a:3 48000 -map "[astr5]" -c:a:4 pcm_s24le -ar:a:4 48000 -map "[astr6]" -c:a:5 pcm_s24le -ar:a:5 48000 -map "[astr7]" -c:a:6 pcm_s24le -ar:a:6 48000 -map "[astr8]" -c:a:7 pcm_s24le -ar:a:7 48000 -vf "sidedata=delete,metadata=delete,setrange=pc,colorspace=fast=1:ispace=bt470bg:itrc=bt709:iprimaries=bt709:all=bt709,bwdif=mode=1:parity=0,scale=w=1920:h=1080:in_range=pc:out_range=tv:flags=lanczos,framerate=50,tinterlace=mode=4,setfield=tff,setsar=r=1:max=1" -timecode 00:00:00:00 -c:v v308 -pix_fmt yuv444p -color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range pc -timecode 00:00:00:00 -max_muxing_queue_size 700 -map_metadata -1 -metadata "creation_time=now" -y "output.mkv"

pause


so I tried to remove the -vf entirely:


"ffmpeg.exe" -analyzeduration 33554432 -stats_period 1.2 -hide_banner -i "1-6-6~231031174804286~2716~20220412-1017-4254-448c-0e506b117754~enc_av_xdcamhd.avs" -f lavfi -i "aevalsrc=0" -f lavfi -i "color=color=black:size=1920x1080" -shortest -map_metadata -1 -map 0:0 -filter_complex "[0:1]pan=1|c0=c0[a1],[0:1]pan=1|c0=0*c0[a2],[0:1]pan=1|c0=0*c0[a3],[0:1]pan=1|c0=0*c0[a4],[0:1]pan=1|c0=0*c0[a5],[0:1]pan=1|c0=0*c0[a6],[0:1]pan=1|c0=0*c0[a7],[0:1]pan=1|c0=0*c0[a8],[a1]amerge=1,apad[astr1],[a2]amerge=1,apad[astr2],[a3]amerge=1,apad[astr3],[a4]amerge=1,apad[astr4],[a5]amerge=1,apad[astr5],[a6]amerge=1,apad[astr6],[a7]amerge=1,apad[astr7],[a8]amerge=1,apad[astr8]" -map "[astr1]" -c:a:0 pcm_s24le -ar:a:0 48000 -map "[astr2]" -c:a:1 pcm_s24le -ar:a:1 48000 -map "[astr3]" -c:a:2 pcm_s24le -ar:a:2 48000 -map "[astr4]" -c:a:3 pcm_s24le -ar:a:3 48000 -map "[astr5]" -c:a:4 pcm_s24le -ar:a:4 48000 -map "[astr6]" -c:a:5 pcm_s24le -ar:a:5 48000 -map "[astr7]" -c:a:6 pcm_s24le -ar:a:6 48000 -map "[astr8]" -c:a:7 pcm_s24le -ar:a:7 48000 -timecode 00:00:00:00 -c:v v308 -pix_fmt yuv444p -color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range pc -timecode 00:00:00:00 -max_muxing_queue_size 700 -map_metadata -1 -metadata "creation_time=now" -y "output.mkv"

pause



and it worked.
One by one I tried to remove every single filter until I found out the guilty one: tinterlace!
This very same command line without tinterlace worked:

"ffmpeg.exe" -analyzeduration 33554432 -stats_period 1.2 -hide_banner -i "\\mibctvan000\Ingest\MEDIA\temp\20231031-1747-3338-479c-5fbf58300be4\1-6-6~231031174804286~2716~20220412-1017-4254-448c-0e506b117754~enc_av_xdcamhd.avs" -f lavfi -i "aevalsrc=0" -f lavfi -i "color=color=black:size=1920x1080" -shortest -map_metadata -1 -map 0:0 -filter_complex "[0:1]pan=1|c0=c0[a1],[0:1]pan=1|c0=0*c0[a2],[0:1]pan=1|c0=0*c0[a3],[0:1]pan=1|c0=0*c0[a4],[0:1]pan=1|c0=0*c0[a5],[0:1]pan=1|c0=0*c0[a6],[0:1]pan=1|c0=0*c0[a7],[0:1]pan=1|c0=0*c0[a8],[a1]amerge=1,apad[astr1],[a2]amerge=1,apad[astr2],[a3]amerge=1,apad[astr3],[a4]amerge=1,apad[astr4],[a5]amerge=1,apad[astr5],[a6]amerge=1,apad[astr6],[a7]amerge=1,apad[astr7],[a8]amerge=1,apad[astr8]" -map "[astr1]" -c:a:0 pcm_s24le -ar:a:0 48000 -map "[astr2]" -c:a:1 pcm_s24le -ar:a:1 48000 -map "[astr3]" -c:a:2 pcm_s24le -ar:a:2 48000 -map "[astr4]" -c:a:3 pcm_s24le -ar:a:3 48000 -map "[astr5]" -c:a:4 pcm_s24le -ar:a:4 48000 -map "[astr6]" -c:a:5 pcm_s24le -ar:a:5 48000 -map "[astr7]" -c:a:6 pcm_s24le -ar:a:6 48000 -map "[astr8]" -c:a:7 pcm_s24le -ar:a:7 48000 -vf "sidedata=delete,metadata=delete,setrange=pc,colorspace=fast=1:ispace=bt470bg:itrc=bt709:iprimaries=bt709:all=bt709,bwdif=mode=1:parity=0,scale=w=1920:h=1080:in_range=pc:out_range=tv:flags=lanczos,framerate=50,setsar=r=1:max=1" -timecode 00:00:00:00 -c:v v308 -pix_fmt yuv444p -color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range pc -timecode 00:00:00:00 -max_muxing_queue_size 700 -map_metadata -1 -metadata "creation_time=now" -y "output.mkv"

pause

so the question is: why?
Why does adding tinterlace=mode=4 in the filterchain cause the continuous RAM allocation and the encode to never finish?

richardpl
31st October 2023, 19:39
Try running visual (a)graphmonitor filter within same filtergraph of FFmpeg, there might be leak in filters processing. And watch that video output for red font numbers.

wonkey_monkey
31st October 2023, 20:38
I'm confused, Image.JPG is only put into those first two variables then they aren't referenced anywhere else... does one of the filters read the variables directly?

FranceBB
31st October 2023, 22:19
I'm confused, Image.JPG is only put into those first two variables then they aren't referenced anywhere else... does one of the filters read the variables directly?

That is because I'm an idiot.
The reference to the .jpg is only kept as a name, however such a jpeg has been re-encoded to H.264 lossless by another processor and that's actually what is being fed to the Indexer.
I updated the post with the appropriate description, though.

FranceBB
31st October 2023, 22:52
After experiencing and testing and testing and testing and testing I found out what was causing it: tinterlace!
I updated the first post with my findings, but I don't really understand why.
Why is tinterlace causing this? Should I open a ticket in the FFMpeg bug tracker?