Skip to content

Commit 52d1753

Browse files
committed
Write TODOs, avoid raw pointers
1 parent 3890227 commit 52d1753

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ class Encoder {
600600
void encode(const torch::Tensor& wf);
601601

602602
private:
603-
void encode_inner_loop(AutoAVPacket& autoAVPacket, AVFrame* avFrame);
603+
void encode_inner_loop(
604+
AutoAVPacket& autoAVPacket,
605+
const UniqueAVFrame& avFrame);
604606

605607
UniqueAVFormatContextForEncoding avFormatContext_;
606608
UniqueAVCodecContext avCodecContext_;

0 commit comments

Comments
 (0)