static void mirror_start_job(const char *job_id, BlockDriverState *bs,

                             int creation_flags, BlockDriverState *target,

                             const char *replaces, int64_t speed,

                             uint32_t granularity, int64_t buf_size,

                             BlockMirrorBackingMode backing_mode,

                             BlockdevOnError on_source_error,

                             BlockdevOnError on_target_error,

                             bool unmap,

                             BlockCompletionFunc *cb,

                             void *opaque,

                             const BlockJobDriver *driver,

                             bool is_none_mode, BlockDriverState *base,

                             bool auto_complete, const char *filter_node_name,

                             bool is_mirror,

                             Error **errp)

{

    MirrorBlockJob *s;

    BlockDriverState *mirror_top_bs;

    bool target_graph_mod;

    bool target_is_backing;

    Error *local_err = NULL;

    int ret;

    if (granularity == 0) {

        granularity = bdrv_get_default_bitmap_granularity(target);

    }

    assert ((granularity & (granularity - 1)) == 0);

    /* Granularity must be large enough for sector-based dirty bitmap */

    assert(granularity >= BDRV_SECTOR_SIZE);

    if (buf_size < 0) {

        error_setg(errp, "Invalid parameter 'buf-size'");

        return;

    }

    if (buf_size == 0) {

        buf_size = DEFAULT_MIRROR_BUF_SIZE;

    }

    /* In the case of active commit, add dummy driver to provide consistent

     * reads on the top, while disabling it in the intermediate nodes, and make

     * the backing chain writable. */

    mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name,

                                         BDRV_O_RDWR, errp);

    if (mirror_top_bs == NULL) {

        return;

    }

    if (!filter_node_name) {

        mirror_top_bs->implicit = true;

    }

    mirror_top_bs->total_sectors = bs->total_sectors;

    bdrv_set_aio_context(mirror_top_bs, bdrv_get_aio_context(bs));

    /* bdrv_append takes ownership of the mirror_top_bs reference, need to keep

     * it alive until block_job_create() succeeds even if bs has no parent. */

    bdrv_ref(mirror_top_bs);

    bdrv_drained_begin(bs);

    bdrv_append(mirror_top_bs, bs, &local_err);

    bdrv_drained_end(bs);

    if (local_err) {

        bdrv_unref(mirror_top_bs);

        error_propagate(errp, local_err);

        return;

    }

    /* Make sure that the source is not resized while the job is running */

    s = block_job_create(job_id, driver, mirror_top_bs,

                         BLK_PERM_CONSISTENT_READ,

                         BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |

                         BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed,

                         creation_flags, cb, opaque, errp);

    if (!s) {

        goto fail;

    }

    /* The block job now has a reference to this node */

    bdrv_unref(mirror_top_bs);

    s->source = bs;

    s->mirror_top_bs = mirror_top_bs;

    /* No resize for the target either; while the mirror is still running, a

     * consistent read isn't necessarily possible. We could possibly allow

     * writes and graph modifications, though it would likely defeat the

     * purpose of a mirror, so leave them blocked for now.

     *

     * In the case of active commit, things look a bit different, though,

     * because the target is an already populated backing file in active use.

     * We can allow anything except resize there.*/

    target_is_backing = bdrv_chain_contains(bs, target);

    target_graph_mod = (backing_mode != MIRROR_LEAVE_BACKING_CHAIN);

    s->target = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE |

                        (target_graph_mod ? BLK_PERM_GRAPH_MOD : 0),

                        BLK_PERM_WRITE_UNCHANGED |

                        (target_is_backing ? BLK_PERM_CONSISTENT_READ |

                                             BLK_PERM_WRITE |

                                             BLK_PERM_GRAPH_MOD : 0));

    ret = blk_insert_bs(s->target, target, errp);

    if (ret < 0) {

        goto fail;

    }

    if (is_mirror) {

        /* XXX: Mirror target could be a NBD server of target QEMU in the case

         * of non-shared block migration. To allow migration completion, we

         * have to allow "inactivate" of the target BB.  When that happens, we

         * know the job is drained, and the vcpus are stopped, so no write

         * operation will be performed. Block layer already has assertions to

         * ensure that. */

        blk_set_force_allow_inactivate(s->target);

    }

    s->replaces = g_strdup(replaces);

    s->on_source_error = on_source_error;

    s->on_target_error = on_target_error;

    s->is_none_mode = is_none_mode;

    s->backing_mode = backing_mode;

    s->base = base;

    s->granularity = granularity;

    s->buf_size = ROUND_UP(buf_size, granularity);

    s->unmap = unmap;

    if (auto_complete) {

        s->should_complete = true;

    }

    s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);

    if (!s->dirty_bitmap) {

        goto fail;

    }

    /* Required permissions are already taken with blk_new() */

    block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL,

                       &error_abort);

    /* In commit_active_start() all intermediate nodes disappear, so

     * any jobs in them must be blocked */

    if (target_is_backing) {

        BlockDriverState *iter;

        for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) {

            /* XXX BLK_PERM_WRITE needs to be allowed so we don't block

             * ourselves at s->base (if writes are blocked for a node, they are

             * also blocked for its backing file). The other options would be a

             * second filter driver above s->base (== target). */

            ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,

                                     BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,

                                     errp);

            if (ret < 0) {

                goto fail;

            }

        }

    }

    trace_mirror_start(bs, s, opaque);

    block_job_start(&s->common);

    return;

fail:

    if (s) {

        /* Make sure this BDS does not go away until we have completed the graph

         * changes below */

        bdrv_ref(mirror_top_bs);

        g_free(s->replaces);

        blk_unref(s->target);

        block_job_early_fail(&s->common);

    }

    bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,

                            &error_abort);

    bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort);

    bdrv_unref(mirror_top_bs);

}


static void test_acpi_tables(test_data *data)

{

    int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */

    int i;

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

        AcpiSdtTable ssdt_table;

        uint32_t addr;

        addr = le32_to_cpu(data->rsdt_tables_addr[i + 1]); /* fadt is first */

        test_dst_table(&ssdt_table, addr);

        g_array_append_val(data->tables, ssdt_table);

    }

}


void cris_mmu_flush_pid(CPUState *env, uint32_t pid)

{

	target_ulong vaddr;

	unsigned int idx;

	uint32_t lo, hi;

	uint32_t tlb_vpn;

	int tlb_pid, tlb_g, tlb_v, tlb_k;

	unsigned int set;

	unsigned int mmu;

	pid &= 0xff;

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

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

		{

			for (idx = 0; idx < 16; idx++) {

				lo = env->tlbsets[mmu][set][idx].lo;

				hi = env->tlbsets[mmu][set][idx].hi;

				tlb_vpn = EXTRACT_FIELD(hi, 13, 31);

				tlb_pid = EXTRACT_FIELD(hi, 0, 7);

				tlb_g  = EXTRACT_FIELD(lo, 4, 4);

				tlb_v = EXTRACT_FIELD(lo, 3, 3);

				tlb_k = EXTRACT_FIELD(lo, 2, 2);

				/* Kernel protected areas need to be flushed

				   as well.  */

				if (tlb_v && !tlb_g && (tlb_pid == pid || tlb_k)) {

					vaddr = tlb_vpn << TARGET_PAGE_BITS;

					D(fprintf(logfile,

						  "flush pid=%x vaddr=%x\n",

						  pid, vaddr));

					tlb_flush_page(env, vaddr);

				}

			}

		}

	}

}


static void vfio_intp_interrupt(VFIOINTp *intp)

{

    int ret;

    VFIOINTp *tmp;

    VFIOPlatformDevice *vdev = intp->vdev;

    bool delay_handling = false;

    qemu_mutex_lock(&vdev->intp_mutex);

    if (intp->state == VFIO_IRQ_INACTIVE) {

        QLIST_FOREACH(tmp, &vdev->intp_list, next) {

            if (tmp->state == VFIO_IRQ_ACTIVE ||

                tmp->state == VFIO_IRQ_PENDING) {

                delay_handling = true;

                break;

            }

        }

    }

    if (delay_handling) {

        /*

         * the new IRQ gets a pending status and is pushed in

         * the pending queue

         */

        intp->state = VFIO_IRQ_PENDING;

        trace_vfio_intp_interrupt_set_pending(intp->pin);

        QSIMPLEQ_INSERT_TAIL(&vdev->pending_intp_queue,

                             intp, pqnext);

        ret = event_notifier_test_and_clear(&intp->interrupt);

        qemu_mutex_unlock(&vdev->intp_mutex);

        return;

    }

    trace_vfio_platform_intp_interrupt(intp->pin,

                              event_notifier_get_fd(&intp->interrupt));

    ret = event_notifier_test_and_clear(&intp->interrupt);

    if (!ret) {

        error_report("Error when clearing fd=%d (ret = %d)",

                     event_notifier_get_fd(&intp->interrupt), ret);

    }

    intp->state = VFIO_IRQ_ACTIVE;

    /* sets slow path */

    vfio_mmap_set_enabled(vdev, false);

    /* trigger the virtual IRQ */

    qemu_set_irq(intp->qemuirq, 1);

    /*

     * Schedule the mmap timer which will restore fastpath when no IRQ

     * is active anymore

     */

    if (vdev->mmap_timeout) {

        timer_mod(vdev->mmap_timer,

                  qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +

                      vdev->mmap_timeout);

    }

    qemu_mutex_unlock(&vdev->intp_mutex);

}


void tb_invalidate_phys_addr(hwaddr addr)

{

    ram_addr_t ram_addr;

    MemoryRegionSection *section;

    section = phys_page_find(address_space_memory.dispatch,

                             addr >> TARGET_PAGE_BITS);

    if (!(memory_region_is_ram(section->mr)

          || memory_region_is_romd(section->mr))) {

        return;

    }

    ram_addr = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)

        + memory_region_section_addr(section, addr);

    tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);

}


static void lsi_class_init(ObjectClass *klass, void *data)

{

    DeviceClass *dc = DEVICE_CLASS(klass);

    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = lsi_scsi_init;

    k->exit = lsi_scsi_uninit;

    k->vendor_id = PCI_VENDOR_ID_LSI_LOGIC;

    k->device_id = PCI_DEVICE_ID_LSI_53C895A;

    k->class_id = PCI_CLASS_STORAGE_SCSI;

    k->subsystem_id = 0x1000;

    dc->alias = "lsi";

    dc->reset = lsi_scsi_reset;

    dc->vmsd = &vmstate_lsi_scsi;

}


static void sigp_stop(CPUState *cs, run_on_cpu_data arg)

{

    S390CPU *cpu = S390_CPU(cs);

    SigpInfo *si = arg.host_ptr;

    if (s390_cpu_get_state(cpu) != CPU_STATE_OPERATING) {

        si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;

        return;

    }

    /* disabled wait - sleeping in user space */

    if (cs->halted) {

        s390_cpu_set_state(CPU_STATE_STOPPED, cpu);

    } else {

        /* execute the stop function */

        cpu->env.sigp_order = SIGP_STOP;

        cpu_inject_stop(cpu);

    }

    si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;

}


