static int decode_subframe(WMAProDecodeCtx *s)

{

    int offset = s->samples_per_frame;

    int subframe_len = s->samples_per_frame;

    int i;

    int total_samples   = s->samples_per_frame * s->num_channels;

    int transmit_coeffs = 0;

    int cur_subwoofer_cutoff;

    s->subframe_offset = get_bits_count(&s->gb);

    /** reset channel context and find the next block offset and size

        == the next block of the channel with the smallest number of

        decoded samples

    */

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

        s->channel[i].grouped = 0;

        if (offset > s->channel[i].decoded_samples) {

            offset = s->channel[i].decoded_samples;

            subframe_len =

                s->channel[i].subframe_len[s->channel[i].cur_subframe];

        }

    }

    av_dlog(s->avctx,

            "processing subframe with offset %i len %i\n", offset, subframe_len);

    /** get a list of all channels that contain the estimated block */

    s->channels_for_cur_subframe = 0;

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

        const int cur_subframe = s->channel[i].cur_subframe;

        /** substract already processed samples */

        total_samples -= s->channel[i].decoded_samples;

        /** and count if there are multiple subframes that match our profile */

        if (offset == s->channel[i].decoded_samples &&

            subframe_len == s->channel[i].subframe_len[cur_subframe]) {

            total_samples -= s->channel[i].subframe_len[cur_subframe];

            s->channel[i].decoded_samples +=

                s->channel[i].subframe_len[cur_subframe];

            s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;

            ++s->channels_for_cur_subframe;

        }

    }

    /** check if the frame will be complete after processing the

        estimated block */

    if (!total_samples)

        s->parsed_all_subframes = 1;

    av_dlog(s->avctx, "subframe is part of %i channels\n",

            s->channels_for_cur_subframe);

    /** calculate number of scale factor bands and their offsets */

    s->table_idx         = av_log2(s->samples_per_frame/subframe_len);

    s->num_bands         = s->num_sfb[s->table_idx];

    s->cur_sfb_offsets   = s->sfb_offsets[s->table_idx];

    cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];

    /** configure the decoder for the current subframe */

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

        int c = s->channel_indexes_for_cur_subframe[i];

        s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)

                                                  + offset];

    }

    s->subframe_len = subframe_len;

    s->esc_len = av_log2(s->subframe_len - 1) + 1;

    /** skip extended header if any */

    if (get_bits1(&s->gb)) {

        int num_fill_bits;

        if (!(num_fill_bits = get_bits(&s->gb, 2))) {

            int len = get_bits(&s->gb, 4);

            num_fill_bits = get_bits(&s->gb, len) + 1;

        }

        if (num_fill_bits >= 0) {

            if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {

                av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");

                return AVERROR_INVALIDDATA;

            }

            skip_bits_long(&s->gb, num_fill_bits);

        }

    }

    /** no idea for what the following bit is used */

    if (get_bits1(&s->gb)) {

        av_log_ask_for_sample(s->avctx, "reserved bit set\n");

        return AVERROR_INVALIDDATA;

    }

    if (decode_channel_transform(s) < 0)

        return AVERROR_INVALIDDATA;

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

        int c = s->channel_indexes_for_cur_subframe[i];

        if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))

            transmit_coeffs = 1;

    }

    if (transmit_coeffs) {

        int step;

        int quant_step = 90 * s->bits_per_sample >> 4;

        /** decode number of vector coded coefficients */

        if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {

            int num_bits = av_log2((s->subframe_len + 3)/4) + 1;

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

                int c = s->channel_indexes_for_cur_subframe[i];

                s->channel[c].num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;

            }

        } else {

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

                int c = s->channel_indexes_for_cur_subframe[i];

                s->channel[c].num_vec_coeffs = s->subframe_len;

            }

        }

        /** decode quantization step */

        step = get_sbits(&s->gb, 6);

        quant_step += step;

        if (step == -32 || step == 31) {

            const int sign = (step == 31) - 1;

            int quant = 0;

            while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&

                   (step = get_bits(&s->gb, 5)) == 31) {

                quant += 31;

            }

            quant_step += ((quant + step) ^ sign) - sign;

        }

        if (quant_step < 0) {

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

        }

        /** decode quantization step modifiers for every channel */

        if (s->channels_for_cur_subframe == 1) {

            s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;

        } else {

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

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

                int c = s->channel_indexes_for_cur_subframe[i];

                s->channel[c].quant_step = quant_step;

                if (get_bits1(&s->gb)) {

                    if (modifier_len) {

                        s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;

                    } else

                        ++s->channel[c].quant_step;

                }

            }

        }

        /** decode scale factors */

        if (decode_scale_factors(s) < 0)

            return AVERROR_INVALIDDATA;

    }

    av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",

            get_bits_count(&s->gb) - s->subframe_offset);

    /** parse coefficients */

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

        int c = s->channel_indexes_for_cur_subframe[i];

        if (s->channel[c].transmit_coefs &&

            get_bits_count(&s->gb) < s->num_saved_bits) {

            decode_coeffs(s, c);

        } else

            memset(s->channel[c].coeffs, 0,

                   sizeof(*s->channel[c].coeffs) * subframe_len);

    }

    av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",

            get_bits_count(&s->gb) - s->subframe_offset);

    if (transmit_coeffs) {

        FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];

        /** reconstruct the per channel data */

        inverse_channel_transform(s);

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

            int c = s->channel_indexes_for_cur_subframe[i];

            const int* sf = s->channel[c].scale_factors;

            int b;

            if (c == s->lfe_channel)

                memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *

                       (subframe_len - cur_subwoofer_cutoff));

            /** inverse quantization and rescaling */

            for (b = 0; b < s->num_bands; b++) {

                const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);

                const int exp = s->channel[c].quant_step -

                            (s->channel[c].max_scale_factor - *sf++) *

                            s->channel[c].scale_factor_step;

                const float quant = pow(10.0, exp / 20.0);

                int start = s->cur_sfb_offsets[b];

                s->dsp.vector_fmul_scalar(s->tmp + start,

                                          s->channel[c].coeffs + start,

                                          quant, end - start);

            }

            /** apply imdct (imdct_half == DCTIV with reverse) */

            mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);

        }

    }

    /** window and overlapp-add */

    wmapro_window(s);

    /** handled one subframe */

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

        int c = s->channel_indexes_for_cur_subframe[i];

        if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {

            av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");

            return AVERROR_INVALIDDATA;

        }

        ++s->channel[c].cur_subframe;

    }

    return 0;

}


int kvm_vm_ioctl(KVMState *s, int type, ...)

{

    int ret;

    void *arg;

    va_list ap;

    va_start(ap, type);

    arg = va_arg(ap, void *);

    va_end(ap);

    ret = ioctl(s->vmfd, type, arg);

    if (ret == -1)

        ret = -errno;

    return ret;

}


void kqemu_cpu_interrupt(CPUState *env)

{

#if defined(_WIN32)

    /* cancelling the I/O request causes KQEMU to finish executing the

       current block and successfully returning. */

    CancelIo(kqemu_fd);

#endif

}


target_ulong helper_cfc1(CPUMIPSState *env, uint32_t reg)

{

    target_ulong arg1;

    switch (reg) {

    case 0:

        arg1 = (int32_t)env->active_fpu.fcr0;

        break;

    case 25:

        arg1 = ((env->active_fpu.fcr31 >> 24) & 0xfe) | ((env->active_fpu.fcr31 >> 23) & 0x1);

        break;

    case 26:

        arg1 = env->active_fpu.fcr31 & 0x0003f07c;

        break;

    case 28:

        arg1 = (env->active_fpu.fcr31 & 0x00000f83) | ((env->active_fpu.fcr31 >> 22) & 0x4);

        break;

    default:

        arg1 = (int32_t)env->active_fpu.fcr31;

        break;

    }

    return arg1;

}


static void setup_frame(int sig, struct target_sigaction *ka,

                        target_sigset_t *set, CPUSH4State *regs)

{

    struct target_sigframe *frame;

    abi_ulong frame_addr;

    int i;

    int err = 0;

    int signal;

    frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));

    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))

	goto give_sigsegv;

    signal = current_exec_domain_sig(sig);

    err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);

    for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {

        __put_user(set->sig[i + 1], &frame->extramask[i]);

    }

    /* Set up to return from userspace.  If provided, use a stub

       already in userspace.  */

    if (ka->sa_flags & TARGET_SA_RESTORER) {

        regs->pr = (unsigned long) ka->sa_restorer;

    } else {

        /* Generate return code (system call to sigreturn) */

        __put_user(MOVW(2), &frame->retcode[0]);

        __put_user(TRAP_NOARG, &frame->retcode[1]);

        __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);

        regs->pr = (unsigned long) frame->retcode;

    }

    if (err)

        goto give_sigsegv;

    /* Set up registers for signal handler */

    regs->gregs[15] = frame_addr;

    regs->gregs[4] = signal; /* Arg for signal handler */

    regs->gregs[5] = 0;

    regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);

    regs->pc = (unsigned long) ka->_sa_handler;

    unlock_user_struct(frame, frame_addr, 1);

    return;

give_sigsegv:

    unlock_user_struct(frame, frame_addr, 1);

    force_sig(TARGET_SIGSEGV);

}


static int qcow_open(BlockDriverState *bs, const char *filename, int flags)

{

    BDRVQcowState *s = bs->opaque;

    int len, i, shift, ret;

    QCowHeader header;

    uint64_t ext_end;

    ret = bdrv_file_open(&s->hd, filename, flags);

    if (ret < 0)

        return ret;

    if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))

        goto fail;

    be32_to_cpus(&header.magic);

    be32_to_cpus(&header.version);

    be64_to_cpus(&header.backing_file_offset);

    be32_to_cpus(&header.backing_file_size);

    be64_to_cpus(&header.size);

    be32_to_cpus(&header.cluster_bits);

    be32_to_cpus(&header.crypt_method);

    be64_to_cpus(&header.l1_table_offset);

    be32_to_cpus(&header.l1_size);

    be64_to_cpus(&header.refcount_table_offset);

    be32_to_cpus(&header.refcount_table_clusters);

    be64_to_cpus(&header.snapshots_offset);

    be32_to_cpus(&header.nb_snapshots);

    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)

        goto fail;

    if (header.size <= 1 ||

        header.cluster_bits < MIN_CLUSTER_BITS ||

        header.cluster_bits > MAX_CLUSTER_BITS)

        goto fail;

    if (header.crypt_method > QCOW_CRYPT_AES)

        goto fail;

    s->crypt_method_header = header.crypt_method;

    if (s->crypt_method_header)

        bs->encrypted = 1;

    s->cluster_bits = header.cluster_bits;

    s->cluster_size = 1 << s->cluster_bits;

    s->cluster_sectors = 1 << (s->cluster_bits - 9);

    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */

    s->l2_size = 1 << s->l2_bits;

    bs->total_sectors = header.size / 512;

    s->csize_shift = (62 - (s->cluster_bits - 8));

    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;

    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;

    s->refcount_table_offset = header.refcount_table_offset;

    s->refcount_table_size =

        header.refcount_table_clusters << (s->cluster_bits - 3);

    s->snapshots_offset = header.snapshots_offset;

    s->nb_snapshots = header.nb_snapshots;

    /* read the level 1 table */

    s->l1_size = header.l1_size;

    shift = s->cluster_bits + s->l2_bits;

    s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;

    /* the L1 table must contain at least enough entries to put

       header.size bytes */

    if (s->l1_size < s->l1_vm_state_index)

        goto fail;

    s->l1_table_offset = header.l1_table_offset;

    s->l1_table = qemu_mallocz(

        align_offset(s->l1_size * sizeof(uint64_t), 512));

    if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=

        s->l1_size * sizeof(uint64_t))

        goto fail;

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

        be64_to_cpus(&s->l1_table[i]);

    }

    /* alloc L2 cache */

    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));

    s->cluster_cache = qemu_malloc(s->cluster_size);

    /* one more sector for decompressed data alignment */

    s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size

                                  + 512);

    s->cluster_cache_offset = -1;

    if (qcow2_refcount_init(bs) < 0)

        goto fail;

    LIST_INIT(&s->cluster_allocs);

    /* read qcow2 extensions */

    if (header.backing_file_offset)

        ext_end = header.backing_file_offset;

    else

        ext_end = s->cluster_size;

    if (qcow_read_extensions(bs, sizeof(header), ext_end))

        goto fail;

    /* read the backing file name */

    if (header.backing_file_offset != 0) {

        len = header.backing_file_size;

        if (len > 1023)

            len = 1023;

        if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)

            goto fail;

        bs->backing_file[len] = '\0';

    }

    if (qcow2_read_snapshots(bs) < 0)

        goto fail;

