@@ -100,7 +100,6 @@ AudioEncoder::AudioEncoder(
100100 // raise. We need to handle this, probably converting the format with
101101 // libswresample.
102102 avCodecContext_->sample_fmt = AV_SAMPLE_FMT_FLTP;
103- // avCodecContext_->sample_fmt = AV_SAMPLE_FMT_S16;
104103
105104 int numChannels = static_cast <int >(wf_.sizes ()[0 ]);
106105 TORCH_CHECK (
@@ -121,6 +120,12 @@ AudioEncoder::AudioEncoder(
121120 " avcodec_open2 failed: " ,
122121 getFFMPEGErrorStringFromErrorCode (status));
123122
123+ TORCH_CHECK (
124+ avCodecContext_->frame_size > 0 ,
125+ " frame_size is " ,
126+ avCodecContext_->frame_size ,
127+ " . Cannot encode. This should probably never happen?" );
128+
124129 // We're allocating the stream here. Streams are meant to be freed by
125130 // avformat_free_context(avFormatContext), which we call in the
126131 // avFormatContext_'s destructor.
@@ -138,10 +143,7 @@ AudioEncoder::AudioEncoder(
138143void AudioEncoder::encode () {
139144 UniqueAVFrame avFrame (av_frame_alloc ());
140145 TORCH_CHECK (avFrame != nullptr , " Couldn't allocate AVFrame." );
141- // Default to 256 like in torchaudio
142- int numSamplesAllocatedPerFrame =
143- avCodecContext_->frame_size > 0 ? avCodecContext_->frame_size : 256 ;
144- avFrame->nb_samples = numSamplesAllocatedPerFrame;
146+ avFrame->nb_samples = avCodecContext_->frame_size ;
145147 avFrame->format = avCodecContext_->sample_fmt ;
146148 avFrame->sample_rate = avCodecContext_->sample_rate ;
147149 avFrame->pts = 0 ;
@@ -158,6 +160,7 @@ void AudioEncoder::encode() {
158160 uint8_t * pwf = static_cast <uint8_t *>(wf_.data_ptr ());
159161 int numSamples = static_cast <int >(wf_.sizes ()[1 ]); // per channel
160162 int numEncodedSamples = 0 ; // per channel
163+ int numSamplesPerFrame = avCodecContext_->frame_size ; // per channel
161164 int numBytesPerSample = static_cast <int >(wf_.element_size ());
162165 int numBytesPerChannel = numSamples * numBytesPerSample;
163166
@@ -175,7 +178,7 @@ void AudioEncoder::encode() {
175178 getFFMPEGErrorStringFromErrorCode (status));
176179
177180 int numSamplesToEncode =
178- std::min (numSamplesAllocatedPerFrame , numSamples - numEncodedSamples);
181+ std::min (numSamplesPerFrame , numSamples - numEncodedSamples);
179182 int numBytesToEncode = numSamplesToEncode * numBytesPerSample;
180183
181184 for (int ch = 0 ; ch < wf_.sizes ()[0 ]; ch++) {
0 commit comments