JuliaIO / VideoIO.jl

Reading and writing of video files in Julia via ffmpeg
https://juliaio.github.io/VideoIO.jl/stable
Other
125 stars 53 forks source link

Enable adjustable codec multithreading (significant read and write speed up!) #331

Closed IanButterworth closed 2 years ago

IanButterworth commented 2 years ago

I had assumed that ffmpeg/h.264 automatically handled multithreading, but it seems not to be the case. The small change in this PR is based on https://stackoverflow.com/questions/43251612/ffmpeg-how-to-use-multithreading

I propose that the default is set to Threads.nthreads() but that users can tune it as they wish.

Master

julia> function foo()
    f = openvideo("vid.mp4")
    i = 1
    img = read(f)
    i += 1 
    while !eof(f)
        read!(f, img)
        i += 1
    end
    close(f)
    @show i
end

julia> @time foo();
i = 1789
 27.231079 seconds (1.91 k allocations: 9.032 MiB, 0.03% gc time)

This PR

Where Threads.nthreads() == 16

julia> @time foo();
i = 1789
  3.339067 seconds (1.91 k allocations: 9.032 MiB)

Saves about 1 minute (6 -> 5 mins on ubuntu) in CI with only 2 threads

IanButterworth commented 2 years ago

Turns out it also helps encoding, but to a lesser extent.. only halves the time.. :)

julia> imgstack = map(_->rand(UInt8, 1000,1000), 1:1000);

julia> @time VideoIO.save("vid.mp4", imgstack, thread_count = 1);
  3.999581 seconds (30.01 M allocations: 610.917 MiB, 2.31% gc time)

julia> @time VideoIO.save("vid.mp4", imgstack); # defaults to thread_count = Threads.nthreads() == 6
  2.025728 seconds (30.01 M allocations: 610.917 MiB, 4.35% gc time)
IanButterworth commented 2 years ago

github appears to have broken this branch and renamed it to a new branch called IB/decode_threading with capital initials. I'm no longer able to push here so I just opened #332 to replace this