int parse_header(davs2_t *h, davs2_bs_t *p_bs)
{
    const uint8_t *data   = p_bs->p_stream;
    int           *bitpos = &p_bs->i_bit_pos;
    int            len    = p_bs->i_stream;
    const uint8_t *p_start_code = 0;

    if (len <= 4) {
        return -1;  // at least 4 bytes are needed for decoding
    }

    while ((p_start_code = find_start_code(data + (*bitpos >> 3), len - (*bitpos >> 3))) != 0) {
        uint32_t start_code;
        *bitpos = (int)((p_start_code - data) << 3);

        if ((*bitpos >> 3) + 4 > len) {
            break;
        }

        start_code = data[(*bitpos >> 3) + 3];
        switch (start_code) {
        case SC_INTRA_PICTURE:
        case SC_INTER_PICTURE:
            /* update the decoder */
            if (task_decoder_update(h) < 0) {
                return -1;
            }

            /* decode the picture header */
            if (parse_picture_header(h, p_bs, start_code) < 0) {
                return -1;
            }

            return 0; /// !!! we only decode one frame for a single call.

        case SC_SEQUENCE_HEADER:
            davs2_seq_t new_seq;
            /* decode the sequence head */
            if (parse_sequence_header(h->task_info.taskmgr, &new_seq, p_bs) < 0) {
                davs2_log(h, 0, "Invalid sequence header.");
                return -1;
            }
            /* update the task manager */
            if (task_set_sequence_head(h->task_info.taskmgr, &new_seq) < 0) {
                return -1;
            }

            break;

        case SC_EXTENSION:
        case SC_USER_DATA:
        case SC_SEQUENCE_END:
        case SC_VIDEO_EDIT_CODE:
        default:
            /* skip this unit */

            /* NOTE: if you want to decode these units, you should avoid */
            /* using a davs2_t structure which will not be updated until a picture header is decoded. */

            *bitpos += 32;
            break;
        }
    }

    return 1;
}
int parse_header(davs2_t *h, davs2_bs_t *p_bs)
{
    const uint8_t *data   = p_bs->p_stream;
    int           *bitpos = &p_bs->i_bit_pos;
    int            len    = p_bs->i_stream;
    const uint8_t *p_start_code = 0;

    if (len <= 4) {
        return -1;   
    }

    while ((p_start_code = find_start_code(data + (*bitpos >> 3), len - (*bitpos >> 3))) != 0) {
        uint32_t start_code;
        *bitpos = (int)((p_start_code - data) << 3);

        if ((*bitpos >> 3) + 4 > len) {
            break;
        }

        start_code = data[(*bitpos >> 3) + 3];
        switch (start_code) {
        case SC_INTRA_PICTURE:
        case SC_INTER_PICTURE:
             
            if (task_decoder_update(h) < 0) {
                return -1;
            }

             
            if (parse_picture_header(h, p_bs, start_code) < 0) {
                return -1;
            }

            return 0;  

        case SC_SEQUENCE_HEADER:
            davs2_seq_t new_seq;
             
            if (parse_sequence_header(h->task_info.taskmgr, &new_seq, p_bs) < 0) {
                davs2_log(h, 0, "Invalid sequence header.");
                return -1;
            }
             
            if (task_set_sequence_head(h->task_info.taskmgr, &new_seq) < 0) {
                return -1;
            }

            break;

        case SC_EXTENSION:
        case SC_USER_DATA:
        case SC_SEQUENCE_END:
        case SC_VIDEO_EDIT_CODE:
        default:
             

             
             

            *bitpos += 32;
            break;
        }
    }

    return 1;
}