static void fdt_add_gic_node(VirtBoardInfo *vbi)

{

    vbi->gic_phandle = qemu_fdt_alloc_phandle(vbi->fdt);

    qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", vbi->gic_phandle);

    qemu_fdt_add_subnode(vbi->fdt, "/intc");

    /* 'cortex-a15-gic' means 'GIC v2' */

    qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",

                            "arm,cortex-a15-gic");

    qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3);

    qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0);

    qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",

                                     2, vbi->memmap[VIRT_GIC_DIST].base,

                                     2, vbi->memmap[VIRT_GIC_DIST].size,

                                     2, vbi->memmap[VIRT_GIC_CPU].base,

                                     2, vbi->memmap[VIRT_GIC_CPU].size);

    qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#address-cells", 0x2);

    qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#size-cells", 0x2);

    qemu_fdt_setprop(vbi->fdt, "/intc", "ranges", NULL, 0);

    qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", vbi->gic_phandle);

}


static int sad_hpel_motion_search(MpegEncContext * s,

                                  int *mx_ptr, int *my_ptr, int dmin,

                                  int src_index, int ref_index,

                                  int size, int h)

{

    MotionEstContext * const c= &s->me;

    const int penalty_factor= c->sub_penalty_factor;

    int mx, my, dminh;

    uint8_t *pix, *ptr;

    int stride= c->stride;

    LOAD_COMMON

    av_assert2(c->sub_flags == 0);

    if(c->skip){

        *mx_ptr = 0;

        *my_ptr = 0;

        return dmin;

    }

    pix = c->src[src_index][0];

    mx = *mx_ptr;

    my = *my_ptr;

    ptr = c->ref[ref_index][0] + (my * stride) + mx;

    dminh = dmin;

    if (mx > xmin && mx < xmax &&

        my > ymin && my < ymax) {

        int dx=0, dy=0;

        int d, pen_x, pen_y;

        const int index= (my<<ME_MAP_SHIFT) + mx;

        const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];

        const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)];

        const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)];

        const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];

        mx<<=1;

        my<<=1;

        pen_x= pred_x + mx;

        pen_y= pred_y + my;

        ptr-= stride;

        if(t<=b){

            CHECK_SAD_HALF_MV(y2 , 0, -1)

            if(l<=r){

                CHECK_SAD_HALF_MV(xy2, -1, -1)

                if(t+r<=b+l){

                    CHECK_SAD_HALF_MV(xy2, +1, -1)

                    ptr+= stride;

                }else{

                    ptr+= stride;

                    CHECK_SAD_HALF_MV(xy2, -1, +1)

                }

                CHECK_SAD_HALF_MV(x2 , -1,  0)

            }else{

                CHECK_SAD_HALF_MV(xy2, +1, -1)

                if(t+l<=b+r){

                    CHECK_SAD_HALF_MV(xy2, -1, -1)

                    ptr+= stride;

                }else{

                    ptr+= stride;

                    CHECK_SAD_HALF_MV(xy2, +1, +1)

                }

                CHECK_SAD_HALF_MV(x2 , +1,  0)

            }

        }else{

            if(l<=r){

                if(t+l<=b+r){

                    CHECK_SAD_HALF_MV(xy2, -1, -1)

                    ptr+= stride;

                }else{

                    ptr+= stride;

                    CHECK_SAD_HALF_MV(xy2, +1, +1)

                }

                CHECK_SAD_HALF_MV(x2 , -1,  0)

                CHECK_SAD_HALF_MV(xy2, -1, +1)

            }else{

                if(t+r<=b+l){

                    CHECK_SAD_HALF_MV(xy2, +1, -1)

                    ptr+= stride;

                }else{

                    ptr+= stride;

                    CHECK_SAD_HALF_MV(xy2, -1, +1)

                }

                CHECK_SAD_HALF_MV(x2 , +1,  0)

                CHECK_SAD_HALF_MV(xy2, +1, +1)

            }

            CHECK_SAD_HALF_MV(y2 ,  0, +1)

        }

        mx+=dx;

        my+=dy;

    }else{

        mx<<=1;

        my<<=1;

    }

    *mx_ptr = mx;

    *my_ptr = my;

    return dminh;

}


int kvm_arch_on_sigbus_vcpu(CPUState *env, int code, void *addr)

{

#ifdef KVM_CAP_MCE

    void *vaddr;

    ram_addr_t ram_addr;

    target_phys_addr_t paddr;

    if ((env->mcg_cap & MCG_SER_P) && addr

        && (code == BUS_MCEERR_AR

            || code == BUS_MCEERR_AO)) {

        vaddr = (void *)addr;

        if (qemu_ram_addr_from_host(vaddr, &ram_addr) ||

            !kvm_physical_memory_addr_from_ram(env->kvm_state, ram_addr, &paddr)) {

            fprintf(stderr, "Hardware memory error for memory used by "

                    "QEMU itself instead of guest system!\n");

            /* Hope we are lucky for AO MCE */

            if (code == BUS_MCEERR_AO) {

                return 0;

            } else {

                hardware_memory_error();

            }

        }

        if (code == BUS_MCEERR_AR) {

            /* Fake an Intel architectural Data Load SRAR UCR */

            kvm_mce_inj_srar_dataload(env, paddr);

        } else {

            /*

             * If there is an MCE excpetion being processed, ignore

             * this SRAO MCE

             */

            if (!kvm_mce_in_progress(env)) {

                /* Fake an Intel architectural Memory scrubbing UCR */

                kvm_mce_inj_srao_memscrub(env, paddr);

            }

        }

    } else

#endif /* KVM_CAP_MCE */

    {

        if (code == BUS_MCEERR_AO) {

            return 0;

        } else if (code == BUS_MCEERR_AR) {

            hardware_memory_error();

        } else {

            return 1;

        }

    }

    return 0;

}


void ff_flac_compute_autocorr(const int32_t *data, int len, int lag,

                              double *autoc)

{

    int i, j;

    double tmp[len + lag + 1];

    double *data1= tmp + lag;

    apply_welch_window(data, len, data1);

    for(j=0; j<lag; j++)

        data1[j-lag]= 0.0;

    data1[len] = 0.0;

    for(j=0; j<lag; j+=2){

        double sum0 = 1.0, sum1 = 1.0;

        for(i=0; i<len; i++){

            sum0 += data1[i] * data1[i-j];

            sum1 += data1[i] * data1[i-j-1];

        }

        autoc[j  ] = sum0;

        autoc[j+1] = sum1;

    }

    if(j==lag){

        double sum = 1.0;

        for(i=0; i<len; i+=2){

            sum += data1[i  ] * data1[i-j  ]

                 + data1[i+1] * data1[i-j+1];

        }

        autoc[j] = sum;

    }

}


static unsigned int rms(const int *data)

{

    int x;

    unsigned int res = 0x10000;

    int b = 0;

    for (x=0; x<10; x++) {

        res = (((0x1000000 - (*data) * (*data)) >> 12) * res) >> 12;

        if (res == 0)

            return 0;

        while (res <= 0x3fff) {

            b++;

            res <<= 2;

        }

        data++;

    }

    if (res > 0)

        res = t_sqrt(res);

    res >>= (b + 10);

    return res;

}


void remove_migration_state_change_notifier(Notifier *notify)

{

    notifier_remove(notify);

}


static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
{
    const int data_ptr = 2 + AV_RL16(&buf[0]);
    int d, r, y;
    d = data_ptr; r = 2; y = 0;
    while(r < data_ptr) {
        int i, j;
        int length = buf[r] & 0x7f;
        int x = buf[r+1] + ((buf[r] & 0x80) << 1);
        r += 2;
        if (length==0) {
            y += x;
            continue;
        }
        for(i=0; i<length; i++) {
            for(j=0; j<8; j++) {
                int replace = (buf[r+i] >> (7-j)) & 1;
                if (replace) {
                    int color = buf[d];
                    s->frame.data[0][y*s->frame.linesize[0] + x] = color;
                    if (half_horiz)
                        s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
                    if (half_vert) {
                        s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
                        if (half_horiz)
                            s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
                    }
                    d++;
                }
                x += 1 + half_horiz;
            }
        }
        r += length;
        y += 1 + half_vert;
    }
}


static void test_tco_ticks_counter(void)

{

    TestData d;

    uint16_t ticks = TCO_SECS_TO_TICKS(8);

    uint16_t rld;

    d.args = NULL;

    d.noreboot = true;

    test_init(&d);

    stop_tco(&d);

    clear_tco_status(&d);

    reset_on_second_timeout(false);

    set_tco_timeout(&d, ticks);

    load_tco(&d);

    start_tco(&d);

    do {

        rld = qpci_io_readw(d.dev, d.tco_io_bar, TCO_RLD) & TCO_RLD_MASK;

        g_assert_cmpint(rld, ==, ticks);

        clock_step(TCO_TICK_NSEC);

        ticks--;

    } while (!(qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS) & TCO_TIMEOUT));

    stop_tco(&d);

    qtest_end();

}


static void decode_mb(MpegEncContext *s, int ref)

{

    s->dest[0] = s->current_picture.f.data[0] + (s->mb_y *  16                       * s->linesize)   + s->mb_x *  16;

    s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);

    s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);

    ff_init_block_index(s);

    ff_update_block_index(s);

    s->dest[1] += (16 >> s->chroma_x_shift) - 8;

    s->dest[2] += (16 >> s->chroma_x_shift) - 8;

    if (CONFIG_H264_DECODER && s->codec_id == AV_CODEC_ID_H264) {

        H264Context *h = (void*)s;

        h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;

        memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));

        av_assert1(ref >= 0);

        /* FIXME: It is possible albeit uncommon that slice references

         * differ between slices. We take the easy approach and ignore

         * it for now. If this turns out to have any relevance in

         * practice then correct remapping should be added. */

        if (ref >= h->ref_count[0])

            ref = 0;

        if (!h->ref_list[0][ref].f.data[0]) {

            av_log(s->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");

            ref = 0;

        fill_rectangle(&s->current_picture.f.ref_index[0][4 * h->mb_xy],

                       2, 2, 2, ref, 1);

        fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);

        fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,

                       pack16to32(s->mv[0][0][0], s->mv[0][0][1]), 4);

        h->mb_mbaff =

        h->mb_field_decoding_flag = 0;

        ff_h264_hl_decode_mb(h);

    } else {

        assert(ref == 0);

        ff_MPV_decode_mb(s, s->block);


static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,

        QCowL2Meta *m)

