static void write_mainheader(NUTContext *nut, AVIOContext *bc)

{

    int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,

        tmp_head_idx;

    int64_t tmp_match;



    ff_put_v(bc, nut->version);

    if (nut->version > 3)

        ff_put_v(bc, nut->minor_version);

    ff_put_v(bc, nut->avf->nb_streams);

    ff_put_v(bc, nut->max_distance);

    ff_put_v(bc, nut->time_base_count);



    for (i = 0; i < nut->time_base_count; i++) {

        ff_put_v(bc, nut->time_base[i].num);

        ff_put_v(bc, nut->time_base[i].den);

    }



    tmp_pts      = 0;

    tmp_mul      = 1;

    tmp_stream   = 0;

    tmp_match    = 1 - (1LL << 62);

    tmp_head_idx = 0;

    for (i = 0; i < 256; ) {

        tmp_fields = 0;

        tmp_size   = 0;

//        tmp_res=0;

        if (tmp_pts      != nut->frame_code[i].pts_delta ) tmp_fields = 1;

        if (tmp_mul      != nut->frame_code[i].size_mul  ) tmp_fields = 2;

        if (tmp_stream   != nut->frame_code[i].stream_id ) tmp_fields = 3;

        if (tmp_size     != nut->frame_code[i].size_lsb  ) tmp_fields = 4;

//        if (tmp_res    != nut->frame_code[i].res            ) tmp_fields=5;

        if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;



        tmp_pts    = nut->frame_code[i].pts_delta;

        tmp_flags  = nut->frame_code[i].flags;

        tmp_stream = nut->frame_code[i].stream_id;

        tmp_mul    = nut->frame_code[i].size_mul;

        tmp_size   = nut->frame_code[i].size_lsb;

//        tmp_res   = nut->frame_code[i].res;

        tmp_head_idx = nut->frame_code[i].header_idx;



        for (j = 0; i < 256; j++, i++) {

            if (i == 'N') {

                j--;

                continue;

            }

            if (nut->frame_code[i].pts_delta  != tmp_pts      ||

                nut->frame_code[i].flags      != tmp_flags    ||

                nut->frame_code[i].stream_id  != tmp_stream   ||

                nut->frame_code[i].size_mul   != tmp_mul      ||

                nut->frame_code[i].size_lsb   != tmp_size + j ||

//              nut->frame_code[i].res        != tmp_res      ||

                nut->frame_code[i].header_idx != tmp_head_idx)

                break;

        }

        if (j != tmp_mul - tmp_size)

            tmp_fields = 6;



        ff_put_v(bc, tmp_flags);

        ff_put_v(bc, tmp_fields);

        if (tmp_fields > 0) put_s(bc, tmp_pts);

        if (tmp_fields > 1) ff_put_v(bc, tmp_mul);

        if (tmp_fields > 2) ff_put_v(bc, tmp_stream);

        if (tmp_fields > 3) ff_put_v(bc, tmp_size);

        if (tmp_fields > 4) ff_put_v(bc, 0 /*tmp_res*/);

        if (tmp_fields > 5) ff_put_v(bc, j);

        if (tmp_fields > 6) ff_put_v(bc, tmp_match);

        if (tmp_fields > 7) ff_put_v(bc, tmp_head_idx);

    }

    ff_put_v(bc, nut->header_count - 1);

    for (i = 1; i < nut->header_count; i++) {

        ff_put_v(bc, nut->header_len[i]);

        avio_write(bc, nut->header[i], nut->header_len[i]);

    }

    // flags had been effectively introduced in version 4

    if (nut->version > NUT_STABLE_VERSION)

        ff_put_v(bc, nut->flags);

}

static void write_mainheader(NUTContext *nut, AVIOContext *bc)

