Skip to content

Commit 7fa5023

Browse files
committed
Fix HLS transcoding audio with non-MP3 input
The buffer splitting I had in place was slightly off, with the result of horrible sounding audio when transcoding from AC3. Feeding entire single frames of audio to the encoder is the trick, and finally, we are there. Fixes #10488
1 parent 495ef16 commit 7fa5023

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

mythtv/programs/mythtranscode/transcode.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,39 +133,61 @@ class AudioReencodeBuffer : public AudioOutput
133133

134134
QMutexLocker locker(&m_bufferMutex);
135135
AudioBuffer *ab;
136+
unsigned char *buf = (unsigned char *)buffer;
137+
int savedlen = 0;
138+
int firstlen = 0;
136139

137140
if (m_audioFrameSize)
138141
{
139-
int currLen = m_saveBuffer.m_buffer.size();
140-
if (currLen)
142+
savedlen = m_saveBuffer.m_buffer.size();
143+
if (savedlen)
141144
{
142145
ab = new AudioBuffer(m_saveBuffer);
143146
m_saveBuffer.clear();
147+
firstlen = m_audioFrameSize - savedlen;
144148
}
145149
else
146150
ab = new AudioBuffer;
147151

148152
int overage;
149153

150-
if ((overage = (currLen + len) % m_audioFrameSize))
154+
if ((overage = (len + savedlen) % m_audioFrameSize))
151155
{
152156
long long newtime = timecode + ((len / m_bytes_per_frame) /
153157
(m_eff_audiorate / 1000));
154158
len -= overage;
155-
m_saveBuffer.setData(&((unsigned char *)buffer)[len], overage,
156-
newtime);
159+
m_saveBuffer.setData(&buf[len], overage, newtime);
157160
}
158161
}
159162
else
160163
{
161164
ab = new AudioBuffer;
162165
}
163166

164-
m_last_audiotime = timecode + ((len / m_bytes_per_frame) /
165-
(m_eff_audiorate / 1000));
166-
ab->appendData((unsigned char *)buffer, len, m_last_audiotime);
167+
int bufflen = (m_audioFrameSize ? m_audioFrameSize : len);
168+
int index = 0;
167169

168-
m_bufferList.append(ab);
170+
do
171+
{
172+
if (!len)
173+
break;
174+
175+
int blocklen = (firstlen ? firstlen : bufflen);
176+
177+
m_last_audiotime = timecode + ((blocklen / m_bytes_per_frame) /
178+
(m_eff_audiorate / 1000));
179+
ab->appendData(&buf[index], blocklen, m_last_audiotime);
180+
m_bufferList.append(ab);
181+
182+
timecode = m_last_audiotime;
183+
index += blocklen;
184+
firstlen = 0;
185+
186+
ab = new AudioBuffer;
187+
} while (index < len);
188+
189+
// The last buffer is actually unused
190+
delete ab;
169191

170192
return true;
171193
}

0 commit comments

Comments
 (0)