static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type, const char *package_name)

{

    MXFContext *mxf = s->priv_data;

    AVIOContext *pb = s->pb;

    int i, track_count = s->nb_streams+1;

    int name_size = mxf_utf16_local_tag_length(package_name);

    int user_comment_count = 0;



    if (type == MaterialPackage) {

        if (mxf->store_user_comments)

            user_comment_count = mxf_write_user_comments(s, s->metadata);

        mxf_write_metadata_key(pb, 0x013600);

        PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);

        klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12*mxf->store_user_comments);

    } else {

        mxf_write_metadata_key(pb, 0x013700);

        PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);

        klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12*mxf->store_user_comments); // 20 bytes length for descriptor reference

    }



    // write uid

    mxf_write_local_tag(pb, 16, 0x3C0A);

    mxf_write_uuid(pb, type, 0);

    av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);

    PRINT_KEY(s, "package uid", pb->buf_ptr - 16);



    // write package umid

    mxf_write_local_tag(pb, 32, 0x4401);

    mxf_write_umid(s, type == SourcePackage);

    PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);



    // package name

    if (name_size)

        mxf_write_local_tag_utf16(pb, 0x4402, package_name);



    // package creation date

    mxf_write_local_tag(pb, 8, 0x4405);

    avio_wb64(pb, mxf->timestamp);



    // package modified date

    mxf_write_local_tag(pb, 8, 0x4404);

    avio_wb64(pb, mxf->timestamp);



    // write track refs

    mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);

    mxf_write_refs_count(pb, track_count);

    mxf_write_uuid(pb, type == MaterialPackage ? Track :

                   Track + TypeBottom, -1); // timecode track

    for (i = 0; i < s->nb_streams; i++)

        mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);



    // write user comment refs

    if (mxf->store_user_comments) {

        mxf_write_local_tag(pb, user_comment_count*16 + 8, 0x4406);

        mxf_write_refs_count(pb, user_comment_count);

        for (i = 0; i < user_comment_count; i++)

            mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);

    }



    // write multiple descriptor reference

    if (type == SourcePackage) {

        mxf_write_local_tag(pb, 16, 0x4701);

        if (s->nb_streams > 1) {

            mxf_write_uuid(pb, MultipleDescriptor, 0);

            mxf_write_multi_descriptor(s);

        } else

            mxf_write_uuid(pb, SubDescriptor, 0);

    }



    // write timecode track

    mxf_write_track(s, mxf->timecode_track, type);

    mxf_write_sequence(s, mxf->timecode_track, type);

    mxf_write_timecode_component(s, mxf->timecode_track, type);



    for (i = 0; i < s->nb_streams; i++) {

        AVStream *st = s->streams[i];

        mxf_write_track(s, st, type);

        mxf_write_sequence(s, st, type);

        mxf_write_structural_component(s, st, type);



        if (type == SourcePackage) {

            MXFStreamContext *sc = st->priv_data;

            mxf_essence_container_uls[sc->index].write_desc(s, st);

        }

    }

}

static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type, const char *package_name)

{

    MXFContext *mxf = s->priv_data;

    AVIOContext *pb = s->pb;

    int i, track_count = s->nb_streams+1;

    int name_size = mxf_utf16_local_tag_length(package_name);

    int user_comment_count = 0;



    if (type == MaterialPackage) {

        if (mxf->store_user_comments)

            user_comment_count = mxf_write_user_comments(s, s->metadata);

        mxf_write_metadata_key(pb, 0x013600);

        PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);

        klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12*mxf->store_user_comments);

    } else {

        mxf_write_metadata_key(pb, 0x013700);

        PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);

        klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12*mxf->store_user_comments); 

    }



    

    mxf_write_local_tag(pb, 16, 0x3C0A);

    mxf_write_uuid(pb, type, 0);

    av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);

    PRINT_KEY(s, "package uid", pb->buf_ptr - 16);



    

    mxf_write_local_tag(pb, 32, 0x4401);

    mxf_write_umid(s, type == SourcePackage);

    PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);



    

    if (name_size)

        mxf_write_local_tag_utf16(pb, 0x4402, package_name);



    

    mxf_write_local_tag(pb, 8, 0x4405);

    avio_wb64(pb, mxf->timestamp);



    

    mxf_write_local_tag(pb, 8, 0x4404);

    avio_wb64(pb, mxf->timestamp);



    

    mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);

    mxf_write_refs_count(pb, track_count);

    mxf_write_uuid(pb, type == MaterialPackage ? Track :

                   Track + TypeBottom, -1); 

    for (i = 0; i < s->nb_streams; i++)

        mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);



    

    if (mxf->store_user_comments) {

        mxf_write_local_tag(pb, user_comment_count*16 + 8, 0x4406);

        mxf_write_refs_count(pb, user_comment_count);

        for (i = 0; i < user_comment_count; i++)

            mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);

    }



    

    if (type == SourcePackage) {

        mxf_write_local_tag(pb, 16, 0x4701);

        if (s->nb_streams > 1) {

            mxf_write_uuid(pb, MultipleDescriptor, 0);

            mxf_write_multi_descriptor(s);

        } else

            mxf_write_uuid(pb, SubDescriptor, 0);

    }



    

    mxf_write_track(s, mxf->timecode_track, type);

    mxf_write_sequence(s, mxf->timecode_track, type);

    mxf_write_timecode_component(s, mxf->timecode_track, type);



    for (i = 0; i < s->nb_streams; i++) {

        AVStream *st = s->streams[i];

        mxf_write_track(s, st, type);

        mxf_write_sequence(s, st, type);

        mxf_write_structural_component(s, st, type);



        if (type == SourcePackage) {

            MXFStreamContext *sc = st->priv_data;

            mxf_essence_container_uls[sc->index].write_desc(s, st);

        }

    }

}

        klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12*mxf->store_user_comments);
