Browse code

avio: Add an internal function for reading without copying

As long as there is enough contiguous data in the avio buffer,
just return a pointer to it instead of copying it to the caller
provided buffer.

Signed-off-by: Martin Storsjö <martin@martin.st>

Ben Avison authored on 2013/08/01 07:46:08
Showing 2 changed files
... ...
@@ -38,6 +38,23 @@ int ffio_init_context(AVIOContext *s,
38 38
 
39 39
 
40 40
 /**
41
+ * Read size bytes from AVIOContext, returning a pointer.
42
+ * Note that the data pointed at by the returned pointer is only
43
+ * valid until the next call that references the same IO context.
44
+ * @param s IO context
45
+ * @param buf pointer to buffer into which to assemble the requested
46
+ *    data if it is not available in contiguous addresses in the
47
+ *    underlying buffer
48
+ * @param size number of bytes requested
49
+ * @param data address at which to store pointer: this will be a
50
+ *    a direct pointer into the underlying buffer if the requested
51
+ *    number of bytes are available at contiguous addresses, otherwise
52
+ *    will be a copy of buf
53
+ * @return number of bytes read or AVERROR
54
+ */
55
+int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, unsigned char **data);
56
+
57
+/**
41 58
  * Read size bytes from AVIOContext into buf.
42 59
  * This reads at most 1 packet. If that is not enough fewer bytes will be
43 60
  * returned.
... ...
@@ -490,6 +490,18 @@ int avio_read(AVIOContext *s, unsigned char *buf, int size)
490 490
     return size1 - size;
491 491
 }
492 492
 
493
+int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, unsigned char **data)
494
+{
495
+    if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
496
+        *data = s->buf_ptr;
497
+        s->buf_ptr += size;
498
+        return size;
499
+    } else {
500
+        *data = buf;
501
+        return avio_read(s, buf, size);
502
+    }
503
+}
504
+
493 505
 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
494 506
 {
495 507
     int len;