{

    int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,

        tmp_head_idx;

    int64_t tmp_match;



    ff_put_v(bc, nut->version);

    if (nut->version > 3)

        ff_put_v(bc, nut->minor_version);

    ff_put_v(bc, nut->avf->nb_streams);

    ff_put_v(bc, nut->max_distance);

    ff_put_v(bc, nut->time_base_count);



    for (i = 0; i < nut->time_base_count; i++) {

        ff_put_v(bc, nut->time_base[i].num);

        ff_put_v(bc, nut->time_base[i].den);

    }



    tmp_pts      = 0;

    tmp_mul      = 1;

    tmp_stream   = 0;

    tmp_match    = 1 - (1LL << 62);

    tmp_head_idx = 0;

    for (i = 0; i < 256; ) {

        tmp_fields = 0;

        tmp_size   = 0;



        if (tmp_pts      != nut->frame_code[i].pts_delta ) tmp_fields = 1;

        if (tmp_mul      != nut->frame_code[i].size_mul  ) tmp_fields = 2;

        if (tmp_stream   != nut->frame_code[i].stream_id ) tmp_fields = 3;

        if (tmp_size     != nut->frame_code[i].size_lsb  ) tmp_fields = 4;



        if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;



        tmp_pts    = nut->frame_code[i].pts_delta;

        tmp_flags  = nut->frame_code[i].flags;

        tmp_stream = nut->frame_code[i].stream_id;

        tmp_mul    = nut->frame_code[i].size_mul;

        tmp_size   = nut->frame_code[i].size_lsb;



        tmp_head_idx = nut->frame_code[i].header_idx;



        for (j = 0; i < 256; j++, i++) {

            if (i == 'N') {

                j--;

                continue;

            }

            if (nut->frame_code[i].pts_delta  != tmp_pts      ||

                nut->frame_code[i].flags      != tmp_flags    ||

                nut->frame_code[i].stream_id  != tmp_stream   ||

                nut->frame_code[i].size_mul   != tmp_mul      ||

                nut->frame_code[i].size_lsb   != tmp_size + j ||



                nut->frame_code[i].header_idx != tmp_head_idx)

                break;

        }

        if (j != tmp_mul - tmp_size)

            tmp_fields = 6;



        ff_put_v(bc, tmp_flags);

        ff_put_v(bc, tmp_fields);

        if (tmp_fields > 0) put_s(bc, tmp_pts);

        if (tmp_fields > 1) ff_put_v(bc, tmp_mul);

        if (tmp_fields > 2) ff_put_v(bc, tmp_stream);

        if (tmp_fields > 3) ff_put_v(bc, tmp_size);

        if (tmp_fields > 4) ff_put_v(bc, 0 );

        if (tmp_fields > 5) ff_put_v(bc, j);

        if (tmp_fields > 6) ff_put_v(bc, tmp_match);

        if (tmp_fields > 7) ff_put_v(bc, tmp_head_idx);

    }

    ff_put_v(bc, nut->header_count - 1);

    for (i = 1; i < nut->header_count; i++) {

        ff_put_v(bc, nut->header_len[i]);

        avio_write(bc, nut->header[i], nut->header_len[i]);

    }

    

    if (nut->version > NUT_STABLE_VERSION)

        ff_put_v(bc, nut->flags);

}

static void FUNC_0(NUTContext *VAR_0, AVIOContext *VAR_1)

{

    int VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9,

        VAR_10;

    int64_t tmp_match;



    ff_put_v(VAR_1, VAR_0->version);

    if (VAR_0->version > 3)

        ff_put_v(VAR_1, VAR_0->minor_version);

    ff_put_v(VAR_1, VAR_0->avf->nb_streams);

    ff_put_v(VAR_1, VAR_0->max_distance);

    ff_put_v(VAR_1, VAR_0->time_base_count);



    for (VAR_2 = 0; VAR_2 < VAR_0->time_base_count; VAR_2++) {

        ff_put_v(VAR_1, VAR_0->time_base[VAR_2].num);

        ff_put_v(VAR_1, VAR_0->time_base[VAR_2].den);

    }



    VAR_4      = 0;

    VAR_7      = 1;

    VAR_6   = 0;

    tmp_match    = 1 - (1LL << 62);

    VAR_10 = 0;

    for (VAR_2 = 0; VAR_2 < 256; ) {

        VAR_9 = 0;

        VAR_8   = 0;



        if (VAR_4      != VAR_0->frame_code[VAR_2].pts_delta ) VAR_9 = 1;

        if (VAR_7      != VAR_0->frame_code[VAR_2].size_mul  ) VAR_9 = 2;

        if (VAR_6   != VAR_0->frame_code[VAR_2].stream_id ) VAR_9 = 3;

        if (VAR_8     != VAR_0->frame_code[VAR_2].size_lsb  ) VAR_9 = 4;



        if (VAR_10 != VAR_0->frame_code[VAR_2].header_idx) VAR_9 = 8;



        VAR_4    = VAR_0->frame_code[VAR_2].pts_delta;

        VAR_5  = VAR_0->frame_code[VAR_2].flags;

        VAR_6 = VAR_0->frame_code[VAR_2].stream_id;

        VAR_7    = VAR_0->frame_code[VAR_2].size_mul;

        VAR_8   = VAR_0->frame_code[VAR_2].size_lsb;



        VAR_10 = VAR_0->frame_code[VAR_2].header_idx;



        for (VAR_3 = 0; VAR_2 < 256; VAR_3++, VAR_2++) {

            if (VAR_2 == 'N') {

                VAR_3--;

                continue;

            }

            if (VAR_0->frame_code[VAR_2].pts_delta  != VAR_4      ||

                VAR_0->frame_code[VAR_2].flags      != VAR_5    ||

                VAR_0->frame_code[VAR_2].stream_id  != VAR_6   ||

                VAR_0->frame_code[VAR_2].size_mul   != VAR_7      ||

                VAR_0->frame_code[VAR_2].size_lsb   != VAR_8 + VAR_3 ||



                VAR_0->frame_code[VAR_2].header_idx != VAR_10)

                break;

        }

        if (VAR_3 != VAR_7 - VAR_8)

            VAR_9 = 6;



        ff_put_v(VAR_1, VAR_5);

        ff_put_v(VAR_1, VAR_9);

        if (VAR_9 > 0) put_s(VAR_1, VAR_4);

        if (VAR_9 > 1) ff_put_v(VAR_1, VAR_7);

        if (VAR_9 > 2) ff_put_v(VAR_1, VAR_6);

        if (VAR_9 > 3) ff_put_v(VAR_1, VAR_8);

        if (VAR_9 > 4) ff_put_v(VAR_1, 0 );

        if (VAR_9 > 5) ff_put_v(VAR_1, VAR_3);

        if (VAR_9 > 6) ff_put_v(VAR_1, tmp_match);

        if (VAR_9 > 7) ff_put_v(VAR_1, VAR_10);

    }

    ff_put_v(VAR_1, VAR_0->header_count - 1);

    for (VAR_2 = 1; VAR_2 < VAR_0->header_count; VAR_2++) {

        ff_put_v(VAR_1, VAR_0->header_len[VAR_2]);

        avio_write(VAR_1, VAR_0->header[VAR_2], VAR_0->header_len[VAR_2]);

    }

    

    if (VAR_0->version > NUT_STABLE_VERSION)

        ff_put_v(VAR_1, VAR_0->flags);

}

