static void vc1_mc_4mv_chroma(VC1Context *v, int dir)

{

    MpegEncContext *s = &v->s;

    H264ChromaContext *h264chroma = &v->h264chroma;

    uint8_t *srcU, *srcV;

    int uvmx, uvmy, uvsrc_x, uvsrc_y;

    int k, tx = 0, ty = 0;

    int mvx[4], mvy[4], intra[4], mv_f[4];

    int valid_count;

    int chroma_ref_type = v->cur_field_type, off = 0;

    int v_edge_pos = s->v_edge_pos >> v->field_mode;

    if (!v->field_mode && !v->s.last_picture.f.data[0])

        return;

    if (s->flags & CODEC_FLAG_GRAY)

        return;

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

        mvx[k] = s->mv[dir][k][0];

        mvy[k] = s->mv[dir][k][1];

        intra[k] = v->mb_type[0][s->block_index[k]];

        if (v->field_mode)

            mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];

    }

    /* calculate chroma MV vector from four luma MVs */

    if (!v->field_mode || (v->field_mode && !v->numref)) {

        valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);

        chroma_ref_type = v->reffield;

        if (!valid_count) {

            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;

            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;

            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;

            return; //no need to do MC for intra blocks

        }

    } else {

        int dominant = 0;

        if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)

            dominant = 1;

        valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);

        if (dominant)

            chroma_ref_type = !v->cur_field_type;

    }

    if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])

        return;

    s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;

    s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;

    uvmx = (tx + ((tx & 3) == 3)) >> 1;

    uvmy = (ty + ((ty & 3) == 3)) >> 1;

    v->luma_mv[s->mb_x][0] = uvmx;

    v->luma_mv[s->mb_x][1] = uvmy;

    if (v->fastuvmc) {

        uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));

        uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));

    }

    // Field conversion bias

    if (v->cur_field_type != chroma_ref_type)

        uvmy += 2 - 4 * chroma_ref_type;

    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);

    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);

    if (v->profile != PROFILE_ADVANCED) {

        uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width  * 8);

        uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);

    } else {

        uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width  >> 1);

        uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);

    }

    if (!dir) {

        if (v->field_mode) {

            if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {

                srcU = s->current_picture.f.data[1];

                srcV = s->current_picture.f.data[2];

            } else {

                srcU = s->last_picture.f.data[1];

                srcV = s->last_picture.f.data[2];

            }

        } else {

            srcU = s->last_picture.f.data[1];

            srcV = s->last_picture.f.data[2];

        }

    } else {

        srcU = s->next_picture.f.data[1];

        srcV = s->next_picture.f.data[2];

    }

    if(!srcU)

        return;

    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;

    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;

    if (v->field_mode) {

        if (chroma_ref_type) {

            srcU += s->current_picture_ptr->f.linesize[1];

            srcV += s->current_picture_ptr->f.linesize[2];

        }

        off = v->second_field ? s->current_picture_ptr->f.linesize[1] : 0;

    }

    if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)

        || s->h_edge_pos < 18 || v_edge_pos < 18

        || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9

        || (unsigned)uvsrc_y > (v_edge_pos    >> 1) - 9) {

        s->vdsp.emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize,

                                 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,

                                 s->h_edge_pos >> 1, v_edge_pos >> 1);

        s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,

                                 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,

                                 s->h_edge_pos >> 1, v_edge_pos >> 1);

        srcU = s->edge_emu_buffer;

        srcV = s->edge_emu_buffer + 16;

        /* if we deal with range reduction we need to scale source blocks */

        if (v->rangeredfrm) {

            int i, j;

            uint8_t *src, *src2;

            src  = srcU;

            src2 = srcV;

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

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

                    src[i]  = ((src[i]  - 128) >> 1) + 128;

                    src2[i] = ((src2[i] - 128) >> 1) + 128;

                }

                src  += s->uvlinesize;

                src2 += s->uvlinesize;

            }

        }

        /* if we deal with intensity compensation we need to scale source blocks */

        if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {

            int i, j;

            uint8_t *src, *src2;

            src  = srcU;

            src2 = srcV;

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

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

                    src[i]  = v->lutuv[src[i]];

                    src2[i] = v->lutuv[src2[i]];

                }

                src  += s->uvlinesize;

                src2 += s->uvlinesize;

            }

        }

    }

    /* Chroma MC always uses qpel bilinear */

    uvmx = (uvmx & 3) << 1;

    uvmy = (uvmy & 3) << 1;

    if (!v->rnd) {

        h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);

        h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);

    } else {

        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);

        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);

    }

}


PPC_OP(addc)

{

    T2 = T0;

    T0 += T1;

    if (T0 < T2) {

        xer_ca = 1;

    } else {

        xer_ca = 0;

    }

    RETURN();

}


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

{

    PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);

    DeviceClass *dc = DEVICE_CLASS(klass);

    HotplugHandlerClass *hp = HOTPLUG_HANDLER_CLASS(klass);

    hc->root_bus_path = spapr_phb_root_bus_path;

    dc->realize = spapr_phb_realize;

    dc->props = spapr_phb_properties;

    dc->reset = spapr_phb_reset;

    dc->vmsd = &vmstate_spapr_pci;

    set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);

    hp->plug = spapr_phb_hot_plug_child;

    hp->unplug = spapr_phb_hot_unplug_child;

}


int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,

                                const uint8_t *in_arg [SWR_CH_MAX], int  in_count){

    AudioData * in= &s->in;

    AudioData *out= &s->out;

    if (!swr_is_initialized(s)) {

        av_log(s, AV_LOG_ERROR, "Context has not been initialized\n");

        return AVERROR(EINVAL);

    }

    while(s->drop_output > 0){

        int ret;

        uint8_t *tmp_arg[SWR_CH_MAX];

#define MAX_DROP_STEP 16384

        if((ret=swri_realloc_audio(&s->drop_temp, FFMIN(s->drop_output, MAX_DROP_STEP)))<0)

            return ret;

        reversefill_audiodata(&s->drop_temp, tmp_arg);

        s->drop_output *= -1; //FIXME find a less hackish solution

        ret = swr_convert(s, tmp_arg, FFMIN(-s->drop_output, MAX_DROP_STEP), in_arg, in_count); //FIXME optimize but this is as good as never called so maybe it doesn't matter

        s->drop_output *= -1;

        in_count = 0;

        if(ret>0) {

            s->drop_output -= ret;

            if (!s->drop_output && !out_arg)

                return 0;

            continue;

        }

        av_assert0(s->drop_output);

        return 0;

    }

    if(!in_arg){

        if(s->resample){

            if (!s->flushed)

                s->resampler->flush(s);

            s->resample_in_constraint = 0;

            s->flushed = 1;

        }else if(!s->in_buffer_count){

            return 0;

        }

    }else

        fill_audiodata(in ,  (void*)in_arg);

    fill_audiodata(out, out_arg);

    if(s->resample){

        int ret = swr_convert_internal(s, out, out_count, in, in_count);

        if(ret>0 && !s->drop_output)

            s->outpts += ret * (int64_t)s->in_sample_rate;

        return ret;

    }else{

        AudioData tmp= *in;

        int ret2=0;

        int ret, size;

        size = FFMIN(out_count, s->in_buffer_count);

        if(size){

            buf_set(&tmp, &s->in_buffer, s->in_buffer_index);

            ret= swr_convert_internal(s, out, size, &tmp, size);

            if(ret<0)

                return ret;

            ret2= ret;

            s->in_buffer_count -= ret;

            s->in_buffer_index += ret;

            buf_set(out, out, ret);

            out_count -= ret;

            if(!s->in_buffer_count)

                s->in_buffer_index = 0;

        }

        if(in_count){

            size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;

            if(in_count > out_count) { //FIXME move after swr_convert_internal

                if(   size > s->in_buffer.count

                && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){

                    buf_set(&tmp, &s->in_buffer, s->in_buffer_index);

                    copy(&s->in_buffer, &tmp, s->in_buffer_count);

                    s->in_buffer_index=0;

                }else

                    if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)

                        return ret;

            }

            if(out_count){

                size = FFMIN(in_count, out_count);

                ret= swr_convert_internal(s, out, size, in, size);

                if(ret<0)

                    return ret;

                buf_set(in, in, ret);

                in_count -= ret;

                ret2 += ret;

            }

            if(in_count){

                buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);

                copy(&tmp, in, in_count);

                s->in_buffer_count += in_count;

            }

        }

        if(ret2>0 && !s->drop_output)

            s->outpts += ret2 * (int64_t)s->in_sample_rate;

        return ret2;

    }

}


static void spapr_machine_2_6_class_options(MachineClass *mc)

{

    sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);

    spapr_machine_2_7_class_options(mc);

    smc->dr_cpu_enabled = false;

    SET_MACHINE_COMPAT(mc, SPAPR_COMPAT_2_6);

}


static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)

{

    mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));

    if (!mxf->metadata_sets)

        return -1;

    mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;

    mxf->metadata_sets_count++;

    return 0;

}


monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,

                     struct disassemble_info *info)

{

    CPUDebug *s = container_of(info, CPUDebug, info);

    if (monitor_disas_is_physical) {

        cpu_physical_memory_read(memaddr, myaddr, length);

    } else {

        cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);

    }

    return 0;

}


static int ftp_type(FTPContext *s)

{

    const char *command = "TYPE I\r\n";

    const int type_codes[] = {200, 0};

    if (!ftp_send_command(s, command, type_codes, NULL))

        return AVERROR(EIO);

    return 0;

}


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

                                int *got_frame_ptr, AVPacket *avpkt)

