Skip to content

Commit a8f4e97

Browse files
mthrokfacebook-github-bot
authored andcommitted
Refactor StreamReader - let StreamProcessor own codec context (#3157)
Summary: Pull Request resolved: #3157 AVCodecContext plays central role in decoding and encoding. Currently in StreamReader, the object is owned inside of Decoder class and it's not accessible from other objects. This commit move the ownership of AVCodecContext out of Decoder to StreamProcessor class so that other components can check access its field. Also, the Decoder class, which is super thin wrapper around AVCodecContext object, is now absorbed to StreamProcessor class. Reviewed By: xiaohui-zhang Differential Revision: D43924664 fbshipit-source-id: e53254955d9ce16871e393bcd8bb2794ce6a51ff
1 parent 430dd17 commit a8f4e97

File tree

9 files changed

+157
-221
lines changed

9 files changed

+157
-221
lines changed

torchaudio/csrc/ffmpeg/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ set(
1212
stream_reader/buffer/common.cpp
1313
stream_reader/buffer/chunked_buffer.cpp
1414
stream_reader/buffer/unchunked_buffer.cpp
15-
stream_reader/decoder.cpp
1615
stream_reader/sink.cpp
1716
stream_reader/stream_processor.cpp
1817
stream_reader/stream_reader.cpp

torchaudio/csrc/ffmpeg/stream_reader/decoder.cpp

Lines changed: 0 additions & 135 deletions
This file was deleted.

torchaudio/csrc/ffmpeg/stream_reader/decoder.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

torchaudio/csrc/ffmpeg/stream_reader/sink.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,27 @@ std::unique_ptr<Buffer> get_buffer(
4949

5050
std::unique_ptr<FilterGraph> get_filter_graph(
5151
AVRational input_time_base,
52-
AVCodecParameters* codecpar,
52+
AVCodecContext* codec_ctx,
5353
AVRational frame_rate,
5454
const std::string& filter_description) {
55-
auto p = std::make_unique<FilterGraph>(codecpar->codec_type);
55+
auto p = std::make_unique<FilterGraph>(codec_ctx->codec_type);
5656

57-
switch (codecpar->codec_type) {
57+
switch (codec_ctx->codec_type) {
5858
case AVMEDIA_TYPE_AUDIO:
5959
p->add_audio_src(
60-
static_cast<AVSampleFormat>(codecpar->format),
60+
codec_ctx->sample_fmt,
6161
input_time_base,
62-
codecpar->sample_rate,
63-
codecpar->channel_layout);
62+
codec_ctx->sample_rate,
63+
codec_ctx->channel_layout);
6464
break;
6565
case AVMEDIA_TYPE_VIDEO:
6666
p->add_video_src(
67-
static_cast<AVPixelFormat>(codecpar->format),
67+
codec_ctx->pix_fmt,
6868
input_time_base,
6969
frame_rate,
70-
codecpar->width,
71-
codecpar->height,
72-
codecpar->sample_aspect_ratio);
70+
codec_ctx->width,
71+
codec_ctx->height,
72+
codec_ctx->sample_aspect_ratio);
7373
break;
7474
default:
7575
TORCH_CHECK(false, "Only audio/video are supported.");
@@ -84,25 +84,25 @@ std::unique_ptr<FilterGraph> get_filter_graph(
8484

8585
Sink::Sink(
8686
AVRational input_time_base_,
87-
AVCodecParameters* codecpar_,
87+
AVCodecContext* codec_ctx_,
8888
int frames_per_chunk,
8989
int num_chunks,
9090
AVRational frame_rate_,
9191
const c10::optional<std::string>& filter_description_,
9292
const torch::Device& device)
9393
: input_time_base(input_time_base_),
94-
codecpar(codecpar_),
94+
codec_ctx(codec_ctx_),
9595
frame_rate(frame_rate_),
9696
filter_description(filter_description_.value_or(
97-
codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? "anull" : "null")),
97+
codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO ? "anull" : "null")),
9898
filter(get_filter_graph(
9999
input_time_base_,
100-
codecpar_,
100+
codec_ctx,
101101
frame_rate,
102102
filter_description)),
103103
output_time_base(filter->get_output_timebase()),
104104
buffer(get_buffer(
105-
codecpar_->codec_type,
105+
codec_ctx->codec_type,
106106
frames_per_chunk,
107107
num_chunks,
108108
double(output_time_base.num) / output_time_base.den,
@@ -139,7 +139,7 @@ FilterGraphOutputInfo Sink::get_filter_output_info() const {
139139

140140
void Sink::flush() {
141141
filter = get_filter_graph(
142-
input_time_base, codecpar, frame_rate, filter_description);
142+
input_time_base, codec_ctx, frame_rate, filter_description);
143143
buffer->flush();
144144
}
145145

torchaudio/csrc/ffmpeg/stream_reader/sink.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Sink {
1212

1313
// Parameters for recreating FilterGraph
1414
AVRational input_time_base;
15-
AVCodecParameters* codecpar;
15+
AVCodecContext* codec_ctx;
1616
AVRational frame_rate;
1717
std::string filter_description;
1818
std::unique_ptr<FilterGraph> filter;
@@ -23,7 +23,7 @@ class Sink {
2323
std::unique_ptr<Buffer> buffer;
2424
Sink(
2525
AVRational input_time_base,
26-
AVCodecParameters* codecpar,
26+
AVCodecContext* codec_ctx,
2727
int frames_per_chunk,
2828
int num_chunks,
2929
AVRational frame_rate,

0 commit comments

Comments
 (0)