static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write,

                             double elapsed_time, uint64_t *wait)

{

    uint64_t iops_limit = 0;

    double   ios_limit, ios_base;

    double   slice_time, wait_time;

    if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {

        iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];

    } else if (bs->io_limits.iops[is_write]) {

        iops_limit = bs->io_limits.iops[is_write];

    } else {

        if (wait) {

            *wait = 0;

        }

        return false;

    }

    slice_time = bs->slice_end - bs->slice_start;

    slice_time /= (NANOSECONDS_PER_SECOND);

    ios_limit  = iops_limit * slice_time;

    ios_base   = bs->nr_ops[is_write] - bs->io_base.ios[is_write];

    if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {

        ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write];

    }

    if (ios_base + 1 <= ios_limit) {

        if (wait) {

            *wait = 0;

        }

        return false;

    }

    /* Calc approx time to dispatch */

    wait_time = (ios_base + 1) / iops_limit;

    if (wait_time > elapsed_time) {

        wait_time = wait_time - elapsed_time;

    } else {

        wait_time = 0;

    }

    bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10;

    bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME;

    if (wait) {

        *wait = wait_time * BLOCK_IO_SLICE_TIME * 10;

    }

    return true;

}


static struct vm_area_struct *vma_next(struct vm_area_struct *vma)

{

    return (TAILQ_NEXT(vma, vma_link));

}


void ff_get_unscaled_swscale(SwsContext *c)

{

    const enum PixelFormat srcFormat = c->srcFormat;

    const enum PixelFormat dstFormat = c->dstFormat;

    const int flags = c->flags;

    const int dstH = c->dstH;

    int needsDither;

    needsDither= isAnyRGB(dstFormat)

        &&  c->dstFormatBpp < 24

        && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));

    /* yv12_to_nv12 */

    if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {

        c->swScale= planarToNv12Wrapper;

    }

    /* yuv2bgr */

    if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)

        && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {

        c->swScale= ff_yuv2rgb_get_func_ptr(c);

    }

    if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {

        c->swScale= yvu9ToYv12Wrapper;

    }

    /* bgr24toYV12 */

    if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))

        c->swScale= bgr24ToYv12Wrapper;

    /* RGB/BGR -> RGB/BGR (no dither needed forms) */

    if (   isAnyRGB(srcFormat)

        && isAnyRGB(dstFormat)

        && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8

        && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8

        && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4

        && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4

        && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE

        && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE

        && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK

        && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE

        && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE

        && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE

        && srcFormat != PIX_FMT_BGR48LE   && dstFormat != PIX_FMT_BGR48LE

        && srcFormat != PIX_FMT_BGR48BE   && dstFormat != PIX_FMT_BGR48BE

        && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))

        c->swScale= rgbToRgbWrapper;

#define isByteRGB(f) (\

        f == PIX_FMT_RGB32   ||\

        f == PIX_FMT_RGB32_1 ||\

        f == PIX_FMT_RGB24   ||\

        f == PIX_FMT_BGR32   ||\

        f == PIX_FMT_BGR32_1 ||\

        f == PIX_FMT_BGR24)

    if (isAnyRGB(srcFormat) && isPlanar(srcFormat) && isByteRGB(dstFormat))

        c->swScale= planarRgbToRgbWrapper;

    if ((usePal(srcFormat) && (

        dstFormat == PIX_FMT_RGB32   ||

        dstFormat == PIX_FMT_RGB32_1 ||

        dstFormat == PIX_FMT_RGB24   ||

        dstFormat == PIX_FMT_BGR32   ||

        dstFormat == PIX_FMT_BGR32_1 ||

        dstFormat == PIX_FMT_BGR24)))

        c->swScale= palToRgbWrapper;

    if (srcFormat == PIX_FMT_YUV422P) {

        if (dstFormat == PIX_FMT_YUYV422)

            c->swScale= yuv422pToYuy2Wrapper;

        else if (dstFormat == PIX_FMT_UYVY422)

            c->swScale= yuv422pToUyvyWrapper;

    }

    /* LQ converters if -sws 0 or -sws 4*/

    if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {

        /* yv12_to_yuy2 */

        if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {

            if (dstFormat == PIX_FMT_YUYV422)

                c->swScale= planarToYuy2Wrapper;

            else if (dstFormat == PIX_FMT_UYVY422)

                c->swScale= planarToUyvyWrapper;

        }

    }

    if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))

        c->swScale= yuyvToYuv420Wrapper;

    if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))

        c->swScale= uyvyToYuv420Wrapper;

    if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)

        c->swScale= yuyvToYuv422Wrapper;

    if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)

        c->swScale= uyvyToYuv422Wrapper;

    /* simple copy */

    if (  srcFormat == dstFormat

        || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)

        || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)

        || (isPlanarYUV(srcFormat) && isGray(dstFormat))

        || (isPlanarYUV(dstFormat) && isGray(srcFormat))

        || (isGray(dstFormat) && isGray(srcFormat))

        || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)

            && c->chrDstHSubSample == c->chrSrcHSubSample

            && c->chrDstVSubSample == c->chrSrcVSubSample

            && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21

            && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))

    {

        if (isPacked(c->srcFormat))

            c->swScale= packedCopyWrapper;

        else /* Planar YUV or gray */

            c->swScale= planarCopyWrapper;

    }

    if (ARCH_BFIN)

        ff_bfin_get_unscaled_swscale(c);

    if (HAVE_ALTIVEC)

        ff_swscale_get_unscaled_altivec(c);

}


void address_space_init_dispatch(AddressSpace *as)

{

    AddressSpaceDispatch *d = g_new(AddressSpaceDispatch, 1);

    d->phys_map  = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 };

    d->listener = (MemoryListener) {

        .begin = mem_begin,

        .region_add = mem_add,

        .region_nop = mem_add,

        .priority = 0,

    };

    d->as = as;

    as->dispatch = d;

    memory_listener_register(&d->listener, as);

}


void helper_fcmp_gt_FT(CPUSH4State *env, float32 t0, float32 t1)

{

    int relation;

    set_float_exception_flags(0, &env->fp_status);

    relation = float32_compare(t0, t1, &env->fp_status);

    if (unlikely(relation == float_relation_unordered)) {

        update_fpscr(env, GETPC());

    } else {

        env->sr_t = (relation == float_relation_greater);

    }

}


static void pxa2xx_i2s_write(void *opaque, hwaddr addr,

                             uint64_t value, unsigned size)

{

    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;

    uint32_t *sample;

    switch (addr) {

    case SACR0:

        if (value & (1 << 3))				/* RST */

            pxa2xx_i2s_reset(s);

        s->control[0] = value & 0xff3d;

        if (!s->enable && (value & 1) && s->tx_len) {	/* ENB */

            for (sample = s->fifo; s->fifo_len > 0; s->fifo_len --, sample ++)

                s->codec_out(s->opaque, *sample);

            s->status &= ~(1 << 7);			/* I2SOFF */

        }

        if (value & (1 << 4))				/* EFWR */

            printf("%s: Attempt to use special function\n", __FUNCTION__);

        s->enable = (value & 9) == 1;			/* ENB && !RST*/

        pxa2xx_i2s_update(s);

        break;

    case SACR1:

        s->control[1] = value & 0x0039;

        if (value & (1 << 5))				/* ENLBF */

            printf("%s: Attempt to use loopback function\n", __FUNCTION__);

        if (value & (1 << 4))				/* DPRL */

            s->fifo_len = 0;

        pxa2xx_i2s_update(s);

        break;

    case SAIMR:

        s->mask = value & 0x0078;

        pxa2xx_i2s_update(s);

        break;

    case SAICR:

        s->status &= ~(value & (3 << 5));

        pxa2xx_i2s_update(s);

        break;

    case SADIV:

        s->clk = value & 0x007f;

        break;

    case SADR:

        if (s->tx_len && s->enable) {

            s->tx_len --;

            pxa2xx_i2s_update(s);

            s->codec_out(s->opaque, value);

        } else if (s->fifo_len < 16) {

            s->fifo[s->fifo_len ++] = value;

            pxa2xx_i2s_update(s);

        }

        break;

    default:

        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);

    }

}


static int pci_unin_main_init_device(SysBusDevice *dev)

{

    UNINState *s;

    int pci_mem_config, pci_mem_data;

    /* Use values found on a real PowerMac */

    /* Uninorth main bus */

    s = FROM_SYSBUS(UNINState, dev);

    pci_mem_config = cpu_register_io_memory(pci_unin_main_config_read,

                                            pci_unin_main_config_write, s);

    pci_mem_data = cpu_register_io_memory(pci_unin_main_read,

                                          pci_unin_main_write, &s->host_state);

    sysbus_init_mmio(dev, 0x1000, pci_mem_config);

    sysbus_init_mmio(dev, 0x1000, pci_mem_data);

    register_savevm("uninorth", 0, 1, pci_unin_save, pci_unin_load, &s->host_state);

    qemu_register_reset(pci_unin_reset, &s->host_state);

    return 0;

}


static inline void wb_SR_F(void)

{

    int label;

    label = gen_new_label();

    tcg_gen_andi_tl(cpu_sr, cpu_sr, ~SR_F);

    tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, label);

    tcg_gen_ori_tl(cpu_sr, cpu_sr, SR_F);

    gen_set_label(label);

}


static inline uint32_t efsctsf(uint32_t val)

{

    CPU_FloatU u;

    float32 tmp;

    u.l = val;

    /* NaN are not treated the same way IEEE 754 does */

    if (unlikely(float32_is_nan(u.f)))

        return 0;

    tmp = uint64_to_float32(1ULL << 32, &env->vec_status);

    u.f = float32_mul(u.f, tmp, &env->vec_status);

    return float32_to_int32(u.f, &env->vec_status);

}


