int ff_hevc_split_packet(HEVCContext *s, HEVCPacket *pkt, const uint8_t *buf, int length,

                         AVCodecContext *avctx, int is_nalff, int nal_length_size)

{

    int consumed, ret = 0;



    pkt->nb_nals = 0;

    while (length >= 4) {

        HEVCNAL *nal;

        int extract_length = 0;



        if (is_nalff) {

            int i;

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

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

            buf    += nal_length_size;

            length -= nal_length_size;



            if (extract_length > length) {

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

                return AVERROR_INVALIDDATA;

            }

        } else {

            /* search start code */

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

                ++buf;

                --length;

                if (length < 4) {

                    if (pkt->nb_nals > 0) {

                        // No more start codes: we discarded some irrelevant

                        // bytes at the end of the packet.

                        return 0;

                    } else {

                        av_log(avctx, AV_LOG_ERROR, "No start code is found.\n");

                        return AVERROR_INVALIDDATA;

                    }

                }

            }



            buf           += 3;

            length        -= 3;

            extract_length = length;

        }



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

            int new_size = pkt->nals_allocated + 1;

            void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));



            if (!tmp)

                return AVERROR(ENOMEM);



            pkt->nals = tmp;

            memset(pkt->nals + pkt->nals_allocated, 0,

                   (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));



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

            nal->skipped_bytes_pos_size = 1024; // initial buffer size

            nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));

            if (!nal->skipped_bytes_pos)

                return AVERROR(ENOMEM);



            pkt->nals_allocated = new_size;

        }

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



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

        if (consumed < 0)

            return consumed;



        pkt->nb_nals++;



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

        if (ret < 0)

            return ret;



        ret = hls_nal_unit(nal, avctx);

        if (ret <= 0) {

            if (ret < 0) {

                av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",

                       nal->type);

            }

            pkt->nb_nals--;

        }



        buf    += consumed;

        length -= consumed;

    }



    return 0;

}

int ff_hevc_split_packet(HEVCContext *s, HEVCPacket *pkt, const uint8_t *buf, int length,

                         AVCodecContext *avctx, int is_nalff, int nal_length_size)

{

    int consumed, ret = 0;



    pkt->nb_nals = 0;

    while (length >= 4) {

        HEVCNAL *nal;

        int extract_length = 0;



        if (is_nalff) {

            int i;

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

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

            buf    += nal_length_size;

            length -= nal_length_size;



            if (extract_length > length) {

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

                return AVERROR_INVALIDDATA;

            }

        } else {

            

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

                ++buf;

                --length;

                if (length < 4) {

                    if (pkt->nb_nals > 0) {

                        

                        

                        return 0;

                    } else {

                        av_log(avctx, AV_LOG_ERROR, "No start code is found.\n");

                        return AVERROR_INVALIDDATA;

                    }

                }

            }



            buf           += 3;

            length        -= 3;

            extract_length = length;

        }



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

            int new_size = pkt->nals_allocated + 1;

            void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));



            if (!tmp)

                return AVERROR(ENOMEM);



            pkt->nals = tmp;

            memset(pkt->nals + pkt->nals_allocated, 0,

                   (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));



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

            nal->skipped_bytes_pos_size = 1024; 

            nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));

            if (!nal->skipped_bytes_pos)

                return AVERROR(ENOMEM);



            pkt->nals_allocated = new_size;

        }

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



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

        if (consumed < 0)

            return consumed;



        pkt->nb_nals++;



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

        if (ret < 0)

            return ret;



        ret = hls_nal_unit(nal, avctx);

        if (ret <= 0) {

            if (ret < 0) {

                av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",

                       nal->type);

            }

            pkt->nb_nals--;

        }



        buf    += consumed;

        length -= consumed;

    }



    return 0;

}

int FUNC_0(HEVCContext *VAR_0, HEVCPacket *VAR_1, const uint8_t *VAR_2, int VAR_3,

                         AVCodecContext *VAR_4, int VAR_5, int VAR_6)

