static int mpeg_mux_read_header(AVFormatContext *s,

                                AVFormatParameters *ap)

{

    MpegDemuxContext *m;

    int size, startcode, c, rate_bound, audio_bound, video_bound, mux_rate, val;

    int codec_id, n, i, type;

    AVStream *st;



    m = av_mallocz(sizeof(MpegDemuxContext));

    if (!m)

        return -ENOMEM;

    s->priv_data = m;



    /* search first pack header */

    m->header_state = 0xff;

    size = MAX_SYNC_SIZE;

    for(;;) {

        while (size > 0) {

            startcode = find_start_code(&s->pb, &size, &m->header_state);

            if (startcode == PACK_START_CODE)

                goto found;

        }

        return -ENODATA;

    found:

        /* search system header just after pack header */

        /* parse pack header */

        get_byte(&s->pb); /* ts1 */

        get_be16(&s->pb); /* ts2 */

        get_be16(&s->pb); /* ts3 */



        mux_rate = get_byte(&s->pb) << 16; 

        mux_rate |= get_byte(&s->pb) << 8;

        mux_rate |= get_byte(&s->pb);

        mux_rate &= (1 << 22) - 1;

        m->mux_rate = mux_rate;



        startcode = find_start_code(&s->pb, &size, &m->header_state);

        if (startcode == SYSTEM_HEADER_START_CODE)

            break;

    }

    size = get_be16(&s->pb);

    rate_bound = get_byte(&s->pb) << 16;

    rate_bound |= get_byte(&s->pb) << 8;

    rate_bound |= get_byte(&s->pb);

    rate_bound = (rate_bound >> 1) & ((1 << 22) - 1);

    audio_bound = get_byte(&s->pb) >> 2;

    video_bound = get_byte(&s->pb) & 0x1f;

    get_byte(&s->pb); /* reserved byte */

#if 0

    printf("mux_rate=%d kbit/s\n", (m->mux_rate * 50 * 8) / 1000);

    printf("rate_bound=%d\n", rate_bound);

    printf("audio_bound=%d\n", audio_bound);

    printf("video_bound=%d\n", video_bound);

#endif

    size -= 6;

    s->nb_streams = 0;

    while (size > 0) {

        c = get_byte(&s->pb);

        size--;

        if ((c & 0x80) == 0)

            break;

        val = get_be16(&s->pb);

        size -= 2;

        if (c >= 0xc0 && c <= 0xdf) {

            /* mpeg audio stream */

            type = CODEC_TYPE_AUDIO;

            codec_id = CODEC_ID_MP2;

            n = 1;

            c = c | 0x100;

        } else if (c >= 0xe0 && c <= 0xef) {

            type = CODEC_TYPE_VIDEO;

            codec_id = CODEC_ID_MPEG1VIDEO;

            n = 1;

            c = c | 0x100;

        } else if (c == 0xb8) {

            /* all audio streams */

            /* XXX: hack for DVD: we force AC3, although we do not

               know that this codec will be used */

            type = CODEC_TYPE_AUDIO;

            codec_id = CODEC_ID_AC3;

            n = audio_bound;

            c = 0x80;

            /*            c = 0x1c0; */

        } else if (c == 0xb9) {

            /* all video streams */

            type = CODEC_TYPE_VIDEO;

            codec_id = CODEC_ID_MPEG1VIDEO;

            n = video_bound;

            c = 0x1e0;

        } else {

            type = 0;

            codec_id = 0;

            n = 0;

        }

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

            st = av_mallocz(sizeof(AVStream));

            if (!st)

                return -ENOMEM;

            s->streams[s->nb_streams++] = st;

            st->id = c + i;

            st->codec.codec_type = type;

            st->codec.codec_id = codec_id;

        }

    }



    return 0;

}

static int mpeg_mux_read_header(AVFormatContext *s,

                                AVFormatParameters *ap)

