int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)

{

    const SPS *sps = h->ps.sps;

    const int max_frame_num = 1 << sps->log2_max_frame_num;

    int field_poc[2];



    h->frame_num_offset = h->prev_frame_num_offset;

    if (h->frame_num < h->prev_frame_num)

        h->frame_num_offset += max_frame_num;



    if (sps->poc_type == 0) {

        const int max_poc_lsb = 1 << sps->log2_max_poc_lsb;



        if (h->poc_lsb < h->prev_poc_lsb &&

            h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)

            h->poc_msb = h->prev_poc_msb + max_poc_lsb;

        else if (h->poc_lsb > h->prev_poc_lsb &&

                 h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)

            h->poc_msb = h->prev_poc_msb - max_poc_lsb;

        else

            h->poc_msb = h->prev_poc_msb;

        field_poc[0] =

        field_poc[1] = h->poc_msb + h->poc_lsb;

        if (h->picture_structure == PICT_FRAME)

            field_poc[1] += h->delta_poc_bottom;

    } else if (sps->poc_type == 1) {

        int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;

        int i;



        if (sps->poc_cycle_length != 0)

            abs_frame_num = h->frame_num_offset + h->frame_num;

        else

            abs_frame_num = 0;



        if (h->nal_ref_idc == 0 && abs_frame_num > 0)

            abs_frame_num--;



        expected_delta_per_poc_cycle = 0;

        for (i = 0; i < sps->poc_cycle_length; i++)

            // FIXME integrate during sps parse

            expected_delta_per_poc_cycle += sps->offset_for_ref_frame[i];



        if (abs_frame_num > 0) {

            int poc_cycle_cnt          = (abs_frame_num - 1) / sps->poc_cycle_length;

            int frame_num_in_poc_cycle = (abs_frame_num - 1) % sps->poc_cycle_length;



            expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;

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

                expectedpoc = expectedpoc + sps->offset_for_ref_frame[i];

        } else

            expectedpoc = 0;



        if (h->nal_ref_idc == 0)

            expectedpoc = expectedpoc + sps->offset_for_non_ref_pic;



        field_poc[0] = expectedpoc + h->delta_poc[0];

        field_poc[1] = field_poc[0] + sps->offset_for_top_to_bottom_field;



        if (h->picture_structure == PICT_FRAME)

            field_poc[1] += h->delta_poc[1];

    } else {

        int poc = 2 * (h->frame_num_offset + h->frame_num);



        if (!h->nal_ref_idc)

            poc--;



        field_poc[0] = poc;

        field_poc[1] = poc;

    }



    if (h->picture_structure != PICT_BOTTOM_FIELD)

        pic_field_poc[0] = field_poc[0];

    if (h->picture_structure != PICT_TOP_FIELD)

        pic_field_poc[1] = field_poc[1];

    *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);



    return 0;

}

int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)

{

    const SPS *sps = h->ps.sps;

    const int max_frame_num = 1 << sps->log2_max_frame_num;

    int field_poc[2];



    h->frame_num_offset = h->prev_frame_num_offset;

    if (h->frame_num < h->prev_frame_num)

        h->frame_num_offset += max_frame_num;



    if (sps->poc_type == 0) {

        const int max_poc_lsb = 1 << sps->log2_max_poc_lsb;



        if (h->poc_lsb < h->prev_poc_lsb &&

            h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)

            h->poc_msb = h->prev_poc_msb + max_poc_lsb;

        else if (h->poc_lsb > h->prev_poc_lsb &&

                 h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)

            h->poc_msb = h->prev_poc_msb - max_poc_lsb;

        else

            h->poc_msb = h->prev_poc_msb;

        field_poc[0] =

        field_poc[1] = h->poc_msb + h->poc_lsb;

        if (h->picture_structure == PICT_FRAME)

            field_poc[1] += h->delta_poc_bottom;

    } else if (sps->poc_type == 1) {

        int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;

        int i;



        if (sps->poc_cycle_length != 0)

            abs_frame_num = h->frame_num_offset + h->frame_num;

        else

            abs_frame_num = 0;



        if (h->nal_ref_idc == 0 && abs_frame_num > 0)

            abs_frame_num--;



        expected_delta_per_poc_cycle = 0;

        for (i = 0; i < sps->poc_cycle_length; i++)

            

            expected_delta_per_poc_cycle += sps->offset_for_ref_frame[i];



        if (abs_frame_num > 0) {

            int poc_cycle_cnt          = (abs_frame_num - 1) / sps->poc_cycle_length;

            int frame_num_in_poc_cycle = (abs_frame_num - 1) % sps->poc_cycle_length;



            expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;

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

                expectedpoc = expectedpoc + sps->offset_for_ref_frame[i];

        } else

            expectedpoc = 0;



        if (h->nal_ref_idc == 0)

            expectedpoc = expectedpoc + sps->offset_for_non_ref_pic;



        field_poc[0] = expectedpoc + h->delta_poc[0];

        field_poc[1] = field_poc[0] + sps->offset_for_top_to_bottom_field;



        if (h->picture_structure == PICT_FRAME)

            field_poc[1] += h->delta_poc[1];

    } else {

        int poc = 2 * (h->frame_num_offset + h->frame_num);



        if (!h->nal_ref_idc)

            poc--;



        field_poc[0] = poc;

        field_poc[1] = poc;

    }



    if (h->picture_structure != PICT_BOTTOM_FIELD)

        pic_field_poc[0] = field_poc[0];

    if (h->picture_structure != PICT_TOP_FIELD)

        pic_field_poc[1] = field_poc[1];

    *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);



    return 0;

}

