static int str_read_packet(AVFormatContext *s,

                           AVPacket *ret_pkt)

{

    AVIOContext *pb = s->pb;

    StrDemuxContext *str = s->priv_data;

    unsigned char sector[RAW_CD_SECTOR_SIZE];

    int channel;

    AVPacket *pkt;

    AVStream *st;



    while (1) {



        if (avio_read(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)

            return AVERROR(EIO);



        channel = sector[0x11];

        if (channel >= 32)

            return AVERROR_INVALIDDATA;



        switch (sector[0x12] & CDXA_TYPE_MASK) {



        case CDXA_TYPE_DATA:

        case CDXA_TYPE_VIDEO:

            {



                int current_sector = AV_RL16(&sector[0x1C]);

                int sector_count   = AV_RL16(&sector[0x1E]);

                int frame_size = AV_RL32(&sector[0x24]);



                if(!(   frame_size>=0

                     && current_sector < sector_count

                     && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){

                    av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);

                    break;

                }



                if(str->channels[channel].video_stream_index < 0){

                    /* allocate a new AVStream */

                    st = avformat_new_stream(s, NULL);

                    if (!st)

                        return AVERROR(ENOMEM);

                    avpriv_set_pts_info(st, 64, 1, 15);



                    str->channels[channel].video_stream_index = st->index;



                    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;

                    st->codec->codec_id   = AV_CODEC_ID_MDEC;

                    st->codec->codec_tag  = 0;  /* no fourcc */

                    st->codec->width      = AV_RL16(&sector[0x28]);

                    st->codec->height     = AV_RL16(&sector[0x2A]);

                }



                /* if this is the first sector of the frame, allocate a pkt */

                pkt = &str->channels[channel].tmp_pkt;



                if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){

                    if(pkt->data)

                        av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");

                    av_free_packet(pkt);

                    if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))

                        return AVERROR(EIO);




                    pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;

                    pkt->stream_index =

                        str->channels[channel].video_stream_index;

                }



                memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,

                       sector + VIDEO_DATA_HEADER_SIZE,

                       VIDEO_DATA_CHUNK_SIZE);



                if (current_sector == sector_count-1) {

                    pkt->size= frame_size;

                    *ret_pkt = *pkt;

                    pkt->data= NULL;

                    pkt->size= -1;

                    pkt->buf = NULL;

#if FF_API_DESTRUCT_PACKET

FF_DISABLE_DEPRECATION_WARNINGS

                    pkt->destruct = NULL;

FF_ENABLE_DEPRECATION_WARNINGS

#endif

                    return 0;

                }



            }

            break;



        case CDXA_TYPE_AUDIO:

            if(str->channels[channel].audio_stream_index < 0){

                int fmt = sector[0x13];

                /* allocate a new AVStream */

                st = avformat_new_stream(s, NULL);

                if (!st)

                    return AVERROR(ENOMEM);



                str->channels[channel].audio_stream_index = st->index;



                st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;

                st->codec->codec_id    = AV_CODEC_ID_ADPCM_XA;

                st->codec->codec_tag   = 0;  /* no fourcc */

                if (fmt & 1) {

                    st->codec->channels       = 2;

                    st->codec->channel_layout = AV_CH_LAYOUT_STEREO;

                } else {

                    st->codec->channels       = 1;

                    st->codec->channel_layout = AV_CH_LAYOUT_MONO;

                }

                st->codec->sample_rate = (fmt&4)?18900:37800;

            //    st->codec->bit_rate = 0; //FIXME;

                st->codec->block_align = 128;



                avpriv_set_pts_info(st, 64, 18 * 224 / st->codec->channels,

                                    st->codec->sample_rate);

                st->start_time = 0;

            }

            pkt = ret_pkt;

            if (av_new_packet(pkt, 2304))

                return AVERROR(EIO);

            memcpy(pkt->data,sector+24,2304);



            pkt->stream_index =

                str->channels[channel].audio_stream_index;

            pkt->duration = 1;

            return 0;

        default:

            av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);

            /* drop the sector and move on */

            break;

        }



        if (url_feof(pb))

            return AVERROR(EIO);

    }

}
static int str_read_packet(AVFormatContext *s,

                           AVPacket *ret_pkt)

