static int nsv_read_chunk(AVFormatContext *s, int fill_header)

{

    NSVContext *nsv = s->priv_data;

    AVIOContext *pb = s->pb;

    AVStream *st[2] = {NULL, NULL};

    NSVStream *nst;

    AVPacket *pkt;

    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;



    av_dlog(s, "%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 (avio_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);

    if (err < 0)

        return err;

    if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)

        return -1;



    auxcount = avio_r8(pb);

    vsize = avio_rl16(pb);

    asize = avio_rl16(pb);

    vsize = (vsize << 4) | (auxcount >> 4);

    auxcount &= 0x0f;

    av_dlog(s, "NSV CHUNK %d aux, %u bytes video, %d bytes audio\n", auxcount, vsize, asize);

    /* skip aux stuff */

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

        uint32_t av_unused auxtag;

        auxsize = avio_rl16(pb);

        auxtag = avio_rl32(pb);

        av_dlog(s, "NSV aux data: '%c%c%c%c', %d bytes\n",

              (auxtag & 0x0ff),

              ((auxtag >> 8) & 0x0ff),

              ((auxtag >> 16) & 0x0ff),

              ((auxtag >> 24) & 0x0ff),

              auxsize);

        avio_skip(pb, auxsize);

        vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); /* that's becoming braindead */

    }



    if (avio_feof(pb))

        return -1;

    if (!vsize && !asize) {

        nsv->state = NSV_UNSYNC;

        goto null_chunk_retry;

    }



    /* map back streams to v,a */

    if (s->nb_streams > 0)

        st[s->streams[0]->id] = s->streams[0];

    if (s->nb_streams > 1)

        st[s->streams[1]->id] = s->streams[1];



    if (vsize && st[NSV_ST_VIDEO]) {

        int ret;

        nst = st[NSV_ST_VIDEO]->priv_data;

        pkt = &nsv->ahead[NSV_ST_VIDEO];

        if ((ret = av_get_packet(pb, pkt, vsize)) < 0)

            return ret;

        pkt->stream_index = st[NSV_ST_VIDEO]->index;//NSV_ST_VIDEO;

        pkt->dts = nst->frame_offset;

        pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */

        for (i = 0; i < FFMIN(8, vsize); i++)

            av_dlog(s, "NSV video: [%d] = %02x\n", i, pkt->data[i]);

    }

    if(st[NSV_ST_VIDEO])

        ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++;



    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 = avio_r8(pb);

            channels = avio_r8(pb);

            samplerate = avio_rl16(pb);

            asize-=4;

            av_dlog(s, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);

            if (fill_header) {

                st[NSV_ST_AUDIO]->need_parsing = AVSTREAM_PARSE_NONE; /* we know everything */

                if (bps != 16) {

                    av_dlog(s, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps);

                }

                if(channels)

                    bps /= channels; // ???

                else

                    av_log(s, AV_LOG_WARNING, "Channels is 0\n");

                if (bps == 8)

                    st[NSV_ST_AUDIO]->codec->codec_id = AV_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_dlog(s, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);

            }

        }

        av_get_packet(pb, pkt, asize);

        pkt->stream_index = st[NSV_ST_AUDIO]->index;//NSV_ST_AUDIO;

        pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */

        if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) {

            /* on a nsvs frame we have new information on a/v sync */

            pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1);

            pkt->dts *= (int64_t)1000        * nsv->framerate.den;

            pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num;

            av_dlog(s, "NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts);

        }

        nst->frame_offset++;

    }



    nsv->state = NSV_UNSYNC;

    return 0;

}

static int nsv_read_chunk(AVFormatContext *s, int fill_header)

{

    NSVContext *nsv = s->priv_data;

    AVIOContext *pb = s->pb;

    AVStream *st[2] = {NULL, NULL};

    NSVStream *nst;

    AVPacket *pkt;

    int i, err = 0;

    uint8_t auxcount; 

    uint32_t vsize;

    uint16_t asize;

    uint16_t auxsize;



    av_dlog(s, "%s(%d)\n", __FUNCTION__, fill_header);



    if (nsv->ahead[0].data || nsv->ahead[1].data)

        return 0; 



null_chunk_retry:

    if (avio_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);

    if (err < 0)

        return err;

    if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)

        return -1;



    auxcount = avio_r8(pb);

    vsize = avio_rl16(pb);

    asize = avio_rl16(pb);

    vsize = (vsize << 4) | (auxcount >> 4);

    auxcount &= 0x0f;

    av_dlog(s, "NSV CHUNK %d aux, %u bytes video, %d bytes audio\n", auxcount, vsize, asize);

    

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

        uint32_t av_unused auxtag;

        auxsize = avio_rl16(pb);

        auxtag = avio_rl32(pb);

        av_dlog(s, "NSV aux data: '%c%c%c%c', %d bytes\n",

              (auxtag & 0x0ff),

              ((auxtag >> 8) & 0x0ff),

              ((auxtag >> 16) & 0x0ff),

              ((auxtag >> 24) & 0x0ff),

              auxsize);

        avio_skip(pb, auxsize);

        vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); 

    }



    if (avio_feof(pb))

        return -1;

    if (!vsize && !asize) {

        nsv->state = NSV_UNSYNC;

        goto null_chunk_retry;

    }



    

    if (s->nb_streams > 0)

        st[s->streams[0]->id] = s->streams[0];

    if (s->nb_streams > 1)

        st[s->streams[1]->id] = s->streams[1];



    if (vsize && st[NSV_ST_VIDEO]) {

        int ret;

        nst = st[NSV_ST_VIDEO]->priv_data;

        pkt = &nsv->ahead[NSV_ST_VIDEO];

        if ((ret = av_get_packet(pb, pkt, vsize)) < 0)

            return ret;

        pkt->stream_index = st[NSV_ST_VIDEO]->index;

        pkt->dts = nst->frame_offset;

        pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; 

        for (i = 0; i < FFMIN(8, vsize); i++)

            av_dlog(s, "NSV video: [%d] = %02x\n", i, pkt->data[i]);

    }

    if(st[NSV_ST_VIDEO])

        ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++;



    if (asize && st[NSV_ST_AUDIO]) {

        nst = st[NSV_ST_AUDIO]->priv_data;

        pkt = &nsv->ahead[NSV_ST_AUDIO];

        

        

        if (asize && st[NSV_ST_AUDIO]->codec->codec_tag == MKTAG('P', 'C', 'M', ' ')) {

            uint8_t bps;

            uint8_t channels;

            uint16_t samplerate;

            bps = avio_r8(pb);

            channels = avio_r8(pb);

            samplerate = avio_rl16(pb);

            asize-=4;

            av_dlog(s, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);

            if (fill_header) {

                st[NSV_ST_AUDIO]->need_parsing = AVSTREAM_PARSE_NONE; 

                if (bps != 16) {

                    av_dlog(s, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps);

                }

                if(channels)

                    bps /= channels; 

                else

                    av_log(s, AV_LOG_WARNING, "Channels is 0\n");

                if (bps == 8)

                    st[NSV_ST_AUDIO]->codec->codec_id = AV_CODEC_ID_PCM_U8;

                samplerate /= 4;

                channels = 1;

                st[NSV_ST_AUDIO]->codec->channels = channels;

                st[NSV_ST_AUDIO]->codec->sample_rate = samplerate;

                av_dlog(s, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);

            }

        }

        av_get_packet(pb, pkt, asize);

        pkt->stream_index = st[NSV_ST_AUDIO]->index;

        pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; 

        if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) {

            

            pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1);

            pkt->dts *= (int64_t)1000        * nsv->framerate.den;

            pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num;

            av_dlog(s, "NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts);

        }

        nst->frame_offset++;

    }



    nsv->state = NSV_UNSYNC;

    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, int VAR_1)