{

    AVFrame *frame     = data;

    const uint8_t *buf = avpkt->data;

    int buf_size       = avpkt->size;

    ShortenContext *s  = avctx->priv_data;

    int i, input_buf_size = 0;

    int ret;

    /* allocate internal bitstream buffer */

    if (s->max_framesize == 0) {

        void *tmp_ptr;

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

        tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,

                                  s->max_framesize + FF_INPUT_BUFFER_PADDING_SIZE);

        if (!tmp_ptr) {

            av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");

            return AVERROR(ENOMEM);

        }

        memset(tmp_ptr, 0, s->allocated_bitstream_size);

        s->bitstream = tmp_ptr;

    }

    /* append current packet data to bitstream buffer */

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

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

        input_buf_size = buf_size;

        if (s->bitstream_index + s->bitstream_size + buf_size + FF_INPUT_BUFFER_PADDING_SIZE >

            s->allocated_bitstream_size) {

            memmove(s->bitstream, &s->bitstream[s->bitstream_index],

                    s->bitstream_size);

            s->bitstream_index = 0;

        }

        if (buf)

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

                   buf_size);

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

        buf_size         += s->bitstream_size;

        s->bitstream_size = buf_size;

        /* do not decode until buffer has at least max_framesize bytes or

         * the end of the file has been reached */

        if (buf_size < s->max_framesize && avpkt->data) {

            *got_frame_ptr = 0;

            return input_buf_size;

        }

    }

    /* init and position bitstream reader */

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

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

    /* process header or next subblock */

    if (!s->got_header) {

        if ((ret = read_header(s)) < 0)

            return ret;

        *got_frame_ptr = 0;

        goto finish_frame;

    }

    /* if quit command was read previously, don't decode anything */

    if (s->got_quit_command) {

        *got_frame_ptr = 0;

        return avpkt->size;

    }

    s->cur_chan = 0;

    while (s->cur_chan < s->channels) {

        unsigned cmd;

        int len;

        if (get_bits_left(&s->gb) < 3 + FNSIZE) {

            *got_frame_ptr = 0;

            break;

        }

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

        if (cmd > FN_VERBATIM) {

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

            *got_frame_ptr = 0;

            break;

        }

        if (!is_audio_command[cmd]) {

            /* process non-audio command */

            switch (cmd) {

            case FN_VERBATIM:

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

                while (len--)

                    get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);

                break;

            case FN_BITSHIFT:

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

                break;

            case FN_BLOCKSIZE: {

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

                if (blocksize > s->blocksize) {

                    av_log(avctx, AV_LOG_ERROR,

                           "Increasing block size is not supported\n");

                    return AVERROR_PATCHWELCOME;

                }

                if (!blocksize || blocksize > MAX_BLOCKSIZE) {

                    av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "

                                                "block size: %d\n", blocksize);

                    return AVERROR(EINVAL);

                }

                s->blocksize = blocksize;

                break;

            }

            case FN_QUIT:

                s->got_quit_command = 1;

                break;

            }

            if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {

                *got_frame_ptr = 0;

                break;

            }

        } else {

            /* process audio command */

            int residual_size = 0;

            int channel = s->cur_chan;

            int32_t coffset;

            /* get Rice code for residual decoding */

            if (cmd != FN_ZERO) {

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

                /* This is a hack as version 0 differed in the definition

                 * of get_sr_golomb_shorten(). */

                if (s->version == 0)

                    residual_size--;

            }

            /* calculate sample offset using means from previous blocks */

            if (s->nmean == 0)

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

            else {

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

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

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

                coffset = sum / s->nmean;

                if (s->version >= 2)

                    coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;

            }

            /* decode samples for this channel */

            if (cmd == FN_ZERO) {

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

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

            } else {

                if ((ret = decode_subframe_lpc(s, cmd, channel,

                                               residual_size, coffset)) < 0)

                    return ret;

            }

            /* update means with info from the current block */

            if (s->nmean > 0) {

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

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

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

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

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

                if (s->version < 2)

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

                else

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

            }

            /* copy wrap samples for use with next block */

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

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

            /* shift samples to add in unused zero bits which were removed

             * during encoding */

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

            /* if this is the last channel in the block, output the samples */

            s->cur_chan++;

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

                uint8_t *samples_u8;

                int16_t *samples_s16;

                int chan;

                /* get output buffer */

                frame->nb_samples = s->blocksize;

                if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)

                    return ret;

                for (chan = 0; chan < s->channels; chan++) {

                    samples_u8  = ((uint8_t **)frame->extended_data)[chan];

                    samples_s16 = ((int16_t **)frame->extended_data)[chan];

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

                        switch (s->internal_ftype) {

                        case TYPE_U8:

                            *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);

                            break;

                        case TYPE_S16HL:

                        case TYPE_S16LH:

                            *samples_s16++ = av_clip_int16(s->decoded[chan][i]);

                            break;

                        }

                    }

                }

                *got_frame_ptr = 1;

            }

        }

    }

    if (s->cur_chan < s->channels)

        *got_frame_ptr = 0;

finish_frame:

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

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

    if (i > buf_size) {

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

        s->bitstream_size  = 0;

        s->bitstream_index = 0;

        return AVERROR_INVALIDDATA;

    }

    if (s->bitstream_size) {

        s->bitstream_index += i;

        s->bitstream_size  -= i;

        return input_buf_size;

    } else

        return i;

}


static PCIReqIDCache pci_req_id_cache_get(PCIDevice *dev)

{

    PCIDevice *parent;

    PCIReqIDCache cache = {

        .dev = dev,

        .type = PCI_REQ_ID_BDF,

    };

    while (!pci_bus_is_root(dev->bus)) {

        /* We are under PCI/PCIe bridges */

        parent = dev->bus->parent_dev;

        if (pci_is_express(parent)) {

            if (pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) {

                /* When we pass through PCIe-to-PCI/PCIX bridges, we

                 * override the requester ID using secondary bus

                 * number of parent bridge with zeroed devfn

                 * (pcie-to-pci bridge spec chap 2.3). */

                cache.type = PCI_REQ_ID_SECONDARY_BUS;

                cache.dev = dev;

            }

        } else {

            /* Legacy PCI, override requester ID with the bridge's

             * BDF upstream.  When the root complex connects to

             * legacy PCI devices (including buses), it can only

             * obtain requester ID info from directly attached

             * devices.  If devices are attached under bridges, only

             * the requester ID of the bridge that is directly

             * attached to the root complex can be recognized. */

            cache.type = PCI_REQ_ID_BDF;

            cache.dev = parent;

        }

        dev = parent;

    }

    return cache;

}


static void test_visitor_out_struct_nested(TestOutputVisitorData *data,

                                           const void *unused)

{

    int64_t value = 42;

    Error *err = NULL;

    UserDefNested *ud2;

    QObject *obj;

    QDict *qdict, *dict1, *dict2, *dict3, *userdef;

    const char *string = "user def string";

    const char *strings[] = { "forty two", "forty three", "forty four",

                              "forty five" };

    ud2 = g_malloc0(sizeof(*ud2));

    ud2->string0 = g_strdup(strings[0]);

    ud2->dict1.string1 = g_strdup(strings[1]);

    ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));

    ud2->dict1.dict2.userdef1->string = g_strdup(string);

    ud2->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);

    ud2->dict1.dict2.userdef1->base->integer = value;

    ud2->dict1.dict2.string2 = g_strdup(strings[2]);

    ud2->dict1.has_dict3 = true;

    ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));

    ud2->dict1.dict3.userdef2->string = g_strdup(string);

    ud2->dict1.dict3.userdef2->base = g_new0(UserDefZero, 1);

    ud2->dict1.dict3.userdef2->base->integer = value;

    ud2->dict1.dict3.string3 = g_strdup(strings[3]);

    visit_type_UserDefNested(data->ov, &ud2, "unused", &err);

    g_assert(!err);

    obj = qmp_output_get_qobject(data->qov);

    g_assert(obj != NULL);

    g_assert(qobject_type(obj) == QTYPE_QDICT);

    qdict = qobject_to_qdict(obj);

    g_assert_cmpint(qdict_size(qdict), ==, 2);

    g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);

    dict1 = qdict_get_qdict(qdict, "dict1");

    g_assert_cmpint(qdict_size(dict1), ==, 3);

    g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);

    dict2 = qdict_get_qdict(dict1, "dict2");

    g_assert_cmpint(qdict_size(dict2), ==, 2);

    g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]);

    userdef = qdict_get_qdict(dict2, "userdef1");

    g_assert_cmpint(qdict_size(userdef), ==, 2);

    g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);

    g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);

    dict3 = qdict_get_qdict(dict1, "dict3");

    g_assert_cmpint(qdict_size(dict3), ==, 2);

    g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]);

    userdef = qdict_get_qdict(dict3, "userdef2");

    g_assert_cmpint(qdict_size(userdef), ==, 2);

    g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);

    g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);

    QDECREF(qdict);

    qapi_free_UserDefNested(ud2);

}


void bdrv_info_stats(Monitor *mon, QObject **ret_data)

{

    QObject *obj;

    QList *devices;

    BlockDriverState *bs;

    devices = qlist_new();

    for (bs = bdrv_first; bs != NULL; bs = bs->next) {

        obj = qobject_from_jsonf("{ 'device': %s, 'stats': {"

                                 "'rd_bytes': %" PRId64 ","

                                 "'wr_bytes': %" PRId64 ","

                                 "'rd_operations': %" PRId64 ","

                                 "'wr_operations': %" PRId64

                                 "} }",

                                 bs->device_name,

                                 bs->rd_bytes, bs->wr_bytes,

                                 bs->rd_ops, bs->wr_ops);

        assert(obj != NULL);

        qlist_append_obj(devices, obj);

    }

    *ret_data = QOBJECT(devices);

}


static block_number_t eckd_block_num(BootMapPointer *p)

{

    const uint64_t sectors = virtio_get_sectors();

    const uint64_t heads = virtio_get_heads();

    const uint64_t cylinder = p->eckd.cylinder

                            + ((p->eckd.head & 0xfff0) << 12);

    const uint64_t head = p->eckd.head & 0x000f;

    const block_number_t block = sectors * heads * cylinder

                               + sectors * head

                               + p->eckd.sector

                               - 1; /* block nr starts with zero */

    return block;

}


static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)

{

    trace_colo_compare_main("compare other");

    trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src),

                               inet_ntoa(ppkt->ip->ip_dst), spkt->size,

                               inet_ntoa(spkt->ip->ip_src),

                               inet_ntoa(spkt->ip->ip_dst));

    return colo_packet_compare(ppkt, spkt);

}


static inline void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, int opc)

{

    int addr_reg, data_reg, data_reg2, bswap;

#ifdef CONFIG_SOFTMMU

    int mem_index, s_bits;

# if TARGET_LONG_BITS == 64

    int addr_reg2;

# endif

    uint32_t *label_ptr;

#endif

#ifdef TARGET_WORDS_BIGENDIAN

    bswap = 1;

#else

    bswap = 0;

#endif

    data_reg = *args++;

    if (opc == 3)

        data_reg2 = *args++;

    else

        data_reg2 = 0; /* suppress warning */

    addr_reg = *args++;

#ifdef CONFIG_SOFTMMU

# if TARGET_LONG_BITS == 64

    addr_reg2 = *args++;

# endif

    mem_index = *args;

    s_bits = opc & 3;

    /* Should generate something like the following:

     *  shr r8, addr_reg, #TARGET_PAGE_BITS

     *  and r0, r8, #(CPU_TLB_SIZE - 1)   @ Assumption: CPU_TLB_BITS <= 8

     *  add r0, env, r0 lsl #CPU_TLB_ENTRY_BITS

     */

    tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                    TCG_REG_R8, 0, addr_reg, SHIFT_IMM_LSR(TARGET_PAGE_BITS));

    tcg_out_dat_imm(s, COND_AL, ARITH_AND,

                    TCG_REG_R0, TCG_REG_R8, CPU_TLB_SIZE - 1);

    tcg_out_dat_reg(s, COND_AL, ARITH_ADD, TCG_REG_R0,

                    TCG_AREG0, TCG_REG_R0, SHIFT_IMM_LSL(CPU_TLB_ENTRY_BITS));

    /* In the

     *  ldr r1 [r0, #(offsetof(CPUState, tlb_table[mem_index][0].addr_write))]

     * below, the offset is likely to exceed 12 bits if mem_index != 0 and

     * not exceed otherwise, so use an

     *  add r0, r0, #(mem_index * sizeof *CPUState.tlb_table)

     * before.

     */

    if (mem_index)

        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_R0, TCG_REG_R0,

                        (mem_index << (TLB_SHIFT & 1)) |

                        ((16 - (TLB_SHIFT >> 1)) << 8));

    tcg_out_ld32_12(s, COND_AL, TCG_REG_R1, TCG_REG_R0,

                    offsetof(CPUState, tlb_table[0][0].addr_write));

    tcg_out_dat_reg(s, COND_AL, ARITH_CMP, 0, TCG_REG_R1,

                    TCG_REG_R8, SHIFT_IMM_LSL(TARGET_PAGE_BITS));

    /* Check alignment.  */

    if (s_bits)

        tcg_out_dat_imm(s, COND_EQ, ARITH_TST,

                        0, addr_reg, (1 << s_bits) - 1);