#ifdef DEBUG_ALLOC

    qcow2_check_refcounts(bs);

#endif

    return 0;

 fail:

    qcow2_free_snapshots(bs);

    qcow2_refcount_close(bs);

    qemu_free(s->l1_table);

    qemu_free(s->l2_cache);

    qemu_free(s->cluster_cache);

    qemu_free(s->cluster_data);

    bdrv_delete(s->hd);

    return -1;

}


static void get_enum(Object *obj, Visitor *v, void *opaque,

                     const char *name, Error **errp)

{

    DeviceState *dev = DEVICE(obj);

    Property *prop = opaque;

    int *ptr = qdev_get_prop_ptr(dev, prop);

    visit_type_enum(v, ptr, prop->info->enum_table,

                    prop->info->name, prop->name, errp);

}


QapiDeallocVisitor *qapi_dealloc_visitor_new(void)

{

    QapiDeallocVisitor *v;

    v = g_malloc0(sizeof(*v));

    v->visitor.type = VISITOR_DEALLOC;

    v->visitor.start_struct = qapi_dealloc_start_struct;

    v->visitor.end_struct = qapi_dealloc_end_struct;

    v->visitor.start_alternate = qapi_dealloc_start_alternate;

    v->visitor.end_alternate = qapi_dealloc_end_alternate;

    v->visitor.start_list = qapi_dealloc_start_list;

    v->visitor.next_list = qapi_dealloc_next_list;

    v->visitor.end_list = qapi_dealloc_end_list;

    v->visitor.type_int64 = qapi_dealloc_type_int64;

    v->visitor.type_uint64 = qapi_dealloc_type_uint64;

    v->visitor.type_bool = qapi_dealloc_type_bool;

    v->visitor.type_str = qapi_dealloc_type_str;

    v->visitor.type_number = qapi_dealloc_type_number;

    v->visitor.type_any = qapi_dealloc_type_anything;

    v->visitor.type_null = qapi_dealloc_type_null;

    return v;

}


static uint32_t dp8393x_readb(void *opaque, target_phys_addr_t addr)

{

    uint16_t v = dp8393x_readw(opaque, addr & ~0x1);

    return (v >> (8 * (addr & 0x1))) & 0xff;

}


int tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,

                                intptr_t offset, const char *name)

{

    TCGContext *s = &tcg_ctx;

    TCGTemp *ts, *base_ts = &s->temps[GET_TCGV_PTR(base)];

    int idx, reg = base_ts->reg;

    idx = s->nb_globals;

#if TCG_TARGET_REG_BITS == 32

    if (type == TCG_TYPE_I64) {

        char buf[64];

        tcg_temp_alloc(s, s->nb_globals + 2);

        ts = &s->temps[s->nb_globals];

        ts->base_type = type;

        ts->type = TCG_TYPE_I32;

        ts->fixed_reg = 0;

        ts->mem_allocated = 1;

        ts->mem_reg = reg;

#ifdef HOST_WORDS_BIGENDIAN

        ts->mem_offset = offset + 4;

#else

        ts->mem_offset = offset;

#endif

        pstrcpy(buf, sizeof(buf), name);

        pstrcat(buf, sizeof(buf), "_0");

        ts->name = strdup(buf);

        ts++;

        ts->base_type = type;

        ts->type = TCG_TYPE_I32;

        ts->fixed_reg = 0;

        ts->mem_allocated = 1;

        ts->mem_reg = reg;

#ifdef HOST_WORDS_BIGENDIAN

        ts->mem_offset = offset;

#else

        ts->mem_offset = offset + 4;

#endif

        pstrcpy(buf, sizeof(buf), name);

        pstrcat(buf, sizeof(buf), "_1");

        ts->name = strdup(buf);

        s->nb_globals += 2;

    } else

#endif

    {

        tcg_temp_alloc(s, s->nb_globals + 1);

        ts = &s->temps[s->nb_globals];

        ts->base_type = type;

        ts->type = type;

        ts->fixed_reg = 0;

        ts->mem_allocated = 1;

        ts->mem_reg = reg;

        ts->mem_offset = offset;

        ts->name = name;

        s->nb_globals++;

    }

    return idx;

}


hwaddr sparc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)

{

    SPARCCPU *cpu = SPARC_CPU(cs);

    CPUSPARCState *env = &cpu->env;

    hwaddr phys_addr;

    int mmu_idx = cpu_mmu_index(env, false);

    MemoryRegionSection section;

    if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 2, mmu_idx) != 0) {

        if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 0, mmu_idx) != 0) {

            return -1;

        }

    }

    section = memory_region_find(get_system_memory(), phys_addr, 1);

    memory_region_unref(section.mr);

    if (!int128_nz(section.size)) {

        return -1;

    }

    return phys_addr;

}


int qed_read_l2_table(BDRVQEDState *s, QEDRequest *request, uint64_t offset)

{

    int ret;

    qed_unref_l2_cache_entry(request->l2_table);

    /* Check for cached L2 entry */

    request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);

    if (request->l2_table) {

        return 0;

    }

    request->l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);

    request->l2_table->table = qed_alloc_table(s);

    BLKDBG_EVENT(s->bs->file, BLKDBG_L2_LOAD);

    ret = qed_read_table(s, offset, request->l2_table->table);

    qed_acquire(s);

    if (ret) {

        /* can't trust loaded L2 table anymore */

        qed_unref_l2_cache_entry(request->l2_table);

        request->l2_table = NULL;

    } else {

        request->l2_table->offset = offset;

        qed_commit_l2_cache_entry(&s->l2_cache, request->l2_table);

        /* This is guaranteed to succeed because we just committed the entry

         * to the cache.

         */

        request->l2_table = qed_find_l2_cache_entry(&s->l2_cache, offset);

        assert(request->l2_table != NULL);

    }

    qed_release(s);

    return ret;

}


static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)

{

    int bits = 8, sign = 0;

    switch (as->fmt) {

    case AUD_FMT_S8:

        sign = 1;

    case AUD_FMT_U8:

        break;

    case AUD_FMT_S16:

        sign = 1;

    case AUD_FMT_U16:

        bits = 16;

        break;

    case AUD_FMT_S32:

        sign = 1;

    case AUD_FMT_U32:

        bits = 32;

        break;

    }

    return info->freq == as->freq

        && info->nchannels == as->nchannels

        && info->sign == sign

        && info->bits == bits

        && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);

}


static int parse_adaptation_sets(AVFormatContext *s)

{

    WebMDashMuxContext *w = s->priv_data;

    char *p = w->adaptation_sets;

    char *q;

    enum { new_set, parsed_id, parsing_streams } state;

    // syntax id=0,streams=0,1,2 id=1,streams=3,4 and so on

    state = new_set;

    while (p < w->adaptation_sets + strlen(w->adaptation_sets)) {

        if (*p == ' ')

            continue;

        else if (state == new_set && !strncmp(p, "id=", 3)) {

            w->as = av_realloc(w->as, sizeof(*w->as) * ++w->nb_as);

            if (w->as == NULL) return -1;

            w->as[w->nb_as - 1].nb_streams = 0;

            w->as[w->nb_as - 1].streams = NULL;

            p += 3; // consume "id="

            q = w->as[w->nb_as - 1].id;

            while (*p != ',') *q++ = *p++;

            *q = 0;

            p++;

            state = parsed_id;

        } else if (state == parsed_id && !strncmp(p, "streams=", 8)) {

            p += 8; // consume "streams="

            state = parsing_streams;

        } else if (state == parsing_streams) {

            struct AdaptationSet *as = &w->as[w->nb_as - 1];

            q = p;

            while (*q != '\0' && *q != ',' && *q != ' ') q++;

            as->streams = av_realloc(as->streams, sizeof(*as->streams) * ++as->nb_streams);

            if (as->streams == NULL) return -1;

            as->streams[as->nb_streams - 1] = to_integer(p, q - p);

            if (as->streams[as->nb_streams - 1] < 0) return -1;

            if (*q == '\0') break;

            if (*q == ' ') state = new_set;

            p = ++q;

        } else {

            return -1;

        }

    }

    return 0;

}


static int filter_frame(AVFilterLink *link, AVFrame *picref)

{

    AVFilterContext *ctx = link->dst;

    IDETContext *idet = ctx->priv;

    if (idet->prev)

        av_frame_free(&idet->prev);

    idet->prev = idet->cur;

    idet->cur  = idet->next;

    idet->next = picref;

    if (!idet->cur)

        return 0;

    if (!idet->prev)

        idet->prev = av_frame_clone(idet->cur);

    if (!idet->csp)

        idet->csp = av_pix_fmt_desc_get(link->format);

    if (idet->csp->comp[0].depth_minus1 / 8 == 1){

        idet->filter_line = (ff_idet_filter_func)ff_idet_filter_line_c_16bit;

        if (ARCH_X86)

            ff_idet_init_x86(idet, 1);

    }

    filter(ctx);

    return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));

}


void ff_dsputil_init_arm(DSPContext* c, AVCodecContext *avctx)

{

    const int high_bit_depth = avctx->bits_per_raw_sample > 8;

    int cpu_flags = av_get_cpu_flags();

    ff_put_pixels_clamped = c->put_pixels_clamped;

    ff_add_pixels_clamped = c->add_pixels_clamped;

    if (avctx->bits_per_raw_sample <= 8) {

        if(avctx->idct_algo == FF_IDCT_AUTO ||

           avctx->idct_algo == FF_IDCT_ARM){

            c->idct_put              = j_rev_dct_arm_put;

            c->idct_add              = j_rev_dct_arm_add;

            c->idct                  = ff_j_rev_dct_arm;

            c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;

        } else if (avctx->idct_algo == FF_IDCT_SIMPLEARM){

            c->idct_put              = simple_idct_arm_put;

            c->idct_add              = simple_idct_arm_add;

            c->idct                  = ff_simple_idct_arm;

            c->idct_permutation_type = FF_NO_IDCT_PERM;

        }

    }

    c->add_pixels_clamped = ff_add_pixels_clamped_arm;

    if (!high_bit_depth) {

    c->put_pixels_tab[0][0] = ff_put_pixels16_arm;

    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_arm;

    c->put_pixels_tab[0][2] = ff_put_pixels16_y2_arm;

    c->put_pixels_tab[0][3] = ff_put_pixels16_xy2_arm;

    c->put_pixels_tab[1][0] = ff_put_pixels8_arm;

    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_arm;

    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_arm;

    c->put_pixels_tab[1][3] = ff_put_pixels8_xy2_arm;

    c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_arm;

    c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_arm;

    c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_arm;

    c->put_no_rnd_pixels_tab[0][3] = ff_put_no_rnd_pixels16_xy2_arm;

    c->put_no_rnd_pixels_tab[1][0] = ff_put_pixels8_arm;

    c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_arm;

    c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_arm;

    c->put_no_rnd_pixels_tab[1][3] = ff_put_no_rnd_pixels8_xy2_arm;

    }

    if (have_armv5te(cpu_flags)) ff_dsputil_init_armv5te(c, avctx);

    if (have_armv6(cpu_flags))   ff_dsputil_init_armv6(c, avctx);

    if (have_vfp(cpu_flags))     ff_dsputil_init_vfp(c, avctx);

    if (have_neon(cpu_flags))    ff_dsputil_init_neon(c, avctx);

}


