@@ -2108,19 +2108,21 @@ Encoder::Encoder(int sampleRate, std::string_view fileName)
21082108 TORCH_CHECK (avCodecContext != nullptr , " Couldn't allocate codec context." );
21092109 avCodecContext_.reset (avCodecContext);
21102110
2111- // I think this will use the default. TODO Should let user choose for
2112- // compressed formats like mp3.
2111+ // This will use the default bit rate
2112+ // TODO-ENCODING Should let user choose for compressed formats like mp3.
21132113 avCodecContext_->bit_rate = 0 ;
21142114
2115- // TODO A given encoder only supports a finite set of output sample rates.
2116- // FFmpeg raises informative error message. Are we happy with that, or do we
2117- // run our own checks by checking against avCodec->supported_samplerates?
2115+ // FFmpeg will raise a reasonably informative error if the desired sample rate
2116+ // isn't supported by the encoder.
21182117 avCodecContext_->sample_rate = sampleRate_;
21192118
21202119 // Note: This is the format of the **input** waveform. This doesn't determine
2121- // the output. TODO check contiguity of the input wf to ensure that it is
2122- // indeed planar.
2123- // TODO What if the encoder doesn't support FLTP? Like flac?
2120+ // the output.
2121+ // TODO-ENCODING check contiguity of the input wf to ensure that it is indeed
2122+ // planar.
2123+ // TODO-ENCODING If the encoder doesn't support FLTP (like flac), FFmpeg will
2124+ // raise. We need to handle this, probably converting the format with
2125+ // libswresample.
21242126 avCodecContext_->sample_fmt = AV_SAMPLE_FMT_FLTP;
21252127
21262128 AVChannelLayout channel_layout;
@@ -2177,8 +2179,8 @@ void Encoder::encode(const torch::Tensor& wf) {
21772179 auto numBytesPerChannel = wf.sizes ()[1 ] * numBytesPerSample;
21782180
21792181 TORCH_CHECK (
2180- // TODO is this even true / needed? We can probably support more with
2181- // non-planar data?
2182+ // TODO-ENCODING is this even true / needed? We can probably support more
2183+ // with non-planar data?
21822184 numChannels <= AV_NUM_DATA_POINTERS,
21832185 " Trying to encode " ,
21842186 numChannels,
@@ -2208,14 +2210,14 @@ void Encoder::encode(const torch::Tensor& wf) {
22082210 avFrame->data [ch], pWf + ch * numBytesPerChannel, numBytesToEncode);
22092211 }
22102212 pWf += numBytesToEncode;
2211- encode_inner_loop (autoAVPacket, avFrame. get () );
2213+ encode_inner_loop (autoAVPacket, avFrame);
22122214
22132215 avFrame->pts += avFrame->nb_samples ;
22142216 numEncodedSamples += numSamplesToEncode;
22152217 }
22162218 TORCH_CHECK (numEncodedSamples == numSamples, " Hmmmmmm something went wrong." );
22172219
2218- encode_inner_loop (autoAVPacket, nullptr ); // flush
2220+ encode_inner_loop (autoAVPacket, UniqueAVFrame ( nullptr ) ); // flush
22192221
22202222 ffmpegRet = av_write_trailer (avFormatContext_.get ());
22212223 TORCH_CHECK (
@@ -2224,8 +2226,10 @@ void Encoder::encode(const torch::Tensor& wf) {
22242226 getFFMPEGErrorStringFromErrorCode (ffmpegRet));
22252227}
22262228
2227- void Encoder::encode_inner_loop (AutoAVPacket& autoAVPacket, AVFrame* avFrame) {
2228- auto ffmpegRet = avcodec_send_frame (avCodecContext_.get (), avFrame);
2229+ void Encoder::encode_inner_loop (
2230+ AutoAVPacket& autoAVPacket,
2231+ const UniqueAVFrame& avFrame) {
2232+ auto ffmpegRet = avcodec_send_frame (avCodecContext_.get (), avFrame.get ());
22292233 TORCH_CHECK (
22302234 ffmpegRet == AVSUCCESS,
22312235 " Error while sending frame: " ,
@@ -2235,7 +2239,7 @@ void Encoder::encode_inner_loop(AutoAVPacket& autoAVPacket, AVFrame* avFrame) {
22352239 ReferenceAVPacket packet (autoAVPacket);
22362240 ffmpegRet = avcodec_receive_packet (avCodecContext_.get (), packet.get ());
22372241 if (ffmpegRet == AVERROR (EAGAIN) || ffmpegRet == AVERROR_EOF) {
2238- // TODO this is from TorchAudio, probably needed, but not sure.
2242+ // TODO-ENCODING this is from TorchAudio, probably needed, but not sure.
22392243 // if (ffmpegRet == AVERROR_EOF) {
22402244 // ffmpegRet = av_interleaved_write_frame(avFormatContext_.get(),
22412245 // nullptr); TORCH_CHECK(
@@ -2250,7 +2254,7 @@ void Encoder::encode_inner_loop(AutoAVPacket& autoAVPacket, AVFrame* avFrame) {
22502254 " Error receiving packet: " ,
22512255 getFFMPEGErrorStringFromErrorCode (ffmpegRet));
22522256
2253- // TODO why are these 2 lines needed??
2257+ // TODO-ENCODING why are these 2 lines needed??
22542258 av_packet_rescale_ts (
22552259 packet.get (), avCodecContext_->time_base , avStream_->time_base );
22562260 packet->stream_index = avStream_->index ;
0 commit comments