int FUNC_0(H264Context *VAR_0, int VAR_1[2], int *VAR_2)

{

    const SPS *VAR_3 = VAR_0->ps.VAR_3;

    const int VAR_4 = 1 << VAR_3->log2_max_frame_num;

    int VAR_5[2];



    VAR_0->frame_num_offset = VAR_0->prev_frame_num_offset;

    if (VAR_0->frame_num < VAR_0->prev_frame_num)

        VAR_0->frame_num_offset += VAR_4;



    if (VAR_3->poc_type == 0) {

        const int VAR_6 = 1 << VAR_3->log2_max_poc_lsb;



        if (VAR_0->poc_lsb < VAR_0->prev_poc_lsb &&

            VAR_0->prev_poc_lsb - VAR_0->poc_lsb >= VAR_6 / 2)

            VAR_0->poc_msb = VAR_0->prev_poc_msb + VAR_6;

        else if (VAR_0->poc_lsb > VAR_0->prev_poc_lsb &&

                 VAR_0->prev_poc_lsb - VAR_0->poc_lsb < -VAR_6 / 2)

            VAR_0->poc_msb = VAR_0->prev_poc_msb - VAR_6;

        else

            VAR_0->poc_msb = VAR_0->prev_poc_msb;

        VAR_5[0] =

        VAR_5[1] = VAR_0->poc_msb + VAR_0->poc_lsb;

        if (VAR_0->picture_structure == PICT_FRAME)

            VAR_5[1] += VAR_0->delta_poc_bottom;

    } else if (VAR_3->poc_type == 1) {

        int VAR_7, VAR_8, VAR_9;

        int VAR_10;



        if (VAR_3->poc_cycle_length != 0)

            VAR_7 = VAR_0->frame_num_offset + VAR_0->frame_num;

        else

            VAR_7 = 0;



        if (VAR_0->nal_ref_idc == 0 && VAR_7 > 0)

            VAR_7--;



        VAR_8 = 0;

        for (VAR_10 = 0; VAR_10 < VAR_3->poc_cycle_length; VAR_10++)

            

            VAR_8 += VAR_3->offset_for_ref_frame[VAR_10];



        if (VAR_7 > 0) {

            int VAR_11          = (VAR_7 - 1) / VAR_3->poc_cycle_length;

            int VAR_12 = (VAR_7 - 1) % VAR_3->poc_cycle_length;



            VAR_9 = VAR_11 * VAR_8;

            for (VAR_10 = 0; VAR_10 <= VAR_12; VAR_10++)

                VAR_9 = VAR_9 + VAR_3->offset_for_ref_frame[VAR_10];

        } else

            VAR_9 = 0;



        if (VAR_0->nal_ref_idc == 0)

            VAR_9 = VAR_9 + VAR_3->offset_for_non_ref_pic;



        VAR_5[0] = VAR_9 + VAR_0->delta_poc[0];

        VAR_5[1] = VAR_5[0] + VAR_3->offset_for_top_to_bottom_field;



        if (VAR_0->picture_structure == PICT_FRAME)

            VAR_5[1] += VAR_0->delta_poc[1];

    } else {

        int VAR_13 = 2 * (VAR_0->frame_num_offset + VAR_0->frame_num);



        if (!VAR_0->nal_ref_idc)

            VAR_13--;



        VAR_5[0] = VAR_13;

        VAR_5[1] = VAR_13;

    }



    if (VAR_0->picture_structure != PICT_BOTTOM_FIELD)

        VAR_1[0] = VAR_5[0];

    if (VAR_0->picture_structure != PICT_TOP_FIELD)

        VAR_1[1] = VAR_5[1];

    *VAR_2 = FFMIN(VAR_1[0], VAR_1[1]);



    return 0;

}

