int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
- s->buffer = buffer;
+ s->buffer = buffer;
s->buffer_size = buffer_size;
- s->buf_ptr = buffer;
- s->opaque = opaque;
+ s->buf_ptr = buffer;
+ s->opaque = opaque;
+
url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
- s->write_packet = write_packet;
- s->read_packet = read_packet;
- s->seek = seek;
- s->pos = 0;
- s->must_flush = 0;
- s->eof_reached = 0;
- s->error = 0;
- s->seekable = AVIO_SEEKABLE_NORMAL;
+
+ s->write_packet = write_packet;
+ s->read_packet = read_packet;
+ s->seek = seek;
+ s->pos = 0;
+ s->must_flush = 0;
+ s->eof_reached = 0;
+ s->error = 0;
+ s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
s->max_packet_size = 0;
- s->update_checksum= NULL;
- if(!read_packet && !write_flag){
- s->pos = buffer_size;
+ s->update_checksum = NULL;
+
+ if (!read_packet && !write_flag) {
+ s->pos = buffer_size;
s->buf_end = s->buffer + buffer_size;
}
s->read_pause = NULL;
s->read_seek = NULL;
+
return 0;
}
static void flush_buffer(AVIOContext *s)
{
if (s->buf_ptr > s->buffer) {
- if (s->write_packet && !s->error){
- int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
- if(ret < 0){
+ if (s->write_packet && !s->error) {
+ int ret = s->write_packet(s->opaque, s->buffer,
+ s->buf_ptr - s->buffer);
+ if (ret < 0) {
s->error = ret;
}
}
- if(s->update_checksum){
- s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
- s->checksum_ptr= s->buffer;
+ if (s->update_checksum) {
+ s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
+ s->buf_ptr - s->checksum_ptr);
+ s->checksum_ptr = s->buffer;
}
s->pos += s->buf_ptr - s->buffer;
}
}
offset1 = offset - pos;
if (!s->must_flush &&
- offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
+ offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
/* can do the seek inside the buffer */
s->buf_ptr = s->buffer + offset1;
} else if ((!s->seekable ||
} else {
int64_t res;
-#if CONFIG_MUXERS || CONFIG_NETWORK
if (s->write_flag) {
flush_buffer(s);
s->must_flush = 1;
}
-#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
if (!s->seek)
return AVERROR(EPIPE);
if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
{
int64_t size;
- if(!s)
+ if (!s)
return AVERROR(EINVAL);
if (!s->seek)
return AVERROR(ENOSYS);
size = s->seek(s->opaque, 0, AVSEEK_SIZE);
- if(size<0){
+ if (size < 0) {
if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
return size;
size++;
uint16_t tmp;
GET_UTF8(ch, *q++, break;)
- PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
+ PUT_UTF16(ch, tmp, avio_wl16(s, tmp); ret += 2;)
}
avio_wl16(s, 0);
ret += 2;
return ret;
}
-int ff_get_v_length(uint64_t val){
- int i=1;
+int ff_get_v_length(uint64_t val)
+{
+ int i = 1;
- while(val>>=7)
+ while (val >>= 7)
i++;
return i;
}
-void ff_put_v(AVIOContext *bc, uint64_t val){
- int i= ff_get_v_length(val);
+void ff_put_v(AVIOContext *bc, uint64_t val)
+{
+ int i = ff_get_v_length(val);
- while(--i>0)
- avio_w8(bc, 128 | (val>>(7*i)));
+ while (--i > 0)
+ avio_w8(bc, 128 | (val >> (7 * i)));
- avio_w8(bc, val&127);
+ avio_w8(bc, val & 127);
}
void avio_wl64(AVIOContext *s, uint64_t val)
static void fill_buffer(AVIOContext *s)
{
- uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
- int len= s->buffer_size - (dst - s->buffer);
- int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
-
- /* can't fill the buffer without read_packet, just set EOF if appropiate */
+ uint8_t *dst = !s->max_packet_size &&
+ s->buf_end - s->buffer < s->buffer_size ?
+ s->buf_end : s->buffer;
+ int len = s->buffer_size - (dst - s->buffer);
+ int max_buffer_size = s->max_packet_size ?
+ s->max_packet_size : IO_BUFFER_SIZE;
+
+ /* can't fill the buffer without read_packet, just set EOF if appropriate */
if (!s->read_packet && s->buf_ptr >= s->buf_end)
s->eof_reached = 1;
if (s->eof_reached)
return;
- if(s->update_checksum && dst == s->buffer){
- if(s->buf_end > s->checksum_ptr)
- s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
- s->checksum_ptr= s->buffer;
+ if (s->update_checksum && dst == s->buffer) {
+ if (s->buf_end > s->checksum_ptr)
+ s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
+ s->buf_end - s->checksum_ptr);
+ s->checksum_ptr = s->buffer;
}
/* make buffer smaller in case it ended up large after probing */
len = s->buffer_size;
}
- if(s->read_packet)
+ if (s->read_packet)
len = s->read_packet(s->opaque, dst, len);
else
len = 0;
/* do not modify buffer if EOF reached so that a seek back can
be done without rereading data */
s->eof_reached = 1;
- if(len<0)
- s->error= len;
+ if (len < 0)
+ s->error = len;
} else {
s->pos += len;
s->buf_ptr = dst;
return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
}
+unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
+ unsigned int len)
+{
+ return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
+}
+
unsigned long ffio_get_checksum(AVIOContext *s)
{
- s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
- s->update_checksum= NULL;
+ s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
+ s->buf_ptr - s->checksum_ptr);
+ s->update_checksum = NULL;
return s->checksum;
}
unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
unsigned long checksum)
{
- s->update_checksum= update_checksum;
- if(s->update_checksum){
- s->checksum= checksum;
- s->checksum_ptr= s->buf_ptr;
+ s->update_checksum = update_checksum;
+ if (s->update_checksum) {
+ s->checksum = checksum;
+ s->checksum_ptr = s->buf_ptr;
}
}
len = s->buf_end - s->buf_ptr;
if (len > size)
len = size;
- if (len == 0) {
+ if (len == 0 || s->write_flag) {
if(size > s->buffer_size && !s->update_checksum){
if(s->read_packet)
len = s->read_packet(s->opaque, buf, size);
s->buf_ptr = s->buffer;
s->buf_end = s->buffer/* + len*/;
}
- }else{
+ } else {
fill_buffer(s);
len = s->buf_end - s->buf_ptr;
if (len == 0)
return size1 - size;
}
+int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
+{
+ if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
+ *data = s->buf_ptr;
+ s->buf_ptr += size;
+ return size;
+ } else {
+ *data = buf;
+ return avio_read(s, buf, size);
+ }
+}
+
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
{
int len;
- if(size<0)
+ if (size < 0)
return -1;
+ if (s->read_packet && s->write_flag) {
+ len = s->read_packet(s->opaque, buf, size);
+ if (len > 0)
+ s->pos += len;
+ return len;
+ }
+
len = s->buf_end - s->buf_ptr;
if (len == 0) {
+ /* Reset the buf_end pointer to the start of the buffer, to make sure
+ * the fill_buffer call tries to read as much data as fits into the
+ * full buffer, instead of just what space is left after buf_end.
+ * This avoids returning partial packets at the end of the buffer,
+ * for packet based inputs.
+ */
+ s->buf_end = s->buf_ptr = s->buffer;
fill_buffer(s);
len = s->buf_end - s->buf_ptr;
}
if (!s->read_seek)
return AVERROR(ENOSYS);
ret = s->read_seek(h, stream_index, timestamp, flags);
- if(ret >= 0) {
+ if (ret >= 0) {
int64_t pos;
s->buf_ptr = s->buf_end; // Flush buffer
pos = s->seek(h, 0, SEEK_CUR);
/* reallocate buffer if needed */
new_size = d->pos + buf_size;
new_allocated_size = d->allocated_size;
- if(new_size < d->pos || new_size > INT_MAX/2)
+ if (new_size < d->pos || new_size > INT_MAX/2)
return -1;
while (new_size > new_allocated_size) {
if (!new_allocated_size)
}
if (new_allocated_size > d->allocated_size) {
- d->buffer = av_realloc(d->buffer, new_allocated_size);
- if(d->buffer == NULL)
- return AVERROR(ENOMEM);
+ int err;
+ if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
+ d->allocated_size = 0;
+ d->size = 0;
+ return err;
+ }
d->allocated_size = new_allocated_size;
}
memcpy(d->buffer + d->pos, buf, buf_size);
/* packetized write: output the header */
AV_WB32(buf1, buf_size);
- ret= dyn_buf_write(opaque, buf1, 4);
- if(ret < 0)
+ ret = dyn_buf_write(opaque, buf1, 4);
+ if (ret < 0)
return ret;
/* then the data */
DynBuffer *d;
unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
- if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
+ if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
return -1;
d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
if (!d)
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
{
- DynBuffer *d = s->opaque;
+ DynBuffer *d;
int size;
static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
int padding = 0;
+ if (!s) {
+ *pbuffer = NULL;
+ return 0;
+ }
+
/* don't attempt to pad fixed-size packet buffers */
if (!s->max_packet_size) {
avio_write(s, padbuf, sizeof(padbuf));
avio_flush(s);
+ d = s->opaque;
*pbuffer = d->buffer;
size = d->size;
av_free(d);
av_free(s);
return size - padding;
}
+
+static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
+{
+ DynBuffer *d = opaque;
+
+ d->pos += buf_size;
+ if (d->pos > d->size)
+ d->size = d->pos;
+ return buf_size;
+}
+
+int ffio_open_null_buf(AVIOContext **s)
+{
+ int ret = url_open_dyn_buf_internal(s, 0);
+ if (ret >= 0) {
+ AVIOContext *pb = *s;
+ pb->write_packet = null_buf_write;
+ }
+ return ret;
+}
+
+int ffio_close_null_buf(AVIOContext *s)
+{
+ DynBuffer *d = s->opaque;
+ int size;
+
+ avio_flush(s);
+
+ size = d->size;
+ av_free(d);
+ av_free(s);
+ return size;
+}