static void FUNC_0(AVFormatContext *VAR_0, enum MXFMetadataSetType VAR_1, const char *VAR_2)

{

    MXFContext *mxf = VAR_0->priv_data;

    AVIOContext *pb = VAR_0->pb;

    int VAR_3, VAR_4 = VAR_0->nb_streams+1;

    int VAR_5 = mxf_utf16_local_tag_length(VAR_2);

    int VAR_6 = 0;



    if (VAR_1 == MaterialPackage) {

        if (mxf->store_user_comments)

            VAR_6 = mxf_write_user_comments(VAR_0, VAR_0->metadata);

        mxf_write_metadata_key(pb, 0x013600);

        PRINT_KEY(VAR_0, "Material Package key", pb->buf_ptr - 16);

        klv_encode_ber_length(pb, 92 + VAR_5 + (16*VAR_4) + (16*VAR_6) + 12*mxf->store_user_comments);

    } else {

        mxf_write_metadata_key(pb, 0x013700);

        PRINT_KEY(VAR_0, "Source Package key", pb->buf_ptr - 16);

        klv_encode_ber_length(pb, 112 + VAR_5 + (16*VAR_4) + 12*mxf->store_user_comments); 

    }



    

    mxf_write_local_tag(pb, 16, 0x3C0A);

    mxf_write_uuid(pb, VAR_1, 0);

    av_log(VAR_0,AV_LOG_DEBUG, "package VAR_1:%d\n", VAR_1);

    PRINT_KEY(VAR_0, "package uid", pb->buf_ptr - 16);



    

    mxf_write_local_tag(pb, 32, 0x4401);

    mxf_write_umid(VAR_0, VAR_1 == SourcePackage);

    PRINT_KEY(VAR_0, "package umid second part", pb->buf_ptr - 16);



    

    if (VAR_5)

        mxf_write_local_tag_utf16(pb, 0x4402, VAR_2);



    

    mxf_write_local_tag(pb, 8, 0x4405);

    avio_wb64(pb, mxf->timestamp);



    

    mxf_write_local_tag(pb, 8, 0x4404);

    avio_wb64(pb, mxf->timestamp);



    

    mxf_write_local_tag(pb, VAR_4*16 + 8, 0x4403);

    mxf_write_refs_count(pb, VAR_4);

    mxf_write_uuid(pb, VAR_1 == MaterialPackage ? Track :

                   Track + TypeBottom, -1); 

    for (VAR_3 = 0; VAR_3 < VAR_0->nb_streams; VAR_3++)

        mxf_write_uuid(pb, VAR_1 == MaterialPackage ? Track : Track + TypeBottom, VAR_3);



    

    if (mxf->store_user_comments) {

        mxf_write_local_tag(pb, VAR_6*16 + 8, 0x4406);

        mxf_write_refs_count(pb, VAR_6);

        for (VAR_3 = 0; VAR_3 < VAR_6; VAR_3++)

            mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - VAR_6 + VAR_3);

    }



    

    if (VAR_1 == SourcePackage) {

        mxf_write_local_tag(pb, 16, 0x4701);

        if (VAR_0->nb_streams > 1) {

            mxf_write_uuid(pb, MultipleDescriptor, 0);

            mxf_write_multi_descriptor(VAR_0);

        } else

            mxf_write_uuid(pb, SubDescriptor, 0);

    }



    

    mxf_write_track(VAR_0, mxf->timecode_track, VAR_1);

    mxf_write_sequence(VAR_0, mxf->timecode_track, VAR_1);

    mxf_write_timecode_component(VAR_0, mxf->timecode_track, VAR_1);



    for (VAR_3 = 0; VAR_3 < VAR_0->nb_streams; VAR_3++) {

        AVStream *st = VAR_0->streams[VAR_3];

        mxf_write_track(VAR_0, st, VAR_1);

        mxf_write_sequence(VAR_0, st, VAR_1);

        mxf_write_structural_component(VAR_0, st, VAR_1);



        if (VAR_1 == SourcePackage) {

            MXFStreamContext *sc = st->priv_data;

            mxf_essence_container_uls[sc->index].write_desc(VAR_0, st);

        }

    }

}

