Unverified Commit fcf38946 authored by moto-meta's avatar moto-meta Committed by GitHub
Browse files

Update StreamReader/Writer name

Differential Revision: D50696105

Pull Request resolved: https://github.com/pytorch/audio/pull/3682
parent 09887246
......@@ -5,71 +5,72 @@
.. note::
The top-level namespace has been changed from ``torchaudio`` to ``torio``.
``StreamReader`` has been renamed to ``StreamingMediaDecoder``.
torio::io::StreamReader
=======================
torio::io::StreamingMediaDecoder
================================
``StreamReader`` is the implementation used by Python equivalent and provides similar interface.
When working with custom I/O, such as in-memory data, ``StreamReaderCustomIO`` class can be used.
``StreamingMediaDecoder`` is the implementation used by Python equivalent and provides similar interface.
When working with custom I/O, such as in-memory data, ``StreamingMediaDecoderCustomIO`` class can be used.
Both classes have the same methods defined, so their usages are the same.
Constructors
------------
StreamReader
^^^^^^^^^^^^
StreamingMediaDecoder
^^^^^^^^^^^^^^^^^^^^^
.. doxygenclass:: torio::io::StreamReader
.. doxygenclass:: torio::io::StreamingMediaDecoder
.. doxygenfunction:: torio::io::StreamReader::StreamReader(const std::string &src, const c10::optional<std::string> &format = {}, const c10::optional<OptionDict> &option = {})
.. doxygenfunction:: torio::io::StreamingMediaDecoder::StreamingMediaDecoder(const std::string &src, const c10::optional<std::string> &format = {}, const c10::optional<OptionDict> &option = {})
StreamReaderCustomIO
^^^^^^^^^^^^^^^^^^^^
StreamingMediaDecoderCustomIO
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. doxygenclass:: torio::io::StreamReaderCustomIO
.. doxygenclass:: torio::io::StreamingMediaDecoderCustomIO
.. doxygenfunction:: torio::io::StreamReaderCustomIO::StreamReaderCustomIO
.. doxygenfunction:: torio::io::StreamingMediaDecoderCustomIO::StreamingMediaDecoderCustomIO
Query Methods
-------------
find_best_audio_stream
^^^^^^^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::find_best_audio_stream
.. doxygenfunction:: torio::io::StreamingMediaDecoder::find_best_audio_stream
find_best_video_stream
^^^^^^^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::find_best_video_stream
.. doxygenfunction:: torio::io::StreamingMediaDecoder::find_best_video_stream
get_metadata
^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::get_metadata
.. doxygenfunction:: torio::io::StreamingMediaDecoder::get_metadata
num_src_streams
^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::num_src_streams
.. doxygenfunction:: torio::io::StreamingMediaDecoder::num_src_streams
get_src_stream_info
^^^^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::get_src_stream_info
.. doxygenfunction:: torio::io::StreamingMediaDecoder::get_src_stream_info
num_out_streams
^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::num_out_streams
.. doxygenfunction:: torio::io::StreamingMediaDecoder::num_out_streams
get_out_stream_info
^^^^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::get_out_stream_info
.. doxygenfunction:: torio::io::StreamingMediaDecoder::get_out_stream_info
is_buffer_ready
^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::is_buffer_ready
.. doxygenfunction:: torio::io::StreamingMediaDecoder::is_buffer_ready
Configure Methods
-----------------
......@@ -77,38 +78,38 @@ Configure Methods
add_audio_stream
^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::add_audio_stream
.. doxygenfunction:: torio::io::StreamingMediaDecoder::add_audio_stream
add_video_stream
^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::add_video_stream
.. doxygenfunction:: torio::io::StreamingMediaDecoder::add_video_stream
remove_stream
^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::remove_stream
.. doxygenfunction:: torio::io::StreamingMediaDecoder::remove_stream
Stream Methods
^^^^^^^^^^^^^^
seek
^^^^
.. doxygenfunction:: torio::io::StreamReader::seek
.. doxygenfunction:: torio::io::StreamingMediaDecoder::seek
process_packet
^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::process_packet()
.. doxygenfunction:: torio::io::StreamingMediaDecoder::process_packet()
process_packet_block
^^^^^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::process_packet_block
.. doxygenfunction:: torio::io::StreamingMediaDecoder::process_packet_block
process_all_packets
^^^^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::process_all_packets
.. doxygenfunction:: torio::io::StreamingMediaDecoder::process_all_packets
fill_buffer
^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::fill_buffer
.. doxygenfunction:: torio::io::StreamingMediaDecoder::fill_buffer
Retrieval Methods
-----------------
......@@ -116,7 +117,7 @@ Retrieval Methods
pop_chunks
^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamReader::pop_chunks
.. doxygenfunction:: torio::io::StreamingMediaDecoder::pop_chunks
Support Structures
......
......@@ -5,32 +5,33 @@
.. note::
The top-level namespace has been changed from ``torchaudio`` to ``torio``.
``StreamWriter`` has been renamed to ``StreamingMediaEncoder``.
torio::io::StreamWriter
=======================
torio::io::StreamingMediaEncoder
================================
``StreamWriter`` is the implementation used by Python equivalent and provides similar interface.
When working with custom I/O, such as in-memory data, ``StreamWriterCustomIO`` class can be used.
``StreamingMediaEncoder`` is the implementation used by Python equivalent and provides similar interface.
When working with custom I/O, such as in-memory data, ``StreamingMediaEncoderCustomIO`` class can be used.
Both classes have the same methods defined, so their usages are the same.
Constructors
------------
StreamWriter
^^^^^^^^^^^^
StreamingMediaEncoder
^^^^^^^^^^^^^^^^^^^^^
.. doxygenclass:: torio::io::StreamWriter
.. doxygenclass:: torio::io::StreamingMediaEncoder
.. doxygenfunction:: torio::io::StreamWriter::StreamWriter(const std::string &dst, const c10::optional<std::string> &format = {})
.. doxygenfunction:: torio::io::StreamingMediaEncoder::StreamingMediaEncoder(const std::string &dst, const c10::optional<std::string> &format = {})
StreamWriterCustomIO
^^^^^^^^^^^^^^^^^^^^
StreamingMediaEncoderCustomIO
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. doxygenclass:: torio::io::StreamWriterCustomIO
.. doxygenclass:: torio::io::StreamingMediaEncoderCustomIO
.. doxygenfunction:: torio::io::StreamWriterCustomIO::StreamWriterCustomIO
.. doxygenfunction:: torio::io::StreamingMediaEncoderCustomIO::StreamingMediaEncoderCustomIO
Config methods
--------------
......@@ -38,17 +39,17 @@ Config methods
add_audio_stream
^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamWriter::add_audio_stream
.. doxygenfunction:: torio::io::StreamingMediaEncoder::add_audio_stream
add_video_stream
^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamWriter::add_video_stream
.. doxygenfunction:: torio::io::StreamingMediaEncoder::add_video_stream
set_metadata
^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamWriter::set_metadata
.. doxygenfunction:: torio::io::StreamingMediaEncoder::set_metadata
Write methods
-------------
......@@ -56,24 +57,24 @@ Write methods
open
^^^^
.. doxygenfunction:: torio::io::StreamWriter::open
.. doxygenfunction:: torio::io::StreamingMediaEncoder::open
close
^^^^^
.. doxygenfunction:: torio::io::StreamWriter::close
.. doxygenfunction:: torio::io::StreamingMediaEncoder::close
write_audio_chunk
^^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamWriter::write_audio_chunk
.. doxygenfunction:: torio::io::StreamingMediaEncoder::write_audio_chunk
write_video_chunk
^^^^^^^^^^^^^^^^^
.. doxygenfunction:: torio::io::StreamWriter::write_video_chunk
.. doxygenfunction:: torio::io::StreamingMediaEncoder::write_video_chunk
flush
^^^^^
.. doxygenfunction:: torio::io::StreamWriter::flush
.. doxygenfunction:: torio::io::StreamingMediaEncoder::flush
......@@ -13,14 +13,14 @@ Practically all the code is re-organization of examples;
https://ffmpeg.org/doxygen/4.1/examples.html
## StreamReader Architecture
## StreamingMediaDecoder Architecture
The top level class is `StreamReader` class. This class handles the input (via `AVFormatContext*`), and manages `StreamProcessor`s for each stream in the input.
The top level class is `StreamingMediaDecoder` class. This class handles the input (via `AVFormatContext*`), and manages `StreamProcessor`s for each stream in the input.
The `StreamReader` object slices the input data into a series of `AVPacket` objects and it feeds the objects to corresponding `StreamProcessor`s.
The `StreamingMediaDecoder` object slices the input data into a series of `AVPacket` objects and it feeds the objects to corresponding `StreamProcessor`s.
```
StreamReader
StreamingMediaDecoder
┌─────────────────────────────────────────────────┐
│ │
│ AVFormatContext* ┌──► StreamProcessor[0] │
......
......@@ -98,7 +98,7 @@ std::string get_build_config() {
}
//////////////////////////////////////////////////////////////////////////////
// StreamReader/Writer FileObj
// StreamingMediaDecoder/Encoder FileObj
//////////////////////////////////////////////////////////////////////////////
struct FileObj {
......@@ -156,14 +156,15 @@ static int64_t seek_func(void* opaque, int64_t offset, int whence) {
} // namespace
struct StreamReaderFileObj : private FileObj, public StreamReaderCustomIO {
StreamReaderFileObj(
struct StreamingMediaDecoderFileObj : private FileObj,
public StreamingMediaDecoderCustomIO {
StreamingMediaDecoderFileObj(
py::object fileobj,
const c10::optional<std::string>& format,
const c10::optional<std::map<std::string, std::string>>& option,
int buffer_size)
: FileObj{fileobj, buffer_size},
StreamReaderCustomIO(
StreamingMediaDecoderCustomIO(
this,
format,
buffer_size,
......@@ -172,13 +173,14 @@ struct StreamReaderFileObj : private FileObj, public StreamReaderCustomIO {
option) {}
};
struct StreamWriterFileObj : private FileObj, public StreamWriterCustomIO {
StreamWriterFileObj(
struct StreamingMediaEncoderFileObj : private FileObj,
public StreamingMediaEncoderCustomIO {
StreamingMediaEncoderFileObj(
py::object fileobj,
const c10::optional<std::string>& format,
int buffer_size)
: FileObj{fileobj, buffer_size},
StreamWriterCustomIO(
StreamingMediaEncoderCustomIO(
this,
format,
buffer_size,
......@@ -187,7 +189,7 @@ struct StreamWriterFileObj : private FileObj, public StreamWriterCustomIO {
};
//////////////////////////////////////////////////////////////////////////////
// StreamReader/Writer Bytes
// StreamingMediaDecoder/Encoder Bytes
//////////////////////////////////////////////////////////////////////////////
struct BytesWrapper {
std::string_view src;
......@@ -225,14 +227,15 @@ static int64_t seek_bytes(void* opaque, int64_t offset, int whence) {
return static_cast<int64_t>(wrapper->index);
}
struct StreamReaderBytes : private BytesWrapper, public StreamReaderCustomIO {
StreamReaderBytes(
struct StreamingMediaDecoderBytes : private BytesWrapper,
public StreamingMediaDecoderCustomIO {
StreamingMediaDecoderBytes(
std::string_view src,
const c10::optional<std::string>& format,
const c10::optional<std::map<std::string, std::string>>& option,
int64_t buffer_size)
: BytesWrapper{src},
StreamReaderCustomIO(
StreamingMediaDecoderCustomIO(
this,
format,
buffer_size,
......@@ -276,28 +279,32 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
.def_readwrite("pts", &Chunk::pts);
py::class_<CodecConfig>(m, "CodecConfig", py::module_local())
.def(py::init<int, int, const c10::optional<int>&, int, int>());
py::class_<StreamWriter>(m, "StreamWriter", py::module_local())
py::class_<StreamingMediaEncoder>(
m, "StreamingMediaEncoder", py::module_local())
.def(py::init<const std::string&, const c10::optional<std::string>&>())
.def("set_metadata", &StreamWriter::set_metadata)
.def("add_audio_stream", &StreamWriter::add_audio_stream)
.def("add_video_stream", &StreamWriter::add_video_stream)
.def("dump_format", &StreamWriter::dump_format)
.def("open", &StreamWriter::open)
.def("write_audio_chunk", &StreamWriter::write_audio_chunk)
.def("write_video_chunk", &StreamWriter::write_video_chunk)
.def("flush", &StreamWriter::flush)
.def("close", &StreamWriter::close);
py::class_<StreamWriterFileObj>(m, "StreamWriterFileObj", py::module_local())
.def("set_metadata", &StreamingMediaEncoder::set_metadata)
.def("add_audio_stream", &StreamingMediaEncoder::add_audio_stream)
.def("add_video_stream", &StreamingMediaEncoder::add_video_stream)
.def("dump_format", &StreamingMediaEncoder::dump_format)
.def("open", &StreamingMediaEncoder::open)
.def("write_audio_chunk", &StreamingMediaEncoder::write_audio_chunk)
.def("write_video_chunk", &StreamingMediaEncoder::write_video_chunk)
.def("flush", &StreamingMediaEncoder::flush)
.def("close", &StreamingMediaEncoder::close);
py::class_<StreamingMediaEncoderFileObj>(
m, "StreamingMediaEncoderFileObj", py::module_local())
.def(py::init<py::object, const c10::optional<std::string>&, int64_t>())
.def("set_metadata", &StreamWriterFileObj::set_metadata)
.def("add_audio_stream", &StreamWriterFileObj::add_audio_stream)
.def("add_video_stream", &StreamWriterFileObj::add_video_stream)
.def("dump_format", &StreamWriterFileObj::dump_format)
.def("open", &StreamWriterFileObj::open)
.def("write_audio_chunk", &StreamWriterFileObj::write_audio_chunk)
.def("write_video_chunk", &StreamWriterFileObj::write_video_chunk)
.def("flush", &StreamWriterFileObj::flush)
.def("close", &StreamWriterFileObj::close);
.def("set_metadata", &StreamingMediaEncoderFileObj::set_metadata)
.def("add_audio_stream", &StreamingMediaEncoderFileObj::add_audio_stream)
.def("add_video_stream", &StreamingMediaEncoderFileObj::add_video_stream)
.def("dump_format", &StreamingMediaEncoderFileObj::dump_format)
.def("open", &StreamingMediaEncoderFileObj::open)
.def(
"write_audio_chunk", &StreamingMediaEncoderFileObj::write_audio_chunk)
.def(
"write_video_chunk", &StreamingMediaEncoderFileObj::write_video_chunk)
.def("flush", &StreamingMediaEncoderFileObj::flush)
.def("close", &StreamingMediaEncoderFileObj::close);
py::class_<OutputStreamInfo>(m, "OutputStreamInfo", py::module_local())
.def_readonly("source_index", &OutputStreamInfo::source_index)
.def_readonly("filter_description", &OutputStreamInfo::filter_description)
......@@ -355,84 +362,107 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
.def_readonly("width", &SrcStreamInfo::width)
.def_readonly("height", &SrcStreamInfo::height)
.def_readonly("frame_rate", &SrcStreamInfo::frame_rate);
py::class_<StreamReader>(m, "StreamReader", py::module_local())
py::class_<StreamingMediaDecoder>(
m, "StreamingMediaDecoder", py::module_local())
.def(py::init<
const std::string&,
const c10::optional<std::string>&,
const c10::optional<OptionDict>&>())
.def("num_src_streams", &StreamReader::num_src_streams)
.def("num_out_streams", &StreamReader::num_out_streams)
.def("find_best_audio_stream", &StreamReader::find_best_audio_stream)
.def("find_best_video_stream", &StreamReader::find_best_video_stream)
.def("get_metadata", &StreamReader::get_metadata)
.def("get_src_stream_info", &StreamReader::get_src_stream_info)
.def("get_out_stream_info", &StreamReader::get_out_stream_info)
.def("seek", &StreamReader::seek)
.def("add_audio_stream", &StreamReader::add_audio_stream)
.def("add_video_stream", &StreamReader::add_video_stream)
.def("remove_stream", &StreamReader::remove_stream)
.def("num_src_streams", &StreamingMediaDecoder::num_src_streams)
.def("num_out_streams", &StreamingMediaDecoder::num_out_streams)
.def(
"find_best_audio_stream",
&StreamingMediaDecoder::find_best_audio_stream)
.def(
"find_best_video_stream",
&StreamingMediaDecoder::find_best_video_stream)
.def("get_metadata", &StreamingMediaDecoder::get_metadata)
.def("get_src_stream_info", &StreamingMediaDecoder::get_src_stream_info)
.def("get_out_stream_info", &StreamingMediaDecoder::get_out_stream_info)
.def("seek", &StreamingMediaDecoder::seek)
.def("add_audio_stream", &StreamingMediaDecoder::add_audio_stream)
.def("add_video_stream", &StreamingMediaDecoder::add_video_stream)
.def("remove_stream", &StreamingMediaDecoder::remove_stream)
.def(
"process_packet",
py::overload_cast<const c10::optional<double>&, const double>(
&StreamReader::process_packet))
.def("process_all_packets", &StreamReader::process_all_packets)
.def("fill_buffer", &StreamReader::fill_buffer)
.def("is_buffer_ready", &StreamReader::is_buffer_ready)
.def("pop_chunks", &StreamReader::pop_chunks);
py::class_<StreamReaderFileObj>(m, "StreamReaderFileObj", py::module_local())
&StreamingMediaDecoder::process_packet))
.def("process_all_packets", &StreamingMediaDecoder::process_all_packets)
.def("fill_buffer", &StreamingMediaDecoder::fill_buffer)
.def("is_buffer_ready", &StreamingMediaDecoder::is_buffer_ready)
.def("pop_chunks", &StreamingMediaDecoder::pop_chunks);
py::class_<StreamingMediaDecoderFileObj>(
m, "StreamingMediaDecoderFileObj", py::module_local())
.def(py::init<
py::object,
const c10::optional<std::string>&,
const c10::optional<OptionDict>&,
int64_t>())
.def("num_src_streams", &StreamReaderFileObj::num_src_streams)
.def("num_out_streams", &StreamReaderFileObj::num_out_streams)
.def("num_src_streams", &StreamingMediaDecoderFileObj::num_src_streams)
.def("num_out_streams", &StreamingMediaDecoderFileObj::num_out_streams)
.def(
"find_best_audio_stream",
&StreamReaderFileObj::find_best_audio_stream)
&StreamingMediaDecoderFileObj::find_best_audio_stream)
.def(
"find_best_video_stream",
&StreamReaderFileObj::find_best_video_stream)
.def("get_metadata", &StreamReaderFileObj::get_metadata)
.def("get_src_stream_info", &StreamReaderFileObj::get_src_stream_info)
.def("get_out_stream_info", &StreamReaderFileObj::get_out_stream_info)
.def("seek", &StreamReaderFileObj::seek)
.def("add_audio_stream", &StreamReaderFileObj::add_audio_stream)
.def("add_video_stream", &StreamReaderFileObj::add_video_stream)
.def("remove_stream", &StreamReaderFileObj::remove_stream)
&StreamingMediaDecoderFileObj::find_best_video_stream)
.def("get_metadata", &StreamingMediaDecoderFileObj::get_metadata)
.def(
"get_src_stream_info",
&StreamingMediaDecoderFileObj::get_src_stream_info)
.def(
"get_out_stream_info",
&StreamingMediaDecoderFileObj::get_out_stream_info)
.def("seek", &StreamingMediaDecoderFileObj::seek)
.def("add_audio_stream", &StreamingMediaDecoderFileObj::add_audio_stream)
.def("add_video_stream", &StreamingMediaDecoderFileObj::add_video_stream)
.def("remove_stream", &StreamingMediaDecoderFileObj::remove_stream)
.def(
"process_packet",
py::overload_cast<const c10::optional<double>&, const double>(
&StreamReader::process_packet))
.def("process_all_packets", &StreamReaderFileObj::process_all_packets)
.def("fill_buffer", &StreamReaderFileObj::fill_buffer)
.def("is_buffer_ready", &StreamReaderFileObj::is_buffer_ready)
.def("pop_chunks", &StreamReaderFileObj::pop_chunks);
py::class_<StreamReaderBytes>(m, "StreamReaderBytes", py::module_local())
&StreamingMediaDecoder::process_packet))
.def(
"process_all_packets",
&StreamingMediaDecoderFileObj::process_all_packets)
.def("fill_buffer", &StreamingMediaDecoderFileObj::fill_buffer)
.def("is_buffer_ready", &StreamingMediaDecoderFileObj::is_buffer_ready)
.def("pop_chunks", &StreamingMediaDecoderFileObj::pop_chunks);
py::class_<StreamingMediaDecoderBytes>(
m, "StreamingMediaDecoderBytes", py::module_local())
.def(py::init<
std::string_view,
const c10::optional<std::string>&,
const c10::optional<OptionDict>&,
int64_t>())
.def("num_src_streams", &StreamReaderBytes::num_src_streams)
.def("num_out_streams", &StreamReaderBytes::num_out_streams)
.def("find_best_audio_stream", &StreamReaderBytes::find_best_audio_stream)
.def("find_best_video_stream", &StreamReaderBytes::find_best_video_stream)
.def("get_metadata", &StreamReaderBytes::get_metadata)
.def("get_src_stream_info", &StreamReaderBytes::get_src_stream_info)
.def("get_out_stream_info", &StreamReaderBytes::get_out_stream_info)
.def("seek", &StreamReaderBytes::seek)
.def("add_audio_stream", &StreamReaderBytes::add_audio_stream)
.def("add_video_stream", &StreamReaderBytes::add_video_stream)
.def("remove_stream", &StreamReaderBytes::remove_stream)
.def("num_src_streams", &StreamingMediaDecoderBytes::num_src_streams)
.def("num_out_streams", &StreamingMediaDecoderBytes::num_out_streams)
.def(
"find_best_audio_stream",
&StreamingMediaDecoderBytes::find_best_audio_stream)
.def(
"find_best_video_stream",
&StreamingMediaDecoderBytes::find_best_video_stream)
.def("get_metadata", &StreamingMediaDecoderBytes::get_metadata)
.def(
"get_src_stream_info",
&StreamingMediaDecoderBytes::get_src_stream_info)
.def(
"get_out_stream_info",
&StreamingMediaDecoderBytes::get_out_stream_info)
.def("seek", &StreamingMediaDecoderBytes::seek)
.def("add_audio_stream", &StreamingMediaDecoderBytes::add_audio_stream)
.def("add_video_stream", &StreamingMediaDecoderBytes::add_video_stream)
.def("remove_stream", &StreamingMediaDecoderBytes::remove_stream)
.def(
"process_packet",
py::overload_cast<const c10::optional<double>&, const double>(
&StreamReader::process_packet))
.def("process_all_packets", &StreamReaderBytes::process_all_packets)
.def("fill_buffer", &StreamReaderBytes::fill_buffer)
.def("is_buffer_ready", &StreamReaderBytes::is_buffer_ready)
.def("pop_chunks", &StreamReaderBytes::pop_chunks);
&StreamingMediaDecoder::process_packet))
.def(
"process_all_packets",
&StreamingMediaDecoderBytes::process_all_packets)
.def("fill_buffer", &StreamingMediaDecoderBytes::fill_buffer)
.def("is_buffer_ready", &StreamingMediaDecoderBytes::is_buffer_ready)
.def("pop_chunks", &StreamingMediaDecoderBytes::pop_chunks);
}
} // namespace
......
......@@ -169,7 +169,7 @@ AVCodecContextPtr get_codec_ctx(
codec_ctx->hw_frames_ctx = get_hw_frames_ctx(codec_ctx);
}
if (ends_with(codec_ctx->codec->name, "_cuvid")) {
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamReaderCUDA");
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamingMediaDecoderCUDA");
}
return codec_ctx;
}
......@@ -197,7 +197,7 @@ KeyType StreamProcessor::add_stream(
// has decoder set without HW accel, it will cause seg fault.
// i.e.
// The following should be rejected here.
// reader = StreamReader(...)
// reader = StreamingMediaDecoder(...)
// reader.add_video_stream(..., decoder="h264_cuvid")
// reader.add_video_stream(..., decoder="h264_cuvid", hw_accel="cuda")
// TODO:
......
......@@ -50,8 +50,9 @@ AVFormatContext* get_input_format_context(
}
} // namespace
StreamReader::StreamReader(AVFormatContext* p) : format_ctx(p) {
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamReader");
StreamingMediaDecoder::StreamingMediaDecoder(AVFormatContext* p)
: format_ctx(p) {
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamingMediaDecoder");
int ret = avformat_find_stream_info(format_ctx, nullptr);
TORCH_CHECK(
ret >= 0, "Failed to find stream information: ", av_err2string(ret));
......@@ -69,21 +70,22 @@ StreamReader::StreamReader(AVFormatContext* p) : format_ctx(p) {
}
}
StreamReader::StreamReader(
StreamingMediaDecoder::StreamingMediaDecoder(
AVIOContext* io_ctx,
const c10::optional<std::string>& format,
const c10::optional<OptionDict>& option)
: StreamReader(get_input_format_context(
: StreamingMediaDecoder(get_input_format_context(
"Custom Input Context",
format,
option,
io_ctx)) {}
StreamReader::StreamReader(
StreamingMediaDecoder::StreamingMediaDecoder(
const std::string& src,
const c10::optional<std::string>& format,
const c10::optional<OptionDict>& option)
: StreamReader(get_input_format_context(src, format, option, nullptr)) {}
: StreamingMediaDecoder(
get_input_format_context(src, format, option, nullptr)) {}
//////////////////////////////////////////////////////////////////////////////
// Helper methods
......@@ -116,7 +118,7 @@ void validate_src_stream_type(
////////////////////////////////////////////////////////////////////////////////
// Query methods
////////////////////////////////////////////////////////////////////////////////
int64_t StreamReader::num_src_streams() const {
int64_t StreamingMediaDecoder::num_src_streams() const {
return format_ctx->nb_streams;
}
......@@ -131,11 +133,11 @@ OptionDict parse_metadata(const AVDictionary* metadata) {
}
} // namespace
OptionDict StreamReader::get_metadata() const {
OptionDict StreamingMediaDecoder::get_metadata() const {
return parse_metadata(format_ctx->metadata);
}
SrcStreamInfo StreamReader::get_src_stream_info(int i) const {
SrcStreamInfo StreamingMediaDecoder::get_src_stream_info(int i) const {
validate_src_stream_index(format_ctx, i);
AVStream* stream = format_ctx->streams[i];
......@@ -186,7 +188,7 @@ AVCodecParameters* get_codecpar() {
}
} // namespace
StreamParams StreamReader::get_src_stream_params(int i) {
StreamParams StreamingMediaDecoder::get_src_stream_params(int i) {
validate_src_stream_index(format_ctx, i);
AVStream* stream = format_ctx->streams[i];
......@@ -200,11 +202,11 @@ StreamParams StreamReader::get_src_stream_params(int i) {
return {std::move(codec_params), stream->time_base, i};
}
int64_t StreamReader::num_out_streams() const {
int64_t StreamingMediaDecoder::num_out_streams() const {
return static_cast<int64_t>(stream_indices.size());
}
OutputStreamInfo StreamReader::get_out_stream_info(int i) const {
OutputStreamInfo StreamingMediaDecoder::get_out_stream_info(int i) const {
TORCH_CHECK(
i >= 0 && static_cast<size_t>(i) < stream_indices.size(),
"Output stream index out of range");
......@@ -232,17 +234,17 @@ OutputStreamInfo StreamReader::get_out_stream_info(int i) const {
return ret;
}
int64_t StreamReader::find_best_audio_stream() const {
int64_t StreamingMediaDecoder::find_best_audio_stream() const {
return av_find_best_stream(
format_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
}
int64_t StreamReader::find_best_video_stream() const {
int64_t StreamingMediaDecoder::find_best_video_stream() const {
return av_find_best_stream(
format_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
}
bool StreamReader::is_buffer_ready() const {
bool StreamingMediaDecoder::is_buffer_ready() const {
if (processors.empty()) {
// If no decoding output streams exist, then determine overall readiness
// from the readiness of packet buffer.
......@@ -262,7 +264,7 @@ bool StreamReader::is_buffer_ready() const {
////////////////////////////////////////////////////////////////////////////////
// Configure methods
////////////////////////////////////////////////////////////////////////////////
void StreamReader::seek(double timestamp_s, int64_t mode) {
void StreamingMediaDecoder::seek(double timestamp_s, int64_t mode) {
TORCH_CHECK(timestamp_s >= 0, "timestamp must be non-negative.");
TORCH_CHECK(
format_ctx->nb_streams > 0,
......@@ -302,7 +304,7 @@ void StreamReader::seek(double timestamp_s, int64_t mode) {
}
}
void StreamReader::add_audio_stream(
void StreamingMediaDecoder::add_audio_stream(
int64_t i,
int64_t frames_per_chunk,
int64_t num_chunks,
......@@ -320,7 +322,7 @@ void StreamReader::add_audio_stream(
torch::Device(torch::DeviceType::CPU));
}
void StreamReader::add_video_stream(
void StreamingMediaDecoder::add_video_stream(
int64_t i,
int64_t frames_per_chunk,
int64_t num_chunks,
......@@ -355,7 +357,7 @@ void StreamReader::add_video_stream(
device);
}
void StreamReader::add_packet_stream(int i) {
void StreamingMediaDecoder::add_packet_stream(int i) {
validate_src_stream_index(format_ctx, i);
if (!packet_buffer) {
packet_buffer = std::make_unique<PacketBuffer>();
......@@ -363,7 +365,7 @@ void StreamReader::add_packet_stream(int i) {
packet_stream_indices.emplace(i);
}
void StreamReader::add_stream(
void StreamingMediaDecoder::add_stream(
int i,
AVMediaType media_type,
int frames_per_chunk,
......@@ -414,7 +416,7 @@ void StreamReader::add_stream(
stream_indices.push_back(std::make_pair<>(i, key));
}
void StreamReader::remove_stream(int64_t i) {
void StreamingMediaDecoder::remove_stream(int64_t i) {
TORCH_CHECK(
i >= 0 && static_cast<size_t>(i) < stream_indices.size(),
"Output stream index out of range");
......@@ -444,7 +446,7 @@ void StreamReader::remove_stream(int64_t i) {
// 0: caller should keep calling this function
// 1: It's done, caller should stop calling
// <0: Some error happened
int StreamReader::process_packet() {
int StreamingMediaDecoder::process_packet() {
int ret = av_read_frame(format_ctx, packet);
if (ret == AVERROR_EOF) {
ret = drain();
......@@ -475,7 +477,9 @@ int StreamReader::process_packet() {
// it keeps retrying until timeout happens,
//
// timeout and backoff is given in millisecond
int StreamReader::process_packet_block(double timeout, double backoff) {
int StreamingMediaDecoder::process_packet_block(
double timeout,
double backoff) {
auto dead_line = [&]() {
// If timeout < 0, then it repeats forever
if (timeout < 0) {
......@@ -505,14 +509,14 @@ int StreamReader::process_packet_block(double timeout, double backoff) {
}
}
void StreamReader::process_all_packets() {
void StreamingMediaDecoder::process_all_packets() {
int64_t ret = 0;
do {
ret = process_packet();
} while (!ret);
}
int StreamReader::process_packet(
int StreamingMediaDecoder::process_packet(
const c10::optional<double>& timeout,
const double backoff) {
int code = [&]() -> int {
......@@ -526,7 +530,7 @@ int StreamReader::process_packet(
return code;
}
int StreamReader::fill_buffer(
int StreamingMediaDecoder::fill_buffer(
const c10::optional<double>& timeout,
const double backoff) {
while (!is_buffer_ready()) {
......@@ -539,7 +543,7 @@ int StreamReader::fill_buffer(
}
// <0: Some error happened.
int StreamReader::drain() {
int StreamingMediaDecoder::drain() {
int ret = 0, tmp = 0;
for (auto& p : processors) {
if (p) {
......@@ -552,7 +556,7 @@ int StreamReader::drain() {
return ret;
}
std::vector<c10::optional<Chunk>> StreamReader::pop_chunks() {
std::vector<c10::optional<Chunk>> StreamingMediaDecoder::pop_chunks() {
std::vector<c10::optional<Chunk>> ret;
ret.reserve(static_cast<size_t>(num_out_streams()));
for (auto& i : stream_indices) {
......@@ -561,12 +565,12 @@ std::vector<c10::optional<Chunk>> StreamReader::pop_chunks() {
return ret;
}
std::vector<AVPacketPtr> StreamReader::pop_packets() {
std::vector<AVPacketPtr> StreamingMediaDecoder::pop_packets() {
return packet_buffer->pop_packets();
}
//////////////////////////////////////////////////////////////////////////////
// StreamReaderCustomIO
// StreamingMediaDecoderCustomIO
//////////////////////////////////////////////////////////////////////////////
namespace detail {
......@@ -596,7 +600,7 @@ CustomInput::CustomInput(
: io_ctx(get_io_context(opaque, buffer_size, read_packet, seek)) {}
} // namespace detail
StreamReaderCustomIO::StreamReaderCustomIO(
StreamingMediaDecoderCustomIO::StreamingMediaDecoderCustomIO(
void* opaque,
const c10::optional<std::string>& format,
int buffer_size,
......@@ -604,6 +608,6 @@ StreamReaderCustomIO::StreamReaderCustomIO(
int64_t (*seek)(void* opaque, int64_t offset, int whence),
const c10::optional<OptionDict>& option)
: CustomInput(opaque, buffer_size, read_packet, seek),
StreamReader(io_ctx, format, option) {}
StreamingMediaDecoder(io_ctx, format, option) {}
} // namespace torio::io
......@@ -9,13 +9,13 @@ namespace torio {
namespace io {
//////////////////////////////////////////////////////////////////////////////
// StreamReader
// StreamingMediaDecoder
//////////////////////////////////////////////////////////////////////////////
///
/// Fetch and decode audio/video streams chunk by chunk.
///
class StreamReader {
class StreamingMediaDecoder {
AVFormatInputContextPtr format_ctx;
AVPacketPtr packet{alloc_avpacket()};
......@@ -49,13 +49,13 @@ class StreamReader {
/// @cond
private:
/// Construct StreamReader from already initialized AVFormatContext.
/// Construct StreamingMediaDecoder from already initialized AVFormatContext.
/// This is a low level constructor interact with FFmpeg directly.
/// One can provide custom AVFormatContext in case the other constructor
/// does not meet a requirement.
/// @param format_ctx An initialized AVFormatContext. StreamReader will
/// own the resources and release it at the end.
explicit StreamReader(AVFormatContext* format_ctx);
/// @param format_ctx An initialized AVFormatContext. StreamingMediaDecoder
/// will own the resources and release it at the end.
explicit StreamingMediaDecoder(AVFormatContext* format_ctx);
protected:
/// Concstruct media processor from custom IO.
......@@ -64,7 +64,7 @@ class StreamReader {
/// @param format Specifies format, such as mp4.
/// @param option Custom option passed when initializing format context
/// (opening source).
explicit StreamReader(
explicit StreamingMediaDecoder(
AVIOContext* io_ctx,
const c10::optional<std::string>& format = c10::nullopt,
const c10::optional<OptionDict>& option = c10::nullopt);
......@@ -79,7 +79,7 @@ class StreamReader {
/// avfoundation)
/// @param option Custom option passed when initializing format context
/// (opening source).
explicit StreamReader(
explicit StreamingMediaDecoder(
const std::string& src,
const c10::optional<std::string>& format = c10::nullopt,
const c10::optional<OptionDict>& option = c10::nullopt);
......@@ -88,13 +88,13 @@ class StreamReader {
/// @cond
~StreamReader() = default;
~StreamingMediaDecoder() = default;
// Non-copyable
StreamReader(const StreamReader&) = delete;
StreamReader& operator=(const StreamReader&) = delete;
StreamingMediaDecoder(const StreamingMediaDecoder&) = delete;
StreamingMediaDecoder& operator=(const StreamingMediaDecoder&) = delete;
// Movable
StreamReader(StreamReader&&) = default;
StreamReader& operator=(StreamReader&&) = default;
StreamingMediaDecoder(StreamingMediaDecoder&&) = default;
StreamingMediaDecoder& operator=(StreamingMediaDecoder&&) = default;
/// @endcond
......@@ -188,8 +188,8 @@ class StreamReader {
///
/// In addition to decoder-specific options, you can also pass options
/// related to multithreading. They are effective only if the decoder
/// supports them. If neither of them are provided, StreamReader defaults to
/// single thread.
/// supports them. If neither of them are provided, StreamingMediaDecoder
/// defaults to single thread.
/// - ``"threads"``: The number of threads or the value ``"0"``
/// to let FFmpeg decide based on its heuristics.
/// - ``"thread_type"``: Which multithreading method to use.
......@@ -217,8 +217,8 @@ class StreamReader {
/// @parblock
/// When video is decoded on CUDA hardware, (for example by specifying
/// `"h264_cuvid"` decoder), passing CUDA device indicator to ``hw_accel``
/// (i.e. ``hw_accel="cuda:0"``) will make StreamReader place the resulting
/// frames directly on the specified CUDA device as a CUDA tensor.
/// (i.e. ``hw_accel="cuda:0"``) will make StreamingMediaDecoder place the
/// resulting frames directly on the specified CUDA device as a CUDA tensor.
///
/// If `None`, the chunk will be moved to CPU memory.
/// @endparblock
......@@ -234,7 +234,7 @@ class StreamReader {
/// @cond
/// Add a output packet stream.
/// Allows for passing packets directly from the source stream, bypassing
/// the decode path, to ``StreamWriter`` for remuxing.
/// the decode path, to ``StreamingMediaEncoder`` for remuxing.
///
/// @param i The index of the source stream.
void add_packet_stream(int i);
......@@ -341,7 +341,7 @@ class StreamReader {
};
//////////////////////////////////////////////////////////////////////////////
// StreamReaderCustomIO
// StreamingMediaDecoderCustomIO
//////////////////////////////////////////////////////////////////////////////
/// @cond
......@@ -360,13 +360,14 @@ struct CustomInput {
/// @endcond
///
/// A subclass of StreamReader which works with custom read function.
/// A subclass of StreamingMediaDecoder which works with custom read function.
/// Can be used for decoding media from memory or custom object.
///
class StreamReaderCustomIO : private detail::CustomInput, public StreamReader {
class StreamingMediaDecoderCustomIO : private detail::CustomInput,
public StreamingMediaDecoder {
public:
///
/// Construct StreamReader with custom read and seek functions.
/// Construct StreamingMediaDecoder with custom read and seek functions.
///
/// @param opaque Custom data used by ``read_packet`` and ``seek`` functions.
/// @param format Specify input format.
......@@ -376,7 +377,7 @@ class StreamReaderCustomIO : private detail::CustomInput, public StreamReader {
/// read data from the destination.
/// @param seek Optional seek function that is used to seek the destination.
/// @param option Custom option passed when initializing format context.
StreamReaderCustomIO(
StreamingMediaDecoderCustomIO(
void* opaque,
const c10::optional<std::string>& format,
int buffer_size,
......@@ -385,6 +386,10 @@ class StreamReaderCustomIO : private detail::CustomInput, public StreamReader {
const c10::optional<OptionDict>& option = c10::nullopt);
};
// For BC
using StreamReader = StreamingMediaDecoder;
using StreamReaderCustomIO = StreamingMediaDecoderCustomIO;
} // namespace io
} // namespace torio
......
......@@ -827,8 +827,8 @@ EncodeProcess get_audio_encode_process(
// 8. encoder
// Note: get_stream modifies AVFormatContext and adds new stream.
// If anything after this throws, it will leave the StreamWriter in an
// invalid state.
// If anything after this throws, it will leave the StreamingMediaEncoder in
// an invalid state.
Encoder enc{format_ctx, codec_ctx, get_stream(format_ctx, codec_ctx)};
return EncodeProcess{
......@@ -925,7 +925,7 @@ EncodeProcess get_video_encode_process(
open_codec(codec_ctx, encoder_option);
if (ends_with(codec_ctx->codec->name, "_nvenc")) {
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamReaderCUDA");
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamingMediaDecoderCUDA");
}
// 5. Build filter graph
......@@ -961,8 +961,8 @@ EncodeProcess get_video_encode_process(
// 8. encoder
// Note: get_stream modifies AVFormatContext and adds new stream.
// If anything after this throws, it will leave the StreamWriter in an
// invalid state.
// If anything after this throws, it will leave the StreamingMediaEncoder in
// an invalid state.
Encoder enc{format_ctx, codec_ctx, get_stream(format_ctx, codec_ctx)};
return EncodeProcess{
......
......@@ -30,7 +30,7 @@ void Encoder::encode(AVFrame* frame) {
// Possible follow up: Add flush_buffer method?
// An alternative is to use `av_write_frame` functoin, but in that case
// client code is responsible for ordering packets, which makes it
// complicated to use StreamWriter
// complicated to use StreamingMediaEncoder
ret = av_interleaved_write_frame(format_ctx, nullptr);
TORCH_CHECK(
ret >= 0, "Failed to flush packet (", av_err2string(ret), ").");
......
......@@ -38,22 +38,23 @@ AVFormatContext* get_output_format_context(
}
} // namespace
StreamWriter::StreamWriter(AVFormatContext* p) : format_ctx(p) {
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamWriter");
StreamingMediaEncoder::StreamingMediaEncoder(AVFormatContext* p)
: format_ctx(p) {
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamingMediaEncoder");
}
StreamWriter::StreamWriter(
StreamingMediaEncoder::StreamingMediaEncoder(
AVIOContext* io_ctx,
const c10::optional<std::string>& format)
: StreamWriter(
: StreamingMediaEncoder(
get_output_format_context("Custom Output Context", format, io_ctx)) {}
StreamWriter::StreamWriter(
StreamingMediaEncoder::StreamingMediaEncoder(
const std::string& dst,
const c10::optional<std::string>& format)
: StreamWriter(get_output_format_context(dst, format, nullptr)) {}
: StreamingMediaEncoder(get_output_format_context(dst, format, nullptr)) {}
void StreamWriter::add_audio_stream(
void StreamingMediaEncoder::add_audio_stream(
int sample_rate,
int num_channels,
const std::string& format,
......@@ -86,7 +87,7 @@ void StreamWriter::add_audio_stream(
current_key++;
}
void StreamWriter::add_video_stream(
void StreamingMediaEncoder::add_video_stream(
double frame_rate,
int width,
int height,
......@@ -125,7 +126,8 @@ void StreamWriter::add_video_stream(
current_key++;
}
void StreamWriter::add_packet_stream(const StreamParams& stream_params) {
void StreamingMediaEncoder::add_packet_stream(
const StreamParams& stream_params) {
packet_writers.emplace(
std::piecewise_construct,
std::forward_as_tuple(stream_params.stream_index),
......@@ -133,7 +135,7 @@ void StreamWriter::add_packet_stream(const StreamParams& stream_params) {
current_key++;
}
void StreamWriter::add_audio_frame_stream(
void StreamingMediaEncoder::add_audio_frame_stream(
int sample_rate,
int num_channels,
const std::string& format,
......@@ -167,7 +169,7 @@ void StreamWriter::add_audio_frame_stream(
current_key++;
}
void StreamWriter::add_video_frame_stream(
void StreamingMediaEncoder::add_video_frame_stream(
double frame_rate,
int width,
int height,
......@@ -207,18 +209,18 @@ void StreamWriter::add_video_frame_stream(
current_key++;
}
void StreamWriter::set_metadata(const OptionDict& metadata) {
void StreamingMediaEncoder::set_metadata(const OptionDict& metadata) {
av_dict_free(&format_ctx->metadata);
for (auto const& [key, value] : metadata) {
av_dict_set(&format_ctx->metadata, key.c_str(), value.c_str(), 0);
}
}
void StreamWriter::dump_format(int64_t i) {
void StreamingMediaEncoder::dump_format(int64_t i) {
av_dump_format(format_ctx, (int)i, format_ctx->url, 1);
}
void StreamWriter::open(const c10::optional<OptionDict>& option) {
void StreamingMediaEncoder::open(const c10::optional<OptionDict>& option) {
TORCH_INTERNAL_ASSERT(
format_ctx->nb_streams == num_output_streams(),
"The number of encode process and the number of output streams do not match.");
......@@ -257,7 +259,7 @@ void StreamWriter::open(const c10::optional<OptionDict>& option) {
is_open = true;
}
void StreamWriter::close() {
void StreamingMediaEncoder::close() {
int ret = av_write_trailer(format_ctx);
if (ret < 0) {
LOG(WARNING) << "Failed to write trailer. (" << av_err2string(ret) << ").";
......@@ -274,7 +276,7 @@ void StreamWriter::close() {
is_open = false;
}
void StreamWriter::write_audio_chunk(
void StreamingMediaEncoder::write_audio_chunk(
int i,
const torch::Tensor& waveform,
const c10::optional<double>& pts) {
......@@ -293,7 +295,7 @@ void StreamWriter::write_audio_chunk(
processes.at(i).process(waveform, pts);
}
void StreamWriter::write_video_chunk(
void StreamingMediaEncoder::write_video_chunk(
int i,
const torch::Tensor& frames,
const c10::optional<double>& pts) {
......@@ -312,7 +314,7 @@ void StreamWriter::write_video_chunk(
processes.at(i).process(frames, pts);
}
void StreamWriter::write_packet(const AVPacketPtr& packet) {
void StreamingMediaEncoder::write_packet(const AVPacketPtr& packet) {
TORCH_CHECK(is_open, "Output is not opened. Did you call `open` method?");
int src_stream_index = packet->stream_index;
TORCH_CHECK(
......@@ -322,7 +324,7 @@ void StreamWriter::write_packet(const AVPacketPtr& packet) {
packet_writers.at(src_stream_index).write_packet(packet);
}
void StreamWriter::write_frame(int i, AVFrame* frame) {
void StreamingMediaEncoder::write_frame(int i, AVFrame* frame) {
TORCH_CHECK(is_open, "Output is not opened. Did you call `open` method?");
TORCH_CHECK(
0 <= i && i < static_cast<int>(format_ctx->nb_streams),
......@@ -333,19 +335,19 @@ void StreamWriter::write_frame(int i, AVFrame* frame) {
processes.at(i).process_frame(frame);
}
void StreamWriter::flush() {
void StreamingMediaEncoder::flush() {
TORCH_CHECK(is_open, "Output is not opened. Did you call `open` method?");
for (auto& p : processes) {
p.second.flush();
}
}
int StreamWriter::num_output_streams() {
int StreamingMediaEncoder::num_output_streams() {
return static_cast<int>(processes.size() + packet_writers.size());
}
////////////////////////////////////////////////////////////////////////////////
// StreamWriterCustomIO
// StreamingMediaEncoderCustomIO
////////////////////////////////////////////////////////////////////////////////
namespace detail {
......@@ -375,14 +377,14 @@ CustomOutput::CustomOutput(
: io_ctx(get_io_context(opaque, buffer_size, write_packet, seek)) {}
} // namespace detail
StreamWriterCustomIO::StreamWriterCustomIO(
StreamingMediaEncoderCustomIO::StreamingMediaEncoderCustomIO(
void* opaque,
const c10::optional<std::string>& format,
int buffer_size,
int (*write_packet)(void* opaque, uint8_t* buf, int buf_size),
int64_t (*seek)(void* opaque, int64_t offset, int whence))
: CustomOutput(opaque, buffer_size, write_packet, seek),
StreamWriter(io_ctx, format) {}
StreamingMediaEncoder(io_ctx, format) {}
} // namespace io
} // namespace torio
......@@ -11,13 +11,13 @@ namespace torio {
namespace io {
////////////////////////////////////////////////////////////////////////////////
// StreamWriter
// StreamingMediaEncoder
////////////////////////////////////////////////////////////////////////////////
///
/// Encode and write audio/video streams chunk by chunk
///
class StreamWriter {
class StreamingMediaEncoder {
AVFormatOutputContextPtr format_ctx;
std::map<int, EncodeProcess> processes;
std::map<int, PacketWriter> packet_writers;
......@@ -29,32 +29,32 @@ class StreamWriter {
/// @cond
private:
explicit StreamWriter(AVFormatContext*);
explicit StreamingMediaEncoder(AVFormatContext*);
protected:
/// Construct StreamWriter from custom IO
/// Construct StreamingMediaEncoder from custom IO
///
/// @param io_ctx Custom IO.
/// @param format Specify output format.
explicit StreamWriter(
explicit StreamingMediaEncoder(
AVIOContext* io_ctx,
const c10::optional<std::string>& format = c10::nullopt);
/// @endcond
public:
/// Construct StreamWriter from destination URI
/// Construct StreamingMediaEncoder from destination URI
///
/// @param dst Destination where encoded data are written.
/// @param format Specify output format. If not provided, it is guessed from
/// ``dst``.
explicit StreamWriter(
explicit StreamingMediaEncoder(
const std::string& dst,
const c10::optional<std::string>& format = c10::nullopt);
// Non-copyable
StreamWriter(const StreamWriter&) = delete;
StreamWriter& operator=(const StreamWriter&) = delete;
StreamingMediaEncoder(const StreamingMediaEncoder&) = delete;
StreamingMediaEncoder& operator=(const StreamingMediaEncoder&) = delete;
//////////////////////////////////////////////////////////////////////////////
// Query methods
......@@ -149,7 +149,7 @@ class StreamWriter {
/// @parblock
/// When video is encoded on CUDA hardware, for example
/// `encoder="h264_nvenc"`, passing CUDA device indicator to `hw_accel`
/// (i.e. `hw_accel="cuda:0"`) will make StreamWriter expect video
/// (i.e. `hw_accel="cuda:0"`) will make StreamingMediaEncoder expect video
/// chunk to be a CUDA Tensor. Passing CPU Tensor will result in an error.
///
/// If `None`, the video chunk Tensor has to be a CPU Tensor.
......@@ -207,10 +207,10 @@ class StreamWriter {
const c10::optional<std::string>& filter_desc = c10::nullopt);
/// Add packet stream. Intended to be used in conjunction with
/// ``StreamReader`` to perform packet passthrough.
/// ``StreamingMediaDecoder`` to perform packet passthrough.
/// @param stream_params Stream parameters returned by
/// ``StreamReader::get_src_stream_params()`` for the packet stream to pass
/// through.
/// ``StreamingMediaDecoder::get_src_stream_params()`` for the packet stream
/// to pass through.
void add_packet_stream(const StreamParams& stream_params);
/// @endcond
......@@ -276,7 +276,7 @@ class StreamWriter {
/// @param frame Frame to write.
void write_frame(int i, AVFrame* frame);
/// Write packet.
/// @param packet Packet to write, passed from ``StreamReader``.
/// @param packet Packet to write, passed from ``StreamingMediaDecoder``.
void write_packet(const AVPacketPtr& packet);
/// @endcond
......@@ -288,7 +288,7 @@ class StreamWriter {
};
////////////////////////////////////////////////////////////////////////////////
// StreamWriterCustomIO
// StreamingMediaEncoderCustomIO
////////////////////////////////////////////////////////////////////////////////
/// @cond
......@@ -307,12 +307,14 @@ struct CustomOutput {
/// @endcond
///
/// A subclass of StreamReader which works with custom read function.
/// A subclass of StreamingMediaDecoder which works with custom read function.
/// Can be used for encoding media into memory or custom object.
///
class StreamWriterCustomIO : private detail::CustomOutput, public StreamWriter {
class StreamingMediaEncoderCustomIO : private detail::CustomOutput,
public StreamingMediaEncoder {
public:
/// Construct StreamWriterCustomIO with custom write and seek functions.
/// Construct StreamingMediaEncoderCustomIO with custom write and seek
/// functions.
///
/// @param opaque Custom data used by ``write_packet`` and ``seek`` functions.
/// @param format Specify output format.
......@@ -321,7 +323,7 @@ class StreamWriterCustomIO : private detail::CustomOutput, public StreamWriter {
/// @param write_packet Custom write function that is called from FFmpeg to
/// actually write data to the custom destination.
/// @param seek Optional seek function that is used to seek the destination.
StreamWriterCustomIO(
StreamingMediaEncoderCustomIO(
void* opaque,
const c10::optional<std::string>& format,
int buffer_size,
......@@ -329,6 +331,10 @@ class StreamWriterCustomIO : private detail::CustomOutput, public StreamWriter {
int64_t (*seek)(void* opaque, int64_t offset, int whence) = nullptr);
};
// For BC
using StreamWriter = StreamingMediaEncoder;
using StreamWriterCustomIO = StreamingMediaEncoderCustomIO;
} // namespace io
} // namespace torio
......
......@@ -519,11 +519,11 @@ class StreamingMediaDecoder:
):
self.src = src
if isinstance(src, bytes):
self._be = ffmpeg_ext.StreamReaderBytes(src, format, option, buffer_size)
self._be = ffmpeg_ext.StreamingMediaDecoderBytes(src, format, option, buffer_size)
elif hasattr(src, "read"):
self._be = ffmpeg_ext.StreamReaderFileObj(src, format, option, buffer_size)
self._be = ffmpeg_ext.StreamingMediaDecoderFileObj(src, format, option, buffer_size)
else:
self._be = ffmpeg_ext.StreamReader(os.path.normpath(src), format, option)
self._be = ffmpeg_ext.StreamingMediaDecoder(os.path.normpath(src), format, option)
i = self._be.find_best_audio_stream()
self._default_audio_stream = None if i < 0 else i
......
......@@ -194,9 +194,9 @@ class StreamingMediaEncoder:
buffer_size: int = 4096,
):
if hasattr(dst, "write"):
self._s = ffmpeg_ext.StreamWriterFileObj(dst, format, buffer_size)
self._s = ffmpeg_ext.StreamingMediaEncoderFileObj(dst, format, buffer_size)
else:
self._s = ffmpeg_ext.StreamWriter(str(dst), format)
self._s = ffmpeg_ext.StreamingMediaEncoder(str(dst), format)
self._is_open = False
@_format_common_args
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment