Merge pull request #8472 from unknownbrackets/mpeg-mono

Mpeg: Correctly handle mono audio in videos
This commit is contained in:
Henrik Rydgård 2016-01-22 09:24:53 +01:00
commit 2abf848a80
3 changed files with 25 additions and 13 deletions

View File

@ -281,7 +281,7 @@ bool MediaEngine::openContext() {
return false;
setVideoDim();
m_audioContext = new SimpleAudio(m_audioType);
m_audioContext = new SimpleAudio(m_audioType, 44100, 2);
m_isVideoEnd = false;
m_mpegheaderReadPos++;
av_seek_frame(m_pFormatCtx, m_videoStream, 0, 0);
@ -862,7 +862,13 @@ int MediaEngine::getAudioSamples(u32 bufferPtr) {
}
int outbytes = 0;
if (m_audioContext != NULL) {
if (m_audioContext != nullptr) {
if (headerCode1 == 0x24) {
// This means mono audio - tell the decoder to expect it before the first frame.
// Note that it will always send us back stereo audio.
m_audioContext->SetChannels(1);
}
if (!m_audioContext->Decode(audioFrame, frameSize, buffer, &outbytes)) {
ERROR_LOG(ME, "Audio (%s) decode failed during video playback", GetCodecName(m_audioType));
}
@ -871,17 +877,6 @@ int MediaEngine::getAudioSamples(u32 bufferPtr) {
#endif
}
if (headerCode1 == 0x24) {
// it a mono atrac3plus, convert it to stereo
s16 *outbuf = (s16*)buffer;
s16 *inbuf = (s16*)buffer;
for (int i = 0x800 - 1; i >= 0; i--) {
s16 sample = inbuf[i];
outbuf[i * 2] = sample;
outbuf[i * 2 + 1] = sample;
}
}
return 0x2000;
}

View File

@ -129,6 +129,21 @@ void SimpleAudio::SetExtraData(u8 *data, int size, int wav_bytes_per_packet) {
#endif
}
void SimpleAudio::SetChannels(int channels) {
if (channels_ == channels) {
// Do nothing, already set.
return;
}
if (codecOpen_) {
ERROR_LOG(ME, "Codec already open, cannot change channels");
} else {
channels_ = channels;
codecCtx_->channels = channels_;
codecCtx_->channel_layout = channels_ == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
}
}
SimpleAudio::~SimpleAudio() {
#ifdef USE_FFMPEG
swr_free(&swrCtx_);

View File

@ -56,6 +56,8 @@ public:
// Not save stated, only used by UI. Used for ATRAC3 (non+) files.
void SetExtraData(u8 *data, int size, int wav_bytes_per_packet);
void SetChannels(int channels);
// These two are only here because of save states.
int GetAudioType() const { return audioType; }
void SetResampleFrequency(int freq) { wanted_resample_freq = freq; }