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
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
@_media_source
class StreamReaderAudioTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestCase):
......@@ -399,9 +414,15 @@ class StreamReaderAudioTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestCase)
# provide the matching dtype
self._test_wav(src, original, fmt=fmt)
if not self.test_fileobj:
# use the internal dtype ffmpeg picks
self._test_wav(src, original, fmt=None)
# use the internal dtype ffmpeg picks
if self.test_fileobj:
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(
["int16", "uint8", "int32"], # "float", "double", "int64"]
......
......@@ -252,8 +252,10 @@ void init_codec_context(
AVBufferRefPtr& pHWBufferRef) {
const AVCodec* pCodec = get_decode_codec(pParams->codec_id, decoder_name);
if (avcodec_parameters_to_context(pCodecContext, pParams) < 0) {
throw std::runtime_error("Failed to set CodecContext parameter.");
int ret = avcodec_parameters_to_context(pCodecContext, pParams);
if (ret < 0) {
throw std::runtime_error(
"Failed to set CodecContext parameter: " + av_err2string(ret));
}
#ifdef USE_CUDA
......@@ -273,16 +275,15 @@ void init_codec_context(
pCodecContext->get_format = get_hw_format;
// 3. Create HW device context and set to pCodecContext.
AVBufferRef* hw_device_ctx = nullptr;
// TODO: check how to deallocate the context
int err = av_hwdevice_ctx_create(
ret = av_hwdevice_ctx_create(
&hw_device_ctx,
AV_HWDEVICE_TYPE_CUDA,
std::to_string(device.index()).c_str(),
nullptr,
0);
if (err < 0) {
if (ret < 0) {
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);
pCodecContext->hw_device_ctx = av_buffer_ref(hw_device_ctx);
......@@ -291,8 +292,10 @@ void init_codec_context(
#endif
AVDictionary* opts = get_option_dict(decoder_option);
if (avcodec_open2(pCodecContext, pCodec, &opts) < 0) {
throw std::runtime_error("Failed to initialize CodecContext.");
ret = avcodec_open2(pCodecContext, pCodec, &opts);
if (ret < 0) {
throw std::runtime_error(
"Failed to initialize CodecContext: " + av_err2string(ret));
}
auto unused_keys = clean_up_dict(opts);
if (unused_keys.size()) {
......
......@@ -235,8 +235,7 @@ void StreamReader::add_stream(
// When media source is file-like object, it is possible that source codec is
// not detected properly.
if (stream->codecpar->format == -1) {
throw std::runtime_error(
"Failed to detect the source stream format. Please provide the decoder to use.");
throw std::runtime_error("Failed to detect the source stream format.");
}
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