{

    AVIOContext *pb = s->pb;

    StrDemuxContext *str = s->priv_data;

    unsigned char sector[RAW_CD_SECTOR_SIZE];

    int channel;

    AVPacket *pkt;

    AVStream *st;



    while (1) {



        if (avio_read(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)

            return AVERROR(EIO);



        channel = sector[0x11];

        if (channel >= 32)

            return AVERROR_INVALIDDATA;



        switch (sector[0x12] & CDXA_TYPE_MASK) {



        case CDXA_TYPE_DATA:

        case CDXA_TYPE_VIDEO:

            {



                int current_sector = AV_RL16(&sector[0x1C]);

                int sector_count   = AV_RL16(&sector[0x1E]);

                int frame_size = AV_RL32(&sector[0x24]);



                if(!(   frame_size>=0

                     && current_sector < sector_count

                     && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){

                    av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);

                    break;

                }



                if(str->channels[channel].video_stream_index < 0){

                    

                    st = avformat_new_stream(s, NULL);

                    if (!st)

                        return AVERROR(ENOMEM);

                    avpriv_set_pts_info(st, 64, 1, 15);



                    str->channels[channel].video_stream_index = st->index;



                    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;

                    st->codec->codec_id   = AV_CODEC_ID_MDEC;

                    st->codec->codec_tag  = 0;  

                    st->codec->width      = AV_RL16(&sector[0x28]);

                    st->codec->height     = AV_RL16(&sector[0x2A]);

                }



                

                pkt = &str->channels[channel].tmp_pkt;



                if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){

                    if(pkt->data)

                        av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");

                    av_free_packet(pkt);

                    if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))

                        return AVERROR(EIO);




                    pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;

                    pkt->stream_index =

                        str->channels[channel].video_stream_index;

                }



                memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,

                       sector + VIDEO_DATA_HEADER_SIZE,

                       VIDEO_DATA_CHUNK_SIZE);



                if (current_sector == sector_count-1) {

                    pkt->size= frame_size;

                    *ret_pkt = *pkt;

                    pkt->data= NULL;

                    pkt->size= -1;

                    pkt->buf = NULL;

#if FF_API_DESTRUCT_PACKET

FF_DISABLE_DEPRECATION_WARNINGS

                    pkt->destruct = NULL;

FF_ENABLE_DEPRECATION_WARNINGS

#endif

                    return 0;

                }



            }

            break;



        case CDXA_TYPE_AUDIO:

            if(str->channels[channel].audio_stream_index < 0){

                int fmt = sector[0x13];

                

                st = avformat_new_stream(s, NULL);

                if (!st)

                    return AVERROR(ENOMEM);



                str->channels[channel].audio_stream_index = st->index;



                st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;

                st->codec->codec_id    = AV_CODEC_ID_ADPCM_XA;

                st->codec->codec_tag   = 0;  

                if (fmt & 1) {

                    st->codec->channels       = 2;

                    st->codec->channel_layout = AV_CH_LAYOUT_STEREO;

                } else {

                    st->codec->channels       = 1;

                    st->codec->channel_layout = AV_CH_LAYOUT_MONO;

                }

                st->codec->sample_rate = (fmt&4)?18900:37800;

            

                st->codec->block_align = 128;



                avpriv_set_pts_info(st, 64, 18 * 224 / st->codec->channels,

                                    st->codec->sample_rate);

                st->start_time = 0;

            }

            pkt = ret_pkt;

            if (av_new_packet(pkt, 2304))

                return AVERROR(EIO);

            memcpy(pkt->data,sector+24,2304);



            pkt->stream_index =

                str->channels[channel].audio_stream_index;

            pkt->duration = 1;

            return 0;

        default:

            av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);

            

            break;

        }



        if (url_feof(pb))

            return AVERROR(EIO);

    }

}
static int FUNC_0(AVFormatContext *VAR_0,

                           AVPacket *VAR_1)

