uint32_t v = 0;
static int nsv_read_chunk(AVFormatContext *s, int fill_header){    NSVContext *nsv = s->priv_data;    ByteIOContext *pb = &s->pb;    AVStream *st[2] = {NULL, NULL};    NSVStream *nst;    AVPacket *pkt;    uint32_t v = 0;    int i, err = 0;    uint8_t auxcount; /* number of aux metadata, also 4 bits of vsize */    uint32_t vsize;    uint16_t asize;    uint16_t auxsize;    uint32_t auxtag;        PRINT(("%s(%d)\n", __FUNCTION__, fill_header));        if (nsv->ahead[0].data || nsv->ahead[1].data)        return 0; //-1; /* hey! eat what you've in your plate first! */null_chunk_retry:    if (url_feof(pb))        return -1;        for (i = 0; i < NSV_MAX_RESYNC_TRIES && nsv->state < NSV_FOUND_NSVS && !err; i++)        err = nsv_resync(s);    if (err < 0)        return err;    if (nsv->state == NSV_FOUND_NSVS)        err = nsv_parse_NSVs_header(s, NULL);    if (err < 0)        return err;    if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)        return -1;        auxcount = get_byte(pb);    vsize = get_le16(pb);    asize = get_le16(pb);    vsize = (vsize << 4) | (auxcount >> 4);    auxcount &= 0x0f;    PRINT(("NSV CHUNK %d aux, %ld bytes video, %d bytes audio\n", auxcount, vsize, asize));    /* skip aux stuff */    for (i = 0; i < auxcount; i++) {        auxsize = get_le16(pb);        auxtag = get_le32(pb);        PRINT(("NSV aux data: '%c%c%c%c', %d bytes\n",               (auxtag & 0x0ff),               ((auxtag >> 8) & 0x0ff),               ((auxtag >> 16) & 0x0ff),              ((auxtag >> 24) & 0x0ff),              auxsize));        url_fskip(pb, auxsize);        vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); /* that's becoming braindead */    }        if (url_feof(pb))        return -1;    if (!vsize && !asize) {        nsv->state = NSV_UNSYNC;        goto null_chunk_retry;    }        /* map back streams to v,a */    if (s->streams[0])        st[s->streams[0]->id] = s->streams[0];    if (s->streams[1])        st[s->streams[1]->id] = s->streams[1];        if (vsize/* && st[NSV_ST_VIDEO]*/) {        nst = st[NSV_ST_VIDEO]->priv_data;        pkt = &nsv->ahead[NSV_ST_VIDEO];        av_new_packet(pkt, vsize);        get_buffer(pb, pkt->data, vsize);        pkt->stream_index = st[NSV_ST_VIDEO]->index;//NSV_ST_VIDEO;        pkt->dts = nst->frame_offset++;        pkt->flags |= PKT_FLAG_KEY; /* stupid format has no way to tell XXX: try the index *//*        for (i = 0; i < MIN(8, vsize); i++)            PRINT(("NSV video: [%d] = %02x\n", i, pkt->data[i]));*/    }    if (asize/*st[NSV_ST_AUDIO]*/) {        nst = st[NSV_ST_AUDIO]->priv_data;        pkt = &nsv->ahead[NSV_ST_AUDIO];        /* read raw audio specific header on the first audio chunk... */        /* on ALL audio chunks ?? seems so! */        if (asize && st[NSV_ST_AUDIO]->codec.codec_tag == MKTAG('P', 'C', 'M', ' ')/* && fill_header*/) {            uint8_t bps;            uint8_t channels;            uint16_t samplerate;            bps = get_byte(pb);            channels = get_byte(pb);            samplerate = get_le16(pb);            asize-=4;            PRINT(("NSV RAWAUDIO: bps %d, nchan %d, srate %ld\n", bps, channels, samplerate));            if (fill_header) {                st[NSV_ST_AUDIO]->need_parsing = 0; /* we know everything */                if (bps != 16) {                    PRINT(("NSV AUDIO bit/sample != 16 (%d)!!!\n", bps));                }                bps /= channels; // ???                if (bps == 8)                    st[NSV_ST_AUDIO]->codec.codec_id = CODEC_ID_PCM_U8;                samplerate /= 4;/* UGH ??? XXX */                channels = 1;                st[NSV_ST_AUDIO]->codec.channels = channels;                st[NSV_ST_AUDIO]->codec.sample_rate = samplerate;                av_set_pts_info(st[NSV_ST_AUDIO], 64, 1,                                 st[NSV_ST_AUDIO]->codec.sample_rate);                PRINT(("NSV RAWAUDIO: bps %d, nchan %d, srate %ld\n", bps, channels, samplerate));            }        }        av_new_packet(pkt, asize);        if (asize)            get_buffer(pb, pkt->data, asize);        pkt->stream_index = st[NSV_ST_AUDIO]->index;//NSV_ST_AUDIO;        //pkt->dts = nst->frame_offset;        //if (nst->sample_size)        //    pkt->dts /= nst->sample_size;        nst->frame_offset += asize; // XXX: that's valid only for PCM !?    }        //pkt->flags |= PKT_FLAG_KEY;    nsv->state = NSV_UNSYNC;    return 0;}