static void FUNC_0(AVFormatContext *VAR_0, enum MXFMetadataSetType VAR_1, const char *VAR_2)
{ MXFContext *mxf = VAR_0->priv_data; AVIOContext *pb = VAR_0->pb; int VAR_3, VAR_4 = VAR_0->nb_streams+1; int VAR_5 = mxf_utf16_local_tag_length(VAR_2); int VAR_6 = 0; if (VAR_1 == MaterialPackage) { if (mxf->store_user_comments)
VAR_6 = mxf_write_user_comments(VAR_0, VAR_0->metadata); mxf_write_metadata_key(pb, 0x013600); PRINT_KEY(VAR_0, "Material Package key", pb->buf_ptr - 16); klv_encode_ber_length(pb, 92 + VAR_5 + (16*VAR_4) + (16*VAR_6) + 12*mxf->store_user_comments); } else { mxf_write_metadata_key(pb, 0x013700); PRINT_KEY(VAR_0, "Source Package key", pb->buf_ptr - 16); klv_encode_ber_length(pb, 112 + VAR_5 + (16*VAR_4) + 12*mxf->store_user_comments); } mxf_write_local_tag(pb, 16, 0x3C0A); mxf_write_uuid(pb, VAR_1, 0); av_log(VAR_0,AV_LOG_DEBUG, "package VAR_1:%d\n", VAR_1); PRINT_KEY(VAR_0, "package uid", pb->buf_ptr - 16); mxf_write_local_tag(pb, 32, 0x4401); mxf_write_umid(VAR_0, VAR_1 == SourcePackage); PRINT_KEY(VAR_0, "package umid second part", pb->buf_ptr - 16); if (VAR_5)
mxf_write_local_tag_utf16(pb, 0x4402, VAR_2); mxf_write_local_tag(pb, 8, 0x4405); avio_wb64(pb, mxf->timestamp); mxf_write_local_tag(pb, 8, 0x4404); avio_wb64(pb, mxf->timestamp); mxf_write_local_tag(pb, VAR_4*16 + 8, 0x4403); mxf_write_refs_count(pb, VAR_4); mxf_write_uuid(pb, VAR_1 == MaterialPackage ? Track :
Track + TypeBottom, -1); for (VAR_3 = 0; VAR_3 < VAR_0->nb_streams; VAR_3++) mxf_write_uuid(pb, VAR_1 == MaterialPackage ? Track : Track + TypeBottom, VAR_3); if (mxf->store_user_comments) { mxf_write_local_tag(pb, VAR_6*16 + 8, 0x4406); mxf_write_refs_count(pb, VAR_6); for (VAR_3 = 0; VAR_3 < VAR_6; VAR_3++) mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - VAR_6 + VAR_3); } if (VAR_1 == SourcePackage) { mxf_write_local_tag(pb, 16, 0x4701); if (VAR_0->nb_streams > 1) { mxf_write_uuid(pb, MultipleDescriptor, 0); mxf_write_multi_descriptor(VAR_0); } else mxf_write_uuid(pb, SubDescriptor, 0); } mxf_write_track(VAR_0, mxf->timecode_track, VAR_1); mxf_write_sequence(VAR_0, mxf->timecode_track, VAR_1); mxf_write_timecode_component(VAR_0, mxf->timecode_track, VAR_1); for (VAR_3 = 0; VAR_3 < VAR_0->nb_streams; VAR_3++) { AVStream *st = VAR_0->streams[VAR_3]; mxf_write_track(VAR_0, st, VAR_1); mxf_write_sequence(VAR_0, st, VAR_1); mxf_write_structural_component(VAR_0, st, VAR_1); if (VAR_1 == SourcePackage) { MXFStreamContext *sc = st->priv_data; mxf_essence_container_uls[sc->index].write_desc(VAR_0, st); } } }