static inline void RENAME(hyscale)(uint16_t *dst, int dstWidth, uint8_t *src, int srcW, int xInc,

				   int flags, int canMMX2BeUsed, int16_t *hLumFilter,

				   int16_t *hLumFilterPos, int hLumFilterSize, void *funnyYCode,

				   int srcFormat, uint8_t *formatConvBuffer)

{

    if(srcFormat==IMGFMT_YUY2)

    {

	RENAME(yuy2ToY)(formatConvBuffer, src, srcW);

	src= formatConvBuffer;

    }

    else if(srcFormat==IMGFMT_BGR32)

    {

	RENAME(bgr32ToY)(formatConvBuffer, src, srcW);

	src= formatConvBuffer;

    }

    else if(srcFormat==IMGFMT_BGR24)

    {

	RENAME(bgr24ToY)(formatConvBuffer, src, srcW);

	src= formatConvBuffer;

    }

    else if(srcFormat==IMGFMT_BGR16)

    {

	RENAME(bgr16ToY)(formatConvBuffer, src, srcW);

	src= formatConvBuffer;

    }

    else if(srcFormat==IMGFMT_BGR15)

    {

	RENAME(bgr15ToY)(formatConvBuffer, src, srcW);

	src= formatConvBuffer;

    }

    else if(srcFormat==IMGFMT_RGB32)

    {

	RENAME(rgb32ToY)(formatConvBuffer, src, srcW);

	src= formatConvBuffer;

    }

    else if(srcFormat==IMGFMT_RGB24)

    {

	RENAME(rgb24ToY)(formatConvBuffer, src, srcW);

	src= formatConvBuffer;

    }

#ifdef HAVE_MMX

	// use the new MMX scaler if th mmx2 cant be used (its faster than the x86asm one)

    if(!(flags&SWS_FAST_BILINEAR) || (!canMMX2BeUsed))

#else

    if(!(flags&SWS_FAST_BILINEAR))

#endif

    {

    	RENAME(hScale)(dst, dstWidth, src, srcW, xInc, hLumFilter, hLumFilterPos, hLumFilterSize);

    }

    else // Fast Bilinear upscale / crap downscale

    {

#ifdef ARCH_X86

#ifdef HAVE_MMX2

	int i;

	if(canMMX2BeUsed)

	{

		asm volatile(

			"pxor %%mm7, %%mm7		\n\t"

			"pxor %%mm2, %%mm2		\n\t" // 2*xalpha

			"movd %5, %%mm6			\n\t" // xInc&0xFFFF

			"punpcklwd %%mm6, %%mm6		\n\t"

			"punpcklwd %%mm6, %%mm6		\n\t"

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

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

			"paddw %%mm6, %%mm2		\n\t"

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

			"paddw %%mm6, %%mm2		\n\t"

			"psllq $16, %%mm2		\n\t" //0,t,2t,3t		t=xInc&0xFF

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

			"movd %4, %%mm6			\n\t" //(xInc*4)&0xFFFF

			"punpcklwd %%mm6, %%mm6		\n\t"

			"punpcklwd %%mm6, %%mm6		\n\t"

			"xorl %%eax, %%eax		\n\t" // i

			"movl %0, %%esi			\n\t" // src

			"movl %1, %%edi			\n\t" // buf1

			"movl %3, %%edx			\n\t" // (xInc*4)>>16

			"xorl %%ecx, %%ecx		\n\t"

			"xorl %%ebx, %%ebx		\n\t"

			"movw %4, %%bx			\n\t" // (xInc*4)&0xFFFF

#define FUNNY_Y_CODE \

			PREFETCH" 1024(%%esi)		\n\t"\

			PREFETCH" 1056(%%esi)		\n\t"\

			PREFETCH" 1088(%%esi)		\n\t"\

			"call *%6			\n\t"\

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

			"xorl %%ecx, %%ecx		\n\t"

FUNNY_Y_CODE

FUNNY_Y_CODE

FUNNY_Y_CODE

FUNNY_Y_CODE

FUNNY_Y_CODE

FUNNY_Y_CODE

FUNNY_Y_CODE

FUNNY_Y_CODE

			:: "m" (src), "m" (dst), "m" (dstWidth), "m" ((xInc*4)>>16),

			"m" ((xInc*4)&0xFFFF), "m" (xInc&0xFFFF), "m" (funnyYCode)

			: "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"

		);

		for(i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) dst[i] = src[srcW-1]*128;

	}

	else

	{

#endif

	//NO MMX just normal asm ...

	asm volatile(

		"xorl %%eax, %%eax		\n\t" // i

		"xorl %%ebx, %%ebx		\n\t" // xx

		"xorl %%ecx, %%ecx		\n\t" // 2*xalpha

		".balign 16			\n\t"

		"1:				\n\t"

		"movzbl  (%0, %%ebx), %%edi	\n\t" //src[xx]

		"movzbl 1(%0, %%ebx), %%esi	\n\t" //src[xx+1]

		"subl %%edi, %%esi		\n\t" //src[xx+1] - src[xx]

		"imull %%ecx, %%esi		\n\t" //(src[xx+1] - src[xx])*2*xalpha

		"shll $16, %%edi		\n\t"

		"addl %%edi, %%esi		\n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)

		"movl %1, %%edi			\n\t"

		"shrl $9, %%esi			\n\t"

		"movw %%si, (%%edi, %%eax, 2)	\n\t"

		"addw %4, %%cx			\n\t" //2*xalpha += xInc&0xFF

		"adcl %3, %%ebx			\n\t" //xx+= xInc>>8 + carry

		"movzbl (%0, %%ebx), %%edi	\n\t" //src[xx]

		"movzbl 1(%0, %%ebx), %%esi	\n\t" //src[xx+1]

		"subl %%edi, %%esi		\n\t" //src[xx+1] - src[xx]

		"imull %%ecx, %%esi		\n\t" //(src[xx+1] - src[xx])*2*xalpha

		"shll $16, %%edi		\n\t"

		"addl %%edi, %%esi		\n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)

		"movl %1, %%edi			\n\t"

		"shrl $9, %%esi			\n\t"

		"movw %%si, 2(%%edi, %%eax, 2)	\n\t"

		"addw %4, %%cx			\n\t" //2*xalpha += xInc&0xFF

		"adcl %3, %%ebx			\n\t" //xx+= xInc>>8 + carry

		"addl $2, %%eax			\n\t"

		"cmpl %2, %%eax			\n\t"

		" jb 1b				\n\t"

		:: "r" (src), "m" (dst), "m" (dstWidth), "m" (xInc>>16), "m" (xInc&0xFFFF)

		: "%eax", "%ebx", "%ecx", "%edi", "%esi"

		);

#ifdef HAVE_MMX2

	} //if MMX2 cant be used

#endif

#else

	int i;

	unsigned int xpos=0;

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

	{

		register unsigned int xx=xpos>>16;

		register unsigned int xalpha=(xpos&0xFFFF)>>9;

		dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;

		xpos+=xInc;

	}

#endif

    }

}


int32 float128_to_int32_round_to_zero( float128 a STATUS_PARAM )

{

    flag aSign;

    int32 aExp, shiftCount;

    uint64_t aSig0, aSig1, savedASig;

    int32 z;

    aSig1 = extractFloat128Frac1( a );

    aSig0 = extractFloat128Frac0( a );

    aExp = extractFloat128Exp( a );

    aSign = extractFloat128Sign( a );

    aSig0 |= ( aSig1 != 0 );

    if ( 0x401E < aExp ) {

        if ( ( aExp == 0x7FFF ) && aSig0 ) aSign = 0;

        goto invalid;

    }

    else if ( aExp < 0x3FFF ) {

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

        return 0;

    }

    aSig0 |= LIT64( 0x0001000000000000 );

    shiftCount = 0x402F - aExp;

    savedASig = aSig0;

    aSig0 >>= shiftCount;

    z = aSig0;

    if ( aSign ) z = - z;

    if ( ( z < 0 ) ^ aSign ) {

 invalid:

        float_raise( float_flag_invalid STATUS_VAR);

        return aSign ? (int32_t) 0x80000000 : 0x7FFFFFFF;

    }

    if ( ( aSig0<<shiftCount ) != savedASig ) {

        STATUS(float_exception_flags) |= float_flag_inexact;

    }

    return z;

}


static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,

                             uint32_t length)

