This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
[QUAD] Enhancement: Multi-thread the convert_colorspace method #6307
Open
ccaillot
wants to merge
1
commit into
ynput:develop
Choose a base branch
from
quadproduction:enhancement/multi-thread_the_convert_colorspace_method
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−36
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
|
||
import xml.etree.ElementTree | ||
|
||
from multiprocessing.pool import ThreadPool | ||
|
||
from .execute import run_subprocess | ||
from .vendor_bin_utils import ( | ||
get_ffmpeg_tool_args, | ||
|
@@ -1134,50 +1136,78 @@ def convert_colorspace( | |
if logger is None: | ||
logger = logging.getLogger(__name__) | ||
|
||
input_info = get_oiio_info_for_input(input_path, logger=logger) | ||
def get_chunks(frame_start, frame_end, chunk_size): | ||
"""Chunk the frame numbers into groups.""" | ||
frames = list(range(frame_start, frame_end + 1)) | ||
chunk_list = [frames[i:i + chunk_size] for i in range(0, len(frames), chunk_size)] | ||
return chunk_list | ||
|
||
# Collect channels to export | ||
input_arg, channels_arg = get_oiio_input_and_channel_args(input_info) | ||
def process_conversion(in_path, out_path, start_frame=None, end_frame=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (80 > 79 characters) |
||
if start_frame is not None: | ||
frame_group = f'.{start_frame}-{end_frame}#.' | ||
frame_regex = r'\.\d+-\d+#\.' | ||
in_path = re.sub(frame_regex, frame_group, in_path) | ||
out_path = re.sub(frame_regex, frame_group, out_path) | ||
|
||
# Prepare subprocess arguments | ||
oiio_cmd = get_oiio_tool_args( | ||
"oiiotool", | ||
# Don't add any additional attributes | ||
"--nosoftwareattrib", | ||
"--colorconfig", config_path | ||
) | ||
input_info = get_oiio_info_for_input(in_path, logger=logger) | ||
|
||
oiio_cmd.extend([ | ||
input_arg, input_path, | ||
# Tell oiiotool which channels should be put to top stack | ||
# (and output) | ||
"--ch", channels_arg, | ||
# Use first subimage | ||
"--subimage", "0" | ||
]) | ||
# Collect channels to export | ||
input_arg, channels_arg = get_oiio_input_and_channel_args(input_info) | ||
|
||
# Prepare subprocess arguments | ||
oiio_cmd = get_oiio_tool_args( | ||
"oiiotool", | ||
# Don't add any additional attributes | ||
"--nosoftwareattrib", | ||
"--colorconfig", config_path | ||
) | ||
|
||
oiio_cmd.extend([ | ||
input_arg, in_path, | ||
# Tell oiiotool which channels should be put to top stack | ||
# (and output) | ||
"--ch", channels_arg, | ||
# Use first subimage | ||
"--subimage", "0" | ||
]) | ||
|
||
if all([target_colorspace, view, display]): | ||
raise ValueError("Colorspace and both screen and display" | ||
" cannot be set together." | ||
"Choose colorspace or screen and display") | ||
if not target_colorspace and not all([view, display]): | ||
raise ValueError("Both screen and display must be set.") | ||
if all([target_colorspace, view, display]): | ||
raise ValueError("Colorspace and both screen and display" | ||
" cannot be set together." | ||
"Choose colorspace or screen and display") | ||
if not target_colorspace and not all([view, display]): | ||
raise ValueError("Both screen and display must be set.") | ||
|
||
if additional_command_args: | ||
oiio_cmd.extend(additional_command_args) | ||
if additional_command_args: | ||
oiio_cmd.extend(additional_command_args) | ||
|
||
if target_colorspace: | ||
oiio_cmd.extend(["--colorconvert", | ||
source_colorspace, | ||
target_colorspace]) | ||
if view and display: | ||
oiio_cmd.extend(["--iscolorspace", source_colorspace]) | ||
oiio_cmd.extend(["--ociodisplay", display, view]) | ||
if target_colorspace: | ||
oiio_cmd.extend(["--colorconvert", | ||
source_colorspace, | ||
target_colorspace]) | ||
if view and display: | ||
oiio_cmd.extend(["--iscolorspace", source_colorspace]) | ||
oiio_cmd.extend(["--ociodisplay", display, view]) | ||
|
||
oiio_cmd.extend(["-o", output_path]) | ||
oiio_cmd.extend(["-o", out_path]) | ||
|
||
logger.debug("Conversion command: {}".format(" ".join(oiio_cmd))) | ||
run_subprocess(oiio_cmd, logger=logger) | ||
logger.debug("Conversion command: {}".format(" ".join(oiio_cmd))) | ||
run_subprocess(oiio_cmd, logger=logger) | ||
|
||
frame_data = input_path.split('.')[-2] | ||
# If it's a range of frames, we multithread it | ||
if '-' in frame_data: | ||
start, end = frame_data[:-1].split('-') | ||
# chunk_size could be editable | ||
chunks = get_chunks(int(start), int(end), chunk_size=10) | ||
|
||
pool = ThreadPool() | ||
for chunk in chunks: | ||
pool.apply_async(process_conversion, (input_path, output_path, chunk[0], chunk[-1])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (96 > 79 characters) |
||
pool.close() | ||
pool.join() | ||
else: | ||
process_conversion(input_path, output_path) | ||
|
||
|
||
def split_cmd_args(in_args): | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (90 > 79 characters)