{

    MpegDemuxContext *m;

    int size, startcode, c, rate_bound, audio_bound, video_bound, mux_rate, val;

    int codec_id, n, i, type;

    AVStream *st;



    m = av_mallocz(sizeof(MpegDemuxContext));

    if (!m)

        return -ENOMEM;

    s->priv_data = m;



    

    m->header_state = 0xff;

    size = MAX_SYNC_SIZE;

    for(;;) {

        while (size > 0) {

            startcode = find_start_code(&s->pb, &size, &m->header_state);

            if (startcode == PACK_START_CODE)

                goto found;

        }

        return -ENODATA;

    found:

        

        

        get_byte(&s->pb); 

        get_be16(&s->pb); 

        get_be16(&s->pb); 



        mux_rate = get_byte(&s->pb) << 16; 

        mux_rate |= get_byte(&s->pb) << 8;

        mux_rate |= get_byte(&s->pb);

        mux_rate &= (1 << 22) - 1;

        m->mux_rate = mux_rate;



        startcode = find_start_code(&s->pb, &size, &m->header_state);

        if (startcode == SYSTEM_HEADER_START_CODE)

            break;

    }

    size = get_be16(&s->pb);

    rate_bound = get_byte(&s->pb) << 16;

    rate_bound |= get_byte(&s->pb) << 8;

    rate_bound |= get_byte(&s->pb);

    rate_bound = (rate_bound >> 1) & ((1 << 22) - 1);

    audio_bound = get_byte(&s->pb) >> 2;

    video_bound = get_byte(&s->pb) & 0x1f;

    get_byte(&s->pb); 

#if 0

    printf("mux_rate=%d kbit/s\n", (m->mux_rate * 50 * 8) / 1000);

    printf("rate_bound=%d\n", rate_bound);

    printf("audio_bound=%d\n", audio_bound);

    printf("video_bound=%d\n", video_bound);

#endif

    size -= 6;

    s->nb_streams = 0;

    while (size > 0) {

        c = get_byte(&s->pb);

        size--;

        if ((c & 0x80) == 0)

            break;

        val = get_be16(&s->pb);

        size -= 2;

        if (c >= 0xc0 && c <= 0xdf) {

            

            type = CODEC_TYPE_AUDIO;

            codec_id = CODEC_ID_MP2;

            n = 1;

            c = c | 0x100;

        } else if (c >= 0xe0 && c <= 0xef) {

            type = CODEC_TYPE_VIDEO;

            codec_id = CODEC_ID_MPEG1VIDEO;

            n = 1;

            c = c | 0x100;

        } else if (c == 0xb8) {

            

            



            type = CODEC_TYPE_AUDIO;

            codec_id = CODEC_ID_AC3;

            n = audio_bound;

            c = 0x80;

            

        } else if (c == 0xb9) {

            

            type = CODEC_TYPE_VIDEO;

            codec_id = CODEC_ID_MPEG1VIDEO;

            n = video_bound;

            c = 0x1e0;

        } else {

            type = 0;

            codec_id = 0;

            n = 0;

        }

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

            st = av_mallocz(sizeof(AVStream));

            if (!st)

                return -ENOMEM;

            s->streams[s->nb_streams++] = st;

            st->id = c + i;

            st->codec.codec_type = type;

            st->codec.codec_id = codec_id;

        }

    }



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0,

                                AVFormatParameters *VAR_1)

