Skip to content

Commit 20c7d21

Browse files
committed
audio fixes?
1 parent 9aff387 commit 20c7d21

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/audio_mixer.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ geode::Result<std::vector<float>> readAudioFile(const char *filename, int target
3838
if (!codecContext)
3939
return geode::Err("Failed to allocate codec context");
4040

41-
if (ret = avcodec_parameters_to_context(codecContext, codecParams); ret < 0)
42-
return geode::Err("Failed to copy codec parameters to codec context: " + ffmpeg::utils::getErrorString(ret));
43-
4441
if (ret = avcodec_open2(codecContext, codec, nullptr); ret < 0)
4542
return geode::Err("Failed to open codec: " + ffmpeg::utils::getErrorString(ret));
4643

44+
if (ret = avcodec_parameters_to_context(codecContext, codecParams); ret < 0)
45+
return geode::Err("Failed to copy codec parameters to codec context: " + ffmpeg::utils::getErrorString(ret));
46+
4747
*outCodecParams = *codecParams;
4848

4949
AVFrame *frame = av_frame_alloc();
@@ -220,10 +220,9 @@ BEGIN_FFMPEG_NAMESPACE_V
220220
outputAudioStream->codecpar->codec_tag = 0;
221221
outputAudioStream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
222222
outputAudioStream->codecpar->sample_rate = sampleRate;
223-
outputAudioStream->codecpar->ch_layout = AV_CHANNEL_LAYOUT_STEREO;
223+
av_channel_layout_default(&outputAudioStream->codecpar->ch_layout, channels);
224224
outputAudioStream->codecpar->codec_id = AV_CODEC_ID_AAC;
225225
outputAudioStream->codecpar->bit_rate = 128000;
226-
outputAudioStream->codecpar->bits_per_coded_sample = 16;
227226
outputAudioStream->codecpar->format = AVSampleFormat::AV_SAMPLE_FMT_FLTP;
228227
outputAudioStream->codecpar->frame_size = frameSize;
229228

@@ -240,7 +239,7 @@ BEGIN_FFMPEG_NAMESPACE_V
240239

241240
audio_codec_context_encoder->sample_rate = sampleRate;
242241
audio_codec_context_encoder->ch_layout = AV_CHANNEL_LAYOUT_STEREO;
243-
audio_codec_context_encoder->sample_fmt = AV_SAMPLE_FMT_FLTP;
242+
audio_codec_context_encoder->sample_fmt = audioCodec->sample_fmts ? audioCodec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
244243
audio_codec_context_encoder->time_base = AVRational{1, static_cast<int>(sampleRate)};
245244

246245
ret = avcodec_open2(audio_codec_context_encoder, audioCodec, nullptr);
@@ -303,14 +302,13 @@ BEGIN_FFMPEG_NAMESPACE_V
303302
return geode::Err("Could not send audio frame to encoder: " + utils::getErrorString(ret));
304303

305304
AVPacket* audioPacket = av_packet_alloc();
306-
audioPacket->data = nullptr;
307-
audioPacket->size = 0;
305+
if (!audioPacket) return geode::Err("Failed to allocate audio packet.");
308306

309307
while (true) {
310308
int ret = avcodec_receive_packet(audio_codec_context_encoder, audioPacket);
311309
if (ret == 0) {
312310
av_packet_rescale_ts(audioPacket, audio_codec_context_encoder->time_base, outputAudioStream->time_base);
313-
audioPacket->stream_index = 1;
311+
audioPacket->stream_index = outputAudioStream->index;
314312
av_interleaved_write_frame(outputFormatContext, audioPacket);
315313
av_packet_unref(audioPacket);
316314
} else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)

0 commit comments

Comments
 (0)