#  if TARGET_LONG_BITS == 64

    /* XXX: possibly we could use a block data load or writeback in

     * the first access.  */

    tcg_out_ld32_12(s, COND_EQ, TCG_REG_R1, TCG_REG_R0,

                    offsetof(CPUState, tlb_table[0][0].addr_write) + 4);

    tcg_out_dat_reg(s, COND_EQ, ARITH_CMP, 0,

                    TCG_REG_R1, addr_reg2, SHIFT_IMM_LSL(0));

#  endif

    tcg_out_ld32_12(s, COND_EQ, TCG_REG_R1, TCG_REG_R0,

                    offsetof(CPUState, tlb_table[0][0].addend));

    switch (opc) {

    case 0:

        tcg_out_st8_r(s, COND_EQ, data_reg, addr_reg, TCG_REG_R1);

        break;

    case 1:

        if (bswap) {

            tcg_out_bswap16(s, COND_EQ, TCG_REG_R0, data_reg);

            tcg_out_st16_r(s, COND_EQ, TCG_REG_R0, addr_reg, TCG_REG_R1);

        } else {

            tcg_out_st16_r(s, COND_EQ, data_reg, addr_reg, TCG_REG_R1);

        }

        break;

    case 2:

    default:

        if (bswap) {

            tcg_out_bswap32(s, COND_EQ, TCG_REG_R0, data_reg);

            tcg_out_st32_r(s, COND_EQ, TCG_REG_R0, addr_reg, TCG_REG_R1);

        } else {

            tcg_out_st32_r(s, COND_EQ, data_reg, addr_reg, TCG_REG_R1);

        }

        break;

    case 3:

        if (bswap) {

            tcg_out_bswap32(s, COND_EQ, TCG_REG_R0, data_reg2);

            tcg_out_st32_rwb(s, COND_EQ, TCG_REG_R0, TCG_REG_R1, addr_reg);

            tcg_out_bswap32(s, COND_EQ, TCG_REG_R0, data_reg);

            tcg_out_st32_12(s, COND_EQ, data_reg, TCG_REG_R1, 4);

        } else {

            tcg_out_st32_rwb(s, COND_EQ, data_reg, TCG_REG_R1, addr_reg);

            tcg_out_st32_12(s, COND_EQ, data_reg2, TCG_REG_R1, 4);

        }

        break;

    }

    label_ptr = (void *) s->code_ptr;

    tcg_out_b(s, COND_EQ, 8);

    /* TODO: move this code to where the constants pool will be */

    if (addr_reg != TCG_REG_R0) {

        tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                        TCG_REG_R0, 0, addr_reg, SHIFT_IMM_LSL(0));

    }

# if TARGET_LONG_BITS == 32

    switch (opc) {

    case 0:

        tcg_out_ext8u(s, COND_AL, TCG_REG_R1, data_reg);

        tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R2, 0, mem_index);

        break;

    case 1:

        tcg_out_ext16u(s, COND_AL, TCG_REG_R1, data_reg);

        tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R2, 0, mem_index);

        break;

    case 2:

        if (data_reg != TCG_REG_R1) {

            tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                            TCG_REG_R1, 0, data_reg, SHIFT_IMM_LSL(0));

        }

        tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R2, 0, mem_index);

        break;

    case 3:

        if (data_reg != TCG_REG_R1) {

            tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                            TCG_REG_R1, 0, data_reg, SHIFT_IMM_LSL(0));

        }

        if (data_reg2 != TCG_REG_R2) {

            tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                            TCG_REG_R2, 0, data_reg2, SHIFT_IMM_LSL(0));

        }

        tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R3, 0, mem_index);

        break;

    }

# else

    if (addr_reg2 != TCG_REG_R1) {

        tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                        TCG_REG_R1, 0, addr_reg2, SHIFT_IMM_LSL(0));

    }

    switch (opc) {

    case 0:

        tcg_out_ext8u(s, COND_AL, TCG_REG_R2, data_reg);

        tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R3, 0, mem_index);

        break;

    case 1:

        tcg_out_ext16u(s, COND_AL, TCG_REG_R2, data_reg);

        tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R3, 0, mem_index);

        break;

    case 2:

        if (data_reg != TCG_REG_R2) {

            tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                            TCG_REG_R2, 0, data_reg, SHIFT_IMM_LSL(0));

        }

        tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R3, 0, mem_index);

        break;

    case 3:

        tcg_out_dat_imm(s, COND_AL, ARITH_MOV, TCG_REG_R8, 0, mem_index);

        tcg_out32(s, (COND_AL << 28) | 0x052d8010); /* str r8, [sp, #-0x10]! */

        if (data_reg != TCG_REG_R2) {

            tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                            TCG_REG_R2, 0, data_reg, SHIFT_IMM_LSL(0));

        }

        if (data_reg2 != TCG_REG_R3) {

            tcg_out_dat_reg(s, COND_AL, ARITH_MOV,

                            TCG_REG_R3, 0, data_reg2, SHIFT_IMM_LSL(0));

        }

        break;

    }

# endif

    tcg_out_bl(s, COND_AL, (tcg_target_long) qemu_st_helpers[s_bits] -

                    (tcg_target_long) s->code_ptr);

# if TARGET_LONG_BITS == 64

    if (opc == 3)

        tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_R13, TCG_REG_R13, 0x10);

# endif

    *label_ptr += ((void *) s->code_ptr - (void *) label_ptr - 8) >> 2;

#else /* !CONFIG_SOFTMMU */

    if (GUEST_BASE) {

        uint32_t offset = GUEST_BASE;

        int i;

        int rot;

        while (offset) {

            i = ctz32(offset) & ~1;

            rot = ((32 - i) << 7) & 0xf00;

            tcg_out_dat_imm(s, COND_AL, ARITH_ADD, TCG_REG_R1, addr_reg,

                            ((offset >> i) & 0xff) | rot);

            addr_reg = TCG_REG_R1;

            offset &= ~(0xff << i);

        }

    }

    switch (opc) {

    case 0:

        tcg_out_st8_12(s, COND_AL, data_reg, addr_reg, 0);

        break;

    case 1:

        if (bswap) {

            tcg_out_bswap16(s, COND_AL, TCG_REG_R0, data_reg);

            tcg_out_st16_8(s, COND_AL, TCG_REG_R0, addr_reg, 0);

        } else {

            tcg_out_st16_8(s, COND_AL, data_reg, addr_reg, 0);

        }

        break;

    case 2:

    default:

        if (bswap) {

            tcg_out_bswap32(s, COND_AL, TCG_REG_R0, data_reg);

            tcg_out_st32_12(s, COND_AL, TCG_REG_R0, addr_reg, 0);

        } else {

            tcg_out_st32_12(s, COND_AL, data_reg, addr_reg, 0);

        }

        break;

    case 3:

        /* TODO: use block store -

         * check that data_reg2 > data_reg or the other way */

        if (bswap) {

            tcg_out_bswap32(s, COND_AL, TCG_REG_R0, data_reg2);

            tcg_out_st32_12(s, COND_AL, TCG_REG_R0, addr_reg, 0);

            tcg_out_bswap32(s, COND_AL, TCG_REG_R0, data_reg);

            tcg_out_st32_12(s, COND_AL, TCG_REG_R0, addr_reg, 4);

        } else {

            tcg_out_st32_12(s, COND_AL, data_reg, addr_reg, 0);

            tcg_out_st32_12(s, COND_AL, data_reg2, addr_reg, 4);

        }

        break;

    }

#endif

}


static av_cold int indeo3_decode_init(AVCodecContext *avctx)

{

    Indeo3DecodeContext *s = avctx->priv_data;

    s->avctx = avctx;

    s->width = avctx->width;

    s->height = avctx->height;

    avctx->pix_fmt = PIX_FMT_YUV410P;

    build_modpred(s);

    iv_alloc_frames(s);

    return 0;

}


static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr)

{

    return 0;

}


static void coroutine_fn mirror_pause(BlockJob *job)

{

    MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);

    mirror_drain(s);

}


static Coroutine *coroutine_new(void)

{

    const size_t stack_size = 1 << 20;

    CoroutineUContext *co;

    CoroutineThreadState *coTS;

    struct sigaction sa;

    struct sigaction osa;

    struct sigaltstack ss;

    struct sigaltstack oss;

    sigset_t sigs;

    sigset_t osigs;

    jmp_buf old_env;

    /* The way to manipulate stack is with the sigaltstack function. We

     * prepare a stack, with it delivering a signal to ourselves and then

     * put setjmp/longjmp where needed.

     * This has been done keeping coroutine-ucontext as a model and with the

     * pth ideas (GNU Portable Threads). See coroutine-ucontext for the basics

     * of the coroutines and see pth_mctx.c (from the pth project) for the

     * sigaltstack way of manipulating stacks.

     */

    co = g_malloc0(sizeof(*co));

    co->stack = g_malloc(stack_size);

    co->base.entry_arg = &old_env; /* stash away our jmp_buf */

    coTS = coroutine_get_thread_state();

    coTS->tr_handler = co;

    /*

     * Preserve the SIGUSR2 signal state, block SIGUSR2,

     * and establish our signal handler. The signal will

     * later transfer control onto the signal stack.

     */

    sigemptyset(&sigs);

    sigaddset(&sigs, SIGUSR2);

    pthread_sigmask(SIG_BLOCK, &sigs, &osigs);

    sa.sa_handler = coroutine_trampoline;

    sigfillset(&sa.sa_mask);

    sa.sa_flags = SA_ONSTACK;

    if (sigaction(SIGUSR2, &sa, &osa) != 0) {

        abort();

    }

    /*

     * Set the new stack.

     */

    ss.ss_sp = co->stack;

    ss.ss_size = stack_size;

    ss.ss_flags = 0;

    if (sigaltstack(&ss, &oss) < 0) {

        abort();

    }

    /*

     * Now transfer control onto the signal stack and set it up.

     * It will return immediately via "return" after the setjmp()

     * was performed. Be careful here with race conditions.  The

     * signal can be delivered the first time sigsuspend() is

     * called.

     */

    coTS->tr_called = 0;

    kill(getpid(), SIGUSR2);

    sigfillset(&sigs);

    sigdelset(&sigs, SIGUSR2);

    while (!coTS->tr_called) {

        sigsuspend(&sigs);

    }

    /*

     * Inform the system that we are back off the signal stack by

     * removing the alternative signal stack. Be careful here: It

     * first has to be disabled, before it can be removed.

     */

    sigaltstack(NULL, &ss);

    ss.ss_flags = SS_DISABLE;

    if (sigaltstack(&ss, NULL) < 0) {

        abort();

    }

    sigaltstack(NULL, &ss);

    if (!(oss.ss_flags & SS_DISABLE)) {

        sigaltstack(&oss, NULL);

    }

    /*

     * Restore the old SIGUSR2 signal handler and mask

     */

    sigaction(SIGUSR2, &osa, NULL);

    pthread_sigmask(SIG_SETMASK, &osigs, NULL);

    /*

     * Now enter the trampoline again, but this time not as a signal

     * handler. Instead we jump into it directly. The functionally

     * redundant ping-pong pointer arithmentic is neccessary to avoid

     * type-conversion warnings related to the `volatile' qualifier and

     * the fact that `jmp_buf' usually is an array type.

     */

    if (!setjmp(old_env)) {

        longjmp(coTS->tr_reenter, 1);

    }

    /*

     * Ok, we returned again, so now we're finished

     */

    return &co->base;

}


