2 * MPEG2 transport stream (aka DVB) muxer
3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/bswap.h"
23 #include "libavutil/crc.h"
24 #include "libavcodec/mpegvideo.h"
28 /* write DVB SI sections */
30 /*********************************************/
31 /* mpegts section writer */
33 typedef struct MpegTSSection {
36 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
40 typedef struct MpegTSService {
41 MpegTSSection pmt; /* MPEG2 pmt table context */
42 int sid; /* service ID */
47 int pcr_packet_period;
50 typedef struct MpegTSWrite {
51 MpegTSSection pat; /* MPEG2 pat table */
52 MpegTSSection sdt; /* MPEG2 sdt table context */
53 MpegTSService **services;
55 int sdt_packet_period;
57 int pat_packet_period;
65 /* NOTE: 4 bytes must be left at the end for the crc32 */
66 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
68 MpegTSWrite *ts = ((AVFormatContext*)s->opaque)->priv_data;
70 unsigned char packet[TS_PACKET_SIZE];
71 const unsigned char *buf_ptr;
73 int first, b, len1, left;
75 crc = bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
76 buf[len - 4] = (crc >> 24) & 0xff;
77 buf[len - 3] = (crc >> 16) & 0xff;
78 buf[len - 2] = (crc >> 8) & 0xff;
79 buf[len - 1] = (crc) & 0xff;
81 /* send each packet */
84 first = (buf == buf_ptr);
93 s->cc = (s->cc + 1) & 0xf;
95 *q++ = 0; /* 0 offset */
96 len1 = TS_PACKET_SIZE - (q - packet);
99 memcpy(q, buf_ptr, len1);
101 /* add known padding data */
102 left = TS_PACKET_SIZE - (q - packet);
104 memset(q, 0xff, left);
106 s->write_packet(s, packet);
111 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
115 static inline void put16(uint8_t **q_ptr, int val)
124 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
125 int version, int sec_num, int last_sec_num,
126 uint8_t *buf, int len)
128 uint8_t section[1024], *q;
129 unsigned int tot_len;
131 tot_len = 3 + 5 + len + 4;
132 /* check if not too big */
138 put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
140 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
145 mpegts_write_section(s, section, tot_len);
149 /*********************************************/
152 #define DEFAULT_PMT_START_PID 0x1000
153 #define DEFAULT_START_PID 0x0100
154 #define DEFAULT_PROVIDER_NAME "FFmpeg"
155 #define DEFAULT_SERVICE_NAME "Service01"
157 /* default network id, transport stream and service identifiers */
158 #define DEFAULT_ONID 0x0001
159 #define DEFAULT_TSID 0x0001
160 #define DEFAULT_SID 0x0001
162 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
163 #define DEFAULT_PES_HEADER_FREQ 16
164 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
166 /* we retransmit the SI info at this rate */
167 #define SDT_RETRANS_TIME 500
168 #define PAT_RETRANS_TIME 100
169 #define PCR_RETRANS_TIME 20
171 typedef struct MpegTSWriteStream {
172 struct MpegTSService *service;
173 int pid; /* stream associated pid */
176 int first_pts_check; ///< first pts check needed
179 uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
182 static void mpegts_write_pat(AVFormatContext *s)
184 MpegTSWrite *ts = s->priv_data;
185 MpegTSService *service;
186 uint8_t data[1012], *q;
190 for(i = 0; i < ts->nb_services; i++) {
191 service = ts->services[i];
192 put16(&q, service->sid);
193 put16(&q, 0xe000 | service->pmt.pid);
195 mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
201 static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
203 // MpegTSWrite *ts = s->priv_data;
204 uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
205 int val, stream_type, i;
208 put16(&q, 0xe000 | service->pcr_pid);
210 program_info_length_ptr = q;
211 q += 2; /* patched after */
213 /* put program info here */
215 val = 0xf000 | (q - program_info_length_ptr - 2);
216 program_info_length_ptr[0] = val >> 8;
217 program_info_length_ptr[1] = val;
219 for(i = 0; i < s->nb_streams; i++) {
220 AVStream *st = s->streams[i];
221 MpegTSWriteStream *ts_st = st->priv_data;
222 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
223 switch(st->codec->codec_id) {
224 case CODEC_ID_MPEG1VIDEO:
225 case CODEC_ID_MPEG2VIDEO:
226 stream_type = STREAM_TYPE_VIDEO_MPEG2;
229 stream_type = STREAM_TYPE_VIDEO_MPEG4;
232 stream_type = STREAM_TYPE_VIDEO_H264;
235 stream_type = STREAM_TYPE_VIDEO_DIRAC;
239 stream_type = STREAM_TYPE_AUDIO_MPEG1;
242 stream_type = STREAM_TYPE_AUDIO_AAC;
245 if (!strcmp(s->oformat->name, "dvb"))
246 stream_type = STREAM_TYPE_PRIVATE_DATA;
248 stream_type = STREAM_TYPE_AUDIO_AC3;
251 stream_type = STREAM_TYPE_PRIVATE_DATA;
255 put16(&q, 0xe000 | ts_st->pid);
257 q += 2; /* patched after */
259 /* write optional descriptors here */
260 switch(st->codec->codec_type) {
261 case CODEC_TYPE_AUDIO:
262 if (lang && strlen(lang->value) == 3) {
263 *q++ = 0x0a; /* ISO 639 language descriptor */
265 *q++ = lang->value[0];
266 *q++ = lang->value[1];
267 *q++ = lang->value[2];
268 *q++ = 0; /* undefined type */
271 case CODEC_TYPE_SUBTITLE:
273 const char *language;
274 language = lang && strlen(lang->value)==3 ? lang->value : "eng";
280 *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
281 put16(&q, 1); /* page id */
282 put16(&q, 1); /* ancillary page id */
285 case CODEC_TYPE_VIDEO:
286 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
287 *q++ = 0x05; /*MPEG-2 registration descriptor*/
297 val = 0xf000 | (q - desc_length_ptr - 2);
298 desc_length_ptr[0] = val >> 8;
299 desc_length_ptr[1] = val;
301 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
305 /* NOTE: str == NULL is accepted for an empty string */
306 static void putstr8(uint8_t **q_ptr, const char *str)
322 static void mpegts_write_sdt(AVFormatContext *s)
324 MpegTSWrite *ts = s->priv_data;
325 MpegTSService *service;
326 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
327 int i, running_status, free_ca_mode, val;
332 for(i = 0; i < ts->nb_services; i++) {
333 service = ts->services[i];
334 put16(&q, service->sid);
335 *q++ = 0xfc | 0x00; /* currently no EIT info */
336 desc_list_len_ptr = q;
338 running_status = 4; /* running */
341 /* write only one descriptor for the service name and provider */
345 *q++ = 0x01; /* digital television service */
346 putstr8(&q, service->provider_name);
347 putstr8(&q, service->name);
348 desc_len_ptr[0] = q - desc_len_ptr - 1;
350 /* fill descriptor length */
351 val = (running_status << 13) | (free_ca_mode << 12) |
352 (q - desc_list_len_ptr - 2);
353 desc_list_len_ptr[0] = val >> 8;
354 desc_list_len_ptr[1] = val;
356 mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
360 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
362 const char *provider_name,
365 MpegTSService *service;
367 service = av_mallocz(sizeof(MpegTSService));
370 service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
372 service->provider_name = av_strdup(provider_name);
373 service->name = av_strdup(name);
374 service->pcr_pid = 0x1fff;
375 dynarray_add(&ts->services, &ts->nb_services, service);
379 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
381 AVFormatContext *ctx = s->opaque;
382 put_buffer(ctx->pb, packet, TS_PACKET_SIZE);
385 static int mpegts_write_header(AVFormatContext *s)
387 MpegTSWrite *ts = s->priv_data;
388 MpegTSWriteStream *ts_st;
389 MpegTSService *service;
391 AVMetadataTag *title;
392 int i, total_bit_rate;
393 const char *service_name;
394 uint64_t sdt_size, pat_pmt_size, pos;
396 ts->tsid = DEFAULT_TSID;
397 ts->onid = DEFAULT_ONID;
398 /* allocate a single DVB service */
399 title = av_metadata_get(s->metadata, "title", NULL, 0);
400 service_name = title ? title->value : DEFAULT_SERVICE_NAME;
401 service = mpegts_add_service(ts, DEFAULT_SID,
402 DEFAULT_PROVIDER_NAME, service_name);
403 service->pmt.write_packet = section_write_packet;
404 service->pmt.opaque = s;
406 ts->pat.pid = PAT_PID;
408 ts->pat.write_packet = section_write_packet;
411 ts->sdt.pid = SDT_PID;
413 ts->sdt.write_packet = section_write_packet;
416 /* assign pids to each stream */
418 for(i = 0;i < s->nb_streams; i++) {
420 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
423 st->priv_data = ts_st;
424 ts_st->service = service;
425 ts_st->pid = DEFAULT_START_PID + i;
426 ts_st->payload_pts = AV_NOPTS_VALUE;
427 ts_st->payload_dts = AV_NOPTS_VALUE;
428 ts_st->first_pts_check = 1;
429 /* update PCR pid by using the first video stream */
430 if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
431 service->pcr_pid == 0x1fff)
432 service->pcr_pid = ts_st->pid;
433 if (st->codec->rc_max_rate)
434 total_bit_rate += st->codec->rc_max_rate;
436 if (!st->codec->bit_rate) {
437 av_log(s, AV_LOG_WARNING,
438 "stream %d, bit rate is not set, this will cause problems\n",
441 total_bit_rate += st->codec->bit_rate;
443 /* PES header size */
444 if (st->codec->codec_type == CODEC_TYPE_VIDEO ||
445 st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
447 * 19 bytes of PES header
448 * on average a half TS-packet (184/2) of padding-overhead every PES */
449 total_bit_rate += (19 + 184/2)*8 / av_q2d(st->codec->time_base);
451 /* 1 PES per DEFAULT_PES_PAYLOAD_SIZE bytes of audio data
452 * 14 bytes of PES header
453 * on average a half TS-packet (184/2) of padding-overhead every PES */
454 total_bit_rate += (14 + 184/2) *
455 st->codec->bit_rate / DEFAULT_PES_PAYLOAD_SIZE;
459 /* if no video stream, use the first stream as PCR */
460 if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
461 ts_st = s->streams[0]->priv_data;
462 service->pcr_pid = ts_st->pid;
465 ts->mux_rate = 1; // avoid div by 0
467 /* write info at the start of the file, so that it will be fast to
469 pos = url_ftell(s->pb);
471 sdt_size = url_ftell(s->pb) - pos;
472 pos = url_ftell(s->pb);
474 for(i = 0; i < ts->nb_services; i++) {
475 mpegts_write_pmt(s, ts->services[i]);
477 pat_pmt_size = url_ftell(s->pb) - pos;
479 if (total_bit_rate <= 8 * 1024)
480 total_bit_rate = 8 * 1024;
483 total_bit_rate * 4 / (TS_PACKET_SIZE-4) + /* TS header size */
484 1000 * 8 * sdt_size / PAT_RETRANS_TIME + /* SDT size */
485 1000 * 8 * pat_pmt_size / SDT_RETRANS_TIME + /* PAT+PMT size */
486 1000 * 8 * 8 / PCR_RETRANS_TIME; /* PCR size */
489 ts->mux_rate = s->mux_rate;
491 ts->mux_rate = total_bit_rate;
493 service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
494 (TS_PACKET_SIZE * 8 * 1000);
495 ts->sdt_packet_period = (ts->mux_rate * SDT_RETRANS_TIME) /
496 (TS_PACKET_SIZE * 8 * 1000);
497 ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) /
498 (TS_PACKET_SIZE * 8 * 1000);
500 // output a PCR as soon as possible
501 service->pcr_packet_count = service->pcr_packet_period;
503 av_log(s, AV_LOG_DEBUG,
504 "calculated bitrate %d bps, muxrate %d bps, "
505 "sdt every %d, pat/pmt every %d pkts\n",
506 total_bit_rate, ts->mux_rate, ts->sdt_packet_period,
507 ts->pat_packet_period);
510 ts->cur_pcr /= ts->mux_rate;
512 put_flush_packet(s->pb);
517 for(i = 0;i < s->nb_streams; i++) {
519 av_free(st->priv_data);
524 /* send SDT, PAT and PMT tables regulary */
525 static void retransmit_si_info(AVFormatContext *s)
527 MpegTSWrite *ts = s->priv_data;
530 if (++ts->sdt_packet_count == ts->sdt_packet_period) {
531 ts->sdt_packet_count = 0;
534 if (++ts->pat_packet_count == ts->pat_packet_period) {
535 ts->pat_packet_count = 0;
537 for(i = 0; i < ts->nb_services; i++) {
538 mpegts_write_pmt(s, ts->services[i]);
543 /* Write a single null transport stream packet */
544 static void mpegts_insert_null_packet(AVFormatContext *s)
546 MpegTSWrite *ts = s->priv_data;
548 uint8_t buf[TS_PACKET_SIZE];
555 memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
556 put_buffer(s->pb, buf, TS_PACKET_SIZE);
557 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
560 /* Write a single transport stream packet with a PCR and no payload */
561 static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
563 MpegTSWrite *ts = s->priv_data;
564 MpegTSWriteStream *ts_st = st->priv_data;
566 uint64_t pcr = ts->cur_pcr;
567 uint8_t buf[TS_PACKET_SIZE];
571 *q++ = ts_st->pid >> 8;
573 *q++ = 0x20 | ts_st->cc; /* Adaptation only */
574 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
575 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
576 *q++ = 0x10; /* Adaptation flags: PCR present */
578 /* PCR coded into 6 bytes */
583 *q++ = (pcr & 1) << 7;
587 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
588 put_buffer(s->pb, buf, TS_PACKET_SIZE);
589 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
592 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
596 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
598 val = (((pts >> 15) & 0x7fff) << 1) | 1;
601 val = (((pts) & 0x7fff) << 1) | 1;
606 /* Add a pes header to the front of payload, and segment into an integer number of
607 * ts packets. The final ts packet is padded using an over-sized adaptation header
608 * to exactly fill the last ts packet.
609 * NOTE: 'payload' contains a complete PES payload.
611 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
612 const uint8_t *payload, int payload_size,
613 int64_t pts, int64_t dts)
615 MpegTSWriteStream *ts_st = st->priv_data;
616 MpegTSWrite *ts = s->priv_data;
617 uint8_t buf[TS_PACKET_SIZE];
619 int val, is_start, len, header_len, write_pcr, private_code, flags;
620 int afc_len, stuffing_len;
621 int64_t pcr = -1; /* avoid warning */
622 int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
625 while (payload_size > 0) {
626 retransmit_si_info(s);
629 if (ts_st->pid == ts_st->service->pcr_pid) {
630 ts_st->service->pcr_packet_count++;
631 if (ts_st->service->pcr_packet_count >=
632 ts_st->service->pcr_packet_period) {
633 ts_st->service->pcr_packet_count = 0;
638 if (dts != AV_NOPTS_VALUE && (dts - (int64_t)ts->cur_pcr) > delay) {
639 /* pcr insert gets priority over null packet insert */
641 mpegts_insert_pcr_only(s, st);
643 mpegts_insert_null_packet(s);
644 continue; /* recalculate write_pcr and possibly retransmit si_info */
647 /* prepare packet header */
650 val = (ts_st->pid >> 8);
655 *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
656 ts_st->cc = (ts_st->cc + 1) & 0xf;
658 // add 11, pcr references the last byte of program clock reference base
659 pcr = ts->cur_pcr + (4+7)*8*90000LL / ts->mux_rate;
660 if (dts != AV_NOPTS_VALUE && dts < pcr)
661 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
662 *q++ = 7; /* AFC length */
663 *q++ = 0x10; /* flags: PCR present */
668 *q++ = (pcr & 1) << 7;
672 int pes_extension = 0;
673 /* write PES header */
678 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
679 if (st->codec->codec_id == CODEC_ID_DIRAC) {
683 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
684 (st->codec->codec_id == CODEC_ID_MP2 ||
685 st->codec->codec_id == CODEC_ID_MP3)) {
689 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
695 if (pts != AV_NOPTS_VALUE) {
699 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
703 if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
704 st->codec->codec_id == CODEC_ID_DIRAC) {
705 /* set PES_extension_flag */
710 * One byte for PES2 extension flag +
711 * one byte for extension length +
712 * one byte for extension id
716 len = payload_size + header_len + 3;
717 if (private_code != 0)
724 /* data alignment indicator is required for subtitle data */
725 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
730 if (pts != AV_NOPTS_VALUE) {
731 write_pts(q, flags >> 6, pts);
734 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
735 write_pts(q, 1, dts);
738 if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
739 flags = 0x01; /* set PES_extension_flag_2 */
741 *q++ = 0x80 | 0x01; /* marker bit + extension length */
743 * Set the stream id extension flag bit to 0 and
744 * write the extended stream id
748 if (private_code != 0)
753 header_len = q - buf;
755 len = TS_PACKET_SIZE - header_len;
756 if (len > payload_size)
758 stuffing_len = TS_PACKET_SIZE - header_len - len;
759 if (stuffing_len > 0) {
760 /* add stuffing with AFC */
762 /* stuffing already present: increase its size */
763 afc_len = buf[4] + 1;
764 memmove(buf + 4 + afc_len + stuffing_len,
766 header_len - (4 + afc_len));
767 buf[4] += stuffing_len;
768 memset(buf + 4 + afc_len, 0xff, stuffing_len);
771 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
773 buf[4] = stuffing_len - 1;
774 if (stuffing_len >= 2) {
776 memset(buf + 6, 0xff, stuffing_len - 2);
780 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
783 put_buffer(s->pb, buf, TS_PACKET_SIZE);
784 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
786 put_flush_packet(s->pb);
789 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
791 AVStream *st = s->streams[pkt->stream_index];
792 int size = pkt->size;
793 uint8_t *buf= pkt->data;
795 MpegTSWriteStream *ts_st = st->priv_data;
796 const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
797 int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE;
799 if (pkt->pts != AV_NOPTS_VALUE)
800 pts = pkt->pts + delay;
801 if (pkt->dts != AV_NOPTS_VALUE)
802 dts = pkt->dts + delay;
804 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
805 av_log(s, AV_LOG_ERROR, "first pts value must set\n");
808 ts_st->first_pts_check = 0;
810 if (st->codec->codec_id == CODEC_ID_H264) {
811 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
812 av_log(s, AV_LOG_ERROR, "h264 bitstream malformated\n");
815 if (pkt->data[4] != 0x09) { // AUD NAL
816 data = av_malloc(pkt->size+6);
819 memcpy(data+6, pkt->data, pkt->size);
820 AV_WB32(data, 0x00000001);
822 data[5] = 0xe0; // any slice type
828 if (st->codec->codec_type != CODEC_TYPE_AUDIO) {
829 // for video and subtitle, write a single pes packet
830 mpegts_write_pes(s, st, buf, size, pts, dts);
835 if (ts_st->payload_index + size > DEFAULT_PES_PAYLOAD_SIZE) {
836 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
837 ts_st->payload_pts, ts_st->payload_dts);
838 ts_st->payload_index = 0;
841 if (!ts_st->payload_index) {
842 ts_st->payload_pts = pts;
843 ts_st->payload_dts = dts;
846 memcpy(ts_st->payload + ts_st->payload_index, buf, size);
847 ts_st->payload_index += size;
852 static int mpegts_write_end(AVFormatContext *s)
854 MpegTSWrite *ts = s->priv_data;
855 MpegTSWriteStream *ts_st;
856 MpegTSService *service;
860 /* flush current packets */
861 for(i = 0; i < s->nb_streams; i++) {
863 ts_st = st->priv_data;
864 if (ts_st->payload_index > 0) {
865 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
866 ts_st->payload_pts, ts_st->payload_dts);
869 put_flush_packet(s->pb);
871 for(i = 0; i < ts->nb_services; i++) {
872 service = ts->services[i];
873 av_freep(&service->provider_name);
874 av_freep(&service->name);
877 av_free(ts->services);
882 AVOutputFormat mpegts_muxer = {
884 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
896 AVOutputFormat dvb_muxer = {
898 NULL_IF_CONFIG_SMALL("DVB style MPEG-2 transport stream format"),