Commit 7ac99aa
Fix memory growth when remuxing with add_stream_from_template
start_encoding() was calling avcodec_open2() on codec contexts created
by add_stream_from_template(), fully initializing the codec (e.g.
libx264 allocates x264_t, thread pools, reference frames) even when
the stream is only used for remuxing and never calls encode()/decode().
After freeing, the C heap retains this memory, causing RSS to grow with
each output segment.
Add a _template_initialized flag to CodecContext, set by
add_stream_from_template(). In start_encoding(), contexts with this
flag skip avcodec_open2() — the codec opens lazily via encode() or
decode() if actually needed.
Fixes #2135
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 1c7beae commit 7ac99aa
2 files changed
Lines changed: 12 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
| 196 | + | |
196 | 197 | | |
197 | 198 | | |
198 | 199 | | |
| |||
368 | 369 | | |
369 | 370 | | |
370 | 371 | | |
371 | | - | |
372 | 372 | | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
377 | 380 | | |
378 | 381 | | |
379 | 382 | | |
| |||
0 commit comments