static int h264_field_start(H264Context *h, const H264SliceContext *sl,

                            const H2645NAL *nal, int first_slice)

{

    int i;

    const SPS *sps;



    int last_pic_structure, last_pic_droppable, ret;



    ret = h264_init_ps(h, sl, first_slice);

    if (ret < 0)

        return ret;



    sps = h->ps.sps;



    last_pic_droppable   = h->droppable;

    last_pic_structure   = h->picture_structure;

    h->droppable         = (nal->ref_idc == 0);

    h->picture_structure = sl->picture_structure;



    h->poc.frame_num        = sl->frame_num;

    h->poc.poc_lsb          = sl->poc_lsb;

    h->poc.delta_poc_bottom = sl->delta_poc_bottom;

    h->poc.delta_poc[0]     = sl->delta_poc[0];

    h->poc.delta_poc[1]     = sl->delta_poc[1];



    /* Shorten frame num gaps so we don't have to allocate reference

     * frames just to throw them away */

    if (h->poc.frame_num != h->poc.prev_frame_num) {

        int unwrap_prev_frame_num = h->poc.prev_frame_num;

        int max_frame_num         = 1 << sps->log2_max_frame_num;



        if (unwrap_prev_frame_num > h->poc.frame_num)

            unwrap_prev_frame_num -= max_frame_num;



        if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {

            unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;

            if (unwrap_prev_frame_num < 0)

                unwrap_prev_frame_num += max_frame_num;



            h->poc.prev_frame_num = unwrap_prev_frame_num;

        }

    }



    /* See if we have a decoded first field looking for a pair...

     * Here, we're using that to see if we should mark previously

     * decode frames as "finished".

     * We have to do that before the "dummy" in-between frame allocation,

     * since that can modify h->cur_pic_ptr. */

    if (h->first_field) {

        av_assert0(h->cur_pic_ptr);

        av_assert0(h->cur_pic_ptr->f->buf[0]);

        assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);



        /* Mark old field/frame as completed */

        if (h->cur_pic_ptr->tf.owner == h->avctx) {

            ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                      last_pic_structure == PICT_BOTTOM_FIELD);

        }



        /* figure out if we have a complementary field pair */

        if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {

            /* Previous field is unmatched. Don't display it, but let it

             * remain for reference if marked as such. */

            if (last_pic_structure != PICT_FRAME) {

                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                          last_pic_structure == PICT_TOP_FIELD);

            }

        } else {

            if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {

                /* This and previous field were reference, but had

                 * different frame_nums. Consider this field first in

                 * pair. Throw away previous field except for reference

                 * purposes. */

                if (last_pic_structure != PICT_FRAME) {

                    ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                              last_pic_structure == PICT_TOP_FIELD);

                }

            } else {

                /* Second field in complementary pair */

                if (!((last_pic_structure   == PICT_TOP_FIELD &&

                       h->picture_structure == PICT_BOTTOM_FIELD) ||

                      (last_pic_structure   == PICT_BOTTOM_FIELD &&

                       h->picture_structure == PICT_TOP_FIELD))) {

                    av_log(h->avctx, AV_LOG_ERROR,

                           "Invalid field mode combination %d/%d\n",

                           last_pic_structure, h->picture_structure);

                    h->picture_structure = last_pic_structure;

                    h->droppable         = last_pic_droppable;

                    return AVERROR_INVALIDDATA;

                } else if (last_pic_droppable != h->droppable) {

                    avpriv_request_sample(h->avctx,

                                          "Found reference and non-reference fields in the same frame, which");

                    h->picture_structure = last_pic_structure;

                    h->droppable         = last_pic_droppable;

                    return AVERROR_PATCHWELCOME;

                }

            }

        }

    }



    while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&

           h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {

        H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;

        av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",

               h->poc.frame_num, h->poc.prev_frame_num);

        if (!sps->gaps_in_frame_num_allowed_flag)

            for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)

                h->last_pocs[i] = INT_MIN;

        ret = h264_frame_start(h);

        if (ret < 0) {

            h->first_field = 0;

            return ret;

        }



        h->poc.prev_frame_num++;

        h->poc.prev_frame_num        %= 1 << sps->log2_max_frame_num;

        h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;

        h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;

        ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);

        ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);



        h->explicit_ref_marking = 0;

        ret = ff_h264_execute_ref_pic_marking(h);

        if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))

            return ret;

        /* Error concealment: If a ref is missing, copy the previous ref

         * in its place.

         * FIXME: Avoiding a memcpy would be nice, but ref handling makes

         * many assumptions about there being no actual duplicates.

         * FIXME: This does not copy padding for out-of-frame motion

         * vectors.  Given we are concealing a lost frame, this probably

         * is not noticeable by comparison, but it should be fixed. */

        if (h->short_ref_count) {

            if (prev &&

                h->short_ref[0]->f->width == prev->f->width &&

                h->short_ref[0]->f->height == prev->f->height &&

                h->short_ref[0]->f->format == prev->f->format) {

                ff_thread_await_progress(&prev->tf, INT_MAX, 0);

                if (prev->field_picture)

                    ff_thread_await_progress(&prev->tf, INT_MAX, 1);

                av_image_copy(h->short_ref[0]->f->data,

                              h->short_ref[0]->f->linesize,

                              (const uint8_t **)prev->f->data,

                              prev->f->linesize,

                              prev->f->format,

                              prev->f->width,

                              prev->f->height);

                h->short_ref[0]->poc = prev->poc + 2;

            }

            h->short_ref[0]->frame_num = h->poc.prev_frame_num;

        }

    }



    /* See if we have a decoded first field looking for a pair...

     * We're using that to see whether to continue decoding in that

     * frame, or to allocate a new one. */

    if (h->first_field) {

        av_assert0(h->cur_pic_ptr);

        av_assert0(h->cur_pic_ptr->f->buf[0]);

        assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);



        /* figure out if we have a complementary field pair */

        if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {

            /* Previous field is unmatched. Don't display it, but let it

             * remain for reference if marked as such. */

            h->missing_fields ++;

            h->cur_pic_ptr = NULL;

            h->first_field = FIELD_PICTURE(h);

        } else {

            h->missing_fields = 0;

            if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {

                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                          h->picture_structure==PICT_BOTTOM_FIELD);

                /* This and the previous field had different frame_nums.

                 * Consider this field first in pair. Throw away previous

                 * one except for reference purposes. */

                h->first_field = 1;

                h->cur_pic_ptr = NULL;

            } else {

                /* Second field in complementary pair */

                h->first_field = 0;

            }

        }

    } else {

        /* Frame or first field in a potentially complementary pair */

        h->first_field = FIELD_PICTURE(h);

    }



    if (!FIELD_PICTURE(h) || h->first_field) {

        if (h264_frame_start(h) < 0) {

            h->first_field = 0;

            return AVERROR_INVALIDDATA;

        }

    } else {

        release_unused_pictures(h, 0);

    }

    /* Some macroblocks can be accessed before they're available in case

    * of lost slices, MBAFF or threading. */

    if (FIELD_PICTURE(h)) {

        for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)

            memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));

    } else {

        memset(h->slice_table, -1,

            (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));

    }



    ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,

                     h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);



    memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));

    h->nb_mmco = sl->nb_mmco;

    h->explicit_ref_marking = sl->explicit_ref_marking;



    h->picture_idr = nal->type == H264_NAL_IDR_SLICE;



    if (h->sei.recovery_point.recovery_frame_cnt >= 0) {

        const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;



        if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)

            h->valid_recovery_point = 1;



        if (   h->recovery_frame < 0

            || av_mod_uintp2(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {

            h->recovery_frame = av_mod_uintp2(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);



            if (!h->valid_recovery_point)

                h->recovery_frame = h->poc.frame_num;

        }

    }



    h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE);



    if (nal->type == H264_NAL_IDR_SLICE ||

        (h->recovery_frame == h->poc.frame_num && nal->ref_idc)) {

        h->recovery_frame         = -1;

        h->cur_pic_ptr->recovered = 1;

    }

    // If we have an IDR, all frames after it in decoded order are

    // "recovered".

    if (nal->type == H264_NAL_IDR_SLICE)

        h->frame_recovered |= FRAME_RECOVERED_IDR;

