static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)

{

    int i, consumed, ret = 0;



    s->ref = NULL;

    s->eos = 0;



    /* split the input packet into NAL units, so we know the upper bound on the

     * number of slices in the frame */

    s->nb_nals = 0;

    while (length >= 4) {

        HEVCNAL *nal;

        int extract_length = 0;



        if (s->is_nalff) {

            int i;

            for (i = 0; i < s->nal_length_size; i++)

                extract_length = (extract_length << 8) | buf[i];

            buf    += s->nal_length_size;

            length -= s->nal_length_size;



            if (extract_length > length) {

                av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }

        } else {

            if (buf[2] == 0) {

                length--;

                buf++;

                continue;

            }

            if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }



            buf    += 3;

            length -= 3;

        }



        if (!s->is_nalff)

            extract_length = length;



        if (s->nals_allocated < s->nb_nals + 1) {

            int new_size = s->nals_allocated + 1;

            HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));

            if (!tmp) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }

            s->nals = tmp;

            memset(s->nals + s->nals_allocated, 0, (new_size - s->nals_allocated) * sizeof(*tmp));

            av_reallocp_array(&s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));

            av_reallocp_array(&s->skipped_bytes_pos_size_nal, new_size, sizeof(*s->skipped_bytes_pos_size_nal));

            av_reallocp_array(&s->skipped_bytes_pos_nal, new_size, sizeof(*s->skipped_bytes_pos_nal));

            s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; // initial buffer size

            s->skipped_bytes_pos_nal[s->nals_allocated] = av_malloc_array(s->skipped_bytes_pos_size_nal[s->nals_allocated], sizeof(*s->skipped_bytes_pos));

            s->nals_allocated = new_size;

        }

        s->skipped_bytes_pos_size = s->skipped_bytes_pos_size_nal[s->nb_nals];

        s->skipped_bytes_pos = s->skipped_bytes_pos_nal[s->nb_nals];

        nal = &s->nals[s->nb_nals];



        consumed = extract_rbsp(s, buf, extract_length, nal);



        s->skipped_bytes_nal[s->nb_nals] = s->skipped_bytes;

        s->skipped_bytes_pos_size_nal[s->nb_nals] = s->skipped_bytes_pos_size;

        s->skipped_bytes_pos_nal[s->nb_nals++] = s->skipped_bytes_pos;





        if (consumed < 0) {

            ret = consumed;

            goto fail;

        }



        ret = init_get_bits8(&s->HEVClc->gb, nal->data, nal->size);

        if (ret < 0)

            goto fail;

        hls_nal_unit(s);



        if (s->nal_unit_type == NAL_EOS_NUT || s->nal_unit_type == NAL_EOB_NUT)

            s->eos = 1;



        buf    += consumed;

        length -= consumed;

    }



    /* parse the NAL units */

    for (i = 0; i < s->nb_nals; i++) {

        int ret;

        s->skipped_bytes = s->skipped_bytes_nal[i];

        s->skipped_bytes_pos = s->skipped_bytes_pos_nal[i];



        ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);

        if (ret < 0) {

            av_log(s->avctx, AV_LOG_WARNING, "Error parsing NAL unit #%d.\n", i);

            if (s->avctx->err_recognition & AV_EF_EXPLODE)

                goto fail;

        }

    }



fail:

    if (s->ref && s->threads_type == FF_THREAD_FRAME)

        ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);



    return ret;

}

static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)