int FUNC_0(H264Context *VAR_0, int VAR_1[2], int *VAR_2)
{ const SPS *VAR_3 = VAR_0->ps.VAR_3; const int VAR_4 = 1 << VAR_3->log2_max_frame_num; int VAR_5[2]; VAR_0->frame_num_offset = VAR_0->prev_frame_num_offset; if (VAR_0->frame_num < VAR_0->prev_frame_num)
VAR_0->frame_num_offset += VAR_4; if (VAR_3->poc_type == 0) { const int VAR_6 = 1 << VAR_3->log2_max_poc_lsb; if (VAR_0->poc_lsb < VAR_0->prev_poc_lsb &&
VAR_0->prev_poc_lsb - VAR_0->poc_lsb >= VAR_6 / 2)
VAR_0->poc_msb = VAR_0->prev_poc_msb + VAR_6; else if (VAR_0->poc_lsb > VAR_0->prev_poc_lsb &&
VAR_0->prev_poc_lsb - VAR_0->poc_lsb < -VAR_6 / 2)
VAR_0->poc_msb = VAR_0->prev_poc_msb - VAR_6; else
VAR_0->poc_msb = VAR_0->prev_poc_msb; VAR_5[0] =
VAR_5[1] = VAR_0->poc_msb + VAR_0->poc_lsb; if (VAR_0->picture_structure == PICT_FRAME)
VAR_5[1] += VAR_0->delta_poc_bottom; } else if (VAR_3->poc_type == 1) { int VAR_7, VAR_8, VAR_9; int VAR_10; if (VAR_3->poc_cycle_length != 0)
VAR_7 = VAR_0->frame_num_offset + VAR_0->frame_num; else
VAR_7 = 0; if (VAR_0->nal_ref_idc == 0 && VAR_7 > 0)
VAR_7--; VAR_8 = 0; for (VAR_10 = 0; VAR_10 < VAR_3->poc_cycle_length; VAR_10++) VAR_8 += VAR_3->offset_for_ref_frame[VAR_10]; if (VAR_7 > 0) { int VAR_11          = (VAR_7 - 1) / VAR_3->poc_cycle_length; int VAR_12 = (VAR_7 - 1) % VAR_3->poc_cycle_length; VAR_9 = VAR_11 * VAR_8; for (VAR_10 = 0; VAR_10 <= VAR_12; VAR_10++) VAR_9 = VAR_9 + VAR_3->offset_for_ref_frame[VAR_10]; } else VAR_9 = 0; if (VAR_0->nal_ref_idc == 0)
VAR_9 = VAR_9 + VAR_3->offset_for_non_ref_pic; VAR_5[0] = VAR_9 + VAR_0->delta_poc[0]; VAR_5[1] = VAR_5[0] + VAR_3->offset_for_top_to_bottom_field; if (VAR_0->picture_structure == PICT_FRAME)
VAR_5[1] += VAR_0->delta_poc[1]; } else { int VAR_13 = 2 * (VAR_0->frame_num_offset + VAR_0->frame_num); if (!VAR_0->nal_ref_idc)
VAR_13--; VAR_5[0] = VAR_13; VAR_5[1] = VAR_13; } if (VAR_0->picture_structure != PICT_BOTTOM_FIELD)
VAR_1[0] = VAR_5[0]; if (VAR_0->picture_structure != PICT_TOP_FIELD)
VAR_1[1] = VAR_5[1]; *VAR_2 = FFMIN(VAR_1[0], VAR_1[1]); return 0; }