目录

警告

TorchAudio 的 C++ API 是一个原型功能。 不保证 API/ABI 向后兼容性。

注意

顶级命名空间已从 更改为 。 已重命名为 。torchaudiotorioStreamReaderStreamingMediaDecoder

torio::io::StreamingMediaDecoder

StreamingMediaDecoder是 Python 等效项使用的实现,并提供类似的接口。 当使用自定义 I/O (如内存数据) 时,可以使用 class 。StreamingMediaDecoderCustomIO

这两个类都定义了相同的方法,因此它们的用法是相同的。

构造 函数

StreamingMedia解码器

StreamingMediaDecoder

逐块获取和解码音频/视频流。

torio::io::StreamingMediaDecoderCustomIO 子类化

警告

doxygen函数:无法在项目“libtorio”的 doxygen xml 输出中解析函数“torio::io::StreamingMediaDecoder::StreamingMediaDecoder””,该函数具有参数(const std::string&、const std::optional<std::string>&、const c10::optional<OptionDict>&)来自目录:cpp/xml。 可能的匹配项:

- StreamingMediaDecoder(const std::string &src, const std::optional<std::string> &format = c10::nullopt, const std::optional<OptionDict> &option = c10::nullopt)

StreamingMediaDecoderCustomIO

StreamingMediaDecoderCustomIO 私有 detail::CustomInput public torio::io::StreamingMedia解码器

StreamingMediaDecoder 的子类,与自定义读取函数一起使用。可用于从内存或自定义对象解码媒体。

torio::io::StreamingMediaDecoderCustomIO::StreamingMediaDecoderCustomIOvoid *opaqueconst std::可选<std::string> &formatint buffer_sizeint *read_packetvoid *opaque uint8_t *buf int buf_sizeint64_t *seek)(void *opaque int64_t offset, int whence = nullptrconst std::optional<OptionDict> &option = c10::nullopt)

使用自定义读取和查找函数构造 StreamingMediaDecoder

参数
  • opaque (不透明) – 函数使用的自定义数据。read_packetseek

  • format (格式) – 指定输入格式。

  • buffer_size – 中间缓冲区的大小,FFmpeg 用于将数据传递给函数 read_packet。

  • read_packet – 从 FFmpeg 调用的自定义读取函数,用于从目标读取数据。

  • seek – 用于查找目标的可选 seek 函数。

  • option – 初始化格式上下文时传递的自定义选项。

查询方法

find_best_audio_stream

int64_t torio::io::StreamingMediaDecoder::find_best_audio_stream() 常量

使用 ffmpeg 中的启发式方法找到合适的音频流。

如果成功,则返回最佳流的索引 (>=0)。否则,将返回负值。

find_best_video_stream

int64_t torio::io::StreamingMediaDecoder::find_best_video_stream() const

使用 ffmpeg 中的启发式方法找到合适的视频流。

如果成功,则返回最佳流的索引 (0>=)。否则返回负值。

get_metadata

OptionDict torio::io::StreamingMediaDecoder::get_metadata 常量

获取源媒体的元数据。

num_src_streams

int64_t torio::io::StreamingMediaDecoder::num_src_streams const

获取在输入媒体中找到的源流数。

源流不仅包括音频/视频流,还包括字幕等。

get_src_stream_info

SrcStreamInfo torio::io::StreamingMediaDecoder::get_src_stream_infoint i const

获取指定源流的信息。

有效值范围为 。[0, num_src_streams())

num_out_streams

int64_t torio::io::StreamingMediaDecoder::num_out_streams() const

获取客户端代码定义的输出流数。

get_out_stream_info

OutputStreamInfo torio::io::StreamingMediaDecoder::get_out_stream_infoint i const

获取有关指定输出流的信息。

有效值范围为 。[0, num_out_streams())

is_buffer_ready

bool torio::io::StreamingMediaDecoder::is_buffer_ready() const

检查输出流的所有缓冲区是否都有足够的解码帧。

配置方法

add_audio_stream