static void fft(AC3MDCTContext *mdct, IComplex *z, int ln)

{

    int j, l, np, np2;

    int nblocks, nloops;

    register IComplex *p,*q;

    int tmp_re, tmp_im;

    np = 1 << ln;

    /* reverse */

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

        int k = av_reverse[j] >> (8 - ln);

        if (k < j)

            FFSWAP(IComplex, z[k], z[j]);

    }

    /* pass 0 */

    p = &z[0];

    j = np >> 1;

    do {

        BF(p[0].re, p[0].im, p[1].re, p[1].im,

           p[0].re, p[0].im, p[1].re, p[1].im);

        p += 2;

    } while (--j);

    /* pass 1 */

    p = &z[0];

    j = np >> 2;

    do {

        BF(p[0].re, p[0].im, p[2].re,  p[2].im,

           p[0].re, p[0].im, p[2].re,  p[2].im);

        BF(p[1].re, p[1].im, p[3].re,  p[3].im,

           p[1].re, p[1].im, p[3].im, -p[3].re);

        p+=4;

    } while (--j);

    /* pass 2 .. ln-1 */

    nblocks = np >> 3;

    nloops  =  1 << 2;

    np2     = np >> 1;

    do {

        p = z;

        q = z + nloops;

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

            BF(p->re, p->im, q->re, q->im,

               p->re, p->im, q->re, q->im);

            p++;

            q++;

            for(l = nblocks; l < np2; l += nblocks) {

                CMUL(tmp_re, tmp_im, mdct->costab[l], -mdct->sintab[l], q->re, q->im, 15);

                BF(p->re, p->im, q->re,  q->im,

                   p->re, p->im, tmp_re, tmp_im);

                p++;

                q++;

            }

            p += nloops;

            q += nloops;

        }

        nblocks = nblocks >> 1;

        nloops  = nloops  << 1;

    } while (nblocks);

}


static inline void gen_lookup_tb(DisasContext *s)

{

    tcg_gen_movi_i32(cpu_R[15], s->pc & ~1);

    s->is_jmp = DISAS_UPDATE;

}


static int parse_interval(Interval *interval, int interval_count,

                          const char **buf, void *log_ctx)

{

    char *intervalstr;

    int ret;

    *buf += strspn(*buf, SPACES);

    if (!**buf)

        return 0;

    /* reset data */

    memset(interval, 0, sizeof(Interval));

    interval->index = interval_count;

    /* format: INTERVAL COMMANDS */

    /* parse interval */

    intervalstr = av_get_token(buf, DELIMS);

    if (intervalstr && intervalstr[0]) {

        char *start, *end;

        start = av_strtok(intervalstr, "-", &end);

        if ((ret = av_parse_time(&interval->start_ts, start, 1)) < 0) {

                   "Invalid start time specification '%s' in interval #%d\n",

                   start, interval_count);

        if (end) {

            if ((ret = av_parse_time(&interval->end_ts, end, 1)) < 0) {

                       "Invalid end time specification '%s' in interval #%d\n",

                       end, interval_count);

        } else {

            interval->end_ts = INT64_MAX;

        if (interval->end_ts < interval->start_ts) {

                   "Invalid end time '%s' in interval #%d: "

                   "cannot be lesser than start time '%s'\n",

                   end, interval_count, start);

    } else {

               "No interval specified for interval #%d\n", interval_count);

    /* parse commands */

    ret = parse_commands(&interval->commands, &interval->nb_commands,

                         interval_count, buf, log_ctx);

end:

    av_free(intervalstr);

    return ret;


static void gen_load_fp(DisasContext *s, int opsize, TCGv addr, TCGv_ptr fp)

{

    TCGv tmp;

    TCGv_i64 t64;

    int index = IS_USER(s);

    t64 = tcg_temp_new_i64();

    tmp = tcg_temp_new();

    switch (opsize) {

    case OS_BYTE:

        tcg_gen_qemu_ld8s(tmp, addr, index);

        gen_helper_exts32(cpu_env, fp, tmp);

        break;

    case OS_WORD:

        tcg_gen_qemu_ld16s(tmp, addr, index);

        gen_helper_exts32(cpu_env, fp, tmp);

        break;

    case OS_LONG:

        tcg_gen_qemu_ld32u(tmp, addr, index);

        gen_helper_exts32(cpu_env, fp, tmp);

        break;

    case OS_SINGLE:

        tcg_gen_qemu_ld32u(tmp, addr, index);

        gen_helper_extf32(cpu_env, fp, tmp);

        break;

    case OS_DOUBLE:

        tcg_gen_qemu_ld64(t64, addr, index);

        gen_helper_extf64(cpu_env, fp, t64);

        tcg_temp_free_i64(t64);

        break;

    case OS_EXTENDED:

        if (m68k_feature(s->env, M68K_FEATURE_CF_FPU)) {

            gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);

            break;

        }

        tcg_gen_qemu_ld32u(tmp, addr, index);

        tcg_gen_shri_i32(tmp, tmp, 16);

        tcg_gen_st16_i32(tmp, fp, offsetof(FPReg, l.upper));

        tcg_gen_addi_i32(tmp, addr, 4);

        tcg_gen_qemu_ld64(t64, tmp, index);

        tcg_gen_st_i64(t64, fp, offsetof(FPReg, l.lower));

        break;

    case OS_PACKED:

        /* unimplemented data type on 68040/ColdFire

         * FIXME if needed for another FPU

         */

        gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);

        break;

    default:

        g_assert_not_reached();

    }

    tcg_temp_free(tmp);

    tcg_temp_free_i64(t64);

}


static av_cold int j2kenc_init(AVCodecContext *avctx)

{

    int i, ret;

    Jpeg2000EncoderContext *s = avctx->priv_data;

    Jpeg2000CodingStyle *codsty = &s->codsty;

    Jpeg2000QuantStyle  *qntsty = &s->qntsty;

    s->avctx = avctx;

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

    // defaults:

    // TODO: implement setting non-standard precinct size

    memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));

    memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));

    codsty->nreslevels2decode=

    codsty->nreslevels       = 7;

    codsty->log2_cblk_width  = 4;

    codsty->log2_cblk_height = 4;

    codsty->transform        = avctx->prediction_method ? FF_DWT53 : FF_DWT97_INT;

    qntsty->nguardbits       = 1;

    s->tile_width            = 256;

    s->tile_height           = 256;

    if (codsty->transform == FF_DWT53)

        qntsty->quantsty = JPEG2000_QSTY_NONE;

    else

        qntsty->quantsty = JPEG2000_QSTY_SE;

    s->width = avctx->width;

    s->height = avctx->height;

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

        s->cbps[i] = 8;

    if (avctx->pix_fmt == AV_PIX_FMT_RGB24){

        s->ncomponents = 3;

    } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){

        s->ncomponents = 1;

    } else{ // planar YUV

        s->planar = 1;

        s->ncomponents = 3;

        avcodec_get_chroma_sub_sample(avctx->pix_fmt,

                s->chroma_shift, s->chroma_shift + 1);

    }

    ff_jpeg2000_init_tier1_luts();

    init_luts();

    init_quantization(s);

    if (ret=init_tiles(s))

        return ret;

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

    return 0;

}


static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,

                             AVStream *st, AVPacket *pkt, uint32_t *timestamp,

                             const uint8_t *buf, int len, uint16_t seq,

                             int flags)

