return &f->meta;
}
-struct membuffer {
- void *data;
- unsigned written;
- unsigned allocated;
-};
-
-static struct membuffer *membuffer_create(void)
-{
- struct membuffer *buf = para_calloc(sizeof(*buf));
-
- buf->allocated = 256;
- buf->data = para_malloc(buf->allocated);
- return buf;
-}
-
-static void membuffer_write(struct membuffer *buf, const void *ptr,
- unsigned bytes)
-{
- unsigned dest_size = buf->written + bytes;
-
- if (dest_size > buf->allocated) {
- do {
- buf->allocated <<= 1;
- } while (dest_size > buf->allocated);
- buf->data = para_realloc(buf->data, buf->allocated);
- }
-
- if (ptr)
- memcpy((char *) buf->data + buf->written, ptr, bytes);
- buf->written += bytes;
-}
-
-static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
-{
- membuffer_write(buf, data, 4);
-}
-
-static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
+/** Total length of an on-disk metadata tag. */
+#define TAG_LEN(_len) (24 + (_len))
+static void create_ilst(const struct mp4_metadata *meta, uint8_t *out)
{
- uint8_t temp[4];
- write_u32_be(temp, data);
- membuffer_write(buf, temp, 4);
-}
-
-static unsigned membuffer_get_size(const struct membuffer *buf)
-{
- return buf->written;
-}
-
-static void *membuffer_detach(struct membuffer *buf)
-{
- void *ret = para_realloc(buf->data, buf->written);
- free(buf);
- return ret;
-}
-
-static void *create_ilst(const struct mp4_metadata *meta, uint32_t *out_size)
-{
- struct membuffer *buf = membuffer_create();
- unsigned n;
-
- for (n = 0; n < meta->count; n++) {
+ for (unsigned n = 0; n < meta->count; n++) {
struct mp4_tag *tag = meta->tags + n;
unsigned len = strlen(tag->value);
const char *atom_name;
atom_name = "\xA9" "cmt";
else
assert(false);
- membuffer_write_int32(buf, 8 /* atom header */
- + 8 /* data atom header */
- + 8 /* flags + reserved */
- + len);
- membuffer_write_atom_name(buf, atom_name);
- membuffer_write_int32(buf, 8 /* data atom header */
+ write_u32_be(out, TAG_LEN(len));
+ memcpy(out + 4, atom_name, 4);
+ write_u32_be(out + 8, 8 /* data atom header */
+ 8 /* flags + reserved */
+ len);
- membuffer_write_atom_name(buf, "data");
- membuffer_write_int32(buf, 1); /* flags */
- membuffer_write_int32(buf, 0); /* reserved */
- membuffer_write(buf, tag->value, len);
+ memcpy(out + 12, "data", 4);
+ write_u32_be(out + 16, 1); /* flags */
+ write_u32_be(out + 20, 0); /* reserved */
+ memcpy(out + 24, tag->value, len);
+ out += TAG_LEN(len);
}
- *out_size = membuffer_get_size(buf);
- return membuffer_detach(buf);
}
static uint32_t fix_byte_order_32(uint32_t src)
int ret;
uint64_t total_base = f->moov_offset + 8;
uint32_t total_size = (uint32_t) (f->moov_size - 8);
- uint32_t new_ilst_size;
- void *new_ilst_buffer, *out_buffer;
+ uint32_t new_ilst_size = 0;
+ void *out_buffer;
uint8_t *p_out;
int32_t size_delta;
uint32_t tmp;
- new_ilst_buffer = create_ilst(&f->meta, &new_ilst_size);
+ for (unsigned n = 0; n < f->meta.count; n++)
+ new_ilst_size += TAG_LEN(strlen(f->meta.tags[n].value));
size_delta = new_ilst_size - (f->ilst_size - 8);
*out_size = total_size + size_delta;
out_buffer = para_malloc(*out_size);
if (ret <= 0)
return NULL;
p_out += 4;
- memcpy(p_out, new_ilst_buffer, new_ilst_size);
+ create_ilst(&f->meta, p_out);
p_out += new_ilst_size;
set_position(f, f->ilst_offset + f->ilst_size);
ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
- f->ilst_size);
if (ret <= 0)
return NULL;
- free(new_ilst_buffer);
return out_buffer;
}