int ff_h2645_extract_rbsp(const uint8_t *src, int length,

                          H2645NAL *nal)

{

    int i, si, di;

    uint8_t *dst;



    nal->skipped_bytes = 0;

#define STARTCODE_TEST                                                  \

        if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {     \

            if (src[i + 2] != 3 && src[i + 2] != 0) {                   \

                /* startcode, so we must be past the end */             \

                length = i;                                             \

            }                                                           \

            break;                                                      \

        }

#if HAVE_FAST_UNALIGNED

#define FIND_FIRST_ZERO                                                 \

        if (i > 0 && !src[i])                                           \

            i--;                                                        \

        while (src[i])                                                  \

            i++

#if HAVE_FAST_64BIT

    for (i = 0; i + 1 < length; i += 9) {

        if (!((~AV_RN64A(src + i) &

               (AV_RN64A(src + i) - 0x0100010001000101ULL)) &

              0x8000800080008080ULL))

            continue;

        FIND_FIRST_ZERO;

        STARTCODE_TEST;

        i -= 7;

    }

#else

    for (i = 0; i + 1 < length; i += 5) {

        if (!((~AV_RN32A(src + i) &

               (AV_RN32A(src + i) - 0x01000101U)) &

              0x80008080U))

            continue;

        FIND_FIRST_ZERO;

        STARTCODE_TEST;

        i -= 3;

    }

#endif /* HAVE_FAST_64BIT */

#else

    for (i = 0; i + 1 < length; i += 2) {

        if (src[i])

            continue;

        if (i > 0 && src[i - 1] == 0)

            i--;

        STARTCODE_TEST;

    }

#endif /* HAVE_FAST_UNALIGNED */



    if (i >= length - 1) { // no escaped 0

        nal->data     =

        nal->raw_data = src;

        nal->size     =

        nal->raw_size = length;

        return length;

    }



    av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,

                   length + AV_INPUT_BUFFER_PADDING_SIZE);

    if (!nal->rbsp_buffer)

        return AVERROR(ENOMEM);



    dst = nal->rbsp_buffer;



    memcpy(dst, src, i);

    si = di = i;

    while (si + 2 < length) {

        // remove escapes (very rare 1:2^22)

        if (src[si + 2] > 3) {

            dst[di++] = src[si++];

            dst[di++] = src[si++];

        } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {

            if (src[si + 2] == 3) { // escape

                dst[di++] = 0;

                dst[di++] = 0;

                si       += 3;



                if (nal->skipped_bytes_pos) {

                    nal->skipped_bytes++;

                    if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {

                        nal->skipped_bytes_pos_size *= 2;

                        av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);

                        av_reallocp_array(&nal->skipped_bytes_pos,

                                nal->skipped_bytes_pos_size,

                                sizeof(*nal->skipped_bytes_pos));

                        if (!nal->skipped_bytes_pos) {

                            nal->skipped_bytes_pos_size = 0;

                            return AVERROR(ENOMEM);

                        }

                    }

                    if (nal->skipped_bytes_pos)

                        nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;

                }

                continue;

            } else // next start code

                goto nsc;

        }



        dst[di++] = src[si++];

    }

    while (si < length)

        dst[di++] = src[si++];



nsc:

    memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);



    nal->data = dst;

    nal->size = di;

    nal->raw_data = src;

    nal->raw_size = si;

    return si;

}

int ff_h2645_extract_rbsp(const uint8_t *src, int length,

                          H2645NAL *nal)

