Commit a6602715 authored by Moto Hira's avatar Moto Hira Committed by Facebook GitHub Bot
Browse files

Get rid of (pseudo) hungarian notation (#3255)

Summary:
Pull Request resolved: https://github.com/pytorch/audio/pull/3255

Prefixing what is always pointer with `p` does not improve readability...

Reviewed By: hwangjeff

Differential Revision: D44799531

fbshipit-source-id: bc2ce4e534009e2cb577719953207ddb82cf2d3d
parent 631bcc9f
......@@ -51,21 +51,21 @@ AVFormatContext* get_input_format_context(
}
} // namespace
StreamReader::StreamReader(AVFormatContext* p) : pFormatContext(p) {
StreamReader::StreamReader(AVFormatContext* p) : format_ctx(p) {
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamReader");
int ret = avformat_find_stream_info(pFormatContext, nullptr);
int ret = avformat_find_stream_info(format_ctx, nullptr);
TORCH_CHECK(
ret >= 0, "Failed to find stream information: ", av_err2string(ret));
processors =
std::vector<std::unique_ptr<StreamProcessor>>(pFormatContext->nb_streams);
for (int i = 0; i < pFormatContext->nb_streams; ++i) {
switch (pFormatContext->streams[i]->codecpar->codec_type) {
std::vector<std::unique_ptr<StreamProcessor>>(format_ctx->nb_streams);
for (int i = 0; i < format_ctx->nb_streams; ++i) {
switch (format_ctx->streams[i]->codecpar->codec_type) {
case AVMEDIA_TYPE_AUDIO:
case AVMEDIA_TYPE_VIDEO:
break;
default:
pFormatContext->streams[i]->discard = AVDISCARD_ALL;
format_ctx->streams[i]->discard = AVDISCARD_ALL;
}
}
}
......@@ -118,7 +118,7 @@ void validate_src_stream_type(
// Query methods
////////////////////////////////////////////////////////////////////////////////
int64_t StreamReader::num_src_streams() const {
return pFormatContext->nb_streams;
return format_ctx->nb_streams;
}
namespace {
......@@ -133,13 +133,13 @@ OptionDict parse_metadata(const AVDictionary* metadata) {
} // namespace
OptionDict StreamReader::get_metadata() const {
return parse_metadata(pFormatContext->metadata);
return parse_metadata(format_ctx->metadata);
}
SrcStreamInfo StreamReader::get_src_stream_info(int i) const {
validate_src_stream_index(pFormatContext, i);
validate_src_stream_index(format_ctx, i);
AVStream* stream = pFormatContext->streams[i];
AVStream* stream = format_ctx->streams[i];
AVCodecParameters* codecpar = stream->codecpar;
SrcStreamInfo ret;
......@@ -188,8 +188,8 @@ AVCodecParameters* get_codecpar() {
} // namespace
StreamParams StreamReader::get_src_stream_params(int i) {
validate_src_stream_index(pFormatContext, i);
AVStream* stream = pFormatContext->streams[i];
validate_src_stream_index(format_ctx, i);
AVStream* stream = format_ctx->streams[i];
AVCodecParametersPtr codec_params(get_codecpar());
int ret = avcodec_parameters_copy(codec_params, stream->codecpar);
......@@ -235,12 +235,12 @@ OutputStreamInfo StreamReader::get_out_stream_info(int i) const {
int64_t StreamReader::find_best_audio_stream() const {
return av_find_best_stream(
pFormatContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
format_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
}
int64_t StreamReader::find_best_video_stream() const {
return av_find_best_stream(
pFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
format_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
}
bool StreamReader::is_buffer_ready() const {
......@@ -266,7 +266,7 @@ bool StreamReader::is_buffer_ready() const {
void StreamReader::seek(double timestamp_s, int64_t mode) {
TORCH_CHECK(timestamp_s >= 0, "timestamp must be non-negative.");
TORCH_CHECK(
pFormatContext->nb_streams > 0,
format_ctx->nb_streams > 0,
"At least one stream must exist in this context");
int64_t timestamp_av_tb = static_cast<int64_t>(timestamp_s * AV_TIME_BASE);
......@@ -289,7 +289,7 @@ void StreamReader::seek(double timestamp_s, int64_t mode) {
TORCH_CHECK(false, "Invalid mode value: ", mode);
}
int ret = av_seek_frame(pFormatContext, -1, timestamp_av_tb, flag);
int ret = av_seek_frame(format_ctx, -1, timestamp_av_tb, flag);
if (ret < 0) {
seek_timestamp = 0;
......@@ -357,7 +357,7 @@ void StreamReader::add_video_stream(
}
void StreamReader::add_packet_stream(int i) {
validate_src_stream_index(pFormatContext, i);
validate_src_stream_index(format_ctx, i);
if (!packet_buffer) {
packet_buffer = std::make_unique<PacketBuffer>();
}
......@@ -373,9 +373,9 @@ void StreamReader::add_stream(
const c10::optional<std::string>& decoder,
const c10::optional<OptionDict>& decoder_option,
const torch::Device& device) {
validate_src_stream_type(pFormatContext, i, media_type);
validate_src_stream_type(format_ctx, i, media_type);
AVStream* stream = pFormatContext->streams[i];
AVStream* stream = format_ctx->streams[i];
// When media source is file-like object, it is possible that source codec
// is not detected properly.
TORCH_CHECK(
......@@ -402,7 +402,7 @@ void StreamReader::add_stream(
case AVMEDIA_TYPE_AUDIO:
return AVRational{0, 1};
case AVMEDIA_TYPE_VIDEO:
return av_guess_frame_rate(pFormatContext, stream, nullptr);
return av_guess_frame_rate(format_ctx, stream, nullptr);
default:
TORCH_INTERNAL_ASSERT(
false,
......@@ -446,7 +446,7 @@ void StreamReader::remove_stream(int64_t i) {
// 1: It's done, caller should stop calling
// <0: Some error happened
int StreamReader::process_packet() {
int ret = av_read_frame(pFormatContext, pPacket);
int ret = av_read_frame(format_ctx, packet);
if (ret == AVERROR_EOF) {
ret = drain();
return (ret < 0) ? ret : 1;
......@@ -454,9 +454,9 @@ int StreamReader::process_packet() {
if (ret < 0) {
return ret;
}
AutoPacketUnref packet{pPacket};
AutoPacketUnref auto_unref{packet};
int stream_index = pPacket->stream_index;
int stream_index = packet->stream_index;
if (packet_stream_indices.count(stream_index)) {
packet_buffer->push_packet(packet);
......
......@@ -12,8 +12,8 @@ namespace io {
/// Fetch and decode audio/video streams chunk by chunk.
///
class StreamReader {
AVFormatInputContextPtr pFormatContext;
AVPacketPtr pPacket{alloc_avpacket()};
AVFormatInputContextPtr format_ctx;
AVPacketPtr packet{alloc_avpacket()};
std::vector<std::unique_ptr<StreamProcessor>> processors;
// Mapping from user-facing stream index to internal index.
......@@ -49,9 +49,9 @@ class StreamReader {
/// 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 pFormatContext An initialized AVFormatContext. StreamReader will
/// @param format_ctx An initialized AVFormatContext. StreamReader will
/// own the resources and release it at the end.
explicit StreamReader(AVFormatContext* pFormatContext);
explicit StreamReader(AVFormatContext* format_ctx);
/// @endcond
......
......@@ -38,7 +38,7 @@ AVFormatContext* get_output_format_context(
}
} // namespace
StreamWriter::StreamWriter(AVFormatContext* p) : pFormatContext(p) {
StreamWriter::StreamWriter(AVFormatContext* p) : format_ctx(p) {
C10_LOG_API_USAGE_ONCE("torchaudio.io.StreamWriter");
}
......@@ -66,13 +66,13 @@ void StreamWriter::add_audio_stream(
const c10::optional<std::string>& filter_desc) {
TORCH_CHECK(!is_open, "Output is already opened. Cannot add a new stream.");
TORCH_INTERNAL_ASSERT(
pFormatContext->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.");
processes.emplace(
std::piecewise_construct,
std::forward_as_tuple(current_key),
std::forward_as_tuple(get_audio_encode_process(
pFormatContext,
format_ctx,
sample_rate,
num_channels,
format,
......@@ -102,13 +102,13 @@ void StreamWriter::add_video_stream(
const c10::optional<std::string>& filter_desc) {
TORCH_CHECK(!is_open, "Output is already opened. Cannot add a new stream.");
TORCH_INTERNAL_ASSERT(
pFormatContext->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.");
processes.emplace(
std::piecewise_construct,
std::forward_as_tuple(current_key),
std::forward_as_tuple(get_video_encode_process(
pFormatContext,
format_ctx,
frame_rate,
width,
height,
......@@ -129,7 +129,7 @@ void StreamWriter::add_packet_stream(const StreamParams& stream_params) {
packet_writers.emplace(
std::piecewise_construct,
std::forward_as_tuple(stream_params.stream_index),
std::forward_as_tuple(pFormatContext, stream_params));
std::forward_as_tuple(format_ctx, stream_params));
current_key++;
}
......@@ -146,13 +146,13 @@ void StreamWriter::add_audio_frame_stream(
const c10::optional<std::string>& filter_desc) {
TORCH_CHECK(!is_open, "Output is already opened. Cannot add a new stream.");
TORCH_INTERNAL_ASSERT(
pFormatContext->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.");
processes.emplace(
std::piecewise_construct,
std::forward_as_tuple(current_key),
std::forward_as_tuple(get_audio_encode_process(
pFormatContext,
format_ctx,
sample_rate,
num_channels,
format,
......@@ -183,13 +183,13 @@ void StreamWriter::add_video_frame_stream(
const c10::optional<std::string>& filter_desc) {
TORCH_CHECK(!is_open, "Output is already opened. Cannot add a new stream.");
TORCH_INTERNAL_ASSERT(
pFormatContext->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.");
processes.emplace(
std::piecewise_construct,
std::forward_as_tuple(current_key),
std::forward_as_tuple(get_video_encode_process(
pFormatContext,
format_ctx,
frame_rate,
width,
height,
......@@ -208,53 +208,49 @@ void StreamWriter::add_video_frame_stream(
}
void StreamWriter::set_metadata(const OptionDict& metadata) {
av_dict_free(&pFormatContext->metadata);
av_dict_free(&format_ctx->metadata);
for (auto const& [key, value] : metadata) {
av_dict_set(&pFormatContext->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) {
av_dump_format(pFormatContext, (int)i, pFormatContext->url, 1);
av_dump_format(format_ctx, (int)i, format_ctx->url, 1);
}
void StreamWriter::open(const c10::optional<OptionDict>& option) {
TORCH_INTERNAL_ASSERT(
pFormatContext->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.");
int ret = 0;
// Open the file if it was not provided by client code (i.e. when not
// file-like object)
AVFORMAT_CONST AVOutputFormat* fmt = pFormatContext->oformat;
AVFORMAT_CONST AVOutputFormat* fmt = format_ctx->oformat;
AVDictionary* opt = get_option_dict(option);
if (!(fmt->flags & AVFMT_NOFILE) &&
!(pFormatContext->flags & AVFMT_FLAG_CUSTOM_IO)) {
!(format_ctx->flags & AVFMT_FLAG_CUSTOM_IO)) {
ret = avio_open2(
&pFormatContext->pb,
pFormatContext->url,
AVIO_FLAG_WRITE,
nullptr,
&opt);
&format_ctx->pb, format_ctx->url, AVIO_FLAG_WRITE, nullptr, &opt);
if (ret < 0) {
av_dict_free(&opt);
TORCH_CHECK(
false,
"Failed to open dst: ",
pFormatContext->url,
format_ctx->url,
" (",
av_err2string(ret),
")");
}
}
ret = avformat_write_header(pFormatContext, &opt);
ret = avformat_write_header(format_ctx, &opt);
clean_up_dict(opt);
TORCH_CHECK(
ret >= 0,
"Failed to write header: ",
pFormatContext->url,
format_ctx->url,
" (",
av_err2string(ret),
")");
......@@ -262,18 +258,18 @@ void StreamWriter::open(const c10::optional<OptionDict>& option) {
}
void StreamWriter::close() {
int ret = av_write_trailer(pFormatContext);
int ret = av_write_trailer(format_ctx);
if (ret < 0) {
LOG(WARNING) << "Failed to write trailer. (" << av_err2string(ret) << ").";
}
// Close the file if it was not provided by client code (i.e. when not
// file-like object)
AVFORMAT_CONST AVOutputFormat* fmt = pFormatContext->oformat;
AVFORMAT_CONST AVOutputFormat* fmt = format_ctx->oformat;
if (!(fmt->flags & AVFMT_NOFILE) &&
!(pFormatContext->flags & AVFMT_FLAG_CUSTOM_IO)) {
!(format_ctx->flags & AVFMT_FLAG_CUSTOM_IO)) {
// avio_closep can be only applied to AVIOContext opened by avio_open
avio_closep(&(pFormatContext->pb));
avio_closep(&(format_ctx->pb));
}
is_open = false;
}
......@@ -284,13 +280,13 @@ void StreamWriter::write_audio_chunk(
const c10::optional<double>& pts) {
TORCH_CHECK(is_open, "Output is not opened. Did you call `open` method?");
TORCH_CHECK(
0 <= i && i < static_cast<int>(pFormatContext->nb_streams),
0 <= i && i < static_cast<int>(format_ctx->nb_streams),
"Invalid stream index. Index must be in range of [0, ",
pFormatContext->nb_streams,
format_ctx->nb_streams,
"). Found: ",
i);
TORCH_CHECK(
pFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO,
format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO,
"Stream ",
i,
" is not audio type.");
......@@ -303,13 +299,13 @@ void StreamWriter::write_video_chunk(
const c10::optional<double>& pts) {
TORCH_CHECK(is_open, "Output is not opened. Did you call `open` method?");
TORCH_CHECK(
0 <= i && i < static_cast<int>(pFormatContext->nb_streams),
0 <= i && i < static_cast<int>(format_ctx->nb_streams),
"Invalid stream index. Index must be in range of [0, ",
pFormatContext->nb_streams,
format_ctx->nb_streams,
"). Found: ",
i);
TORCH_CHECK(
pFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO,
format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO,
"Stream ",
i,
" is not video type.");
......@@ -329,9 +325,9 @@ void StreamWriter::write_packet(const AVPacketPtr& packet) {
void StreamWriter::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>(pFormatContext->nb_streams),
0 <= i && i < static_cast<int>(format_ctx->nb_streams),
"Invalid stream index. Index must be in range of [0, ",
pFormatContext->nb_streams,
format_ctx->nb_streams,
"). Found: ",
i);
processes.at(i).process_frame(frame);
......
......@@ -14,7 +14,7 @@ namespace io {
/// Encode and write audio/video streams chunk by chunk
///
class StreamWriter {
AVFormatOutputContextPtr pFormatContext;
AVFormatOutputContextPtr format_ctx;
std::map<int, EncodeProcess> processes;
std::map<int, PacketWriter> packet_writers;
......
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