@@ -62,28 +62,26 @@ int64_t getDuration(const AVFrame* frame) {
6262
6363AVIOBytesContext::AVIOBytesContext (
6464 const void * data,
65- size_t data_size,
66- size_t tempBufferSize) {
67- auto buffer = static_cast <uint8_t *>(av_malloc (tempBufferSize));
68- if (!buffer) {
69- throw std::runtime_error (
70- " Failed to allocate buffer of size " + std::to_string (tempBufferSize));
71- }
72- bufferData_.data = static_cast <const uint8_t *>(data);
73- bufferData_.size = data_size;
74- bufferData_.current = 0 ;
65+ size_t dataSize,
66+ size_t bufferSize)
67+ : bufferData_{static_cast <const uint8_t *>(data), dataSize, 0 } {
68+ auto buffer = static_cast <uint8_t *>(av_malloc (bufferSize));
69+ TORCH_CHECK (
70+ buffer != nullptr ,
71+ " Failed to allocate buffer of size " + std::to_string (bufferSize));
7572
7673 avioContext_.reset (avio_alloc_context (
7774 buffer,
78- tempBufferSize ,
75+ bufferSize ,
7976 0 ,
8077 &bufferData_,
8178 &AVIOBytesContext::read,
8279 nullptr ,
8380 &AVIOBytesContext::seek));
81+
8482 if (!avioContext_) {
8583 av_freep (&buffer);
86- throw std::runtime_error ( " Failed to allocate AVIOContext" );
84+ TORCH_CHECK ( false , " Failed to allocate AVIOContext" );
8785 }
8886}
8987
@@ -99,14 +97,14 @@ AVIOContext* AVIOBytesContext::getAVIO() {
9997
10098// The signature of this function is defined by FFMPEG.
10199int AVIOBytesContext::read (void * opaque, uint8_t * buf, int buf_size) {
102- struct AVIOBufferData * bufferData =
103- static_cast <struct AVIOBufferData *>(opaque);
100+ auto bufferData = static_cast <AVIOBufferData*>(opaque);
104101 TORCH_CHECK (
105102 bufferData->current <= bufferData->size ,
106103 " Tried to read outside of the buffer: current=" ,
107104 bufferData->current ,
108105 " , size=" ,
109106 bufferData->size );
107+
110108 buf_size =
111109 FFMIN (buf_size, static_cast <int >(bufferData->size - bufferData->current ));
112110 TORCH_CHECK (
@@ -117,6 +115,7 @@ int AVIOBytesContext::read(void* opaque, uint8_t* buf, int buf_size) {
117115 bufferData->size ,
118116 " , current=" ,
119117 bufferData->current );
118+
120119 if (!buf_size) {
121120 return AVERROR_EOF;
122121 }
@@ -127,7 +126,7 @@ int AVIOBytesContext::read(void* opaque, uint8_t* buf, int buf_size) {
127126
128127// The signature of this function is defined by FFMPEG.
129128int64_t AVIOBytesContext::seek (void * opaque, int64_t offset, int whence) {
130- AVIOBufferData* bufferData = ( AVIOBufferData*) opaque;
129+ auto bufferData = static_cast < AVIOBufferData*>( opaque) ;
131130 int64_t ret = -1 ;
132131
133132 switch (whence) {
@@ -141,6 +140,7 @@ int64_t AVIOBytesContext::seek(void* opaque, int64_t offset, int whence) {
141140 default :
142141 break ;
143142 }
143+
144144 return ret;
145145}
146146
0 commit comments