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

{

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

    int size = pkt->size;

    uint8_t *buf = pkt->data;

    uint8_t *data = NULL;

    MpegTSWrite *ts = s->priv_data;

    MpegTSWriteStream *ts_st = st->priv_data;

    const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;

    int64_t dts = pkt->dts, pts = pkt->pts;

    if (ts->reemit_pat_pmt) {

        av_log(s, AV_LOG_WARNING,

               "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");

        ts->reemit_pat_pmt = 0;

        ts->flags         |= MPEGTS_FLAG_REEMIT_PAT_PMT;

    }

    if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {

        ts->pat_packet_count = ts->pat_packet_period - 1;

        ts->sdt_packet_count = ts->sdt_packet_period - 1;

        ts->flags           &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;

    }

    if (ts->copyts < 1) {

        if (pts != AV_NOPTS_VALUE)

            pts += delay;

        if (dts != AV_NOPTS_VALUE)

            dts += delay;

    }

    if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {

        av_log(s, AV_LOG_ERROR, "first pts value must be set\n");

        return AVERROR_INVALIDDATA;

    }

    ts_st->first_pts_check = 0;

    if (st->codec->codec_id == AV_CODEC_ID_H264) {

        const uint8_t *p = buf, *buf_end = p + size;

        uint32_t state = -1;

        int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codec->extradata_size : 0;

        int ret = ff_check_h264_startcode(s, st, pkt);

        if (ret < 0)

            return ret;

        if (extradd && AV_RB24(st->codec->extradata) > 1)

            extradd = 0;

        do {

            p = avpriv_find_start_code(p, buf_end, &state);

            av_log(s, AV_LOG_TRACE, "nal %d\n", state & 0x1f);

            if ((state & 0x1f) == 7)

                extradd = 0;

        } while (p < buf_end && (state & 0x1f) != 9 &&

                 (state & 0x1f) != 5 && (state & 0x1f) != 1);

        if ((state & 0x1f) != 5)

            extradd = 0;

        if ((state & 0x1f) != 9) { // AUD NAL

            data = av_malloc(pkt->size + 6 + extradd);

            if (!data)

                return AVERROR(ENOMEM);

            memcpy(data + 6, st->codec->extradata, extradd);

            memcpy(data + 6 + extradd, pkt->data, pkt->size);

            AV_WB32(data, 0x00000001);

            data[4] = 0x09;

            data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit

            buf     = data;

            size    = pkt->size + 6 + extradd;

        }

    } else if (st->codec->codec_id == AV_CODEC_ID_AAC) {

        if (pkt->size < 2) {

            av_log(s, AV_LOG_ERROR, "AAC packet too short\n");

            return AVERROR_INVALIDDATA;

        }

        if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {

            int ret;

            AVPacket pkt2;

            if (!ts_st->amux) {

                av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "

                                        "and extradata missing\n");

                return AVERROR_INVALIDDATA;

            }

            av_init_packet(&pkt2);

            pkt2.data = pkt->data;

            pkt2.size = pkt->size;

            av_assert0(pkt->dts != AV_NOPTS_VALUE);

            pkt2.dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);

            ret = avio_open_dyn_buf(&ts_st->amux->pb);

            if (ret < 0)

                return AVERROR(ENOMEM);

            ret = av_write_frame(ts_st->amux, &pkt2);

            if (ret < 0) {

                ffio_free_dyn_buf(&ts_st->amux->pb);

                return ret;

            }

            size            = avio_close_dyn_buf(ts_st->amux->pb, &data);

            ts_st->amux->pb = NULL;

            buf             = data;

        }

    } else if (st->codec->codec_id == AV_CODEC_ID_HEVC) {

        int ret = check_hevc_startcode(s, st, pkt);

        if (ret < 0)

            return ret;

    }

    if (pkt->dts != AV_NOPTS_VALUE) {

        int i;

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

            AVStream *st2 = s->streams[i];

            MpegTSWriteStream *ts_st2 = st2->priv_data;

            if (   ts_st2->payload_size

               && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {

                mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,

                                ts_st2->payload_pts, ts_st2->payload_dts,

                                ts_st2->payload_flags & AV_PKT_FLAG_KEY);

                ts_st2->payload_size = 0;

            }

        }

    }

    if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||

        (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&

         av_compare_ts(dts - ts_st->payload_dts, st->time_base,

                       s->max_delay, AV_TIME_BASE_Q) >= 0))) {

        mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,

                         ts_st->payload_pts, ts_st->payload_dts,

                         ts_st->payload_flags & AV_PKT_FLAG_KEY);

        ts_st->payload_size = 0;

    }

    if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {

        av_assert0(!ts_st->payload_size);

        // for video and subtitle, write a single pes packet

        mpegts_write_pes(s, st, buf, size, pts, dts,

                         pkt->flags & AV_PKT_FLAG_KEY);

        av_free(data);

        return 0;

    }

    if (!ts_st->payload_size) {

        ts_st->payload_pts   = pts;

        ts_st->payload_dts   = dts;

        ts_st->payload_flags = pkt->flags;

    }

    memcpy(ts_st->payload + ts_st->payload_size, buf, size);

    ts_st->payload_size += size;

    av_free(data);

    return 0;

}


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

{

    MP3Context *mp3 = s->priv_data;

    if (pkt->stream_index == mp3->audio_stream_idx) {

        if (mp3->pics_to_write) {

            /* buffer audio packets until we get all the pictures */

            AVPacketList *pktl = av_mallocz(sizeof(*pktl));

            if (!pktl)

                return AVERROR(ENOMEM);

            pktl->pkt     = *pkt;

            pktl->pkt.buf = av_buffer_ref(pkt->buf);

            if (!pktl->pkt.buf) {

                av_freep(&pktl);

                return AVERROR(ENOMEM);

            }

            if (mp3->queue_end)

                mp3->queue_end->next = pktl;

            else

                mp3->queue = pktl;

            mp3->queue_end = pktl;

        } else

            return mp3_write_audio_packet(s, pkt);

    } else {

        int ret;

        /* warn only once for each stream */

        if (s->streams[pkt->stream_index]->nb_frames == 1) {

            av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"

                   " ignoring.\n", pkt->stream_index);

        }

        if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)

            return 0;

        if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)

            return ret;

        mp3->pics_to_write--;

        /* flush the buffered audio packets */

        if (!mp3->pics_to_write &&

            (ret = mp3_queue_flush(s)) < 0)

            return ret;

    }

    return 0;

}


static void ioport_write(void *opaque, target_phys_addr_t addr,

                         uint64_t val, unsigned size)

