Skip to content

Commit 37eb37a

Browse files
authored
Revert "Implementing multithreaded video decoding (#3389)" (#3623)
This reverts commit 3bfdb42.
1 parent 02a1918 commit 37eb37a

File tree

7 files changed

+19
-82
lines changed

7 files changed

+19
-82
lines changed

torchvision/csrc/io/decoder/decoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ bool Decoder::openStreams(std::vector<DecoderMetadata>* metadata) {
432432
it->format,
433433
params_.loggingUuid);
434434
CHECK(stream);
435-
if (stream->openCodec(metadata, params_.numThreads) < 0) {
435+
if (stream->openCodec(metadata) < 0) {
436436
LOG(ERROR) << "uuid=" << params_.loggingUuid
437437
<< " open codec failed, stream_idx=" << i;
438438
return false;

torchvision/csrc/io/decoder/defs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ struct DecoderParameters {
194194
bool preventStaleness{true};
195195
// seek tolerated accuracy (us)
196196
double seekAccuracy{1000000.0};
197-
// Allow multithreaded decoding for numThreads > 1;
198-
// 0 numThreads=0 sets up sensible defaults
199-
int numThreads{1};
200197
// what media types should be processed, default none
201198
std::set<MediaFormat> formats;
202199

torchvision/csrc/io/decoder/stream.cpp

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "stream.h"
2-
#include <ATen/Parallel.h>
32
#include <c10/util/Logging.h>
4-
#include <stdio.h>
5-
#include <string.h>
63
#include "util.h"
74

85
namespace ffmpeg {
@@ -31,7 +28,7 @@ AVCodec* Stream::findCodec(AVCodecParameters* params) {
3128
return avcodec_find_decoder(params->codec_id);
3229
}
3330

34-
int Stream::openCodec(std::vector<DecoderMetadata>* metadata, int num_threads) {
31+
int Stream::openCodec(std::vector<DecoderMetadata>* metadata) {
3532
AVStream* steam = inputCtx_->streams[format_.stream];
3633

3734
AVCodec* codec = findCodec(steam->codecpar);
@@ -56,49 +53,6 @@ int Stream::openCodec(std::vector<DecoderMetadata>* metadata, int num_threads) {
5653
return ret;
5754
}
5855

59-
// multithreading heuristics
60-
int max_threads = at::get_num_threads();
61-
// first a safety check
62-
if (num_threads > max_threads) {
63-
num_threads = max_threads;
64-
}
65-
66-
if (num_threads > 0) {
67-
if (num_threads > 1) {
68-
codecCtx_->active_thread_type = 1;
69-
}
70-
// if user defined, respect that
71-
codecCtx_->thread_count = num_threads;
72-
73-
} else {
74-
// otherwise set sensible defaults
75-
// with the special case for the different MPEG4 codecs
76-
// that don't have threading context functions
77-
// TODO: potentially automate this using native c++ function lookups
78-
if (strcmp(codecCtx_->codec->name, "mpeg4") == 0 &&
79-
codecCtx_->codec_type == 0) {
80-
if (codecCtx_->codec_tag == 1684633208) {
81-
codecCtx_->thread_count = (8 <= max_threads) ? 8 : max_threads;
82-
codecCtx_->thread_type = 1;
83-
} else {
84-
codecCtx_->thread_count = (2 <= max_threads) ? 2 : max_threads;
85-
codecCtx_->thread_type = 2;
86-
}
87-
} else {
88-
// otherwise default to multithreading
89-
codecCtx_->thread_count = (8 <= max_threads) ? 8 : max_threads;
90-
codecCtx_->active_thread_type = 1;
91-
}
92-
}
93-
94-
// print codec type and number of threads
95-
LOG(INFO) << "Codec " << codecCtx_->codec->long_name
96-
<< " Codec id: " << codecCtx_->codec_id
97-
<< " Codec tag: " << codecCtx_->codec_tag
98-
<< " Codec type: " << codecCtx_->codec_type
99-
<< " Codec extradata: " << codecCtx_->extradata
100-
<< " Number of threads: " << at::get_num_threads();
101-
10256
// after avcodec_open2, value of codecCtx_->time_base is NOT meaningful
10357
if ((ret = avcodec_open2(codecCtx_, codec, nullptr)) < 0) {
10458
LOG(ERROR) << "LoggingUuid #" << loggingUuid_

torchvision/csrc/io/decoder/stream.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class Stream {
2020
virtual ~Stream();
2121

2222
// returns 0 - on success or negative error
23-
// num_threads sets up the codec context for multithreading if needed
24-
int openCodec(std::vector<DecoderMetadata>* metadata, int num_threads = 1);
23+
int openCodec(std::vector<DecoderMetadata>* metadata);
2524
// returns 1 - if packet got consumed, 0 - if it's not, and < 0 on error
2625
int decodePacket(
2726
const AVPacket* packet,

torchvision/csrc/io/video/video.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,15 @@ void Video::_getDecoderParams(
9797
double videoStartS,
9898
int64_t getPtsOnly,
9999
std::string stream,
100-
long stream_id,
101-
bool all_streams,
102-
int64_t num_threads,
103-
double seekFrameMarginUs) {
100+
long stream_id = -1,
101+
bool all_streams = false,
102+
double seekFrameMarginUs = 10) {
104103
int64_t videoStartUs = int64_t(videoStartS * 1e6);
105104

106105
params.timeoutMs = decoderTimeoutMs;
107106
params.startOffset = videoStartUs;
108107
params.seekAccuracy = seekFrameMarginUs;
109108
params.headerOnly = false;
110-
params.numThreads = num_threads;
111109

112110
params.preventStaleness = false; // not sure what this is about
113111

@@ -154,9 +152,7 @@ void Video::_getDecoderParams(
154152

155153
} // _get decoder params
156154

157-
Video::Video(std::string videoPath, std::string stream, int64_t numThreads) {
158-
// set number of threads global
159-
numThreads_ = numThreads;
155+
Video::Video(std::string videoPath, std::string stream) {
160156
// parse stream information
161157
current_stream = _parseStream(stream);
162158
// note that in the initial call we want to get all streams
@@ -165,8 +161,7 @@ Video::Video(std::string videoPath, std::string stream, int64_t numThreads) {
165161
0, // headerOnly
166162
std::get<0>(current_stream), // stream info - remove that
167163
long(-1), // stream_id parsed from info above change to -2
168-
true, // read all streams
169-
numThreads_ // global number of Threads for decoding
164+
true // read all streams
170165
);
171166

172167
std::string logMessage, logType;
@@ -230,7 +225,7 @@ Video::Video(std::string videoPath, std::string stream, int64_t numThreads) {
230225
}
231226
} // video
232227

233-
bool Video::setCurrentStream(std::string stream) {
228+
bool Video::setCurrentStream(std::string stream = "video") {
234229
if ((!stream.empty()) && (_parseStream(stream) != current_stream)) {
235230
current_stream = _parseStream(stream);
236231
}
@@ -246,8 +241,7 @@ bool Video::setCurrentStream(std::string stream) {
246241
std::get<0>(current_stream), // stream
247242
long(std::get<1>(
248243
current_stream)), // stream_id parsed from info above change to -2
249-
false, // read all streams
250-
numThreads_ // global number of threads
244+
false // read all streams
251245
);
252246

253247
// calback and metadata defined in Video.h
@@ -271,8 +265,7 @@ void Video::Seek(double ts) {
271265
std::get<0>(current_stream), // stream
272266
long(std::get<1>(
273267
current_stream)), // stream_id parsed from info above change to -2
274-
false, // read all streams
275-
numThreads_ // global num threads
268+
false // read all streams
276269
);
277270

278271
// calback and metadata defined in Video.h
@@ -338,7 +331,7 @@ std::tuple<torch::Tensor, double> Video::Next() {
338331

339332
static auto registerVideo =
340333
torch::class_<Video>("torchvision", "Video")
341-
.def(torch::init<std::string, std::string, int64_t>())
334+
.def(torch::init<std::string, std::string>())
342335
.def("get_current_stream", &Video::getCurrentStream)
343336
.def("set_current_stream", &Video::setCurrentStream)
344337
.def("get_metadata", &Video::getStreamMetadata)

torchvision/csrc/io/video/video.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ struct Video : torch::CustomClassHolder {
1616
// global video metadata
1717
c10::Dict<std::string, c10::Dict<std::string, std::vector<double>>>
1818
streamsMetadata;
19-
int64_t numThreads_{0};
2019

2120
public:
22-
Video(std::string videoPath, std::string stream, int64_t numThreads);
21+
Video(std::string videoPath, std::string stream);
2322
std::tuple<std::string, int64_t> getCurrentStream() const;
2423
c10::Dict<std::string, c10::Dict<std::string, std::vector<double>>>
2524
getStreamMetadata() const;
2625
void Seek(double ts);
27-
bool setCurrentStream(std::string stream = "video");
26+
bool setCurrentStream(std::string stream);
2827
std::tuple<torch::Tensor, double> Next();
2928

3029
private:
@@ -38,10 +37,9 @@ struct Video : torch::CustomClassHolder {
3837
double videoStartS,
3938
int64_t getPtsOnly,
4039
std::string stream,
41-
long stream_id = -1,
42-
bool all_streams = false,
43-
int64_t num_threads = 0,
44-
double seekFrameMarginUs = 10); // this needs to be improved
40+
long stream_id,
41+
bool all_streams,
42+
double seekFrameMarginUs); // this needs to be improved
4543

4644
std::map<std::string, std::vector<double>> streamTimeBase; // not used
4745

torchvision/io/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,17 @@ class VideoReader:
9696
stream (string, optional): descriptor of the required stream, followed by the stream id,
9797
in the format ``{stream_type}:{stream_id}``. Defaults to ``"video:0"``.
9898
Currently available options include ``['video', 'audio']``
99-
100-
num_threads (int, optional): number of threads used by the codec to decode video.
101-
Default value (0) enables multithreading with codec-dependent heuristic. The performance
102-
will depend on the version of FFMPEG codecs supported.
10399
"""
104100

105-
def __init__(self, path, stream="video", num_threads=0):
101+
def __init__(self, path, stream="video"):
106102
if not _has_video_opt():
107103
raise RuntimeError(
108104
"Not compiled with video_reader support, "
109105
+ "to enable video_reader support, please install "
110106
+ "ffmpeg (version 4.2 is currently supported) and"
111107
+ "build torchvision from source."
112108
)
113-
self._c = torch.classes.torchvision.Video(path, stream, num_threads)
109+
self._c = torch.classes.torchvision.Video(path, stream)
114110

115111
def __next__(self):
116112
"""Decodes and returns the next frame of the current stream.

0 commit comments

Comments
 (0)