{

    int i, si, di;

    uint8_t *dst;



    nal->skipped_bytes = 0;

#define STARTCODE_TEST                                                  \

        if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {     \

            if (src[i + 2] != 3 && src[i + 2] != 0) {                   \

                             \

                length = i;                                             \

            }                                                           \

            break;                                                      \

        }

#if HAVE_FAST_UNALIGNED

#define FIND_FIRST_ZERO                                                 \

        if (i > 0 && !src[i])                                           \

            i--;                                                        \

        while (src[i])                                                  \

            i++

#if HAVE_FAST_64BIT

    for (i = 0; i + 1 < length; i += 9) {

        if (!((~AV_RN64A(src + i) &

               (AV_RN64A(src + i) - 0x0100010001000101ULL)) &

              0x8000800080008080ULL))

            continue;

        FIND_FIRST_ZERO;

        STARTCODE_TEST;

        i -= 7;

    }

#else

    for (i = 0; i + 1 < length; i += 5) {

        if (!((~AV_RN32A(src + i) &

               (AV_RN32A(src + i) - 0x01000101U)) &

              0x80008080U))

            continue;

        FIND_FIRST_ZERO;

        STARTCODE_TEST;

        i -= 3;

    }

#endif 

#else

    for (i = 0; i + 1 < length; i += 2) {

        if (src[i])

            continue;

        if (i > 0 && src[i - 1] == 0)

            i--;

        STARTCODE_TEST;

    }

#endif 



    if (i >= length - 1) { 

        nal->data     =

        nal->raw_data = src;

        nal->size     =

        nal->raw_size = length;

        return length;

    }



    av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,

                   length + AV_INPUT_BUFFER_PADDING_SIZE);

    if (!nal->rbsp_buffer)

        return AVERROR(ENOMEM);



    dst = nal->rbsp_buffer;



    memcpy(dst, src, i);

    si = di = i;

    while (si + 2 < length) {

        

        if (src[si + 2] > 3) {

            dst[di++] = src[si++];

            dst[di++] = src[si++];

        } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {

            if (src[si + 2] == 3) { 

                dst[di++] = 0;

                dst[di++] = 0;

                si       += 3;



                if (nal->skipped_bytes_pos) {

                    nal->skipped_bytes++;

                    if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {

                        nal->skipped_bytes_pos_size *= 2;

                        av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);

                        av_reallocp_array(&nal->skipped_bytes_pos,

                                nal->skipped_bytes_pos_size,

                                sizeof(*nal->skipped_bytes_pos));

                        if (!nal->skipped_bytes_pos) {

                            nal->skipped_bytes_pos_size = 0;

                            return AVERROR(ENOMEM);

                        }

                    }

                    if (nal->skipped_bytes_pos)

                        nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;

                }

                continue;

            } else 

                goto nsc;

        }



        dst[di++] = src[si++];

    }

    while (si < length)

        dst[di++] = src[si++];



nsc:

    memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);



    nal->data = dst;

    nal->size = di;

    nal->raw_data = src;

    nal->raw_size = si;

    return si;

}

int FUNC_0(const uint8_t *VAR_0, int VAR_1,

                          H2645NAL *VAR_2)

