Commit 6c324ce
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 6c324ce
2 files changed
Lines changed: 22 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
17 | 22 | | |
18 | 23 | | |
19 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
196 | 199 | | |
197 | 200 | | |
198 | 201 | | |
| |||
368 | 371 | | |
369 | 372 | | |
370 | 373 | | |
371 | | - | |
372 | 374 | | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
377 | 389 | | |
378 | 390 | | |
379 | 391 | | |
| |||
0 commit comments