@@ -68,16 +68,32 @@ int64_t AVIOBytesContext::seek(void* opaque, int64_t offset, int whence) {
6868}
6969
7070AVIOToTensorContext::AVIOToTensorContext ()
71- : dataContext_{torch::empty ({OUTPUT_TENSOR_SIZE }, {torch::kUInt8 }), 0 } {
71+ : dataContext_{torch::empty ({INITIAL_TENSOR_SIZE }, {torch::kUInt8 }), 0 } {
7272 createAVIOContext (nullptr , &write, &seek, &dataContext_);
7373}
7474
7575// The signature of this function is defined by FFMPEG.
7676int AVIOToTensorContext::write (void * opaque, uint8_t * buf, int buf_size) {
7777 auto dataContext = static_cast <DataContext*>(opaque);
78+
79+ if (dataContext->current + buf_size > dataContext->outputTensor .numel ()) {
80+ TORCH_CHECK (
81+ dataContext->outputTensor .numel () * 2 <= MAX_TENSOR_SIZE,
82+ " We tried to allocate an output encoded tensor larger than " ,
83+ MAX_TENSOR_SIZE,
84+ " bytes. If you think this should be supported, please report." );
85+
86+ // We double the size of the outpout tensor. Calling cat() may not be the
87+ // most efficient, but it's simple.
88+ dataContext->outputTensor =
89+ torch::cat ({dataContext->outputTensor , dataContext->outputTensor });
90+ }
91+
7892 TORCH_CHECK (
79- dataContext->current + buf_size <= OUTPUT_TENSOR_SIZE,
80- " Can't encode more, output tensor needs to be re-allocated and this isn't supported yet." );
93+ dataContext->current + buf_size <= dataContext->outputTensor .numel (),
94+ " Re-allocation of the output tensor didn't work. " ,
95+ " This should not happen, please report on TorchCodec bug tracker" );
96+
8197 uint8_t * outputTensorData = dataContext->outputTensor .data_ptr <uint8_t >();
8298 std::memcpy (outputTensorData + dataContext->current , buf, buf_size);
8399 dataContext->current += static_cast <int64_t >(buf_size);
0 commit comments