{

    int VAR_3, VAR_4, VAR_5;

    uint8_t *dst;



    VAR_2->skipped_bytes = 0;

#define STARTCODE_TEST                                                  \

        if (VAR_3 + 2 < VAR_1 && VAR_0[VAR_3 + 1] == 0 && VAR_0[VAR_3 + 2] <= 3) {     \

            if (VAR_0[VAR_3 + 2] != 3 && VAR_0[VAR_3 + 2] != 0) {                   \

                             \

                VAR_1 = VAR_3;                                             \

            }                                                           \

            break;                                                      \

        }

#if HAVE_FAST_UNALIGNED

#define FIND_FIRST_ZERO                                                 \

        if (VAR_3 > 0 && !VAR_0[VAR_3])                                           \

            VAR_3--;                                                        \

        while (VAR_0[VAR_3])                                                  \

            VAR_3++

#if HAVE_FAST_64BIT

    for (VAR_3 = 0; VAR_3 + 1 < VAR_1; VAR_3 += 9) {

        if (!((~AV_RN64A(VAR_0 + VAR_3) &

               (AV_RN64A(VAR_0 + VAR_3) - 0x0100010001000101ULL)) &

              0x8000800080008080ULL))

            continue;

        FIND_FIRST_ZERO;

        STARTCODE_TEST;

        VAR_3 -= 7;

    }

#else

    for (VAR_3 = 0; VAR_3 + 1 < VAR_1; VAR_3 += 5) {

        if (!((~AV_RN32A(VAR_0 + VAR_3) &

               (AV_RN32A(VAR_0 + VAR_3) - 0x01000101U)) &

              0x80008080U))

            continue;

        FIND_FIRST_ZERO;

        STARTCODE_TEST;

        VAR_3 -= 3;

    }

#endif 

#else

    for (VAR_3 = 0; VAR_3 + 1 < VAR_1; VAR_3 += 2) {

        if (VAR_0[VAR_3])

            continue;

        if (VAR_3 > 0 && VAR_0[VAR_3 - 1] == 0)

            VAR_3--;

        STARTCODE_TEST;

    }

#endif 



    if (VAR_3 >= VAR_1 - 1) { 

        VAR_2->data     =

        VAR_2->raw_data = VAR_0;

        VAR_2->size     =

        VAR_2->raw_size = VAR_1;

        return VAR_1;

    }



    av_fast_malloc(&VAR_2->rbsp_buffer, &VAR_2->rbsp_buffer_size,

                   VAR_1 + AV_INPUT_BUFFER_PADDING_SIZE);

    if (!VAR_2->rbsp_buffer)

        return AVERROR(ENOMEM);



    dst = VAR_2->rbsp_buffer;



    memcpy(dst, VAR_0, VAR_3);

    VAR_4 = VAR_5 = VAR_3;

    while (VAR_4 + 2 < VAR_1) {

        

        if (VAR_0[VAR_4 + 2] > 3) {

            dst[VAR_5++] = VAR_0[VAR_4++];

            dst[VAR_5++] = VAR_0[VAR_4++];

        } else if (VAR_0[VAR_4] == 0 && VAR_0[VAR_4 + 1] == 0 && VAR_0[VAR_4 + 2] != 0) {

            if (VAR_0[VAR_4 + 2] == 3) { 

                dst[VAR_5++] = 0;

                dst[VAR_5++] = 0;

                VAR_4       += 3;



                if (VAR_2->skipped_bytes_pos) {

                    VAR_2->skipped_bytes++;

                    if (VAR_2->skipped_bytes_pos_size < VAR_2->skipped_bytes) {

                        VAR_2->skipped_bytes_pos_size *= 2;

                        av_assert0(VAR_2->skipped_bytes_pos_size >= VAR_2->skipped_bytes);

                        av_reallocp_array(&VAR_2->skipped_bytes_pos,

                                VAR_2->skipped_bytes_pos_size,

                                sizeof(*VAR_2->skipped_bytes_pos));

                        if (!VAR_2->skipped_bytes_pos) {

                            VAR_2->skipped_bytes_pos_size = 0;

                            return AVERROR(ENOMEM);

                        }

                    }

                    if (VAR_2->skipped_bytes_pos)

                        VAR_2->skipped_bytes_pos[VAR_2->skipped_bytes-1] = VAR_5 - 1;

                }

                continue;

            } else 

                goto nsc;

        }



        dst[VAR_5++] = VAR_0[VAR_4++];

    }

    while (VAR_4 < VAR_1)

        dst[VAR_5++] = VAR_0[VAR_4++];



nsc:

    memset(dst + VAR_5, 0, AV_INPUT_BUFFER_PADDING_SIZE);



    VAR_2->data = dst;

    VAR_2->size = VAR_5;

    VAR_2->raw_data = VAR_0;

    VAR_2->raw_size = VAR_4;

    return VAR_4;

}