{

    BDRVQcowState *s = bs->opaque;

    int i, j = 0, l2_index, ret;

    uint64_t *old_cluster, start_sect, l2_offset, *l2_table;

    if (m->nb_clusters == 0)

        return 0;

    old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));

    /* copy content of unmodified sectors */

    start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;

    if (m->n_start) {

        ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);

        if (ret < 0)

            goto err;

    }

    if (m->nb_available & (s->cluster_sectors - 1)) {

        uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);

        ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),

                m->nb_available - end, s->cluster_sectors);

        if (ret < 0)

            goto err;

    }

    ret = -EIO;

    /* update L2 table */

    if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))

        goto err;

    for (i = 0; i < m->nb_clusters; i++) {

        if(l2_table[l2_index + i] != 0)

            old_cluster[j++] = l2_table[l2_index + i];

        l2_table[l2_index + i] = cpu_to_be64((cluster_offset +

                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);

     }

    if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t),

                l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) !=

            m->nb_clusters * sizeof(uint64_t))

        goto err;

    for (i = 0; i < j; i++)

        free_any_clusters(bs, old_cluster[i], 1);

    ret = 0;

err:

    qemu_free(old_cluster);

    return ret;

 }


int av_set_options_string(void *ctx, const char *opts,

                          const char *key_val_sep, const char *pairs_sep)

{

    int ret, count = 0;

    while (*opts) {

        if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)

            return ret;

        count++;

        if (*opts)

            opts++;

    }

    return count;

}


static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)

{

    char *buffer;

    int ret = -1;

    char *path = fs_path->data;

    if (fs_ctx->export_flags & V9FS_SM_MAPPED) {

        buffer = rpath(fs_ctx, path);

        ret = local_set_xattr(buffer, credp);

        g_free(buffer);

    } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {

        return local_set_mapped_file_attr(fs_ctx, path, credp);

    } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||

               (fs_ctx->export_flags & V9FS_SM_NONE)) {

        buffer = rpath(fs_ctx, path);

        ret = chmod(buffer, credp->fc_mode);

        g_free(buffer);

    }

    return ret;

}


static int dpcm_decode_frame(AVCodecContext *avctx,

                             void *data, int *data_size,

                             uint8_t *buf, int buf_size)

{

    DPCMContext *s = avctx->priv_data;

    int in, out = 0;

    int predictor[2];

    int channel_number = 0;

    short *output_samples = data;

    int shift[2];

    unsigned char byte;

    short diff;

    if (!buf_size)

        return 0;

    // almost every DPCM variant expands one byte of data into two

    if(*data_size/2 < buf_size)

    switch(avctx->codec->id) {

    case CODEC_ID_ROQ_DPCM:

        if (s->channels == 1)

            predictor[0] = AV_RL16(&buf[6]);

        else {

            predictor[0] = buf[7] << 8;

            predictor[1] = buf[6] << 8;

        }

        SE_16BIT(predictor[0]);

        SE_16BIT(predictor[1]);

        /* decode the samples */

        for (in = 8, out = 0; in < buf_size; in++, out++) {

            predictor[channel_number] += s->roq_square_array[buf[in]];

            predictor[channel_number] = av_clip_int16(predictor[channel_number]);

            output_samples[out] = predictor[channel_number];

            /* toggle channel */

            channel_number ^= s->channels - 1;

        }

        break;

    case CODEC_ID_INTERPLAY_DPCM:

        in = 6;  /* skip over the stream mask and stream length */

        predictor[0] = AV_RL16(&buf[in]);

        in += 2;

        SE_16BIT(predictor[0])

        output_samples[out++] = predictor[0];

        if (s->channels == 2) {

            predictor[1] = AV_RL16(&buf[in]);

            in += 2;

            SE_16BIT(predictor[1])

            output_samples[out++] = predictor[1];

        }

        while (in < buf_size) {

            predictor[channel_number] += interplay_delta_table[buf[in++]];

            predictor[channel_number] = av_clip_int16(predictor[channel_number]);

            output_samples[out++] = predictor[channel_number];

            /* toggle channel */

            channel_number ^= s->channels - 1;

        }

        break;

    case CODEC_ID_XAN_DPCM:

        in = 0;

        shift[0] = shift[1] = 4;

        predictor[0] = AV_RL16(&buf[in]);

        in += 2;

        SE_16BIT(predictor[0]);

        if (s->channels == 2) {

            predictor[1] = AV_RL16(&buf[in]);

            in += 2;

            SE_16BIT(predictor[1]);

        }

        while (in < buf_size) {

            byte = buf[in++];

            diff = (byte & 0xFC) << 8;

            if ((byte & 0x03) == 3)

                shift[channel_number]++;

            else

                shift[channel_number] -= (2 * (byte & 3));

            /* saturate the shifter to a lower limit of 0 */

            if (shift[channel_number] < 0)

                shift[channel_number] = 0;

            diff >>= shift[channel_number];

            predictor[channel_number] += diff;

            predictor[channel_number] = av_clip_int16(predictor[channel_number]);

            output_samples[out++] = predictor[channel_number];

            /* toggle channel */

            channel_number ^= s->channels - 1;

        }

        break;

    case CODEC_ID_SOL_DPCM:

        in = 0;

        if (avctx->codec_tag != 3) {

            while (in < buf_size) {

                int n1, n2;

                n1 = (buf[in] >> 4) & 0xF;

                n2 = buf[in++] & 0xF;

                s->sample[0] += s->sol_table[n1];

                 if (s->sample[0] < 0) s->sample[0] = 0;

                if (s->sample[0] > 255) s->sample[0] = 255;

                output_samples[out++] = (s->sample[0] - 128) << 8;

                s->sample[s->channels - 1] += s->sol_table[n2];

                if (s->sample[s->channels - 1] < 0) s->sample[s->channels - 1] = 0;

                if (s->sample[s->channels - 1] > 255) s->sample[s->channels - 1] = 255;

                output_samples[out++] = (s->sample[s->channels - 1] - 128) << 8;

            }

        } else {

            while (in < buf_size) {

                int n;

                n = buf[in++];

                if (n & 0x80) s->sample[channel_number] -= s->sol_table[n & 0x7F];

                else s->sample[channel_number] += s->sol_table[n & 0x7F];

                s->sample[channel_number] = av_clip_int16(s->sample[channel_number]);

                output_samples[out++] = s->sample[channel_number];

                /* toggle channel */

                channel_number ^= s->channels - 1;

            }

        }

        break;

    }

    *data_size = out * sizeof(short);

    return buf_size;

}


static void kvm_reset_vcpu(void *opaque)

{

    CPUState *env = opaque;

    kvm_arch_reset_vcpu(env);

    if (kvm_arch_put_registers(env)) {

        fprintf(stderr, "Fatal: kvm vcpu reset failed\n");

        abort();

    }

}


static int parse_cube(AVFilterContext *ctx, FILE *f)

{

    LUT3DContext *lut3d = ctx->priv;

    char line[MAX_LINE_SIZE];

    float min[3] = {0.0, 0.0, 0.0};

    float max[3] = {1.0, 1.0, 1.0};

    while (fgets(line, sizeof(line), f)) {

        if (!strncmp(line, "LUT_3D_SIZE ", 12)) {

            int i, j, k;

            const int size = strtol(line + 12, NULL, 0);

            if (size > MAX_LEVEL) {

                av_log(ctx, AV_LOG_ERROR, "Too large 3D LUT\n");

                return AVERROR(EINVAL);

            }

            lut3d->lutsize = size;

            for (k = 0; k < size; k++) {

                for (j = 0; j < size; j++) {

                    for (i = 0; i < size; i++) {

                        struct rgbvec *vec = &lut3d->lut[k][j][i];

                        do {

                            NEXT_LINE(0);

                            if (!strncmp(line, "DOMAIN_", 7)) {

                                float *vals = NULL;

                                if      (!strncmp(line + 7, "MIN ", 4)) vals = min;

                                else if (!strncmp(line + 7, "MAX ", 4)) vals = max;

                                if (!vals)

                                    return AVERROR_INVALIDDATA;

                                sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);

                                av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",

                                       min[0], min[1], min[2], max[0], max[1], max[2]);

                                continue;

                            }

                        } while (skip_line(line));

                        if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)

                            return AVERROR_INVALIDDATA;

                        vec->r *= max[0] - min[0];

                        vec->g *= max[1] - min[1];

                        vec->b *= max[2] - min[2];

                    }

                }

            }

            break;

        }

    }

    return 0;

}


static void csrhci_in_packet(struct csrhci_s *s, uint8_t *pkt)