#if 1

    h->cur_pic_ptr->recovered |= h->frame_recovered;

#else

    h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);

#endif



    /* Set the frame properties/side data. Only done for the second field in

     * field coded frames, since some SEI information is present for each field

     * and is merged by the SEI parsing code. */

    if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) {

        ret = h264_export_frame_props(h);

        if (ret < 0)

            return ret;



        ret = h264_select_output_frame(h);

        if (ret < 0)

            return ret;

    }



    return 0;

}

static int h264_field_start(H264Context *h, const H264SliceContext *sl,

                            const H2645NAL *nal, int first_slice)

{

    int i;

    const SPS *sps;



    int last_pic_structure, last_pic_droppable, ret;



    ret = h264_init_ps(h, sl, first_slice);

    if (ret < 0)

        return ret;



    sps = h->ps.sps;



    last_pic_droppable   = h->droppable;

    last_pic_structure   = h->picture_structure;

    h->droppable         = (nal->ref_idc == 0);

    h->picture_structure = sl->picture_structure;



    h->poc.frame_num        = sl->frame_num;

    h->poc.poc_lsb          = sl->poc_lsb;

    h->poc.delta_poc_bottom = sl->delta_poc_bottom;

    h->poc.delta_poc[0]     = sl->delta_poc[0];

    h->poc.delta_poc[1]     = sl->delta_poc[1];



    



    if (h->poc.frame_num != h->poc.prev_frame_num) {

        int unwrap_prev_frame_num = h->poc.prev_frame_num;

        int max_frame_num         = 1 << sps->log2_max_frame_num;



        if (unwrap_prev_frame_num > h->poc.frame_num)

            unwrap_prev_frame_num -= max_frame_num;



        if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {

            unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;

            if (unwrap_prev_frame_num < 0)

                unwrap_prev_frame_num += max_frame_num;



            h->poc.prev_frame_num = unwrap_prev_frame_num;

        }

    }



    









    if (h->first_field) {

        av_assert0(h->cur_pic_ptr);

        av_assert0(h->cur_pic_ptr->f->buf[0]);

        assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);



        

        if (h->cur_pic_ptr->tf.owner == h->avctx) {

            ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                      last_pic_structure == PICT_BOTTOM_FIELD);

        }



        

        if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {

            



            if (last_pic_structure != PICT_FRAME) {

                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                          last_pic_structure == PICT_TOP_FIELD);

            }

        } else {

            if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {

                







                if (last_pic_structure != PICT_FRAME) {

                    ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                              last_pic_structure == PICT_TOP_FIELD);

                }

            } else {

                

                if (!((last_pic_structure   == PICT_TOP_FIELD &&

                       h->picture_structure == PICT_BOTTOM_FIELD) ||

                      (last_pic_structure   == PICT_BOTTOM_FIELD &&

                       h->picture_structure == PICT_TOP_FIELD))) {

                    av_log(h->avctx, AV_LOG_ERROR,

                           "Invalid field mode combination %d/%d\n",

                           last_pic_structure, h->picture_structure);

                    h->picture_structure = last_pic_structure;

                    h->droppable         = last_pic_droppable;

                    return AVERROR_INVALIDDATA;

                } else if (last_pic_droppable != h->droppable) {

                    avpriv_request_sample(h->avctx,

                                          "Found reference and non-reference fields in the same frame, which");

                    h->picture_structure = last_pic_structure;

                    h->droppable         = last_pic_droppable;

                    return AVERROR_PATCHWELCOME;

                }

            }

        }

    }



    while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&

           h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {

        H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;

        av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",

               h->poc.frame_num, h->poc.prev_frame_num);

        if (!sps->gaps_in_frame_num_allowed_flag)

            for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)

                h->last_pocs[i] = INT_MIN;

        ret = h264_frame_start(h);

        if (ret < 0) {

            h->first_field = 0;

            return ret;

        }



        h->poc.prev_frame_num++;

        h->poc.prev_frame_num        %= 1 << sps->log2_max_frame_num;

        h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;

        h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;

        ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);

        ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);



        h->explicit_ref_marking = 0;

        ret = ff_h264_execute_ref_pic_marking(h);

        if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))

            return ret;

        













        if (h->short_ref_count) {

            if (prev &&

                h->short_ref[0]->f->width == prev->f->width &&

                h->short_ref[0]->f->height == prev->f->height &&

                h->short_ref[0]->f->format == prev->f->format) {

                ff_thread_await_progress(&prev->tf, INT_MAX, 0);

                if (prev->field_picture)

                    ff_thread_await_progress(&prev->tf, INT_MAX, 1);

                av_image_copy(h->short_ref[0]->f->data,

                              h->short_ref[0]->f->linesize,

                              (const uint8_t **)prev->f->data,

                              prev->f->linesize,

                              prev->f->format,

                              prev->f->width,

                              prev->f->height);

                h->short_ref[0]->poc = prev->poc + 2;

            }

            h->short_ref[0]->frame_num = h->poc.prev_frame_num;

        }

    }



    





    if (h->first_field) {

        av_assert0(h->cur_pic_ptr);

        av_assert0(h->cur_pic_ptr->f->buf[0]);

        assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);



        

        if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {

            



            h->missing_fields ++;

            h->cur_pic_ptr = NULL;

            h->first_field = FIELD_PICTURE(h);

        } else {

            h->missing_fields = 0;

            if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {

                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,

                                          h->picture_structure==PICT_BOTTOM_FIELD);

                





                h->first_field = 1;

                h->cur_pic_ptr = NULL;

            } else {

                

                h->first_field = 0;

            }

        }

    } else {

        

        h->first_field = FIELD_PICTURE(h);

    }



    if (!FIELD_PICTURE(h) || h->first_field) {

        if (h264_frame_start(h) < 0) {

            h->first_field = 0;

            return AVERROR_INVALIDDATA;

        }

    } else {

        release_unused_pictures(h, 0);

    }

    



    if (FIELD_PICTURE(h)) {

        for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)

            memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));

    } else {

        memset(h->slice_table, -1,

            (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));

    }



    ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,

                     h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);



    memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));

    h->nb_mmco = sl->nb_mmco;

    h->explicit_ref_marking = sl->explicit_ref_marking;



    h->picture_idr = nal->type == H264_NAL_IDR_SLICE;



    if (h->sei.recovery_point.recovery_frame_cnt >= 0) {

        const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;



        if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)

            h->valid_recovery_point = 1;



        if (   h->recovery_frame < 0

            || av_mod_uintp2(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {

            h->recovery_frame = av_mod_uintp2(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);



            if (!h->valid_recovery_point)

                h->recovery_frame = h->poc.frame_num;

        }

    }



    h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE);



    if (nal->type == H264_NAL_IDR_SLICE ||

        (h->recovery_frame == h->poc.frame_num && nal->ref_idc)) {

        h->recovery_frame         = -1;

        h->cur_pic_ptr->recovered = 1;

    }

    

    

    if (nal->type == H264_NAL_IDR_SLICE)

        h->frame_recovered |= FRAME_RECOVERED_IDR;

#if 1

    h->cur_pic_ptr->recovered |= h->frame_recovered;

#else

    h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);

#endif



    





    if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) {

        ret = h264_export_frame_props(h);

        if (ret < 0)

            return ret;



        ret = h264_select_output_frame(h);

        if (ret < 0)

            return ret;

    }



    return 0;

}

        if (h->cur_pic_ptr->tf.owner == h->avctx) {             ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,                                       last_pic_structure == PICT_BOTTOM_FIELD);
static int FUNC_0(H264Context *VAR_0, const H264SliceContext *VAR_1,

                            const H2645NAL *VAR_2, int VAR_3)

{

    int VAR_4;

    const SPS *VAR_5;



    int VAR_6, VAR_7, VAR_8;



    VAR_8 = h264_init_ps(VAR_0, VAR_1, VAR_3);

    if (VAR_8 < 0)

        return VAR_8;



    VAR_5 = VAR_0->ps.VAR_5;



    VAR_7   = VAR_0->droppable;

    VAR_6   = VAR_0->picture_structure;

    VAR_0->droppable         = (VAR_2->ref_idc == 0);

    VAR_0->picture_structure = VAR_1->picture_structure;



    VAR_0->poc.frame_num        = VAR_1->frame_num;

    VAR_0->poc.poc_lsb          = VAR_1->poc_lsb;

    VAR_0->poc.delta_poc_bottom = VAR_1->delta_poc_bottom;

    VAR_0->poc.delta_poc[0]     = VAR_1->delta_poc[0];

    VAR_0->poc.delta_poc[1]     = VAR_1->delta_poc[1];



    



    if (VAR_0->poc.frame_num != VAR_0->poc.prev_frame_num) {

        int VAR_9 = VAR_0->poc.prev_frame_num;

        int VAR_10         = 1 << VAR_5->log2_max_frame_num;



        if (VAR_9 > VAR_0->poc.frame_num)

            VAR_9 -= VAR_10;



        if ((VAR_0->poc.frame_num - VAR_9) > VAR_5->ref_frame_count) {

            VAR_9 = (VAR_0->poc.frame_num - VAR_5->ref_frame_count) - 1;

            if (VAR_9 < 0)

                VAR_9 += VAR_10;



            VAR_0->poc.prev_frame_num = VAR_9;

        }

    }



    









    if (VAR_0->first_field) {

        av_assert0(VAR_0->cur_pic_ptr);

        av_assert0(VAR_0->cur_pic_ptr->f->buf[0]);

        assert(VAR_0->cur_pic_ptr->reference != DELAYED_PIC_REF);



        

        if (VAR_0->cur_pic_ptr->tf.owner == VAR_0->avctx) {

            ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,

                                      VAR_6 == PICT_BOTTOM_FIELD);

        }



        

        if (!FIELD_PICTURE(VAR_0) || VAR_0->picture_structure == VAR_6) {

            



            if (VAR_6 != PICT_FRAME) {

                ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,

                                          VAR_6 == PICT_TOP_FIELD);

            }

        } else {

            if (VAR_0->cur_pic_ptr->frame_num != VAR_0->poc.frame_num) {

                







                if (VAR_6 != PICT_FRAME) {

                    ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,

                                              VAR_6 == PICT_TOP_FIELD);

                }

            } else {

                

                if (!((VAR_6   == PICT_TOP_FIELD &&

                       VAR_0->picture_structure == PICT_BOTTOM_FIELD) ||

                      (VAR_6   == PICT_BOTTOM_FIELD &&

                       VAR_0->picture_structure == PICT_TOP_FIELD))) {

                    av_log(VAR_0->avctx, AV_LOG_ERROR,

                           "Invalid field mode combination %d/%d\n",

                           VAR_6, VAR_0->picture_structure);

                    VAR_0->picture_structure = VAR_6;

                    VAR_0->droppable         = VAR_7;

                    return AVERROR_INVALIDDATA;

                } else if (VAR_7 != VAR_0->droppable) {

                    avpriv_request_sample(VAR_0->avctx,

                                          "Found reference and non-reference fields in the same frame, which");

                    VAR_0->picture_structure = VAR_6;

                    VAR_0->droppable         = VAR_7;

                    return AVERROR_PATCHWELCOME;

                }

            }

        }

    }



    while (VAR_0->poc.frame_num != VAR_0->poc.prev_frame_num && !VAR_0->first_field &&

           VAR_0->poc.frame_num != (VAR_0->poc.prev_frame_num + 1) % (1 << VAR_5->log2_max_frame_num)) {

        H264Picture *prev = VAR_0->short_ref_count ? VAR_0->short_ref[0] : NULL;

        av_log(VAR_0->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",

               VAR_0->poc.frame_num, VAR_0->poc.prev_frame_num);

        if (!VAR_5->gaps_in_frame_num_allowed_flag)

            for(VAR_4=0; VAR_4<FF_ARRAY_ELEMS(VAR_0->last_pocs); VAR_4++)

                VAR_0->last_pocs[VAR_4] = INT_MIN;

        VAR_8 = h264_frame_start(VAR_0);

        if (VAR_8 < 0) {

            VAR_0->first_field = 0;

            return VAR_8;

        }



        VAR_0->poc.prev_frame_num++;

        VAR_0->poc.prev_frame_num        %= 1 << VAR_5->log2_max_frame_num;

        VAR_0->cur_pic_ptr->frame_num = VAR_0->poc.prev_frame_num;

        VAR_0->cur_pic_ptr->invalid_gap = !VAR_5->gaps_in_frame_num_allowed_flag;

        ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX, 0);

        ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX, 1);



        VAR_0->explicit_ref_marking = 0;

        VAR_8 = ff_h264_execute_ref_pic_marking(VAR_0);

        if (VAR_8 < 0 && (VAR_0->avctx->err_recognition & AV_EF_EXPLODE))

            return VAR_8;

        













        if (VAR_0->short_ref_count) {

            if (prev &&

                VAR_0->short_ref[0]->f->width == prev->f->width &&

                VAR_0->short_ref[0]->f->height == prev->f->height &&

                VAR_0->short_ref[0]->f->format == prev->f->format) {

                ff_thread_await_progress(&prev->tf, INT_MAX, 0);

                if (prev->field_picture)

                    ff_thread_await_progress(&prev->tf, INT_MAX, 1);

                av_image_copy(VAR_0->short_ref[0]->f->data,

                              VAR_0->short_ref[0]->f->linesize,

                              (const uint8_t **)prev->f->data,

                              prev->f->linesize,

                              prev->f->format,

                              prev->f->width,

                              prev->f->height);

                VAR_0->short_ref[0]->poc = prev->poc + 2;

            }

            VAR_0->short_ref[0]->frame_num = VAR_0->poc.prev_frame_num;

        }

    }



    





    if (VAR_0->first_field) {

        av_assert0(VAR_0->cur_pic_ptr);

        av_assert0(VAR_0->cur_pic_ptr->f->buf[0]);

        assert(VAR_0->cur_pic_ptr->reference != DELAYED_PIC_REF);



        

        if (!FIELD_PICTURE(VAR_0) || VAR_0->picture_structure == VAR_6) {

            



            VAR_0->missing_fields ++;

            VAR_0->cur_pic_ptr = NULL;

            VAR_0->first_field = FIELD_PICTURE(VAR_0);

        } else {

            VAR_0->missing_fields = 0;

            if (VAR_0->cur_pic_ptr->frame_num != VAR_0->poc.frame_num) {

                ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,

                                          VAR_0->picture_structure==PICT_BOTTOM_FIELD);

                





                VAR_0->first_field = 1;

                VAR_0->cur_pic_ptr = NULL;

            } else {

                

                VAR_0->first_field = 0;

            }

        }

    } else {

        

        VAR_0->first_field = FIELD_PICTURE(VAR_0);

    }



    if (!FIELD_PICTURE(VAR_0) || VAR_0->first_field) {

        if (h264_frame_start(VAR_0) < 0) {

            VAR_0->first_field = 0;

            return AVERROR_INVALIDDATA;

        }

    } else {

        release_unused_pictures(VAR_0, 0);

    }

    



    if (FIELD_PICTURE(VAR_0)) {

        for(VAR_4 = (VAR_0->picture_structure == PICT_BOTTOM_FIELD); VAR_4<VAR_0->mb_height; VAR_4++)

            memset(VAR_0->slice_table + VAR_4*VAR_0->mb_stride, -1, (VAR_0->mb_stride - (VAR_4+1==VAR_0->mb_height)) * sizeof(*VAR_0->slice_table));

    } else {

        memset(VAR_0->slice_table, -1,

            (VAR_0->mb_height * VAR_0->mb_stride - 1) * sizeof(*VAR_0->slice_table));

    }



    ff_h264_init_poc(VAR_0->cur_pic_ptr->field_poc, &VAR_0->cur_pic_ptr->poc,

                     VAR_0->ps.VAR_5, &VAR_0->poc, VAR_0->picture_structure, VAR_2->ref_idc);



    memcpy(VAR_0->mmco, VAR_1->mmco, VAR_1->nb_mmco * sizeof(*VAR_0->mmco));

    VAR_0->nb_mmco = VAR_1->nb_mmco;

    VAR_0->explicit_ref_marking = VAR_1->explicit_ref_marking;



    VAR_0->picture_idr = VAR_2->type == H264_NAL_IDR_SLICE;



    if (VAR_0->sei.recovery_point.recovery_frame_cnt >= 0) {

        const int VAR_11 = VAR_0->sei.recovery_point.recovery_frame_cnt;



        if (VAR_0->poc.frame_num != VAR_11 || VAR_1->slice_type_nos != AV_PICTURE_TYPE_I)

            VAR_0->valid_recovery_point = 1;



        if (   VAR_0->recovery_frame < 0

            || av_mod_uintp2(VAR_0->recovery_frame - VAR_0->poc.frame_num, VAR_0->ps.VAR_5->log2_max_frame_num) > VAR_11) {

            VAR_0->recovery_frame = av_mod_uintp2(VAR_0->poc.frame_num + VAR_11, VAR_0->ps.VAR_5->log2_max_frame_num);



            if (!VAR_0->valid_recovery_point)

                VAR_0->recovery_frame = VAR_0->poc.frame_num;

        }

    }



    VAR_0->cur_pic_ptr->f->key_frame |= (VAR_2->type == H264_NAL_IDR_SLICE);



    if (VAR_2->type == H264_NAL_IDR_SLICE ||

        (VAR_0->recovery_frame == VAR_0->poc.frame_num && VAR_2->ref_idc)) {

        VAR_0->recovery_frame         = -1;

        VAR_0->cur_pic_ptr->recovered = 1;

    }

    

    

    if (VAR_2->type == H264_NAL_IDR_SLICE)

        VAR_0->frame_recovered |= FRAME_RECOVERED_IDR;

