-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathlib.rs
1092 lines (955 loc) · 36.9 KB
/
lib.rs
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use anyhow::Result;
use cap_project::{
AspectRatio, BackgroundSource, CameraXPosition, CameraYPosition, Crop, CursorEvents,
ProjectConfiguration, RecordingMeta, StudioRecordingMeta, XY,
};
use composite_frame::{CompositeVideoFramePipeline, CompositeVideoFrameUniforms};
use core::f64;
use decoder::{spawn_decoder, AsyncVideoDecoderHandle};
use frame_pipeline::{FramePipeline, FramePipelineEncoder, FramePipelineState};
use futures::future::OptionFuture;
use futures::FutureExt;
use layers::{
Background, BackgroundBlurPipeline, BackgroundLayer, CameraLayer, CursorLayer, DisplayLayer,
GradientOrColorPipeline, ImageBackgroundPipeline,
};
use specta::Type;
use std::{collections::HashMap, sync::Arc};
use tokio::sync::mpsc;
use image::GenericImageView;
use std::{path::PathBuf, time::Instant};
mod composite_frame;
mod coord;
pub mod decoder;
mod frame_pipeline;
mod layers;
mod project_recordings;
mod spring_mass_damper;
mod zoom;
pub use coord::*;
pub use decoder::DecodedFrame;
pub use project_recordings::{ProjectRecordings, SegmentRecordings, Video};
use zoom::*;
const STANDARD_CURSOR_HEIGHT: f32 = 75.0;
#[derive(Debug, Clone, Copy, Type)]
pub struct RenderOptions {
pub camera_size: Option<XY<u32>>,
pub screen_size: XY<u32>,
}
#[derive(Clone)]
pub struct RecordingSegmentDecoders {
screen: AsyncVideoDecoderHandle,
camera: Option<AsyncVideoDecoderHandle>,
}
pub struct SegmentVideoPaths {
pub display: PathBuf,
pub camera: Option<PathBuf>,
}
impl RecordingSegmentDecoders {
pub async fn new(
recording_meta: &RecordingMeta,
meta: &StudioRecordingMeta,
segment: SegmentVideoPaths,
) -> Result<Self, String> {
let screen = spawn_decoder(
"screen",
recording_meta.project_path.join(segment.display),
match &meta {
StudioRecordingMeta::SingleSegment { segment } => segment.display.fps,
StudioRecordingMeta::MultipleSegments { inner } => inner.segments[0].display.fps,
},
)
.await
.map_err(|e| format!("Screen:{e}"))?;
let camera = OptionFuture::from(segment.camera.map(|camera| {
spawn_decoder(
"camera",
recording_meta.project_path.join(camera),
match &meta {
StudioRecordingMeta::SingleSegment { segment } => {
segment.camera.as_ref().unwrap().fps
}
StudioRecordingMeta::MultipleSegments { inner } => {
inner.segments[0].camera.as_ref().unwrap().fps
}
},
)
.then(|r| async { r.map_err(|e| format!("Camera:{e}")) })
}))
.await
.transpose()?;
Ok(Self { screen, camera })
}
pub async fn get_frames(
&self,
segment_time: f32,
needs_camera: bool,
) -> Option<DecodedSegmentFrames> {
let (screen, camera) = tokio::join!(
self.screen.get_frame(segment_time),
OptionFuture::from(
needs_camera
.then(|| self.camera.as_ref().map(|d| d.get_frame(segment_time)))
.flatten()
)
);
Some(DecodedSegmentFrames {
screen_frame: screen?,
camera_frame: camera.flatten(),
segment_time,
})
}
}
#[derive(thiserror::Error, Debug)]
pub enum RenderingError {
#[error("No GPU adapter found")]
NoAdapter,
#[error(transparent)]
RequestDeviceFailed(#[from] wgpu::RequestDeviceError),
#[error("Failed to wait for buffer mapping")]
BufferMapWaitingFailed,
#[error(transparent)]
BufferMapFailed(#[from] wgpu::BufferAsyncError),
#[error("Sending frame to channel failed")]
ChannelSendFrameFailed(#[from] mpsc::error::SendError<(RenderedFrame, u32)>),
#[error("Failed to load image: {0}")]
ImageLoadError(String),
}
pub struct RenderSegment {
pub cursor: Arc<CursorEvents>,
pub decoders: RecordingSegmentDecoders,
}
pub async fn render_video_to_channel(
options: RenderOptions,
project: ProjectConfiguration,
sender: mpsc::Sender<(RenderedFrame, u32)>,
recording_meta: &RecordingMeta,
meta: &StudioRecordingMeta,
segments: Vec<RenderSegment>,
fps: u32,
resolution_base: XY<u32>,
) -> Result<(), RenderingError> {
let constants = RenderVideoConstants::new(options, recording_meta, meta).await?;
let recordings = ProjectRecordings::new(&recording_meta.project_path, meta);
ffmpeg::init().unwrap();
let start_time = Instant::now();
// Get the duration from the timeline if it exists, otherwise use the longest source duration
let duration = get_duration(&recordings, recording_meta, meta, &project);
let total_frames = (fps as f64 * duration).ceil() as u32;
println!(
"Final export duration: {} seconds ({} frames at {}fps)",
duration, total_frames, fps
);
let mut frame_number = 0;
let background = project.background.source.clone();
let mut frame_renderer = FrameRenderer::new(&constants);
loop {
if frame_number >= total_frames {
break;
}
let Some((segment_time, segment_i)) =
project.get_segment_time(frame_number as f64 / fps as f64)
else {
break;
};
let segment = &segments[segment_i as usize];
// do this after all usages but before any 'continue' to handle frame skip
let frame_number = {
let prev = frame_number;
std::mem::replace(&mut frame_number, prev + 1)
};
if let Some(segment_frames) = segment
.decoders
.get_frames(segment_time as f32, !project.camera.hide)
.await
{
let uniforms =
ProjectUniforms::new(&constants, &project, frame_number, fps, resolution_base);
let frame = frame_renderer
.render(segment_frames, uniforms, &segment.cursor)
.await?;
if frame.width == 0 || frame.height == 0 {
continue;
}
sender.send((frame, frame_number)).await?;
}
}
let total_time = start_time.elapsed();
println!(
"Render complete. Processed {frame_number} frames in {:?} seconds",
total_time.as_secs_f32()
);
Ok(())
}
pub fn get_duration(
recordings: &ProjectRecordings,
recording_meta: &RecordingMeta,
meta: &StudioRecordingMeta,
project: &ProjectConfiguration,
) -> f64 {
let mut max_duration = recordings.duration();
println!("Initial screen recording duration: {}", max_duration);
// Check camera duration if it exists
if let Some(camera_path) = meta.camera_path() {
if let Ok(camera_duration) =
recordings.get_source_duration(&recording_meta.path(&camera_path))
{
println!("Camera recording duration: {}", camera_duration);
max_duration = max_duration.max(camera_duration);
println!("New max duration after camera check: {}", max_duration);
}
}
// If there's a timeline, ensure all segments extend to the max duration
if let Some(timeline) = &project.timeline {
println!("Found timeline with {} segments", timeline.segments.len());
// for (i, segment) in timeline.segments.iter().enumerate() {
// println!(
// "Segment {} - current end: {}, max_duration: {}",
// i, segment.end, max_duration
// );
// if segment.end < max_duration {
// segment.end = max_duration;
// println!("Extended segment {} to new end: {}", i, segment.end);
// }
// }
let final_duration = timeline.duration();
println!(
"Final timeline duration after adjustments: {}",
final_duration
);
final_duration
} else {
println!("No timeline found, using max_duration: {}", max_duration);
max_duration
}
}
pub struct CursorTexture {
inner: wgpu::Texture,
hotspot: XY<f64>,
}
pub struct RenderVideoConstants {
pub _instance: wgpu::Instance,
pub _adapter: wgpu::Adapter,
pub queue: wgpu::Queue,
pub device: wgpu::Device,
pub options: RenderOptions,
composite_video_frame_pipeline: CompositeVideoFramePipeline,
pub cursor_textures: HashMap<String, CursorTexture>,
gradient_or_color_pipeline: GradientOrColorPipeline,
image_background_pipeline: ImageBackgroundPipeline,
pub background_blur_pipeline: BackgroundBlurPipeline,
background_textures: std::sync::Arc<tokio::sync::RwLock<HashMap<String, wgpu::Texture>>>,
screen_frame: (wgpu::Texture, wgpu::TextureView),
camera_frame: Option<(wgpu::Texture, wgpu::TextureView)>,
cursor_layer: CursorLayer,
}
impl RenderVideoConstants {
pub async fn new(
options: RenderOptions,
recording_meta: &RecordingMeta,
meta: &StudioRecordingMeta,
) -> Result<Self, RenderingError> {
println!("Initializing wgpu...");
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::default());
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions::default())
.await
.ok_or(RenderingError::NoAdapter)?;
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
required_features: wgpu::Features::MAPPABLE_PRIMARY_BUFFERS,
..Default::default()
},
None,
)
.await?;
let cursor_textures = Self::load_cursor_textures(&device, &queue, recording_meta, meta);
let composite_video_frame_pipeline = CompositeVideoFramePipeline::new(&device);
let gradient_or_color_pipeline = GradientOrColorPipeline::new(&device);
let image_background_pipeline = ImageBackgroundPipeline::new(&device);
let background_textures = Arc::new(tokio::sync::RwLock::new(HashMap::new()));
let background_blur_pipeline = BackgroundBlurPipeline::new(&device);
let screen_frame = {
let texture = device.create_texture(
&(wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: options.screen_size.x,
height: options.screen_size.y,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::COPY_DST,
label: Some("Screen Frame texture"),
view_formats: &[],
}),
);
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
(texture, texture_view)
};
let camera_frame = options.camera_size.map(|s| {
let texture = device.create_texture(
&(wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: s.x,
height: s.y,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::COPY_DST,
label: Some("Camera texture"),
view_formats: &[],
}),
);
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
(texture, texture_view)
});
Ok(Self {
_instance: instance,
_adapter: adapter,
cursor_layer: CursorLayer::new(&device),
device,
queue,
options,
composite_video_frame_pipeline,
gradient_or_color_pipeline,
cursor_textures,
image_background_pipeline,
background_textures,
screen_frame,
camera_frame,
background_blur_pipeline,
})
}
fn load_cursor_textures(
device: &wgpu::Device,
queue: &wgpu::Queue,
recording_meta: &RecordingMeta,
meta: &StudioRecordingMeta,
) -> HashMap<String, CursorTexture> {
println!("Starting to load cursor textures");
println!("Project path: {:?}", recording_meta.project_path);
// println!("Cursor images to load: {:?}", cursor.cursor_images);
let mut textures = HashMap::new();
// Create the full path to the cursors directory
let cursors_dir = recording_meta.project_path.join("content").join("cursors");
println!("Cursors directory: {:?}", cursors_dir);
let cursor_images = match &meta {
StudioRecordingMeta::SingleSegment { .. } => Default::default(),
StudioRecordingMeta::MultipleSegments { inner } => {
inner.cursor_images(recording_meta).unwrap_or_default()
}
};
for (cursor_id, cursor) in &cursor_images.0 {
println!(
"Loading cursor image: {} -> {}",
cursor_id,
cursor.path.display()
);
println!("Full cursor path: {:?}", cursor);
if !cursor.path.exists() {
println!("Cursor image file does not exist: {:?}", cursor);
continue;
}
match image::open(&cursor.path) {
Ok(img) => {
let dimensions = img.dimensions();
println!(
"Loaded cursor image dimensions: {}x{}",
dimensions.0, dimensions.1
);
let rgba = img.into_rgba8();
// Create the texture
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some(&format!("Cursor Texture {}", cursor_id)),
size: wgpu::Extent3d {
width: dimensions.0,
height: dimensions.1,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
queue.write_texture(
wgpu::ImageCopyTexture {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&rgba,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(4 * dimensions.0),
rows_per_image: None,
},
wgpu::Extent3d {
width: dimensions.0,
height: dimensions.1,
depth_or_array_layers: 1,
},
);
textures.insert(
cursor_id.clone(),
CursorTexture {
inner: texture,
hotspot: cursor.hotspot,
},
);
println!("Successfully loaded cursor texture: {}", cursor_id);
}
Err(e) => {
println!(
"Failed to load cursor image {}: {}",
cursor.path.display(),
e
);
// Don't return error, just skip this cursor image
continue;
}
}
}
println!(
"Completed loading cursor textures. Total loaded: {}",
textures.len()
);
textures
}
}
#[derive(Clone, Debug)]
pub struct ProjectUniforms {
pub output_size: (u32, u32),
pub cursor_size: f32,
display: CompositeVideoFrameUniforms,
camera: Option<CompositeVideoFrameUniforms>,
pub project: ProjectConfiguration,
pub zoom: InterpolatedZoom,
pub resolution_base: XY<u32>,
}
#[derive(Debug, Clone)]
pub struct Zoom {
pub amount: f64,
pub zoom_origin: Coord<FrameSpace>,
}
impl Zoom {
pub fn apply_scale(&self, screen_position: Coord<FrameSpace>) -> Coord<FrameSpace> {
(screen_position - self.zoom_origin) * self.amount + self.zoom_origin
}
}
const CAMERA_PADDING: f32 = 50.0;
const SCREEN_MAX_PADDING: f64 = 0.4;
impl ProjectUniforms {
fn get_crop(options: &RenderOptions, project: &ProjectConfiguration) -> Crop {
project.background.crop.as_ref().cloned().unwrap_or(Crop {
position: XY { x: 0, y: 0 },
size: XY {
x: options.screen_size.x,
y: options.screen_size.y,
},
})
}
fn get_padding(options: &RenderOptions, project: &ProjectConfiguration) -> f64 {
let crop = Self::get_crop(options, project);
let basis = u32::max(crop.size.x, crop.size.y);
let padding_factor = project.background.padding / 100.0 * SCREEN_MAX_PADDING;
basis as f64 * padding_factor
}
pub fn get_output_size(
options: &RenderOptions,
project: &ProjectConfiguration,
resolution_base: XY<u32>,
) -> (u32, u32) {
let crop = Self::get_crop(options, project);
let crop_aspect = crop.aspect_ratio();
let padding = Self::get_padding(options, project) * 2.0;
let (base_width, base_height) = match &project.aspect_ratio {
None => {
let width = ((crop.size.x as f64 + padding) as u32 + 1) & !1;
let height = ((crop.size.y as f64 + padding) as u32 + 1) & !1;
(width, height)
}
Some(AspectRatio::Square) => {
let size = if crop_aspect > 1.0 {
crop.size.y
} else {
crop.size.x
};
(size, size)
}
Some(AspectRatio::Wide) => {
if crop_aspect > 16.0 / 9.0 {
(((crop.size.y as f32 * 16.0 / 9.0) as u32), crop.size.y)
} else {
(crop.size.x, ((crop.size.x as f32 * 9.0 / 16.0) as u32))
}
}
Some(AspectRatio::Vertical) => {
if crop_aspect > 9.0 / 16.0 {
((crop.size.y as f32 * 9.0 / 16.0) as u32, crop.size.y)
} else {
(crop.size.x, ((crop.size.x as f32 * 16.0 / 9.0) as u32))
}
}
Some(AspectRatio::Classic) => {
if crop_aspect > 4.0 / 3.0 {
((crop.size.y as f32 * 4.0 / 3.0) as u32, crop.size.y)
} else {
(crop.size.x, ((crop.size.x as f32 * 3.0 / 4.0) as u32))
}
}
Some(AspectRatio::Tall) => {
if crop_aspect > 3.0 / 4.0 {
((crop.size.y as f32 * 3.0 / 4.0) as u32, crop.size.y)
} else {
(crop.size.x, ((crop.size.x as f32 * 4.0 / 3.0) as u32))
}
}
};
let width_scale = resolution_base.x as f32 / base_width as f32;
let height_scale = resolution_base.y as f32 / base_height as f32;
let scale = width_scale.min(height_scale);
let scaled_width = ((base_width as f32 * scale) as u32 + 1) & !1;
let scaled_height = ((base_height as f32 * scale) as u32 + 1) & !1;
return (scaled_width, scaled_height);
// ((base_width + 1) & !1, (base_height + 1) & !1)
}
pub fn display_offset(
options: &RenderOptions,
project: &ProjectConfiguration,
resolution_base: XY<u32>,
) -> Coord<FrameSpace> {
let output_size = Self::get_output_size(options, project, resolution_base);
let output_size = XY::new(output_size.0 as f64, output_size.1 as f64);
let output_aspect = output_size.x / output_size.y;
let crop = Self::get_crop(options, project);
let crop_start =
Coord::<RawDisplaySpace>::new(XY::new(crop.position.x as f64, crop.position.y as f64));
let crop_end = Coord::<RawDisplaySpace>::new(XY::new(
(crop.position.x + crop.size.x) as f64,
(crop.position.y + crop.size.y) as f64,
));
let cropped_size = crop_end.coord - crop_start.coord;
let cropped_aspect = cropped_size.x / cropped_size.y;
let padding = Self::get_padding(options, project);
let is_height_constrained = cropped_aspect <= output_aspect;
let available_size = output_size - 2.0 * padding;
let target_size = if is_height_constrained {
XY::new(available_size.y * cropped_aspect, available_size.y)
} else {
XY::new(available_size.x, available_size.x / cropped_aspect)
};
let target_offset = (output_size - target_size) / 2.0;
Coord::new(if is_height_constrained {
XY::new(target_offset.x, padding)
} else {
XY::new(padding, target_offset.y)
})
}
pub fn display_size(
options: &RenderOptions,
project: &ProjectConfiguration,
resolution_base: XY<u32>,
) -> Coord<FrameSpace> {
let output_size = Self::get_output_size(options, project, resolution_base);
let output_size = XY::new(output_size.0 as f64, output_size.1 as f64);
let display_offset = Self::display_offset(options, project, resolution_base);
let end = Coord::new(output_size) - display_offset;
end - display_offset
}
pub fn new(
constants: &RenderVideoConstants,
project: &ProjectConfiguration,
frame_number: u32,
fps: u32,
resolution_base: XY<u32>,
) -> Self {
let options = &constants.options;
let output_size = Self::get_output_size(options, project, resolution_base);
let frame_time = frame_number as f32 / fps as f32;
// let zoom_keyframes = ZoomKeyframes::new(project);
// let current_zoom = zoom_keyframes.interpolate(time as f64);
// let prev_zoom = zoom_keyframes.interpolate((time - 1.0 / 30.0) as f64);
let velocity = [0.0, 0.0];
// if current_zoom.amount != prev_zoom.amount {
// let scale_change = (current_zoom.amount - prev_zoom.amount) as f32;
// // Reduce the velocity scale from 0.05 to 0.02
// [
// (scale_change * output_size.0 as f32) * 0.02, // Reduced from 0.05
// (scale_change * output_size.1 as f32) * 0.02,
// ]
// } else {
// [0.0, 0.0]
// };
let motion_blur_amount = 0.0;
// if current_zoom.amount != prev_zoom.amount {
// project.motion_blur.unwrap_or(0.2) // Reduced from 0.5 to 0.2
// } else {
// 0.0
// };
let crop = Self::get_crop(options, project);
let segment_cursor = SegmentsCursor::new(
frame_time as f64,
project
.timeline
.as_ref()
.map(|t| t.zoom_segments.as_slice())
.unwrap_or(&[]),
);
let zoom = InterpolatedZoom::new(segment_cursor);
let display = {
let output_size = XY::new(output_size.0 as f64, output_size.1 as f64);
let size = [options.screen_size.x as f32, options.screen_size.y as f32];
let crop_start = Coord::<RawDisplaySpace>::new(XY::new(
crop.position.x as f64,
crop.position.y as f64,
));
let crop_end = Coord::<RawDisplaySpace>::new(XY::new(
(crop.position.x + crop.size.x) as f64,
(crop.position.y + crop.size.y) as f64,
));
let display_offset = Self::display_offset(options, project, resolution_base);
let display_size = Self::display_size(options, project, resolution_base);
let end = Coord::new(output_size) - display_offset;
let (zoom_start, zoom_end) = (
Coord::new(zoom.bounds.top_left * display_size.coord),
Coord::new((zoom.bounds.bottom_right - 1.0) * display_size.coord),
);
let start = display_offset + zoom_start;
let end = end + zoom_end;
let target_size = end - start;
let min_target_axis = target_size.x.min(target_size.y);
CompositeVideoFrameUniforms {
output_size: [output_size.x as f32, output_size.y as f32],
frame_size: size,
crop_bounds: [
crop_start.x as f32,
crop_start.y as f32,
crop_end.x as f32,
crop_end.y as f32,
],
target_bounds: [start.x as f32, start.y as f32, end.x as f32, end.y as f32],
target_size: [target_size.x as f32, target_size.y as f32],
rounding_px: (project.background.rounding / 100.0 * 0.5 * min_target_axis) as f32,
mirror_x: 0.0,
velocity_uv: velocity,
motion_blur_amount,
camera_motion_blur_amount: 0.0,
shadow: project.background.shadow,
shadow_size: project
.background
.advanced_shadow
.as_ref()
.map_or(50.0, |s| s.size),
shadow_opacity: project
.background
.advanced_shadow
.as_ref()
.map_or(18.0, |s| s.opacity),
shadow_blur: project
.background
.advanced_shadow
.as_ref()
.map_or(50.0, |s| s.blur),
_padding: [0.0; 3],
}
};
let camera = options
.camera_size
.filter(|_| !project.camera.hide)
.map(|camera_size| {
let output_size = [output_size.0 as f32, output_size.1 as f32];
let frame_size = [camera_size.x as f32, camera_size.y as f32];
let min_axis = output_size[0].min(output_size[1]);
// Calculate camera size based on zoom
let base_size = project.camera.size / 100.0;
let zoom_size = project
.camera
.zoom_size
.unwrap_or(cap_project::Camera::default_zoom_size())
/ 100.0;
let zoomed_size =
(zoom.t as f32) * zoom_size * base_size + (1.0 - zoom.t as f32) * base_size;
let size = [
min_axis * zoomed_size + CAMERA_PADDING,
min_axis * zoomed_size + CAMERA_PADDING,
];
let position = {
let x = match &project.camera.position.x {
CameraXPosition::Left => CAMERA_PADDING,
CameraXPosition::Center => output_size[0] / 2.0 - (size[0]) / 2.0,
CameraXPosition::Right => output_size[0] - CAMERA_PADDING - size[0],
};
let y = match &project.camera.position.y {
CameraYPosition::Top => CAMERA_PADDING,
CameraYPosition::Bottom => output_size[1] - size[1] - CAMERA_PADDING,
};
[x, y]
};
let target_bounds = [
position[0],
position[1],
position[0] + size[0],
position[1] + size[1],
];
// Calculate camera motion blur based on zoom transition
let camera_motion_blur = 0.0;
CompositeVideoFrameUniforms {
output_size,
frame_size,
crop_bounds: [
(frame_size[0] - frame_size[1]) / 2.0,
0.0,
frame_size[0] - (frame_size[0] - frame_size[1]) / 2.0,
frame_size[1],
],
target_bounds,
target_size: [
target_bounds[2] - target_bounds[0],
target_bounds[3] - target_bounds[1],
],
rounding_px: project.camera.rounding / 100.0 * 0.5 * size[0],
mirror_x: if project.camera.mirror { 1.0 } else { 0.0 },
velocity_uv: [0.0, 0.0],
motion_blur_amount,
camera_motion_blur_amount: camera_motion_blur,
shadow: project.camera.shadow,
shadow_size: project
.camera
.advanced_shadow
.as_ref()
.map_or(50.0, |s| s.size),
shadow_opacity: project
.camera
.advanced_shadow
.as_ref()
.map_or(18.0, |s| s.opacity),
shadow_blur: project
.camera
.advanced_shadow
.as_ref()
.map_or(50.0, |s| s.blur),
_padding: [0.0; 3],
}
});
Self {
output_size,
cursor_size: project.cursor.size as f32,
resolution_base,
display,
camera,
project: project.clone(),
zoom,
}
}
}
#[derive(Clone)]
pub struct RenderedFrame {
pub data: Vec<u8>,
pub width: u32,
pub height: u32,
pub padded_bytes_per_row: u32,
}
pub struct DecodedSegmentFrames {
pub screen_frame: DecodedFrame,
pub camera_frame: Option<DecodedFrame>,
pub segment_time: f32,
}
pub struct FrameRenderer<'a> {
constants: &'a RenderVideoConstants,
output_texture_desc: Option<wgpu::TextureDescriptor<'static>>,
output_textures: Option<(wgpu::Texture, wgpu::Texture)>,
}
impl<'a> FrameRenderer<'a> {
pub fn new(constants: &'a RenderVideoConstants) -> Self {
Self {
constants,
output_texture_desc: None,
output_textures: None,
}
}
fn update_output_textures(&mut self, width: u32, height: u32) {
if let Some(desc) = &self.output_texture_desc {
if desc.size.width == width && desc.size.height == height {
return;
}
}
let output_texture_desc = self.output_texture_desc.insert(wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::COPY_SRC,
label: Some("Intermediate Texture"),
view_formats: &[],
});
self.output_textures = Some((
self.constants.device.create_texture(output_texture_desc),
self.constants.device.create_texture(output_texture_desc),
));
}
pub async fn render(
&mut self,
segment_frames: DecodedSegmentFrames,
uniforms: ProjectUniforms,
cursor: &CursorEvents,
) -> Result<RenderedFrame, RenderingError> {
self.update_output_textures(uniforms.output_size.0, uniforms.output_size.1);
produce_frame(
&self.constants,
segment_frames,
uniforms,
self.output_textures.as_ref().unwrap(),
cursor,
)
.await
}
}
// TODO: reuse as many resources as possible
// https://github.com/gfx-rs/wgpu/wiki/Encapsulating-Graphics-Work
async fn produce_frame(
constants: &RenderVideoConstants,
segment_frames: DecodedSegmentFrames,
uniforms: ProjectUniforms,
textures: &(wgpu::Texture, wgpu::Texture),
cursor: &CursorEvents,
) -> Result<RenderedFrame, RenderingError> {
let background = Background::from(uniforms.project.background.source.clone());
let mut state = FramePipelineState::new(constants, &uniforms, textures);
let mut encoder = FramePipelineEncoder::new(&state);
{
let mut pipeline = FramePipeline {
state: &mut state,
encoder: &mut encoder,
};
BackgroundLayer::render(&mut pipeline, background).await?;
DisplayLayer::render(&mut pipeline, &segment_frames);
constants.cursor_layer.render(
&mut pipeline,
&segment_frames,
uniforms.resolution_base,
&cursor,
&uniforms.zoom,
);
if let (
Some(camera_size),
Some(camera_frame),
Some(uniforms),
Some((texture, texture_view)),
) = (
constants.options.camera_size,
&segment_frames.camera_frame,
&uniforms.camera,
&constants.camera_frame,
) {
CameraLayer::render(
&mut pipeline,
camera_size,
camera_frame,
uniforms,
(texture, texture_view),
);