{

    if (length != 13)

        return AVERROR_INVALIDDATA;

    if (s->state & PNG_IDAT) {

        av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");

        return AVERROR_INVALIDDATA;

    if (s->state & PNG_IHDR) {

        av_log(avctx, AV_LOG_ERROR, "Multiple IHDR\n");

        return AVERROR_INVALIDDATA;

    s->width  = s->cur_w = bytestream2_get_be32(&s->gb);

    s->height = s->cur_h = bytestream2_get_be32(&s->gb);

    if (av_image_check_size(s->width, s->height, 0, avctx)) {

        s->cur_w = s->cur_h = s->width = s->height = 0;

        av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");

        return AVERROR_INVALIDDATA;

    s->bit_depth        = bytestream2_get_byte(&s->gb);

    s->color_type       = bytestream2_get_byte(&s->gb);

    s->compression_type = bytestream2_get_byte(&s->gb);

    s->filter_type      = bytestream2_get_byte(&s->gb);

    s->interlace_type   = bytestream2_get_byte(&s->gb);

    bytestream2_skip(&s->gb, 4); /* crc */

    s->state |= PNG_IHDR;

    if (avctx->debug & FF_DEBUG_PICT_INFO)

        av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "

                "compression_type=%d filter_type=%d interlace_type=%d\n",

                s->width, s->height, s->bit_depth, s->color_type,

                s->compression_type, s->filter_type, s->interlace_type);

    return 0;

error:

    s->cur_w = s->cur_h = s->width = s->height = 0;

    s->bit_depth = 8;

    return AVERROR_INVALIDDATA;


int page_unprotect(target_ulong address, uintptr_t pc)

{

    unsigned int prot;

    bool current_tb_invalidated;

    PageDesc *p;

    target_ulong host_start, host_end, addr;

    /* Technically this isn't safe inside a signal handler.  However we

       know this only ever happens in a synchronous SEGV handler, so in

       practice it seems to be ok.  */

    mmap_lock();

    p = page_find(address >> TARGET_PAGE_BITS);

    if (!p) {

        mmap_unlock();

        return 0;

    }

    /* if the page was really writable, then we change its

       protection back to writable */

    if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {

        host_start = address & qemu_host_page_mask;

        host_end = host_start + qemu_host_page_size;

        prot = 0;

        current_tb_invalidated = false;

        for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {

            p = page_find(addr >> TARGET_PAGE_BITS);

            p->flags |= PAGE_WRITE;

            prot |= p->flags;

            /* and since the content will be modified, we must invalidate

               the corresponding translated code. */

            current_tb_invalidated |= tb_invalidate_phys_page(addr, pc);

#ifdef CONFIG_USER_ONLY

            if (DEBUG_TB_CHECK_GATE) {

                tb_invalidate_check(addr);

            }

#endif

        }

        mprotect((void *)g2h(host_start), qemu_host_page_size,

                 prot & PAGE_BITS);

        mmap_unlock();

        /* If current TB was invalidated return to main loop */

        return current_tb_invalidated ? 2 : 1;

    }

    mmap_unlock();

    return 0;

}


static void v9fs_readlink(void *opaque)

{

    V9fsPDU *pdu = opaque;

    size_t offset = 7;

    V9fsString target;

    int32_t fid;

    int err = 0;

    V9fsFidState *fidp;

    pdu_unmarshal(pdu, offset, "d", &fid);

    fidp = get_fid(pdu, fid);

    if (fidp == NULL) {

        err = -ENOENT;

        goto out_nofid;

    }

    v9fs_string_init(&target);

    err = v9fs_co_readlink(pdu, &fidp->path, &target);

    if (err < 0) {

        goto out;

    }

    offset += pdu_marshal(pdu, offset, "s", &target);

    err = offset;

    v9fs_string_free(&target);

out:

    put_fid(pdu, fidp);

out_nofid:

    trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);

    complete_pdu(pdu->s, pdu, err);

}


int kvm_init(MachineClass *mc)

{

    static const char upgrade_note[] =

        "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"

        "(see http://sourceforge.net/projects/kvm).\n";

    struct {

        const char *name;

        int num;

    } num_cpus[] = {

        { "SMP",          smp_cpus },

        { "hotpluggable", max_cpus },

        { NULL, }

    }, *nc = num_cpus;

    int soft_vcpus_limit, hard_vcpus_limit;

    KVMState *s;

    const KVMCapabilityInfo *missing_cap;

    int ret;

    int i, type = 0;

    const char *kvm_type;

    s = g_malloc0(sizeof(KVMState));

    /*

     * On systems where the kernel can support different base page

     * sizes, host page size may be different from TARGET_PAGE_SIZE,

     * even with KVM.  TARGET_PAGE_SIZE is assumed to be the minimum

     * page size for the system though.

     */

    assert(TARGET_PAGE_SIZE <= getpagesize());

    page_size_init();

#ifdef KVM_CAP_SET_GUEST_DEBUG

    QTAILQ_INIT(&s->kvm_sw_breakpoints);

#endif

    s->vmfd = -1;

    s->fd = qemu_open("/dev/kvm", O_RDWR);

    if (s->fd == -1) {

        fprintf(stderr, "Could not access KVM kernel module: %m\n");

        ret = -errno;

        goto err;

    }

    ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);

    if (ret < KVM_API_VERSION) {

        if (ret > 0) {

            ret = -EINVAL;

        }

        fprintf(stderr, "kvm version too old\n");

        goto err;

    }

    if (ret > KVM_API_VERSION) {

        ret = -EINVAL;

        fprintf(stderr, "kvm version not supported\n");

        goto err;

    }

    s->nr_slots = kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);

    /* If unspecified, use the default value */

    if (!s->nr_slots) {

        s->nr_slots = 32;

    }

    s->slots = g_malloc0(s->nr_slots * sizeof(KVMSlot));

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

        s->slots[i].slot = i;

    }

    /* check the vcpu limits */

    soft_vcpus_limit = kvm_recommended_vcpus(s);

    hard_vcpus_limit = kvm_max_vcpus(s);

    while (nc->name) {

        if (nc->num > soft_vcpus_limit) {

            fprintf(stderr,

                    "Warning: Number of %s cpus requested (%d) exceeds "

                    "the recommended cpus supported by KVM (%d)\n",

                    nc->name, nc->num, soft_vcpus_limit);

            if (nc->num > hard_vcpus_limit) {

                fprintf(stderr, "Number of %s cpus requested (%d) exceeds "

                        "the maximum cpus supported by KVM (%d)\n",

                        nc->name, nc->num, hard_vcpus_limit);

                exit(1);

            }

        }

        nc++;

    }

    kvm_type = qemu_opt_get(qemu_get_machine_opts(), "kvm-type");

    if (mc->kvm_type) {

        type = mc->kvm_type(kvm_type);

    } else if (kvm_type) {

        fprintf(stderr, "Invalid argument kvm-type=%s\n", kvm_type);

        goto err;

    }

    do {

        ret = kvm_ioctl(s, KVM_CREATE_VM, type);

    } while (ret == -EINTR);

    if (ret < 0) {

        fprintf(stderr, "ioctl(KVM_CREATE_VM) failed: %d %s\n", -ret,

                strerror(-ret));

#ifdef TARGET_S390X

        fprintf(stderr, "Please add the 'switch_amode' kernel parameter to "

                        "your host kernel command line\n");

#endif

        goto err;

    }

    s->vmfd = ret;

    missing_cap = kvm_check_extension_list(s, kvm_required_capabilites);

    if (!missing_cap) {

        missing_cap =

            kvm_check_extension_list(s, kvm_arch_required_capabilities);

    }

    if (missing_cap) {

        ret = -EINVAL;

        fprintf(stderr, "kvm does not support %s\n%s",

                missing_cap->name, upgrade_note);

        goto err;

    }

    s->coalesced_mmio = kvm_check_extension(s, KVM_CAP_COALESCED_MMIO);

    s->broken_set_mem_region = 1;

    ret = kvm_check_extension(s, KVM_CAP_JOIN_MEMORY_REGIONS_WORKS);

    if (ret > 0) {

        s->broken_set_mem_region = 0;

    }

#ifdef KVM_CAP_VCPU_EVENTS

    s->vcpu_events = kvm_check_extension(s, KVM_CAP_VCPU_EVENTS);

#endif

    s->robust_singlestep =

        kvm_check_extension(s, KVM_CAP_X86_ROBUST_SINGLESTEP);

#ifdef KVM_CAP_DEBUGREGS

    s->debugregs = kvm_check_extension(s, KVM_CAP_DEBUGREGS);

#endif

#ifdef KVM_CAP_XSAVE

    s->xsave = kvm_check_extension(s, KVM_CAP_XSAVE);

#endif

#ifdef KVM_CAP_XCRS

    s->xcrs = kvm_check_extension(s, KVM_CAP_XCRS);

#endif

#ifdef KVM_CAP_PIT_STATE2

    s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);

#endif

#ifdef KVM_CAP_IRQ_ROUTING

    s->direct_msi = (kvm_check_extension(s, KVM_CAP_SIGNAL_MSI) > 0);

#endif

    s->intx_set_mask = kvm_check_extension(s, KVM_CAP_PCI_2_3);

    s->irq_set_ioctl = KVM_IRQ_LINE;

    if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {

        s->irq_set_ioctl = KVM_IRQ_LINE_STATUS;

    }

#ifdef KVM_CAP_READONLY_MEM

    kvm_readonly_mem_allowed =

        (kvm_check_extension(s, KVM_CAP_READONLY_MEM) > 0);

#endif

    ret = kvm_arch_init(s);

    if (ret < 0) {

        goto err;

    }

    ret = kvm_irqchip_create(s);

    if (ret < 0) {

        goto err;

    }

    kvm_state = s;

    memory_listener_register(&kvm_memory_listener, &address_space_memory);

    memory_listener_register(&kvm_io_listener, &address_space_io);

    s->many_ioeventfds = kvm_check_many_ioeventfds();

    cpu_interrupt_handler = kvm_handle_interrupt;

    return 0;

err:

    if (s->vmfd >= 0) {

        close(s->vmfd);

    }

    if (s->fd != -1) {

        close(s->fd);

    }

    g_free(s->slots);

    g_free(s);

    return ret;

}


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

                              unsigned size)

{

    OpenPICState *opp = opaque;

    int idx = opp->irq_msi;

    int srs, ibs;

    DPRINTF("%s: addr " TARGET_FMT_plx " <= %08x\n", __func__, addr, val);

    if (addr & 0xF) {

        return;

    }

    switch (addr) {

    case MSIIR_OFFSET:

        srs = val >> MSIIR_SRS_SHIFT;

        idx += srs;

        ibs = (val & MSIIR_IBS_MASK) >> MSIIR_IBS_SHIFT;

        opp->msi[srs].msir |= 1 << ibs;

        openpic_set_irq(opp, idx, 1);

        break;

    default:

        /* most registers are read-only, thus ignored */

        break;

    }

}


static int eval_refl(const int16_t *coefs, int *refl, RA144Context *ractx)

{

    int retval = 0;

    int b, c, i;

    unsigned int u;

    int buffer1[10];

    int buffer2[10];

    int *bp1 = buffer1;

    int *bp2 = buffer2;

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

        buffer2[i] = coefs[i];

    u = refl[9] = bp2[9];

    if (u + 0x1000 > 0x1fff) {

        av_log(ractx, AV_LOG_ERROR, "Overflow. Broken sample?\n");

        return 0;

    }

    for (c=8; c >= 0; c--) {

        if (u == 0x1000)

            u++;

        if (u == 0xfffff000)

            u--;

        b = 0x1000-((u * u) >> 12);

        if (b == 0)

            b++;

        for (u=0; u<=c; u++)

            bp1[u] = ((bp2[u] - ((refl[c+1] * bp2[c-u]) >> 12)) * (0x1000000 / b)) >> 12;

        refl[c] = u = bp1[c];

        if ((u + 0x1000) > 0x1fff)

            retval = 1;

        FFSWAP(int *, bp1, bp2);

    }

    return retval;

}


static void vfio_disable_interrupts(VFIOPCIDevice *vdev)

{

    switch (vdev->interrupt) {

    case VFIO_INT_INTx:

        vfio_disable_intx(vdev);

        break;

    case VFIO_INT_MSI:

        vfio_disable_msi(vdev);

        break;

    case VFIO_INT_MSIX:

        vfio_disable_msix(vdev);

        break;

    }

}


static inline int get_ue_code(GetBitContext *gb, int order)

{

    if (order) {

        int ret = get_ue_golomb(gb) << order;

        return ret + get_bits(gb, order);

    }

    return get_ue_golomb(gb);

}


int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)

{

    int i, nb_samples1;

    short *bufin[2];

    short *bufout[2];

    short *buftmp2[2], *buftmp3[2];

    short *output_bak = NULL;

    int lenout;

    if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {

        /* nothing to do */

        memcpy(output, input, nb_samples * s->input_channels * sizeof(short));

        return nb_samples;

    }

    if (s->sample_fmt[0] != SAMPLE_FMT_S16) {

        int istride[1] = { s->sample_size[0] };

        int ostride[1] = { 2 };

        const void *ibuf[1] = { input };

        void       *obuf[1];

        unsigned input_size = nb_samples*s->input_channels*s->sample_size[0];

        if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {

            av_free(s->buffer[0]);

            s->buffer_size[0] = input_size;

            s->buffer[0] = av_malloc(s->buffer_size[0]);

            if (!s->buffer[0]) {

                av_log(s, AV_LOG_ERROR, "Could not allocate buffer\n");

                return 0;

            }

        }

        obuf[0] = s->buffer[0];

        if (av_audio_convert(s->convert_ctx[0], obuf, ostride,

                             ibuf, istride, nb_samples*s->input_channels) < 0) {

            av_log(s, AV_LOG_ERROR, "Audio sample format conversion failed\n");

            return 0;

        }

        input  = s->buffer[0];

    }

    lenout= 4*nb_samples * s->ratio + 16;

    if (s->sample_fmt[1] != SAMPLE_FMT_S16) {

        output_bak = output;

        if (!s->buffer_size[1] || s->buffer_size[1] < lenout) {

            av_free(s->buffer[1]);

            s->buffer_size[1] = lenout;

            s->buffer[1] = av_malloc(s->buffer_size[1]);

            if (!s->buffer[1]) {

                av_log(s, AV_LOG_ERROR, "Could not allocate buffer\n");

                return 0;

            }

        }

        output = s->buffer[1];

    }

    /* XXX: move those malloc to resample init code */

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

        bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );

        memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));

        buftmp2[i] = bufin[i] + s->temp_len;

    }

    /* make some zoom to avoid round pb */

    bufout[0]= av_malloc( lenout * sizeof(short) );

    bufout[1]= av_malloc( lenout * sizeof(short) );

    if (s->input_channels == 2 &&

        s->output_channels == 1) {

        buftmp3[0] = output;

        stereo_to_mono(buftmp2[0], input, nb_samples);

    } else if (s->output_channels >= 2 && s->input_channels == 1) {

        buftmp3[0] = bufout[0];

        memcpy(buftmp2[0], input, nb_samples*sizeof(short));

    } else if (s->output_channels >= 2) {

        buftmp3[0] = bufout[0];

        buftmp3[1] = bufout[1];

        stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);

    } else {

        buftmp3[0] = output;

        memcpy(buftmp2[0], input, nb_samples*sizeof(short));

    }

    nb_samples += s->temp_len;

    /* resample each channel */

    nb_samples1 = 0; /* avoid warning */

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

        int consumed;

        int is_last= i+1 == s->filter_channels;

        nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);

        s->temp_len= nb_samples - consumed;

        s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));

        memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));

    }

    if (s->output_channels == 2 && s->input_channels == 1) {

        mono_to_stereo(output, buftmp3[0], nb_samples1);

    } else if (s->output_channels == 2) {

        stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);

    } else if (s->output_channels == 6) {

        ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);

    }

    if (s->sample_fmt[1] != SAMPLE_FMT_S16) {

        int istride[1] = { 2 };

        int ostride[1] = { s->sample_size[1] };

        const void *ibuf[1] = { output };

        void       *obuf[1] = { output_bak };

        if (av_audio_convert(s->convert_ctx[1], obuf, ostride,

                             ibuf, istride, nb_samples1*s->output_channels) < 0) {

            av_log(s, AV_LOG_ERROR, "Audio sample format convertion failed\n");

            return 0;

        }

    }

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

        av_free(bufin[i]);

    av_free(bufout[0]);

    av_free(bufout[1]);

    return nb_samples1;

}


int ff_h264_decode_extradata(H264Context *h)

{

    AVCodecContext *avctx = h->s.avctx;

    if (avctx->extradata[0] == 1) {

        int i, cnt, nalsize;

        unsigned char *p = avctx->extradata;

        h->is_avc = 1;

        if (avctx->extradata_size < 7) {

            av_log(avctx, AV_LOG_ERROR, "avcC too short\n");

            return -1;

        }

        /* sps and pps in the avcC always have length coded with 2 bytes,

         * so put a fake nal_length_size = 2 while parsing them */

        h->nal_length_size = 2;

        // Decode sps from avcC

        cnt = *(p + 5) & 0x1f; // Number of sps

        p  += 6;

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

            nalsize = AV_RB16(p) + 2;

            if (p - avctx->extradata + nalsize > avctx->extradata_size)

                return -1;

            if (decode_nal_units(h, p, nalsize) < 0) {

                av_log(avctx, AV_LOG_ERROR,

                       "Decoding sps %d from avcC failed\n", i);

                return -1;

            }

            p += nalsize;

        }

        // Decode pps from avcC

        cnt = *(p++); // Number of pps

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

            nalsize = AV_RB16(p) + 2;

            if (p - avctx->extradata + nalsize > avctx->extradata_size)

                return -1;

            if (decode_nal_units(h, p, nalsize) < 0) {

                av_log(avctx, AV_LOG_ERROR,

                       "Decoding pps %d from avcC failed\n", i);

                return -1;

            }

            p += nalsize;

        }

        // Now store right nal length size, that will be used to parse all other nals

        h->nal_length_size = (avctx->extradata[4] & 0x03) + 1;

    } else {

        h->is_avc = 0;

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

            return -1;

    }

    return 0;

}


void video_encode_example(const char *filename)

{

    AVCodec *codec;

    AVCodecContext *c= NULL;

    int i, out_size, size, x, y, outbuf_size;

    FILE *f;

    AVFrame *picture;

    uint8_t *outbuf, *picture_buf;

    printf("Video encoding\n");

    /* find the mpeg1 video encoder */

    codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);

    if (!codec) {

        fprintf(stderr, "codec not found\n");

        exit(1);

    }

    c= avcodec_alloc_context();

    picture= avcodec_alloc_frame();

    /* put sample parameters */

    c->bit_rate = 400000;

    /* resolution must be a multiple of two */

    c->width = 352;

    c->height = 288;

    /* frames per second */

    c->frame_rate = 25;

    c->frame_rate_base= 1;

    c->gop_size = 10; /* emit one intra frame every ten frames */

    c->max_b_frames=1;

    /* open it */

    if (avcodec_open(c, codec) < 0) {

        fprintf(stderr, "could not open codec\n");

        exit(1);

    }

    /* the codec gives us the frame size, in samples */

    f = fopen(filename, "w");

    if (!f) {

        fprintf(stderr, "could not open %s\n", filename);

        exit(1);

    }

    /* alloc image and output buffer */

    outbuf_size = 100000;

    outbuf = malloc(outbuf_size);

    size = c->width * c->height;

    picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */

    picture->data[0] = picture_buf;

    picture->data[1] = picture->data[0] + size;

    picture->data[2] = picture->data[1] + size / 4;

    picture->linesize[0] = c->width;

    picture->linesize[1] = c->width / 2;

    picture->linesize[2] = c->width / 2;

    /* encode 1 second of video */

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

        fflush(stdout);

        /* prepare a dummy image */

        /* Y */

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

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

                picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;

            }

        }

        /* Cb and Cr */

        for(y=0;y<c->height/2;y++) {

            for(x=0;x<c->width/2;x++) {

                picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;

                picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;

            }

        }

        /* encode the image */

        out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);

        printf("encoding frame %3d (size=%5d)\n", i, out_size);

        fwrite(outbuf, 1, out_size, f);

    }

    /* get the delayed frames */

    for(; out_size; i++) {

        fflush(stdout);

        out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);

        printf("write frame %3d (size=%5d)\n", i, out_size);

        fwrite(outbuf, 1, out_size, f);

    }

    /* add sequence end code to have a real mpeg file */

    outbuf[0] = 0x00;

    outbuf[1] = 0x00;

    outbuf[2] = 0x01;

    outbuf[3] = 0xb7;

    fwrite(outbuf, 1, 4, f);

    fclose(f);

    free(picture_buf);

    free(outbuf);

    avcodec_close(c);

    free(c);

    free(picture);

    printf("\n");

}


static void socket_outgoing_migration(Object *src,

                                      Error *err,

                                      gpointer opaque)

{

    MigrationState *s = opaque;

    QIOChannel *sioc = QIO_CHANNEL(src);

    if (err) {

        trace_migration_socket_outgoing_error(error_get_pretty(err));

        s->to_dst_file = NULL;

        migrate_fd_error(s, err);

    } else {

        trace_migration_socket_outgoing_connected();

        migration_set_outgoing_channel(s, sioc);

    }

    object_unref(src);

}


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

{

    ICCDeviceClass *idc = ICC_DEVICE_CLASS(klass);

    DeviceClass *dc = DEVICE_CLASS(klass);

    dc->vmsd = &vmstate_apic_common;

    dc->reset = apic_reset_common;

    dc->no_user = 1;

    dc->props = apic_properties_common;

    idc->init = apic_init_common;

}


static void do_unassigned_access(target_ulong addr, int is_write, int is_exec,

                          int is_asi, int size)

#else

void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,

                          int is_asi, int size)

#endif

{

    CPUState *saved_env;

    /* XXX: hack to restore env in all cases, even if not called from

       generated code */

    saved_env = env;

    env = cpu_single_env;

#ifdef DEBUG_UNASSIGNED

    printf("Unassigned mem access to " TARGET_FMT_plx " from " TARGET_FMT_lx

           "\n", addr, env->pc);

#endif

    if (is_exec)

        raise_exception(TT_CODE_ACCESS);

    else

        raise_exception(TT_DATA_ACCESS);

    env = saved_env;

}


int qemu_fsdev_add(QemuOpts *opts)

{

    int i;

    struct FsDriverListEntry *fsle;

    const char *fsdev_id = qemu_opts_id(opts);

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

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

    bool ro = qemu_opt_get_bool(opts, "readonly", 0);

    if (!fsdev_id) {

        fprintf(stderr, "fsdev: No id specified\n");

        return -1;

    }

    if (fsdriver) {

        for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {

            if (strcmp(FsDrivers[i].name, fsdriver) == 0) {

                break;

            }

        }

        if (i == ARRAY_SIZE(FsDrivers)) {

            fprintf(stderr, "fsdev: fsdriver %s not found\n", fsdriver);

            return -1;

        }

    } else {

        fprintf(stderr, "fsdev: No fsdriver specified\n");

        return -1;

    }

    fsle = g_malloc0(sizeof(*fsle));

    fsle->fse.fsdev_id = g_strdup(fsdev_id);

    fsle->fse.ops = FsDrivers[i].ops;

    if (writeout) {

        if (!strcmp(writeout, "immediate")) {

            fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;

        }

    }

    if (ro) {

        fsle->fse.export_flags |= V9FS_RDONLY;

    } else {

        fsle->fse.export_flags &= ~V9FS_RDONLY;

    }

    if (fsle->fse.ops->parse_opts) {

        if (fsle->fse.ops->parse_opts(opts, &fsle->fse)) {

            return -1;

        }

    }

    QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);

    return 0;

}


void OPPROTO op_set_Rc0 (void)

{

    env->crf[0] = T0 | xer_ov;

    RETURN();

}


static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)

{

    AVStream *st;

    int len, ret;

    for(;;) {

        /* select current input stream component */

        st = s->cur_st;

        if (st) {

            if (!st->parser) {

                /* no parsing needed: we just output the packet as is */

                /* raw data support */

                *pkt = s->cur_pkt;

                compute_pkt_fields(s, st, NULL, pkt);

                s->cur_st = NULL;

                return 0;

            } else if (s->cur_len > 0) {

                /* we use the MPEG semantics: the pts and dts in a

                   packet are given from the first frame beginning in

                   it */

                if (!st->got_frame) {

                    st->cur_frame_pts = s->cur_pkt.pts;

                    st->cur_frame_dts = s->cur_pkt.dts;

                    s->cur_pkt.pts = AV_NOPTS_VALUE;

                    s->cur_pkt.dts = AV_NOPTS_VALUE;

                    st->got_frame = 1;

                }

                len = av_parser_parse(st->parser, &st->codec, &pkt->data, &pkt->size,

                                      s->cur_ptr, s->cur_len);

                /* increment read pointer */

                s->cur_ptr += len;

                s->cur_len -= len;

                /* return packet if any */

                if (pkt->size) {

                    pkt->duration = 0;

                    pkt->stream_index = st->index;

                    pkt->pts = st->cur_frame_pts;

                    pkt->dts = st->cur_frame_dts;

                    pkt->destruct = av_destruct_packet_nofree;

                    compute_pkt_fields(s, st, st->parser, pkt);

                    st->got_frame = 0;

                    return 0;

                }

            } else {

                s->cur_st = NULL;

            }

        } else {

            /* free previous packet */

            if (s->cur_st && s->cur_st->parser)

                av_free_packet(&s->cur_pkt);

            /* read next packet */

            ret = av_read_packet(s, &s->cur_pkt);

            if (ret < 0)

                return ret;

            /* convert the packet time stamp units and handle wrapping */

            s->cur_pkt.pts = convert_timestamp_units(s,

                                               &s->last_pkt_pts, &s->last_pkt_pts_frac,

                                               &s->last_pkt_stream_pts,

                                               s->cur_pkt.pts);

            s->cur_pkt.dts = convert_timestamp_units(s,

                                               &s->last_pkt_dts,  &s->last_pkt_dts_frac,

                                               &s->last_pkt_stream_dts,

                                               s->cur_pkt.dts);

#if 0

            if (s->cur_pkt.stream_index == 1) {

                if (s->cur_pkt.pts != AV_NOPTS_VALUE)

                    printf("PACKET pts=%0.3f\n",

                           (double)s->cur_pkt.pts / AV_TIME_BASE);

                if (s->cur_pkt.dts != AV_NOPTS_VALUE)

                    printf("PACKET dts=%0.3f\n",

                           (double)s->cur_pkt.dts / AV_TIME_BASE);

            }

#endif

            /* duration field */

            if (s->cur_pkt.duration != 0) {

                s->cur_pkt.duration = ((int64_t)s->cur_pkt.duration * AV_TIME_BASE * s->pts_num) /

                    s->pts_den;

            }

            st = s->streams[s->cur_pkt.stream_index];

            s->cur_st = st;

            s->cur_ptr = s->cur_pkt.data;

            s->cur_len = s->cur_pkt.size;

            if (st->need_parsing && !st->parser) {

                st->parser = av_parser_init(st->codec.codec_id);

                if (!st->parser) {

                    /* no parser available : just output the raw packets */

                    st->need_parsing = 0;

                }

            }

        }

    }

}


static void monitor_data_destroy(Monitor *mon)

