static void idct(uint8_t *dst, int dst_linesize, int src[64])

{

    int i, j, k;

    double tmp[64];

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

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

            double sum = 0.0;

            for (k = 0; k < 8; k++)

                sum += c[k*8+j] * src[8*i+k];

            tmp[8*i+j] = sum;

        }

    }

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

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

            double sum = 0.0;

            for (k = 0; k < 8; k++)

                sum += c[k*8+i]*tmp[8*k+j];

            dst[dst_linesize*i + j] = av_clip_uint8((int)floor(sum+0.5));

        }

    }

}


static int usb_hid_handle_control(USBDevice *dev, int request, int value,

                                  int index, int length, uint8_t *data)

{

    USBHIDState *s = (USBHIDState *)dev;

    int ret;

    ret = usb_desc_handle_control(dev, request, value, index, length, data);

    if (ret >= 0) {

        return ret;

    }

    ret = 0;

    switch(request) {

    case DeviceRequest | USB_REQ_GET_STATUS:

        data[0] = (1 << USB_DEVICE_SELF_POWERED) |

            (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);

        data[1] = 0x00;

        ret = 2;

        break;

    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:

        if (value == USB_DEVICE_REMOTE_WAKEUP) {

            dev->remote_wakeup = 0;

        } else {

            goto fail;

        }

        ret = 0;

        break;

    case DeviceOutRequest | USB_REQ_SET_FEATURE:

        if (value == USB_DEVICE_REMOTE_WAKEUP) {

            dev->remote_wakeup = 1;

        } else {

            goto fail;

        }

        ret = 0;

        break;

    case DeviceRequest | USB_REQ_GET_CONFIGURATION:

        data[0] = 1;

        ret = 1;

        break;

    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:

        ret = 0;

        break;

    case DeviceRequest | USB_REQ_GET_INTERFACE:

        data[0] = 0;

        ret = 1;

        break;

    case DeviceOutRequest | USB_REQ_SET_INTERFACE:

        ret = 0;

        break;

        /* hid specific requests */

    case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:

        switch(value >> 8) {

        case 0x22:

	    if (s->kind == USB_MOUSE) {

		memcpy(data, qemu_mouse_hid_report_descriptor,

		       sizeof(qemu_mouse_hid_report_descriptor));

		ret = sizeof(qemu_mouse_hid_report_descriptor);

	    } else if (s->kind == USB_TABLET) {

		memcpy(data, qemu_tablet_hid_report_descriptor,

		       sizeof(qemu_tablet_hid_report_descriptor));

		ret = sizeof(qemu_tablet_hid_report_descriptor);

            } else if (s->kind == USB_KEYBOARD) {

                memcpy(data, qemu_keyboard_hid_report_descriptor,

                       sizeof(qemu_keyboard_hid_report_descriptor));

                ret = sizeof(qemu_keyboard_hid_report_descriptor);

            }

            break;

        default:

            goto fail;

        }

        break;

    case GET_REPORT:

	if (s->kind == USB_MOUSE)

            ret = usb_mouse_poll(s, data, length);

	else if (s->kind == USB_TABLET)

            ret = usb_tablet_poll(s, data, length);

        else if (s->kind == USB_KEYBOARD)

            ret = usb_keyboard_poll(&s->kbd, data, length);

        break;

    case SET_REPORT:

        if (s->kind == USB_KEYBOARD)

            ret = usb_keyboard_write(&s->kbd, data, length);

        else

            goto fail;

        break;

    case GET_PROTOCOL:

        if (s->kind != USB_KEYBOARD)

            goto fail;

        ret = 1;

        data[0] = s->protocol;

        break;

    case SET_PROTOCOL:

        if (s->kind != USB_KEYBOARD)

            goto fail;

        ret = 0;

        s->protocol = value;

        break;

    case GET_IDLE:

        ret = 1;

        data[0] = s->idle;

        break;

    case SET_IDLE:

        s->idle = (uint8_t) (value >> 8);

        usb_hid_set_next_idle(s, qemu_get_clock(vm_clock));

        ret = 0;

        break;

    default:

    fail:

        ret = USB_RET_STALL;

        break;

    }

    return ret;

}


static int protocol_client_auth(VncState *vs, char *data, size_t len)

{

    /* We only advertise 1 auth scheme at a time, so client

     * must pick the one we sent. Verify this */

    if (data[0] != vs->auth) { /* Reject auth */

       VNC_DEBUG("Reject auth %d\n", (int)data[0]);

       vnc_write_u32(vs, 1);

       if (vs->minor >= 8) {

           static const char err[] = "Authentication failed";

           vnc_write_u32(vs, sizeof(err));

           vnc_write(vs, err, sizeof(err));

       }

       vnc_client_error(vs);

    } else { /* Accept requested auth */

       VNC_DEBUG("Client requested auth %d\n", (int)data[0]);

       switch (vs->auth) {

       case VNC_AUTH_NONE:

           VNC_DEBUG("Accept auth none\n");

           vnc_write_u32(vs, 0); /* Accept auth completion */

           vnc_read_when(vs, protocol_client_init, 1);

           break;

       case VNC_AUTH_VNC:

           VNC_DEBUG("Start VNC auth\n");

           return start_auth_vnc(vs);

#if CONFIG_VNC_TLS

       case VNC_AUTH_VENCRYPT:

           VNC_DEBUG("Accept VeNCrypt auth\n");;

           return start_auth_vencrypt(vs);

#endif /* CONFIG_VNC_TLS */

       default: /* Should not be possible, but just in case */

           VNC_DEBUG("Reject auth %d\n", vs->auth);

           vnc_write_u8(vs, 1);

           if (vs->minor >= 8) {

               static const char err[] = "Authentication failed";

               vnc_write_u32(vs, sizeof(err));

               vnc_write(vs, err, sizeof(err));

           }

           vnc_client_error(vs);

       }

    }

    return 0;

}


static int write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,

                            int phdr_index, target_phys_addr_t offset)

{

    Elf32_Phdr phdr;

    int ret;

    int endian = s->dump_info.d_endian;

    memset(&phdr, 0, sizeof(Elf32_Phdr));

    phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian);

    phdr.p_offset = cpu_convert_to_target32(offset, endian);

    phdr.p_paddr = cpu_convert_to_target32(memory_mapping->phys_addr, endian);

    if (offset == -1) {

        /* When the memory is not stored into vmcore, offset will be -1 */

        phdr.p_filesz = 0;

    } else {

        phdr.p_filesz = cpu_convert_to_target32(memory_mapping->length, endian);

    }

    phdr.p_memsz = cpu_convert_to_target32(memory_mapping->length, endian);

    phdr.p_vaddr = cpu_convert_to_target32(memory_mapping->virt_addr, endian);

    ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);

    if (ret < 0) {

        dump_error(s, "dump: failed to write program header table.\n");

        return -1;

    }

    return 0;

}


static void rtas_nvram_fetch(PowerPCCPU *cpu, sPAPREnvironment *spapr,

                             uint32_t token, uint32_t nargs,

                             target_ulong args,

                             uint32_t nret, target_ulong rets)

{

    sPAPRNVRAM *nvram = spapr->nvram;

    hwaddr offset, buffer, len;

    int alen;

    void *membuf;

    if ((nargs != 3) || (nret != 2)) {

        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);

        return;

    }

    if (!nvram) {

        rtas_st(rets, 0, RTAS_OUT_HW_ERROR);

        rtas_st(rets, 1, 0);

        return;

    }

    offset = rtas_ld(args, 0);

    buffer = rtas_ld(args, 1);

    len = rtas_ld(args, 2);

    if (((offset + len) < offset)

        || ((offset + len) > nvram->size)) {

        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);

        rtas_st(rets, 1, 0);

        return;

    }

    membuf = cpu_physical_memory_map(buffer, &len, 1);

    if (nvram->drive) {

        alen = bdrv_pread(nvram->drive, offset, membuf, len);

    } else {

        assert(nvram->buf);

        memcpy(membuf, nvram->buf + offset, len);

        alen = len;

    }

    cpu_physical_memory_unmap(membuf, len, 1, len);

    rtas_st(rets, 0, (alen < len) ? RTAS_OUT_HW_ERROR : RTAS_OUT_SUCCESS);

    rtas_st(rets, 1, (alen < 0) ? 0 : alen);

}


void address_space_init(AddressSpace *as, MemoryRegion *root)

{

    memory_region_transaction_begin();

    as->root = root;

    as->current_map = g_new(FlatView, 1);

    flatview_init(as->current_map);

    as->ioeventfd_nb = 0;

    as->ioeventfds = NULL;

    QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);

    as->name = NULL;

    memory_region_transaction_commit();

    address_space_init_dispatch(as);

}


static int virtio_net_init_pci(PCIDevice *pci_dev)

{

    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

    VirtIODevice *vdev;

    vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);

    vdev->nvectors = proxy->nvectors;

    virtio_init_pci(proxy, vdev,

                    PCI_VENDOR_ID_REDHAT_QUMRANET,

                    PCI_DEVICE_ID_VIRTIO_NET,

                    PCI_CLASS_NETWORK_ETHERNET,

                    0x00);

    /* make the actual value visible */

    proxy->nvectors = vdev->nvectors;

    return 0;

}


static void z2_init(MachineState *machine)

{

    const char *cpu_model = machine->cpu_model;

    const char *kernel_filename = machine->kernel_filename;

    const char *kernel_cmdline = machine->kernel_cmdline;

    const char *initrd_filename = machine->initrd_filename;

    MemoryRegion *address_space_mem = get_system_memory();

    uint32_t sector_len = 0x10000;

    PXA2xxState *mpu;

    DriveInfo *dinfo;

    int be;

    void *z2_lcd;

    I2CBus *bus;

    DeviceState *wm;

    if (!cpu_model) {

        cpu_model = "pxa270-c5";

    }

    /* Setup CPU & memory */

    mpu = pxa270_init(address_space_mem, z2_binfo.ram_size, cpu_model);

#ifdef TARGET_WORDS_BIGENDIAN

    be = 1;

#else

    be = 0;

#endif

    dinfo = drive_get(IF_PFLASH, 0, 0);

    if (!dinfo && !qtest_enabled()) {

        fprintf(stderr, "Flash image must be given with the "

                "'pflash' parameter\n");

        exit(1);

    }

    if (!pflash_cfi01_register(Z2_FLASH_BASE,

                               NULL, "z2.flash0", Z2_FLASH_SIZE,

                               dinfo ? blk_bs(blk_by_legacy_dinfo(dinfo)) : NULL,

                               sector_len, Z2_FLASH_SIZE / sector_len,

                               4, 0, 0, 0, 0, be)) {

        fprintf(stderr, "qemu: Error registering flash memory.\n");

        exit(1);

    }

    /* setup keypad */

    pxa27x_register_keypad(mpu->kp, map, 0x100);

    /* MMC/SD host */

    pxa2xx_mmci_handlers(mpu->mmc,

        NULL,

        qdev_get_gpio_in(mpu->gpio, Z2_GPIO_SD_DETECT));

    type_register_static(&zipit_lcd_info);

    type_register_static(&aer915_info);

    z2_lcd = ssi_create_slave(mpu->ssp[1], "zipit-lcd");

    bus = pxa2xx_i2c_bus(mpu->i2c[0]);

    i2c_create_slave(bus, TYPE_AER915, 0x55);

    wm = i2c_create_slave(bus, "wm8750", 0x1b);

    mpu->i2s->opaque = wm;

    mpu->i2s->codec_out = wm8750_dac_dat;

    mpu->i2s->codec_in = wm8750_adc_dat;

    wm8750_data_req_set(wm, mpu->i2s->data_req, mpu->i2s);

    qdev_connect_gpio_out(mpu->gpio, Z2_GPIO_LCD_CS,

                          qemu_allocate_irq(z2_lcd_cs, z2_lcd, 0));

    z2_binfo.kernel_filename = kernel_filename;

    z2_binfo.kernel_cmdline = kernel_cmdline;

    z2_binfo.initrd_filename = initrd_filename;

    z2_binfo.board_id = 0x6dd;

    arm_load_kernel(mpu->cpu, &z2_binfo);

}


