Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sink class #2111

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions torchaudio/csrc/ffmpeg/sink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <torchaudio/csrc/ffmpeg/sink.h>

namespace torchaudio {
namespace ffmpeg {

namespace {
std::unique_ptr<Buffer> get_buffer(
AVMediaType type,
int frames_per_chunk,
int num_chunks) {
switch (type) {
case AVMEDIA_TYPE_AUDIO:
return std::unique_ptr<Buffer>(
new AudioBuffer(frames_per_chunk, num_chunks));
case AVMEDIA_TYPE_VIDEO:
return std::unique_ptr<Buffer>(
new VideoBuffer(frames_per_chunk, num_chunks));
default:
throw std::runtime_error(
std::string("Unsupported media type: ") +
av_get_media_type_string(type));
}
}
} // namespace

Sink::Sink(
AVRational input_time_base,
AVCodecParameters* codecpar,
int frames_per_chunk,
int num_chunks,
double output_time_base,
std::string filter_description)
: filter(input_time_base, codecpar, filter_description),
buffer(get_buffer(codecpar->codec_type, frames_per_chunk, num_chunks)),
time_base(output_time_base) {}

// 0: some kind of success
// <0: Some error happened
int Sink::process_frame(AVFrame* pFrame) {
int ret = filter.add_frame(pFrame);
while (ret >= 0) {
ret = filter.get_frame(frame);
// AVERROR(EAGAIN) means that new input data is required to return new
// output.
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return 0;
if (ret >= 0)
buffer->push_frame(frame);
av_frame_unref(frame);
}
return ret;
}

bool Sink::is_buffer_ready() const {
return buffer->is_ready();
}
} // namespace ffmpeg
} // namespace torchaudio
30 changes: 30 additions & 0 deletions torchaudio/csrc/ffmpeg/sink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <torchaudio/csrc/ffmpeg/buffer.h>
#include <torchaudio/csrc/ffmpeg/ffmpeg.h>
#include <torchaudio/csrc/ffmpeg/filter_graph.h>

namespace torchaudio {
namespace ffmpeg {

class Sink {
AVFramePtr frame;

public:
FilterGraph filter;
std::unique_ptr<Buffer> buffer;
double time_base;
Sink(
AVRational input_time_base,
AVCodecParameters* codecpar,
int frames_per_chunk,
int num_chunks,
double output_time_base,
std::string filter_description);

int process_frame(AVFrame* frame);
bool is_buffer_ready() const;
};

} // namespace ffmpeg
} // namespace torchaudio