{

    MpegDemuxContext *m;

    int VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9;

    int VAR_10, VAR_11, VAR_12, VAR_13;

    AVStream *st;



    m = av_mallocz(sizeof(MpegDemuxContext));

    if (!m)

        return -ENOMEM;

    VAR_0->priv_data = m;



    

    m->header_state = 0xff;

    VAR_2 = MAX_SYNC_SIZE;

    for(;;) {

        while (VAR_2 > 0) {

            VAR_3 = find_start_code(&VAR_0->pb, &VAR_2, &m->header_state);

            if (VAR_3 == PACK_START_CODE)

                goto found;

        }

        return -ENODATA;

    found:

        

        

        get_byte(&VAR_0->pb); 

        get_be16(&VAR_0->pb); 

        get_be16(&VAR_0->pb); 



        VAR_8 = get_byte(&VAR_0->pb) << 16; 

        VAR_8 |= get_byte(&VAR_0->pb) << 8;

        VAR_8 |= get_byte(&VAR_0->pb);

        VAR_8 &= (1 << 22) - 1;

        m->VAR_8 = VAR_8;



        VAR_3 = find_start_code(&VAR_0->pb, &VAR_2, &m->header_state);

        if (VAR_3 == SYSTEM_HEADER_START_CODE)

            break;

    }

    VAR_2 = get_be16(&VAR_0->pb);

    VAR_5 = get_byte(&VAR_0->pb) << 16;

    VAR_5 |= get_byte(&VAR_0->pb) << 8;

    VAR_5 |= get_byte(&VAR_0->pb);

    VAR_5 = (VAR_5 >> 1) & ((1 << 22) - 1);

    VAR_6 = get_byte(&VAR_0->pb) >> 2;

    VAR_7 = get_byte(&VAR_0->pb) & 0x1f;

    get_byte(&VAR_0->pb); 

#if 0

    printf("VAR_8=%d kbit/VAR_0\VAR_11", (m->VAR_8 * 50 * 8) / 1000);

    printf("VAR_5=%d\VAR_11", VAR_5);

    printf("VAR_6=%d\VAR_11", VAR_6);

    printf("VAR_7=%d\VAR_11", VAR_7);

#endif

    VAR_2 -= 6;

    VAR_0->nb_streams = 0;

    while (VAR_2 > 0) {

        VAR_4 = get_byte(&VAR_0->pb);

        VAR_2--;

        if ((VAR_4 & 0x80) == 0)

            break;

        VAR_9 = get_be16(&VAR_0->pb);

        VAR_2 -= 2;

        if (VAR_4 >= 0xc0 && VAR_4 <= 0xdf) {

            

            VAR_13 = CODEC_TYPE_AUDIO;

            VAR_10 = CODEC_ID_MP2;

            VAR_11 = 1;

            VAR_4 = VAR_4 | 0x100;

        } else if (VAR_4 >= 0xe0 && VAR_4 <= 0xef) {

            VAR_13 = CODEC_TYPE_VIDEO;

            VAR_10 = CODEC_ID_MPEG1VIDEO;

            VAR_11 = 1;

            VAR_4 = VAR_4 | 0x100;

        } else if (VAR_4 == 0xb8) {

            

            



            VAR_13 = CODEC_TYPE_AUDIO;

            VAR_10 = CODEC_ID_AC3;

            VAR_11 = VAR_6;

            VAR_4 = 0x80;

            

        } else if (VAR_4 == 0xb9) {

            

            VAR_13 = CODEC_TYPE_VIDEO;

            VAR_10 = CODEC_ID_MPEG1VIDEO;

            VAR_11 = VAR_7;

            VAR_4 = 0x1e0;

        } else {

            VAR_13 = 0;

            VAR_10 = 0;

            VAR_11 = 0;

        }

        for(VAR_12=0;VAR_12<VAR_11;VAR_12++) {

            st = av_mallocz(sizeof(AVStream));

            if (!st)

                return -ENOMEM;

            VAR_0->streams[VAR_0->nb_streams++] = st;

            st->id = VAR_4 + VAR_12;

            st->codec.codec_type = VAR_13;

            st->codec.VAR_10 = VAR_10;

        }

    }



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0,
AVFormatParameters *VAR_1)
{ MpegDemuxContext *m; int VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9; int VAR_10, VAR_11, VAR_12, VAR_13; AVStream *st; m = av_mallocz(sizeof(MpegDemuxContext)); if (!m)
return -ENOMEM; VAR_0->priv_data = m; m->header_state = 0xff; VAR_2 = MAX_SYNC_SIZE; for(;;) { while (VAR_2 > 0) { VAR_3 = find_start_code(&VAR_0->pb, &VAR_2, &m->header_state); if (VAR_3 == PACK_START_CODE)
goto found; } return -ENODATA; found:
get_byte(&VAR_0->pb); get_be16(&VAR_0->pb); get_be16(&VAR_0->pb); VAR_8 = get_byte(&VAR_0->pb) << 16; VAR_8 |= get_byte(&VAR_0->pb) << 8; VAR_8 |= get_byte(&VAR_0->pb); VAR_8 &= (1 << 22) - 1; m->VAR_8 = VAR_8; VAR_3 = find_start_code(&VAR_0->pb, &VAR_2, &m->header_state); if (VAR_3 == SYSTEM_HEADER_START_CODE)
break; } VAR_2 = get_be16(&VAR_0->pb); VAR_5 = get_byte(&VAR_0->pb) << 16; VAR_5 |= get_byte(&VAR_0->pb) << 8; VAR_5 |= get_byte(&VAR_0->pb); VAR_5 = (VAR_5 >> 1) & ((1 << 22) - 1); VAR_6 = get_byte(&VAR_0->pb) >> 2; VAR_7 = get_byte(&VAR_0->pb) & 0x1f; get_byte(&VAR_0->pb); #if 0
printf("VAR_8=%d kbit/VAR_0\VAR_11", (m->VAR_8 * 50 * 8) / 1000); printf("VAR_5=%d\VAR_11", VAR_5); printf("VAR_6=%d\VAR_11", VAR_6); printf("VAR_7=%d\VAR_11", VAR_7); #endif
VAR_2 -= 6; VAR_0->nb_streams = 0; while (VAR_2 > 0) { VAR_4 = get_byte(&VAR_0->pb); VAR_2--; if ((VAR_4 & 0x80) == 0)
break; VAR_9 = get_be16(&VAR_0->pb); VAR_2 -= 2; if (VAR_4 >= 0xc0 && VAR_4 <= 0xdf) { VAR_13 = CODEC_TYPE_AUDIO; VAR_10 = CODEC_ID_MP2; VAR_11 = 1; VAR_4 = VAR_4 | 0x100; } else if (VAR_4 >= 0xe0 && VAR_4 <= 0xef) { VAR_13 = CODEC_TYPE_VIDEO; VAR_10 = CODEC_ID_MPEG1VIDEO; VAR_11 = 1; VAR_4 = VAR_4 | 0x100; } else if (VAR_4 == 0xb8) { VAR_13 = CODEC_TYPE_AUDIO; VAR_10 = CODEC_ID_AC3; VAR_11 = VAR_6; VAR_4 = 0x80; } else if (VAR_4 == 0xb9) { VAR_13 = CODEC_TYPE_VIDEO; VAR_10 = CODEC_ID_MPEG1VIDEO; VAR_11 = VAR_7; VAR_4 = 0x1e0; } else { VAR_13 = 0; VAR_10 = 0; VAR_11 = 0; } for(VAR_12=0;VAR_12<VAR_11;VAR_12++) { st = av_mallocz(sizeof(AVStream)); if (!st)
return -ENOMEM; VAR_0->streams[VAR_0->nb_streams++] = st; st->id = VAR_4 + VAR_12; st->codec.codec_type = VAR_13; st->codec.VAR_10 = VAR_10; } } return 0; }