static int proxy_opendir(FsContext *ctx,

                         V9fsPath *fs_path, V9fsFidOpenState *fs)

{

    int serrno, fd;

    fs->dir = NULL;

    fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, O_DIRECTORY);

    if (fd < 0) {

        errno = -fd;

        return -1;

    }

    fs->dir = fdopendir(fd);

    if (!fs->dir) {

        serrno = errno;

        close(fd);

        errno = serrno;

        return -1;

    }

    return 0;

}


static sd_rsp_type_t sd_normal_command(SDState *sd,

                                       SDRequest req)

{

    uint32_t rca = 0x0000;

    uint64_t addr = (sd->ocr & (1 << 30)) ? (uint64_t) req.arg << 9 : req.arg;

    /* Not interpreting this as an app command */

    sd->card_status &= ~APP_CMD;

    if (sd_cmd_type[req.cmd] == sd_ac || sd_cmd_type[req.cmd] == sd_adtc)

        rca = req.arg >> 16;

    DPRINTF("CMD%d 0x%08x state %d\n", req.cmd, req.arg, sd->state);

    switch (req.cmd) {

    /* Basic commands (Class 0 and Class 1) */

    case 0:	/* CMD0:   GO_IDLE_STATE */

        switch (sd->state) {

        case sd_inactive_state:

            return sd->spi ? sd_r1 : sd_r0;

        default:

            sd->state = sd_idle_state;

            sd_reset(sd, sd->bdrv);

            return sd->spi ? sd_r1 : sd_r0;

        }

        break;

    case 1:	/* CMD1:   SEND_OP_CMD */

        if (!sd->spi)

            goto bad_cmd;

        sd->state = sd_transfer_state;

        return sd_r1;

    case 2:	/* CMD2:   ALL_SEND_CID */

        if (sd->spi)

            goto bad_cmd;

        switch (sd->state) {

        case sd_ready_state:

            sd->state = sd_identification_state;

            return sd_r2_i;

        default:

            break;

        }

        break;

    case 3:	/* CMD3:   SEND_RELATIVE_ADDR */

        if (sd->spi)

            goto bad_cmd;

        switch (sd->state) {

        case sd_identification_state:

        case sd_standby_state:

            sd->state = sd_standby_state;

            sd_set_rca(sd);

            return sd_r6;

        default:

            break;

        }

        break;

    case 4:	/* CMD4:   SEND_DSR */

        if (sd->spi)

            goto bad_cmd;

        switch (sd->state) {

        case sd_standby_state:

            break;

        default:

            break;

        }

        break;

    case 5: /* CMD5: reserved for SDIO cards */

        return sd_illegal;

    case 6:	/* CMD6:   SWITCH_FUNCTION */

        if (sd->spi)

            goto bad_cmd;

        switch (sd->mode) {

        case sd_data_transfer_mode:

            sd_function_switch(sd, req.arg);

            sd->state = sd_sendingdata_state;

            sd->data_start = 0;

            sd->data_offset = 0;

            return sd_r1;

        default:

            break;

        }

        break;

    case 7:	/* CMD7:   SELECT/DESELECT_CARD */

        if (sd->spi)

            goto bad_cmd;

        switch (sd->state) {

        case sd_standby_state:

            if (sd->rca != rca)

                return sd_r0;

            sd->state = sd_transfer_state;

            return sd_r1b;

        case sd_transfer_state:

        case sd_sendingdata_state:

            if (sd->rca == rca)

                break;

            sd->state = sd_standby_state;

            return sd_r1b;

        case sd_disconnect_state:

            if (sd->rca != rca)

                return sd_r0;

            sd->state = sd_programming_state;

            return sd_r1b;

        case sd_programming_state:

            if (sd->rca == rca)

                break;

            sd->state = sd_disconnect_state;

            return sd_r1b;

        default:

            break;

        }

        break;

    case 8:	/* CMD8:   SEND_IF_COND */

        /* Physical Layer Specification Version 2.00 command */

        switch (sd->state) {

        case sd_idle_state:

            sd->vhs = 0;

            /* No response if not exactly one VHS bit is set.  */

            if (!(req.arg >> 8) || (req.arg >> ffs(req.arg & ~0xff)))

                return sd->spi ? sd_r7 : sd_r0;

            /* Accept.  */

            sd->vhs = req.arg;

            return sd_r7;

        default:

            break;

        }

        break;

    case 9:	/* CMD9:   SEND_CSD */

        switch (sd->state) {

        case sd_standby_state:

            if (sd->rca != rca)

                return sd_r0;

            return sd_r2_s;

        case sd_transfer_state:

            if (!sd->spi)

                break;

            sd->state = sd_sendingdata_state;

            memcpy(sd->data, sd->csd, 16);

            sd->data_start = addr;

            sd->data_offset = 0;

            return sd_r1;

        default:

            break;

        }

        break;

    case 10:	/* CMD10:  SEND_CID */

        switch (sd->state) {

        case sd_standby_state:

            if (sd->rca != rca)

                return sd_r0;

            return sd_r2_i;

        case sd_transfer_state:

            if (!sd->spi)

                break;

            sd->state = sd_sendingdata_state;

            memcpy(sd->data, sd->cid, 16);

            sd->data_start = addr;

            sd->data_offset = 0;

            return sd_r1;

        default:

            break;

        }

        break;

    case 11:	/* CMD11:  READ_DAT_UNTIL_STOP */

        if (sd->spi)

            goto bad_cmd;

        switch (sd->state) {

        case sd_transfer_state:

            sd->state = sd_sendingdata_state;

            sd->data_start = req.arg;

            sd->data_offset = 0;

            if (sd->data_start + sd->blk_len > sd->size)

                sd->card_status |= ADDRESS_ERROR;

            return sd_r0;

        default:

            break;

        }

        break;

    case 12:	/* CMD12:  STOP_TRANSMISSION */

        switch (sd->state) {

        case sd_sendingdata_state:

            sd->state = sd_transfer_state;

            return sd_r1b;

        case sd_receivingdata_state:

            sd->state = sd_programming_state;

            /* Bzzzzzzztt .... Operation complete.  */

            sd->state = sd_transfer_state;

            return sd_r1b;

        default:

            break;

        }

        break;

    case 13:	/* CMD13:  SEND_STATUS */

        switch (sd->mode) {

        case sd_data_transfer_mode:

            if (sd->rca != rca)

                return sd_r0;

            return sd_r1;

        default:

            break;

        }

        break;

    case 15:	/* CMD15:  GO_INACTIVE_STATE */

        if (sd->spi)

            goto bad_cmd;

        switch (sd->mode) {

        case sd_data_transfer_mode:

            if (sd->rca != rca)

                return sd_r0;

            sd->state = sd_inactive_state;

            return sd_r0;

        default:

            break;

        }

        break;

    /* Block read commands (Classs 2) */

    case 16:	/* CMD16:  SET_BLOCKLEN */

        switch (sd->state) {

        case sd_transfer_state:

            if (req.arg > (1 << HWBLOCK_SHIFT))

                sd->card_status |= BLOCK_LEN_ERROR;

            else

                sd->blk_len = req.arg;

            return sd_r1;

        default:

            break;

        }

        break;

    case 17:	/* CMD17:  READ_SINGLE_BLOCK */

        switch (sd->state) {

        case sd_transfer_state:

            sd->state = sd_sendingdata_state;

            sd->data_start = addr;

            sd->data_offset = 0;

            if (sd->data_start + sd->blk_len > sd->size)

                sd->card_status |= ADDRESS_ERROR;

            return sd_r1;

        default:

            break;

        }

        break;

    case 18:	/* CMD18:  READ_MULTIPLE_BLOCK */

        switch (sd->state) {

        case sd_transfer_state:

            sd->state = sd_sendingdata_state;

            sd->data_start = addr;

            sd->data_offset = 0;

            if (sd->data_start + sd->blk_len > sd->size)

                sd->card_status |= ADDRESS_ERROR;

            return sd_r1;

        default:

            break;

        }

        break;

    /* Block write commands (Class 4) */

    case 24:	/* CMD24:  WRITE_SINGLE_BLOCK */

        if (sd->spi)

            goto unimplemented_cmd;

        switch (sd->state) {

        case sd_transfer_state:

            /* Writing in SPI mode not implemented.  */

            if (sd->spi)

                break;

            sd->state = sd_receivingdata_state;

            sd->data_start = addr;

            sd->data_offset = 0;

            sd->blk_written = 0;

            if (sd->data_start + sd->blk_len > sd->size)

                sd->card_status |= ADDRESS_ERROR;

            if (sd_wp_addr(sd, sd->data_start))

                sd->card_status |= WP_VIOLATION;

            if (sd->csd[14] & 0x30)

                sd->card_status |= WP_VIOLATION;

            return sd_r1;

        default:

            break;

        }

        break;

    case 25:	/* CMD25:  WRITE_MULTIPLE_BLOCK */

        if (sd->spi)

            goto unimplemented_cmd;

        switch (sd->state) {

        case sd_transfer_state:

            /* Writing in SPI mode not implemented.  */

            if (sd->spi)

                break;

            sd->state = sd_receivingdata_state;

            sd->data_start = addr;

            sd->data_offset = 0;

            sd->blk_written = 0;

            if (sd->data_start + sd->blk_len > sd->size)

                sd->card_status |= ADDRESS_ERROR;

            if (sd_wp_addr(sd, sd->data_start))

                sd->card_status |= WP_VIOLATION;

            if (sd->csd[14] & 0x30)

                sd->card_status |= WP_VIOLATION;

            return sd_r1;

        default:

            break;

        }

        break;

    case 26:	/* CMD26:  PROGRAM_CID */

        if (sd->spi)

            goto bad_cmd;

        switch (sd->state) {

        case sd_transfer_state:

            sd->state = sd_receivingdata_state;

            sd->data_start = 0;

            sd->data_offset = 0;

            return sd_r1;

        default:

            break;

        }

        break;

    case 27:	/* CMD27:  PROGRAM_CSD */

        if (sd->spi)

            goto unimplemented_cmd;

        switch (sd->state) {

        case sd_transfer_state:

            sd->state = sd_receivingdata_state;

            sd->data_start = 0;

            sd->data_offset = 0;

            return sd_r1;

        default:

            break;

        }

        break;

    /* Write protection (Class 6) */

    case 28:	/* CMD28:  SET_WRITE_PROT */

        switch (sd->state) {

        case sd_transfer_state:

            if (addr >= sd->size) {

                sd->card_status |= ADDRESS_ERROR;

                return sd_r1b;

            }

            sd->state = sd_programming_state;

            set_bit(sd_addr_to_wpnum(addr), sd->wp_groups);

            /* Bzzzzzzztt .... Operation complete.  */

            sd->state = sd_transfer_state;

            return sd_r1b;

        default:

            break;

        }

        break;

    case 29:	/* CMD29:  CLR_WRITE_PROT */

        switch (sd->state) {

        case sd_transfer_state:

            if (addr >= sd->size) {

                sd->card_status |= ADDRESS_ERROR;

                return sd_r1b;

            }

            sd->state = sd_programming_state;

            clear_bit(sd_addr_to_wpnum(addr), sd->wp_groups);

            /* Bzzzzzzztt .... Operation complete.  */

            sd->state = sd_transfer_state;

            return sd_r1b;

        default:

            break;

        }

        break;

    case 30:	/* CMD30:  SEND_WRITE_PROT */

        switch (sd->state) {

        case sd_transfer_state:

            sd->state = sd_sendingdata_state;

            *(uint32_t *) sd->data = sd_wpbits(sd, req.arg);

            sd->data_start = addr;

            sd->data_offset = 0;

            return sd_r1b;

        default:

            break;

        }

        break;

    /* Erase commands (Class 5) */

    case 32:	/* CMD32:  ERASE_WR_BLK_START */

        switch (sd->state) {

        case sd_transfer_state:

            sd->erase_start = req.arg;

            return sd_r1;

        default:

            break;

        }

        break;

    case 33:	/* CMD33:  ERASE_WR_BLK_END */

        switch (sd->state) {

        case sd_transfer_state:

            sd->erase_end = req.arg;

            return sd_r1;

        default:

            break;

        }

        break;

    case 38:	/* CMD38:  ERASE */

        switch (sd->state) {

        case sd_transfer_state:

            if (sd->csd[14] & 0x30) {

                sd->card_status |= WP_VIOLATION;

                return sd_r1b;

            }

            sd->state = sd_programming_state;

            sd_erase(sd);

            /* Bzzzzzzztt .... Operation complete.  */

            sd->state = sd_transfer_state;

            return sd_r1b;

        default:

            break;

        }

        break;

    /* Lock card commands (Class 7) */

    case 42:	/* CMD42:  LOCK_UNLOCK */

        if (sd->spi)

            goto unimplemented_cmd;

        switch (sd->state) {

        case sd_transfer_state:

            sd->state = sd_receivingdata_state;

            sd->data_start = 0;

            sd->data_offset = 0;

            return sd_r1;

        default:

            break;

        }

        break;

    case 52:

    case 53:

        /* CMD52, CMD53: reserved for SDIO cards

         * (see the SDIO Simplified Specification V2.0)

         * Handle as illegal command but do not complain

         * on stderr, as some OSes may use these in their

         * probing for presence of an SDIO card.

         */

        return sd_illegal;

    /* Application specific commands (Class 8) */

    case 55:	/* CMD55:  APP_CMD */

        if (sd->rca != rca)

            return sd_r0;

        sd->expecting_acmd = true;

        sd->card_status |= APP_CMD;

        return sd_r1;

    case 56:	/* CMD56:  GEN_CMD */

        fprintf(stderr, "SD: GEN_CMD 0x%08x\n", req.arg);

        switch (sd->state) {

        case sd_transfer_state:

            sd->data_offset = 0;

            if (req.arg & 1)

                sd->state = sd_sendingdata_state;

            else

                sd->state = sd_receivingdata_state;

            return sd_r1;

        default:

            break;

        }

        break;

    default:

    bad_cmd:

        fprintf(stderr, "SD: Unknown CMD%i\n", req.cmd);

        return sd_illegal;

    unimplemented_cmd:

        /* Commands that are recognised but not yet implemented in SPI mode.  */

        fprintf(stderr, "SD: CMD%i not implemented in SPI mode\n", req.cmd);

        return sd_illegal;

    }

    fprintf(stderr, "SD: CMD%i in a wrong state\n", req.cmd);

    return sd_illegal;

}