{

    PCIQXLDevice *d = opaque;

    uint32_t io_port = addr;

    qxl_async_io async = QXL_SYNC;

    uint32_t orig_io_port = io_port;

    if (d->guest_bug && !io_port == QXL_IO_RESET) {

        return;

    if (d->revision <= QXL_REVISION_STABLE_V10 &&

        io_port >= QXL_IO_FLUSH_SURFACES_ASYNC) {

        qxl_set_guest_bug(d, "unsupported io %d for revision %d\n",

            io_port, d->revision);

        return;

    switch (io_port) {

    case QXL_IO_RESET:

    case QXL_IO_SET_MODE:

    case QXL_IO_MEMSLOT_ADD:

    case QXL_IO_MEMSLOT_DEL:

    case QXL_IO_CREATE_PRIMARY:

    case QXL_IO_UPDATE_IRQ:

    case QXL_IO_LOG:

    case QXL_IO_MEMSLOT_ADD_ASYNC:

    case QXL_IO_CREATE_PRIMARY_ASYNC:

    default:

        if (d->mode != QXL_MODE_VGA) {

        trace_qxl_io_unexpected_vga_mode(d->id,

            io_port, io_port_to_string(io_port));

        /* be nice to buggy guest drivers */

        if (io_port >= QXL_IO_UPDATE_AREA_ASYNC &&

            io_port < QXL_IO_RANGE_SIZE) {

            qxl_send_events(d, QXL_INTERRUPT_IO_CMD);

        return;

    /* we change the io_port to avoid ifdeffery in the main switch */

    orig_io_port = io_port;

    switch (io_port) {

    case QXL_IO_UPDATE_AREA_ASYNC:

        io_port = QXL_IO_UPDATE_AREA;

        goto async_common;

    case QXL_IO_MEMSLOT_ADD_ASYNC:

        io_port = QXL_IO_MEMSLOT_ADD;

        goto async_common;

    case QXL_IO_CREATE_PRIMARY_ASYNC:

        io_port = QXL_IO_CREATE_PRIMARY;

        goto async_common;

    case QXL_IO_DESTROY_PRIMARY_ASYNC:

        io_port = QXL_IO_DESTROY_PRIMARY;

        goto async_common;

    case QXL_IO_DESTROY_SURFACE_ASYNC:

        io_port = QXL_IO_DESTROY_SURFACE_WAIT;

        goto async_common;

    case QXL_IO_DESTROY_ALL_SURFACES_ASYNC:

        io_port = QXL_IO_DESTROY_ALL_SURFACES;

        goto async_common;

    case QXL_IO_FLUSH_SURFACES_ASYNC:

    case QXL_IO_MONITORS_CONFIG_ASYNC:

async_common:

        async = QXL_ASYNC;

        qemu_mutex_lock(&d->async_lock);

        if (d->current_async != QXL_UNDEFINED_IO) {

            qxl_set_guest_bug(d, "%d async started before last (%d) complete",

                io_port, d->current_async);

            qemu_mutex_unlock(&d->async_lock);

            return;

        d->current_async = orig_io_port;

        qemu_mutex_unlock(&d->async_lock);

    default:

    trace_qxl_io_write(d->id, qxl_mode_to_string(d->mode), addr, val, size,

                       async);

    switch (io_port) {

    case QXL_IO_UPDATE_AREA:

    {

        QXLCookie *cookie = NULL;

        QXLRect update = d->ram->update_area;

        if (d->ram->update_surface > d->ssd.num_surfaces) {

            qxl_set_guest_bug(d, "QXL_IO_UPDATE_AREA: invalid surface id %d\n",

                              d->ram->update_surface);

            return;

        if (update.left >= update.right || update.top >= update.bottom) {

            qxl_set_guest_bug(d,

                    "QXL_IO_UPDATE_AREA: invalid area (%ux%u)x(%ux%u)\n",

                    update.left, update.top, update.right, update.bottom);

            return;

        if (async == QXL_ASYNC) {

            cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,

                                    QXL_IO_UPDATE_AREA_ASYNC);

            cookie->u.area = update;

        qxl_spice_update_area(d, d->ram->update_surface,

                              cookie ? &cookie->u.area : &update,

                              NULL, 0, 0, async, cookie);

    case QXL_IO_NOTIFY_CMD:

        qemu_spice_wakeup(&d->ssd);

    case QXL_IO_NOTIFY_CURSOR:

        qemu_spice_wakeup(&d->ssd);

    case QXL_IO_UPDATE_IRQ:

        qxl_update_irq(d);

    case QXL_IO_NOTIFY_OOM:

        if (!SPICE_RING_IS_EMPTY(&d->ram->release_ring)) {

        d->oom_running = 1;

        qxl_spice_oom(d);

        d->oom_running = 0;

    case QXL_IO_SET_MODE:

        qxl_set_mode(d, val, 0);

    case QXL_IO_LOG:

        if (d->guestdebug) {

            fprintf(stderr, "qxl/guest-%d: %" PRId64 ": %s", d->id,

                    qemu_get_clock_ns(vm_clock), d->ram->log_buf);

    case QXL_IO_RESET:

        qxl_hard_reset(d, 0);

    case QXL_IO_MEMSLOT_ADD:

        if (val >= NUM_MEMSLOTS) {

            qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_ADD: val out of range");

        if (d->guest_slots[val].active) {

            qxl_set_guest_bug(d,

                        "QXL_IO_MEMSLOT_ADD: memory slot already active");

        d->guest_slots[val].slot = d->ram->mem_slot;

        qxl_add_memslot(d, val, 0, async);

    case QXL_IO_MEMSLOT_DEL:

        if (val >= NUM_MEMSLOTS) {

            qxl_set_guest_bug(d, "QXL_IO_MEMSLOT_DEL: val out of range");

        qxl_del_memslot(d, val);

    case QXL_IO_CREATE_PRIMARY:

        if (val != 0) {

            qxl_set_guest_bug(d, "QXL_IO_CREATE_PRIMARY (async=%d): val != 0",

                          async);

            goto cancel_async;

        d->guest_primary.surface = d->ram->create_surface;

        qxl_create_guest_primary(d, 0, async);

    case QXL_IO_DESTROY_PRIMARY:

        if (val != 0) {

            qxl_set_guest_bug(d, "QXL_IO_DESTROY_PRIMARY (async=%d): val != 0",

                          async);

            goto cancel_async;

        if (!qxl_destroy_primary(d, async)) {

            trace_qxl_io_destroy_primary_ignored(d->id,

                                                 qxl_mode_to_string(d->mode));

            goto cancel_async;

    case QXL_IO_DESTROY_SURFACE_WAIT:

        if (val >= d->ssd.num_surfaces) {

            qxl_set_guest_bug(d, "QXL_IO_DESTROY_SURFACE (async=%d):"

                             "%" PRIu64 " >= NUM_SURFACES", async, val);

            goto cancel_async;

        qxl_spice_destroy_surface_wait(d, val, async);

    case QXL_IO_FLUSH_RELEASE: {

        QXLReleaseRing *ring = &d->ram->release_ring;

        if (ring->prod - ring->cons + 1 == ring->num_items) {

            fprintf(stderr,

                "ERROR: no flush, full release ring [p%d,%dc]\n",

                ring->prod, ring->cons);

        qxl_push_free_res(d, 1 /* flush */);

    case QXL_IO_FLUSH_SURFACES_ASYNC:

        qxl_spice_flush_surfaces_async(d);

    case QXL_IO_DESTROY_ALL_SURFACES:

        d->mode = QXL_MODE_UNDEFINED;

        qxl_spice_destroy_surfaces(d, async);

    case QXL_IO_MONITORS_CONFIG_ASYNC:

        qxl_spice_monitors_config_async(d, 0);

    default:

        qxl_set_guest_bug(d, "%s: unexpected ioport=0x%x\n", __func__, io_port);

    return;

cancel_async:

    if (async) {

        qxl_send_events(d, QXL_INTERRUPT_IO_CMD);

        qemu_mutex_lock(&d->async_lock);

        d->current_async = QXL_UNDEFINED_IO;

        qemu_mutex_unlock(&d->async_lock);


static int decode_frame_byterun1(AVCodecContext *avctx,

                            void *data, int *data_size,

                            AVPacket *avpkt)

{

    IffContext *s = avctx->priv_data;

    const uint8_t *buf = avpkt->data;

    unsigned buf_size = avpkt->size;

    const uint8_t *buf_end = buf+buf_size;

    unsigned y, plane, x;

    if (avctx->reget_buffer(avctx, &s->frame) < 0){

        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");

        return -1;

    }

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

        uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];

        if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved

            memset(row, 0, avctx->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4));

            for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {

                for(x = 0; x < s->planesize && buf < buf_end; ) {

                    int8_t value = *buf++;

                    unsigned length;

                    if (value >= 0) {

                        length = value + 1;

                        memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));

                        buf += length;

                    } else if (value > -128) {

                        length = -value + 1;

                        memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));

                    } else { //noop

                        continue;

                    }

                    x += length;

                }

                if (avctx->pix_fmt == PIX_FMT_PAL8) {

                    decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);

                } else { //PIX_FMT_BGR32

                    decodeplane32((uint32_t *) row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);

                }

            }

        } else {

            for(x = 0; x < avctx->width && buf < buf_end; ) {

                int8_t value = *buf++;

                unsigned length;

                if (value >= 0) {

                    length = value + 1;

                    memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));

                    buf += length;

                } else if (value > -128) {

                    length = -value + 1;

                    memset(row + x, *buf++, FFMIN(length, avctx->width - x));

                } else { //noop

                    continue;

                }

                x += length;

            }

        }

    }

    *data_size = sizeof(AVFrame);

    *(AVFrame*)data = s->frame;

    return buf_size;

}


static void avc_loopfilter_luma_inter_edge_ver_msa(uint8_t *data,

                                                   uint8_t bs0, uint8_t bs1,

                                                   uint8_t bs2, uint8_t bs3,

                                                   uint8_t tc0, uint8_t tc1,

                                                   uint8_t tc2, uint8_t tc3,

                                                   uint8_t alpha_in,

                                                   uint8_t beta_in,

                                                   uint32_t img_width)

{

    uint8_t *src;

    v16u8 beta, tmp_vec, bs = { 0 };

    v16u8 tc = { 0 };

    v16u8 is_less_than, is_less_than_beta;

    v16u8 p1, p0, q0, q1;

    v8i16 p0_r, q0_r, p1_r = { 0 };

    v8i16 q1_r = { 0 };

    v8i16 p0_l, q0_l, p1_l = { 0 };

    v8i16 q1_l = { 0 };

    v16u8 p3_org, p2_org, p1_org, p0_org, q0_org, q1_org, q2_org, q3_org;

    v8i16 p2_org_r, p1_org_r, p0_org_r, q0_org_r, q1_org_r, q2_org_r;

    v8i16 p2_org_l, p1_org_l, p0_org_l, q0_org_l, q1_org_l, q2_org_l;

    v8i16 tc_r, tc_l;

    v16i8 zero = { 0 };

    v16u8 is_bs_greater_than0;

    tmp_vec = (v16u8) __msa_fill_b(bs0);

    bs = (v16u8) __msa_insve_w((v4i32) bs, 0, (v4i32) tmp_vec);

    tmp_vec = (v16u8) __msa_fill_b(bs1);

    bs = (v16u8) __msa_insve_w((v4i32) bs, 1, (v4i32) tmp_vec);

    tmp_vec = (v16u8) __msa_fill_b(bs2);

    bs = (v16u8) __msa_insve_w((v4i32) bs, 2, (v4i32) tmp_vec);

    tmp_vec = (v16u8) __msa_fill_b(bs3);

    bs = (v16u8) __msa_insve_w((v4i32) bs, 3, (v4i32) tmp_vec);

    if (!__msa_test_bz_v(bs)) {

        tmp_vec = (v16u8) __msa_fill_b(tc0);

        tc = (v16u8) __msa_insve_w((v4i32) tc, 0, (v4i32) tmp_vec);

        tmp_vec = (v16u8) __msa_fill_b(tc1);

        tc = (v16u8) __msa_insve_w((v4i32) tc, 1, (v4i32) tmp_vec);

        tmp_vec = (v16u8) __msa_fill_b(tc2);

        tc = (v16u8) __msa_insve_w((v4i32) tc, 2, (v4i32) tmp_vec);

        tmp_vec = (v16u8) __msa_fill_b(tc3);

        tc = (v16u8) __msa_insve_w((v4i32) tc, 3, (v4i32) tmp_vec);

        is_bs_greater_than0 = (zero < bs);

        {

            v16u8 row0, row1, row2, row3, row4, row5, row6, row7;

            v16u8 row8, row9, row10, row11, row12, row13, row14, row15;

            src = data;

            src -= 4;

            LOAD_8VECS_UB(src, img_width,

                          row0, row1, row2, row3, row4, row5, row6, row7);

            src += (8 * img_width);

            LOAD_8VECS_UB(src, img_width,

                          row8, row9, row10, row11, row12, row13, row14, row15);

            TRANSPOSE16x8_B_UB(row0, row1, row2, row3, row4, row5, row6, row7,

                               row8, row9, row10, row11,

                               row12, row13, row14, row15,

                               p3_org, p2_org, p1_org, p0_org,

                               q0_org, q1_org, q2_org, q3_org);

        }

        {

            v16u8 p0_asub_q0, p1_asub_p0, q1_asub_q0, alpha;

            v16u8 is_less_than_alpha;

            p0_asub_q0 = __msa_asub_u_b(p0_org, q0_org);

            p1_asub_p0 = __msa_asub_u_b(p1_org, p0_org);

            q1_asub_q0 = __msa_asub_u_b(q1_org, q0_org);

            alpha = (v16u8) __msa_fill_b(alpha_in);

            beta = (v16u8) __msa_fill_b(beta_in);

            is_less_than_alpha = (p0_asub_q0 < alpha);

            is_less_than_beta = (p1_asub_p0 < beta);

            is_less_than = is_less_than_beta & is_less_than_alpha;

            is_less_than_beta = (q1_asub_q0 < beta);

            is_less_than = is_less_than_beta & is_less_than;

            is_less_than = is_less_than & is_bs_greater_than0;

        }

        if (!__msa_test_bz_v(is_less_than)) {

            v16i8 negate_tc, sign_negate_tc;

            v8i16 negate_tc_r, i16_negatetc_l;

            negate_tc = zero - (v16i8) tc;

            sign_negate_tc = __msa_clti_s_b(negate_tc, 0);

            negate_tc_r = (v8i16) __msa_ilvr_b(sign_negate_tc, negate_tc);

            i16_negatetc_l = (v8i16) __msa_ilvl_b(sign_negate_tc, negate_tc);

            tc_r = (v8i16) __msa_ilvr_b(zero, (v16i8) tc);

            tc_l = (v8i16) __msa_ilvl_b(zero, (v16i8) tc);

            p1_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) p1_org);

            p0_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) p0_org);

            q0_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) q0_org);

            p1_org_l = (v8i16) __msa_ilvl_b(zero, (v16i8) p1_org);

            p0_org_l = (v8i16) __msa_ilvl_b(zero, (v16i8) p0_org);

            q0_org_l = (v8i16) __msa_ilvl_b(zero, (v16i8) q0_org);

            {

                v16u8 p2_asub_p0;

                v16u8 is_less_than_beta_r, is_less_than_beta_l;

                p2_asub_p0 = __msa_asub_u_b(p2_org, p0_org);

                is_less_than_beta = (p2_asub_p0 < beta);

                is_less_than_beta = is_less_than_beta & is_less_than;

                is_less_than_beta_r =

                    (v16u8) __msa_sldi_b((v16i8) is_less_than_beta, zero, 8);

                if (!__msa_test_bz_v(is_less_than_beta_r)) {

                    p2_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) p2_org);

                    AVC_LOOP_FILTER_P1_OR_Q1(p0_org_r, q0_org_r,

                                             p1_org_r, p2_org_r,

                                             negate_tc_r, tc_r, p1_r);

                }

                is_less_than_beta_l =

                    (v16u8) __msa_sldi_b(zero, (v16i8) is_less_than_beta, 8);

                if (!__msa_test_bz_v(is_less_than_beta_l)) {

                    p2_org_l = (v8i16) __msa_ilvl_b(zero, (v16i8) p2_org);

                    AVC_LOOP_FILTER_P1_OR_Q1(p0_org_l, q0_org_l,

                                             p1_org_l, p2_org_l,

                                             i16_negatetc_l, tc_l, p1_l);

                }

            }

            if (!__msa_test_bz_v(is_less_than_beta)) {

                p1 = (v16u8) __msa_pckev_b((v16i8) p1_l, (v16i8) p1_r);

                p1_org = __msa_bmnz_v(p1_org, p1, is_less_than_beta);

                is_less_than_beta = __msa_andi_b(is_less_than_beta, 1);

                tc = tc + is_less_than_beta;

            }

            {

                v16u8 u8_q2asub_q0;

                v16u8 is_less_than_beta_l, is_less_than_beta_r;

                u8_q2asub_q0 = __msa_asub_u_b(q2_org, q0_org);

                is_less_than_beta = (u8_q2asub_q0 < beta);

                is_less_than_beta = is_less_than_beta & is_less_than;

                q1_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) q1_org);

                is_less_than_beta_r =

                    (v16u8) __msa_sldi_b((v16i8) is_less_than_beta, zero, 8);

                if (!__msa_test_bz_v(is_less_than_beta_r)) {

                    q2_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) q2_org);

                    AVC_LOOP_FILTER_P1_OR_Q1(p0_org_r, q0_org_r,

                                             q1_org_r, q2_org_r,

                                             negate_tc_r, tc_r, q1_r);

                }

                q1_org_l = (v8i16) __msa_ilvl_b(zero, (v16i8) q1_org);

                is_less_than_beta_l =

                    (v16u8) __msa_sldi_b(zero, (v16i8) is_less_than_beta, 8);

                if (!__msa_test_bz_v(is_less_than_beta_l)) {

                    q2_org_l = (v8i16) __msa_ilvl_b(zero, (v16i8) q2_org);

                    AVC_LOOP_FILTER_P1_OR_Q1(p0_org_l, q0_org_l,

                                             q1_org_l, q2_org_l,

                                             i16_negatetc_l, tc_l, q1_l);

                }

            }

            if (!__msa_test_bz_v(is_less_than_beta)) {

                q1 = (v16u8) __msa_pckev_b((v16i8) q1_l, (v16i8) q1_r);

                q1_org = __msa_bmnz_v(q1_org, q1, is_less_than_beta);

                is_less_than_beta = __msa_andi_b(is_less_than_beta, 1);

                tc = tc + is_less_than_beta;

            }

            {

                v8i16 threshold_r, negate_thresh_r;

                v8i16 threshold_l, negate_thresh_l;

                v16i8 negate_thresh, sign_negate_thresh;

                negate_thresh = zero - (v16i8) tc;

                sign_negate_thresh = __msa_clti_s_b(negate_thresh, 0);

                threshold_r = (v8i16) __msa_ilvr_b(zero, (v16i8) tc);

                negate_thresh_r = (v8i16) __msa_ilvr_b(sign_negate_thresh,

                                                       negate_thresh);

                AVC_LOOP_FILTER_P0Q0(q0_org_r, p0_org_r, p1_org_r, q1_org_r,

                                     negate_thresh_r, threshold_r, p0_r, q0_r);

                threshold_l = (v8i16) __msa_ilvl_b(zero, (v16i8) tc);

                negate_thresh_l = (v8i16) __msa_ilvl_b(sign_negate_thresh,

                                                       negate_thresh);

                AVC_LOOP_FILTER_P0Q0(q0_org_l, p0_org_l, p1_org_l, q1_org_l,

                                     negate_thresh_l, threshold_l, p0_l, q0_l);

            }

            p0 = (v16u8) __msa_pckev_b((v16i8) p0_l, (v16i8) p0_r);

            q0 = (v16u8) __msa_pckev_b((v16i8) q0_l, (v16i8) q0_r);

            p0_org = __msa_bmnz_v(p0_org, p0, is_less_than);

            q0_org = __msa_bmnz_v(q0_org, q0, is_less_than);

        }

        {

            v16i8 tmp0, tmp1;

            v8i16 tmp2, tmp5;

            v4i32 tmp3, tmp4, tmp6, tmp7;

            uint32_t out0, out2;

            uint16_t out1, out3;

            src = data - 3;

            tmp0 = __msa_ilvr_b((v16i8) p1_org, (v16i8) p2_org);

            tmp1 = __msa_ilvr_b((v16i8) q0_org, (v16i8) p0_org);

            tmp2 = (v8i16) __msa_ilvr_b((v16i8) q2_org, (v16i8) q1_org);

            tmp3 = (v4i32) __msa_ilvr_h((v8i16) tmp1, (v8i16) tmp0);

            tmp4 = (v4i32) __msa_ilvl_h((v8i16) tmp1, (v8i16) tmp0);

            tmp0 = __msa_ilvl_b((v16i8) p1_org, (v16i8) p2_org);

            tmp1 = __msa_ilvl_b((v16i8) q0_org, (v16i8) p0_org);

            tmp5 = (v8i16) __msa_ilvl_b((v16i8) q2_org, (v16i8) q1_org);

            tmp6 = (v4i32) __msa_ilvr_h((v8i16) tmp1, (v8i16) tmp0);

            tmp7 = (v4i32) __msa_ilvl_h((v8i16) tmp1, (v8i16) tmp0);

            out0 = __msa_copy_u_w(tmp3, 0);

            out1 = __msa_copy_u_h(tmp2, 0);

            out2 = __msa_copy_u_w(tmp3, 1);

            out3 = __msa_copy_u_h(tmp2, 1);

            STORE_WORD(src, out0);

            STORE_HWORD((src + 4), out1);

            src += img_width;

            STORE_WORD(src, out2);

            STORE_HWORD((src + 4), out3);

            out0 = __msa_copy_u_w(tmp3, 2);

            out1 = __msa_copy_u_h(tmp2, 2);

            out2 = __msa_copy_u_w(tmp3, 3);

            out3 = __msa_copy_u_h(tmp2, 3);

            src += img_width;

            STORE_WORD(src, out0);

            STORE_HWORD((src + 4), out1);

            src += img_width;

            STORE_WORD(src, out2);

            STORE_HWORD((src + 4), out3);

            out0 = __msa_copy_u_w(tmp4, 0);

            out1 = __msa_copy_u_h(tmp2, 4);

            out2 = __msa_copy_u_w(tmp4, 1);

            out3 = __msa_copy_u_h(tmp2, 5);

            src += img_width;

            STORE_WORD(src, out0);

            STORE_HWORD((src + 4), out1);

            src += img_width;

            STORE_WORD(src, out2);

            STORE_HWORD((src + 4), out3);

            out0 = __msa_copy_u_w(tmp4, 2);

            out1 = __msa_copy_u_h(tmp2, 6);

            out2 = __msa_copy_u_w(tmp4, 3);

            out3 = __msa_copy_u_h(tmp2, 7);

            src += img_width;

            STORE_WORD(src, out0);

            STORE_HWORD((src + 4), out1);

            src += img_width;

            STORE_WORD(src, out2);

            STORE_HWORD((src + 4), out3);

            out0 = __msa_copy_u_w(tmp6, 0);

            out1 = __msa_copy_u_h(tmp5, 0);

            out2 = __msa_copy_u_w(tmp6, 1);

            out3 = __msa_copy_u_h(tmp5, 1);

            src += img_width;

            STORE_WORD(src, out0);

            STORE_HWORD((src + 4), out1);

            src += img_width;

            STORE_WORD(src, out2);

            STORE_HWORD((src + 4), out3);

            out0 = __msa_copy_u_w(tmp6, 2);

            out1 = __msa_copy_u_h(tmp5, 2);

            out2 = __msa_copy_u_w(tmp6, 3);

            out3 = __msa_copy_u_h(tmp5, 3);

            src += img_width;

            STORE_WORD(src, out0);

            STORE_HWORD((src + 4), out1);

            src += img_width;

            STORE_WORD(src, out2);

            STORE_HWORD((src + 4), out3);

            out0 = __msa_copy_u_w(tmp7, 0);

            out1 = __msa_copy_u_h(tmp5, 4);

            out2 = __msa_copy_u_w(tmp7, 1);

            out3 = __msa_copy_u_h(tmp5, 5);

            src += img_width;

            STORE_WORD(src, out0);

            STORE_HWORD((src + 4), out1);

            src += img_width;

            STORE_WORD(src, out2);

            STORE_HWORD((src + 4), out3);

            out0 = __msa_copy_u_w(tmp7, 2);

            out1 = __msa_copy_u_h(tmp5, 6);

            out2 = __msa_copy_u_w(tmp7, 3);

            out3 = __msa_copy_u_h(tmp5, 7);

            src += img_width;

            STORE_WORD(src, out0);

            STORE_HWORD((src + 4), out1);

            src += img_width;

            STORE_WORD(src, out2);

            STORE_HWORD((src + 4), out3);

        }

    }

}


static void dvbsub_parse_region_segment(AVCodecContext *avctx,

                                        const uint8_t *buf, int buf_size)

{

    DVBSubContext *ctx = avctx->priv_data;

    const uint8_t *buf_end = buf + buf_size;

    int region_id, object_id;

    DVBSubRegion *region;

    DVBSubObject *object;

    DVBSubObjectDisplay *display;

    int fill;

    if (buf_size < 10)

        return;

    region_id = *buf++;

    region = get_region(ctx, region_id);

    if (!region) {

        region = av_mallocz(sizeof(DVBSubRegion));

        region->id = region_id;

        region->next = ctx->region_list;

        ctx->region_list = region;

    }

    fill = ((*buf++) >> 3) & 1;

    region->width = AV_RB16(buf);

    buf += 2;

    region->height = AV_RB16(buf);

    buf += 2;

    if (region->width * region->height != region->buf_size) {

        av_free(region->pbuf);

        region->buf_size = region->width * region->height;

        region->pbuf = av_malloc(region->buf_size);

        fill = 1;

    }

    region->depth = 1 << (((*buf++) >> 2) & 7);

    if(region->depth<2 || region->depth>8){

        av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);

        region->depth= 4;

    }

    region->clut = *buf++;

    if (region->depth == 8)

        region->bgcolor = *buf++;

    else {

        buf += 1;

        if (region->depth == 4)

            region->bgcolor = (((*buf++) >> 4) & 15);

        else

            region->bgcolor = (((*buf++) >> 2) & 3);

    }

    av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);

    if (fill) {

        memset(region->pbuf, region->bgcolor, region->buf_size);

        av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);

    }

    delete_region_display_list(ctx, region);

    while (buf + 5 < buf_end) {

        object_id = AV_RB16(buf);

        buf += 2;

        object = get_object(ctx, object_id);

        if (!object) {

            object = av_mallocz(sizeof(DVBSubObject));

            object->id = object_id;

            object->next = ctx->object_list;

            ctx->object_list = object;

        }

        object->type = (*buf) >> 6;

        display = av_mallocz(sizeof(DVBSubObjectDisplay));

        display->object_id = object_id;

        display->region_id = region_id;

        display->x_pos = AV_RB16(buf) & 0xfff;

        buf += 2;

        display->y_pos = AV_RB16(buf) & 0xfff;

        buf += 2;

        if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {

            display->fgcolor = *buf++;

            display->bgcolor = *buf++;

        }

        display->region_list_next = region->display_list;

        region->display_list = display;

        display->object_list_next = object->display_list;

        object->display_list = display;

    }

}


