+ByteIOContext *av_alloc_put_byte(
+ unsigned char *buffer,
+ int buffer_size,
+ int write_flag,
+ void *opaque,
+ int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
+ int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
+ int64_t (*seek)(void *opaque, int64_t offset, int whence))
+{
+ ByteIOContext *s = av_mallocz(sizeof(ByteIOContext));
+ init_put_byte(s, buffer, buffer_size, write_flag, opaque,
+ read_packet, write_packet, seek);
+ return s;
+}
+
+#define BLOCK_SIZE 512
+
+static void flush_buffer_o_direct(ByteIOContext *s)
+{
+ size_t data_len = s->buf_ptr - s->buffer;
+
+ /* Write all 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);
+ if (data_len > 0) {
+ if (s->write_packet && !s->error){
+ int ret= s->write_packet(s->opaque, s->buffer, to_write);
+ if(ret < 0){
+ s->error = ret;
+ }
+ }
+ if(s->update_checksum){
+ av_log(NULL, AV_LOG_ERROR, "Checksumming not implemented in O_DIRECT patch\n");
+ s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
+ s->checksum_ptr= s->buffer;
+ }
+ s->pos += to_write;
+ 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 - to_write + whole_blocks, SEEK_SET);
+ if (res < 0)
+ av_log(NULL, AV_LOG_ERROR, "seek error inside %s: %lld\n", __func__, res);
+ //av_log(NULL, AV_LOG_ERROR, "ZZZ tw=%zu, pos=%zd\n", to_write, s->pos);
+ }
+}
+