#if 1

    VAR_0->cur_pic_ptr->recovered |= VAR_0->frame_recovered;

#else

    VAR_0->cur_pic_ptr->recovered |= !!(VAR_0->frame_recovered & FRAME_RECOVERED_IDR);

#endif



    





    if (!FIELD_PICTURE(VAR_0) || !VAR_0->first_field || VAR_0->missing_fields > 1) {

        VAR_8 = h264_export_frame_props(VAR_0);

        if (VAR_8 < 0)

            return VAR_8;



        VAR_8 = h264_select_output_frame(VAR_0);

        if (VAR_8 < 0)

            return VAR_8;

    }



    return 0;

}

static int FUNC_0(H264Context *VAR_0, const H264SliceContext *VAR_1,
const H2645NAL *VAR_2, int VAR_3)
{ int VAR_4; const SPS *VAR_5; int VAR_6, VAR_7, VAR_8; VAR_8 = h264_init_ps(VAR_0, VAR_1, VAR_3); if (VAR_8 < 0)
return VAR_8; VAR_5 = VAR_0->ps.VAR_5; VAR_7   = VAR_0->droppable; VAR_6   = VAR_0->picture_structure; VAR_0->droppable         = (VAR_2->ref_idc == 0); VAR_0->picture_structure = VAR_1->picture_structure; VAR_0->poc.frame_num        = VAR_1->frame_num; VAR_0->poc.poc_lsb          = VAR_1->poc_lsb; VAR_0->poc.delta_poc_bottom = VAR_1->delta_poc_bottom; VAR_0->poc.delta_poc[0]     = VAR_1->delta_poc[0]; VAR_0->poc.delta_poc[1]     = VAR_1->delta_poc[1]; if (VAR_0->poc.frame_num != VAR_0->poc.prev_frame_num) { int VAR_9 = VAR_0->poc.prev_frame_num; int VAR_10         = 1 << VAR_5->log2_max_frame_num; if (VAR_9 > VAR_0->poc.frame_num)
VAR_9 -= VAR_10; if ((VAR_0->poc.frame_num - VAR_9) > VAR_5->ref_frame_count) { VAR_9 = (VAR_0->poc.frame_num - VAR_5->ref_frame_count) - 1; if (VAR_9 < 0)
VAR_9 += VAR_10; VAR_0->poc.prev_frame_num = VAR_9; } } if (VAR_0->first_field) { av_assert0(VAR_0->cur_pic_ptr); av_assert0(VAR_0->cur_pic_ptr->f->buf[0]); assert(VAR_0->cur_pic_ptr->reference != DELAYED_PIC_REF); if (VAR_0->cur_pic_ptr->tf.owner == VAR_0->avctx) { ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,
VAR_6 == PICT_BOTTOM_FIELD); } if (!FIELD_PICTURE(VAR_0) || VAR_0->picture_structure == VAR_6) { if (VAR_6 != PICT_FRAME) { ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,
VAR_6 == PICT_TOP_FIELD); } } else { if (VAR_0->cur_pic_ptr->frame_num != VAR_0->poc.frame_num) { if (VAR_6 != PICT_FRAME) { ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,
VAR_6 == PICT_TOP_FIELD); } } else { if (!((VAR_6   == PICT_TOP_FIELD &&
VAR_0->picture_structure == PICT_BOTTOM_FIELD) ||
(VAR_6   == PICT_BOTTOM_FIELD &&
VAR_0->picture_structure == PICT_TOP_FIELD))) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Invalid field mode combination %d/%d\n",
VAR_6, VAR_0->picture_structure); VAR_0->picture_structure = VAR_6; VAR_0->droppable         = VAR_7; return AVERROR_INVALIDDATA; } else if (VAR_7 != VAR_0->droppable) { avpriv_request_sample(VAR_0->avctx,
"Found reference and non-reference fields in the same frame, which"); VAR_0->picture_structure = VAR_6; VAR_0->droppable         = VAR_7; return AVERROR_PATCHWELCOME; } } } } while (VAR_0->poc.frame_num != VAR_0->poc.prev_frame_num && !VAR_0->first_field &&
VAR_0->poc.frame_num != (VAR_0->poc.prev_frame_num + 1) % (1 << VAR_5->log2_max_frame_num)) { H264Picture *prev = VAR_0->short_ref_count ? VAR_0->short_ref[0] : NULL; av_log(VAR_0->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
VAR_0->poc.frame_num, VAR_0->poc.prev_frame_num); if (!VAR_5->gaps_in_frame_num_allowed_flag)
for(VAR_4=0; VAR_4<FF_ARRAY_ELEMS(VAR_0->last_pocs); VAR_4++) VAR_0->last_pocs[VAR_4] = INT_MIN; VAR_8 = h264_frame_start(VAR_0); if (VAR_8 < 0) { VAR_0->first_field = 0; return VAR_8; } VAR_0->poc.prev_frame_num++; VAR_0->poc.prev_frame_num        %= 1 << VAR_5->log2_max_frame_num; VAR_0->cur_pic_ptr->frame_num = VAR_0->poc.prev_frame_num; VAR_0->cur_pic_ptr->invalid_gap = !VAR_5->gaps_in_frame_num_allowed_flag; ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX, 0); ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX, 1); VAR_0->explicit_ref_marking = 0; VAR_8 = ff_h264_execute_ref_pic_marking(VAR_0); if (VAR_8 < 0 && (VAR_0->avctx->err_recognition & AV_EF_EXPLODE))
return VAR_8; if (VAR_0->short_ref_count) { if (prev &&
VAR_0->short_ref[0]->f->width == prev->f->width &&
VAR_0->short_ref[0]->f->height == prev->f->height &&
VAR_0->short_ref[0]->f->format == prev->f->format) { ff_thread_await_progress(&prev->tf, INT_MAX, 0); if (prev->field_picture)
ff_thread_await_progress(&prev->tf, INT_MAX, 1); av_image_copy(VAR_0->short_ref[0]->f->data,
VAR_0->short_ref[0]->f->linesize,
(const uint8_t **)prev->f->data,
prev->f->linesize,
prev->f->format,
prev->f->width,
prev->f->height); VAR_0->short_ref[0]->poc = prev->poc + 2; } VAR_0->short_ref[0]->frame_num = VAR_0->poc.prev_frame_num; } } if (VAR_0->first_field) { av_assert0(VAR_0->cur_pic_ptr); av_assert0(VAR_0->cur_pic_ptr->f->buf[0]); assert(VAR_0->cur_pic_ptr->reference != DELAYED_PIC_REF); if (!FIELD_PICTURE(VAR_0) || VAR_0->picture_structure == VAR_6) { VAR_0->missing_fields ++; VAR_0->cur_pic_ptr = NULL; VAR_0->first_field = FIELD_PICTURE(VAR_0); } else { VAR_0->missing_fields = 0; if (VAR_0->cur_pic_ptr->frame_num != VAR_0->poc.frame_num) { ff_thread_report_progress(&VAR_0->cur_pic_ptr->tf, INT_MAX,
VAR_0->picture_structure==PICT_BOTTOM_FIELD); VAR_0->first_field = 1; VAR_0->cur_pic_ptr = NULL; } else { VAR_0->first_field = 0; } } } else { VAR_0->first_field = FIELD_PICTURE(VAR_0); } if (!FIELD_PICTURE(VAR_0) || VAR_0->first_field) { if (h264_frame_start(VAR_0) < 0) { VAR_0->first_field = 0; return AVERROR_INVALIDDATA; } } else { release_unused_pictures(VAR_0, 0); } if (FIELD_PICTURE(VAR_0)) { for(VAR_4 = (VAR_0->picture_structure == PICT_BOTTOM_FIELD); VAR_4<VAR_0->mb_height; VAR_4++) memset(VAR_0->slice_table + VAR_4*VAR_0->mb_stride, -1, (VAR_0->mb_stride - (VAR_4+1==VAR_0->mb_height)) * sizeof(*VAR_0->slice_table)); } else { memset(VAR_0->slice_table, -1,
(VAR_0->mb_height * VAR_0->mb_stride - 1) * sizeof(*VAR_0->slice_table)); } ff_h264_init_poc(VAR_0->cur_pic_ptr->field_poc, &VAR_0->cur_pic_ptr->poc,
VAR_0->ps.VAR_5, &VAR_0->poc, VAR_0->picture_structure, VAR_2->ref_idc); memcpy(VAR_0->mmco, VAR_1->mmco, VAR_1->nb_mmco * sizeof(*VAR_0->mmco)); VAR_0->nb_mmco = VAR_1->nb_mmco; VAR_0->explicit_ref_marking = VAR_1->explicit_ref_marking; VAR_0->picture_idr = VAR_2->type == H264_NAL_IDR_SLICE; if (VAR_0->sei.recovery_point.recovery_frame_cnt >= 0) { const int VAR_11 = VAR_0->sei.recovery_point.recovery_frame_cnt; if (VAR_0->poc.frame_num != VAR_11 || VAR_1->slice_type_nos != AV_PICTURE_TYPE_I)
VAR_0->valid_recovery_point = 1; if (   VAR_0->recovery_frame < 0
|| av_mod_uintp2(VAR_0->recovery_frame - VAR_0->poc.frame_num, VAR_0->ps.VAR_5->log2_max_frame_num) > VAR_11) { VAR_0->recovery_frame = av_mod_uintp2(VAR_0->poc.frame_num + VAR_11, VAR_0->ps.VAR_5->log2_max_frame_num); if (!VAR_0->valid_recovery_point)
VAR_0->recovery_frame = VAR_0->poc.frame_num; } } VAR_0->cur_pic_ptr->f->key_frame |= (VAR_2->type == H264_NAL_IDR_SLICE); if (VAR_2->type == H264_NAL_IDR_SLICE ||
(VAR_0->recovery_frame == VAR_0->poc.frame_num && VAR_2->ref_idc)) { VAR_0->recovery_frame         = -1; VAR_0->cur_pic_ptr->recovered = 1; } if (VAR_2->type == H264_NAL_IDR_SLICE)
VAR_0->frame_recovered |= FRAME_RECOVERED_IDR; #if 1
VAR_0->cur_pic_ptr->recovered |= VAR_0->frame_recovered; #else
VAR_0->cur_pic_ptr->recovered |= !!(VAR_0->frame_recovered & FRAME_RECOVERED_IDR); #endif
if (!FIELD_PICTURE(VAR_0) || !VAR_0->first_field || VAR_0->missing_fields > 1) { VAR_8 = h264_export_frame_props(VAR_0); if (VAR_8 < 0)
return VAR_8; VAR_8 = h264_select_output_frame(VAR_0); if (VAR_8 < 0)
return VAR_8; } return 0; }