From: Andre Noll Date: Sun, 22 Aug 2021 19:16:48 +0000 (+0200) Subject: mp4: Kill membuffer API. X-Git-Tag: v0.7.1~7^2~35 X-Git-Url: http://git.tuebingen.mpg.de/?a=commitdiff_plain;h=eac096021cc916a4fdddf87dc71548e141c208cf;p=paraslash.git mp4: Kill membuffer API. Thanks to the previous cleanups, create_ilst() is the last remaining membuffer user. Since the size of the ilst atom can be computed as the sum of the tag lengths plus a constant times the number of tag items, we can allocate a suitably sized buffer up-front instead of relying on the membuffer framework to allocate and resize buffers as needed. --- diff --git a/mp4.c b/mp4.c index 1d8af6cb..1f504fc3 100644 --- a/mp4.c +++ b/mp4.c @@ -1083,68 +1083,11 @@ struct mp4_metadata *mp4_get_meta(struct mp4 *f) 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; @@ -1161,21 +1104,17 @@ static void *create_ilst(const struct mp4_metadata *meta, uint32_t *out_size) 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) @@ -1188,13 +1127,14 @@ static void *modify_moov(struct mp4 *f, uint32_t *out_size) 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); @@ -1239,14 +1179,13 @@ static void *modify_moov(struct mp4 *f, uint32_t *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; }