int parse_picture_header(davs2_t *h, davs2_bs_t *bs, uint32_t start_code)
{
    davs2_mgr_t *mgr = h->task_info.taskmgr;

    assert(start_code == SC_INTRA_PICTURE || start_code == SC_INTER_PICTURE);

    if (start_code == SC_INTRA_PICTURE) {
        if (parse_picture_header_intra(h, bs) < 0) {
            return -1;
        }
    } else {
        if (mgr->outpics.output == -1) {
            /* An I frame is expected for the first frame or after the decoder is flushed. */
            davs2_log(h, DAVS2_LOG_ERROR, "sequence should start with an I frame.");
            return -1;
        }

        if (parse_picture_header_inter(h, bs) < 0) {
            return -1;
        }
    }

    /* field picture ? */
    if (h->i_pic_coding_type != FRAME) {
        davs2_log(h, DAVS2_LOG_ERROR, "field is not supported.");
        return -1;
    }

    /* COI should be a periodically-repeated value from 0 to 255 */
    if (mgr->outpics.output != -1 &&
        h->i_coi != (mgr->i_prev_coi + 1) % AVS2_COI_CYCLE) {
        davs2_log(h, DAVS2_LOG_DEBUG, "discontinuous COI (prev: %d --> curr: %d).", mgr->i_prev_coi, h->i_coi);
    }

    /* update COI */
    if (h->i_coi < mgr->i_prev_coi) { /// !!! '='
        mgr->i_tr_wrap_cnt++;
    }

    mgr->i_prev_coi = h->i_coi;

    h->i_coi += mgr->i_tr_wrap_cnt * AVS2_COI_CYCLE;

    if (h->seq_info.head.low_delay == 0) {
        h->i_poc = h->i_coi + h->i_display_delay - h->seq_info.picture_reorder_delay;
    } else {
        h->i_poc = h->i_coi;
    }

    assert(h->i_coi >= 0 && h->i_poc >= 0); /// 'int' (2147483647) should be large enough for 'i_coi' & 'i_poc'.

    if (mgr->outpics.output == -1 && start_code == SC_INTRA_PICTURE) {
        if (h->i_coi != 0) {
            davs2_log(h, DAVS2_LOG_INFO, "COI of the first frame is %d.", h->i_coi);
        }

        mgr->outpics.output = h->i_poc;
    }

    return 0;
}
int parse_picture_header(davs2_t *h, davs2_bs_t *bs, uint32_t start_code)
{
    davs2_mgr_t *mgr = h->task_info.taskmgr;

    assert(start_code == SC_INTRA_PICTURE || start_code == SC_INTER_PICTURE);

    if (start_code == SC_INTRA_PICTURE) {
        if (parse_picture_header_intra(h, bs) < 0) {
            return -1;
        }
    } else {
        if (mgr->outpics.output == -1) {
             
            davs2_log(h, DAVS2_LOG_ERROR, "sequence should start with an I frame.");
            return -1;
        }

        if (parse_picture_header_inter(h, bs) < 0) {
            return -1;
        }
    }

     
    if (h->i_pic_coding_type != FRAME) {
        davs2_log(h, DAVS2_LOG_ERROR, "field is not supported.");
        return -1;
    }

     
    if (mgr->outpics.output != -1 &&
        h->i_coi != (mgr->i_prev_coi + 1) % AVS2_COI_CYCLE) {
        davs2_log(h, DAVS2_LOG_DEBUG, "discontinuous COI (prev: %d --> curr: %d).", mgr->i_prev_coi, h->i_coi);
    }

     
    if (h->i_coi < mgr->i_prev_coi) {  
        mgr->i_tr_wrap_cnt++;
    }

    mgr->i_prev_coi = h->i_coi;

    h->i_coi += mgr->i_tr_wrap_cnt * AVS2_COI_CYCLE;

    if (h->seq_info.head.low_delay == 0) {
        h->i_poc = h->i_coi + h->i_display_delay - h->seq_info.picture_reorder_delay;
    } else {
        h->i_poc = h->i_coi;
    }

    assert(h->i_coi >= 0 && h->i_poc >= 0);  

    if (mgr->outpics.output == -1 && start_code == SC_INTRA_PICTURE) {
        if (h->i_coi != 0) {
            davs2_log(h, DAVS2_LOG_INFO, "COI of the first frame is %d.", h->i_coi);
        }

        mgr->outpics.output = h->i_poc;
    }

    return 0;
}