int ff_mjpeg_find_marker(MJpegDecodeContext *s,

                         const uint8_t **buf_ptr, const uint8_t *buf_end,

                         const uint8_t **unescaped_buf_ptr,

                         int *unescaped_buf_size)

{

    int start_code;

    start_code = find_marker(buf_ptr, buf_end);



    av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);

    if (!s->buffer)

        return AVERROR(ENOMEM);



    /* unescape buffer of SOS, use special treatment for JPEG-LS */

    if (start_code == SOS && !s->ls) {

        const uint8_t *src = *buf_ptr;

        uint8_t *dst = s->buffer;



        while (src < buf_end) {

            uint8_t x = *(src++);



            *(dst++) = x;

            if (s->avctx->codec_id != AV_CODEC_ID_THP) {

                if (x == 0xff) {

                    while (src < buf_end && x == 0xff)

                        x = *(src++);



                    if (x >= 0xd0 && x <= 0xd7)

                        *(dst++) = x;

                    else if (x)

                        break;

                }

            }

        }

        *unescaped_buf_ptr  = s->buffer;

        *unescaped_buf_size = dst - s->buffer;

        memset(s->buffer + *unescaped_buf_size, 0,

               FF_INPUT_BUFFER_PADDING_SIZE);



        av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",

               (buf_end - *buf_ptr) - (dst - s->buffer));

    } else if (start_code == SOS && s->ls) {

        const uint8_t *src = *buf_ptr;

        uint8_t *dst  = s->buffer;

        int bit_count = 0;

        int t = 0, b = 0;

        PutBitContext pb;



        s->cur_scan++;



        /* find marker */

        while (src + t < buf_end) {

            uint8_t x = src[t++];

            if (x == 0xff) {

                while ((src + t < buf_end) && x == 0xff)

                    x = src[t++];

                if (x & 0x80) {

                    t -= 2;

                    break;

                }

            }

        }

        bit_count = t * 8;

        init_put_bits(&pb, dst, t);



        /* unescape bitstream */

        while (b < t) {

            uint8_t x = src[b++];

            put_bits(&pb, 8, x);

            if (x == 0xFF) {

                x = src[b++];

                put_bits(&pb, 7, x);

                bit_count--;

            }

        }

        flush_put_bits(&pb);



        *unescaped_buf_ptr  = dst;

        *unescaped_buf_size = (bit_count + 7) >> 3;

        memset(s->buffer + *unescaped_buf_size, 0,

               FF_INPUT_BUFFER_PADDING_SIZE);

    } else {

        *unescaped_buf_ptr  = *buf_ptr;

        *unescaped_buf_size = buf_end - *buf_ptr;

    }



    return start_code;

}

int ff_mjpeg_find_marker(MJpegDecodeContext *s,

                         const uint8_t **buf_ptr, const uint8_t *buf_end,

                         const uint8_t **unescaped_buf_ptr,

                         int *unescaped_buf_size)

{

    int start_code;

    start_code = find_marker(buf_ptr, buf_end);



    av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);

    if (!s->buffer)

        return AVERROR(ENOMEM);



    

    if (start_code == SOS && !s->ls) {

        const uint8_t *src = *buf_ptr;

        uint8_t *dst = s->buffer;



        while (src < buf_end) {

            uint8_t x = *(src++);



            *(dst++) = x;

            if (s->avctx->codec_id != AV_CODEC_ID_THP) {

                if (x == 0xff) {

                    while (src < buf_end && x == 0xff)

                        x = *(src++);



                    if (x >= 0xd0 && x <= 0xd7)

                        *(dst++) = x;

                    else if (x)

                        break;

                }

            }

        }

        *unescaped_buf_ptr  = s->buffer;

        *unescaped_buf_size = dst - s->buffer;

        memset(s->buffer + *unescaped_buf_size, 0,

               FF_INPUT_BUFFER_PADDING_SIZE);



        av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",

               (buf_end - *buf_ptr) - (dst - s->buffer));

    } else if (start_code == SOS && s->ls) {

        const uint8_t *src = *buf_ptr;

        uint8_t *dst  = s->buffer;

        int bit_count = 0;

        int t = 0, b = 0;

        PutBitContext pb;



        s->cur_scan++;



        

        while (src + t < buf_end) {

            uint8_t x = src[t++];

            if (x == 0xff) {

                while ((src + t < buf_end) && x == 0xff)

                    x = src[t++];

                if (x & 0x80) {

                    t -= 2;

                    break;

                }

            }

        }

        bit_count = t * 8;

        init_put_bits(&pb, dst, t);



        

        while (b < t) {

            uint8_t x = src[b++];

            put_bits(&pb, 8, x);

            if (x == 0xFF) {

                x = src[b++];

                put_bits(&pb, 7, x);

                bit_count--;

            }

        }

        flush_put_bits(&pb);



        *unescaped_buf_ptr  = dst;

        *unescaped_buf_size = (bit_count + 7) >> 3;

        memset(s->buffer + *unescaped_buf_size, 0,

               FF_INPUT_BUFFER_PADDING_SIZE);

    } else {

        *unescaped_buf_ptr  = *buf_ptr;

        *unescaped_buf_size = buf_end - *buf_ptr;

    }



    return start_code;

}

int FUNC_0(MJpegDecodeContext *VAR_0,

                         const uint8_t **VAR_1, const uint8_t *VAR_2,

                         const uint8_t **VAR_3,

                         int *VAR_4)