static void amdvi_mmio_trace(hwaddr addr, unsigned size)

{

    uint8_t index = (addr & ~0x2000) / 8;

    if ((addr & 0x2000)) {

        /* high table */

        index = index >= AMDVI_MMIO_REGS_HIGH ? AMDVI_MMIO_REGS_HIGH : index;

        trace_amdvi_mmio_read(amdvi_mmio_high[index], addr, size, addr & ~0x07);

    } else {

        index = index >= AMDVI_MMIO_REGS_LOW ? AMDVI_MMIO_REGS_LOW : index;

        trace_amdvi_mmio_read(amdvi_mmio_high[index], addr, size, addr & ~0x07);

    }

}


void virtio_cleanup(VirtIODevice *vdev)

{

    qemu_del_vm_change_state_handler(vdev->vmstate);

    g_free(vdev->config);

    g_free(vdev->vq);

    g_free(vdev->vector_queues);

}


static void gen_pool32axf (CPUMIPSState *env, DisasContext *ctx, int rt, int rs)

{

    int extension = (ctx->opcode >> 6) & 0x3f;

    int minor = (ctx->opcode >> 12) & 0xf;

    uint32_t mips32_op;

    switch (extension) {

    case TEQ:

        mips32_op = OPC_TEQ;

        goto do_trap;

    case TGE:

        mips32_op = OPC_TGE;

        goto do_trap;

    case TGEU:

        mips32_op = OPC_TGEU;

        goto do_trap;

    case TLT:

        mips32_op = OPC_TLT;

        goto do_trap;

    case TLTU:

        mips32_op = OPC_TLTU;

        goto do_trap;

    case TNE:

        mips32_op = OPC_TNE;

    do_trap:

        gen_trap(ctx, mips32_op, rs, rt, -1);

        break;

#ifndef CONFIG_USER_ONLY

    case MFC0:

    case MFC0 + 32:

        check_cp0_enabled(ctx);

        if (rt == 0) {

            /* Treat as NOP. */

            break;

        }

        gen_mfc0(ctx, cpu_gpr[rt], rs, (ctx->opcode >> 11) & 0x7);

        break;

    case MTC0:

    case MTC0 + 32:

        check_cp0_enabled(ctx);

        {

            TCGv t0 = tcg_temp_new();

            gen_load_gpr(t0, rt);

            gen_mtc0(ctx, t0, rs, (ctx->opcode >> 11) & 0x7);

            tcg_temp_free(t0);

        }

        break;

#endif

    case 0x2a:

        switch (minor & 3) {

        case MADD_ACC:

            gen_muldiv(ctx, OPC_MADD, (ctx->opcode >> 14) & 3, rs, rt);

            break;

        case MADDU_ACC:

            gen_muldiv(ctx, OPC_MADDU, (ctx->opcode >> 14) & 3, rs, rt);

            break;

        case MSUB_ACC:

            gen_muldiv(ctx, OPC_MSUB, (ctx->opcode >> 14) & 3, rs, rt);

            break;

        case MSUBU_ACC:

            gen_muldiv(ctx, OPC_MSUBU, (ctx->opcode >> 14) & 3, rs, rt);

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    case 0x32:

        switch (minor & 3) {

        case MULT_ACC:

            gen_muldiv(ctx, OPC_MULT, (ctx->opcode >> 14) & 3, rs, rt);

            break;

        case MULTU_ACC:

            gen_muldiv(ctx, OPC_MULTU, (ctx->opcode >> 14) & 3, rs, rt);

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    case 0x2c:

        switch (minor) {

        case BITSWAP:

            check_insn(ctx, ISA_MIPS32R6);

            gen_bitswap(ctx, OPC_BITSWAP, rs, rt);

            break;

        case SEB:

            gen_bshfl(ctx, OPC_SEB, rs, rt);

            break;

        case SEH:

            gen_bshfl(ctx, OPC_SEH, rs, rt);

            break;

        case CLO:

            mips32_op = OPC_CLO;

            goto do_cl;

        case CLZ:

            mips32_op = OPC_CLZ;

        do_cl:

            check_insn(ctx, ISA_MIPS32);

            gen_cl(ctx, mips32_op, rt, rs);

            break;

        case RDHWR:

            gen_rdhwr(ctx, rt, rs);

            break;

        case WSBH:

            gen_bshfl(ctx, OPC_WSBH, rs, rt);

            break;

        case MULT:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            mips32_op = OPC_MULT;

            goto do_mul;

        case MULTU:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            mips32_op = OPC_MULTU;

            goto do_mul;

        case DIV:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            mips32_op = OPC_DIV;

            goto do_div;

        case DIVU:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            mips32_op = OPC_DIVU;

            goto do_div;

        do_div:

            check_insn(ctx, ISA_MIPS32);

            gen_muldiv(ctx, mips32_op, 0, rs, rt);

            break;

        case MADD:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            mips32_op = OPC_MADD;

            goto do_mul;

        case MADDU:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            mips32_op = OPC_MADDU;

            goto do_mul;

        case MSUB:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            mips32_op = OPC_MSUB;

            goto do_mul;

        case MSUBU:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            mips32_op = OPC_MSUBU;

        do_mul:

            check_insn(ctx, ISA_MIPS32);

            gen_muldiv(ctx, mips32_op, 0, rs, rt);

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    case 0x34:

        switch (minor) {

        case MFC2:

        case MTC2:

        case MFHC2:

        case MTHC2:

        case CFC2:

        case CTC2:

            generate_exception_err(ctx, EXCP_CpU, 2);

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    case 0x3c:

        switch (minor) {

        case JALR:    /* JALRC */

        case JALR_HB: /* JALRC_HB */

            if (ctx->insn_flags & ISA_MIPS32R6) {

                /* JALRC, JALRC_HB */

                gen_compute_branch(ctx, OPC_JALR, 4, rs, rt, 0, 0);

            } else {

                /* JALR, JALR_HB */

                gen_compute_branch(ctx, OPC_JALR, 4, rs, rt, 0, 4);

                ctx->hflags |= MIPS_HFLAG_BDS_STRICT;

            }

            break;

        case JALRS:

        case JALRS_HB:

            check_insn_opc_removed(ctx, ISA_MIPS32R6);

            gen_compute_branch(ctx, OPC_JALR, 4, rs, rt, 0, 2);

            ctx->hflags |= MIPS_HFLAG_BDS_STRICT;

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    case 0x05:

        switch (minor) {

        case RDPGPR:

            check_cp0_enabled(ctx);

            check_insn(ctx, ISA_MIPS32R2);

            gen_load_srsgpr(rs, rt);

            break;

        case WRPGPR:

            check_cp0_enabled(ctx);

            check_insn(ctx, ISA_MIPS32R2);

            gen_store_srsgpr(rs, rt);

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

#ifndef CONFIG_USER_ONLY

    case 0x0d:

        switch (minor) {

        case TLBP:

            mips32_op = OPC_TLBP;

            goto do_cp0;

        case TLBR:

            mips32_op = OPC_TLBR;

            goto do_cp0;

        case TLBWI:

            mips32_op = OPC_TLBWI;

            goto do_cp0;

        case TLBWR:

            mips32_op = OPC_TLBWR;

            goto do_cp0;

        case TLBINV:

            mips32_op = OPC_TLBINV;

            goto do_cp0;

        case TLBINVF:

            mips32_op = OPC_TLBINVF;

            goto do_cp0;

        case WAIT:

            mips32_op = OPC_WAIT;

            goto do_cp0;

        case DERET:

            mips32_op = OPC_DERET;

            goto do_cp0;

        case ERET:

            mips32_op = OPC_ERET;

        do_cp0:

            gen_cp0(env, ctx, mips32_op, rt, rs);

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    case 0x1d:

        switch (minor) {

        case DI:

            check_cp0_enabled(ctx);

            {

                TCGv t0 = tcg_temp_new();

                save_cpu_state(ctx, 1);

                gen_helper_di(t0, cpu_env);

                gen_store_gpr(t0, rs);

                /* Stop translation as we may have switched the execution mode */

                ctx->bstate = BS_STOP;

                tcg_temp_free(t0);

            }

            break;

        case EI:

            check_cp0_enabled(ctx);

            {

                TCGv t0 = tcg_temp_new();

                save_cpu_state(ctx, 1);

                gen_helper_ei(t0, cpu_env);

                gen_store_gpr(t0, rs);

                /* Stop translation as we may have switched the execution mode */

                ctx->bstate = BS_STOP;

                tcg_temp_free(t0);

            }

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

#endif

    case 0x2d:

        switch (minor) {

        case SYNC:

            /* NOP */

            break;

        case SYSCALL:

            generate_exception_end(ctx, EXCP_SYSCALL);

            break;

        case SDBBP:

            if (is_uhi(extract32(ctx->opcode, 16, 10))) {

                gen_helper_do_semihosting(cpu_env);

            } else {

                check_insn(ctx, ISA_MIPS32);

                if (ctx->hflags & MIPS_HFLAG_SBRI) {

                    generate_exception_end(ctx, EXCP_RI);

                } else {

                    generate_exception_end(ctx, EXCP_DBp);

                }

            }

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    case 0x01:

        switch (minor & 3) {

        case MFHI_ACC:

            gen_HILO(ctx, OPC_MFHI, minor >> 2, rs);

            break;

        case MFLO_ACC:

            gen_HILO(ctx, OPC_MFLO, minor >> 2, rs);

            break;

        case MTHI_ACC:

            gen_HILO(ctx, OPC_MTHI, minor >> 2, rs);

            break;

        case MTLO_ACC:

            gen_HILO(ctx, OPC_MTLO, minor >> 2, rs);

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    case 0x35:

        check_insn_opc_removed(ctx, ISA_MIPS32R6);

        switch (minor) {

        case MFHI32:

            gen_HILO(ctx, OPC_MFHI, 0, rs);

            break;

        case MFLO32:

            gen_HILO(ctx, OPC_MFLO, 0, rs);

            break;

        case MTHI32:

            gen_HILO(ctx, OPC_MTHI, 0, rs);

            break;

        case MTLO32:

            gen_HILO(ctx, OPC_MTLO, 0, rs);

            break;

        default:

            goto pool32axf_invalid;

        }

        break;

    default:

    pool32axf_invalid:

        MIPS_INVAL("pool32axf");

        generate_exception_end(ctx, EXCP_RI);

        break;

    }

}


void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,

                     int16_t (*mv_table)[2], int f_code, int type, int truncate)

{

    MotionEstContext * const c= &s->me;

    int y, h_range, v_range;

    // RAL: 8 in MPEG-1, 16 in MPEG-4

    int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);

    if(s->msmpeg4_version) range= 16;

    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;

    h_range= range;

    v_range= field_select_table ? range>>1 : range;

    /* clip / convert to intra 16x16 type MVs */

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

        int x;

        int xy= y*s->mb_stride;

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

            if (s->mb_type[xy] & type){    // RAL: "type" test added...

                if(field_select_table==NULL || field_select_table[xy] == field_select){

                    if(   mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range

                       || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){

                        if(truncate){

                            if     (mv_table[xy][0] > h_range-1) mv_table[xy][0]=  h_range-1;

                            else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;

                            if     (mv_table[xy][1] > v_range-1) mv_table[xy][1]=  v_range-1;

                            else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;

                        }else{

                            s->mb_type[xy] &= ~type;

                            s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;

                            mv_table[xy][0]=

                            mv_table[xy][1]= 0;

                        }

                    }

                }

            }

            xy++;

        }

    }

}


ThreadPool *thread_pool_new(AioContext *ctx)

{

    ThreadPool *pool = g_new(ThreadPool, 1);

    thread_pool_init_one(pool, ctx);

    return pool;

}


static int parse_bootdevices(char *devices)

{

    /* We just do some generic consistency checks */

    const char *p;

    int bitmap = 0;

    for (p = devices; *p != '\0'; p++) {

        /* Allowed boot devices are:

         * a-b: floppy disk drives

         * c-f: IDE disk drives

         * g-m: machine implementation dependant drives

         * n-p: network devices

         * It's up to each machine implementation to check if the given boot

         * devices match the actual hardware implementation and firmware

         * features.

         */

        if (*p < 'a' || *p > 'p') {

            fprintf(stderr, "Invalid boot device '%c'\n", *p);

            exit(1);

        }

        if (bitmap & (1 << (*p - 'a'))) {

            fprintf(stderr, "Boot device '%c' was given twice\n", *p);

            exit(1);

        }

        bitmap |= 1 << (*p - 'a');

    }

    return bitmap;

}


static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,

                           TCGReg arg2, int label_index)

{

    static const MIPSInsn b_zero[16] = {

        [TCG_COND_LT] = OPC_BLTZ,

        [TCG_COND_GT] = OPC_BGTZ,

        [TCG_COND_LE] = OPC_BLEZ,

        [TCG_COND_GE] = OPC_BGEZ,

    };

    TCGLabel *l;

    MIPSInsn s_opc = OPC_SLTU;

    MIPSInsn b_opc;

    int cmp_map;

    switch (cond) {

    case TCG_COND_EQ:

        b_opc = OPC_BEQ;

        break;

    case TCG_COND_NE:

        b_opc = OPC_BNE;

        break;

    case TCG_COND_LT:

    case TCG_COND_GT:

    case TCG_COND_LE:

    case TCG_COND_GE:

        if (arg2 == 0) {

            b_opc = b_zero[cond];

            arg2 = arg1;

            arg1 = 0;

            break;

        }

        s_opc = OPC_SLT;

        /* FALLTHRU */

    case TCG_COND_LTU:

    case TCG_COND_GTU:

    case TCG_COND_LEU:

    case TCG_COND_GEU:

        cmp_map = mips_cmp_map[cond];

        if (cmp_map & MIPS_CMP_SWAP) {

            TCGReg t = arg1;

            arg1 = arg2;

            arg2 = t;

        }

        tcg_out_opc_reg(s, s_opc, TCG_TMP0, arg1, arg2);

        b_opc = (cmp_map & MIPS_CMP_INV ? OPC_BEQ : OPC_BNE);

        arg1 = TCG_TMP0;

        arg2 = TCG_REG_ZERO;

        break;

    default:

        tcg_abort();

        break;

    }

    tcg_out_opc_br(s, b_opc, arg1, arg2);

    l = &s->labels[label_index];

    if (l->has_value) {

        reloc_pc16(s->code_ptr - 1, l->u.value_ptr);

    } else {

        tcg_out_reloc(s, s->code_ptr - 1, R_MIPS_PC16, label_index, 0);

    }

    tcg_out_nop(s);

}


static unsigned int dec_move_rp(DisasContext *dc)

{

	TCGv t[2];

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

	cris_cc_mask(dc, 0);

	t[0] = tcg_temp_new(TCG_TYPE_TL);

	if (dc->op2 == PR_CCS) {

		cris_evaluate_flags(dc);

		t_gen_mov_TN_reg(t[0], dc->op1);

		if (dc->tb_flags & U_FLAG) {

			t[1] = tcg_temp_new(TCG_TYPE_TL);

			/* User space is not allowed to touch all flags.  */

			tcg_gen_andi_tl(t[0], t[0], 0x39f);

			tcg_gen_andi_tl(t[1], cpu_PR[PR_CCS], ~0x39f);

			tcg_gen_or_tl(t[0], t[1], t[0]);

			tcg_temp_free(t[1]);

		}

	}

	else

		t_gen_mov_TN_reg(t[0], dc->op1);

	t_gen_mov_preg_TN(dc, dc->op2, t[0]);

	if (dc->op2 == PR_CCS) {

		cris_update_cc_op(dc, CC_OP_FLAGS, 4);

		dc->flags_uptodate = 1;

	}

	tcg_temp_free(t[0]);

	return 2;

}


static uint32_t get_elf_hwcap(void)

{

    ARMCPU *cpu = ARM_CPU(thread_cpu);

    uint32_t hwcaps = 0;

    hwcaps |= ARM_HWCAP_ARM_SWP;

    hwcaps |= ARM_HWCAP_ARM_HALF;

    hwcaps |= ARM_HWCAP_ARM_THUMB;

    hwcaps |= ARM_HWCAP_ARM_FAST_MULT;

    /* probe for the extra features */

#define GET_FEATURE(feat, hwcap) \

    do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)

    /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */

    GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);

    GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);

    GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);

    GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);

    GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);

    GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);

    GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);

    GET_FEATURE(ARM_FEATURE_VFP4, ARM_HWCAP_ARM_VFPv4);

    GET_FEATURE(ARM_FEATURE_ARM_DIV, ARM_HWCAP_ARM_IDIVA);

    GET_FEATURE(ARM_FEATURE_THUMB_DIV, ARM_HWCAP_ARM_IDIVT);

    /* All QEMU's VFPv3 CPUs have 32 registers, see VFP_DREG in translate.c.

     * Note that the ARM_HWCAP_ARM_VFPv3D16 bit is always the inverse of

     * ARM_HWCAP_ARM_VFPD32 (and so always clear for QEMU); it is unrelated

     * to our VFP_FP16 feature bit.

     */

    GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPD32);

    GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);

#undef GET_FEATURE

    return hwcaps;

}


static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,

                                                const void *unused)

{

    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);

}


BlockAIOCB *bdrv_aio_flush(BlockDriverState *bs,

        BlockCompletionFunc *cb, void *opaque)

{

    trace_bdrv_aio_flush(bs, opaque);

    Coroutine *co;

    BlockAIOCBCoroutine *acb;

    acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);

    acb->need_bh = true;

    acb->req.error = -EINPROGRESS;

    co = qemu_coroutine_create(bdrv_aio_flush_co_entry);

    qemu_coroutine_enter(co, acb);

    bdrv_co_maybe_schedule_bh(acb);

    return &acb->common;

}


xmit_seg(E1000State *s)

{

    uint16_t len;

    unsigned int frames = s->tx.tso_frames, css, sofar;

    struct e1000_tx *tp = &s->tx;

    if (tp->props.tse && tp->props.cptse) {

        css = tp->props.ipcss;

        DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",

               frames, tp->size, css);

        if (tp->props.ip) {    /* IPv4 */

            stw_be_p(tp->data+css+2, tp->size - css);

            stw_be_p(tp->data+css+4,

                     lduw_be_p(tp->data + css + 4) + frames);

        } else {         /* IPv6 */

            stw_be_p(tp->data+css+4, tp->size - css);

        }

        css = tp->props.tucss;

        len = tp->size - css;

        DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", tp->props.tcp, css, len);

        if (tp->props.tcp) {

            sofar = frames * tp->props.mss;

            stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */

            if (tp->props.paylen - sofar > tp->props.mss) {

                tp->data[css + 13] &= ~9;    /* PSH, FIN */

            } else if (frames) {

                e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC);

            }

        } else    /* UDP */

            stw_be_p(tp->data+css+4, len);

        if (tp->props.sum_needed & E1000_TXD_POPTS_TXSM) {

            unsigned int phsum;

            // add pseudo-header length before checksum calculation

            void *sp = tp->data + tp->props.tucso;

            phsum = lduw_be_p(sp) + len;

            phsum = (phsum >> 16) + (phsum & 0xffff);

            stw_be_p(sp, phsum);

        }

        tp->tso_frames++;

    }

    if (tp->props.sum_needed & E1000_TXD_POPTS_TXSM) {

        putsum(tp->data, tp->size, tp->props.tucso,

               tp->props.tucss, tp->props.tucse);

    }

    if (tp->props.sum_needed & E1000_TXD_POPTS_IXSM) {

        putsum(tp->data, tp->size, tp->props.ipcso,

               tp->props.ipcss, tp->props.ipcse);

    }

    if (tp->vlan_needed) {

        memmove(tp->vlan, tp->data, 4);

        memmove(tp->data, tp->data + 4, 8);

        memcpy(tp->data + 8, tp->vlan_header, 4);

        e1000_send_packet(s, tp->vlan, tp->size + 4);

    } else {

        e1000_send_packet(s, tp->data, tp->size);

    }

    e1000x_inc_reg_if_not_full(s->mac_reg, TPT);

    e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size);

    s->mac_reg[GPTC] = s->mac_reg[TPT];

    s->mac_reg[GOTCL] = s->mac_reg[TOTL];

    s->mac_reg[GOTCH] = s->mac_reg[TOTH];

}