{

    uint8_t *rpkt;

    int opc;

    switch (*pkt ++) {

    case H4_CMD_PKT:

        opc = le16_to_cpu(((struct hci_command_hdr *) pkt)->opcode);

        if (cmd_opcode_ogf(opc) == OGF_VENDOR_CMD) {

            csrhci_in_packet_vendor(s, cmd_opcode_ocf(opc),

                            pkt + sizeof(struct hci_command_hdr),

                            s->in_len - sizeof(struct hci_command_hdr) - 1);

            return;

        }

        /* TODO: if the command is OCF_READ_LOCAL_COMMANDS or the likes,

         * we need to send it to the HCI layer and then add our supported

         * commands to the returned mask (such as OGF_VENDOR_CMD).  With

         * bt-hci.c we could just have hooks for this kind of commands but

         * we can't with bt-host.c.  */

        s->hci->cmd_send(s->hci, pkt, s->in_len - 1);

        break;

    case H4_EVT_PKT:

        goto bad_pkt;

    case H4_ACL_PKT:

        s->hci->acl_send(s->hci, pkt, s->in_len - 1);

        break;

    case H4_SCO_PKT:

        s->hci->sco_send(s->hci, pkt, s->in_len - 1);

        break;

    case H4_NEG_PKT:

        if (s->in_hdr != sizeof(csrhci_neg_packet) ||

                        memcmp(pkt - 1, csrhci_neg_packet, s->in_hdr)) {

            fprintf(stderr, "%s: got a bad NEG packet\n", __func__);

            return;

        }

        pkt += 2;

        rpkt = csrhci_out_packet_csr(s, H4_NEG_PKT, 10);

        *rpkt ++ = 0x20;	/* Operational settings negotiation Ok */

        memcpy(rpkt, pkt, 7); rpkt += 7;

        *rpkt ++ = 0xff;

        *rpkt = 0xff;

        break;

    case H4_ALIVE_PKT:

        if (s->in_hdr != 4 || pkt[1] != 0x55 || pkt[2] != 0x00) {

            fprintf(stderr, "%s: got a bad ALIVE packet\n", __func__);

            return;

        }

        rpkt = csrhci_out_packet_csr(s, H4_ALIVE_PKT, 2);

        *rpkt ++ = 0xcc;

        *rpkt = 0x00;

        break;

    default:

    bad_pkt:

        /* TODO: error out */

        fprintf(stderr, "%s: got a bad packet\n", __func__);

        break;

    }

    csrhci_fifo_wake(s);

}


static int client_migrate_info(Monitor *mon, const QDict *qdict,

                               QObject **ret_data)

{

    const char *protocol = qdict_get_str(qdict, "protocol");

    const char *hostname = qdict_get_str(qdict, "hostname");

    const char *subject  = qdict_get_try_str(qdict, "cert-subject");

    int port             = qdict_get_try_int(qdict, "port", -1);

    int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);

    Error *err = NULL;

    int ret;

    if (strcmp(protocol, "spice") == 0) {

        if (!qemu_using_spice(&err)) {

            qerror_report_err(err);

            error_free(err);

            return -1;

        }

        if (port == -1 && tls_port == -1) {

            qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");

            return -1;

        }

        ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);

        if (ret != 0) {

            qerror_report(QERR_UNDEFINED_ERROR);

            return -1;

        }

        return 0;

    }

    qerror_report(QERR_INVALID_PARAMETER, "protocol");

    return -1;

}


void backup_start(BlockDriverState *bs, BlockDriverState *target,

                  int64_t speed,

                  BlockdevOnError on_source_error,

                  BlockdevOnError on_target_error,

                  BlockDriverCompletionFunc *cb, void *opaque,

                  Error **errp)

{

    int64_t len;

    assert(bs);

    assert(target);

    assert(cb);

    if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||

         on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&

        !bdrv_iostatus_is_enabled(bs)) {

        error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");

        return;

    }

    len = bdrv_getlength(bs);

    if (len < 0) {

        error_setg_errno(errp, -len, "unable to get length for '%s'",

                         bdrv_get_device_name(bs));

        return;

    }

    BackupBlockJob *job = block_job_create(&backup_job_type, bs, speed,

                                           cb, opaque, errp);

    if (!job) {

        return;

    }

    job->on_source_error = on_source_error;

    job->on_target_error = on_target_error;

    job->target = target;

    job->common.len = len;

    job->common.co = qemu_coroutine_create(backup_run);

    qemu_coroutine_enter(job->common.co, job);

}


void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename

    const int linesize   = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics

    const int uvlinesize = s->current_picture.f->linesize[1];

    const int mb_size= 4;

    s->block_index[0]= s->b8_stride*(s->mb_y*2    ) - 2 + s->mb_x*2;

    s->block_index[1]= s->b8_stride*(s->mb_y*2    ) - 1 + s->mb_x*2;

    s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;

    s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;

    s->block_index[4]= s->mb_stride*(s->mb_y + 1)                + s->b8_stride*s->mb_height*2 + s->mb_x - 1;

    s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;

    //block_index is not used by mpeg2, so it is not affected by chroma_format

    s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) <<  mb_size);

    s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));

    s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));

    if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))

    {

        if(s->picture_structure==PICT_FRAME){

        s->dest[0] += s->mb_y *   linesize << mb_size;

        s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);

        s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);

        }else{

            s->dest[0] += (s->mb_y>>1) *   linesize << mb_size;

            s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);

            s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);

            assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));

        }

    }

}


int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)

{

    int offset = 0;

    int res = 0;

    qemu_mutex_lock(&s->chr_write_lock);

    while (offset < len) {

        do {

            res = s->chr_write(s, buf + offset, len - offset);

            if (res == -1 && errno == EAGAIN) {

                g_usleep(100);

            }

        } while (res == -1 && errno == EAGAIN);

        if (res <= 0) {

            break;

        }

        offset += res;

    }

    if (offset > 0) {

        qemu_chr_fe_write_log(s, buf, offset);

    }

    qemu_mutex_unlock(&s->chr_write_lock);

    if (res < 0) {

        return res;

    }

    return offset;

}


static GenericList *qmp_output_next_list(Visitor *v, GenericList *tail,

                                         size_t size)

{

    return tail->next;

}


int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,

                          int rw, int access_type)

{

    int ret;

#if 0

    qemu_log("%s\n", __func__);

#endif

    if ((access_type == ACCESS_CODE && msr_ir == 0) ||

        (access_type != ACCESS_CODE && msr_dr == 0)) {

        /* No address translation */

        ret = check_physical(env, ctx, eaddr, rw);

    } else {

        ret = -1;

        switch (env->mmu_model) {

        case POWERPC_MMU_32B:

        case POWERPC_MMU_601:

        case POWERPC_MMU_SOFT_6xx:

        case POWERPC_MMU_SOFT_74xx:

#if defined(TARGET_PPC64)

        case POWERPC_MMU_620:

        case POWERPC_MMU_64B:

#endif

            /* Try to find a BAT */

            if (env->nb_BATs != 0)

                ret = get_bat(env, ctx, eaddr, rw, access_type);

            if (ret < 0) {

                /* We didn't match any BAT entry or don't have BATs */

                ret = get_segment(env, ctx, eaddr, rw, access_type);

            }

            break;

        case POWERPC_MMU_SOFT_4xx:

        case POWERPC_MMU_SOFT_4xx_Z:

            ret = mmu40x_get_physical_address(env, ctx, eaddr,

                                              rw, access_type);

            break;

        case POWERPC_MMU_BOOKE:

            ret = mmubooke_get_physical_address(env, ctx, eaddr,

                                                rw, access_type);

            break;

        case POWERPC_MMU_MPC8xx:

            /* XXX: TODO */

            cpu_abort(env, "MPC8xx MMU model is not implemented\n");

            break;

        case POWERPC_MMU_BOOKE_FSL:

            /* XXX: TODO */

            cpu_abort(env, "BookE FSL MMU model not implemented\n");

            return -1;

        case POWERPC_MMU_REAL:

            cpu_abort(env, "PowerPC in real mode do not do any translation\n");

            return -1;

        default:

            cpu_abort(env, "Unknown or invalid MMU model\n");

            return -1;

        }

    }

#if 0

    qemu_log("%s address " ADDRX " => %d " PADDRX "\n",

                __func__, eaddr, ret, ctx->raddr);

#endif

    return ret;

}


static unsigned int dec_btstq(DisasContext *dc)

{

	TCGv l0;

	dc->op1 = EXTRACT_FIELD(dc->ir, 0, 4);

	DIS(fprintf (logfile, "btstq %u, $r%d\n", dc->op1, dc->op2));

	cris_cc_mask(dc, CC_MASK_NZ);

	l0 = tcg_temp_local_new(TCG_TYPE_TL);

	cris_alu(dc, CC_OP_BTST,

		 l0, cpu_R[dc->op2], tcg_const_tl(dc->op1), 4);

	cris_update_cc_op(dc, CC_OP_FLAGS, 4);

	t_gen_mov_preg_TN(dc, PR_CCS, l0);

	dc->flags_uptodate = 1;

	tcg_temp_free(l0);

	return 2;

}


static void tcg_out_brcond2(TCGContext *s, const TCGArg *args,

                            const int *const_args, int small)

