Make fill_buffer a public API and move the impl to C++ (#2954)
Summary:
Currently, when iterating media data with StreamReader, using the for-loop is the only way with public API.
This does not support usecases like "Fetch one chunk after seek" well.
```python
s = StreamReader
s.add_audio_stream(...)
s.seek(10)
chunk = None
for chunk, in s.stream():
break
```
This commit make the `fill_buffer` used in iterative method public API so that one acn do
```python
s.seek(10)
s.fill_buffer()
chunk, = s.pop_chunks()
```
---
Also this commit moves the implementation to C++ so that it reduces the number of FFI boundary crossing.
This improves the performance when the iteration is longer.
AVI (generated with `ffmpeg -hide_banner -f lavfi -t ${duration} -i testsrc "${file}.avi"`)
| Video Duration [sec] | Original [msec] | Fill Buffer C++ | One Go (reference) |
|----------------------|----------|-----------------|--------|
| 1 | 18 | 18.4 | 16.6 |
| 5 | 44 | 42.6 | 35.1 |
| 10 | 75.3 | 74.4 | 60.9 |
| 30 | 200 | 195 | 158 |
| 60 | 423 | 382 | 343 |
MP4 (generated with `ffmpeg -hide_banner -f lavfi -t ${duration} -i testsrc "${file}.mp4"`)
| Video Duration [sec] | Original [msec] | Fill Buffer C++ | One Go |
|----------------------|-----------------|-----------------|--------|
| 1 | 18.7 | 18.1 | 10.3 |
| 5 | 42.2 | 40.6 | 25.2 |
| 10 | 73.9 | 71.8 | 43.6 |
| 30 | 202 | 194 | 116 |
| 60 | 396 | 386 | 227 |
* Original (Python implementation)
```python
r = StreamReader(src)
r.add_video_stream(1, decoder_option={"threads": "1"})
for chunk, in r.stream():
pass
```
* This (C++)
```python
r = StreamReader(src)
r.add_video_stream(1, decoder_option={"threads": "1"})
for chunk, in r.stream():
pass
```
* Using `process_all_packets` (process all in one go)
```python
r = StreamReader(src)
r.add_video_stream(1, decoder_option={"threads": "1"})
r.process_all_packets()
```
Pull Request resolved: https://github.com/pytorch/audio/pull/2954
Reviewed By: carolineechen
Differential Revision: D42349446
Pulled By: mthrok
fbshipit-source-id: 9e4e37923e46299c3f43f4ad17a2a2b938b2b197
Showing
Please register or sign in to comment