{

    AVIOContext *pb = VAR_0->pb;

    StrDemuxContext *str = VAR_0->priv_data;

    unsigned char VAR_2[RAW_CD_SECTOR_SIZE];

    int VAR_3;

    AVPacket *pkt;

    AVStream *st;



    while (1) {



        if (avio_read(pb, VAR_2, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)

            return AVERROR(EIO);



        VAR_3 = VAR_2[0x11];

        if (VAR_3 >= 32)

            return AVERROR_INVALIDDATA;



        switch (VAR_2[0x12] & CDXA_TYPE_MASK) {



        case CDXA_TYPE_DATA:

        case CDXA_TYPE_VIDEO:

            {



                int VAR_4 = AV_RL16(&VAR_2[0x1C]);

                int VAR_5   = AV_RL16(&VAR_2[0x1E]);

                int VAR_6 = AV_RL32(&VAR_2[0x24]);



                if(!(   VAR_6>=0

                     && VAR_4 < VAR_5

                     && VAR_5*VIDEO_DATA_CHUNK_SIZE >=VAR_6)){

                    av_log(VAR_0, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", VAR_4, VAR_5, VAR_6);

                    break;

                }



                if(str->channels[VAR_3].video_stream_index < 0){

                    

                    st = avformat_new_stream(VAR_0, NULL);

                    if (!st)

                        return AVERROR(ENOMEM);

                    avpriv_set_pts_info(st, 64, 1, 15);



                    str->channels[VAR_3].video_stream_index = st->index;



                    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;

                    st->codec->codec_id   = AV_CODEC_ID_MDEC;

                    st->codec->codec_tag  = 0;  

                    st->codec->width      = AV_RL16(&VAR_2[0x28]);

                    st->codec->height     = AV_RL16(&VAR_2[0x2A]);

                }



                

                pkt = &str->channels[VAR_3].tmp_pkt;



                if(pkt->size != VAR_5*VIDEO_DATA_CHUNK_SIZE){

                    if(pkt->data)

                        av_log(VAR_0, AV_LOG_ERROR, "missmatching VAR_5\n");

                    av_free_packet(pkt);

                    if (av_new_packet(pkt, VAR_5*VIDEO_DATA_CHUNK_SIZE))

                        return AVERROR(EIO);




                    pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;

                    pkt->stream_index =

                        str->channels[VAR_3].video_stream_index;

                }



                memcpy(pkt->data + VAR_4*VIDEO_DATA_CHUNK_SIZE,

                       VAR_2 + VIDEO_DATA_HEADER_SIZE,

                       VIDEO_DATA_CHUNK_SIZE);



                if (VAR_4 == VAR_5-1) {

                    pkt->size= VAR_6;

                    *VAR_1 = *pkt;

                    pkt->data= NULL;

                    pkt->size= -1;

                    pkt->buf = NULL;

#if FF_API_DESTRUCT_PACKET

FF_DISABLE_DEPRECATION_WARNINGS

                    pkt->destruct = NULL;

FF_ENABLE_DEPRECATION_WARNINGS

#endif

                    return 0;

                }



            }

            break;



        case CDXA_TYPE_AUDIO:

            if(str->channels[VAR_3].audio_stream_index < 0){

                int VAR_7 = VAR_2[0x13];

                

                st = avformat_new_stream(VAR_0, NULL);

                if (!st)

                    return AVERROR(ENOMEM);



                str->channels[VAR_3].audio_stream_index = st->index;



                st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;

                st->codec->codec_id    = AV_CODEC_ID_ADPCM_XA;

                st->codec->codec_tag   = 0;  

                if (VAR_7 & 1) {

                    st->codec->channels       = 2;

                    st->codec->channel_layout = AV_CH_LAYOUT_STEREO;

                } else {

                    st->codec->channels       = 1;

                    st->codec->channel_layout = AV_CH_LAYOUT_MONO;

                }

                st->codec->sample_rate = (VAR_7&4)?18900:37800;

            

                st->codec->block_align = 128;



                avpriv_set_pts_info(st, 64, 18 * 224 / st->codec->channels,

                                    st->codec->sample_rate);

                st->start_time = 0;

            }

            pkt = VAR_1;

            if (av_new_packet(pkt, 2304))

                return AVERROR(EIO);

            memcpy(pkt->data,VAR_2+24,2304);



            pkt->stream_index =

                str->channels[VAR_3].audio_stream_index;

            pkt->duration = 1;

            return 0;

        default:

            av_log(VAR_0, AV_LOG_WARNING, "Unknown VAR_2 type %02X\n", VAR_2[0x12]);

            

            break;

        }



        if (url_feof(pb))

            return AVERROR(EIO);

    }

}
static int FUNC_0(AVFormatContext *VAR_0,
AVPacket *VAR_1)
{ AVIOContext *pb = VAR_0->pb; StrDemuxContext *str = VAR_0->priv_data; unsigned char VAR_2[RAW_CD_SECTOR_SIZE]; int VAR_3; AVPacket *pkt; AVStream *st; while (1) { if (avio_read(pb, VAR_2, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
return AVERROR(EIO); VAR_3 = VAR_2[0x11]; if (VAR_3 >= 32)
return AVERROR_INVALIDDATA; switch (VAR_2[0x12] & CDXA_TYPE_MASK) { case CDXA_TYPE_DATA:
case CDXA_TYPE_VIDEO:
{ int VAR_4 = AV_RL16(&VAR_2[0x1C]); int VAR_5   = AV_RL16(&VAR_2[0x1E]); int VAR_6 = AV_RL32(&VAR_2[0x24]); if(!(   VAR_6>=0
&& VAR_4 < VAR_5
&& VAR_5*VIDEO_DATA_CHUNK_SIZE >=VAR_6)){ av_log(VAR_0, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", VAR_4, VAR_5, VAR_6); break; } if(str->channels[VAR_3].video_stream_index < 0){ st = avformat_new_stream(VAR_0, NULL); if (!st)
return AVERROR(ENOMEM); avpriv_set_pts_info(st, 64, 1, 15); str->channels[VAR_3].video_stream_index = st->index; st->codec->codec_type = AVMEDIA_TYPE_VIDEO; st->codec->codec_id   = AV_CODEC_ID_MDEC; st->codec->codec_tag  = 0; st->codec->width      = AV_RL16(&VAR_2[0x28]); st->codec->height     = AV_RL16(&VAR_2[0x2A]); } pkt = &str->channels[VAR_3].tmp_pkt; if(pkt->size != VAR_5*VIDEO_DATA_CHUNK_SIZE){ if(pkt->data)
av_log(VAR_0, AV_LOG_ERROR, "missmatching VAR_5\n"); av_free_packet(pkt); if (av_new_packet(pkt, VAR_5*VIDEO_DATA_CHUNK_SIZE))
return AVERROR(EIO); pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE; pkt->stream_index =
str->channels[VAR_3].video_stream_index; } memcpy(pkt->data + VAR_4*VIDEO_DATA_CHUNK_SIZE,
VAR_2 + VIDEO_DATA_HEADER_SIZE,
VIDEO_DATA_CHUNK_SIZE); if (VAR_4 == VAR_5-1) { pkt->size= VAR_6; *VAR_1 = *pkt; pkt->data= NULL; pkt->size= -1; pkt->buf = NULL; #if FF_API_DESTRUCT_PACKET
FF_DISABLE_DEPRECATION_WARNINGS
pkt->destruct = NULL; FF_ENABLE_DEPRECATION_WARNINGS
#endif
return 0; } } break; case CDXA_TYPE_AUDIO:
if(str->channels[VAR_3].audio_stream_index < 0){ int VAR_7 = VAR_2[0x13]; st = avformat_new_stream(VAR_0, NULL); if (!st)
return AVERROR(ENOMEM); str->channels[VAR_3].audio_stream_index = st->index; st->codec->codec_type  = AVMEDIA_TYPE_AUDIO; st->codec->codec_id    = AV_CODEC_ID_ADPCM_XA; st->codec->codec_tag   = 0; if (VAR_7 & 1) { st->codec->channels       = 2; st->codec->channel_layout = AV_CH_LAYOUT_STEREO; } else { st->codec->channels       = 1; st->codec->channel_layout = AV_CH_LAYOUT_MONO; } st->codec->sample_rate = (VAR_7&4)?18900:37800; st->codec->block_align = 128; avpriv_set_pts_info(st, 64, 18 * 224 / st->codec->channels,
st->codec->sample_rate); st->start_time = 0; } pkt = VAR_1; if (av_new_packet(pkt, 2304))
return AVERROR(EIO); memcpy(pkt->data,VAR_2+24,2304); pkt->stream_index =
str->channels[VAR_3].audio_stream_index; pkt->duration = 1; return 0; default:
av_log(VAR_0, AV_LOG_WARNING, "Unknown VAR_2 type %02X\n", VAR_2[0x12]); break; } if (url_feof(pb))
return AVERROR(EIO); } }