{

    int has_pic_id, has_layer_idc, has_ref_idc, has_ss_data, has_su_data;

    av_unused int pic_id = 0, non_key_frame = 0;

    av_unused int layer_temporal = -1, layer_spatial = -1, layer_quality = -1;

    int ref_fields = 0, has_ref_field_ext_pic_id = 0;

    int first_fragment, last_fragment;

    int rtp_m;

    int res = 0;

    /* drop data of previous packets in case of non-continuous (lossy) packet stream */

    if (rtp_vp9_ctx->buf && rtp_vp9_ctx->timestamp != *timestamp)

        ffio_free_dyn_buf(&rtp_vp9_ctx->buf);

    /* sanity check for size of input packet: 1 byte payload at least */

    if (len < RTP_VP9_DESC_REQUIRED_SIZE + 1) {

        av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet, got %d bytes\n", len);

        return AVERROR_INVALIDDATA;

    }

    /*

     *     decode the required VP9 payload descriptor according to section 4.2 of the spec.:

     *

     *      0 1 2 3 4 5 6 7

     *     +-+-+-+-+-+-+-+-+

     *     |I|L|F|B|E|V|U|-| (REQUIRED)

     *     +-+-+-+-+-+-+-+-+

     *

     *     I: PictureID present

     *     L: Layer indices present

     *     F: Reference indices present

     *     B: Start of VP9 frame

     *     E: End of picture

     *     V: Scalability Structure (SS) present

     *     U: Scalability Structure Update (SU) present

     */

    has_pic_id     = !!(buf[0] & 0x80);

    has_layer_idc  = !!(buf[0] & 0x40);

    has_ref_idc    = !!(buf[0] & 0x20);

    first_fragment = !!(buf[0] & 0x10);

    last_fragment  = !!(buf[0] & 0x08);

    has_ss_data    = !!(buf[0] & 0x04);

    has_su_data    = !!(buf[0] & 0x02);

    rtp_m = !!(flags & RTP_FLAG_MARKER);

    /* sanity check for markers: B should always be equal to the RTP M marker */

    if (last_fragment != rtp_m) {

        av_log(ctx, AV_LOG_ERROR, "Invalid combination of B and M marker (%d != %d)\n", last_fragment, rtp_m);

        return AVERROR_INVALIDDATA;

    }

    /* pass the extensions field */

    buf += RTP_VP9_DESC_REQUIRED_SIZE;

    len -= RTP_VP9_DESC_REQUIRED_SIZE;

    /*

     *         decode the 1-byte/2-byte picture ID:

     *

     *          0 1 2 3 4 5 6 7

     *         +-+-+-+-+-+-+-+-+

     *   I:    |M|PICTURE ID   | (RECOMMENDED)

     *         +-+-+-+-+-+-+-+-+

     *   M:    | EXTENDED PID  | (RECOMMENDED)

     *         +-+-+-+-+-+-+-+-+

     *

     *   M: The most significant bit of the first octet is an extension flag.

     *   PictureID:  8 or 16 bits including the M bit.

     */

    if (has_pic_id) {

        if (len < 1) {

            av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");

            return AVERROR_INVALIDDATA;

        }

        /* check for 1-byte or 2-byte picture index */

        if (buf[0] & 0x80) {

            if (len < 2) {

                av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");

                return AVERROR_INVALIDDATA;

            }

            pic_id = AV_RB16(buf) & 0x7fff;

            buf += 2;

            len -= 2;

        } else {

            pic_id = buf[0] & 0x7f;

            buf++;

            len--;

        }

    }

    /*

     *         decode layer indices

     *

     *          0 1 2 3 4 5 6 7

     *         +-+-+-+-+-+-+-+-+

     *   L:    | T | S | Q | R | (CONDITIONALLY RECOMMENDED)

     *         +-+-+-+-+-+-+-+-+

     *

     *   T, S and Q are 2-bit indices for temporal, spatial, and quality layers.

     *   If "F" is set in the initial octet, R is 2 bits representing the number

     *   of reference fields this frame refers to.

     */

    if (has_layer_idc) {

        if (len < 1) {

            av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");

            return AVERROR_INVALIDDATA;

        }

        layer_temporal = buf[0] & 0xC0;

        layer_spatial  = buf[0] & 0x30;

        layer_quality  = buf[0] & 0x0C;

        if (has_ref_idc) {

            ref_fields = buf[0] & 0x03;

            if (ref_fields)

                non_key_frame = 1;

        }

        buf++;

        len--;

    }

    /*

     *         decode the reference fields

     *

     *          0 1 2 3 4 5 6 7

     *         +-+-+-+-+-+-+-+-+              -\

     *   F:    | PID |X| RS| RQ| (OPTIONAL)    .

     *         +-+-+-+-+-+-+-+-+               . - R times

     *   X:    | EXTENDED PID  | (OPTIONAL)    .

     *         +-+-+-+-+-+-+-+-+              -/

     *

     *   PID:  The relative Picture ID referred to by this frame.

     *   RS and RQ:  The spatial and quality layer IDs.

     *   X: 1 if this layer index has an extended relative Picture ID.

     */

    if (has_ref_idc) {

        while (ref_fields) {

            if (len < 1) {

                av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");

                return AVERROR_INVALIDDATA;

            }

            has_ref_field_ext_pic_id = buf[0] & 0x10;

            /* pass ref. field */

            if (has_ref_field_ext_pic_id) {

                if (len < 2) {

                    av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");

                    return AVERROR_INVALIDDATA;

                }

                /* ignore ref. data */

                buf += 2;

                len -= 2;

            } else {

                /* ignore ref. data */

                buf++;

                len--;

            }

            ref_fields--;

        }

    }

    /*

     *         decode the scalability structure (SS)

     *

     *          0 1 2 3 4 5 6 7

     *         +-+-+-+-+-+-+-+-+

     *   V:    | PATTERN LENGTH|

     *         +-+-+-+-+-+-+-+-+                           -\

     *         | T | S | Q | R | (OPTIONAL)                 .

     *         +-+-+-+-+-+-+-+-+              -\            .

     *         | PID |X| RS| RQ| (OPTIONAL)    .            . - PAT. LEN. times

     *         +-+-+-+-+-+-+-+-+               . - R times  .

     *   X:    | EXTENDED PID  | (OPTIONAL)    .            .

     *         +-+-+-+-+-+-+-+-+              -/           -/

     *

     *   PID:  The relative Picture ID referred to by this frame.

     *   RS and RQ:  The spatial and quality layer IDs.

     *   X: 1 if this layer index has an extended relative Picture ID.

     */

    if (has_ss_data) {

        avpriv_report_missing_feature(ctx, "VP9 scalability structure data");

        return AVERROR(ENOSYS);

    }

    /*

     * decode the scalability update structure (SU)

     *

     *  spec. is tbd

     */

    if (has_su_data) {

        avpriv_report_missing_feature(ctx, "VP9 scalability update structure data");

        return AVERROR(ENOSYS);

    }

    /*

     * decode the VP9 payload header

     *

     *  spec. is tbd

     */

    //XXX: implement when specified

    /* sanity check: 1 byte payload as minimum */

    if (len < 1) {

        av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");

        return AVERROR_INVALIDDATA;

    }

    /* start frame buffering with new dynamic buffer */

    if (!rtp_vp9_ctx->buf) {

        /* sanity check: a new frame should have started */

        if (first_fragment) {

            res = avio_open_dyn_buf(&rtp_vp9_ctx->buf);

            if (res < 0)

                return res;

            /* update the timestamp in the frame packet with the one from the RTP packet */

            rtp_vp9_ctx->timestamp = *timestamp;

        } else {

            /* frame not started yet, need more packets */

            return AVERROR(EAGAIN);

        }

    }

    /* write the fragment to the dyn. buffer */

    avio_write(rtp_vp9_ctx->buf, buf, len);

    /* do we need more fragments? */

    if (!last_fragment)

        return AVERROR(EAGAIN);

    /* close frame buffering and create resulting A/V packet */

    res = ff_rtp_finalize_packet(pkt, &rtp_vp9_ctx->buf, st->index);

    if (res < 0)

        return res;

    return 0;

}


int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,

                         uint64_t bytes, QEMUIOVector *qiov, int flags)

{

    NBDRequest request = {

        .type = NBD_CMD_READ,

        .from = offset,

        .len = bytes,

    };

    assert(bytes <= NBD_MAX_BUFFER_SIZE);

    assert(!flags);

    return nbd_co_request(bs, &request, qiov);

}


writev_f(int argc, char **argv)

{

	struct timeval t1, t2;

	int Cflag = 0, qflag = 0;

	int c, cnt;

	char *buf;

	int64_t offset;

	int total;

	int nr_iov;

	int pattern = 0xcd;

	QEMUIOVector qiov;

	while ((c = getopt(argc, argv, "CqP:")) != EOF) {

		switch (c) {

		case 'C':

			Cflag = 1;

			break;

		case 'q':

			qflag = 1;

			break;

		case 'P':

			pattern = atoi(optarg);

			break;

		default:

			return command_usage(&writev_cmd);

		}

	}

	if (optind > argc - 2)

		return command_usage(&writev_cmd);

	offset = cvtnum(argv[optind]);

	if (offset < 0) {

		printf("non-numeric length argument -- %s\n", argv[optind]);

		return 0;

	}

	optind++;

	if (offset & 0x1ff) {

		printf("offset %lld is not sector aligned\n",

			(long long)offset);

		return 0;

	}

	nr_iov = argc - optind;

	buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);

	gettimeofday(&t1, NULL);

	cnt = do_aio_writev(&qiov, offset, &total);

	gettimeofday(&t2, NULL);

	if (cnt < 0) {

		printf("writev failed: %s\n", strerror(-cnt));

		return 0;

	}

	if (qflag)

		return 0;

	/* Finally, report back -- -C gives a parsable format */

	t2 = tsub(t2, t1);

	print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);

	qemu_io_free(buf);

	return 0;

}


static off_t read_off(BlockDriverState *bs, int64_t offset)

{

	uint64_t buffer;

	if (bdrv_pread(bs->file, offset, &buffer, 8) < 8)

		return 0;

	return be64_to_cpu(buffer);

}


static void qmp_input_type_int(Visitor *v, int64_t *obj, const char *name,

                               Error **errp)

{

    QmpInputVisitor *qiv = to_qiv(v);

    QObject *qobj = qmp_input_get_object(qiv, name, true);

    if (!qobj || qobject_type(qobj) != QTYPE_QINT) {

        error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",

                   "integer");

        return;

    }

    *obj = qint_get_int(qobject_to_qint(qobj));

}


static void emulated_push_type(EmulatedState *card, uint32_t type)

{

    EmulEvent *event = (EmulEvent *)g_malloc(sizeof(EmulEvent));

    assert(event);

    event->p.gen.type = type;

    emulated_push_event(card, event);

}


static target_ulong h_protect(CPUPPCState *env, sPAPREnvironment *spapr,

                              target_ulong opcode, target_ulong *args)

{

    target_ulong flags = args[0];

    target_ulong pte_index = args[1];

    target_ulong avpn = args[2];

    uint8_t *hpte;

    target_ulong v, r, rb;

    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {

        return H_PARAMETER;

    }

    hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);

    while (!lock_hpte(hpte, HPTE_V_HVLOCK)) {

        /* We have no real concurrency in qemu soft-emulation, so we

         * will never actually have a contested lock */

        assert(0);

    }

    v = ldq_p(hpte);

    r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));

    if ((v & HPTE_V_VALID) == 0 ||

        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {

        stq_p(hpte, v & ~HPTE_V_HVLOCK);

        assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));

        return H_NOT_FOUND;

    }

    r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |

           HPTE_R_KEY_HI | HPTE_R_KEY_LO);

    r |= (flags << 55) & HPTE_R_PP0;

    r |= (flags << 48) & HPTE_R_KEY_HI;

    r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);

    rb = compute_tlbie_rb(v, r, pte_index);

    stq_p(hpte, v & ~HPTE_V_VALID);

    ppc_tlb_invalidate_one(env, rb);

    stq_p(hpte + (HASH_PTE_SIZE_64/2), r);

    /* Don't need a memory barrier, due to qemu's global lock */

    stq_p(hpte, v & ~HPTE_V_HVLOCK);

    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));

    return H_SUCCESS;

}


static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,

                                  int pitch, const uint8_t *table)

{

    int j;

    int out = 0;

    int c;

    int t;

    if (width & 1)

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

        out = 0;

        while (out < width) {

            c = ir2_get_code(&ctx->gb);

            if (c >= 0x80) { /* we have a skip */

                c   -= 0x7F;

                out += c * 2;

            } else { /* add two deltas from table */

                t        = dst[out] + (((table[c * 2] - 128)*3) >> 2);

                t        = av_clip_uint8(t);

                dst[out] = t;

                out++;

                t        = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);

                t        = av_clip_uint8(t);

                dst[out] = t;

                out++;

            }

        }

        dst += pitch;

    }

    return 0;

}


static ExitStatus op_ex(DisasContext *s, DisasOps *o)

{

    /* ??? Perhaps a better way to implement EXECUTE is to set a bit in

       tb->flags, (ab)use the tb->cs_base field as the address of

       the template in memory, and grab 8 bits of tb->flags/cflags for

       the contents of the register.  We would then recognize all this

       in gen_intermediate_code_internal, generating code for exactly

       one instruction.  This new TB then gets executed normally.

       On the other hand, this seems to be mostly used for modifying

       MVC inside of memcpy, which needs a helper call anyway.  So

       perhaps this doesn't bear thinking about any further.  */

    TCGv_i64 tmp;

    update_psw_addr(s);

    update_cc_op(s);

    tmp = tcg_const_i64(s->next_pc);

    gen_helper_ex(cc_op, cpu_env, cc_op, o->in1, o->in2, tmp);

    tcg_temp_free_i64(tmp);

    set_cc_static(s);

    return NO_EXIT;

}


static int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,

                      int srcW, int dstW, int filterAlign, int one, int flags, int cpu_flags,

                      SwsVector *srcFilter, SwsVector *dstFilter, double param[2])

{

    int i;

    int filterSize;

    int filter2Size;

    int minFilterSize;

    int64_t *filter=NULL;

    int64_t *filter2=NULL;

    const int64_t fone= 1LL<<54;

    int ret= -1;

    emms_c(); //FIXME this should not be required but it IS (even for non-MMX versions)

    // NOTE: the +3 is for the MMX(+1)/SSE(+3) scaler which reads over the end

    FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+3)*sizeof(int16_t), fail);

    if (FFABS(xInc - 0x10000) <10) { // unscaled

        int i;

        filterSize= 1;

        FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);

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

            filter[i*filterSize]= fone;

            (*filterPos)[i]=i;

        }

    } else if (flags&SWS_POINT) { // lame looking point sampling mode

        int i;

        int xDstInSrc;

        filterSize= 1;

        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);

        xDstInSrc= xInc/2 - 0x8000;

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

            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;

            (*filterPos)[i]= xx;

            filter[i]= fone;

            xDstInSrc+= xInc;

        }

    } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale

        int i;

        int xDstInSrc;

        filterSize= 2;

        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);

        xDstInSrc= xInc/2 - 0x8000;

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

            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;

            int j;

            (*filterPos)[i]= xx;

            //bilinear upscale / linear interpolate / area averaging

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

                int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);

                if (coeff<0) coeff=0;

                filter[i*filterSize + j]= coeff;

                xx++;

            }

            xDstInSrc+= xInc;

        }

    } else {

        int xDstInSrc;

        int sizeFactor;

        if      (flags&SWS_BICUBIC)      sizeFactor=  4;

        else if (flags&SWS_X)            sizeFactor=  8;

        else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear

        else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)

        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;

        else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)

        else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)

        else if (flags&SWS_BILINEAR)     sizeFactor=  2;

        else {

            sizeFactor= 0; //GCC warning killer

            assert(0);

        }

        if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale

        else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;

        if (filterSize > srcW-2) filterSize=srcW-2;

        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);

        xDstInSrc= xInc - 0x10000;

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

            int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);

            int j;

            (*filterPos)[i]= xx;

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

                int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;

                double floatd;

                int64_t coeff;

                if (xInc > 1<<16)

                    d= d*dstW/srcW;

                floatd= d * (1.0/(1<<30));

                if (flags & SWS_BICUBIC) {

                    int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);

                    int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);

                    if (d >= 1LL<<31) {

                        coeff = 0.0;

                    } else {

                        int64_t dd  = (d  * d) >> 30;

                        int64_t ddd = (dd * d) >> 30;

                        if (d < 1LL<<30)

                            coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);

                        else

                            coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);

                    }

                    coeff *= fone>>(30+24);

                }