static int stdio_fclose(void *opaque)

{

    QEMUFileStdio *s = opaque;

    int ret = 0;

    if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {

        int fd = fileno(s->stdio_file);

        struct stat st;

        ret = fstat(fd, &st);

        if (ret == 0 && S_ISREG(st.st_mode)) {

            /*

             * If the file handle is a regular file make sure the

             * data is flushed to disk before signaling success.

             */

            ret = fsync(fd);

            if (ret != 0) {

                ret = -errno;

                return ret;

            }

        }

    }

    if (fclose(s->stdio_file) == EOF) {

        ret = -errno;

    }

    g_free(s);

    return ret;

}


static int decode_cce(AACContext * ac, GetBitContext * gb, ChannelElement * che) {

    int num_gain = 0;

    int c, g, sfb, ret, idx = 0;

    int sign;

    float scale;

    SingleChannelElement * sce = &che->ch[0];

    ChannelCoupling * coup     = &che->coup;

    coup->coupling_point = 2*get_bits1(gb);

    coup->num_coupled = get_bits(gb, 3);

    for (c = 0; c <= coup->num_coupled; c++) {

        num_gain++;

        coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;

        coup->id_select[c] = get_bits(gb, 4);

        if (coup->type[c] == TYPE_CPE) {

            coup->ch_select[c] = get_bits(gb, 2);

            if (coup->ch_select[c] == 3)

                num_gain++;

        } else

            coup->ch_select[c] = 1;

    }

    coup->coupling_point += get_bits1(gb);

    if (coup->coupling_point == 2) {

        av_log(ac->avccontext, AV_LOG_ERROR,

            "Independently switched CCE with 'invalid' domain signalled.\n");

        memset(coup, 0, sizeof(ChannelCoupling));

        return -1;

    }

    sign = get_bits(gb, 1);

    scale = pow(2., pow(2., (int)get_bits(gb, 2) - 3));

    if ((ret = decode_ics(ac, sce, gb, 0, 0)))

        return ret;

    for (c = 0; c < num_gain; c++) {

        int cge = 1;

        int gain = 0;

        float gain_cache = 1.;

        if (c) {

            cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);

            gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;

            gain_cache = pow(scale, gain);

        }

        for (g = 0; g < sce->ics.num_window_groups; g++) {

            for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {

                if (sce->band_type[idx] != ZERO_BT) {

                    if (!cge) {

                        int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;

                        if (t) {

                            int s = 1;

                            if (sign) {

                                s  -= 2 * (t & 0x1);

                                t >>= 1;

                            }

                            gain += t;

                            gain_cache = pow(scale, gain) * s;

                        }

                    }

                    coup->gain[c][idx] = gain_cache;

                }

            }

        }

    }

    return 0;

}


void ff_avg_dirac_pixels32_sse2(uint8_t *dst, const uint8_t *src[5], int stride, int h)

{

    if (h&3) {

        ff_avg_dirac_pixels32_c(dst, src, stride, h);

    } else {

    ff_avg_pixels16_sse2(dst   , src[0]   , stride, h);

    ff_avg_pixels16_sse2(dst+16, src[0]+16, stride, h);

    }

}


static int ra288_decode_frame(AVCodecContext * avctx, void *data,

                              int *data_size, const uint8_t * buf,

                              int buf_size)

{

    int16_t *out = data;

    int i, j;

    RA288Context *ractx = avctx->priv_data;

    GetBitContext gb;

    if (buf_size < avctx->block_align) {

        av_log(avctx, AV_LOG_ERROR,

               "Error! Input buffer is too small [%d<%d]\n",

               buf_size, avctx->block_align);

        return 0;

    }

    init_get_bits(&gb, buf, avctx->block_align * 8);

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

        float gain = amptable[get_bits(&gb, 3)];

        int cb_coef = get_bits(&gb, 6 + (i&1));

        decode(ractx, gain, cb_coef);

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

            *(out++) = 8 * ractx->sp_block[4 - j];

        if ((i & 7) == 3)

            backward_filter(ractx);

    }

    *data_size = (char *)out - (char *)data;

    return avctx->block_align;

}


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

                                ptrdiff_t stride)

{

    avc_luma_hv_qrt_and_aver_dst_8x8_msa(src - 2,

                                         src - (stride * 2) +

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

}


static void output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)

{

    int ret = 0;

    /* apply the output bitstream filters, if any */

    if (ost->nb_bitstream_filters) {

        int idx;

        ret = av_bsf_send_packet(ost->bsf_ctx[0], pkt);

        if (ret < 0)

            goto finish;

        idx = 1;

        while (idx) {

            /* get a packet from the previous filter up the chain */

            ret = av_bsf_receive_packet(ost->bsf_ctx[idx - 1], pkt);

            if (ret == AVERROR(EAGAIN)) {

                ret = 0;

                idx--;

                continue;

            } else if (ret < 0)

                goto finish;

            /* HACK! - aac_adtstoasc updates extradata after filtering the first frame when

             * the api states this shouldn't happen after init(). Propagate it here to the

             * muxer and to the next filters in the chain to workaround this.

             * TODO/FIXME - Make aac_adtstoasc use new packet side data instead of changing

             * par_out->extradata and adapt muxers accordingly to get rid of this. */

            if (!(ost->bsf_extradata_updated[idx - 1] & 1)) {

                ret = avcodec_parameters_copy(ost->st->codecpar, ost->bsf_ctx[idx - 1]->par_out);

                if (ret < 0)

                    goto finish;

                ost->bsf_extradata_updated[idx - 1] |= 1;

            }

            /* send it to the next filter down the chain or to the muxer */

            if (idx < ost->nb_bitstream_filters) {

                /* HACK/FIXME! - See above */

                if (!(ost->bsf_extradata_updated[idx] & 2)) {

                    ret = avcodec_parameters_copy(ost->bsf_ctx[idx]->par_out, ost->bsf_ctx[idx - 1]->par_out);

                    if (ret < 0)

                        goto finish;

                    ost->bsf_extradata_updated[idx] |= 2;

                }

                ret = av_bsf_send_packet(ost->bsf_ctx[idx], pkt);

                if (ret < 0)

                    goto finish;

                idx++;

            } else

                write_packet(of, pkt, ost);

        }

    } else

        write_packet(of, pkt, ost);

finish:

    if (ret < 0 && ret != AVERROR_EOF) {

        av_log(NULL, AV_LOG_ERROR, "Error applying bitstream filters to an output "

               "packet for stream #%d:%d.\n", ost->file_index, ost->index);

        if(exit_on_error)

            exit_program(1);

    }

}


static int decode_frame(AVCodecContext *avctx,

                             void *data, int *data_size,

                             uint8_t *buf, int buf_size)

{

    H264Context *h = avctx->priv_data;

    MpegEncContext *s = &h->s;

    AVFrame *pict = data;

    float new_aspect;

    int buf_index;

    s->flags= avctx->flags;

    *data_size = 0;

   /* no supplementary picture */

    if (buf_size == 0) {

        return 0;

    }

    if(s->flags&CODEC_FLAG_TRUNCATED){

        int next= find_frame_end(s, buf, buf_size);

        if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )

            return buf_size;

//printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);

    }

    if(s->avctx->extradata_size && s->picture_number==0){

        if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) )

            return -1;

    }

    buf_index=decode_nal_units(h, buf, buf_size);

    if(buf_index < 0)

        return -1;

    //FIXME do something with unavailable reference frames

//    if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_index, buf_size);

#if 0

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

        *pict= *(AVFrame*)&s->current_picture;

    } else {

        *pict= *(AVFrame*)&s->last_picture;

    }

#endif

    *pict= *(AVFrame*)&s->current_picture; //FIXME

    assert(pict->data[0]);

//printf("out %d\n", (int)pict->data[0]);

    if(avctx->debug&FF_DEBUG_QP){

        int8_t *qtab= pict->qscale_table;

        int x,y;

        for(y=0; y<s->mb_height; y++){

            for(x=0; x<s->mb_width; x++){

                printf("%2d ", qtab[x + y*s->mb_width]);

            }

            printf("\n");

        }

        printf("\n");

    }

#if 0 //?

    /* Return the Picture timestamp as the frame number */

    /* we substract 1 because it is added on utils.c    */

    avctx->frame_number = s->picture_number - 1;

#endif

#if 0

    /* dont output the last pic after seeking */

    if(s->last_picture_ptr || s->low_delay)

    //Note this isnt a issue as a IDR pic should flush teh buffers

#endif

        *data_size = sizeof(AVFrame);

    return get_consumed_bytes(s, buf_index, buf_size);

}


static int slice_decode_thread(AVCodecContext *c, void *arg){

    MpegEncContext *s= *(void**)arg;

    const uint8_t *buf= s->gb.buffer;

    int mb_y= s->start_mb_y;

    const int field_pic= s->picture_structure != PICT_FRAME;

    s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;

    for(;;){

        uint32_t start_code;

        int ret;

        ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);

        emms_c();

//av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",

//ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);

        if(ret < 0){

            if(s->resync_mb_x>=0 && s->resync_mb_y>=0)

                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);

        }else{

            ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);

        }

        if(s->mb_y == s->end_mb_y)

            return 0;

        start_code= -1;

        buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);

        mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;

        if (s->picture_structure == PICT_BOTTOM_FIELD)

            mb_y++;

        if(mb_y < 0 || mb_y >= s->end_mb_y)

            return -1;

    }

    return 0; //not reached

}


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

                                 ptrdiff_t stride)

{

    avc_luma_midh_qrt_16w_msa(src - (2 * stride) - 2,

                              stride, dst, stride, 16, 0);

}


static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {

    int i;

    const int index_a = qp + h->slice_alpha_c0_offset;

    const int alpha = (alpha_table+52)[index_a];

    const int beta  = (beta_table+52)[qp + h->slice_beta_offset];

    if( bS[0] < 4 ) {

        int8_t tc[4];

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

            tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;

        h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);

    } else {

        h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);

    }

}


static int vmdk_open_vmdk4(BlockDriverState *bs,

                           BlockDriverState *file,

                           int flags, Error **errp)

