diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2013-03-15 20:57:19 +0400 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2013-03-15 20:57:19 +0400 |
commit | 08a8d11216053855460fdf54a88e33d88400a985 (patch) | |
tree | f562f129adfcde117cab6e33b48d9866386eb9d6 /source/blender/editors/space_clip/clip_ops.c | |
parent | bcec00dddcaedab7afbede3c4974b6c0cd1745c5 (diff) |
Further improvement for multi-threaded proxies
Handle sequences in a special case for dealing with
sequence sources.
Namely handle separate frames in separate threads,
but do disk read from a critical section since HDD
is not so friendly with lots threads requesting for
data from it.
Makes proxy building much faster than it was before.
Diffstat (limited to 'source/blender/editors/space_clip/clip_ops.c')
-rw-r--r-- | source/blender/editors/space_clip/clip_ops.c | 236 |
1 files changed, 211 insertions, 25 deletions
diff --git a/source/blender/editors/space_clip/clip_ops.c b/source/blender/editors/space_clip/clip_ops.c index 1938cd7b53d..a38673daeb9 100644 --- a/source/blender/editors/space_clip/clip_ops.c +++ b/source/blender/editors/space_clip/clip_ops.c @@ -30,6 +30,14 @@ */ #include <errno.h> +#include <sys/types.h> +#include <fcntl.h> + +#ifndef WIN32 +# include <unistd.h> +#else +# include <io.h> +#endif #include "MEM_guardedalloc.h" @@ -37,6 +45,7 @@ #include "DNA_scene_types.h" /* min/max frames */ #include "BLI_utildefines.h" +#include "BLI_fileops.h" #include "BLI_path_util.h" #include "BLI_math.h" #include "BLI_rect.h" @@ -972,49 +981,39 @@ static int proxy_bitflag_to_array(int size_flag, int build_sizes[4], int undisto return build_count; } -/* only this runs inside thread */ -static void proxy_startjob(void *pjv, short *stop, short *do_update, float *progress) +/* simple case for movies -- handle frame-by-frame, do threading within single frame */ +static void do_movie_proxy(void *pjv, int *UNUSED(build_sizes), int UNUSED(build_count), + int *build_undistort_sizes, int build_undistort_count, + short *stop, short *do_update, float *progress) { ProxyJob *pj = pjv; Scene *scene = pj->scene; MovieClip *clip = pj->clip; struct MovieDistortion *distortion = NULL; - short size_flag; int cfra, sfra = SFRA, efra = EFRA; - int build_sizes[4], build_count = 0; - int build_undistort_sizes[4], build_undistort_count = 0; - - size_flag = clip->proxy.build_size_flag; - build_count = proxy_bitflag_to_array(size_flag, build_sizes, 0); - build_undistort_count = proxy_bitflag_to_array(size_flag, build_undistort_sizes, 1); - - if (clip->source == MCLIP_SRC_MOVIE) { - if (pj->index_context) - IMB_anim_index_rebuild(pj->index_context, stop, do_update, progress); + if (pj->index_context) + IMB_anim_index_rebuild(pj->index_context, stop, do_update, progress); - if (!build_undistort_count) { - if (*stop) - pj->stop = 1; + if (!build_undistort_count) { + if (*stop) + pj->stop = 1; - return; - } - else { - sfra = 1; - efra = IMB_anim_get_duration(clip->anim, IMB_TC_NONE); - } + return; + } + else { + sfra = 1; + efra = IMB_anim_get_duration(clip->anim, IMB_TC_NONE); } if (build_undistort_count) { int threads = BLI_system_thread_count(); + distortion = BKE_tracking_distortion_new(); BKE_tracking_distortion_set_threads(distortion, threads); } for (cfra = sfra; cfra <= efra; cfra++) { - if (clip->source != MCLIP_SRC_MOVIE) - BKE_movieclip_build_proxy_frame(clip, pj->clip_flag, NULL, cfra, build_sizes, build_count, 0); - BKE_movieclip_build_proxy_frame(clip, pj->clip_flag, distortion, cfra, build_undistort_sizes, build_undistort_count, 1); @@ -1032,6 +1031,193 @@ static void proxy_startjob(void *pjv, short *stop, short *do_update, float *prog pj->stop = 1; } +/* ***** + * special case for sequences -- handle different frames in different threads, + * loading from disk happens in critical section, decoding frame happens from + * thread for maximal speed + */ + +typedef struct ProxyQueue { + int cfra; + int sfra; + int efra; + SpinLock spin; + + short *stop; + short *do_update; + float *progress; +} ProxyQueue; + +typedef struct ProxyThread { + MovieClip *clip; + ProxyQueue *queue; + + struct MovieDistortion *distortion; + + int *build_sizes, build_count; + int *build_undistort_sizes, build_undistort_count; +} ProxyThread; + +static unsigned char *proxy_thread_next_frame(ProxyQueue *queue, MovieClip *clip, size_t *size_r, int *cfra_r) +{ + unsigned char *mem = NULL; + + BLI_spin_lock(&queue->spin); + if (!*queue->stop && queue->cfra <= queue->efra) { + char name[FILE_MAX]; + size_t size; + int file; + + BKE_movieclip_filename_for_frame(clip, queue->cfra, name); + + file = open(name, O_BINARY | O_RDONLY, 0); + if (file < 0) { + BLI_spin_unlock(&queue->spin); + return NULL; + } + + size = BLI_file_descriptor_size(file); + if (size < 1) { + close(file); + BLI_spin_unlock(&queue->spin); + return NULL; + } + + mem = MEM_mallocN(size, "movieclip proxy memory file"); + + if (read(file, mem, size) != size) { + close(file); + BLI_spin_unlock(&queue->spin); + MEM_freeN(mem); + return NULL; + } + + *size_r = size; + *cfra_r = queue->cfra; + + queue->cfra++; + close(file); + + *queue->do_update = 1; + *queue->progress = (float)(queue->cfra - queue->sfra) / (queue->efra - queue->sfra); + } + BLI_spin_unlock(&queue->spin); + + return mem; +} + +static void *do_proxy_thread(void *data_v) +{ + ProxyThread *data = (ProxyThread *) data_v; + unsigned char *mem; + size_t size; + int cfra; + + while ((mem = proxy_thread_next_frame(data->queue, data->clip, &size, &cfra))) { + ImBuf *ibuf; + + ibuf = IMB_ibImageFromMemory(mem, size, IB_rect | IB_multilayer | IB_alphamode_detect, NULL, "proxy frame"); + + BKE_movieclip_build_proxy_frame_for_ibuf(data->clip, ibuf, NULL, cfra, + data->build_sizes, data->build_count, FALSE); + + BKE_movieclip_build_proxy_frame_for_ibuf(data->clip, ibuf, data->distortion, cfra, + data->build_undistort_sizes, data->build_undistort_count, TRUE); + + IMB_freeImBuf(ibuf); + + MEM_freeN(mem); + } + + return NULL; +} + +static void do_sequence_proxy(void *pjv, int *build_sizes, int build_count, + int *build_undistort_sizes, int build_undistort_count, + short *stop, short *do_update, float *progress) +{ + ProxyJob *pj = pjv; + MovieClip *clip = pj->clip; + Scene *scene = pj->scene; + int sfra = SFRA, efra = EFRA; + ProxyThread *handles; + ListBase threads; + int i, tot_thread = BLI_system_thread_count(); + ProxyQueue queue; + + BLI_spin_init(&queue.spin); + + queue.cfra = sfra; + queue.sfra = sfra; + queue.efra = efra; + queue.stop = stop; + queue.do_update = do_update; + queue.progress = progress; + + handles = MEM_callocN(sizeof(ProxyThread) * tot_thread, "proxy threaded handles"); + + if (tot_thread > 1) + BLI_init_threads(&threads, do_proxy_thread, tot_thread); + + for (i = 0; i < tot_thread; i++) { + ProxyThread *handle = &handles[i]; + + handle->clip = clip; + handle->queue = &queue; + + handle->build_count = build_count; + handle->build_sizes = build_sizes; + + handle->build_undistort_count = build_undistort_count; + handle->build_undistort_sizes = build_undistort_sizes; + + if (build_undistort_count) + handle->distortion = BKE_tracking_distortion_new(); + + if (tot_thread > 1) + BLI_insert_thread(&threads, handle); + } + + if (tot_thread > 1) + BLI_end_threads(&threads); + else + do_proxy_thread(handles); + + MEM_freeN(handles); + + if (build_undistort_count) { + for (i = 0; i < tot_thread; i++) { + ProxyThread *handle = &handles[i]; + + BKE_tracking_distortion_free(handle->distortion); + } + } +} + +static void proxy_startjob(void *pjv, short *stop, short *do_update, float *progress) +{ + ProxyJob *pj = pjv; + MovieClip *clip = pj->clip; + + short size_flag; + int build_sizes[4], build_count = 0; + int build_undistort_sizes[4], build_undistort_count = 0; + + size_flag = clip->proxy.build_size_flag; + + build_count = proxy_bitflag_to_array(size_flag, build_sizes, 0); + build_undistort_count = proxy_bitflag_to_array(size_flag, build_undistort_sizes, 1); + + if (clip->source == MCLIP_SRC_MOVIE) { + do_movie_proxy(pjv, build_sizes, build_count, build_undistort_sizes, + build_undistort_count, stop, do_update, progress); + } + else { + do_sequence_proxy(pjv, build_sizes, build_count, build_undistort_sizes, + build_undistort_count, stop, do_update, progress); + } +} + static void proxy_endjob(void *pjv) { ProxyJob *pj = pjv; |