{

    int VAR_5;

    VAR_5 = find_marker(VAR_1, VAR_2);



    av_fast_padded_malloc(&VAR_0->buffer, &VAR_0->buffer_size, VAR_2 - *VAR_1);

    if (!VAR_0->buffer)

        return AVERROR(ENOMEM);



    

    if (VAR_5 == SOS && !VAR_0->ls) {

        const uint8_t *VAR_7 = *VAR_1;

        uint8_t *dst = VAR_0->buffer;



        while (VAR_7 < VAR_2) {

            uint8_t x = *(VAR_7++);



            *(dst++) = x;

            if (VAR_0->avctx->codec_id != AV_CODEC_ID_THP) {

                if (x == 0xff) {

                    while (VAR_7 < VAR_2 && x == 0xff)

                        x = *(VAR_7++);



                    if (x >= 0xd0 && x <= 0xd7)

                        *(dst++) = x;

                    else if (x)

                        break;

                }

            }

        }

        *VAR_3  = VAR_0->buffer;

        *VAR_4 = dst - VAR_0->buffer;

        memset(VAR_0->buffer + *VAR_4, 0,

               FF_INPUT_BUFFER_PADDING_SIZE);



        av_log(VAR_0->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",

               (VAR_2 - *VAR_1) - (dst - VAR_0->buffer));

    } else if (VAR_5 == SOS && VAR_0->ls) {

        const uint8_t *VAR_7 = *VAR_1;

        uint8_t *dst  = VAR_0->buffer;

        int VAR_7 = 0;

        int VAR_8 = 0, VAR_9 = 0;

        PutBitContext pb;



        VAR_0->cur_scan++;



        

        while (VAR_7 + VAR_8 < VAR_2) {

            uint8_t x = VAR_7[VAR_8++];

            if (x == 0xff) {

                while ((VAR_7 + VAR_8 < VAR_2) && x == 0xff)

                    x = VAR_7[VAR_8++];

                if (x & 0x80) {

                    VAR_8 -= 2;

                    break;

                }

            }

        }

        VAR_7 = VAR_8 * 8;

        init_put_bits(&pb, dst, VAR_8);



        

        while (VAR_9 < VAR_8) {

            uint8_t x = VAR_7[VAR_9++];

            put_bits(&pb, 8, x);

            if (x == 0xFF) {

                x = VAR_7[VAR_9++];

                put_bits(&pb, 7, x);

                VAR_7--;

            }

        }

        flush_put_bits(&pb);



        *VAR_3  = dst;

        *VAR_4 = (VAR_7 + 7) >> 3;

        memset(VAR_0->buffer + *VAR_4, 0,

               FF_INPUT_BUFFER_PADDING_SIZE);

    } else {

        *VAR_3  = *VAR_1;

        *VAR_4 = VAR_2 - *VAR_1;

    }



    return VAR_5;

}

int FUNC_0(MJpegDecodeContext *VAR_0,
const uint8_t **VAR_1, const uint8_t *VAR_2,
const uint8_t **VAR_3,
int *VAR_4)
{ int VAR_5; VAR_5 = find_marker(VAR_1, VAR_2); av_fast_padded_malloc(&VAR_0->buffer, &VAR_0->buffer_size, VAR_2 - *VAR_1); if (!VAR_0->buffer)
return AVERROR(ENOMEM); if (VAR_5 == SOS && !VAR_0->ls) { const uint8_t *VAR_7 = *VAR_1; uint8_t *dst = VAR_0->buffer; while (VAR_7 < VAR_2) { uint8_t x = *(VAR_7++); *(dst++) = x; if (VAR_0->avctx->codec_id != AV_CODEC_ID_THP) { if (x == 0xff) { while (VAR_7 < VAR_2 && x == 0xff)
x = *(VAR_7++); if (x >= 0xd0 && x <= 0xd7)
*(dst++) = x; else if (x)
break; } } } *VAR_3  = VAR_0->buffer; *VAR_4 = dst - VAR_0->buffer; memset(VAR_0->buffer + *VAR_4, 0,
FF_INPUT_BUFFER_PADDING_SIZE); av_log(VAR_0->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
(VAR_2 - *VAR_1) - (dst - VAR_0->buffer)); } else if (VAR_5 == SOS && VAR_0->ls) { const uint8_t *VAR_7 = *VAR_1; uint8_t *dst  = VAR_0->buffer; int VAR_7 = 0; int VAR_8 = 0, VAR_9 = 0; PutBitContext pb; VAR_0->cur_scan++; while (VAR_7 + VAR_8 < VAR_2) { uint8_t x = VAR_7[VAR_8++]; if (x == 0xff) { while ((VAR_7 + VAR_8 < VAR_2) && x == 0xff)
x = VAR_7[VAR_8++]; if (x & 0x80) { VAR_8 -= 2; break; } } } VAR_7 = VAR_8 * 8; init_put_bits(&pb, dst, VAR_8); while (VAR_9 < VAR_8) { uint8_t x = VAR_7[VAR_9++]; put_bits(&pb, 8, x); if (x == 0xFF) { x = VAR_7[VAR_9++]; put_bits(&pb, 7, x); VAR_7--; } } flush_put_bits(&pb); *VAR_3  = dst; *VAR_4 = (VAR_7 + 7) >> 3; memset(VAR_0->buffer + *VAR_4, 0,
FF_INPUT_BUFFER_PADDING_SIZE); } else { *VAR_3  = *VAR_1; *VAR_4 = VAR_2 - *VAR_1; } return VAR_5; }