{

    int label_next;

    label_next = gen_new_label();

    switch(args[4]) {

    case TCG_COND_EQ:

        tcg_out_brcond32(s, TCG_COND_NE, args[0], args[2], const_args[2],

                         label_next, 1);

        tcg_out_brcond32(s, TCG_COND_EQ, args[1], args[3], const_args[3],

                         args[5], small);

        break;

    case TCG_COND_NE:

        tcg_out_brcond32(s, TCG_COND_NE, args[0], args[2], const_args[2],

                         args[5], small);

        tcg_out_brcond32(s, TCG_COND_NE, args[1], args[3], const_args[3],

                         args[5], small);

        break;

    case TCG_COND_LT:

        tcg_out_brcond32(s, TCG_COND_LT, args[1], args[3], const_args[3],

                         args[5], small);

        tcg_out_jxx(s, JCC_JNE, label_next, 1);

        tcg_out_brcond32(s, TCG_COND_LTU, args[0], args[2], const_args[2],

                         args[5], small);

        break;

    case TCG_COND_LE:

        tcg_out_brcond32(s, TCG_COND_LT, args[1], args[3], const_args[3],

                         args[5], small);

        tcg_out_jxx(s, JCC_JNE, label_next, 1);

        tcg_out_brcond32(s, TCG_COND_LEU, args[0], args[2], const_args[2],

                         args[5], small);

        break;

    case TCG_COND_GT:

        tcg_out_brcond32(s, TCG_COND_GT, args[1], args[3], const_args[3],

                         args[5], small);

        tcg_out_jxx(s, JCC_JNE, label_next, 1);

        tcg_out_brcond32(s, TCG_COND_GTU, args[0], args[2], const_args[2],

                         args[5], small);

        break;

    case TCG_COND_GE:

        tcg_out_brcond32(s, TCG_COND_GT, args[1], args[3], const_args[3],

                         args[5], small);

        tcg_out_jxx(s, JCC_JNE, label_next, 1);

        tcg_out_brcond32(s, TCG_COND_GEU, args[0], args[2], const_args[2],

                         args[5], small);

        break;

    case TCG_COND_LTU:

        tcg_out_brcond32(s, TCG_COND_LTU, args[1], args[3], const_args[3],

                         args[5], small);

        tcg_out_jxx(s, JCC_JNE, label_next, 1);

        tcg_out_brcond32(s, TCG_COND_LTU, args[0], args[2], const_args[2],

                         args[5], small);

        break;

    case TCG_COND_LEU:

        tcg_out_brcond32(s, TCG_COND_LTU, args[1], args[3], const_args[3],

                         args[5], small);

        tcg_out_jxx(s, JCC_JNE, label_next, 1);

        tcg_out_brcond32(s, TCG_COND_LEU, args[0], args[2], const_args[2],

                         args[5], small);

        break;

    case TCG_COND_GTU:

        tcg_out_brcond32(s, TCG_COND_GTU, args[1], args[3], const_args[3],

                         args[5], small);

        tcg_out_jxx(s, JCC_JNE, label_next, 1);

        tcg_out_brcond32(s, TCG_COND_GTU, args[0], args[2], const_args[2],

                         args[5], small);

        break;

    case TCG_COND_GEU:

        tcg_out_brcond32(s, TCG_COND_GTU, args[1], args[3], const_args[3],

                         args[5], small);

        tcg_out_jxx(s, JCC_JNE, label_next, 1);

        tcg_out_brcond32(s, TCG_COND_GEU, args[0], args[2], const_args[2],

                         args[5], small);

        break;

    default:

        tcg_abort();

    }

    tcg_out_label(s, label_next, s->code_ptr);

}


int qemu_savevm_state_begin(QEMUFile *f)

{

    SaveStateEntry *se;

    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);

    qemu_put_be32(f, QEMU_VM_FILE_VERSION);

    TAILQ_FOREACH(se, &savevm_handlers, entry) {

        int len;

        if (se->save_live_state == NULL)

            continue;

        /* Section type */

        qemu_put_byte(f, QEMU_VM_SECTION_START);

        qemu_put_be32(f, se->section_id);

        /* ID string */

        len = strlen(se->idstr);

        qemu_put_byte(f, len);

        qemu_put_buffer(f, (uint8_t *)se->idstr, len);

        qemu_put_be32(f, se->instance_id);

        qemu_put_be32(f, se->version_id);

        se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);

    }

    if (qemu_file_has_error(f))

        return -EIO;

    return 0;

}


static void gen_jumpi(DisasContext *dc, uint32_t dest, int slot)

{

    TCGv_i32 tmp = tcg_const_i32(dest);

    if (((dc->pc ^ dest) & TARGET_PAGE_MASK) != 0) {

        slot = -1;

    }

    gen_jump_slot(dc, tmp, slot);

    tcg_temp_free(tmp);

}


int qed_read_l1_table_sync(BDRVQEDState *s)

{

    int ret = -EINPROGRESS;

    async_context_push();

    qed_read_table(s, s->header.l1_table_offset,

                   s->l1_table, qed_sync_cb, &ret);

    while (ret == -EINPROGRESS) {

        qemu_aio_wait();

    }

    async_context_pop();

    return ret;

}


static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)

{

    int ret = 0, i;

    int repeating = 0;

    int eof_reached = 0;

    AVPacket avpkt;

    if (!ist->saw_first_ts) {

        ist->dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;

        ist->pts = 0;

        if (pkt && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {

            ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);

            ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong

        }

        ist->saw_first_ts = 1;

    }

    if (ist->next_dts == AV_NOPTS_VALUE)

        ist->next_dts = ist->dts;

    if (ist->next_pts == AV_NOPTS_VALUE)

        ist->next_pts = ist->pts;

    if (!pkt) {

        /* EOF handling */

        av_init_packet(&avpkt);

        avpkt.data = NULL;

        avpkt.size = 0;

    } else {

        avpkt = *pkt;

    }

    if (pkt && pkt->dts != AV_NOPTS_VALUE) {

        ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);

        if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)

            ist->next_pts = ist->pts = ist->dts;

    }

    // while we have more to decode or while the decoder did output something on EOF

    while (ist->decoding_needed) {

        int duration = 0;

        int got_output = 0;

        ist->pts = ist->next_pts;

        ist->dts = ist->next_dts;

        switch (ist->dec_ctx->codec_type) {

        case AVMEDIA_TYPE_AUDIO:

            ret = decode_audio    (ist, repeating ? NULL : &avpkt, &got_output);

            break;

        case AVMEDIA_TYPE_VIDEO:

            ret = decode_video    (ist, repeating ? NULL : &avpkt, &got_output, !pkt);

            if (!repeating || !pkt || got_output) {

                if (pkt && pkt->duration) {

                    duration = av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);

                } else if(ist->dec_ctx->framerate.num != 0 && ist->dec_ctx->framerate.den != 0) {

                    int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict+1 : ist->dec_ctx->ticks_per_frame;

                    duration = ((int64_t)AV_TIME_BASE *

                                    ist->dec_ctx->framerate.den * ticks) /

                                    ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;

                }

                if(ist->dts != AV_NOPTS_VALUE && duration) {

                    ist->next_dts += duration;

                }else

                    ist->next_dts = AV_NOPTS_VALUE;

            }

            if (got_output)

                ist->next_pts += duration; //FIXME the duration is not correct in some cases

            break;

        case AVMEDIA_TYPE_SUBTITLE:

            if (repeating)

                break;

            ret = transcode_subtitles(ist, &avpkt, &got_output);

            if (!pkt && ret >= 0)

                ret = AVERROR_EOF;

            break;

        default:

            return -1;

        }

        if (ret == AVERROR_EOF) {

            eof_reached = 1;

            break;

        }

        if (ret < 0) {

            av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",

                   ist->file_index, ist->st->index, av_err2str(ret));

            if (exit_on_error)

                exit_program(1);

            // Decoding might not terminate if we're draining the decoder, and

            // the decoder keeps returning an error.

            // This should probably be considered a libavcodec issue.

            // Sample: fate-vsynth1-dnxhd-720p-hr-lb

            if (!pkt)

                eof_reached = 1;

            break;

        }

        if (got_output)

            ist->got_output = 1;

        if (!got_output)

            break;

        // During draining, we might get multiple output frames in this loop.

        // ffmpeg.c does not drain the filter chain on configuration changes,

        // which means if we send multiple frames at once to the filters, and

        // one of those frames changes configuration, the buffered frames will

        // be lost. This can upset certain FATE tests.

        // Decode only 1 frame per call on EOF to appease these FATE tests.

        // The ideal solution would be to rewrite decoding to use the new

        // decoding API in a better way.

        if (!pkt)

            break;

        repeating = 1;

    }

    /* after flushing, send an EOF on all the filter inputs attached to the stream */

    /* except when looping we need to flush but not to send an EOF */

    if (!pkt && ist->decoding_needed && eof_reached && !no_eof) {

        int ret = send_filter_eof(ist);

        if (ret < 0) {

            av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");

            exit_program(1);

        }

    }

    /* handle stream copy */

    if (!ist->decoding_needed) {

        ist->dts = ist->next_dts;

        switch (ist->dec_ctx->codec_type) {

        case AVMEDIA_TYPE_AUDIO:

            ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /

                             ist->dec_ctx->sample_rate;

            break;

        case AVMEDIA_TYPE_VIDEO:

            if (ist->framerate.num) {

                // TODO: Remove work-around for c99-to-c89 issue 7

                AVRational time_base_q = AV_TIME_BASE_Q;

                int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));

                ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);

            } else if (pkt->duration) {

                ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);

            } else if(ist->dec_ctx->framerate.num != 0) {

                int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;

                ist->next_dts += ((int64_t)AV_TIME_BASE *

                                  ist->dec_ctx->framerate.den * ticks) /

                                  ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;

            }

            break;

        }

        ist->pts = ist->dts;

        ist->next_pts = ist->next_dts;

    }

    for (i = 0; pkt && i < nb_output_streams; i++) {

        OutputStream *ost = output_streams[i];

        if (!check_output_constraints(ist, ost) || ost->encoding_needed)

            continue;

        do_streamcopy(ist, ost, pkt);

    }

    return !eof_reached;

}


BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)

{

    BlockDeviceInfoList *list, *entry;

    BlockDriverState *bs;

    list = NULL;

    QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {

        BlockDeviceInfo *info = bdrv_block_device_info(bs, errp);

        if (!info) {

            qapi_free_BlockDeviceInfoList(list);

            return NULL;

        }

        entry = g_malloc0(sizeof(*entry));

        entry->value = info;

        entry->next = list;

        list = entry;

    }

    return list;

}


static uint32_t dp8393x_readw(void *opaque, target_phys_addr_t addr)

{

    dp8393xState *s = opaque;

    int reg;

    if ((addr & ((1 << s->it_shift) - 1)) != 0) {

        return 0;

    }

    reg = addr >> s->it_shift;

    return read_register(s, reg);

}


void cpu_outw(pio_addr_t addr, uint16_t val)

{

    LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);

    trace_cpu_out(addr, val);

    ioport_write(1, addr, val);

}


void HELPER(wfe)(CPUARMState *env)

{

    CPUState *cs = CPU(arm_env_get_cpu(env));

    /* Don't actually halt the CPU, just yield back to top

     * level loop. This is not going into a "low power state"

     * (ie halting until some event occurs), so we never take

     * a configurable trap to a different exception level.

     */

    cs->exception_index = EXCP_YIELD;

    cpu_loop_exit(cs);

}