{

    int ret;

    uint32_t magic;

    uint32_t l1_size, l1_entry_sectors;

    VMDK4Header header;

    VmdkExtent *extent;

    BDRVVmdkState *s = bs->opaque;

    int64_t l1_backup_offset = 0;

    ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));

    if (ret < 0) {

        error_setg_errno(errp, -ret,

                         "Could not read header from file '%s'",

                         file->filename);

        return -EINVAL;

    }

    if (header.capacity == 0) {

        uint64_t desc_offset = le64_to_cpu(header.desc_offset);

        if (desc_offset) {

            char *buf = vmdk_read_desc(file, desc_offset << 9, errp);

            if (!buf) {

                return -EINVAL;

            }

            ret = vmdk_open_desc_file(bs, flags, buf, errp);

            g_free(buf);

            return ret;

        }

    }

    if (!s->create_type) {

        s->create_type = g_strdup("monolithicSparse");

    }

    if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {

        /*

         * The footer takes precedence over the header, so read it in. The

         * footer starts at offset -1024 from the end: One sector for the

         * footer, and another one for the end-of-stream marker.

         */

        struct {

            struct {

                uint64_t val;

                uint32_t size;

                uint32_t type;

                uint8_t pad[512 - 16];

            } QEMU_PACKED footer_marker;

            uint32_t magic;

            VMDK4Header header;

            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];

            struct {

                uint64_t val;

                uint32_t size;

                uint32_t type;

                uint8_t pad[512 - 16];

            } QEMU_PACKED eos_marker;

        } QEMU_PACKED footer;

        ret = bdrv_pread(file,

            bs->file->total_sectors * 512 - 1536,

            &footer, sizeof(footer));

        if (ret < 0) {

            error_setg_errno(errp, -ret, "Failed to read footer");

            return ret;

        }

        /* Some sanity checks for the footer */

        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||

            le32_to_cpu(footer.footer_marker.size) != 0  ||

            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||

            le64_to_cpu(footer.eos_marker.val) != 0  ||

            le32_to_cpu(footer.eos_marker.size) != 0  ||

            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)

        {

            error_setg(errp, "Invalid footer");

            return -EINVAL;

        }

        header = footer.header;

    }

    if (le32_to_cpu(header.version) > 3) {

        char buf[64];

        snprintf(buf, sizeof(buf), "VMDK version %" PRId32,

                 le32_to_cpu(header.version));

        error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,

                  bdrv_get_device_or_node_name(bs), "vmdk", buf);

        return -ENOTSUP;

    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {

        /* VMware KB 2064959 explains that version 3 added support for

         * persistent changed block tracking (CBT), and backup software can

         * read it as version=1 if it doesn't care about the changed area

         * information. So we are safe to enable read only. */

        error_setg(errp, "VMDK version 3 must be read only");

        return -EINVAL;

    }

    if (le32_to_cpu(header.num_gtes_per_gt) > 512) {

        error_setg(errp, "L2 table size too big");

        return -EINVAL;

    }

    l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)

                        * le64_to_cpu(header.granularity);

    if (l1_entry_sectors == 0) {

        error_setg(errp, "L1 entry size is invalid");

        return -EINVAL;

    }

    l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)

                / l1_entry_sectors;

    if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {

        l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;

    }

    if (bdrv_nb_sectors(file) < le64_to_cpu(header.grain_offset)) {

        error_setg(errp, "File truncated, expecting at least %" PRId64 " bytes",

                   (int64_t)(le64_to_cpu(header.grain_offset)

                             * BDRV_SECTOR_SIZE));

        return -EINVAL;

    }

    ret = vmdk_add_extent(bs, file, false,

                          le64_to_cpu(header.capacity),

                          le64_to_cpu(header.gd_offset) << 9,

                          l1_backup_offset,

                          l1_size,

                          le32_to_cpu(header.num_gtes_per_gt),

                          le64_to_cpu(header.granularity),

                          &extent,

                          errp);

    if (ret < 0) {

        return ret;

    }

    extent->compressed =

        le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;

    if (extent->compressed) {

        g_free(s->create_type);

        s->create_type = g_strdup("streamOptimized");

    }

    extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;

    extent->version = le32_to_cpu(header.version);

    extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;

    ret = vmdk_init_tables(bs, extent, errp);

    if (ret) {

        /* free extent allocated by vmdk_add_extent */

        vmdk_free_last_extent(bs);

    }

    return ret;

}


dma_write(void *opaque, target_phys_addr_t addr,

	  uint64_t val64, unsigned int size)

{

        struct fs_dma_ctrl *ctrl = opaque;

	uint32_t value = val64;

	int c;

	if (size != 4) {

		dma_winvalid(opaque, addr, value);

	}

        /* Make addr relative to this channel and bounded to nr regs.  */

	c = fs_channel(addr);

        addr &= 0xff;

        addr >>= 2;

        switch (addr)

	{

		case RW_DATA:

			ctrl->channels[c].regs[addr] = value;

			break;

		case RW_CFG:

			ctrl->channels[c].regs[addr] = value;

			dma_update_state(ctrl, c);

			break;

		case RW_CMD:

			/* continue.  */

			if (value & ~1)

				printf("Invalid store to ch=%d RW_CMD %x\n",

				       c, value);

			ctrl->channels[c].regs[addr] = value;

			channel_continue(ctrl, c);

			break;

		case RW_SAVED_DATA:

		case RW_SAVED_DATA_BUF:

		case RW_GROUP:

		case RW_GROUP_DOWN:

			ctrl->channels[c].regs[addr] = value;

			break;

		case RW_ACK_INTR:

		case RW_INTR_MASK:

			ctrl->channels[c].regs[addr] = value;

			channel_update_irq(ctrl, c);

			if (addr == RW_ACK_INTR)

				ctrl->channels[c].regs[RW_ACK_INTR] = 0;

			break;

		case RW_STREAM_CMD:

			if (value & ~1023)

				printf("Invalid store to ch=%d "

				       "RW_STREAMCMD %x\n",

				       c, value);

			ctrl->channels[c].regs[addr] = value;

			D(printf("stream_cmd ch=%d\n", c));

			channel_stream_cmd(ctrl, c, value);

			break;

	        default:

			D(printf ("%s c=%d " TARGET_FMT_plx "\n",

				__func__, c, addr));

			break;

        }

}


static int scsi_generic_initfn(SCSIDevice *dev)

{

    SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, dev);

    int sg_version;

    struct sg_scsi_id scsiid;

    if (!s->qdev.conf.dinfo || !s->qdev.conf.dinfo->bdrv) {

        error_report("scsi-generic: drive property not set");

        return -1;

    }

    s->bs = s->qdev.conf.dinfo->bdrv;

    /* check we are really using a /dev/sg* file */

    if (!bdrv_is_sg(s->bs)) {

        error_report("scsi-generic: not /dev/sg*");

        return -1;

    }

    /* check we are using a driver managing SG_IO (version 3 and after */

    if (bdrv_ioctl(s->bs, SG_GET_VERSION_NUM, &sg_version) < 0 ||

        sg_version < 30000) {

        error_report("scsi-generic: scsi generic interface too old");

        return -1;

    }

    /* get LUN of the /dev/sg? */

    if (bdrv_ioctl(s->bs, SG_GET_SCSI_ID, &scsiid)) {

        error_report("scsi-generic: SG_GET_SCSI_ID ioctl failed");

        return -1;

    }

    /* define device state */

    s->lun = scsiid.lun;

    DPRINTF("LUN %d\n", s->lun);

    s->qdev.type = scsiid.scsi_type;

    DPRINTF("device type %d\n", s->qdev.type);

    if (s->qdev.type == TYPE_TAPE) {

        s->qdev.blocksize = get_stream_blocksize(s->bs);

        if (s->qdev.blocksize == -1)

            s->qdev.blocksize = 0;

    } else {

        s->qdev.blocksize = get_blocksize(s->bs);

        /* removable media returns 0 if not present */

        if (s->qdev.blocksize <= 0) {

            if (s->qdev.type == TYPE_ROM || s->qdev.type  == TYPE_WORM)

                s->qdev.blocksize = 2048;

            else

                s->qdev.blocksize = 512;

        }

    }

    DPRINTF("block size %d\n", s->qdev.blocksize);

    s->driver_status = 0;

    memset(s->sensebuf, 0, sizeof(s->sensebuf));

    return 0;

}


void memory_region_add_subregion_overlap(MemoryRegion *mr,

                                         hwaddr offset,

                                         MemoryRegion *subregion,

                                         unsigned priority)

{

    subregion->may_overlap = true;

    subregion->priority = priority;

    memory_region_add_subregion_common(mr, offset, subregion);

}


static int mpeg_decode_frame(AVCodecContext *avctx,

                             void *data, int *data_size,

                             const uint8_t *buf, int buf_size)

{

    Mpeg1Context *s = avctx->priv_data;

    AVFrame *picture = data;

    MpegEncContext *s2 = &s->mpeg_enc_ctx;

    dprintf(avctx, "fill_buffer\n");

    if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {

        /* special case for last picture */

        if (s2->low_delay==0 && s2->next_picture_ptr) {

            *picture= *(AVFrame*)s2->next_picture_ptr;

            s2->next_picture_ptr= NULL;

            *data_size = sizeof(AVFrame);

        }

        return buf_size;

    }

    if(s2->flags&CODEC_FLAG_TRUNCATED){

        int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);

        if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )

            return buf_size;

    }

#if 0

    if (s->repeat_field % 2 == 1) {

        s->repeat_field++;

        //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,

        //        s2->picture_number, s->repeat_field);

        if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {

            *data_size = sizeof(AVPicture);

            goto the_end;

        }

    }

#endif

    if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))

        vcr2_init_sequence(avctx);

    s->slice_count= 0;

    if(avctx->extradata && !avctx->frame_number)

        decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);

    return decode_chunks(avctx, picture, data_size, buf, buf_size);

}


void bdrv_parent_drained_begin(BlockDriverState *bs)

{

    BdrvChild *c;

    QLIST_FOREACH(c, &bs->parents, next_parent) {

        if (c->role->drained_begin) {

            c->role->drained_begin(c);

        }

    }

}


static int qemu_event_init(void)

{

    int err;

    int fds[2];

    err = qemu_eventfd(fds);

    if (err == -1) {

        return -errno;

    }

    err = fcntl_setfl(fds[0], O_NONBLOCK);

    if (err < 0) {

        goto fail;

    }

    err = fcntl_setfl(fds[1], O_NONBLOCK);

    if (err < 0) {

        goto fail;

    }

    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,

                         (void *)(intptr_t)fds[0]);

    io_thread_fd = fds[1];

    return 0;

fail:

    close(fds[0]);

    close(fds[1]);

    return err;

}


static void machine_set_kernel_irqchip(Object *obj, bool value, Error **errp)

{

    MachineState *ms = MACHINE(obj);

    ms->kernel_irqchip = value;

}


static char *pcibus_get_dev_path(DeviceState *dev)

{

    PCIDevice *d = container_of(dev, PCIDevice, qdev);

    PCIDevice *t;

    int slot_depth;

    /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.

     * 00 is added here to make this format compatible with

     * domain:Bus:Slot.Func for systems without nested PCI bridges.

     * Slot.Function list specifies the slot and function numbers for all

     * devices on the path from root to the specific device. */

    int domain_len = strlen("DDDD:00");

    int slot_len = strlen(":SS.F");

    int path_len;

    char *path, *p;

    /* Calculate # of slots on path between device and root. */;

    slot_depth = 0;

    for (t = d; t; t = t->bus->parent_dev) {

        ++slot_depth;

    }

    path_len = domain_len + slot_len * slot_depth;

    /* Allocate memory, fill in the terminating null byte. */

    path = malloc(path_len + 1 /* For '\0' */);

    path[path_len] = '\0';

    /* First field is the domain. */

    snprintf(path, domain_len, "%04x:00", pci_find_domain(d->bus));

    /* Fill in slot numbers. We walk up from device to root, so need to print

     * them in the reverse order, last to first. */

    p = path + path_len;

    for (t = d; t; t = t->bus->parent_dev) {

        p -= slot_len;

        snprintf(p, slot_len, ":%02x.%x", PCI_SLOT(t->devfn), PCI_FUNC(d->devfn));

    }

    return path;

}


int virtio_scsi_read_many(VDev *vdev,

                          ulong sector, void *load_addr, int sec_num)

{

    int sector_count;

    int f = vdev->blk_factor;

    unsigned int data_size;

    do {

        sector_count = MIN_NON_ZERO(sec_num, vdev->config.scsi.max_sectors);

        data_size = sector_count * virtio_get_block_size() * f;

        if (!scsi_read_10(vdev, sector * f, sector_count * f, load_addr,

                          data_size)) {

            virtio_scsi_verify_response(&resp, "virtio-scsi:read_many");

        }

        load_addr += data_size;

        sector += sector_count;

        sec_num -= sector_count;

    } while (sec_num > 0);

    return 0;

}


static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,

                                      uint32_t opt, uint32_t len, Error **errp)

{

    uint64_t magic;

    trace_nbd_negotiate_send_rep_len(opt, type, len);

    magic = cpu_to_be64(NBD_REP_MAGIC);

    if (nbd_write(ioc, &magic, sizeof(magic), errp) < 0) {

        error_prepend(errp, "write failed (rep magic): ");

        return -EINVAL;

    }

    opt = cpu_to_be32(opt);

    if (nbd_write(ioc, &opt, sizeof(opt), errp) < 0) {

        error_prepend(errp, "write failed (rep opt): ");

        return -EINVAL;

    }

    type = cpu_to_be32(type);

    if (nbd_write(ioc, &type, sizeof(type), errp) < 0) {

        error_prepend(errp, "write failed (rep type): ");

        return -EINVAL;

    }

    len = cpu_to_be32(len);

    if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {

        error_prepend(errp, "write failed (rep data length): ");

        return -EINVAL;

    }

    return 0;

}


