Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
37eb37a8
Unverified
Commit
37eb37a8
authored
Mar 31, 2021
by
Francisco Massa
Committed by
GitHub
Mar 31, 2021
Browse files
Revert "Implementing multithreaded video decoding (#3389)" (#3623)
This reverts commit
3bfdb427
.
parent
02a1918a
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
19 additions
and
82 deletions
+19
-82
torchvision/csrc/io/decoder/decoder.cpp
torchvision/csrc/io/decoder/decoder.cpp
+1
-1
torchvision/csrc/io/decoder/defs.h
torchvision/csrc/io/decoder/defs.h
+0
-3
torchvision/csrc/io/decoder/stream.cpp
torchvision/csrc/io/decoder/stream.cpp
+1
-47
torchvision/csrc/io/decoder/stream.h
torchvision/csrc/io/decoder/stream.h
+1
-2
torchvision/csrc/io/video/video.cpp
torchvision/csrc/io/video/video.cpp
+9
-16
torchvision/csrc/io/video/video.h
torchvision/csrc/io/video/video.h
+5
-7
torchvision/io/__init__.py
torchvision/io/__init__.py
+2
-6
No files found.
torchvision/csrc/io/decoder/decoder.cpp
View file @
37eb37a8
...
@@ -432,7 +432,7 @@ bool Decoder::openStreams(std::vector<DecoderMetadata>* metadata) {
...
@@ -432,7 +432,7 @@ bool Decoder::openStreams(std::vector<DecoderMetadata>* metadata) {
it
->
format
,
it
->
format
,
params_
.
loggingUuid
);
params_
.
loggingUuid
);
CHECK
(
stream
);
CHECK
(
stream
);
if
(
stream
->
openCodec
(
metadata
,
params_
.
numThreads
)
<
0
)
{
if
(
stream
->
openCodec
(
metadata
)
<
0
)
{
LOG
(
ERROR
)
<<
"uuid="
<<
params_
.
loggingUuid
LOG
(
ERROR
)
<<
"uuid="
<<
params_
.
loggingUuid
<<
" open codec failed, stream_idx="
<<
i
;
<<
" open codec failed, stream_idx="
<<
i
;
return
false
;
return
false
;
...
...
torchvision/csrc/io/decoder/defs.h
View file @
37eb37a8
...
@@ -194,9 +194,6 @@ struct DecoderParameters {
...
@@ -194,9 +194,6 @@ struct DecoderParameters {
bool
preventStaleness
{
true
};
bool
preventStaleness
{
true
};
// seek tolerated accuracy (us)
// seek tolerated accuracy (us)
double
seekAccuracy
{
1000000.0
};
double
seekAccuracy
{
1000000.0
};
// Allow multithreaded decoding for numThreads > 1;
// 0 numThreads=0 sets up sensible defaults
int
numThreads
{
1
};
// what media types should be processed, default none
// what media types should be processed, default none
std
::
set
<
MediaFormat
>
formats
;
std
::
set
<
MediaFormat
>
formats
;
...
...
torchvision/csrc/io/decoder/stream.cpp
View file @
37eb37a8
#include "stream.h"
#include "stream.h"
#include <ATen/Parallel.h>
#include <c10/util/Logging.h>
#include <c10/util/Logging.h>
#include <stdio.h>
#include <string.h>
#include "util.h"
#include "util.h"
namespace
ffmpeg
{
namespace
ffmpeg
{
...
@@ -31,7 +28,7 @@ AVCodec* Stream::findCodec(AVCodecParameters* params) {
...
@@ -31,7 +28,7 @@ AVCodec* Stream::findCodec(AVCodecParameters* params) {
return
avcodec_find_decoder
(
params
->
codec_id
);
return
avcodec_find_decoder
(
params
->
codec_id
);
}
}
int
Stream
::
openCodec
(
std
::
vector
<
DecoderMetadata
>*
metadata
,
int
num_threads
)
{
int
Stream
::
openCodec
(
std
::
vector
<
DecoderMetadata
>*
metadata
)
{
AVStream
*
steam
=
inputCtx_
->
streams
[
format_
.
stream
];
AVStream
*
steam
=
inputCtx_
->
streams
[
format_
.
stream
];
AVCodec
*
codec
=
findCodec
(
steam
->
codecpar
);
AVCodec
*
codec
=
findCodec
(
steam
->
codecpar
);
...
@@ -56,49 +53,6 @@ int Stream::openCodec(std::vector<DecoderMetadata>* metadata, int num_threads) {
...
@@ -56,49 +53,6 @@ int Stream::openCodec(std::vector<DecoderMetadata>* metadata, int num_threads) {
return
ret
;
return
ret
;
}
}
// multithreading heuristics
int
max_threads
=
at
::
get_num_threads
();
// first a safety check
if
(
num_threads
>
max_threads
)
{
num_threads
=
max_threads
;
}
if
(
num_threads
>
0
)
{
if
(
num_threads
>
1
)
{
codecCtx_
->
active_thread_type
=
1
;
}
// if user defined, respect that
codecCtx_
->
thread_count
=
num_threads
;
}
else
{
// otherwise set sensible defaults
// with the special case for the different MPEG4 codecs
// that don't have threading context functions
// TODO: potentially automate this using native c++ function lookups
if
(
strcmp
(
codecCtx_
->
codec
->
name
,
"mpeg4"
)
==
0
&&
codecCtx_
->
codec_type
==
0
)
{
if
(
codecCtx_
->
codec_tag
==
1684633208
)
{
codecCtx_
->
thread_count
=
(
8
<=
max_threads
)
?
8
:
max_threads
;
codecCtx_
->
thread_type
=
1
;
}
else
{
codecCtx_
->
thread_count
=
(
2
<=
max_threads
)
?
2
:
max_threads
;
codecCtx_
->
thread_type
=
2
;
}
}
else
{
// otherwise default to multithreading
codecCtx_
->
thread_count
=
(
8
<=
max_threads
)
?
8
:
max_threads
;
codecCtx_
->
active_thread_type
=
1
;
}
}
// print codec type and number of threads
LOG
(
INFO
)
<<
"Codec "
<<
codecCtx_
->
codec
->
long_name
<<
" Codec id: "
<<
codecCtx_
->
codec_id
<<
" Codec tag: "
<<
codecCtx_
->
codec_tag
<<
" Codec type: "
<<
codecCtx_
->
codec_type
<<
" Codec extradata: "
<<
codecCtx_
->
extradata
<<
" Number of threads: "
<<
at
::
get_num_threads
();
// after avcodec_open2, value of codecCtx_->time_base is NOT meaningful
// after avcodec_open2, value of codecCtx_->time_base is NOT meaningful
if
((
ret
=
avcodec_open2
(
codecCtx_
,
codec
,
nullptr
))
<
0
)
{
if
((
ret
=
avcodec_open2
(
codecCtx_
,
codec
,
nullptr
))
<
0
)
{
LOG
(
ERROR
)
<<
"LoggingUuid #"
<<
loggingUuid_
LOG
(
ERROR
)
<<
"LoggingUuid #"
<<
loggingUuid_
...
...
torchvision/csrc/io/decoder/stream.h
View file @
37eb37a8
...
@@ -20,8 +20,7 @@ class Stream {
...
@@ -20,8 +20,7 @@ class Stream {
virtual
~
Stream
();
virtual
~
Stream
();
// returns 0 - on success or negative error
// returns 0 - on success or negative error
// num_threads sets up the codec context for multithreading if needed
int
openCodec
(
std
::
vector
<
DecoderMetadata
>*
metadata
);
int
openCodec
(
std
::
vector
<
DecoderMetadata
>*
metadata
,
int
num_threads
=
1
);
// returns 1 - if packet got consumed, 0 - if it's not, and < 0 on error
// returns 1 - if packet got consumed, 0 - if it's not, and < 0 on error
int
decodePacket
(
int
decodePacket
(
const
AVPacket
*
packet
,
const
AVPacket
*
packet
,
...
...
torchvision/csrc/io/video/video.cpp
View file @
37eb37a8
...
@@ -97,17 +97,15 @@ void Video::_getDecoderParams(
...
@@ -97,17 +97,15 @@ void Video::_getDecoderParams(
double
videoStartS
,
double
videoStartS
,
int64_t
getPtsOnly
,
int64_t
getPtsOnly
,
std
::
string
stream
,
std
::
string
stream
,
long
stream_id
,
long
stream_id
=
-
1
,
bool
all_streams
,
bool
all_streams
=
false
,
int64_t
num_threads
,
double
seekFrameMarginUs
=
10
)
{
double
seekFrameMarginUs
)
{
int64_t
videoStartUs
=
int64_t
(
videoStartS
*
1e6
);
int64_t
videoStartUs
=
int64_t
(
videoStartS
*
1e6
);
params
.
timeoutMs
=
decoderTimeoutMs
;
params
.
timeoutMs
=
decoderTimeoutMs
;
params
.
startOffset
=
videoStartUs
;
params
.
startOffset
=
videoStartUs
;
params
.
seekAccuracy
=
seekFrameMarginUs
;
params
.
seekAccuracy
=
seekFrameMarginUs
;
params
.
headerOnly
=
false
;
params
.
headerOnly
=
false
;
params
.
numThreads
=
num_threads
;
params
.
preventStaleness
=
false
;
// not sure what this is about
params
.
preventStaleness
=
false
;
// not sure what this is about
...
@@ -154,9 +152,7 @@ void Video::_getDecoderParams(
...
@@ -154,9 +152,7 @@ void Video::_getDecoderParams(
}
// _get decoder params
}
// _get decoder params
Video
::
Video
(
std
::
string
videoPath
,
std
::
string
stream
,
int64_t
numThreads
)
{
Video
::
Video
(
std
::
string
videoPath
,
std
::
string
stream
)
{
// set number of threads global
numThreads_
=
numThreads
;
// parse stream information
// parse stream information
current_stream
=
_parseStream
(
stream
);
current_stream
=
_parseStream
(
stream
);
// note that in the initial call we want to get all streams
// 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) {
...
@@ -165,8 +161,7 @@ Video::Video(std::string videoPath, std::string stream, int64_t numThreads) {
0
,
// headerOnly
0
,
// headerOnly
std
::
get
<
0
>
(
current_stream
),
// stream info - remove that
std
::
get
<
0
>
(
current_stream
),
// stream info - remove that
long
(
-
1
),
// stream_id parsed from info above change to -2
long
(
-
1
),
// stream_id parsed from info above change to -2
true
,
// read all streams
true
// read all streams
numThreads_
// global number of Threads for decoding
);
);
std
::
string
logMessage
,
logType
;
std
::
string
logMessage
,
logType
;
...
@@ -230,7 +225,7 @@ Video::Video(std::string videoPath, std::string stream, int64_t numThreads) {
...
@@ -230,7 +225,7 @@ Video::Video(std::string videoPath, std::string stream, int64_t numThreads) {
}
}
}
// video
}
// video
bool
Video
::
setCurrentStream
(
std
::
string
stream
)
{
bool
Video
::
setCurrentStream
(
std
::
string
stream
=
"video"
)
{
if
((
!
stream
.
empty
())
&&
(
_parseStream
(
stream
)
!=
current_stream
))
{
if
((
!
stream
.
empty
())
&&
(
_parseStream
(
stream
)
!=
current_stream
))
{
current_stream
=
_parseStream
(
stream
);
current_stream
=
_parseStream
(
stream
);
}
}
...
@@ -246,8 +241,7 @@ bool Video::setCurrentStream(std::string stream) {
...
@@ -246,8 +241,7 @@ bool Video::setCurrentStream(std::string stream) {
std
::
get
<
0
>
(
current_stream
),
// stream
std
::
get
<
0
>
(
current_stream
),
// stream
long
(
std
::
get
<
1
>
(
long
(
std
::
get
<
1
>
(
current_stream
)),
// stream_id parsed from info above change to -2
current_stream
)),
// stream_id parsed from info above change to -2
false
,
// read all streams
false
// read all streams
numThreads_
// global number of threads
);
);
// calback and metadata defined in Video.h
// calback and metadata defined in Video.h
...
@@ -271,8 +265,7 @@ void Video::Seek(double ts) {
...
@@ -271,8 +265,7 @@ void Video::Seek(double ts) {
std
::
get
<
0
>
(
current_stream
),
// stream
std
::
get
<
0
>
(
current_stream
),
// stream
long
(
std
::
get
<
1
>
(
long
(
std
::
get
<
1
>
(
current_stream
)),
// stream_id parsed from info above change to -2
current_stream
)),
// stream_id parsed from info above change to -2
false
,
// read all streams
false
// read all streams
numThreads_
// global num threads
);
);
// calback and metadata defined in Video.h
// calback and metadata defined in Video.h
...
@@ -338,7 +331,7 @@ std::tuple<torch::Tensor, double> Video::Next() {
...
@@ -338,7 +331,7 @@ std::tuple<torch::Tensor, double> Video::Next() {
static
auto
registerVideo
=
static
auto
registerVideo
=
torch
::
class_
<
Video
>
(
"torchvision"
,
"Video"
)
torch
::
class_
<
Video
>
(
"torchvision"
,
"Video"
)
.
def
(
torch
::
init
<
std
::
string
,
std
::
string
,
int64_t
>
())
.
def
(
torch
::
init
<
std
::
string
,
std
::
string
>
())
.
def
(
"get_current_stream"
,
&
Video
::
getCurrentStream
)
.
def
(
"get_current_stream"
,
&
Video
::
getCurrentStream
)
.
def
(
"set_current_stream"
,
&
Video
::
setCurrentStream
)
.
def
(
"set_current_stream"
,
&
Video
::
setCurrentStream
)
.
def
(
"get_metadata"
,
&
Video
::
getStreamMetadata
)
.
def
(
"get_metadata"
,
&
Video
::
getStreamMetadata
)
...
...
torchvision/csrc/io/video/video.h
View file @
37eb37a8
...
@@ -16,15 +16,14 @@ struct Video : torch::CustomClassHolder {
...
@@ -16,15 +16,14 @@ struct Video : torch::CustomClassHolder {
// global video metadata
// global video metadata
c10
::
Dict
<
std
::
string
,
c10
::
Dict
<
std
::
string
,
std
::
vector
<
double
>>>
c10
::
Dict
<
std
::
string
,
c10
::
Dict
<
std
::
string
,
std
::
vector
<
double
>>>
streamsMetadata
;
streamsMetadata
;
int64_t
numThreads_
{
0
};
public:
public:
Video
(
std
::
string
videoPath
,
std
::
string
stream
,
int64_t
numThreads
);
Video
(
std
::
string
videoPath
,
std
::
string
stream
);
std
::
tuple
<
std
::
string
,
int64_t
>
getCurrentStream
()
const
;
std
::
tuple
<
std
::
string
,
int64_t
>
getCurrentStream
()
const
;
c10
::
Dict
<
std
::
string
,
c10
::
Dict
<
std
::
string
,
std
::
vector
<
double
>>>
c10
::
Dict
<
std
::
string
,
c10
::
Dict
<
std
::
string
,
std
::
vector
<
double
>>>
getStreamMetadata
()
const
;
getStreamMetadata
()
const
;
void
Seek
(
double
ts
);
void
Seek
(
double
ts
);
bool
setCurrentStream
(
std
::
string
stream
=
"video"
);
bool
setCurrentStream
(
std
::
string
stream
);
std
::
tuple
<
torch
::
Tensor
,
double
>
Next
();
std
::
tuple
<
torch
::
Tensor
,
double
>
Next
();
private:
private:
...
@@ -38,10 +37,9 @@ struct Video : torch::CustomClassHolder {
...
@@ -38,10 +37,9 @@ struct Video : torch::CustomClassHolder {
double
videoStartS
,
double
videoStartS
,
int64_t
getPtsOnly
,
int64_t
getPtsOnly
,
std
::
string
stream
,
std
::
string
stream
,
long
stream_id
=
-
1
,
long
stream_id
,
bool
all_streams
=
false
,
bool
all_streams
,
int64_t
num_threads
=
0
,
double
seekFrameMarginUs
);
// this needs to be improved
double
seekFrameMarginUs
=
10
);
// this needs to be improved
std
::
map
<
std
::
string
,
std
::
vector
<
double
>>
streamTimeBase
;
// not used
std
::
map
<
std
::
string
,
std
::
vector
<
double
>>
streamTimeBase
;
// not used
...
...
torchvision/io/__init__.py
View file @
37eb37a8
...
@@ -96,13 +96,9 @@ class VideoReader:
...
@@ -96,13 +96,9 @@ class VideoReader:
stream (string, optional): descriptor of the required stream, followed by the stream id,
stream (string, optional): descriptor of the required stream, followed by the stream id,
in the format ``{stream_type}:{stream_id}``. Defaults to ``"video:0"``.
in the format ``{stream_type}:{stream_id}``. Defaults to ``"video:0"``.
Currently available options include ``['video', 'audio']``
Currently available options include ``['video', 'audio']``
num_threads (int, optional): number of threads used by the codec to decode video.
Default value (0) enables multithreading with codec-dependent heuristic. The performance
will depend on the version of FFMPEG codecs supported.
"""
"""
def
__init__
(
self
,
path
,
stream
=
"video"
,
num_threads
=
0
):
def
__init__
(
self
,
path
,
stream
=
"video"
):
if
not
_has_video_opt
():
if
not
_has_video_opt
():
raise
RuntimeError
(
raise
RuntimeError
(
"Not compiled with video_reader support, "
"Not compiled with video_reader support, "
...
@@ -110,7 +106,7 @@ class VideoReader:
...
@@ -110,7 +106,7 @@ class VideoReader:
+
"ffmpeg (version 4.2 is currently supported) and"
+
"ffmpeg (version 4.2 is currently supported) and"
+
"build torchvision from source."
+
"build torchvision from source."
)
)
self
.
_c
=
torch
.
classes
.
torchvision
.
Video
(
path
,
stream
,
num_threads
)
self
.
_c
=
torch
.
classes
.
torchvision
.
Video
(
path
,
stream
)
def
__next__
(
self
):
def
__next__
(
self
):
"""Decodes and returns the next frame of the current stream.
"""Decodes and returns the next frame of the current stream.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment