@ -91,6 +91,7 @@ int ffio_init_context(AVIOContext *s,
s - > orig_buffer_size =
s - > orig_buffer_size =
s - > buffer_size = buffer_size ;
s - > buffer_size = buffer_size ;
s - > buf_ptr = buffer ;
s - > buf_ptr = buffer ;
s - > buf_ptr_max = buffer ;
s - > opaque = opaque ;
s - > opaque = opaque ;
s - > direct = 0 ;
s - > direct = 0 ;
@ -100,7 +101,6 @@ int ffio_init_context(AVIOContext *s,
s - > read_packet = read_packet ;
s - > read_packet = read_packet ;
s - > seek = seek ;
s - > seek = seek ;
s - > pos = 0 ;
s - > pos = 0 ;
s - > must_flush = 0 ;
s - > eof_reached = 0 ;
s - > eof_reached = 0 ;
s - > error = 0 ;
s - > error = 0 ;
s - > seekable = seek ? AVIO_SEEKABLE_NORMAL : 0 ;
s - > seekable = seek ? AVIO_SEEKABLE_NORMAL : 0 ;
@ -171,15 +171,16 @@ static void writeout(AVIOContext *s, const uint8_t *data, int len)
static void flush_buffer ( AVIOContext * s )
static void flush_buffer ( AVIOContext * s )
{
{
if ( s - > write_flag & & s - > buf_ptr > s - > buffer ) {
s - > buf_ptr_max = FFMAX ( s - > buf_ptr , s - > buf_ptr_max ) ;
writeout ( s , s - > buffer , s - > buf_ptr - s - > buffer ) ;
if ( s - > write_flag & & s - > buf_ptr_max > s - > buffer ) {
writeout ( s , s - > buffer , s - > buf_ptr_max - s - > buffer ) ;
if ( s - > update_checksum ) {
if ( s - > update_checksum ) {
s - > checksum = s - > update_checksum ( s - > checksum , s - > checksum_ptr ,
s - > checksum = s - > update_checksum ( s - > checksum , s - > checksum_ptr ,
s - > buf_ptr - s - > checksum_ptr ) ;
s - > buf_ptr_max - s - > checksum_ptr ) ;
s - > checksum_ptr = s - > buffer ;
s - > checksum_ptr = s - > buffer ;
}
}
}
}
s - > buf_ptr = s - > buffer ;
s - > buf_ptr = s - > buf_ptr_max = s - > buf fer ;
if ( ! s - > write_flag )
if ( ! s - > write_flag )
s - > buf_end = s - > buffer ;
s - > buf_end = s - > buffer ;
}
}
@ -228,8 +229,10 @@ void avio_write(AVIOContext *s, const unsigned char *buf, int size)
void avio_flush ( AVIOContext * s )
void avio_flush ( AVIOContext * s )
{
{
int seekback = s - > write_flag ? FFMIN ( 0 , s - > buf_ptr - s - > buf_ptr_max ) : 0 ;
flush_buffer ( s ) ;
flush_buffer ( s ) ;
s - > must_flush = 0 ;
if ( seekback )
avio_seek ( s , seekback , SEEK_CUR ) ;
}
}
int64_t avio_seek ( AVIOContext * s , int64_t offset , int whence )
int64_t avio_seek ( AVIOContext * s , int64_t offset , int whence )
@ -269,8 +272,9 @@ int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
short_seek = s - > short_seek_threshold ;
short_seek = s - > short_seek_threshold ;
offset1 = offset - pos ; // "offset1" is the relative offset from the beginning of s->buffer
offset1 = offset - pos ; // "offset1" is the relative offset from the beginning of s->buffer
if ( ! s - > must_flush & & ( ! s - > direct | | ! s - > seek ) & &
s - > buf_ptr_max = FFMAX ( s - > buf_ptr_max , s - > buf_ptr ) ;
offset1 > = 0 & & offset1 < = buffer_size - s - > write_flag ) {
if ( ( ! s - > direct | | ! s - > seek ) & &
offset1 > = 0 & & offset1 < = ( s - > write_flag ? s - > buf_ptr_max - s - > buffer : buffer_size ) ) {
/* can do the seek inside the buffer */
/* can do the seek inside the buffer */
s - > buf_ptr = s - > buffer + offset1 ;
s - > buf_ptr = s - > buffer + offset1 ;
} else if ( ( ! ( s - > seekable & AVIO_SEEKABLE_NORMAL ) | |
} else if ( ( ! ( s - > seekable & AVIO_SEEKABLE_NORMAL ) | |
@ -299,7 +303,6 @@ int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
int64_t res ;
int64_t res ;
if ( s - > write_flag ) {
if ( s - > write_flag ) {
flush_buffer ( s ) ;
flush_buffer ( s ) ;
s - > must_flush = 1 ;
}
}
if ( ! s - > seek )
if ( ! s - > seek )
return AVERROR ( EPIPE ) ;
return AVERROR ( EPIPE ) ;
@ -308,7 +311,7 @@ int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
s - > seek_count + + ;
s - > seek_count + + ;
if ( ! s - > write_flag )
if ( ! s - > write_flag )
s - > buf_end = s - > buffer ;
s - > buf_end = s - > buffer ;
s - > buf_ptr = s - > buffer ;
s - > buf_ptr = s - > buf_ptr_max = s - > buf fer ;
s - > pos = offset ;
s - > pos = offset ;
}
}
s - > eof_reached = 0 ;
s - > eof_reached = 0 ;
@ -994,7 +997,7 @@ int ffio_set_buf_size(AVIOContext *s, int buf_size)
s - > buffer = buffer ;
s - > buffer = buffer ;
s - > orig_buffer_size =
s - > orig_buffer_size =
s - > buffer_size = buf_size ;
s - > buffer_size = buf_size ;
s - > buf_ptr = buffer ;
s - > buf_ptr = s - > buf_ptr_max = buffer ;
url_resetbuf ( s , s - > write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ ) ;
url_resetbuf ( s , s - > write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ ) ;
return 0 ;
return 0 ;
}
}
@ -1052,7 +1055,6 @@ int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_si
s - > pos = buf_size ;
s - > pos = buf_size ;
s - > buf_end = s - > buf_ptr + buf_size ;
s - > buf_end = s - > buf_ptr + buf_size ;
s - > eof_reached = 0 ;
s - > eof_reached = 0 ;
s - > must_flush = 0 ;
return 0 ;
return 0 ;
}
}