static int xenfb_send_position(struct XenInput *xenfb,

			       int abs_x, int abs_y, int z)

{

    union xenkbd_in_event event;

    memset(&event, 0, XENKBD_IN_EVENT_SIZE);

    event.type = XENKBD_TYPE_POS;

    event.pos.abs_x = abs_x;

    event.pos.abs_y = abs_y;

#if __XEN_LATEST_INTERFACE_VERSION__ == 0x00030207

    event.pos.abs_z = z;

#endif

#if __XEN_LATEST_INTERFACE_VERSION__ >= 0x00030208

    event.pos.rel_z = z;

#endif

    return xenfb_kbd_event(xenfb, &event);

}


static int latm_decode_frame(AVCodecContext *avctx, void *out,

                             int *got_frame_ptr, AVPacket *avpkt)

{

    struct LATMContext *latmctx = avctx->priv_data;

    int                 muxlength, err;

    GetBitContext       gb;

    init_get_bits(&gb, avpkt->data, avpkt->size * 8);

    // check for LOAS sync word

    if (get_bits(&gb, 11) != LOAS_SYNC_WORD)

        return AVERROR_INVALIDDATA;

    muxlength = get_bits(&gb, 13) + 3;

    // not enough data, the parser should have sorted this

    if (muxlength > avpkt->size)

        return AVERROR_INVALIDDATA;

    if ((err = read_audio_mux_element(latmctx, &gb)) < 0)

        return err;

    if (!latmctx->initialized) {

        if (!avctx->extradata) {

            *got_frame_ptr = 0;

            return avpkt->size;

        } else {

            push_output_configuration(&latmctx->aac_ctx);

            if ((err = decode_audio_specific_config(

                    &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,

                    avctx->extradata, avctx->extradata_size*8, 1)) < 0) {

                pop_output_configuration(&latmctx->aac_ctx);

                return err;

            }

            latmctx->initialized = 1;

        }

    }

    if (show_bits(&gb, 12) == 0xfff) {

        av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,

               "ADTS header detected, probably as result of configuration "

               "misparsing\n");

        return AVERROR_INVALIDDATA;

    }

    if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb)) < 0)

        return err;

    return muxlength;

}


static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)

{

    MotionEstContext * const c= &s->me;

    int P[10][2];

    const int mot_stride = s->mb_stride;

    const int mot_xy = mb_y*mot_stride + mb_x;

    const int shift= 1+s->quarter_sample;

    int dmin, i;

    const int time_pp= s->pp_time;

    const int time_pb= s->pb_time;

    int mx, my, xmin, xmax, ymin, ymax;

    int16_t (*mv_table)[2]= s->b_direct_mv_table;

    c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;

    ymin= xmin=(-32)>>shift;

    ymax= xmax=   31>>shift;

    if (IS_8X8(s->next_picture.mb_type[mot_xy])) {

        s->mv_type= MV_TYPE_8X8;

    }else{

        s->mv_type= MV_TYPE_16X16;

    }

    for(i=0; i<4; i++){

        int index= s->block_index[i];

        int min, max;

        c->co_located_mv[i][0] = s->next_picture.motion_val[0][index][0];

        c->co_located_mv[i][1] = s->next_picture.motion_val[0][index][1];

        c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));

        c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));

//        c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);

//        c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);

        max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;

        min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;

        max+= 16*mb_x + 1; // +-1 is for the simpler rounding

        min+= 16*mb_x - 1;

        xmax= FFMIN(xmax, s->width - max);

        xmin= FFMAX(xmin, - 16     - min);

        max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;

        min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;

        max+= 16*mb_y + 1; // +-1 is for the simpler rounding

        min+= 16*mb_y - 1;

        ymax= FFMIN(ymax, s->height - max);

        ymin= FFMAX(ymin, - 16      - min);

        if(s->mv_type == MV_TYPE_16X16) break;

    }

    av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);

    if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){

        s->b_direct_mv_table[mot_xy][0]= 0;

        s->b_direct_mv_table[mot_xy][1]= 0;

        return 256*256*256*64;

    }

    c->xmin= xmin;

    c->ymin= ymin;

    c->xmax= xmax;

    c->ymax= ymax;

    c->flags     |= FLAG_DIRECT;

    c->sub_flags |= FLAG_DIRECT;

    c->pred_x=0;

    c->pred_y=0;

    P_LEFT[0]        = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);

    P_LEFT[1]        = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);

    /* special case for first line */

    if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped

        P_TOP[0]      = av_clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);

        P_TOP[1]      = av_clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);

        P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);

        P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);

        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);

        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);

    }

    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);

    if(c->sub_flags&FLAG_QPEL)

        dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);

    else

        dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);

    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)

        dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);

    get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed

    mv_table[mot_xy][0]= mx;

    mv_table[mot_xy][1]= my;

    c->flags     &= ~FLAG_DIRECT;

    c->sub_flags &= ~FLAG_DIRECT;

    return dmin;

}


static av_cold void iv_alloc_frames(Indeo3DecodeContext *s)

{

  int luma_width, luma_height, luma_pixels, chroma_width, chroma_height,

      chroma_pixels, i;

  unsigned int bufsize;

  luma_width   = (s->width  + 3) & (~3);

  luma_height  = (s->height + 3) & (~3);

  s->iv_frame[0].y_w = s->iv_frame[0].y_h =

    s->iv_frame[0].the_buf_size = 0;

  s->iv_frame[1].y_w = s->iv_frame[1].y_h =

    s->iv_frame[1].the_buf_size = 0;

  s->iv_frame[1].the_buf = NULL;

  chroma_width  = ((luma_width >> 2) + 3) & (~3);

  chroma_height = ((luma_height>> 2) + 3) & (~3);

  luma_pixels = luma_width * luma_height;

  chroma_pixels = chroma_width * chroma_height;

  bufsize = luma_pixels * 2 + luma_width * 3 +

    (chroma_pixels + chroma_width) * 4;

  if(!(s->iv_frame[0].the_buf = av_malloc(bufsize)))

    return;

  s->iv_frame[0].y_w = s->iv_frame[1].y_w = luma_width;

  s->iv_frame[0].y_h = s->iv_frame[1].y_h = luma_height;

  s->iv_frame[0].uv_w = s->iv_frame[1].uv_w = chroma_width;

  s->iv_frame[0].uv_h = s->iv_frame[1].uv_h = chroma_height;

  s->iv_frame[0].the_buf_size = bufsize;

  s->iv_frame[0].Ybuf = s->iv_frame[0].the_buf + luma_width;

  i = luma_pixels + luma_width * 2;

  s->iv_frame[1].Ybuf = s->iv_frame[0].the_buf + i;

  i += (luma_pixels + luma_width);

  s->iv_frame[0].Ubuf = s->iv_frame[0].the_buf + i;

  i += (chroma_pixels + chroma_width);

  s->iv_frame[1].Ubuf = s->iv_frame[0].the_buf + i;

  i += (chroma_pixels + chroma_width);

  s->iv_frame[0].Vbuf = s->iv_frame[0].the_buf + i;

  i += (chroma_pixels + chroma_width);

  s->iv_frame[1].Vbuf = s->iv_frame[0].the_buf + i;

  for(i = 1; i <= luma_width; i++)

    s->iv_frame[0].Ybuf[-i] = s->iv_frame[1].Ybuf[-i] =

      s->iv_frame[0].Ubuf[-i] = 0x80;

  for(i = 1; i <= chroma_width; i++) {

    s->iv_frame[1].Ubuf[-i] = 0x80;

    s->iv_frame[0].Vbuf[-i] = 0x80;

    s->iv_frame[1].Vbuf[-i] = 0x80;

    s->iv_frame[1].Vbuf[chroma_pixels+i-1] = 0x80;

  }

}


int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)

{

    int frame_size_code;

    memset(hdr, 0, sizeof(*hdr));

    hdr->sync_word = get_bits(gbc, 16);

    if(hdr->sync_word != 0x0B77)

        return AAC_AC3_PARSE_ERROR_SYNC;

    /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */

    hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;

    if(hdr->bitstream_id > 16)

        return AAC_AC3_PARSE_ERROR_BSID;

    hdr->num_blocks = 6;

    /* set default mix levels */

    hdr->center_mix_level   = 1;  // -4.5dB

    hdr->surround_mix_level = 1;  // -6.0dB

    if(hdr->bitstream_id <= 10) {

        /* Normal AC-3 */

        hdr->crc1 = get_bits(gbc, 16);

        hdr->sr_code = get_bits(gbc, 2);

        if(hdr->sr_code == 3)

            return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;

        frame_size_code = get_bits(gbc, 6);

        if(frame_size_code > 37)

            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;

        skip_bits(gbc, 5); // skip bsid, already got it

        hdr->bitstream_mode = get_bits(gbc, 3);

        hdr->channel_mode = get_bits(gbc, 3);

        if(hdr->channel_mode == AC3_CHMODE_STEREO) {

            skip_bits(gbc, 2); // skip dsurmod

        } else {

            if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)

                hdr->center_mix_level = get_bits(gbc, 2);

            if(hdr->channel_mode & 4)

                hdr->surround_mix_level = get_bits(gbc, 2);

        }

        hdr->lfe_on = get_bits1(gbc);

        hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;

        hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;

        hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;

        hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;

        hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;

        hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;

        hdr->substreamid = 0;

    } else {

        /* Enhanced AC-3 */

        hdr->crc1 = 0;

        hdr->frame_type = get_bits(gbc, 2);

        if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)

            return AAC_AC3_PARSE_ERROR_FRAME_TYPE;

        hdr->substreamid = get_bits(gbc, 3);

        hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;

        if(hdr->frame_size < AC3_HEADER_SIZE)

            return AAC_AC3_PARSE_ERROR_FRAME_SIZE;

        hdr->sr_code = get_bits(gbc, 2);

        if (hdr->sr_code == 3) {

            int sr_code2 = get_bits(gbc, 2);

            if(sr_code2 == 3)

                return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;

            hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;

            hdr->sr_shift = 1;

        } else {

            hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];

            hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];

            hdr->sr_shift = 0;

        }

        hdr->channel_mode = get_bits(gbc, 3);

        hdr->lfe_on = get_bits1(gbc);

        hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /

                        (hdr->num_blocks * 256.0));

        hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;

    }

    hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];

    if (hdr->lfe_on)

        hdr->channel_layout |= AV_CH_LOW_FREQUENCY;

    return 0;

}