{

    QDECREF(mon->outbuf);

    qemu_mutex_destroy(&mon->out_lock);


static void qio_channel_websock_encode(QIOChannelWebsock *ioc)

{

    size_t header_size;

    union {

        char buf[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT];

        QIOChannelWebsockHeader ws;

    } header;

    if (!ioc->rawoutput.offset) {

        return;

    }

    header.ws.b0 = (1 << QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN) |

        (QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME &

         QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE);

    if (ioc->rawoutput.offset <

        QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) {

        header.ws.b1 = (uint8_t)ioc->rawoutput.offset;

        header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;

    } else if (ioc->rawoutput.offset <

               QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) {

        header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT;

        header.ws.u.s16.l16 = cpu_to_be16((uint16_t)ioc->rawoutput.offset);

        header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;

    } else {

        header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT;

        header.ws.u.s64.l64 = cpu_to_be64(ioc->rawoutput.offset);

        header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;

    }

    header_size -= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK;

    buffer_reserve(&ioc->encoutput, header_size + ioc->rawoutput.offset);

    buffer_append(&ioc->encoutput, header.buf, header_size);

    buffer_append(&ioc->encoutput, ioc->rawoutput.buffer,

                  ioc->rawoutput.offset);

    buffer_reset(&ioc->rawoutput);

}


static void process_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)

{

    AVFilterContext *ctx  = inlink->dst;

    ConcatContext *cat    = ctx->priv;

    unsigned in_no = FF_INLINK_IDX(inlink);

    if (in_no < cat->cur_idx) {

        av_log(ctx, AV_LOG_ERROR, "Frame after EOF on input %s\n",

               ctx->input_pads[in_no].name);

        avfilter_unref_buffer(buf);

    } if (in_no >= cat->cur_idx + ctx->nb_outputs) {

        ff_bufqueue_add(ctx, &cat->in[in_no].queue, buf);

    } else {

        push_frame(ctx, in_no, buf);

    }

}


static int make_ydt15_entry(int p2, int p1, int16_t *ydt)

#else

static int make_ydt15_entry(int p1, int p2, int16_t *ydt)

#endif

{

    int lo, hi;

    lo = ydt[p1];

    lo += (lo * 32) + (lo * 1024);

    hi = ydt[p2];

    hi += (hi * 32) + (hi * 1024);

    return (lo + (hi * (1 << 16))) * 2;

}


static void coroutine_fn v9fs_xattrwalk(void *opaque)

{

    int64_t size;

    V9fsString name;

    ssize_t err = 0;

    size_t offset = 7;

    int32_t fid, newfid;

    V9fsFidState *file_fidp;

    V9fsFidState *xattr_fidp = NULL;

    V9fsPDU *pdu = opaque;

    V9fsState *s = pdu->s;

    v9fs_string_init(&name);

    err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);

    if (err < 0) {

        goto out_nofid;

    }

    trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);

    file_fidp = get_fid(pdu, fid);

    if (file_fidp == NULL) {

        err = -ENOENT;

        goto out_nofid;

    }

    xattr_fidp = alloc_fid(s, newfid);

    if (xattr_fidp == NULL) {

        err = -EINVAL;

        goto out;

    }

    v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);

    if (!v9fs_string_size(&name)) {

        /*

         * listxattr request. Get the size first

         */

        size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);

        if (size < 0) {

            err = size;

            clunk_fid(s, xattr_fidp->fid);

            goto out;

        }

        /*

         * Read the xattr value

         */

        xattr_fidp->fs.xattr.len = size;

        xattr_fidp->fid_type = P9_FID_XATTR;

        xattr_fidp->fs.xattr.xattrwalk_fid = true;

        if (size) {

            xattr_fidp->fs.xattr.value = g_malloc(size);

            err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,

                                     xattr_fidp->fs.xattr.value,

                                     xattr_fidp->fs.xattr.len);

            if (err < 0) {

                clunk_fid(s, xattr_fidp->fid);

                goto out;

            }

        }

        err = pdu_marshal(pdu, offset, "q", size);

        if (err < 0) {

            goto out;

        }

        err += offset;

    } else {

        /*

         * specific xattr fid. We check for xattr

         * presence also collect the xattr size

         */

        size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,

                                 &name, NULL, 0);

        if (size < 0) {

            err = size;

            clunk_fid(s, xattr_fidp->fid);

            goto out;

        }

        /*

         * Read the xattr value

         */

        xattr_fidp->fs.xattr.len = size;

        xattr_fidp->fid_type = P9_FID_XATTR;

        xattr_fidp->fs.xattr.xattrwalk_fid = true;

        if (size) {

            xattr_fidp->fs.xattr.value = g_malloc(size);

            err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,

                                    &name, xattr_fidp->fs.xattr.value,

                                    xattr_fidp->fs.xattr.len);

            if (err < 0) {

                clunk_fid(s, xattr_fidp->fid);

                goto out;

            }

        }

        err = pdu_marshal(pdu, offset, "q", size);

        if (err < 0) {

            goto out;

        }

        err += offset;

    }

    trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);

out:

    put_fid(pdu, file_fidp);

    if (xattr_fidp) {

        put_fid(pdu, xattr_fidp);

    }

out_nofid:

    pdu_complete(pdu, err);

    v9fs_string_free(&name);

}


static void gen_slbmfev(DisasContext *ctx)

{

#if defined(CONFIG_USER_ONLY)

    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);

#else

    if (unlikely(ctx->pr)) {

        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);

        return;

    }

    gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,

                             cpu_gpr[rB(ctx->opcode)]);

#endif

}


void vga_common_init(VGACommonState *s, Object *obj, bool global_vmstate)

{

    int i, j, v, b;

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

        v = 0;

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

            v |= ((i >> j) & 1) << (j * 4);

        }

        expand4[i] = v;

        v = 0;

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

            v |= ((i >> (2 * j)) & 3) << (j * 4);

        }

        expand2[i] = v;

    }

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

        v = 0;

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

            b = ((i >> j) & 1);

            v |= b << (2 * j);

            v |= b << (2 * j + 1);

        }

        expand4to8[i] = v;

    }

    s->vram_size_mb = uint_clamp(s->vram_size_mb, 1, 512);

    s->vram_size_mb = pow2ceil(s->vram_size_mb);

    s->vram_size = s->vram_size_mb << 20;

    if (!s->vbe_size) {

        s->vbe_size = s->vram_size;

    }

    s->is_vbe_vmstate = 1;

    memory_region_init_ram(&s->vram, obj, "vga.vram", s->vram_size,

                           &error_abort);

    vmstate_register_ram(&s->vram, global_vmstate ? NULL : DEVICE(obj));

    xen_register_framebuffer(&s->vram);

    s->vram_ptr = memory_region_get_ram_ptr(&s->vram);

    s->get_bpp = vga_get_bpp;

    s->get_offsets = vga_get_offsets;

    s->get_resolution = vga_get_resolution;

    s->hw_ops = &vga_ops;

    switch (vga_retrace_method) {

    case VGA_RETRACE_DUMB:

        s->retrace = vga_dumb_retrace;

        s->update_retrace_info = vga_dumb_update_retrace_info;

        break;

    case VGA_RETRACE_PRECISE:

        s->retrace = vga_precise_retrace;

        s->update_retrace_info = vga_precise_update_retrace_info;

        break;

    }

    /*

     * Set default fb endian based on target, could probably be turned

     * into a device attribute set by the machine/platform to remove

     * all target endian dependencies from this file.

     */

#ifdef TARGET_WORDS_BIGENDIAN

    s->default_endian_fb = true;

#else

    s->default_endian_fb = false;

#endif

    vga_dirty_log_start(s);

}


static void dss_sp_scale_vector(int32_t *vec, int bits, int size)

{

    int i;

    if (bits < 0)

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

            vec[i] = vec[i] >> -bits;

    else

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

            vec[i] = vec[i] << bits;

}


void OPPROTO op_store_msr (void)

{

    do_store_msr(env, T0);

    RETURN();

}


static void rac_normalise(RangeCoder *c)

{

    for (;;) {

        c->range <<= 8;

        c->low   <<= 8;

        if (c->src < c->src_end) {

            c->low |= *c->src++;

        } else if (!c->low) {

            c->got_error = 1;

            return;

        }

        if (c->range >= RAC_BOTTOM)

            return;

    }

}


static int64_t scene_sad16(FrameRateContext *s, const uint16_t *p1, int p1_linesize, const uint16_t* p2, int p2_linesize, int height)

{

    int64_t sad;

    int x, y;

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

        for (x = 0; x < p1_linesize; x += 8) {

            sad += sad_8x8_16(p1 + y * p1_linesize + x,

                              p1_linesize,

                              p2 + y * p2_linesize + x,

                              p2_linesize);

        }

    }

    return sad;

}


static int hds_write_packet(AVFormatContext *s, AVPacket *pkt)

{

    HDSContext *c = s->priv_data;

    AVStream *st = s->streams[pkt->stream_index];

    OutputStream *os = &c->streams[s->streams[pkt->stream_index]->id];

    int64_t end_dts = (os->fragment_index) * c->min_frag_duration;

    int ret;

    if (st->first_dts == AV_NOPTS_VALUE)

        st->first_dts = pkt->dts;

    if ((!os->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&

        av_compare_ts(pkt->dts - st->first_dts, st->time_base,

                      end_dts, AV_TIME_BASE_Q) >= 0 &&

        pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {

        if ((ret = hds_flush(s, os, 0, pkt->dts)) < 0)

            return ret;

    }

    // Note, these fragment start timestamps, that represent a whole

    // OutputStream, assume all streams in it have the same time base.

    if (!os->packets_written)

        os->frag_start_ts = pkt->dts;

    os->last_ts = pkt->dts;

    os->packets_written++;

    return ff_write_chained(os->ctx, pkt->stream_index - os->first_stream, pkt, s);

}


static int gif_read_extension(GifState *s)

{

    int ext_code, ext_len, gce_flags, gce_transparent_index;

    /* There must be at least 2 bytes:

     * 1 for extension label and 1 for extension length. */

    if (bytestream2_get_bytes_left(&s->gb) < 2)

        return AVERROR_INVALIDDATA;

    ext_code = bytestream2_get_byteu(&s->gb);

    ext_len = bytestream2_get_byteu(&s->gb);

    av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);

    switch(ext_code) {

    case GIF_GCE_EXT_LABEL:

        if (ext_len != 4)

            goto discard_ext;

        /* We need at least 5 bytes more: 4 is for extension body

         * and 1 for next block size. */

        if (bytestream2_get_bytes_left(&s->gb) < 5)

            return AVERROR_INVALIDDATA;

        gce_flags = bytestream2_get_byteu(&s->gb);

        bytestream2_skipu(&s->gb, 2);    // delay during which the frame is shown

        gce_transparent_index = bytestream2_get_byteu(&s->gb);

        if (gce_flags & 0x01)

            s->transparent_color_index = gce_transparent_index;

        else

            s->transparent_color_index = -1;

        s->gce_disposal = (gce_flags >> 2) & 0x7;

        av_dlog(s->avctx, "gce_flags=%x tcolor=%d disposal=%d\n",

               gce_flags,

               s->transparent_color_index, s->gce_disposal);

        if (s->gce_disposal > 3) {

            s->gce_disposal = GCE_DISPOSAL_NONE;

            av_dlog(s->avctx, "invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);

        }

        ext_len = bytestream2_get_byteu(&s->gb);

        break;

    }

    /* NOTE: many extension blocks can come after */

 discard_ext:

    while (ext_len != 0) {

        /* There must be at least ext_len bytes and 1 for next block size byte. */

        if (bytestream2_get_bytes_left(&s->gb) < ext_len + 1)

            return AVERROR_INVALIDDATA;

        bytestream2_skipu(&s->gb, ext_len);

        ext_len = bytestream2_get_byteu(&s->gb);

        av_dlog(s->avctx, "ext_len1=%d\n", ext_len);

    }

    return 0;

}


yuv2rgb_2_c_template(SwsContext *c, const uint16_t *buf0,

                     const uint16_t *buf1, const uint16_t *ubuf0,

                     const uint16_t *ubuf1, const uint16_t *vbuf0,

                     const uint16_t *vbuf1, const uint16_t *abuf0,

                     const uint16_t *abuf1, uint8_t *dest, int dstW,

                     int yalpha, int uvalpha, int y,

                     enum PixelFormat target, int hasAlpha)

{

    int  yalpha1 = 4095 - yalpha;

    int uvalpha1 = 4095 - uvalpha;

    int i;

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

        int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;

        int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;

        int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;

        int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;

        int A1, A2;

        const void *r =  c->table_rV[V],

                   *g = (c->table_gU[U] + c->table_gV[V]),

                   *b =  c->table_bU[U];

        if (hasAlpha) {

            A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 19;

            A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;

        }

        yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,

                      r, g, b, y, target, hasAlpha);

    }

}