static AddressParts gen_lea_modrm_0(CPUX86State *env, DisasContext *s,

                                    int modrm)

{

    int def_seg, base, index, scale, mod, rm;

    target_long disp;

    bool havesib;

    def_seg = R_DS;

    index = -1;

    scale = 0;

    disp = 0;

    mod = (modrm >> 6) & 3;

    rm = modrm & 7;

    base = rm | REX_B(s);

    if (mod == 3) {

        /* Normally filtered out earlier, but including this path

           simplifies multi-byte nop, as well as bndcl, bndcu, bndcn.  */

        goto done;

    }

    switch (s->aflag) {

    case MO_64:

    case MO_32:

        havesib = 0;

        if (rm == 4) {

            int code = cpu_ldub_code(env, s->pc++);

            scale = (code >> 6) & 3;

            index = ((code >> 3) & 7) | REX_X(s);

            if (index == 4) {

                index = -1;  /* no index */

            }

            base = (code & 7) | REX_B(s);

            havesib = 1;

        }

        switch (mod) {

        case 0:

            if ((base & 7) == 5) {

                base = -1;

                disp = (int32_t)cpu_ldl_code(env, s->pc);

                s->pc += 4;

                if (CODE64(s) && !havesib) {

                    base = -2;

                    disp += s->pc + s->rip_offset;

                }

            }

            break;

        case 1:

            disp = (int8_t)cpu_ldub_code(env, s->pc++);

            break;

        default:

        case 2:

            disp = (int32_t)cpu_ldl_code(env, s->pc);

            s->pc += 4;

            break;

        }

        /* For correct popl handling with esp.  */

        if (base == R_ESP && s->popl_esp_hack) {

            disp += s->popl_esp_hack;

        }

        if (base == R_EBP || base == R_ESP) {

            def_seg = R_SS;

        }

        break;

    case MO_16:

        if (mod == 0) {

            if (rm == 6) {

                base = -1;

                disp = cpu_lduw_code(env, s->pc);

                s->pc += 2;

                break;

            }

        } else if (mod == 1) {

            disp = (int8_t)cpu_ldub_code(env, s->pc++);

        } else {

            disp = (int16_t)cpu_lduw_code(env, s->pc);

            s->pc += 2;

        }

        switch (rm) {

        case 0:

            base = R_EBX;

            index = R_ESI;

            break;

        case 1:

            base = R_EBX;

            index = R_EDI;

            break;

        case 2:

            base = R_EBP;

            index = R_ESI;

            def_seg = R_SS;

            break;

        case 3:

            base = R_EBP;

            index = R_EDI;

            def_seg = R_SS;

            break;

        case 4:

            base = R_ESI;

            break;

        case 5:

            base = R_EDI;

            break;

        case 6:

            base = R_EBP;

            def_seg = R_SS;

            break;

        default:

        case 7:

            base = R_EBX;

            break;

        }

        break;

    default:

        tcg_abort();

    }

 done:

    return (AddressParts){ def_seg, base, index, scale, disp };

}


static int omap_validate_local_addr(struct omap_mpu_state_s *s,

                target_phys_addr_t addr)

{

    return addr >= OMAP_LOCALBUS_BASE && addr < OMAP_LOCALBUS_BASE + 0x1000000;

}


int net_init_bridge(const NetClientOptions *opts, const char *name,

                    NetClientState *peer, Error **errp)

{

    const NetdevBridgeOptions *bridge;

    const char *helper, *br;

    TAPState *s;

    int fd, vnet_hdr;

    assert(opts->type == NET_CLIENT_OPTIONS_KIND_BRIDGE);

    bridge = opts->u.bridge;

    helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;

    br     = bridge->has_br     ? bridge->br     : DEFAULT_BRIDGE_INTERFACE;

    fd = net_bridge_run_helper(helper, br, errp);

    if (fd == -1) {

        return -1;

    }

    fcntl(fd, F_SETFL, O_NONBLOCK);

    vnet_hdr = tap_probe_vnet_hdr(fd);

    s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);

    snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,

             br);

    return 0;

}


static inline void t_gen_raise_exception(uint32_t index)

{

	tcg_gen_helper_0_1(helper_raise_exception, tcg_const_tl(index));

}


uint64_t ram_bytes_total(void)

{

    return last_ram_offset;

}


void ff_g729_postfilter(DSPContext *dsp, int16_t* ht_prev_data, int* voicing,

                     const int16_t *lp_filter_coeffs, int pitch_delay_int,

                     int16_t* residual, int16_t* res_filter_data,

                     int16_t* pos_filter_data, int16_t *speech, int subframe_size)

{

    int16_t residual_filt_buf[SUBFRAME_SIZE+10];

    int16_t lp_gn[33]; // (3.12)

    int16_t lp_gd[11]; // (3.12)

    int tilt_comp_coeff;

    int i;

    /* Zero-filling is necessary for tilt-compensation filter. */

    memset(lp_gn, 0, 33 * sizeof(int16_t));

    /* Calculate A(z/FORMANT_PP_FACTOR_NUM) filter coefficients. */

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

        lp_gn[i + 11] = (lp_filter_coeffs[i + 1] * formant_pp_factor_num_pow[i] + 0x4000) >> 15;

    /* Calculate A(z/FORMANT_PP_FACTOR_DEN) filter coefficients. */

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

        lp_gd[i + 1] = (lp_filter_coeffs[i + 1] * formant_pp_factor_den_pow[i] + 0x4000) >> 15;

    /* residual signal calculation (one-half of short-term postfilter) */

    memcpy(speech - 10, res_filter_data, 10 * sizeof(int16_t));

    residual_filter(residual + RES_PREV_DATA_SIZE, lp_gn + 11, speech, subframe_size);

    /* Save data to use it in the next subframe. */

    memcpy(res_filter_data, speech + subframe_size - 10, 10 * sizeof(int16_t));

    /* long-term filter. If long-term prediction gain is larger than 3dB (returned value is

       nonzero) then declare current subframe as periodic. */

    *voicing = FFMAX(*voicing, long_term_filter(dsp, pitch_delay_int,

                                                residual, residual_filt_buf + 10,

                                                subframe_size));

    /* shift residual for using in next subframe */

    memmove(residual, residual + subframe_size, RES_PREV_DATA_SIZE * sizeof(int16_t));

    /* short-term filter tilt compensation */

    tilt_comp_coeff = get_tilt_comp(dsp, lp_gn, lp_gd, residual_filt_buf + 10, subframe_size);

    /* Apply second half of short-term postfilter: 1/A(z/FORMANT_PP_FACTOR_DEN) */

    ff_celp_lp_synthesis_filter(pos_filter_data + 10, lp_gd + 1,

                                residual_filt_buf + 10,

                                subframe_size, 10, 0, 0, 0x800);

    memcpy(pos_filter_data, pos_filter_data + subframe_size, 10 * sizeof(int16_t));

    *ht_prev_data = apply_tilt_comp(speech, pos_filter_data + 10, tilt_comp_coeff,

                                    subframe_size, *ht_prev_data);

}


static int asink_query_formats(AVFilterContext *ctx)

{

    BufferSinkContext *buf = ctx->priv;

    AVFilterFormats *formats = NULL;

    AVFilterChannelLayouts *layouts = NULL;

    unsigned i;

    int ret;

    if (buf->sample_fmts_size     % sizeof(*buf->sample_fmts)     ||

        buf->sample_rates_size    % sizeof(*buf->sample_rates)    ||

        buf->channel_layouts_size % sizeof(*buf->channel_layouts) ||

        buf->channel_counts_size  % sizeof(*buf->channel_counts)) {

        av_log(ctx, AV_LOG_ERROR, "Invalid size for format lists\n");

#define LOG_ERROR(field) \

        if (buf->field ## _size % sizeof(*buf->field)) \

            av_log(ctx, AV_LOG_ERROR, "  " #field " is %d, should be " \

                   "multiple of %d\n", \

                   buf->field ## _size, (int)sizeof(*buf->field));

        LOG_ERROR(sample_fmts);

        LOG_ERROR(sample_rates);

        LOG_ERROR(channel_layouts);

        LOG_ERROR(channel_counts);

#undef LOG_ERROR

        return AVERROR(EINVAL);

    }

    if (buf->sample_fmts_size) {

        for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)

            if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0)

                return ret;

        ff_set_common_formats(ctx, formats);

    }

    if (buf->channel_layouts_size || buf->channel_counts_size ||

        buf->all_channel_counts) {

        for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)

            if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0)

                return ret;

        for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)

            if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0)

                return ret;

        if (buf->all_channel_counts) {

            if (layouts)

                av_log(ctx, AV_LOG_WARNING,

                       "Conflicting all_channel_counts and list in options\n");

            else if (!(layouts = ff_all_channel_counts()))

                return AVERROR(ENOMEM);

        }

        ff_set_common_channel_layouts(ctx, layouts);

    }

    if (buf->sample_rates_size) {

        formats = NULL;

        for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)

            if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0)

                return ret;

        ff_set_common_samplerates(ctx, formats);

    }

    return 0;

}


void avcodec_set_dimensions(AVCodecContext *s, int width, int height){

    s->coded_width = width;

    s->coded_height= height;

    s->width  = width;

    s->height = height;

}


int qemu_acl_remove(qemu_acl *acl,

                    const char *match)

{

    qemu_acl_entry *entry;

    int i = 0;

    QTAILQ_FOREACH(entry, &acl->entries, next) {

        i++;

        if (strcmp(entry->match, match) == 0) {

            QTAILQ_REMOVE(&acl->entries, entry, next);

            return i;

        }

    }

    return -1;

}


static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size)

{

    SDHCIState *s = (SDHCIState *)opaque;

    uint32_t ret = 0;

    switch (offset & ~0x3) {

    case SDHC_SYSAD:

        ret = s->sdmasysad;

        break;

    case SDHC_BLKSIZE:

        ret = s->blksize | (s->blkcnt << 16);

        break;

    case SDHC_ARGUMENT:

        ret = s->argument;

        break;

    case SDHC_TRNMOD:

        ret = s->trnmod | (s->cmdreg << 16);

        break;

    case SDHC_RSPREG0 ... SDHC_RSPREG3:

        ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2];

        break;

    case  SDHC_BDATA:

        if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {

            ret = sdhci_read_dataport(s, size);

            DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, (int)offset,

                      ret, ret);

            return ret;

        }

        break;

    case SDHC_PRNSTS:

        ret = s->prnsts;

        break;

    case SDHC_HOSTCTL:

        ret = s->hostctl | (s->pwrcon << 8) | (s->blkgap << 16) |

              (s->wakcon << 24);

        break;

    case SDHC_CLKCON:

        ret = s->clkcon | (s->timeoutcon << 16);

        break;

    case SDHC_NORINTSTS:

        ret = s->norintsts | (s->errintsts << 16);

        break;

    case SDHC_NORINTSTSEN:

        ret = s->norintstsen | (s->errintstsen << 16);

        break;

    case SDHC_NORINTSIGEN:

        ret = s->norintsigen | (s->errintsigen << 16);

        break;

    case SDHC_ACMD12ERRSTS:

        ret = s->acmd12errsts;

        break;

    case SDHC_CAPAREG:

        ret = s->capareg;

        break;

    case SDHC_MAXCURR:

        ret = s->maxcurr;

        break;

    case SDHC_ADMAERR:

        ret =  s->admaerr;

        break;

    case SDHC_ADMASYSADDR:

        ret = (uint32_t)s->admasysaddr;

        break;

    case SDHC_ADMASYSADDR + 4:

        ret = (uint32_t)(s->admasysaddr >> 32);

        break;

    case SDHC_SLOT_INT_STATUS:

        ret = (SD_HOST_SPECv2_VERS << 16) | sdhci_slotint(s);

        break;

    default:

        qemu_log_mask(LOG_UNIMP, "SDHC rd_%ub @0x%02" HWADDR_PRIx " "

                      "not implemented\n", size, offset);

        break;

    }

    ret >>= (offset & 0x3) * 8;

    ret &= (1ULL << (size * 8)) - 1;

    DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, (int)offset, ret, ret);

    return ret;

}


static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){

    Plane *p= &s->plane[plane_index];

    const int mb_w= s->b_width  << s->block_max_depth;

    const int mb_h= s->b_height << s->block_max_depth;

    int x, y, mb_x;

    int block_size = MB_SIZE >> s->block_max_depth;

    int block_w    = plane_index ? block_size>>s->chroma_h_shift : block_size;

    int block_h    = plane_index ? block_size>>s->chroma_v_shift : block_size;

    const uint8_t *obmc  = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];

    int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;

    int ref_stride= s->current_picture->linesize[plane_index];

    uint8_t *dst8= s->current_picture->data[plane_index];

    int w= p->width;

    int h= p->height;

    if(s->keyframe || (s->avctx->debug&512)){

        if(mb_y==mb_h)

            return;

        if(add){

            for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){

//                DWTELEM * line = slice_buffer_get_line(sb, y);

                IDWTELEM * line = sb->line[y];

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

//                    int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));

                    int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));

                    v >>= FRAC_BITS;

                    if(v&(~255)) v= ~(v>>31);

                    dst8[x + y*ref_stride]= v;

                }

            }

        }else{

            for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){

//                DWTELEM * line = slice_buffer_get_line(sb, y);

                IDWTELEM * line = sb->line[y];

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

                    line[x] -= 128 << FRAC_BITS;

//                    buf[x + y*w]-= 128<<FRAC_BITS;

                }

            }

        }

        return;

    }

    for(mb_x=0; mb_x<=mb_w; mb_x++){

        add_yblock(s, 1, sb, old_buffer, dst8, obmc,

                   block_w*mb_x - block_w/2,

                   block_h*mb_y - block_h/2,

                   block_w, block_h,

                   w, h,

                   w, ref_stride, obmc_stride,

                   mb_x - 1, mb_y - 1,

                   add, 0, plane_index);

    }

    if(s->avmv && mb_y < mb_h && plane_index == 0)

        for(mb_x=0; mb_x<mb_w; mb_x++){

            AVMotionVector *avmv = s->avmv + (s->avmv_index++);

            const int b_width = s->b_width  << s->block_max_depth;

            const int b_stride= b_width;

            BlockNode *bn= &s->block[mb_x + mb_y*b_stride];

            if (bn->type)

                continue;

            avmv->w = block_w;

            avmv->h = block_h;

            avmv->dst_x = block_w*mb_x - block_w/2;

            avmv->dst_y = block_h*mb_y - block_h/2;

            avmv->src_x = avmv->dst_x + (bn->mx * s->mv_scale)/8;

            avmv->src_y = avmv->dst_y + (bn->my * s->mv_scale)/8;

            avmv->source= -1 - bn->ref;

            avmv->flags = 0;

        }

}


static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,

                                   struct elfhdr *exec,

                                   struct image_info *info,

                                   struct image_info *interp_info)

{

    abi_ulong sp;

    abi_ulong u_argc, u_argv, u_envp, u_auxv;

    int size;

    int i;

    abi_ulong u_rand_bytes;

    uint8_t k_rand_bytes[16];

    abi_ulong u_platform;

    const char *k_platform;

    const int n = sizeof(elf_addr_t);

    sp = p;

#ifdef CONFIG_USE_FDPIC

    /* Needs to be before we load the env/argc/... */

    if (elf_is_fdpic(exec)) {

        /* Need 4 byte alignment for these structs */

        sp &= ~3;

        sp = loader_build_fdpic_loadmap(info, sp);

        info->other_info = interp_info;

        if (interp_info) {

            interp_info->other_info = info;

            sp = loader_build_fdpic_loadmap(interp_info, sp);

        }

    }

#endif

    u_platform = 0;

    k_platform = ELF_PLATFORM;

    if (k_platform) {

        size_t len = strlen(k_platform) + 1;

        if (STACK_GROWS_DOWN) {

            sp -= (len + n - 1) & ~(n - 1);

            u_platform = sp;

            /* FIXME - check return value of memcpy_to_target() for failure */

            memcpy_to_target(sp, k_platform, len);

        } else {

            memcpy_to_target(sp, k_platform, len);

            u_platform = sp;

            sp += len + 1;

        }

    }

    /* Provide 16 byte alignment for the PRNG, and basic alignment for

     * the argv and envp pointers.

     */

    if (STACK_GROWS_DOWN) {

        sp = QEMU_ALIGN_DOWN(sp, 16);

    } else {

        sp = QEMU_ALIGN_UP(sp, 16);

    }

    /*

     * Generate 16 random bytes for userspace PRNG seeding (not

     * cryptically secure but it's not the aim of QEMU).

     */

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

        k_rand_bytes[i] = rand();

    }

    if (STACK_GROWS_DOWN) {

        sp -= 16;

        u_rand_bytes = sp;

        /* FIXME - check return value of memcpy_to_target() for failure */

        memcpy_to_target(sp, k_rand_bytes, 16);

    } else {

        memcpy_to_target(sp, k_rand_bytes, 16);

        u_rand_bytes = sp;

        sp += 16;

    }

    size = (DLINFO_ITEMS + 1) * 2;

    if (k_platform)

        size += 2;

#ifdef DLINFO_ARCH_ITEMS

    size += DLINFO_ARCH_ITEMS * 2;

#endif

#ifdef ELF_HWCAP2

    size += 2;

#endif

    size += envc + argc + 2;

    size += 1;  /* argc itself */

    size *= n;

    /* Allocate space and finalize stack alignment for entry now.  */

    if (STACK_GROWS_DOWN) {

        u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);

        sp = u_argc;

    } else {

        u_argc = sp;

        sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);

    }

    u_argv = u_argc + n;

    u_envp = u_argv + (argc + 1) * n;

    u_auxv = u_envp + (envc + 1) * n;

    info->saved_auxv = u_auxv;

    info->arg_start = u_argv;

    info->arg_end = u_argv + argc * n;

    /* This is correct because Linux defines

     * elf_addr_t as Elf32_Off / Elf64_Off

     */

#define NEW_AUX_ENT(id, val) do {               \

        put_user_ual(id, u_auxv);  u_auxv += n; \

        put_user_ual(val, u_auxv); u_auxv += n; \

    } while(0)

    /* There must be exactly DLINFO_ITEMS entries here.  */

#ifdef ARCH_DLINFO

    /*

     * ARCH_DLINFO must come first so platform specific code can enforce

     * special alignment requirements on the AUXV if necessary (eg. PPC).

     */

    ARCH_DLINFO;

#endif

    NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));

    NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));

    NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));

    NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE, getpagesize())));

    NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));

    NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);

    NEW_AUX_ENT(AT_ENTRY, info->entry);

    NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());

    NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());

    NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());

    NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());

    NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);

    NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));

    NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);

#ifdef ELF_HWCAP2

    NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);

#endif

    if (u_platform) {

        NEW_AUX_ENT(AT_PLATFORM, u_platform);

    }

    NEW_AUX_ENT (AT_NULL, 0);

#undef NEW_AUX_ENT

    info->auxv_len = u_argv - info->saved_auxv;

    put_user_ual(argc, u_argc);

    p = info->arg_strings;

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

        put_user_ual(p, u_argv);

        u_argv += n;

        p += target_strlen(p) + 1;

    }

    put_user_ual(0, u_argv);

    p = info->env_strings;

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

        put_user_ual(p, u_envp);

        u_envp += n;

        p += target_strlen(p) + 1;

    }

    put_user_ual(0, u_envp);

    return sp;

}


void cpu_reset(CPUM68KState *env)
{
    memset(env, 0, offsetof(CPUM68KState, breakpoints));
#if !defined (CONFIG_USER_ONLY)
    env->sr = 0x2700;
#endif
    m68k_switch_sp(env);
    /* ??? FP regs should be initialized to NaN.  */
    env->cc_op = CC_OP_FLAGS;
    /* TODO: We should set PC from the interrupt vector.  */
    env->pc = 0;
    tlb_flush(env, 1);


static int serial_parse(const char *devname)

{

    static int index = 0;

    char label[32];

    if (strcmp(devname, "none") == 0)

        return 0;

    if (index == MAX_SERIAL_PORTS) {

        fprintf(stderr, "qemu: too many serial ports\n");

        exit(1);

    }

    snprintf(label, sizeof(label), "serial%d", index);

    serial_hds[index] = qemu_chr_new(label, devname, NULL);

    if (!serial_hds[index]) {

        fprintf(stderr, "qemu: could not connect serial device"

                " to character backend '%s'\n", devname);

        return -1;

    }

    index++;

    return 0;

}


void do_info_migrate(Monitor *mon, QObject **ret_data)

{

    QDict *qdict;

    MigrationState *s = current_migration;

    if (s) {

        switch (s->get_status(s)) {

        case MIG_STATE_ACTIVE:

            qdict = qdict_new();

            qdict_put(qdict, "status", qstring_from_str("active"));

            migrate_put_status(qdict, "ram", ram_bytes_transferred(),

                               ram_bytes_remaining(), ram_bytes_total());

            if (blk_mig_active()) {

                migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),

                                   blk_mig_bytes_remaining(),

                                   blk_mig_bytes_total());

            }

            *ret_data = QOBJECT(qdict);

            break;

        case MIG_STATE_COMPLETED:

            *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");

            break;

        case MIG_STATE_ERROR:

            *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");

            break;

        case MIG_STATE_CANCELLED:

            *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");

            break;

        }

        assert(*ret_data != NULL);

    }

}


static abi_long do_connect(int sockfd, abi_ulong target_addr,

                           socklen_t addrlen)

{

    void *addr;

    if (addrlen < 0)

        return -TARGET_EINVAL;

    addr = alloca(addrlen);

    target_to_host_sockaddr(addr, target_addr, addrlen);

    return get_errno(connect(sockfd, addr, addrlen));

}


static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,

                                        Error **errp)

{

    QmpInputVisitor *qiv = to_qiv(v);

    GenericList *entry;

    StackObject *so = &qiv->stack[qiv->nb_stack - 1];

    if (so->entry == NULL) {

        return NULL;

    }

    entry = g_malloc0(sizeof(*entry));

    if (*list) {

        so->entry = qlist_next(so->entry);

        if (so->entry == NULL) {

            g_free(entry);

            return NULL;

        }

        (*list)->next = entry;

    }

    return entry;

}


int gdbserver_start(const char *port)

{

    GDBState *s;

    char gdbstub_port_name[128];

    int port_num;

    char *p;

    CharDriverState *chr;

    if (!port || !*port)

      return -1;

    port_num = strtol(port, &p, 10);

    if (*p == 0) {

        /* A numeric value is interpreted as a port number.  */

        snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),

                 "tcp::%d,nowait,nodelay,server", port_num);

        port = gdbstub_port_name;

    }

    chr = qemu_chr_open("gdb", port);

    if (!chr)

        return -1;

    s = qemu_mallocz(sizeof(GDBState));

    if (!s) {

        return -1;

    }

    s->env = first_cpu; /* XXX: allow to change CPU */

    s->chr = chr;

    qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,

                          gdb_chr_event, s);

    qemu_add_vm_stop_handler(gdb_vm_stopped, s);

    return 0;

}


static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x,

                             float z[320], float W[2][32][32][2])

{

    int i, k;

    memcpy(W[0], W[1], sizeof(W[0]));

    memcpy(x    , x+1024, (320-32)*sizeof(x[0]));

    memcpy(x+288, in,         1024*sizeof(x[0]));

    for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames

                               // are not supported

        dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);

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

            float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256];

            z[k] = f;

        }

        //Shuffle to IMDCT

        z[64] = z[0];

        for (k = 1; k < 32; k++) {

            z[64+2*k-1] =  z[   k];

            z[64+2*k  ] = -z[64-k];

        }

        z[64+63] = z[32];

        mdct->imdct_half(mdct, z, z+64);

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

            W[1][i][k][0] = -z[63-k];

            W[1][i][k][1] = z[k];

        }

        x += 32;

    }

}


static void tcg_out_op(TCGContext *s, TCGOpcode opc,

                       const TCGArg args[TCG_MAX_OP_ARGS],

                       const int const_args[TCG_MAX_OP_ARGS])

{

    /* 99% of the time, we can signal the use of extension registers

       by looking to see if the opcode handles 64-bit data.  */

    TCGType ext = (tcg_op_defs[opc].flags & TCG_OPF_64BIT) != 0;

    /* Hoist the loads of the most common arguments.  */

    TCGArg a0 = args[0];

    TCGArg a1 = args[1];

    TCGArg a2 = args[2];

    int c2 = const_args[2];

    /* Some operands are defined with "rZ" constraint, a register or

       the zero register.  These need not actually test args[I] == 0.  */

#define REG0(I)  (const_args[I] ? TCG_REG_XZR : (TCGReg)args[I])

    switch (opc) {

    case INDEX_op_exit_tb:

        tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_X0, a0);

        tcg_out_goto(s, tb_ret_addr);

        break;

    case INDEX_op_goto_tb:

#ifndef USE_DIRECT_JUMP

#error "USE_DIRECT_JUMP required for aarch64"

#endif

        assert(s->tb_jmp_offset != NULL); /* consistency for USE_DIRECT_JUMP */

        s->tb_jmp_offset[a0] = tcg_current_code_size(s);

        /* actual branch destination will be patched by

           aarch64_tb_set_jmp_target later, beware retranslation. */

        tcg_out_goto_noaddr(s);

        s->tb_next_offset[a0] = tcg_current_code_size(s);

        break;

    case INDEX_op_br:

        tcg_out_goto_label(s, arg_label(a0));

        break;

    case INDEX_op_ld8u_i32:

    case INDEX_op_ld8u_i64:

        tcg_out_ldst(s, I3312_LDRB, a0, a1, a2);

        break;

    case INDEX_op_ld8s_i32:

        tcg_out_ldst(s, I3312_LDRSBW, a0, a1, a2);

        break;

    case INDEX_op_ld8s_i64:

        tcg_out_ldst(s, I3312_LDRSBX, a0, a1, a2);

        break;

    case INDEX_op_ld16u_i32:

    case INDEX_op_ld16u_i64:

        tcg_out_ldst(s, I3312_LDRH, a0, a1, a2);

        break;

    case INDEX_op_ld16s_i32:

        tcg_out_ldst(s, I3312_LDRSHW, a0, a1, a2);

        break;

    case INDEX_op_ld16s_i64:

        tcg_out_ldst(s, I3312_LDRSHX, a0, a1, a2);

        break;

    case INDEX_op_ld_i32:

    case INDEX_op_ld32u_i64:

        tcg_out_ldst(s, I3312_LDRW, a0, a1, a2);

        break;

    case INDEX_op_ld32s_i64:

        tcg_out_ldst(s, I3312_LDRSWX, a0, a1, a2);

        break;

    case INDEX_op_ld_i64:

        tcg_out_ldst(s, I3312_LDRX, a0, a1, a2);

        break;

    case INDEX_op_st8_i32:

    case INDEX_op_st8_i64:

        tcg_out_ldst(s, I3312_STRB, REG0(0), a1, a2);

        break;

    case INDEX_op_st16_i32:

    case INDEX_op_st16_i64:

        tcg_out_ldst(s, I3312_STRH, REG0(0), a1, a2);

        break;

    case INDEX_op_st_i32:

    case INDEX_op_st32_i64:

        tcg_out_ldst(s, I3312_STRW, REG0(0), a1, a2);

        break;

    case INDEX_op_st_i64:

        tcg_out_ldst(s, I3312_STRX, REG0(0), a1, a2);

        break;

    case INDEX_op_add_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_add_i64:

        if (c2) {

            tcg_out_addsubi(s, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3502, ADD, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_sub_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_sub_i64:

        if (c2) {

            tcg_out_addsubi(s, ext, a0, a1, -a2);

        } else {

            tcg_out_insn(s, 3502, SUB, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_neg_i64:

    case INDEX_op_neg_i32:

        tcg_out_insn(s, 3502, SUB, ext, a0, TCG_REG_XZR, a1);

        break;

    case INDEX_op_and_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_and_i64:

        if (c2) {

            tcg_out_logicali(s, I3404_ANDI, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3510, AND, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_andc_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_andc_i64:

        if (c2) {

            tcg_out_logicali(s, I3404_ANDI, ext, a0, a1, ~a2);

        } else {

            tcg_out_insn(s, 3510, BIC, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_or_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_or_i64:

        if (c2) {

            tcg_out_logicali(s, I3404_ORRI, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3510, ORR, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_orc_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_orc_i64:

        if (c2) {

            tcg_out_logicali(s, I3404_ORRI, ext, a0, a1, ~a2);

        } else {

            tcg_out_insn(s, 3510, ORN, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_xor_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_xor_i64:

        if (c2) {

            tcg_out_logicali(s, I3404_EORI, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3510, EOR, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_eqv_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_eqv_i64:

        if (c2) {

            tcg_out_logicali(s, I3404_EORI, ext, a0, a1, ~a2);

        } else {

            tcg_out_insn(s, 3510, EON, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_not_i64:

    case INDEX_op_not_i32:

        tcg_out_insn(s, 3510, ORN, ext, a0, TCG_REG_XZR, a1);

        break;

    case INDEX_op_mul_i64:

    case INDEX_op_mul_i32:

        tcg_out_insn(s, 3509, MADD, ext, a0, a1, a2, TCG_REG_XZR);

        break;

    case INDEX_op_div_i64:

    case INDEX_op_div_i32:

        tcg_out_insn(s, 3508, SDIV, ext, a0, a1, a2);

        break;

    case INDEX_op_divu_i64:

    case INDEX_op_divu_i32:

        tcg_out_insn(s, 3508, UDIV, ext, a0, a1, a2);

        break;

    case INDEX_op_rem_i64:

    case INDEX_op_rem_i32:

        tcg_out_insn(s, 3508, SDIV, ext, TCG_REG_TMP, a1, a2);

        tcg_out_insn(s, 3509, MSUB, ext, a0, TCG_REG_TMP, a2, a1);

        break;

    case INDEX_op_remu_i64:

    case INDEX_op_remu_i32:

        tcg_out_insn(s, 3508, UDIV, ext, TCG_REG_TMP, a1, a2);

        tcg_out_insn(s, 3509, MSUB, ext, a0, TCG_REG_TMP, a2, a1);

        break;

    case INDEX_op_shl_i64:

    case INDEX_op_shl_i32:

        if (c2) {

            tcg_out_shl(s, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3508, LSLV, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_shr_i64:

    case INDEX_op_shr_i32:

        if (c2) {

            tcg_out_shr(s, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3508, LSRV, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_sar_i64:

    case INDEX_op_sar_i32:

        if (c2) {

            tcg_out_sar(s, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3508, ASRV, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_rotr_i64:

    case INDEX_op_rotr_i32:

        if (c2) {

            tcg_out_rotr(s, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3508, RORV, ext, a0, a1, a2);

        }

        break;

    case INDEX_op_rotl_i64:

    case INDEX_op_rotl_i32:

        if (c2) {

            tcg_out_rotl(s, ext, a0, a1, a2);

        } else {

            tcg_out_insn(s, 3502, SUB, 0, TCG_REG_TMP, TCG_REG_XZR, a2);

            tcg_out_insn(s, 3508, RORV, ext, a0, a1, TCG_REG_TMP);

        }

        break;

    case INDEX_op_brcond_i32:

        a1 = (int32_t)a1;

        /* FALLTHRU */

    case INDEX_op_brcond_i64:

        tcg_out_brcond(s, ext, a2, a0, a1, const_args[1], arg_label(args[3]));

        break;

    case INDEX_op_setcond_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_setcond_i64:

        tcg_out_cmp(s, ext, a1, a2, c2);

        /* Use CSET alias of CSINC Wd, WZR, WZR, invert(cond).  */

        tcg_out_insn(s, 3506, CSINC, TCG_TYPE_I32, a0, TCG_REG_XZR,

                     TCG_REG_XZR, tcg_invert_cond(args[3]));

        break;

    case INDEX_op_movcond_i32:

        a2 = (int32_t)a2;

        /* FALLTHRU */

    case INDEX_op_movcond_i64:

        tcg_out_cmp(s, ext, a1, a2, c2);

        tcg_out_insn(s, 3506, CSEL, ext, a0, REG0(3), REG0(4), args[5]);

        break;

    case INDEX_op_qemu_ld_i32:

    case INDEX_op_qemu_ld_i64:

        tcg_out_qemu_ld(s, a0, a1, a2, ext);

        break;

    case INDEX_op_qemu_st_i32:

    case INDEX_op_qemu_st_i64:

        tcg_out_qemu_st(s, REG0(0), a1, a2);

        break;

    case INDEX_op_bswap64_i64:

        tcg_out_rev64(s, a0, a1);

        break;

    case INDEX_op_bswap32_i64:

    case INDEX_op_bswap32_i32:

        tcg_out_rev32(s, a0, a1);

        break;

    case INDEX_op_bswap16_i64:

    case INDEX_op_bswap16_i32:

        tcg_out_rev16(s, a0, a1);

        break;

    case INDEX_op_ext8s_i64:

    case INDEX_op_ext8s_i32:

        tcg_out_sxt(s, ext, MO_8, a0, a1);

        break;

    case INDEX_op_ext16s_i64:

    case INDEX_op_ext16s_i32:

        tcg_out_sxt(s, ext, MO_16, a0, a1);

        break;

    case INDEX_op_ext_i32_i64:

    case INDEX_op_ext32s_i64:

        tcg_out_sxt(s, TCG_TYPE_I64, MO_32, a0, a1);

        break;

    case INDEX_op_ext8u_i64:

    case INDEX_op_ext8u_i32:

        tcg_out_uxt(s, MO_8, a0, a1);

        break;

    case INDEX_op_ext16u_i64:

    case INDEX_op_ext16u_i32:

        tcg_out_uxt(s, MO_16, a0, a1);

        break;

    case INDEX_op_extu_i32_i64:

    case INDEX_op_ext32u_i64:

        tcg_out_movr(s, TCG_TYPE_I32, a0, a1);

        break;

    case INDEX_op_deposit_i64:

    case INDEX_op_deposit_i32:

        tcg_out_dep(s, ext, a0, REG0(2), args[3], args[4]);

        break;

    case INDEX_op_add2_i32:

        tcg_out_addsub2(s, TCG_TYPE_I32, a0, a1, REG0(2), REG0(3),

                        (int32_t)args[4], args[5], const_args[4],

                        const_args[5], false);

        break;

    case INDEX_op_add2_i64:

        tcg_out_addsub2(s, TCG_TYPE_I64, a0, a1, REG0(2), REG0(3), args[4],

                        args[5], const_args[4], const_args[5], false);

        break;

    case INDEX_op_sub2_i32:

        tcg_out_addsub2(s, TCG_TYPE_I32, a0, a1, REG0(2), REG0(3),

                        (int32_t)args[4], args[5], const_args[4],

                        const_args[5], true);

        break;

    case INDEX_op_sub2_i64:

        tcg_out_addsub2(s, TCG_TYPE_I64, a0, a1, REG0(2), REG0(3), args[4],

                        args[5], const_args[4], const_args[5], true);

        break;

    case INDEX_op_muluh_i64:

        tcg_out_insn(s, 3508, UMULH, TCG_TYPE_I64, a0, a1, a2);

        break;

    case INDEX_op_mulsh_i64:

        tcg_out_insn(s, 3508, SMULH, TCG_TYPE_I64, a0, a1, a2);

        break;

    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */

    case INDEX_op_mov_i64:

    case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */

    case INDEX_op_movi_i64:

    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */

    default:

        tcg_abort();

    }

#undef REG0

}


void kvm_arch_reset_vcpu(CPUX86State *env)

{

    env->exception_injected = -1;

    env->interrupt_injected = -1;

    env->xcr0 = 1;

    if (kvm_irqchip_in_kernel()) {

        env->mp_state = cpu_is_bsp(env) ? KVM_MP_STATE_RUNNABLE :

                                          KVM_MP_STATE_UNINITIALIZED;

    } else {

        env->mp_state = KVM_MP_STATE_RUNNABLE;

    }

}


static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)

{

    uint32_t *buf32;

    uint32_t pix;

    int rshift, gshift, bshift;

    buf32 = (uint32_t *)buf;

    if ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) ==

        (vs->ds->surface->flags & QEMU_BIG_ENDIAN_FLAG)) {

        rshift = vs->clientds.pf.rshift;

        gshift = vs->clientds.pf.gshift;

        bshift = vs->clientds.pf.bshift;

    } else {

        rshift = 24 - vs->clientds.pf.rshift;

        gshift = 24 - vs->clientds.pf.gshift;

        bshift = 24 - vs->clientds.pf.bshift;

    }

    if (ret) {

        *ret = count * 3;

    }

    while (count--) {

        pix = *buf32++;

        *buf++ = (char)(pix >> rshift);

        *buf++ = (char)(pix >> gshift);

        *buf++ = (char)(pix >> bshift);

    }

}


static void slow_bar_writew(void *opaque, target_phys_addr_t addr, uint32_t val)

{

    AssignedDevRegion *d = opaque;

    uint16_t *out = (uint16_t *)(d->u.r_virtbase + addr);

    DEBUG("slow_bar_writew addr=0x" TARGET_FMT_plx " val=0x%04x\n", addr, val);

    *out = val;

}


static uint64_t gic_thiscpu_read(void *opaque, target_phys_addr_t addr,

                                 unsigned size)

{

    GICState *s = (GICState *)opaque;

    return gic_cpu_read(s, gic_get_current_cpu(s), addr);

}


int isa_vga_mm_init(hwaddr vram_base,

                    hwaddr ctrl_base, int it_shift,

                    MemoryRegion *address_space)

{

    ISAVGAMMState *s;

    s = g_malloc0(sizeof(*s));

    s->vga.vram_size_mb = VGA_RAM_SIZE >> 20;

    vga_common_init(&s->vga);

    vga_mm_init(s, vram_base, ctrl_base, it_shift, address_space);

    s->vga.con = graphic_console_init(s->vga.update, s->vga.invalidate,

                                      s->vga.screen_dump, s->vga.text_update,

                                      s);

    vga_init_vbe(&s->vga, address_space);

    return 0;

}


float64 helper_fitod(CPUSPARCState *env, int32_t src)

{

    /* No possible exceptions converting int to double.  */

    return int32_to_float64(src, &env->fp_status);

}


audio_get_output_timestamp(AVFormatContext *s1, int stream,

    int64_t *dts, int64_t *wall)

{

    AlsaData *s  = s1->priv_data;

    snd_pcm_sframes_t delay = 0;

    *wall = av_gettime();

    snd_pcm_delay(s->h, &delay);

    *dts = s1->streams[0]->cur_dts - delay;

}


static void do_vm_stop(RunState state)

{

    if (runstate_is_running()) {

        cpu_disable_ticks();

        pause_all_vcpus();

        runstate_set(state);

        vm_state_notify(0, state);

        qemu_aio_flush();

        bdrv_flush_all();

        monitor_protocol_event(QEVENT_STOP, NULL);

    }

}


static int vt82c686b_pm_initfn(PCIDevice *dev)

{

    VT686PMState *s = DO_UPCAST(VT686PMState, dev, dev);

    uint8_t *pci_conf;

    pci_conf = s->dev.config;

    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_VIA);

    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_VIA_ACPI);

    pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);

    pci_config_set_revision(pci_conf, 0x40);

    pci_set_word(pci_conf + PCI_COMMAND, 0);

    pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_FAST_BACK |

                 PCI_STATUS_DEVSEL_MEDIUM);

    /* 0x48-0x4B is Power Management I/O Base */

    pci_set_long(pci_conf + 0x48, 0x00000001);

    /* SMB ports:0xeee0~0xeeef */

    s->smb_io_base =((s->smb_io_base & 0xfff0) + 0x0);

    pci_conf[0x90] = s->smb_io_base | 1;

    pci_conf[0x91] = s->smb_io_base >> 8;

    pci_conf[0xd2] = 0x90;

    register_ioport_write(s->smb_io_base, 0xf, 1, smb_ioport_writeb, &s->smb);

    register_ioport_read(s->smb_io_base, 0xf, 1, smb_ioport_readb, &s->smb);

    apm_init(&s->apm, NULL, s);

    acpi_pm_tmr_init(&s->tmr, pm_tmr_timer);

    acpi_pm1_cnt_init(&s->pm1_cnt, NULL);

    pm_smbus_init(&s->dev.qdev, &s->smb);

    return 0;

}


restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)

{

    int temp;

    __get_user(env->aregs[7], &sc->sc_usp);

    __get_user(env->dregs[1], &sc->sc_d1);

    __get_user(env->aregs[0], &sc->sc_a0);

    __get_user(env->aregs[1], &sc->sc_a1);

    __get_user(env->pc, &sc->sc_pc);

    __get_user(temp, &sc->sc_sr);

    env->sr = (env->sr & 0xff00) | (temp & 0xff);

    *pd0 = tswapl(sc->sc_d0);

}


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

                          const char *name, Error **errp)

{

    DeviceState *dev = DEVICE(obj);

    Property *prop = opaque;

    uint16_t value, *ptr = qdev_get_prop_ptr(dev, prop);

    Error *local_err = NULL;

    const int64_t min = 512;

    const int64_t max = 32768;

    if (dev->realized) {

        qdev_prop_set_after_realize(dev, name, errp);

        return;

    }

    visit_type_uint16(v, &value, name, &local_err);

    if (local_err) {

        error_propagate(errp, local_err);

        return;

    }

    if (value < min || value > max) {

        error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,

                  dev->id?:"", name, (int64_t)value, min, max);

        return;

    }

    /* We rely on power-of-2 blocksizes for bitmasks */

    if ((value & (value - 1)) != 0) {

        error_setg(errp,

                  "Property %s.%s doesn't take value '%" PRId64 "', it's not a power of 2",

                  dev->id ?: "", name, (int64_t)value);

        return;

    }

    *ptr = value;

}


static void load_linux(FWCfgState *fw_cfg,

                       const char *kernel_filename,

                       const char *initrd_filename,

                       const char *kernel_cmdline,

                       hwaddr max_ram_size)

{

    uint16_t protocol;

    int setup_size, kernel_size, initrd_size = 0, cmdline_size;

    uint32_t initrd_max;

    uint8_t header[8192], *setup, *kernel, *initrd_data;

    hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;

    FILE *f;

    char *vmode;

    /* Align to 16 bytes as a paranoia measure */

    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;

    /* load the kernel header */

    f = fopen(kernel_filename, "rb");

    if (!f || !(kernel_size = get_file_size(f)) ||

        fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=

        MIN(ARRAY_SIZE(header), kernel_size)) {

        fprintf(stderr, "qemu: could not load kernel '%s': %s\n",

                kernel_filename, strerror(errno));

        exit(1);

    }

    /* kernel protocol version */

#if 0

    fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));

#endif

    if (ldl_p(header+0x202) == 0x53726448) {

        protocol = lduw_p(header+0x206);

    } else {

        /* This looks like a multiboot kernel. If it is, let's stop

           treating it like a Linux kernel. */

        if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,

                           kernel_cmdline, kernel_size, header)) {

            return;

        }

        protocol = 0;

    }

    if (protocol < 0x200 || !(header[0x211] & 0x01)) {

        /* Low kernel */

        real_addr    = 0x90000;

        cmdline_addr = 0x9a000 - cmdline_size;

        prot_addr    = 0x10000;

    } else if (protocol < 0x202) {

        /* High but ancient kernel */

        real_addr    = 0x90000;

        cmdline_addr = 0x9a000 - cmdline_size;

        prot_addr    = 0x100000;

    } else {

        /* High and recent kernel */

        real_addr    = 0x10000;

        cmdline_addr = 0x20000;

        prot_addr    = 0x100000;

    }

#if 0

    fprintf(stderr,

            "qemu: real_addr     = 0x" TARGET_FMT_plx "\n"

            "qemu: cmdline_addr  = 0x" TARGET_FMT_plx "\n"

            "qemu: prot_addr     = 0x" TARGET_FMT_plx "\n",

            real_addr,

            cmdline_addr,

            prot_addr);

#endif

    /* highest address for loading the initrd */

    if (protocol >= 0x203) {

        initrd_max = ldl_p(header+0x22c);

    } else {

        initrd_max = 0x37ffffff;

    }

    if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)

    	initrd_max = max_ram_size-ACPI_DATA_SIZE-1;

    fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);

    fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);

    fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);

    if (protocol >= 0x202) {

        stl_p(header+0x228, cmdline_addr);

    } else {

        stw_p(header+0x20, 0xA33F);

        stw_p(header+0x22, cmdline_addr-real_addr);

    }

    /* handle vga= parameter */

    vmode = strstr(kernel_cmdline, "vga=");

    if (vmode) {

        unsigned int video_mode;

        /* skip "vga=" */

        vmode += 4;

        if (!strncmp(vmode, "normal", 6)) {

            video_mode = 0xffff;

        } else if (!strncmp(vmode, "ext", 3)) {

            video_mode = 0xfffe;

        } else if (!strncmp(vmode, "ask", 3)) {

            video_mode = 0xfffd;

        } else {

            video_mode = strtol(vmode, NULL, 0);

        }

        stw_p(header+0x1fa, video_mode);

    }

    /* loader type */

    /* High nybble = B reserved for QEMU; low nybble is revision number.

       If this code is substantially changed, you may want to consider

       incrementing the revision. */

    if (protocol >= 0x200) {

        header[0x210] = 0xB0;

    }

    /* heap */

    if (protocol >= 0x201) {

        header[0x211] |= 0x80;	/* CAN_USE_HEAP */

        stw_p(header+0x224, cmdline_addr-real_addr-0x200);

    }

    /* load initrd */

    if (initrd_filename) {

        if (protocol < 0x200) {

            fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");

            exit(1);

        }

        initrd_size = get_image_size(initrd_filename);

        if (initrd_size < 0) {

            fprintf(stderr, "qemu: error reading initrd %s: %s\n",

                    initrd_filename, strerror(errno));

            exit(1);

        }

        initrd_addr = (initrd_max-initrd_size) & ~4095;

        initrd_data = g_malloc(initrd_size);

        load_image(initrd_filename, initrd_data);

        fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);

        fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);

        fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);

        stl_p(header+0x218, initrd_addr);

        stl_p(header+0x21c, initrd_size);

    }

    /* load kernel and setup */

    setup_size = header[0x1f1];

    if (setup_size == 0) {

        setup_size = 4;

    }

    setup_size = (setup_size+1)*512;

    kernel_size -= setup_size;

    setup  = g_malloc(setup_size);

    kernel = g_malloc(kernel_size);

    fseek(f, 0, SEEK_SET);

    if (fread(setup, 1, setup_size, f) != setup_size) {

        fprintf(stderr, "fread() failed\n");

        exit(1);

    }

    if (fread(kernel, 1, kernel_size, f) != kernel_size) {

        fprintf(stderr, "fread() failed\n");

        exit(1);

    }

    fclose(f);

    memcpy(setup, header, MIN(sizeof(header), setup_size));

    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);

    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);

    fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);

    fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);

    fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);

    fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);

    option_rom[nb_option_roms].name = "linuxboot.bin";

    option_rom[nb_option_roms].bootindex = 0;

    nb_option_roms++;

}


