Blender  V2.93
sequencer_preview.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include "DNA_sequence_types.h"
25 #include "DNA_sound_types.h"
26 
27 #include "BLI_listbase.h"
28 #include "BLI_threads.h"
29 
30 #include "BKE_context.h"
31 #include "BKE_global.h"
32 #include "BKE_sound.h"
33 
34 #include "WM_api.h"
35 #include "WM_types.h"
36 
37 #include "ED_screen.h"
38 
39 #include "MEM_guardedalloc.h"
40 
41 #include "sequencer_intern.h"
42 
43 typedef struct PreviewJob {
47  int total;
48  int processed;
50 
51 typedef struct PreviewJobAudio {
53  struct Main *bmain;
55  int lr; /* Sample left or right. */
57  bool waveform; /* Reload sound or waveform. */
59 
60 static void free_preview_job(void *data)
61 {
62  PreviewJob *pj = (PreviewJob *)data;
63 
64  BLI_mutex_free(pj->mutex);
65  BLI_freelistN(&pj->previews);
66  MEM_freeN(pj);
67 }
68 
69 /* Only this runs inside thread. */
70 static void preview_startjob(void *data, short *stop, short *do_update, float *progress)
71 {
72  PreviewJob *pj = data;
73  PreviewJobAudio *previewjb;
74 
75  BLI_mutex_lock(pj->mutex);
76  previewjb = pj->previews.first;
78 
79  while (previewjb) {
80  PreviewJobAudio *preview_next;
81  bSound *sound = previewjb->sound;
82 
83  BKE_sound_read_waveform(previewjb->bmain, sound, stop);
84 
85  if (*stop || G.is_break) {
86  BLI_mutex_lock(pj->mutex);
87  previewjb = previewjb->next;
89  while (previewjb) {
90  sound = previewjb->sound;
91 
92  /* Make sure we cleanup the loading flag! */
93  BLI_spin_lock(sound->spinlock);
95  BLI_spin_unlock(sound->spinlock);
96 
97  BLI_mutex_lock(pj->mutex);
98  previewjb = previewjb->next;
100  }
101 
102  BLI_mutex_lock(pj->mutex);
103  BLI_freelistN(&pj->previews);
104  pj->total = 0;
105  pj->processed = 0;
106  BLI_mutex_unlock(pj->mutex);
107  break;
108  }
109 
110  BLI_mutex_lock(pj->mutex);
111  preview_next = previewjb->next;
112  BLI_freelinkN(&pj->previews, previewjb);
113  previewjb = preview_next;
114  pj->processed++;
115  *progress = (pj->total > 0) ? (float)pj->processed / (float)pj->total : 1.0f;
116  *do_update = true;
117  BLI_mutex_unlock(pj->mutex);
118  }
119 }
120 
121 static void preview_endjob(void *data)
122 {
123  PreviewJob *pj = data;
124 
126 }
127 
129 {
130  wmJob *wm_job;
131  PreviewJob *pj;
133  PreviewJobAudio *audiojob = MEM_callocN(sizeof(PreviewJobAudio), "preview_audio");
134  wm_job = WM_jobs_get(CTX_wm_manager(C),
135  CTX_wm_window(C),
136  CTX_data_scene(C),
137  "Strip Previews",
140 
141  /* Get the preview job if it exists. */
142  pj = WM_jobs_customdata_get(wm_job);
143 
144  if (!pj) {
145  pj = MEM_callocN(sizeof(PreviewJob), "preview rebuild job");
146 
147  pj->mutex = BLI_mutex_alloc();
148  pj->scene = CTX_data_scene(C);
149 
153  }
154 
155  audiojob->bmain = CTX_data_main(C);
156  audiojob->sound = seq->sound;
157 
158  BLI_mutex_lock(pj->mutex);
159  BLI_addtail(&pj->previews, audiojob);
160  pj->total++;
161  BLI_mutex_unlock(pj->mutex);
162 
163  if (!WM_jobs_is_running(wm_job)) {
164  G.is_break = false;
165  WM_jobs_start(CTX_wm_manager(C), wm_job);
166  }
167 
169 }
typedef float(TangentPoint)[2]
struct ScrArea * CTX_wm_area(const bContext *C)
Definition: context.c:714
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:699
void BKE_sound_read_waveform(struct Main *bmain, struct bSound *sound, short *stop)
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:281
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_mutex_free(ThreadMutex *mutex)
Definition: threads.cc:428
ThreadMutex * BLI_mutex_alloc(void)
Definition: threads.cc:421
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:401
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:406
void BLI_spin_unlock(SpinLock *spin)
Definition: threads.cc:480
void BLI_spin_lock(SpinLock *spin)
Definition: threads.cc:461
pthread_mutex_t ThreadMutex
Definition: BLI_threads.h:83
@ SOUND_TAGS_WAVEFORM_LOADING
void ED_area_tag_redraw(ScrArea *area)
Definition: area.c:745
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
@ WM_JOB_TYPE_SEQ_BUILD_PREVIEW
Definition: WM_api.h:748
@ WM_JOB_PROGRESS
Definition: WM_api.h:726
#define ND_SEQUENCER
Definition: WM_types.h:337
#define NC_SCENE
Definition: WM_types.h:279
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void area(int d1, int d2, int e1, int e2, float weights[2])
static void preview_startjob(void *data, short *stop, short *do_update, float *progress)
static void preview_endjob(void *data)
struct PreviewJobAudio PreviewJobAudio
void sequencer_preview_add_sound(const bContext *C, Sequence *seq)
struct PreviewJob PreviewJob
static void free_preview_job(void *data)
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
struct PreviewJobAudio * next
struct Main * bmain
struct PreviewJobAudio * prev
ListBase previews
ThreadMutex * mutex
struct bSound * sound
void * spinlock
short tags
Definition: wm_jobs.c:73
#define G(x, y, z)
void WM_main_add_notifier(unsigned int type, void *reference)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition: wm_jobs.c:450
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, void *owner, const char *name, int flag, int job_type)
Definition: wm_jobs.c:196
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition: wm_jobs.c:372
void * WM_jobs_customdata_get(wmJob *wm_job)
Definition: wm_jobs.c:336
bool WM_jobs_is_running(wmJob *wm_job)
Definition: wm_jobs.c:325
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *))
Definition: wm_jobs.c:344
void WM_jobs_timer(wmJob *wm_job, double timestep, unsigned int note, unsigned int endnote)
Definition: wm_jobs.c:360