static inline void dv_encode_video_segment(DVVideoContext *s,
                                           uint8_t *dif,
                                           const uint16_t *mb_pos_ptr)
{
    int mb_index, i, j, v;
    int mb_x, mb_y, c_offset, linesize;
    uint8_t*  y_ptr;
    uint8_t*  data;
    uint8_t*  ptr;
    int       do_edge_wrap;
    DECLARE_ALIGNED_16(DCTELEM, block[64]);
    EncBlockInfo  enc_blks[5*6];
    PutBitContext pbs[5*6];
    PutBitContext* pb;
    EncBlockInfo* enc_blk;
    int       vs_bit_size = 0;
    int       qnos[5];
    assert((((int)block) & 15) == 0);
    enc_blk = &enc_blks[0];
    pb = &pbs[0];
    for(mb_index = 0; mb_index < 5; mb_index++) {
        v = *mb_pos_ptr++;
        mb_x = v & 0xff;
        mb_y = v >> 8;
        if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
            y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 4);
        } else {
            y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
        }
        if (s->sys->pix_fmt == PIX_FMT_YUV420P) {
            c_offset = (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
        } else {
            c_offset = ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8));
        }
        do_edge_wrap = 0;
        qnos[mb_index] = 15;
        ptr = dif + mb_index*80 + 4;
        for(j = 0;j < 6; j++) {
            int dummy = 0;
            if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
                if (j == 0 || j == 2) {
                    data = y_ptr + ((j>>1) * 8);
                    linesize = s->picture.linesize[0];
                } else if (j > 3) {
                    data = s->picture.data[6 - j] + c_offset;
                    linesize = s->picture.linesize[6 - j];
                } else {
                    data = 0;
                    linesize = 0;
                    dummy = 1;
                }
            } else {
                if (j < 4) {
                    if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
                        data = y_ptr + (j * 8);
                    } else {
                        data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
                    }
                    linesize = s->picture.linesize[0];
                } else {
                    data = s->picture.data[6 - j] + c_offset;
                    linesize = s->picture.linesize[6 - j];
                    if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
                        do_edge_wrap = 1;
                }
            }
            if (do_edge_wrap) {
                uint8_t* d;
                DCTELEM *b = block;
                for (i=0;i<8;i++) {
                   d = data + 8 * linesize;
                   b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
                   b[4] =    d[0]; b[5] =    d[1]; b[6] =    d[2]; b[7] =    d[3];
                   data += linesize;
                   b += 8;
                }
            } else {
                if (!dummy)
                    s->get_pixels(block, data, linesize);
            }
            if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT)
                enc_blk->dct_mode = dv_guess_dct_mode(block);
            else
                enc_blk->dct_mode = 0;
            enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
            enc_blk->partial_bit_count = 0;
            enc_blk->partial_bit_buffer = 0;
            enc_blk->cur_ac = 0;
            if (dummy) {
                memset(block, 0, sizeof(block));
            } else {
                s->fdct[enc_blk->dct_mode](block);
            }
            dv_set_class_number(block, enc_blk,
                                enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
                                enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
                                j/4);
            init_put_bits(pb, ptr, block_sizes[j]/8);
            put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));
            put_bits(pb, 1, enc_blk->dct_mode);
            put_bits(pb, 2, enc_blk->cno);
            vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
                           enc_blk->bit_size[2] + enc_blk->bit_size[3];
            ++enc_blk;
            ++pb;
            ptr += block_sizes[j]/8;
        }
    }
    if (vs_total_ac_bits < vs_bit_size)
        dv_guess_qnos(&enc_blks[0], &qnos[0]);
    for (i=0; i<5; i++) {
       dif[i*80 + 3] = qnos[i];
    }
    for (j=0; j<5*6; j++)
       dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);
    for (j=0; j<5*6; j+=6) {
        pb= &pbs[j];
        for (i=0; i<6; i++) {
            if (enc_blks[i+j].partial_bit_count)
                pb=dv_encode_ac(&enc_blks[i+j], pb, &pbs[j+6]);
        }
    }
    pb= &pbs[0];
    for (j=0; j<5*6; j++) {
       if (enc_blks[j].partial_bit_count)
           pb=dv_encode_ac(&enc_blks[j], pb, &pbs[6*5]);
       if (enc_blks[j].partial_bit_count)
            av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\n");
    }
    for (j=0; j<5*6; j++)
       flush_put_bits(&pbs[j]);
}
['static inline void dv_encode_video_segment(DVVideoContext *s,\n                                           uint8_t *dif,\n                                           const uint16_t *mb_pos_ptr)\n{\n    int mb_index, i, j, v;\n    int mb_x, mb_y, c_offset, linesize;\n    uint8_t*  y_ptr;\n    uint8_t*  data;\n    uint8_t*  ptr;\n    int       do_edge_wrap;\n    DECLARE_ALIGNED_16(DCTELEM, block[64]);\n    EncBlockInfo  enc_blks[5*6];\n    PutBitContext pbs[5*6];\n    PutBitContext* pb;\n    EncBlockInfo* enc_blk;\n    int       vs_bit_size = 0;\n    int       qnos[5];\n    assert((((int)block) & 15) == 0);\n    enc_blk = &enc_blks[0];\n    pb = &pbs[0];\n    for(mb_index = 0; mb_index < 5; mb_index++) {\n        v = *mb_pos_ptr++;\n        mb_x = v & 0xff;\n        mb_y = v >> 8;\n        if (s->sys->pix_fmt == PIX_FMT_YUV422P) {\n            y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 4);\n        } else {\n            y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);\n        }\n        if (s->sys->pix_fmt == PIX_FMT_YUV420P) {\n            c_offset = (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));\n        } else {\n            c_offset = ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8));\n        }\n        do_edge_wrap = 0;\n        qnos[mb_index] = 15;\n        ptr = dif + mb_index*80 + 4;\n        for(j = 0;j < 6; j++) {\n            int dummy = 0;\n            if (s->sys->pix_fmt == PIX_FMT_YUV422P) {\n                if (j == 0 || j == 2) {\n                    data = y_ptr + ((j>>1) * 8);\n                    linesize = s->picture.linesize[0];\n                } else if (j > 3) {\n                    data = s->picture.data[6 - j] + c_offset;\n                    linesize = s->picture.linesize[6 - j];\n                } else {\n                    data = 0;\n                    linesize = 0;\n                    dummy = 1;\n                }\n            } else {\n                if (j < 4) {\n                    if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {\n                        data = y_ptr + (j * 8);\n                    } else {\n                        data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);\n                    }\n                    linesize = s->picture.linesize[0];\n                } else {\n                    data = s->picture.data[6 - j] + c_offset;\n                    linesize = s->picture.linesize[6 - j];\n                    if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))\n                        do_edge_wrap = 1;\n                }\n            }\n            if (do_edge_wrap) {\n                uint8_t* d;\n                DCTELEM *b = block;\n                for (i=0;i<8;i++) {\n                   d = data + 8 * linesize;\n                   b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];\n                   b[4] =    d[0]; b[5] =    d[1]; b[6] =    d[2]; b[7] =    d[3];\n                   data += linesize;\n                   b += 8;\n                }\n            } else {\n                if (!dummy)\n                    s->get_pixels(block, data, linesize);\n            }\n            if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT)\n                enc_blk->dct_mode = dv_guess_dct_mode(block);\n            else\n                enc_blk->dct_mode = 0;\n            enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;\n            enc_blk->partial_bit_count = 0;\n            enc_blk->partial_bit_buffer = 0;\n            enc_blk->cur_ac = 0;\n            if (dummy) {\n                memset(block, 0, sizeof(block));\n            } else {\n                s->fdct[enc_blk->dct_mode](block);\n            }\n            dv_set_class_number(block, enc_blk,\n                                enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,\n                                enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,\n                                j/4);\n            init_put_bits(pb, ptr, block_sizes[j]/8);\n            put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));\n            put_bits(pb, 1, enc_blk->dct_mode);\n            put_bits(pb, 2, enc_blk->cno);\n            vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +\n                           enc_blk->bit_size[2] + enc_blk->bit_size[3];\n            ++enc_blk;\n            ++pb;\n            ptr += block_sizes[j]/8;\n        }\n    }\n    if (vs_total_ac_bits < vs_bit_size)\n        dv_guess_qnos(&enc_blks[0], &qnos[0]);\n    for (i=0; i<5; i++) {\n       dif[i*80 + 3] = qnos[i];\n    }\n    for (j=0; j<5*6; j++)\n       dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);\n    for (j=0; j<5*6; j+=6) {\n        pb= &pbs[j];\n        for (i=0; i<6; i++) {\n            if (enc_blks[i+j].partial_bit_count)\n                pb=dv_encode_ac(&enc_blks[i+j], pb, &pbs[j+6]);\n        }\n    }\n    pb= &pbs[0];\n    for (j=0; j<5*6; j++) {\n       if (enc_blks[j].partial_bit_count)\n           pb=dv_encode_ac(&enc_blks[j], pb, &pbs[6*5]);\n       if (enc_blks[j].partial_bit_count)\n            av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\\n");\n    }\n    for (j=0; j<5*6; j++)\n       flush_put_bits(&pbs[j]);\n}']