int ff_ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile)

{

    int         mbn, blk, num_blocks, num_coeffs, blk_size, scan_pos, run, val,

                pos, is_intra, mc_type, mv_x, mv_y, col_mask;

    uint8_t     col_flags[8];

    int32_t     prev_dc, trvec[64];

    uint32_t    cbp, sym, lo, hi, quant, buf_offs, q;

    IVIMbInfo   *mb;

    RVMapDesc   *rvmap = band->rv_map;

    void (*mc_with_delta_func)(int16_t *buf, const int16_t *ref_buf, uint32_t pitch, int mc_type);

    void (*mc_no_delta_func)  (int16_t *buf, const int16_t *ref_buf, uint32_t pitch, int mc_type);

    const uint16_t  *base_tab;

    const uint8_t   *scale_tab;

    prev_dc = 0; /* init intra prediction for the DC coefficient */

    blk_size   = band->blk_size;

    col_mask   = blk_size - 1; /* column mask for tracking non-zero coeffs */

    num_blocks = (band->mb_size != blk_size) ? 4 : 1; /* number of blocks per mb */

    num_coeffs = blk_size * blk_size;

    if (blk_size == 8) {

        mc_with_delta_func = ff_ivi_mc_8x8_delta;

        mc_no_delta_func   = ff_ivi_mc_8x8_no_delta;

    } else {

        mc_with_delta_func = ff_ivi_mc_4x4_delta;

        mc_no_delta_func   = ff_ivi_mc_4x4_no_delta;

    }

    for (mbn = 0, mb = tile->mbs; mbn < tile->num_MBs; mb++, mbn++) {

        is_intra = !mb->type;

        cbp      = mb->cbp;

        buf_offs = mb->buf_offs;

        quant = av_clip(band->glob_quant + mb->q_delta, 0, 23);

        base_tab  = is_intra ? band->intra_base  : band->inter_base;

        scale_tab = is_intra ? band->intra_scale : band->inter_scale;

        if (scale_tab)

            quant = scale_tab[quant];

        if (!is_intra) {

            mv_x = mb->mv_x;

            mv_y = mb->mv_y;

            if (!band->is_halfpel) {

                mc_type = 0; /* we have only fullpel vectors */

            } else {

                mc_type = ((mv_y & 1) << 1) | (mv_x & 1);

                mv_x >>= 1;

                mv_y >>= 1; /* convert halfpel vectors into fullpel ones */

            }

        }

        for (blk = 0; blk < num_blocks; blk++) {

            /* adjust block position in the buffer according to its number */

            if (blk & 1) {

                buf_offs += blk_size;

            } else if (blk == 2) {

                buf_offs -= blk_size;

                buf_offs += blk_size * band->pitch;

            }

            if (cbp & 1) { /* block coded ? */

                scan_pos = -1;

                memset(trvec, 0, num_coeffs*sizeof(trvec[0])); /* zero transform vector */

                memset(col_flags, 0, sizeof(col_flags));      /* zero column flags */

                while (scan_pos <= num_coeffs) {

                    sym = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);

                    if (sym == rvmap->eob_sym)

                        break; /* End of block */

                    if (sym == rvmap->esc_sym) { /* Escape - run/val explicitly coded using 3 vlc codes */

                        run = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1) + 1;

                        lo  = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);

                        hi  = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);

                        val = IVI_TOSIGNED((hi << 6) | lo); /* merge them and convert into signed val */

                    } else {

                        if (sym >= 256U) {

                            av_log(NULL, AV_LOG_ERROR, "Invalid sym encountered: %d.\n", sym);

                            return -1;

                        }

                        run = rvmap->runtab[sym];

                        val = rvmap->valtab[sym];

                    }

                    /* de-zigzag and dequantize */

                    scan_pos += run;

                    if (scan_pos >= num_coeffs)

                        break;

                    pos = band->scan[scan_pos];

                    if (!val)

                        av_dlog(NULL, "Val = 0 encountered!\n");

                    q = (base_tab[pos] * quant) >> 9;

                    if (q > 1)

                        val = val * q + FFSIGN(val) * (((q ^ 1) - 1) >> 1);

                    trvec[pos] = val;

                    col_flags[pos & col_mask] |= !!val; /* track columns containing non-zero coeffs */

                }// while

                if (scan_pos >= num_coeffs && sym != rvmap->eob_sym)

                    return -1; /* corrupt block data */

                /* undoing DC coeff prediction for intra-blocks */

                if (is_intra && band->is_2d_trans) {

                    prev_dc      += trvec[0];

                    trvec[0]      = prev_dc;

                    col_flags[0] |= !!prev_dc;

                }

                /* apply inverse transform */

                band->inv_transform(trvec, band->buf + buf_offs,

                                    band->pitch, col_flags);

                /* apply motion compensation */

                if (!is_intra)

                    mc_with_delta_func(band->buf + buf_offs,

                                       band->ref_buf + buf_offs + mv_y * band->pitch + mv_x,

                                       band->pitch, mc_type);

            } else {

                /* block not coded */

                /* for intra blocks apply the dc slant transform */

                /* for inter - perform the motion compensation without delta */

                if (is_intra && band->dc_transform) {

                    band->dc_transform(&prev_dc, band->buf + buf_offs,

                                       band->pitch, blk_size);

                } else

                    mc_no_delta_func(band->buf + buf_offs,

                                     band->ref_buf + buf_offs + mv_y * band->pitch + mv_x,

                                     band->pitch, mc_type);

            }

            cbp >>= 1;

        }// for blk

    }// for mbn

    align_get_bits(gb);

    return 0;

}


static void assigned_dev_ioport_write(void *opaque, target_phys_addr_t addr,

                                      uint64_t data, unsigned size)

{

    assigned_dev_ioport_rw(opaque, addr, size, &data);

}


static int kvm_put_fpu(CPUState *env)

{

    struct kvm_fpu fpu;

    int i;

    memset(&fpu, 0, sizeof fpu);

    fpu.fsw = env->fpus & ~(7 << 11);

    fpu.fsw |= (env->fpstt & 7) << 11;

    fpu.fcw = env->fpuc;

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

	fpu.ftwx |= (!env->fptags[i]) << i;

    memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);

    memcpy(fpu.xmm, env->xmm_regs, sizeof env->xmm_regs);

    fpu.mxcsr = env->mxcsr;

    return kvm_vcpu_ioctl(env, KVM_SET_FPU, &fpu);

}


target_read_memory (bfd_vma memaddr,

                    bfd_byte *myaddr,

                    int length,

                    struct disassemble_info *info)

{

    int i;

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

        myaddr[i] = ldub_code(memaddr + i);

    }

    return 0;

}


static int http_connect(URLContext *h, const char *path, const char *hoststr,

                        const char *auth, int *new_location)

{

    HTTPContext *s = h->priv_data;

    int post, err, ch;

    char line[1024], *q;

    char *auth_b64;

    int auth_b64_len = strlen(auth)* 4 / 3 + 12;

    int64_t off = s->off;

    /* send http header */

    post = h->flags & URL_WRONLY;

    auth_b64 = av_malloc(auth_b64_len);

    av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth));

    snprintf(s->buffer, sizeof(s->buffer),

             "%s %s HTTP/1.1\r\n"

             "User-Agent: %s\r\n"

             "Accept: */*\r\n"

             "Range: bytes=%"PRId64"-\r\n"

             "Host: %s\r\n"

             "Authorization: Basic %s\r\n"

             "Connection: close\r\n"

             "\r\n",

             post ? "POST" : "GET",

             path,

             LIBAVFORMAT_IDENT,

             s->off,

             hoststr,

             auth_b64);

    av_freep(&auth_b64);

    if (http_write(h, s->buffer, strlen(s->buffer)) < 0)

        return AVERROR(EIO);

    /* init input buffer */

    s->buf_ptr = s->buffer;

    s->buf_end = s->buffer;

    s->line_count = 0;

    s->off = 0;

    s->filesize = -1;

    if (post) {

        return 0;

    }

    /* wait for header */

    q = line;

    for(;;) {

        ch = http_getc(s);

        if (ch < 0)

            return AVERROR(EIO);

        if (ch == '\n') {

            /* process line */

            if (q > line && q[-1] == '\r')

                q--;

            *q = '\0';

#ifdef DEBUG

            printf("header='%s'\n", line);

#endif

            err = process_line(h, line, s->line_count, new_location);

            if (err < 0)

                return err;

            if (err == 0)

                break;

            s->line_count++;

            q = line;

        } else {

            if ((q - line) < sizeof(line) - 1)

                *q++ = ch;

        }

    }

    return (off == s->off) ? 0 : -1;

}


static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)

{

    VirtConsole *vcon = VIRTIO_CONSOLE(port);

    DeviceState *dev = DEVICE(port);

    if (vcon->chr) {

        qemu_chr_fe_set_open(vcon->chr, guest_connected);

    }

    if (dev->id) {

        qapi_event_send_vserport_change(dev->id, guest_connected,

                                        &error_abort);

    }

}


void error_setg_errno(Error **errp, int os_errno, const char *fmt, ...)

{

    va_list ap;

    char *msg;

    int saved_errno = errno;

    if (errp == NULL) {

        return;

    }

    va_start(ap, fmt);

    error_setv(errp, ERROR_CLASS_GENERIC_ERROR, fmt, ap);

    va_end(ap);

    if (os_errno != 0) {

        msg = (*errp)->msg;

        (*errp)->msg = g_strdup_printf("%s: %s", msg, strerror(os_errno));

        g_free(msg);

    }

    errno = saved_errno;

}


void bitmap_set(unsigned long *map, long start, long nr)
{
    unsigned long *p = map + BIT_WORD(start);
    const long size = start + nr;
    int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
    unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
    while (nr - bits_to_set >= 0) {
        *p |= mask_to_set;
        nr -= bits_to_set;
        bits_to_set = BITS_PER_LONG;
        mask_to_set = ~0UL;
        p++;
    }
    if (nr) {
        mask_to_set &= BITMAP_LAST_WORD_MASK(size);
        *p |= mask_to_set;
    }
}