static void sdhci_initfn(Object *obj)

{

    SDHCIState *s = SDHCI(obj);

    DriveInfo *di;

    di = drive_get_next(IF_SD);

    s->card = sd_init(di ? blk_bs(blk_by_legacy_dinfo(di)) : NULL, false);

    if (s->card == NULL) {

        exit(1);

    }

    s->eject_cb = qemu_allocate_irq(sdhci_insert_eject_cb, s, 0);

    s->ro_cb = qemu_allocate_irq(sdhci_card_readonly_cb, s, 0);

    sd_set_cb(s->card, s->ro_cb, s->eject_cb);

    s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s);

    s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_do_data_transfer, s);

}


static int print_ptr(DeviceState *dev, Property *prop, char *dest, size_t len)

{

    void **ptr = qdev_get_prop_ptr(dev, prop);

    return snprintf(dest, len, "<%p>", *ptr);

}


static ssize_t drop_sync(int fd, size_t size)

{

    ssize_t ret, dropped = size;

    uint8_t *buffer = g_malloc(MIN(65536, size));

    while (size > 0) {

        ret = read_sync(fd, buffer, MIN(65536, size));

        if (ret < 0) {

            g_free(buffer);

            return ret;

        }

        assert(ret <= size);

        size -= ret;

    }

    g_free(buffer);

    return dropped;

}


static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)

{

    IscsiLun *iscsilun = bs->opaque;

    struct IscsiTask iTask;

    if (bs->sg) {

        return 0;

    }

    if (!iscsilun->force_next_flush) {

        return 0;

    }

    iscsilun->force_next_flush = false;

    iscsi_co_init_iscsitask(iscsilun, &iTask);

retry:

    if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,

                                      0, iscsi_co_generic_cb, &iTask) == NULL) {

        return -ENOMEM;

    }

    while (!iTask.complete) {

        iscsi_set_events(iscsilun);

        qemu_coroutine_yield();

    }

    if (iTask.task != NULL) {

        scsi_free_scsi_task(iTask.task);

        iTask.task = NULL;

    }

    if (iTask.do_retry) {

        iTask.complete = 0;

        goto retry;

    }

    if (iTask.status != SCSI_STATUS_GOOD) {

        return -EIO;

    }

    return 0;

}


static void mmio_ide_write(void *opaque, target_phys_addr_t addr,

                           uint64_t val, unsigned size)

{

    MMIOState *s = opaque;

    addr >>= s->shift;

    if (addr & 7)

        ide_ioport_write(&s->bus, addr, val);

    else

        ide_data_writew(&s->bus, 0, val);

}