static inline int decode_seq_parameter_set(H264Context *h){

    MpegEncContext * const s = &h->s;

    int profile_idc, level_idc;

    unsigned int sps_id, tmp, mb_width, mb_height;

    int i;

    SPS *sps;

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

    get_bits1(&s->gb);   //constraint_set0_flag

    get_bits1(&s->gb);   //constraint_set1_flag

    get_bits1(&s->gb);   //constraint_set2_flag

    get_bits1(&s->gb);   //constraint_set3_flag

    get_bits(&s->gb, 4); // reserved

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

    sps_id= get_ue_golomb(&s->gb);

    sps = alloc_parameter_set(h, (void **)h->sps_buffers, sps_id, MAX_SPS_COUNT, sizeof(SPS), "sps");

    if(sps == NULL)

        return -1;

    sps->profile_idc= profile_idc;

    sps->level_idc= level_idc;

    memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));

    memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));

    sps->scaling_matrix_present = 0;

    if(sps->profile_idc >= 100){ //high profile

        sps->chroma_format_idc= get_ue_golomb(&s->gb);

        if(sps->chroma_format_idc == 3)

            get_bits1(&s->gb);  //residual_color_transform_flag

        get_ue_golomb(&s->gb);  //bit_depth_luma_minus8

        get_ue_golomb(&s->gb);  //bit_depth_chroma_minus8

        sps->transform_bypass = get_bits1(&s->gb);

        decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);

    }else{

        sps->chroma_format_idc= 1;

    }

    sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;

    sps->poc_type= get_ue_golomb(&s->gb);

    if(sps->poc_type == 0){ //FIXME #define

        sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;

    } else if(sps->poc_type == 1){//FIXME #define

        sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);

        sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);

        sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);

        tmp= get_ue_golomb(&s->gb);

        if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){

            av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);

            return -1;

        }

        sps->poc_cycle_length= tmp;

        for(i=0; i<sps->poc_cycle_length; i++)

            sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);

    }else if(sps->poc_type != 2){

        av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);

        return -1;

    }

    tmp= get_ue_golomb(&s->gb);

    if(tmp > MAX_PICTURE_COUNT-2 || tmp >= 32){

        av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");

        return -1;

    }

    sps->ref_frame_count= tmp;

    sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);

    mb_width= get_ue_golomb(&s->gb) + 1;

    mb_height= get_ue_golomb(&s->gb) + 1;

    if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||

       avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){

        av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");

        return -1;

    }

    sps->mb_width = mb_width;

    sps->mb_height= mb_height;

    sps->frame_mbs_only_flag= get_bits1(&s->gb);

    if(!sps->frame_mbs_only_flag)

        sps->mb_aff= get_bits1(&s->gb);

    else

        sps->mb_aff= 0;

    sps->direct_8x8_inference_flag= get_bits1(&s->gb);

#ifndef ALLOW_INTERLACE

    if(sps->mb_aff)

        av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");

#endif

    if(!sps->direct_8x8_inference_flag && sps->mb_aff)

        av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");

    sps->crop= get_bits1(&s->gb);

    if(sps->crop){

        sps->crop_left  = get_ue_golomb(&s->gb);

        sps->crop_right = get_ue_golomb(&s->gb);

        sps->crop_top   = get_ue_golomb(&s->gb);

        sps->crop_bottom= get_ue_golomb(&s->gb);

        if(sps->crop_left || sps->crop_top){

            av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");

        }

        if(sps->crop_right >= 8 || sps->crop_bottom >= (8>> !sps->frame_mbs_only_flag)){

            av_log(h->s.avctx, AV_LOG_ERROR, "brainfart cropping not supported, this could look slightly wrong ...\n");

        }

    }else{

        sps->crop_left  =

        sps->crop_right =

        sps->crop_top   =

        sps->crop_bottom= 0;

    }

    sps->vui_parameters_present_flag= get_bits1(&s->gb);

    if( sps->vui_parameters_present_flag )

        decode_vui_parameters(h, sps);

    if(s->avctx->debug&FF_DEBUG_PICT_INFO){

        av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s %s\n",

               sps_id, sps->profile_idc, sps->level_idc,

               sps->poc_type,

               sps->ref_frame_count,

               sps->mb_width, sps->mb_height,

               sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),

               sps->direct_8x8_inference_flag ? "8B8" : "",

               sps->crop_left, sps->crop_right,

               sps->crop_top, sps->crop_bottom,

               sps->vui_parameters_present_flag ? "VUI" : "",

               ((const char*[]){"Gray","420","422","444"})[sps->chroma_format_idc]

               );

    }

    return 0;

}


static enum AVPixelFormat dshow_pixfmt(DWORD biCompression, WORD biBitCount)

{

    switch(biCompression) {

    case BI_BITFIELDS:

    case BI_RGB:

        switch(biBitCount) { /* 1-8 are untested */

            case 1:

                return AV_PIX_FMT_MONOWHITE;

            case 4:

                return AV_PIX_FMT_RGB4;

            case 8:

                return AV_PIX_FMT_RGB8;

            case 16:

                return AV_PIX_FMT_RGB555;

            case 24:

                return AV_PIX_FMT_BGR24;

            case 32:

                return AV_PIX_FMT_0RGB32;

        }

    }

    return avpriv_find_pix_fmt(ff_raw_pix_fmt_tags, biCompression); // all others

}


static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,

                                const uint8_t *jvt_list,

                                const uint8_t *fallback_list)

{

    int i, last = 8, next = 8;

    const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct;

    if (!get_bits1(&h->gb)) /* matrix not written, we use the predicted one */

        memcpy(factors, fallback_list, size * sizeof(uint8_t));

    else

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

            if (next)

                next = (last + get_se_golomb(&h->gb)) & 0xff;

            if (!i && !next) { /* matrix not written, we use the preset one */

                memcpy(factors, jvt_list, size * sizeof(uint8_t));

                break;

            }

            last = factors[scan[i]] = next ? next : last;

        }

}


static void iscsi_co_generic_bh_cb(void *opaque)

{

    struct IscsiTask *iTask = opaque;

    iTask->complete = 1;

    qemu_bh_delete(iTask->bh);

    qemu_coroutine_enter(iTask->co, NULL);

}


av_cold int ff_dct_common_init(MpegEncContext *s)

{

    ff_dsputil_init(&s->dsp, s->avctx);

    ff_videodsp_init(&s->vdsp, 8);

    s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;

    s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;

    s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;

    s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;

    s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;

    if (s->flags & CODEC_FLAG_BITEXACT)

        s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;

    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;

#if ARCH_X86

    ff_MPV_common_init_x86(s);

#elif ARCH_ALPHA

    ff_MPV_common_init_axp(s);

#elif ARCH_ARM

    ff_MPV_common_init_arm(s);

#elif HAVE_ALTIVEC

    ff_MPV_common_init_altivec(s);

#elif ARCH_BFIN

    ff_MPV_common_init_bfin(s);

#endif

    /* load & permutate scantables

     * note: only wmv uses different ones

     */

    if (s->alternate_scan) {

        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);

        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);

    } else {

        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);

        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);

    }

    ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);

    ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);

    return 0;

}


int s390_ccw_cmd_request(ORB *orb, SCSW *scsw, void *data)

{

    S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(data);

    if (cdc->handle_request) {

        return cdc->handle_request(orb, scsw, data);

    } else {

        return -ENOSYS;

    }

}


static void v9fs_walk(void *opaque)