static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)

{

    int i;

    float (*output)[256] = ctx->audio_block.block_output;

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

        output[1][i] += output[2][i];

        output[2][i] += output[3][i];

    }

    memset(output[3], 0, sizeof(output[3]));

}


static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,

                           int bpno, int bandno, int bpass_csty_symbol,

                           int vert_causal_ctx_csty_symbol)

{

    int mask = 3 << (bpno - 1), y0, x, y;

    for (y0 = 0; y0 < height; y0 += 4)

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

            for (y = y0; y < height && y < y0 + 4; y++) {

                if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)

                && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {

                    int flags_mask = -1;

                    if (vert_causal_ctx_csty_symbol && y == y0 + 3)

                        flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);

                    if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {

                        int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);

                        if (bpass_csty_symbol)

                             t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;

                        else

                             t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?

                                               -mask : mask;

                        ff_jpeg2000_set_significance(t1, x, y,

                                                     t1->data[y][x] < 0);

                    }

                    t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;

                }

            }

}


void ff_fix_long_p_mvs(MpegEncContext * s)

{

    const int f_code= s->f_code;

    int y;

    UINT8 * fcode_tab= s->fcode_tab;

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

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

        int x;

        int xy= (y+1)* (s->mb_width+2)+1;

        int i= y*s->mb_width;

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

            if(s->mb_type[i]&MB_TYPE_INTER){

                if(   fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code

                   || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0

                   || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code

                   || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){

                    s->mb_type[i] &= ~MB_TYPE_INTER;

                    s->mb_type[i] |= MB_TYPE_INTRA;

                    s->p_mv_table[xy][0] = 0;

                    s->p_mv_table[xy][1] = 0;

                }

            }

            xy++;

            i++;

        }

    }

    if(s->flags&CODEC_FLAG_4MV){

        const int wrap= 2+ s->mb_width*2;

        /* clip / convert to intra 8x8 type MVs */

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

            int xy= (y*2 + 1)*wrap + 1;

            int i= y*s->mb_width;

            int x;

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

                if(s->mb_type[i]&MB_TYPE_INTER4V){

                    int block;

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

                        int off= (block& 1) + (block>>1)*wrap;

                        int mx= s->motion_val[ xy + off ][0];

                        int my= s->motion_val[ xy + off ][1];

                        if(   fcode_tab[mx + MAX_MV] > f_code

                           || fcode_tab[mx + MAX_MV] == 0

                           || fcode_tab[my + MAX_MV] > f_code

                           || fcode_tab[my + MAX_MV] == 0 ){

                            s->mb_type[i] &= ~MB_TYPE_INTER4V;

                            s->mb_type[i] |= MB_TYPE_INTRA;

                        }

                    }

                    xy+=2;

                    i++;

                }

            }

        }

    }

}


static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)

{

    PCMDVDContext *s = avctx->priv_data;

    /* Invalid header to force parsing of the first header */

    s->last_header = -1;

    /* reserve space for 8 channels, 3 bytes/sample, 4 samples/block */

    if (!(s->extra_samples = av_malloc(8 * 3 * 4)))

        return AVERROR(ENOMEM);

    s->extra_sample_count = 0;

    return 0;

}


void ff_h264_v_lpf_chroma_inter_msa(uint8_t *data, int img_width,

                                    int alpha, int beta, int8_t *tc)

{

    uint8_t bs0 = 1;

    uint8_t bs1 = 1;

    uint8_t bs2 = 1;

    uint8_t bs3 = 1;

    if (tc[0] < 0)

        bs0 = 0;

    if (tc[1] < 0)

        bs1 = 0;

    if (tc[2] < 0)

        bs2 = 0;

    if (tc[3] < 0)

        bs3 = 0;

    avc_loopfilter_cb_or_cr_inter_edge_hor_msa(data,

                                               bs0, bs1, bs2, bs3,

                                               tc[0], tc[1], tc[2], tc[3],

                                               alpha, beta,

                                               img_width);

}


static AVFrame *get_palette_frame(AVFilterContext *ctx)

{

    AVFrame *out;

    PaletteGenContext *s = ctx->priv;

    AVFilterLink *outlink = ctx->outputs[0];

    double ratio;

    int box_id = 0;

    struct range_box *box;

    /* reference only the used colors from histogram */

    s->refs = load_color_refs(s->histogram, s->nb_refs);

    if (!s->refs) {

        av_log(ctx, AV_LOG_ERROR, "Unable to allocate references for %d different colors\n", s->nb_refs);

        return NULL;

    }

    /* create the palette frame */

    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);

    if (!out)

        return NULL;

    out->pts = 0;

    /* set first box for 0..nb_refs */

    box = &s->boxes[box_id];

    box->len = s->nb_refs;

    box->sorted_by = -1;

    box->color = get_avg_color(s->refs, box);

    box->variance = -1;

    s->nb_boxes = 1;

    while (box && box->len > 1) {

        int i, rr, gr, br, longest;

        uint64_t median, box_weight = 0;

        /* compute the box weight (sum all the weights of the colors in the

         * range) and its boundings */

        uint8_t min[3] = {0xff, 0xff, 0xff};

        uint8_t max[3] = {0x00, 0x00, 0x00};

        for (i = box->start; i < box->start + box->len; i++) {

            const struct color_ref *ref = s->refs[i];

            const uint32_t rgb = ref->color;

            const uint8_t r = rgb >> 16 & 0xff, g = rgb >> 8 & 0xff, b = rgb & 0xff;

            min[0] = FFMIN(r, min[0]), max[0] = FFMAX(r, max[0]);

            min[1] = FFMIN(g, min[1]), max[1] = FFMAX(g, max[1]);

            min[2] = FFMIN(b, min[2]), max[2] = FFMAX(b, max[2]);

            box_weight += ref->count;

        }

        /* define the axis to sort by according to the widest range of colors */

        rr = max[0] - min[0];

        gr = max[1] - min[1];

        br = max[2] - min[2];

        longest = 1; // pick green by default (the color the eye is the most sensitive to)

        if (br >= rr && br >= gr) longest = 2;

        if (rr >= gr && rr >= br) longest = 0;

        if (gr >= rr && gr >= br) longest = 1; // prefer green again

        av_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" ranges:[%2x %2x %2x] sort by %c (already sorted:%c) ",

                box_id, box->start, box->start + box->len - 1, box->len, box_weight,

                rr, gr, br, "rgb"[longest], box->sorted_by == longest ? 'y':'n');

        /* sort the range by its longest axis if it's not already sorted */

        if (box->sorted_by != longest) {

            cmp_func cmpf = cmp_funcs[longest];

            AV_QSORT(&s->refs[box->start], box->len, const struct color_ref *, cmpf);

            box->sorted_by = longest;

        }

        /* locate the median where to split */

        median = (box_weight + 1) >> 1;

        box_weight = 0;

        /* if you have 2 boxes, the maximum is actually #0: you must have at

         * least 1 color on each side of the split, hence the -2 */

        for (i = box->start; i < box->start + box->len - 2; i++) {

            box_weight += s->refs[i]->count;

            if (box_weight > median)

                break;

        }

        av_dlog(ctx, "split @ i=%-6d with w=%-6"PRIu64" (target=%6"PRIu64")\n", i, box_weight, median);

        split_box(s, box, i);

        box_id = get_next_box_id_to_split(s);

        box = box_id >= 0 ? &s->boxes[box_id] : NULL;

    }

    ratio = set_colorquant_ratio_meta(out, s->nb_boxes, s->nb_refs);

    av_log(ctx, AV_LOG_INFO, "%d%s colors generated out of %d colors; ratio=%f\n",

           s->nb_boxes, s->reserve_transparent ? "(+1)" : "", s->nb_refs, ratio);

    qsort(s->boxes, s->nb_boxes, sizeof(*s->boxes), cmp_color);

    write_palette(ctx, out);

    return out;

}


static void sun4uv_init(MemoryRegion *address_space_mem,

                        MachineState *machine,

                        const struct hwdef *hwdef)

{

    SPARCCPU *cpu;

    M48t59State *nvram;

    unsigned int i;

    uint64_t initrd_addr, initrd_size, kernel_addr, kernel_size, kernel_entry;

    PCIBus *pci_bus, *pci_bus2, *pci_bus3;

    ISABus *isa_bus;

    qemu_irq *ivec_irqs, *pbm_irqs;

    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];

    DriveInfo *fd[MAX_FD];

    FWCfgState *fw_cfg;

    /* init CPUs */

    cpu = cpu_devinit(machine->cpu_model, hwdef);

    /* set up devices */

    ram_init(0, machine->ram_size);

    prom_init(hwdef->prom_addr, bios_name);

    ivec_irqs = qemu_allocate_irqs(cpu_set_ivec_irq, cpu, IVEC_MAX);

    pci_bus = pci_apb_init(APB_SPECIAL_BASE, APB_MEM_BASE, ivec_irqs, &pci_bus2,

                           &pci_bus3, &pbm_irqs);

    pci_vga_init(pci_bus);

    // XXX Should be pci_bus3

    isa_bus = pci_ebus_init(pci_bus, -1, pbm_irqs);

    i = 0;

    if (hwdef->console_serial_base) {

        serial_mm_init(address_space_mem, hwdef->console_serial_base, 0,

                       NULL, 115200, serial_hds[i], DEVICE_BIG_ENDIAN);

        i++;

    }

    for(; i < MAX_SERIAL_PORTS; i++) {

        if (serial_hds[i]) {

            serial_isa_init(isa_bus, i, serial_hds[i]);

        }

    }

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

        if (parallel_hds[i]) {

            parallel_init(isa_bus, i, parallel_hds[i]);

        }

    }

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

        pci_nic_init_nofail(&nd_table[i], pci_bus, "ne2k_pci", NULL);

    ide_drive_get(hd, MAX_IDE_BUS);

    pci_cmd646_ide_init(pci_bus, hd, 1);

    isa_create_simple(isa_bus, "i8042");

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

        fd[i] = drive_get(IF_FLOPPY, 0, i);

    }

    fdctrl_init_isa(isa_bus, fd);

    nvram = m48t59_init_isa(isa_bus, 0x0074, NVRAM_SIZE, 59);

    initrd_size = 0;

    initrd_addr = 0;

    kernel_size = sun4u_load_kernel(machine->kernel_filename,

                                    machine->initrd_filename,

                                    ram_size, &initrd_size, &initrd_addr,

                                    &kernel_addr, &kernel_entry);

    sun4u_NVRAM_set_params(nvram, NVRAM_SIZE, "Sun4u", machine->ram_size,

                           machine->boot_order,

                           kernel_addr, kernel_size,

                           machine->kernel_cmdline,

                           initrd_addr, initrd_size,

                           /* XXX: need an option to load a NVRAM image */

                           0,

                           graphic_width, graphic_height, graphic_depth,

                           (uint8_t *)&nd_table[0].macaddr);

    fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);

    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);

    fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);

    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);

    fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);

    fw_cfg_add_i64(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_entry);

    fw_cfg_add_i64(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);

    if (machine->kernel_cmdline) {

        fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,

                       strlen(machine->kernel_cmdline) + 1);

        fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, machine->kernel_cmdline);

    } else {

        fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 0);

    }

    fw_cfg_add_i64(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);

    fw_cfg_add_i64(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);

    fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, machine->boot_order[0]);

    fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_WIDTH, graphic_width);

    fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_HEIGHT, graphic_height);

    fw_cfg_add_i16(fw_cfg, FW_CFG_SPARC64_DEPTH, graphic_depth);

    qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);

}


static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,

                                  const char *key)