av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)

{

    MpegEncContext *s = &v->s;

    int i;

    int mb_height = FFALIGN(s->mb_height, 2);

    /* Allocate mb bitplanes */

    v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);

    v->direct_mb_plane  = av_malloc (s->mb_stride * mb_height);

    v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);

    v->fieldtx_plane    = av_mallocz(s->mb_stride * mb_height);

    v->acpred_plane     = av_malloc (s->mb_stride * mb_height);

    v->over_flags_plane = av_malloc (s->mb_stride * mb_height);

    if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->forward_mb_plane ||

        !v->fieldtx_plane || !v->acpred_plane || !v->over_flags_plane)

        goto error;

    v->n_allocated_blks = s->mb_width + 2;

    v->block            = av_malloc(sizeof(*v->block) * v->n_allocated_blks);

    v->cbp_base         = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);

    if (!v->block || !v->cbp_base)

        goto error;

    v->cbp              = v->cbp_base + s->mb_stride;

    v->ttblk_base       = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);

    if (!v->ttblk_base)

        goto error;

    v->ttblk            = v->ttblk_base + s->mb_stride;

    v->is_intra_base    = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);

    if (!v->is_intra_base)

        goto error;

    v->is_intra         = v->is_intra_base + s->mb_stride;

    v->luma_mv_base     = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);

    if (!v->luma_mv_base)

        goto error;

    v->luma_mv          = v->luma_mv_base + s->mb_stride;

    /* allocate block type info in that way so it could be used with s->block_index[] */

    v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);

    if (!v->mb_type_base)

        goto error;

    v->mb_type[0]   = v->mb_type_base + s->b8_stride + 1;

    v->mb_type[1]   = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;

    v->mb_type[2]   = v->mb_type[1] + s->mb_stride * (mb_height + 1);

    /* allocate memory to store block level MV info */

    v->blk_mv_type_base = av_mallocz(     s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);

    if (!v->blk_mv_type_base)

        goto error;

    v->blk_mv_type      = v->blk_mv_type_base + s->b8_stride + 1;

    v->mv_f_base        = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));

    if (!v->mv_f_base)

        goto error;

    v->mv_f[0]          = v->mv_f_base + s->b8_stride + 1;

    v->mv_f[1]          = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);

    v->mv_f_next_base   = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));

    if (!v->mv_f_next_base)

        goto error;

    v->mv_f_next[0]     = v->mv_f_next_base + s->b8_stride + 1;

    v->mv_f_next[1]     = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);

    ff_intrax8_common_init(&v->x8,s);

    if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {

        for (i = 0; i < 4; i++) {

            v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width);

            if (!v->sr_rows[i >> 1][i & 1])

                goto error;

        }

    }

    return 0;

error:

    ff_vc1_decode_end(s->avctx);

    return AVERROR(ENOMEM);

}


static void iv_Decode_Chunk(Indeo3DecodeContext *s,

        uint8_t *cur, uint8_t *ref, int width, int height,

        const uint8_t *buf1, long cb_offset, const uint8_t *hdr,

        const uint8_t *buf2, int min_width_160)