{

    int name_idx;

    V9fsQID *qids = NULL;

    int i, err = 0;

    V9fsPath dpath, path;

    uint16_t nwnames;

    struct stat stbuf;

    size_t offset = 7;

    int32_t fid, newfid;

    V9fsString *wnames = NULL;

    V9fsFidState *fidp;

    V9fsFidState *newfidp = NULL;

    V9fsPDU *pdu = opaque;

    V9fsState *s = pdu->s;

    err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);

    if (err < 0) {

        pdu_complete(pdu, err);

        return ;

    offset += err;

    trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);

    if (nwnames && nwnames <= P9_MAXWELEM) {

        wnames = g_malloc0(sizeof(wnames[0]) * nwnames);

        qids   = g_malloc0(sizeof(qids[0]) * nwnames);

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

            err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);

            if (err < 0) {

            offset += err;

    } else if (nwnames > P9_MAXWELEM) {

        err = -EINVAL;

    fidp = get_fid(pdu, fid);

    if (fidp == NULL) {

    v9fs_path_init(&dpath);

    v9fs_path_init(&path);

    /*

     * Both dpath and path initially poin to fidp.

     * Needed to handle request with nwnames == 0

     */

    v9fs_path_copy(&dpath, &fidp->path);

    v9fs_path_copy(&path, &fidp->path);

    for (name_idx = 0; name_idx < nwnames; name_idx++) {

        err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, &path);

        if (err < 0) {

            goto out;

        err = v9fs_co_lstat(pdu, &path, &stbuf);

        if (err < 0) {

            goto out;

        stat_to_qid(&stbuf, &qids[name_idx]);

        v9fs_path_copy(&dpath, &path);

    if (fid == newfid) {

        BUG_ON(fidp->fid_type != P9_FID_NONE);

        v9fs_path_copy(&fidp->path, &path);

    } else {

        newfidp = alloc_fid(s, newfid);

        if (newfidp == NULL) {

            err = -EINVAL;

            goto out;

        newfidp->uid = fidp->uid;

        v9fs_path_copy(&newfidp->path, &path);

    err = v9fs_walk_marshal(pdu, nwnames, qids);

    trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);

out:

    put_fid(pdu, fidp);

    if (newfidp) {

        put_fid(pdu, newfidp);

    v9fs_path_free(&dpath);

    v9fs_path_free(&path);

out_nofid:

    pdu_complete(pdu, err);

    if (nwnames && nwnames <= P9_MAXWELEM) {

        for (name_idx = 0; name_idx < nwnames; name_idx++) {

            v9fs_string_free(&wnames[name_idx]);

        g_free(wnames);

        g_free(qids);


static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)

{

    int rt = extract32(insn, 0, 5);

    int rn = extract32(insn, 5, 5);

    int imm9 = sextract32(insn, 12, 9);

    int opc = extract32(insn, 22, 2);

    int size = extract32(insn, 30, 2);

    int idx = extract32(insn, 10, 2);

    bool is_signed = false;

    bool is_store = false;

    bool is_extended = false;

    bool is_unpriv = (idx == 2);

    bool is_vector = extract32(insn, 26, 1);

    bool post_index;

    bool writeback;

    TCGv_i64 tcg_addr;

    if (is_vector) {

        size |= (opc & 2) << 1;

        if (size > 4 || is_unpriv) {

            unallocated_encoding(s);

            return;

        }

        is_store = ((opc & 1) == 0);

        if (!fp_access_check(s)) {

            return;

        }

    } else {

        if (size == 3 && opc == 2) {

            /* PRFM - prefetch */

            if (is_unpriv) {

                unallocated_encoding(s);

                return;

            }

            return;

        }

        if (opc == 3 && size > 1) {

            unallocated_encoding(s);

            return;

        }

        is_store = (opc == 0);

        is_signed = opc & (1<<1);

        is_extended = (size < 3) && (opc & 1);

    }

    switch (idx) {

    case 0:

    case 2:

        post_index = false;

        writeback = false;

        break;

    case 1:

        post_index = true;

        writeback = true;

        break;

    case 3:

        post_index = false;

        writeback = true;

        break;

    }

    if (rn == 31) {

        gen_check_sp_alignment(s);

    }

    tcg_addr = read_cpu_reg_sp(s, rn, 1);

    if (!post_index) {

        tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);

    }

    if (is_vector) {

        if (is_store) {

            do_fp_st(s, rt, tcg_addr, size);

        } else {

            do_fp_ld(s, rt, tcg_addr, size);

        }

    } else {

        TCGv_i64 tcg_rt = cpu_reg(s, rt);

        int memidx = is_unpriv ? 1 : get_mem_index(s);

        if (is_store) {

            do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx);

        } else {

            do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,

                             is_signed, is_extended, memidx);

        }

    }

    if (writeback) {

        TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);

        if (post_index) {

            tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);

        }

        tcg_gen_mov_i64(tcg_rn, tcg_addr);

    }

}


static int clv_decode_frame(AVCodecContext *avctx, void *data,
                            int *got_frame, AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
    CLVContext *c = avctx->priv_data;
    GetByteContext gb;
    uint32_t frame_type;
    int i, j;
    int ret;
    int mb_ret = 0;
    bytestream2_init(&gb, buf, buf_size);
    if (avctx->codec_tag == MKTAG('C','L','V','1')) {
        int skip = bytestream2_get_byte(&gb);
        bytestream2_skip(&gb, (skip + 1) * 8);
    frame_type = bytestream2_get_byte(&gb);
    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
        return ret;
    c->pic->key_frame = frame_type & 0x20 ? 1 : 0;
    c->pic->pict_type = frame_type & 0x20 ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
    if (frame_type & 0x2) {
        bytestream2_get_be32(&gb); // frame size;
        c->ac_quant        = bytestream2_get_byte(&gb);
        c->luma_dc_quant   = 32;
        c->chroma_dc_quant = 32;
        if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
                                  (buf_size - bytestream2_tell(&gb)))) < 0)
            return ret;
        for (i = 0; i < 3; i++)
            c->top_dc[i] = 32;
        for (i = 0; i < 4; i++)
            c->left_dc[i] = 32;
        for (j = 0; j < c->mb_height; j++) {
            for (i = 0; i < c->mb_width; i++) {
                ret = decode_mb(c, i, j);
                if (ret < 0)
                    mb_ret = ret;
    } else {
    if ((ret = av_frame_ref(data, c->pic)) < 0)
        return ret;
    *got_frame = 1;
    return mb_ret < 0 ? mb_ret : buf_size;


static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,

    AVPacket *pkt)

{

    unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];

    int chunk_type;

    int chunk_size;

    unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];

    unsigned char opcode_type;

    unsigned char opcode_version;

    int opcode_size;

    unsigned char scratch[1024];

    int i, j;

    int first_color, last_color;

    int audio_flags;

    unsigned char r, g, b;

    unsigned int width, height;

    /* see if there are any pending packets */

    chunk_type = load_ipmovie_packet(s, pb, pkt);

    if (chunk_type != CHUNK_DONE)

        return chunk_type;

    /* read the next chunk, wherever the file happens to be pointing */

    if (url_feof(pb))

        return CHUNK_EOF;

    if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=

        CHUNK_PREAMBLE_SIZE)

        return CHUNK_BAD;

    chunk_size = AV_RL16(&chunk_preamble[0]);

    chunk_type = AV_RL16(&chunk_preamble[2]);

    av_dlog(NULL, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);

    switch (chunk_type) {

    case CHUNK_INIT_AUDIO:

        av_dlog(NULL, "initialize audio\n");

        break;

    case CHUNK_AUDIO_ONLY:

        av_dlog(NULL, "audio only\n");

        break;

    case CHUNK_INIT_VIDEO:

        av_dlog(NULL, "initialize video\n");

        break;

    case CHUNK_VIDEO:

        av_dlog(NULL, "video (and audio)\n");

        break;

    case CHUNK_SHUTDOWN:

        av_dlog(NULL, "shutdown\n");

        break;

    case CHUNK_END:

        av_dlog(NULL, "end\n");

        break;

    default:

        av_dlog(NULL, "invalid chunk\n");

        chunk_type = CHUNK_BAD;

        break;

    }

    while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {

        /* read the next chunk, wherever the file happens to be pointing */

        if (url_feof(pb)) {

            chunk_type = CHUNK_EOF;

            break;

        }

        if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=

            CHUNK_PREAMBLE_SIZE) {

            chunk_type = CHUNK_BAD;

            break;

        }

        opcode_size = AV_RL16(&opcode_preamble[0]);

        opcode_type = opcode_preamble[2];

        opcode_version = opcode_preamble[3];

        chunk_size -= OPCODE_PREAMBLE_SIZE;

        chunk_size -= opcode_size;

        if (chunk_size < 0) {

            av_dlog(NULL, "chunk_size countdown just went negative\n");

            chunk_type = CHUNK_BAD;

            break;

        }

        av_dlog(NULL, "  opcode type %02X, version %d, 0x%04X bytes: ",

                opcode_type, opcode_version, opcode_size);

        switch (opcode_type) {

        case OPCODE_END_OF_STREAM:

            av_dlog(NULL, "end of stream\n");

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_END_OF_CHUNK:

            av_dlog(NULL, "end of chunk\n");

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_CREATE_TIMER:

            av_dlog(NULL, "create timer\n");

            if ((opcode_version > 0) || (opcode_size != 6)) {

                av_dlog(NULL, "bad create_timer opcode\n");

                chunk_type = CHUNK_BAD;

                break;

            }

            if (avio_read(pb, scratch, opcode_size) !=

                opcode_size) {

                chunk_type = CHUNK_BAD;

                break;

            }

            s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);

            av_dlog(NULL, "  %.2f frames/second (timer div = %d, subdiv = %d)\n",

                    1000000.0 / s->frame_pts_inc, AV_RL32(&scratch[0]),

                    AV_RL16(&scratch[4]));

            break;

        case OPCODE_INIT_AUDIO_BUFFERS:

            av_dlog(NULL, "initialize audio buffers\n");

            if ((opcode_version > 1) || (opcode_size > 10)) {

                av_dlog(NULL, "bad init_audio_buffers opcode\n");

                chunk_type = CHUNK_BAD;

                break;

            }

            if (avio_read(pb, scratch, opcode_size) !=

                opcode_size) {

                chunk_type = CHUNK_BAD;

                break;

            }

            s->audio_sample_rate = AV_RL16(&scratch[4]);

            audio_flags = AV_RL16(&scratch[2]);

            /* bit 0 of the flags: 0 = mono, 1 = stereo */

            s->audio_channels = (audio_flags & 1) + 1;

            /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */

            s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;

            /* bit 2 indicates compressed audio in version 1 opcode */

            if ((opcode_version == 1) && (audio_flags & 0x4))

                s->audio_type = AV_CODEC_ID_INTERPLAY_DPCM;

            else if (s->audio_bits == 16)

                s->audio_type = AV_CODEC_ID_PCM_S16LE;

            else

                s->audio_type = AV_CODEC_ID_PCM_U8;

            av_dlog(NULL, "audio: %d bits, %d Hz, %s, %s format\n",

                    s->audio_bits, s->audio_sample_rate,

                    (s->audio_channels == 2) ? "stereo" : "mono",

                    (s->audio_type == AV_CODEC_ID_INTERPLAY_DPCM) ?

                    "Interplay audio" : "PCM");

            break;

        case OPCODE_START_STOP_AUDIO:

            av_dlog(NULL, "start/stop audio\n");

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_INIT_VIDEO_BUFFERS:

            av_dlog(NULL, "initialize video buffers\n");

            if ((opcode_version > 2) || (opcode_size > 8) || opcode_size < 4

                || opcode_version == 2 && opcode_size < 8

            ) {

                av_dlog(NULL, "bad init_video_buffers opcode\n");

                chunk_type = CHUNK_BAD;

                break;

            }

            if (avio_read(pb, scratch, opcode_size) !=

                opcode_size) {

                chunk_type = CHUNK_BAD;

                break;

            }

            width  = AV_RL16(&scratch[0]) * 8;

            height = AV_RL16(&scratch[2]) * 8;

            if (width != s->video_width) {

                s->video_width = width;

                s->changed++;

            }

            if (height != s->video_height) {

                s->video_height = height;

                s->changed++;

            }

            if (opcode_version < 2 || !AV_RL16(&scratch[6])) {

                s->video_bpp = 8;

            } else {

                s->video_bpp = 16;

            }

            av_dlog(NULL, "video resolution: %d x %d\n",

                    s->video_width, s->video_height);

            break;

        case OPCODE_UNKNOWN_06:

        case OPCODE_UNKNOWN_0E:

        case OPCODE_UNKNOWN_10:

        case OPCODE_UNKNOWN_12:

        case OPCODE_UNKNOWN_13:

        case OPCODE_UNKNOWN_14:

        case OPCODE_UNKNOWN_15:

            av_dlog(NULL, "unknown (but documented) opcode %02X\n", opcode_type);

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_SEND_BUFFER:

            av_dlog(NULL, "send buffer\n");

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_AUDIO_FRAME:

            av_dlog(NULL, "audio frame\n");

            /* log position and move on for now */

            s->audio_chunk_offset = avio_tell(pb);

            s->audio_chunk_size = opcode_size;

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_SILENCE_FRAME:

            av_dlog(NULL, "silence frame\n");

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_INIT_VIDEO_MODE:

            av_dlog(NULL, "initialize video mode\n");

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_CREATE_GRADIENT:

            av_dlog(NULL, "create gradient\n");

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_SET_PALETTE:

            av_dlog(NULL, "set palette\n");

            /* check for the logical maximum palette size

             * (3 * 256 + 4 bytes) */

            if (opcode_size > 0x304) {

                av_dlog(NULL, "demux_ipmovie: set_palette opcode too large\n");

                chunk_type = CHUNK_BAD;

                break;

            }

            if (avio_read(pb, scratch, opcode_size) != opcode_size) {

                chunk_type = CHUNK_BAD;

                break;

            }

            /* load the palette into internal data structure */

            first_color = AV_RL16(&scratch[0]);

            last_color = first_color + AV_RL16(&scratch[2]) - 1;

            /* sanity check (since they are 16 bit values) */

            if ((first_color > 0xFF) || (last_color > 0xFF)) {

                av_dlog(NULL, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",

                    first_color, last_color);

                chunk_type = CHUNK_BAD;

                break;

            }

            j = 4;  /* offset of first palette data */

            for (i = first_color; i <= last_color; i++) {

                /* the palette is stored as a 6-bit VGA palette, thus each

                 * component is shifted up to a 8-bit range */

                r = scratch[j++] * 4;

                g = scratch[j++] * 4;

                b = scratch[j++] * 4;

                s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);

                s->palette[i] |= s->palette[i] >> 6 & 0x30303;

            }

            s->has_palette = 1;

            break;

        case OPCODE_SET_PALETTE_COMPRESSED:

            av_dlog(NULL, "set palette compressed\n");

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_SET_DECODING_MAP:

            av_dlog(NULL, "set decoding map\n");

            /* log position and move on for now */

            s->decode_map_chunk_offset = avio_tell(pb);

            s->decode_map_chunk_size = opcode_size;

            avio_skip(pb, opcode_size);

            break;

        case OPCODE_VIDEO_DATA:

            av_dlog(NULL, "set video data\n");

            /* log position and move on for now */

            s->video_chunk_offset = avio_tell(pb);

            s->video_chunk_size = opcode_size;

            avio_skip(pb, opcode_size);

            break;

        default:

            av_dlog(NULL, "*** unknown opcode type\n");

            chunk_type = CHUNK_BAD;

            break;

        }

    }

    /* make a note of where the stream is sitting */

    s->next_chunk_offset = avio_tell(pb);

    /* dispatch the first of any pending packets */

    if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))

        chunk_type = load_ipmovie_packet(s, pb, pkt);

    return chunk_type;

}


static av_cold int svq1_encode_end(AVCodecContext *avctx)

{

    SVQ1EncContext *const s = avctx->priv_data;

    int i;

    av_log(avctx, AV_LOG_DEBUG, "RD: %f\n",

           s->rd_total / (double)(avctx->width * avctx->height *

                                  avctx->frame_number));

    s->m.mb_type = NULL;

    ff_mpv_common_end(&s->m);

    av_freep(&s->m.me.scratchpad);

    av_freep(&s->m.me.map);

    av_freep(&s->m.me.score_map);

    av_freep(&s->mb_type);

    av_freep(&s->dummy);

    av_freep(&s->scratchbuf);

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

        av_freep(&s->motion_val8[i]);

        av_freep(&s->motion_val16[i]);

    }

    av_frame_free(&s->current_picture);

    av_frame_free(&s->last_picture);

    av_frame_free(&avctx->coded_frame);

    return 0;

}


void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],

                            int is_mpeg12)

{

    const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;

#if FF_API_XVMC

FF_DISABLE_DEPRECATION_WARNINGS

    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){

        ff_xvmc_decode_mb(s);//xvmc uses pblocks

        return;

    }

FF_ENABLE_DEPRECATION_WARNINGS

#endif /* FF_API_XVMC */

    if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {

       /* print DCT coefficients */

       int i,j;

       av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);

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

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

               av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);

           }

           av_log(s->avctx, AV_LOG_DEBUG, "\n");

       }

    }

    s->current_picture.qscale_table[mb_xy] = s->qscale;

    /* update DC predictors for P macroblocks */

    if (!s->mb_intra) {

        if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {

            if(s->mbintra_table[mb_xy])

                ff_clean_intra_table_entries(s);

        } else {

            s->last_dc[0] =

            s->last_dc[1] =

            s->last_dc[2] = 128 << s->intra_dc_precision;

        }

    }

    else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))

        s->mbintra_table[mb_xy]=1;

    if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc

        uint8_t *dest_y, *dest_cb, *dest_cr;

        int dct_linesize, dct_offset;

        op_pixels_func (*op_pix)[4];

        qpel_mc_func (*op_qpix)[16];

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

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

        const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;

        const int block_size = 8;

        /* avoid copy if macroblock skipped in last frame too */

        /* skip only during decoding as we might trash the buffers during encoding a bit */

        if(!s->encoding){

            uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];

            if (s->mb_skipped) {

                s->mb_skipped= 0;

                assert(s->pict_type!=AV_PICTURE_TYPE_I);

                *mbskip_ptr = 1;

            } else if(!s->current_picture.reference) {

                *mbskip_ptr = 1;

            } else{

                *mbskip_ptr = 0; /* not skipped */

            }

        }

        dct_linesize = linesize << s->interlaced_dct;

        dct_offset   = s->interlaced_dct ? linesize : linesize * block_size;

        if(readable){

            dest_y=  s->dest[0];

            dest_cb= s->dest[1];

            dest_cr= s->dest[2];

        }else{

            dest_y = s->b_scratchpad;

            dest_cb= s->b_scratchpad+16*linesize;

            dest_cr= s->b_scratchpad+32*linesize;

        }

        if (!s->mb_intra) {

            /* motion handling */

            /* decoding or more than one mb_type (MC was already done otherwise) */

            if(!s->encoding){

                if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {

                    if (s->mv_dir & MV_DIR_FORWARD) {

                        ff_thread_await_progress(&s->last_picture_ptr->tf,

                                                 ff_MPV_lowest_referenced_row(s, 0),

                                                 0);

                    }

                    if (s->mv_dir & MV_DIR_BACKWARD) {

                        ff_thread_await_progress(&s->next_picture_ptr->tf,

                                                 ff_MPV_lowest_referenced_row(s, 1),

                                                 0);

                    }

                }

                op_qpix= s->me.qpel_put;

                if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){

                    op_pix = s->hdsp.put_pixels_tab;

                }else{

                    op_pix = s->hdsp.put_no_rnd_pixels_tab;

                }

                if (s->mv_dir & MV_DIR_FORWARD) {

                    ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);

                    op_pix = s->hdsp.avg_pixels_tab;

                    op_qpix= s->me.qpel_avg;

                }

                if (s->mv_dir & MV_DIR_BACKWARD) {

                    ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);

                }

            }

            /* skip dequant / idct if we are really late ;) */

            if(s->avctx->skip_idct){

                if(  (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)

                   ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)

                   || s->avctx->skip_idct >= AVDISCARD_ALL)

                    goto skip_idct;

            }

            /* add dct residue */

            if(s->encoding || !(   s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO

                                || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){

                add_dequant_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);

                add_dequant_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);

                add_dequant_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);

                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);

                if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){

                    if (s->chroma_y_shift){

                        add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);

                        add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);

                    }else{

                        dct_linesize >>= 1;

                        dct_offset >>=1;

                        add_dequant_dct(s, block[4], 4, dest_cb,              dct_linesize, s->chroma_qscale);

                        add_dequant_dct(s, block[5], 5, dest_cr,              dct_linesize, s->chroma_qscale);

                        add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);

                        add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);

                    }

                }

            } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){

                add_dct(s, block[0], 0, dest_y                          , dct_linesize);

                add_dct(s, block[1], 1, dest_y              + block_size, dct_linesize);

                add_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize);

                add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);

                if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){

                    if(s->chroma_y_shift){//Chroma420

                        add_dct(s, block[4], 4, dest_cb, uvlinesize);

                        add_dct(s, block[5], 5, dest_cr, uvlinesize);

                    }else{

                        //chroma422

                        dct_linesize = uvlinesize << s->interlaced_dct;

                        dct_offset   = s->interlaced_dct ? uvlinesize : uvlinesize * 8;

                        add_dct(s, block[4], 4, dest_cb, dct_linesize);

                        add_dct(s, block[5], 5, dest_cr, dct_linesize);

                        add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);

                        add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);

                        if(!s->chroma_x_shift){//Chroma444

                            add_dct(s, block[8], 8, dest_cb+8, dct_linesize);

                            add_dct(s, block[9], 9, dest_cr+8, dct_linesize);

                            add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);

                            add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);

                        }

                    }

                }//fi gray

            }

            else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {

                ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);

            }

        } else {

            /* dct only in intra block */

            if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){

                put_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);

                put_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);

                put_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);

                put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);

                if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){

                    if(s->chroma_y_shift){

                        put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);

                        put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);

                    }else{

                        dct_offset >>=1;

                        dct_linesize >>=1;

                        put_dct(s, block[4], 4, dest_cb,              dct_linesize, s->chroma_qscale);

                        put_dct(s, block[5], 5, dest_cr,              dct_linesize, s->chroma_qscale);

                        put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);

                        put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);

                    }

                }

            }else{

                s->dsp.idct_put(dest_y                          , dct_linesize, block[0]);

                s->dsp.idct_put(dest_y              + block_size, dct_linesize, block[1]);

                s->dsp.idct_put(dest_y + dct_offset             , dct_linesize, block[2]);

                s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);

                if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){

                    if(s->chroma_y_shift){

                        s->dsp.idct_put(dest_cb, uvlinesize, block[4]);

                        s->dsp.idct_put(dest_cr, uvlinesize, block[5]);

                    }else{

                        dct_linesize = uvlinesize << s->interlaced_dct;

                        dct_offset   = s->interlaced_dct ? uvlinesize : uvlinesize * 8;

                        s->dsp.idct_put(dest_cb,              dct_linesize, block[4]);

                        s->dsp.idct_put(dest_cr,              dct_linesize, block[5]);

                        s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);

                        s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);

                        if(!s->chroma_x_shift){//Chroma444

                            s->dsp.idct_put(dest_cb + 8,              dct_linesize, block[8]);

                            s->dsp.idct_put(dest_cr + 8,              dct_linesize, block[9]);

                            s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);

                            s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);

                        }

                    }

                }//gray

            }

        }

skip_idct:

        if(!readable){

            s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y ,   linesize,16);

            s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);

            s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);

        }

    }

}


static void vfio_start_irqfd_injection(SysBusDevice *sbdev, qemu_irq irq)

{

    VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);

    VFIOINTp *intp;

    if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||

        !vdev->irqfd_allowed) {

        goto fail_irqfd;

    }

    QLIST_FOREACH(intp, &vdev->intp_list, next) {

        if (intp->qemuirq == irq) {

            break;

        }

    }

    assert(intp);

    if (kvm_irqchip_add_irqfd_notifier(kvm_state, intp->interrupt,

                                   intp->unmask, irq) < 0) {

        goto fail_irqfd;

    }

    if (vfio_set_trigger_eventfd(intp, NULL) < 0) {

        goto fail_vfio;

    }

    if (vfio_set_resample_eventfd(intp) < 0) {

        goto fail_vfio;

    }

    intp->kvm_accel = true;

    trace_vfio_platform_start_irqfd_injection(intp->pin,

                                     event_notifier_get_fd(intp->interrupt),

                                     event_notifier_get_fd(intp->unmask));

    return;

fail_vfio:

    kvm_irqchip_remove_irqfd_notifier(kvm_state, intp->interrupt, irq);

    error_report("vfio: failed to start eventfd signaling for IRQ %d: %m",

                 intp->pin);

    abort();

fail_irqfd:

    vfio_start_eventfd_injection(sbdev, irq);

    return;

}


static inline void RENAME(BEToUV)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src1, const uint8_t *src2, int width, uint32_t *unused)