{

    AVIOContext *pb = s->pb;

    char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };

    int y, month, d, h, minute, sec;

    int ret;

    avio_seek(pb, offset, SEEK_SET);

    ret = avio_read(s->pb, string, DSS_TIME_SIZE);

    if (ret < DSS_TIME_SIZE)

        return ret < 0 ? ret : AVERROR_EOF;

    sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec);

    /* We deal with a two-digit year here, so set the default date to 2000

     * and hope it will never be used in the next century. */

    snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",

             y + 2000, month, d, h, minute, sec);

    return av_dict_set(&s->metadata, key, datetime, 0);

}


static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction)

{

    FDrive *cur_drv;

    uint8_t kh, kt, ks;

    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);

    cur_drv = get_cur_drv(fdctrl);

    kt = fdctrl->fifo[2];

    kh = fdctrl->fifo[3];

    ks = fdctrl->fifo[4];

    FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",

                   GET_CUR_DRV(fdctrl), kh, kt, ks,

                   fd_sector_calc(kh, kt, ks, cur_drv->last_sect,

                                  NUM_SIDES(cur_drv)));

    switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {

    case 2:

        /* sect too big */

        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);

        fdctrl->fifo[3] = kt;

        fdctrl->fifo[4] = kh;

        fdctrl->fifo[5] = ks;

        return;

    case 3:

        /* track too big */

        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);

        fdctrl->fifo[3] = kt;

        fdctrl->fifo[4] = kh;

        fdctrl->fifo[5] = ks;

        return;

    case 4:

        /* No seek enabled */

        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);

        fdctrl->fifo[3] = kt;

        fdctrl->fifo[4] = kh;

        fdctrl->fifo[5] = ks;

        return;

    case 1:

        fdctrl->status0 |= FD_SR0_SEEK;

        break;

    default:

        break;

    }

    /* Check the data rate. If the programmed data rate does not match

     * the currently inserted medium, the operation has to fail. */

    if (fdctrl->check_media_rate &&

        (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) {

        FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n",

                       fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate);

        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00);

        fdctrl->fifo[3] = kt;

        fdctrl->fifo[4] = kh;

        fdctrl->fifo[5] = ks;

        return;

    }

    /* Set the FIFO state */

    fdctrl->data_dir = direction;

    fdctrl->data_pos = 0;

    fdctrl->msr |= FD_MSR_CMDBUSY;

    if (fdctrl->fifo[0] & 0x80)

        fdctrl->data_state |= FD_STATE_MULTI;

    else

        fdctrl->data_state &= ~FD_STATE_MULTI;

    if (fdctrl->fifo[5] == 00) {

        fdctrl->data_len = fdctrl->fifo[8];

    } else {

        int tmp;

        fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]);

        tmp = (fdctrl->fifo[6] - ks + 1);

        if (fdctrl->fifo[0] & 0x80)

            tmp += fdctrl->fifo[6];

        fdctrl->data_len *= tmp;

    }

    fdctrl->eot = fdctrl->fifo[6];

    if (fdctrl->dor & FD_DOR_DMAEN) {

        int dma_mode;

        /* DMA transfer are enabled. Check if DMA channel is well programmed */

        dma_mode = DMA_get_channel_mode(fdctrl->dma_chann);

        dma_mode = (dma_mode >> 2) & 3;

        FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",

                       dma_mode, direction,

                       (128 << fdctrl->fifo[5]) *

                       (cur_drv->last_sect - ks + 1), fdctrl->data_len);

        if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||

              direction == FD_DIR_SCANH) && dma_mode == 0) ||

            (direction == FD_DIR_WRITE && dma_mode == 2) ||

            (direction == FD_DIR_READ && dma_mode == 1)) {

            /* No access is allowed until DMA transfer has completed */

            fdctrl->msr &= ~FD_MSR_RQM;

            /* Now, we just have to wait for the DMA controller to

             * recall us...

             */

            DMA_hold_DREQ(fdctrl->dma_chann);

            DMA_schedule(fdctrl->dma_chann);

            return;

        } else {

            FLOPPY_DPRINTF("bad dma_mode=%d direction=%d\n", dma_mode,

                           direction);

        }

    }

    FLOPPY_DPRINTF("start non-DMA transfer\n");

    fdctrl->msr |= FD_MSR_NONDMA;

    if (direction != FD_DIR_WRITE)

        fdctrl->msr |= FD_MSR_DIO;

    /* IO based transfer: calculate len */

    fdctrl_raise_irq(fdctrl);

}


static int mxf_write_footer(AVFormatContext *s)
{
    MXFContext *mxf = s->priv_data;
    AVIOContext *pb = s->pb;
    int err = 0;
    mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
    mxf_write_klv_fill(s);
    mxf->footer_partition_offset = avio_tell(pb);
    if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) { // no need to repeat index
        if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
    } else {
        if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
        mxf_write_klv_fill(s);
        mxf_write_index_table_segment(s);
    mxf_write_klv_fill(s);
    mxf_write_random_index_pack(s);
    if (s->pb->seekable) {
        if (s->oformat == &ff_mxf_opatom_muxer){
            /* rewrite body partition to update lengths */
            avio_seek(pb, mxf->body_partition_offset[0], SEEK_SET);
            if ((err = mxf_write_opatom_body_partition(s)) < 0)
        avio_seek(pb, 0, SEEK_SET);
        if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) {
            if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
            mxf_write_klv_fill(s);
            mxf_write_index_table_segment(s);
        } else {
            if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
end:
    ff_audio_interleave_close(s);
    av_freep(&mxf->index_entries);
    av_freep(&mxf->body_partition_offset);
    av_freep(&mxf->timecode_track->priv_data);
    av_freep(&mxf->timecode_track);
    mxf_free(s);
    return err < 0 ? err : 0;


static int inet_listen_saddr(InetSocketAddress *saddr,

                             int port_offset,

                             bool update_addr,

                             Error **errp)

{

    struct addrinfo ai,*res,*e;

    char port[33];

    char uaddr[INET6_ADDRSTRLEN+1];

    char uport[33];

    int rc, port_min, port_max, p;

    int slisten = 0;

    int saved_errno = 0;

    bool socket_created = false;

    Error *err = NULL;

    memset(&ai,0, sizeof(ai));

    ai.ai_flags = AI_PASSIVE;

    if (saddr->has_numeric && saddr->numeric) {

        ai.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;

    }

    ai.ai_family = inet_ai_family_from_address(saddr, &err);

    ai.ai_socktype = SOCK_STREAM;

    if (err) {

        error_propagate(errp, err);

        return -1;

    }

    if (saddr->host == NULL) {

        error_setg(errp, "host not specified");

        return -1;

    }

    if (saddr->port != NULL) {

        pstrcpy(port, sizeof(port), saddr->port);

    } else {

        port[0] = '\0';

    }

    /* lookup */

    if (port_offset) {

        unsigned long long baseport;

        if (strlen(port) == 0) {

            error_setg(errp, "port not specified");

            return -1;

        }

        if (parse_uint_full(port, &baseport, 10) < 0) {

            error_setg(errp, "can't convert to a number: %s", port);

            return -1;

        }

        if (baseport > 65535 ||

            baseport + port_offset > 65535) {

            error_setg(errp, "port %s out of range", port);

            return -1;

        }

        snprintf(port, sizeof(port), "%d", (int)baseport + port_offset);

    }

    rc = getaddrinfo(strlen(saddr->host) ? saddr->host : NULL,

                     strlen(port) ? port : NULL, &ai, &res);

    if (rc != 0) {

        error_setg(errp, "address resolution failed for %s:%s: %s",

                   saddr->host, port, gai_strerror(rc));

        return -1;

    }

    /* create socket + bind/listen */

    for (e = res; e != NULL; e = e->ai_next) {

        getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,

		        uaddr,INET6_ADDRSTRLEN,uport,32,

		        NI_NUMERICHOST | NI_NUMERICSERV);

        slisten = create_fast_reuse_socket(e);

        if (slisten < 0) {

            continue;

        }

        socket_created = true;

        port_min = inet_getport(e);

        port_max = saddr->has_to ? saddr->to + port_offset : port_min;

        for (p = port_min; p <= port_max; p++) {

            inet_setport(e, p);

            rc = try_bind(slisten, saddr, e);

            if (rc) {

                if (errno == EADDRINUSE) {

                    continue;

                } else {

                    error_setg_errno(errp, errno, "Failed to bind socket");

                    goto listen_failed;

                }

            }

            if (!listen(slisten, 1)) {

                goto listen_ok;

            }

            if (errno != EADDRINUSE) {

                error_setg_errno(errp, errno, "Failed to listen on socket");

                goto listen_failed;

            }

            /* Someone else managed to bind to the same port and beat us

             * to listen on it! Socket semantics does not allow us to

             * recover from this situation, so we need to recreate the

             * socket to allow bind attempts for subsequent ports:

             */

            closesocket(slisten);

            slisten = create_fast_reuse_socket(e);

            if (slisten < 0) {

                error_setg_errno(errp, errno,

                                 "Failed to recreate failed listening socket");

                goto listen_failed;

            }

        }

    }

    error_setg_errno(errp, errno,

                     socket_created ?

                     "Failed to find an available port" :

                     "Failed to create a socket");

listen_failed:

    saved_errno = errno;

    if (slisten >= 0) {

        closesocket(slisten);

    }

    freeaddrinfo(res);

    errno = saved_errno;

    return -1;

listen_ok:

    if (update_addr) {

        g_free(saddr->host);

        saddr->host = g_strdup(uaddr);

        g_free(saddr->port);

        saddr->port = g_strdup_printf("%d",

                                      inet_getport(e) - port_offset);

        saddr->has_ipv6 = saddr->ipv6 = e->ai_family == PF_INET6;

        saddr->has_ipv4 = saddr->ipv4 = e->ai_family != PF_INET6;

    }

    freeaddrinfo(res);

    return slisten;

}


static void *circular_buffer_task( void *_URLContext)

{

    URLContext *h = _URLContext;

    UDPContext *s = h->priv_data;

    int old_cancelstate;

    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);

    ff_socket_nonblock(s->udp_fd, 0);

    while(1) {

        int left;

        int len;

        /* How much do we have left to the end of the buffer */

        /* Whats the minimum we can read so that we dont comletely fill the buffer */

        left = av_fifo_space(s->fifo);

        /* Blocking operations are always cancellation points;

           see "General Information" / "Thread Cancelation Overview"

           in Single Unix. */

        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);

        len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0);

        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);

        if (len < 0) {

            if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {

                s->circular_buffer_error = AVERROR(EIO);

                goto end;

            }

            continue;

        }

        AV_WL32(s->tmp, len);

        if(left < len + 4) {

            /* No Space left */

            if (s->overrun_nonfatal) {

                av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "

                        "Surviving due to overrun_nonfatal option\n");

                continue;

            } else {

                av_log(h, AV_LOG_ERROR, "Circular buffer overrun. "

                        "To avoid, increase fifo_size URL option. "

                        "To survive in such case, use overrun_nonfatal option\n");

                s->circular_buffer_error = AVERROR(EIO);

                goto end;

            }

        }

        pthread_mutex_lock(&s->mutex);

        av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);

        pthread_cond_signal(&s->cond);

        pthread_mutex_unlock(&s->mutex);

    }

end:

    pthread_mutex_lock(&s->mutex);

    pthread_cond_signal(&s->cond);

    pthread_mutex_unlock(&s->mutex);

    return NULL;

}


static void FUNCC(pred8x8l_horizontal)(uint8_t *_src, int has_topleft, int has_topright, int _stride)

{

    pixel *src = (pixel*)_src;

    int stride = _stride/sizeof(pixel);

    PREDICT_8x8_LOAD_LEFT;

#define ROW(y) ((pixel4*)(src+y*stride))[0] =\

               ((pixel4*)(src+y*stride))[1] = PIXEL_SPLAT_X4(l##y)

    ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);

#undef ROW

}


static int g726_init(AVCodecContext * avctx)

{

    AVG726Context* c = (AVG726Context*)avctx->priv_data;

    if (avctx->sample_rate != 8000 || avctx->channels != 1 ||

        (avctx->bit_rate != 16000 && avctx->bit_rate != 24000 &&

	 avctx->bit_rate != 32000 && avctx->bit_rate != 40000)) {

        av_log(avctx, AV_LOG_ERROR, "G726: unsupported audio format\n");

	return -1;

    }

    g726_reset(&c->c, avctx->bit_rate);

    c->code_size = c->c.tbls->bits;

    c->bit_buffer = 0;

    c->bits_left = 0;

    return 0;

}


static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)

{

    MpegEncContext *s = &ctx->m;

    char buf[256];

    int i;

    int e;

    int ver = 0, build = 0, ver2 = 0, ver3 = 0;

    char last;

    for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {

        if (show_bits(gb, 23) == 0)

            break;

        buf[i] = get_bits(gb, 8);

    }

    buf[i] = 0;

    /* divx detection */

    e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);

    if (e < 2)

        e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);

    if (e >= 2) {

        ctx->divx_version = ver;

        ctx->divx_build   = build;

        s->divx_packed  = e == 3 && last == 'p';

    }

    /* libavcodec detection */

    e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;

    if (e != 4)

        e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);

    if (e != 4) {

        e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;

        if (e > 1) {

            if (ver > 0xFF || ver2 > 0xFF || ver3 > 0xFF) {

                av_log(s->avctx, AV_LOG_WARNING,

                     "Unknown Lavc version string encountered, %d.%d.%d; "

                     "clamping sub-version values to 8-bits.\n",

                     ver, ver2, ver3);

            }

            build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);

        }

    }

    if (e != 4) {

        if (strcmp(buf, "ffmpeg") == 0)

            ctx->lavc_build = 4600;

    }

    if (e == 4)

        ctx->lavc_build = build;

    /* Xvid detection */

    e = sscanf(buf, "XviD%d", &build);

    if (e == 1)

        ctx->xvid_build = build;

    return 0;

}


int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,

                      AVCodecContext *codec, int size)

{

    int id;

    if (size < 14)

        return AVERROR_INVALIDDATA;

    id                 = avio_rl16(pb);

    codec->codec_type  = AVMEDIA_TYPE_AUDIO;

    codec->channels    = avio_rl16(pb);

    codec->sample_rate = avio_rl32(pb);

    codec->bit_rate    = avio_rl32(pb) * 8;

    codec->block_align = avio_rl16(pb);

    if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */

        codec->bits_per_coded_sample = 8;

    } else

        codec->bits_per_coded_sample = avio_rl16(pb);

    if (id == 0xFFFE) {

        codec->codec_tag = 0;

    } else {

        codec->codec_tag = id;

        codec->codec_id  = ff_wav_codec_get_id(id,

                                               codec->bits_per_coded_sample);

    }

    if (size >= 18) {  /* We're obviously dealing with WAVEFORMATEX */

        int cbSize = avio_rl16(pb); /* cbSize */

        size  -= 18;

        cbSize = FFMIN(size, cbSize);

        if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */

            parse_waveformatex(pb, codec);

            cbSize -= 22;

            size   -= 22;

        }

        codec->extradata_size = cbSize;

        if (cbSize > 0) {

            av_free(codec->extradata);

            codec->extradata = av_mallocz(codec->extradata_size +

                                          FF_INPUT_BUFFER_PADDING_SIZE);

            if (!codec->extradata)

                return AVERROR(ENOMEM);

            avio_read(pb, codec->extradata, codec->extradata_size);

            size -= cbSize;

        }

        /* It is possible for the chunk to contain garbage at the end */

        if (size > 0)

            avio_skip(pb, size);

    }

    if (codec->sample_rate <= 0) {

        av_log(s, AV_LOG_ERROR,

               "Invalid sample rate: %d\n", codec->sample_rate);

        return AVERROR_INVALIDDATA;

    }

    if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {

        /* Channels and sample_rate values are those prior to applying SBR

         * and/or PS. */

        codec->channels    = 0;

        codec->sample_rate = 0;

    }

    /* override bits_per_coded_sample for G.726 */

    if (codec->codec_id == AV_CODEC_ID_ADPCM_G726)

        codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;

    return 0;

}


static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_cue)

{

    MatroskaMuxContext *mkv = s->priv_data;

    AVIOContext *pb         = s->pb;

    AVCodecParameters *par  = s->streams[pkt->stream_index]->codecpar;

    int keyframe            = !!(pkt->flags & AV_PKT_FLAG_KEY);

    int duration            = pkt->duration;

    int ret;

    int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;

    int64_t relative_packet_pos;

    int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;

    if (ts == AV_NOPTS_VALUE) {

        av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");

        return AVERROR(EINVAL);

    }

    ts += mkv->tracks[pkt->stream_index].ts_offset;

    if (mkv->cluster_pos != -1) {

        int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;

        if ((int16_t)cluster_time != cluster_time) {

            av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");

            mkv_start_new_cluster(s, pkt);

        }

    }

    if (mkv->cluster_pos == -1) {

        mkv->cluster_pos = avio_tell(s->pb);

        ret = start_ebml_master_crc32(s->pb, &mkv->dyn_bc, &mkv->cluster, MATROSKA_ID_CLUSTER, 0);

        if (ret < 0)

            return ret;

        put_ebml_uint(mkv->dyn_bc, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));

        mkv->cluster_pts = FFMAX(0, ts);

    }

    pb = mkv->dyn_bc;

    relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos + avio_tell(pb);

    if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {

        mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe);

        if (s->pb->seekable && (par->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue)) {

            ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);

            if (ret < 0) return ret;

        }

    } else {

        if (par->codec_id == AV_CODEC_ID_WEBVTT) {

            duration = mkv_write_vtt_blocks(s, pb, pkt);

        } else {

            ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,

                                                       mkv_blockgroup_size(pkt->size));

#if FF_API_CONVERGENCE_DURATION

FF_DISABLE_DEPRECATION_WARNINGS

            /* For backward compatibility, prefer convergence_duration. */

            if (pkt->convergence_duration > 0) {

                duration = pkt->convergence_duration;

            }

FF_ENABLE_DEPRECATION_WARNINGS

#endif

            /* All subtitle blocks are considered to be keyframes. */

            mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 1);

            put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);

            end_ebml_master(pb, blockgroup);

        }

        if (s->pb->seekable) {

            ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,

                                   mkv->cluster_pos, relative_packet_pos, duration);

            if (ret < 0)

                return ret;

        }

    }

    mkv->duration = FFMAX(mkv->duration, ts + duration);

    if (mkv->stream_durations)

        mkv->stream_durations[pkt->stream_index] =

            FFMAX(mkv->stream_durations[pkt->stream_index], ts + duration);

    return 0;

}


static av_cold int XAVS_init(AVCodecContext *avctx)

{

    XavsContext *x4 = avctx->priv_data;

    x4->sei_size = 0;

    xavs_param_default(&x4->params);

    x4->params.pf_log               = XAVS_log;

    x4->params.p_log_private        = avctx;

    x4->params.i_keyint_max         = avctx->gop_size;

    if (avctx->bit_rate) {

        x4->params.rc.i_bitrate   = avctx->bit_rate / 1000;

        x4->params.rc.i_rc_method = XAVS_RC_ABR;

    }

    x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;

    x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate    / 1000;

    x4->params.rc.b_stat_write      = avctx->flags & AV_CODEC_FLAG_PASS1;

    if (avctx->flags & AV_CODEC_FLAG_PASS2) {

        x4->params.rc.b_stat_read = 1;

    } else {

        if (x4->crf >= 0) {

            x4->params.rc.i_rc_method   = XAVS_RC_CRF;

            x4->params.rc.f_rf_constant = x4->crf;

        } else if (x4->cqp >= 0) {

            x4->params.rc.i_rc_method   = XAVS_RC_CQP;

            x4->params.rc.i_qp_constant = x4->cqp;

        }

    }

    if (x4->aud >= 0)

        x4->params.b_aud                      = x4->aud;

    if (x4->mbtree >= 0)

        x4->params.rc.b_mb_tree               = x4->mbtree;

    if (x4->direct_pred >= 0)

        x4->params.analyse.i_direct_mv_pred   = x4->direct_pred;

    if (x4->fast_pskip >= 0)

        x4->params.analyse.b_fast_pskip       = x4->fast_pskip;

    if (x4->motion_est >= 0)

        x4->params.analyse.i_me_method        = x4->motion_est;

    if (x4->mixed_refs >= 0)

        x4->params.analyse.b_mixed_references = x4->mixed_refs;

    if (x4->b_bias != INT_MIN)

        x4->params.i_bframe_bias              = x4->b_bias;

    if (x4->cplxblur >= 0)

        x4->params.rc.f_complexity_blur = x4->cplxblur;

#if FF_API_MOTION_EST

FF_DISABLE_DEPRECATION_WARNINGS

    if (x4->motion_est < 0) {

        switch (avctx->me_method) {

        case  ME_EPZS:

            x4->params.analyse.i_me_method = XAVS_ME_DIA;

            break;

        case  ME_HEX:

            x4->params.analyse.i_me_method = XAVS_ME_HEX;

            break;

        case  ME_UMH:

            x4->params.analyse.i_me_method = XAVS_ME_UMH;

            break;

        case  ME_FULL:

            x4->params.analyse.i_me_method = XAVS_ME_ESA;

            break;

        case  ME_TESA:

            x4->params.analyse.i_me_method = XAVS_ME_TESA;

            break;

        default:

            x4->params.analyse.i_me_method = XAVS_ME_HEX;

        }

    }

FF_ENABLE_DEPRECATION_WARNINGS

#endif

    x4->params.i_bframe          = avctx->max_b_frames;

    /* cabac is not included in AVS JiZhun Profile */

    x4->params.b_cabac           = 0;

    x4->params.i_bframe_adaptive = avctx->b_frame_strategy;

    avctx->has_b_frames          = !!avctx->max_b_frames;

    /* AVS doesn't allow B picture as reference */

    /* The max allowed reference frame number of B is 2 */

    x4->params.i_keyint_min      = avctx->keyint_min;

    if (x4->params.i_keyint_min > x4->params.i_keyint_max)

        x4->params.i_keyint_min = x4->params.i_keyint_max;

    x4->params.i_scenecut_threshold        = avctx->scenechange_threshold;

   // x4->params.b_deblocking_filter       = avctx->flags & AV_CODEC_FLAG_LOOP_FILTER;

    x4->params.rc.i_qp_min                 = avctx->qmin;

    x4->params.rc.i_qp_max                 = avctx->qmax;

    x4->params.rc.i_qp_step                = avctx->max_qdiff;

    x4->params.rc.f_qcompress       = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */

    x4->params.rc.f_qblur           = avctx->qblur;     /* temporally blur quants */

    x4->params.i_frame_reference    = avctx->refs;

    x4->params.i_width              = avctx->width;

    x4->params.i_height             = avctx->height;

    x4->params.vui.i_sar_width      = avctx->sample_aspect_ratio.num;

    x4->params.vui.i_sar_height     = avctx->sample_aspect_ratio.den;

    /* This is only used for counting the fps */

    x4->params.i_fps_num            = avctx->time_base.den;

    x4->params.i_fps_den            = avctx->time_base.num;

    x4->params.analyse.inter        = XAVS_ANALYSE_I8x8 |XAVS_ANALYSE_PSUB16x16| XAVS_ANALYSE_BSUB16x16;

    x4->params.analyse.i_me_range = avctx->me_range;

    x4->params.analyse.i_subpel_refine    = avctx->me_subpel_quality;

    x4->params.analyse.b_chroma_me        = avctx->me_cmp & FF_CMP_CHROMA;

    /* AVS P2 only enables 8x8 transform */

    x4->params.analyse.b_transform_8x8    = 1; //avctx->flags2 & AV_CODEC_FLAG2_8X8DCT;

    x4->params.analyse.i_trellis          = avctx->trellis;

    x4->params.analyse.i_noise_reduction  = avctx->noise_reduction;

    if (avctx->level > 0)

        x4->params.i_level_idc = avctx->level;

    if (avctx->bit_rate > 0)

        x4->params.rc.f_rate_tolerance =

            (float)avctx->bit_rate_tolerance / avctx->bit_rate;

    if ((avctx->rc_buffer_size) &&

        (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {

        x4->params.rc.f_vbv_buffer_init =

            (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;

    } else

        x4->params.rc.f_vbv_buffer_init = 0.9;

    /* TAG:do we have MB tree RC method */

    /* what is the RC method we are now using? Default NO */

    x4->params.rc.f_ip_factor             = 1 / fabs(avctx->i_quant_factor);

    x4->params.rc.f_pb_factor             = avctx->b_quant_factor;

    x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;

    x4->params.analyse.b_psnr = avctx->flags & AV_CODEC_FLAG_PSNR;

    x4->params.i_log_level    = XAVS_LOG_DEBUG;

    x4->params.i_threads      = avctx->thread_count;

    x4->params.b_interlaced   = avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT;

    if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)

        x4->params.b_repeat_headers = 0;

    x4->enc = xavs_encoder_open(&x4->params);

    if (!x4->enc)

        return -1;

    if (!(x4->pts_buffer = av_mallocz((avctx->max_b_frames+1) * sizeof(*x4->pts_buffer))))

        return AVERROR(ENOMEM);

    /* TAG: Do we have GLOBAL HEADER in AVS */

    /* We Have PPS and SPS in AVS */

    if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {

        xavs_nal_t *nal;

        int nnal, s, i, size;

        uint8_t *p;

        s = xavs_encoder_headers(x4->enc, &nal, &nnal);

        avctx->extradata = p = av_malloc(s);

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

            /* Don't put the SEI in extradata. */

            if (nal[i].i_type == NAL_SEI) {

                x4->sei = av_malloc( 5 + nal[i].i_payload * 4 / 3 );

                if (xavs_nal_encode(x4->sei, &x4->sei_size, 1, nal + i) < 0)

                    return -1;

                continue;

            }

            size = xavs_nal_encode(p, &s, 1, nal + i);

            if (size < 0)

                return -1;

            p += size;

        }

        avctx->extradata_size = p - avctx->extradata;

    }

    return 0;

}


static int film_read_close(AVFormatContext *s)

{

    FilmDemuxContext *film = s->priv_data;

    av_freep(&film->sample_table);

    av_freep(&film->stereo_buffer);

    return 0;

}


static av_cold int png_enc_close(AVCodecContext *avctx)

{

    av_frame_free(&avctx->coded_frame);

    return 0;

}


int opt_opencl_bench(void *optctx, const char *opt, const char *arg)

{

    int i, j, nb_devices = 0, count = 0;

    int64_t score = 0;

    AVOpenCLDeviceList *device_list;

    AVOpenCLDeviceNode *device_node = NULL;

    OpenCLDeviceBenchmark *devices = NULL;

    cl_platform_id platform;

    av_opencl_get_device_list(&device_list);

    for (i = 0; i < device_list->platform_num; i++)

        nb_devices += device_list->platform_node[i]->device_num;

    if (!nb_devices) {

        av_log(NULL, AV_LOG_ERROR, "No OpenCL device detected!\n");

        return AVERROR(EINVAL);

    }

    if (!(devices = av_malloc_array(nb_devices, sizeof(OpenCLDeviceBenchmark)))) {

        av_log(NULL, AV_LOG_ERROR, "Could not allocate buffer\n");

        return AVERROR(ENOMEM);

    }

    for (i = 0; i < device_list->platform_num; i++) {

        for (j = 0; j < device_list->platform_node[i]->device_num; j++) {

            device_node = device_list->platform_node[i]->device_node[j];

            platform = device_list->platform_node[i]->platform_id;

            score = av_opencl_benchmark(device_node, platform, run_opencl_bench);

            if (score > 0) {

                devices[count].platform_idx = i;

                devices[count].device_idx = j;

                devices[count].runtime = score;

                av_strlcpy(devices[count].device_name, device_node->device_name,

                           sizeof(devices[count].device_name));

                count++;

            }

        }

    }

    qsort(devices, count, sizeof(OpenCLDeviceBenchmark), compare_ocl_device_desc);

    fprintf(stderr, "platform_idx\tdevice_idx\tdevice_name\truntime\n");

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

        fprintf(stdout, "%d\t%d\t%s\t%"PRId64"\n",

                devices[i].platform_idx, devices[i].device_idx,

                devices[i].device_name, devices[i].runtime);

    av_opencl_free_device_list(&device_list);

    av_free(devices);

    return 0;

}


void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)

{

#if !defined(FLUSH_ALL_TLBS)

    addr &= TARGET_PAGE_MASK;

    switch (env->mmu_model) {

    case POWERPC_MMU_SOFT_6xx:

    case POWERPC_MMU_SOFT_74xx:

        ppc6xx_tlb_invalidate_virt(env, addr, 0);

        if (env->id_tlbs == 1)

            ppc6xx_tlb_invalidate_virt(env, addr, 1);

        break;

    case POWERPC_MMU_SOFT_4xx:

    case POWERPC_MMU_SOFT_4xx_Z:

        ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);

        break;

    case POWERPC_MMU_REAL_4xx:

        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");

        break;

    case POWERPC_MMU_BOOKE:

        /* XXX: TODO */

        cpu_abort(env, "MMU model not implemented\n");

        break;

    case POWERPC_MMU_BOOKE_FSL:

        /* XXX: TODO */

        cpu_abort(env, "MMU model not implemented\n");

        break;

    case POWERPC_MMU_32B:

    case POWERPC_MMU_601:

        /* tlbie invalidate TLBs for all segments */

        addr &= ~((target_ulong)-1 << 28);

        /* XXX: this case should be optimized,

         * giving a mask to tlb_flush_page

         */

        tlb_flush_page(env, addr | (0x0 << 28));

        tlb_flush_page(env, addr | (0x1 << 28));

        tlb_flush_page(env, addr | (0x2 << 28));

        tlb_flush_page(env, addr | (0x3 << 28));

        tlb_flush_page(env, addr | (0x4 << 28));

        tlb_flush_page(env, addr | (0x5 << 28));

        tlb_flush_page(env, addr | (0x6 << 28));

        tlb_flush_page(env, addr | (0x7 << 28));

        tlb_flush_page(env, addr | (0x8 << 28));

        tlb_flush_page(env, addr | (0x9 << 28));

        tlb_flush_page(env, addr | (0xA << 28));

        tlb_flush_page(env, addr | (0xB << 28));

        tlb_flush_page(env, addr | (0xC << 28));

        tlb_flush_page(env, addr | (0xD << 28));

        tlb_flush_page(env, addr | (0xE << 28));

        tlb_flush_page(env, addr | (0xF << 28));

        break;

#if defined(TARGET_PPC64)

    case POWERPC_MMU_64B:

        /* tlbie invalidate TLBs for all segments */

        /* XXX: given the fact that there are too many segments to invalidate,

         *      and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,

         *      we just invalidate all TLBs

         */

        tlb_flush(env, 1);

        break;

#endif /* defined(TARGET_PPC64) */

    default:

        /* XXX: TODO */

        cpu_abort(env, "Unknown MMU model\n");

        break;

    }

#else

    ppc_tlb_invalidate_all(env);

#endif

}


static void test_visitor_in_fail_list(TestInputVisitorData *data,
                                      const void *unused)
{
    int64_t i64 = -1;
    Error *err = NULL;
    Visitor *v;
    /* Unvisited list tail */
    v = visitor_input_test_init(data, "[ 1, 2, 3 ]");
    visit_type_int(v, NULL, &i64, &error_abort);
    g_assert_cmpint(i64, ==, 1);
    visit_type_int(v, NULL, &i64, &error_abort);
    g_assert_cmpint(i64, ==, 2);
    visit_check_list(v, &err);
}


int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar)

{

    CPUS390XState *env = &cpu->env;

    uint32_t fh;

    ZpciFib fib;

    S390PCIBusDevice *pbdev;

    uint32_t data;

    uint64_t cc = ZPCI_PCI_LS_OK;

    if (env->psw.mask & PSW_MASK_PSTATE) {

        program_interrupt(env, PGM_PRIVILEGED, 6);

        return 0;

    }

    fh = env->regs[r1] >> 32;

    if (fiba & 0x7) {

        program_interrupt(env, PGM_SPECIFICATION, 6);

        return 0;

    }

    pbdev = s390_pci_find_dev_by_fh(fh);

    if (!pbdev) {

        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);

        return 0;

    }

    memset(&fib, 0, sizeof(fib));

    switch (pbdev->state) {

    case ZPCI_FS_RESERVED:

    case ZPCI_FS_STANDBY:

        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);

        return 0;

    case ZPCI_FS_DISABLED:

        if (fh & FH_MASK_ENABLE) {

            setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);

            return 0;

        }

        goto out;

    /* BLOCKED bit is set to one coincident with the setting of ERROR bit.

     * FH Enabled bit is set to one in states of ENABLED, BLOCKED or ERROR. */

    case ZPCI_FS_ERROR:

        fib.fc |= 0x20;

    case ZPCI_FS_BLOCKED:

        fib.fc |= 0x40;

    case ZPCI_FS_ENABLED:

        fib.fc |= 0x80;

        if (pbdev->iommu_enabled) {

            fib.fc |= 0x10;

        }

        if (!(fh & FH_MASK_ENABLE)) {

            env->regs[r1] |= 1ULL << 63;

        }

        break;

    case ZPCI_FS_PERMANENT_ERROR:

        setcc(cpu, ZPCI_PCI_LS_ERR);

        s390_set_status_code(env, r1, ZPCI_STPCIFC_ST_PERM_ERROR);

        return 0;

    }

    stq_p(&fib.pba, pbdev->pba);

    stq_p(&fib.pal, pbdev->pal);

    stq_p(&fib.iota, pbdev->g_iota);

    stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);

    stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);

    stq_p(&fib.fmb_addr, pbdev->fmb_addr);

    data = ((uint32_t)pbdev->isc << 28) | ((uint32_t)pbdev->noi << 16) |

           ((uint32_t)pbdev->routes.adapter.ind_offset << 8) |

           ((uint32_t)pbdev->sum << 7) | pbdev->routes.adapter.summary_offset;

    stl_p(&fib.data, data);

out:

    if (s390_cpu_virt_mem_write(cpu, fiba, ar, (uint8_t *)&fib, sizeof(fib))) {

        return 0;

    }

    setcc(cpu, cc);

    return 0;

}


static void mips_cpu_initfn(Object *obj)

{

    CPUState *cs = CPU(obj);

    MIPSCPU *cpu = MIPS_CPU(obj);

    CPUMIPSState *env = &cpu->env;

    cs->env_ptr = env;

    cpu_exec_init(cs, &error_abort);

    if (tcg_enabled()) {

        mips_tcg_init();

    }

}


static av_cold int omx_try_load(OMXContext *s, void *logctx,

                                const char *libname, const char *prefix)

{

    s->lib = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);

    if (!s->lib) {

        av_log(logctx, AV_LOG_WARNING, "%s not found\n", libname);

        return AVERROR_ENCODER_NOT_FOUND;

    }

    s->ptr_Init                = dlsym_prefixed(s->lib, "OMX_Init", prefix);

    s->ptr_Deinit              = dlsym_prefixed(s->lib, "OMX_Deinit", prefix);

    s->ptr_ComponentNameEnum   = dlsym_prefixed(s->lib, "OMX_ComponentNameEnum", prefix);

    s->ptr_GetHandle           = dlsym_prefixed(s->lib, "OMX_GetHandle", prefix);

    s->ptr_FreeHandle          = dlsym_prefixed(s->lib, "OMX_FreeHandle", prefix);

    s->ptr_GetComponentsOfRole = dlsym_prefixed(s->lib, "OMX_GetComponentsOfRole", prefix);

    s->ptr_GetRolesOfComponent = dlsym_prefixed(s->lib, "OMX_GetRolesOfComponent", prefix);

    if (!s->ptr_Init || !s->ptr_Deinit || !s->ptr_ComponentNameEnum ||

        !s->ptr_GetHandle || !s->ptr_FreeHandle ||

        !s->ptr_GetComponentsOfRole || !s->ptr_GetRolesOfComponent) {

        av_log(logctx, AV_LOG_WARNING, "Not all functions found in %s\n", libname);

        dlclose(s->lib);

        s->lib = NULL;

        return AVERROR_ENCODER_NOT_FOUND;

    }

    return 0;

}