/*                else if (flags & SWS_X) {

                    double p= param ? param*0.01 : 0.3;

                    coeff = d ? sin(d*M_PI)/(d*M_PI) : 1.0;

                    coeff*= pow(2.0, - p*d*d);

                }*/

                else if (flags & SWS_X) {

                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;

                    double c;

                    if (floatd<1.0)

                        c = cos(floatd*M_PI);

                    else

                        c=-1.0;

                    if (c<0.0)      c= -pow(-c, A);

                    else            c=  pow( c, A);

                    coeff= (c*0.5 + 0.5)*fone;

                } else if (flags & SWS_AREA) {

                    int64_t d2= d - (1<<29);

                    if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));

                    else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));

                    else coeff=0.0;

                    coeff *= fone>>(30+16);

                } else if (flags & SWS_GAUSS) {

                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;

                    coeff = (pow(2.0, - p*floatd*floatd))*fone;

                } else if (flags & SWS_SINC) {

                    coeff = (d ? sin(floatd*M_PI)/(floatd*M_PI) : 1.0)*fone;

                } else if (flags & SWS_LANCZOS) {

                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;

                    coeff = (d ? sin(floatd*M_PI)*sin(floatd*M_PI/p)/(floatd*floatd*M_PI*M_PI/p) : 1.0)*fone;

                    if (floatd>p) coeff=0;

                } else if (flags & SWS_BILINEAR) {

                    coeff= (1<<30) - d;

                    if (coeff<0) coeff=0;

                    coeff *= fone >> 30;

                } else if (flags & SWS_SPLINE) {

                    double p=-2.196152422706632;

                    coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;

                } else {

                    coeff= 0.0; //GCC warning killer

                    assert(0);

                }

                filter[i*filterSize + j]= coeff;

                xx++;

            }

            xDstInSrc+= 2*xInc;

        }

    }

    /* apply src & dst Filter to filter -> filter2

       av_free(filter);

    */

    assert(filterSize>0);

    filter2Size= filterSize;

    if (srcFilter) filter2Size+= srcFilter->length - 1;

    if (dstFilter) filter2Size+= dstFilter->length - 1;

    assert(filter2Size>0);

    FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);

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

        int j, k;

        if(srcFilter) {

            for (k=0; k<srcFilter->length; k++) {

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

                    filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];

            }

        } else {

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

                filter2[i*filter2Size + j]= filter[i*filterSize + j];

        }

        //FIXME dstFilter

        (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;

    }

    av_freep(&filter);

    /* try to reduce the filter-size (step1 find size and shift left) */

    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).

    minFilterSize= 0;

    for (i=dstW-1; i>=0; i--) {

        int min= filter2Size;

        int j;

        int64_t cutOff=0.0;

        /* get rid of near zero elements on the left by shifting left */

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

            int k;

            cutOff += FFABS(filter2[i*filter2Size]);

            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;

            /* preserve monotonicity because the core can't handle the filter otherwise */

            if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;

            // move filter coefficients left

            for (k=1; k<filter2Size; k++)

                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];

            filter2[i*filter2Size + k - 1]= 0;

            (*filterPos)[i]++;

        }

        cutOff=0;

        /* count near zeros on the right */

        for (j=filter2Size-1; j>0; j--) {

            cutOff += FFABS(filter2[i*filter2Size + j]);

            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;

            min--;

        }

        if (min>minFilterSize) minFilterSize= min;

    }

    if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) {

        // we can handle the special case 4,

        // so we don't want to go to the full 8

        if (minFilterSize < 5)

            filterAlign = 4;

        // We really don't want to waste our time

        // doing useless computation, so fall back on

        // the scalar C code for very small filters.

        // Vectorizing is worth it only if you have a

        // decent-sized vector.

        if (minFilterSize < 3)

            filterAlign = 1;

    }

    if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {

        // special case for unscaled vertical filtering

        if (minFilterSize == 1 && filterAlign == 2)

            filterAlign= 1;

    }

    assert(minFilterSize > 0);

    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));

    assert(filterSize > 0);

    filter= av_malloc(filterSize*dstW*sizeof(*filter));

    if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)

        goto fail;

    *outFilterSize= filterSize;

    if (flags&SWS_PRINT_INFO)

        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);

    /* try to reduce the filter-size (step2 reduce it) */

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

        int j;

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

            if (j>=filter2Size) filter[i*filterSize + j]= 0;

            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];

            if((flags & SWS_BITEXACT) && j>=minFilterSize)

                filter[i*filterSize + j]= 0;

        }

    }

    //FIXME try to align filterPos if possible

    //fix borders

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

        int j;

        if ((*filterPos)[i] < 0) {

            // move filter coefficients left to compensate for filterPos

            for (j=1; j<filterSize; j++) {

                int left= FFMAX(j + (*filterPos)[i], 0);

                filter[i*filterSize + left] += filter[i*filterSize + j];

                filter[i*filterSize + j]=0;

            }

            (*filterPos)[i]= 0;

        }

        if ((*filterPos)[i] + filterSize > srcW) {

            int shift= (*filterPos)[i] + filterSize - srcW;

            // move filter coefficients right to compensate for filterPos

            for (j=filterSize-2; j>=0; j--) {

                int right= FFMIN(j + shift, filterSize-1);

                filter[i*filterSize +right] += filter[i*filterSize +j];

                filter[i*filterSize +j]=0;

            }

            (*filterPos)[i]= srcW - filterSize;

        }

    }

    // Note the +1 is for the MMX scaler which reads over the end

    /* align at 16 for AltiVec (needed by hScale_altivec_real) */

    FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+3)*sizeof(int16_t), fail);

    /* normalize & store in outFilter */

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

        int j;

        int64_t error=0;

        int64_t sum=0;

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

            sum+= filter[i*filterSize + j];

        }

        sum= (sum + one/2)/ one;

        for (j=0; j<*outFilterSize; j++) {

            int64_t v= filter[i*filterSize + j] + error;

            int intV= ROUNDED_DIV(v, sum);

            (*outFilter)[i*(*outFilterSize) + j]= intV;

            error= v - intV*sum;

        }

    }

    (*filterPos)[dstW+0] =

    (*filterPos)[dstW+1] =

    (*filterPos)[dstW+2] = (*filterPos)[dstW-1]; // the MMX/SSE scaler will read over the end

    for (i=0; i<*outFilterSize; i++) {

        int k= (dstW - 1) * (*outFilterSize) + i;

        (*outFilter)[k + 1 * (*outFilterSize)] =

        (*outFilter)[k + 2 * (*outFilterSize)] =

        (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];

    }

    ret=0;

fail:

    av_free(filter);

    av_free(filter2);

    return ret;

}


int update_dimensions(VP8Context *s, int width, int height, int is_vp7)

{

    AVCodecContext *avctx = s->avctx;

    int i, ret;

    if (width  != s->avctx->width ||

        height != s->avctx->height) {

        vp8_decode_flush_impl(s->avctx, 1);

        ret = ff_set_dimensions(s->avctx, width, height);

        if (ret < 0)

            return ret;

    }

    s->mb_width  = (s->avctx->coded_width  + 15) / 16;

    s->mb_height = (s->avctx->coded_height + 15) / 16;

    s->mb_layout = is_vp7 || avctx->active_thread_type == FF_THREAD_SLICE &&

                   FFMIN(s->num_coeff_partitions, avctx->thread_count) > 1;

    if (!s->mb_layout) { // Frame threading and one thread

        s->macroblocks_base       = av_mallocz((s->mb_width + s->mb_height * 2 + 1) *

                                               sizeof(*s->macroblocks));

        s->intra4x4_pred_mode_top = av_mallocz(s->mb_width * 4);

    } else // Sliced threading

        s->macroblocks_base = av_mallocz((s->mb_width + 2) * (s->mb_height + 2) *

                                         sizeof(*s->macroblocks));

    s->top_nnz     = av_mallocz(s->mb_width * sizeof(*s->top_nnz));

    s->top_border  = av_mallocz((s->mb_width + 1) * sizeof(*s->top_border));

    s->thread_data = av_mallocz(MAX_THREADS * sizeof(VP8ThreadData));

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

        s->thread_data[i].filter_strength =

            av_mallocz(s->mb_width * sizeof(*s->thread_data[0].filter_strength));

#if HAVE_THREADS

        pthread_mutex_init(&s->thread_data[i].lock, NULL);

        pthread_cond_init(&s->thread_data[i].cond, NULL);

#endif

    }

    if (!s->macroblocks_base || !s->top_nnz || !s->top_border ||

        (!s->intra4x4_pred_mode_top && !s->mb_layout))

        return AVERROR(ENOMEM);

    s->macroblocks = s->macroblocks_base + 1;

    return 0;

}


static int decode_main_header(NUTContext *nut){

    AVFormatContext *s= nut->avf;

    ByteIOContext *bc = &s->pb;

    uint64_t tmp;

    int i, j;

    get_packetheader(nut, bc, 8, 1);

    tmp = get_v(bc);

    if (tmp != 1){

	av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp);

        return -1;

    }

    nut->stream_count = get_v(bc);

    get_v(bc); //checksum threshold

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

        int tmp_flags = get_v(bc);

        int tmp_stream= get_v(bc);

        int tmp_mul   = get_v(bc);

        int tmp_size  = get_v(bc);

        int count     = get_v(bc);

        if(count == 0 || i+count > 256){

            av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);

            return -1;

        }

        if((tmp_flags & FLAG_FRAME_TYPE) && tmp_flags != 1){

            if(tmp_flags & FLAG_PRED_KEY_FRAME){

                av_log(s, AV_LOG_ERROR, "keyframe prediction in non 0 frame type\n");

                return -1;

            }

            if(!(tmp_flags & FLAG_PTS) || !(tmp_flags & FLAG_FULL_PTS) ){

                av_log(s, AV_LOG_ERROR, "no full pts in non 0 frame type\n");

                return -1;

            }

        }

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

            if(tmp_stream > nut->stream_count + 1){

                av_log(s, AV_LOG_ERROR, "illegal stream number\n");

                return -1;

            }

            nut->frame_code[i].flags           = tmp_flags ;

            nut->frame_code[i].stream_id_plus1 = tmp_stream;

            nut->frame_code[i].size_mul        = tmp_mul   ;

            nut->frame_code[i].size_lsb        = tmp_size  ;

            if(++tmp_size >= tmp_mul){

                tmp_size=0;

                tmp_stream++;

            }

        }

    }

    if(nut->frame_code['N'].flags != 1){

        av_log(s, AV_LOG_ERROR, "illegal frame_code table\n");

        return -1;

    }

    if(check_checksum(bc)){

        av_log(s, AV_LOG_ERROR, "Main header checksum missmatch\n");

        return -1;

    }

    return 0;

}


void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)

{

    /* see similar ffmpeg.c:grow_array() */

    int nb, nb_alloc;

    intptr_t *tab;

    nb = *nb_ptr;

    tab = *(intptr_t**)tab_ptr;

    if ((nb & (nb - 1)) == 0) {

        if (nb == 0)

            nb_alloc = 1;

        else

            nb_alloc = nb * 2;

        tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));

        *(intptr_t**)tab_ptr = tab;

    }

    tab[nb++] = (intptr_t)elem;

    *nb_ptr = nb;

}


static void write_long(unsigned char *p,uint32_t v)

{

	p[0] = v>>24;

	p[1] = v>>16;

	p[2] = v>>8;

	p[3] = v;

}


static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,

                               int w, int prefs, int mrefs, int parity, int mode)

{

    uint16_t *dst  = dst1;

    uint16_t *prev = prev1;

    uint16_t *cur  = cur1;

    uint16_t *next = next1;

    int x;

    uint16_t *prev2 = parity ? prev : cur ;

    uint16_t *next2 = parity ? cur  : next;

    mrefs /= 2;

    prefs /= 2;

    FILTER(0, 3, 0)

    dst   = (uint16_t*)dst1  + w - 3;

    prev  = (uint16_t*)prev1 + w - 3;

    cur   = (uint16_t*)cur1  + w - 3;

    next  = (uint16_t*)next1 + w - 3;

    prev2 = (uint16_t*)(parity ? prev : cur);

    next2 = (uint16_t*)(parity ? cur  : next);

    FILTER(w - 3, w, 0)

}


int32 float32_to_int32_round_to_zero( float32 a STATUS_PARAM )

{

    flag aSign;

    int16 aExp, shiftCount;

    uint32_t aSig;

    int32 z;

    a = float32_squash_input_denormal(a STATUS_VAR);

    aSig = extractFloat32Frac( a );

    aExp = extractFloat32Exp( a );

    aSign = extractFloat32Sign( a );

    shiftCount = aExp - 0x9E;

    if ( 0 <= shiftCount ) {

        if ( float32_val(a) != 0xCF000000 ) {

            float_raise( float_flag_invalid STATUS_VAR);

            if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) return 0x7FFFFFFF;

        }

        return (int32_t) 0x80000000;

    }

    else if ( aExp <= 0x7E ) {

        if ( aExp | aSig ) STATUS(float_exception_flags) |= float_flag_inexact;

        return 0;

    }

    aSig = ( aSig | 0x00800000 )<<8;

    z = aSig>>( - shiftCount );

    if ( (uint32_t) ( aSig<<( shiftCount & 31 ) ) ) {

        STATUS(float_exception_flags) |= float_flag_inexact;

    }

    if ( aSign ) z = - z;

    return z;

}


static int avi_load_index(AVFormatContext *s)

{

    AVIContext *avi = s->priv_data;

    ByteIOContext *pb = s->pb;

    uint32_t tag, size;

    int64_t pos= url_ftell(pb);

    url_fseek(pb, avi->movi_end, SEEK_SET);

#ifdef DEBUG_SEEK

    printf("movi_end=0x%"PRIx64"\n", avi->movi_end);

#endif

    for(;;) {

        if (url_feof(pb))

            break;

        tag = get_le32(pb);

        size = get_le32(pb);

#ifdef DEBUG_SEEK

        printf("tag=%c%c%c%c size=0x%x\n",

               tag & 0xff,

               (tag >> 8) & 0xff,

               (tag >> 16) & 0xff,

               (tag >> 24) & 0xff,

               size);

#endif

        switch(tag) {

        case MKTAG('i', 'd', 'x', '1'):

            if (avi_read_idx1(s, size) < 0)

                goto skip;

            else

                goto the_end;

            break;

        default:

        skip:

            size += (size & 1);

            url_fskip(pb, size);

            break;

        }

    }

 the_end:

    url_fseek(pb, pos, SEEK_SET);

    return 0;

}


int usb_desc_handle_control(USBDevice *dev, USBPacket *p,

        int request, int value, int index, int length, uint8_t *data)

{

    const USBDesc *desc = usb_device_get_usb_desc(dev);

    int ret = -1;

    assert(desc != NULL);

    switch(request) {

    case DeviceOutRequest | USB_REQ_SET_ADDRESS:

        dev->addr = value;

        trace_usb_set_addr(dev->addr);

        ret = 0;

        break;

    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:

        ret = usb_desc_get_descriptor(dev, value, data, length);

        break;

    case DeviceRequest | USB_REQ_GET_CONFIGURATION:

        data[0] = dev->config->bConfigurationValue;

        ret = 1;

        break;

    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:

        ret = usb_desc_set_config(dev, value);

        trace_usb_set_config(dev->addr, value, ret);

        break;

    case DeviceRequest | USB_REQ_GET_STATUS:

        data[0] = 0;

        if (dev->config->bmAttributes & 0x40) {

            data[0] |= 1 << USB_DEVICE_SELF_POWERED;

        }

        if (dev->remote_wakeup) {

            data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;

        }

        data[1] = 0x00;

        ret = 2;

        break;

    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:

        if (value == USB_DEVICE_REMOTE_WAKEUP) {

            dev->remote_wakeup = 0;

            ret = 0;

        }

        trace_usb_clear_device_feature(dev->addr, value, ret);

        break;

    case DeviceOutRequest | USB_REQ_SET_FEATURE:

        if (value == USB_DEVICE_REMOTE_WAKEUP) {

            dev->remote_wakeup = 1;

            ret = 0;

        }

        trace_usb_set_device_feature(dev->addr, value, ret);

        break;

    case InterfaceRequest | USB_REQ_GET_INTERFACE:

        if (index < 0 || index >= dev->ninterfaces) {

            break;

        }

        data[0] = dev->altsetting[index];

        ret = 1;

        break;

    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:

        ret = usb_desc_set_interface(dev, index, value);

        trace_usb_set_interface(dev->addr, index, value, ret);

        break;

    }

    return ret;

}


static void imx_eth_write(void *opaque, hwaddr offset, uint64_t value,

                           unsigned size)

{

    IMXFECState *s = IMX_FEC(opaque);

    uint32_t index = offset >> 2;

    FEC_PRINTF("reg[%s] <= 0x%" PRIx32 "\n", imx_eth_reg_name(s, index),

                (uint32_t)value);

    switch (index) {

    case ENET_EIR:

        s->regs[index] &= ~value;

        break;

    case ENET_EIMR:

        s->regs[index] = value;

        break;

    case ENET_RDAR:

        if (s->regs[ENET_ECR] & ENET_ECR_ETHEREN) {

            if (!s->regs[index]) {

                s->regs[index] = ENET_RDAR_RDAR;

                imx_eth_enable_rx(s);

            }

        } else {

            s->regs[index] = 0;

        }

        break;

    case ENET_TDAR:

        if (s->regs[ENET_ECR] & ENET_ECR_ETHEREN) {

            s->regs[index] = ENET_TDAR_TDAR;

            imx_eth_do_tx(s);

        }

        s->regs[index] = 0;

        break;

    case ENET_ECR:

        if (value & ENET_ECR_RESET) {

            return imx_eth_reset(DEVICE(s));

        }

        s->regs[index] = value;

        if ((s->regs[index] & ENET_ECR_ETHEREN) == 0) {

            s->regs[ENET_RDAR] = 0;

            s->rx_descriptor = s->regs[ENET_RDSR];

            s->regs[ENET_TDAR] = 0;

            s->tx_descriptor = s->regs[ENET_TDSR];

        }

        break;

    case ENET_MMFR:

        s->regs[index] = value;

        if (extract32(value, 29, 1)) {

            /* This is a read operation */

            s->regs[ENET_MMFR] = deposit32(s->regs[ENET_MMFR], 0, 16,

                                           do_phy_read(s,

                                                       extract32(value,

                                                                 18, 10)));

        } else {

            /* This a write operation */

            do_phy_write(s, extract32(value, 18, 10), extract32(value, 0, 16));

        }

        /* raise the interrupt as the PHY operation is done */

        s->regs[ENET_EIR] |= ENET_INT_MII;

        break;

    case ENET_MSCR:

        s->regs[index] = value & 0xfe;

        break;

    case ENET_MIBC:

        /* TODO: Implement MIB.  */

        s->regs[index] = (value & 0x80000000) ? 0xc0000000 : 0;

        break;

    case ENET_RCR:

        s->regs[index] = value & 0x07ff003f;

        /* TODO: Implement LOOP mode.  */

        break;

    case ENET_TCR:

        /* We transmit immediately, so raise GRA immediately.  */

        s->regs[index] = value;

        if (value & 1) {

            s->regs[ENET_EIR] |= ENET_INT_GRA;

        }

        break;

    case ENET_PALR:

        s->regs[index] = value;

        s->conf.macaddr.a[0] = value >> 24;

        s->conf.macaddr.a[1] = value >> 16;

        s->conf.macaddr.a[2] = value >> 8;

        s->conf.macaddr.a[3] = value;

        break;

    case ENET_PAUR:

        s->regs[index] = (value | 0x0000ffff) & 0xffff8808;

        s->conf.macaddr.a[4] = value >> 24;

        s->conf.macaddr.a[5] = value >> 16;

        break;

    case ENET_OPD:

        s->regs[index] = (value & 0x0000ffff) | 0x00010000;

        break;

    case ENET_IAUR:

    case ENET_IALR:

    case ENET_GAUR:

    case ENET_GALR:

        /* TODO: implement MAC hash filtering.  */

        break;

    case ENET_TFWR:

        if (s->is_fec) {

            s->regs[index] = value & 0x3;

        } else {

            s->regs[index] = value & 0x13f;

        }

        break;

    case ENET_RDSR:

        if (s->is_fec) {

            s->regs[index] = value & ~3;

        } else {

            s->regs[index] = value & ~7;

        }

        s->rx_descriptor = s->regs[index];

        break;

    case ENET_TDSR:

        if (s->is_fec) {

            s->regs[index] = value & ~3;

        } else {

            s->regs[index] = value & ~7;

        }

        s->tx_descriptor = s->regs[index];

        break;

    case ENET_MRBR:

        s->regs[index] = value & 0x00003ff0;

        break;

    default:

        if (s->is_fec) {

            imx_fec_write(s, index, value);

        } else {

            imx_enet_write(s, index, value);

        }

        return;

    }

    imx_eth_update(s);

}


int qemu_chr_open_spice(QemuOpts *opts, CharDriverState **_chr)

{

    CharDriverState *chr;

    SpiceCharDriver *s;

    const char* name = qemu_opt_get(opts, "name");

    uint32_t debug = qemu_opt_get_number(opts, "debug", 0);

    const char** psubtype = spice_server_char_device_recognized_subtypes();

    const char *subtype = NULL;

    if (name == NULL) {

        fprintf(stderr, "spice-qemu-char: missing name parameter\n");

        print_allowed_subtypes();

        return -EINVAL;

    }

    for(;*psubtype != NULL; ++psubtype) {

        if (strcmp(name, *psubtype) == 0) {

            subtype = *psubtype;

            break;

        }

    }

    if (subtype == NULL) {

        fprintf(stderr, "spice-qemu-char: unsupported name\n");

        print_allowed_subtypes();

        return -EINVAL;

    }

    chr = g_malloc0(sizeof(CharDriverState));

    s = g_malloc0(sizeof(SpiceCharDriver));

    s->chr = chr;

    s->debug = debug;

    s->active = false;

    s->sin.subtype = subtype;

    chr->opaque = s;

    chr->chr_write = spice_chr_write;

    chr->chr_close = spice_chr_close;

    chr->chr_guest_open = spice_chr_guest_open;

    chr->chr_guest_close = spice_chr_guest_close;

#if SPICE_SERVER_VERSION < 0x000901

    /* See comment in vmc_state() */

    if (strcmp(subtype, "vdagent") == 0) {

        qemu_chr_generic_open(chr);

    }

#endif

    *_chr = chr;

    return 0;

}


static char *get_content_url(xmlNodePtr *baseurl_nodes,

                             int n_baseurl_nodes,

                             char *rep_id_val,

                             char *rep_bandwidth_val,

                             char *val)

{

    int i;

    char *text;

    char *url = NULL;

    char tmp_str[MAX_URL_SIZE];

    char tmp_str_2[MAX_URL_SIZE];

    memset(tmp_str, 0, sizeof(tmp_str));

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

        if (baseurl_nodes[i] &&

            baseurl_nodes[i]->children &&

            baseurl_nodes[i]->children->type == XML_TEXT_NODE) {

            text = xmlNodeGetContent(baseurl_nodes[i]->children);

            if (text) {

                memset(tmp_str, 0, sizeof(tmp_str));

                memset(tmp_str_2, 0, sizeof(tmp_str_2));

                ff_make_absolute_url(tmp_str_2, MAX_URL_SIZE, tmp_str, text);

                av_strlcpy(tmp_str, tmp_str_2, sizeof(tmp_str));

                xmlFree(text);

            }

        }

    }

    if (val)

        av_strlcat(tmp_str, (const char*)val, sizeof(tmp_str));

    if (rep_id_val) {

        url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);

        if (!url) {

            return NULL;

        }

        av_strlcpy(tmp_str, url, sizeof(tmp_str));

        av_free(url);

    }

    if (rep_bandwidth_val && tmp_str[0] != '\0') {

        url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);

        if (!url) {

            return NULL;

        }

    }

    return url;

}


av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)

{

    const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;

    if (avctx->lowres==1) {

        c->idct_put  = ff_jref_idct4_put;

        c->idct_add  = ff_jref_idct4_add;

        c->idct      = ff_j_rev_dct4;

        c->perm_type = FF_IDCT_PERM_NONE;

    } else if (avctx->lowres==2) {

        c->idct_put  = ff_jref_idct2_put;

        c->idct_add  = ff_jref_idct2_add;

        c->idct      = ff_j_rev_dct2;

        c->perm_type = FF_IDCT_PERM_NONE;

    } else if (avctx->lowres==3) {

        c->idct_put  = ff_jref_idct1_put;

        c->idct_add  = ff_jref_idct1_add;

        c->idct      = ff_j_rev_dct1;

        c->perm_type = FF_IDCT_PERM_NONE;

    } else {

        if (avctx->bits_per_raw_sample == 10 || avctx->bits_per_raw_sample == 9) {

            c->idct_put              = ff_simple_idct_put_10;

            c->idct_add              = ff_simple_idct_add_10;

            c->idct                  = ff_simple_idct_10;

            c->perm_type             = FF_IDCT_PERM_NONE;

        } else if (avctx->bits_per_raw_sample == 12) {

            c->idct_put              = ff_simple_idct_put_12;

            c->idct_add              = ff_simple_idct_add_12;

            c->idct                  = ff_simple_idct_12;

            c->perm_type             = FF_IDCT_PERM_NONE;

        } else {

            if (avctx->idct_algo == FF_IDCT_INT) {

                c->idct_put  = ff_jref_idct_put;

                c->idct_add  = ff_jref_idct_add;

                c->idct      = ff_j_rev_dct;

                c->perm_type = FF_IDCT_PERM_LIBMPEG2;

#if CONFIG_FAANIDCT

            } else if (avctx->idct_algo == FF_IDCT_FAAN) {

                c->idct_put  = ff_faanidct_put;

                c->idct_add  = ff_faanidct_add;

                c->idct      = ff_faanidct;

                c->perm_type = FF_IDCT_PERM_NONE;

#endif /* CONFIG_FAANIDCT */

            } else { // accurate/default

                c->idct_put  = ff_simple_idct_put_8;

                c->idct_add  = ff_simple_idct_add_8;

                c->idct      = ff_simple_idct_8;

                c->perm_type = FF_IDCT_PERM_NONE;

            }

        }

    }

    c->put_pixels_clamped        = put_pixels_clamped_c;

    c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;

    c->add_pixels_clamped        = add_pixels_clamped_c;

    if (CONFIG_MPEG4_DECODER && avctx->idct_algo == FF_IDCT_XVID)

        ff_xvid_idct_init(c, avctx);

    if (ARCH_AARCH64)

        ff_idctdsp_init_aarch64(c, avctx, high_bit_depth);

    if (ARCH_ALPHA)

        ff_idctdsp_init_alpha(c, avctx, high_bit_depth);

    if (ARCH_ARM)

        ff_idctdsp_init_arm(c, avctx, high_bit_depth);

    if (ARCH_PPC)

        ff_idctdsp_init_ppc(c, avctx, high_bit_depth);

    if (ARCH_X86)

        ff_idctdsp_init_x86(c, avctx, high_bit_depth);

    if (ARCH_MIPS)

        ff_idctdsp_init_mips(c, avctx, high_bit_depth);

    ff_put_pixels_clamped = c->put_pixels_clamped;

    ff_add_pixels_clamped = c->add_pixels_clamped;

    ff_init_scantable_permutation(c->idct_permutation,

                                  c->perm_type);

}


static void vnc_display_close(VncDisplay *vs)

{

    if (!vs)

        return;

    vs->enabled = false;

    vs->is_unix = false;

    if (vs->lsock != NULL) {

        if (vs->lsock_tag) {

            g_source_remove(vs->lsock_tag);

        }

        object_unref(OBJECT(vs->lsock));

        vs->lsock = NULL;

    }

    vs->ws_enabled = false;

    if (vs->lwebsock != NULL) {

        if (vs->lwebsock_tag) {

            g_source_remove(vs->lwebsock_tag);

        }

        object_unref(OBJECT(vs->lwebsock));

        vs->lwebsock = NULL;

    }

    vs->auth = VNC_AUTH_INVALID;

    vs->subauth = VNC_AUTH_INVALID;

    if (vs->tlscreds) {

        object_unparent(OBJECT(vs->tlscreds));

    }

    g_free(vs->tlsaclname);

    vs->tlsaclname = NULL;

}


void cpu_reset(CPUX86State *env)
{
    int i;
    memset(env, 0, offsetof(CPUX86State, breakpoints));
    tlb_flush(env, 1);
    env->old_exception = -1;
    /* init to reset state */
#ifdef CONFIG_SOFTMMU
    env->hflags |= HF_SOFTMMU_MASK;
#endif
    env->hflags2 |= HF2_GIF_MASK;
    cpu_x86_update_cr0(env, 0x60000010);
    env->a20_mask = ~0x0;
    env->smbase = 0x30000;
    env->idt.limit = 0xffff;
    env->gdt.limit = 0xffff;
    env->ldt.limit = 0xffff;
    env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
    env->tr.limit = 0xffff;
    env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
    cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff,
                           DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK | DESC_R_MASK);
    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff,
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff,
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff,
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
    cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff,
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
    cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff,
                           DESC_P_MASK | DESC_S_MASK | DESC_W_MASK);
    env->eip = 0xfff0;
    env->regs[R_EDX] = env->cpuid_version;
    env->eflags = 0x2;
    /* FPU init */
    for(i = 0;i < 8; i++)
        env->fptags[i] = 1;
    env->fpuc = 0x37f;
    env->mxcsr = 0x1f80;
    memset(env->dr, 0, sizeof(env->dr));
    env->dr[6] = DR6_FIXED_1;
    env->dr[7] = DR7_FIXED_1;
    cpu_breakpoint_remove_all(env, BP_CPU);
    cpu_watchpoint_remove_all(env, BP_CPU);


static int get_cod(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)

{

    J2kCodingStyle tmp;

    int compno;

    if (s->buf_end - s->buf < 5)

        return AVERROR(EINVAL);

    tmp.log2_prec_width  =

    tmp.log2_prec_height = 15;

    tmp.csty = bytestream_get_byte(&s->buf);

    if (bytestream_get_byte(&s->buf)){ // progression level

        av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");

        return -1;

    }

    tmp.nlayers = bytestream_get_be16(&s->buf);

        tmp.mct = bytestream_get_byte(&s->buf); // multiple component transformation

    get_cox(s, &tmp);

    for (compno = 0; compno < s->ncomponents; compno++){

        if (!(properties[compno] & HAD_COC))

            memcpy(c + compno, &tmp, sizeof(J2kCodingStyle));

    }

    return 0;

}


static void create_cps(MaltaState *s, const char *cpu_model,

                       qemu_irq *cbus_irq, qemu_irq *i8259_irq)

{

    Error *err = NULL;

    s->cps = g_new0(MIPSCPSState, 1);

    object_initialize(s->cps, sizeof(MIPSCPSState), TYPE_MIPS_CPS);

    qdev_set_parent_bus(DEVICE(s->cps), sysbus_get_default());

    object_property_set_str(OBJECT(s->cps), cpu_model, "cpu-model", &err);

    object_property_set_int(OBJECT(s->cps), smp_cpus, "num-vp", &err);

    object_property_set_bool(OBJECT(s->cps), true, "realized", &err);

    if (err != NULL) {

        error_report("%s", error_get_pretty(err));

        exit(1);

    }

    sysbus_mmio_map_overlap(SYS_BUS_DEVICE(s->cps), 0, 0, 1);

    /* FIXME: When GIC is present then we should use GIC's IRQ 3.

       Until then CPS exposes CPU's IRQs thus use the default IRQ 2. */

    *i8259_irq = get_cps_irq(s->cps, 2);

    *cbus_irq = NULL;

}


void qmp_change_backing_file(const char *device,

                             const char *image_node_name,

                             const char *backing_file,

                             Error **errp)

{

    BlockBackend *blk;

    BlockDriverState *bs = NULL;

    AioContext *aio_context;

    BlockDriverState *image_bs = NULL;

    Error *local_err = NULL;

    bool ro;

    int open_flags;

    int ret;

    blk = blk_by_name(device);

    if (!blk) {

        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,

                  "Device '%s' not found", device);

        return;

    }

    aio_context = blk_get_aio_context(blk);

    aio_context_acquire(aio_context);

    if (!blk_is_available(blk)) {

        error_setg(errp, "Device '%s' has no medium", device);

        goto out;

    }

    bs = blk_bs(blk);

    image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        goto out;

    }

    if (!image_bs) {

        error_setg(errp, "image file not found");

        goto out;

    }

    if (bdrv_find_base(image_bs) == image_bs) {

        error_setg(errp, "not allowing backing file change on an image "

                         "without a backing file");

        goto out;

    }

    /* even though we are not necessarily operating on bs, we need it to

     * determine if block ops are currently prohibited on the chain */

    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {

        goto out;

    }

    /* final sanity check */

    if (!bdrv_chain_contains(bs, image_bs)) {

        error_setg(errp, "'%s' and image file are not in the same chain",

                   device);

        goto out;

    }

    /* if not r/w, reopen to make r/w */

    open_flags = image_bs->open_flags;

    ro = bdrv_is_read_only(image_bs);

    if (ro) {

        bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);

        if (local_err) {

            error_propagate(errp, local_err);

            goto out;

        }

    }

    ret = bdrv_change_backing_file(image_bs, backing_file,

                               image_bs->drv ? image_bs->drv->format_name : "");

    if (ret < 0) {

        error_setg_errno(errp, -ret, "Could not change backing file to '%s'",

                         backing_file);

        /* don't exit here, so we can try to restore open flags if

         * appropriate */

    }

    if (ro) {

        bdrv_reopen(image_bs, open_flags, &local_err);

        if (local_err) {

            error_propagate(errp, local_err); /* will preserve prior errp */

        }

    }

out:

    aio_context_release(aio_context);

}


int pci_parse_devaddr(const char *addr, int *domp, int *busp,

                      unsigned int *slotp, unsigned int *funcp)