{

#if COMPILE_TEMPLATE_MMX

    __asm__ volatile(

        "movq "MANGLE(bm01010101)", %%mm4           \n\t"

        "mov                    %0, %%"REG_a"       \n\t"

        "1:                                         \n\t"

        "movq    (%1, %%"REG_a",2), %%mm0           \n\t"

        "movq   8(%1, %%"REG_a",2), %%mm1           \n\t"

        "movq    (%2, %%"REG_a",2), %%mm2           \n\t"

        "movq   8(%2, %%"REG_a",2), %%mm3           \n\t"

        "pand                %%mm4, %%mm0           \n\t"

        "pand                %%mm4, %%mm1           \n\t"

        "pand                %%mm4, %%mm2           \n\t"

        "pand                %%mm4, %%mm3           \n\t"

        "packuswb            %%mm1, %%mm0           \n\t"

        "packuswb            %%mm3, %%mm2           \n\t"

        "movq                %%mm0, (%3, %%"REG_a") \n\t"

        "movq                %%mm2, (%4, %%"REG_a") \n\t"

        "add                    $8, %%"REG_a"       \n\t"

        " js                    1b                  \n\t"

        : : "g" ((x86_reg)-width), "r" (src1+width*2), "r" (src2+width*2), "r" (dstU+width), "r" (dstV+width)

        : "%"REG_a

    );

#else

    int i;

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

        dstU[i]= src1[2*i];

        dstV[i]= src2[2*i];

    }

#endif

}


static inline void RENAME(rgb24to16)(const uint8_t *src, uint8_t *dst, int src_size)

{

    const uint8_t *s = src;

    const uint8_t *end;

    const uint8_t *mm_end;

    uint16_t *d = (uint16_t *)dst;

    end = s + src_size;

    __asm__ volatile(PREFETCH"    %0"::"m"(*src):"memory");

    __asm__ volatile(

        "movq         %0, %%mm7     \n\t"

        "movq         %1, %%mm6     \n\t"

        ::"m"(red_16mask),"m"(green_16mask));

    mm_end = end - 15;

    while (s < mm_end) {

        __asm__ volatile(

            PREFETCH"    32%1           \n\t"

            "movd          %1, %%mm0    \n\t"

            "movd         3%1, %%mm3    \n\t"

            "punpckldq    6%1, %%mm0    \n\t"

            "punpckldq    9%1, %%mm3    \n\t"

            "movq       %%mm0, %%mm1    \n\t"

            "movq       %%mm0, %%mm2    \n\t"

            "movq       %%mm3, %%mm4    \n\t"

            "movq       %%mm3, %%mm5    \n\t"

            "psllq         $8, %%mm0    \n\t"

            "psllq         $8, %%mm3    \n\t"

            "pand       %%mm7, %%mm0    \n\t"

            "pand       %%mm7, %%mm3    \n\t"

            "psrlq         $5, %%mm1    \n\t"

            "psrlq         $5, %%mm4    \n\t"

            "pand       %%mm6, %%mm1    \n\t"

            "pand       %%mm6, %%mm4    \n\t"

            "psrlq        $19, %%mm2    \n\t"

            "psrlq        $19, %%mm5    \n\t"

            "pand          %2, %%mm2    \n\t"

            "pand          %2, %%mm5    \n\t"

            "por        %%mm1, %%mm0    \n\t"

            "por        %%mm4, %%mm3    \n\t"

            "por        %%mm2, %%mm0    \n\t"

            "por        %%mm5, %%mm3    \n\t"

            "psllq        $16, %%mm3    \n\t"

            "por        %%mm3, %%mm0    \n\t"

            MOVNTQ"     %%mm0, %0       \n\t"

            :"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");

        d += 4;

        s += 12;

    }

    __asm__ volatile(SFENCE:::"memory");

    __asm__ volatile(EMMS:::"memory");

    while (s < end) {

        const int r = *s++;

        const int g = *s++;

        const int b = *s++;

        *d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);

    }

}


int socket_connect(SocketAddress *addr, Error **errp,

                   NonBlockingConnectHandler *callback, void *opaque)

{

    QemuOpts *opts;

    int fd;

    opts = qemu_opts_create_nofail(&socket_optslist);

    switch (addr->kind) {

    case SOCKET_ADDRESS_KIND_INET:

        inet_addr_to_opts(opts, addr->inet);

        fd = inet_connect_opts(opts, errp, callback, opaque);

        break;

    case SOCKET_ADDRESS_KIND_UNIX:

        qemu_opt_set(opts, "path", addr->q_unix->path);

        fd = unix_connect_opts(opts, errp, callback, opaque);

        break;

    case SOCKET_ADDRESS_KIND_FD:

        fd = monitor_get_fd(cur_mon, addr->fd->str, errp);

        if (callback) {

            callback(fd, opaque);

        }

        break;

    default:

        abort();

    }

    qemu_opts_del(opts);

    return fd;

}


static void dump_json_image_info(ImageInfo *info)

{

    Error *local_err = NULL;

    QString *str;

    QmpOutputVisitor *ov = qmp_output_visitor_new();

    QObject *obj;

    visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);

    obj = qmp_output_get_qobject(ov);

    str = qobject_to_json_pretty(obj);

    assert(str != NULL);

    printf("%s\n", qstring_get_str(str));

    qobject_decref(obj);

    qmp_output_visitor_cleanup(ov);

    QDECREF(str);

}


static av_cold int vtenc_init(AVCodecContext *avctx)

{

    CFMutableDictionaryRef enc_info;

    CFMutableDictionaryRef pixel_buffer_info;

    CMVideoCodecType       codec_type;

    VTEncContext           *vtctx = avctx->priv_data;

    CFStringRef            profile_level;

    CFBooleanRef           has_b_frames_cfbool;

    CFNumberRef            gamma_level = NULL;

    int                    status;

    pthread_once(&once_ctrl, loadVTEncSymbols);

    codec_type = get_cm_codec_type(avctx->codec_id);

    if (!codec_type) {

        av_log(avctx, AV_LOG_ERROR, "Error: no mapping for AVCodecID %d\n", avctx->codec_id);

        return AVERROR(EINVAL);

    }

    vtctx->has_b_frames = avctx->max_b_frames > 0;

    if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){

        av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");

        vtctx->has_b_frames = false;

    }

    if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {

        av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");

        vtctx->entropy = VT_ENTROPY_NOT_SET;

    }

    if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);

    vtctx->session = NULL;

    enc_info = CFDictionaryCreateMutable(

        kCFAllocatorDefault,

        20,

        &kCFCopyStringDictionaryKeyCallBacks,

        &kCFTypeDictionaryValueCallBacks

    );

    if (!enc_info) return AVERROR(ENOMEM);

#if !TARGET_OS_IPHONE

    if (!vtctx->allow_sw) {

        CFDictionarySetValue(enc_info,

                             compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,

                             kCFBooleanTrue);

    } else {

        CFDictionarySetValue(enc_info,

                             compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,

                             kCFBooleanTrue);

    }

#endif

    if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {

        status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);

        if (status)

            goto init_cleanup;

    } else {

        pixel_buffer_info = NULL;

    }

    pthread_mutex_init(&vtctx->lock, NULL);

    pthread_cond_init(&vtctx->cv_sample_sent, NULL);

    vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;

    get_cv_transfer_function(avctx, &vtctx->transfer_function, &gamma_level);

    get_cv_ycbcr_matrix(avctx, &vtctx->ycbcr_matrix);

    get_cv_color_primaries(avctx, &vtctx->color_primaries);

    if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {

        status = vtenc_populate_extradata(avctx,

                                          codec_type,

                                          profile_level,

                                          gamma_level,

                                          enc_info,

                                          pixel_buffer_info);

        if (status)

            goto init_cleanup;

    }

    status = vtenc_create_encoder(avctx,

                                  codec_type,

                                  profile_level,

                                  gamma_level,

                                  enc_info,

                                  pixel_buffer_info,

                                  &vtctx->session);

    if (status < 0)

        goto init_cleanup;

    status = VTSessionCopyProperty(vtctx->session,

                                   kVTCompressionPropertyKey_AllowFrameReordering,

                                   kCFAllocatorDefault,

                                   &has_b_frames_cfbool);

    if (!status) {

        //Some devices don't output B-frames for main profile, even if requested.

        vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);

        CFRelease(has_b_frames_cfbool);

    }

    avctx->has_b_frames = vtctx->has_b_frames;

init_cleanup:

    if (gamma_level)

        CFRelease(gamma_level);

    if (pixel_buffer_info)

        CFRelease(pixel_buffer_info);

    CFRelease(enc_info);

    return status;

}


bool qemu_run_timers(QEMUClock *clock)

{

    return qemu_clock_run_timers(clock->type);

}


void *cpu_physical_memory_map(target_phys_addr_t addr,

                              target_phys_addr_t *plen,

                              int is_write)

{

    return address_space_map(&address_space_memory, addr, plen, is_write);

}


static void virtio_pci_device_unplugged(DeviceState *d)

{

    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);

    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);

    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;

    virtio_pci_stop_ioeventfd(proxy);

    if (modern) {

        virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);

        virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);

        virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);

        virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);

        if (modern_pio) {

            virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);

        }

    }

}


static void apb_config_writel (void *opaque, target_phys_addr_t addr,

                               uint64_t val, unsigned size)

{

    APBState *s = opaque;

    APB_DPRINTF("%s: addr " TARGET_FMT_lx " val %" PRIx64 "\n", __func__, addr, val);

    switch (addr & 0xffff) {

    case 0x30 ... 0x4f: /* DMA error registers */

        /* XXX: not implemented yet */

        break;

    case 0x200 ... 0x20b: /* IOMMU */

        s->iommu[(addr & 0xf) >> 2] = val;

        break;

    case 0x20c ... 0x3ff: /* IOMMU flush */

        break;

    case 0xc00 ... 0xc3f: /* PCI interrupt control */

        if (addr & 4) {

            s->pci_irq_map[(addr & 0x3f) >> 3] &= PBM_PCI_IMR_MASK;

            s->pci_irq_map[(addr & 0x3f) >> 3] |= val & ~PBM_PCI_IMR_MASK;

        }

        break;

    case 0x1000 ... 0x1080: /* OBIO interrupt control */

        if (addr & 4) {

            s->obio_irq_map[(addr & 0xff) >> 3] &= PBM_PCI_IMR_MASK;

            s->obio_irq_map[(addr & 0xff) >> 3] |= val & ~PBM_PCI_IMR_MASK;

        }

        break;

    case 0x1400 ... 0x143f: /* PCI interrupt clear */

        if (addr & 4) {

            pci_apb_set_irq(s, (addr & 0x3f) >> 3, 0);

        }

        break;

    case 0x1800 ... 0x1860: /* OBIO interrupt clear */

        if (addr & 4) {

            pci_apb_set_irq(s, 0x20 | ((addr & 0xff) >> 3), 0);

        }

        break;

    case 0x2000 ... 0x202f: /* PCI control */

        s->pci_control[(addr & 0x3f) >> 2] = val;

        break;

    case 0xf020 ... 0xf027: /* Reset control */

        if (addr & 4) {

            val &= RESET_MASK;

            s->reset_control &= ~(val & RESET_WCMASK);

            s->reset_control |= val & RESET_WMASK;

            if (val & SOFT_POR) {

                s->nr_resets = 0;

                qemu_system_reset_request();

            } else if (val & SOFT_XIR) {

                qemu_system_reset_request();

            }

        }

        break;

    case 0x5000 ... 0x51cf: /* PIO/DMA diagnostics */

    case 0xa400 ... 0xa67f: /* IOMMU diagnostics */

    case 0xa800 ... 0xa80f: /* Interrupt diagnostics */

    case 0xf000 ... 0xf01f: /* FFB config, memory control */

        /* we don't care */

    default:

        break;

    }

}


