Commit 5d86054a authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

Tweak StreamReader error messages and tests (#2429)

Summary:
* Update error messages
* Update audio stream tests

Pull Request resolved: https://github.com/pytorch/audio/pull/2429

Reviewed By: carolineechen, nateanl

Differential Revision: D36812769

Pulled By: mthrok

fbshipit-source-id: 7a51d0c4dbae558010d2e59412333e4a7f00d318
parent ac82bdc4
...@@ -360,6 +360,21 @@ class StreamReaderInterfaceTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestC ...@@ -360,6 +360,21 @@ class StreamReaderInterfaceTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestC
s.seek(-1.0) s.seek(-1.0)
def _to_fltp(original):
"""Convert Tensor to float32 with value range [-1, 1]"""
denom = {
torch.uint8: 2**7,
torch.int16: 2**15,
torch.int32: 2**31,
}[original.dtype]
fltp = original.to(torch.float32)
if original.dtype == torch.uint8:
fltp -= 128
fltp /= denom
return fltp
@skipIfNoFFmpeg @skipIfNoFFmpeg
@_media_source @_media_source
class StreamReaderAudioTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestCase): class StreamReaderAudioTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestCase):
...@@ -399,9 +414,15 @@ class StreamReaderAudioTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestCase) ...@@ -399,9 +414,15 @@ class StreamReaderAudioTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestCase)
# provide the matching dtype # provide the matching dtype
self._test_wav(src, original, fmt=fmt) self._test_wav(src, original, fmt=fmt)
if not self.test_fileobj: # use the internal dtype ffmpeg picks
# use the internal dtype ffmpeg picks if self.test_fileobj:
self._test_wav(src, original, fmt=None) src.seek(0)
self._test_wav(src, original, fmt=None)
# convert to float32
expected = _to_fltp(original)
if self.test_fileobj:
src.seek(0)
self._test_wav(src, expected, fmt="fltp")
@nested_params( @nested_params(
["int16", "uint8", "int32"], # "float", "double", "int64"] ["int16", "uint8", "int32"], # "float", "double", "int64"]
......
...@@ -252,8 +252,10 @@ void init_codec_context( ...@@ -252,8 +252,10 @@ void init_codec_context(
AVBufferRefPtr& pHWBufferRef) { AVBufferRefPtr& pHWBufferRef) {
const AVCodec* pCodec = get_decode_codec(pParams->codec_id, decoder_name); const AVCodec* pCodec = get_decode_codec(pParams->codec_id, decoder_name);
if (avcodec_parameters_to_context(pCodecContext, pParams) < 0) { int ret = avcodec_parameters_to_context(pCodecContext, pParams);
throw std::runtime_error("Failed to set CodecContext parameter."); if (ret < 0) {
throw std::runtime_error(
"Failed to set CodecContext parameter: " + av_err2string(ret));
} }
#ifdef USE_CUDA #ifdef USE_CUDA
...@@ -273,16 +275,15 @@ void init_codec_context( ...@@ -273,16 +275,15 @@ void init_codec_context(
pCodecContext->get_format = get_hw_format; pCodecContext->get_format = get_hw_format;
// 3. Create HW device context and set to pCodecContext. // 3. Create HW device context and set to pCodecContext.
AVBufferRef* hw_device_ctx = nullptr; AVBufferRef* hw_device_ctx = nullptr;
// TODO: check how to deallocate the context ret = av_hwdevice_ctx_create(
int err = av_hwdevice_ctx_create(
&hw_device_ctx, &hw_device_ctx,
AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_CUDA,
std::to_string(device.index()).c_str(), std::to_string(device.index()).c_str(),
nullptr, nullptr,
0); 0);
if (err < 0) { if (ret < 0) {
throw std::runtime_error( throw std::runtime_error(
"Failed to create CUDA device context: " + av_err2string(err)); "Failed to create CUDA device context: " + av_err2string(ret));
} }
assert(hw_device_ctx); assert(hw_device_ctx);
pCodecContext->hw_device_ctx = av_buffer_ref(hw_device_ctx); pCodecContext->hw_device_ctx = av_buffer_ref(hw_device_ctx);
...@@ -291,8 +292,10 @@ void init_codec_context( ...@@ -291,8 +292,10 @@ void init_codec_context(
#endif #endif
AVDictionary* opts = get_option_dict(decoder_option); AVDictionary* opts = get_option_dict(decoder_option);
if (avcodec_open2(pCodecContext, pCodec, &opts) < 0) { ret = avcodec_open2(pCodecContext, pCodec, &opts);
throw std::runtime_error("Failed to initialize CodecContext."); if (ret < 0) {
throw std::runtime_error(
"Failed to initialize CodecContext: " + av_err2string(ret));
} }
auto unused_keys = clean_up_dict(opts); auto unused_keys = clean_up_dict(opts);
if (unused_keys.size()) { if (unused_keys.size()) {
......
...@@ -235,8 +235,7 @@ void StreamReader::add_stream( ...@@ -235,8 +235,7 @@ void StreamReader::add_stream(
// When media source is file-like object, it is possible that source codec is // When media source is file-like object, it is possible that source codec is
// not detected properly. // not detected properly.
if (stream->codecpar->format == -1) { if (stream->codecpar->format == -1) {
throw std::runtime_error( throw std::runtime_error("Failed to detect the source stream format.");
"Failed to detect the source stream format. Please provide the decoder to use.");
} }
if (!processors[i]) { if (!processors[i]) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment