Skip to content

Commit b4407e0

Browse files
authored
[codemod] c10::optional -> std::optional in pytorch/audio/src/libtorio/ffmpeg/stream_reader/stream_processor.h +20
Differential Revision: D57294285 Pull Request resolved: #3792
1 parent ea437b3 commit b4407e0

File tree

8 files changed

+165
-165
lines changed

8 files changed

+165
-165
lines changed

src/libtorio/ffmpeg/stream_reader/stream_processor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class StreamProcessor {
6666

6767
void set_decoder(
6868
const AVCodecParameters* codecpar,
69-
const c10::optional<std::string>& decoder_name,
70-
const c10::optional<OptionDict>& decoder_option,
69+
const std::optional<std::string>& decoder_name,
70+
const std::optional<OptionDict>& decoder_option,
7171
const torch::Device& device);
7272

7373
//////////////////////////////////////////////////////////////////////////////
@@ -100,7 +100,7 @@ class StreamProcessor {
100100
//////////////////////////////////////////////////////////////////////////////
101101
public:
102102
// Get the chunk from the given filter result
103-
c10::optional<Chunk> pop_chunk(KeyType key);
103+
std::optional<Chunk> pop_chunk(KeyType key);
104104
};
105105

106106
} // namespace io

src/libtorio/ffmpeg/stream_reader/stream_reader.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ using KeyType = StreamProcessor::KeyType;
1515
namespace {
1616
AVFormatContext* get_input_format_context(
1717
const std::string& src,
18-
const c10::optional<std::string>& format,
19-
const c10::optional<OptionDict>& option,
18+
const std::optional<std::string>& format,
19+
const std::optional<OptionDict>& option,
2020
AVIOContext* io_ctx) {
2121
AVFormatContext* p = avformat_alloc_context();
2222
TORCH_CHECK(p, "Failed to allocate AVFormatContext.");
@@ -72,8 +72,8 @@ StreamingMediaDecoder::StreamingMediaDecoder(AVFormatContext* p)
7272

7373
StreamingMediaDecoder::StreamingMediaDecoder(
7474
AVIOContext* io_ctx,
75-
const c10::optional<std::string>& format,
76-
const c10::optional<OptionDict>& option)
75+
const std::optional<std::string>& format,
76+
const std::optional<OptionDict>& option)
7777
: StreamingMediaDecoder(get_input_format_context(
7878
"Custom Input Context",
7979
format,
@@ -82,8 +82,8 @@ StreamingMediaDecoder::StreamingMediaDecoder(
8282

8383
StreamingMediaDecoder::StreamingMediaDecoder(
8484
const std::string& src,
85-
const c10::optional<std::string>& format,
86-
const c10::optional<OptionDict>& option)
85+
const std::optional<std::string>& format,
86+
const std::optional<OptionDict>& option)
8787
: StreamingMediaDecoder(
8888
get_input_format_context(src, format, option, nullptr)) {}
8989

@@ -308,9 +308,9 @@ void StreamingMediaDecoder::add_audio_stream(
308308
int64_t i,
309309
int64_t frames_per_chunk,
310310
int64_t num_chunks,
311-
const c10::optional<std::string>& filter_desc,
312-
const c10::optional<std::string>& decoder,
313-
const c10::optional<OptionDict>& decoder_option) {
311+
const std::optional<std::string>& filter_desc,
312+
const std::optional<std::string>& decoder,
313+
const std::optional<OptionDict>& decoder_option) {
314314
add_stream(
315315
static_cast<int>(i),
316316
AVMEDIA_TYPE_AUDIO,
@@ -326,10 +326,10 @@ void StreamingMediaDecoder::add_video_stream(
326326
int64_t i,
327327
int64_t frames_per_chunk,
328328
int64_t num_chunks,
329-
const c10::optional<std::string>& filter_desc,
330-
const c10::optional<std::string>& decoder,
331-
const c10::optional<OptionDict>& decoder_option,
332-
const c10::optional<std::string>& hw_accel) {
329+
const std::optional<std::string>& filter_desc,
330+
const std::optional<std::string>& decoder,
331+
const std::optional<OptionDict>& decoder_option,
332+
const std::optional<std::string>& hw_accel) {
333333
const torch::Device device = [&]() {
334334
if (!hw_accel) {
335335
return torch::Device{c10::DeviceType::CPU};
@@ -371,8 +371,8 @@ void StreamingMediaDecoder::add_stream(
371371
int frames_per_chunk,
372372
int num_chunks,
373373
const std::string& filter_desc,
374-
const c10::optional<std::string>& decoder,
375-
const c10::optional<OptionDict>& decoder_option,
374+
const std::optional<std::string>& decoder,
375+
const std::optional<OptionDict>& decoder_option,
376376
const torch::Device& device) {
377377
validate_src_stream_type(format_ctx, i, media_type);
378378

@@ -517,7 +517,7 @@ void StreamingMediaDecoder::process_all_packets() {
517517
}
518518

519519
int StreamingMediaDecoder::process_packet(
520-
const c10::optional<double>& timeout,
520+
const std::optional<double>& timeout,
521521
const double backoff) {
522522
int code = [&]() -> int {
523523
if (timeout.has_value()) {
@@ -531,7 +531,7 @@ int StreamingMediaDecoder::process_packet(
531531
}
532532

533533
int StreamingMediaDecoder::fill_buffer(
534-
const c10::optional<double>& timeout,
534+
const std::optional<double>& timeout,
535535
const double backoff) {
536536
while (!is_buffer_ready()) {
537537
int code = process_packet(timeout, backoff);
@@ -556,8 +556,8 @@ int StreamingMediaDecoder::drain() {
556556
return ret;
557557
}
558558

559-
std::vector<c10::optional<Chunk>> StreamingMediaDecoder::pop_chunks() {
560-
std::vector<c10::optional<Chunk>> ret;
559+
std::vector<std::optional<Chunk>> StreamingMediaDecoder::pop_chunks() {
560+
std::vector<std::optional<Chunk>> ret;
561561
ret.reserve(static_cast<size_t>(num_out_streams()));
562562
for (auto& i : stream_indices) {
563563
ret.emplace_back(processors[i.first]->pop_chunk(i.second));
@@ -602,11 +602,11 @@ CustomInput::CustomInput(
602602

603603
StreamingMediaDecoderCustomIO::StreamingMediaDecoderCustomIO(
604604
void* opaque,
605-
const c10::optional<std::string>& format,
605+
const std::optional<std::string>& format,
606606
int buffer_size,
607607
int (*read_packet)(void* opaque, uint8_t* buf, int buf_size),
608608
int64_t (*seek)(void* opaque, int64_t offset, int whence),
609-
const c10::optional<OptionDict>& option)
609+
const std::optional<OptionDict>& option)
610610
: CustomInput(opaque, buffer_size, read_packet, seek),
611611
StreamingMediaDecoder(io_ctx, format, option) {}
612612

src/libtorio/ffmpeg/stream_reader/stream_reader.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class StreamingMediaDecoder {
6666
/// (opening source).
6767
explicit StreamingMediaDecoder(
6868
AVIOContext* io_ctx,
69-
const c10::optional<std::string>& format = c10::nullopt,
70-
const c10::optional<OptionDict>& option = c10::nullopt);
69+
const std::optional<std::string>& format = c10::nullopt,
70+
const std::optional<OptionDict>& option = c10::nullopt);
7171

7272
/// @endcond
7373

@@ -81,8 +81,8 @@ class StreamingMediaDecoder {
8181
/// (opening source).
8282
explicit StreamingMediaDecoder(
8383
const std::string& src,
84-
const c10::optional<std::string>& format = c10::nullopt,
85-
const c10::optional<OptionDict>& option = c10::nullopt);
84+
const std::optional<std::string>& format = c10::nullopt,
85+
const std::optional<OptionDict>& option = c10::nullopt);
8686

8787
///@}
8888

@@ -205,9 +205,9 @@ class StreamingMediaDecoder {
205205
int64_t i,
206206
int64_t frames_per_chunk,
207207
int64_t num_chunks,
208-
const c10::optional<std::string>& filter_desc = c10::nullopt,
209-
const c10::optional<std::string>& decoder = c10::nullopt,
210-
const c10::optional<OptionDict>& decoder_option = c10::nullopt);
208+
const std::optional<std::string>& filter_desc = c10::nullopt,
209+
const std::optional<std::string>& decoder = c10::nullopt,
210+
const std::optional<OptionDict>& decoder_option = c10::nullopt);
211211
/// Define an output video stream.
212212
///
213213
/// @param i,frames_per_chunk,num_chunks,filter_desc,decoder,decoder_option
@@ -226,10 +226,10 @@ class StreamingMediaDecoder {
226226
int64_t i,
227227
int64_t frames_per_chunk,
228228
int64_t num_chunks,
229-
const c10::optional<std::string>& filter_desc = c10::nullopt,
230-
const c10::optional<std::string>& decoder = c10::nullopt,
231-
const c10::optional<OptionDict>& decoder_option = c10::nullopt,
232-
const c10::optional<std::string>& hw_accel = c10::nullopt);
229+
const std::optional<std::string>& filter_desc = c10::nullopt,
230+
const std::optional<std::string>& decoder = c10::nullopt,
231+
const std::optional<OptionDict>& decoder_option = c10::nullopt,
232+
const std::optional<std::string>& hw_accel = c10::nullopt);
233233

234234
/// @cond
235235
/// Add a output packet stream.
@@ -255,8 +255,8 @@ class StreamingMediaDecoder {
255255
int frames_per_chunk,
256256
int num_chunks,
257257
const std::string& filter_desc,
258-
const c10::optional<std::string>& decoder,
259-
const c10::optional<OptionDict>& decoder_option,
258+
const std::optional<std::string>& decoder,
259+
const std::optional<OptionDict>& decoder_option,
260260
const torch::Device& device);
261261

262262
//////////////////////////////////////////////////////////////////////////////
@@ -303,7 +303,7 @@ class StreamingMediaDecoder {
303303
/// @cond
304304
// High-level method used by Python bindings.
305305
int process_packet(
306-
const c10::optional<double>& timeout,
306+
const std::optional<double>& timeout,
307307
const double backoff);
308308
/// @endcond
309309

@@ -315,7 +315,7 @@ class StreamingMediaDecoder {
315315
/// @param timeout See `process_packet_block()`
316316
/// @param backoff See `process_packet_block()`
317317
int fill_buffer(
318-
const c10::optional<double>& timeout = c10::nullopt,
318+
const std::optional<double>& timeout = c10::nullopt,
319319
const double backoff = 10.);
320320

321321
///@}
@@ -331,7 +331,7 @@ class StreamingMediaDecoder {
331331
///@{
332332

333333
/// Pop one chunk from each output stream if it is available.
334-
std::vector<c10::optional<Chunk>> pop_chunks();
334+
std::vector<std::optional<Chunk>> pop_chunks();
335335

336336
/// @cond
337337
/// Pop packets from buffer, if available.
@@ -379,11 +379,11 @@ class StreamingMediaDecoderCustomIO : private detail::CustomInput,
379379
/// @param option Custom option passed when initializing format context.
380380
StreamingMediaDecoderCustomIO(
381381
void* opaque,
382-
const c10::optional<std::string>& format,
382+
const std::optional<std::string>& format,
383383
int buffer_size,
384384
int (*read_packet)(void* opaque, uint8_t* buf, int buf_size),
385385
int64_t (*seek)(void* opaque, int64_t offset, int whence) = nullptr,
386-
const c10::optional<OptionDict>& option = c10::nullopt);
386+
const std::optional<OptionDict>& option = c10::nullopt);
387387
};
388388

389389
// For BC

src/libtorio/ffmpeg/stream_writer/encode_process.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ EncodeProcess::EncodeProcess(
2222

2323
void EncodeProcess::process(
2424
const torch::Tensor& tensor,
25-
const c10::optional<double>& pts) {
25+
const std::optional<double>& pts) {
2626
if (pts) {
2727
const double& pts_val = pts.value();
2828
TORCH_CHECK(
@@ -127,7 +127,7 @@ enum AVPixelFormat get_src_pix_fmt(const std::string& src) {
127127
////////////////////////////////////////////////////////////////////////////////
128128
const AVCodec* get_codec(
129129
AVCodecID default_codec,
130-
const c10::optional<std::string>& encoder) {
130+
const std::optional<std::string>& encoder) {
131131
if (encoder) {
132132
const AVCodec* c = avcodec_find_encoder_by_name(encoder.value().c_str());
133133
TORCH_CHECK(c, "Unexpected codec: ", encoder.value());
@@ -151,7 +151,7 @@ AVCodecContextPtr get_codec_ctx(const AVCodec* codec, int flags) {
151151

152152
void open_codec(
153153
AVCodecContext* codec_ctx,
154-
const c10::optional<OptionDict>& option) {
154+
const std::optional<OptionDict>& option) {
155155
AVDictionary* opt = get_option_dict(option);
156156

157157
// Enable experimental feature if required
@@ -224,7 +224,7 @@ std::string get_supported_formats(const AVSampleFormat* sample_fmts) {
224224

225225
AVSampleFormat get_enc_fmt(
226226
AVSampleFormat src_fmt,
227-
const c10::optional<std::string>& encoder_format,
227+
const std::optional<std::string>& encoder_format,
228228
const AVCodec* codec) {
229229
if (encoder_format) {
230230
auto& enc_fmt_val = encoder_format.value();
@@ -273,7 +273,7 @@ std::string get_supported_samplerates(const int* supported_samplerates) {
273273

274274
int get_enc_sr(
275275
int src_sample_rate,
276-
const c10::optional<int>& encoder_sample_rate,
276+
const std::optional<int>& encoder_sample_rate,
277277
const AVCodec* codec) {
278278
// G.722 only supports 16000 Hz, but it does not list the sample rate in
279279
// supported_samplerates so we hard code it here.
@@ -325,7 +325,7 @@ std::string get_supported_channels(const uint64_t* channel_layouts) {
325325

326326
uint64_t get_channel_layout(
327327
const uint64_t src_ch_layout,
328-
const c10::optional<int> enc_num_channels,
328+
const std::optional<int> enc_num_channels,
329329
const AVCodec* codec) {
330330
// If the override is presented, and if it is supported by codec, we use it.
331331
if (enc_num_channels) {
@@ -370,7 +370,7 @@ void configure_audio_codec_ctx(
370370
AVSampleFormat format,
371371
int sample_rate,
372372
uint64_t channel_layout,
373-
const c10::optional<CodecConfig>& codec_config) {
373+
const std::optional<CodecConfig>& codec_config) {
374374
codec_ctx->sample_fmt = format;
375375
codec_ctx->sample_rate = sample_rate;
376376
codec_ctx->time_base = av_inv_q(av_d2q(sample_rate, 1 << 24));
@@ -421,7 +421,7 @@ std::string get_supported_formats(const AVPixelFormat* pix_fmts) {
421421

422422
AVPixelFormat get_enc_fmt(
423423
AVPixelFormat src_fmt,
424-
const c10::optional<std::string>& encoder_format,
424+
const std::optional<std::string>& encoder_format,
425425
const AVCodec* codec) {
426426
if (encoder_format) {
427427
const auto& val = encoder_format.value();
@@ -455,7 +455,7 @@ bool supported_frame_rate(AVRational rate, const AVRational* rates) {
455455

456456
AVRational get_enc_rate(
457457
AVRational src_rate,
458-
const c10::optional<double>& encoder_sample_rate,
458+
const std::optional<double>& encoder_sample_rate,
459459
const AVCodec* codec) {
460460
if (encoder_sample_rate) {
461461
const double& enc_rate = encoder_sample_rate.value();
@@ -494,7 +494,7 @@ void configure_video_codec_ctx(
494494
AVRational frame_rate,
495495
int width,
496496
int height,
497-
const c10::optional<CodecConfig>& codec_config) {
497+
const std::optional<CodecConfig>& codec_config) {
498498
// TODO: Review other options and make them configurable?
499499
// https://ffmpeg.org/doxygen/4.1/muxing_8c_source.html#l00147
500500
// - bit_rate_tolerance
@@ -596,7 +596,7 @@ FilterGraph get_audio_filter_graph(
596596
AVSampleFormat src_fmt,
597597
int src_sample_rate,
598598
uint64_t src_ch_layout,
599-
const c10::optional<std::string>& filter_desc,
599+
const std::optional<std::string>& filter_desc,
600600
AVSampleFormat enc_fmt,
601601
int enc_sample_rate,
602602
uint64_t enc_ch_layout,
@@ -639,7 +639,7 @@ FilterGraph get_video_filter_graph(
639639
AVRational src_rate,
640640
int src_width,
641641
int src_height,
642-
const c10::optional<std::string>& filter_desc,
642+
const std::optional<std::string>& filter_desc,
643643
AVPixelFormat enc_fmt,
644644
AVRational enc_rate,
645645
int enc_width,
@@ -743,13 +743,13 @@ EncodeProcess get_audio_encode_process(
743743
int src_sample_rate,
744744
int src_num_channels,
745745
const std::string& format,
746-
const c10::optional<std::string>& encoder,
747-
const c10::optional<OptionDict>& encoder_option,
748-
const c10::optional<std::string>& encoder_format,
749-
const c10::optional<int>& encoder_sample_rate,
750-
const c10::optional<int>& encoder_num_channels,
751-
const c10::optional<CodecConfig>& codec_config,
752-
const c10::optional<std::string>& filter_desc,
746+
const std::optional<std::string>& encoder,
747+
const std::optional<OptionDict>& encoder_option,
748+
const std::optional<std::string>& encoder_format,
749+
const std::optional<int>& encoder_sample_rate,
750+
const std::optional<int>& encoder_num_channels,
751+
const std::optional<CodecConfig>& codec_config,
752+
const std::optional<std::string>& filter_desc,
753753
bool disable_converter) {
754754
// 1. Check the source format, rate and channels
755755
TORCH_CHECK(
@@ -854,15 +854,15 @@ EncodeProcess get_video_encode_process(
854854
int src_width,
855855
int src_height,
856856
const std::string& format,
857-
const c10::optional<std::string>& encoder,
858-
const c10::optional<OptionDict>& encoder_option,
859-
const c10::optional<std::string>& encoder_format,
860-
const c10::optional<double>& encoder_frame_rate,
861-
const c10::optional<int>& encoder_width,
862-
const c10::optional<int>& encoder_height,
863-
const c10::optional<std::string>& hw_accel,
864-
const c10::optional<CodecConfig>& codec_config,
865-
const c10::optional<std::string>& filter_desc,
857+
const std::optional<std::string>& encoder,
858+
const std::optional<OptionDict>& encoder_option,
859+
const std::optional<std::string>& encoder_format,
860+
const std::optional<double>& encoder_frame_rate,
861+
const std::optional<int>& encoder_width,
862+
const std::optional<int>& encoder_height,
863+
const std::optional<std::string>& hw_accel,
864+
const std::optional<CodecConfig>& codec_config,
865+
const std::optional<std::string>& filter_desc,
866866
bool disable_converter) {
867867
// 1. Checkc the source format, rate and resolution
868868
TORCH_CHECK(

0 commit comments

Comments
 (0)