static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,

                                       UINT8 *new_pic, UINT8 *old_pic, int pic_stride,

                                       int pred_x, int pred_y, UINT16 *mv_penalty, int quant,

                                       int xmin, int ymin, int xmax, int ymax, int shift)

{

    int next_dir=-1;

    for(;;){

        int d;

        const int dir= next_dir;

        const int x= best[0];

        const int y= best[1];

        next_dir=-1;

//printf("%d", dir);

        if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y  , 0)

        if(dir!=3 && y>ymin) CHECK_MV_DIR(x  , y-1, 1)

        if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y  , 2)

        if(dir!=1 && y<ymax) CHECK_MV_DIR(x  , y+1, 3)

        if(next_dir==-1){

            return dmin;

        }

    }

/*    for(;;){

        int d;

        const int x= best[0];

        const int y= best[1];

        const int last_min=dmin;

        if(x>xmin) CHECK_MV(x-1, y  )

        if(y>xmin) CHECK_MV(x  , y-1)

        if(x<xmax) CHECK_MV(x+1, y  )

        if(y<xmax) CHECK_MV(x  , y+1)

        if(x>xmin && y>ymin) CHECK_MV(x-1, y-1)

        if(x>xmin && y<ymax) CHECK_MV(x-1, y+1)

        if(x<xmax && y>ymin) CHECK_MV(x+1, y-1)

        if(x<xmax && y<ymax) CHECK_MV(x+1, y+1)

        if(x-1>xmin) CHECK_MV(x-2, y  )

        if(y-1>xmin) CHECK_MV(x  , y-2)

        if(x+1<xmax) CHECK_MV(x+2, y  )

        if(y+1<xmax) CHECK_MV(x  , y+2)

        if(x-1>xmin && y-1>ymin) CHECK_MV(x-2, y-2)

        if(x-1>xmin && y+1<ymax) CHECK_MV(x-2, y+2)

        if(x+1<xmax && y-1>ymin) CHECK_MV(x+2, y-2)

        if(x+1<xmax && y+1<ymax) CHECK_MV(x+2, y+2)

        if(dmin==last_min) return dmin;

    }

    */

}


pci_ebus_init1(PCIDevice *s)

{

    isa_bus_new(&s->qdev);

    pci_config_set_vendor_id(s->config, PCI_VENDOR_ID_SUN);

    pci_config_set_device_id(s->config, PCI_DEVICE_ID_SUN_EBUS);

    s->config[0x04] = 0x06; // command = bus master, pci mem

    s->config[0x05] = 0x00;

    s->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error

    s->config[0x07] = 0x03; // status = medium devsel

    s->config[0x08] = 0x01; // revision

    s->config[0x09] = 0x00; // programming i/f

    pci_config_set_class(s->config, PCI_CLASS_BRIDGE_OTHER);

    s->config[0x0D] = 0x0a; // latency_timer

    pci_register_bar(s, 0, 0x1000000, PCI_BASE_ADDRESS_SPACE_MEMORY,

                           ebus_mmio_mapfunc);

    pci_register_bar(s, 1, 0x800000,  PCI_BASE_ADDRESS_SPACE_MEMORY,

                           ebus_mmio_mapfunc);

    return 0;

}


static int v9fs_synth_fsync(FsContext *ctx, int fid_type,

                            V9fsFidOpenState *fs, int datasync)

{

    errno = ENOSYS;

    return 0;

}


int64_t qmp_guest_fsfreeze_freeze(Error **err)

{

    error_set(err, QERR_UNSUPPORTED);

    return 0;

}


static void raw_probe_alignment(BlockDriverState *bs)

{

    BDRVRawState *s = bs->opaque;

    DWORD sectorsPerCluster, freeClusters, totalClusters, count;

    DISK_GEOMETRY_EX dg;

    BOOL status;

    if (s->type == FTYPE_CD) {

        bs->request_alignment = 2048;

        return;

    }

    if (s->type == FTYPE_HARDDISK) {

        status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,

                                 NULL, 0, &dg, sizeof(dg), &count, NULL);

        if (status != 0) {

            bs->request_alignment = dg.Geometry.BytesPerSector;

            return;

        }

        /* try GetDiskFreeSpace too */

    }

    if (s->drive_path[0]) {

        GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,

                         &dg.Geometry.BytesPerSector,

                         &freeClusters, &totalClusters);

        bs->request_alignment = dg.Geometry.BytesPerSector;

    }

}


static inline void gen_neon_narrow_satu(int size, TCGv dest, TCGv src)

{

    switch (size) {

    case 0: gen_helper_neon_narrow_sat_u8(dest, cpu_env, src); break;

    case 1: gen_helper_neon_narrow_sat_u16(dest, cpu_env, src); break;

    case 2: gen_helper_neon_narrow_sat_u32(dest, cpu_env, src); break;

    default: abort();

    }

}


static void block_dirty_bitmap_clear_prepare(BlkActionState *common,

                                             Error **errp)

{

    BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,

                                             common, common);

    BlockDirtyBitmap *action;

    if (action_check_completion_mode(common, errp) < 0) {

        return;

    }

    action = common->action->u.block_dirty_bitmap_clear.data;

    state->bitmap = block_dirty_bitmap_lookup(action->node,

                                              action->name,

                                              &state->bs,

                                              &state->aio_context,

                                              errp);

    if (!state->bitmap) {

        return;

    }

    if (bdrv_dirty_bitmap_frozen(state->bitmap)) {

        error_setg(errp, "Cannot modify a frozen bitmap");

        return;

    } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {

        error_setg(errp, "Cannot clear a disabled bitmap");

        return;

    }

    bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);

    /* AioContext is released in .clean() */

}


static uint64_t bonito_ldma_readl(void *opaque, target_phys_addr_t addr,

                                  unsigned size)

{

    uint32_t val;

    PCIBonitoState *s = opaque;

    val = ((uint32_t *)(&s->bonldma))[addr/sizeof(uint32_t)];

    return val;

}


static void tcg_out_movi(TCGContext *s, TCGType type,

                         TCGReg t0, tcg_target_long arg)

{

    uint8_t *old_code_ptr = s->code_ptr;

    uint32_t arg32 = arg;

    if (type == TCG_TYPE_I32 || arg == arg32) {

        tcg_out_op_t(s, INDEX_op_movi_i32);

        tcg_out_r(s, t0);

        tcg_out32(s, arg32);

    } else {

        assert(type == TCG_TYPE_I64);

#if TCG_TARGET_REG_BITS == 64

        tcg_out_op_t(s, INDEX_op_movi_i64);

        tcg_out_r(s, t0);

        tcg_out64(s, arg);

#else

        TODO();

#endif

    }

    old_code_ptr[1] = s->code_ptr - old_code_ptr;

}


static int mjpeg_probe(AVProbeData *p)

{

    int i;

    int state = -1;

    int nb_invalid = 0;

    int nb_frames = 0;

    for (i=0; i<p->buf_size-2; i++) {

        int c;

        if (p->buf[i] != 0xFF)

            continue;

        c = p->buf[i+1];

        switch (c) {

        case 0xD8:

            state = 0xD8;

            break;

        case 0xC0:

        case 0xC1:

        case 0xC2:

        case 0xC3:

        case 0xC5:

        case 0xC6:

        case 0xC7:

        case 0xF7:

            if (state == 0xD8) {

                state = 0xC0;

            } else

                nb_invalid++;

            break;

        case 0xDA:

            if (state == 0xC0) {

                state = 0xDA;

            } else

                nb_invalid++;

            break;

        case 0xD9:

            if (state == 0xDA) {

                state = 0xD9;

                nb_frames++;

            } else

                nb_invalid++;

            break;

        default:

            if (  (c >= 0x02 && c <= 0xBF)

                || c == 0xC8) {

                nb_invalid++;

            }

        }

    }

    if (nb_invalid*4 + 1 < nb_frames) {

        static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n\r\n";

        int i;

        for (i=0; i<FFMIN(p->buf_size - sizeof(ct_jpeg), 100); i++)

            if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))

                return AVPROBE_SCORE_EXTENSION;

        if (nb_invalid == 0 && nb_frames > 2)

            return AVPROBE_SCORE_EXTENSION / 2;

        return AVPROBE_SCORE_EXTENSION / 4;

    }

    return 0;

}


static void omap_mcbsp_writew(void *opaque, target_phys_addr_t addr,

                uint32_t value)

{

    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;

    int offset = addr & OMAP_MPUI_REG_MASK;

    if (offset == 0x04) {				/* DXR */

        if (((s->xcr[0] >> 5) & 7) < 3)			/* XWDLEN1 */

            return;

        if (s->tx_req > 3) {

            s->tx_req -= 4;

            if (s->codec && s->codec->cts) {

                s->codec->out.fifo[s->codec->out.len ++] =

                        (value >> 24) & 0xff;

                s->codec->out.fifo[s->codec->out.len ++] =

                        (value >> 16) & 0xff;

                s->codec->out.fifo[s->codec->out.len ++] =

                        (value >> 8) & 0xff;

                s->codec->out.fifo[s->codec->out.len ++] =

                        (value >> 0) & 0xff;

            }

            if (s->tx_req < 4)

                omap_mcbsp_tx_done(s);

        } else

            printf("%s: Tx FIFO overrun\n", __FUNCTION__);

        return;

    }

    omap_badwidth_write16(opaque, addr, value);

}


static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)

{

    FlatView *view;

    FlatRange *fr;

    CoalescedMemoryRange *cmr;

    AddrRange tmp;

    MemoryRegionSection section;

    view = as->current_map;

    FOR_EACH_FLAT_RANGE(fr, view) {

        if (fr->mr == mr) {

            section = (MemoryRegionSection) {

                .address_space = as,

                .offset_within_address_space = int128_get64(fr->addr.start),

                .size = fr->addr.size,

            };

            MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, &section,

                                 int128_get64(fr->addr.start),

                                 int128_get64(fr->addr.size));

            QTAILQ_FOREACH(cmr, &mr->coalesced, link) {

                tmp = addrrange_shift(cmr->addr,

                                      int128_sub(fr->addr.start,

                                                 int128_make64(fr->offset_in_region)));

                if (!addrrange_intersects(tmp, fr->addr)) {

                    continue;

                }

                tmp = addrrange_intersection(tmp, fr->addr);

                MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, &section,

                                     int128_get64(tmp.start),

                                     int128_get64(tmp.size));

            }

        }

    }

}


