Skip to content

Commit

Permalink
Enable adjustable codec multithreading (#332)
Browse files Browse the repository at this point in the history
* enable adjustable multithreaded decoding

* enable 2 threads during CI

* also enable multithreaded encoding

* default threading for encode, Sys.CPU_THREADS for decode

* Revert "enable 2 threads during CI"

This reverts commit ffd91cc.
  • Loading branch information
IanButterworth authored Aug 12, 2021
1 parent bbc0d6e commit d953781
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/avio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ function VideoReader(avin::AVInput{I}, video_stream = 1;
target_colorspace_details = nothing,
allow_vio_gray_transform = true,
swscale_options::OptionsT = (;),
sws_color_options::OptionsT = (;)) where I
sws_color_options::OptionsT = (;),
thread_count::Union{Nothing, Int} = Sys.CPU_THREADS) where I
bad_px_type = transcode && target_format !== nothing &&
!is_pixel_type_supported(target_format)
bad_px_type && error("Unsupported pixel format $target_format")
Expand All @@ -268,6 +269,7 @@ function VideoReader(avin::AVInput{I}, video_stream = 1;

# Create decoder context
codec_context = AVCodecContextPtr(codec) # Allocates
codec_context.thread_count = thread_count === nothing ? codec_context.thread_count : thread_count
# Transfer parameters to decoder context
ret = avcodec_parameters_to_context(codec_context, stream.codecpar)
ret < 0 && error("Could not copy the codec parameters to the decoder")
Expand Down Expand Up @@ -584,6 +586,8 @@ arguments listed below.
- `sws_color_options::OptionsT = (;)`: Additional keyword arguments passed to
[sws_setColorspaceDetails]
(http://ffmpeg.org/doxygen/2.5/group__libsws.html#ga541bdffa8149f5f9203664f955faa040).
- `thread_count::Union{Nothing, Int} = Sys.CPU_THREADS`: The number of threads the codec is
allowed to use or `nothing` for default codec behavior. Defaults to `Sys.CPU_THREADS`.
"""
openvideo(s::Union{IO, AbstractString, AVInput}, args...; kwargs...) =
VideoReader(s, args...; kwargs...)
Expand Down
6 changes: 5 additions & 1 deletion src/encoding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ function VideoWriter(filename::AbstractString, ::Type{T},
pix_fmt_loss_flags = 0,
input_colorspace_details = nothing,
allow_vio_gray_transform = true,
sws_color_options::OptionsT = (;)) where T
sws_color_options::OptionsT = (;),
thread_count::Union{Nothing, Int} = nothing) where T
framerate > 0 || error("Framerate must be strictly positive")

if haskey(encoder_options, :priv_data)
Expand Down Expand Up @@ -300,6 +301,7 @@ function VideoWriter(filename::AbstractString, ::Type{T},
codec_context.time_base = target_timebase
codec_context.framerate = framerate_rat
codec_context.pix_fmt = encoding_pix_fmt
codec_context.thread_count = thread_count === nothing ? codec_context.thread_count : thread_count

if format_context.oformat.flags & AVFMT_GLOBALHEADER != 0
codec_context.flags |= AV_CODEC_FLAG_GLOBAL_HEADER
Expand Down Expand Up @@ -453,6 +455,8 @@ occurred.
- `sws_color_options::OptionsT = (;)`: Additional keyword arguments passed to
[sws_setColorspaceDetails]
(http://ffmpeg.org/doxygen/2.5/group__libsws.html#ga541bdffa8149f5f9203664f955faa040).
- `thread_count::Union{Nothing, Int} = nothing`: The number of threads the codec is
allowed to use, or `nothing` for default codec behavior. Defaults to `nothing`.
See also: [`write`](@ref), [`close_video_out!`](@ref)
"""
Expand Down

0 comments on commit d953781

Please sign in to comment.