Skip to content

Commit d60678a

Browse files
author
Chris Kirmse
committed
correctly calculate remaining_frame_samples
- sometimes the output from the SWR_CONVERT is not the exact amount we asked for - without this fix, the old code would sometimes read past the end of a buffer - valgrind complained about the old code - read https://stackoverflow.com/questions/39587839/libswresample-swr-convert-not-producing-enough-samples
1 parent 271c390 commit d60678a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/FFmpegWriter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) {
14171417
// unless "qp" was set for CQP, switch to VBR RC mode
14181418
av_opt_set(video_codec->priv_data, "rc_mode", "VBR", 0);
14191419

1420-
// In the current state (ffmpeg-4.2-4 libva-mesa-driver-19.1.5-1) to use VBR,
1420+
// In the current state (ffmpeg-4.2-4 libva-mesa-driver-19.1.5-1) to use VBR,
14211421
// one has to specify both bit_rate and maxrate, otherwise a small low quality file is generated on Intel iGPU).
14221422
video_codec->rc_max_rate = video_codec->bit_rate;
14231423
}
@@ -1569,9 +1569,6 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
15691569
total_frame_samples *= (float(info.sample_rate) / sample_rate_in_frame); // adjust for different byte sizes
15701570
total_frame_samples *= (float(info.channels) / channels_in_frame); // adjust for different # of channels
15711571

1572-
// Set remaining samples
1573-
remaining_frame_samples = total_frame_samples;
1574-
15751572
// Create output frame (and allocate arrays)
15761573
AVFrame *audio_converted = AV_ALLOCATE_FRAME();
15771574
AV_RESET_FRAME(audio_converted);
@@ -1604,6 +1601,9 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
16041601
audio_frame->linesize[0], // input plane size, in bytes (0 if unknown)
16051602
audio_frame->nb_samples); // number of input samples to convert
16061603

1604+
// Set remaining samples
1605+
remaining_frame_samples = nb_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);
1606+
16071607
// Create a new array (to hold all resampled S16 audio samples)
16081608
all_resampled_samples = (int16_t *) av_malloc(
16091609
sizeof(int16_t) * nb_samples * info.channels * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)));

0 commit comments

Comments
 (0)