uint16_t cpu_inw(pio_addr_t addr)

{

    uint16_t val;

    val = ioport_read(1, addr);

    trace_cpu_in(addr, val);

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

    return val;

}


static int local_chmod(FsContext *ctx, const char *path, mode_t mode)

{

    return chmod(rpath(ctx, path), mode);

}


int ff_wmv2_decode_picture_header(MpegEncContext * s)

{

    Wmv2Context * const w= (Wmv2Context*)s;

    int code;

#if 0

{

int i;

for(i=0; i<s->gb.size*8; i++)

    printf("%d", get_bits1(&s->gb));

//    get_bits1(&s->gb);

printf("END\n");

return -1;

}

#endif

    if(s->picture_number==0)

        decode_ext_header(w);

    s->pict_type = get_bits1(&s->gb) + 1;

    if(s->pict_type == I_TYPE){

        code = get_bits(&s->gb, 7);

        av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code);

    }

    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);

    if(s->qscale < 0)

       return -1;

    return 0;

}


static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s,

                              bool generate_data_write_guid, MSGUID *log_guid)

{

    int ret = 0;

    int hdr_idx = 0;

    uint64_t header_offset = VHDX_HEADER1_OFFSET;

    VHDXHeader *active_header;

    VHDXHeader *inactive_header;

    /* operate on the non-current header */

    if (s->curr_header == 0) {

        hdr_idx = 1;

        header_offset = VHDX_HEADER2_OFFSET;

    }

    active_header   = s->headers[s->curr_header];

    inactive_header = s->headers[hdr_idx];

    inactive_header->sequence_number = active_header->sequence_number + 1;

    /* a new file guid must be generated before any file write, including

     * headers */

    inactive_header->file_write_guid = s->session_guid;

    /* a new data guid only needs to be generated before any guest-visible

     * writes (i.e. something observable via virtual disk read) */

    if (generate_data_write_guid) {

        vhdx_guid_generate(&inactive_header->data_write_guid);

    }

    /* update the log guid if present */

    if (log_guid) {

        inactive_header->log_guid = *log_guid;

    }

    vhdx_write_header(bs->file, inactive_header, header_offset, true);

    if (ret < 0) {

        goto exit;

    }

    s->curr_header = hdr_idx;

exit:

    return ret;

}


static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,

				     int off_pitch, int bytesperline,

				     int lines)

{

    int y;

    int off_cur;

    int off_cur_end;

    if (off_pitch < 0) {

        off_begin -= bytesperline - 1;

    }

    for (y = 0; y < lines; y++) {

	off_cur = off_begin;

	off_cur_end = (off_cur + bytesperline) & s->cirrus_addr_mask;

        assert(off_cur_end >= off_cur);

        memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);

	off_begin += off_pitch;

    }

}


static void msmouse_event(void *opaque,

                          int dx, int dy, int dz, int buttons_state)

{

    CharDriverState *chr = (CharDriverState *)opaque;

    unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };

    /* Movement deltas */

    bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);

    bytes[1] |= MSMOUSE_LO6(dx);

    bytes[2] |= MSMOUSE_LO6(dy);

    /* Buttons */

    bytes[0] |= (buttons_state & 0x01 ? 0x20 : 0x00);

    bytes[0] |= (buttons_state & 0x02 ? 0x10 : 0x00);

    bytes[3] |= (buttons_state & 0x04 ? 0x20 : 0x00);

    /* We always send the packet of, so that we do not have to keep track

       of previous state of the middle button. This can potentially confuse

       some very old drivers for two button mice though. */

    qemu_chr_be_write(chr, bytes, 4);

}


static void gen_load(DisasContext *dc, TCGv dst, TCGv addr,

		     unsigned int size, int sign)

{

	int mem_index = cpu_mmu_index(dc->env);

	/* If we get a fault on a delayslot we must keep the jmp state in

	   the cpu-state to be able to re-execute the jmp.  */

	if (dc->delayed_branch == 1)

		cris_store_direct_jmp(dc);

	if (size == 1) {

		if (sign)

			tcg_gen_qemu_ld8s(dst, addr, mem_index);

		else

			tcg_gen_qemu_ld8u(dst, addr, mem_index);

	}

	else if (size == 2) {

		if (sign)

			tcg_gen_qemu_ld16s(dst, addr, mem_index);

		else

			tcg_gen_qemu_ld16u(dst, addr, mem_index);

	}

	else if (size == 4) {

		tcg_gen_qemu_ld32u(dst, addr, mem_index);

	}

	else if (size == 8) {

		tcg_gen_qemu_ld64(dst, addr, mem_index);

	}

}


static void pci_apb_iowritew (void *opaque, target_phys_addr_t addr,

                                  uint32_t val)

{

    cpu_outw(addr & IOPORTS_MASK, bswap16(val));

}


static struct ioreq *ioreq_start(struct XenBlkDev *blkdev)

{

    struct ioreq *ioreq = NULL;

    if (LIST_EMPTY(&blkdev->freelist)) {

	if (blkdev->requests_total >= max_requests)

	    goto out;

	/* allocate new struct */

	ioreq = qemu_mallocz(sizeof(*ioreq));

	ioreq->blkdev = blkdev;

	blkdev->requests_total++;

        qemu_iovec_init(&ioreq->v, BLKIF_MAX_SEGMENTS_PER_REQUEST);

    } else {

	/* get one from freelist */

	ioreq = LIST_FIRST(&blkdev->freelist);

	LIST_REMOVE(ioreq, list);

        qemu_iovec_reset(&ioreq->v);

    }

    LIST_INSERT_HEAD(&blkdev->inflight, ioreq, list);

    blkdev->requests_inflight++;

out:

    return ioreq;

}


static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds,

                         fd_set *wfds, fd_set *xfds)

{

    int i;

    for (i = 0; i < pollfds->len; i++) {

        GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);

        int fd = pfd->fd;

        int revents = 0;

        if (FD_ISSET(fd, rfds)) {

            revents |= G_IO_IN;

        }

        if (FD_ISSET(fd, wfds)) {

            revents |= G_IO_OUT;

        }

        if (FD_ISSET(fd, xfds)) {

            revents |= G_IO_PRI;

        }

        pfd->revents = revents & pfd->events;

    }

}


target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)

{

    return addr;

}


static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs)

{

    lhs->selector = rhs->selector;

    lhs->base = rhs->base;

    lhs->limit = rhs->limit;

    lhs->flags =

	(rhs->type << DESC_TYPE_SHIFT)

	| (rhs->present * DESC_P_MASK)

	| (rhs->dpl << DESC_DPL_SHIFT)

	| (rhs->db << DESC_B_SHIFT)

	| (rhs->s * DESC_S_MASK)

	| (rhs->l << DESC_L_SHIFT)

	| (rhs->g * DESC_G_MASK)

	| (rhs->avl * DESC_AVL_MASK);

}


static always_inline int get_bat (CPUState *env, mmu_ctx_t *ctx,

                                  target_ulong virtual, int rw, int type)

{

    target_ulong *BATlt, *BATut, *BATu, *BATl;

    target_ulong base, BEPIl, BEPIu, bl;

    int i, pp, pr;

    int ret = -1;

#if defined (DEBUG_BATS)

    if (loglevel != 0) {

        fprintf(logfile, "%s: %cBAT v 0x" ADDRX "\n", __func__,

                type == ACCESS_CODE ? 'I' : 'D', virtual);

    }

#endif

    pr = msr_pr;

    switch (type) {

    case ACCESS_CODE:

        BATlt = env->IBAT[1];

        BATut = env->IBAT[0];

        break;

    default:

        BATlt = env->DBAT[1];

        BATut = env->DBAT[0];

        break;

    }

#if defined (DEBUG_BATS)

    if (loglevel != 0) {

        fprintf(logfile, "%s...: %cBAT v 0x" ADDRX "\n", __func__,

                type == ACCESS_CODE ? 'I' : 'D', virtual);

    }

#endif

    base = virtual & 0xFFFC0000;

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

        BATu = &BATut[i];

        BATl = &BATlt[i];

        BEPIu = *BATu & 0xF0000000;

        BEPIl = *BATu & 0x0FFE0000;

        bl = (*BATu & 0x00001FFC) << 15;

#if defined (DEBUG_BATS)

        if (loglevel != 0) {

            fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX

                    " BATl 0x" ADDRX "\n",

                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,

                    *BATu, *BATl);

        }

#endif

        if ((virtual & 0xF0000000) == BEPIu &&

            ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {

            /* BAT matches */

            if (((pr == 0) && (*BATu & 0x00000002)) ||

                ((pr != 0) && (*BATu & 0x00000001))) {

                /* Get physical address */

                ctx->raddr = (*BATl & 0xF0000000) |

                    ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |

                    (virtual & 0x0001F000);

                /* Compute access rights */

                pp = *BATl & 0x00000003;

                ctx->prot = 0;

                if (pp != 0) {

                    ctx->prot = PAGE_READ | PAGE_EXEC;

                    if (pp == 0x2)

                        ctx->prot |= PAGE_WRITE;

                }

                ret = check_prot(ctx->prot, rw, type);

#if defined (DEBUG_BATS)

                if (ret == 0 && loglevel != 0) {

                    fprintf(logfile, "BAT %d match: r 0x" PADDRX

                            " prot=%c%c\n",

                            i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',

                            ctx->prot & PAGE_WRITE ? 'W' : '-');

                }

#endif

                break;

            }

        }

    }

    if (ret < 0) {

#if defined (DEBUG_BATS)

        if (loglevel != 0) {

            fprintf(logfile, "no BAT match for 0x" ADDRX ":\n", virtual);

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

                BATu = &BATut[i];

                BATl = &BATlt[i];

                BEPIu = *BATu & 0xF0000000;

                BEPIl = *BATu & 0x0FFE0000;

                bl = (*BATu & 0x00001FFC) << 15;

                fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX

                        " BATl 0x" ADDRX " \n\t"

                        "0x" ADDRX " 0x" ADDRX " 0x" ADDRX "\n",

                        __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,

                        *BATu, *BATl, BEPIu, BEPIl, bl);

            }

        }

#endif

    }

    /* No hit */

    return ret;

}


void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VP56RangeCoder *c,

                      VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9])

{

    int i, x, y, luma_start = 0, luma_ctx = 3;

    int nnz_pred, nnz, nnz_total = 0;

    int segment = mb->segment;

    int block_dc = 0;

    if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {

        nnz_pred = t_nnz[8] + l_nnz[8];

        // decode DC values and do hadamard

        nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0,

                                  nnz_pred, s->qmat[segment].luma_dc_qmul);

        l_nnz[8] = t_nnz[8] = !!nnz;

        if (nnz) {

            nnz_total += nnz;

            block_dc   = 1;

            if (nnz == 1)

                s->vp8dsp.vp8_luma_dc_wht_dc(td->block, td->block_dc);

            else

                s->vp8dsp.vp8_luma_dc_wht(td->block, td->block_dc);

        }

        luma_start = 1;

        luma_ctx   = 0;

    }

    // luma blocks

    for (y = 0; y < 4; y++)

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

            nnz_pred = l_nnz[y] + t_nnz[x];

            nnz = decode_block_coeffs(c, td->block[y][x],

                                      s->prob->token[luma_ctx],

                                      luma_start, nnz_pred,

                                      s->qmat[segment].luma_qmul);

            /* nnz+block_dc may be one more than the actual last index,

             * but we don't care */

            td->non_zero_count_cache[y][x] = nnz + block_dc;

            t_nnz[x] = l_nnz[y] = !!nnz;

            nnz_total += nnz;

        }

    // chroma blocks

    // TODO: what to do about dimensions? 2nd dim for luma is x,

    // but for chroma it's (y<<1)|x

    for (i = 4; i < 6; i++)

        for (y = 0; y < 2; y++)

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

                nnz_pred = l_nnz[i + 2 * y] + t_nnz[i + 2 * x];

                nnz      = decode_block_coeffs(c, td->block[i][(y << 1) + x],

                                               s->prob->token[2],

                                               0, nnz_pred,

                                               s->qmat[segment].chroma_qmul);

                td->non_zero_count_cache[i][(y << 1) + x] = nnz;

                t_nnz[i + 2 * x] = l_nnz[i + 2 * y] = !!nnz;

                nnz_total       += nnz;

            }

    // if there were no coded coeffs despite the macroblock not being marked skip,

    // we MUST not do the inner loop filter and should not do IDCT

    // Since skip isn't used for bitstream prediction, just manually set it.

    if (!nnz_total)

        mb->skip = 1;

}