static int vmdaudio_decode_frame(AVCodecContext *avctx,

                                 void *data, int *data_size,

                                 AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    VmdAudioContext *s = avctx->priv_data;

    int block_type;

    unsigned char *output_samples = (unsigned char *)data;

    if (buf_size < 16) {

        av_log(avctx, AV_LOG_WARNING, "skipping small junk packet\n");

        *data_size = 0;

        return buf_size;

    }

    block_type = buf[6];

    if (block_type < BLOCK_TYPE_AUDIO || block_type > BLOCK_TYPE_SILENCE) {

        av_log(avctx, AV_LOG_ERROR, "unknown block type: %d\n", block_type);

        return AVERROR(EINVAL);

    }

    buf      += 16;

    buf_size -= 16;

    if (block_type == BLOCK_TYPE_AUDIO) {

        /* the chunk contains audio */

        *data_size = vmdaudio_loadsound(s, output_samples, buf, 0, buf_size);

    } else if (block_type == BLOCK_TYPE_INITIAL) {

        /* initial chunk, may contain audio and silence */

        uint32_t flags = AV_RB32(buf);

        int silent_chunks = av_popcount(flags);

        buf      += 4;

        buf_size -= 4;

        if(*data_size < (s->block_align*silent_chunks + buf_size) * 2)

            return -1;

        *data_size = vmdaudio_loadsound(s, output_samples, buf, silent_chunks, buf_size);

    } else if (block_type == BLOCK_TYPE_SILENCE) {

        /* silent chunk */

        *data_size = vmdaudio_loadsound(s, output_samples, buf, 1, 0);

    }

    return avpkt->size;

}


static int usb_parse(const char *cmdline)

{

    int r;

    r = usb_device_add(cmdline);

    if (r < 0) {

        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);

    }

    return r;

}


static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)

{

    int i;

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

        int type = qcow2_get_cluster_type(be64_to_cpu(l2_table[i]));

        if (type != QCOW2_CLUSTER_UNALLOCATED) {

            break;

        }

    }

    return i;

}


void bdrv_set_geometry_hint(BlockDriverState *bs,

                            int cyls, int heads, int secs)

{

    bs->cyls = cyls;

    bs->heads = heads;

    bs->secs = secs;

}


static BlockDriverState *bdrv_new_open(const char *filename,

                                       const char *fmt,

                                       int flags)

{

    BlockDriverState *bs;

    BlockDriver *drv;

    char password[256];

    bs = bdrv_new("");

    if (!bs) {

        error_report("Not enough memory");

        goto fail;

    }

    if (fmt) {

        drv = bdrv_find_format(fmt);

        if (!drv) {

            error_report("Unknown file format '%s'", fmt);

            goto fail;

        }

    } else {

        drv = NULL;

    }

    if (bdrv_open(bs, filename, flags, drv) < 0) {

        error_report("Could not open '%s'", filename);

        goto fail;

    }

    if (bdrv_is_encrypted(bs)) {

        printf("Disk image '%s' is encrypted.\n", filename);

        if (read_password(password, sizeof(password)) < 0) {

            error_report("No password given");

            goto fail;

        }

        if (bdrv_set_key(bs, password) < 0) {

            error_report("invalid password");

            goto fail;

        }

    }

    return bs;

fail:

    if (bs) {

        bdrv_delete(bs);

    }

    return NULL;

}


static uint32_t bitband_readl(void *opaque, target_phys_addr_t offset)

{

    uint32_t addr;

    uint32_t mask;

    uint32_t v;

    addr = bitband_addr(opaque, offset) & ~3;

    mask = (1 << ((offset >> 2) & 31));

    mask = tswap32(mask);

    cpu_physical_memory_read(addr, (uint8_t *)&v, 4);

    return (v & mask) != 0;

}


int64_t qemu_strtosz_MiB(const char *nptr, char **end)

{

    return do_strtosz(nptr, end, 'M', 1024);

}


static void to_json(const QObject *obj, QString *str, int pretty, int indent)

{

    switch (qobject_type(obj)) {

    case QTYPE_QINT: {

        QInt *val = qobject_to_qint(obj);

        char buffer[1024];

        snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val));

        qstring_append(str, buffer);

        break;

    }

    case QTYPE_QSTRING: {

        QString *val = qobject_to_qstring(obj);

        const char *ptr;

        ptr = qstring_get_str(val);

        qstring_append(str, "\"");

        while (*ptr) {

            if ((ptr[0] & 0xE0) == 0xE0 &&

                (ptr[1] & 0x80) && (ptr[2] & 0x80)) {

                uint16_t wchar;

                char escape[7];

                wchar  = (ptr[0] & 0x0F) << 12;

                wchar |= (ptr[1] & 0x3F) << 6;

                wchar |= (ptr[2] & 0x3F);

                ptr += 2;

                snprintf(escape, sizeof(escape), "\\u%04X", wchar);

                qstring_append(str, escape);

            } else if ((ptr[0] & 0xE0) == 0xC0 && (ptr[1] & 0x80)) {

                uint16_t wchar;

                char escape[7];

                wchar  = (ptr[0] & 0x1F) << 6;

                wchar |= (ptr[1] & 0x3F);

                ptr++;

                snprintf(escape, sizeof(escape), "\\u%04X", wchar);

                qstring_append(str, escape);

            } else switch (ptr[0]) {

                case '\"':

                    qstring_append(str, "\\\"");

                    break;

                case '\\':

                    qstring_append(str, "\\\\");

                    break;

                case '\b':

                    qstring_append(str, "\\b");

                    break;

                case '\f':

                    qstring_append(str, "\\f");

                    break;

                case '\n':

                    qstring_append(str, "\\n");

                    break;

                case '\r':

                    qstring_append(str, "\\r");

                    break;

                case '\t':

                    qstring_append(str, "\\t");

                    break;

                default: {

                    if (ptr[0] <= 0x1F) {

                        char escape[7];

                        snprintf(escape, sizeof(escape), "\\u%04X", ptr[0]);

                        qstring_append(str, escape);

                    } else {

                        char buf[2] = { ptr[0], 0 };

                        qstring_append(str, buf);

                    }

                    break;

                }

                }

            ptr++;

        }

        qstring_append(str, "\"");

        break;

    }

    case QTYPE_QDICT: {

        ToJsonIterState s;

        QDict *val = qobject_to_qdict(obj);

        s.count = 0;

        s.str = str;

        s.indent = indent + 1;

        s.pretty = pretty;

        qstring_append(str, "{");

        qdict_iter(val, to_json_dict_iter, &s);

        if (pretty) {

            int j;

            qstring_append(str, "\n");

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

                qstring_append(str, "    ");

        }

        qstring_append(str, "}");

        break;

    }

    case QTYPE_QLIST: {

        ToJsonIterState s;

        QList *val = qobject_to_qlist(obj);

        s.count = 0;

        s.str = str;

        s.indent = indent + 1;

        s.pretty = pretty;

        qstring_append(str, "[");

        qlist_iter(val, (void *)to_json_list_iter, &s);

        if (pretty) {

            int j;

            qstring_append(str, "\n");

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

                qstring_append(str, "    ");

        }

        qstring_append(str, "]");

        break;

    }

    case QTYPE_QFLOAT: {

        QFloat *val = qobject_to_qfloat(obj);

        char buffer[1024];

        int len;

        len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val));

        while (len > 0 && buffer[len - 1] == '0') {

            len--;

        }

        if (len && buffer[len - 1] == '.') {

            buffer[len - 1] = 0;

        } else {

            buffer[len] = 0;

        }

        qstring_append(str, buffer);

        break;

    }

    case QTYPE_QBOOL: {

        QBool *val = qobject_to_qbool(obj);

        if (qbool_get_int(val)) {

            qstring_append(str, "true");

        } else {

            qstring_append(str, "false");

        }

        break;

    }

    case QTYPE_QERROR:

        /* XXX: should QError be emitted? */

    case QTYPE_NONE:

        break;

    }

}


static int rm_assemble_video_frame(AVFormatContext *s, RMContext *rm, AVPacket *pkt, int len)

{

    ByteIOContext *pb = &s->pb;

    int hdr, seq, pic_num, len2, pos;

    int type;

    int ssize;

    hdr = get_byte(pb); len--;

    type = hdr >> 6;

    switch(type){

    case 0: // slice

    case 2: // last slice

        seq = get_byte(pb); len--;

        len2 = get_num(pb, &len);

        pos = get_num(pb, &len);

        pic_num = get_byte(pb); len--;

        rm->remaining_len = len;

        break;

    case 1: //whole frame

        seq = get_byte(pb); len--;

        if(av_new_packet(pkt, len + 9) < 0)

            return AVERROR(EIO);

        pkt->data[0] = 0;

        AV_WL32(pkt->data + 1, 1);

        AV_WL32(pkt->data + 5, 0);

        get_buffer(pb, pkt->data + 9, len);

        rm->remaining_len = 0;

        return 0;

    case 3: //frame as a part of packet

        len2 = get_num(pb, &len);

        pos = get_num(pb, &len);

        pic_num = get_byte(pb); len--;

        rm->remaining_len = len - len2;

        if(av_new_packet(pkt, len2 + 9) < 0)

            return AVERROR(EIO);

        pkt->data[0] = 0;

        AV_WL32(pkt->data + 1, 1);

        AV_WL32(pkt->data + 5, 0);

        get_buffer(pb, pkt->data + 9, len2);

        return 0;

    }

    //now we have to deal with single slice

    if((seq & 0x7F) == 1 || rm->curpic_num != pic_num){

        rm->slices = ((hdr & 0x3F) << 1) + 1;

        ssize = len2 + 8*rm->slices + 1;

        rm->videobuf = av_realloc(rm->videobuf, ssize);

        rm->videobufsize = ssize;

        rm->videobufpos = 8*rm->slices + 1;

        rm->cur_slice = 0;

        rm->curpic_num = pic_num;

        rm->pktpos = url_ftell(pb);

    }

    if(type == 2){

        len = FFMIN(len, pos);

        pos = len2 - pos;

    }

    if(++rm->cur_slice > rm->cur_slice)

        return 1;

    AV_WL32(rm->videobuf - 7 + 8*rm->cur_slice, 1);

    AV_WL32(rm->videobuf - 3 + 8*rm->cur_slice, rm->videobufpos - 8*rm->slices - 1);

    if(rm->videobufpos + len > rm->videobufsize)

        return 1;

    if (get_buffer(pb, rm->videobuf + rm->videobufpos, len) != len)

        return AVERROR(EIO);

    rm->videobufpos += len,

    rm->remaining_len-= len;

    if(type == 2 || (rm->videobufpos) == rm->videobufsize){

         //adjust slice headers

         memmove(rm->videobuf + 1 + 8*rm->cur_slice, rm->videobuf + 1 + 8*rm->slices, rm->videobufsize - 1 - 8*rm->slices);

         ssize = rm->videobufsize - 8*(rm->slices - rm->cur_slice);

         rm->videobuf[0] = rm->cur_slice-1;

         if(av_new_packet(pkt, ssize) < 0)

             return AVERROR(ENOMEM);

         memcpy(pkt->data, rm->videobuf, ssize);

         pkt->pts = AV_NOPTS_VALUE;

         pkt->pos = rm->pktpos;

         return 0;

    }

    return 1;

}


static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)

{

    if (d->term_got_escape) {

        d->term_got_escape = 0;

        if (ch == term_escape_char)

            goto send_char;

        switch(ch) {

        case '?':

        case 'h':

            mux_print_help(chr);

            break;

        case 'x':

            {

                 const char *term =  "QEMU: Terminated\n\r";

                 qemu_chr_fe_write(chr, (uint8_t *)term, strlen(term));

                 exit(0);

                 break;

            }

        case 's':

            blk_commit_all();

            break;

        case 'b':

            qemu_chr_be_event(chr, CHR_EVENT_BREAK);

            break;

        case 'c':

            /* Switch to the next registered device */

            mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);

            d->focus++;

            if (d->focus >= d->mux_cnt)

                d->focus = 0;

            mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);

            break;

        case 't':

            d->timestamps = !d->timestamps;

            d->timestamps_start = -1;

            d->linestart = 0;

            break;

        }

    } else if (ch == term_escape_char) {

        d->term_got_escape = 1;

    } else {

    send_char:

        return 1;

    }

    return 0;

}


static void win_chr_readfile(CharDriverState *chr)

{

    WinCharState *s = chr->opaque;

    int ret, err;

    uint8_t buf[1024];

    DWORD size;

    ZeroMemory(&s->orecv, sizeof(s->orecv));

    s->orecv.hEvent = s->hrecv;

    ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);

    if (!ret) {

        err = GetLastError();

        if (err == ERROR_IO_PENDING) {

            ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);

        }

    }

    if (size > 0) {

        qemu_chr_read(chr, buf, size);

    }

}


