static void mxf_write_partition(AVFormatContext *s, int bodysid,
                                int indexsid,
                                const uint8_t *key, int write_metadata)
{
    MXFContext *mxf = s->priv_data;
    AVIOContext *pb = s->pb;
    int64_t header_byte_count_offset;
    unsigned index_byte_count = 0;
    uint64_t partition_offset = avio_tell(pb);
    if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
        index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
            12+mxf->edit_units_count*(11+mxf->slice_count*4);
    else if (mxf->edit_unit_byte_count && indexsid)
        index_byte_count = 80;
    if (index_byte_count) {
        index_byte_count += 16 + klv_ber_length(index_byte_count);
        index_byte_count += klv_fill_size(index_byte_count);
    }
    if (!memcmp(key, body_partition_key, 16)) {
        mxf->body_partition_offset =
            av_realloc(mxf->body_partition_offset,
                       (mxf->body_partitions_count+1)*
                       sizeof(*mxf->body_partition_offset));
        mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
    }
    avio_write(pb, key, 16);
    klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
    avio_wb16(pb, 1);
    avio_wb16(pb, 2);
    avio_wb32(pb, KAG_SIZE);
    avio_wb64(pb, partition_offset);
    if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
        avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]);
    else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
        avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]);
    else
        avio_wb64(pb, 0);
    avio_wb64(pb, mxf->footer_partition_offset);
    header_byte_count_offset = avio_tell(pb);
    avio_wb64(pb, 0);
    avio_wb64(pb, index_byte_count);
    avio_wb32(pb, index_byte_count ? indexsid : 0);
    if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
        avio_wb64(pb, mxf->body_offset);
    } else
        avio_wb64(pb, 0);
    avio_wb32(pb, bodysid);
    avio_write(pb, op1a_ul, 16);
    mxf_write_essence_container_refs(s);
    if (write_metadata) {
        int64_t pos, start;
        unsigned header_byte_count;
        mxf_write_klv_fill(s);
        start = avio_tell(s->pb);
        mxf_write_primer_pack(s);
        mxf_write_header_metadata_sets(s);
        pos = avio_tell(s->pb);
        header_byte_count = pos - start + klv_fill_size(pos);
        avio_seek(pb, header_byte_count_offset, SEEK_SET);
        avio_wb64(pb, header_byte_count);
        avio_seek(pb, pos, SEEK_SET);
    }
    avio_flush(pb);
}
['static void mxf_write_partition(AVFormatContext *s, int bodysid,\n                                int indexsid,\n                                const uint8_t *key, int write_metadata)\n{\n    MXFContext *mxf = s->priv_data;\n    AVIOContext *pb = s->pb;\n    int64_t header_byte_count_offset;\n    unsigned index_byte_count = 0;\n    uint64_t partition_offset = avio_tell(pb);\n    if (!mxf->edit_unit_byte_count && mxf->edit_units_count)\n        index_byte_count = 85 + 12+(s->nb_streams+1)*6 +\n            12+mxf->edit_units_count*(11+mxf->slice_count*4);\n    else if (mxf->edit_unit_byte_count && indexsid)\n        index_byte_count = 80;\n    if (index_byte_count) {\n        index_byte_count += 16 + klv_ber_length(index_byte_count);\n        index_byte_count += klv_fill_size(index_byte_count);\n    }\n    if (!memcmp(key, body_partition_key, 16)) {\n        mxf->body_partition_offset =\n            av_realloc(mxf->body_partition_offset,\n                       (mxf->body_partitions_count+1)*\n                       sizeof(*mxf->body_partition_offset));\n        mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;\n    }\n    avio_write(pb, key, 16);\n    klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);\n    avio_wb16(pb, 1);\n    avio_wb16(pb, 2);\n    avio_wb32(pb, KAG_SIZE);\n    avio_wb64(pb, partition_offset);\n    if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)\n        avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]);\n    else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)\n        avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]);\n    else\n        avio_wb64(pb, 0);\n    avio_wb64(pb, mxf->footer_partition_offset);\n    header_byte_count_offset = avio_tell(pb);\n    avio_wb64(pb, 0);\n    avio_wb64(pb, index_byte_count);\n    avio_wb32(pb, index_byte_count ? indexsid : 0);\n    if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {\n        avio_wb64(pb, mxf->body_offset);\n    } else\n        avio_wb64(pb, 0);\n    avio_wb32(pb, bodysid);\n    avio_write(pb, op1a_ul, 16);\n    mxf_write_essence_container_refs(s);\n    if (write_metadata) {\n        int64_t pos, start;\n        unsigned header_byte_count;\n        mxf_write_klv_fill(s);\n        start = avio_tell(s->pb);\n        mxf_write_primer_pack(s);\n        mxf_write_header_metadata_sets(s);\n        pos = avio_tell(s->pb);\n        header_byte_count = pos - start + klv_fill_size(pos);\n        avio_seek(pb, header_byte_count_offset, SEEK_SET);\n        avio_wb64(pb, header_byte_count);\n        avio_seek(pb, pos, SEEK_SET);\n    }\n    avio_flush(pb);\n}', 'static av_always_inline int64_t avio_tell(AVIOContext *s)\n{\n    return avio_seek(s, 0, SEEK_CUR);\n}', 'static int klv_ber_length(uint64_t len)\n{\n    if (len < 128)\n        return 1;\n    else\n        return (av_log2(len) >> 3) + 2;\n}', 'static unsigned klv_fill_size(uint64_t size)\n{\n    unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));\n    if (pad < 20)\n        return pad + KAG_SIZE;\n    else\n        return pad & (KAG_SIZE-1);\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n#if CONFIG_MEMALIGN_HACK\n    int diff;\n#endif\n    if (size > (INT_MAX - 16))\n        return NULL;\n#if CONFIG_MEMALIGN_HACK\n    if (!ptr)\n        return av_malloc(size);\n    diff = ((char *)ptr)[-1];\n    return (char *)realloc((char *)ptr - diff, size + diff) + diff;\n#elif HAVE_ALIGNED_MALLOC\n    return _aligned_realloc(ptr, size, 32);\n#else\n    return realloc(ptr, size);\n#endif\n}']