{

    const char *p;

    char *e;

    unsigned long val;

    unsigned long dom = 0, bus = 0;

    unsigned int slot = 0;

    unsigned int func = 0;

    p = addr;

    val = strtoul(p, &e, 16);

    if (e == p)

	return -1;

    if (*e == ':') {

	bus = val;

	p = e + 1;

	val = strtoul(p, &e, 16);

	if (e == p)

	    return -1;

	if (*e == ':') {

	    dom = bus;

	    bus = val;

	    p = e + 1;

	    val = strtoul(p, &e, 16);

	    if (e == p)

		return -1;

	}

    }

    slot = val;

    if (funcp != NULL) {

        if (*e != '.')

            return -1;

        p = e + 1;

        val = strtoul(p, &e, 16);

        if (e == p)

            return -1;

        func = val;

    }

    /* if funcp == NULL func is 0 */

    if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)

	return -1;

    if (*e)

	return -1;

    /* Note: QEMU doesn't implement domains other than 0 */

    if (!pci_find_bus(pci_find_root_bus(dom), bus))

	return -1;

    *domp = dom;

    *busp = bus;

    *slotp = slot;

    if (funcp != NULL)

        *funcp = func;

    return 0;

}


static void x86_cpu_load_features(X86CPU *cpu, Error **errp)

{

    CPUX86State *env = &cpu->env;

    FeatureWord w;

    GList *l;

    Error *local_err = NULL;

    /*TODO: cpu->max_features incorrectly overwrites features

     * set using "feat=on|off". Once we fix this, we can convert

     * plus_features & minus_features to global properties

     * inside x86_cpu_parse_featurestr() too.

     */

    if (cpu->max_features) {

        for (w = 0; w < FEATURE_WORDS; w++) {

            env->features[w] =

                x86_cpu_get_supported_feature_word(w, cpu->migratable);

        }

    }

    for (l = plus_features; l; l = l->next) {

        const char *prop = l->data;

        object_property_set_bool(OBJECT(cpu), true, prop, &local_err);

        if (local_err) {

            goto out;

        }

    }

    for (l = minus_features; l; l = l->next) {

        const char *prop = l->data;

        object_property_set_bool(OBJECT(cpu), false, prop, &local_err);

        if (local_err) {

            goto out;

        }

    }

    if (!kvm_enabled() || !cpu->expose_kvm) {

        env->features[FEAT_KVM] = 0;

    }

    x86_cpu_enable_xsave_components(cpu);

    /* CPUID[EAX=7,ECX=0].EBX always increased level automatically: */

    x86_cpu_adjust_feat_level(cpu, FEAT_7_0_EBX);

    if (cpu->full_cpuid_auto_level) {

        x86_cpu_adjust_feat_level(cpu, FEAT_1_EDX);

        x86_cpu_adjust_feat_level(cpu, FEAT_1_ECX);

        x86_cpu_adjust_feat_level(cpu, FEAT_6_EAX);

        x86_cpu_adjust_feat_level(cpu, FEAT_7_0_ECX);

        x86_cpu_adjust_feat_level(cpu, FEAT_8000_0001_EDX);

        x86_cpu_adjust_feat_level(cpu, FEAT_8000_0001_ECX);

        x86_cpu_adjust_feat_level(cpu, FEAT_8000_0007_EDX);

        x86_cpu_adjust_feat_level(cpu, FEAT_C000_0001_EDX);

        x86_cpu_adjust_feat_level(cpu, FEAT_SVM);

        x86_cpu_adjust_feat_level(cpu, FEAT_XSAVE);

        /* SVM requires CPUID[0x8000000A] */

        if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {

            x86_cpu_adjust_level(cpu, &env->cpuid_min_xlevel, 0x8000000A);

        }

    }

    /* Set cpuid_*level* based on cpuid_min_*level, if not explicitly set */

    if (env->cpuid_level == UINT32_MAX) {

        env->cpuid_level = env->cpuid_min_level;

    }

    if (env->cpuid_xlevel == UINT32_MAX) {

        env->cpuid_xlevel = env->cpuid_min_xlevel;

    }

    if (env->cpuid_xlevel2 == UINT32_MAX) {

        env->cpuid_xlevel2 = env->cpuid_min_xlevel2;

    }

out:

    if (local_err != NULL) {

        error_propagate(errp, local_err);

    }

}


static void vnc_init_basic_info(SocketAddressLegacy *addr,

                                VncBasicInfo *info,

                                Error **errp)

{

    switch (addr->type) {

    case SOCKET_ADDRESS_LEGACY_KIND_INET:

        info->host = g_strdup(addr->u.inet.data->host);

        info->service = g_strdup(addr->u.inet.data->port);

        if (addr->u.inet.data->ipv6) {

            info->family = NETWORK_ADDRESS_FAMILY_IPV6;

        } else {

            info->family = NETWORK_ADDRESS_FAMILY_IPV4;

        }

        break;

    case SOCKET_ADDRESS_LEGACY_KIND_UNIX:

        info->host = g_strdup("");

        info->service = g_strdup(addr->u.q_unix.data->path);

        info->family = NETWORK_ADDRESS_FAMILY_UNIX;

        break;

    case SOCKET_ADDRESS_LEGACY_KIND_VSOCK:

    case SOCKET_ADDRESS_LEGACY_KIND_FD:

        error_setg(errp, "Unsupported socket address type %s",

                   SocketAddressLegacyKind_lookup[addr->type]);

        break;

    default:

        abort();

    }

    return;

}


static int mirror_cow_align(MirrorBlockJob *s,

                            int64_t *sector_num,

                            int *nb_sectors)

{

    bool need_cow;

    int ret = 0;

    int chunk_sectors = s->granularity >> BDRV_SECTOR_BITS;

    int64_t align_sector_num = *sector_num;

    int align_nb_sectors = *nb_sectors;

    int max_sectors = chunk_sectors * s->max_iov;

    need_cow = !test_bit(*sector_num / chunk_sectors, s->cow_bitmap);

    need_cow |= !test_bit((*sector_num + *nb_sectors - 1) / chunk_sectors,

                          s->cow_bitmap);

    if (need_cow) {

        bdrv_round_sectors_to_clusters(blk_bs(s->target), *sector_num,

                                       *nb_sectors, &align_sector_num,

                                       &align_nb_sectors);

    }

    if (align_nb_sectors > max_sectors) {

        align_nb_sectors = max_sectors;

        if (need_cow) {

            align_nb_sectors = QEMU_ALIGN_DOWN(align_nb_sectors,

                                               s->target_cluster_sectors);

        }

    }

    /* Clipping may result in align_nb_sectors unaligned to chunk boundary, but

     * that doesn't matter because it's already the end of source image. */

    mirror_clip_sectors(s, align_sector_num, &align_nb_sectors);

    ret = align_sector_num + align_nb_sectors - (*sector_num + *nb_sectors);

    *sector_num = align_sector_num;

    *nb_sectors = align_nb_sectors;

    assert(ret >= 0);

    return ret;

}


static void bonito_pciconf_writel(void *opaque, target_phys_addr_t addr,

                                  uint32_t val)

{

    PCIBonitoState *s = opaque;

    DPRINTF("bonito_pciconf_writel "TARGET_FMT_plx" val %x \n", addr, val);

    s->dev.config_write(&s->dev, addr, val, 4);

}


static int vhdx_create(const char *filename, QemuOpts *opts, Error **errp)

{

    int ret = 0;

    uint64_t image_size = (uint64_t) 2 * GiB;

    uint32_t log_size   = 1 * MiB;

    uint32_t block_size = 0;

    uint64_t signature;

    uint64_t metadata_offset;

    bool use_zero_blocks = false;

    gunichar2 *creator = NULL;

    glong creator_items;

    BlockBackend *blk;

    char *type = NULL;

    VHDXImageType image_type;

    Error *local_err = NULL;

    image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),

                          BDRV_SECTOR_SIZE);

    log_size = qemu_opt_get_size_del(opts, VHDX_BLOCK_OPT_LOG_SIZE, 0);

    block_size = qemu_opt_get_size_del(opts, VHDX_BLOCK_OPT_BLOCK_SIZE, 0);

    type = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT);

    use_zero_blocks = qemu_opt_get_bool_del(opts, VHDX_BLOCK_OPT_ZERO, true);

    if (image_size > VHDX_MAX_IMAGE_SIZE) {

        error_setg_errno(errp, EINVAL, "Image size too large; max of 64TB");

        ret = -EINVAL;

        goto exit;

    }

    if (type == NULL) {

        type = g_strdup("dynamic");

    }

    if (!strcmp(type, "dynamic")) {

        image_type = VHDX_TYPE_DYNAMIC;

    } else if (!strcmp(type, "fixed")) {

        image_type = VHDX_TYPE_FIXED;

    } else if (!strcmp(type, "differencing")) {

        error_setg_errno(errp, ENOTSUP,

                         "Differencing files not yet supported");

        ret = -ENOTSUP;

        goto exit;

    } else {

        ret = -EINVAL;

        goto exit;

    }

    /* These are pretty arbitrary, and mainly designed to keep the BAT

     * size reasonable to load into RAM */

    if (block_size == 0) {

        if (image_size > 32 * TiB) {

            block_size = 64 * MiB;

        } else if (image_size > (uint64_t) 100 * GiB) {

            block_size = 32 * MiB;

        } else if (image_size > 1 * GiB) {

            block_size = 16 * MiB;

        } else {

            block_size = 8 * MiB;

        }

    }

    /* make the log size close to what was specified, but must be

     * min 1MB, and multiple of 1MB */

    log_size = ROUND_UP(log_size, MiB);

    block_size = ROUND_UP(block_size, MiB);

    block_size = block_size > VHDX_BLOCK_SIZE_MAX ? VHDX_BLOCK_SIZE_MAX :

                                                    block_size;

    ret = bdrv_create_file(filename, opts, &local_err);

    if (ret < 0) {

        error_propagate(errp, local_err);

        goto exit;

    }

    blk = blk_new_open(filename, NULL, NULL,

                       BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err);

    if (blk == NULL) {

        error_propagate(errp, local_err);

        ret = -EIO;

        goto exit;

    }

    blk_set_allow_write_beyond_eof(blk, true);

    /* Create (A) */

    /* The creator field is optional, but may be useful for

     * debugging / diagnostics */

    creator = g_utf8_to_utf16("QEMU v" QEMU_VERSION, -1, NULL,

                              &creator_items, NULL);

    signature = cpu_to_le64(VHDX_FILE_SIGNATURE);

    ret = blk_pwrite(blk, VHDX_FILE_ID_OFFSET, &signature, sizeof(signature),

                     0);

    if (ret < 0) {

        goto delete_and_exit;

    }

    if (creator) {

        ret = blk_pwrite(blk, VHDX_FILE_ID_OFFSET + sizeof(signature),

                         creator, creator_items * sizeof(gunichar2), 0);

        if (ret < 0) {

            goto delete_and_exit;

        }

    }

    /* Creates (B),(C) */

    ret = vhdx_create_new_headers(blk_bs(blk), image_size, log_size);

    if (ret < 0) {

        goto delete_and_exit;

    }

    /* Creates (D),(E),(G) explicitly. (F) created as by-product */

    ret = vhdx_create_new_region_table(blk_bs(blk), image_size, block_size, 512,

                                       log_size, use_zero_blocks, image_type,

                                       &metadata_offset);

    if (ret < 0) {

        goto delete_and_exit;

    }

    /* Creates (H) */

    ret = vhdx_create_new_metadata(blk_bs(blk), image_size, block_size, 512,

                                   metadata_offset, image_type);

    if (ret < 0) {

        goto delete_and_exit;

    }

delete_and_exit:

    blk_unref(blk);

exit:

    g_free(type);

    g_free(creator);

    return ret;

}