ColorConverter#
- class torchcodec.decoders.ColorConverter(device: str | device | None = None, output_dtype: dtype | Literal['auto'] = torch.uint8)[source]#
Turn a
RawFrame(typically YUV) into an RGBFrame.This is a low-level API: for straightforward decoding, use
VideoDecoderinstead.converter = ColorConverter() for packet in demuxer: for raw_frame in packet_decoder.decode(packet): frame = converter.convert(raw_frame) frame.data # uint8 [3, height, width], RGB
Unlike the other stages this one isn’t tied to a specific video stream. Everything it needs (dimensions, pixel format, color space, rotation) comes from the
RawFrameitself, so the same converter instance can process frames from any video stream, provided that they share the same device.- Parameters:
device (str or torch.device, optional) – The device to convert on. If
None(default), the current default device is used (seetorch.set_default_device). It has to be the device the frames are already on, i.e. it must match what was passed to theVideoPacketDecoderthat produced theRawFrame.output_dtype (torch.dtype or
"auto", optional) –torch.uint8(default) for values in[0, 255],torch.float32for[0, 1], or"auto"for uint8 from 8-bit sources and float32 from deeper ones. Since this block isn’t tied to a stream,"auto"is resolved per frame rather than once per video, so feeding it a mix of SDR and HDR frames gives you a mix of dtypes.
Examples using
ColorConverter:- convert(raw_frame: RawFrame) Frame[source]#
Convert one
RawFrameto an RGBFrame.RawFrame.rotationis applied, so the output is upright and matches what aVideoDecodergives you.If you run this on a different CUDA stream than the one the frame was decoded on, refer to Low-level APIs and CUDA streams synchronization for pitfalls and how to avoid them.
- Parameters:
raw_frame (RawFrame) – The frame to convert. It has to be on this converter’s device.
- Returns:
The RGB
[3, height, width]frame in the converter’soutput_dtype.- Raises:
RuntimeError – If the frame is not on this converter’s device.