const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,

                                  int *dst_length, int *consumed, int length)

{

    int i, si, di;

    uint8_t *dst;

    int bufidx;



    // src[0]&0x80; // forbidden bit

    h->nal_ref_idc   = src[0] >> 5;

    h->nal_unit_type = src[0] & 0x1F;



    src++;

    length--;



#define STARTCODE_TEST                                                  \

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

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

            /* 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

#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) { // no escaped 0

        *dst_length = length;

        *consumed   = length + 1; // +1 for the header

        return src;

    }



    // use second escape buffer for inter data

    bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;

    av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx],

                   length + FF_INPUT_BUFFER_PADDING_SIZE);

    dst = h->rbsp_buffer[bufidx];



    if (dst == NULL)

        return NULL;



    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) {

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

                dst[di++]  = 0;

                dst[di++]  = 0;

                si        += 3;

                continue;

            } else // next start code

                goto nsc;

        }



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

    }

    while (si < length)

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



nsc:

    memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);



    *dst_length = di;

    *consumed   = si + 1; // +1 for the header

    /* FIXME store exact number of bits in the getbitcontext

     * (it is needed for decoding) */

    return dst;

}

const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,

                                  int *dst_length, int *consumed, int length)

{

    int i, si, di;

    uint8_t *dst;

    int bufidx;



    

    h->nal_ref_idc   = src[0] >> 5;

    h->nal_unit_type = src[0] & 0x1F;



    src++;

    length--;



#define STARTCODE_TEST                                                  \

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

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

                             \

            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) { 

        *dst_length = length;

        *consumed   = length + 1; 

        return src;

    }



    

    bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;

    av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx],

                   length + FF_INPUT_BUFFER_PADDING_SIZE);

    dst = h->rbsp_buffer[bufidx];



    if (dst == NULL)

        return NULL;



    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) {

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

                dst[di++]  = 0;

                dst[di++]  = 0;

                si        += 3;

                continue;

            } else 

                goto nsc;

        }



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

    }

    while (si < length)

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



nsc:

    memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);



    *dst_length = di;

    *consumed   = si + 1; 

    



    return dst;

}

const uint8_t *FUNC_0(H264Context *h, const uint8_t *src,

                                  int *dst_length, int *consumed, int length)

{

    int VAR_0, VAR_1, VAR_2;

    uint8_t *dst;

    int VAR_3;



    

    h->nal_ref_idc   = src[0] >> 5;

    h->nal_unit_type = src[0] & 0x1F;



    src++;

    length--;



#define STARTCODE_TEST                                                  \

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

        if (src[VAR_0 + 2] != 3) {                                          \

                             \

            length = VAR_0;                                                 \

        }                                                               \

        break;                                                          \

    }



#if HAVE_FAST_UNALIGNED

#define FIND_FIRST_ZERO                                                 \

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

        VAR_0--;                                                            \

    while (src[VAR_0])                                                      \

        VAR_0++



#if HAVE_FAST_64BIT

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

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

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

              0x8000800080008080ULL))

            continue;

        FIND_FIRST_ZERO;

        STARTCODE_TEST;

        VAR_0 -= 7;

    }

#else

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

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

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

              0x80008080U))

            continue;

        FIND_FIRST_ZERO;

        STARTCODE_TEST;

        VAR_0 -= 3;

    }

#endif

#else

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

        if (src[VAR_0])

            continue;

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

            VAR_0--;

        STARTCODE_TEST;

    }

#endif



    if (VAR_0 >= length - 1) { 

        *dst_length = length;

        *consumed   = length + 1; 

        return src;

    }



    

    VAR_3 = h->nal_unit_type == NAL_DPC ? 1 : 0;

    av_fast_malloc(&h->rbsp_buffer[VAR_3], &h->rbsp_buffer_size[VAR_3],

                   length + FF_INPUT_BUFFER_PADDING_SIZE);

    dst = h->rbsp_buffer[VAR_3];



    if (dst == NULL)

        return NULL;



    memcpy(dst, src, VAR_0);

    VAR_1 = VAR_2 = VAR_0;

    while (VAR_1 + 2 < length) {

        

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

            dst[VAR_2++] = src[VAR_1++];

            dst[VAR_2++] = src[VAR_1++];

        } else if (src[VAR_1] == 0 && src[VAR_1 + 1] == 0) {

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

                dst[VAR_2++]  = 0;

                dst[VAR_2++]  = 0;

                VAR_1        += 3;

                continue;

            } else 

                goto nsc;

        }



        dst[VAR_2++] = src[VAR_1++];

    }

    while (VAR_1 < length)

        dst[VAR_2++] = src[VAR_1++];



