static void flush_buffer_o_direct(ByteIOContext *s)
{
size_t data_len = s->buf_ptr - s->buffer;
+ size_t to_write = data_len & ~(BLOCK_SIZE - 1);
+
+ if (s->flush_all) {
+ s->flush_all = 0;
+ to_write = (data_len + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1);
+ }
- /* Write all the data in the buffer to the disk together with any
- * additional space at the end of the buffer up to the first
- * multiple of BLOCK_SIZE. The last block is always written to the
- * disk despite not being fully filled. If the last block is not
- * fully filled, we keep it (moved to the begginig of the buffer)
- * and any subsequent call to this function will rewrite the block
- * together with the additional data added to the buffer. */
- size_t to_write = (data_len + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1);
- size_t whole_blocks = data_len & ~(BLOCK_SIZE - 1);
- int64_t res = AVERROR(EPIPE);
- //av_log(NULL, AV_LOG_ERROR, "%p: YYY dl=%zd, tw=%zd\n", s, data_len, to_write);
if (data_len > 0) {
if (s->write_packet && !s->error){
int ret= s->write_packet(s->opaque, s->buffer, to_write);
s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
s->checksum_ptr= s->buffer;
}
- s->pos += whole_blocks;
- memcpy(s->buffer, &s->buffer[whole_blocks], data_len - whole_blocks);
- s->buf_ptr -= whole_blocks;
-
- if (s->seek)
- res = s->seek(s->opaque, s->pos, SEEK_SET);
- if (res < 0)
- av_log(NULL, AV_LOG_ERROR, "seek error inside flush_buffer: %lld\n", res);
- //av_log(NULL, AV_LOG_ERROR, "ZZZ tw=%zu, pos=%zd\n", to_write, s->pos);
+ s->pos += to_write;
+ if (data_len > to_write) {
+ /* flush_all was not set */
+ memcpy(s->buffer, &s->buffer[to_write], data_len - to_write);
+ s->buf_ptr -= to_write;
+ } else
+ s->buf_ptr = s->buffer;
}
}
return offset1;
offset += offset1;
}
- av_log(NULL, AV_LOG_ERROR, "%p: SEEK from %llu to %llu\n", s, pos, offset);
offset1 = offset - pos;
if (!s->must_flush &&
offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
/* can do the seek inside the buffer */
s->buf_ptr = s->buffer + offset1;
- av_log(NULL, AV_LOG_ERROR, "Internal seek by %llu\n", offset1);
} else if(s->is_streamed && !s->write_flag &&
offset1 >= 0 && offset1 < (s->buf_end - s->buffer) + (1<<16)){
while(s->pos < offset && !s->eof_reached)
#if CONFIG_MUXERS || CONFIG_NETWORK
if (s->write_flag) {
+ s->flush_all = 1;
flush_buffer(s);
s->must_flush = 1;
}
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
-#warning TODO read the whole block from the file to the buffer
- if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
- return res;
- if (!s->write_flag)
- s->buf_end = s->buffer;
- s->buf_ptr = s->buffer;
- s->pos = offset;
+ if (s->o_direct_flag) {
+ /* Read the whole block from the seek destination */
+ offset1 = offset & ~(BLOCK_SIZE - 1);
+ if (!s->seek || (res = s->seek(s->opaque, offset1, SEEK_SET)) < 0)
+ return res;
+ if ((res = s->read_packet(s->opaque, s->buffer, BLOCK_SIZE)) < 0)
+ return res;
+ if (!s->seek || (res = s->seek(s->opaque, offset1, SEEK_SET)) < 0)
+ return res;
+ s->buf_ptr = s->buffer + offset - offset1;
+ s->pos = offset1;
+ } else {
+ if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
+ return res;
+ if (!s->write_flag)
+ s->buf_end = s->buffer;
+ s->buf_ptr = s->buffer;
+ s->pos = offset;
+ }
}
s->eof_reached = 0;
return offset;
} else {
buffer_size = IO_BUFFER_SIZE;
}
- (*s)->o_direct_flag = !!(h->flags & URL_DIRECT);
- if ((*s)->o_direct_flag) {
+ if (h->flags & URL_DIRECT) {
+ if ((h->flags & URL_RDWR) == 0)
+ return AVERROR(EINVAL); /* We need read beacuse of url_fseek() */
buffer_size = 2*buffer_size+BLOCK_SIZE;
buffer = memalign(sysconf(_SC_PAGESIZE), buffer_size);
- ;
} else {
buffer = av_malloc(buffer_size);
}
}
(*s)->is_streamed = h->is_streamed;
(*s)->max_packet_size = max_packet_size;
+ (*s)->o_direct_flag = !!(h->flags & URL_DIRECT);
if(h->prot) {
(*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
(*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;