static void vmdk_free_extents(BlockDriverState *bs)

{

    int i;

    BDRVVmdkState *s = bs->opaque;

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

        g_free(s->extents[i].l1_table);

        g_free(s->extents[i].l2_cache);

        g_free(s->extents[i].l1_backup_table);

    }

    g_free(s->extents);

}


static void console_putchar(TextConsole *s, int ch)

{

    TextCell *c;

    int y1, i;

    int x, y;

    switch(s->state) {

    case TTY_STATE_NORM:

        switch(ch) {

        case '\r':  /* carriage return */

            s->x = 0;

            break;

        case '\n':  /* newline */

            console_put_lf(s);

            break;

        case '\b':  /* backspace */

            if (s->x > 0)

                s->x--;

            break;

        case '\t':  /* tabspace */

            if (s->x + (8 - (s->x % 8)) > s->width) {

                s->x = 0;

                console_put_lf(s);

            } else {

                s->x = s->x + (8 - (s->x % 8));

            }

            break;

        case '\a':  /* alert aka. bell */

            /* TODO: has to be implemented */

            break;

        case 14:

            /* SI (shift in), character set 0 (ignored) */

            break;

        case 15:

            /* SO (shift out), character set 1 (ignored) */

            break;

        case 27:    /* esc (introducing an escape sequence) */

            s->state = TTY_STATE_ESC;

            break;

        default:

            if (s->x >= s->width) {

                /* line wrap */

                s->x = 0;

                console_put_lf(s);

            }

            y1 = (s->y_base + s->y) % s->total_height;

            c = &s->cells[y1 * s->width + s->x];

            c->ch = ch;

            c->t_attrib = s->t_attrib;

            update_xy(s, s->x, s->y);

            s->x++;

            break;

        }

        break;

    case TTY_STATE_ESC: /* check if it is a terminal escape sequence */

        if (ch == '[') {

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

                s->esc_params[i] = 0;

            s->nb_esc_params = 0;

            s->state = TTY_STATE_CSI;

        } else {

            s->state = TTY_STATE_NORM;

        }

        break;

    case TTY_STATE_CSI: /* handle escape sequence parameters */

        if (ch >= '0' && ch <= '9') {

            if (s->nb_esc_params < MAX_ESC_PARAMS) {

                s->esc_params[s->nb_esc_params] =

                    s->esc_params[s->nb_esc_params] * 10 + ch - '0';

            }

        } else {

            s->nb_esc_params++;

            if (ch == ';')

                break;

#ifdef DEBUG_CONSOLE

            fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",

                    s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);

#endif

            s->state = TTY_STATE_NORM;

            switch(ch) {

            case 'A':

                /* move cursor up */

                if (s->esc_params[0] == 0) {

                    s->esc_params[0] = 1;

                }

                s->y -= s->esc_params[0];

                if (s->y < 0) {

                    s->y = 0;

                }

                break;

            case 'B':

                /* move cursor down */

                if (s->esc_params[0] == 0) {

                    s->esc_params[0] = 1;

                }

                s->y += s->esc_params[0];

                if (s->y >= s->height) {

                    s->y = s->height - 1;

                }

                break;

            case 'C':

                /* move cursor right */

                if (s->esc_params[0] == 0) {

                    s->esc_params[0] = 1;

                }

                s->x += s->esc_params[0];

                if (s->x >= s->width) {

                    s->x = s->width - 1;

                }

                break;

            case 'D':

                /* move cursor left */

                if (s->esc_params[0] == 0) {

                    s->esc_params[0] = 1;

                }

                s->x -= s->esc_params[0];

                if (s->x < 0) {

                    s->x = 0;

                }

                break;

            case 'G':

                /* move cursor to column */

                s->x = s->esc_params[0] - 1;

                if (s->x < 0) {

                    s->x = 0;

                }

                break;

            case 'f':

            case 'H':

                /* move cursor to row, column */

                s->x = s->esc_params[1] - 1;

                if (s->x < 0) {

                    s->x = 0;

                }

                s->y = s->esc_params[0] - 1;

                if (s->y < 0) {

                    s->y = 0;

                }

                break;

            case 'J':

                switch (s->esc_params[0]) {

                case 0:

                    /* clear to end of screen */

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

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

                            if (y == s->y && x < s->x) {

                                continue;

                            }

                            console_clear_xy(s, x, y);

                        }

                    }

                    break;

                case 1:

                    /* clear from beginning of screen */

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

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

                            if (y == s->y && x > s->x) {

                                break;

                            }

                            console_clear_xy(s, x, y);

                        }

                    }

                    break;

                case 2:

                    /* clear entire screen */

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

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

                            console_clear_xy(s, x, y);

                        }

                    }

                    break;

                }

                break;

            case 'K':

                switch (s->esc_params[0]) {

                case 0:

                    /* clear to eol */

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

                        console_clear_xy(s, x, s->y);

                    }

                    break;

                case 1:

                    /* clear from beginning of line */

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

                        console_clear_xy(s, x, s->y);

                    }

                    break;

                case 2:

                    /* clear entire line */

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

                        console_clear_xy(s, x, s->y);

                    }

                    break;

                }

                break;

            case 'm':

                console_handle_escape(s);

                break;

            case 'n':

                /* report cursor position */

                /* TODO: send ESC[row;colR */

                break;

            case 's':

                /* save cursor position */

                s->x_saved = s->x;

                s->y_saved = s->y;

                break;

            case 'u':

                /* restore cursor position */

                s->x = s->x_saved;

                s->y = s->y_saved;

                break;

            default:

#ifdef DEBUG_CONSOLE

                fprintf(stderr, "unhandled escape character '%c'\n", ch);

#endif

                break;

            }

            break;

        }

    }

}


static void blkverify_err(BlkverifyAIOCB *acb, const char *fmt, ...)

{

    va_list ap;

    va_start(ap, fmt);

    fprintf(stderr, "blkverify: %s sector_num=%ld nb_sectors=%d ",

            acb->is_write ? "write" : "read", acb->sector_num,

            acb->nb_sectors);

    vfprintf(stderr, fmt, ap);

    fprintf(stderr, "\n");

    va_end(ap);

    exit(1);

}


static void format_line(void *ptr, int level, const char *fmt, va_list vl,

                        char part[3][512], int part_size, int *print_prefix, int type[2])

{

    AVClass* avc = ptr ? *(AVClass **) ptr : NULL;

    part[0][0] = part[1][0] = part[2][0] = 0;

    if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;

    if (*print_prefix && avc) {

        if (avc->parent_log_context_offset) {

            AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +

                                   avc->parent_log_context_offset);

            if (parent && *parent) {

                snprintf(part[0], part_size, "[%s @ %p] ",

                         (*parent)->item_name(parent), parent);

                if(type) type[0] = get_category(((uint8_t *) ptr) + avc->parent_log_context_offset);

            }

        }

        snprintf(part[1], part_size, "[%s @ %p] ",

                 avc->item_name(ptr), ptr);

        if(type) type[1] = get_category(ptr);

    }

    vsnprintf(part[2], part_size, fmt, vl);

    *print_prefix = strlen(part[2]) && part[2][strlen(part[2]) - 1] == '\n';

}


find_c_packed_planar_out_funcs(SwsContext *c,

                               yuv2planar1_fn *yuv2yuv1,    yuv2planarX_fn *yuv2yuvX,

                               yuv2packed1_fn *yuv2packed1, yuv2packed2_fn *yuv2packed2,

                               yuv2packedX_fn *yuv2packedX)