static void FUNC_0(NUTContext *VAR_0, AVIOContext *VAR_1)
{ int VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9,
VAR_10; int64_t tmp_match; ff_put_v(VAR_1, VAR_0->version); if (VAR_0->version > 3)
ff_put_v(VAR_1, VAR_0->minor_version); ff_put_v(VAR_1, VAR_0->avf->nb_streams); ff_put_v(VAR_1, VAR_0->max_distance); ff_put_v(VAR_1, VAR_0->time_base_count); for (VAR_2 = 0; VAR_2 < VAR_0->time_base_count; VAR_2++) { ff_put_v(VAR_1, VAR_0->time_base[VAR_2].num); ff_put_v(VAR_1, VAR_0->time_base[VAR_2].den); } VAR_4      = 0; VAR_7      = 1; VAR_6   = 0; tmp_match    = 1 - (1LL << 62); VAR_10 = 0; for (VAR_2 = 0; VAR_2 < 256; ) { VAR_9 = 0; VAR_8   = 0; if (VAR_4      != VAR_0->frame_code[VAR_2].pts_delta ) VAR_9 = 1; if (VAR_7      != VAR_0->frame_code[VAR_2].size_mul  ) VAR_9 = 2; if (VAR_6   != VAR_0->frame_code[VAR_2].stream_id ) VAR_9 = 3; if (VAR_8     != VAR_0->frame_code[VAR_2].size_lsb  ) VAR_9 = 4; if (VAR_10 != VAR_0->frame_code[VAR_2].header_idx) VAR_9 = 8; VAR_4    = VAR_0->frame_code[VAR_2].pts_delta; VAR_5  = VAR_0->frame_code[VAR_2].flags; VAR_6 = VAR_0->frame_code[VAR_2].stream_id; VAR_7    = VAR_0->frame_code[VAR_2].size_mul; VAR_8   = VAR_0->frame_code[VAR_2].size_lsb; VAR_10 = VAR_0->frame_code[VAR_2].header_idx; for (VAR_3 = 0; VAR_2 < 256; VAR_3++, VAR_2++) { if (VAR_2 == 'N') { VAR_3--; continue; } if (VAR_0->frame_code[VAR_2].pts_delta  != VAR_4      ||
VAR_0->frame_code[VAR_2].flags      != VAR_5    ||
VAR_0->frame_code[VAR_2].stream_id  != VAR_6   ||
VAR_0->frame_code[VAR_2].size_mul   != VAR_7      ||
VAR_0->frame_code[VAR_2].size_lsb   != VAR_8 + VAR_3 ||
VAR_0->frame_code[VAR_2].header_idx != VAR_10)
break; } if (VAR_3 != VAR_7 - VAR_8)
VAR_9 = 6; ff_put_v(VAR_1, VAR_5); ff_put_v(VAR_1, VAR_9); if (VAR_9 > 0) put_s(VAR_1, VAR_4); if (VAR_9 > 1) ff_put_v(VAR_1, VAR_7); if (VAR_9 > 2) ff_put_v(VAR_1, VAR_6); if (VAR_9 > 3) ff_put_v(VAR_1, VAR_8); if (VAR_9 > 4) ff_put_v(VAR_1, 0 ); if (VAR_9 > 5) ff_put_v(VAR_1, VAR_3); if (VAR_9 > 6) ff_put_v(VAR_1, tmp_match); if (VAR_9 > 7) ff_put_v(VAR_1, VAR_10); } ff_put_v(VAR_1, VAR_0->header_count - 1); for (VAR_2 = 1; VAR_2 < VAR_0->header_count; VAR_2++) { ff_put_v(VAR_1, VAR_0->header_len[VAR_2]); avio_write(VAR_1, VAR_0->header[VAR_2], VAR_0->header_len[VAR_2]); } if (VAR_0->version > NUT_STABLE_VERSION)
ff_put_v(VAR_1, VAR_0->flags); }