{

    uint8_t bit_buf;

    unsigned long bit_pos, lv, lv1, lv2;

    long *width_tbl, width_tbl_arr[10];

    const signed char *ref_vectors;

    uint8_t *cur_frm_pos, *ref_frm_pos, *cp, *cp2;

    uint32_t *cur_lp, *ref_lp;

    const uint32_t *correction_lp[2], *correctionloworder_lp[2], *correctionhighorder_lp[2];

    uint8_t *correction_type_sp[2];

    struct ustr strip_tbl[20], *strip;

    int i, j, k, lp1, lp2, flag1, cmd, blks_width, blks_height, region_160_width,

        rle_v1, rle_v2, rle_v3;

    unsigned short res;

    bit_buf = 0;

    ref_vectors = NULL;

    width_tbl = width_tbl_arr + 1;

    i = (width < 0 ? width + 3 : width)/4;

    for(j = -1; j < 8; j++)

        width_tbl[j] = i * j;

    strip = strip_tbl;

    for(region_160_width = 0; region_160_width < (width - min_width_160); region_160_width += min_width_160);

    strip->ypos = strip->xpos = 0;

    for(strip->width = min_width_160; width > strip->width; strip->width *= 2);

    strip->height = height;

    strip->split_direction = 0;

    strip->split_flag = 0;

    strip->usl7 = 0;

    bit_pos = 0;

    rle_v1 = rle_v2 = rle_v3 = 0;

    while(strip >= strip_tbl) {

        if(bit_pos <= 0) {

            bit_pos = 8;

            bit_buf = *buf1++;

        }

        bit_pos -= 2;

        cmd = (bit_buf >> bit_pos) & 0x03;

        if(cmd == 0) {

            strip++;

            if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {

                av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");

                break;

            }

            memcpy(strip, strip-1, sizeof(*strip));

            strip->split_flag = 1;

            strip->split_direction = 0;

            strip->height = (strip->height > 8 ? ((strip->height+8)>>4)<<3 : 4);

            continue;

        } else if(cmd == 1) {

            strip++;

            if(strip >= strip_tbl + FF_ARRAY_ELEMS(strip_tbl)) {

                av_log(s->avctx, AV_LOG_WARNING, "out of range strip\n");

                break;

            }

            memcpy(strip, strip-1, sizeof(*strip));

            strip->split_flag = 1;

            strip->split_direction = 1;

            strip->width = (strip->width > 8 ? ((strip->width+8)>>4)<<3 : 4);

            continue;

        } else if(cmd == 2) {

            if(strip->usl7 == 0) {

                strip->usl7 = 1;

                ref_vectors = NULL;

                continue;

            }

        } else if(cmd == 3) {

            if(strip->usl7 == 0) {

                strip->usl7 = 1;

                ref_vectors = (const signed char*)buf2 + (*buf1 * 2);

                buf1++;

                continue;

            }

        }

        cur_frm_pos = cur + width * strip->ypos + strip->xpos;

        if((blks_width = strip->width) < 0)

            blks_width += 3;

        blks_width >>= 2;

        blks_height = strip->height;

        if(ref_vectors != NULL) {

            ref_frm_pos = ref + (ref_vectors[0] + strip->ypos) * width +

                ref_vectors[1] + strip->xpos;

        } else

            ref_frm_pos = cur_frm_pos - width_tbl[4];

        if(cmd == 2) {

            if(bit_pos <= 0) {

                bit_pos = 8;

                bit_buf = *buf1++;

            }

            bit_pos -= 2;

            cmd = (bit_buf >> bit_pos) & 0x03;

            if(cmd == 0 || ref_vectors != NULL) {

                for(lp1 = 0; lp1 < blks_width; lp1++) {

                    for(i = 0, j = 0; i < blks_height; i++, j += width_tbl[1])

                        ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];

                    cur_frm_pos += 4;

                    ref_frm_pos += 4;

                }

            } else if(cmd != 1)

                return;

        } else {

            k = *buf1 >> 4;

            j = *buf1 & 0x0f;

            buf1++;

            lv = j + cb_offset;

            if((lv - 8) <= 7 && (k == 0 || k == 3 || k == 10)) {

                cp2 = s->ModPred + ((lv - 8) << 7);

                cp = ref_frm_pos;

                for(i = 0; i < blks_width << 2; i++) {

                    int v = *cp >> 1;

                    *(cp++) = cp2[v];

                }

            }

            if(k == 1 || k == 4) {

                lv = (hdr[j] & 0xf) + cb_offset;

                correction_type_sp[0] = s->corrector_type + (lv << 8);

                correction_lp[0] = correction + (lv << 8);

                lv = (hdr[j] >> 4) + cb_offset;

                correction_lp[1] = correction + (lv << 8);

                correction_type_sp[1] = s->corrector_type + (lv << 8);

            } else {

                correctionloworder_lp[0] = correctionloworder_lp[1] = correctionloworder + (lv << 8);

                correctionhighorder_lp[0] = correctionhighorder_lp[1] = correctionhighorder + (lv << 8);

                correction_type_sp[0] = correction_type_sp[1] = s->corrector_type + (lv << 8);

                correction_lp[0] = correction_lp[1] = correction + (lv << 8);

            }

            switch(k) {

            case 1:

            case 0:                    /********** CASE 0 **********/

                for( ; blks_height > 0; blks_height -= 4) {

                    for(lp1 = 0; lp1 < blks_width; lp1++) {

                        for(lp2 = 0; lp2 < 4; ) {

                            k = *buf1++;

                            cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2];

                            ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2];

                            switch(correction_type_sp[0][k]) {

                            case 0:

                                *cur_lp = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);

                                lp2++;

                                break;

                            case 1:

                                res = ((le2me_16(((unsigned short *)(ref_lp))[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;

                                ((unsigned short *)cur_lp)[0] = le2me_16(res);

                                res = ((le2me_16(((unsigned short *)(ref_lp))[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;

                                ((unsigned short *)cur_lp)[1] = le2me_16(res);

                                buf1++;

                                lp2++;

                                break;

                            case 2:

                                if(lp2 == 0) {

                                    for(i = 0, j = 0; i < 2; i++, j += width_tbl[1])

                                        cur_lp[j] = ref_lp[j];

                                    lp2 += 2;

                                }

                                break;

                            case 3:

                                if(lp2 < 2) {

                                    for(i = 0, j = 0; i < (3 - lp2); i++, j += width_tbl[1])

                                        cur_lp[j] = ref_lp[j];

                                    lp2 = 3;

                                }

                                break;

                            case 8:

                                if(lp2 == 0) {

                                    RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)

                                    if(rle_v1 == 1 || ref_vectors != NULL) {

                                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])

                                            cur_lp[j] = ref_lp[j];

                                    }

                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)

                                    break;

                                } else {

                                    rle_v1 = 1;

                                    rle_v2 = *buf1 - 1;

                                }

                            case 5:

                                LP2_CHECK(buf1,rle_v3,lp2)

                            case 4:

                                for(i = 0, j = 0; i < (4 - lp2); i++, j += width_tbl[1])

                                    cur_lp[j] = ref_lp[j];

                                lp2 = 4;

                                break;

                            case 7:

                                if(rle_v3 != 0)

                                    rle_v3 = 0;

                                else {

                                    buf1--;

                                    rle_v3 = 1;

                                }

                            case 6:

                                if(ref_vectors != NULL) {

                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])

                                        cur_lp[j] = ref_lp[j];

                                }

                                lp2 = 4;

                                break;

                            case 9:

                                lv1 = *buf1++;

                                lv = (lv1 & 0x7F) << 1;

                                lv += (lv << 8);

                                lv += (lv << 16);

                                for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])

                                    cur_lp[j] = lv;

                                LV1_CHECK(buf1,rle_v3,lv1,lp2)

                                break;

                            default:

                                return;

                            }

                        }

                        cur_frm_pos += 4;

                        ref_frm_pos += 4;

                    }

                    cur_frm_pos += ((width - blks_width) * 4);

                    ref_frm_pos += ((width - blks_width) * 4);

                }

                break;

            case 4:

            case 3:                    /********** CASE 3 **********/

                if(ref_vectors != NULL)

                    return;

                flag1 = 1;

                for( ; blks_height > 0; blks_height -= 8) {

                    for(lp1 = 0; lp1 < blks_width; lp1++) {

                        for(lp2 = 0; lp2 < 4; ) {

                            k = *buf1++;

                            cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];

                            ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];

                            switch(correction_type_sp[lp2 & 0x01][k]) {

                            case 0:

                                cur_lp[width_tbl[1]] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);

                                if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)

                                    cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;

                                else

                                    cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);

                                lp2++;

                                break;

                            case 1:

                                res = ((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + correction_lp[lp2 & 0x01][*buf1]) << 1;

                                ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);

                                res = ((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1;

                                ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);

                                if(lp2 > 0 || flag1 == 0 || strip->ypos != 0)

                                    cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;

                                else

                                    cur_lp[0] = cur_lp[width_tbl[1]];

                                buf1++;

                                lp2++;

                                break;

                            case 2:

                                if(lp2 == 0) {

                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])

                                        cur_lp[j] = *ref_lp;

                                    lp2 += 2;

                                }

                                break;

                            case 3:

                                if(lp2 < 2) {

                                    for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])

                                        cur_lp[j] = *ref_lp;

                                    lp2 = 3;

                                }

                                break;

                            case 6:

                                lp2 = 4;

                                break;

                            case 7:

                                if(rle_v3 != 0)

                                    rle_v3 = 0;

                                else {

                                    buf1--;

                                    rle_v3 = 1;

                                }

                                lp2 = 4;

                                break;

                            case 8:

                                if(lp2 == 0) {

                                    RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)

                                    if(rle_v1 == 1) {

                                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])

                                            cur_lp[j] = ref_lp[j];

                                    }

                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)

                                    break;

                                } else {

                                    rle_v2 = (*buf1) - 1;

                                    rle_v1 = 1;

                                }

                            case 5:

                                LP2_CHECK(buf1,rle_v3,lp2)

                            case 4:

                                for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])

                                    cur_lp[j] = *ref_lp;

                                lp2 = 4;

                                break;

                            case 9:

                                av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");

                                lv1 = *buf1++;

                                lv = (lv1 & 0x7F) << 1;

                                lv += (lv << 8);

                                lv += (lv << 16);

                                for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])

                                    cur_lp[j] = lv;

                                LV1_CHECK(buf1,rle_v3,lv1,lp2)

                                break;

                            default:

                                return;

                            }

                        }

                        cur_frm_pos += 4;

                    }

                    cur_frm_pos += (((width * 2) - blks_width) * 4);

                    flag1 = 0;

                }

                break;

            case 10:                    /********** CASE 10 **********/

                if(ref_vectors == NULL) {

                    flag1 = 1;

                    for( ; blks_height > 0; blks_height -= 8) {

                        for(lp1 = 0; lp1 < blks_width; lp1 += 2) {

                            for(lp2 = 0; lp2 < 4; ) {

                                k = *buf1++;

                                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];

                                ref_lp = ((uint32_t *)cur_frm_pos) + width_tbl[(lp2 * 2) - 1];

                                lv1 = ref_lp[0];

                                lv2 = ref_lp[1];

                                if(lp2 == 0 && flag1 != 0) {

#ifdef WORDS_BIGENDIAN

                                    lv1 = lv1 & 0xFF00FF00;

                                    lv1 = (lv1 >> 8) | lv1;

                                    lv2 = lv2 & 0xFF00FF00;

                                    lv2 = (lv2 >> 8) | lv2;

#else

                                    lv1 = lv1 & 0x00FF00FF;

                                    lv1 = (lv1 << 8) | lv1;

                                    lv2 = lv2 & 0x00FF00FF;

                                    lv2 = (lv2 << 8) | lv2;

#endif

                                }

                                switch(correction_type_sp[lp2 & 0x01][k]) {

                                case 0:

                                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);

                                    cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionhighorder_lp[lp2 & 0x01][k]) << 1);

                                    if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {

                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;

                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;

                                    } else {

                                        cur_lp[0] = cur_lp[width_tbl[1]];

                                        cur_lp[1] = cur_lp[width_tbl[1]+1];

                                    }

                                    lp2++;

                                    break;

                                case 1:

                                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(lv1) >> 1) + correctionloworder_lp[lp2 & 0x01][*buf1]) << 1);

                                    cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(lv2) >> 1) + correctionloworder_lp[lp2 & 0x01][k]) << 1);

                                    if(lp2 > 0 || strip->ypos != 0 || flag1 == 0) {

                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;

                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;

                                    } else {

                                        cur_lp[0] = cur_lp[width_tbl[1]];

                                        cur_lp[1] = cur_lp[width_tbl[1]+1];

                                    }

                                    buf1++;

                                    lp2++;

                                    break;

                                case 2:

                                    if(lp2 == 0) {

                                        if(flag1 != 0) {

                                            for(i = 0, j = width_tbl[1]; i < 3; i++, j += width_tbl[1]) {

                                                cur_lp[j] = lv1;

                                                cur_lp[j+1] = lv2;

                                            }

                                            cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;

                                            cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;

                                        } else {

                                            for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {

                                                cur_lp[j] = lv1;

                                                cur_lp[j+1] = lv2;

                                            }

                                        }

                                        lp2 += 2;

                                    }

                                    break;

                                case 3:

                                    if(lp2 < 2) {

                                        if(lp2 == 0 && flag1 != 0) {

                                            for(i = 0, j = width_tbl[1]; i < 5; i++, j += width_tbl[1]) {

                                                cur_lp[j] = lv1;

                                                cur_lp[j+1] = lv2;

                                            }

                                            cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;

                                            cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;

                                        } else {

                                            for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {

                                                cur_lp[j] = lv1;

                                                cur_lp[j+1] = lv2;

                                            }

                                        }

                                        lp2 = 3;

                                    }

                                    break;

                                case 8:

                                    if(lp2 == 0) {

                                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)

                                        if(rle_v1 == 1) {

                                            if(flag1 != 0) {

                                                for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {

                                                    cur_lp[j] = lv1;

                                                    cur_lp[j+1] = lv2;

                                                }

                                                cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;

                                                cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;

                                            } else {

                                                for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {

                                                    cur_lp[j] = lv1;

                                                    cur_lp[j+1] = lv2;

                                                }

                                            }

                                        }

                                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)

                                        break;

                                    } else {

                                        rle_v1 = 1;

                                        rle_v2 = (*buf1) - 1;

                                    }

                                case 5:

                                    LP2_CHECK(buf1,rle_v3,lp2)

                                case 4:

                                    if(lp2 == 0 && flag1 != 0) {

                                        for(i = 0, j = width_tbl[1]; i < 7; i++, j += width_tbl[1]) {

                                            cur_lp[j] = lv1;

                                            cur_lp[j+1] = lv2;

                                        }

                                        cur_lp[0] = ((cur_lp[-width_tbl[1]] >> 1) + (cur_lp[width_tbl[1]] >> 1)) & 0xFEFEFEFE;

                                        cur_lp[1] = ((cur_lp[-width_tbl[1]+1] >> 1) + (cur_lp[width_tbl[1]+1] >> 1)) & 0xFEFEFEFE;

                                    } else {

                                        for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {

                                            cur_lp[j] = lv1;

                                            cur_lp[j+1] = lv2;

                                        }

                                    }

                                    lp2 = 4;

                                    break;

                                case 6:

                                    lp2 = 4;

                                    break;

                                case 7:

                                    if(lp2 == 0) {

                                        if(rle_v3 != 0)

                                            rle_v3 = 0;

                                        else {

                                            buf1--;

                                            rle_v3 = 1;

                                        }

                                        lp2 = 4;

                                    }

                                    break;

                                case 9:

                                    av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");

                                    lv1 = *buf1;

                                    lv = (lv1 & 0x7F) << 1;

                                    lv += (lv << 8);

                                    lv += (lv << 16);

                                    for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])

                                        cur_lp[j] = lv;

                                    LV1_CHECK(buf1,rle_v3,lv1,lp2)

                                    break;

                                default:

                                    return;

                                }

                            }

                            cur_frm_pos += 8;

                        }

                        cur_frm_pos += (((width * 2) - blks_width) * 4);

                        flag1 = 0;

                    }

                } else {

                    for( ; blks_height > 0; blks_height -= 8) {

                        for(lp1 = 0; lp1 < blks_width; lp1 += 2) {

                            for(lp2 = 0; lp2 < 4; ) {

                                k = *buf1++;

                                cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];

                                ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];

                                switch(correction_type_sp[lp2 & 0x01][k]) {

                                case 0:

                                    lv1 = correctionloworder_lp[lp2 & 0x01][k];

                                    lv2 = correctionhighorder_lp[lp2 & 0x01][k];

                                    cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);

                                    cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);

                                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);

                                    cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);

                                    lp2++;

                                    break;

                                case 1:

                                    lv1 = correctionloworder_lp[lp2 & 0x01][*buf1++];

                                    lv2 = correctionloworder_lp[lp2 & 0x01][k];

                                    cur_lp[0] = le2me_32(((le2me_32(ref_lp[0]) >> 1) + lv1) << 1);

                                    cur_lp[1] = le2me_32(((le2me_32(ref_lp[1]) >> 1) + lv2) << 1);

                                    cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + lv1) << 1);

                                    cur_lp[width_tbl[1]+1] = le2me_32(((le2me_32(ref_lp[width_tbl[1]+1]) >> 1) + lv2) << 1);

                                    lp2++;

                                    break;

                                case 2:

                                    if(lp2 == 0) {

                                        for(i = 0, j = 0; i < 4; i++, j += width_tbl[1]) {

                                            cur_lp[j] = ref_lp[j];

                                            cur_lp[j+1] = ref_lp[j+1];

                                        }

                                        lp2 += 2;

                                    }

                                    break;

                                case 3:

                                    if(lp2 < 2) {

                                        for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1]) {

                                            cur_lp[j] = ref_lp[j];

                                            cur_lp[j+1] = ref_lp[j+1];

                                        }

                                        lp2 = 3;

                                    }

                                    break;

                                case 8:

                                    if(lp2 == 0) {

                                        RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)

                                        for(i = 0, j = 0; i < 8; i++, j += width_tbl[1]) {

                                            ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)ref_frm_pos)[j];

                                            ((uint32_t *)cur_frm_pos)[j+1] = ((uint32_t *)ref_frm_pos)[j+1];

                                        }

                                        RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)

                                        break;

                                    } else {

                                        rle_v1 = 1;

                                        rle_v2 = (*buf1) - 1;

                                    }

                                case 5:

                                case 7:

                                    LP2_CHECK(buf1,rle_v3,lp2)

                                case 6:

                                case 4:

                                    for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1]) {

                                        cur_lp[j] = ref_lp[j];

                                        cur_lp[j+1] = ref_lp[j+1];

                                    }

                                    lp2 = 4;

                                    break;

                                case 9:

                                    av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");

                                    lv1 = *buf1;

                                    lv = (lv1 & 0x7F) << 1;

                                    lv += (lv << 8);

                                    lv += (lv << 16);

                                    for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])

                                        ((uint32_t *)cur_frm_pos)[j] = ((uint32_t *)cur_frm_pos)[j+1] = lv;

                                    LV1_CHECK(buf1,rle_v3,lv1,lp2)

                                    break;

                                default:

                                    return;

                                }

                            }

                            cur_frm_pos += 8;

                            ref_frm_pos += 8;

                        }

                        cur_frm_pos += (((width * 2) - blks_width) * 4);

                        ref_frm_pos += (((width * 2) - blks_width) * 4);

                    }

                }

                break;

            case 11:                    /********** CASE 11 **********/

                if(ref_vectors == NULL)

                    return;

                for( ; blks_height > 0; blks_height -= 8) {

                    for(lp1 = 0; lp1 < blks_width; lp1++) {

                        for(lp2 = 0; lp2 < 4; ) {

                            k = *buf1++;

                            cur_lp = ((uint32_t *)cur_frm_pos) + width_tbl[lp2 * 2];

                            ref_lp = ((uint32_t *)ref_frm_pos) + width_tbl[lp2 * 2];

                            switch(correction_type_sp[lp2 & 0x01][k]) {

                            case 0:

                                cur_lp[0] = le2me_32(((le2me_32(*ref_lp) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);

                                cur_lp[width_tbl[1]] = le2me_32(((le2me_32(ref_lp[width_tbl[1]]) >> 1) + correction_lp[lp2 & 0x01][k]) << 1);

                                lp2++;

                                break;

                            case 1:

                                lv1 = (unsigned short)(correction_lp[lp2 & 0x01][*buf1++]);

                                lv2 = (unsigned short)(correction_lp[lp2 & 0x01][k]);

                                res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[0]) >> 1) + lv1) << 1);

                                ((unsigned short *)cur_lp)[0] = le2me_16(res);

                                res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[1]) >> 1) + lv2) << 1);

                                ((unsigned short *)cur_lp)[1] = le2me_16(res);

                                res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]]) >> 1) + lv1) << 1);

                                ((unsigned short *)cur_lp)[width_tbl[2]] = le2me_16(res);

                                res = (unsigned short)(((le2me_16(((unsigned short *)ref_lp)[width_tbl[2]+1]) >> 1) + lv2) << 1);

                                ((unsigned short *)cur_lp)[width_tbl[2]+1] = le2me_16(res);

                                lp2++;

                                break;

                            case 2:

                                if(lp2 == 0) {

                                    for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])

                                        cur_lp[j] = ref_lp[j];

                                    lp2 += 2;

                                }

                                break;

                            case 3:

                                if(lp2 < 2) {

                                    for(i = 0, j = 0; i < 6 - (lp2 * 2); i++, j += width_tbl[1])

                                        cur_lp[j] = ref_lp[j];

                                    lp2 = 3;

                                }

                                break;

                            case 8:

                                if(lp2 == 0) {

                                    RLE_V3_CHECK(buf1,rle_v1,rle_v2,rle_v3)

                                    for(i = 0, j = 0; i < 8; i++, j += width_tbl[1])

                                        cur_lp[j] = ref_lp[j];

                                    RLE_V2_CHECK(buf1,rle_v2, rle_v3,lp2)

                                    break;

                                } else {

                                    rle_v1 = 1;

                                    rle_v2 = (*buf1) - 1;

                                }

                            case 5:

                            case 7:

                                LP2_CHECK(buf1,rle_v3,lp2)

                            case 4:

                            case 6:

                                for(i = 0, j = 0; i < 8 - (lp2 * 2); i++, j += width_tbl[1])

                                    cur_lp[j] = ref_lp[j];

                                lp2 = 4;

                                break;

                            case 9:

                                av_log(s->avctx, AV_LOG_ERROR, "UNTESTED.\n");

                                lv1 = *buf1++;

                                lv = (lv1 & 0x7F) << 1;

                                lv += (lv << 8);

                                lv += (lv << 16);

                                for(i = 0, j = 0; i < 4; i++, j += width_tbl[1])

                                    cur_lp[j] = lv;

                                LV1_CHECK(buf1,rle_v3,lv1,lp2)

                                break;

                            default:

                                return;

                            }

                        }

                        cur_frm_pos += 4;

                        ref_frm_pos += 4;

                    }

                    cur_frm_pos += (((width * 2) - blks_width) * 4);

                    ref_frm_pos += (((width * 2) - blks_width) * 4);

                }

                break;

            default:

                return;

            }

        }

        if(strip < strip_tbl)

            return;

        for( ; strip >= strip_tbl; strip--) {

            if(strip->split_flag != 0) {

                strip->split_flag = 0;

                strip->usl7 = (strip-1)->usl7;

                if(strip->split_direction) {

                    strip->xpos += strip->width;

                    strip->width = (strip-1)->width - strip->width;

                    if(region_160_width <= strip->xpos && width < strip->width + strip->xpos)

                        strip->width = width - strip->xpos;

                } else {

                    strip->ypos += strip->height;

                    strip->height = (strip-1)->height - strip->height;

                }

                break;

            }

        }

    }

}