void torio::io::StreamingMediaDecoder::add_audio_streamint64_t iint64_t frames_per_chunkint64_t num_chunks,const std::optional<std::string> &filter_desc = c10::nulloptconst std::optional<std::string> & 解码器 = c10::nulloptconst std::optional<OptionDict> &decoder_option = c10::nullopt)

定义输出音频流。

参数
  • i – 源流的索引。

  • frames_per_chunk – 作为一个区块返回的帧数。

    如果源流在缓冲帧之前耗尽,则块将按原样返回。因此,块中的帧数可能小于 ''。frames_per_chunkframes_per_chunk

    提供会禁用分块,在这种情况下,method 会将所有缓冲帧作为一个块返回。-1pop_chunks()

  • num_chunks – 内部缓冲区大小。

    当缓冲的 chunk 数量超过此数量时,将丢弃旧的 chunk。例如,如果为 5 且为 3,则会丢弃早于 15 的帧。frames_per_chunkbuffer_chunk_size

    提供会禁用此行为,强制保留所有块。-1

  • filter_desc – 应用于源流的筛选条件图的描述。

  • decoder – 要使用的解码器的名称。如果提供,请使用指定的解码器,而不是默认的解码器。

  • decoder_option – 传递给 decoder 的选项。

    要列出解码器的解码器选项,您可以使用 command。ffmpeg -h decoder=<DECODER>

    除了特定于 decoder 的选项外,您还可以传递与多线程相关的选项。只有当解码器支持它们时,它们才有效。如果未提供这两个选项,则 StreamingMediaDecoder 默认为单线程。

    • "threads":线程数或让 FFmpeg 根据其启发式方法决定的值。"0"

    • "thread_type":要使用的多线程方法。有效值为 或 。请注意,每个解码器都支持一组不同的方法。如果未提供,则使用默认值。"frame""slice"

      • "frame":一次解码多个帧。每个线程处理一个帧。这将使解码延迟每个线程增加一帧

      • "slice":一次解码单个帧的多个部分。

add_video_stream

void torio::io::StreamingMediaDecoder::add_video_streamint64_t iint64_t frames_per_chunkint64_t num_chunks,const std::optional<std::string> &filter_desc = c10::nulloptconst std::optional<std::string> & 解码器 = c10::nulloptconst std::optional<OptionDict> &decoder_option = c10::nulloptconst std::optional<std::string> &hw_accel = c10::nullopt)

定义输出视频流。

参数
  • i、frames_per_chunk、num_chunks、filter_desc、解码器decoder_option – 见 。add_audio_stream()

  • hw_accel – 启用硬件加速。

    当视频在 CUDA 硬件上解码时(例如,通过指定解码器),将 CUDA 设备指示符传递给(即)将使 StreamingMediaDecoder 将结果帧作为 CUDA 张量直接放置在指定的 CUDA 设备上。"h264_cuvid"hw_accelhw_accel="cuda:0"

    如果 ,则块将移动到 CPU 内存。None

remove_stream

无效 torio::io::StreamingMediaDecoder::remove_streamint64_t i)

删除输出流。

参数

i – 要删除的输出流的索引。有效值范围为 。[0, num_out_streams())

Stream 方法

寻求

void torio::io::StreamingMediaDecoder::seekdouble timestampint64_t mode)

查找给定的时间戳。

参数
  • timestamp – 目标时间戳(以秒为单位)。

  • mode (模式) – Seek (查找) 模式。

    • 0:关键帧模式。在给定时间戳之前查找最近的关键帧。

    • 1:任何模式。在给定时间戳之前查找任何帧(包括非关键帧)。

    • 2:精确模式。首先查找给定时间戳之前最近的关键帧,然后解码帧,直到它到达最接近给定时间戳的帧。

process_packet

int torio::io::StreamingMediaDecoder::process_packet()

解复用并处理一个数据包。

结果

  • 0:数据包处理成功,但流中仍有数据包,因此客户端代码可以再次调用该方法。

  • 1:数据包处理成功并到达 EOF。客户端代码不应再次调用此方法。

  • <0:发生错误。

