@@ -38,12 +38,12 @@ geode::Result<std::vector<float>> readAudioFile(const char *filename, int target
38
38
if (!codecContext)
39
39
return geode::Err (" Failed to allocate codec context" );
40
40
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
-
44
41
if (ret = avcodec_open2 (codecContext, codec, nullptr ); ret < 0 )
45
42
return geode::Err (" Failed to open codec: " + ffmpeg::utils::getErrorString (ret));
46
43
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
+
47
47
*outCodecParams = *codecParams;
48
48
49
49
AVFrame *frame = av_frame_alloc ();
@@ -220,10 +220,9 @@ BEGIN_FFMPEG_NAMESPACE_V
220
220
outputAudioStream->codecpar ->codec_tag = 0 ;
221
221
outputAudioStream->codecpar ->codec_type = AVMEDIA_TYPE_AUDIO;
222
222
outputAudioStream->codecpar ->sample_rate = sampleRate;
223
- outputAudioStream->codecpar ->ch_layout = AV_CHANNEL_LAYOUT_STEREO ;
223
+ av_channel_layout_default (& outputAudioStream->codecpar ->ch_layout , channels) ;
224
224
outputAudioStream->codecpar ->codec_id = AV_CODEC_ID_AAC;
225
225
outputAudioStream->codecpar ->bit_rate = 128000 ;
226
- outputAudioStream->codecpar ->bits_per_coded_sample = 16 ;
227
226
outputAudioStream->codecpar ->format = AVSampleFormat::AV_SAMPLE_FMT_FLTP;
228
227
outputAudioStream->codecpar ->frame_size = frameSize;
229
228
@@ -240,7 +239,7 @@ BEGIN_FFMPEG_NAMESPACE_V
240
239
241
240
audio_codec_context_encoder->sample_rate = sampleRate;
242
241
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;
244
243
audio_codec_context_encoder->time_base = AVRational{1 , static_cast <int >(sampleRate)};
245
244
246
245
ret = avcodec_open2 (audio_codec_context_encoder, audioCodec, nullptr );
@@ -303,14 +302,13 @@ BEGIN_FFMPEG_NAMESPACE_V
303
302
return geode::Err (" Could not send audio frame to encoder: " + utils::getErrorString (ret));
304
303
305
304
AVPacket* audioPacket = av_packet_alloc ();
306
- audioPacket->data = nullptr ;
307
- audioPacket->size = 0 ;
305
+ if (!audioPacket) return geode::Err (" Failed to allocate audio packet." );
308
306
309
307
while (true ) {
310
308
int ret = avcodec_receive_packet (audio_codec_context_encoder, audioPacket);
311
309
if (ret == 0 ) {
312
310
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 ;
314
312
av_interleaved_write_frame (outputFormatContext, audioPacket);
315
313
av_packet_unref (audioPacket);
316
314
} else if (ret == AVERROR (EAGAIN) || ret == AVERROR_EOF)
0 commit comments