void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)

{

    SaveStateEntry *se, *new_se;

    char id[256] = "";

    if (dev) {

        char *path = qdev_get_dev_path(dev);

        if (path) {

            pstrcpy(id, sizeof(id), path);

            pstrcat(id, sizeof(id), "/");

            g_free(path);

        }

    }

    pstrcat(id, sizeof(id), idstr);

    QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {

        if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {

            QTAILQ_REMOVE(&savevm_state.handlers, se, entry);

            if (se->compat) {

                g_free(se->compat);

            }

            g_free(se->ops);

            g_free(se);

        }

    }

}


static int get_cluster_offset(BlockDriverState *bs,

                                    VmdkExtent *extent,

                                    VmdkMetaData *m_data,

                                    uint64_t offset,

                                    int allocate,

                                    uint64_t *cluster_offset)

{

    unsigned int l1_index, l2_offset, l2_index;

    int min_index, i, j;

    uint32_t min_count, *l2_table, tmp = 0;

    if (m_data)

        m_data->valid = 0;

    if (extent->flat) {

        *cluster_offset = extent->flat_start_offset;

        return 0;

    }

    l1_index = (offset >> 9) / extent->l1_entry_sectors;

    if (l1_index >= extent->l1_size) {

        return -1;

    }

    l2_offset = extent->l1_table[l1_index];

    if (!l2_offset) {

        return -1;

    }

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

        if (l2_offset == extent->l2_cache_offsets[i]) {

            /* increment the hit count */

            if (++extent->l2_cache_counts[i] == 0xffffffff) {

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

                    extent->l2_cache_counts[j] >>= 1;

                }

            }

            l2_table = extent->l2_cache + (i * extent->l2_size);

            goto found;

        }

    }

    /* not found: load a new entry in the least used one */

    min_index = 0;

    min_count = 0xffffffff;

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

        if (extent->l2_cache_counts[i] < min_count) {

            min_count = extent->l2_cache_counts[i];

            min_index = i;

        }

    }

    l2_table = extent->l2_cache + (min_index * extent->l2_size);

    if (bdrv_pread(

                extent->file,

                (int64_t)l2_offset * 512,

                l2_table,

                extent->l2_size * sizeof(uint32_t)

            ) != extent->l2_size * sizeof(uint32_t)) {

        return -1;

    }

    extent->l2_cache_offsets[min_index] = l2_offset;

    extent->l2_cache_counts[min_index] = 1;

 found:

    l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;

    *cluster_offset = le32_to_cpu(l2_table[l2_index]);

    if (!*cluster_offset) {

        if (!allocate) {

            return -1;

        }

        // Avoid the L2 tables update for the images that have snapshots.

        *cluster_offset = bdrv_getlength(extent->file);

        bdrv_truncate(

            extent->file,

            *cluster_offset + (extent->cluster_sectors << 9)

        );

        *cluster_offset >>= 9;

        tmp = cpu_to_le32(*cluster_offset);

        l2_table[l2_index] = tmp;

        /* First of all we write grain itself, to avoid race condition

         * that may to corrupt the image.

         * This problem may occur because of insufficient space on host disk

         * or inappropriate VM shutdown.

         */

        if (get_whole_cluster(

                bs, extent, *cluster_offset, offset, allocate) == -1)

            return -1;

        if (m_data) {

            m_data->offset = tmp;

            m_data->l1_index = l1_index;

            m_data->l2_index = l2_index;

            m_data->l2_offset = l2_offset;

            m_data->valid = 1;

        }

    }

    *cluster_offset <<= 9;

    return 0;

}


static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,

			    int is_user, uint32_t *phys_ptr, int *prot)

{

    int code;

    uint32_t table;

    uint32_t desc;

    uint32_t xn;

    int type;

    int ap;

    int domain;

    uint32_t phys_addr;

    /* Pagetable walk.  */

    /* Lookup l1 descriptor.  */

    table = get_level1_table_address(env, address);

    desc = ldl_phys(table);

    type = (desc & 3);

    if (type == 0) {

        /* Section translation fault.  */

        code = 5;

        domain = 0;

        goto do_fault;

    } else if (type == 2 && (desc & (1 << 18))) {

        /* Supersection.  */

        domain = 0;

    } else {

        /* Section or page.  */

        domain = (desc >> 4) & 0x1e;

    }

    domain = (env->cp15.c3 >> domain) & 3;

    if (domain == 0 || domain == 2) {

        if (type == 2)

            code = 9; /* Section domain fault.  */

        else

            code = 11; /* Page domain fault.  */

        goto do_fault;

    }

    if (type == 2) {

        if (desc & (1 << 18)) {

            /* Supersection.  */

            phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);

        } else {

            /* Section.  */

            phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);

        }

        ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);

        xn = desc & (1 << 4);

        code = 13;

    } else {

        /* Lookup l2 entry.  */

        table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);

        desc = ldl_phys(table);

        ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);

        switch (desc & 3) {

        case 0: /* Page translation fault.  */

            code = 7;

            goto do_fault;

        case 1: /* 64k page.  */

            phys_addr = (desc & 0xffff0000) | (address & 0xffff);

            xn = desc & (1 << 15);

            break;

        case 2: case 3: /* 4k page.  */

            phys_addr = (desc & 0xfffff000) | (address & 0xfff);

            xn = desc & 1;

            break;

        default:

            /* Never happens, but compiler isn't smart enough to tell.  */

            abort();

        }

        code = 15;

    }

    if (xn && access_type == 2)

        goto do_fault;

    /* The simplified model uses AP[0] as an access control bit.  */

    if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {

        /* Access flag fault.  */

        code = (code == 15) ? 6 : 3;

        goto do_fault;

    }

    *prot = check_ap(env, ap, domain, access_type, is_user);

    if (!*prot) {

        /* Access permission fault.  */

        goto do_fault;

    }

    *phys_ptr = phys_addr;

    return 0;

do_fault:

    return code | (domain << 4);

}


void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,

                                        uint8_t *buf, uint64_t start,

                                        uint64_t count, bool finish)

{

    hbitmap_deserialize_part(bitmap->bitmap, buf, start, count, finish);

}


void avcodec_get_context_defaults2(AVCodecContext *s, enum AVMediaType codec_type){

    int flags=0;

    memset(s, 0, sizeof(AVCodecContext));

    s->av_class= &av_codec_context_class;

    s->codec_type = codec_type;

    if(codec_type == AVMEDIA_TYPE_AUDIO)

        flags= AV_OPT_FLAG_AUDIO_PARAM;

    else if(codec_type == AVMEDIA_TYPE_VIDEO)

        flags= AV_OPT_FLAG_VIDEO_PARAM;

    else if(codec_type == AVMEDIA_TYPE_SUBTITLE)

        flags= AV_OPT_FLAG_SUBTITLE_PARAM;

    av_opt_set_defaults2(s, flags, flags);

    s->time_base= (AVRational){0,1};

    s->get_buffer= avcodec_default_get_buffer;

    s->release_buffer= avcodec_default_release_buffer;

    s->get_format= avcodec_default_get_format;

    s->execute= avcodec_default_execute;

    s->execute2= avcodec_default_execute2;

    s->sample_aspect_ratio= (AVRational){0,1};

    s->pix_fmt= PIX_FMT_NONE;

    s->sample_fmt= AV_SAMPLE_FMT_NONE;

    s->palctrl = NULL;

    s->reget_buffer= avcodec_default_reget_buffer;

    s->reordered_opaque= AV_NOPTS_VALUE;

}


static bool qemu_gluster_test_seek(struct glfs_fd *fd)

{

    off_t ret, eof;

    eof = glfs_lseek(fd, 0, SEEK_END);

    if (eof < 0) {

        /* this should never occur */

        return false;

    }

    /* this should always fail with ENXIO if SEEK_DATA is supported */

    ret = glfs_lseek(fd, eof, SEEK_DATA);

    return (ret < 0) && (errno == ENXIO);

}