{

    enum PixelFormat dstFormat = c->dstFormat;

    if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21) {

        *yuv2yuvX     = yuv2nv12X_c;

    } else if (is16BPS(dstFormat)) {

        *yuv2yuvX     = isBE(dstFormat) ? yuv2yuvX16BE_c  : yuv2yuvX16LE_c;

    } else if (is9_OR_10BPS(dstFormat)) {

        if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {

            *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX9BE_c :  yuv2yuvX9LE_c;

        } else {

            *yuv2yuvX = isBE(dstFormat) ? yuv2yuvX10BE_c : yuv2yuvX10LE_c;

        }

    } else {

        *yuv2yuv1     = yuv2yuv1_c;

        *yuv2yuvX     = yuv2yuvX_c;

    }

    if(c->flags & SWS_FULL_CHR_H_INT) {

        switch (dstFormat) {

            case PIX_FMT_RGBA:

#if CONFIG_SMALL

                *yuv2packedX = yuv2rgba32_full_X_c;

#else

#if CONFIG_SWSCALE_ALPHA

                if (c->alpPixBuf) {

                    *yuv2packedX = yuv2rgba32_full_X_c;

                } else

#endif /* CONFIG_SWSCALE_ALPHA */

                {

                    *yuv2packedX = yuv2rgbx32_full_X_c;

                }

#endif /* !CONFIG_SMALL */

                break;

            case PIX_FMT_ARGB:

#if CONFIG_SMALL

                *yuv2packedX = yuv2argb32_full_X_c;

#else

#if CONFIG_SWSCALE_ALPHA

                if (c->alpPixBuf) {

                    *yuv2packedX = yuv2argb32_full_X_c;

                } else

#endif /* CONFIG_SWSCALE_ALPHA */

                {

                    *yuv2packedX = yuv2xrgb32_full_X_c;

                }

#endif /* !CONFIG_SMALL */

                break;

            case PIX_FMT_BGRA:

#if CONFIG_SMALL

                *yuv2packedX = yuv2bgra32_full_X_c;

#else

#if CONFIG_SWSCALE_ALPHA

                if (c->alpPixBuf) {

                    *yuv2packedX = yuv2bgra32_full_X_c;

                } else

#endif /* CONFIG_SWSCALE_ALPHA */

                {

                    *yuv2packedX = yuv2bgrx32_full_X_c;

                }

#endif /* !CONFIG_SMALL */

                break;

            case PIX_FMT_ABGR:

#if CONFIG_SMALL

                *yuv2packedX = yuv2abgr32_full_X_c;

#else

#if CONFIG_SWSCALE_ALPHA

                if (c->alpPixBuf) {

                    *yuv2packedX = yuv2abgr32_full_X_c;

                } else

#endif /* CONFIG_SWSCALE_ALPHA */

                {

                    *yuv2packedX = yuv2xbgr32_full_X_c;

                }

#endif /* !CONFIG_SMALL */

                break;

            case PIX_FMT_RGB24:

            *yuv2packedX = yuv2rgb24_full_X_c;

            break;

        case PIX_FMT_BGR24:

            *yuv2packedX = yuv2bgr24_full_X_c;

            break;

        }

    } else {

        switch (dstFormat) {

        case PIX_FMT_GRAY16BE:

            *yuv2packed1 = yuv2gray16BE_1_c;

            *yuv2packed2 = yuv2gray16BE_2_c;

            *yuv2packedX = yuv2gray16BE_X_c;

            break;

        case PIX_FMT_GRAY16LE:

            *yuv2packed1 = yuv2gray16LE_1_c;

            *yuv2packed2 = yuv2gray16LE_2_c;

            *yuv2packedX = yuv2gray16LE_X_c;

            break;

        case PIX_FMT_MONOWHITE:

            *yuv2packed1 = yuv2monowhite_1_c;

            *yuv2packed2 = yuv2monowhite_2_c;

            *yuv2packedX = yuv2monowhite_X_c;

            break;

        case PIX_FMT_MONOBLACK:

            *yuv2packed1 = yuv2monoblack_1_c;

            *yuv2packed2 = yuv2monoblack_2_c;

            *yuv2packedX = yuv2monoblack_X_c;

            break;

        case PIX_FMT_YUYV422:

            *yuv2packed1 = yuv2yuyv422_1_c;

            *yuv2packed2 = yuv2yuyv422_2_c;

            *yuv2packedX = yuv2yuyv422_X_c;

            break;

        case PIX_FMT_UYVY422:

            *yuv2packed1 = yuv2uyvy422_1_c;

            *yuv2packed2 = yuv2uyvy422_2_c;

            *yuv2packedX = yuv2uyvy422_X_c;

            break;

        case PIX_FMT_RGB48LE:

            //*yuv2packed1 = yuv2rgb48le_1_c;

            //*yuv2packed2 = yuv2rgb48le_2_c;

            //*yuv2packedX = yuv2rgb48le_X_c;

            //break;

        case PIX_FMT_RGB48BE:

            *yuv2packed1 = yuv2rgb48be_1_c;

            *yuv2packed2 = yuv2rgb48be_2_c;

            *yuv2packedX = yuv2rgb48be_X_c;

            break;

        case PIX_FMT_BGR48LE:

            //*yuv2packed1 = yuv2bgr48le_1_c;

            //*yuv2packed2 = yuv2bgr48le_2_c;

            //*yuv2packedX = yuv2bgr48le_X_c;

            //break;

        case PIX_FMT_BGR48BE:

            *yuv2packed1 = yuv2bgr48be_1_c;

            *yuv2packed2 = yuv2bgr48be_2_c;

            *yuv2packedX = yuv2bgr48be_X_c;

            break;

        case PIX_FMT_RGB32:

        case PIX_FMT_BGR32:

#if CONFIG_SMALL

            *yuv2packed1 = yuv2rgb32_1_c;

            *yuv2packed2 = yuv2rgb32_2_c;

            *yuv2packedX = yuv2rgb32_X_c;

#else

#if CONFIG_SWSCALE_ALPHA

                if (c->alpPixBuf) {

                    *yuv2packed1 = yuv2rgba32_1_c;

                    *yuv2packed2 = yuv2rgba32_2_c;

                    *yuv2packedX = yuv2rgba32_X_c;

                } else

#endif /* CONFIG_SWSCALE_ALPHA */

                {

                    *yuv2packed1 = yuv2rgbx32_1_c;

                    *yuv2packed2 = yuv2rgbx32_2_c;

                    *yuv2packedX = yuv2rgbx32_X_c;

                }

#endif /* !CONFIG_SMALL */

            break;

        case PIX_FMT_RGB32_1:

        case PIX_FMT_BGR32_1:

#if CONFIG_SMALL

                *yuv2packed1 = yuv2rgb32_1_1_c;

                *yuv2packed2 = yuv2rgb32_1_2_c;

                *yuv2packedX = yuv2rgb32_1_X_c;

#else

#if CONFIG_SWSCALE_ALPHA

                if (c->alpPixBuf) {

                    *yuv2packed1 = yuv2rgba32_1_1_c;

                    *yuv2packed2 = yuv2rgba32_1_2_c;

                    *yuv2packedX = yuv2rgba32_1_X_c;

                } else

#endif /* CONFIG_SWSCALE_ALPHA */

                {

                    *yuv2packed1 = yuv2rgbx32_1_1_c;

                    *yuv2packed2 = yuv2rgbx32_1_2_c;

                    *yuv2packedX = yuv2rgbx32_1_X_c;

                }

#endif /* !CONFIG_SMALL */

                break;

        case PIX_FMT_RGB24:

            *yuv2packed1 = yuv2rgb24_1_c;

            *yuv2packed2 = yuv2rgb24_2_c;

            *yuv2packedX = yuv2rgb24_X_c;

            break;

        case PIX_FMT_BGR24:

            *yuv2packed1 = yuv2bgr24_1_c;

            *yuv2packed2 = yuv2bgr24_2_c;

            *yuv2packedX = yuv2bgr24_X_c;

            break;

        case PIX_FMT_RGB565:

        case PIX_FMT_BGR565:

            *yuv2packed1 = yuv2rgb16_1_c;

            *yuv2packed2 = yuv2rgb16_2_c;

            *yuv2packedX = yuv2rgb16_X_c;

            break;

        case PIX_FMT_RGB555:

        case PIX_FMT_BGR555:

            *yuv2packed1 = yuv2rgb15_1_c;

            *yuv2packed2 = yuv2rgb15_2_c;

            *yuv2packedX = yuv2rgb15_X_c;

            break;

        case PIX_FMT_RGB444:

        case PIX_FMT_BGR444:

            *yuv2packed1 = yuv2rgb12_1_c;

            *yuv2packed2 = yuv2rgb12_2_c;

            *yuv2packedX = yuv2rgb12_X_c;

            break;

        case PIX_FMT_RGB8:

        case PIX_FMT_BGR8:

            *yuv2packed1 = yuv2rgb8_1_c;

            *yuv2packed2 = yuv2rgb8_2_c;

            *yuv2packedX = yuv2rgb8_X_c;

            break;

        case PIX_FMT_RGB4:

        case PIX_FMT_BGR4:

            *yuv2packed1 = yuv2rgb4_1_c;

            *yuv2packed2 = yuv2rgb4_2_c;

            *yuv2packedX = yuv2rgb4_X_c;

            break;

        case PIX_FMT_RGB4_BYTE:

        case PIX_FMT_BGR4_BYTE:

            *yuv2packed1 = yuv2rgb4b_1_c;

            *yuv2packed2 = yuv2rgb4b_2_c;

            *yuv2packedX = yuv2rgb4b_X_c;

            break;

        }

    }

}


static av_always_inline void h264_filter_mb_fast_internal(H264Context *h,

                                                          int mb_x, int mb_y,

                                                          uint8_t *img_y,

                                                          uint8_t *img_cb,

                                                          uint8_t *img_cr,

                                                          unsigned int linesize,

                                                          unsigned int uvlinesize,

                                                          int pixel_shift)