process_packet_block

int torio::io::StreamingMediaDecoder::process_packet_blockconst double timeoutconst 回退)

与 类似,但如果由于资源暂时不可用而失败,它会自动重试。process_packet()

当使用设备输入(例如麦克风)时,此行为非常有用,在此期间,缓冲区可能会在进行样本采集时繁忙。

参数
  • timeout – 超时(以毫秒为单位)。

    • >=0:继续重试,直到给定的时间过去。

    • <0:永远重试。

  • backoff – 重试前等待的时间(以毫秒为单位)。

process_all_packets

无效 torio::io::StreamingMediaDecoder::process_all_packets()

Process packets unitl EOF.

fill_buffer

int torio::io::StreamingMediaDecoder::fill_bufferconst std::optional<double > &timeout = c10::nulloptconst double backoff = 10.)

处理数据包,直到所有块缓冲区都至少有一个块

参数

检索方法

pop_chunks

std::vector<std::optional<Chunk>> torio::io::StreamingMediaDecoder::pop_chunks()

如果可用,请从每个输出流中弹出一个 chunk。

支撑结构

struct

存储解码的帧和元数据。

公共会员

torch::Tensor

音频/视频帧。

对于音频,形状为 ,取决于输出流配置。[time, num_channels]dtype

对于视频,形状为 ,形状为 ,形状为 。[time, channel, height, width]dtypetorch.uint8

双倍 Pts

第一帧的演示文稿时间戳,以秒为单位。

SrcStreaminfo

struct SrcStreamInfo

有关在输入媒体中找到的源流的信息。

普通成员

AVMediaType media_type

流媒体类型。

请参阅 FFmpeg 文档了解可用值

待办事项:

引入自己的枚举并摆脱 FFmpeg 依赖

const char *codec_name = “N/A”

编解码器的名称。

const char *codec_long_name = “N/A”

编解码器的名称,采用较长的、人类友好的形式。

const char *fmt_name = “N/A”

对于音频,它是示例格式。

常见的值是;

  • "u8", : 8 位无符号整数。"u8p"

  • "s16", : 16 位有符号整数。"s16p"

  • "s32", : 32 位有符号整数。"s32p"

  • "s64", : 64 位有符号整数。"s64p"

  • "flt", : 32 位浮点数。"fltp"

  • "dbl", : 64 位浮点数。"dblp"

对于视频,它是颜色通道格式。

常见的值包括;

  • "gray8": 灰度

  • "rgb24":RGB

  • "bgr24": BGR

  • "yuv420p": YUV420p

int64_t bit_rate = 0

比特率。

int64_t num_frames = 0

帧数。

注意

在某些格式中,该值不可靠或不不可用。

int bits_per_sample = 0

每个样本的位数。

OptionDict 元数据 = {}

元数据

此方法可以从 MP3 中获取 ID3 标签。

例:

{
  "title": "foo",
  "artist": "bar",
  "date": "2017"
}

特定于音频的成员

sample_rate = 0

采样率。

int num_channels = 0

通道数。

特定于视频的成员

int 宽度 = 0

宽度。

int 高度 = 0

高度。

frame_rate = 0

帧速率。

输出流信息

struct OutputStreamInfo

有关用户代码配置的输出流的信息。

特定于音频的成员

sample_rate = -1

采样率。

int num_channels = -1

通道数。

特定于视频的成员

int 宽度 = -1

宽度。

int 高度 = -1

高度。

AVRational frame_rate = {0 1}

帧速率。

公共会员

int source_index

输入源流的索引。

AVMediaType media_type = AVMEDIA_TYPE_UNKNOWN

流媒体类型。

请参阅 FFmpeg 文档了解可用值

待办事项:

引入自己的枚举并摆脱 FFmpeg 依赖

int 格式 = -1

媒体格式。AVSampleFormat 用于音频,AVPixelFormat 用于视频。

std::string filter_description = {}

筛选图形定义,如 ."aresample=16000,aformat=sample_fmts=fltp"

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源