Skip to content

Commit d5b4ec8

Browse files
committed
Move stuff into ffmpeg.cpp
1 parent 3da223c commit d5b4ec8

File tree

3 files changed

+66
-40
lines changed

3 files changed

+66
-40
lines changed

src/torchcodec/decoders/_core/FFMPEGCommon.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,57 @@ int getNumChannels(const UniqueAVCodecContext& avCodecContext) {
7878
#endif
7979
}
8080

81+
void setChannelLayout(
82+
UniqueAVFrame& dstAVFrame,
83+
const UniqueAVFrame& srcAVFrame) {
84+
#if LIBAVFILTER_VERSION_MAJOR > 7 // FFmpeg > 4
85+
dstAVFrame->ch_layout = srcAVFrame->ch_layout;
86+
#else
87+
dstAVFrame->channel_layout = avFrame->channel_layout;
88+
#endif
89+
}
90+
91+
SwrContext* allocateSwrContext(
92+
UniqueAVCodecContext& avCodecContext,
93+
int sampleRate,
94+
AVSampleFormat sourceSampleFormat,
95+
AVSampleFormat desiredSampleFormat) {
96+
SwrContext* swrContext = nullptr;
97+
#if LIBAVFILTER_VERSION_MAJOR > 7 // FFmpeg > 4
98+
AVChannelLayout layout = avCodecContext->ch_layout;
99+
auto status = swr_alloc_set_opts2(
100+
&swrContext,
101+
&layout,
102+
desiredSampleFormat,
103+
sampleRate,
104+
&layout,
105+
sourceSampleFormat,
106+
sampleRate,
107+
0,
108+
nullptr);
109+
110+
TORCH_CHECK(
111+
status == AVSUCCESS,
112+
"Couldn't create SwrContext: ",
113+
getFFMPEGErrorStringFromErrorCode(status));
114+
#else
115+
int64_t layout = static_cast<int64_t>(avCodecContext->channel_layout);
116+
swrContext = swr_alloc_set_opts(
117+
nullptr,
118+
layout,
119+
desiredSampleFormat,
120+
sampleRate,
121+
layout,
122+
sourceSampleFormat,
123+
sampleRate,
124+
0,
125+
nullptr);
126+
#endif
127+
128+
TORCH_CHECK(swrContext != nullptr, "Couldn't create swrContext");
129+
return swrContext;
130+
}
131+
81132
AVIOBytesContext::AVIOBytesContext(
82133
const void* data,
83134
size_t dataSize,

src/torchcodec/decoders/_core/FFMPEGCommon.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ int64_t getDuration(const AVFrame* frame);
145145
int getNumChannels(const UniqueAVFrame& avFrame);
146146
int getNumChannels(const UniqueAVCodecContext& avCodecContext);
147147

148+
void setChannelLayout(
149+
UniqueAVFrame& dstAVFrame,
150+
const UniqueAVFrame& srcAVFrame);
151+
SwrContext* allocateSwrContext(
152+
UniqueAVCodecContext& avCodecContext,
153+
int sampleRate,
154+
AVSampleFormat sourceSampleFormat,
155+
AVSampleFormat desiredSampleFormat);
156+
148157
// Returns true if sws_scale can handle unaligned data.
149158
bool canSwsScaleHandleUnalignedData();
150159

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,11 +1415,7 @@ UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormat(
14151415
convertedAVFrame,
14161416
"Could not allocate frame for sample format conversion.");
14171417

1418-
#if LIBAVFILTER_VERSION_MAJOR > 7 // FFmpeg > 4
1419-
convertedAVFrame->ch_layout = avFrame->ch_layout;
1420-
#else
1421-
convertedAVFrame->channel_layout = avFrame->channel_layout;
1422-
#endif
1418+
setChannelLayout(convertedAVFrame, avFrame);
14231419
convertedAVFrame->format = static_cast<int>(desiredSampleFormat);
14241420
convertedAVFrame->sample_rate = avFrame->sample_rate;
14251421
convertedAVFrame->nb_samples = avFrame->nb_samples;
@@ -1434,7 +1430,7 @@ UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormat(
14341430
streamInfo.swrContext.get(),
14351431
convertedAVFrame->data,
14361432
convertedAVFrame->nb_samples,
1437-
(const uint8_t**)avFrame->data,
1433+
(const uint8_t**)(avFrame->data),
14381434
avFrame->nb_samples);
14391435
TORCH_CHECK(
14401436
numSampleConverted > 0,
@@ -1682,44 +1678,14 @@ void VideoDecoder::createSwrContext(
16821678
int sampleRate,
16831679
AVSampleFormat sourceSampleFormat,
16841680
AVSampleFormat desiredSampleFormat) {
1685-
SwrContext* swrContext = nullptr;
16861681

1687-
int status = AVSUCCESS;
1688-
#if LIBAVFILTER_VERSION_MAJOR > 7 // FFmpeg > 4
1689-
AVChannelLayout layout = streamInfo.codecContext->ch_layout;
1690-
status = swr_alloc_set_opts2(
1691-
&swrContext,
1692-
&layout,
1693-
desiredSampleFormat,
1682+
auto swrContext = allocateSwrContext(
1683+
streamInfo.codecContext,
16941684
sampleRate,
1695-
&layout,
16961685
sourceSampleFormat,
1697-
sampleRate,
1698-
0,
1699-
nullptr);
1700-
1701-
TORCH_CHECK(
1702-
status == AVSUCCESS,
1703-
"Couldn't create SwrContext: ",
1704-
getFFMPEGErrorStringFromErrorCode(status));
1705-
#else
1706-
int64_t layout =
1707-
static_cast<int64_t>(streamInfo.codecContext->channel_layout);
1708-
swrContext = swr_alloc_set_opts(
1709-
nullptr,
1710-
layout,
1711-
desiredSampleFormat,
1712-
sampleRate,
1713-
layout,
1714-
sourceSampleFormat,
1715-
sampleRate,
1716-
0,
1717-
nullptr);
1718-
#endif
1719-
1720-
TORCH_CHECK(swrContext != nullptr, "Couldn't create swrContext");
1686+
desiredSampleFormat);
17211687

1722-
status = swr_init(swrContext);
1688+
auto status = swr_init(swrContext);
17231689
TORCH_CHECK(
17241690
status == AVSUCCESS,
17251691
"Couldn't initialize SwrContext: ",

0 commit comments

Comments
 (0)