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