int FUNC_0(const uint8_t *VAR_0, int VAR_1,
H2645NAL *VAR_2)
{ int VAR_3, VAR_4, VAR_5; uint8_t *dst; VAR_2->skipped_bytes = 0; #define STARTCODE_TEST                                                  \
if (VAR_3 + 2 < VAR_1 && VAR_0[VAR_3 + 1] == 0 && VAR_0[VAR_3 + 2] <= 3) {     \ if (VAR_0[VAR_3 + 2] != 3 && VAR_0[VAR_3 + 2] != 0) {                   \ \
VAR_1 = VAR_3;                                             \ }                                                           \ break;                                                      \ } #if HAVE_FAST_UNALIGNED
#define FIND_FIRST_ZERO                                                 \
if (VAR_3 > 0 && !VAR_0[VAR_3])                                           \
VAR_3--;                                                        \ while (VAR_0[VAR_3])                                                  \
VAR_3++
#if HAVE_FAST_64BIT
for (VAR_3 = 0; VAR_3 + 1 < VAR_1; VAR_3 += 9) { if (!((~AV_RN64A(VAR_0 + VAR_3) &
(AV_RN64A(VAR_0 + VAR_3) - 0x0100010001000101ULL)) &
0x8000800080008080ULL))
continue; FIND_FIRST_ZERO; STARTCODE_TEST; VAR_3 -= 7; } #else
for (VAR_3 = 0; VAR_3 + 1 < VAR_1; VAR_3 += 5) { if (!((~AV_RN32A(VAR_0 + VAR_3) &
(AV_RN32A(VAR_0 + VAR_3) - 0x01000101U)) &
0x80008080U))
continue; FIND_FIRST_ZERO; STARTCODE_TEST; VAR_3 -= 3; } #endif
#else
for (VAR_3 = 0; VAR_3 + 1 < VAR_1; VAR_3 += 2) { if (VAR_0[VAR_3])
continue; if (VAR_3 > 0 && VAR_0[VAR_3 - 1] == 0)
VAR_3--; STARTCODE_TEST; } #endif
if (VAR_3 >= VAR_1 - 1) { VAR_2->data     =
VAR_2->raw_data = VAR_0; VAR_2->size     =
VAR_2->raw_size = VAR_1; return VAR_1; } av_fast_malloc(&VAR_2->rbsp_buffer, &VAR_2->rbsp_buffer_size,
VAR_1 + AV_INPUT_BUFFER_PADDING_SIZE); if (!VAR_2->rbsp_buffer)
return AVERROR(ENOMEM); dst = VAR_2->rbsp_buffer; memcpy(dst, VAR_0, VAR_3); VAR_4 = VAR_5 = VAR_3; while (VAR_4 + 2 < VAR_1) { if (VAR_0[VAR_4 + 2] > 3) { dst[VAR_5++] = VAR_0[VAR_4++]; dst[VAR_5++] = VAR_0[VAR_4++]; } else if (VAR_0[VAR_4] == 0 && VAR_0[VAR_4 + 1] == 0 && VAR_0[VAR_4 + 2] != 0) { if (VAR_0[VAR_4 + 2] == 3) { dst[VAR_5++] = 0; dst[VAR_5++] = 0; VAR_4       += 3; if (VAR_2->skipped_bytes_pos) { VAR_2->skipped_bytes++; if (VAR_2->skipped_bytes_pos_size < VAR_2->skipped_bytes) { VAR_2->skipped_bytes_pos_size *= 2; av_assert0(VAR_2->skipped_bytes_pos_size >= VAR_2->skipped_bytes); av_reallocp_array(&VAR_2->skipped_bytes_pos,
VAR_2->skipped_bytes_pos_size,
sizeof(*VAR_2->skipped_bytes_pos)); if (!VAR_2->skipped_bytes_pos) { VAR_2->skipped_bytes_pos_size = 0; return AVERROR(ENOMEM); } } if (VAR_2->skipped_bytes_pos)
VAR_2->skipped_bytes_pos[VAR_2->skipped_bytes-1] = VAR_5 - 1; } continue; } else goto nsc; } dst[VAR_5++] = VAR_0[VAR_4++]; } while (VAR_4 < VAR_1)
dst[VAR_5++] = VAR_0[VAR_4++]; nsc:
memset(dst + VAR_5, 0, AV_INPUT_BUFFER_PADDING_SIZE); VAR_2->data = dst; VAR_2->size = VAR_5; VAR_2->raw_data = VAR_0; VAR_2->raw_size = VAR_4; return VAR_4; }