{

    int i, consumed, ret = 0;



    s->ref = NULL;

    s->eos = 0;



    



    s->nb_nals = 0;

    while (length >= 4) {

        HEVCNAL *nal;

        int extract_length = 0;



        if (s->is_nalff) {

            int i;

            for (i = 0; i < s->nal_length_size; i++)

                extract_length = (extract_length << 8) | buf[i];

            buf    += s->nal_length_size;

            length -= s->nal_length_size;



            if (extract_length > length) {

                av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }

        } else {

            if (buf[2] == 0) {

                length--;

                buf++;

                continue;

            }

            if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {

                ret = AVERROR_INVALIDDATA;

                goto fail;

            }



            buf    += 3;

            length -= 3;

        }



        if (!s->is_nalff)

            extract_length = length;



        if (s->nals_allocated < s->nb_nals + 1) {

            int new_size = s->nals_allocated + 1;

            HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));

            if (!tmp) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }

            s->nals = tmp;

            memset(s->nals + s->nals_allocated, 0, (new_size - s->nals_allocated) * sizeof(*tmp));

            av_reallocp_array(&s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));

            av_reallocp_array(&s->skipped_bytes_pos_size_nal, new_size, sizeof(*s->skipped_bytes_pos_size_nal));

            av_reallocp_array(&s->skipped_bytes_pos_nal, new_size, sizeof(*s->skipped_bytes_pos_nal));

            s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; 

            s->skipped_bytes_pos_nal[s->nals_allocated] = av_malloc_array(s->skipped_bytes_pos_size_nal[s->nals_allocated], sizeof(*s->skipped_bytes_pos));

            s->nals_allocated = new_size;

        }

        s->skipped_bytes_pos_size = s->skipped_bytes_pos_size_nal[s->nb_nals];

        s->skipped_bytes_pos = s->skipped_bytes_pos_nal[s->nb_nals];

        nal = &s->nals[s->nb_nals];



        consumed = extract_rbsp(s, buf, extract_length, nal);



        s->skipped_bytes_nal[s->nb_nals] = s->skipped_bytes;

        s->skipped_bytes_pos_size_nal[s->nb_nals] = s->skipped_bytes_pos_size;

        s->skipped_bytes_pos_nal[s->nb_nals++] = s->skipped_bytes_pos;





        if (consumed < 0) {

            ret = consumed;

            goto fail;

        }



        ret = init_get_bits8(&s->HEVClc->gb, nal->data, nal->size);

        if (ret < 0)

            goto fail;

        hls_nal_unit(s);



        if (s->nal_unit_type == NAL_EOS_NUT || s->nal_unit_type == NAL_EOB_NUT)

            s->eos = 1;



        buf    += consumed;

        length -= consumed;

    }



    

    for (i = 0; i < s->nb_nals; i++) {

        int ret;

        s->skipped_bytes = s->skipped_bytes_nal[i];

        s->skipped_bytes_pos = s->skipped_bytes_pos_nal[i];



        ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);

        if (ret < 0) {

            av_log(s->avctx, AV_LOG_WARNING, "Error parsing NAL unit #%d.\n", i);

            if (s->avctx->err_recognition & AV_EF_EXPLODE)

                goto fail;

        }

    }



fail:

    if (s->ref && s->threads_type == FF_THREAD_FRAME)

        ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);



    return ret;

}

static int FUNC_0(HEVCContext *VAR_0, const uint8_t *VAR_1, int VAR_2)

