static int 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);

    int err;



    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) {

        // add encoded ber length

        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)) {

        if ((err = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1,

                                     sizeof(*mxf->body_partition_offset))) < 0) {

            mxf->body_partitions_count = 0;

            return err;

        }

        mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;

    }



    // write klv

    avio_write(pb, key, 16);

    klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);



    // write partition value

    avio_wb16(pb, 1); // majorVersion

    avio_wb16(pb, 2); // minorVersion

    avio_wb32(pb, KAG_SIZE); // KAGSize



    avio_wb64(pb, partition_offset); // ThisPartition



    if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)

        avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition

    else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)

        avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition

    else

        avio_wb64(pb, 0);



    avio_wb64(pb, mxf->footer_partition_offset); // footerPartition



    // set offset

    header_byte_count_offset = avio_tell(pb);

    avio_wb64(pb, 0); // headerByteCount, update later



    // indexTable

    avio_wb64(pb, index_byte_count); // indexByteCount

    avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID



    // BodyOffset

    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); // bodySID



    // operational pattern

    avio_write(pb, op1a_ul, 16);



    // essence container

    mxf_write_essence_container_refs(s);



    if (write_metadata) {

        // mark the start of the headermetadata and calculate metadata size

        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);



        // update header_byte_count

        avio_seek(pb, header_byte_count_offset, SEEK_SET);

        avio_wb64(pb, header_byte_count);

        avio_seek(pb, pos, SEEK_SET);

    }



    avio_flush(pb);



    return 0;

}

static int 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);

    int err;



    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)) {

        if ((err = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1,

                                     sizeof(*mxf->body_partition_offset))) < 0) {

            mxf->body_partitions_count = 0;

            return err;

        }

        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);



    return 0;

}

    klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
static int FUNC_0(AVFormatContext *VAR_0, int VAR_1,

                                int VAR_2,

                                const uint8_t *VAR_3, int VAR_4)

{

    MXFContext *mxf = VAR_0->priv_data;

    AVIOContext *pb = VAR_0->pb;

    int64_t header_byte_count_offset;

    unsigned VAR_5 = 0;

    uint64_t partition_offset = avio_tell(pb);

    int VAR_6;



    if (!mxf->edit_unit_byte_count && mxf->edit_units_count)

        VAR_5 = 85 + 12+(VAR_0->nb_streams+1)*6 +

            12+mxf->edit_units_count*(11+mxf->slice_count*4);

    else if (mxf->edit_unit_byte_count && VAR_2)

        VAR_5 = 80;



    if (VAR_5) {

        

        VAR_5 += 16 + klv_ber_length(VAR_5);

        VAR_5 += klv_fill_size(VAR_5);

    }



    if (!memcmp(VAR_3, body_partition_key, 16)) {

        if ((VAR_6 = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1,

                                     sizeof(*mxf->body_partition_offset))) < 0) {

            mxf->body_partitions_count = 0;

            return VAR_6;

        }

        mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;

    }



    

    avio_write(pb, VAR_3, 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(VAR_3, body_partition_key, 16) && mxf->body_partitions_count > 1)

        avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); 

    else if (!memcmp(VAR_3, 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, VAR_5); 

    avio_wb32(pb, VAR_5 ? VAR_2 : 0); 



    

    if (VAR_1 && mxf->edit_units_count && mxf->body_partitions_count) {

        avio_wb64(pb, mxf->body_offset);

    } else

        avio_wb64(pb, 0);



    avio_wb32(pb, VAR_1); 



    

    avio_write(pb, op1a_ul, 16);



    

    mxf_write_essence_container_refs(VAR_0);



    if (VAR_4) {

        

        int64_t pos, start;

        unsigned VAR_7;



        mxf_write_klv_fill(VAR_0);

        start = avio_tell(VAR_0->pb);

        mxf_write_primer_pack(VAR_0);

        mxf_write_header_metadata_sets(VAR_0);

        pos = avio_tell(VAR_0->pb);

        VAR_7 = pos - start + klv_fill_size(pos);



        

        avio_seek(pb, header_byte_count_offset, SEEK_SET);

        avio_wb64(pb, VAR_7);

        avio_seek(pb, pos, SEEK_SET);

    }



    avio_flush(pb);



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, int VAR_1,
int VAR_2,
const uint8_t *VAR_3, int VAR_4)
{ MXFContext *mxf = VAR_0->priv_data; AVIOContext *pb = VAR_0->pb; int64_t header_byte_count_offset; unsigned VAR_5 = 0; uint64_t partition_offset = avio_tell(pb); int VAR_6; if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
VAR_5 = 85 + 12+(VAR_0->nb_streams+1)*6 +
12+mxf->edit_units_count*(11+mxf->slice_count*4); else if (mxf->edit_unit_byte_count && VAR_2)
VAR_5 = 80; if (VAR_5) { VAR_5 += 16 + klv_ber_length(VAR_5); VAR_5 += klv_fill_size(VAR_5); } if (!memcmp(VAR_3, body_partition_key, 16)) { if ((VAR_6 = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1,
sizeof(*mxf->body_partition_offset))) < 0) { mxf->body_partitions_count = 0; return VAR_6; } mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset; } avio_write(pb, VAR_3, 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(VAR_3, body_partition_key, 16) && mxf->body_partitions_count > 1)
avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); else if (!memcmp(VAR_3, 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, VAR_5); avio_wb32(pb, VAR_5 ? VAR_2 : 0); if (VAR_1 && mxf->edit_units_count && mxf->body_partitions_count) { avio_wb64(pb, mxf->body_offset); } else avio_wb64(pb, 0); avio_wb32(pb, VAR_1); avio_write(pb, op1a_ul, 16); mxf_write_essence_container_refs(VAR_0); if (VAR_4) { int64_t pos, start; unsigned VAR_7; mxf_write_klv_fill(VAR_0); start = avio_tell(VAR_0->pb); mxf_write_primer_pack(VAR_0); mxf_write_header_metadata_sets(VAR_0); pos = avio_tell(VAR_0->pb); VAR_7 = pos - start + klv_fill_size(pos); avio_seek(pb, header_byte_count_offset, SEEK_SET); avio_wb64(pb, VAR_7); avio_seek(pb, pos, SEEK_SET); } avio_flush(pb); return 0; }