@@ -559,26 +559,9 @@ void SingleStreamDecoder::add_video_stream(
559559 active_stream_index_, custom_frame_mappings.value ());
560560 }
561561
562- // Resolve the user-facing OutputDtypeConfig (which may be AUTO) into an
563- // OutputDtype that downstream code can use directly.
564- // TODO_HDR: This is basically our heuristic that defines how we identify HDR
565- // videos, we might want to refine it.
566- switch (stream_info.video_stream_options .output_dtype_config ) {
567- case OutputDtypeConfig::UINT8 :
568- stream_info.video_stream_options .output_dtype = OutputDtype::UINT8 ;
569- break ;
570- case OutputDtypeConfig::FLOAT32 :
571- stream_info.video_stream_options .output_dtype = OutputDtype::FLOAT32 ;
572- break ;
573- case OutputDtypeConfig::AUTO : {
574- const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get (
575- static_cast <AVPixelFormat>(stream_info.stream ->codecpar ->format ));
576- stream_info.video_stream_options .output_dtype =
577- (desc != nullptr && desc->comp [0 ].depth > 8 ) ? OutputDtype::FLOAT32
578- : OutputDtype::UINT8 ;
579- break ;
580- }
581- }
562+ stream_info.video_stream_options .output_dtype = resolve_output_dtype (
563+ stream_info.video_stream_options .output_dtype_config ,
564+ static_cast <AVPixelFormat>(stream_info.stream ->codecpar ->format ));
582565
583566 // Set preRotationDims_ for the active stream. These are the raw encoded
584567 // dimensions from FFmpeg, used as a fallback for tensor pre-allocation when
@@ -678,7 +661,7 @@ void SingleStreamDecoder::add_audio_stream(
678661FrameOutput SingleStreamDecoder::get_next_frame () {
679662 auto output = get_next_frame_internal ();
680663 if (stream_infos_[active_stream_index_].av_media_type == AVMEDIA_TYPE_VIDEO ) {
681- output.data = maybe_permute_and_convert_to_float32 (output.data );
664+ output.data = maybe_permute_and_convert_dtype (output.data );
682665 }
683666 return output;
684667}
@@ -695,7 +678,7 @@ FrameOutput SingleStreamDecoder::get_next_frame_internal(
695678
696679FrameOutput SingleStreamDecoder::get_frame_at_index (int64_t frame_index) {
697680 auto frame_output = get_frame_at_index_internal (frame_index);
698- frame_output.data = maybe_permute_and_convert_to_float32 (frame_output.data );
681+ frame_output.data = maybe_permute_and_convert_dtype (frame_output.data );
699682 return frame_output;
700683}
701684
@@ -805,7 +788,7 @@ FrameBatchOutput SingleStreamDecoder::get_frames_at_indices(
805788 previous_index_in_video = index_in_video;
806789 }
807790 frame_batch_output.data =
808- maybe_permute_and_convert_to_float32 (frame_batch_output.data );
791+ maybe_permute_and_convert_dtype (frame_batch_output.data );
809792 return frame_batch_output;
810793}
811794
@@ -855,7 +838,7 @@ FrameBatchOutput SingleStreamDecoder::get_frames_in_range(
855838 frame_batch_output_duration_seconds[f] = frame_output.duration_seconds ;
856839 }
857840 frame_batch_output.data =
858- maybe_permute_and_convert_to_float32 (frame_batch_output.data );
841+ maybe_permute_and_convert_dtype (frame_batch_output.data );
859842 return frame_batch_output;
860843}
861844
@@ -897,7 +880,7 @@ FrameOutput SingleStreamDecoder::get_frame_played_at(double seconds) {
897880
898881 // Convert the frame to tensor.
899882 FrameOutput frame_output = convert_av_frame_to_frame_output (*av_frame);
900- frame_output.data = maybe_permute_and_convert_to_float32 (frame_output.data );
883+ frame_output.data = maybe_permute_and_convert_dtype (frame_output.data );
901884 return frame_output;
902885}
903886
@@ -987,7 +970,7 @@ FrameBatchOutput SingleStreamDecoder::get_frames_played_in_range(
987970 device_interface_->get_pre_allocation_dtype (
988971 video_stream_options.output_dtype ));
989972 frame_batch_output.data =
990- maybe_permute_and_convert_to_float32 (frame_batch_output.data );
973+ maybe_permute_and_convert_dtype (frame_batch_output.data );
991974 return frame_batch_output;
992975 }
993976
@@ -1061,7 +1044,7 @@ FrameBatchOutput SingleStreamDecoder::get_frames_played_in_range(
10611044 }
10621045
10631046 frame_batch_output.data =
1064- maybe_permute_and_convert_to_float32 (frame_batch_output.data );
1047+ maybe_permute_and_convert_dtype (frame_batch_output.data );
10651048 return frame_batch_output;
10661049 } else {
10671050 // Note that we look at nextPts for a frame, and not its pts or duration.
@@ -1098,7 +1081,7 @@ FrameBatchOutput SingleStreamDecoder::get_frames_played_in_range(
10981081 frame_batch_output_duration_seconds[f] = frame_output.duration_seconds ;
10991082 }
11001083 frame_batch_output.data =
1101- maybe_permute_and_convert_to_float32 (frame_batch_output.data );
1084+ maybe_permute_and_convert_dtype (frame_batch_output.data );
11021085
11031086 return frame_batch_output;
11041087 }
@@ -1692,7 +1675,7 @@ FrameOutput SingleStreamDecoder::convert_av_frame_to_frame_output(
16921675// OUTPUT ALLOCATION AND SHAPE CONVERSION
16931676// --------------------------------------------------------------------------
16941677
1695- torch::stable::Tensor SingleStreamDecoder::maybe_permute_and_convert_to_float32 (
1678+ torch::stable::Tensor SingleStreamDecoder::maybe_permute_and_convert_dtype (
16961679 torch::stable::Tensor& hwc_tensor) {
16971680 // Permute HWC to CHW if needed. Returns a view of the input tensor, the
16981681 // leading batch-dimension [N] is optional i.e. the input tensor can be 3D or
@@ -1718,19 +1701,9 @@ torch::stable::Tensor SingleStreamDecoder::maybe_permute_and_convert_to_float32(
17181701 }
17191702 }
17201703
1721- // Convert to float32 and normalize to [0, 1] if needed.
1722- OutputDtype output_dtype =
1723- stream_infos_[active_stream_index_].video_stream_options .output_dtype ;
1724- if (output_dtype != OutputDtype::FLOAT32 ) {
1725- return tensor;
1726- }
1727- bool is_uint16 =
1728- tensor.scalar_type () == torch::headeronly::ScalarType::UInt16;
1729- double max_val = static_cast <double >(
1730- is_uint16 ? std::numeric_limits<uint16_t >::max ()
1731- : std::numeric_limits<uint8_t >::max ());
1732- auto as_float = torch::stable::to (tensor, kStableFloat32 );
1733- return stable_div (as_float, max_val);
1704+ return convert_to_output_dtype (
1705+ tensor,
1706+ stream_infos_[active_stream_index_].video_stream_options .output_dtype );
17341707}
17351708
17361709// --------------------------------------------------------------------------
0 commit comments