{

    int VAR_7, VAR_4, VAR_5 = 0;



    VAR_0->ref = NULL;

    VAR_0->eos = 0;



    



    VAR_0->nb_nals = 0;

    while (VAR_2 >= 4) {

        HEVCNAL *nal;

        int VAR_6 = 0;



        if (VAR_0->is_nalff) {

            int VAR_7;

            for (VAR_7 = 0; VAR_7 < VAR_0->nal_length_size; VAR_7++)

                VAR_6 = (VAR_6 << 8) | VAR_1[VAR_7];

            VAR_1    += VAR_0->nal_length_size;

            VAR_2 -= VAR_0->nal_length_size;



            if (VAR_6 > VAR_2) {

                av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");

                VAR_5 = AVERROR_INVALIDDATA;

                goto fail;

            }

        } else {

            if (VAR_1[2] == 0) {

                VAR_2--;

                VAR_1++;

                continue;

            }

            if (VAR_1[0] != 0 || VAR_1[1] != 0 || VAR_1[2] != 1) {

                VAR_5 = AVERROR_INVALIDDATA;

                goto fail;

            }



            VAR_1    += 3;

            VAR_2 -= 3;

        }



        if (!VAR_0->is_nalff)

            VAR_6 = VAR_2;



        if (VAR_0->nals_allocated < VAR_0->nb_nals + 1) {

            int VAR_7 = VAR_0->nals_allocated + 1;

            HEVCNAL *tmp = av_realloc_array(VAR_0->nals, VAR_7, sizeof(*tmp));

            if (!tmp) {

                VAR_5 = AVERROR(ENOMEM);

                goto fail;

            }

            VAR_0->nals = tmp;

            memset(VAR_0->nals + VAR_0->nals_allocated, 0, (VAR_7 - VAR_0->nals_allocated) * sizeof(*tmp));

            av_reallocp_array(&VAR_0->skipped_bytes_nal, VAR_7, sizeof(*VAR_0->skipped_bytes_nal));

            av_reallocp_array(&VAR_0->skipped_bytes_pos_size_nal, VAR_7, sizeof(*VAR_0->skipped_bytes_pos_size_nal));

            av_reallocp_array(&VAR_0->skipped_bytes_pos_nal, VAR_7, sizeof(*VAR_0->skipped_bytes_pos_nal));

            VAR_0->skipped_bytes_pos_size_nal[VAR_0->nals_allocated] = 1024; 

            VAR_0->skipped_bytes_pos_nal[VAR_0->nals_allocated] = av_malloc_array(VAR_0->skipped_bytes_pos_size_nal[VAR_0->nals_allocated], sizeof(*VAR_0->skipped_bytes_pos));

            VAR_0->nals_allocated = VAR_7;

        }

        VAR_0->skipped_bytes_pos_size = VAR_0->skipped_bytes_pos_size_nal[VAR_0->nb_nals];

        VAR_0->skipped_bytes_pos = VAR_0->skipped_bytes_pos_nal[VAR_0->nb_nals];

        nal = &VAR_0->nals[VAR_0->nb_nals];



        VAR_4 = extract_rbsp(VAR_0, VAR_1, VAR_6, nal);



        VAR_0->skipped_bytes_nal[VAR_0->nb_nals] = VAR_0->skipped_bytes;

        VAR_0->skipped_bytes_pos_size_nal[VAR_0->nb_nals] = VAR_0->skipped_bytes_pos_size;

        VAR_0->skipped_bytes_pos_nal[VAR_0->nb_nals++] = VAR_0->skipped_bytes_pos;





        if (VAR_4 < 0) {

            VAR_5 = VAR_4;

            goto fail;

        }



        VAR_5 = init_get_bits8(&VAR_0->HEVClc->gb, nal->data, nal->size);

        if (VAR_5 < 0)

            goto fail;

        hls_nal_unit(VAR_0);



        if (VAR_0->nal_unit_type == NAL_EOS_NUT || VAR_0->nal_unit_type == NAL_EOB_NUT)

            VAR_0->eos = 1;



        VAR_1    += VAR_4;

        VAR_2 -= VAR_4;

    }



    

    for (VAR_7 = 0; VAR_7 < VAR_0->nb_nals; VAR_7++) {

        int VAR_5;

        VAR_0->skipped_bytes = VAR_0->skipped_bytes_nal[VAR_7];

        VAR_0->skipped_bytes_pos = VAR_0->skipped_bytes_pos_nal[VAR_7];



        VAR_5 = decode_nal_unit(VAR_0, VAR_0->nals[VAR_7].data, VAR_0->nals[VAR_7].size);

        if (VAR_5 < 0) {

            av_log(VAR_0->avctx, AV_LOG_WARNING, "Error parsing NAL unit #%d.\n", VAR_7);

            if (VAR_0->avctx->err_recognition & AV_EF_EXPLODE)

                goto fail;

        }

    }



fail:

    if (VAR_0->ref && VAR_0->threads_type == FF_THREAD_FRAME)

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



    return VAR_5;

}