static void lance_cleanup(NetClientState *nc)

{

    PCNetState *d = qemu_get_nic_opaque(nc);

    pcnet_common_cleanup(d);

}


static int kvm_put_sregs(CPUState *env)

{

    struct kvm_sregs sregs;

    memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap));

    if (env->interrupt_injected >= 0) {

        sregs.interrupt_bitmap[env->interrupt_injected / 64] |=

                (uint64_t)1 << (env->interrupt_injected % 64);

    }

    if ((env->eflags & VM_MASK)) {

	    set_v8086_seg(&sregs.cs, &env->segs[R_CS]);

	    set_v8086_seg(&sregs.ds, &env->segs[R_DS]);

	    set_v8086_seg(&sregs.es, &env->segs[R_ES]);

	    set_v8086_seg(&sregs.fs, &env->segs[R_FS]);

	    set_v8086_seg(&sregs.gs, &env->segs[R_GS]);

	    set_v8086_seg(&sregs.ss, &env->segs[R_SS]);

    } else {

	    set_seg(&sregs.cs, &env->segs[R_CS]);

	    set_seg(&sregs.ds, &env->segs[R_DS]);

	    set_seg(&sregs.es, &env->segs[R_ES]);

	    set_seg(&sregs.fs, &env->segs[R_FS]);

	    set_seg(&sregs.gs, &env->segs[R_GS]);

	    set_seg(&sregs.ss, &env->segs[R_SS]);

    }

    set_seg(&sregs.tr, &env->tr);

    set_seg(&sregs.ldt, &env->ldt);

    sregs.idt.limit = env->idt.limit;

    sregs.idt.base = env->idt.base;

    sregs.gdt.limit = env->gdt.limit;

    sregs.gdt.base = env->gdt.base;

    sregs.cr0 = env->cr[0];

    sregs.cr2 = env->cr[2];

    sregs.cr3 = env->cr[3];

    sregs.cr4 = env->cr[4];

    sregs.cr8 = cpu_get_apic_tpr(env->apic_state);

    sregs.apic_base = cpu_get_apic_base(env->apic_state);

    sregs.efer = env->efer;

    return kvm_vcpu_ioctl(env, KVM_SET_SREGS, &sregs);

}


static int check_refcounts_l1(BlockDriverState *bs,

                              BdrvCheckResult *res,

                              uint16_t *refcount_table,

                              int64_t refcount_table_size,

                              int64_t l1_table_offset, int l1_size,

                              int flags)

{

    BDRVQcowState *s = bs->opaque;

    uint64_t *l1_table = NULL, l2_offset, l1_size2;

    int i, ret;

    l1_size2 = l1_size * sizeof(uint64_t);

    /* Mark L1 table as used */

    ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,

                        l1_table_offset, l1_size2);

    if (ret < 0) {

        goto fail;

    }

    /* Read L1 table entries from disk */

    if (l1_size2 > 0) {

        l1_table = g_try_malloc(l1_size2);

        if (l1_table == NULL) {

            ret = -ENOMEM;

            res->check_errors++;

            goto fail;

        }

        ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2);

        if (ret < 0) {

            fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");

            res->check_errors++;

            goto fail;

        }

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

            be64_to_cpus(&l1_table[i]);

    }

    /* Do the actual checks */

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

        l2_offset = l1_table[i];

        if (l2_offset) {

            /* Mark L2 table as used */

            l2_offset &= L1E_OFFSET_MASK;

            ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,

                                l2_offset, s->cluster_size);

            if (ret < 0) {

                goto fail;

            }

            /* L2 tables are cluster aligned */

            if (offset_into_cluster(s, l2_offset)) {

                fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "

                    "cluster aligned; L1 entry corrupted\n", l2_offset);

                res->corruptions++;

            }

            /* Process and check L2 entries */

            ret = check_refcounts_l2(bs, res, refcount_table,

                                     refcount_table_size, l2_offset, flags);

            if (ret < 0) {

                goto fail;

            }

        }

    }

    g_free(l1_table);

    return 0;

fail:

    g_free(l1_table);

    return ret;

}


static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,

                                        int64_t sector_num, int nb_sectors,

                                        QEMUIOVector *iov)

{

    IscsiLun *iscsilun = bs->opaque;

    struct IscsiTask iTask;

    uint64_t lba;

    uint32_t num_sectors;

    int fua;

    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {

        return -EINVAL;

    }

    if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) {

        error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len "

                     "of %d sectors", nb_sectors, bs->bl.max_transfer_length);

        return -EINVAL;

    }

    lba = sector_qemu2lun(sector_num, iscsilun);

    num_sectors = sector_qemu2lun(nb_sectors, iscsilun);

    iscsi_co_init_iscsitask(iscsilun, &iTask);

retry:

    fua = iscsilun->dpofua && !bdrv_enable_write_cache(bs);

    iTask.force_next_flush = !fua;

    if (iscsilun->use_16_for_rw) {

        iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,

                                        NULL, num_sectors * iscsilun->block_size,

                                        iscsilun->block_size, 0, 0, fua, 0, 0,

                                        iscsi_co_generic_cb, &iTask);

    } else {

        iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,

                                        NULL, num_sectors * iscsilun->block_size,

                                        iscsilun->block_size, 0, 0, fua, 0, 0,

                                        iscsi_co_generic_cb, &iTask);

    }

    if (iTask.task == NULL) {

        return -ENOMEM;

    }

    scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,

                          iov->niov);

    while (!iTask.complete) {

        iscsi_set_events(iscsilun);

        qemu_coroutine_yield();

    }

    if (iTask.task != NULL) {

        scsi_free_scsi_task(iTask.task);

        iTask.task = NULL;

    }

    if (iTask.do_retry) {

        iTask.complete = 0;

        goto retry;

    }

    if (iTask.status != SCSI_STATUS_GOOD) {

        return iTask.err_code;

    }

    iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);

    return 0;

}


static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,

        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,

        BlockDriverCompletionFunc *cb, void *opaque, int type)

{

    BDRVRawState *s = bs->opaque;

    if (fd_open(bs) < 0)

        return NULL;

    /*

     * If O_DIRECT is used the buffer needs to be aligned on a sector

     * boundary.  Check if this is the case or tell the low-level

     * driver that it needs to copy the buffer.

     */

    if ((bs->open_flags & BDRV_O_NOCACHE)) {

        if (!qiov_is_aligned(bs, qiov)) {

            type |= QEMU_AIO_MISALIGNED;

#ifdef CONFIG_LINUX_AIO

        } else if (s->use_aio) {

            return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,

                               nb_sectors, cb, opaque, type);

#endif

        }

    }

    return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,

                       cb, opaque, type);

}


static void encode_header(SnowContext *s){

    int plane_index, level, orientation;

    put_cabac(&s->c, s->header_state, s->keyframe); // state clearing stuff?

    if(s->keyframe){

        put_symbol(&s->c, s->header_state, s->version, 0);

        put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);

        put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);

        put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);

        put_symbol(&s->c, s->header_state, s->colorspace_type, 0);

        put_symbol(&s->c, s->header_state, s->b_width, 0);

        put_symbol(&s->c, s->header_state, s->b_height, 0);

        put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);

        put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);

        put_cabac(&s->c, s->header_state, s->spatial_scalability);

//        put_cabac(&s->c, s->header_state, s->rate_scalability);

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

            for(level=0; level<s->spatial_decomposition_count; level++){

                for(orientation=level ? 1:0; orientation<4; orientation++){

                    if(orientation==2) continue;

                    put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);

                }

            }

        }

    }

    put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);

    put_symbol(&s->c, s->header_state, s->qlog, 1);

    put_symbol(&s->c, s->header_state, s->mv_scale, 0);

    put_symbol(&s->c, s->header_state, s->qbias, 1);

}


void bdrv_iterate_format(void (*it)(void *opaque, const char *name),

                         void *opaque)

{

    BlockDriver *drv;

    int count = 0;

    int i;

    const char **formats = NULL;

    QLIST_FOREACH(drv, &bdrv_drivers, list) {

        if (drv->format_name) {

            bool found = false;

            int i = count;

            while (formats && i && !found) {

                found = !strcmp(formats[--i], drv->format_name);

            }

            if (!found) {

                formats = g_renew(const char *, formats, count + 1);

                formats[count++] = drv->format_name;

            }

        }

    }

    qsort(formats, count, sizeof(formats[0]), qsort_strcmp);

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

        it(opaque, formats[i]);

    }

    g_free(formats);

}


static int tcg_target_const_match(tcg_target_long val, TCGType type,

                                  const TCGArgConstraint *arg_ct)

{

    int ct = arg_ct->ct;

    if (ct & TCG_CT_CONST) {

        return 1;

    }

    if (type == TCG_TYPE_I32) {

        val = (int32_t)val;

    }

    /* The following are mutually exclusive.  */

    if (ct & TCG_CT_CONST_S16) {

        return val == (int16_t)val;

    } else if (ct & TCG_CT_CONST_S32) {

        return val == (int32_t)val;

    } else if (ct & TCG_CT_CONST_ADLI) {

        return tcg_match_add2i(type, val);

    } else if (ct & TCG_CT_CONST_ORI) {

        return tcg_match_ori(type, val);

    } else if (ct & TCG_CT_CONST_XORI) {

        return tcg_match_xori(type, val);

    } else if (ct & TCG_CT_CONST_U31) {

        return val >= 0 && val <= 0x7fffffff;

    } else if (ct & TCG_CT_CONST_ZERO) {

        return val == 0;

    }

    return 0;

}


static void cpu_exec_nocache(CPUArchState *env, int max_cycles,

                             TranslationBlock *orig_tb)

{

    CPUState *cpu = ENV_GET_CPU(env);

    TranslationBlock *tb;

    target_ulong pc = orig_tb->pc;

    target_ulong cs_base = orig_tb->cs_base;

    uint64_t flags = orig_tb->flags;

    /* Should never happen.

       We only end up here when an existing TB is too long.  */

    if (max_cycles > CF_COUNT_MASK)

        max_cycles = CF_COUNT_MASK;

    /* tb_gen_code can flush our orig_tb, invalidate it now */

    tb_phys_invalidate(orig_tb, -1);

    tb = tb_gen_code(cpu, pc, cs_base, flags,

                     max_cycles);

    cpu->current_tb = tb;

    /* execute the generated code */

    trace_exec_tb_nocache(tb, tb->pc);

    cpu_tb_exec(cpu, tb->tc_ptr);

    cpu->current_tb = NULL;

    tb_phys_invalidate(tb, -1);

    tb_free(tb);

}