void ff_avg_h264_qpel4_mc33_msa(uint8_t *dst, const uint8_t *src,

                                ptrdiff_t stride)

{

    avc_luma_hv_qrt_and_aver_dst_4x4_msa(src + stride - 2,

                                         src - (stride * 2) +

                                         sizeof(uint8_t), stride, dst, stride);

}


static int mpeg1_decode_picture(AVCodecContext *avctx,

                                const uint8_t *buf, int buf_size)

{

    Mpeg1Context *s1 = avctx->priv_data;

    MpegEncContext *s = &s1->mpeg_enc_ctx;

    int ref, f_code, vbv_delay;

    if(mpeg_decode_postinit(s->avctx) < 0)

       return -2;

    init_get_bits(&s->gb, buf, buf_size*8);

    ref = get_bits(&s->gb, 10); /* temporal ref */

    s->pict_type = get_bits(&s->gb, 3);

    vbv_delay= get_bits(&s->gb, 16);

    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {

        s->full_pel[0] = get_bits1(&s->gb);

        f_code = get_bits(&s->gb, 3);

        if (f_code == 0)

            return -1;

        s->mpeg_f_code[0][0] = f_code;

        s->mpeg_f_code[0][1] = f_code;

    }

    if (s->pict_type == B_TYPE) {

        s->full_pel[1] = get_bits1(&s->gb);

        f_code = get_bits(&s->gb, 3);

        if (f_code == 0)

            return -1;

        s->mpeg_f_code[1][0] = f_code;

        s->mpeg_f_code[1][1] = f_code;

    }

    s->current_picture.pict_type= s->pict_type;

    s->current_picture.key_frame= s->pict_type == I_TYPE;

//    if(avctx->debug & FF_DEBUG_PICT_INFO)

//        av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d\n", vbv_delay, ref);

    s->y_dc_scale = 8;

    s->c_dc_scale = 8;

    s->first_slice = 1;

    return 0;

}