static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)

{

    int predictor;

    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;

    predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;

    c->sample2 = c->sample1;

    c->sample1 = av_clip_int16(predictor);

    c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;

    if (c->idelta < 16) c->idelta = 16;

    return c->sample1;


static int __qemu_rdma_add_block(RDMAContext *rdma, void *host_addr,

                         ram_addr_t block_offset, uint64_t length)

{

    RDMALocalBlocks *local = &rdma->local_ram_blocks;

    RDMALocalBlock *block = g_hash_table_lookup(rdma->blockmap,

        (void *) block_offset);

    RDMALocalBlock *old = local->block;

    assert(block == NULL);

    local->block = g_malloc0(sizeof(RDMALocalBlock) * (local->nb_blocks + 1));

    if (local->nb_blocks) {

        int x;

        for (x = 0; x < local->nb_blocks; x++) {

            g_hash_table_remove(rdma->blockmap, (void *)old[x].offset);

            g_hash_table_insert(rdma->blockmap, (void *)old[x].offset,

                                                &local->block[x]);

        }

        memcpy(local->block, old, sizeof(RDMALocalBlock) * local->nb_blocks);

        g_free(old);

    }

    block = &local->block[local->nb_blocks];

    block->local_host_addr = host_addr;

    block->offset = block_offset;

    block->length = length;

    block->index = local->nb_blocks;

    block->nb_chunks = ram_chunk_index(host_addr, host_addr + length) + 1UL;

    block->transit_bitmap = bitmap_new(block->nb_chunks);

    bitmap_clear(block->transit_bitmap, 0, block->nb_chunks);

    block->unregister_bitmap = bitmap_new(block->nb_chunks);

    bitmap_clear(block->unregister_bitmap, 0, block->nb_chunks);

    block->remote_keys = g_malloc0(block->nb_chunks * sizeof(uint32_t));

    block->is_ram_block = local->init ? false : true;

    g_hash_table_insert(rdma->blockmap, (void *) block_offset, block);

    DDPRINTF("Added Block: %d, addr: %" PRIu64 ", offset: %" PRIu64

           " length: %" PRIu64 " end: %" PRIu64 " bits %" PRIu64 " chunks %d\n",

            local->nb_blocks, (uint64_t) block->local_host_addr, block->offset,

            block->length, (uint64_t) (block->local_host_addr + block->length),

                BITS_TO_LONGS(block->nb_chunks) *

                    sizeof(unsigned long) * 8, block->nb_chunks);

    local->nb_blocks++;

    return 0;

}


void ff_lzw_decode_tail(LZWState *p)

{

    struct LZWState *s = (struct LZWState *)p;

    while(!s->eob_reached)

        lzw_get_code(s);

}


static void x86_cpu_unrealizefn(DeviceState *dev, Error **errp)

{

    X86CPU *cpu = X86_CPU(dev);

#ifndef CONFIG_USER_ONLY

    cpu_remove_sync(CPU(dev));

    qemu_unregister_reset(x86_cpu_machine_reset_cb, dev);

#endif

    if (cpu->apic_state) {

        object_unparent(OBJECT(cpu->apic_state));

        cpu->apic_state = NULL;

    }

    xcc->parent_unrealize(dev, &local_err);

    if (local_err != NULL) {

        error_propagate(errp, local_err);

        return;

    }

}


static int net_tap_init(VLANState *vlan, const char *model,

                        const char *name, const char *ifname1,

                        const char *setup_script, const char *down_script)

{

    TAPState *s;

    int fd;

    char ifname[128];

    if (ifname1 != NULL)

        pstrcpy(ifname, sizeof(ifname), ifname1);

    else

        ifname[0] = '\0';

    TFR(fd = tap_open(ifname, sizeof(ifname)));

    if (fd < 0)

        return -1;

    if (!setup_script || !strcmp(setup_script, "no"))

        setup_script = "";

    if (setup_script[0] != '\0') {

	if (launch_script(setup_script, ifname, fd))

	    return -1;

    }

    s = net_tap_fd_init(vlan, model, name, fd);

    if (!s)

        return -1;

    snprintf(s->vc->info_str, sizeof(s->vc->info_str),

             "ifname=%s,script=%s,downscript=%s",

             ifname, setup_script, down_script);

    if (down_script && strcmp(down_script, "no")) {

        snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);

        snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);

    }

    return 0;

}


static void ide_test_start(const char *cmdline_fmt, ...)
{
    va_list ap;
    char *cmdline;
    va_start(ap, cmdline_fmt);
    cmdline = g_strdup_vprintf(cmdline_fmt, ap);
    va_end(ap);
    qtest_start(cmdline);
    qtest_irq_intercept_in(global_qtest, "ioapic");
    guest_malloc = pc_alloc_init();
}


static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)

{

    AVDictionary *new_params = NULL;

    AVDictionaryEntry *e, *cookie_entry;

    char *eql, *name;

    // ensure the cookie is parsable

    if (parse_set_cookie(p, &new_params))

        return -1;

    // if there is no cookie value there is nothing to parse

    cookie_entry = av_dict_get(new_params, "", NULL, AV_DICT_IGNORE_SUFFIX);

    if (!cookie_entry || !cookie_entry->value) {

        return -1;

    }

    // ensure the cookie is not expired or older than an existing value

    if ((e = av_dict_get(new_params, "expires", NULL, 0)) && e->value) {

        struct tm new_tm = {0};

        if (!parse_set_cookie_expiry_time(e->value, &new_tm)) {

            AVDictionaryEntry *e2;

            // if the cookie has already expired ignore it

            if (av_timegm(&new_tm) < av_gettime() / 1000000) {

                return -1;

            }

            // only replace an older cookie with the same name

            e2 = av_dict_get(*cookies, cookie_entry->key, NULL, 0);

            if (e2 && e2->value) {

                AVDictionary *old_params = NULL;

                if (!parse_set_cookie(p, &old_params)) {

                    e2 = av_dict_get(old_params, "expires", NULL, 0);

                    if (e2 && e2->value) {

                        struct tm old_tm = {0};

                        if (!parse_set_cookie_expiry_time(e->value, &old_tm)) {

                            if (av_timegm(&new_tm) < av_timegm(&old_tm)) {

                                av_dict_free(&old_params);

                                return -1;

                            }

                        }

                    }

                }

                av_dict_free(&old_params);

            }

        }

    }

    // duplicate the cookie name (dict will dupe the value)

    if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);

    if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);

    // add the cookie to the dictionary

    av_dict_set(cookies, name, eql, AV_DICT_DONT_STRDUP_KEY);

    return 0;

}


ISADevice *isa_ide_init(ISABus *bus, int iobase, int iobase2, int isairq,

                        DriveInfo *hd0, DriveInfo *hd1)

{

    DeviceState *dev;

    ISADevice *isadev;

    ISAIDEState *s;

    isadev = isa_create(bus, TYPE_ISA_IDE);

    dev = DEVICE(isadev);

    qdev_prop_set_uint32(dev, "iobase",  iobase);

    qdev_prop_set_uint32(dev, "iobase2", iobase2);

    qdev_prop_set_uint32(dev, "irq",     isairq);

    if (qdev_init(dev) < 0) {

        return NULL;

    }

    s = ISA_IDE(dev);

    if (hd0) {

        ide_create_drive(&s->bus, 0, hd0);

    }

    if (hd1) {

        ide_create_drive(&s->bus, 1, hd1);

    }

    return isadev;

}


int rx_produce(World *world, uint32_t pport,

               const struct iovec *iov, int iovcnt, uint8_t copy_to_cpu)

{

    Rocker *r = world_rocker(world);

    PCIDevice *dev = (PCIDevice *)r;

    DescRing *ring = rocker_get_rx_ring_by_pport(r, pport);

    DescInfo *info = desc_ring_fetch_desc(ring);

    char *data;

    size_t data_size = iov_size(iov, iovcnt);

    char *buf;

    uint16_t rx_flags = 0;

    uint16_t rx_csum = 0;

    size_t tlv_size;

    RockerTlv *tlvs[ROCKER_TLV_RX_MAX + 1];

    hwaddr frag_addr;

    uint16_t frag_max_len;

    int pos;

    int err;

    if (!info) {

        return -ROCKER_ENOBUFS;

    }

    buf = desc_get_buf(info, false);

    if (!buf) {

        err = -ROCKER_ENXIO;

        goto out;

    }

    rocker_tlv_parse(tlvs, ROCKER_TLV_RX_MAX, buf, desc_tlv_size(info));

    if (!tlvs[ROCKER_TLV_RX_FRAG_ADDR] ||

        !tlvs[ROCKER_TLV_RX_FRAG_MAX_LEN]) {

        err = -ROCKER_EINVAL;

        goto out;

    }

    frag_addr = rocker_tlv_get_le64(tlvs[ROCKER_TLV_RX_FRAG_ADDR]);

    frag_max_len = rocker_tlv_get_le16(tlvs[ROCKER_TLV_RX_FRAG_MAX_LEN]);

    if (data_size > frag_max_len) {

        err = -ROCKER_EMSGSIZE;

        goto out;

    }

    if (copy_to_cpu) {

        rx_flags |= ROCKER_RX_FLAGS_FWD_OFFLOAD;

    }

    /* XXX calc rx flags/csum */

    tlv_size = rocker_tlv_total_size(sizeof(uint16_t)) + /* flags */

               rocker_tlv_total_size(sizeof(uint16_t)) + /* scum */

               rocker_tlv_total_size(sizeof(uint64_t)) + /* frag addr */

               rocker_tlv_total_size(sizeof(uint16_t)) + /* frag max len */

               rocker_tlv_total_size(sizeof(uint16_t));  /* frag len */

    if (tlv_size > desc_buf_size(info)) {

        err = -ROCKER_EMSGSIZE;

        goto out;

    }

    /* TODO:

     * iov dma write can be optimized in similar way e1000 does it in

     * e1000_receive_iov. But maybe if would make sense to introduce

     * generic helper iov_dma_write.

     */

    data = g_malloc(data_size);

    if (!data) {

        err = -ROCKER_ENOMEM;

        goto out;

    }

    iov_to_buf(iov, iovcnt, 0, data, data_size);

    pci_dma_write(dev, frag_addr, data, data_size);

    g_free(data);

    pos = 0;

    rocker_tlv_put_le16(buf, &pos, ROCKER_TLV_RX_FLAGS, rx_flags);

    rocker_tlv_put_le16(buf, &pos, ROCKER_TLV_RX_CSUM, rx_csum);

    rocker_tlv_put_le64(buf, &pos, ROCKER_TLV_RX_FRAG_ADDR, frag_addr);

    rocker_tlv_put_le16(buf, &pos, ROCKER_TLV_RX_FRAG_MAX_LEN, frag_max_len);

    rocker_tlv_put_le16(buf, &pos, ROCKER_TLV_RX_FRAG_LEN, data_size);

    err = desc_set_buf(info, tlv_size);

out:

    if (desc_ring_post_desc(ring, err)) {

        rocker_msix_irq(r, ROCKER_MSIX_VEC_RX(pport - 1));

    }

    return err;

}


dshow_cycle_pins(AVFormatContext *avctx, enum dshowDeviceType devtype,

                 IBaseFilter *device_filter, IPin **ppin)

