Skip to content

Commit 254529f

Browse files
committed
Fix compilation on FFmpeg7?
1 parent 42c6373 commit 254529f

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

src/torchcodec/_core/AVIOBytesContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ AVIOToTensorContext::AVIOToTensorContext()
7373
}
7474

7575
// The signature of this function is defined by FFMPEG.
76-
int AVIOToTensorContext::write(void* opaque, uint8_t* buf, int buf_size) {
76+
int AVIOToTensorContext::write(void* opaque, const uint8_t* buf, int buf_size) {
7777
auto dataContext = static_cast<DataContext*>(opaque);
7878

7979
if (dataContext->current + buf_size > dataContext->outputTensor.numel()) {

src/torchcodec/_core/AVIOBytesContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class AVIOToTensorContext : public AVIOContextHolder {
4444

4545
static const int INITIAL_TENSOR_SIZE = 10'000'000; // 10MB
4646
static const int MAX_TENSOR_SIZE = 320'000'000; // 320 MB
47-
static int write(void* opaque, uint8_t* buf, int buf_size);
47+
static int write(void* opaque, const uint8_t* buf, int buf_size);
4848
// We need to expose seek() for some formats like mp3.
4949
static int64_t seek(void* opaque, int64_t offset, int whence);
5050

src/torchcodec/_core/AVIOContextHolder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void AVIOContextHolder::createAVIOContext(
2727
(seek != nullptr) && ((write != nullptr) ^ (read != nullptr)),
2828
"seek method must be defined, and either write or read must be defined. "
2929
"But not both!")
30-
avioContext_.reset(avio_alloc_context(
30+
avioContext_.reset(AVIOAllocContext(
3131
buffer,
3232
bufferSize,
3333
/*write_flag=*/write != nullptr,

src/torchcodec/_core/AVIOContextHolder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AVIOContextHolder {
4646

4747
// These signatures are defined by FFmpeg.
4848
using AVIOReadFunction = int (*)(void*, uint8_t*, int);
49-
using AVIOWriteFunction = int (*)(void*, uint8_t*, int);
49+
using AVIOWriteFunction = int (*)(void*, const uint8_t*, int);
5050
using AVIOSeekFunction = int64_t (*)(void*, int64_t, int);
5151

5252
// Deriving classes should call this function in their constructor.

src/torchcodec/_core/FFMPEGCommon.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,27 @@ void setFFmpegLogLevel() {
261261
av_log_set_level(logLevel);
262262
}
263263

264+
AVIOContext* AVIOAllocContext(
265+
uint8_t* buffer,
266+
int buffer_size,
267+
int write_flag,
268+
void* opaque,
269+
int (*read_packet)(void* opaque, uint8_t* buf, int buf_size),
270+
int (*write_packet)(void* opaque, const uint8_t* buf, int buf_size),
271+
int64_t (*seek)(void* opaque, int64_t offset, int whence)) {
272+
return avio_alloc_context(
273+
buffer,
274+
buffer_size,
275+
write_flag,
276+
opaque,
277+
read_packet,
278+
// The buf parameter of the write function is not const before FFmpeg 7.
279+
#if LIBAVFILTER_VERSION_MAJOR >= 10 // FFmpeg >= 7
280+
write_packet,
281+
#else
282+
reinterpret_cast<int (*)(void*, uint8_t*, int)>(write_packet),
283+
#endif
284+
seek);
285+
}
286+
264287
} // namespace facebook::torchcodec

src/torchcodec/_core/FFMPEGCommon.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,13 @@ bool canSwsScaleHandleUnalignedData();
177177

178178
void setFFmpegLogLevel();
179179

180+
AVIOContext* AVIOAllocContext(
181+
uint8_t* buffer,
182+
int buffer_size,
183+
int write_flag,
184+
void* opaque,
185+
int (*read_packet)(void* opaque, uint8_t* buf, int buf_size),
186+
int (*write_packet)(void* opaque, const uint8_t* buf, int buf_size),
187+
int64_t (*seek)(void* opaque, int64_t offset, int whence));
188+
180189
} // namespace facebook::torchcodec

0 commit comments

Comments
 (0)