-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.c
288 lines (252 loc) · 6.18 KB
/
renderer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <gegl.h>
#include <mrg.h>
#include "gcut.h"
#include <SDL.h>
#include <gegl-audio-fragment.h>
static GThread *thread = NULL;
long babl_ticks (void);
static long prev_ticks = 0;
int rendering_frame = -1;
int done_frame = -1;
static int audio_started = 0;
static int audio_len = 0;
static int audio_pos = 0;
static int audio_post = 0;
#define AUDIO_BUF_LEN 819200000
int16_t audio_data[AUDIO_BUF_LEN];
void gcut_cache_invalid (GeglEDL *edl)
{
edl->frame = -1;
done_frame=-1;
rendering_frame=-1;
}
static void sdl_audio_cb(void *udata, Uint8 *stream, int len)
{
int audio_remaining = audio_len - audio_pos;
if (audio_remaining < 0)
return;
if (audio_remaining < len) len = audio_remaining;
//SDL_MixAudio(stream, (uint8_t*)&audio_data[audio_pos/2], len, SDL_MIX_MAXVOLUME);
memcpy (stream, (uint8_t*)&audio_data[audio_pos/2], len);
audio_pos += len;
audio_post += len;
if (audio_pos >= AUDIO_BUF_LEN)
{
audio_pos = 0;
}
}
static void sdl_add_audio_sample (int sample_pos, float left, float right)
{
audio_data[audio_len/2 + 0] = left * 32767.0 * 0.46;
audio_data[audio_len/2 + 1] = right * 32767.0 * 0.46;
audio_len += 4;
if (audio_len >= AUDIO_BUF_LEN)
{
audio_len = 0;
}
}
static void open_audio (int frequency)
{
SDL_AudioSpec spec = {0};
SDL_Init(SDL_INIT_AUDIO);
spec.freq = frequency;
spec.format = AUDIO_S16SYS;
spec.channels = 2;
spec.samples = 1024;
spec.callback = sdl_audio_cb;
SDL_OpenAudio(&spec, 0);
if (spec.format != AUDIO_S16SYS)
{
fprintf (stderr, "not getting format we wanted\n");
}
if (spec.freq != frequency)
{
fprintf (stderr, "not getting desires samplerate(%i) we wanted got %i instead\n", frequency, spec.freq);
}
}
static void end_audio (void)
{
}
static inline void skipped_frames (int count)
{
//fprintf (stderr, "[%i]", count);
}
static inline void wait_for_frame (void)
{
//fprintf (stderr, ".");
}
void playing_iteration (Mrg *mrg, GeglEDL *edl);
extern int got_cached;
static gpointer renderer_thread (gpointer data)
{
GeglEDL *edl = data;
for (;;)
{
playing_iteration (edl->mrg, edl);
{
if ((int)(edl->frame_pos_ui * edl->fps) != done_frame)
{
rendering_frame = edl->frame_pos_ui * edl->fps;
{
GeglRectangle ext = {0,0,edl->width, edl->height};
//GeglRectangle ext = gegl_node_get_bounding_box (edl->result);
gegl_buffer_set_extent (edl->buffer, &ext);
}
gcut_set_pos (edl, edl->frame_pos_ui); /* this does the frame-set, which causes render */
#if 0
{
GeglRectangle ext = gegl_node_get_bounding_box (edl->result);
gegl_buffer_set_extent (edl->buffer, &ext);
}
#endif
{
GeglAudioFragment *audio = gcut_get_audio (edl);
if (audio)
{
int sample_count = gegl_audio_fragment_get_sample_count (audio);
if (sample_count > 0)
{
int i;
if (!audio_started)
{
open_audio (gegl_audio_fragment_get_sample_rate (audio));
SDL_PauseAudio(0);
audio_started = 1;
}
for (i = 0; i < sample_count; i++)
{
sdl_add_audio_sample (0, audio->data[0][i], audio->data[1][i]);
}
}
}
}
done_frame = rendering_frame;
mrg_queue_draw (edl->mrg, NULL); // could queue only rect - if we had it
}
else
{
g_usleep (50);
}
}
}
end_audio ();
return NULL;
}
void renderer_start (GeglEDL *edl)
{
if (!thread)
thread = g_thread_new ("renderer", renderer_thread, edl);
}
gboolean cache_renderer_iteration (Mrg *mrg, gpointer data);
void renderer_toggle_playing (MrgEvent *event, void *data1, void *data2)
{
GeglEDL *edl = data1;
edl->playing = !edl->playing;
if (!edl->playing)
{
cache_renderer_iteration (event->mrg, edl);
}
else
{
killpg(0, SIGUSR2);
}
mrg_event_stop_propagate (event);
mrg_queue_draw (event->mrg, NULL);
prev_ticks = babl_ticks ();
}
static double max_pos (GeglEDL *edl)
{
GList *l;
int t = 0;
double start, end;
gcut_get_range (edl, &start, &end);
if (end)
return end;
for (l = edl->clips; l; l = l->next)
{
Clip *clip = l->data;
t += clip_get_duration (clip);
}
return t;
}
void playing_iteration (Mrg *mrg, GeglEDL *edl)
{
long ticks = 0;
double delta = 1;
double fragment = 1.0 / edl->fps;
ticks = babl_ticks ();
if (prev_ticks == 0) prev_ticks = ticks;
if (edl->playing)
{
#if 0
if (prev_ticks - ticks < 1000000.0 / gcut_get_fps (edl))
return;
#endif
delta = (((ticks - prev_ticks) / 1000000.0) * ( edl->fps ));
//fprintf (stderr, "%f\n", delta);
if (delta < 1.0)
{
wait_for_frame ();
mrg_queue_draw (mrg, NULL);
return;
}
{
#if 0
static int frameskip = -1;
if (frameskip < 0)
{
if (getenv ("GEDL_FRAMESKIP"))
frameskip = 1;
else
frameskip = 0;
}
if (!frameskip)
delta = 1;
#else
if (edl->framedrop)
{
if (delta >= 2.0)
{
skipped_frames ( (int)(delta)-1 );
}
}
else
{
if (delta > 1.0)
delta = 1;
else
delta = 0;
}
#endif
}
if (rendering_frame != done_frame)
return;
if (delta >= 1.0)
{
if (edl->active_clip)
{
double start, end;
edl->frame_pos_ui += delta * fragment;
gcut_get_range (edl, &start, &end);
if (edl->frame_pos_ui > max_pos (edl))
{
edl->frame_pos_ui = 0;
if (end)
edl->frame_pos_ui = start;
}
gcut_snap_ui_pos (edl);
edl->active_clip = edl_get_clip_for_pos (edl, edl->frame_pos_ui);
prev_ticks = ticks;
}
}
}
}
int renderer_done (GeglEDL *edl)
{
return done_frame == (gint)(edl->frame_pos_ui * edl->fps); //rendering_frame;
}