int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result)
{
- return open_file(cb, true, result);
+ struct mp4 *f;
+ int ret = open_file(cb, true, &f);
+
+ if (ret < 0)
+ return ret;
+ if (f->udta_size == 0 || f->meta_size == 0 || f->ilst_size == 0) {
+ mp4_close(f);
+ *result = NULL;
+ return -E_MP4_MISSING_ATOM;
+ }
+ *result = f;
+ return 1;
}
/**
return membuffer_detach(buf);
}
-static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
- const void *data)
-{
- membuffer_write_int32(buf, size + 8);
- membuffer_write_atom_name(buf, name);
- membuffer_write(buf, data, size);
-}
-
-static void *membuffer_get_ptr(const struct membuffer *buf)
-{
- return buf->data;
-}
-
-static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
- unsigned bytes)
-{
- unsigned oldsize = membuffer_get_size(buf);
- char *bufptr;
-
- membuffer_write(buf, 0, bytes);
- bufptr = membuffer_get_ptr(buf);
- if (read_data(src, bufptr + oldsize, bytes) != 1) {
- free(buf->data);
- free(buf);
- return false;
- }
- return true;
-}
-
-static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
- uint32_t * out_size)
-{
- struct membuffer *buf;
- uint32_t ilst_size;
- void *ilst_buffer;
-
- ilst_buffer = create_ilst(meta, &ilst_size);
-
- buf = membuffer_create();
- membuffer_write_int32(buf, 0);
- membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
- free(ilst_buffer);
-
- *out_size = membuffer_get_size(buf);
- *out_buffer = membuffer_detach(buf);
- return 1;
-}
-
-static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
-uint32_t * out_size)
-{
- struct membuffer *buf;
- uint32_t meta_size;
- void *meta_buffer;
-
- if (!create_meta(meta, &meta_buffer, &meta_size))
- return 0;
-
- buf = membuffer_create();
-
- membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
-
- free(meta_buffer);
-
- *out_size = membuffer_get_size(buf);
- *out_buffer = membuffer_detach(buf);
- return 1;
-}
-
static uint32_t fix_byte_order_32(uint32_t src)
{
return read_u32_be(&src);
int32_t size_delta;
uint32_t tmp;
- if (f->udta_size == 0) {
- struct membuffer *buf;
- void *new_udta_buffer;
- uint32_t new_udta_size;
- if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
- return NULL;
-
- buf = membuffer_create();
- set_position(f, total_base);
- if (!membuffer_transfer_from_file(buf, f, total_size)) {
- free(new_udta_buffer);
- return NULL;
- }
- membuffer_write_atom(buf, "udta", new_udta_size,
- new_udta_buffer);
-
- free(new_udta_buffer);
-
- *out_size = membuffer_get_size(buf);
- return membuffer_detach(buf);
- }
- if (f->meta_size == 0 || f->ilst_size == 0) {
- struct membuffer *buf;
- void *new_meta_buffer;
- uint32_t new_meta_size;
-
- if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
- return NULL;
-
- buf = membuffer_create();
- set_position(f, total_base);
- if (!membuffer_transfer_from_file(buf, f,
- f->udta_offset - total_base)) {
- free(new_meta_buffer);
- return NULL;
- }
-
- membuffer_write_int32(buf, f->udta_size + 8 + new_meta_size);
- membuffer_write_atom_name(buf, "udta");
- if (!membuffer_transfer_from_file(buf, f, f->udta_size)) {
- free(new_meta_buffer);
- return NULL;
- }
- membuffer_write_atom(buf, "meta", new_meta_size,
- new_meta_buffer);
- free(new_meta_buffer);
-
- *out_size = membuffer_get_size(buf);
- return membuffer_detach(buf);
- }
new_ilst_buffer = create_ilst(&f->meta, &new_ilst_size);
size_delta = new_ilst_size - (f->ilst_size - 8);
*out_size = total_size + size_delta;