docs: add Apple native video performance analysis
性能對比測試: - Rust (ffmpeg): 0.81s - Swift (Apple Native): 6.18s - 差異: ffmpeg 7.65x 更快 原因分析: 1. ffmpeg 高度優化 (C + SIMD) 2. Swift 原型開銷大: - CVPixelBuffer 創建 - Core Image 處理 - CGImage 創建 - CPU-GPU 傳輸 優化方向: 1. Phase 1: 快速優化 (3x, 1天) - 減少幀處理頻率 - 直接訪問像素 - 跳過 Core Image 2. Phase 2: Vision + ANE (5x, 2-3天) - 使用 Vision Framework - ANE 加速 3. Phase 3: Metal GPU (10x, 1周) - Metal shader - GPU 並行 建議: - 短期: 繼續使用 ffmpeg (性能優) - 中期: 優化 Apple Native - 長期: 根據需求選擇 結論: 可行但需優化
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
---
|
||||
title: Apple Native Video Performance Analysis
|
||||
version: 1.0
|
||||
date: 2026-09-28
|
||||
author: OpenCode
|
||||
status: completed
|
||||
---
|
||||
|
||||
## 性能對比測試結果
|
||||
|
||||
### 測試環境
|
||||
|
||||
**視頻**: 020.mp4 (8.8s, 60 FPS, 529 frames, 1920x1080)
|
||||
|
||||
---
|
||||
|
||||
### 性能對比
|
||||
|
||||
| 方法 | 執行時間 | 檢測結果 | 相對速度 |
|
||||
|------|----------|----------|----------|
|
||||
| **Rust (ffmpeg)** | **0.81s** | 1 scene | **基準 (1x)** |
|
||||
| Swift (Apple Native) | 6.18s | 1 scene | 7.65x 更慢 |
|
||||
|
||||
---
|
||||
|
||||
## 性能差異分析
|
||||
|
||||
### Rust (ffmpeg) 優勢
|
||||
|
||||
1. **高度優化的 C 代碼**
|
||||
- ffmpeg 已經過多年優化
|
||||
- 使用 SIMD 指令集
|
||||
- 底層 C 實現效率高
|
||||
|
||||
2. **select filter 效率**
|
||||
- 專門設計用於場景檢測
|
||||
- 單次遍歷視頻
|
||||
- 內置閾值判斷
|
||||
|
||||
3. **進程開銷**
|
||||
- 單個進程
|
||||
- 直接讀取視頻流
|
||||
|
||||
---
|
||||
|
||||
### Swift (Apple Native) 瓶頸
|
||||
|
||||
1. **幀提取開銷**
|
||||
```swift
|
||||
// 每次都需要:
|
||||
let sampleBuffer = output.copyNextSampleBuffer()
|
||||
let currentBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
|
||||
```
|
||||
- CVPixelBuffer 創建開銷
|
||||
- 內存分配
|
||||
|
||||
2. **Core Image 處理**
|
||||
```swift
|
||||
// 每幀都調用 Core Image
|
||||
let ciImage1 = CIImage(cvPixelBuffer: frame1)
|
||||
let ciImage2 = CIImage(cvPixelBuffer: frame2)
|
||||
```
|
||||
- CIImage 創建開銷
|
||||
- GPU 上傳/下載
|
||||
|
||||
3. **像素採樣**
|
||||
```swift
|
||||
// 對每幀進行 100 次採樣
|
||||
for i in 0..<sampleCount {
|
||||
if let cgImage = context.createCGImage(...) {
|
||||
// 創建 CGImage 開銷
|
||||
}
|
||||
}
|
||||
```
|
||||
- CGImage 創建開銷
|
||||
- CPU-GPU 數據傳輸
|
||||
|
||||
4. **降分辨率影響**
|
||||
```swift
|
||||
// 降低到 160x90
|
||||
kCVPixelBufferWidthKey as String: 160,
|
||||
kCVPixelBufferHeightKey as String: 90
|
||||
```
|
||||
- 需要縮放開銷
|
||||
- 信息損失可能影響準確性
|
||||
|
||||
---
|
||||
|
||||
## 優化方向
|
||||
|
||||
### 1. 使用 Vision Framework (推薦)
|
||||
|
||||
**原理**: 利用 Vision 的原生圖像分析
|
||||
|
||||
```swift
|
||||
import Vision
|
||||
|
||||
let request = VNSequenceRequestHandler()
|
||||
// 使用 Vision 的幀差異檢測
|
||||
```
|
||||
|
||||
**優勢**:
|
||||
- ✅ ANE 加速
|
||||
- ✅ 優化的底層實現
|
||||
- ✅ 無需手動像素處理
|
||||
|
||||
**預期性能**: 2-3x 提升
|
||||
|
||||
---
|
||||
|
||||
### 2. 使用 Metal Shader
|
||||
|
||||
**原理**: GPU 並行計算
|
||||
|
||||
```swift
|
||||
import Metal
|
||||
|
||||
// Metal shader 計算幀差異
|
||||
kernel void computeDifference(...) {
|
||||
// GPU 並行處理所有像素
|
||||
}
|
||||
```
|
||||
|
||||
**優勢**:
|
||||
- ✅ GPU 並行
|
||||
- ✅ 無 CPU-GPU 傳輸開銷
|
||||
- ✅ 最高性能
|
||||
|
||||
**預期性能**: 5-10x 提升
|
||||
|
||||
---
|
||||
|
||||
### 3. 優化當前實現
|
||||
|
||||
**改進點**:
|
||||
|
||||
#### A. 減少幀處理頻率
|
||||
|
||||
```swift
|
||||
// 當前: 每 3 幀處理一次
|
||||
if frameNum % 3 == 0 {
|
||||
|
||||
// 改進: 每 10 幀處理一次
|
||||
if frameNum % 10 == 0 {
|
||||
```
|
||||
|
||||
**預期性能**: 3x 提升
|
||||
|
||||
---
|
||||
|
||||
#### B. 使用像素緩衝區直接訪問
|
||||
|
||||
```swift
|
||||
// 當前: 使用 Core Image
|
||||
let ciImage = CIImage(cvPixelBuffer: buffer)
|
||||
let cgImage = context.createCGImage(...)
|
||||
|
||||
// 改進: 直接訪問像素數據
|
||||
CVPixelBufferLockBaseAddress(buffer, .readOnly)
|
||||
let pixelData = CVPixelBufferGetBaseAddress(buffer)
|
||||
// 直接計算差異
|
||||
```
|
||||
|
||||
**優期性能**: 2-3x 提升
|
||||
|
||||
---
|
||||
|
||||
#### C. 跳過 Core Image 創建
|
||||
|
||||
```swift
|
||||
// 當前: 每幀創建 CIImage
|
||||
let ciImage1 = CIImage(cvPixelBuffer: frame1)
|
||||
|
||||
// 改進: 直接比較像素緩衝區
|
||||
func compareBuffers(_ b1: CVPixelBuffer, _ b2: CVPixelBuffer) -> Float {
|
||||
// 直接訪問內存
|
||||
}
|
||||
```
|
||||
|
||||
**預期性能**: 2x 提升
|
||||
|
||||
---
|
||||
|
||||
## 綜合優化方案
|
||||
|
||||
### Phase 1: 快速優化 (1 天)
|
||||
|
||||
**目標**: 3x 性能提升 (2s)
|
||||
|
||||
**措施**:
|
||||
1. 減少幀處理頻率 (10 幀一次)
|
||||
2. 直接訪問像素緩衝區
|
||||
3. 跳過 Core Image 創建
|
||||
|
||||
---
|
||||
|
||||
### Phase 2: Vision 集成 (2-3 天)
|
||||
|
||||
**目標**: 5x 性能提升 (1.2s)
|
||||
|
||||
**措施**:
|
||||
1. 使用 Vision Framework
|
||||
2. 利用 ANE 加速
|
||||
3. 優化 pipeline
|
||||
|
||||
---
|
||||
|
||||
### Phase 3: Metal 實現 (1 周)
|
||||
|
||||
**目標**: 10x 性能提升 (0.6s, 超越 ffmpeg)
|
||||
|
||||
**措施**:
|
||||
1. Metal shader 編寫
|
||||
2. GPU 並行計算
|
||||
3. 內存優化
|
||||
|
||||
---
|
||||
|
||||
## 成本效益分析
|
||||
|
||||
### ffmpeg 方案
|
||||
|
||||
**優勢**:
|
||||
- ✅ 性能優秀 (0.8s)
|
||||
- ✅ 成熟穩定
|
||||
- ✅ 跨平台
|
||||
|
||||
**劣勢**:
|
||||
- ⚠️ 外部依賴
|
||||
- ⚠️ 進程開銷
|
||||
|
||||
---
|
||||
|
||||
### Apple Native 方案
|
||||
|
||||
**優勢**:
|
||||
- ✅ 無外部依賴
|
||||
- ✅ 原生集成
|
||||
|
||||
**劣勢**:
|
||||
- ⚠️ 當前性能較差 (6.2s)
|
||||
- ⚠️ 需要優化時間
|
||||
|
||||
---
|
||||
|
||||
### 建議
|
||||
|
||||
**短期 (現在)**:
|
||||
- ✅ 繼續使用 Rust + ffmpeg
|
||||
- ✅ 性能優秀,穩定可靠
|
||||
|
||||
**中期 (1-2 周後)**:
|
||||
- ⏳ 實現 Vision + ANE 版本
|
||||
- ⏳ 達到相近性能
|
||||
|
||||
**長期 (1 月後)**:
|
||||
- ⏳ Metal 版本
|
||||
- ⏳ 可能超越 ffmpeg
|
||||
|
||||
---
|
||||
|
||||
## 測試數據
|
||||
|
||||
### 原始測試
|
||||
|
||||
```bash
|
||||
Rust (ffmpeg): 0.81s
|
||||
Swift (Apple Native): 6.18s
|
||||
Performance ratio: 7.65x
|
||||
```
|
||||
|
||||
### 預期優化後
|
||||
|
||||
| 階段 | 性能 | vs ffmpeg |
|
||||
|------|------|-----------|
|
||||
| 當前 | 6.18s | 7.65x 更慢 |
|
||||
| Phase 1 | ~2.0s | 2.5x 更慢 |
|
||||
| Phase 2 | ~1.2s | 1.5x 更慢 |
|
||||
| Phase 3 | ~0.6s | 1.3x 更快 |
|
||||
|
||||
---
|
||||
|
||||
## 結論
|
||||
|
||||
### 當前狀態
|
||||
|
||||
Apple Native 實現**技術可行**,但當前性能不如 ffmpeg。
|
||||
|
||||
### 建議策略
|
||||
|
||||
1. **保留 ffmpeg 版本** (當前主力)
|
||||
2. **逐步優化 Apple Native 版本**
|
||||
3. **根據需求選擇**:
|
||||
- 追求性能 → ffmpeg
|
||||
- 追求原生 → Apple Native (優化後)
|
||||
|
||||
---
|
||||
|
||||
**研究日期**: 2026-09-28
|
||||
**狀態**: ⚠️ **可行但需優化,建議保留 ffmpeg 版本**
|
||||
Reference in New Issue
Block a user