void mips_r4k_init(MachineState *machine)

{

    ram_addr_t ram_size = machine->ram_size;

    const char *cpu_model = machine->cpu_model;

    const char *kernel_filename = machine->kernel_filename;

    const char *kernel_cmdline = machine->kernel_cmdline;

    const char *initrd_filename = machine->initrd_filename;

    char *filename;

    MemoryRegion *address_space_mem = get_system_memory();

    MemoryRegion *ram = g_new(MemoryRegion, 1);

    MemoryRegion *bios;

    MemoryRegion *iomem = g_new(MemoryRegion, 1);

    MemoryRegion *isa_io = g_new(MemoryRegion, 1);

    MemoryRegion *isa_mem = g_new(MemoryRegion, 1);

    int bios_size;

    MIPSCPU *cpu;

    CPUMIPSState *env;

    ResetData *reset_info;

    int i;

    qemu_irq *i8259;

    ISABus *isa_bus;

    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];

    DriveInfo *dinfo;

    int be;

    /* init CPUs */

    if (cpu_model == NULL) {

#ifdef TARGET_MIPS64

        cpu_model = "R4000";

#else

        cpu_model = "24Kf";

#endif

    }

    cpu = cpu_mips_init(cpu_model);

    if (cpu == NULL) {

        fprintf(stderr, "Unable to find CPU definition\n");

        exit(1);

    }

    env = &cpu->env;

    reset_info = g_malloc0(sizeof(ResetData));

    reset_info->cpu = cpu;

    reset_info->vector = env->active_tc.PC;

    qemu_register_reset(main_cpu_reset, reset_info);

    /* allocate RAM */

    if (ram_size > (256 << 20)) {

        fprintf(stderr,

                "qemu: Too much memory for this machine: %d MB, maximum 256 MB\n",

                ((unsigned int)ram_size / (1 << 20)));

        exit(1);

    }

    memory_region_allocate_system_memory(ram, NULL, "mips_r4k.ram", ram_size);

    memory_region_add_subregion(address_space_mem, 0, ram);

    memory_region_init_io(iomem, NULL, &mips_qemu_ops, NULL, "mips-qemu", 0x10000);

    memory_region_add_subregion(address_space_mem, 0x1fbf0000, iomem);

    /* Try to load a BIOS image. If this fails, we continue regardless,

       but initialize the hardware ourselves. When a kernel gets

       preloaded we also initialize the hardware, since the BIOS wasn't

       run. */

    if (bios_name == NULL)

        bios_name = BIOS_FILENAME;

    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);

    if (filename) {

        bios_size = get_image_size(filename);

    } else {

        bios_size = -1;

    }

#ifdef TARGET_WORDS_BIGENDIAN

    be = 1;

#else

    be = 0;

#endif

    if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {

        bios = g_new(MemoryRegion, 1);

        memory_region_init_ram(bios, NULL, "mips_r4k.bios", BIOS_SIZE,

                               &error_fatal);

        memory_region_set_readonly(bios, true);

        memory_region_add_subregion(get_system_memory(), 0x1fc00000, bios);

        load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);

    } else if ((dinfo = drive_get(IF_PFLASH, 0, 0)) != NULL) {

        uint32_t mips_rom = 0x00400000;

        if (!pflash_cfi01_register(0x1fc00000, NULL, "mips_r4k.bios", mips_rom,

                                   blk_by_legacy_dinfo(dinfo),

                                   sector_len, mips_rom / sector_len,

                                   4, 0, 0, 0, 0, be)) {

            fprintf(stderr, "qemu: Error registering flash memory.\n");

	}

    } else if (!qtest_enabled()) {

	/* not fatal */

        fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",

		bios_name);

    }

    g_free(filename);

    if (kernel_filename) {

        loaderparams.ram_size = ram_size;

        loaderparams.kernel_filename = kernel_filename;

        loaderparams.kernel_cmdline = kernel_cmdline;

        loaderparams.initrd_filename = initrd_filename;

        reset_info->vector = load_kernel();

    }

    /* Init CPU internal devices */

    cpu_mips_irq_init_cpu(cpu);

    cpu_mips_clock_init(cpu);

    /* ISA bus: IO space at 0x14000000, mem space at 0x10000000 */

    memory_region_init_alias(isa_io, NULL, "isa-io",

                             get_system_io(), 0, 0x00010000);

    memory_region_init(isa_mem, NULL, "isa-mem", 0x01000000);

    memory_region_add_subregion(get_system_memory(), 0x14000000, isa_io);

    memory_region_add_subregion(get_system_memory(), 0x10000000, isa_mem);

    isa_bus = isa_bus_new(NULL, isa_mem, get_system_io(), &error_abort);

    /* The PIC is attached to the MIPS CPU INT0 pin */

    i8259 = i8259_init(isa_bus, env->irq[2]);

    isa_bus_irqs(isa_bus, i8259);

    rtc_init(isa_bus, 2000, NULL);

    pit = pit_init(isa_bus, 0x40, 0, NULL);

    serial_hds_isa_init(isa_bus, 0, MAX_SERIAL_PORTS);

    isa_vga_init(isa_bus);

    if (nd_table[0].used)

        isa_ne2000_init(isa_bus, 0x300, 9, &nd_table[0]);

    ide_drive_get(hd, ARRAY_SIZE(hd));

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

        isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],

                     hd[MAX_IDE_DEVS * i],

		     hd[MAX_IDE_DEVS * i + 1]);

    isa_create_simple(isa_bus, "i8042");

}


static int write_note(struct memelfnote *men, int fd)

{

    struct elf_note en;

    en.n_namesz = men->namesz;

    en.n_type = men->type;

    en.n_descsz = men->datasz;

    bswap_note(&en);

    if (dump_write(fd, &en, sizeof(en)) != 0)

        return (-1);

    if (dump_write(fd, men->name, men->namesz_rounded) != 0)

        return (-1);

    if (dump_write(fd, men->data, men->datasz) != 0)

        return (-1);

    return (0);

}


static int vhost_virtqueue_start(struct vhost_dev *dev,

                                struct VirtIODevice *vdev,

                                struct vhost_virtqueue *vq,

                                unsigned idx)

{

    hwaddr s, l, a;

    int r;

    int vhost_vq_index = idx - dev->vq_index;

    struct vhost_vring_file file = {

        .index = vhost_vq_index

    };

    struct vhost_vring_state state = {

        .index = vhost_vq_index

    };

    struct VirtQueue *vvq = virtio_get_queue(vdev, idx);

    assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);

    vq->num = state.num = virtio_queue_get_num(vdev, idx);

    r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_NUM, &state);

    if (r) {

        return -errno;

    }

    state.num = virtio_queue_get_last_avail_idx(vdev, idx);

    r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_BASE, &state);

    if (r) {

        return -errno;

    }

    if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1) &&

        virtio_legacy_is_cross_endian(vdev)) {

        r = vhost_virtqueue_set_vring_endian_legacy(dev,

                                                    virtio_is_big_endian(vdev),

                                                    vhost_vq_index);

        if (r) {

            return -errno;

        }

    }

    s = l = virtio_queue_get_desc_size(vdev, idx);

    a = virtio_queue_get_desc_addr(vdev, idx);

    vq->desc = cpu_physical_memory_map(a, &l, 0);

    if (!vq->desc || l != s) {

        r = -ENOMEM;

        goto fail_alloc_desc;

    }

    s = l = virtio_queue_get_avail_size(vdev, idx);

    a = virtio_queue_get_avail_addr(vdev, idx);

    vq->avail = cpu_physical_memory_map(a, &l, 0);

    if (!vq->avail || l != s) {

        r = -ENOMEM;

        goto fail_alloc_avail;

    }

    vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);

    vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);

    vq->used = cpu_physical_memory_map(a, &l, 1);

    if (!vq->used || l != s) {

        r = -ENOMEM;

        goto fail_alloc_used;

    }

    vq->ring_size = s = l = virtio_queue_get_ring_size(vdev, idx);

    vq->ring_phys = a = virtio_queue_get_ring_addr(vdev, idx);

    vq->ring = cpu_physical_memory_map(a, &l, 1);

    if (!vq->ring || l != s) {

        r = -ENOMEM;

        goto fail_alloc_ring;

    }

    r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);

    if (r < 0) {

        r = -errno;

        goto fail_alloc;

    }

    file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));

    r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_KICK, &file);

    if (r) {

        r = -errno;

        goto fail_kick;

    }

    /* Clear and discard previous events if any. */

    event_notifier_test_and_clear(&vq->masked_notifier);

    return 0;

fail_kick:

fail_alloc:

    cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),

                              0, 0);

fail_alloc_ring:

    cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),

                              0, 0);

fail_alloc_used:

    cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),

                              0, 0);

fail_alloc_avail:

    cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),

                              0, 0);

fail_alloc_desc:

    return r;

}


void vnc_client_read(void *opaque)

{

    VncState *vs = opaque;

    long ret;

#ifdef CONFIG_VNC_SASL

    if (vs->sasl.conn && vs->sasl.runSSF)

        ret = vnc_client_read_sasl(vs);

    else

#endif /* CONFIG_VNC_SASL */

#ifdef CONFIG_VNC_WS

        if (vs->encode_ws) {

            ret = vnc_client_read_ws(vs);

            if (ret == -1) {

                vnc_disconnect_start(vs);

                return;

            } else if (ret == -2) {

                vnc_client_error(vs);

                return;

            }

        } else

#endif /* CONFIG_VNC_WS */

        {

        ret = vnc_client_read_plain(vs);

        }

    if (!ret) {

        if (vs->csock == -1)

            vnc_disconnect_finish(vs);

        return;

    }

    while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {

        size_t len = vs->read_handler_expect;

        int ret;

        ret = vs->read_handler(vs, vs->input.buffer, len);

        if (vs->csock == -1) {

            vnc_disconnect_finish(vs);

            return;

        }

        if (!ret) {

            buffer_advance(&vs->input, len);

        } else {

            vs->read_handler_expect = ret;

        }

    }

}


static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)

{

    int x, fmt = sono->format, w = sono->width;

    uint8_t *lpy = sono->data[0] + idx * sono->linesize[0];

    uint8_t *lpu = sono->data[1] + idx * sono->linesize[1];

    uint8_t *lpv = sono->data[2] + idx * sono->linesize[2];

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

        *lpy++ = c[x].yuv.y + 0.5f;

        *lpu++ = c[x].yuv.u + 0.5f;

        *lpv++ = c[x].yuv.v + 0.5f;

        *lpy++ = c[x+1].yuv.y + 0.5f;

        if (fmt == AV_PIX_FMT_YUV444P) {

            *lpu++ = c[x+1].yuv.u + 0.5f;

            *lpv++ = c[x+1].yuv.v + 0.5f;

        }

    }

}


static int find_pte32(CPUPPCState *env, struct mmu_ctx_hash32 *ctx,

                      target_ulong sr, target_ulong eaddr, int rwx)

{

    hwaddr pteg_off, pte_offset;

    ppc_hash_pte32_t pte;

    hwaddr hash;

    uint32_t vsid, pgidx, ptem;

    int ret;

    ret = -1; /* No entry found */

    vsid = sr & SR32_VSID;

    ctx->key = (((sr & SR32_KP) && (msr_pr != 0)) ||

                ((sr & SR32_KS) && (msr_pr == 0))) ? 1 : 0;

    pgidx = (eaddr & ~SEGMENT_MASK_256M) >> TARGET_PAGE_BITS;

    hash = vsid ^ pgidx;

    ptem = (vsid << 7) | (pgidx >> 10);

    /* Page address translation */

    LOG_MMU("htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx

            " hash " TARGET_FMT_plx "\n",

            env->htab_base, env->htab_mask, hash);

    /* Primary PTEG lookup */

    LOG_MMU("0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx

            " vsid=%" PRIx32 " ptem=%" PRIx32

            " hash=" TARGET_FMT_plx "\n",

            env->htab_base, env->htab_mask, vsid, ptem, hash);

    pteg_off = get_pteg_offset32(env, hash);

    pte_offset = ppc_hash32_pteg_search(env, pteg_off, 0, ptem, &pte);

    if (pte_offset == -1) {

        /* Secondary PTEG lookup */

        LOG_MMU("1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx

                " vsid=%" PRIx32 " api=%" PRIx32

                " hash=" TARGET_FMT_plx "\n", env->htab_base,

                env->htab_mask, vsid, ptem, ~hash);

        pteg_off = get_pteg_offset32(env, ~hash);

        pte_offset = ppc_hash32_pteg_search(env, pteg_off, 1, ptem, &pte);

    }

    if (pte_offset != -1) {

        ret = pte_check_hash32(ctx, pte.pte0, pte.pte1, rwx);

        LOG_MMU("found PTE at addr %08" HWADDR_PRIx " prot=%01x ret=%d\n",

                ctx->raddr, ctx->prot, ret);

        /* Update page flags */

        if (ppc_hash32_pte_update_flags(ctx, &pte.pte1, ret, rwx) == 1) {

            ppc_hash32_store_hpte1(env, pte_offset, pte.pte1);

        }

    }

    return ret;

}


