Blender V5.3
clip_editor.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cerrno>
10#include <cstddef>
11#include <fcntl.h>
12#include <sys/types.h>
13
14#ifndef WIN32
15# include <unistd.h>
16#else
17# include <io.h>
18#endif
19
20#include "MEM_guardedalloc.h"
21
22#include "DNA_mask_types.h"
23
24#include "BLI_fileops.hh"
25#include "BLI_listbase.hh"
26#include "BLI_mutex.hh"
27#include "BLI_rect.hh"
28#include "BLI_task_c.hh"
29#include "BLI_utildefines.hh"
30
31#include "BKE_context.hh"
32#include "BKE_global.hh"
33#include "BKE_lib_id.hh"
34#include "BKE_main.hh"
35#include "BKE_movieclip.hh"
36#include "BKE_tracking.hh"
37
39#include "IMB_imbuf.hh"
40#include "IMB_imbuf_types.hh"
41
42#include "ED_clip.hh"
43#include "ED_select_utils.hh"
44
45#include "WM_api.hh"
46#include "WM_types.hh"
47
48#include "UI_view2d.hh"
49
50#include "clip_intern.hh" /* own include */
51
52namespace blender {
53
54/* -------------------------------------------------------------------- */
57
59{
61
62 if (sc && sc->clip) {
63 return true;
64 }
65
66 return false;
67}
68
70{
72
73 if (sc) {
74 return sc->view == SC_VIEW_CLIP;
75 }
76
77 return false;
78}
80{
81 const ARegion *region = CTX_wm_region(C);
82 if (!(region && region->regiontype == RGN_TYPE_WINDOW)) {
83 return false;
84 }
86}
87
89{
91
92 if (sc && sc->clip) {
94 }
95
96 return false;
97}
98
100{
102
103 if (sc && sc->clip) {
105 }
106
107 return false;
108}
109
111{
113 return false;
114 }
115
116 const SpaceClip *space_clip = CTX_wm_space_clip(C);
117 return space_clip->overlay.flag & SC_SHOW_OVERLAYS &&
119}
120
122{
125
126 if (clip) {
128
129 return sc->mask_info.mask != nullptr;
130 }
131 }
132
133 return false;
134}
135
137{
139 return false;
140 }
141
142 const SpaceClip *space_clip = CTX_wm_space_clip(C);
143 return space_clip->overlay.flag & SC_SHOW_OVERLAYS &&
145}
146
148
149/* -------------------------------------------------------------------- */
152
153void ED_space_clip_get_size(const SpaceClip *sc, int *r_width, int *r_height)
154{
155 if (sc->clip) {
156 BKE_movieclip_get_size(sc->clip, &sc->user, r_width, r_height);
157 }
158 else {
159 *r_width = *r_height = IMG_SIZE_FALLBACK;
160 }
161}
162
163void ED_space_clip_get_size_fl(const SpaceClip *sc, float r_size[2])
164{
165 int size_i[2];
166 ED_space_clip_get_size(sc, &size_i[0], &size_i[1]);
167 r_size[0] = size_i[0];
168 r_size[1] = size_i[1];
169}
170
172 const ARegion *region,
173 float *r_zoomx,
174 float *r_zoomy)
175{
176 int width, height;
177
178 ED_space_clip_get_size(sc, &width, &height);
179
180 *r_zoomx = float(BLI_rcti_size_x(&region->winrct) + 1) /
181 (BLI_rctf_size_x(&region->v2d.cur) * width);
182 *r_zoomy = float(BLI_rcti_size_y(&region->winrct) + 1) /
183 (BLI_rctf_size_y(&region->v2d.cur) * height);
184}
185
186void ED_space_clip_get_aspect(const SpaceClip *sc, float *r_aspx, float *r_aspy)
187{
189
190 if (clip) {
191 BKE_movieclip_get_aspect(clip, r_aspx, r_aspy);
192 }
193 else {
194 *r_aspx = *r_aspy = 1.0f;
195 }
196
197 if (*r_aspx < *r_aspy) {
198 *r_aspy = *r_aspy / *r_aspx;
199 *r_aspx = 1.0f;
200 }
201 else {
202 *r_aspx = *r_aspx / *r_aspy;
203 *r_aspy = 1.0f;
204 }
205}
206
207void ED_space_clip_get_aspect_dimension_aware(const SpaceClip *sc, float *r_aspx, float *r_aspy)
208{
209 int w, h;
210
211 /* most of tools does not require aspect to be returned with dimensions correction
212 * due to they're invariant to this stuff, but some transformation tools like rotation
213 * should be aware of aspect correction caused by different resolution in different
214 * directions.
215 * mainly this is used for transformation stuff
216 */
217
218 if (!sc->clip) {
219 *r_aspx = 1.0f;
220 *r_aspy = 1.0f;
221
222 return;
223 }
224
225 ED_space_clip_get_aspect(sc, r_aspx, r_aspy);
226 BKE_movieclip_get_size(sc->clip, &sc->user, &w, &h);
227
228 *r_aspx *= float(w);
229 *r_aspy *= float(h);
230
231 if (*r_aspx < *r_aspy) {
232 *r_aspy = *r_aspy / *r_aspx;
233 *r_aspx = 1.0f;
234 }
235 else {
236 *r_aspx = *r_aspx / *r_aspy;
237 *r_aspy = 1.0f;
238 }
239}
240
242{
244
245 /* Caller must ensure space does have a valid clip, otherwise it will crash, see #45017. */
247}
248
250{
251 if (sc->clip) {
252 ImBuf *ibuf;
253
256
257 if (ibuf && (ibuf->byte_data() || ibuf->float_data())) {
258 return ibuf;
259 }
260
261 if (ibuf) {
262 IMB_freeImBuf(ibuf);
263 }
264 }
265
266 return nullptr;
267}
268
270 float loc[2],
271 float *scale,
272 float *angle)
273{
274 if (sc->clip) {
275 ImBuf *ibuf;
276
278 sc->clip, &sc->user, MovieClipPostprocFlag(sc->postproc_flag), loc, scale, angle);
279
280 if (ibuf && (ibuf->byte_data() || ibuf->float_data())) {
281 return ibuf;
282 }
283
284 if (ibuf) {
285 IMB_freeImBuf(ibuf);
286 }
287 }
288
289 return nullptr;
290}
291
293 const ARegion *region,
294 const int mval[2],
295 float r_fpos[2])
296{
298 if (!ibuf) {
299 return false;
300 }
301
302 /* map the mouse coords to the backdrop image space */
303 ED_clip_mouse_pos(sc, region, mval, r_fpos);
304
305 IMB_freeImBuf(ibuf);
306 return true;
307}
308
310 const ARegion *region,
311 const int mval[2],
312 float r_col[3])
313{
314 ImBuf *ibuf;
315 float fx, fy, co[2];
316 bool ret = false;
317
318 ibuf = ED_space_clip_get_buffer(sc);
319 if (!ibuf) {
320 return false;
321 }
322
323 /* map the mouse coords to the backdrop image space */
324 ED_clip_mouse_pos(sc, region, mval, co);
325
326 fx = co[0];
327 fy = co[1];
328
329 if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
330 const float *fp;
331 const uchar *cp;
332 int x = int(fx * ibuf->x), y = int(fy * ibuf->y);
333
334 CLAMP(x, 0, ibuf->x - 1);
335 CLAMP(y, 0, ibuf->y - 1);
336
337 if (ibuf->float_data()) {
338 fp = (ibuf->float_data() + (ibuf->channels) * (y * ibuf->x + x));
339 copy_v3_v3(r_col, fp);
340 ret = true;
341 }
342 else if (ibuf->byte_data()) {
343 cp = ibuf->byte_data() + 4 * (y * ibuf->x + x);
344 rgb_uchar_to_float(r_col, cp);
346 ret = true;
347 }
348 }
349
350 IMB_freeImBuf(ibuf);
351
352 return ret;
353}
354
355void ED_clip_update_frame(const Main *mainp, int cfra)
356{
357 /* image window, compo node users */
358 for (wmWindowManager &wm : mainp->wm) {
359 for (wmWindow &win : wm.windows) {
360 bScreen *screen = WM_window_get_active_screen(&win);
361
362 for (ScrArea &area : screen->areabase) {
363 if (area.spacetype == SPACE_CLIP) {
364 SpaceClip *sc = area.spacedata.first_as<SpaceClip>();
365
366 sc->scopes.ok = false;
367
369 }
370 }
371 }
372 }
373}
374
375bool ED_clip_view_selection(const bContext *C, const ARegion * /*region*/, bool fit)
376{
377 float offset_x, offset_y;
378 float zoom;
379 if (!clip_view_calculate_view_selection(C, fit, &offset_x, &offset_y, &zoom)) {
380 return false;
381 }
382
384 sc->xof = offset_x;
385 sc->yof = offset_y;
386 sc->zoom = zoom;
387
388 return true;
389}
390
391void ED_clip_select_all(const SpaceClip *sc, int action, bool *r_has_selection)
392{
394 const MovieTrackingObject *tracking_object = BKE_tracking_object_get_active(&clip->tracking);
395 const int framenr = ED_space_clip_get_clip_frame_number(sc);
396 bool has_selection = false;
397
398 if (action == SEL_TOGGLE) {
399 action = SEL_SELECT;
400
401 for (MovieTrackingTrack &track : tracking_object->tracks) {
402 if (!TRACK_VIEW_SELECTED(sc, &track)) {
403 continue;
404 }
405
406 const MovieTrackingMarker *marker = BKE_tracking_marker_get(&track, framenr);
407
408 if (ED_space_clip_marker_is_visible(sc, tracking_object, &track, marker)) {
409 action = SEL_DESELECT;
410 break;
411 }
412 }
413
414 for (MovieTrackingPlaneTrack &plane_track : tracking_object->plane_tracks) {
415 if (PLANE_TRACK_VIEW_SELECTED(&plane_track)) {
416 action = SEL_DESELECT;
417 break;
418 }
419 }
420 }
421
422 for (MovieTrackingTrack &track : tracking_object->tracks) {
423 if (track.flag & TRACK_HIDDEN) {
424 continue;
425 }
426
427 const MovieTrackingMarker *marker = BKE_tracking_marker_get(&track, framenr);
428
429 if (ED_space_clip_marker_is_visible(sc, tracking_object, &track, marker)) {
430 switch (action) {
431 case SEL_SELECT:
432 track.flag |= TRACK_SELECT;
433 track.pat_flag |= TRACK_SELECT;
434 track.search_flag |= TRACK_SELECT;
435 break;
436 case SEL_DESELECT:
437 track.flag &= ~TRACK_SELECT;
438 track.pat_flag &= ~TRACK_SELECT;
439 track.search_flag &= ~TRACK_SELECT;
440 break;
441 case SEL_INVERT:
442 track.flag ^= TRACK_SELECT;
443 track.pat_flag ^= TRACK_SELECT;
444 track.search_flag ^= TRACK_SELECT;
445 break;
446 }
447 }
448
449 if (TRACK_VIEW_SELECTED(sc, &track)) {
450 has_selection = true;
451 }
452 }
453
454 for (MovieTrackingPlaneTrack &plane_track : tracking_object->plane_tracks) {
455 if (plane_track.flag & PLANE_TRACK_HIDDEN) {
456 continue;
457 }
458
459 switch (action) {
460 case SEL_SELECT:
461 plane_track.flag |= PLANE_TRACK_SELECT;
462 break;
463 case SEL_DESELECT:
464 plane_track.flag &= ~PLANE_TRACK_SELECT;
465 break;
466 case SEL_INVERT:
467 plane_track.flag ^= PLANE_TRACK_SELECT;
468 break;
469 }
470 if (plane_track.flag & SELECT) {
471 has_selection = true;
472 }
473 }
474
475 if (r_has_selection) {
476 *r_has_selection = has_selection;
477 }
478}
479
480void ED_clip_point_undistorted_pos(const SpaceClip *sc, const float co[2], float r_co[2])
481{
482 copy_v2_v2(r_co, co);
483
486 float aspy = 1.0f / clip->tracking.camera.pixel_aspect;
487 int width, height;
488
489 BKE_movieclip_get_size(sc->clip, &sc->user, &width, &height);
490
491 r_co[0] *= width;
492 r_co[1] *= height * aspy;
493
494 BKE_tracking_undistort_v2(&clip->tracking, width, height, r_co, r_co);
495
496 r_co[0] /= width;
497 r_co[1] /= height * aspy;
498 }
499}
500
502 const SpaceClip *sc, const ARegion *region, float x, float y, float *xr, float *yr)
503{
504 int sx, sy, width, height;
505 float zoomx, zoomy, pos[3], imat[4][4];
506
507 ED_space_clip_get_zoom(sc, region, &zoomx, &zoomy);
508 ED_space_clip_get_size(sc, &width, &height);
509
510 ui::view2d_view_to_region(&region->v2d, 0.0f, 0.0f, &sx, &sy);
511
512 pos[0] = (x - sx) / zoomx;
513 pos[1] = (y - sy) / zoomy;
514 pos[2] = 0.0f;
515
516 invert_m4_m4(imat, sc->stabmat);
517 mul_v3_m4v3(pos, imat, pos);
518
519 *xr = pos[0] / width;
520 *yr = pos[1] / height;
521
524 if (clip != nullptr) {
525 MovieTracking *tracking = &clip->tracking;
526 float aspy = 1.0f / tracking->camera.pixel_aspect;
527 float tmp[2] = {*xr * width, *yr * height * aspy};
528
529 BKE_tracking_distort_v2(tracking, width, height, tmp, tmp);
530
531 *xr = tmp[0] / width;
532 *yr = tmp[1] / (height * aspy);
533 }
534 }
535}
536
538 const ARegion *region,
539 const float co[2],
540 float r_co[2])
541{
542 float zoomx, zoomy;
543 float pos[3];
544 int width, height;
545 int sx, sy;
546
547 ui::view2d_view_to_region(&region->v2d, 0.0f, 0.0f, &sx, &sy);
548 ED_space_clip_get_size(sc, &width, &height);
549 ED_space_clip_get_zoom(sc, region, &zoomx, &zoomy);
550
552 pos[2] = 0.0f;
553
554 /* untested */
555 mul_v3_m4v3(pos, sc->stabmat, pos);
556
557 r_co[0] = (pos[0] * width * zoomx) + float(sx);
558 r_co[1] = (pos[1] * height * zoomy) + float(sy);
559}
560
562 const ARegion *region,
563 const int mval[2],
564 float r_co[2])
565{
566 ED_clip_point_stable_pos(sc, region, mval[0], mval[1], &r_co[0], &r_co[1]);
567}
568
570{
571 if (sc) {
572 return sc->mode == SC_MODE_TRACKING;
573 }
574
575 return false;
576}
577
579{
580 if (sc) {
581 return sc->mode == SC_MODE_MASKEDIT;
582 }
583
584 return false;
585}
586
588
589/* -------------------------------------------------------------------- */
592
594{
595 return sc->clip;
596}
597
599{
600 MovieClip *old_clip;
601 bool old_clip_visible = false;
602
603 if (!screen && C) {
604 screen = CTX_wm_screen(C);
605 }
606
607 old_clip = sc->clip;
608 sc->clip = clip;
609
610 id_us_ensure_real(id_cast<ID *>(sc->clip));
611
612 if (screen && sc->view == SC_VIEW_CLIP) {
613 for (ScrArea &area : screen->areabase) {
614 for (SpaceLink &sl : area.spacedata) {
615 if (sl.spacetype == SPACE_CLIP) {
616 SpaceClip *cur_sc = reinterpret_cast<SpaceClip *>(&sl);
617
618 if (cur_sc != sc) {
619 if (cur_sc->view == SC_VIEW_CLIP) {
620 if (cur_sc->clip == old_clip) {
621 old_clip_visible = true;
622 }
623 }
624 else {
625 if (ELEM(cur_sc->clip, old_clip, nullptr)) {
626 cur_sc->clip = clip;
627 }
628 }
629 }
630 }
631 }
632 }
633 }
634
635 /* If clip is no longer visible on screen, free memory used by its cache */
636 if (old_clip && old_clip != clip && !old_clip_visible) {
638 }
639
640 if (C) {
642 }
643}
644
646
647/* -------------------------------------------------------------------- */
650
652{
653 return sc->mask_info.mask;
654}
655
657{
658 sc->mask_info.mask = mask;
659
660 id_us_ensure_real(id_cast<ID *>(sc->mask_info.mask));
661
662 if (C) {
664 }
665}
666
668
669/* -------------------------------------------------------------------- */
672
676
677 /* Local copy of the clip which is used to decouple reading in a way which does not require
678 * threading lock which might "conflict" with the main thread,
679 *
680 * Used, for example, for animation prefetching (`clip->anim` can not be used from multiple
681 * threads and main thread might need it). */
683
687};
688
693
694 /* If true pre-fetching goes forward in time,
695 * otherwise it goes backwards in time (starting from current frame).
696 */
698
700
701 bool *stop;
703 float *progress;
704};
705
706/* check whether pre-fetching is allowed */
708{
709 return G.is_break;
710}
711
712/* read file for specified frame number to the memory */
714 int current_frame,
715 eMovieClipProxy_RenderSize render_size,
716 eMovieClipProxy_RenderFlag render_flag,
717 size_t *r_size)
718{
719 MovieClipUser user = {};
720 user.framenr = current_frame;
721 user.render_size = render_size;
722 user.render_flag = render_flag;
723
724 char filepath[FILE_MAX];
725 BKE_movieclip_filepath_for_frame(clip, &user, filepath);
726
727 int file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
728 if (file == -1) {
729 return nullptr;
730 }
731
732 const size_t size = BLI_file_descriptor_size(file);
733 if (ELEM(size, 0, size_t(-1))) [[unlikely]] {
734 close(file);
735 return nullptr;
736 }
737
738 uchar *mem = MEM_new_array_zeroed<uchar>(size, "movieclip prefetch memory file");
739 if (mem == nullptr) {
740 close(file);
741 return nullptr;
742 }
743
744 if (BLI_read(file, mem, size) != size) {
745 close(file);
746 MEM_delete(mem);
747 return nullptr;
748 }
749
750 *r_size = size;
751
752 close(file);
753
754 return mem;
755}
756
757/* find first uncached frame within prefetching frame range */
759 int from_frame,
760 int end_frame,
761 eMovieClipProxy_RenderSize render_size,
762 eMovieClipProxy_RenderFlag render_flag,
763 short direction)
764{
765 int current_frame;
766 MovieClipUser user = {};
767
768 user.render_size = render_size;
769 user.render_flag = render_flag;
770
771 if (direction > 0) {
772 for (current_frame = from_frame; current_frame <= end_frame; current_frame++) {
773 user.framenr = current_frame;
774
775 if (!BKE_movieclip_has_cached_frame(clip, &user)) {
776 break;
777 }
778 }
779 }
780 else {
781 for (current_frame = from_frame; current_frame >= end_frame; current_frame--) {
782 user.framenr = current_frame;
783
784 if (!BKE_movieclip_has_cached_frame(clip, &user)) {
785 break;
786 }
787 }
788 }
789
790 return current_frame;
791}
792
793/* get memory buffer for first uncached frame within prefetch frame range */
795 MovieClip *clip,
796 size_t *r_size,
797 int *r_current_frame)
798{
799 uchar *mem = nullptr;
800
801 std::lock_guard lock(queue->mutex);
802 if (!*queue->stop && !check_prefetch_break() &&
803 IN_RANGE_INCL(queue->current_frame, queue->start_frame, queue->end_frame))
804 {
805 int current_frame;
806
807 if (queue->forward) {
808 current_frame = prefetch_find_uncached_frame(clip,
809 queue->current_frame + 1,
810 queue->end_frame,
811 queue->render_size,
812 queue->render_flag,
813 1);
814 /* switch direction if read frames from current up to scene end frames */
815 if (current_frame > queue->end_frame) {
816 queue->current_frame = queue->initial_frame;
817 queue->forward = false;
818 }
819 }
820
821 if (!queue->forward) {
822 current_frame = prefetch_find_uncached_frame(clip,
823 queue->current_frame - 1,
824 queue->start_frame,
825 queue->render_size,
826 queue->render_flag,
827 -1);
828 }
829
830 if (IN_RANGE_INCL(current_frame, queue->start_frame, queue->end_frame)) {
831 int frames_processed;
832
834 clip, current_frame, queue->render_size, queue->render_flag, r_size);
835
836 *r_current_frame = current_frame;
837
838 queue->current_frame = current_frame;
839
840 if (queue->forward) {
841 frames_processed = queue->current_frame - queue->initial_frame;
842 }
843 else {
844 frames_processed = (queue->end_frame - queue->initial_frame) +
845 (queue->initial_frame - queue->current_frame);
846 }
847
848 *queue->do_update = true;
849 *queue->progress = float(frames_processed) / (queue->end_frame - queue->start_frame);
850 }
851 }
852
853 return mem;
854}
855
856static void prefetch_task_func(TaskPool *__restrict pool, void *task_data)
857{
858 PrefetchQueue *queue = static_cast<PrefetchQueue *>(BLI_task_pool_user_data(pool));
859 MovieClip *clip = static_cast<MovieClip *>(task_data);
860 uchar *mem;
861 size_t size;
862 int current_frame;
863
864 while ((mem = prefetch_thread_next_frame(queue, clip, &size, &current_frame))) {
865 ImBuf *ibuf;
866 MovieClipUser user = {};
868 int result;
869 ColorManagedColorspaceSettings *colorspace_settings = nullptr;
870 const bool use_proxy = (clip->flag & MCLIP_USE_PROXY) &&
872
873 user.framenr = current_frame;
874 user.render_size = queue->render_size;
875 user.render_flag = queue->render_flag;
876
877 /* Proxies are stored in the display space. */
878 if (!use_proxy) {
879 colorspace_settings = &clip->colorspace_settings;
880 }
881
883 mem, size, flag, "prefetch frame", nullptr, colorspace_settings);
884 if (ibuf == nullptr) {
885 continue;
886 }
887
888 result = BKE_movieclip_put_frame_if_possible(clip, &user, ibuf);
889
890 IMB_freeImBuf(ibuf);
891
892 MEM_delete(mem);
893
894 if (!result) {
895 /* no more space in the cache, stop reading frames */
896 *queue->stop = true;
897 break;
898 }
899 }
900}
901
903 int start_frame,
904 int current_frame,
905 int end_frame,
906 eMovieClipProxy_RenderSize render_size,
907 eMovieClipProxy_RenderFlag render_flag,
908 bool *stop,
909 bool *do_update,
910 float *progress)
911{
912 int tot_thread = BLI_task_scheduler_num_threads();
913
914 /* initialize queue */
915 PrefetchQueue queue;
916 queue.current_frame = current_frame;
917 queue.initial_frame = current_frame;
918 queue.start_frame = start_frame;
919 queue.end_frame = end_frame;
920 queue.render_size = render_size;
921 queue.render_flag = render_flag;
922 queue.forward = true;
923
924 queue.stop = stop;
925 queue.do_update = do_update;
926 queue.progress = progress;
927
928 TaskPool *task_pool = BLI_task_pool_create(&queue, TASK_PRIORITY_LOW);
929 for (int i = 0; i < tot_thread; i++) {
930 BLI_task_pool_push(task_pool, prefetch_task_func, clip, false, nullptr);
931 }
933 BLI_task_pool_free(task_pool);
934}
935
936/* NOTE: Reading happens from `clip_local` into `clip->cache`. */
938 MovieClip *clip_local,
939 int frame,
940 eMovieClipProxy_RenderSize render_size,
941 eMovieClipProxy_RenderFlag render_flag,
942 bool *stop)
943{
944 MovieClipUser user = {};
945
946 if (check_prefetch_break() || *stop) {
947 return false;
948 }
949
950 user.framenr = frame;
951 user.render_size = render_size;
952 user.render_flag = render_flag;
953
954 if (!BKE_movieclip_has_cached_frame(clip, &user)) {
955 ImBuf *ibuf = BKE_movieclip_anim_ibuf_for_frame_no_lock(clip_local, &user);
956
957 if (ibuf) {
958 int result;
959
960 result = BKE_movieclip_put_frame_if_possible(clip, &user, ibuf);
961
962 if (!result) {
963 /* no more space in the cache, we could stop prefetching here */
964 *stop = true;
965 }
966
967 IMB_freeImBuf(ibuf);
968 }
969 else {
970 /* error reading frame, fair enough stop attempting further reading */
971 *stop = true;
972 }
973 }
974
975 return true;
976}
977
978static void do_prefetch_movie(MovieClip *clip,
979 MovieClip *clip_local,
980 int start_frame,
981 int current_frame,
982 int end_frame,
983 eMovieClipProxy_RenderSize render_size,
984 eMovieClipProxy_RenderFlag render_flag,
985 bool *stop,
986 bool *do_update,
987 float *progress)
988{
989 int frame;
990 int frames_processed = 0;
991
992 /* read frames starting from current frame up to scene end frame */
993 for (frame = current_frame; frame <= end_frame; frame++) {
994 if (!prefetch_movie_frame(clip, clip_local, frame, render_size, render_flag, stop)) {
995 return;
996 }
997
998 frames_processed++;
999
1000 *do_update = true;
1001 *progress = float(frames_processed) / (end_frame - start_frame);
1002 }
1003
1004 /* read frames starting from current frame up to scene start frame */
1005 for (frame = current_frame; frame >= start_frame; frame--) {
1006 if (!prefetch_movie_frame(clip, clip_local, frame, render_size, render_flag, stop)) {
1007 return;
1008 }
1009
1010 frames_processed++;
1011
1012 *do_update = true;
1013 *progress = float(frames_processed) / (end_frame - start_frame);
1014 }
1015}
1016
1017static void prefetch_startjob(void *pjv, wmJobWorkerStatus *worker_status)
1018{
1019 PrefetchJob *pj = static_cast<PrefetchJob *>(pjv);
1020
1021 if (pj->clip->source == MCLIP_SRC_SEQUENCE) {
1022 /* read sequence files in multiple threads */
1024 pj->start_frame,
1025 pj->current_frame,
1026 pj->end_frame,
1027 pj->render_size,
1028 pj->render_flag,
1029 &worker_status->stop,
1030 &worker_status->do_update,
1031 &worker_status->progress);
1032 }
1033 else if (pj->clip->source == MCLIP_SRC_MOVIE) {
1034 /* read movie in a single thread */
1036 pj->clip_local,
1037 pj->start_frame,
1038 pj->current_frame,
1039 pj->end_frame,
1040 pj->render_size,
1041 pj->render_flag,
1042 &worker_status->stop,
1043 &worker_status->do_update,
1044 &worker_status->progress);
1045 }
1046 else {
1047 BLI_assert_msg(0, "Unknown movie clip source when prefetching frames");
1048 }
1049}
1050
1051static void prefetch_freejob(void *pjv)
1052{
1053 PrefetchJob *pj = static_cast<PrefetchJob *>(pjv);
1054
1055 MovieClip *clip_local = pj->clip_local;
1056 if (clip_local != nullptr) {
1057 BKE_libblock_free_datablock(&clip_local->id, 0);
1058 BKE_libblock_free_data(&clip_local->id, false);
1059 BLI_assert(!clip_local->id.py_instance); /* Or call #BKE_libblock_free_data_py. */
1060 MEM_delete(clip_local);
1061 }
1062
1063 MEM_delete(pj);
1064}
1065
1067{
1068 Scene *scene = CTX_data_scene(C);
1069
1070 return scene->r.sfra;
1071}
1072
1074{
1075 Scene *scene = CTX_data_scene(C);
1078 int end_frame;
1079
1080 /* check whether all the frames from prefetch range are cached */
1081 end_frame = scene->r.efra;
1082
1083 if (clip->len) {
1084 end_frame = min_ii(end_frame, scene->r.sfra + clip->len - 1);
1085 }
1086
1087 return end_frame;
1088}
1089
1090/* returns true if early out is possible */
1092{
1095 int first_uncached_frame, end_frame;
1096 int clip_len;
1097
1098 if (clip == nullptr) {
1099 return true;
1100 }
1101
1102 clip_len = BKE_movieclip_get_duration(clip);
1103
1104 /* check whether all the frames from prefetch range are cached */
1105 end_frame = prefetch_get_final_frame(C);
1106
1107 first_uncached_frame = prefetch_find_uncached_frame(
1108 clip, sc->user.framenr, end_frame, sc->user.render_size, sc->user.render_flag, 1);
1109
1110 if (first_uncached_frame > end_frame || first_uncached_frame == clip_len) {
1111 int start_frame = prefetch_get_content_start(C);
1112
1113 first_uncached_frame = prefetch_find_uncached_frame(
1114 clip, sc->user.framenr, start_frame, sc->user.render_size, sc->user.render_flag, -1);
1115
1116 if (first_uncached_frame < start_frame) {
1117 return true;
1118 }
1119 }
1120
1121 return false;
1122}
1123
1125{
1126 wmJob *wm_job;
1127 PrefetchJob *pj;
1129
1131 return;
1132 }
1133
1134 wm_job = WM_jobs_get(CTX_wm_manager(C),
1137 "Prefetching...",
1140
1141 /* create new job */
1142 pj = MEM_new_zeroed<PrefetchJob>("prefetch job");
1143 pj->clip = ED_space_clip_get_clip(sc);
1145 pj->current_frame = sc->user.framenr;
1147 pj->render_size = sc->user.render_size;
1148 pj->render_flag = sc->user.render_flag;
1149
1150 /* Create a local copy of the clip, so that video file (clip->anim) access can happen without
1151 * acquiring the lock which will interfere with the main thread. */
1152 if (pj->clip->source == MCLIP_SRC_MOVIE) {
1154 nullptr, (&pj->clip->id), reinterpret_cast<ID **>(&pj->clip_local), LIB_ID_COPY_LOCALIZE);
1155 }
1156
1158 WM_jobs_timer(wm_job, 0.2, NC_MOVIECLIP | ND_DISPLAY, 0);
1159 WM_jobs_callbacks(wm_job, prefetch_startjob, nullptr, nullptr, nullptr);
1160
1161 G.is_break = false;
1162
1163 /* and finally start the job */
1164 WM_jobs_start(CTX_wm_manager(C), wm_job);
1165}
1166
1168{
1169 SpaceClip *space_clip = CTX_wm_space_clip(C);
1170 BLI_assert(space_clip != nullptr);
1171
1172 state->offset_x = space_clip->xof;
1173 state->offset_y = space_clip->yof;
1174 state->zoom = space_clip->zoom;
1175
1176 state->lock_offset_x = 0.0f;
1177 state->lock_offset_y = 0.0f;
1178
1179 if ((space_clip->flag & SC_LOCK_SELECTION) == 0) {
1180 return;
1181 }
1182
1184 C, false, &state->offset_x, &state->offset_y, &state->zoom))
1185 {
1186 return;
1187 }
1188
1189 state->lock_offset_x = space_clip->xlockof;
1190 state->lock_offset_y = space_clip->ylockof;
1191}
1192
1194{
1195 SpaceClip *space_clip = CTX_wm_space_clip(C);
1196 BLI_assert(space_clip != nullptr);
1197
1198 if ((space_clip->flag & SC_LOCK_SELECTION) == 0) {
1199 return;
1200 }
1201
1202 float offset_x, offset_y;
1203 float zoom;
1204 if (!clip_view_calculate_view_selection(C, false, &offset_x, &offset_y, &zoom)) {
1205 return;
1206 }
1207
1208 space_clip->xlockof = state->offset_x + state->lock_offset_x - offset_x;
1209 space_clip->ylockof = state->offset_y + state->lock_offset_y - offset_y;
1210}
1211
1213
1214} // namespace blender
#define SELECT
Definition BKE_mask.hh:19
#define PLANE_TRACK_VIEW_SELECTED(plane_track)
#define TRACK_VIEW_SELECTED(sc, track)
File and directory operations.
#define O_BINARY
#define FILE_MAX
#define IMG_SIZE_FALLBACK
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_DISPLAY
Definition WM_types.hh:534
#define NC_MOVIECLIP
Definition WM_types.hh:410
#define NC_MASK
Definition WM_types.hh:411
#define NA_SELECTED
Definition WM_types.hh:639
volatile int lock
#define CLAMP(a, b, c)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define IN_RANGE_INCL(a, b, c)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
float3x3 scale(float3x3 mat, float2 scale)
const int state
void view2d_view_to_region(const View2D *v2d, float x, float y, int *r_region_x, int *r_region_y) ATTR_NONNULL()
Definition view2d.cc:1705
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void ED_space_clip_get_size(const SpaceClip *sc, int *r_width, int *r_height)
ImBuf * ED_space_clip_get_buffer(const SpaceClip *sc)
void BKE_movieclip_get_size(MovieClip *clip, const MovieClipUser *user, int *r_width, int *r_height)
bool ED_space_clip_maskedit_mask_visible_splines_poll(bContext *C)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
MINLINE void copy_v3_v3(float r[3], const float a[3])
bool ED_space_clip_get_position(const SpaceClip *sc, const ARegion *region, const int mval[2], float r_fpos[2])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
MovieTrackingObject * BKE_tracking_object_get_active(const MovieTracking *tracking)
Definition tracking.cc:1982
@ TASK_PRIORITY_LOW
Definition BLI_task_c.hh:60
void ED_clip_point_undistorted_pos(const SpaceClip *sc, const float co[2], float r_co[2])
void BKE_movieclip_clear_cache(MovieClip *clip)
@ WM_JOB_TYPE_CLIP_PREFETCH
Definition WM_api.hh:2031
static int prefetch_find_uncached_frame(MovieClip *clip, int from_frame, int end_frame, eMovieClipProxy_RenderSize render_size, eMovieClipProxy_RenderFlag render_flag, short direction)
int BLI_task_scheduler_num_threads()
SpaceClip * CTX_wm_space_clip(const bContext *C)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
int64_t BLI_read(int fd, void *buf, size_t nbytes)
Definition fileops_c.cc:98
void ED_clip_point_stable_pos__reverse(const SpaceClip *sc, const ARegion *region, const float co[2], float r_co[2])
the reverse of ED_clip_point_stable_pos(), gets the marker region coords. better name here?...
static bool prefetch_check_early_out(const bContext *C)
ImBuf * BKE_movieclip_get_postprocessed_ibuf(MovieClip *clip, const MovieClipUser *user, MovieClipPostprocFlag postprocess_flag)
void ED_clip_point_stable_pos(const SpaceClip *sc, const ARegion *region, float x, float y, float *xr, float *yr)
wmWindow * CTX_wm_window(const bContext *C)
static void prefetch_task_func(TaskPool *__restrict pool, void *task_data)
bool clip_view_calculate_view_selection(const bContext *C, bool fit, float *r_offset_x, float *r_offset_y, float *r_zoom)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
bool ED_space_clip_view_clip_with_region_poll(bContext *C)
@ MCLIP_PROXY_RENDER_UNDISTORT
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ MCLIP_PROXY_RENDER_SIZE_FULL
bool ED_space_clip_check_show_trackedit(const SpaceClip *sc)
void ED_clip_view_lock_state_restore_no_jump(const bContext *C, const ClipViewLockState *state)
@ LIB_ID_COPY_LOCALIZE
bool ED_space_clip_maskedit_visible_splines_poll(bContext *C)
static int prefetch_get_content_start(const bContext *C)
BLI_INLINE bool ED_space_clip_marker_is_visible(const SpaceClip *space_clip, const MovieTrackingObject *tracking_object, const MovieTrackingTrack *track, const MovieTrackingMarker *marker)
void ED_space_clip_get_aspect(const SpaceClip *sc, float *r_aspx, float *r_aspy)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
void ED_space_clip_set_mask(bContext *C, SpaceClip *sc, Mask *mask)
bool ED_space_clip_poll(bContext *C)
void BKE_libblock_free_data(ID *id, bool do_id_user) ATTR_NONNULL()
void * BLI_task_pool_user_data(TaskPool *pool)
Definition task_pool.cc:460
Global G
Definition blender.cc:57
ImBuf * BKE_movieclip_anim_ibuf_for_frame_no_lock(MovieClip *clip, const MovieClipUser *user)
static void start_prefetch_threads(MovieClip *clip, int start_frame, int current_frame, int end_frame, eMovieClipProxy_RenderSize render_size, eMovieClipProxy_RenderFlag render_flag, bool *stop, bool *do_update, float *progress)
void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], const ColorSpace *colorspace)
void BKE_movieclip_get_aspect(MovieClip *clip, float *aspx, float *aspy)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void IMB_freeImBuf(ImBuf *ibuf)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
unsigned char uchar
Definition BKE_text.h:58
void BKE_tracking_undistort_v2(MovieTracking *tracking, int image_width, int image_height, const float co[2], float r_co[2])
Definition tracking.cc:2448
bool ED_clip_view_selection(const bContext *C, const ARegion *region, bool fit)
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
wmWindowManager * CTX_wm_manager(const bContext *C)
static bool check_prefetch_break()
int ED_space_clip_get_clip_frame_number(const SpaceClip *sc)
bool ED_space_clip_check_show_maskedit(const SpaceClip *sc)
ImBuf * ED_space_clip_get_stable_buffer(const SpaceClip *sc, float loc[2], float *scale, float *angle)
bool ED_space_clip_color_sample(const SpaceClip *sc, const ARegion *region, const int mval[2], float r_col[3])
bool BKE_movieclip_put_frame_if_possible(MovieClip *clip, const MovieClipUser *user, ImBuf *ibuf)
ImBuf * BKE_movieclip_get_stable_ibuf(MovieClip *clip, const MovieClipUser *user, MovieClipPostprocFlag postprocess_flag, float r_loc[2], float *r_scale, float *r_angle)
void ED_space_clip_get_aspect_dimension_aware(const SpaceClip *sc, float *r_aspx, float *r_aspy)
MovieClip * CTX_data_edit_movieclip(const bContext *C)
static uchar * prefetch_thread_next_frame(PrefetchQueue *queue, MovieClip *clip, size_t *r_size, int *r_current_frame)
@ MASK_DRAWFLAG_SPLINE
void ED_clip_mouse_pos(const SpaceClip *sc, const ARegion *region, const int mval[2], float r_co[2])
static bool prefetch_movie_frame(MovieClip *clip, MovieClip *clip_local, int frame, eMovieClipProxy_RenderSize render_size, eMovieClipProxy_RenderFlag render_flag, bool *stop)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
void BKE_movieclip_user_set_frame(MovieClipUser *user, int framenr)
bool ED_space_clip_maskedit_poll(bContext *C)
float BKE_movieclip_remap_scene_to_clip_frame(const MovieClip *clip, float framenr)
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:404
void id_us_ensure_real(ID *id)
Definition lib_id.cc:388
ImBuf * IMB_load_image_from_memory(const unsigned char *mem, size_t size, ImBufFlags flags, const char *descr, const char *filepath=nullptr, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:132
bool ED_space_clip_maskedit_mask_poll(bContext *C)
int BKE_movieclip_get_duration(MovieClip *clip)
static int prefetch_get_final_frame(const bContext *C)
MovieClip * ED_space_clip_get_clip(const SpaceClip *sc)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
void ED_clip_select_all(const SpaceClip *sc, int action, bool *r_has_selection)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
void ED_space_clip_get_size_fl(const SpaceClip *sc, float r_size[2])
std::mutex Mutex
Definition BLI_mutex.hh:51
static void do_prefetch_movie(MovieClip *clip, MovieClip *clip_local, int start_frame, int current_frame, int end_frame, eMovieClipProxy_RenderSize render_size, eMovieClipProxy_RenderFlag render_flag, bool *stop, bool *do_update, float *progress)
size_t BLI_file_descriptor_size(int file) ATTR_WARN_UNUSED_RESULT
Definition storage.cc:219
MINLINE void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
bool BKE_movieclip_has_cached_frame(MovieClip *clip, const MovieClipUser *user)
bool ED_space_clip_tracking_poll(bContext *C)
static uchar * prefetch_read_file_to_memory(MovieClip *clip, int current_frame, eMovieClipProxy_RenderSize render_size, eMovieClipProxy_RenderFlag render_flag, size_t *r_size)
Scene * CTX_data_scene(const bContext *C)
bScreen * CTX_wm_screen(const bContext *C)
static void prefetch_startjob(void *pjv, wmJobWorkerStatus *worker_status)
void BKE_libblock_free_datablock(ID *id, int flag) ATTR_NONNULL()
static void prefetch_freejob(void *pjv)
Mask * ED_space_clip_get_mask(const SpaceClip *sc)
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
void ED_space_clip_set_clip(bContext *C, bScreen *screen, SpaceClip *sc, MovieClip *clip)
void ED_clip_view_lock_state_store(const bContext *C, ClipViewLockState *state)
bScreen * WM_window_get_active_screen(const wmWindow *win)
MINLINE int min_ii(int a, int b)
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
ARegion * CTX_wm_region(const bContext *C)
void ED_space_clip_get_zoom(const SpaceClip *sc, const ARegion *region, float *r_zoomx, float *r_zoomy)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
void ED_clip_update_frame(const Main *mainp, int cfra)
void BKE_tracking_distort_v2(MovieTracking *tracking, int image_width, int image_height, const float co[2], float r_co[2])
Definition tracking.cc:2421
bool ED_space_clip_view_clip_poll(bContext *C)
void clip_start_prefetch_job(const bContext *C)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_movieclip_filepath_for_frame(MovieClip *clip, const MovieClipUser *user, char *filepath)
MovieTrackingMarker * BKE_tracking_marker_get(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1363
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegion_Type regiontype
void * py_instance
Definition DNA_ID.h:519
const ColorSpace * colorspace
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
const float * float_data() const
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
eMovieClipProxy_RenderFlag render_flag
eMovieClipProxy_RenderSize render_size
ColorManagedColorspaceSettings colorspace_settings
struct MovieTracking tracking
ListBaseT< MovieTrackingTrack > tracks
ListBaseT< MovieTrackingPlaneTrack > plane_tracks
MovieTrackingCamera camera
eMovieClipProxy_RenderSize render_size
eMovieClipProxy_RenderFlag render_flag
eMovieClipProxy_RenderFlag render_flag
eMovieClipProxy_RenderSize render_size
struct RenderData r
ListBaseT< SpaceLink > spacedata
struct MovieClip * clip
eSpaceClip_Flag flag
eSpaceClip_View view
MaskSpaceInfo mask_info
eSpaceClip_Mode mode
struct MovieClipScopes scopes
struct MovieClipUser user
struct SpaceClipOverlay overlay
ListBaseT< struct ScrArea > areabase