static void mpegts_write_pes(AVFormatContext *s, AVStream *st,

                             const uint8_t *payload, int payload_size,

                             int64_t pts, int64_t dts, int key, int stream_id)

{

    MpegTSWriteStream *ts_st = st->priv_data;

    MpegTSWrite *ts = s->priv_data;

    uint8_t buf[TS_PACKET_SIZE];

    uint8_t *q;

    int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;

    int afc_len, stuffing_len;

    int64_t pcr = -1; /* avoid warning */

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

    int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;



    av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);

    if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

        force_pat = 1;




    is_start = 1;

    while (payload_size > 0) {

        retransmit_si_info(s, force_pat, dts);

        force_pat = 0;



        write_pcr = 0;

        if (ts_st->pid == ts_st->service->pcr_pid) {

            if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames

                ts_st->service->pcr_packet_count++;

            if (ts_st->service->pcr_packet_count >=

                ts_st->service->pcr_packet_period) {

                ts_st->service->pcr_packet_count = 0;

                write_pcr = 1;





        if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&

            (dts - get_pcr(ts, s->pb) / 300) > delay) {

            /* pcr insert gets priority over null packet insert */

            if (write_pcr)

                mpegts_insert_pcr_only(s, st);

            else

                mpegts_insert_null_packet(s);

            /* recalculate write_pcr and possibly retransmit si_info */

            continue;




        /* prepare packet header */

        q    = buf;

        *q++ = 0x47;

        val  = ts_st->pid >> 8;

        if (is_start)

            val |= 0x40;

        *q++      = val;

        *q++      = ts_st->pid;

        ts_st->cc = ts_st->cc + 1 & 0xf;

        *q++      = 0x10 | ts_st->cc; // payload indicator + CC






        if (key && is_start && pts != AV_NOPTS_VALUE) {

            // set Random Access for key frames

            if (ts_st->pid == ts_st->service->pcr_pid)

                write_pcr = 1;

            set_af_flag(buf, 0x40);



        if (write_pcr) {

            set_af_flag(buf, 0x10);


            // add 11, pcr references the last byte of program clock reference base

            if (ts->mux_rate > 1)

                pcr = get_pcr(ts, s->pb);

            else

                pcr = (dts - delay) * 300;

            if (dts != AV_NOPTS_VALUE && dts < pcr / 300)

                av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");

            extend_af(buf, write_pcr_bits(q, pcr));



        if (is_start) {

            int pes_extension = 0;

            int pes_header_stuffing_bytes = 0;

            /* write PES header */

            *q++ = 0x00;

            *q++ = 0x00;

            *q++ = 0x01;

            is_dvb_subtitle = 0;

            is_dvb_teletext = 0;

            if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

                if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)

                    *q++ = 0xfd;

                else

                    *q++ = 0xe0;

            } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                       (st->codecpar->codec_id == AV_CODEC_ID_MP2 ||

                        st->codecpar->codec_id == AV_CODEC_ID_MP3 ||

                        st->codecpar->codec_id == AV_CODEC_ID_AAC)) {

                *q++ = 0xc0;

            } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                        st->codecpar->codec_id == AV_CODEC_ID_AC3 &&

                        ts->m2ts_mode) {

                *q++ = 0xfd;

            } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&

                       st->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {

                *q++ = 0xbd;

            } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {

                *q++ = stream_id != -1 ? stream_id : 0xfc;



                if (stream_id == 0xbd) /* asynchronous KLV */

                    pts = dts = AV_NOPTS_VALUE;

            } else {

                *q++ = 0xbd;

                if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {

                    if (st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {

                        is_dvb_subtitle = 1;

                    } else if (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {

                        is_dvb_teletext = 1;




            header_len = 0;

            flags      = 0;

            if (pts != AV_NOPTS_VALUE) {

                header_len += 5;

                flags      |= 0x80;


            if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {

                header_len += 5;

                flags      |= 0x40;


            if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {

                /* set PES_extension_flag */

                pes_extension = 1;

                flags        |= 0x01;



                /* One byte for PES2 extension flag +

                 * one byte for extension length +

                 * one byte for extension id */

                header_len += 3;


            /* for Blu-ray AC3 Audio the PES Extension flag should be as follow

             * otherwise it will not play sound on blu-ray

             */

            if (ts->m2ts_mode &&

                st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                st->codecpar->codec_id == AV_CODEC_ID_AC3) {

                        /* set PES_extension_flag */

                        pes_extension = 1;

                        flags |= 0x01;

                        header_len += 3;


            if (is_dvb_teletext) {

                pes_header_stuffing_bytes = 0x24 - header_len;

                header_len = 0x24;


            len = payload_size + header_len + 3;

            /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */

            if (is_dvb_subtitle) {

                len += 3;

                payload_size++;


            if (len > 0xffff)

                len = 0;

            if (ts->omit_video_pes_length && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

                len = 0;


            *q++ = len >> 8;

            *q++ = len;

            val  = 0x80;

            /* data alignment indicator is required for subtitle and data streams */

            if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA)

                val |= 0x04;

            *q++ = val;

            *q++ = flags;

            *q++ = header_len;

            if (pts != AV_NOPTS_VALUE) {

                write_pts(q, flags >> 6, pts);

                q += 5;


            if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {

                write_pts(q, 1, dts);

                q += 5;


            if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {

                flags = 0x01;  /* set PES_extension_flag_2 */

                *q++  = flags;

                *q++  = 0x80 | 0x01; /* marker bit + extension length */

                /* Set the stream ID extension flag bit to 0 and

                 * write the extended stream ID. */

                *q++ = 0x00 | 0x60;


            /* For Blu-ray AC3 Audio Setting extended flags */

          if (ts->m2ts_mode &&

              pes_extension &&

              st->codecpar->codec_id == AV_CODEC_ID_AC3) {

                      flags = 0x01; /* set PES_extension_flag_2 */

                      *q++ = flags;

                      *q++ = 0x80 | 0x01; /* marker bit + extension length */

                      *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */






            if (is_dvb_subtitle) {

                /* First two fields of DVB subtitles PES data:

                 * data_identifier: for DVB subtitle streams shall be coded with the value 0x20

                 * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */

                *q++ = 0x20;

                *q++ = 0x00;


            if (is_dvb_teletext) {

                memset(q, 0xff, pes_header_stuffing_bytes);

                q += pes_header_stuffing_bytes;


            is_start = 0;


        /* header size */

        header_len = q - buf;

        /* data len */

        len = TS_PACKET_SIZE - header_len;

        if (len > payload_size)

            len = payload_size;

        stuffing_len = TS_PACKET_SIZE - header_len - len;

        if (stuffing_len > 0) {

            /* add stuffing with AFC */

            if (buf[3] & 0x20) {

                /* stuffing already present: increase its size */

                afc_len = buf[4] + 1;

                memmove(buf + 4 + afc_len + stuffing_len,

                        buf + 4 + afc_len,

                        header_len - (4 + afc_len));

                buf[4] += stuffing_len;

                memset(buf + 4 + afc_len, 0xff, stuffing_len);

            } else {

                /* add stuffing */

                memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);

                buf[3] |= 0x20;

                buf[4]  = stuffing_len - 1;

                if (stuffing_len >= 2) {

                    buf[5] = 0x00;

                    memset(buf + 6, 0xff, stuffing_len - 2);






        if (is_dvb_subtitle && payload_size == len) {

            memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);

            buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */

        } else {

            memcpy(buf + TS_PACKET_SIZE - len, payload, len);




        payload      += len;

        payload_size -= len;

        mpegts_prefix_m2ts_header(s);

        avio_write(s->pb, buf, TS_PACKET_SIZE);


    ts_st->prev_payload_key = key;

static void mpegts_write_pes(AVFormatContext *s, AVStream *st,

                             const uint8_t *payload, int payload_size,

                             int64_t pts, int64_t dts, int key, int stream_id)

{

    MpegTSWriteStream *ts_st = st->priv_data;

    MpegTSWrite *ts = s->priv_data;

    uint8_t buf[TS_PACKET_SIZE];

    uint8_t *q;

    int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;

    int afc_len, stuffing_len;

    int64_t pcr = -1; 

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

    int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;



    av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);

    if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

        force_pat = 1;




    is_start = 1;

    while (payload_size > 0) {

        retransmit_si_info(s, force_pat, dts);

        force_pat = 0;



        write_pcr = 0;

        if (ts_st->pid == ts_st->service->pcr_pid) {

            if (ts->mux_rate > 1 || is_start) 

                ts_st->service->pcr_packet_count++;

            if (ts_st->service->pcr_packet_count >=

                ts_st->service->pcr_packet_period) {

                ts_st->service->pcr_packet_count = 0;

                write_pcr = 1;





        if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&

            (dts - get_pcr(ts, s->pb) / 300) > delay) {

            

            if (write_pcr)

                mpegts_insert_pcr_only(s, st);

            else

                mpegts_insert_null_packet(s);

            

            continue;




        

        q    = buf;

        *q++ = 0x47;

        val  = ts_st->pid >> 8;

        if (is_start)

            val |= 0x40;

        *q++      = val;

        *q++      = ts_st->pid;

        ts_st->cc = ts_st->cc + 1 & 0xf;

        *q++      = 0x10 | ts_st->cc; 






        if (key && is_start && pts != AV_NOPTS_VALUE) {

            

            if (ts_st->pid == ts_st->service->pcr_pid)

                write_pcr = 1;

            set_af_flag(buf, 0x40);



        if (write_pcr) {

            set_af_flag(buf, 0x10);


            

            if (ts->mux_rate > 1)

                pcr = get_pcr(ts, s->pb);

            else

                pcr = (dts - delay) * 300;

            if (dts != AV_NOPTS_VALUE && dts < pcr / 300)

                av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");

            extend_af(buf, write_pcr_bits(q, pcr));



        if (is_start) {

            int pes_extension = 0;

            int pes_header_stuffing_bytes = 0;

            

            *q++ = 0x00;

            *q++ = 0x00;

            *q++ = 0x01;

            is_dvb_subtitle = 0;

            is_dvb_teletext = 0;

            if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

                if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)

                    *q++ = 0xfd;

                else

                    *q++ = 0xe0;

            } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                       (st->codecpar->codec_id == AV_CODEC_ID_MP2 ||

                        st->codecpar->codec_id == AV_CODEC_ID_MP3 ||

                        st->codecpar->codec_id == AV_CODEC_ID_AAC)) {

                *q++ = 0xc0;

            } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                        st->codecpar->codec_id == AV_CODEC_ID_AC3 &&

                        ts->m2ts_mode) {

                *q++ = 0xfd;

            } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&

                       st->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {

                *q++ = 0xbd;

            } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {

                *q++ = stream_id != -1 ? stream_id : 0xfc;



                if (stream_id == 0xbd) 

                    pts = dts = AV_NOPTS_VALUE;

            } else {

                *q++ = 0xbd;

                if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {

                    if (st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {

                        is_dvb_subtitle = 1;

                    } else if (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {

                        is_dvb_teletext = 1;




            header_len = 0;

            flags      = 0;

            if (pts != AV_NOPTS_VALUE) {

                header_len += 5;

                flags      |= 0x80;


            if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {

                header_len += 5;

                flags      |= 0x40;


            if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {

                

                pes_extension = 1;

                flags        |= 0x01;



                





                header_len += 3;


            





            if (ts->m2ts_mode &&

                st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                st->codecpar->codec_id == AV_CODEC_ID_AC3) {

                        

                        pes_extension = 1;

                        flags |= 0x01;

                        header_len += 3;


            if (is_dvb_teletext) {

                pes_header_stuffing_bytes = 0x24 - header_len;

                header_len = 0x24;


            len = payload_size + header_len + 3;

            

            if (is_dvb_subtitle) {

                len += 3;

                payload_size++;


            if (len > 0xffff)

                len = 0;

            if (ts->omit_video_pes_length && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

                len = 0;


            *q++ = len >> 8;

            *q++ = len;

            val  = 0x80;

            

            if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA)

                val |= 0x04;

            *q++ = val;

            *q++ = flags;

            *q++ = header_len;

            if (pts != AV_NOPTS_VALUE) {

                write_pts(q, flags >> 6, pts);

                q += 5;


            if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {

                write_pts(q, 1, dts);

                q += 5;


            if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {

                flags = 0x01;  

                *q++  = flags;

                *q++  = 0x80 | 0x01; 

                



                *q++ = 0x00 | 0x60;


            

          if (ts->m2ts_mode &&

              pes_extension &&

              st->codecpar->codec_id == AV_CODEC_ID_AC3) {

                      flags = 0x01; 

                      *q++ = flags;

                      *q++ = 0x80 | 0x01; 

                      *q++ = 0x00 | 0x71; 






            if (is_dvb_subtitle) {

                





                *q++ = 0x20;

                *q++ = 0x00;


            if (is_dvb_teletext) {

                memset(q, 0xff, pes_header_stuffing_bytes);

                q += pes_header_stuffing_bytes;


            is_start = 0;


        

        header_len = q - buf;

        

        len = TS_PACKET_SIZE - header_len;

        if (len > payload_size)

            len = payload_size;

        stuffing_len = TS_PACKET_SIZE - header_len - len;

        if (stuffing_len > 0) {

            

            if (buf[3] & 0x20) {

                

                afc_len = buf[4] + 1;

                memmove(buf + 4 + afc_len + stuffing_len,

                        buf + 4 + afc_len,

                        header_len - (4 + afc_len));

                buf[4] += stuffing_len;

                memset(buf + 4 + afc_len, 0xff, stuffing_len);

            } else {

                

                memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);

                buf[3] |= 0x20;

                buf[4]  = stuffing_len - 1;

                if (stuffing_len >= 2) {

                    buf[5] = 0x00;

                    memset(buf + 6, 0xff, stuffing_len - 2);






        if (is_dvb_subtitle && payload_size == len) {

            memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);

            buf[TS_PACKET_SIZE - 1] = 0xff; 

        } else {

            memcpy(buf + TS_PACKET_SIZE - len, payload, len);




        payload      += len;

        payload_size -= len;

        mpegts_prefix_m2ts_header(s);

        avio_write(s->pb, buf, TS_PACKET_SIZE);


    ts_st->prev_payload_key = key;

static void FUNC_0(AVFormatContext *VAR_0, AVStream *VAR_1,

                             const uint8_t *VAR_2, int VAR_3,

                             int64_t VAR_4, int64_t VAR_5, int VAR_6, int VAR_7)

{

    MpegTSWriteStream *ts_st = VAR_1->priv_data;

    MpegTSWrite *ts = VAR_0->priv_data;

    uint8_t buf[TS_PACKET_SIZE];

    uint8_t *q;

    int VAR_8, VAR_9, VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15;

    int VAR_16, VAR_17;

    int64_t pcr = -1; 

    int64_t delay = av_rescale(VAR_0->max_delay, 90000, AV_TIME_BASE);

    int VAR_18 = VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && VAR_6 && !ts_st->prev_payload_key;



    av_assert0(ts_st->VAR_2 != buf || VAR_1->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);

    if (ts->VAR_15 & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

        VAR_18 = 1;




    VAR_9 = 1;

    while (VAR_3 > 0) {

        retransmit_si_info(VAR_0, VAR_18, VAR_5);

        VAR_18 = 0;



        VAR_12 = 0;

        if (ts_st->pid == ts_st->service->pcr_pid) {

            if (ts->mux_rate > 1 || VAR_9) 

                ts_st->service->pcr_packet_count++;

            if (ts_st->service->pcr_packet_count >=

                ts_st->service->pcr_packet_period) {

                ts_st->service->pcr_packet_count = 0;

                VAR_12 = 1;





        if (ts->mux_rate > 1 && VAR_5 != AV_NOPTS_VALUE &&

            (VAR_5 - get_pcr(ts, VAR_0->pb) / 300) > delay) {

            

            if (VAR_12)

                mpegts_insert_pcr_only(VAR_0, VAR_1);

            else

                mpegts_insert_null_packet(VAR_0);

            

            continue;




        

        q    = buf;

        *q++ = 0x47;

        VAR_8  = ts_st->pid >> 8;

        if (VAR_9)

            VAR_8 |= 0x40;

        *q++      = VAR_8;

        *q++      = ts_st->pid;

        ts_st->cc = ts_st->cc + 1 & 0xf;

        *q++      = 0x10 | ts_st->cc; 






        if (VAR_6 && VAR_9 && VAR_4 != AV_NOPTS_VALUE) {

            

            if (ts_st->pid == ts_st->service->pcr_pid)

                VAR_12 = 1;

            set_af_flag(buf, 0x40);



        if (VAR_12) {

            set_af_flag(buf, 0x10);


            

            if (ts->mux_rate > 1)

                pcr = get_pcr(ts, VAR_0->pb);

            else

                pcr = (VAR_5 - delay) * 300;

            if (VAR_5 != AV_NOPTS_VALUE && VAR_5 < pcr / 300)

                av_log(VAR_0, AV_LOG_WARNING, "VAR_5 < pcr, TS is invalid\n");

            extend_af(buf, write_pcr_bits(q, pcr));



        if (VAR_9) {

            int VAR_19 = 0;

            int VAR_20 = 0;

            

            *q++ = 0x00;

            *q++ = 0x00;

            *q++ = 0x01;

            VAR_13 = 0;

            VAR_14 = 0;

            if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

                if (VAR_1->codecpar->codec_id == AV_CODEC_ID_DIRAC)

                    *q++ = 0xfd;

                else

                    *q++ = 0xe0;

            } else if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                       (VAR_1->codecpar->codec_id == AV_CODEC_ID_MP2 ||

                        VAR_1->codecpar->codec_id == AV_CODEC_ID_MP3 ||

                        VAR_1->codecpar->codec_id == AV_CODEC_ID_AAC)) {

                *q++ = 0xc0;

            } else if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                        VAR_1->codecpar->codec_id == AV_CODEC_ID_AC3 &&

                        ts->m2ts_mode) {

                *q++ = 0xfd;

            } else if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_DATA &&

                       VAR_1->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {

                *q++ = 0xbd;

            } else if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_DATA) {

                *q++ = VAR_7 != -1 ? VAR_7 : 0xfc;



                if (VAR_7 == 0xbd) 

                    VAR_4 = VAR_5 = AV_NOPTS_VALUE;

            } else {

                *q++ = 0xbd;

                if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {

                    if (VAR_1->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {

                        VAR_13 = 1;

                    } else if (VAR_1->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {

                        VAR_14 = 1;




            VAR_11 = 0;

            VAR_15      = 0;

            if (VAR_4 != AV_NOPTS_VALUE) {

                VAR_11 += 5;

                VAR_15      |= 0x80;


            if (VAR_5 != AV_NOPTS_VALUE && VAR_4 != AV_NOPTS_VALUE && VAR_5 != VAR_4) {

                VAR_11 += 5;

                VAR_15      |= 0x40;


            if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                VAR_1->codecpar->codec_id == AV_CODEC_ID_DIRAC) {

                

                VAR_19 = 1;

                VAR_15        |= 0x01;



                





                VAR_11 += 3;


            





            if (ts->m2ts_mode &&

                VAR_1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&

                VAR_1->codecpar->codec_id == AV_CODEC_ID_AC3) {

                        

                        VAR_19 = 1;

                        VAR_15 |= 0x01;

                        VAR_11 += 3;


            if (VAR_14) {

                VAR_20 = 0x24 - VAR_11;

                VAR_11 = 0x24;


            VAR_10 = VAR_3 + VAR_11 + 3;

            

            if (VAR_13) {

                VAR_10 += 3;

                VAR_3++;


            if (VAR_10 > 0xffff)

                VAR_10 = 0;

            if (ts->omit_video_pes_length && VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {

                VAR_10 = 0;


            *q++ = VAR_10 >> 8;

            *q++ = VAR_10;

            VAR_8  = 0x80;

            

            if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || VAR_1->codecpar->codec_type == AVMEDIA_TYPE_DATA)

                VAR_8 |= 0x04;

            *q++ = VAR_8;

            *q++ = VAR_15;

            *q++ = VAR_11;

            if (VAR_4 != AV_NOPTS_VALUE) {

                write_pts(q, VAR_15 >> 6, VAR_4);

                q += 5;


            if (VAR_5 != AV_NOPTS_VALUE && VAR_4 != AV_NOPTS_VALUE && VAR_5 != VAR_4) {

                write_pts(q, 1, VAR_5);

                q += 5;


            if (VAR_19 && VAR_1->codecpar->codec_id == AV_CODEC_ID_DIRAC) {

                VAR_15 = 0x01;  

                *q++  = VAR_15;

                *q++  = 0x80 | 0x01; 

                



                *q++ = 0x00 | 0x60;


            

          if (ts->m2ts_mode &&

              VAR_19 &&

              VAR_1->codecpar->codec_id == AV_CODEC_ID_AC3) {

                      VAR_15 = 0x01; 

                      *q++ = VAR_15;

                      *q++ = 0x80 | 0x01; 

                      *q++ = 0x00 | 0x71; 






            if (VAR_13) {

                





                *q++ = 0x20;

                *q++ = 0x00;


            if (VAR_14) {

                memset(q, 0xff, VAR_20);

                q += VAR_20;


            VAR_9 = 0;


        

        VAR_11 = q - buf;

        

        VAR_10 = TS_PACKET_SIZE - VAR_11;

        if (VAR_10 > VAR_3)

            VAR_10 = VAR_3;

        VAR_17 = TS_PACKET_SIZE - VAR_11 - VAR_10;

        if (VAR_17 > 0) {

            

            if (buf[3] & 0x20) {

                

                VAR_16 = buf[4] + 1;

                memmove(buf + 4 + VAR_16 + VAR_17,

                        buf + 4 + VAR_16,

                        VAR_11 - (4 + VAR_16));

                buf[4] += VAR_17;

                memset(buf + 4 + VAR_16, 0xff, VAR_17);

            } else {

                

                memmove(buf + 4 + VAR_17, buf + 4, VAR_11 - 4);

                buf[3] |= 0x20;

                buf[4]  = VAR_17 - 1;

                if (VAR_17 >= 2) {

                    buf[5] = 0x00;

                    memset(buf + 6, 0xff, VAR_17 - 2);






        if (VAR_13 && VAR_3 == VAR_10) {

            memcpy(buf + TS_PACKET_SIZE - VAR_10, VAR_2, VAR_10 - 1);

            buf[TS_PACKET_SIZE - 1] = 0xff; 

        } else {

            memcpy(buf + TS_PACKET_SIZE - VAR_10, VAR_2, VAR_10);




        VAR_2      += VAR_10;

        VAR_3 -= VAR_10;

        mpegts_prefix_m2ts_header(VAR_0);

        avio_write(VAR_0->pb, buf, TS_PACKET_SIZE);


    ts_st->prev_payload_key = VAR_6;

static void FUNC_0(AVFormatContext *VAR_0, AVStream *VAR_1,
const uint8_t *VAR_2, int VAR_3,
int64_t VAR_4, int64_t VAR_5, int VAR_6, int VAR_7)
{ MpegTSWriteStream *ts_st = VAR_1->priv_data; MpegTSWrite *ts = VAR_0->priv_data; uint8_t buf[TS_PACKET_SIZE]; uint8_t *q; int VAR_8, VAR_9, VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15; int VAR_16, VAR_17; int64_t pcr = -1; int64_t delay = av_rescale(VAR_0->max_delay, 90000, AV_TIME_BASE); int VAR_18 = VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && VAR_6 && !ts_st->prev_payload_key; av_assert0(ts_st->VAR_2 != buf || VAR_1->codecpar->codec_type != AVMEDIA_TYPE_VIDEO); if (ts->VAR_15 & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { VAR_18 = 1; VAR_9 = 1; while (VAR_3 > 0) { retransmit_si_info(VAR_0, VAR_18, VAR_5); VAR_18 = 0; VAR_12 = 0; if (ts_st->pid == ts_st->service->pcr_pid) { if (ts->mux_rate > 1 || VAR_9)
ts_st->service->pcr_packet_count++; if (ts_st->service->pcr_packet_count >=
ts_st->service->pcr_packet_period) { ts_st->service->pcr_packet_count = 0; VAR_12 = 1; if (ts->mux_rate > 1 && VAR_5 != AV_NOPTS_VALUE &&
(VAR_5 - get_pcr(ts, VAR_0->pb) / 300) > delay) { if (VAR_12)
mpegts_insert_pcr_only(VAR_0, VAR_1); else
mpegts_insert_null_packet(VAR_0); continue; q    = buf; *q++ = 0x47; VAR_8  = ts_st->pid >> 8; if (VAR_9)
VAR_8 |= 0x40; *q++      = VAR_8; *q++      = ts_st->pid; ts_st->cc = ts_st->cc + 1 & 0xf; *q++      = 0x10 | ts_st->cc; if (VAR_6 && VAR_9 && VAR_4 != AV_NOPTS_VALUE) { if (ts_st->pid == ts_st->service->pcr_pid)
VAR_12 = 1; set_af_flag(buf, 0x40); if (VAR_12) { set_af_flag(buf, 0x10); if (ts->mux_rate > 1)
pcr = get_pcr(ts, VAR_0->pb); else
pcr = (VAR_5 - delay) * 300; if (VAR_5 != AV_NOPTS_VALUE && VAR_5 < pcr / 300)
av_log(VAR_0, AV_LOG_WARNING, "VAR_5 < pcr, TS is invalid\n"); extend_af(buf, write_pcr_bits(q, pcr)); if (VAR_9) { int VAR_19 = 0; int VAR_20 = 0; *q++ = 0x00; *q++ = 0x00; *q++ = 0x01; VAR_13 = 0; VAR_14 = 0; if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { if (VAR_1->codecpar->codec_id == AV_CODEC_ID_DIRAC)
*q++ = 0xfd; else
*q++ = 0xe0; } else if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && (VAR_1->codecpar->codec_id == AV_CODEC_ID_MP2 ||
VAR_1->codecpar->codec_id == AV_CODEC_ID_MP3 ||
VAR_1->codecpar->codec_id == AV_CODEC_ID_AAC)) { *q++ = 0xc0; } else if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && VAR_1->codecpar->codec_id == AV_CODEC_ID_AC3 &&
ts->m2ts_mode) { *q++ = 0xfd; } else if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_DATA && VAR_1->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) { *q++ = 0xbd; } else if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_DATA) { *q++ = VAR_7 != -1 ? VAR_7 : 0xfc; if (VAR_7 == 0xbd)
VAR_4 = VAR_5 = AV_NOPTS_VALUE; } else { *q++ = 0xbd; if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { if (VAR_1->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE) { VAR_13 = 1; } else if (VAR_1->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) { VAR_14 = 1; VAR_11 = 0; VAR_15      = 0; if (VAR_4 != AV_NOPTS_VALUE) { VAR_11 += 5; VAR_15      |= 0x80; if (VAR_5 != AV_NOPTS_VALUE && VAR_4 != AV_NOPTS_VALUE && VAR_5 != VAR_4) { VAR_11 += 5; VAR_15      |= 0x40; if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
VAR_1->codecpar->codec_id == AV_CODEC_ID_DIRAC) { VAR_19 = 1; VAR_15        |= 0x01; VAR_11 += 3; if (ts->m2ts_mode &&
VAR_1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
VAR_1->codecpar->codec_id == AV_CODEC_ID_AC3) { VAR_19 = 1; VAR_15 |= 0x01; VAR_11 += 3; if (VAR_14) { VAR_20 = 0x24 - VAR_11; VAR_11 = 0x24; VAR_10 = VAR_3 + VAR_11 + 3; if (VAR_13) { VAR_10 += 3; VAR_3++; if (VAR_10 > 0xffff)
VAR_10 = 0; if (ts->omit_video_pes_length && VAR_1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { VAR_10 = 0; *q++ = VAR_10 >> 8; *q++ = VAR_10; VAR_8  = 0x80; if (VAR_1->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || VAR_1->codecpar->codec_type == AVMEDIA_TYPE_DATA)
VAR_8 |= 0x04; *q++ = VAR_8; *q++ = VAR_15; *q++ = VAR_11; if (VAR_4 != AV_NOPTS_VALUE) { write_pts(q, VAR_15 >> 6, VAR_4); q += 5; if (VAR_5 != AV_NOPTS_VALUE && VAR_4 != AV_NOPTS_VALUE && VAR_5 != VAR_4) { write_pts(q, 1, VAR_5); q += 5; if (VAR_19 && VAR_1->codecpar->codec_id == AV_CODEC_ID_DIRAC) { VAR_15 = 0x01; *q++  = VAR_15; *q++  = 0x80 | 0x01; *q++ = 0x00 | 0x60; if (ts->m2ts_mode &&
VAR_19 &&
VAR_1->codecpar->codec_id == AV_CODEC_ID_AC3) { VAR_15 = 0x01; *q++ = VAR_15; *q++ = 0x80 | 0x01; *q++ = 0x00 | 0x71; if (VAR_13) { *q++ = 0x20; *q++ = 0x00; if (VAR_14) { memset(q, 0xff, VAR_20); q += VAR_20; VAR_9 = 0; VAR_11 = q - buf; VAR_10 = TS_PACKET_SIZE - VAR_11; if (VAR_10 > VAR_3)
VAR_10 = VAR_3; VAR_17 = TS_PACKET_SIZE - VAR_11 - VAR_10; if (VAR_17 > 0) { if (buf[3] & 0x20) { VAR_16 = buf[4] + 1; memmove(buf + 4 + VAR_16 + VAR_17,
buf + 4 + VAR_16,
VAR_11 - (4 + VAR_16)); buf[4] += VAR_17; memset(buf + 4 + VAR_16, 0xff, VAR_17); } else { memmove(buf + 4 + VAR_17, buf + 4, VAR_11 - 4); buf[3] |= 0x20; buf[4]  = VAR_17 - 1; if (VAR_17 >= 2) { buf[5] = 0x00; memset(buf + 6, 0xff, VAR_17 - 2); if (VAR_13 && VAR_3 == VAR_10) { memcpy(buf + TS_PACKET_SIZE - VAR_10, VAR_2, VAR_10 - 1); buf[TS_PACKET_SIZE - 1] = 0xff; } else { memcpy(buf + TS_PACKET_SIZE - VAR_10, VAR_2, VAR_10); VAR_2      += VAR_10; VAR_3 -= VAR_10; mpegts_prefix_m2ts_header(VAR_0); avio_write(VAR_0->pb, buf, TS_PACKET_SIZE); ts_st->prev_payload_key = VAR_6;