{

    NSVContext *nsv = VAR_0->priv_data;

    AVIOContext *pb = VAR_0->pb;

    AVStream *st[2] = {NULL, NULL};

    NSVStream *nst;

    AVPacket *pkt;

    int VAR_2, VAR_3 = 0;

    uint8_t auxcount; 

    uint32_t vsize;

    uint16_t asize;

    uint16_t auxsize;



    av_dlog(VAR_0, "%VAR_0(%d)\n", __FUNCTION__, VAR_1);



    if (nsv->ahead[0].data || nsv->ahead[1].data)

        return 0; 



null_chunk_retry:

    if (avio_feof(pb))

        return -1;



    for (VAR_2 = 0; VAR_2 < NSV_MAX_RESYNC_TRIES && nsv->state < NSV_FOUND_NSVS && !VAR_3; VAR_2++)

        VAR_3 = nsv_resync(VAR_0);

    if (VAR_3 < 0)

        return VAR_3;

    if (nsv->state == NSV_FOUND_NSVS)

        VAR_3 = nsv_parse_NSVs_header(VAR_0);

    if (VAR_3 < 0)

        return VAR_3;

    if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)

        return -1;



    auxcount = avio_r8(pb);

    vsize = avio_rl16(pb);

    asize = avio_rl16(pb);

    vsize = (vsize << 4) | (auxcount >> 4);

    auxcount &= 0x0f;

    av_dlog(VAR_0, "NSV CHUNK %d aux, %u bytes video, %d bytes audio\n", auxcount, vsize, asize);

    

    for (VAR_2 = 0; VAR_2 < auxcount; VAR_2++) {

        uint32_t av_unused auxtag;

        auxsize = avio_rl16(pb);

        auxtag = avio_rl32(pb);

        av_dlog(VAR_0, "NSV aux data: '%c%c%c%c', %d bytes\n",

              (auxtag & 0x0ff),

              ((auxtag >> 8) & 0x0ff),

              ((auxtag >> 16) & 0x0ff),

              ((auxtag >> 24) & 0x0ff),

              auxsize);

        avio_skip(pb, auxsize);

        vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); 

    }



    if (avio_feof(pb))

        return -1;

    if (!vsize && !asize) {

        nsv->state = NSV_UNSYNC;

        goto null_chunk_retry;

    }



    

    if (VAR_0->nb_streams > 0)

        st[VAR_0->streams[0]->id] = VAR_0->streams[0];

    if (VAR_0->nb_streams > 1)

        st[VAR_0->streams[1]->id] = VAR_0->streams[1];



    if (vsize && st[NSV_ST_VIDEO]) {

        int VAR_4;

        nst = st[NSV_ST_VIDEO]->priv_data;

        pkt = &nsv->ahead[NSV_ST_VIDEO];

        if ((VAR_4 = av_get_packet(pb, pkt, vsize)) < 0)

            return VAR_4;

        pkt->stream_index = st[NSV_ST_VIDEO]->index;

        pkt->dts = nst->frame_offset;

        pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; 

        for (VAR_2 = 0; VAR_2 < FFMIN(8, vsize); VAR_2++)

            av_dlog(VAR_0, "NSV video: [%d] = %02x\n", VAR_2, pkt->data[VAR_2]);

    }

    if(st[NSV_ST_VIDEO])

        ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++;



    if (asize && st[NSV_ST_AUDIO]) {

        nst = st[NSV_ST_AUDIO]->priv_data;

        pkt = &nsv->ahead[NSV_ST_AUDIO];

        

        

        if (asize && st[NSV_ST_AUDIO]->codec->codec_tag == MKTAG('P', 'C', 'M', ' ')) {

            uint8_t bps;

            uint8_t channels;

            uint16_t samplerate;

            bps = avio_r8(pb);

            channels = avio_r8(pb);

            samplerate = avio_rl16(pb);

            asize-=4;

            av_dlog(VAR_0, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);

            if (VAR_1) {

                st[NSV_ST_AUDIO]->need_parsing = AVSTREAM_PARSE_NONE; 

                if (bps != 16) {

                    av_dlog(VAR_0, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps);

                }

                if(channels)

                    bps /= channels; 

                else

                    av_log(VAR_0, AV_LOG_WARNING, "Channels is 0\n");

                if (bps == 8)

                    st[NSV_ST_AUDIO]->codec->codec_id = AV_CODEC_ID_PCM_U8;

                samplerate /= 4;

                channels = 1;

                st[NSV_ST_AUDIO]->codec->channels = channels;

                st[NSV_ST_AUDIO]->codec->sample_rate = samplerate;

                av_dlog(VAR_0, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);

            }

        }

        av_get_packet(pb, pkt, asize);

        pkt->stream_index = st[NSV_ST_AUDIO]->index;

        pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; 

        if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) {

            

            pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1);

            pkt->dts *= (int64_t)1000        * nsv->framerate.den;

            pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num;

            av_dlog(VAR_0, "NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts);

        }

        nst->frame_offset++;

    }



    nsv->state = NSV_UNSYNC;

    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, int VAR_1)
{ NSVContext *nsv = VAR_0->priv_data; AVIOContext *pb = VAR_0->pb; AVStream *st[2] = {NULL, NULL}; NSVStream *nst; AVPacket *pkt; int VAR_2, VAR_3 = 0; uint8_t auxcount; uint32_t vsize; uint16_t asize; uint16_t auxsize; av_dlog(VAR_0, "%VAR_0(%d)\n", __FUNCTION__, VAR_1); if (nsv->ahead[0].data || nsv->ahead[1].data)
return 0; null_chunk_retry:
if (avio_feof(pb))
return -1; for (VAR_2 = 0; VAR_2 < NSV_MAX_RESYNC_TRIES && nsv->state < NSV_FOUND_NSVS && !VAR_3; VAR_2++) VAR_3 = nsv_resync(VAR_0); if (VAR_3 < 0)
return VAR_3; if (nsv->state == NSV_FOUND_NSVS)
VAR_3 = nsv_parse_NSVs_header(VAR_0); if (VAR_3 < 0)
return VAR_3; if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)
return -1; auxcount = avio_r8(pb); vsize = avio_rl16(pb); asize = avio_rl16(pb); vsize = (vsize << 4) | (auxcount >> 4); auxcount &= 0x0f; av_dlog(VAR_0, "NSV CHUNK %d aux, %u bytes video, %d bytes audio\n", auxcount, vsize, asize); for (VAR_2 = 0; VAR_2 < auxcount; VAR_2++) { uint32_t av_unused auxtag; auxsize = avio_rl16(pb); auxtag = avio_rl32(pb); av_dlog(VAR_0, "NSV aux data: '%c%c%c%c', %d bytes\n",
(auxtag & 0x0ff),
((auxtag >> 8) & 0x0ff),
((auxtag >> 16) & 0x0ff),
((auxtag >> 24) & 0x0ff),
auxsize); avio_skip(pb, auxsize); vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); } if (avio_feof(pb))
return -1; if (!vsize && !asize) { nsv->state = NSV_UNSYNC; goto null_chunk_retry; } if (VAR_0->nb_streams > 0)
st[VAR_0->streams[0]->id] = VAR_0->streams[0]; if (VAR_0->nb_streams > 1)
st[VAR_0->streams[1]->id] = VAR_0->streams[1]; if (vsize && st[NSV_ST_VIDEO]) { int VAR_4; nst = st[NSV_ST_VIDEO]->priv_data; pkt = &nsv->ahead[NSV_ST_VIDEO]; if ((VAR_4 = av_get_packet(pb, pkt, vsize)) < 0)
return VAR_4; pkt->stream_index = st[NSV_ST_VIDEO]->index; pkt->dts = nst->frame_offset; pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; for (VAR_2 = 0; VAR_2 < FFMIN(8, vsize); VAR_2++) av_dlog(VAR_0, "NSV video: [%d] = %02x\n", VAR_2, pkt->data[VAR_2]); } if(st[NSV_ST_VIDEO])
((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++; if (asize && st[NSV_ST_AUDIO]) { nst = st[NSV_ST_AUDIO]->priv_data; pkt = &nsv->ahead[NSV_ST_AUDIO]; if (asize && st[NSV_ST_AUDIO]->codec->codec_tag == MKTAG('P', 'C', 'M', ' ')) { uint8_t bps; uint8_t channels; uint16_t samplerate; bps = avio_r8(pb); channels = avio_r8(pb); samplerate = avio_rl16(pb); asize-=4; av_dlog(VAR_0, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate); if (VAR_1) { st[NSV_ST_AUDIO]->need_parsing = AVSTREAM_PARSE_NONE; if (bps != 16) { av_dlog(VAR_0, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps); } if(channels)
bps /= channels; else
av_log(VAR_0, AV_LOG_WARNING, "Channels is 0\n"); if (bps == 8)
st[NSV_ST_AUDIO]->codec->codec_id = AV_CODEC_ID_PCM_U8; samplerate /= 4; channels = 1; st[NSV_ST_AUDIO]->codec->channels = channels; st[NSV_ST_AUDIO]->codec->sample_rate = samplerate; av_dlog(VAR_0, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate); } } av_get_packet(pb, pkt, asize); pkt->stream_index = st[NSV_ST_AUDIO]->index; pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) { pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1); pkt->dts *= (int64_t)1000        * nsv->framerate.den; pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num; av_dlog(VAR_0, "NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts); } nst->frame_offset++; } nsv->state = NSV_UNSYNC; return 0; }