static int FUNC_0(HEVCContext *VAR_0, const uint8_t *VAR_1, int VAR_2)
{ int VAR_7, VAR_4, VAR_5 = 0; VAR_0->ref = NULL; VAR_0->eos = 0; VAR_0->nb_nals = 0; while (VAR_2 >= 4) { HEVCNAL *nal; int VAR_6 = 0; if (VAR_0->is_nalff) { int VAR_7; for (VAR_7 = 0; VAR_7 < VAR_0->nal_length_size; VAR_7++) VAR_6 = (VAR_6 << 8) | VAR_1[VAR_7]; VAR_1    += VAR_0->nal_length_size; VAR_2 -= VAR_0->nal_length_size; if (VAR_6 > VAR_2) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n"); VAR_5 = AVERROR_INVALIDDATA; goto fail; } } else { if (VAR_1[2] == 0) { VAR_2--; VAR_1++; continue; } if (VAR_1[0] != 0 || VAR_1[1] != 0 || VAR_1[2] != 1) { VAR_5 = AVERROR_INVALIDDATA; goto fail; } VAR_1    += 3; VAR_2 -= 3; } if (!VAR_0->is_nalff)
VAR_6 = VAR_2; if (VAR_0->nals_allocated < VAR_0->nb_nals + 1) { int VAR_7 = VAR_0->nals_allocated + 1; HEVCNAL *tmp = av_realloc_array(VAR_0->nals, VAR_7, sizeof(*tmp)); if (!tmp) { VAR_5 = AVERROR(ENOMEM); goto fail; } VAR_0->nals = tmp; memset(VAR_0->nals + VAR_0->nals_allocated, 0, (VAR_7 - VAR_0->nals_allocated) * sizeof(*tmp)); av_reallocp_array(&VAR_0->skipped_bytes_nal, VAR_7, sizeof(*VAR_0->skipped_bytes_nal)); av_reallocp_array(&VAR_0->skipped_bytes_pos_size_nal, VAR_7, sizeof(*VAR_0->skipped_bytes_pos_size_nal)); av_reallocp_array(&VAR_0->skipped_bytes_pos_nal, VAR_7, sizeof(*VAR_0->skipped_bytes_pos_nal)); VAR_0->skipped_bytes_pos_size_nal[VAR_0->nals_allocated] = 1024; VAR_0->skipped_bytes_pos_nal[VAR_0->nals_allocated] = av_malloc_array(VAR_0->skipped_bytes_pos_size_nal[VAR_0->nals_allocated], sizeof(*VAR_0->skipped_bytes_pos)); VAR_0->nals_allocated = VAR_7; } VAR_0->skipped_bytes_pos_size = VAR_0->skipped_bytes_pos_size_nal[VAR_0->nb_nals]; VAR_0->skipped_bytes_pos = VAR_0->skipped_bytes_pos_nal[VAR_0->nb_nals]; nal = &VAR_0->nals[VAR_0->nb_nals]; VAR_4 = extract_rbsp(VAR_0, VAR_1, VAR_6, nal); VAR_0->skipped_bytes_nal[VAR_0->nb_nals] = VAR_0->skipped_bytes; VAR_0->skipped_bytes_pos_size_nal[VAR_0->nb_nals] = VAR_0->skipped_bytes_pos_size; VAR_0->skipped_bytes_pos_nal[VAR_0->nb_nals++] = VAR_0->skipped_bytes_pos; if (VAR_4 < 0) { VAR_5 = VAR_4; goto fail; } VAR_5 = init_get_bits8(&VAR_0->HEVClc->gb, nal->data, nal->size); if (VAR_5 < 0)
goto fail; hls_nal_unit(VAR_0); if (VAR_0->nal_unit_type == NAL_EOS_NUT || VAR_0->nal_unit_type == NAL_EOB_NUT)
VAR_0->eos = 1; VAR_1    += VAR_4; VAR_2 -= VAR_4; } for (VAR_7 = 0; VAR_7 < VAR_0->nb_nals; VAR_7++) { int VAR_5; VAR_0->skipped_bytes = VAR_0->skipped_bytes_nal[VAR_7]; VAR_0->skipped_bytes_pos = VAR_0->skipped_bytes_pos_nal[VAR_7]; VAR_5 = decode_nal_unit(VAR_0, VAR_0->nals[VAR_7].data, VAR_0->nals[VAR_7].size); if (VAR_5 < 0) { av_log(VAR_0->avctx, AV_LOG_WARNING, "Error parsing NAL unit #%d.\n", VAR_7); if (VAR_0->avctx->err_recognition & AV_EF_EXPLODE)
goto fail; } } fail:
if (VAR_0->ref && VAR_0->threads_type == FF_THREAD_FRAME)
ff_thread_report_progress(&VAR_0->ref->tf, INT_MAX, 0); return VAR_5; }