| 35 | } |
| 36 | |
| 37 | void SequenceParser::Parse(const TensorSequence& data, SampleWorkspace* ws) { |
| 38 | auto& decoder = GetDecoder(ws->thread_idx()); |
| 39 | if (!decoder) |
| 40 | decoder = imgcodec::NvImageCodecDecoder::Create(instance_, &exec_params_, {}); |
| 41 | Index seq_length = data.tensors.size(); |
| 42 | TensorShape<4> seq_shape; |
| 43 | seq_shape[0] = seq_length; |
| 44 | int64_t& nchannels = seq_shape[3]; |
| 45 | auto& sequence = ws->Output<CPUBackend>(0); |
| 46 | |
| 47 | nvimgcodecDecodeParams_t decode_params = {NVIMGCODEC_STRUCTURE_TYPE_DECODE_PARAMS, |
| 48 | sizeof(nvimgcodecDecodeParams_t), nullptr}; |
| 49 | decode_params.apply_exif_orientation = 1; |
| 50 | |
| 51 | for (int i = 0; i < static_cast<int>(data.tensors.size()); i++) { |
| 52 | nvimgcodecImageInfo_t info{NVIMGCODEC_STRUCTURE_TYPE_IMAGE_INFO, sizeof(nvimgcodecImageInfo_t), |
| 53 | nullptr}; |
| 54 | |
| 55 | auto file_name = data.tensors[i].GetSourceInfo(); |
| 56 | const uint8_t* data_ptr = data.tensors[i].data<uint8_t>(); |
| 57 | size_t data_size = data.tensors[i].size(); |
| 58 | auto encoded_stream = |
| 59 | imgcodec::NvImageCodecCodeStream::FromHostMem(instance_, data_ptr, data_size); |
| 60 | CHECK_NVIMGCODEC(nvimgcodecCodeStreamGetImageInfo(encoded_stream, &info)); |
| 61 | |
| 62 | int64_t new_nchannels = image_type_ == DALI_GRAY ? 1 : |
| 63 | image_type_ == DALI_RGB || image_type_ == DALI_BGR ? 3 : |
| 64 | info.num_planes > 1 ? info.num_planes : |
| 65 | info.plane_info[0].num_channels; |
| 66 | if (i == 0) { |
| 67 | seq_shape[1] = info.plane_info[0].height; |
| 68 | seq_shape[2] = info.plane_info[0].width; |
| 69 | nchannels = new_nchannels; |
| 70 | sequence.SetLayout("FHWC"); |
| 71 | sequence.Resize(seq_shape, DALI_UINT8); |
| 72 | } else { |
| 73 | DALI_ENFORCE( |
| 74 | info.plane_info[0].height == seq_shape[1] && info.plane_info[0].width == seq_shape[2] && |
| 75 | new_nchannels == nchannels, |
| 76 | make_string("Expected all frames to have same shape. (", info.plane_info[0].height, ", ", |
| 77 | info.plane_info[0].width, ", ", info.plane_info[0].num_channels, ") != ( ", |
| 78 | seq_shape[1], ", ", seq_shape[2], ", ", seq_shape[3], ")")); |
| 79 | } |
| 80 | |
| 81 | auto view_i = sequence.SubspaceTensor(i); |
| 82 | info.buffer = view_i.raw_mutable_data(); |
| 83 | |
| 84 | // Decode to format |
| 85 | info.color_spec = NVIMGCODEC_COLORSPEC_SRGB; |
| 86 | info.sample_format = image_type_to_sample_format(image_type_); |
| 87 | DALI_ENFORCE(info.sample_format != NVIMGCODEC_SAMPLEFORMAT_UNSUPPORTED, |
| 88 | "Format not supported. Only RGB, BGR, GRAY or ANY_DATA are supported on this operator"); |
| 89 | |
| 90 | info.cuda_stream = nullptr; |
| 91 | info.buffer_kind = NVIMGCODEC_IMAGE_BUFFER_KIND_STRIDED_HOST; |
| 92 | info.num_planes = 1; |
| 93 | info.plane_info[0].height = seq_shape[1]; |
| 94 | info.plane_info[0].width = seq_shape[2]; |
no test coverage detected