static void csrhci_in_packet_vendor(struct csrhci_s *s, int ocf,

                uint8_t *data, int len)

{

    int offset;

    uint8_t *rpkt;

    switch (ocf) {

    case OCF_CSR_SEND_FIRMWARE:

        /* Check if this is the bd_address packet */

        if (len >= 18 + 8 && data[12] == 0x01 && data[13] == 0x00) {

            offset = 18;

            s->bd_addr.b[0] = data[offset + 7];	/* Beyond cmd packet end(!?) */

            s->bd_addr.b[1] = data[offset + 6];

            s->bd_addr.b[2] = data[offset + 4];

            s->bd_addr.b[3] = data[offset + 0];

            s->bd_addr.b[4] = data[offset + 3];

            s->bd_addr.b[5] = data[offset + 2];

            s->hci->bdaddr_set(s->hci, s->bd_addr.b);

            fprintf(stderr, "%s: bd_address loaded from firmware: "

                            "%02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,

                            s->bd_addr.b[0], s->bd_addr.b[1], s->bd_addr.b[2],

                            s->bd_addr.b[3], s->bd_addr.b[4], s->bd_addr.b[5]);

        }

        rpkt = csrhci_out_packet_event(s, EVT_VENDOR, 11);

        /* Status bytes: no error */

        rpkt[9] = 0x00;

        rpkt[10] = 0x00;

        break;

    default:

        fprintf(stderr, "%s: got a bad CMD packet\n", __FUNCTION__);

        return;

    }

    csrhci_fifo_wake(s);

}


static void spapr_cpu_core_class_init(ObjectClass *oc, void *data)

{

    DeviceClass *dc = DEVICE_CLASS(oc);

    dc->realize = spapr_cpu_core_realize;

}


static int vnc_display_listen(VncDisplay *vd,

                              SocketAddressLegacy **saddr,

                              size_t nsaddr,

                              SocketAddressLegacy **wsaddr,

                              size_t nwsaddr,

                              Error **errp)

{

    size_t i;

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

        if (vnc_display_listen_addr(vd, saddr[i],

                                    "vnc-listen",

                                    &vd->lsock,

                                    &vd->lsock_tag,

                                    &vd->nlsock,

                                    errp) < 0) {

            return -1;

        }

    }

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

        if (vnc_display_listen_addr(vd, wsaddr[i],

                                    "vnc-ws-listen",

                                    &vd->lwebsock,

                                    &vd->lwebsock_tag,

                                    &vd->nlwebsock,

                                    errp) < 0) {

            return -1;

        }

    }

    return 0;

}


static int do_create_others(int type, struct iovec *iovec)

{

    dev_t rdev;

    int retval = 0;

    int offset = PROXY_HDR_SZ;

    V9fsString oldpath, path;

    int mode, uid, gid, cur_uid, cur_gid;

    v9fs_string_init(&path);

    v9fs_string_init(&oldpath);

    cur_uid = geteuid();

    cur_gid = getegid();

    retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);

    if (retval < 0) {

        return retval;

    }

    offset += retval;

    retval = setfsugid(uid, gid);

    if (retval < 0) {

        retval = -errno;

        goto err_out;

    }

    switch (type) {

    case T_MKNOD:

        retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);

        if (retval < 0) {

            goto err_out;

        }

        retval = mknod(path.data, mode, rdev);

        break;

    case T_MKDIR:

        retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);

        if (retval < 0) {

            goto err_out;

        }

        retval = mkdir(path.data, mode);

        break;

    case T_SYMLINK:

        retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);

        if (retval < 0) {

            goto err_out;

        }

        retval = symlink(oldpath.data, path.data);

        break;

    }

    if (retval < 0) {

        retval = -errno;

    }

err_out:

    v9fs_string_free(&path);

    v9fs_string_free(&oldpath);

    setfsugid(cur_uid, cur_gid);

    return retval;

}


void blockdev_auto_del(BlockDriverState *bs)

{

    DriveInfo *dinfo = drive_get_by_blockdev(bs);

    if (dinfo->auto_del) {

        drive_uninit(dinfo);

    }

}


void helper_lcall_protected_T0_T1(int shift, int next_eip)

{

    int new_cs, new_eip;

    uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;

    uint32_t ss, ss_e1, ss_e2, push_size, sp, type, ss_dpl;

    uint32_t old_ss, old_esp, val, i, limit;

    uint8_t *ssp, *old_ssp;

    new_cs = T0;

    new_eip = T1;

    if ((new_cs & 0xfffc) == 0)

        raise_exception_err(EXCP0D_GPF, 0);

    if (load_segment(&e1, &e2, new_cs) != 0)

        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);

    cpl = env->hflags & HF_CPL_MASK;

    if (e2 & DESC_S_MASK) {

        if (!(e2 & DESC_CS_MASK))

            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);

        dpl = (e2 >> DESC_DPL_SHIFT) & 3;

        if (e2 & DESC_CS_MASK) {

            /* conforming code segment */

            if (dpl > cpl)

                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);

        } else {

            /* non conforming code segment */

            rpl = new_cs & 3;

            if (rpl > cpl)

                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);

            if (dpl != cpl)

                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);

        }

        if (!(e2 & DESC_P_MASK))

            raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);

        sp = ESP;

        if (!(env->segs[R_SS].flags & DESC_B_MASK))

            sp &= 0xffff;

        ssp = env->segs[R_SS].base + sp;

        if (shift) {

            ssp -= 4;

            stl_kernel(ssp, env->segs[R_CS].selector);

            ssp -= 4;

            stl_kernel(ssp, next_eip);

        } else {

            ssp -= 2;

            stw_kernel(ssp, env->segs[R_CS].selector);

            ssp -= 2;

            stw_kernel(ssp, next_eip);

        }

        sp -= (4 << shift);

        limit = get_seg_limit(e1, e2);

        if (new_eip > limit)

            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);

        /* from this point, not restartable */

        if (!(env->segs[R_SS].flags & DESC_B_MASK))

            ESP = (ESP & 0xffff0000) | (sp & 0xffff);

        else

            ESP = sp;

        cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,

                       get_seg_base(e1, e2), limit, e2);

        EIP = new_eip;

    } else {

        /* check gate type */

        type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;

        switch(type) {

        case 1: /* available 286 TSS */

        case 9: /* available 386 TSS */

        case 5: /* task gate */

            cpu_abort(env, "task gate not supported");

            break;

        case 4: /* 286 call gate */

        case 12: /* 386 call gate */

            break;

        default:

            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);

            break;

        }

        shift = type >> 3;

        dpl = (e2 >> DESC_DPL_SHIFT) & 3;

        rpl = new_cs & 3;

        if (dpl < cpl || dpl < rpl)

            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);

        /* check valid bit */

        if (!(e2 & DESC_P_MASK))

            raise_exception_err(EXCP0B_NOSEG,  new_cs & 0xfffc);

        selector = e1 >> 16;

        offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);

        if ((selector & 0xfffc) == 0)

            raise_exception_err(EXCP0D_GPF, 0);

        if (load_segment(&e1, &e2, selector) != 0)

            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);

        if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))

            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);

        dpl = (e2 >> DESC_DPL_SHIFT) & 3;

        if (dpl > cpl)

            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);

        if (!(e2 & DESC_P_MASK))

            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);

        if (!(e2 & DESC_C_MASK) && dpl < cpl) {

            /* to inner priviledge */

            get_ss_esp_from_tss(&ss, &sp, dpl);

            if ((ss & 0xfffc) == 0)

                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);

            if ((ss & 3) != dpl)

                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);

            if (load_segment(&ss_e1, &ss_e2, ss) != 0)

                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);

            ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;

            if (ss_dpl != dpl)

                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);

            if (!(ss_e2 & DESC_S_MASK) ||

                (ss_e2 & DESC_CS_MASK) ||

                !(ss_e2 & DESC_W_MASK))

                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);

            if (!(ss_e2 & DESC_P_MASK))

                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);

            param_count = e2 & 0x1f;

            push_size = ((param_count * 2) + 8) << shift;

            old_esp = ESP;

            old_ss = env->segs[R_SS].selector;

            if (!(env->segs[R_SS].flags & DESC_B_MASK))

                old_esp &= 0xffff;

            old_ssp = env->segs[R_SS].base + old_esp;

            /* XXX: from this point not restartable */

            ss = (ss & ~3) | dpl;

            cpu_x86_load_seg_cache(env, R_SS, ss,

                           get_seg_base(ss_e1, ss_e2),

                           get_seg_limit(ss_e1, ss_e2),

                           ss_e2);

            if (!(env->segs[R_SS].flags & DESC_B_MASK))

                sp &= 0xffff;

            ssp = env->segs[R_SS].base + sp;

            if (shift) {

                ssp -= 4;

                stl_kernel(ssp, old_ss);

                ssp -= 4;

                stl_kernel(ssp, old_esp);

                ssp -= 4 * param_count;

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

                    val = ldl_kernel(old_ssp + i * 4);

                    stl_kernel(ssp + i * 4, val);

                }

            } else {

                ssp -= 2;

                stw_kernel(ssp, old_ss);

                ssp -= 2;

                stw_kernel(ssp, old_esp);

                ssp -= 2 * param_count;

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

                    val = lduw_kernel(old_ssp + i * 2);

                    stw_kernel(ssp + i * 2, val);

                }

            }

        } else {

            /* to same priviledge */

            if (!(env->segs[R_SS].flags & DESC_B_MASK))

                sp &= 0xffff;

            ssp = env->segs[R_SS].base + sp;

            push_size = (4 << shift);

        }

        if (shift) {

            ssp -= 4;

            stl_kernel(ssp, env->segs[R_CS].selector);

            ssp -= 4;

            stl_kernel(ssp, next_eip);

        } else {

            ssp -= 2;

            stw_kernel(ssp, env->segs[R_CS].selector);

            ssp -= 2;

            stw_kernel(ssp, next_eip);

        }

        sp -= push_size;

        selector = (selector & ~3) | dpl;

        cpu_x86_load_seg_cache(env, R_CS, selector,

                       get_seg_base(e1, e2),

                       get_seg_limit(e1, e2),

                       e2);

        cpu_x86_set_cpl(env, dpl);

        /* from this point, not restartable if same priviledge */

        if (!(env->segs[R_SS].flags & DESC_B_MASK))

            ESP = (ESP & 0xffff0000) | (sp & 0xffff);

        else

            ESP = sp;

        EIP = offset;

    }

}


static void armv7m_nvic_realize(DeviceState *dev, Error **errp)

{

    NVICState *s = NVIC(dev);

    SysBusDevice *systick_sbd;

    Error *err = NULL;

    s->cpu = ARM_CPU(qemu_get_cpu(0));

    assert(s->cpu);

    if (s->num_irq > NVIC_MAX_IRQ) {

        error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq);

        return;

    }

    qdev_init_gpio_in(dev, set_irq_level, s->num_irq);

    /* include space for internal exception vectors */

    s->num_irq += NVIC_FIRST_IRQ;

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

    if (err != NULL) {

        error_propagate(errp, err);

        return;

    }

    systick_sbd = SYS_BUS_DEVICE(&s->systick);

    sysbus_connect_irq(systick_sbd, 0,

                       qdev_get_gpio_in_named(dev, "systick-trigger", 0));

    /* The NVIC and System Control Space (SCS) starts at 0xe000e000

     * and looks like this:

     *  0x004 - ICTR

     *  0x010 - 0xff - systick

     *  0x100..0x7ec - NVIC

     *  0x7f0..0xcff - Reserved

     *  0xd00..0xd3c - SCS registers

     *  0xd40..0xeff - Reserved or Not implemented

     *  0xf00 - STIR

     */

    memory_region_init(&s->container, OBJECT(s), "nvic", 0x1000);

    /* The system register region goes at the bottom of the priority

     * stack as it covers the whole page.

     */

    memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,

                          "nvic_sysregs", 0x1000);

    memory_region_add_subregion(&s->container, 0, &s->sysregmem);

    memory_region_add_subregion_overlap(&s->container, 0x10,

                                        sysbus_mmio_get_region(systick_sbd, 0),

                                        1);

    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container);

}