{

    struct dshow_ctx *ctx = avctx->priv_data;

    IEnumPins *pins = 0;

    IPin *device_pin = NULL;

    IPin *pin;

    int r;

    const GUID *mediatype[2] = { &MEDIATYPE_Video, &MEDIATYPE_Audio };

    const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";

    int set_format = (devtype == VideoDevice && (ctx->video_size || ctx->framerate))

                  || (devtype == AudioDevice && (ctx->channels || ctx->sample_rate));

    int format_set = 0;

    r = IBaseFilter_EnumPins(device_filter, &pins);

    if (r != S_OK) {

        av_log(avctx, AV_LOG_ERROR, "Could not enumerate pins.\n");

        return AVERROR(EIO);

    }

    if (!ppin) {

        av_log(avctx, AV_LOG_INFO, "DirectShow %s device options\n",

               devtypename);

    }

    while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !device_pin) {

        IKsPropertySet *p = NULL;

        IEnumMediaTypes *types = NULL;

        PIN_INFO info = {0};

        AM_MEDIA_TYPE *type;

        GUID category;

        DWORD r2;

        IPin_QueryPinInfo(pin, &info);

        IBaseFilter_Release(info.pFilter);

        if (info.dir != PINDIR_OUTPUT)

            goto next;

        if (IPin_QueryInterface(pin, &IID_IKsPropertySet, (void **) &p) != S_OK)

            goto next;

        if (IKsPropertySet_Get(p, &AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY,

                               NULL, 0, &category, sizeof(GUID), &r2) != S_OK)

            goto next;

        if (!IsEqualGUID(&category, &PIN_CATEGORY_CAPTURE))

            goto next;

        if (!ppin) {

            char *buf = dup_wchar_to_utf8(info.achName);

            av_log(avctx, AV_LOG_INFO, " Pin \"%s\"\n", buf);

            av_free(buf);

            dshow_cycle_formats(avctx, devtype, pin, NULL);

            goto next;

        }

        if (set_format) {

            dshow_cycle_formats(avctx, devtype, pin, &format_set);

            if (!format_set) {

                goto next;

            }

        }

        if (IPin_EnumMediaTypes(pin, &types) != S_OK)

            goto next;

        IEnumMediaTypes_Reset(types);

        while (IEnumMediaTypes_Next(types, 1, &type, NULL) == S_OK && !device_pin) {

            if (IsEqualGUID(&type->majortype, mediatype[devtype])) {

                device_pin = pin;

                goto next;

            }

            CoTaskMemFree(type);

        }

next:

        if (types)

            IEnumMediaTypes_Release(types);

        if (p)

            IKsPropertySet_Release(p);

        if (device_pin != pin)

            IPin_Release(pin);

    }

    IEnumPins_Release(pins);

    if (ppin) {

        if (set_format && !format_set) {

            av_log(avctx, AV_LOG_ERROR, "Could not set %s options\n", devtypename);

            return AVERROR(EIO);

        }

        if (!device_pin) {

            av_log(avctx, AV_LOG_ERROR,

                "Could not find output pin from %s capture device.\n", devtypename);

            return AVERROR(EIO);

        }

        *ppin = device_pin;

    }

    return 0;

}


static inline int yv12touyvy_unscaled_altivec(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,

     int srcSliceH, uint8_t* dstParam[], int dstStride_a[]) {

  uint8_t *dst=dstParam[0] + dstStride_a[0]*srcSliceY;

  // yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );

  uint8_t *ysrc = src[0];

  uint8_t *usrc = src[1];

  uint8_t *vsrc = src[2];

  const int width = c->srcW;

  const int height = srcSliceH;

  const int lumStride = srcStride[0];

  const int chromStride = srcStride[1];

  const int dstStride = dstStride_a[0];

  const int vertLumPerChroma = 2;

  const vector unsigned char yperm = vec_lvsl(0, ysrc);

  register unsigned int y;

  if(width&15){

    yv12touyvy( ysrc, usrc, vsrc, dst,c->srcW,srcSliceH, lumStride, chromStride, dstStride);

    return srcSliceH;

  }

  /* this code assume:

  1) dst is 16 bytes-aligned

  2) dstStride is a multiple of 16

  3) width is a multiple of 16

  4) lum&chrom stride are multiple of 8

  */

  for(y=0; y<height; y++)

    {

      int i;

      for (i = 0; i < width - 31; i+= 32) {

	const unsigned int j = i >> 1;

	vector unsigned char v_yA = vec_ld(i, ysrc);

	vector unsigned char v_yB = vec_ld(i + 16, ysrc);

	vector unsigned char v_yC = vec_ld(i + 32, ysrc);

	vector unsigned char v_y1 = vec_perm(v_yA, v_yB, yperm);

	vector unsigned char v_y2 = vec_perm(v_yB, v_yC, yperm);

	vector unsigned char v_uA = vec_ld(j, usrc);

	vector unsigned char v_uB = vec_ld(j + 16, usrc);

	vector unsigned char v_u = vec_perm(v_uA, v_uB, vec_lvsl(j, usrc));

	vector unsigned char v_vA = vec_ld(j, vsrc);

	vector unsigned char v_vB = vec_ld(j + 16, vsrc);

	vector unsigned char v_v = vec_perm(v_vA, v_vB, vec_lvsl(j, vsrc));

	vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);

	vector unsigned char v_uv_b = vec_mergel(v_u, v_v);

	vector unsigned char v_uyvy_0 = vec_mergeh(v_uv_a, v_y1);

	vector unsigned char v_uyvy_1 = vec_mergel(v_uv_a, v_y1);

	vector unsigned char v_uyvy_2 = vec_mergeh(v_uv_b, v_y2);

	vector unsigned char v_uyvy_3 = vec_mergel(v_uv_b, v_y2);

	vec_st(v_uyvy_0, (i << 1), dst);

	vec_st(v_uyvy_1, (i << 1) + 16, dst);

	vec_st(v_uyvy_2, (i << 1) + 32, dst);

	vec_st(v_uyvy_3, (i << 1) + 48, dst);

      }

      if (i < width) {

	const unsigned int j = i >> 1;

	vector unsigned char v_y1 = vec_ld(i, ysrc);

	vector unsigned char v_u = vec_ld(j, usrc);

	vector unsigned char v_v = vec_ld(j, vsrc);

	vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);

	vector unsigned char v_uyvy_0 = vec_mergeh(v_uv_a, v_y1);

	vector unsigned char v_uyvy_1 = vec_mergel(v_uv_a, v_y1);

	vec_st(v_uyvy_0, (i << 1), dst);

	vec_st(v_uyvy_1, (i << 1) + 16, dst);

      }

      if((y&(vertLumPerChroma-1))==(vertLumPerChroma-1) )

	{

	  usrc += chromStride;

	  vsrc += chromStride;

	}

      ysrc += lumStride;

      dst += dstStride;

    }

  return srcSliceH;

}


static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,

                                unsigned size)

{

    SerialState *s = opaque;

    addr &= 7;

    DPRINTF("write addr=0x%" HWADDR_PRIx " val=0x%" PRIx64 "\n", addr, val);

    switch(addr) {

    default:

    case 0:

        if (s->lcr & UART_LCR_DLAB) {

            s->divider = (s->divider & 0xff00) | val;

            serial_update_parameters(s);

        } else {

            s->thr = (uint8_t) val;

            if(s->fcr & UART_FCR_FE) {

                /* xmit overruns overwrite data, so make space if needed */

                if (fifo8_is_full(&s->xmit_fifo)) {

                    fifo8_pop(&s->xmit_fifo);

                }

                fifo8_push(&s->xmit_fifo, s->thr);

                s->lsr &= ~UART_LSR_TEMT;

            }

            s->thr_ipending = 0;

            s->lsr &= ~UART_LSR_THRE;

            serial_update_irq(s);

            serial_xmit(NULL, G_IO_OUT, s);

        }

        break;

    case 1:

        if (s->lcr & UART_LCR_DLAB) {

            s->divider = (s->divider & 0x00ff) | (val << 8);

            serial_update_parameters(s);

        } else {

            s->ier = val & 0x0f;

            /* If the backend device is a real serial port, turn polling of the modem

               status lines on physical port on or off depending on UART_IER_MSI state */

            if (s->poll_msl >= 0) {

                if (s->ier & UART_IER_MSI) {

                     s->poll_msl = 1;

                     serial_update_msl(s);

                } else {

                     timer_del(s->modem_status_poll);

                     s->poll_msl = 0;

                }

            }

            if (s->lsr & UART_LSR_THRE) {

                s->thr_ipending = 1;

                serial_update_irq(s);

            }

        }

        break;

    case 2:

        val = val & 0xFF;

        if (s->fcr == val)

            break;

        /* Did the enable/disable flag change? If so, make sure FIFOs get flushed */

        if ((val ^ s->fcr) & UART_FCR_FE)

            val |= UART_FCR_XFR | UART_FCR_RFR;

        /* FIFO clear */

        if (val & UART_FCR_RFR) {

            timer_del(s->fifo_timeout_timer);

            s->timeout_ipending=0;

            fifo8_reset(&s->recv_fifo);

        }

        if (val & UART_FCR_XFR) {

            fifo8_reset(&s->xmit_fifo);

        }

        if (val & UART_FCR_FE) {

            s->iir |= UART_IIR_FE;

            /* Set recv_fifo trigger Level */

            switch (val & 0xC0) {

            case UART_FCR_ITL_1:

                s->recv_fifo_itl = 1;

                break;

            case UART_FCR_ITL_2:

                s->recv_fifo_itl = 4;

                break;

            case UART_FCR_ITL_3:

                s->recv_fifo_itl = 8;

                break;

            case UART_FCR_ITL_4:

                s->recv_fifo_itl = 14;

                break;

            }

        } else

            s->iir &= ~UART_IIR_FE;

        /* Set fcr - or at least the bits in it that are supposed to "stick" */

        s->fcr = val & 0xC9;

        serial_update_irq(s);

        break;

    case 3:

        {

            int break_enable;

            s->lcr = val;

            serial_update_parameters(s);

            break_enable = (val >> 6) & 1;

            if (break_enable != s->last_break_enable) {

                s->last_break_enable = break_enable;

                qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK,

                               &break_enable);

            }

        }

        break;

    case 4:

        {

            int flags;

            int old_mcr = s->mcr;

            s->mcr = val & 0x1f;

            if (val & UART_MCR_LOOP)

                break;

            if (s->poll_msl >= 0 && old_mcr != s->mcr) {

                qemu_chr_fe_ioctl(s->chr,CHR_IOCTL_SERIAL_GET_TIOCM, &flags);

                flags &= ~(CHR_TIOCM_RTS | CHR_TIOCM_DTR);

                if (val & UART_MCR_RTS)

                    flags |= CHR_TIOCM_RTS;

                if (val & UART_MCR_DTR)

                    flags |= CHR_TIOCM_DTR;

                qemu_chr_fe_ioctl(s->chr,CHR_IOCTL_SERIAL_SET_TIOCM, &flags);

                /* Update the modem status after a one-character-send wait-time, since there may be a response

                   from the device/computer at the other end of the serial line */

                timer_mod(s->modem_status_poll, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time);

            }

        }

        break;

    case 5:

        break;

    case 6:

        break;

    case 7:

        s->scr = val;

        break;

    }

}


static void FUNC(put_hevc_qpel_bi_w_v)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,

                                       int16_t *src2,

                                       int height, int denom, int wx0, int wx1,

                                       int ox0, int ox1, intptr_t mx, intptr_t my, int width)

