* Try to buffer at least this amount of data before flushing it
*/
int min_packet_size;
+
+ /**
+ * If set, all output will be wrapped in the Metacube format,
+ * for consumption by the Cubemap reflector. This is so that Cubemap
+ * can know what the header is, and where it is possible to start
+ * the stream (ie., from keyframes) without actually parsing and
+ * understanding the mux. Only relevant if write_flag is set.
+ *
+ * When wrapping in Metacube, s->buffer will have room for a 16-byte
+ * Metacube header while writing, which is constructed in avio_flush()
+ * before sending. This header is invisible to the calling code;
+ * e.g., it will not be counted in seeks and similar.
+ */
+ int metacube;
+
+ /**
+ * If the metacube flag is set, we need to know whether we've seen
+ * at least one sync point marker (AVIO_DATA_MARKER_SYNC_POINT),
+ * as many muxes don't send them out at all. If we haven't seen any sync
+ * point markers, we assume that all packets (in particular
+ * AVIO_DATA_MARKER_UNKNOWN) are valid sync start points.
+ * (This may not hold for all codecs in practice.)
+ */
+ int seen_sync_point;
} AVIOContext;
/**
*/
#define AVIO_FLAG_NONBLOCK 8
+/**
+ * If set, all output will be wrapped in the Metacube format.
+ * See AVIOContext::metacube for more information.
+ */
+#define AVIO_FLAG_METACUBE 16
+
/**
* Use direct mode.
* avio_read and avio_write should if possible be satisfied directly
#include "libavutil/log.h"
#include "libavutil/opt.h"
#include "libavutil/avassert.h"
+#include "libavutil/thread.h"
#include "avformat.h"
#include "avio.h"
#include "avio_internal.h"
#include "internal.h"
+#include "metacube2.h"
#include "url.h"
#include <stdarg.h>
s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
s->min_packet_size = 0;
s->max_packet_size = 0;
+ s->metacube = 0;
s->update_checksum = NULL;
s->short_seek_threshold = SHORT_SEEK_THRESHOLD;
s->pos += len;
}
+static AVOnce metacube2_crc_once_control = AV_ONCE_INIT;
+static AVCRC metacube2_crc_table[257];
+
+static void metacube2_crc_init_table_once(void)
+{
+ av_assert0(av_crc_init(metacube2_crc_table, 0, 16, 0x8fdb, sizeof(metacube2_crc_table)) >= 0);
+}
+
+static uint16_t metacube2_compute_crc(const struct metacube2_block_header *hdr)
+{
+ static const int data_len = sizeof(hdr->size) + sizeof(hdr->flags);
+ const uint8_t *data = (uint8_t *)&hdr->size;
+ uint16_t crc;
+
+ ff_thread_once(&metacube2_crc_once_control, metacube2_crc_init_table_once);
+
+ // Metacube2 specifies a CRC start of 0x1234, but its pycrc-derived CRC
+ // includes a finalization step that is done somewhat differently in av_crc().
+ // 0x1234 alone sent through that finalization becomes 0x394a, and then we
+ // need a byte-swap of the CRC value (both on input and output) to account for
+ // differing conventions.
+ crc = av_crc(metacube2_crc_table, 0x4a39, data, data_len);
+ return av_bswap16(crc);
+}
+
+static void finalize_metacube_block_header(AVIOContext *s)
+{
+ struct metacube2_block_header hdr;
+ int len = s->buf_ptr_max - s->buffer;
+ int flags = 0;
+
+ if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT)
+ s->seen_sync_point = 1;
+ else if (s->current_type == AVIO_DATA_MARKER_HEADER)
+ // NOTE: If there are multiple blocks marked METACUBE_FLAGS_HEADER,
+ // only the last one will count. This may become a problem if the
+ // mux flushes halfway through the stream header; if so, we would
+ // need to keep track of and concatenate the different parts.
+ flags |= METACUBE_FLAGS_HEADER;
+ else if (s->seen_sync_point)
+ flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
+
+ memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
+ AV_WB32(&hdr.size, len - sizeof(hdr));
+ AV_WB16(&hdr.flags, flags);
+ AV_WB16(&hdr.csum, metacube2_compute_crc(&hdr));
+ memcpy(s->buffer, &hdr, sizeof(hdr));
+}
+
static void flush_buffer(AVIOContext *s)
{
+ int buffer_empty;
s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
- if (s->write_flag && s->buf_ptr_max > s->buffer) {
+ if (s->metacube)
+ buffer_empty = s->buf_ptr_max <= s->buffer + sizeof(struct metacube2_block_header);
+ else
+ buffer_empty = s->buf_ptr_max <= s->buffer;
+ if (s->write_flag && !buffer_empty) {
+ if (s->metacube)
+ finalize_metacube_block_header(s);
writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
if (s->update_checksum) {
s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
}
}
s->buf_ptr = s->buf_ptr_max = s->buffer;
+
+ // Add space for Metacube header.
+ if (s->write_flag && s->metacube)
+ s->buf_ptr += sizeof(struct metacube2_block_header);
if (!s->write_flag)
s->buf_end = s->buffer;
}
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
{
- if (s->direct && !s->update_checksum) {
+ if (s->direct && !s->update_checksum && !s->metacube) {
avio_flush(s);
writeout(s, buf, size);
return;
if (whence == SEEK_CUR) {
offset1 = pos + (s->buf_ptr - s->buffer);
- if (offset == 0)
- return offset1;
+ if (offset == 0) {
+ if (s->metacube && s->write_flag)
+ return offset1 - sizeof(struct metacube2_block_header);
+ else
+ return offset1;
+ }
if (offset > INT64_MAX - offset1)
return AVERROR(EINVAL);
offset += offset1;
+ } else if (s->metacube && s->write_flag) {
+ offset += sizeof(struct metacube2_block_header);
}
if (offset < 0)
return AVERROR(EINVAL);
s->pos = offset;
}
s->eof_reached = 0;
- return offset;
+ if (s->metacube && s->write_flag)
+ return offset - sizeof(struct metacube2_block_header);
+ else
+ return offset;
}
int64_t avio_skip(AVIOContext *s, int64_t offset)
avio_flush(s);
return;
}
- if (!s->write_data_type)
+ if (!s->write_data_type && !s->metacube)
return;
// If ignoring boundary points, just treat it as unknown
if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
}
(*s)->short_seek_get = (int (*)(void *))ffurl_get_short_seek;
(*s)->av_class = &ff_avio_class;
+ (*s)->metacube = h->flags & AVIO_FLAG_METACUBE;
+ (*s)->seen_sync_point = 0;
return 0;
fail:
av_freep(&buffer);
int ffio_set_buf_size(AVIOContext *s, int buf_size)
{
uint8_t *buffer;
+
+ if (s->metacube)
+ buf_size += sizeof(struct metacube2_block_header);
+
buffer = av_malloc(buf_size);
if (!buffer)
return AVERROR(ENOMEM);
s->orig_buffer_size =
s->buffer_size = buf_size;
s->buf_ptr = s->buf_ptr_max = buffer;
+
+ // Add space for Metacube header.
+ if (s->metacube)
+ s->buf_ptr += sizeof(struct metacube2_block_header);
+
url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
return 0;
}
uint8_t *buffer;
int data_size;
+ if (s->metacube && s->write_flag)
+ buf_size += sizeof(struct metacube2_block_header);
+
if (!s->buffer_size)
return ffio_set_buf_size(s, buf_size);
s->orig_buffer_size = buf_size;
s->buffer_size = buf_size;
s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
+
+ // Add space for Metacube header.
+ if (s->metacube && s->write_flag && data_size == 0)
+ s->buf_ptr += sizeof(struct metacube2_block_header);
+
if (s->write_flag)
s->buf_ptr_max = s->buffer + data_size;
--- /dev/null
+#ifndef AVFORMAT_METACUBE2_H
+#define AVFORMAT_METACUBE2_H
+
+/*
+ * Definitions for the Metacube2 protocol, used to communicate with Cubemap.
+ *
+ * Note: This file is meant to compile as both C and C++, for easier inclusion
+ * in other projects.
+ */
+
+#include <stdint.h>
+
+#define METACUBE2_SYNC "cube!map" /* 8 bytes long. */
+#define METACUBE_FLAGS_HEADER 0x1 /* NOTE: Replaces the previous header. */
+#define METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START 0x2
+
+/*
+ * Metadata packets; should not be counted as data, but rather
+ * parsed (or ignored if you don't understand them).
+ *
+ * Metadata packets start with a uint64_t (network byte order)
+ * that describe the type; the rest is defined by the type.
+ */
+#define METACUBE_FLAGS_METADATA 0x4
+
+struct metacube2_block_header {
+ char sync[8]; /* METACUBE2_SYNC */
+ uint32_t size; /* Network byte order. Does not include header. */
+ uint16_t flags; /* Network byte order. METACUBE_FLAGS_*. */
+ uint16_t csum; /* Network byte order. CRC16 of size and flags.
+ If METACUBE_FLAGS_METADATA is set, inverted
+ so that older clients will ignore it as broken. */
+};
+
+#endif /* AVFORMAT_METACUBE2_H */
{"bitexact", "do not write random/volatile data", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_BITEXACT }, 0, 0, E, "fflags" },
{"shortest", "stop muxing with the shortest stream", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_SHORTEST }, 0, 0, E, "fflags" },
{"autobsf", "add needed bsfs automatically", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_AUTO_BSF }, 0, 0, E, "fflags" },
+{"metacube", "wrap output data in Metacube", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_METACUBE }, 0, 0, E, "fflags" },
{"seek2any", "allow seeking to non-keyframes on demuxer level when supported", OFFSET(seek2any), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, D},
{"analyzeduration", "specify how many microseconds are analyzed to probe the input", OFFSET(max_analyze_duration), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, D},
{"cryptokey", "decryption key", OFFSET(key), AV_OPT_TYPE_BINARY, {.dbl = 0}, 0, 0, D},