static int nprobe(AVFormatContext *s, uint8_t *enc_header, const uint8_t *n_val)

{

    OMAContext *oc = s->priv_data;

    uint32_t pos, taglen, datalen;

    struct AVDES av_des;

    if (!enc_header || !n_val)

        return -1;

    pos = OMA_ENC_HEADER_SIZE + oc->k_size;

    if (!memcmp(&enc_header[pos], "EKB ", 4))

        pos += 32;

    if (AV_RB32(&enc_header[pos]) != oc->rid)

        av_log(s, AV_LOG_DEBUG, "Mismatching RID\n");

    taglen = AV_RB32(&enc_header[pos+32]);

    datalen = AV_RB32(&enc_header[pos+36]) >> 4;

    pos += 44 + taglen;

    av_des_init(&av_des, n_val, 192, 1);

    while (datalen-- > 0) {

        av_des_crypt(&av_des, oc->r_val, &enc_header[pos], 2, NULL, 1);

        kset(s, oc->r_val, NULL, 16);

        if (!rprobe(s, enc_header, oc->r_val))

            return 0;

        pos += 16;

    }

    return -1;

}


static gboolean nbd_negotiate_continue(QIOChannel *ioc,

                                       GIOCondition condition,

                                       void *opaque)

{

    qemu_coroutine_enter(opaque, NULL);

    return TRUE;

}


void object_add(const char *type, const char *id, const QDict *qdict,

                Visitor *v, Error **errp)

{

    Object *obj;

    const QDictEntry *e;

    Error *local_err = NULL;

    if (!object_class_by_name(type)) {

        error_setg(errp, "invalid class name");

        return;

    }

    obj = object_new(type);

    if (qdict) {

        for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {

            object_property_set(obj, v, e->key, &local_err);

            if (local_err) {

                goto out;

            }

        }

    }

    if (!object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {

        error_setg(&local_err, "object type '%s' isn't supported by object-add",

                   type);

        goto out;

    }

    user_creatable_complete(obj, &local_err);

    if (local_err) {

        goto out;

    }

    object_property_add_child(container_get(object_get_root(), "/objects"),

                              id, obj, &local_err);

out:

    if (local_err) {

        error_propagate(errp, local_err);

    }

    object_unref(obj);

}


static int create_filtergraph(AVFilterContext *ctx,

                              const AVFrame *in, const AVFrame *out)

{

    ColorSpaceContext *s = ctx->priv;

    const AVPixFmtDescriptor *in_desc  = av_pix_fmt_desc_get(in->format);

    const AVPixFmtDescriptor *out_desc = av_pix_fmt_desc_get(out->format);

    int emms = 0, m, n, o, res, fmt_identical, redo_yuv2rgb = 0, redo_rgb2yuv = 0;

#define supported_depth(d) ((d) == 8 || (d) == 10 || (d) == 12)

#define supported_subsampling(lcw, lch) \

    (((lcw) == 0 && (lch) == 0) || ((lcw) == 1 && (lch) == 0) || ((lcw) == 1 && (lch) == 1))

#define supported_format(d) \

    ((d) != NULL && (d)->nb_components == 3 && \

     !((d)->flags & AV_PIX_FMT_FLAG_RGB) && \

     supported_depth((d)->comp[0].depth) && \

     supported_subsampling((d)->log2_chroma_w, (d)->log2_chroma_h))

    if (!supported_format(in_desc)) {

        av_log(ctx, AV_LOG_ERROR,

               "Unsupported input format %d (%s) or bitdepth (%d)\n",

               in->format, av_get_pix_fmt_name(in->format),

               in_desc ? in_desc->comp[0].depth : -1);

        return AVERROR(EINVAL);

    }

    if (!supported_format(out_desc)) {

        av_log(ctx, AV_LOG_ERROR,

               "Unsupported output format %d (%s) or bitdepth (%d)\n",

               out->format, av_get_pix_fmt_name(out->format),

               out_desc ? out_desc->comp[0].depth : -1);

        return AVERROR(EINVAL);

    }

    if (in->color_primaries  != s->in_prm)  s->in_primaries  = NULL;

    if (out->color_primaries != s->out_prm) s->out_primaries = NULL;

    if (in->color_trc        != s->in_trc)  s->in_txchr      = NULL;

    if (out->color_trc       != s->out_trc) s->out_txchr     = NULL;

    if (in->colorspace       != s->in_csp ||

        in->color_range      != s->in_rng)  s->in_lumacoef   = NULL;

    if (out->colorspace      != s->out_csp ||

        out->color_range     != s->out_rng) s->out_lumacoef  = NULL;

    if (!s->out_primaries || !s->in_primaries) {

        s->in_prm = in->color_primaries;

        s->in_primaries = get_color_primaries(s->in_prm);

        if (!s->in_primaries) {

            av_log(ctx, AV_LOG_ERROR,

                   "Unsupported input primaries %d (%s)\n",

                   s->in_prm, av_color_primaries_name(s->in_prm));

            return AVERROR(EINVAL);

        }

        s->out_prm = out->color_primaries;

        s->out_primaries = get_color_primaries(s->out_prm);

        if (!s->out_primaries) {

            if (s->out_prm == AVCOL_PRI_UNSPECIFIED) {

                if (s->user_all == CS_UNSPECIFIED) {

                    av_log(ctx, AV_LOG_ERROR, "Please specify output primaries\n");

                } else {

                    av_log(ctx, AV_LOG_ERROR,

                           "Unsupported output color property %d\n", s->user_all);

                }

            } else {

                av_log(ctx, AV_LOG_ERROR,

                       "Unsupported output primaries %d (%s)\n",

                       s->out_prm, av_color_primaries_name(s->out_prm));

            }

            return AVERROR(EINVAL);

        }

        s->lrgb2lrgb_passthrough = !memcmp(s->in_primaries, s->out_primaries,

                                           sizeof(*s->in_primaries));

        if (!s->lrgb2lrgb_passthrough) {

            double rgb2xyz[3][3], xyz2rgb[3][3], rgb2rgb[3][3];

            fill_rgb2xyz_table(s->out_primaries, rgb2xyz);

            invert_matrix3x3(rgb2xyz, xyz2rgb);

            fill_rgb2xyz_table(s->in_primaries, rgb2xyz);

            if (s->out_primaries->wp != s->in_primaries->wp &&

                s->wp_adapt != WP_ADAPT_IDENTITY) {

                double wpconv[3][3], tmp[3][3];

                fill_whitepoint_conv_table(wpconv, s->wp_adapt, s->in_primaries->wp,

                                           s->out_primaries->wp);

                mul3x3(tmp, rgb2xyz, wpconv);

                mul3x3(rgb2rgb, tmp, xyz2rgb);

            } else {

                mul3x3(rgb2rgb, rgb2xyz, xyz2rgb);

            }

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

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

                    s->lrgb2lrgb_coeffs[m][n][0] = lrint(16384.0 * rgb2rgb[m][n]);

                    for (o = 1; o < 8; o++)

                        s->lrgb2lrgb_coeffs[m][n][o] = s->lrgb2lrgb_coeffs[m][n][0];

                }

            emms = 1;

        }

    }

    if (!s->in_txchr) {

        av_freep(&s->lin_lut);

        s->in_trc = in->color_trc;

        s->in_txchr = get_transfer_characteristics(s->in_trc);

        if (!s->in_txchr) {

            av_log(ctx, AV_LOG_ERROR,

                   "Unsupported input transfer characteristics %d (%s)\n",

                   s->in_trc, av_color_transfer_name(s->in_trc));

            return AVERROR(EINVAL);

        }

    }

    if (!s->out_txchr) {

        av_freep(&s->lin_lut);

        s->out_trc = out->color_trc;

        s->out_txchr = get_transfer_characteristics(s->out_trc);

        if (!s->out_txchr) {

            if (s->out_trc == AVCOL_TRC_UNSPECIFIED) {

                if (s->user_all == CS_UNSPECIFIED) {

                    av_log(ctx, AV_LOG_ERROR,

                           "Please specify output transfer characteristics\n");

                } else {

                    av_log(ctx, AV_LOG_ERROR,

                           "Unsupported output color property %d\n", s->user_all);

                }

            } else {

                av_log(ctx, AV_LOG_ERROR,

                       "Unsupported output transfer characteristics %d (%s)\n",

                       s->out_trc, av_color_transfer_name(s->out_trc));

            }

            return AVERROR(EINVAL);

        }

    }

    s->rgb2rgb_passthrough = s->fast_mode || (s->lrgb2lrgb_passthrough &&

                             !memcmp(s->in_txchr, s->out_txchr, sizeof(*s->in_txchr)));

    if (!s->rgb2rgb_passthrough && !s->lin_lut) {

        res = fill_gamma_table(s);

        if (res < 0)

            return res;

        emms = 1;

    }

    if (!s->in_lumacoef) {

        s->in_csp = in->colorspace;

        s->in_rng = in->color_range;

        s->in_lumacoef = get_luma_coefficients(s->in_csp);

        if (!s->in_lumacoef) {

            av_log(ctx, AV_LOG_ERROR,

                   "Unsupported input colorspace %d (%s)\n",

                   s->in_csp, av_color_space_name(s->in_csp));

            return AVERROR(EINVAL);

        }

        redo_yuv2rgb = 1;

    }

    if (!s->out_lumacoef) {

        s->out_csp = out->colorspace;

        s->out_rng = out->color_range;

        s->out_lumacoef = get_luma_coefficients(s->out_csp);

        if (!s->out_lumacoef) {

            if (s->out_csp == AVCOL_SPC_UNSPECIFIED) {

                if (s->user_all == CS_UNSPECIFIED) {

                    av_log(ctx, AV_LOG_ERROR,

                           "Please specify output transfer characteristics\n");

                } else {

                    av_log(ctx, AV_LOG_ERROR,

                           "Unsupported output color property %d\n", s->user_all);

                }

            } else {

                av_log(ctx, AV_LOG_ERROR,

                       "Unsupported output transfer characteristics %d (%s)\n",

                       s->out_csp, av_color_space_name(s->out_csp));

            }

            return AVERROR(EINVAL);

        }

        redo_rgb2yuv = 1;

    }

    fmt_identical = in_desc->log2_chroma_h == out_desc->log2_chroma_h &&

                    in_desc->log2_chroma_w == out_desc->log2_chroma_w;

    s->yuv2yuv_fastmode = s->rgb2rgb_passthrough && fmt_identical;

    s->yuv2yuv_passthrough = s->yuv2yuv_fastmode && s->in_rng == s->out_rng &&

                             !memcmp(s->in_lumacoef, s->out_lumacoef,

                                     sizeof(*s->in_lumacoef));

    if (!s->yuv2yuv_passthrough) {

        if (redo_yuv2rgb) {

            double rgb2yuv[3][3], (*yuv2rgb)[3] = s->yuv2rgb_dbl_coeffs;

            int off, bits, in_rng;

            res = get_range_off(&off, &s->in_y_rng, &s->in_uv_rng,

                                s->in_rng, in_desc->comp[0].depth);

            if (res < 0) {

                av_log(ctx, AV_LOG_ERROR,

                       "Unsupported input color range %d (%s)\n",

                       s->in_rng, av_color_range_name(s->in_rng));

                return res;

            }

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

                s->yuv_offset[0][n] = off;

            fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv);

            invert_matrix3x3(rgb2yuv, yuv2rgb);

            bits = 1 << (in_desc->comp[0].depth - 1);

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

                for (in_rng = s->in_y_rng, m = 0; m < 3; m++, in_rng = s->in_uv_rng) {

                    s->yuv2rgb_coeffs[n][m][0] = lrint(28672 * bits * yuv2rgb[n][m] / in_rng);

                    for (o = 1; o < 8; o++)

                        s->yuv2rgb_coeffs[n][m][o] = s->yuv2rgb_coeffs[n][m][0];

                }

            }

            av_assert2(s->yuv2rgb_coeffs[0][1][0] == 0);

            av_assert2(s->yuv2rgb_coeffs[2][2][0] == 0);

            av_assert2(s->yuv2rgb_coeffs[0][0][0] == s->yuv2rgb_coeffs[1][0][0]);

            av_assert2(s->yuv2rgb_coeffs[0][0][0] == s->yuv2rgb_coeffs[2][0][0]);

            s->yuv2rgb = s->dsp.yuv2rgb[(in_desc->comp[0].depth - 8) >> 1]

                                       [in_desc->log2_chroma_h + in_desc->log2_chroma_w];

            emms = 1;

        }

        if (redo_rgb2yuv) {

            double (*rgb2yuv)[3] = s->rgb2yuv_dbl_coeffs;

            int off, out_rng, bits;

            res = get_range_off(&off, &s->out_y_rng, &s->out_uv_rng,

                                s->out_rng, out_desc->comp[0].depth);

            if (res < 0) {

                av_log(ctx, AV_LOG_ERROR,

                       "Unsupported output color range %d (%s)\n",

                       s->out_rng, av_color_range_name(s->out_rng));

                return res;

            }

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

                s->yuv_offset[1][n] = off;

            fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv);

            bits = 1 << (29 - out_desc->comp[0].depth);

            for (out_rng = s->out_y_rng, n = 0; n < 3; n++, out_rng = s->out_uv_rng) {

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

                    s->rgb2yuv_coeffs[n][m][0] = lrint(bits * out_rng * rgb2yuv[n][m] / 28672);

                    for (o = 1; o < 8; o++)

                        s->rgb2yuv_coeffs[n][m][o] = s->rgb2yuv_coeffs[n][m][0];

                }

            }

            av_assert2(s->rgb2yuv_coeffs[1][2][0] == s->rgb2yuv_coeffs[2][0][0]);

            s->rgb2yuv = s->dsp.rgb2yuv[(out_desc->comp[0].depth - 8) >> 1]

                                       [out_desc->log2_chroma_h + out_desc->log2_chroma_w];

            s->rgb2yuv_fsb = s->dsp.rgb2yuv_fsb[(out_desc->comp[0].depth - 8) >> 1]

                                       [out_desc->log2_chroma_h + out_desc->log2_chroma_w];

            emms = 1;

        }

        if (s->yuv2yuv_fastmode && (redo_yuv2rgb || redo_rgb2yuv)) {

            int idepth = in_desc->comp[0].depth, odepth = out_desc->comp[0].depth;

            double (*rgb2yuv)[3] = s->rgb2yuv_dbl_coeffs;

            double (*yuv2rgb)[3] = s->yuv2rgb_dbl_coeffs;

            double yuv2yuv[3][3];

            int in_rng, out_rng;

            mul3x3(yuv2yuv, yuv2rgb, rgb2yuv);

            for (out_rng = s->out_y_rng, m = 0; m < 3; m++, out_rng = s->out_uv_rng) {

                for (in_rng = s->in_y_rng, n = 0; n < 3; n++, in_rng = s->in_uv_rng) {

                    s->yuv2yuv_coeffs[m][n][0] =

                        lrint(16384 * yuv2yuv[m][n] * out_rng * (1 << idepth) /

                              (in_rng * (1 << odepth)));

                    for (o = 1; o < 8; o++)

                        s->yuv2yuv_coeffs[m][n][o] = s->yuv2yuv_coeffs[m][n][0];

                }

            }

            av_assert2(s->yuv2yuv_coeffs[1][0][0] == 0);

            av_assert2(s->yuv2yuv_coeffs[2][0][0] == 0);

            s->yuv2yuv = s->dsp.yuv2yuv[(idepth - 8) >> 1][(odepth - 8) >> 1]

                                       [in_desc->log2_chroma_h + in_desc->log2_chroma_w];

        }

    }

    if (emms)

        emms_c();

    return 0;

}


AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){

    AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));

    double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);

    int phase_count= 1<<phase_shift;

    if (!c)

        return NULL;

    c->phase_shift= phase_shift;

    c->phase_mask= phase_count-1;

    c->linear= linear;

    c->filter_length= FFMAX((int)ceil(filter_size/factor), 1);

    c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM));

    if (!c->filter_bank)

        goto error;

    if (build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE))

        goto error;

    memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));

    c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];

    c->src_incr= out_rate;

    c->ideal_dst_incr= c->dst_incr= in_rate * phase_count;

    c->index= -phase_count*((c->filter_length-1)/2);

    return c;

error:

    av_free(c->filter_bank);

    av_free(c);

    return NULL;

}


static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,

    QDict *options, int flags, BlockDriver *drv, Error **errp)

{

    int ret, open_flags;

    const char *filename;

    Error *local_err = NULL;

    assert(drv != NULL);

    assert(bs->file == NULL);

    assert(options != NULL && bs->options != options);

    if (file != NULL) {

        filename = file->filename;

    } else {

        filename = qdict_get_try_str(options, "filename");

    }

    trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);

    /* bdrv_open() with directly using a protocol as drv. This layer is already

     * opened, so assign it to bs (while file becomes a closed BlockDriverState)

     * and return immediately. */

    if (file != NULL && drv->bdrv_file_open) {

        bdrv_swap(file, bs);

        return 0;

    }

    bs->open_flags = flags;

    bs->buffer_alignment = 512;

    bs->zero_beyond_eof = true;

    open_flags = bdrv_open_flags(bs, flags);

    bs->read_only = !(open_flags & BDRV_O_RDWR);

    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {

        error_setg(errp, "Driver '%s' is not whitelisted", drv->format_name);

        return -ENOTSUP;

    }

    assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */

    if (!bs->read_only && (flags & BDRV_O_COPY_ON_READ)) {

        bdrv_enable_copy_on_read(bs);

    }

    if (filename != NULL) {

        pstrcpy(bs->filename, sizeof(bs->filename), filename);

    } else {

        bs->filename[0] = '\0';

    }

    bs->drv = drv;

    bs->opaque = g_malloc0(drv->instance_size);

    bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);

    /* Open the image, either directly or using a protocol */

    if (drv->bdrv_file_open) {

        assert(file == NULL);

        assert(!drv->bdrv_needs_filename || filename != NULL);

        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);

    } else {

        if (file == NULL) {

            error_setg(errp, "Can't use '%s' as a block driver for the "

                       "protocol level", drv->format_name);

            ret = -EINVAL;

            goto free_and_fail;

        }

        bs->file = file;

        ret = drv->bdrv_open(bs, options, open_flags, &local_err);

    }

    if (ret < 0) {

        if (error_is_set(&local_err)) {

            error_propagate(errp, local_err);

        } else if (bs->filename[0]) {

            error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);

        } else {

            error_setg_errno(errp, -ret, "Could not open image");

        }

        goto free_and_fail;

    }

    ret = refresh_total_sectors(bs, bs->total_sectors);

    if (ret < 0) {

        error_setg_errno(errp, -ret, "Could not refresh total sector count");

        goto free_and_fail;

    }

#ifndef _WIN32

    if (bs->is_temporary) {

        assert(bs->filename[0] != '\0');

        unlink(bs->filename);

    }

#endif

    return 0;

free_and_fail:

    bs->file = NULL;

    g_free(bs->opaque);

    bs->opaque = NULL;

    bs->drv = NULL;

    return ret;

}


static void usage(void)

{

    printf("qemu-" TARGET_ARCH " version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"

           "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"

           "Linux CPU emulator (compiled for %s emulation)\n"

           "\n"

           "Standard options:\n"

           "-h                print this help\n"

           "-g port           wait gdb connection to port\n"

           "-L path           set the elf interpreter prefix (default=%s)\n"

           "-s size           set the stack size in bytes (default=%ld)\n"

           "-cpu model        select CPU (-cpu ? for list)\n"

           "-drop-ld-preload  drop LD_PRELOAD for target process\n"

           "-E var=value      sets/modifies targets environment variable(s)\n"

           "-U var            unsets targets environment variable(s)\n"

           "-0 argv0          forces target process argv[0] to be argv0\n"

#if defined(CONFIG_USE_GUEST_BASE)

           "-B address        set guest_base address to address\n"

#endif

           "\n"

           "Debug options:\n"

           "-d options   activate log (logfile=%s)\n"

           "-p pagesize  set the host page size to 'pagesize'\n"

           "-singlestep  always run in singlestep mode\n"

           "-strace      log system calls\n"

           "\n"

           "Environment variables:\n"

           "QEMU_STRACE       Print system calls and arguments similar to the\n"

           "                  'strace' program.  Enable by setting to any value.\n"

           "You can use -E and -U options to set/unset environment variables\n"

           "for target process.  It is possible to provide several variables\n"

           "by repeating the option.  For example:\n"

           "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"

           "Note that if you provide several changes to single variable\n"

           "last change will stay in effect.\n"

           ,

           TARGET_ARCH,

           interp_prefix,

           x86_stack_size,

           DEBUG_LOGFILE);

    exit(1);

}


void visit_end_struct(Visitor *v, Error **errp)

{

    assert(!error_is_set(errp));

    v->end_struct(v, errp);

}


static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,

                           uint8_t *buf, int buf_size)

{

    RTSPState *rt = s->priv_data;

    RTSPStream *rtsp_st;

    fd_set rfds;

    int fd, fd_max, n, i, ret, tcp_fd;

    struct timeval tv;

    for(;;) {

        if (url_interrupt_cb())

            return AVERROR(EINTR);

        FD_ZERO(&rfds);

        tcp_fd = fd_max = url_get_file_handle(rt->rtsp_hd);

        FD_SET(tcp_fd, &rfds);

        for(i = 0; i < rt->nb_rtsp_streams; i++) {

            rtsp_st = rt->rtsp_streams[i];

            if (rtsp_st->rtp_handle) {

                /* currently, we cannot probe RTCP handle because of

                 * blocking restrictions */

                fd = url_get_file_handle(rtsp_st->rtp_handle);

                if (fd > fd_max)

                    fd_max = fd;

                FD_SET(fd, &rfds);

            }

        }

        tv.tv_sec = 0;

        tv.tv_usec = 100 * 1000;

        n = select(fd_max + 1, &rfds, NULL, NULL, &tv);

        if (n > 0) {

            for(i = 0; i < rt->nb_rtsp_streams; i++) {

                rtsp_st = rt->rtsp_streams[i];

                if (rtsp_st->rtp_handle) {

                    fd = url_get_file_handle(rtsp_st->rtp_handle);

                    if (FD_ISSET(fd, &rfds)) {

                        ret = url_read(rtsp_st->rtp_handle, buf, buf_size);

                        if (ret > 0) {

                            *prtsp_st = rtsp_st;

                            return ret;

                        }

                    }

                }

            }

            if (FD_ISSET(tcp_fd, &rfds)) {

                RTSPMessageHeader reply;

                rtsp_read_reply(s, &reply, NULL, 0);

                /* XXX: parse message */

            }

        }

    }

}


static float **alloc_audio_arrays(int channels, int frame_size)

{

    float **audio = av_mallocz_array(channels, sizeof(float *));

    if (!audio)

        return NULL;

    for (int ch = 0; ch < channels; ch++) {

        audio[ch] = av_mallocz_array(frame_size, sizeof(float));

        if (!audio[ch]) {

            // alloc has failed, free everything allocated thus far

            for (ch--; ch >= 0; ch--)

                av_free(audio[ch]);

            av_free(audio);

            return NULL;

        }

    }

    return audio;

}


static void unterminated_sq_string(void)

{

    QObject *obj = qobject_from_json("'abc", NULL);

    g_assert(obj == NULL);

}


static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)

{

    /* No direct tb linking with singlestep (either QEMU's or the ARM

     * debug architecture kind) or deterministic io

     */

    if (s->base.singlestep_enabled || s->ss_active || (s->base.tb->cflags & CF_LAST_IO)) {

        return false;

    }

#ifndef CONFIG_USER_ONLY

    /* Only link tbs from inside the same guest page */

    if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {

        return false;

    }

#endif

    return true;

}


static int common_end(AVCodecContext *avctx){

    FFV1Context *s = avctx->priv_data;

    int i;

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

        PlaneContext *p= &s->plane[i];

        av_freep(&p->state);

    }

    return 0;

}