Skip to content

Commit 7548687

Browse files
committed
rename codec_options to extra_options
1 parent 36a2c41 commit 7548687

File tree

6 files changed

+50
-50
lines changed

6 files changed

+50
-50
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,13 +620,13 @@ void tryToValidateCodecOption(
620620
}
621621

622622
void sortCodecOptions(
623-
const std::map<std::string, std::string>& codecOptions,
623+
const std::map<std::string, std::string>& extraOptions,
624624
AVDictionary** codecDict,
625625
AVDictionary** formatDict) {
626626
// Accepts a map of options as input, then sorts them into codec options and
627627
// format options. The sorted options are returned into two separate dicts.
628628
const AVClass* formatClass = avformat_get_class();
629-
for (const auto& [key, value] : codecOptions) {
629+
for (const auto& [key, value] : extraOptions) {
630630
const AVOption* fmtOpt = av_opt_find2(
631631
&formatClass,
632632
key.c_str(),
@@ -798,12 +798,12 @@ void VideoEncoder::initializeEncoder(
798798

799799
// Apply videoStreamOptions
800800
AVDictionary* avCodecOptions = nullptr;
801-
if (videoStreamOptions.codecOptions.has_value()) {
802-
for (const auto& [key, value] : videoStreamOptions.codecOptions.value()) {
801+
if (videoStreamOptions.extraOptions.has_value()) {
802+
for (const auto& [key, value] : videoStreamOptions.extraOptions.value()) {
803803
tryToValidateCodecOption(*avCodec, key.c_str(), value);
804804
}
805805
sortCodecOptions(
806-
videoStreamOptions.codecOptions.value(),
806+
videoStreamOptions.extraOptions.value(),
807807
&avCodecOptions,
808808
&avFormatOptions_);
809809
}

src/torchcodec/_core/StreamOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct VideoStreamOptions {
5252
std::optional<std::string> pixelFormat;
5353
std::optional<double> crf;
5454
std::optional<std::string> preset;
55-
std::optional<std::map<std::string, std::string>> codecOptions;
55+
std::optional<std::map<std::string, std::string>> extraOptions;
5656
};
5757

5858
struct AudioStreamOptions {

src/torchcodec/_core/custom_ops.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ TORCH_LIBRARY(torchcodec_ns, m) {
3737
m.def(
3838
"_encode_audio_to_file_like(Tensor samples, int sample_rate, str format, int file_like_context, int? bit_rate=None, int? num_channels=None, int? desired_sample_rate=None) -> ()");
3939
m.def(
40-
"encode_video_to_file(Tensor frames, int frame_rate, str filename, str? codec=None, str? pixel_format=None, float? crf=None, str? preset=None, str[]? codec_options=None) -> ()");
40+
"encode_video_to_file(Tensor frames, int frame_rate, str filename, str? codec=None, str? pixel_format=None, float? crf=None, str? preset=None, str[]? extra_options=None) -> ()");
4141
m.def(
42-
"encode_video_to_tensor(Tensor frames, int frame_rate, str format, str? codec=None, str? pixel_format=None, float? crf=None, str? preset=None, str[]? codec_options=None) -> Tensor");
42+
"encode_video_to_tensor(Tensor frames, int frame_rate, str format, str? codec=None, str? pixel_format=None, float? crf=None, str? preset=None, str[]? extra_options=None) -> Tensor");
4343
m.def(
44-
"_encode_video_to_file_like(Tensor frames, int frame_rate, str format, int file_like_context, str? codec=None, str? pixel_format=None, float? crf=None, str? preset=None, str[]? codec_options=None) -> ()");
44+
"_encode_video_to_file_like(Tensor frames, int frame_rate, str format, int file_like_context, str? codec=None, str? pixel_format=None, float? crf=None, str? preset=None, str[]? extra_options=None) -> ()");
4545
m.def(
4646
"create_from_tensor(Tensor video_tensor, str? seek_mode=None) -> Tensor");
4747
m.def(
@@ -158,8 +158,8 @@ std::string quoteValue(const std::string& value) {
158158
return "\"" + value + "\"";
159159
}
160160

161-
// Helper function to unflatten codec_options, alternating keys and values
162-
std::map<std::string, std::string> unflattenCodecOptions(
161+
// Helper function to unflatten extra_options, alternating keys and values
162+
std::map<std::string, std::string> unflattenExtraOptions(
163163
const std::vector<std::string>& opts) {
164164
std::map<std::string, std::string> optionsMap;
165165
for (size_t i = 0; i < opts.size(); i += 2) {
@@ -617,16 +617,16 @@ void encode_video_to_file(
617617
std::optional<std::string_view> pixel_format = std::nullopt,
618618
std::optional<double> crf = std::nullopt,
619619
std::optional<std::string_view> preset = std::nullopt,
620-
std::optional<std::vector<std::string>> codec_options = std::nullopt) {
620+
std::optional<std::vector<std::string>> extra_options = std::nullopt) {
621621
VideoStreamOptions videoStreamOptions;
622622
videoStreamOptions.codec = codec;
623623
videoStreamOptions.pixelFormat = pixel_format;
624624
videoStreamOptions.crf = crf;
625625
videoStreamOptions.preset = preset;
626626

627-
if (codec_options.has_value()) {
628-
videoStreamOptions.codecOptions =
629-
unflattenCodecOptions(codec_options.value());
627+
if (extra_options.has_value()) {
628+
videoStreamOptions.extraOptions =
629+
unflattenExtraOptions(extra_options.value());
630630
}
631631

632632
VideoEncoder(
@@ -645,17 +645,17 @@ at::Tensor encode_video_to_tensor(
645645
std::optional<std::string_view> pixel_format = std::nullopt,
646646
std::optional<double> crf = std::nullopt,
647647
std::optional<std::string_view> preset = std::nullopt,
648-
std::optional<std::vector<std::string>> codec_options = std::nullopt) {
648+
std::optional<std::vector<std::string>> extra_options = std::nullopt) {
649649
auto avioContextHolder = std::make_unique<AVIOToTensorContext>();
650650
VideoStreamOptions videoStreamOptions;
651651
videoStreamOptions.codec = codec;
652652
videoStreamOptions.pixelFormat = pixel_format;
653653
videoStreamOptions.crf = crf;
654654
videoStreamOptions.preset = preset;
655655

656-
if (codec_options.has_value()) {
657-
videoStreamOptions.codecOptions =
658-
unflattenCodecOptions(codec_options.value());
656+
if (extra_options.has_value()) {
657+
videoStreamOptions.extraOptions =
658+
unflattenExtraOptions(extra_options.value());
659659
}
660660

661661
return VideoEncoder(
@@ -676,7 +676,7 @@ void _encode_video_to_file_like(
676676
std::optional<std::string_view> pixel_format = std::nullopt,
677677
std::optional<double> crf = std::nullopt,
678678
std::optional<std::string_view> preset = std::nullopt,
679-
std::optional<std::vector<std::string>> codec_options = std::nullopt) {
679+
std::optional<std::vector<std::string>> extra_options = std::nullopt) {
680680
auto fileLikeContext =
681681
reinterpret_cast<AVIOFileLikeContext*>(file_like_context);
682682
TORCH_CHECK(
@@ -689,9 +689,9 @@ void _encode_video_to_file_like(
689689
videoStreamOptions.crf = crf;
690690
videoStreamOptions.preset = preset;
691691

692-
if (codec_options.has_value()) {
693-
videoStreamOptions.codecOptions =
694-
unflattenCodecOptions(codec_options.value());
692+
if (extra_options.has_value()) {
693+
videoStreamOptions.extraOptions =
694+
unflattenExtraOptions(extra_options.value());
695695
}
696696

697697
VideoEncoder encoder(

src/torchcodec/_core/ops.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def encode_video_to_file_like(
217217
pixel_format: Optional[str] = None,
218218
crf: Optional[Union[int, float]] = None,
219219
preset: Optional[str] = None,
220-
codec_options: Optional[list[str]] = None,
220+
extra_options: Optional[list[str]] = None,
221221
) -> None:
222222
"""Encode video frames to a file-like object.
223223
@@ -230,7 +230,7 @@ def encode_video_to_file_like(
230230
pixel_format: Optional pixel format (e.g., "yuv420p", "yuv444p")
231231
crf: Optional constant rate factor for encoding quality
232232
preset: Optional encoder preset as string (e.g., "ultrafast", "medium")
233-
codec_options: Optional list of codec options as flattened key-value pairs
233+
extra_options: Optional list of extra options as flattened key-value pairs
234234
"""
235235
assert _pybind_ops is not None
236236

@@ -243,7 +243,7 @@ def encode_video_to_file_like(
243243
pixel_format,
244244
crf,
245245
preset,
246-
codec_options,
246+
extra_options,
247247
)
248248

249249

@@ -335,7 +335,7 @@ def encode_video_to_file_abstract(
335335
pixel_format: Optional[str] = None,
336336
preset: Optional[str] = None,
337337
crf: Optional[Union[int, float]] = None,
338-
codec_options: Optional[list[str]] = None,
338+
extra_options: Optional[list[str]] = None,
339339
) -> None:
340340
return
341341

@@ -349,7 +349,7 @@ def encode_video_to_tensor_abstract(
349349
pixel_format: Optional[str] = None,
350350
preset: Optional[str] = None,
351351
crf: Optional[Union[int, float]] = None,
352-
codec_options: Optional[list[str]] = None,
352+
extra_options: Optional[list[str]] = None,
353353
) -> torch.Tensor:
354354
return torch.empty([], dtype=torch.long)
355355

@@ -364,7 +364,7 @@ def _encode_video_to_file_like_abstract(
364364
pixel_format: Optional[str] = None,
365365
preset: Optional[str] = None,
366366
crf: Optional[Union[int, float]] = None,
367-
codec_options: Optional[list[str]] = None,
367+
extra_options: Optional[list[str]] = None,
368368
) -> None:
369369
return
370370

src/torchcodec/encoders/_video_encoder.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, frames: Tensor, *, frame_rate: int):
3535
def to_file(
3636
self,
3737
dest: Union[str, Path],
38-
codec_options: Optional[Dict[str, Any]] = None,
38+
extra_options: Optional[Dict[str, Any]] = None,
3939
*,
4040
codec: Optional[str] = None,
4141
pixel_format: Optional[str] = None,
@@ -60,8 +60,8 @@ def to_file(
6060
encoding speed and compression. Valid values depend on the encoder (commonly
6161
a string: "fast", "medium", "slow"). Defaults to None
6262
(which will use encoder's default).
63-
codec_options (dict[str, Any], optional): A dictionary of codec-specific
64-
options to pass to the encoder, e.g. ``{"qp": 5, "tune": "film"}``.
63+
extra_options (dict[str, Any], optional): A dictionary of additional
64+
encoder options to pass, e.g. ``{"qp": 5, "tune": "film"}``.
6565
Values will be converted to strings before passing to the encoder.
6666
"""
6767
preset = str(preset) if isinstance(preset, int) else preset
@@ -73,8 +73,8 @@ def to_file(
7373
pixel_format=pixel_format,
7474
crf=crf,
7575
preset=preset,
76-
codec_options=[
77-
x for k, v in (codec_options or {}).items() for x in (k, str(v))
76+
extra_options=[
77+
x for k, v in (extra_options or {}).items() for x in (k, str(v))
7878
],
7979
)
8080

@@ -86,7 +86,7 @@ def to_tensor(
8686
pixel_format: Optional[str] = None,
8787
crf: Optional[Union[int, float]] = None,
8888
preset: Optional[Union[str, int]] = None,
89-
codec_options: Optional[Dict[str, Any]] = None,
89+
extra_options: Optional[Dict[str, Any]] = None,
9090
) -> Tensor:
9191
"""Encode frames into raw bytes, as a 1D uint8 Tensor.
9292
@@ -105,8 +105,8 @@ def to_tensor(
105105
encoding speed and compression. Valid values depend on the encoder (commonly
106106
a string: "fast", "medium", "slow"). Defaults to None
107107
(which will use encoder's default).
108-
codec_options (dict[str, Any], optional): A dictionary of codec-specific
109-
options to pass to the encoder, e.g. ``{"preset": "slow", "tune": "film"}``.
108+
extra_options (dict[str, Any], optional): A dictionary of additional
109+
encoder options to pass, e.g. ``{"preset": "slow", "tune": "film"}``.
110110
Values will be converted to strings before passing to the encoder.
111111
112112
Returns:
@@ -121,8 +121,8 @@ def to_tensor(
121121
pixel_format=pixel_format,
122122
crf=crf,
123123
preset=preset_value,
124-
codec_options=[
125-
x for k, v in (codec_options or {}).items() for x in (k, str(v))
124+
extra_options=[
125+
x for k, v in (extra_options or {}).items() for x in (k, str(v))
126126
],
127127
)
128128

@@ -135,7 +135,7 @@ def to_file_like(
135135
pixel_format: Optional[str] = None,
136136
crf: Optional[Union[int, float]] = None,
137137
preset: Optional[Union[str, int]] = None,
138-
codec_options: Optional[Dict[str, Any]] = None,
138+
extra_options: Optional[Dict[str, Any]] = None,
139139
) -> None:
140140
"""Encode frames into a file-like object.
141141
@@ -159,8 +159,8 @@ def to_file_like(
159159
encoding speed and compression. Valid values depend on the encoder (commonly
160160
a string: "fast", "medium", "slow"). Defaults to None
161161
(which will use encoder's default).
162-
codec_options (dict[str, Any], optional): A dictionary of codec-specific
163-
options to pass to the encoder, e.g. ``{"preset": "slow", "tune": "film"}``.
162+
extra_options (dict[str, Any], optional): A dictionary of additional
163+
encoder options to pass, e.g. ``{"preset": "slow", "tune": "film"}``.
164164
Values will be converted to strings before passing to the encoder.
165165
"""
166166
preset = str(preset) if isinstance(preset, int) else preset
@@ -173,7 +173,7 @@ def to_file_like(
173173
pixel_format=pixel_format,
174174
crf=crf,
175175
preset=preset,
176-
codec_options=[
177-
x for k, v in (codec_options or {}).items() for x in (k, str(v))
176+
extra_options=[
177+
x for k, v in (extra_options or {}).items() for x in (k, str(v))
178178
],
179179
)

test/test_encoders.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ def test_pixel_format_errors(self, method, tmp_path):
726726
getattr(encoder, method)(**valid_params, pixel_format="rgb24")
727727

728728
@pytest.mark.parametrize(
729-
"codec_options,error",
729+
"extra_options,error",
730730
[
731731
({"qp": -10}, "qp=-10 is out of valid range"),
732732
(
@@ -745,7 +745,7 @@ def test_pixel_format_errors(self, method, tmp_path):
745745
],
746746
)
747747
@pytest.mark.parametrize("method", ("to_file", "to_tensor", "to_file_like"))
748-
def test_codec_options_errors(self, method, tmp_path, codec_options, error):
748+
def test_extra_options_errors(self, method, tmp_path, extra_options, error):
749749
frames = torch.zeros((5, 3, 64, 64), dtype=torch.uint8)
750750
encoder = VideoEncoder(frames, frame_rate=30)
751751

@@ -762,7 +762,7 @@ def test_codec_options_errors(self, method, tmp_path, codec_options, error):
762762
RuntimeError,
763763
match=error,
764764
):
765-
getattr(encoder, method)(**valid_params, codec_options=codec_options)
765+
getattr(encoder, method)(**valid_params, extra_options=extra_options)
766766

767767
@pytest.mark.parametrize("method", ("to_file", "to_tensor", "to_file_like"))
768768
def test_contiguity(self, method, tmp_path):
@@ -1156,15 +1156,15 @@ def test_codec_spec_vs_impl_equivalence(self, tmp_path, codec_spec, codec_impl):
11561156
("high", "fcc", "pc"),
11571157
],
11581158
)
1159-
def test_codec_options_utilized(self, tmp_path, profile, colorspace, color_range):
1160-
# Test setting profile, colorspace, and color_range via codec_options is utilized
1159+
def test_extra_options_utilized(self, tmp_path, profile, colorspace, color_range):
1160+
# Test setting profile, colorspace, and color_range via extra_options is utilized
11611161
source_frames = torch.zeros((5, 3, 64, 64), dtype=torch.uint8)
11621162
encoder = VideoEncoder(frames=source_frames, frame_rate=30)
11631163

11641164
output_path = str(tmp_path / "output.mp4")
11651165
encoder.to_file(
11661166
dest=output_path,
1167-
codec_options={
1167+
extra_options={
11681168
"profile": profile,
11691169
"colorspace": colorspace,
11701170
"color_range": color_range,

0 commit comments

Comments
 (0)