{

    int VAR_7, VAR_8 = 0;



    VAR_1->nb_nals = 0;

    while (VAR_3 >= 4) {

        HEVCNAL *nal;

        int VAR_9 = 0;



        if (VAR_5) {

            int VAR_10;

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

                VAR_9 = (VAR_9 << 8) | VAR_2[VAR_10];

            VAR_2    += VAR_6;

            VAR_3 -= VAR_6;



            if (VAR_9 > VAR_3) {

                av_log(VAR_4, AV_LOG_ERROR, "Invalid NAL unit size.\n");

                return AVERROR_INVALIDDATA;

            }

        } else {

            

            while (VAR_2[0] != 0 || VAR_2[1] != 0 || VAR_2[2] != 1) {

                ++VAR_2;

                --VAR_3;

                if (VAR_3 < 4) {

                    if (VAR_1->nb_nals > 0) {

                        

                        

                        return 0;

                    } else {

                        av_log(VAR_4, AV_LOG_ERROR, "No start code is found.\n");

                        return AVERROR_INVALIDDATA;

                    }

                }

            }



            VAR_2           += 3;

            VAR_3        -= 3;

            VAR_9 = VAR_3;

        }



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

            int VAR_11 = VAR_1->nals_allocated + 1;

            void *VAR_12 = av_realloc_array(VAR_1->nals, VAR_11, sizeof(*VAR_1->nals));



            if (!VAR_12)

                return AVERROR(ENOMEM);



            VAR_1->nals = VAR_12;

            memset(VAR_1->nals + VAR_1->nals_allocated, 0,

                   (VAR_11 - VAR_1->nals_allocated) * sizeof(*VAR_1->nals));



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

            nal->skipped_bytes_pos_size = 1024; 

            nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));

            if (!nal->skipped_bytes_pos)

                return AVERROR(ENOMEM);



            VAR_1->nals_allocated = VAR_11;

        }

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



        VAR_7 = ff_hevc_extract_rbsp(VAR_0, VAR_2, VAR_9, nal);

        if (VAR_7 < 0)

            return VAR_7;



        VAR_1->nb_nals++;



        VAR_8 = init_get_bits8(&nal->gb, nal->data, nal->size);

        if (VAR_8 < 0)

            return VAR_8;



        VAR_8 = hls_nal_unit(nal, VAR_4);

        if (VAR_8 <= 0) {

            if (VAR_8 < 0) {

                av_log(VAR_4, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",

                       nal->type);

            }

            VAR_1->nb_nals--;

        }



        VAR_2    += VAR_7;

        VAR_3 -= VAR_7;

    }



    return 0;

}

int FUNC_0(HEVCContext *VAR_0, HEVCPacket *VAR_1, const uint8_t *VAR_2, int VAR_3,
AVCodecContext *VAR_4, int VAR_5, int VAR_6)
{ int VAR_7, VAR_8 = 0; VAR_1->nb_nals = 0; while (VAR_3 >= 4) { HEVCNAL *nal; int VAR_9 = 0; if (VAR_5) { int VAR_10; for (VAR_10 = 0; VAR_10 < VAR_6; VAR_10++) VAR_9 = (VAR_9 << 8) | VAR_2[VAR_10]; VAR_2    += VAR_6; VAR_3 -= VAR_6; if (VAR_9 > VAR_3) { av_log(VAR_4, AV_LOG_ERROR, "Invalid NAL unit size.\n"); return AVERROR_INVALIDDATA; } } else { while (VAR_2[0] != 0 || VAR_2[1] != 0 || VAR_2[2] != 1) { ++VAR_2; --VAR_3; if (VAR_3 < 4) { if (VAR_1->nb_nals > 0) { return 0; } else { av_log(VAR_4, AV_LOG_ERROR, "No start code is found.\n"); return AVERROR_INVALIDDATA; } } } VAR_2           += 3; VAR_3        -= 3; VAR_9 = VAR_3; } if (VAR_1->nals_allocated < VAR_1->nb_nals + 1) { int VAR_11 = VAR_1->nals_allocated + 1; void *VAR_12 = av_realloc_array(VAR_1->nals, VAR_11, sizeof(*VAR_1->nals)); if (!VAR_12)
return AVERROR(ENOMEM); VAR_1->nals = VAR_12; memset(VAR_1->nals + VAR_1->nals_allocated, 0,
(VAR_11 - VAR_1->nals_allocated) * sizeof(*VAR_1->nals)); nal = &VAR_1->nals[VAR_1->nb_nals]; nal->skipped_bytes_pos_size = 1024; nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos)); if (!nal->skipped_bytes_pos)
return AVERROR(ENOMEM); VAR_1->nals_allocated = VAR_11; } nal = &VAR_1->nals[VAR_1->nb_nals]; VAR_7 = ff_hevc_extract_rbsp(VAR_0, VAR_2, VAR_9, nal); if (VAR_7 < 0)
return VAR_7; VAR_1->nb_nals++; VAR_8 = init_get_bits8(&nal->gb, nal->data, nal->size); if (VAR_8 < 0)
return VAR_8; VAR_8 = hls_nal_unit(nal, VAR_4); if (VAR_8 <= 0) { if (VAR_8 < 0) { av_log(VAR_4, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
nal->type); } VAR_1->nb_nals--; } VAR_2    += VAR_7; VAR_3 -= VAR_7; } return 0; }