{

    int chroma = !(CONFIG_GRAY && (h->flags&CODEC_FLAG_GRAY));

    int chroma444 = CHROMA444(h);

    int chroma422 = CHROMA422(h);

    int mb_xy = h->mb_xy;

    int left_type= h->left_type[LTOP];

    int top_type= h->top_type;

    int qp_bd_offset = 6 * (h->sps.bit_depth_luma - 8);

    int a = h->slice_alpha_c0_offset - qp_bd_offset;

    int b = h->slice_beta_offset - qp_bd_offset;

    int mb_type = h->cur_pic.mb_type[mb_xy];

    int qp      = h->cur_pic.qscale_table[mb_xy];

    int qp0     = h->cur_pic.qscale_table[mb_xy - 1];

    int qp1     = h->cur_pic.qscale_table[h->top_mb_xy];

    int qpc = get_chroma_qp( h, 0, qp );

    int qpc0 = get_chroma_qp( h, 0, qp0 );

    int qpc1 = get_chroma_qp( h, 0, qp1 );

    qp0 = (qp + qp0 + 1) >> 1;

    qp1 = (qp + qp1 + 1) >> 1;

    qpc0 = (qpc + qpc0 + 1) >> 1;

    qpc1 = (qpc + qpc1 + 1) >> 1;

    if( IS_INTRA(mb_type) ) {

        static const int16_t bS4[4] = {4,4,4,4};

        static const int16_t bS3[4] = {3,3,3,3};

        const int16_t *bSH = FIELD_PICTURE(h) ? bS3 : bS4;

        if(left_type)

            filter_mb_edgev( &img_y[4*0<<pixel_shift], linesize, bS4, qp0, a, b, h, 1);

        if( IS_8x8DCT(mb_type) ) {

            filter_mb_edgev( &img_y[4*2<<pixel_shift], linesize, bS3, qp, a, b, h, 0);

            if(top_type){

                filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, a, b, h, 1);

            }

            filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, a, b, h, 0);

        } else {

            filter_mb_edgev( &img_y[4*1<<pixel_shift], linesize, bS3, qp, a, b, h, 0);

            filter_mb_edgev( &img_y[4*2<<pixel_shift], linesize, bS3, qp, a, b, h, 0);

            filter_mb_edgev( &img_y[4*3<<pixel_shift], linesize, bS3, qp, a, b, h, 0);

            if(top_type){

                filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, a, b, h, 1);

            }

            filter_mb_edgeh( &img_y[4*1*linesize], linesize, bS3, qp, a, b, h, 0);

            filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, a, b, h, 0);

            filter_mb_edgeh( &img_y[4*3*linesize], linesize, bS3, qp, a, b, h, 0);

        }

        if(chroma){

            if(chroma444){

                if(left_type){

                    filter_mb_edgev( &img_cb[4*0<<pixel_shift], linesize, bS4, qpc0, a, b, h, 1);

                    filter_mb_edgev( &img_cr[4*0<<pixel_shift], linesize, bS4, qpc0, a, b, h, 1);

                }

                if( IS_8x8DCT(mb_type) ) {

                    filter_mb_edgev( &img_cb[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgev( &img_cr[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);

                    if(top_type){

                        filter_mb_edgeh( &img_cb[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1 );

                        filter_mb_edgeh( &img_cr[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1 );

                    }

                    filter_mb_edgeh( &img_cb[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgeh( &img_cr[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);

                } else {

                    filter_mb_edgev( &img_cb[4*1<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgev( &img_cr[4*1<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgev( &img_cb[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgev( &img_cr[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgev( &img_cb[4*3<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgev( &img_cr[4*3<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);

                    if(top_type){

                        filter_mb_edgeh( &img_cb[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1);

                        filter_mb_edgeh( &img_cr[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1);

                    }

                    filter_mb_edgeh( &img_cb[4*1*linesize], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgeh( &img_cr[4*1*linesize], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgeh( &img_cb[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgeh( &img_cr[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgeh( &img_cb[4*3*linesize], linesize, bS3, qpc, a, b, h, 0);

                    filter_mb_edgeh( &img_cr[4*3*linesize], linesize, bS3, qpc, a, b, h, 0);

                }

            }else if(chroma422){

                if(left_type){

                    filter_mb_edgecv(&img_cb[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);

                    filter_mb_edgecv(&img_cr[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);

                }

                filter_mb_edgecv(&img_cb[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);

                filter_mb_edgecv(&img_cr[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);

                if(top_type){

                    filter_mb_edgech(&img_cb[4*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);

                    filter_mb_edgech(&img_cr[4*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);

                }

                filter_mb_edgech(&img_cb[4*1*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);

                filter_mb_edgech(&img_cr[4*1*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);

                filter_mb_edgech(&img_cb[4*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);

                filter_mb_edgech(&img_cr[4*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);

                filter_mb_edgech(&img_cb[4*3*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);

                filter_mb_edgech(&img_cr[4*3*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);

            }else{

                if(left_type){

                    filter_mb_edgecv( &img_cb[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);

                    filter_mb_edgecv( &img_cr[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);

                }

                filter_mb_edgecv( &img_cb[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);

                filter_mb_edgecv( &img_cr[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);

                if(top_type){

                    filter_mb_edgech( &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);

                    filter_mb_edgech( &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);

                }

                filter_mb_edgech( &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);

                filter_mb_edgech( &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);

            }

        }

        return;

    } else {

        LOCAL_ALIGNED_8(int16_t, bS, [2], [4][4]);

        int edges;

        if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 && !chroma444 ) {

            edges = 4;

            AV_WN64A(bS[0][0], 0x0002000200020002ULL);

            AV_WN64A(bS[0][2], 0x0002000200020002ULL);

            AV_WN64A(bS[1][0], 0x0002000200020002ULL);

            AV_WN64A(bS[1][2], 0x0002000200020002ULL);

        } else {

            int mask_edge1 = (3*(((5*mb_type)>>5)&1)) | (mb_type>>4); //(mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 : (mb_type & MB_TYPE_16x8) ? 1 : 0;

            int mask_edge0 = 3*((mask_edge1>>1) & ((5*left_type)>>5)&1); // (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) && (h->left_type[LTOP] & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 : 0;

            int step =  1+(mb_type>>24); //IS_8x8DCT(mb_type) ? 2 : 1;

            edges = 4 - 3*((mb_type>>3) & !(h->cbp & 15)); //(mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;

            h->h264dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,

                                              h->list_count==2, edges, step, mask_edge0, mask_edge1, FIELD_PICTURE(h));

        }

        if( IS_INTRA(left_type) )

            AV_WN64A(bS[0][0], 0x0004000400040004ULL);

        if( IS_INTRA(top_type) )

            AV_WN64A(bS[1][0], FIELD_PICTURE(h) ? 0x0003000300030003ULL : 0x0004000400040004ULL);

#define FILTER(hv,dir,edge,intra)\

        if(AV_RN64A(bS[dir][edge])) {                                   \

            filter_mb_edge##hv( &img_y[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qp : qp##dir, a, b, h, intra );\

            if(chroma){\

                if(chroma444){\

                    filter_mb_edge##hv( &img_cb[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\

                    filter_mb_edge##hv( &img_cr[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\

                } else if(!(edge&1)) {\

                    filter_mb_edgec##hv( &img_cb[2*edge*(dir?uvlinesize:1<<pixel_shift)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\

                    filter_mb_edgec##hv( &img_cr[2*edge*(dir?uvlinesize:1<<pixel_shift)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\

                }\

            }\

        }

        if(left_type)

            FILTER(v,0,0,1);

        if( edges == 1 ) {

            if(top_type)

                FILTER(h,1,0,1);

        } else if( IS_8x8DCT(mb_type) ) {

            FILTER(v,0,2,0);

            if(top_type)

                FILTER(h,1,0,1);

            FILTER(h,1,2,0);

        } else {

            FILTER(v,0,1,0);

            FILTER(v,0,2,0);

            FILTER(v,0,3,0);

            if(top_type)

                FILTER(h,1,0,1);

            FILTER(h,1,1,0);

            FILTER(h,1,2,0);

            FILTER(h,1,3,0);

        }

#undef FILTER

    }

}


static int h263_decode_init(AVCodecContext *avctx)

{

    MpegEncContext *s = avctx->priv_data;

    int i;

    s->avctx = avctx;

    s->out_format = FMT_H263;

    s->width = avctx->width;

    s->height = avctx->height;

    /* select sub codec */

    switch(avctx->codec->id) {

    case CODEC_ID_H263:

        s->gob_number = 0;

        s->first_gob_line = 0;

        break;

    case CODEC_ID_MPEG4:

        s->time_increment_bits = 4; /* default value for broken headers */

        s->h263_pred = 1;

        s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing

        break;

    case CODEC_ID_MSMPEG4V1:

        s->h263_msmpeg4 = 1;

        s->h263_pred = 1;

        s->msmpeg4_version=1;

        break;

    case CODEC_ID_MSMPEG4V2:

        s->h263_msmpeg4 = 1;

        s->h263_pred = 1;

        s->msmpeg4_version=2;

        break;

    case CODEC_ID_MSMPEG4V3:

        s->h263_msmpeg4 = 1;

        s->h263_pred = 1;

        s->msmpeg4_version=3;

        break;

    case CODEC_ID_WMV1:

        s->h263_msmpeg4 = 1;

        s->h263_pred = 1;

        s->msmpeg4_version=4;

        break;

    case CODEC_ID_H263I:

        s->h263_intel = 1;

        break;

    default:

        return -1;

    }

    /* for h263, we allocate the images after having read the header */

    if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)

        if (MPV_common_init(s) < 0)

            return -1;

    /* XXX: suppress this matrix init, only needed because using mpeg1

       dequantize in mmx case */

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

        s->non_intra_matrix[i] = default_non_intra_matrix[i];

    if (s->h263_msmpeg4)

        msmpeg4_decode_init_vlc(s);

    else

        h263_decode_init_vlc(s);

    return 0;

}


static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)

{

    IEC958Context *ctx = s->priv_data;

    int ret, padding;

    ctx->out_bytes = pkt->size;

    ctx->length_code = FFALIGN(pkt->size, 2) << 3;

    ret = ctx->header_info(s, pkt);

    if (ret < 0)

        return -1;

    if (!ctx->pkt_offset)

        return 0;

    padding = (ctx->pkt_offset - BURST_HEADER_SIZE - ctx->out_bytes) >> 1;

    if (padding < 0) {

        av_log(s, AV_LOG_ERROR, "bitrate is too high\n");

        return -1;

    }

    put_le16(s->pb, SYNCWORD1);      //Pa

    put_le16(s->pb, SYNCWORD2);      //Pb

    put_le16(s->pb, ctx->data_type); //Pc

    put_le16(s->pb, ctx->length_code);//Pd

#if HAVE_BIGENDIAN

    put_buffer(s->pb, ctx->out_buf, ctx->out_bytes & ~1);

#else

    av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + FF_INPUT_BUFFER_PADDING_SIZE);

    if (!ctx->buffer)

        return AVERROR(ENOMEM);

    ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);

    put_buffer(s->pb, ctx->buffer, ctx->out_bytes & ~1);

#endif

    if (ctx->out_bytes & 1)

        put_be16(s->pb, ctx->out_buf[ctx->out_bytes - 1]);

    for (; padding > 0; padding--)

        put_be16(s->pb, 0);

    av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",

           ctx->data_type, ctx->out_bytes, ctx->pkt_offset);

    put_flush_packet(s->pb);

    return 0;

}


static void blkverify_refresh_filename(BlockDriverState *bs, QDict *options)

{

    BDRVBlkverifyState *s = bs->opaque;

    /* bs->file->bs has already been refreshed */

    bdrv_refresh_filename(s->test_file->bs);

    if (bs->file->bs->full_open_options

        && s->test_file->bs->full_open_options)

    {

        QDict *opts = qdict_new();

        qdict_put_str(opts, "driver", "blkverify");

        QINCREF(bs->file->bs->full_open_options);

        qdict_put(opts, "raw", bs->file->bs->full_open_options);

        QINCREF(s->test_file->bs->full_open_options);

        qdict_put(opts, "test", s->test_file->bs->full_open_options);

        bs->full_open_options = opts;

    }

    if (bs->file->bs->exact_filename[0]

        && s->test_file->bs->exact_filename[0])

    {

        snprintf(bs->exact_filename, sizeof(bs->exact_filename),

                 "blkverify:%s:%s",

                 bs->file->bs->exact_filename,

                 s->test_file->bs->exact_filename);

    }

}


static int ffmmal_read_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)

{

    MMALDecodeContext *ctx = avctx->priv_data;

    MMAL_BUFFER_HEADER_T *buffer = NULL;

    MMAL_STATUS_T status = 0;

    int ret = 0;

    if (ctx->eos_received)

        goto done;

    while (1) {

        // To ensure decoding in lockstep with a constant delay between fed packets

        // and output frames, we always wait until an output buffer is available.

        // Except during start we don't know after how many input packets the decoder

        // is going to return the first buffer, and we can't distinguish decoder

        // being busy from decoder waiting for input. So just poll at the start and

        // keep feeding new data to the buffer.

        // We are pretty sure the decoder will produce output if we sent more input

        // frames than what a h264 decoder could logically delay. This avoids too

        // excessive buffering.

        // We also wait if we sent eos, but didn't receive it yet (think of decoding

        // stream with a very low number of frames).

        if (ctx->frames_output || ctx->packets_sent > MAX_DELAYED_FRAMES || ctx->eos_sent) {

            buffer = mmal_queue_wait(ctx->queue_decoded_frames);

        } else {

            buffer = mmal_queue_get(ctx->queue_decoded_frames);

        }

        if (!buffer)

            goto done;

        ctx->eos_received |= !!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS);

        if (ctx->eos_received)

            goto done;

        if (buffer->cmd == MMAL_EVENT_FORMAT_CHANGED) {

            MMAL_COMPONENT_T *decoder = ctx->decoder;

            MMAL_EVENT_FORMAT_CHANGED_T *ev = mmal_event_format_changed_get(buffer);

            MMAL_BUFFER_HEADER_T *stale_buffer;

            av_log(avctx, AV_LOG_INFO, "Changing output format.\n");

            if ((status = mmal_port_disable(decoder->output[0])))

                goto done;

            while ((stale_buffer = mmal_queue_get(ctx->queue_decoded_frames)))

                mmal_buffer_header_release(stale_buffer);

            mmal_format_copy(decoder->output[0]->format, ev->format);

            if ((ret = ffmal_update_format(avctx)) < 0)

                goto done;

            if ((status = mmal_port_enable(decoder->output[0], output_callback)))

                goto done;

            if ((ret = ffmmal_fill_output_port(avctx)) < 0)

                goto done;

            if ((ret = ffmmal_fill_input_port(avctx)) < 0)

                goto done;

            mmal_buffer_header_release(buffer);

            continue;

        } else if (buffer->cmd) {

            char s[20];

            av_get_codec_tag_string(s, sizeof(s), buffer->cmd);

            av_log(avctx, AV_LOG_WARNING, "Unknown MMAL event %s on output port\n", s);

            goto done;

        } else if (buffer->length == 0) {

            // Unused output buffer that got drained after format change.

            mmal_buffer_header_release(buffer);

            continue;

        }

        ctx->frames_output++;

        if ((ret = ffmal_copy_frame(avctx, frame, buffer)) < 0)

            goto done;

        *got_frame = 1;

        break;

    }

done:

    if (buffer)

        mmal_buffer_header_release(buffer);

    if (status && ret >= 0)

        ret = AVERROR_UNKNOWN;

    return ret;

}


void ff_sbrdsp_init_x86(SBRDSPContext *s)

{

    if (HAVE_YASM) {

        int mm_flags = av_get_cpu_flags();

        if (mm_flags & AV_CPU_FLAG_SSE) {

            s->sum_square = ff_sbr_sum_square_sse;

            s->hf_g_filt = ff_sbr_hf_g_filt_sse;

        }

    }

}


static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)

{

    struct usbdevfs_urb *urb;

    AsyncURB *aurb;

    int ret, value, index;

    /*

     * Process certain standard device requests.

     * These are infrequent and are processed synchronously.

     */

    value = le16_to_cpu(s->ctrl.req.wValue);

    index = le16_to_cpu(s->ctrl.req.wIndex);

    dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",

        s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,

        s->ctrl.len);

    if (s->ctrl.req.bRequestType == 0) {

        switch (s->ctrl.req.bRequest) {

        case USB_REQ_SET_ADDRESS:

            return usb_host_set_address(s, value);

        case USB_REQ_SET_CONFIGURATION:

            return usb_host_set_config(s, value & 0xff);

        }

    }

    if (s->ctrl.req.bRequestType == 1 &&

                  s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)

        return usb_host_set_interface(s, index, value);

    /* The rest are asynchronous */

    aurb = async_alloc();

    aurb->hdev   = s;

    aurb->packet = p;

    /*

     * Setup ctrl transfer.

     *

     * s->ctrl is layed out such that data buffer immediately follows

     * 'req' struct which is exactly what usbdevfs expects.

     */

    urb = &aurb->urb;

    urb->type     = USBDEVFS_URB_TYPE_CONTROL;

    urb->endpoint = p->devep;

    urb->buffer        = &s->ctrl.req;

    urb->buffer_length = 8 + s->ctrl.len;

    urb->usercontext = s;

    ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);

    dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);

    if (ret < 0) {

        dprintf("husb: submit failed. errno %d\n", errno);

        async_free(aurb);

        switch(errno) {

        case ETIMEDOUT:

            return USB_RET_NAK;

        case EPIPE:

        default:

            return USB_RET_STALL;

        }

    }

    usb_defer_packet(p, async_cancel, aurb);

    return USB_RET_ASYNC;

}


static void pc_init1(MachineState *machine)

{

    PCMachineState *pc_machine = PC_MACHINE(machine);

    MemoryRegion *system_memory = get_system_memory();

    MemoryRegion *system_io = get_system_io();

    int i;

    ram_addr_t below_4g_mem_size, above_4g_mem_size;

    PCIBus *pci_bus;

    ISABus *isa_bus;

    PCII440FXState *i440fx_state;

    int piix3_devfn = -1;

    qemu_irq *cpu_irq;

    qemu_irq *gsi;

    qemu_irq *i8259;

    qemu_irq *smi_irq;

    GSIState *gsi_state;

    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];

    BusState *idebus[MAX_IDE_BUS];

    ISADevice *rtc_state;

    ISADevice *floppy;

    MemoryRegion *ram_memory;

    MemoryRegion *pci_memory;

    MemoryRegion *rom_memory;

    DeviceState *icc_bridge;

    FWCfgState *fw_cfg = NULL;

    PcGuestInfo *guest_info;

    ram_addr_t lowmem;

    /* Check whether RAM fits below 4G (leaving 1/2 GByte for IO memory).

     * If it doesn't, we need to split it in chunks below and above 4G.

     * In any case, try to make sure that guest addresses aligned at

     * 1G boundaries get mapped to host addresses aligned at 1G boundaries.

     * For old machine types, use whatever split we used historically to avoid

     * breaking migration.

     */

    if (machine->ram_size >= 0xe0000000) {

        lowmem = gigabyte_align ? 0xc0000000 : 0xe0000000;

    } else {

        lowmem = 0xe0000000;

    }

    /* Handle the machine opt max-ram-below-4g.  It is basically doing

     * min(qemu limit, user limit).

     */

    if (lowmem > pc_machine->max_ram_below_4g) {

        lowmem = pc_machine->max_ram_below_4g;

        if (machine->ram_size - lowmem > lowmem &&

            lowmem & ((1ULL << 30) - 1)) {

            error_report("Warning: Large machine and max_ram_below_4g(%"PRIu64

                         ") not a multiple of 1G; possible bad performance.",

                         pc_machine->max_ram_below_4g);

        }

    }

    if (machine->ram_size >= lowmem) {

        above_4g_mem_size = machine->ram_size - lowmem;

        below_4g_mem_size = lowmem;

    } else {

        above_4g_mem_size = 0;

        below_4g_mem_size = machine->ram_size;

    }

    if (xen_enabled() && xen_hvm_init(&below_4g_mem_size, &above_4g_mem_size,

                                      &ram_memory) != 0) {

        fprintf(stderr, "xen hardware virtual machine initialisation failed\n");

        exit(1);

    }

    icc_bridge = qdev_create(NULL, TYPE_ICC_BRIDGE);

    object_property_add_child(qdev_get_machine(), "icc-bridge",

                              OBJECT(icc_bridge), NULL);

    pc_cpus_init(machine->cpu_model, icc_bridge);

    if (kvm_enabled() && kvmclock_enabled) {

        kvmclock_create();

    }

    if (pci_enabled) {

        pci_memory = g_new(MemoryRegion, 1);

        memory_region_init(pci_memory, NULL, "pci", UINT64_MAX);

        rom_memory = pci_memory;

    } else {

        pci_memory = NULL;

        rom_memory = system_memory;

    }

    guest_info = pc_guest_info_init(below_4g_mem_size, above_4g_mem_size);

    guest_info->has_acpi_build = has_acpi_build;

    guest_info->legacy_acpi_table_size = legacy_acpi_table_size;

    guest_info->isapc_ram_fw = !pci_enabled;

    guest_info->has_reserved_memory = has_reserved_memory;

    guest_info->rsdp_in_ram = rsdp_in_ram;

    if (smbios_defaults) {

        MachineClass *mc = MACHINE_GET_CLASS(machine);

        /* These values are guest ABI, do not change */

        smbios_set_defaults("QEMU", "Standard PC (i440FX + PIIX, 1996)",

                            mc->name, smbios_legacy_mode, smbios_uuid_encoded);

    }

    /* allocate ram and load rom/bios */

    if (!xen_enabled()) {

        fw_cfg = pc_memory_init(machine, system_memory,

                                below_4g_mem_size, above_4g_mem_size,

                                rom_memory, &ram_memory, guest_info);

    } else if (machine->kernel_filename != NULL) {

        /* For xen HVM direct kernel boot, load linux here */

        fw_cfg = xen_load_linux(machine->kernel_filename,

                                machine->kernel_cmdline,

                                machine->initrd_filename,

                                below_4g_mem_size,

                                guest_info);

    }

    gsi_state = g_malloc0(sizeof(*gsi_state));

    if (kvm_irqchip_in_kernel()) {

        kvm_pc_setup_irq_routing(pci_enabled);

        gsi = qemu_allocate_irqs(kvm_pc_gsi_handler, gsi_state,

                                 GSI_NUM_PINS);

    } else {

        gsi = qemu_allocate_irqs(gsi_handler, gsi_state, GSI_NUM_PINS);

    }

    if (pci_enabled) {

        pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, &isa_bus, gsi,

                              system_memory, system_io, machine->ram_size,

                              below_4g_mem_size,

                              above_4g_mem_size,

                              pci_memory, ram_memory);

    } else {

        pci_bus = NULL;

        i440fx_state = NULL;

        isa_bus = isa_bus_new(NULL, get_system_memory(), system_io);

        no_hpet = 1;

    }

    isa_bus_irqs(isa_bus, gsi);

    if (kvm_irqchip_in_kernel()) {

        i8259 = kvm_i8259_init(isa_bus);

    } else if (xen_enabled()) {

        i8259 = xen_interrupt_controller_init();

    } else {

        cpu_irq = pc_allocate_cpu_irq();

        i8259 = i8259_init(isa_bus, cpu_irq[0]);

    }

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

        gsi_state->i8259_irq[i] = i8259[i];

    }

    if (pci_enabled) {

        ioapic_init_gsi(gsi_state, "i440fx");

    }

    qdev_init_nofail(icc_bridge);

    pc_register_ferr_irq(gsi[13]);

    pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);

    assert(pc_machine->vmport != ON_OFF_AUTO_MAX);

    if (pc_machine->vmport == ON_OFF_AUTO_AUTO) {

        pc_machine->vmport = xen_enabled() ? ON_OFF_AUTO_OFF : ON_OFF_AUTO_ON;

    }

    /* init basic PC hardware */

    pc_basic_device_init(isa_bus, gsi, &rtc_state, true, &floppy,

                         (pc_machine->vmport != ON_OFF_AUTO_ON), 0x4);

    pc_nic_init(isa_bus, pci_bus);

    ide_drive_get(hd, ARRAY_SIZE(hd));

    if (pci_enabled) {

        PCIDevice *dev;

        if (xen_enabled()) {

            dev = pci_piix3_xen_ide_init(pci_bus, hd, piix3_devfn + 1);

        } else {

            dev = pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);

        }

        idebus[0] = qdev_get_child_bus(&dev->qdev, "ide.0");

        idebus[1] = qdev_get_child_bus(&dev->qdev, "ide.1");

    } else {

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

            ISADevice *dev;

            char busname[] = "ide.0";

            dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

                               ide_irq[i],

                               hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);

            /*

             * The ide bus name is ide.0 for the first bus and ide.1 for the

             * second one.

             */

            busname[4] = '0' + i;

            idebus[i] = qdev_get_child_bus(DEVICE(dev), busname);

        }

    }

    pc_cmos_init(below_4g_mem_size, above_4g_mem_size, machine->boot_order,

                 machine, floppy, idebus[0], idebus[1], rtc_state);

    if (pci_enabled && usb_enabled()) {

        pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");

    }

    if (pci_enabled && acpi_enabled) {

        DeviceState *piix4_pm;

        I2CBus *smbus;

        smi_irq = qemu_allocate_irqs(pc_acpi_smi_interrupt, first_cpu, 1);

        /* TODO: Populate SPD eeprom data.  */

        smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,

                              gsi[9], *smi_irq,

                              kvm_enabled(), fw_cfg, &piix4_pm);

        smbus_eeprom_init(smbus, 8, NULL, 0);

        object_property_add_link(OBJECT(machine), PC_MACHINE_ACPI_DEVICE_PROP,

                                 TYPE_HOTPLUG_HANDLER,

                                 (Object **)&pc_machine->acpi_dev,

                                 object_property_allow_set_link,

                                 OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort);

        object_property_set_link(OBJECT(machine), OBJECT(piix4_pm),

                                 PC_MACHINE_ACPI_DEVICE_PROP, &error_abort);

    }

    if (pci_enabled) {

        pc_pci_device_init(pci_bus);

    }

}


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

                             Error **errp)

{

    StringInputVisitor *siv = to_siv(v);

    if (!siv->string) {

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

                   "integer");

        return;

    }

    parse_str(siv, errp);

    if (!siv->ranges) {

        goto error;

    }

    if (!siv->cur_range) {

        Range *r;

        siv->cur_range = g_list_first(siv->ranges);

        if (!siv->cur_range) {

            goto error;

        }

        r = siv->cur_range->data;

        if (!r) {

            goto error;

        }

        siv->cur = r->begin;

    }

    *obj = siv->cur;

    siv->cur++;

    return;

error:

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

               "an int64 value or range");

}


void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)

{

    s->chr_read(s->handler_opaque, buf, len);

}


static void virtio_set_status(struct subchannel_id schid,

                              unsigned long dev_addr)

{

    unsigned char status = dev_addr;

    if (run_ccw(schid, CCW_CMD_WRITE_STATUS, &status, sizeof(status))) {

        virtio_panic("Could not write status to host!\n");

    }

}