nsc:

    memset(dst + VAR_2, 0, FF_INPUT_BUFFER_PADDING_SIZE);



    *dst_length = VAR_2;

    *consumed   = VAR_1 + 1; 

    



    return dst;

}

const uint8_t *FUNC_0(H264Context *h, const uint8_t *src,
int *dst_length, int *consumed, int length)
{ int VAR_0, VAR_1, VAR_2; uint8_t *dst; int VAR_3; h->nal_ref_idc   = src[0] >> 5; h->nal_unit_type = src[0] & 0x1F; src++; length--; #define STARTCODE_TEST                                                  \
if (VAR_0 + 2 < length && src[VAR_0 + 1] == 0 && src[VAR_0 + 2] <= 3) {         \ if (src[VAR_0 + 2] != 3) {                                          \ \
length = VAR_0;                                                 \ }                                                               \ break;                                                          \ } #if HAVE_FAST_UNALIGNED
#define FIND_FIRST_ZERO                                                 \
if (VAR_0 > 0 && !src[VAR_0])                                               \
VAR_0--;                                                            \ while (src[VAR_0])                                                      \
VAR_0++
#if HAVE_FAST_64BIT
for (VAR_0 = 0; VAR_0 + 1 < length; VAR_0 += 9) { if (!((~AV_RN64A(src + VAR_0) &
(AV_RN64A(src + VAR_0) - 0x0100010001000101ULL)) &
0x8000800080008080ULL))
continue; FIND_FIRST_ZERO; STARTCODE_TEST; VAR_0 -= 7; } #else
for (VAR_0 = 0; VAR_0 + 1 < length; VAR_0 += 5) { if (!((~AV_RN32A(src + VAR_0) &
(AV_RN32A(src + VAR_0) - 0x01000101U)) &
0x80008080U))
continue; FIND_FIRST_ZERO; STARTCODE_TEST; VAR_0 -= 3; } #endif
#else
for (VAR_0 = 0; VAR_0 + 1 < length; VAR_0 += 2) { if (src[VAR_0])
continue; if (VAR_0 > 0 && src[VAR_0 - 1] == 0)
VAR_0--; STARTCODE_TEST; } #endif
if (VAR_0 >= length - 1) { *dst_length = length; *consumed   = length + 1; return src; } VAR_3 = h->nal_unit_type == NAL_DPC ? 1 : 0; av_fast_malloc(&h->rbsp_buffer[VAR_3], &h->rbsp_buffer_size[VAR_3],
length + FF_INPUT_BUFFER_PADDING_SIZE); dst = h->rbsp_buffer[VAR_3]; if (dst == NULL)
return NULL; memcpy(dst, src, VAR_0); VAR_1 = VAR_2 = VAR_0; while (VAR_1 + 2 < length) { if (src[VAR_1 + 2] > 3) { dst[VAR_2++] = src[VAR_1++]; dst[VAR_2++] = src[VAR_1++]; } else if (src[VAR_1] == 0 && src[VAR_1 + 1] == 0) { if (src[VAR_1 + 2] == 3) { dst[VAR_2++]  = 0; dst[VAR_2++]  = 0; VAR_1        += 3; continue; } else goto nsc; } dst[VAR_2++] = src[VAR_1++]; } while (VAR_1 < length)
dst[VAR_2++] = src[VAR_1++]; nsc:
memset(dst + VAR_2, 0, FF_INPUT_BUFFER_PADDING_SIZE); *dst_length = VAR_2; *consumed   = VAR_1 + 1; return dst; }