static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)

{

    DNXHDEncContext *ctx = avctx->priv_data;

    int mb_y = jobnr, mb_x;

    ctx = ctx->thread[threadnr];

    if (ctx->cid_table->bit_depth == 8) {

        uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y<<4) * ctx->m.linesize);

        for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {

            unsigned mb  = mb_y * ctx->m.mb_width + mb_x;

            int sum = ctx->m.dsp.pix_sum(pix, ctx->m.linesize);

            int varc = (ctx->m.dsp.pix_norm1(pix, ctx->m.linesize) - (((unsigned)(sum*sum))>>8)+128)>>8;

            ctx->mb_cmp[mb].value = varc;

            ctx->mb_cmp[mb].mb = mb;

        }

    } else { // 10-bit

        int const linesize = ctx->m.linesize >> 1;

        for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {

            uint16_t *pix = (uint16_t*)ctx->thread[0]->src[0] + ((mb_y << 4) * linesize) + (mb_x << 4);

            unsigned mb  = mb_y * ctx->m.mb_width + mb_x;

            int sum = 0;

            int sqsum = 0;

            int mean, sqmean;

            // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.

            for (int i = 0; i < 16; ++i) {

                for (int j = 0; j < 16; ++j) {

                    // Turn 16-bit pixels into 10-bit ones.

                    int const sample = (unsigned)pix[j] >> 6;

                    sum += sample;

                    sqsum += sample * sample;

                    // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX

                }

                pix += linesize;

            }

            mean = sum >> 8; // 16*16 == 2^8

            sqmean = sqsum >> 8;

            ctx->mb_cmp[mb].value = sqmean - mean * mean;

            ctx->mb_cmp[mb].mb = mb;

        }

    }

    return 0;

}

static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)

{

    DNXHDEncContext *ctx = avctx->priv_data;

    int mb_y = jobnr, mb_x;

    ctx = ctx->thread[threadnr];

    if (ctx->cid_table->bit_depth == 8) {

        uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y<<4) * ctx->m.linesize);

        for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {

            unsigned mb  = mb_y * ctx->m.mb_width + mb_x;

            int sum = ctx->m.dsp.pix_sum(pix, ctx->m.linesize);

            int varc = (ctx->m.dsp.pix_norm1(pix, ctx->m.linesize) - (((unsigned)(sum*sum))>>8)+128)>>8;

            ctx->mb_cmp[mb].value = varc;

            ctx->mb_cmp[mb].mb = mb;

        }

    } else { 

        int const linesize = ctx->m.linesize >> 1;

        for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {

            uint16_t *pix = (uint16_t*)ctx->thread[0]->src[0] + ((mb_y << 4) * linesize) + (mb_x << 4);

            unsigned mb  = mb_y * ctx->m.mb_width + mb_x;

            int sum = 0;

            int sqsum = 0;

            int mean, sqmean;

            

            for (int i = 0; i < 16; ++i) {

                for (int j = 0; j < 16; ++j) {

                    

                    int const sample = (unsigned)pix[j] >> 6;

                    sum += sample;

                    sqsum += sample * sample;

                    

                }

                pix += linesize;

            }

            mean = sum >> 8; 

            sqmean = sqsum >> 8;

            ctx->mb_cmp[mb].value = sqmean - mean * mean;

            ctx->mb_cmp[mb].mb = mb;

        }

    }

    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int VAR_2, int VAR_3)

{

    DNXHDEncContext *ctx = VAR_0->priv_data;

    int VAR_4 = VAR_2, VAR_5;

    ctx = ctx->thread[VAR_3];

    if (ctx->cid_table->bit_depth == 8) {

        uint8_t *pix = ctx->thread[0]->src[0] + ((VAR_4<<4) * ctx->m.VAR_6);

        for (VAR_5 = 0; VAR_5 < ctx->m.mb_width; ++VAR_5, pix += 16) {

            unsigned mb  = VAR_4 * ctx->m.mb_width + VAR_5;

            int sum = ctx->m.dsp.pix_sum(pix, ctx->m.VAR_6);

            int varc = (ctx->m.dsp.pix_norm1(pix, ctx->m.VAR_6) - (((unsigned)(sum*sum))>>8)+128)>>8;

            ctx->mb_cmp[mb].value = varc;

            ctx->mb_cmp[mb].mb = mb;

        }

    } else { 

        int const VAR_6 = ctx->m.VAR_6 >> 1;

        for (VAR_5 = 0; VAR_5 < ctx->m.mb_width; ++VAR_5) {

            uint16_t *pix = (uint16_t*)ctx->thread[0]->src[0] + ((VAR_4 << 4) * VAR_6) + (VAR_5 << 4);

            unsigned mb  = VAR_4 * ctx->m.mb_width + VAR_5;

            int sum = 0;

            int sqsum = 0;

            int mean, sqmean;

            

            for (int i = 0; i < 16; ++i) {

                for (int j = 0; j < 16; ++j) {

                    

                    int const sample = (unsigned)pix[j] >> 6;

                    sum += sample;

                    sqsum += sample * sample;

                    

                }

                pix += VAR_6;

            }

            mean = sum >> 8; 

            sqmean = sqsum >> 8;

            ctx->mb_cmp[mb].value = sqmean - mean * mean;

            ctx->mb_cmp[mb].mb = mb;

        }

    }

    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int VAR_2, int VAR_3)
{ DNXHDEncContext *ctx = VAR_0->priv_data; int VAR_4 = VAR_2, VAR_5; ctx = ctx->thread[VAR_3]; if (ctx->cid_table->bit_depth == 8) { uint8_t *pix = ctx->thread[0]->src[0] + ((VAR_4<<4) * ctx->m.VAR_6); for (VAR_5 = 0; VAR_5 < ctx->m.mb_width; ++VAR_5, pix += 16) { unsigned mb  = VAR_4 * ctx->m.mb_width + VAR_5; int sum = ctx->m.dsp.pix_sum(pix, ctx->m.VAR_6); int varc = (ctx->m.dsp.pix_norm1(pix, ctx->m.VAR_6) - (((unsigned)(sum*sum))>>8)+128)>>8; ctx->mb_cmp[mb].value = varc; ctx->mb_cmp[mb].mb = mb; } } else { int const VAR_6 = ctx->m.VAR_6 >> 1; for (VAR_5 = 0; VAR_5 < ctx->m.mb_width; ++VAR_5) { uint16_t *pix = (uint16_t*)ctx->thread[0]->src[0] + ((VAR_4 << 4) * VAR_6) + (VAR_5 << 4); unsigned mb  = VAR_4 * ctx->m.mb_width + VAR_5; int sum = 0; int sqsum = 0; int mean, sqmean; for (int i = 0; i < 16; ++i) { for (int j = 0; j < 16; ++j) { int const sample = (unsigned)pix[j] >> 6; sum += sample; sqsum += sample * sample; } pix += VAR_6; } mean = sum >> 8; sqmean = sqsum >> 8; ctx->mb_cmp[mb].value = sqmean - mean * mean; ctx->mb_cmp[mb].mb = mb; } } return 0; }