{

    int x, y;

    pixel        *src       = (pixel*)_src;

    ptrdiff_t     srcstride = _srcstride / sizeof(pixel);

    pixel *dst          = (pixel *)_dst;

    ptrdiff_t dststride = _dststride / sizeof(pixel);

    const int8_t *filter    = ff_hevc_qpel_filters[my - 1];

    int shift = 14 + 1 - BIT_DEPTH;

    int log2Wd = denom + shift - 1;

    ox0     = ox0 * (1 << (BIT_DEPTH - 8));

    ox1     = ox1 * (1 << (BIT_DEPTH - 8));

    for (y = 0; y < height; y++) {

        for (x = 0; x < width; x++)

            dst[x] = av_clip_pixel(((QPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) * wx1 + src2[x] * wx0 +

                                    ((ox0 + ox1 + 1) << log2Wd)) >> (log2Wd + 1));

        src  += srcstride;

        dst  += dststride;

        src2 += MAX_PB_SIZE;

    }

}


int spapr_ovec_populate_dt(void *fdt, int fdt_offset,

                           sPAPROptionVector *ov, const char *name)

{

    uint8_t vec[OV_MAXBYTES + 1];

    uint16_t vec_len;

    unsigned long lastbit;

    int i;

    g_assert(ov);

    lastbit = find_last_bit(ov->bitmap, OV_MAXBITS);

    /* if no bits are set, include at least 1 byte of the vector so we can

     * still encoded this in the device tree while abiding by the same

     * encoding/sizing expected in ibm,client-architecture-support

     */

    vec_len = (lastbit == OV_MAXBITS) ? 1 : lastbit / BITS_PER_BYTE + 1;

    g_assert_cmpint(vec_len, <=, OV_MAXBYTES);

    /* guest expects vector len encoded as vec_len - 1, since the length byte

     * is assumed and not included, and the first byte of the vector

     * is assumed as well

     */

    vec[0] = vec_len - 1;

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

        vec[i] = guest_byte_from_bitmap(ov->bitmap, (i - 1) * BITS_PER_BYTE);

        if (vec[i]) {

            DPRINTFN("encoding guest vector byte %3d / %3d: 0x%.2x",

                     i, vec_len, vec[i]);

        }

    }

    return fdt_setprop(fdt, fdt_offset, name, vec, vec_len);

}


static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)

{

    int isv34, tlen, unsync;

    char tag[5];

    int64_t next, end = avio_tell(s->pb) + len;

    int taghdrlen;

    const char *reason = NULL;

    AVIOContext pb;

    AVIOContext *pbx;

    unsigned char *buffer = NULL;

    int buffer_size = 0;

    void (*extra_func)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta**) = NULL;

    switch (version) {

    case 2:

        if (flags & 0x40) {

            reason = "compression";

            goto error;

        }

        isv34 = 0;

        taghdrlen = 6;

        break;

    case 3:

    case 4:

        isv34 = 1;

        taghdrlen = 10;

        break;

    default:

        reason = "version";

        goto error;

    }

    unsync = flags & 0x80;

    if (isv34 && flags & 0x40) /* Extended header present, just skip over it */

        avio_skip(s->pb, get_size(s->pb, 4));

    while (len >= taghdrlen) {

        unsigned int tflags = 0;

        int tunsync = 0;

        if (isv34) {

            avio_read(s->pb, tag, 4);

            tag[4] = 0;

            if(version==3){

                tlen = avio_rb32(s->pb);

            }else

                tlen = get_size(s->pb, 4);

            tflags = avio_rb16(s->pb);

            tunsync = tflags & ID3v2_FLAG_UNSYNCH;

        } else {

            avio_read(s->pb, tag, 3);

            tag[3] = 0;

            tlen = avio_rb24(s->pb);

        }

        if (tlen < 0 || tlen > len - taghdrlen) {

            av_log(s, AV_LOG_WARNING, "Invalid size in frame %s, skipping the rest of tag.\n", tag);

            break;

        }

        len -= taghdrlen + tlen;

        next = avio_tell(s->pb) + tlen;

        if (!tlen) {

            if (tag[0])

                av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);

            continue;

        }

        if (tflags & ID3v2_FLAG_DATALEN) {

            avio_rb32(s->pb);

            tlen -= 4;

        }

        if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {

            av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);

            avio_skip(s->pb, tlen);

        /* check for text tag or supported special meta tag */

        } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)->read))) {

            if (unsync || tunsync) {

                int i, j;

                av_fast_malloc(&buffer, &buffer_size, tlen);

                if (!buffer) {

                    av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);

                    goto seek;

                }

                for (i = 0, j = 0; i < tlen; i++, j++) {

                    buffer[j] = avio_r8(s->pb);

                    if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {

                        /* Unsynchronised byte, skip it */

                        j--;

                    }

                }

                ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);

                tlen = j;

                pbx = &pb; // read from sync buffer

            } else {

                pbx = s->pb; // read straight from input

            }

            if (tag[0] == 'T')

                /* parse text tag */

                read_ttag(s, pbx, tlen, tag);

            else

                /* parse special meta tag */

                extra_func(s, pbx, tlen, tag, extra_meta);

        }

        else if (!tag[0]) {

            if (tag[1])

                av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");

            avio_skip(s->pb, tlen);

            break;

        }

        /* Skip to end of tag */

seek:

        avio_seek(s->pb, next, SEEK_SET);

    }

    if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */

        end += 10;

  error:

    if (reason)

        av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);

    avio_seek(s->pb, end, SEEK_SET);

    av_free(buffer);

    return;

}


static int shorten_decode_frame(AVCodecContext *avctx,

        void *data, int *data_size,

        uint8_t *buf, int buf_size)

{

    ShortenContext *s = avctx->priv_data;

    int i, input_buf_size = 0;

    int16_t *samples = data;

    if(s->max_framesize == 0){

        s->max_framesize= 1024; // should hopefully be enough for the first header

        s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);

    }

    if(1 && s->max_framesize){//FIXME truncated

        buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);

        input_buf_size= buf_size;

        if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){

            //                printf("memmove\n");

            memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);

            s->bitstream_index=0;

        }

        memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);

        buf= &s->bitstream[s->bitstream_index];

        buf_size += s->bitstream_size;

        s->bitstream_size= buf_size;

        if(buf_size < s->max_framesize){

            //dprintf("wanna more data ... %d\n", buf_size);

            return input_buf_size;

        }

    }

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

    get_bits(&s->gb, s->bitindex);

    if (!s->blocksize)

    {

        int maxnlpc = 0;

        /* shorten signature */

        if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {

            av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");

            return -1;

        }

        s->lpcqoffset = 0;

        s->blocksize = DEFAULT_BLOCK_SIZE;

        s->channels = 1;

        s->nmean = -1;

        s->version = get_bits(&s->gb, 8);

        s->internal_ftype = get_uint(s, TYPESIZE);

        s->channels = get_uint(s, CHANSIZE);

        if (s->channels > MAX_CHANNELS) {

            av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);

            return -1;

        }

        /* get blocksize if version > 0 */

        if (s->version > 0) {

            int skip_bytes;

            s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));

            maxnlpc = get_uint(s, LPCQSIZE);

            s->nmean = get_uint(s, 0);

            skip_bytes = get_uint(s, NSKIPSIZE);

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

                skip_bits(&s->gb, 8);

            }

        }

        s->nwrap = FFMAX(NWRAP, maxnlpc);

        allocate_buffers(s);

        init_offset(s);

        if (s->version > 1)

            s->lpcqoffset = V2LPCQOFFSET;

        if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {

            av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at begining of stream\n");

            return -1;

        }

        s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);

        if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {

            av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);

            return -1;

        }

        for (i=0; i<s->header_size; i++)

            s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);

        if (decode_wave_header(avctx, s->header, s->header_size) < 0)

            return -1;

        s->cur_chan = 0;

        s->bitshift = 0;

    }

    else

    {

        int cmd;

        int len;

        cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);

        switch (cmd) {

            case FN_ZERO:

            case FN_DIFF0:

            case FN_DIFF1:

            case FN_DIFF2:

            case FN_DIFF3:

            case FN_QLPC:

                {

                    int residual_size = 0;

                    int channel = s->cur_chan;

                    int32_t coffset;

                    if (cmd != FN_ZERO) {

                        residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);

                        /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */

                        if (s->version == 0)

                            residual_size--;

                    }

                    if (s->nmean == 0)

                        coffset = s->offset[channel][0];

                    else {

                        int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;

                        for (i=0; i<s->nmean; i++)

                            sum += s->offset[channel][i];

                        coffset = sum / s->nmean;

                        if (s->version >= 2)

                            coffset >>= FFMIN(1, s->bitshift);

                    }

                    switch (cmd) {

                        case FN_ZERO:

                            for (i=0; i<s->blocksize; i++)

                                s->decoded[channel][i] = 0;

                            break;

                        case FN_DIFF0:

                            for (i=0; i<s->blocksize; i++)

                                s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;

                            break;

                        case FN_DIFF1:

                            for (i=0; i<s->blocksize; i++)

                                s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];

                            break;

                        case FN_DIFF2:

                            for (i=0; i<s->blocksize; i++)

                                s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]

                                                                                -   s->decoded[channel][i-2];

                            break;

                        case FN_DIFF3:

                            for (i=0; i<s->blocksize; i++)

                                s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]

                                                                                - 3*s->decoded[channel][i-2]

                                                                                +   s->decoded[channel][i-3];

                            break;

                        case FN_QLPC:

                            {

                                int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);

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

                                    s->decoded[channel][i - pred_order] -= coffset;

                                decode_subframe_lpc(s, channel, residual_size, pred_order);

                                if (coffset != 0)

                                    for (i=0; i < s->blocksize; i++)

                                        s->decoded[channel][i] += coffset;

                            }

                    }

                    if (s->nmean > 0) {

                        int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;

                        for (i=0; i<s->blocksize; i++)

                            sum += s->decoded[channel][i];

                        for (i=1; i<s->nmean; i++)

                            s->offset[channel][i-1] = s->offset[channel][i];

                        if (s->version < 2)

                            s->offset[channel][s->nmean - 1] = sum / s->blocksize;

                        else

                            s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;

                    }

                    for (i=-s->nwrap; i<0; i++)

                        s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];

                    fix_bitshift(s, s->decoded[channel]);

                    s->cur_chan++;

                    if (s->cur_chan == s->channels) {

                        samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);

                        s->cur_chan = 0;

                        goto frame_done;

                    }

                    break;

                }

                break;

            case FN_VERBATIM:

                len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);

                while (len--) {

                    get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);

                }

                break;

            case FN_BITSHIFT:

                s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);

                break;

            case FN_BLOCKSIZE:

                s->blocksize = get_uint(s, av_log2(s->blocksize));

                break;

            case FN_QUIT:

                return buf_size;

                break;

            default:

                av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);

                return -1;

                break;

        }

    }

frame_done:

    *data_size = (int8_t *)samples - (int8_t *)data;

    //    s->last_blocksize = s->blocksize;

    s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);

    i= (get_bits_count(&s->gb))/8;

    if (i > buf_size) {

        av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);

        s->bitstream_size=0;

        s->bitstream_index=0;

        return -1;

    }

    if (s->bitstream_size) {

        s->bitstream_index += i;

        s->bitstream_size  -= i;

        return input_buf_size;

    } else

        return i;

}


static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)

{

    int size = 0;

    const uint8_t *data;

    uint32_t flags;

    if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))

        return;

    data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);

    if (!data || size < 4)

        return;

    flags = bytestream_get_le32(&data);

    size -= 4;

    if (size < 4) /* Required for any of the changes */

        return;

    if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {

        avctx->channels = bytestream_get_le32(&data);

        size -= 4;

    }

    if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {

        if (size < 8)

            return;

        avctx->channel_layout = bytestream_get_le64(&data);

        size -= 8;

    }

    if (size < 4)

        return;

    if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {

        avctx->sample_rate = bytestream_get_le32(&data);

        size -= 4;

    }

    if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {

        if (size < 8)

            return;

        avctx->width  = bytestream_get_le32(&data);

        avctx->height = bytestream_get_le32(&data);

        avcodec_set_dimensions(avctx, avctx->width, avctx->height);

        size -= 8;

    }

}


static int net_client_init1(const void *object, int is_netdev, Error **errp)

