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"
29 /* write DVB SI sections */
31 /*********************************************/
32 /* mpegts section writer */
34 typedef struct MpegTSSection {
37 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
41 typedef struct MpegTSService {
42 MpegTSSection pmt; /* MPEG2 pmt table context */
43 int sid; /* service ID */
48 int pcr_packet_period;
51 typedef struct MpegTSWrite {
52 MpegTSSection pat; /* MPEG2 pat table */
53 MpegTSSection sdt; /* MPEG2 sdt table context */
54 MpegTSService **services;
56 int sdt_packet_period;
58 int pat_packet_period;
63 int mux_rate; ///< set to 1 when VBR
66 /* NOTE: 4 bytes must be left at the end for the crc32 */
67 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
69 MpegTSWrite *ts = ((AVFormatContext*)s->opaque)->priv_data;
71 unsigned char packet[TS_PACKET_SIZE];
72 const unsigned char *buf_ptr;
74 int first, b, len1, left;
76 crc = bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
77 buf[len - 4] = (crc >> 24) & 0xff;
78 buf[len - 3] = (crc >> 16) & 0xff;
79 buf[len - 2] = (crc >> 8) & 0xff;
80 buf[len - 1] = (crc) & 0xff;
82 /* send each packet */
85 first = (buf == buf_ptr);
93 s->cc = (s->cc + 1) & 0xf;
96 *q++ = 0; /* 0 offset */
97 len1 = TS_PACKET_SIZE - (q - packet);
100 memcpy(q, buf_ptr, len1);
102 /* add known padding data */
103 left = TS_PACKET_SIZE - (q - packet);
105 memset(q, 0xff, left);
107 s->write_packet(s, packet);
112 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
116 static inline void put16(uint8_t **q_ptr, int val)
125 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
126 int version, int sec_num, int last_sec_num,
127 uint8_t *buf, int len)
129 uint8_t section[1024], *q;
130 unsigned int tot_len;
132 tot_len = 3 + 5 + len + 4;
133 /* check if not too big */
139 put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
141 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
146 mpegts_write_section(s, section, tot_len);
150 /*********************************************/
153 #define DEFAULT_PMT_START_PID 0x1000
154 #define DEFAULT_START_PID 0x0100
155 #define DEFAULT_PROVIDER_NAME "FFmpeg"
156 #define DEFAULT_SERVICE_NAME "Service01"
158 /* default network id, transport stream and service identifiers */
159 #define DEFAULT_ONID 0x0001
160 #define DEFAULT_TSID 0x0001
161 #define DEFAULT_SID 0x0001
163 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
164 #define DEFAULT_PES_HEADER_FREQ 16
165 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
167 /* we retransmit the SI info at this rate */
168 #define SDT_RETRANS_TIME 500
169 #define PAT_RETRANS_TIME 100
170 #define PCR_RETRANS_TIME 20
172 typedef struct MpegTSWriteStream {
173 struct MpegTSService *service;
174 int pid; /* stream associated pid */
177 int first_pts_check; ///< first pts check needed
180 uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
184 static void mpegts_write_pat(AVFormatContext *s)
186 MpegTSWrite *ts = s->priv_data;
187 MpegTSService *service;
188 uint8_t data[1012], *q;
192 for(i = 0; i < ts->nb_services; i++) {
193 service = ts->services[i];
194 put16(&q, service->sid);
195 put16(&q, 0xe000 | service->pmt.pid);
197 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 stream_type = STREAM_TYPE_AUDIO_AC3;
248 stream_type = STREAM_TYPE_PRIVATE_DATA;
252 put16(&q, 0xe000 | ts_st->pid);
254 q += 2; /* patched after */
256 /* write optional descriptors here */
257 switch(st->codec->codec_type) {
258 case CODEC_TYPE_AUDIO:
259 if (lang && strlen(lang->value) == 3) {
260 *q++ = 0x0a; /* ISO 639 language descriptor */
262 *q++ = lang->value[0];
263 *q++ = lang->value[1];
264 *q++ = lang->value[2];
265 *q++ = 0; /* undefined type */
268 case CODEC_TYPE_SUBTITLE:
270 const char *language;
271 language = lang && strlen(lang->value)==3 ? lang->value : "eng";
277 *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
278 put16(&q, 1); /* page id */
279 put16(&q, 1); /* ancillary page id */
282 case CODEC_TYPE_VIDEO:
283 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
284 *q++ = 0x05; /*MPEG-2 registration descriptor*/
294 val = 0xf000 | (q - desc_length_ptr - 2);
295 desc_length_ptr[0] = val >> 8;
296 desc_length_ptr[1] = val;
298 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
302 /* NOTE: str == NULL is accepted for an empty string */
303 static void putstr8(uint8_t **q_ptr, const char *str)
319 static void mpegts_write_sdt(AVFormatContext *s)
321 MpegTSWrite *ts = s->priv_data;
322 MpegTSService *service;
323 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
324 int i, running_status, free_ca_mode, val;
329 for(i = 0; i < ts->nb_services; i++) {
330 service = ts->services[i];
331 put16(&q, service->sid);
332 *q++ = 0xfc | 0x00; /* currently no EIT info */
333 desc_list_len_ptr = q;
335 running_status = 4; /* running */
338 /* write only one descriptor for the service name and provider */
342 *q++ = 0x01; /* digital television service */
343 putstr8(&q, service->provider_name);
344 putstr8(&q, service->name);
345 desc_len_ptr[0] = q - desc_len_ptr - 1;
347 /* fill descriptor length */
348 val = (running_status << 13) | (free_ca_mode << 12) |
349 (q - desc_list_len_ptr - 2);
350 desc_list_len_ptr[0] = val >> 8;
351 desc_list_len_ptr[1] = val;
353 mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
357 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
359 const char *provider_name,
362 MpegTSService *service;
364 service = av_mallocz(sizeof(MpegTSService));
367 service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
369 service->provider_name = av_strdup(provider_name);
370 service->name = av_strdup(name);
371 service->pcr_pid = 0x1fff;
372 dynarray_add(&ts->services, &ts->nb_services, service);
376 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
378 AVFormatContext *ctx = s->opaque;
379 put_buffer(ctx->pb, packet, TS_PACKET_SIZE);
382 static int mpegts_write_header(AVFormatContext *s)
384 MpegTSWrite *ts = s->priv_data;
385 MpegTSWriteStream *ts_st;
386 MpegTSService *service;
387 AVStream *st, *pcr_st = NULL;
388 AVMetadataTag *title;
390 const char *service_name;
392 ts->tsid = DEFAULT_TSID;
393 ts->onid = DEFAULT_ONID;
394 /* allocate a single DVB service */
395 title = av_metadata_get(s->metadata, "title", NULL, 0);
396 service_name = title ? title->value : DEFAULT_SERVICE_NAME;
397 service = mpegts_add_service(ts, DEFAULT_SID,
398 DEFAULT_PROVIDER_NAME, service_name);
399 service->pmt.write_packet = section_write_packet;
400 service->pmt.opaque = s;
401 service->pmt.cc = 15;
403 ts->pat.pid = PAT_PID;
404 ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
405 ts->pat.write_packet = section_write_packet;
408 ts->sdt.pid = SDT_PID;
410 ts->sdt.write_packet = section_write_packet;
413 /* assign pids to each stream */
414 for(i = 0;i < s->nb_streams; i++) {
416 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
419 st->priv_data = ts_st;
420 ts_st->service = service;
421 ts_st->pid = DEFAULT_START_PID + i;
422 ts_st->payload_pts = AV_NOPTS_VALUE;
423 ts_st->payload_dts = AV_NOPTS_VALUE;
424 ts_st->first_pts_check = 1;
426 /* update PCR pid by using the first video stream */
427 if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
428 service->pcr_pid == 0x1fff) {
429 service->pcr_pid = ts_st->pid;
432 if (st->codec->codec_id == CODEC_ID_AAC &&
433 st->codec->extradata_size > 0) {
434 ts_st->adts = av_mallocz(sizeof(*ts_st->adts));
436 return AVERROR_NOMEM;
437 if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata,
438 st->codec->extradata_size) < 0)
443 /* if no video stream, use the first stream as PCR */
444 if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
445 pcr_st = s->streams[0];
446 ts_st = pcr_st->priv_data;
447 service->pcr_pid = ts_st->pid;
450 ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
452 if (ts->mux_rate > 1) {
453 service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
454 (TS_PACKET_SIZE * 8 * 1000);
455 ts->sdt_packet_period = (ts->mux_rate * SDT_RETRANS_TIME) /
456 (TS_PACKET_SIZE * 8 * 1000);
457 ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) /
458 (TS_PACKET_SIZE * 8 * 1000);
460 ts->cur_pcr = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
462 /* Arbitrary values, PAT/PMT could be written on key frames */
463 ts->sdt_packet_period = 200;
464 ts->pat_packet_period = 40;
465 if (pcr_st->codec->codec_type == CODEC_TYPE_AUDIO) {
466 if (!pcr_st->codec->frame_size) {
467 av_log(s, AV_LOG_WARNING, "frame size not set\n");
468 service->pcr_packet_period =
469 pcr_st->codec->sample_rate/(10*512);
471 service->pcr_packet_period =
472 pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
475 // max delta PCR 0.1s
476 service->pcr_packet_period =
477 pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
481 // output a PCR as soon as possible
482 service->pcr_packet_count = service->pcr_packet_period;
483 ts->pat_packet_count = ts->pat_packet_period-1;
484 ts->sdt_packet_count = ts->sdt_packet_period-1;
486 av_log(s, AV_LOG_INFO,
487 "muxrate %d bps, pcr every %d pkts, "
488 "sdt every %d, pat/pmt every %d pkts\n",
489 ts->mux_rate, service->pcr_packet_period,
490 ts->sdt_packet_period, ts->pat_packet_period);
493 put_flush_packet(s->pb);
498 for(i = 0;i < s->nb_streams; i++) {
500 av_free(st->priv_data);
505 /* send SDT, PAT and PMT tables regulary */
506 static void retransmit_si_info(AVFormatContext *s)
508 MpegTSWrite *ts = s->priv_data;
511 if (++ts->sdt_packet_count == ts->sdt_packet_period) {
512 ts->sdt_packet_count = 0;
515 if (++ts->pat_packet_count == ts->pat_packet_period) {
516 ts->pat_packet_count = 0;
518 for(i = 0; i < ts->nb_services; i++) {
519 mpegts_write_pmt(s, ts->services[i]);
524 /* Write a single null transport stream packet */
525 static void mpegts_insert_null_packet(AVFormatContext *s)
527 MpegTSWrite *ts = s->priv_data;
529 uint8_t buf[TS_PACKET_SIZE];
536 memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
537 put_buffer(s->pb, buf, TS_PACKET_SIZE);
538 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
541 /* Write a single transport stream packet with a PCR and no payload */
542 static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
544 MpegTSWrite *ts = s->priv_data;
545 MpegTSWriteStream *ts_st = st->priv_data;
547 uint64_t pcr = ts->cur_pcr;
548 uint8_t buf[TS_PACKET_SIZE];
552 *q++ = ts_st->pid >> 8;
554 *q++ = 0x20 | ts_st->cc; /* Adaptation only */
555 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
556 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
557 *q++ = 0x10; /* Adaptation flags: PCR present */
559 /* PCR coded into 6 bytes */
564 *q++ = (pcr & 1) << 7;
568 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
569 put_buffer(s->pb, buf, TS_PACKET_SIZE);
570 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
573 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
577 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
579 val = (((pts >> 15) & 0x7fff) << 1) | 1;
582 val = (((pts) & 0x7fff) << 1) | 1;
587 /* Add a pes header to the front of payload, and segment into an integer number of
588 * ts packets. The final ts packet is padded using an over-sized adaptation header
589 * to exactly fill the last ts packet.
590 * NOTE: 'payload' contains a complete PES payload.
592 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
593 const uint8_t *payload, int payload_size,
594 int64_t pts, int64_t dts)
596 MpegTSWriteStream *ts_st = st->priv_data;
597 MpegTSWrite *ts = s->priv_data;
598 uint8_t buf[TS_PACKET_SIZE];
600 int val, is_start, len, header_len, write_pcr, private_code, flags;
601 int afc_len, stuffing_len;
602 int64_t pcr = -1; /* avoid warning */
603 int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
606 while (payload_size > 0) {
607 retransmit_si_info(s);
610 if (ts_st->pid == ts_st->service->pcr_pid) {
611 if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
612 ts_st->service->pcr_packet_count++;
613 if (ts_st->service->pcr_packet_count >=
614 ts_st->service->pcr_packet_period) {
615 ts_st->service->pcr_packet_count = 0;
620 if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
621 (dts - (int64_t)ts->cur_pcr) > delay) {
622 /* pcr insert gets priority over null packet insert */
624 mpegts_insert_pcr_only(s, st);
626 mpegts_insert_null_packet(s);
627 continue; /* recalculate write_pcr and possibly retransmit si_info */
630 /* prepare packet header */
633 val = (ts_st->pid >> 8);
638 ts_st->cc = (ts_st->cc + 1) & 0xf;
639 *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
641 // add 11, pcr references the last byte of program clock reference base
642 if (ts->mux_rate > 1)
643 pcr = ts->cur_pcr + (4+7)*8*90000LL / ts->mux_rate;
646 if (dts != AV_NOPTS_VALUE && dts < pcr)
647 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
648 *q++ = 7; /* AFC length */
649 *q++ = 0x10; /* flags: PCR present */
654 *q++ = (pcr & 1) << 7;
658 int pes_extension = 0;
659 /* write PES header */
664 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
665 if (st->codec->codec_id == CODEC_ID_DIRAC) {
669 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
670 (st->codec->codec_id == CODEC_ID_MP2 ||
671 st->codec->codec_id == CODEC_ID_MP3)) {
675 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
681 if (pts != AV_NOPTS_VALUE) {
685 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
689 if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
690 st->codec->codec_id == CODEC_ID_DIRAC) {
691 /* set PES_extension_flag */
696 * One byte for PES2 extension flag +
697 * one byte for extension length +
698 * one byte for extension id
702 len = payload_size + header_len + 3;
703 if (private_code != 0)
710 /* data alignment indicator is required for subtitle data */
711 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
716 if (pts != AV_NOPTS_VALUE) {
717 write_pts(q, flags >> 6, pts);
720 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
721 write_pts(q, 1, dts);
724 if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
725 flags = 0x01; /* set PES_extension_flag_2 */
727 *q++ = 0x80 | 0x01; /* marker bit + extension length */
729 * Set the stream id extension flag bit to 0 and
730 * write the extended stream id
734 if (private_code != 0)
739 header_len = q - buf;
741 len = TS_PACKET_SIZE - header_len;
742 if (len > payload_size)
744 stuffing_len = TS_PACKET_SIZE - header_len - len;
745 if (stuffing_len > 0) {
746 /* add stuffing with AFC */
748 /* stuffing already present: increase its size */
749 afc_len = buf[4] + 1;
750 memmove(buf + 4 + afc_len + stuffing_len,
752 header_len - (4 + afc_len));
753 buf[4] += stuffing_len;
754 memset(buf + 4 + afc_len, 0xff, stuffing_len);
757 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
759 buf[4] = stuffing_len - 1;
760 if (stuffing_len >= 2) {
762 memset(buf + 6, 0xff, stuffing_len - 2);
766 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
769 put_buffer(s->pb, buf, TS_PACKET_SIZE);
770 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
772 put_flush_packet(s->pb);
775 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
777 AVStream *st = s->streams[pkt->stream_index];
778 int size = pkt->size;
779 uint8_t *buf= pkt->data;
781 MpegTSWriteStream *ts_st = st->priv_data;
782 const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
783 int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE;
785 if (pkt->pts != AV_NOPTS_VALUE)
786 pts = pkt->pts + delay;
787 if (pkt->dts != AV_NOPTS_VALUE)
788 dts = pkt->dts + delay;
790 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
791 av_log(s, AV_LOG_ERROR, "first pts value must set\n");
794 ts_st->first_pts_check = 0;
796 if (st->codec->codec_id == CODEC_ID_H264) {
797 const uint8_t *p = buf, *buf_end = p+size;
800 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
801 av_log(s, AV_LOG_ERROR, "h264 bitstream malformated, "
802 "no startcode found, use -vbsf h264_mp4toannexb\n");
807 p = ff_find_start_code(p, buf_end, &state);
808 //av_log(s, AV_LOG_INFO, "nal %d\n", state & 0x1f);
809 } while (p < buf_end && (state & 0x1f) != 9 &&
810 (state & 0x1f) != 5 && (state & 0x1f) != 1);
812 if ((state & 0x1f) != 9) { // AUD NAL
813 data = av_malloc(pkt->size+6);
816 memcpy(data+6, pkt->data, pkt->size);
817 AV_WB32(data, 0x00000001);
819 data[5] = 0xe0; // any slice type
823 } else if (st->codec->codec_id == CODEC_ID_AAC) {
826 if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
827 ADTSContext *adts = ts_st->adts;
830 av_log(s, AV_LOG_ERROR, "aac bitstream not in adts format "
831 "and extradata missing\n");
834 new_size = ADTS_HEADER_SIZE+adts->pce_size+pkt->size;
835 if ((unsigned)new_size >= INT_MAX)
837 data = av_malloc(new_size);
839 return AVERROR_NOMEM;
840 ff_adts_write_frame_header(adts, data, pkt->size, adts->pce_size);
841 if (adts->pce_size) {
842 memcpy(data+ADTS_HEADER_SIZE, adts->pce_data, adts->pce_size);
845 memcpy(data+ADTS_HEADER_SIZE+adts->pce_size, pkt->data, pkt->size);
851 if (st->codec->codec_type != CODEC_TYPE_AUDIO) {
852 // for video and subtitle, write a single pes packet
853 mpegts_write_pes(s, st, buf, size, pts, dts);
858 if (ts_st->payload_index + size > DEFAULT_PES_PAYLOAD_SIZE) {
859 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
860 ts_st->payload_pts, ts_st->payload_dts);
861 ts_st->payload_index = 0;
864 if (!ts_st->payload_index) {
865 ts_st->payload_pts = pts;
866 ts_st->payload_dts = dts;
869 memcpy(ts_st->payload + ts_st->payload_index, buf, size);
870 ts_st->payload_index += size;
875 static int mpegts_write_end(AVFormatContext *s)
877 MpegTSWrite *ts = s->priv_data;
878 MpegTSWriteStream *ts_st;
879 MpegTSService *service;
883 /* flush current packets */
884 for(i = 0; i < s->nb_streams; i++) {
886 ts_st = st->priv_data;
887 if (ts_st->payload_index > 0) {
888 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
889 ts_st->payload_pts, ts_st->payload_dts);
892 put_flush_packet(s->pb);
894 for(i = 0; i < ts->nb_services; i++) {
895 service = ts->services[i];
896 av_freep(&service->provider_name);
897 av_freep(&service->name);
900 av_free(ts->services);
905 AVOutputFormat mpegts_muxer = {
907 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),