Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions docs/source/api_ref_transforms.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,3 @@ Encoding
api_ref_decoders
api_ref_encoders
api_ref_samplers
api_ref_transforms
2 changes: 1 addition & 1 deletion src/torchcodec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Note: usort wants to put Frame and FrameBatch after decoders and samplers,
# but that results in circular import.
from ._frame import AudioSamples, Frame, FrameBatch # usort:skip # noqa
from . import decoders, encoders, samplers, transforms # noqa
from . import decoders, encoders, samplers # noqa

try:
# Note that version.py is generated during install.
Expand Down
85 changes: 2 additions & 83 deletions src/torchcodec/decoders/_video_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
import json
import numbers
from pathlib import Path
from typing import List, Literal, Optional, Sequence, Tuple, Union
from typing import Literal, Optional, Tuple, Union

import torch
from torch import device as torch_device, nn, Tensor
from torch import device as torch_device, Tensor

from torchcodec import _core as core, Frame, FrameBatch
from torchcodec.decoders._decoder_utils import (
_get_cuda_backend,
create_decoder,
ERROR_REPORTING_INSTRUCTIONS,
)
from torchcodec.transforms import DecoderTransform, Resize


class VideoDecoder:
Expand Down Expand Up @@ -68,11 +67,6 @@ class VideoDecoder:
probably is. Default: "exact".
Read more about this parameter in:
:ref:`sphx_glr_generated_examples_decoding_approximate_mode.py`
transforms (sequence of transform objects, optional): Sequence of transforms to be
applied to the decoded frames by the decoder itself, in order. Accepts both
:class:`~torchcodec.transforms.DecoderTransform` and
:class:`~torchvision.transforms.v2.Transform`
objects. Read more about this parameter in: TODO_DECODER_TRANSFORMS_TUTORIAL.
custom_frame_mappings (str, bytes, or file-like object, optional):
Mapping of frames to their metadata, typically generated via ffprobe.
This enables accurate frame seeking without requiring a full video scan.
Expand Down Expand Up @@ -111,7 +105,6 @@ def __init__(
num_ffmpeg_threads: int = 1,
device: Optional[Union[str, torch_device]] = None,
seek_mode: Literal["exact", "approximate"] = "exact",
transforms: Optional[Sequence[Union[DecoderTransform, nn.Module]]] = None,
custom_frame_mappings: Optional[
Union[str, bytes, io.RawIOBase, io.BufferedReader]
] = None,
Expand Down Expand Up @@ -167,7 +160,6 @@ def __init__(
device = str(device)

device_variant = _get_cuda_backend()
transform_specs = _make_transform_specs(transforms)

core.add_video_stream(
self._decoder,
Expand All @@ -176,7 +168,6 @@ def __init__(
num_threads=num_ffmpeg_threads,
device=device,
device_variant=device_variant,
transform_specs=transform_specs,
custom_frame_mappings=custom_frame_mappings_data,
)

Expand Down Expand Up @@ -448,78 +439,6 @@ def _get_and_validate_stream_metadata(
)


def _convert_to_decoder_transforms(
transforms: Sequence[Union[DecoderTransform, nn.Module]],
) -> List[DecoderTransform]:
"""Convert a sequence of transforms that may contain TorchVision transform
objects into a list of only TorchCodec transform objects.
Args:
transforms: Squence of transform objects. The objects can be one of two
types:
1. torchcodec.transforms.DecoderTransform
2. torchvision.transforms.v2.Transform, but our type annotation
only mentions its base, nn.Module. We don't want to take a
hard dependency on TorchVision.
Returns:
List of DecoderTransform objects.
"""
try:
from torchvision.transforms import v2

tv_available = True
except ImportError:
tv_available = False

converted_transforms: list[DecoderTransform] = []
for transform in transforms:
if not isinstance(transform, DecoderTransform):
if not tv_available:
raise ValueError(
f"The supplied transform, {transform}, is not a TorchCodec "
" DecoderTransform. TorchCodec also accept TorchVision "
"v2 transforms, but TorchVision is not installed."
)
elif isinstance(transform, v2.Resize):
converted_transforms.append(Resize._from_torchvision(transform))
else:
raise ValueError(
f"Unsupported transform: {transform}. Transforms must be "
"either a TorchCodec DecoderTransform or a TorchVision "
"v2 transform."
)
else:
converted_transforms.append(transform)

return converted_transforms


def _make_transform_specs(
transforms: Optional[Sequence[Union[DecoderTransform, nn.Module]]],
) -> str:
"""Given a sequence of transforms, turn those into the specification string
the core API expects.
Args:
transforms: Optional sequence of transform objects. The objects can be
one of two types:
1. torchcodec.transforms.DecoderTransform
2. torchvision.transforms.v2.Transform, but our type annotation
only mentions its base, nn.Module. We don't want to take a
hard dependency on TorchVision.
Returns:
String of transforms in the format the core API expects: transform
specifications separate by semicolons.
"""
if transforms is None:
return ""

transforms = _convert_to_decoder_transforms(transforms)
return ";".join([t._make_transform_spec() for t in transforms])


def _read_custom_frame_mappings(
custom_frame_mappings: Union[str, bytes, io.RawIOBase, io.BufferedReader]
) -> tuple[Tensor, Tensor, Tensor]:
Expand Down
7 changes: 0 additions & 7 deletions src/torchcodec/transforms/__init__.py

This file was deleted.

Loading
Loading