Skip to content

Commit 136ac2a

Browse files
committed
added multithreading and gpu supprot for exporting
1 parent 87d3f06 commit 136ac2a

7 files changed

Lines changed: 1105 additions & 84 deletions

File tree

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,20 @@ endif()
131131
# Create a library
132132
add_library(pythonic src/pythonicDispatchStubs.cpp)
133133

134+
# Find and require threading support (needed for std::thread in video export)
135+
set(THREADS_PREFER_PTHREAD_FLAG ON)
136+
find_package(Threads REQUIRED)
137+
134138
# Set the include directory for the library
135139
# Use generator expressions for build/install interface
136140
target_include_directories(pythonic PUBLIC
137141
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
138142
$<INSTALL_INTERFACE:include>
139143
)
140144

145+
# Link threading library
146+
target_link_libraries(pythonic PUBLIC Threads::Threads)
147+
141148
# Link audio libraries if enabled
142149
if(PYTHONIC_SDL2_FOUND)
143150
target_compile_definitions(pythonic PUBLIC PYTHONIC_ENABLE_SDL2_AUDIO)

docs/Print/print.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ bool export_media(
373373
int threshold = 128,
374374
Audio audio = Audio::off,
375375
int fps = 0,
376-
const ExportConfig& config = ExportConfig() // Customize rendering style
376+
const ExportConfig& config = ExportConfig(), // Customize rendering style
377+
bool use_gpu = true // Use GPU/hardware acceleration
377378
);
378379
```
379380

@@ -391,6 +392,7 @@ bool export_media(
391392
| `audio` | `Audio` | `Audio::off` | Whether to include audio in video exports |
392393
| `fps` | `int` | `0` | Frame rate for video export (0 = original fps) |
393394
| `config` | `ExportConfig` | `ExportConfig()` | Rendering customization (dot_size, density, colors) |
395+
| `use_gpu` | `bool` | `true` | Use GPU/hardware acceleration for encoding (see below) |
394396

395397
### Format Enum
396398

@@ -573,12 +575,18 @@ int main() {
573575

574576
### How Video Export Works
575577

576-
1. **Frame extraction**: FFmpeg extracts frames from the source video
577-
2. **ASCII rendering**: Each frame is rendered to ASCII/Braille art using the specified mode
578+
1. **Preprocessing**: FFmpeg extracts frames from the source video (progress shown with animated bar)
579+
2. **ASCII rendering**: Each frame is rendered to ASCII/Braille art using **multithreaded processing**
578580
3. **Image rendering**: The ASCII art is rendered to actual pixels (proper Braille dot rendering)
579-
4. **Video encoding**: All frames are combined back into an MP4 video
581+
4. **Video encoding**: All frames are combined back into an MP4 video using hardware or CPU encoder
580582
5. **Audio muxing** (optional): Audio track is extracted and muxed with the video
581583

584+
**Multithreading:**
585+
586+
- Frame rendering automatically uses multiple CPU threads (up to 16)
587+
- Thread count is based on `std::thread::hardware_concurrency()`
588+
- Progress bar shows real-time completion percentage
589+
582590
### Output Quality
583591

584592
The exported video/image quality depends on:
@@ -599,6 +607,46 @@ sudo apt install ffmpeg
599607
brew install ffmpeg
600608
```
601609

610+
### GPU Acceleration
611+
612+
Video export automatically uses hardware acceleration when available. The `use_gpu` parameter controls this behavior:
613+
614+
| `use_gpu` | Behavior |
615+
| ---------------- | ---------------------------------------------------------------- |
616+
| `true` (default) | Automatically detect and use the best available hardware encoder |
617+
| `false` | Force CPU-only encoding with `libx264` |
618+
619+
**Supported Hardware Encoders:**
620+
621+
| GPU Vendor | Encoder | Platform |
622+
| ---------- | ------------------- | ------------------------ |
623+
| NVIDIA | `h264_nvenc` | Linux, Windows |
624+
| AMD/Intel | `h264_vaapi` | Linux |
625+
| Intel | `h264_qsv` | Linux, Windows |
626+
| Apple | `h264_videotoolbox` | macOS |
627+
| CPU | `libx264` | All platforms (fallback) |
628+
629+
**How it works:**
630+
631+
1. On startup, the system detects available GPUs (NVIDIA, AMD, Intel, Apple)
632+
2. FFmpeg is probed to find which hardware encoders are available
633+
3. The best encoder is automatically selected based on your hardware
634+
4. If no hardware encoder is available, falls back to CPU encoding
635+
636+
**Performance:**
637+
638+
- Hardware encoding is typically **5-20x faster** than CPU encoding
639+
- CPU encoding uses **multithreading** (auto-detects cores, up to 16 threads)
640+
- Progress is shown during both preprocessing (frame extraction) and rendering
641+
642+
**Forcing CPU encoding:**
643+
644+
```cpp
645+
// Force CPU encoding (useful for compatibility or debugging)
646+
export_media("video.mp4", "output", Type::video, Format::video,
647+
Mode::colored, 80, 128, Audio::on, 0, {}, false);
648+
```
649+
602650
Image export requires **ImageMagick** for PNG conversion:
603651
604652
```bash

docs/examples/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ project(UsingPythonic)
55
set(CMAKE_CXX_STANDARD 20)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

8+
# Find threading support (required for std::thread in video export)
9+
set(THREADS_PREFER_PTHREAD_FLAG ON)
10+
find_package(Threads REQUIRED)
11+
812
# The graph viewer target that can be exported by the installed
913
# package requires OpenGL to be found by CMake before importing
1014
# the `pythonic` config. Ensure OpenGL is available for downstream
@@ -73,7 +77,7 @@ add_executable(demoapp demo.cpp)
7377

7478
# Link libraries to your target
7579
# --> Add other libraries here if your project depends on them
76-
target_link_libraries(demoapp PRIVATE pythonic::pythonic ImageMagick::Magick++ ${FFMPEG_LIBRARIES})
80+
target_link_libraries(demoapp PRIVATE pythonic::pythonic ImageMagick::Magick++ ${FFMPEG_LIBRARIES} Threads::Threads)
7781

7882
# Add FFmpeg include directories
7983
target_include_directories(demoapp PRIVATE ${FFMPEG_INCLUDE_DIRS})

0 commit comments

Comments
 (0)