2 * MPEG2 transport stream (aka DVB) muxer
3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 "libavutil/dict.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/opt.h"
27 #include "libavcodec/mpegvideo.h"
33 #define PCR_TIME_BASE 27000000
35 /* write DVB SI sections */
37 /*********************************************/
38 /* mpegts section writer */
40 typedef struct MpegTSSection {
43 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
47 typedef struct MpegTSService {
48 MpegTSSection pmt; /* MPEG2 pmt table context */
49 int sid; /* service ID */
54 int pcr_packet_period;
57 typedef struct MpegTSWrite {
58 const AVClass *av_class;
59 MpegTSSection pat; /* MPEG2 pat table */
60 MpegTSSection sdt; /* MPEG2 sdt table context */
61 MpegTSService **services;
63 int sdt_packet_period;
65 int pat_packet_period;
70 int mux_rate; ///< set to 1 when VBR
72 int transport_stream_id;
73 int original_network_id;
80 static const AVOption options[] = {
81 { "mpegts_transport_stream_id", "Set transport_stream_id field.",
82 offsetof(MpegTSWrite, transport_stream_id), FF_OPT_TYPE_INT, {.dbl = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
83 { "mpegts_original_network_id", "Set original_network_id field.",
84 offsetof(MpegTSWrite, original_network_id), FF_OPT_TYPE_INT, {.dbl = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
85 { "mpegts_service_id", "Set service_id field.",
86 offsetof(MpegTSWrite, service_id), FF_OPT_TYPE_INT, {.dbl = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
87 { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
88 offsetof(MpegTSWrite, pmt_start_pid), FF_OPT_TYPE_INT, {.dbl = 0x1000 }, 0x1000, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM},
89 { "mpegts_start_pid", "Set the first pid.",
90 offsetof(MpegTSWrite, start_pid), FF_OPT_TYPE_INT, {.dbl = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM},
94 static const AVClass mpegts_muxer_class = {
95 .class_name = "MPEGTS muxer",
96 .item_name = av_default_item_name,
98 .version = LIBAVUTIL_VERSION_INT,
101 /* NOTE: 4 bytes must be left at the end for the crc32 */
102 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
105 unsigned char packet[TS_PACKET_SIZE];
106 const unsigned char *buf_ptr;
108 int first, b, len1, left;
110 crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
111 buf[len - 4] = (crc >> 24) & 0xff;
112 buf[len - 3] = (crc >> 16) & 0xff;
113 buf[len - 2] = (crc >> 8) & 0xff;
114 buf[len - 1] = (crc) & 0xff;
116 /* send each packet */
119 first = (buf == buf_ptr);
127 s->cc = (s->cc + 1) & 0xf;
130 *q++ = 0; /* 0 offset */
131 len1 = TS_PACKET_SIZE - (q - packet);
134 memcpy(q, buf_ptr, len1);
136 /* add known padding data */
137 left = TS_PACKET_SIZE - (q - packet);
139 memset(q, 0xff, left);
141 s->write_packet(s, packet);
148 static inline void put16(uint8_t **q_ptr, int val)
157 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
158 int version, int sec_num, int last_sec_num,
159 uint8_t *buf, int len)
161 uint8_t section[1024], *q;
162 unsigned int tot_len;
163 /* reserved_future_use field must be set to 1 for SDT */
164 unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
166 tot_len = 3 + 5 + len + 4;
167 /* check if not too big */
173 put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
175 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
180 mpegts_write_section(s, section, tot_len);
184 /*********************************************/
187 #define DEFAULT_PROVIDER_NAME "Libav"
188 #define DEFAULT_SERVICE_NAME "Service01"
190 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
191 #define DEFAULT_PES_HEADER_FREQ 16
192 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
194 /* we retransmit the SI info at this rate */
195 #define SDT_RETRANS_TIME 500
196 #define PAT_RETRANS_TIME 100
197 #define PCR_RETRANS_TIME 20
199 typedef struct MpegTSWriteStream {
200 struct MpegTSService *service;
201 int pid; /* stream associated pid */
204 int first_pts_check; ///< first pts check needed
207 uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
211 static void mpegts_write_pat(AVFormatContext *s)
213 MpegTSWrite *ts = s->priv_data;
214 MpegTSService *service;
215 uint8_t data[1012], *q;
219 for(i = 0; i < ts->nb_services; i++) {
220 service = ts->services[i];
221 put16(&q, service->sid);
222 put16(&q, 0xe000 | service->pmt.pid);
224 mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
228 static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
230 // MpegTSWrite *ts = s->priv_data;
231 uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
232 int val, stream_type, i;
235 put16(&q, 0xe000 | service->pcr_pid);
237 program_info_length_ptr = q;
238 q += 2; /* patched after */
240 /* put program info here */
242 val = 0xf000 | (q - program_info_length_ptr - 2);
243 program_info_length_ptr[0] = val >> 8;
244 program_info_length_ptr[1] = val;
246 for(i = 0; i < s->nb_streams; i++) {
247 AVStream *st = s->streams[i];
248 MpegTSWriteStream *ts_st = st->priv_data;
249 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
250 switch(st->codec->codec_id) {
251 case CODEC_ID_MPEG1VIDEO:
252 case CODEC_ID_MPEG2VIDEO:
253 stream_type = STREAM_TYPE_VIDEO_MPEG2;
256 stream_type = STREAM_TYPE_VIDEO_MPEG4;
259 stream_type = STREAM_TYPE_VIDEO_H264;
262 stream_type = STREAM_TYPE_VIDEO_DIRAC;
266 stream_type = STREAM_TYPE_AUDIO_MPEG1;
269 stream_type = STREAM_TYPE_AUDIO_AAC;
271 case CODEC_ID_AAC_LATM:
272 stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
275 stream_type = STREAM_TYPE_AUDIO_AC3;
278 stream_type = STREAM_TYPE_PRIVATE_DATA;
282 put16(&q, 0xe000 | ts_st->pid);
284 q += 2; /* patched after */
286 /* write optional descriptors here */
287 switch(st->codec->codec_type) {
288 case AVMEDIA_TYPE_AUDIO:
291 char *next = lang->value;
294 *q++ = 0x0a; /* ISO 639 language descriptor */
298 for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
299 next = strchr(p, ',');
300 if (strlen(p) != 3 && (!next || next != p + 3))
301 continue; /* not a 3-letter code */
307 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
309 else if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
311 else if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
314 *q++ = 0; /* undefined type */
320 q -= 2; /* no language codes were written */
323 case AVMEDIA_TYPE_SUBTITLE:
325 const char *language;
326 language = lang && strlen(lang->value)==3 ? lang->value : "eng";
332 *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
333 if(st->codec->extradata_size == 4) {
334 memcpy(q, st->codec->extradata, 4);
337 put16(&q, 1); /* page id */
338 put16(&q, 1); /* ancillary page id */
342 case AVMEDIA_TYPE_VIDEO:
343 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
344 *q++ = 0x05; /*MPEG-2 registration descriptor*/
354 val = 0xf000 | (q - desc_length_ptr - 2);
355 desc_length_ptr[0] = val >> 8;
356 desc_length_ptr[1] = val;
358 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
362 /* NOTE: str == NULL is accepted for an empty string */
363 static void putstr8(uint8_t **q_ptr, const char *str)
379 static void mpegts_write_sdt(AVFormatContext *s)
381 MpegTSWrite *ts = s->priv_data;
382 MpegTSService *service;
383 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
384 int i, running_status, free_ca_mode, val;
389 for(i = 0; i < ts->nb_services; i++) {
390 service = ts->services[i];
391 put16(&q, service->sid);
392 *q++ = 0xfc | 0x00; /* currently no EIT info */
393 desc_list_len_ptr = q;
395 running_status = 4; /* running */
398 /* write only one descriptor for the service name and provider */
402 *q++ = 0x01; /* digital television service */
403 putstr8(&q, service->provider_name);
404 putstr8(&q, service->name);
405 desc_len_ptr[0] = q - desc_len_ptr - 1;
407 /* fill descriptor length */
408 val = (running_status << 13) | (free_ca_mode << 12) |
409 (q - desc_list_len_ptr - 2);
410 desc_list_len_ptr[0] = val >> 8;
411 desc_list_len_ptr[1] = val;
413 mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
417 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
419 const char *provider_name,
422 MpegTSService *service;
424 service = av_mallocz(sizeof(MpegTSService));
427 service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
429 service->provider_name = av_strdup(provider_name);
430 service->name = av_strdup(name);
431 service->pcr_pid = 0x1fff;
432 dynarray_add(&ts->services, &ts->nb_services, service);
436 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
438 AVFormatContext *ctx = s->opaque;
439 avio_write(ctx->pb, packet, TS_PACKET_SIZE);
442 static int mpegts_write_header(AVFormatContext *s)
444 MpegTSWrite *ts = s->priv_data;
445 MpegTSWriteStream *ts_st;
446 MpegTSService *service;
447 AVStream *st, *pcr_st = NULL;
448 AVDictionaryEntry *title, *provider;
450 const char *service_name;
451 const char *provider_name;
454 ts->tsid = ts->transport_stream_id;
455 ts->onid = ts->original_network_id;
456 /* allocate a single DVB service */
457 title = av_dict_get(s->metadata, "service_name", NULL, 0);
459 title = av_dict_get(s->metadata, "title", NULL, 0);
460 service_name = title ? title->value : DEFAULT_SERVICE_NAME;
461 provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
462 provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
463 service = mpegts_add_service(ts, ts->service_id, provider_name, service_name);
464 service->pmt.write_packet = section_write_packet;
465 service->pmt.opaque = s;
466 service->pmt.cc = 15;
468 ts->pat.pid = PAT_PID;
469 ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
470 ts->pat.write_packet = section_write_packet;
473 ts->sdt.pid = SDT_PID;
475 ts->sdt.write_packet = section_write_packet;
478 pids = av_malloc(s->nb_streams * sizeof(*pids));
480 return AVERROR(ENOMEM);
482 /* assign pids to each stream */
483 for(i = 0;i < s->nb_streams; i++) {
485 av_set_pts_info(st, 33, 1, 90000);
486 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
489 st->priv_data = ts_st;
490 ts_st->service = service;
491 /* MPEG pid values < 16 are reserved. Applications which set st->id in
492 * this range are assigned a calculated pid. */
494 ts_st->pid = ts->start_pid + i;
495 } else if (st->id < 0x1FFF) {
498 av_log(s, AV_LOG_ERROR, "Invalid stream id %d, must be less than 8191\n", st->id);
501 if (ts_st->pid == service->pmt.pid) {
502 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
505 for (j = 0; j < i; j++)
506 if (pids[j] == ts_st->pid) {
507 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
510 pids[i] = ts_st->pid;
511 ts_st->payload_pts = AV_NOPTS_VALUE;
512 ts_st->payload_dts = AV_NOPTS_VALUE;
513 ts_st->first_pts_check = 1;
515 /* update PCR pid by using the first video stream */
516 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
517 service->pcr_pid == 0x1fff) {
518 service->pcr_pid = ts_st->pid;
521 if (st->codec->codec_id == CODEC_ID_AAC &&
522 st->codec->extradata_size > 0) {
523 ts_st->adts = av_mallocz(sizeof(*ts_st->adts));
525 return AVERROR(ENOMEM);
526 if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata,
527 st->codec->extradata_size) < 0)
534 /* if no video stream, use the first stream as PCR */
535 if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
536 pcr_st = s->streams[0];
537 ts_st = pcr_st->priv_data;
538 service->pcr_pid = ts_st->pid;
541 ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
543 if (ts->mux_rate > 1) {
544 service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
545 (TS_PACKET_SIZE * 8 * 1000);
546 ts->sdt_packet_period = (ts->mux_rate * SDT_RETRANS_TIME) /
547 (TS_PACKET_SIZE * 8 * 1000);
548 ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) /
549 (TS_PACKET_SIZE * 8 * 1000);
551 ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
553 /* Arbitrary values, PAT/PMT could be written on key frames */
554 ts->sdt_packet_period = 200;
555 ts->pat_packet_period = 40;
556 if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
557 if (!pcr_st->codec->frame_size) {
558 av_log(s, AV_LOG_WARNING, "frame size not set\n");
559 service->pcr_packet_period =
560 pcr_st->codec->sample_rate/(10*512);
562 service->pcr_packet_period =
563 pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
566 // max delta PCR 0.1s
567 service->pcr_packet_period =
568 pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
572 // output a PCR as soon as possible
573 service->pcr_packet_count = service->pcr_packet_period;
574 ts->pat_packet_count = ts->pat_packet_period-1;
575 ts->sdt_packet_count = ts->sdt_packet_period-1;
577 if (ts->mux_rate == 1)
578 av_log(s, AV_LOG_INFO, "muxrate VBR, ");
580 av_log(s, AV_LOG_INFO, "muxrate %d, ", ts->mux_rate);
581 av_log(s, AV_LOG_INFO, "pcr every %d pkts, "
582 "sdt every %d, pat/pmt every %d pkts\n",
583 service->pcr_packet_period,
584 ts->sdt_packet_period, ts->pat_packet_period);
592 for(i = 0;i < s->nb_streams; i++) {
594 av_freep(&st->priv_data);
599 /* send SDT, PAT and PMT tables regulary */
600 static void retransmit_si_info(AVFormatContext *s)
602 MpegTSWrite *ts = s->priv_data;
605 if (++ts->sdt_packet_count == ts->sdt_packet_period) {
606 ts->sdt_packet_count = 0;
609 if (++ts->pat_packet_count == ts->pat_packet_period) {
610 ts->pat_packet_count = 0;
612 for(i = 0; i < ts->nb_services; i++) {
613 mpegts_write_pmt(s, ts->services[i]);
618 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
620 return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
624 static uint8_t* write_pcr_bits(uint8_t *buf, int64_t pcr)
626 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
628 *buf++ = pcr_high >> 25;
629 *buf++ = pcr_high >> 17;
630 *buf++ = pcr_high >> 9;
631 *buf++ = pcr_high >> 1;
632 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
638 /* Write a single null transport stream packet */
639 static void mpegts_insert_null_packet(AVFormatContext *s)
642 uint8_t buf[TS_PACKET_SIZE];
649 memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
650 avio_write(s->pb, buf, TS_PACKET_SIZE);
653 /* Write a single transport stream packet with a PCR and no payload */
654 static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
656 MpegTSWrite *ts = s->priv_data;
657 MpegTSWriteStream *ts_st = st->priv_data;
659 uint8_t buf[TS_PACKET_SIZE];
663 *q++ = ts_st->pid >> 8;
665 *q++ = 0x20 | ts_st->cc; /* Adaptation only */
666 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
667 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
668 *q++ = 0x10; /* Adaptation flags: PCR present */
670 /* PCR coded into 6 bytes */
671 q = write_pcr_bits(q, get_pcr(ts, s->pb));
674 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
675 avio_write(s->pb, buf, TS_PACKET_SIZE);
678 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
682 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
684 val = (((pts >> 15) & 0x7fff) << 1) | 1;
687 val = (((pts) & 0x7fff) << 1) | 1;
692 /* Add a pes header to the front of payload, and segment into an integer number of
693 * ts packets. The final ts packet is padded using an over-sized adaptation header
694 * to exactly fill the last ts packet.
695 * NOTE: 'payload' contains a complete PES payload.
697 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
698 const uint8_t *payload, int payload_size,
699 int64_t pts, int64_t dts)
701 MpegTSWriteStream *ts_st = st->priv_data;
702 MpegTSWrite *ts = s->priv_data;
703 uint8_t buf[TS_PACKET_SIZE];
705 int val, is_start, len, header_len, write_pcr, private_code, flags;
706 int afc_len, stuffing_len;
707 int64_t pcr = -1; /* avoid warning */
708 int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
711 while (payload_size > 0) {
712 retransmit_si_info(s);
715 if (ts_st->pid == ts_st->service->pcr_pid) {
716 if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
717 ts_st->service->pcr_packet_count++;
718 if (ts_st->service->pcr_packet_count >=
719 ts_st->service->pcr_packet_period) {
720 ts_st->service->pcr_packet_count = 0;
725 if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
726 (dts - get_pcr(ts, s->pb)/300) > delay) {
727 /* pcr insert gets priority over null packet insert */
729 mpegts_insert_pcr_only(s, st);
731 mpegts_insert_null_packet(s);
732 continue; /* recalculate write_pcr and possibly retransmit si_info */
735 /* prepare packet header */
738 val = (ts_st->pid >> 8);
743 ts_st->cc = (ts_st->cc + 1) & 0xf;
744 *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
746 // add 11, pcr references the last byte of program clock reference base
747 if (ts->mux_rate > 1)
748 pcr = get_pcr(ts, s->pb);
750 pcr = (dts - delay)*300;
751 if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
752 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
753 *q++ = 7; /* AFC length */
754 *q++ = 0x10; /* flags: PCR present */
755 q = write_pcr_bits(q, pcr);
758 int pes_extension = 0;
759 /* write PES header */
764 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
765 if (st->codec->codec_id == CODEC_ID_DIRAC) {
769 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
770 (st->codec->codec_id == CODEC_ID_MP2 ||
771 st->codec->codec_id == CODEC_ID_MP3 ||
772 st->codec->codec_id == CODEC_ID_AAC)) {
776 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
782 if (pts != AV_NOPTS_VALUE) {
786 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
790 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
791 st->codec->codec_id == CODEC_ID_DIRAC) {
792 /* set PES_extension_flag */
797 * One byte for PES2 extension flag +
798 * one byte for extension length +
799 * one byte for extension id
803 len = payload_size + header_len + 3;
804 if (private_code != 0)
811 /* data alignment indicator is required for subtitle data */
812 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
817 if (pts != AV_NOPTS_VALUE) {
818 write_pts(q, flags >> 6, pts);
821 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
822 write_pts(q, 1, dts);
825 if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
826 flags = 0x01; /* set PES_extension_flag_2 */
828 *q++ = 0x80 | 0x01; /* marker bit + extension length */
830 * Set the stream id extension flag bit to 0 and
831 * write the extended stream id
835 if (private_code != 0)
840 header_len = q - buf;
842 len = TS_PACKET_SIZE - header_len;
843 if (len > payload_size)
845 stuffing_len = TS_PACKET_SIZE - header_len - len;
846 if (stuffing_len > 0) {
847 /* add stuffing with AFC */
849 /* stuffing already present: increase its size */
850 afc_len = buf[4] + 1;
851 memmove(buf + 4 + afc_len + stuffing_len,
853 header_len - (4 + afc_len));
854 buf[4] += stuffing_len;
855 memset(buf + 4 + afc_len, 0xff, stuffing_len);
858 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
860 buf[4] = stuffing_len - 1;
861 if (stuffing_len >= 2) {
863 memset(buf + 6, 0xff, stuffing_len - 2);
867 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
870 avio_write(s->pb, buf, TS_PACKET_SIZE);
875 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
877 AVStream *st = s->streams[pkt->stream_index];
878 int size = pkt->size;
879 uint8_t *buf= pkt->data;
881 MpegTSWriteStream *ts_st = st->priv_data;
882 const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
883 int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE;
885 if (pkt->pts != AV_NOPTS_VALUE)
886 pts = pkt->pts + delay;
887 if (pkt->dts != AV_NOPTS_VALUE)
888 dts = pkt->dts + delay;
890 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
891 av_log(s, AV_LOG_ERROR, "first pts value must set\n");
894 ts_st->first_pts_check = 0;
896 if (st->codec->codec_id == CODEC_ID_H264) {
897 const uint8_t *p = buf, *buf_end = p+size;
900 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
901 av_log(s, AV_LOG_ERROR, "h264 bitstream malformated, "
902 "no startcode found, use -vbsf h264_mp4toannexb\n");
907 p = ff_find_start_code(p, buf_end, &state);
908 //av_log(s, AV_LOG_INFO, "nal %d\n", state & 0x1f);
909 } while (p < buf_end && (state & 0x1f) != 9 &&
910 (state & 0x1f) != 5 && (state & 0x1f) != 1);
912 if ((state & 0x1f) != 9) { // AUD NAL
913 data = av_malloc(pkt->size+6);
916 memcpy(data+6, pkt->data, pkt->size);
917 AV_WB32(data, 0x00000001);
919 data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
923 } else if (st->codec->codec_id == CODEC_ID_AAC) {
926 if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
927 ADTSContext *adts = ts_st->adts;
930 av_log(s, AV_LOG_ERROR, "aac bitstream not in adts format "
931 "and extradata missing\n");
934 new_size = ADTS_HEADER_SIZE+adts->pce_size+pkt->size;
935 if ((unsigned)new_size >= INT_MAX)
937 data = av_malloc(new_size);
939 return AVERROR(ENOMEM);
940 ff_adts_write_frame_header(adts, data, pkt->size, adts->pce_size);
941 if (adts->pce_size) {
942 memcpy(data+ADTS_HEADER_SIZE, adts->pce_data, adts->pce_size);
945 memcpy(data+ADTS_HEADER_SIZE+adts->pce_size, pkt->data, pkt->size);
951 if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
952 // for video and subtitle, write a single pes packet
953 mpegts_write_pes(s, st, buf, size, pts, dts);
958 if (ts_st->payload_index + size > DEFAULT_PES_PAYLOAD_SIZE) {
959 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
960 ts_st->payload_pts, ts_st->payload_dts);
961 ts_st->payload_index = 0;
964 if (!ts_st->payload_index) {
965 ts_st->payload_pts = pts;
966 ts_st->payload_dts = dts;
969 memcpy(ts_st->payload + ts_st->payload_index, buf, size);
970 ts_st->payload_index += size;
977 static int mpegts_write_end(AVFormatContext *s)
979 MpegTSWrite *ts = s->priv_data;
980 MpegTSWriteStream *ts_st;
981 MpegTSService *service;
985 /* flush current packets */
986 for(i = 0; i < s->nb_streams; i++) {
988 ts_st = st->priv_data;
989 if (ts_st->payload_index > 0) {
990 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
991 ts_st->payload_pts, ts_st->payload_dts);
993 av_freep(&ts_st->adts);
997 for(i = 0; i < ts->nb_services; i++) {
998 service = ts->services[i];
999 av_freep(&service->provider_name);
1000 av_freep(&service->name);
1003 av_free(ts->services);
1008 AVOutputFormat ff_mpegts_muxer = {
1010 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
1013 sizeof(MpegTSWrite),
1015 CODEC_ID_MPEG2VIDEO,
1016 mpegts_write_header,
1017 mpegts_write_packet,
1019 .priv_class = &mpegts_muxer_class,