{

    union {

        const Netdev    *netdev;

        const NetLegacy *net;

    } u;

    const NetClientOptions *opts;

    const char *name;

    if (is_netdev) {

        u.netdev = object;

        opts = u.netdev->opts;

        name = u.netdev->id;

        switch (opts->kind) {

#ifdef CONFIG_SLIRP

        case NET_CLIENT_OPTIONS_KIND_USER:

#endif

        case NET_CLIENT_OPTIONS_KIND_TAP:

        case NET_CLIENT_OPTIONS_KIND_SOCKET:

#ifdef CONFIG_VDE

        case NET_CLIENT_OPTIONS_KIND_VDE:

#endif

#ifdef CONFIG_NETMAP

        case NET_CLIENT_OPTIONS_KIND_NETMAP:

#endif

#ifdef CONFIG_NET_BRIDGE

        case NET_CLIENT_OPTIONS_KIND_BRIDGE:

#endif

        case NET_CLIENT_OPTIONS_KIND_HUBPORT:

#ifdef CONFIG_VHOST_NET_USED

        case NET_CLIENT_OPTIONS_KIND_VHOST_USER:

#endif

#ifdef CONFIG_LINUX

        case NET_CLIENT_OPTIONS_KIND_L2TPV3:

#endif

            break;

        default:

            error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",

                      "a netdev backend type");

            return -1;

        }

    } else {

        u.net = object;

        opts = u.net->opts;

        /* missing optional values have been initialized to "all bits zero" */

        name = u.net->has_id ? u.net->id : u.net->name;

    }

    if (net_client_init_fun[opts->kind]) {

        NetClientState *peer = NULL;

        /* Do not add to a vlan if it's a -netdev or a nic with a netdev=

         * parameter. */

        if (!is_netdev &&

            (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||

             !opts->nic->has_netdev)) {

            peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);

        }

        if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {

            /* TODO push error reporting into init() methods */

            error_set(errp, QERR_DEVICE_INIT_FAILED,

                      NetClientOptionsKind_lookup[opts->kind]);

            return -1;

        }

    }

    return 0;

}


bdrv_driver_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
                               uint64_t bytes, QEMUIOVector *qiov)
{
    BlockDriver *drv = bs->drv;
    if (!drv->bdrv_co_pwritev_compressed) {
        return -ENOTSUP;
    return drv->bdrv_co_pwritev_compressed(bs, offset, bytes, qiov);


static void cirrus_init_common(CirrusVGAState * s, int device_id, int is_pci)

{

    int i;

    static int inited;

    if (!inited) {

        inited = 1;

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

            rop_to_index[i] = CIRRUS_ROP_NOP_INDEX; /* nop rop */

        rop_to_index[CIRRUS_ROP_0] = 0;

        rop_to_index[CIRRUS_ROP_SRC_AND_DST] = 1;

        rop_to_index[CIRRUS_ROP_NOP] = 2;

        rop_to_index[CIRRUS_ROP_SRC_AND_NOTDST] = 3;

        rop_to_index[CIRRUS_ROP_NOTDST] = 4;

        rop_to_index[CIRRUS_ROP_SRC] = 5;

        rop_to_index[CIRRUS_ROP_1] = 6;

        rop_to_index[CIRRUS_ROP_NOTSRC_AND_DST] = 7;

        rop_to_index[CIRRUS_ROP_SRC_XOR_DST] = 8;

        rop_to_index[CIRRUS_ROP_SRC_OR_DST] = 9;

        rop_to_index[CIRRUS_ROP_NOTSRC_OR_NOTDST] = 10;

        rop_to_index[CIRRUS_ROP_SRC_NOTXOR_DST] = 11;

        rop_to_index[CIRRUS_ROP_SRC_OR_NOTDST] = 12;

        rop_to_index[CIRRUS_ROP_NOTSRC] = 13;

        rop_to_index[CIRRUS_ROP_NOTSRC_OR_DST] = 14;

        rop_to_index[CIRRUS_ROP_NOTSRC_AND_NOTDST] = 15;

    }

    register_ioport_write(0x3c0, 16, 1, vga_ioport_write, s);

    register_ioport_write(0x3b4, 2, 1, vga_ioport_write, s);

    register_ioport_write(0x3d4, 2, 1, vga_ioport_write, s);

    register_ioport_write(0x3ba, 1, 1, vga_ioport_write, s);

    register_ioport_write(0x3da, 1, 1, vga_ioport_write, s);

    register_ioport_read(0x3c0, 16, 1, vga_ioport_read, s);

    register_ioport_read(0x3b4, 2, 1, vga_ioport_read, s);

    register_ioport_read(0x3d4, 2, 1, vga_ioport_read, s);

    register_ioport_read(0x3ba, 1, 1, vga_ioport_read, s);

    register_ioport_read(0x3da, 1, 1, vga_ioport_read, s);

    s->vga_io_memory = cpu_register_io_memory(0, cirrus_vga_mem_read,

                                           cirrus_vga_mem_write, s);

    cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,

                                 s->vga_io_memory);

    s->sr[0x06] = 0x0f;

    if (device_id == CIRRUS_ID_CLGD5446) {

        /* 4MB 64 bit memory config, always PCI */

        s->sr[0x1F] = 0x2d;		// MemClock

        s->gr[0x18] = 0x0f;             // fastest memory configuration

#if 1

        s->sr[0x0f] = 0x98;

        s->sr[0x17] = 0x20;

        s->sr[0x15] = 0x04; /* memory size, 3=2MB, 4=4MB */

        s->real_vram_size = 4096 * 1024;

#else

        s->sr[0x0f] = 0x18;

        s->sr[0x17] = 0x20;

        s->sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */

        s->real_vram_size = 2048 * 1024;

#endif

    } else {

        s->sr[0x1F] = 0x22;		// MemClock

        s->sr[0x0F] = CIRRUS_MEMSIZE_2M;

        if (is_pci)

            s->sr[0x17] = CIRRUS_BUSTYPE_PCI;

        else

            s->sr[0x17] = CIRRUS_BUSTYPE_ISA;

        s->real_vram_size = 2048 * 1024;

        s->sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */

    }

    s->cr[0x27] = device_id;

    /* Win2K seems to assume that the pattern buffer is at 0xff

       initially ! */

    memset(s->vram_ptr, 0xff, s->real_vram_size);

    s->cirrus_hidden_dac_lockindex = 5;

    s->cirrus_hidden_dac_data = 0;

    /* I/O handler for LFB */

    s->cirrus_linear_io_addr =

	cpu_register_io_memory(0, cirrus_linear_read, cirrus_linear_write,

			       s);

    s->cirrus_linear_write = cpu_get_io_memory_write(s->cirrus_linear_io_addr);

    /* I/O handler for LFB */

    s->cirrus_linear_bitblt_io_addr =

	cpu_register_io_memory(0, cirrus_linear_bitblt_read, cirrus_linear_bitblt_write,

			       s);

    /* I/O handler for memory-mapped I/O */

    s->cirrus_mmio_io_addr =

	cpu_register_io_memory(0, cirrus_mmio_read, cirrus_mmio_write, s);

    /* XXX: s->vram_size must be a power of two */

    s->cirrus_addr_mask = s->real_vram_size - 1;

    s->linear_mmio_mask = s->real_vram_size - 256;

    s->get_bpp = cirrus_get_bpp;

    s->get_offsets = cirrus_get_offsets;

    s->get_resolution = cirrus_get_resolution;

    s->cursor_invalidate = cirrus_cursor_invalidate;

    s->cursor_draw_line = cirrus_cursor_draw_line;

    register_savevm("cirrus_vga", 0, 2, cirrus_vga_save, cirrus_vga_load, s);

}


static int omap2_gpio_init(SysBusDevice *sbd)

{

    DeviceState *dev = DEVICE(sbd);

    struct omap2_gpif_s *s = OMAP2_GPIO(dev);

    int i;

    if (!s->iclk) {

        hw_error("omap2-gpio: iclk not connected\n");

    }

    if (s->mpu_model < omap3430) {

        s->modulecount = (s->mpu_model < omap2430) ? 4 : 5;

        memory_region_init_io(&s->iomem, OBJECT(s), &omap2_gpif_top_ops, s,

                              "omap2.gpio", 0x1000);

        sysbus_init_mmio(sbd, &s->iomem);

    } else {

        s->modulecount = 6;

    }

    s->modules = g_new0(struct omap2_gpio_s, s->modulecount);

    s->handler = g_new0(qemu_irq, s->modulecount * 32);

    qdev_init_gpio_in(dev, omap2_gpio_set, s->modulecount * 32);

    qdev_init_gpio_out(dev, s->handler, s->modulecount * 32);

    for (i = 0; i < s->modulecount; i++) {

        struct omap2_gpio_s *m = &s->modules[i];

        if (!s->fclk[i]) {

            hw_error("omap2-gpio: fclk%d not connected\n", i);

        }

        m->revision = (s->mpu_model < omap3430) ? 0x18 : 0x25;

        m->handler = &s->handler[i * 32];

        sysbus_init_irq(sbd, &m->irq[0]); /* mpu irq */

        sysbus_init_irq(sbd, &m->irq[1]); /* dsp irq */

        sysbus_init_irq(sbd, &m->wkup);

        memory_region_init_io(&m->iomem, OBJECT(s), &omap2_gpio_module_ops, m,

                              "omap.gpio-module", 0x1000);

        sysbus_init_mmio(sbd, &m->iomem);

    }

    return 0;

}


void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)

{

    FrameThreadContext *fctx = avctx->internal->thread_ctx;

    const AVCodec *codec = avctx->codec;

    int i;

    park_frame_worker_threads(fctx, thread_count);

    if (fctx->prev_thread && fctx->prev_thread != fctx->threads)

        if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {

            av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");

            fctx->prev_thread->avctx->internal->is_copy = fctx->threads->avctx->internal->is_copy;

            fctx->threads->avctx->internal->is_copy = 1;

        }

    fctx->die = 1;

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

        PerThreadContext *p = &fctx->threads[i];

        pthread_mutex_lock(&p->mutex);

        pthread_cond_signal(&p->input_cond);

        pthread_mutex_unlock(&p->mutex);

        if (p->thread_init)

            pthread_join(p->thread, NULL);

        p->thread_init=0;

        if (codec->close)

            codec->close(p->avctx);

        release_delayed_buffers(p);

        av_frame_free(&p->frame);

    }

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

        PerThreadContext *p = &fctx->threads[i];

        pthread_mutex_destroy(&p->mutex);

        pthread_mutex_destroy(&p->progress_mutex);

        pthread_cond_destroy(&p->input_cond);

        pthread_cond_destroy(&p->progress_cond);

        pthread_cond_destroy(&p->output_cond);

        av_packet_unref(&p->avpkt);

        av_freep(&p->released_buffers);

        if (i) {

            av_freep(&p->avctx->priv_data);

            av_freep(&p->avctx->slice_offset);

        }

        av_freep(&p->avctx->internal);

        av_freep(&p->avctx);

    }

    av_freep(&fctx->threads);

    pthread_mutex_destroy(&fctx->buffer_mutex);

    av_freep(&avctx->internal->thread_ctx);

    if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)

        av_opt_free(avctx->priv_data);

    avctx->codec = NULL;

}


static bool ranges_can_merge(Range *range1, Range *range2)

{

    return !(range1->end < range2->begin || range2->end < range1->begin);

}


static int nbd_open(BlockDriverState *bs, QDict *options, int flags,

                    Error **errp)

{

    BDRVNBDState *s = bs->opaque;

    char *export = NULL;

    int result, sock;

    Error *local_err = NULL;

    /* Pop the config into our state object. Exit if invalid. */

    nbd_config(s, options, &export, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        return -EINVAL;

    }

    /* establish TCP connection, return error if it fails

     * TODO: Configurable retry-until-timeout behaviour.

     */

    sock = nbd_establish_connection(bs, errp);

    if (sock < 0) {

        return sock;

    }

    /* NBD handshake */

    result = nbd_client_init(bs, sock, export, errp);

    return result;

}