static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block,
                        int component, int dc_index, int ac_index, int16_t *quant_matrix,
                        int ss, int se, int Ah, int Al, int *EOBRUN)
{
    int code, i, j, level, val, run;
    if(!ss){
        val = mjpeg_decode_dc(s, dc_index);
        if (val == 0xffff) {
            av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
            return -1;
        }
        val = (val * quant_matrix[0] << Al) + s->last_dc[component];
    }else
        val = 0;
    s->last_dc[component] = val;
    block[0] = val;
    if(!se) return 0;
    if(*EOBRUN){
        (*EOBRUN)--;
        return 0;
    }
    {OPEN_READER(re, &s->gb)
    for(i=ss;;i++) {
        UPDATE_CACHE(re, &s->gb);
        GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
        code -= 16;
        if(code & 0xF) {
            i += ((unsigned) code) >> 4;
            code &= 0xf;
            if(code > MIN_CACHE_BITS - 16){
                UPDATE_CACHE(re, &s->gb)
            }
            {
                int cache=GET_CACHE(re,&s->gb);
                int sign=(~cache)>>31;
                level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
            }
            LAST_SKIP_BITS(re, &s->gb, code)
            if (i >= se) {
                if(i == se){
                    j = s->scantable.permutated[se];
                    block[j] = level * quant_matrix[j] << Al;
                    break;
                }
                av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
                return -1;
            }
            j = s->scantable.permutated[i];
            block[j] = level * quant_matrix[j] << Al;
        }else{
            run = ((unsigned) code) >> 4;
            if(run == 0xF){
                i += 15;
            }else{
                val = run;
                run = (1 << run);
                UPDATE_CACHE(re, &s->gb);
                run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1);
                if(val)
                    LAST_SKIP_BITS(re, &s->gb, val);
                *EOBRUN = run - 1;
                break;
            }
        }
    }
    CLOSE_READER(re, &s->gb)}
    return 0;
}
['int ff_mjpeg_decode_frame(AVCodecContext *avctx,\n                              void *data, int *data_size,\n                              const uint8_t *buf, int buf_size)\n{\n    MJpegDecodeContext *s = avctx->priv_data;\n    const uint8_t *buf_end, *buf_ptr;\n    int start_code;\n    AVFrame *picture = data;\n    buf_ptr = buf;\n    buf_end = buf + buf_size;\n    while (buf_ptr < buf_end) {\n        start_code = find_marker(&buf_ptr, buf_end);\n        {\n            if (start_code < 0) {\n                goto the_end;\n            } else {\n                av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\\n", start_code, buf_end - buf_ptr);\n                if ((buf_end - buf_ptr) > s->buffer_size)\n                {\n                    av_free(s->buffer);\n                    s->buffer_size = buf_end-buf_ptr;\n                    s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);\n                    av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\\n",\n                        s->buffer_size);\n                }\n                if (start_code == SOS && !s->ls)\n                {\n                    const uint8_t *src = buf_ptr;\n                    uint8_t *dst = s->buffer;\n                    while (src<buf_end)\n                    {\n                        uint8_t x = *(src++);\n                        *(dst++) = x;\n                        if (avctx->codec_id != CODEC_ID_THP)\n                        {\n                            if (x == 0xff) {\n                                while (src < buf_end && x == 0xff)\n                                    x = *(src++);\n                                if (x >= 0xd0 && x <= 0xd7)\n                                    *(dst++) = x;\n                                else if (x)\n                                    break;\n                            }\n                        }\n                    }\n                    init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);\n                    av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\\n",\n                           (buf_end - buf_ptr) - (dst - s->buffer));\n                }\n                else if(start_code == SOS && s->ls){\n                    const uint8_t *src = buf_ptr;\n                    uint8_t *dst = s->buffer;\n                    int bit_count = 0;\n                    int t = 0, b = 0;\n                    PutBitContext pb;\n                    s->cur_scan++;\n                    while (src + t < buf_end){\n                        uint8_t x = src[t++];\n                        if (x == 0xff){\n                            while((src + t < buf_end) && x == 0xff)\n                                x = src[t++];\n                            if (x & 0x80) {\n                                t -= 2;\n                                break;\n                            }\n                        }\n                    }\n                    bit_count = t * 8;\n                    init_put_bits(&pb, dst, t);\n                    while(b < t){\n                        uint8_t x = src[b++];\n                        put_bits(&pb, 8, x);\n                        if(x == 0xFF){\n                            x = src[b++];\n                            put_bits(&pb, 7, x);\n                            bit_count--;\n                        }\n                    }\n                    flush_put_bits(&pb);\n                    init_get_bits(&s->gb, dst, bit_count);\n                }\n                else\n                    init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);\n                s->start_code = start_code;\n                if(s->avctx->debug & FF_DEBUG_STARTCODE){\n                    av_log(avctx, AV_LOG_DEBUG, "startcode: %X\\n", start_code);\n                }\n                if (start_code >= 0xd0 && start_code <= 0xd7) {\n                    av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\\n", start_code&0x0f);\n                } else if (start_code >= APP0 && start_code <= APP15) {\n                    mjpeg_decode_app(s);\n                } else if (start_code == COM){\n                    mjpeg_decode_com(s);\n                }\n                switch(start_code) {\n                case SOI:\n                    s->restart_interval = 0;\n                    s->restart_count = 0;\n                    break;\n                case DQT:\n                    ff_mjpeg_decode_dqt(s);\n                    break;\n                case DHT:\n                    if(ff_mjpeg_decode_dht(s) < 0){\n                        av_log(avctx, AV_LOG_ERROR, "huffman table decode error\\n");\n                        return -1;\n                    }\n                    break;\n                case SOF0:\n                    s->lossless=0;\n                    s->ls=0;\n                    s->progressive=0;\n                    if (ff_mjpeg_decode_sof(s) < 0)\n                        return -1;\n                    break;\n                case SOF2:\n                    s->lossless=0;\n                    s->ls=0;\n                    s->progressive=1;\n                    if (ff_mjpeg_decode_sof(s) < 0)\n                        return -1;\n                    break;\n                case SOF3:\n                    s->lossless=1;\n                    s->ls=0;\n                    s->progressive=0;\n                    if (ff_mjpeg_decode_sof(s) < 0)\n                        return -1;\n                    break;\n                case SOF48:\n                    s->lossless=1;\n                    s->ls=1;\n                    s->progressive=0;\n                    if (ff_mjpeg_decode_sof(s) < 0)\n                        return -1;\n                    break;\n                case LSE:\n                    if (!ENABLE_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)\n                        return -1;\n                    break;\n                case EOI:\n                    s->cur_scan = 0;\n                    if ((s->buggy_avid && !s->interlaced) || s->restart_interval)\n                        break;\neoi_parser:\n                    {\n                        if (s->interlaced) {\n                            s->bottom_field ^= 1;\n                            if (s->bottom_field == !s->interlace_polarity)\n                                goto not_the_end;\n                        }\n                        *picture = s->picture;\n                        *data_size = sizeof(AVFrame);\n                        if(!s->lossless){\n                            picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);\n                            picture->qstride= 0;\n                            picture->qscale_table= s->qscale_table;\n                            memset(picture->qscale_table, picture->quality, (s->width+15)/16);\n                            if(avctx->debug & FF_DEBUG_QP)\n                                av_log(avctx, AV_LOG_DEBUG, "QP: %d\\n", picture->quality);\n                            picture->quality*= FF_QP2LAMBDA;\n                        }\n                        goto the_end;\n                    }\n                    break;\n                case SOS:\n                    ff_mjpeg_decode_sos(s);\n                    if ((s->buggy_avid && !s->interlaced) || s->restart_interval)\n                        goto eoi_parser;\n                    break;\n                case DRI:\n                    mjpeg_decode_dri(s);\n                    break;\n                case SOF1:\n                case SOF5:\n                case SOF6:\n                case SOF7:\n                case SOF9:\n                case SOF10:\n                case SOF11:\n                case SOF13:\n                case SOF14:\n                case SOF15:\n                case JPG:\n                    av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\\n", start_code);\n                    break;\n                }\nnot_the_end:\n                buf_ptr += (get_bits_count(&s->gb)+7)/8;\n                av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\\n",\n                       (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));\n            }\n        }\n    }\nthe_end:\n    av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\\n", buf_end - buf_ptr);\n    return buf_ptr - buf;\n}', 'static inline void init_get_bits(GetBitContext *s,\n                   const uint8_t *buffer, int bit_size)\n{\n    int buffer_size= (bit_size+7)>>3;\n    if(buffer_size < 0 || bit_size < 0) {\n        buffer_size = bit_size = 0;\n        buffer = NULL;\n    }\n    s->buffer= buffer;\n    s->size_in_bits= bit_size;\n    s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n    s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n    s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n    s->bit_count = 16 + 8*((intptr_t)buffer&1);\n    skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n    s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n    s->bit_count = 32 + 8*((intptr_t)buffer&3);\n    skip_bits_long(s, 0);\n#endif\n}', 'int ff_mjpeg_decode_sos(MJpegDecodeContext *s)\n{\n    int len, nb_components, i, h, v, predictor, point_transform;\n    int vmax, hmax, index, id;\n    const int block_size= s->lossless ? 1 : 8;\n    int ilv, prev_shift;\n    len = get_bits(&s->gb, 16);\n    nb_components = get_bits(&s->gb, 8);\n    if (len != 6+2*nb_components)\n    {\n        av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\\n", len);\n        return -1;\n    }\n    vmax = 0;\n    hmax = 0;\n    for(i=0;i<nb_components;i++) {\n        id = get_bits(&s->gb, 8) - 1;\n        av_log(s->avctx, AV_LOG_DEBUG, "component: %d\\n", id);\n        for(index=0;index<s->nb_components;index++)\n            if (id == s->component_id[index])\n                break;\n        if (index == s->nb_components)\n        {\n            av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\\n", index);\n            return -1;\n        }\n        s->comp_index[i] = index;\n        s->nb_blocks[i] = s->h_count[index] * s->v_count[index];\n        s->h_scount[i] = s->h_count[index];\n        s->v_scount[i] = s->v_count[index];\n        s->dc_index[i] = get_bits(&s->gb, 4);\n        s->ac_index[i] = get_bits(&s->gb, 4);\n        if (s->dc_index[i] <  0 || s->ac_index[i] < 0 ||\n            s->dc_index[i] >= 4 || s->ac_index[i] >= 4)\n            goto out_of_range;\n#if 0\n        switch(s->start_code)\n        {\n            case SOF0:\n                if (dc_index[i] > 1 || ac_index[i] > 1)\n                    goto out_of_range;\n                break;\n            case SOF1:\n            case SOF2:\n                if (dc_index[i] > 3 || ac_index[i] > 3)\n                    goto out_of_range;\n                break;\n            case SOF3:\n                if (dc_index[i] > 3 || ac_index[i] != 0)\n                    goto out_of_range;\n                break;\n        }\n#endif\n    }\n    predictor= get_bits(&s->gb, 8);\n    ilv= get_bits(&s->gb, 8);\n    prev_shift = get_bits(&s->gb, 4);\n    point_transform= get_bits(&s->gb, 4);\n    for(i=0;i<nb_components;i++)\n        s->last_dc[i] = 1024;\n    if (nb_components > 1) {\n        s->mb_width  = (s->width  + s->h_max * block_size - 1) / (s->h_max * block_size);\n        s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);\n    } else if(!s->ls) {\n        h = s->h_max / s->h_scount[0];\n        v = s->v_max / s->v_scount[0];\n        s->mb_width  = (s->width  + h * block_size - 1) / (h * block_size);\n        s->mb_height = (s->height + v * block_size - 1) / (v * block_size);\n        s->nb_blocks[0] = 1;\n        s->h_scount[0] = 1;\n        s->v_scount[0] = 1;\n    }\n    if(s->avctx->debug & FF_DEBUG_PICT_INFO)\n        av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "",\n               predictor, point_transform, ilv, s->bits,\n               s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));\n    for (i = s->mjpb_skiptosod; i > 0; i--)\n        skip_bits(&s->gb, 8);\n    if(s->lossless){\n        if(ENABLE_JPEGLS_DECODER && s->ls){\n            ff_jpegls_decode_picture(s, predictor, point_transform, ilv);\n        }else{\n            if(s->rgb){\n                if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)\n                    return -1;\n            }else{\n                if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)\n                    return -1;\n            }\n        }\n    }else{\n        if(mjpeg_decode_scan(s, nb_components, predictor, ilv, prev_shift, point_transform) < 0)\n            return -1;\n    }\n    emms_c();\n    return 0;\n out_of_range:\n    av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\\n");\n    return -1;\n}', 'static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int ss, int se, int Ah, int Al){\n    int i, mb_x, mb_y;\n    int EOBRUN = 0;\n    uint8_t* data[MAX_COMPONENTS];\n    int linesize[MAX_COMPONENTS];\n    if(Ah) return 0;\n    for(i=0; i < nb_components; i++) {\n        int c = s->comp_index[i];\n        data[c] = s->picture.data[c];\n        linesize[c]=s->linesize[c];\n        if(s->avctx->codec->id==CODEC_ID_AMV) {\n            assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));\n            data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));\n            linesize[c] *= -1;\n        }\n    }\n    for(mb_y = 0; mb_y < s->mb_height; mb_y++) {\n        for(mb_x = 0; mb_x < s->mb_width; mb_x++) {\n            if (s->restart_interval && !s->restart_count)\n                s->restart_count = s->restart_interval;\n            for(i=0;i<nb_components;i++) {\n                uint8_t *ptr;\n                int n, h, v, x, y, c, j;\n                n = s->nb_blocks[i];\n                c = s->comp_index[i];\n                h = s->h_scount[i];\n                v = s->v_scount[i];\n                x = 0;\n                y = 0;\n                for(j=0;j<n;j++) {\n                    memset(s->block, 0, sizeof(s->block));\n                    if (!s->progressive && decode_block(s, s->block, i,\n                                     s->dc_index[i], s->ac_index[i],\n                                     s->quant_matrixes[ s->quant_index[c] ]) < 0) {\n                        av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\\n", mb_y, mb_x);\n                        return -1;\n                    }\n                    if (s->progressive && decode_block_progressive(s, s->block, i,\n                                     s->dc_index[i], s->ac_index[i],\n                                     s->quant_matrixes[ s->quant_index[c] ], ss, se, Ah, Al, &EOBRUN) < 0) {\n                        av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\\n", mb_y, mb_x);\n                        return -1;\n                    }\n                    ptr = data[c] +\n                        (((linesize[c] * (v * mb_y + y) * 8) +\n                        (h * mb_x + x) * 8) >> s->avctx->lowres);\n                    if (s->interlaced && s->bottom_field)\n                        ptr += linesize[c] >> 1;\n                    if(!s->progressive)\n                        s->dsp.idct_put(ptr, linesize[c], s->block);\n                    else\n                        s->dsp.idct_add(ptr, linesize[c], s->block);\n                    if (++x == h) {\n                        x = 0;\n                        y++;\n                    }\n                }\n            }\n            if (s->restart_interval && (s->restart_interval < 1350) &&\n                !--s->restart_count) {\n                align_get_bits(&s->gb);\n                skip_bits(&s->gb, 16);\n                for (i=0; i<nb_components; i++)\n                    s->last_dc[i] = 1024;\n            }\n        }\n    }\n    return 0;\n}', 'static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block,\n                        int component, int dc_index, int ac_index, int16_t *quant_matrix,\n                        int ss, int se, int Ah, int Al, int *EOBRUN)\n{\n    int code, i, j, level, val, run;\n    if(!ss){\n        val = mjpeg_decode_dc(s, dc_index);\n        if (val == 0xffff) {\n            av_log(s->avctx, AV_LOG_ERROR, "error dc\\n");\n            return -1;\n        }\n        val = (val * quant_matrix[0] << Al) + s->last_dc[component];\n    }else\n        val = 0;\n    s->last_dc[component] = val;\n    block[0] = val;\n    if(!se) return 0;\n    if(*EOBRUN){\n        (*EOBRUN)--;\n        return 0;\n    }\n    {OPEN_READER(re, &s->gb)\n    for(i=ss;;i++) {\n        UPDATE_CACHE(re, &s->gb);\n        GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)\n        code -= 16;\n        if(code & 0xF) {\n            i += ((unsigned) code) >> 4;\n            code &= 0xf;\n            if(code > MIN_CACHE_BITS - 16){\n                UPDATE_CACHE(re, &s->gb)\n            }\n            {\n                int cache=GET_CACHE(re,&s->gb);\n                int sign=(~cache)>>31;\n                level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;\n            }\n            LAST_SKIP_BITS(re, &s->gb, code)\n            if (i >= se) {\n                if(i == se){\n                    j = s->scantable.permutated[se];\n                    block[j] = level * quant_matrix[j] << Al;\n                    break;\n                }\n                av_log(s->avctx, AV_LOG_ERROR, "error count: %d\\n", i);\n                return -1;\n            }\n            j = s->scantable.permutated[i];\n            block[j] = level * quant_matrix[j] << Al;\n        }else{\n            run = ((unsigned) code) >> 4;\n            if(run == 0xF){\n                i += 15;\n            }else{\n                val = run;\n                run = (1 << run);\n                UPDATE_CACHE(re, &s->gb);\n                run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1);\n                if(val)\n                    LAST_SKIP_BITS(re, &s->gb, val);\n                *EOBRUN = run - 1;\n                break;\n            }\n        }\n    }\n    CLOSE_READER(re, &s->gb)}\n    return 0;\n}']