1
From: Martin Storsjö <email@unknown>
2
Subject: fix the ffmpeg 'buffer too small' bug.
4
Found in the marillat lame package.
7
diff --git a/libmp3lame/lame.c b/libmp3lame/lame.c
8
index 5f93307..b1c89f1 100644
9
--- a/libmp3lame/lame.c
10
+++ b/libmp3lame/lame.c
11
@@ -1602,6 +1602,13 @@ lame_encode_buffer_sample_t(lame_global_flags * gfp,
12
/* update mfbuf[] counters */
13
gfc->mf_size += n_out;
14
assert(gfc->mf_size <= MFSIZE);
16
+ /* lame_encode_flush may have set gfc->mf_sample_to_encode to 0
17
+ * so we have to reinitialize it here when that happened.
19
+ if (gfc->mf_samples_to_encode < 1) {
20
+ gfc->mf_samples_to_encode = ENCDELAY + POSTDELAY;
22
gfc->mf_samples_to_encode += n_out;
25
@@ -1931,6 +1938,10 @@ lame_encode_flush(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buff
27
int samples_to_encode = gfc->mf_samples_to_encode;
29
+ /* Was flush already called? */
30
+ if (gfc->mf_samples_to_encode < 1) {
33
memset(buffer, 0, sizeof(buffer));
36
@@ -1942,7 +1953,9 @@ lame_encode_flush(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buff
37
end_padding += pad_out_samples;
39
frames_left = (samples_to_encode + pad_out_samples) / gfp->framesize;
40
- while (frames_left > 0) {
42
+ /* send in a frame of 0 padding until all internal sample buffers are flushed */
43
+ while (frames_left > 0 && imp3 >= 0) {
44
int frame_num = gfp->frameNum;
46
mp3buffer_size_remaining = mp3buffer_size - mp3count;
47
@@ -1951,22 +1964,23 @@ lame_encode_flush(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buff
48
if (mp3buffer_size == 0)
49
mp3buffer_size_remaining = 0;
51
- /* send in a frame of 0 padding until all internal sample buffers
54
imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], 32,
55
mp3buffer, mp3buffer_size_remaining);
57
- if (frame_num != gfp->frameNum) {
61
- /* some type of fatal error */
66
+ frames_left -= (frame_num != gfp->frameNum) ? 1 : 0;
68
+ /* Set gfc->mf_samples_to_encode to 0, so we may detect
69
+ * and break loops calling it more than once in a row.
71
+ gfc->mf_samples_to_encode = 0;
74
+ /* some type of fatal error */
78
mp3buffer_size_remaining = mp3buffer_size - mp3count;
79
/* if user specifed buffer size = 0, dont check size */
80
if (mp3buffer_size == 0)